Example #1
0
        protected sealed override async Task <ISslStream> Start(TestContext ctx, Stream stream, CancellationToken cancellationToken)
        {
            UserSettings userSettings = new UserSettings();

            if (SupportsInstrumentation && InstrumentationProvider != null)
            {
                var instrumentation = InstrumentationProvider.CreateInstrument(ctx);
                if (instrumentation != null && instrumentation.HasSettingsInstrument)
                {
                    userSettings = instrumentation.SettingsInstrument.UserSettings;
                }
                userSettings.Instrumentation = instrumentation;
            }

            GetSettings(userSettings);

            if (ConnectionProvider.IsNewTls)
            {
                settings = new MSI.MonoTlsSettings();
                settings.UserSettings = userSettings;
            }

            monoSslStream = await Start(ctx, stream, settings, cancellationToken);

            return(monoSslStream);
        }
        public async Task <MonoSslStream> CreateClientStreamAsync(Stream stream, string targetHost, ConnectionParameters parameters, MSI.MonoTlsSettings settings, CancellationToken cancellationToken)
        {
            var protocol = GetProtocol(parameters, false);

            CallbackHelpers.AddCertificateValidator(settings, parameters.ClientCertificateValidator);
            CallbackHelpers.AddCertificateSelector(settings, parameters.ClientCertificateSelector);
            var clientCertificates = CallbackHelpers.GetClientCertificates(parameters);

            var sslStream     = tlsProvider.CreateSslStream(stream, false, settings);
            var monoSslStream = new MonoSslStream(sslStream);

            try {
                await sslStream.AuthenticateAsClientAsync(targetHost, clientCertificates, protocol, false).ConfigureAwait(false);
            } catch (Exception ex) {
                var lastError = monoSslStream.LastError;
                if (lastError != null)
                {
                    throw new AggregateException(ex, lastError);
                }
                throw;
            }

            return(monoSslStream);
        }
        public async Task <MonoSslStream> CreateServerStreamAsync(Stream stream, ConnectionParameters parameters, MSI.MonoTlsSettings settings, CancellationToken cancellationToken)
        {
            var certificate = CertificateProvider.GetCertificate(parameters.ServerCertificate);
            var protocol    = GetProtocol(parameters, true);

            CallbackHelpers.AddCertificateValidator(settings, parameters.ServerCertificateValidator);

            var askForCert    = parameters.AskForClientCertificate || parameters.RequireClientCertificate;
            var sslStream     = tlsProvider.CreateSslStream(stream, false, settings);
            var monoSslStream = new MonoSslStream(sslStream);

            try {
                await sslStream.AuthenticateAsServerAsync(certificate, askForCert, protocol, false).ConfigureAwait(false);
            } catch (Exception ex) {
                var lastError = monoSslStream.LastError;
                if (lastError != null)
                {
                    throw new AggregateException(ex, lastError);
                }
                throw;
            }

            return(monoSslStream);
        }