public async Task ConnectAsync(CancellationToken cancellationToken) { var socket = await _socket.ConnectAsync(cancellationToken); if (ReferenceEquals(_tcpSocket, socket)) { return; } Interlocked.Exchange(ref _stream, null)?.Dispose(); try { _stream = new NetworkStream(socket, true); var sslStream = new SslStream( _stream, false, _sslConfiguration.RemoteCertificateValidationCallback, _sslConfiguration.LocalCertificateSelectionCallback, _sslConfiguration.EncryptionPolicy ); _stream = sslStream; _log.Verbose(() => LogEvent.Create($"Attempting SSL connection to {_endpoint.Host}, SslProtocol:{_sslConfiguration.EnabledProtocols}, Policy:{_sslConfiguration.EncryptionPolicy}")); await sslStream.AuthenticateAsClientAsync(_endpoint.Host, _sslConfiguration.LocalCertificates, _sslConfiguration.EnabledProtocols, _sslConfiguration.CheckCertificateRevocation).ThrowIfCancellationRequested(cancellationToken).ConfigureAwait(false); _stream = sslStream; _log.Info(() => LogEvent.Create($"Successful SSL connection, SslProtocol:{sslStream.SslProtocol}, KeyExchange:{sslStream.KeyExchangeAlgorithm}.{sslStream.KeyExchangeStrength}, Cipher:{sslStream.CipherAlgorithm}.{sslStream.CipherStrength}, Hash:{sslStream.HashAlgorithm}.{sslStream.HashStrength}, Authenticated:{sslStream.IsAuthenticated}, MutuallyAuthenticated:{sslStream.IsMutuallyAuthenticated}, Encrypted:{sslStream.IsEncrypted}, Signed:{sslStream.IsSigned}")); _tcpSocket = socket; } catch (Exception ex) { _log.Warn(() => LogEvent.Create(ex, "SSL connection failed")); Interlocked.Exchange(ref _stream, null)?.Dispose(); } }
public async Task ConnectAsync(CancellationToken cancellationToken) { _tcpSocket = await _socket.ConnectAsync(cancellationToken); }