public static async Task <MllpHost> Create(
            TcpClient tcpClient,
            IMessageLog messageLog,
            IHl7MessageMiddleware middleware,
            PipeParser?parser = null,
            Encoding?encoding = null,
            X509Certificate?serverCertificate = null,
            RemoteCertificateValidationCallback?
            userCertificateValidationCallback = null)
        {
            var host = new MllpHost(
                tcpClient,
                messageLog,
                parser ?? new PipeParser(),
                encoding ?? Encoding.ASCII,
                middleware);
            Stream stream = tcpClient.GetStream();

            if (serverCertificate != null)
            {
                var ssl = new SslStream(
                    stream,
                    false,
                    userCertificateValidationCallback);
                await ssl.AuthenticateAsServerAsync(
                    serverCertificate,
                    true,
                    SslProtocols.Tls11 | SslProtocols.Tls12,
                    false)
                .ConfigureAwait(false);

                host._stream = ssl;
            }
            else
            {
                host._stream = stream;
            }

            host._readThread = host.ReadStream(host._tokenSource.Token);
            return(host);
        }
Exemple #2
0
        public static async Task <MllpHost> Create(TcpClient tcpClient, IHl7MessageMiddleware middleware, Encoding encoding = null, ServerSecurityDetails securityDetails = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            Stream        stream;
            NetworkStream networkStream = tcpClient.GetStream();

            if (securityDetails != null)
            {
                var sslStream = new SslStream(networkStream, true, securityDetails.ClientCertificateValidationCallback, null);

                try
                {
                    bool askForClientCertificate = securityDetails.ForceClientAuthentciation;
                    await sslStream.AuthenticateAsServerAsync(securityDetails.ServerCertificate, askForClientCertificate, securityDetails.SupportedSslProtocols, false);

                    if (askForClientCertificate && !sslStream.IsMutuallyAuthenticated)
                    {
                        throw new AuthenticationException("mutual authentication failed.");
                    }
                }
                catch (Exception)
                {
                    sslStream.Dispose();
                    throw;
                }

                stream = sslStream;
            }
            else
            {
                stream = networkStream;
            }

            var host = new MllpHost(tcpClient, encoding ?? Encoding.ASCII, middleware, stream, cancellationToken);

            host.ReadStream(host._token);
            return(host);
        }