Ejemplo n.º 1
0
        public override void NotifyAlertReceived(byte alertLevel, byte alertDescription)
        {
            string description = AlertDescription.GetText(alertDescription);

            AlertLevelsEnum level     = AlertLevelsEnum.Warning;
            AlertTypesEnum  alertType = AlertTypesEnum.unknown;

            if (Enum.IsDefined(typeof(AlertLevelsEnum), alertLevel))
            {
                level = (AlertLevelsEnum)alertLevel;
            }

            if (Enum.IsDefined(typeof(AlertTypesEnum), alertDescription))
            {
                alertType = (AlertTypesEnum)alertDescription;
            }

            string alertMsg = $"{AlertLevel.GetText(alertLevel)}";

            alertMsg += (!string.IsNullOrEmpty(description)) ? $", {description}." : ".";

            if (alertType == AlertTypesEnum.close_notify)
            {
                logger.LogDebug($"DTLS server received close notification: {alertMsg}");
            }
            else
            {
                logger.LogWarning($"DTLS server received unexpected alert: {alertMsg}");
            }

            OnAlert?.Invoke(level, alertType, description);
        }
Ejemplo n.º 2
0
        public override void NotifyAlertReceived(byte alertLevel, byte alertDescription)
        {
            string description = AlertDescription.GetText(alertDescription);

            AlertLevelsEnum level     = AlertLevelsEnum.Warning;
            AlertTypesEnum  alertType = AlertTypesEnum.unknown;

            if (Enum.IsDefined(typeof(AlertLevelsEnum), alertLevel))
            {
                level = (AlertLevelsEnum)alertLevel;
            }

            if (Enum.IsDefined(typeof(AlertTypesEnum), alertDescription))
            {
                alertType = (AlertTypesEnum)alertDescription;
            }

            if (alertType == AlertTypesEnum.close_notify)
            {
                logger.LogDebug(
                    $"DTLS client received close notification: {AlertLevel.GetText(alertLevel)}, {description}.");
            }
            else
            {
                logger.LogWarning(
                    $"DTLS client received unexpected alert: {AlertLevel.GetText(alertLevel)}, {description}.");
            }

            OnAlert?.Invoke(level, alertType, description);
        }
Ejemplo n.º 3
0
        public override void NotifyAlertReceived(byte alertLevel, byte alertDescription)
        {
            TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;

            output.WriteLine("TLS-PSK client received alert: " + AlertLevel.GetText(alertLevel)
                             + ", " + AlertDescription.GetText(alertDescription));
        }
Ejemplo n.º 4
0
        public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
        {
            string description = null;

            if (message != null)
            {
                description += message;
            }
            if (cause != null)
            {
                description += cause;
            }

            string alertMessage = $"{AlertLevel.GetText(alertLevel)}, {AlertDescription.GetText(alertDescription)}";

            alertMessage += !string.IsNullOrEmpty(description) ? $", {description}." : ".";

            if (alertDescription == AlertTypesEnum.close_notify.GetHashCode())
            {
                logger.LogDebug($"DTLS client raised close notification: {alertMessage}");
            }
            else
            {
                logger.LogWarning($"DTLS client raised unexpected alert: {alertMessage}");
            }
        }
Ejemplo n.º 5
0
        public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
        {
            TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;

            output.WriteLine("TLS-PSK client raised alert: " + AlertLevel.GetText(alertLevel)
                             + ", " + AlertDescription.GetText(alertDescription));
            if (message != null)
            {
                output.WriteLine("> " + message);
            }
            if (cause != null)
            {
                output.WriteLine(cause);
            }
        }
Ejemplo n.º 6
0
        public override void NotifyAlertReceived(byte alertLevel, byte alertDescription)
        {
            if (alertLevel == AlertLevel.fatal && firstFatalAlertConnectionEnd == -1)
            {
                firstFatalAlertConnectionEnd = ConnectionEnd.server;
                firstFatalAlertDescription   = alertDescription;
            }

            if (TlsTestConfig.DEBUG)
            {
                TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;
                output.WriteLine("TLS client received alert: " + AlertLevel.GetText(alertLevel)
                                 + ", " + AlertDescription.GetText(alertDescription));
            }
        }
Ejemplo n.º 7
0
        public async Task ConnectAsync(CoapTransportLayerConnectOptions connectOptions, CancellationToken cancellationToken)
        {
            if (connectOptions == null)
            {
                throw new ArgumentNullException(nameof(connectOptions));
            }

            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                _udpTransport = new UdpTransport(connectOptions);
                var clientProtocol = new DtlsClientProtocol(_secureRandom);
                _dtlsClient = new DtlsClient(ConvertProtocolVersion(DtlsVersion), (PreSharedKey)Credentials);

                using (cancellationToken.Register(() =>
                {
                    _udpTransport.Close();
                }))
                {
                    _dtlsTransport = await Task.Run(() => clientProtocol.Connect(_dtlsClient, _udpTransport), cancellationToken).ConfigureAwait(false);
                }
            }
            catch
            {
                _udpTransport?.Dispose();

                if (cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException();
                }

                if (_dtlsClient.ReceivedAlert != 0)
                {
                    throw new DtlsException($"Received alert {AlertDescription.GetText(_dtlsClient.ReceivedAlert)}.", null)
                          {
                              ReceivedAlert = _dtlsClient.ReceivedAlert
                          };
                }

                throw;
            }
        }
Ejemplo n.º 8
0
        public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
        {
            if (alertLevel == AlertLevel.fatal && firstFatalAlertConnectionEnd == -1)
            {
                firstFatalAlertConnectionEnd = ConnectionEnd.client;
                firstFatalAlertDescription   = alertDescription;
            }

            if (TlsTestConfig.DEBUG)
            {
                TextWriter output = (alertLevel == AlertLevel.fatal) ? Console.Error : Console.Out;
                output.WriteLine("TLS client raised alert: " + AlertLevel.GetText(alertLevel)
                                 + ", " + AlertDescription.GetText(alertDescription));
                if (message != null)
                {
                    output.WriteLine("> " + message);
                }
                if (cause != null)
                {
                    output.WriteLine(cause);
                }
            }
        }
Ejemplo n.º 9
0
        public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
        {
            string description = null;

            if (message != null)
            {
                description += message;
            }

            if (cause != null)
            {
                description += cause;
            }

            if (alertDescription == AlertTypesEnum.close_notify.GetHashCode())
            {
                logger.LogDebug(
                    $"DTLS client raised close notify: {AlertLevel.GetText(alertLevel)}, {AlertDescription.GetText(alertDescription)}, {description}.");
            }
            else
            {
                logger.LogWarning(
                    $"DTLS client raised unexpected alert: {AlertLevel.GetText(alertLevel)}, {AlertDescription.GetText(alertDescription)}, {description}.");
            }
        }
Ejemplo n.º 10
0
 public override void NotifyAlertReceived(byte alertLevel, byte alertDescription)
 {
     logger.LogWarning($"DTLS client received alert: {AlertLevel.GetText(alertLevel)}, {AlertDescription.GetText(alertDescription)}.");
 }
Ejemplo n.º 11
0
        public override void NotifyAlertRaised(byte alertLevel, byte alertDescription, string message, Exception cause)
        {
            string description = null;

            if (message != null)
            {
                description += message;
            }
            if (cause != null)
            {
                description += cause;
            }

            logger.LogWarning($"DTLS client raised alert: {AlertLevel.GetText(alertLevel)}, {AlertDescription.GetText(alertDescription)}, {description}.");
        }