From b2107c5e48d2fa5163aa6bf3182a530a04d1533c Mon Sep 17 00:00:00 2001 From: Tomas Weinfurt Date: Tue, 20 Jul 2021 20:12:31 -0700 Subject: [PATCH] throw PNSE for unsupported SSL options in Quic. (#55877) * throw PNSP for unsupported SSL options * add missing resource file change * fix spacing --- .../src/Resources/Strings.resx | 3 ++ .../Interop/SafeMsQuicConfigurationHandle.cs | 53 +++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/libraries/System.Net.Quic/src/Resources/Strings.resx b/src/libraries/System.Net.Quic/src/Resources/Strings.resx index a29352a0578f59..061e1647eb8cf8 100644 --- a/src/libraries/System.Net.Quic/src/Resources/Strings.resx +++ b/src/libraries/System.Net.Quic/src/Resources/Strings.resx @@ -150,5 +150,8 @@ Writing is not allowed on stream. + + The '{0}' is not supported by System.Net.Quic. + diff --git a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Interop/SafeMsQuicConfigurationHandle.cs b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Interop/SafeMsQuicConfigurationHandle.cs index df48e0db377aed..96b168977af69c 100644 --- a/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Interop/SafeMsQuicConfigurationHandle.cs +++ b/src/libraries/System.Net.Quic/src/System/Net/Quic/Implementations/MsQuic/Interop/SafeMsQuicConfigurationHandle.cs @@ -36,20 +36,39 @@ protected override bool ReleaseHandle() public static unsafe SafeMsQuicConfigurationHandle Create(QuicClientConnectionOptions options) { X509Certificate? certificate = null; - if (options.ClientAuthenticationOptions?.ClientCertificates != null) + + if (options.ClientAuthenticationOptions != null) { - foreach (var cert in options.ClientAuthenticationOptions.ClientCertificates) + if (options.ClientAuthenticationOptions.CipherSuitesPolicy != null) + { + throw new PlatformNotSupportedException(SR.Format(SR.net_quic_ssl_option, nameof(options.ClientAuthenticationOptions.CipherSuitesPolicy))); + } + + if (options.ClientAuthenticationOptions.EncryptionPolicy == EncryptionPolicy.NoEncryption) { - try + throw new PlatformNotSupportedException(SR.Format(SR.net_quic_ssl_option, nameof(options.ClientAuthenticationOptions.EncryptionPolicy))); + } + + if (options.ClientAuthenticationOptions.LocalCertificateSelectionCallback != null) + { + throw new PlatformNotSupportedException(SR.Format(SR.net_quic_ssl_option, nameof(options.ClientAuthenticationOptions.LocalCertificateSelectionCallback))); + } + + if (options.ClientAuthenticationOptions.ClientCertificates != null) + { + foreach (var cert in options.ClientAuthenticationOptions.ClientCertificates) { - if (((X509Certificate2)cert).HasPrivateKey) + try { - // Pick first certificate with private key. - certificate = cert; - break; + if (((X509Certificate2)cert).HasPrivateKey) + { + // Pick first certificate with private key. + certificate = cert; + break; + } } + catch { } } - catch { } } } @@ -59,9 +78,23 @@ public static unsafe SafeMsQuicConfigurationHandle Create(QuicClientConnectionOp public static unsafe SafeMsQuicConfigurationHandle Create(QuicListenerOptions options) { QUIC_CREDENTIAL_FLAGS flags = QUIC_CREDENTIAL_FLAGS.NONE; - if (options.ServerAuthenticationOptions != null && options.ServerAuthenticationOptions.ClientCertificateRequired) + + if (options.ServerAuthenticationOptions != null) { - flags |= QUIC_CREDENTIAL_FLAGS.REQUIRE_CLIENT_AUTHENTICATION | QUIC_CREDENTIAL_FLAGS.INDICATE_CERTIFICATE_RECEIVED | QUIC_CREDENTIAL_FLAGS.NO_CERTIFICATE_VALIDATION; + if (options.ServerAuthenticationOptions.CipherSuitesPolicy != null) + { + throw new PlatformNotSupportedException(SR.Format(SR.net_quic_ssl_option, nameof(options.ServerAuthenticationOptions.CipherSuitesPolicy))); + } + + if (options.ServerAuthenticationOptions.EncryptionPolicy == EncryptionPolicy.NoEncryption) + { + throw new PlatformNotSupportedException(SR.Format(SR.net_quic_ssl_option, nameof(options.ServerAuthenticationOptions.EncryptionPolicy))); + } + + if (options.ServerAuthenticationOptions.ClientCertificateRequired) + { + flags |= QUIC_CREDENTIAL_FLAGS.REQUIRE_CLIENT_AUTHENTICATION | QUIC_CREDENTIAL_FLAGS.INDICATE_CERTIFICATE_RECEIVED | QUIC_CREDENTIAL_FLAGS.NO_CERTIFICATE_VALIDATION; + } } return Create(options, flags, options.ServerAuthenticationOptions?.ServerCertificate, options.ServerAuthenticationOptions?.ServerCertificateContext, options.ServerAuthenticationOptions?.ApplicationProtocols);