| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics; |
| | | 7 | | using System.Security.Authentication; |
| | | 8 | | using System.Security.Cryptography.X509Certificates; |
| | | 9 | | |
| | | 10 | | namespace System.Net.Security |
| | | 11 | | { |
| | | 12 | | internal sealed partial class SslAuthenticationOptions : IDisposable |
| | | 13 | | { |
| | | 14 | | // Hook invoked by OpenSSL's CertVerifyCallback to drive remote |
| | | 15 | | // certificate validation. Set by SslStream and by standalone TlsSession |
| | | 16 | | // so both flows share the same callback plumbing. Declared on the |
| | | 17 | | // cross-platform partial so the delegate type is resolvable in test |
| | | 18 | | // projects (unit-test fakes) that compile SslStream.cs on non-Linux |
| | | 19 | | // targets where the OpenSSL partial file isn't included. |
| | | 20 | | internal delegate bool VerifyRemoteCertificateCallback( |
| | | 21 | | X509Certificate2? certificate, |
| | | 22 | | X509Chain? chain, |
| | | 23 | | SslCertificateTrust? trust, |
| | | 24 | | ref ProtocolToken alertToken, |
| | | 25 | | ref SslPolicyErrors sslPolicyErrors, |
| | | 26 | | out X509ChainStatusFlags chainStatus); |
| | | 27 | | |
| | | 28 | | internal VerifyRemoteCertificateCallback? RemoteCertificateValidator { get; set; } |
| | | 29 | | |
| | | 30 | | internal const X509RevocationMode DefaultRevocationMode = X509RevocationMode.NoCheck; |
| | | 31 | | |
| | 31 | 32 | | internal SslAuthenticationOptions() |
| | 31 | 33 | | { |
| | 31 | 34 | | TargetHost = string.Empty; |
| | 31 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal void UpdateOptions(SslClientAuthenticationOptions sslClientAuthenticationOptions) |
| | 0 | 38 | | { |
| | 0 | 39 | | if (CertValidationDelegate == null) |
| | 0 | 40 | | { |
| | 0 | 41 | | CertValidationDelegate = sslClientAuthenticationOptions.RemoteCertificateValidationCallback; |
| | 0 | 42 | | } |
| | 0 | 43 | | else if (sslClientAuthenticationOptions.RemoteCertificateValidationCallback != null && |
| | 0 | 44 | | CertValidationDelegate != sslClientAuthenticationOptions.RemoteCertificateValidationCallback) |
| | 0 | 45 | | { |
| | | 46 | | // Callback was set in constructor to different value. |
| | 0 | 47 | | throw new InvalidOperationException(SR.Format(SR.net_conflicting_options, nameof(RemoteCertificateValida |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | if (CertSelectionDelegate == null) |
| | 0 | 51 | | { |
| | 0 | 52 | | CertSelectionDelegate = sslClientAuthenticationOptions.LocalCertificateSelectionCallback; |
| | 0 | 53 | | } |
| | 0 | 54 | | else if (sslClientAuthenticationOptions.LocalCertificateSelectionCallback != null && |
| | 0 | 55 | | CertSelectionDelegate != sslClientAuthenticationOptions.LocalCertificateSelectionCallback) |
| | 0 | 56 | | { |
| | 0 | 57 | | throw new InvalidOperationException(SR.Format(SR.net_conflicting_options, nameof(LocalCertificateSelecti |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // Common options. |
| | 0 | 61 | | AllowRenegotiation = sslClientAuthenticationOptions.AllowRenegotiation; |
| | 0 | 62 | | AllowTlsResume = sslClientAuthenticationOptions.AllowTlsResume; |
| | 0 | 63 | | ApplicationProtocols = sslClientAuthenticationOptions.ApplicationProtocols; |
| | 0 | 64 | | CheckCertName = !(sslClientAuthenticationOptions.CertificateChainPolicy?.VerificationFlags.HasFlag(X509Verif |
| | 0 | 65 | | EnabledSslProtocols = FilterOutIncompatibleSslProtocols(sslClientAuthenticationOptions.EnabledSslProtocols); |
| | 0 | 66 | | EncryptionPolicy = sslClientAuthenticationOptions.EncryptionPolicy; |
| | 0 | 67 | | IsServer = false; |
| | 0 | 68 | | RemoteCertRequired = true; |
| | 0 | 69 | | CertificateContext = sslClientAuthenticationOptions.ClientCertificateContext; |
| | 0 | 70 | | TargetHost = sslClientAuthenticationOptions.TargetHost ?? string.Empty; |
| | | 71 | | |
| | 0 | 72 | | AllowRsaPssPadding = sslClientAuthenticationOptions.AllowRsaPssPadding; |
| | 0 | 73 | | AllowRsaPkcs1Padding = sslClientAuthenticationOptions.AllowRsaPkcs1Padding; |
| | | 74 | | |
| | 0 | 75 | | if (!OperatingSystem.IsWindows() && !OperatingSystem.IsLinux()) |
| | 0 | 76 | | { |
| | 0 | 77 | | if (!sslClientAuthenticationOptions.AllowRsaPssPadding || !sslClientAuthenticationOptions.AllowRsaPkcs1P |
| | 0 | 78 | | { |
| | 0 | 79 | | throw new PlatformNotSupportedException(SR.net_ssl_allow_rsa_padding_not_supported); |
| | | 80 | | } |
| | 0 | 81 | | } |
| | | 82 | | |
| | | 83 | | // Client specific options. |
| | 0 | 84 | | CertificateRevocationCheckMode = sslClientAuthenticationOptions.CertificateRevocationCheckMode; |
| | 0 | 85 | | ClientCertificates = sslClientAuthenticationOptions.ClientCertificates; |
| | 0 | 86 | | CipherSuitesPolicy = sslClientAuthenticationOptions.CipherSuitesPolicy; |
| | | 87 | | |
| | 0 | 88 | | if (sslClientAuthenticationOptions.CertificateChainPolicy != null) |
| | 0 | 89 | | { |
| | 0 | 90 | | CertificateChainPolicy = sslClientAuthenticationOptions.CertificateChainPolicy.Clone(); |
| | 0 | 91 | | } |
| | 0 | 92 | | } |
| | | 93 | | |
| | | 94 | | internal void UpdateOptions(ServerOptionsSelectionCallback optionCallback, object? state) |
| | 31 | 95 | | { |
| | 31 | 96 | | CheckCertName = false; |
| | 31 | 97 | | TargetHost = string.Empty; |
| | 31 | 98 | | IsServer = true; |
| | 31 | 99 | | UserState = state; |
| | 31 | 100 | | ServerOptionDelegate = optionCallback; |
| | 31 | 101 | | } |
| | | 102 | | |
| | | 103 | | internal void UpdateOptions(SslServerAuthenticationOptions sslServerAuthenticationOptions) |
| | 0 | 104 | | { |
| | 0 | 105 | | if (sslServerAuthenticationOptions.ServerCertificate == null && |
| | 0 | 106 | | sslServerAuthenticationOptions.ServerCertificateContext == null && |
| | 0 | 107 | | sslServerAuthenticationOptions.ServerCertificateSelectionCallback == null && |
| | 0 | 108 | | CertSelectionDelegate == null) |
| | 0 | 109 | | { |
| | 0 | 110 | | throw new NotSupportedException(SR.net_ssl_io_no_server_cert); |
| | | 111 | | } |
| | | 112 | | |
| | 0 | 113 | | if ((sslServerAuthenticationOptions.ServerCertificate != null || |
| | 0 | 114 | | sslServerAuthenticationOptions.ServerCertificateContext != null || |
| | 0 | 115 | | CertSelectionDelegate != null) && |
| | 0 | 116 | | sslServerAuthenticationOptions.ServerCertificateSelectionCallback != null) |
| | 0 | 117 | | { |
| | 0 | 118 | | throw new InvalidOperationException(SR.Format(SR.net_conflicting_options, nameof(ServerCertificateSelect |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | if (CertValidationDelegate == null) |
| | 0 | 122 | | { |
| | 0 | 123 | | CertValidationDelegate = sslServerAuthenticationOptions.RemoteCertificateValidationCallback; |
| | 0 | 124 | | } |
| | 0 | 125 | | else if (sslServerAuthenticationOptions.RemoteCertificateValidationCallback != null && |
| | 0 | 126 | | CertValidationDelegate != sslServerAuthenticationOptions.RemoteCertificateValidationCallback) |
| | 0 | 127 | | { |
| | | 128 | | // Callback was set in constructor to differet value. |
| | 0 | 129 | | throw new InvalidOperationException(SR.Format(SR.net_conflicting_options, nameof(RemoteCertificateValida |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | IsServer = true; |
| | 0 | 133 | | AllowRenegotiation = sslServerAuthenticationOptions.AllowRenegotiation; |
| | 0 | 134 | | AllowTlsResume = sslServerAuthenticationOptions.AllowTlsResume; |
| | 0 | 135 | | ApplicationProtocols = sslServerAuthenticationOptions.ApplicationProtocols; |
| | 0 | 136 | | EnabledSslProtocols = FilterOutIncompatibleSslProtocols(sslServerAuthenticationOptions.EnabledSslProtocols); |
| | 0 | 137 | | EncryptionPolicy = sslServerAuthenticationOptions.EncryptionPolicy; |
| | 0 | 138 | | RemoteCertRequired = sslServerAuthenticationOptions.ClientCertificateRequired; |
| | 0 | 139 | | CipherSuitesPolicy = sslServerAuthenticationOptions.CipherSuitesPolicy; |
| | 0 | 140 | | CertificateRevocationCheckMode = sslServerAuthenticationOptions.CertificateRevocationCheckMode; |
| | | 141 | | |
| | 0 | 142 | | AllowRsaPssPadding = sslServerAuthenticationOptions.AllowRsaPssPadding; |
| | 0 | 143 | | AllowRsaPkcs1Padding = sslServerAuthenticationOptions.AllowRsaPkcs1Padding; |
| | | 144 | | |
| | 0 | 145 | | if (!OperatingSystem.IsWindows() && !OperatingSystem.IsLinux()) |
| | 0 | 146 | | { |
| | 0 | 147 | | if (!sslServerAuthenticationOptions.AllowRsaPssPadding || !sslServerAuthenticationOptions.AllowRsaPkcs1P |
| | 0 | 148 | | { |
| | 0 | 149 | | throw new PlatformNotSupportedException(SR.net_ssl_allow_rsa_padding_not_supported); |
| | | 150 | | } |
| | 0 | 151 | | } |
| | | 152 | | |
| | 0 | 153 | | if (sslServerAuthenticationOptions.ServerCertificateContext != null) |
| | 0 | 154 | | { |
| | | 155 | | // Release any previously owned context before replacing it with the caller's context. |
| | 0 | 156 | | if (OwnsCertificateContext && CertificateContext is not null && |
| | 0 | 157 | | !ReferenceEquals(CertificateContext, sslServerAuthenticationOptions.ServerCertificateContext)) |
| | 0 | 158 | | { |
| | 0 | 159 | | CertificateContext.ReleaseResources(); |
| | 0 | 160 | | } |
| | 0 | 161 | | CertificateContext = sslServerAuthenticationOptions.ServerCertificateContext; |
| | 0 | 162 | | OwnsCertificateContext = false; |
| | 0 | 163 | | } |
| | 0 | 164 | | else if (sslServerAuthenticationOptions.ServerCertificate != null) |
| | 0 | 165 | | { |
| | 0 | 166 | | X509Certificate2? certificateWithKey = sslServerAuthenticationOptions.ServerCertificate as X509Certifica |
| | | 167 | | |
| | 0 | 168 | | if (certificateWithKey != null && certificateWithKey.HasPrivateKey) |
| | 0 | 169 | | { |
| | 0 | 170 | | bool ocspFetch = LocalAppContextSwitches.EnableOcspStapling; |
| | | 171 | | // given cert is X509Certificate2 with key. We can use it directly. |
| | 0 | 172 | | SetCertificateContextFromCert(certificateWithKey, !ocspFetch); |
| | 0 | 173 | | } |
| | | 174 | | else |
| | 0 | 175 | | { |
| | | 176 | | // This is legacy fix-up. If the Certificate did not have key, we will search stores and we |
| | | 177 | | // will try to find one with matching hash. |
| | 0 | 178 | | certificateWithKey = SslStream.FindCertificateWithPrivateKey(this, true, sslServerAuthenticationOpti |
| | 0 | 179 | | if (certificateWithKey == null) |
| | 0 | 180 | | { |
| | 0 | 181 | | throw new AuthenticationException(SR.net_ssl_io_no_server_cert); |
| | | 182 | | } |
| | | 183 | | |
| | 0 | 184 | | SetCertificateContextFromCert(certificateWithKey); |
| | 0 | 185 | | } |
| | 0 | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | if (sslServerAuthenticationOptions.ServerCertificateSelectionCallback != null) |
| | 0 | 189 | | { |
| | 0 | 190 | | ServerCertSelectionDelegate = sslServerAuthenticationOptions.ServerCertificateSelectionCallback; |
| | 0 | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | if (sslServerAuthenticationOptions.CertificateChainPolicy != null) |
| | 0 | 194 | | { |
| | 0 | 195 | | CertificateChainPolicy = sslServerAuthenticationOptions.CertificateChainPolicy.Clone(); |
| | 0 | 196 | | } |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | private static SslProtocols FilterOutIncompatibleSslProtocols(SslProtocols protocols) |
| | 0 | 200 | | { |
| | 0 | 201 | | if ((protocols & (SslProtocols.Tls12 | SslProtocols.Tls13)) != SslProtocols.None) |
| | 0 | 202 | | { |
| | | 203 | | #pragma warning disable 0618 |
| | | 204 | | // SSL2 is mutually exclusive with >= TLS1.2 |
| | 0 | 205 | | protocols &= ~SslProtocols.Ssl2; |
| | | 206 | | #pragma warning restore 0618 |
| | 0 | 207 | | } |
| | | 208 | | |
| | 0 | 209 | | return protocols; |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | internal void SetCertificateContextFromCert(X509Certificate2 certificate, bool? noOcspFetch = null) |
| | 0 | 213 | | { |
| | 0 | 214 | | CertificateContext = SslStreamCertificateContext.Create(certificate, null, offline: false, null, noOcspFetch |
| | 0 | 215 | | OwnsCertificateContext = true; |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | // Shallow copy of the configuration carried by this bag. Per-handle/per-stream |
| | | 219 | | // state (SafeSslHandle, SslStream, RemoteCertificateValidator) is intentionally |
| | | 220 | | // not propagated, and the clone does not take ownership of CertificateContext |
| | | 221 | | // even if the source did. |
| | | 222 | | internal SslAuthenticationOptions Clone() |
| | 0 | 223 | | { |
| | 0 | 224 | | SslAuthenticationOptions copy = new SslAuthenticationOptions |
| | 0 | 225 | | { |
| | 0 | 226 | | AllowRenegotiation = AllowRenegotiation, |
| | 0 | 227 | | TargetHost = TargetHost, |
| | 0 | 228 | | ClientCertificates = ClientCertificates, |
| | 0 | 229 | | ApplicationProtocols = ApplicationProtocols, |
| | 0 | 230 | | IsServer = IsServer, |
| | 0 | 231 | | CertificateContext = CertificateContext, |
| | 0 | 232 | | OwnsCertificateContext = false, |
| | 0 | 233 | | EnabledSslProtocols = EnabledSslProtocols, |
| | 0 | 234 | | CertificateRevocationCheckMode = CertificateRevocationCheckMode, |
| | 0 | 235 | | EncryptionPolicy = EncryptionPolicy, |
| | 0 | 236 | | RemoteCertRequired = RemoteCertRequired, |
| | 0 | 237 | | CheckCertName = CheckCertName, |
| | 0 | 238 | | CertValidationDelegate = CertValidationDelegate, |
| | 0 | 239 | | CertSelectionDelegate = CertSelectionDelegate, |
| | 0 | 240 | | ServerCertSelectionDelegate = ServerCertSelectionDelegate, |
| | 0 | 241 | | CipherSuitesPolicy = CipherSuitesPolicy, |
| | 0 | 242 | | UserState = UserState, |
| | 0 | 243 | | ServerOptionDelegate = ServerOptionDelegate, |
| | 0 | 244 | | CertificateChainPolicy = CertificateChainPolicy, |
| | 0 | 245 | | AllowTlsResume = AllowTlsResume, |
| | 0 | 246 | | AllowRsaPssPadding = AllowRsaPssPadding, |
| | 0 | 247 | | AllowRsaPkcs1Padding = AllowRsaPkcs1Padding, |
| | 0 | 248 | | ForceSyncPal = ForceSyncPal, |
| | 0 | 249 | | }; |
| | 0 | 250 | | return copy; |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | // Bulk-copy field values from another options bag into this one. Used by |
| | | 254 | | // TlsSession.SetContext to inherit a fully-configured server context's |
| | | 255 | | // options into an existing session (whose bag was originally created empty |
| | | 256 | | // from a deferred TlsContext.Create((SslServerAuthenticationOptions?)null)). |
| | | 257 | | // Mirrors the field set copied by Clone(). Session-scoped state (SafeSslHandle, |
| | | 258 | | // RemoteCertificateValidator, SocketHandle, ReplayPrefix, PreallocatedSslContext) |
| | | 259 | | // is intentionally NOT copied — those belong to the receiving session. |
| | | 260 | | internal void CopyFrom(SslAuthenticationOptions other) |
| | 0 | 261 | | { |
| | 0 | 262 | | AllowRenegotiation = other.AllowRenegotiation; |
| | 0 | 263 | | TargetHost = other.TargetHost; |
| | 0 | 264 | | ClientCertificates = other.ClientCertificates; |
| | 0 | 265 | | ApplicationProtocols = other.ApplicationProtocols; |
| | 0 | 266 | | IsServer = other.IsServer; |
| | 0 | 267 | | CertificateContext = other.CertificateContext; |
| | 0 | 268 | | OwnsCertificateContext = false; |
| | 0 | 269 | | EnabledSslProtocols = other.EnabledSslProtocols; |
| | 0 | 270 | | CertificateRevocationCheckMode = other.CertificateRevocationCheckMode; |
| | 0 | 271 | | EncryptionPolicy = other.EncryptionPolicy; |
| | 0 | 272 | | RemoteCertRequired = other.RemoteCertRequired; |
| | 0 | 273 | | CheckCertName = other.CheckCertName; |
| | 0 | 274 | | CertValidationDelegate = other.CertValidationDelegate; |
| | 0 | 275 | | CertSelectionDelegate = other.CertSelectionDelegate; |
| | 0 | 276 | | ServerCertSelectionDelegate = other.ServerCertSelectionDelegate; |
| | 0 | 277 | | CipherSuitesPolicy = other.CipherSuitesPolicy; |
| | 0 | 278 | | UserState = other.UserState; |
| | 0 | 279 | | ServerOptionDelegate = other.ServerOptionDelegate; |
| | 0 | 280 | | CertificateChainPolicy = other.CertificateChainPolicy; |
| | 0 | 281 | | AllowTlsResume = other.AllowTlsResume; |
| | 0 | 282 | | AllowRsaPssPadding = other.AllowRsaPssPadding; |
| | 0 | 283 | | AllowRsaPkcs1Padding = other.AllowRsaPkcs1Padding; |
| | 0 | 284 | | ForceSyncPal = other.ForceSyncPal; |
| | 0 | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | internal bool AllowRenegotiation { get; set; } |
| | 62 | 288 | | internal string TargetHost { get; set; } |
| | 0 | 289 | | internal X509CertificateCollection? ClientCertificates { get; set; } |
| | 0 | 290 | | internal List<SslApplicationProtocol>? ApplicationProtocols { get; set; } |
| | 71 | 291 | | internal bool IsServer { get; set; } |
| | | 292 | | internal bool IsClient => !IsServer; |
| | 0 | 293 | | internal SslStreamCertificateContext? CertificateContext { get; set; } |
| | | 294 | | // If true, the certificate context was created by the SslStream and |
| | | 295 | | // certificates inside should be disposed when no longer needed. |
| | 31 | 296 | | internal bool OwnsCertificateContext { get; set; } |
| | 0 | 297 | | internal SslProtocols EnabledSslProtocols { get; set; } |
| | 0 | 298 | | internal X509RevocationMode CertificateRevocationCheckMode { get; set; } |
| | 31 | 299 | | internal EncryptionPolicy EncryptionPolicy { get; set; } |
| | 0 | 300 | | internal bool RemoteCertRequired { get; set; } |
| | 31 | 301 | | internal bool CheckCertName { get; set; } |
| | 31 | 302 | | internal RemoteCertificateValidationCallback? CertValidationDelegate { get; set; } |
| | 31 | 303 | | internal LocalCertificateSelectionCallback? CertSelectionDelegate { get; set; } |
| | 0 | 304 | | internal ServerCertificateSelectionCallback? ServerCertSelectionDelegate { get; set; } |
| | 0 | 305 | | internal CipherSuitesPolicy? CipherSuitesPolicy { get; set; } |
| | 31 | 306 | | internal object? UserState { get; set; } |
| | 31 | 307 | | internal ServerOptionsSelectionCallback? ServerOptionDelegate { get; set; } |
| | 0 | 308 | | internal X509ChainPolicy? CertificateChainPolicy { get; set; } |
| | 0 | 309 | | internal bool AllowTlsResume { get; set; } |
| | 0 | 310 | | internal bool AllowRsaPssPadding { get; set; } |
| | 0 | 311 | | internal bool AllowRsaPkcs1Padding { get; set; } |
| | | 312 | | // Set by callers (e.g. TlsSession) whose state machine is intrinsically synchronous |
| | | 313 | | // and cannot use the async Network Framework PAL path on macOS. |
| | 0 | 314 | | internal bool ForceSyncPal { get; set; } |
| | | 315 | | |
| | | 316 | | #if TARGET_ANDROID |
| | | 317 | | internal SslStream.JavaProxy? SslStreamProxy { get; set; } |
| | | 318 | | #endif |
| | | 319 | | |
| | | 320 | | #if !TARGET_WINDOWS && !SYSNETSECURITY_NO_OPENSSL |
| | | 321 | | internal SslStream? SslStream { get; set; } |
| | | 322 | | #endif |
| | | 323 | | |
| | | 324 | | public void Dispose() |
| | 31 | 325 | | { |
| | 31 | 326 | | if (OwnsCertificateContext && CertificateContext != null) |
| | 0 | 327 | | { |
| | 0 | 328 | | CertificateContext.ReleaseResources(); |
| | 0 | 329 | | } |
| | | 330 | | |
| | | 331 | | #if TARGET_ANDROID |
| | | 332 | | SslStreamProxy?.Dispose(); |
| | | 333 | | #endif |
| | 31 | 334 | | } |
| | | 335 | | } |
| | | 336 | | } |
| | | 337 | | |