| | | 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.Diagnostics; |
| | | 5 | | using System.Net.Security; |
| | | 6 | | using System.Runtime.InteropServices; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | using System.Security.Cryptography.X509Certificates; |
| | | 9 | | using System.Security.Principal; |
| | | 10 | | using Microsoft.Win32.SafeHandles; |
| | | 11 | | using static Interop.SspiCli; |
| | | 12 | | |
| | | 13 | | namespace System.Net |
| | | 14 | | { |
| | | 15 | | internal static partial class CertificateValidationPal |
| | | 16 | | { |
| | | 17 | | internal static SslPolicyErrors VerifyCertificateProperties( |
| | | 18 | | SafeDeleteContext? _ /*securityContext*/, |
| | | 19 | | X509Chain chain, |
| | | 20 | | X509Certificate2 remoteCertificate, |
| | | 21 | | bool checkCertName, |
| | | 22 | | bool isServer, |
| | | 23 | | string? hostName) |
| | 0 | 24 | | { |
| | 0 | 25 | | return CertificateValidation.BuildChainAndVerifyProperties(chain, remoteCertificate, checkCertName, isServer |
| | 0 | 26 | | } |
| | | 27 | | |
| | | 28 | | // |
| | | 29 | | // Extracts a remote certificate upon request. |
| | | 30 | | // |
| | | 31 | | |
| | | 32 | | private static X509Certificate2? GetRemoteCertificate( |
| | | 33 | | SafeDeleteContext? securityContext, |
| | | 34 | | bool retrieveChainCertificates, |
| | | 35 | | ref X509Chain? chain, |
| | | 36 | | X509ChainPolicy? chainPolicy) |
| | 0 | 37 | | { |
| | 0 | 38 | | if (securityContext == null) |
| | 0 | 39 | | { |
| | 0 | 40 | | return null; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | X509Certificate2? result = null; |
| | 0 | 44 | | SafeFreeCertContext? remoteContext = null; |
| | | 45 | | try |
| | 0 | 46 | | { |
| | | 47 | | // SECPKG_ATTR_REMOTE_CERT_CONTEXT will not succeed before TLS handshake completes. Inside the handshake |
| | | 48 | | // we need to use (more expensive) SECPKG_ATTR_REMOTE_CERT_CHAIN. That one may be unsupported on older |
| | | 49 | | // versions of windows. In that case, we have no option than to return null. |
| | | 50 | | // |
| | | 51 | | // We can use retrieveCollection to distinguish between in-handshake and after-handshake calls, because |
| | | 52 | | // the collection is retrieved for cert validation purposes after the handshake completes. |
| | 0 | 53 | | if (retrieveChainCertificates) // handshake completed |
| | 0 | 54 | | { |
| | 0 | 55 | | SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CONTEXT(GlobalSSPI.SSPISecureChannel, sec |
| | 0 | 56 | | } |
| | | 57 | | else // in handshake |
| | 0 | 58 | | { |
| | 0 | 59 | | SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_REMOTE_CERT_CHAIN(GlobalSSPI.SSPISecureChannel, secur |
| | 0 | 60 | | } |
| | | 61 | | |
| | 0 | 62 | | if (remoteContext != null && !remoteContext.IsInvalid) |
| | 0 | 63 | | { |
| | 0 | 64 | | result = new X509Certificate2(remoteContext.DangerousGetHandle()); |
| | 0 | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | finally |
| | 0 | 68 | | { |
| | 0 | 69 | | if (remoteContext != null) |
| | 0 | 70 | | { |
| | 0 | 71 | | if (!remoteContext.IsInvalid) |
| | 0 | 72 | | { |
| | 0 | 73 | | if (retrieveChainCertificates) |
| | 0 | 74 | | { |
| | 0 | 75 | | chain ??= new X509Chain(); |
| | 0 | 76 | | if (chainPolicy != null) |
| | 0 | 77 | | { |
| | 0 | 78 | | chain.ChainPolicy = chainPolicy; |
| | 0 | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | UnmanagedCertificateContext.GetRemoteCertificatesFromStoreContext(remoteContext, chain.Chain |
| | 0 | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | remoteContext.Dispose(); |
| | 0 | 86 | | } |
| | 0 | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Log.RemoteCertificate(result); |
| | 0 | 90 | | return result; |
| | 0 | 91 | | } |
| | | 92 | | |
| | | 93 | | // Check that local certificate was used by schannel. |
| | | 94 | | internal static bool IsLocalCertificateUsed(SafeFreeCredentials? _credentialsHandle, SafeDeleteContext securityC |
| | 0 | 95 | | { |
| | 0 | 96 | | SecPkgContext_SessionInfo info = default; |
| | | 97 | | // fails on Server 2008 and older. We will fall-back to probing LOCAL_CERT_CONTEXT in that case. |
| | 0 | 98 | | if (SSPIWrapper.QueryBlittableContextAttributes( |
| | 0 | 99 | | GlobalSSPI.SSPISecureChannel, |
| | 0 | 100 | | securityContext, |
| | 0 | 101 | | Interop.SspiCli.ContextAttribute.SECPKG_ATTR_SESSION_INFO, |
| | 0 | 102 | | ref info) && |
| | 0 | 103 | | ((SecPkgContext_SessionInfo.Flags)info.dwFlags).HasFlag(SecPkgContext_SessionInfo.Flags.SSL_SESSION_RECON |
| | 0 | 104 | | { |
| | | 105 | | // This is TLS Resumed session. Windows can fail to query the local cert bellow. |
| | | 106 | | // Instead, we will determine the usage form used credentials. |
| | 0 | 107 | | SafeFreeCredential_SECURITY creds = (SafeFreeCredential_SECURITY)_credentialsHandle!; |
| | 0 | 108 | | return creds.HasLocalCertificate; |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | SafeFreeCertContext? localContext = null; |
| | | 112 | | try |
| | 0 | 113 | | { |
| | 0 | 114 | | if (SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_LOCAL_CERT_CONTEXT(GlobalSSPI.SSPISecureChannel, secu |
| | 0 | 115 | | { |
| | 0 | 116 | | return localContext != null ? !localContext.IsInvalid : false; |
| | | 117 | | } |
| | 0 | 118 | | } |
| | | 119 | | finally |
| | 0 | 120 | | { |
| | 0 | 121 | | localContext?.Dispose(); |
| | 0 | 122 | | } |
| | | 123 | | |
| | | 124 | | // Some older Windows do not support that. This is only called when client certificate was provided |
| | | 125 | | // so assume it was for a reason. |
| | 0 | 126 | | return true; |
| | 0 | 127 | | } |
| | | 128 | | |
| | | 129 | | // |
| | | 130 | | // Used only by client SSL code, never returns null. |
| | | 131 | | // |
| | | 132 | | internal static string[] GetRequestCertificateAuthorities(SafeDeleteContext securityContext) |
| | 0 | 133 | | { |
| | 0 | 134 | | Interop.SspiCli.SecPkgContext_IssuerListInfoEx issuerList = default; |
| | 0 | 135 | | bool success = SSPIWrapper.QueryContextAttributes_SECPKG_ATTR_ISSUER_LIST_EX(GlobalSSPI.SSPISecureChannel, s |
| | | 136 | | |
| | 0 | 137 | | string[] issuers = Array.Empty<string>(); |
| | | 138 | | try |
| | 0 | 139 | | { |
| | 0 | 140 | | if (success && issuerList.cIssuers > 0) |
| | 0 | 141 | | { |
| | | 142 | | unsafe |
| | 0 | 143 | | { |
| | 0 | 144 | | issuers = new string[issuerList.cIssuers]; |
| | 0 | 145 | | var elements = new Span<Interop.SspiCli.CERT_CHAIN_ELEMENT>((void*)sspiHandle!.DangerousGetHandl |
| | 0 | 146 | | for (int i = 0; i < elements.Length; ++i) |
| | 0 | 147 | | { |
| | 0 | 148 | | Debug.Assert(elements[i].cbSize > 0, $"Interop.SspiCli._CERT_CHAIN_ELEMENT size is not posit |
| | 0 | 149 | | if (elements[i].cbSize > 0) |
| | 0 | 150 | | { |
| | 0 | 151 | | ReadOnlySpan<byte> x = new ReadOnlySpan<byte>((byte*)elements[i].pCertContext, checked(( |
| | 0 | 152 | | var x500DistinguishedName = new X500DistinguishedName(x); |
| | 0 | 153 | | issuers[i] = x500DistinguishedName.Name; |
| | 0 | 154 | | if (NetEventSource.Log.IsEnabled()) NetEventSource.Info(securityContext, $"IssuerListEx[ |
| | 0 | 155 | | } |
| | 0 | 156 | | } |
| | 0 | 157 | | } |
| | 0 | 158 | | } |
| | 0 | 159 | | } |
| | | 160 | | finally |
| | 0 | 161 | | { |
| | 0 | 162 | | sspiHandle?.Dispose(); |
| | 0 | 163 | | } |
| | | 164 | | |
| | 0 | 165 | | return issuers; |
| | 0 | 166 | | } |
| | | 167 | | |
| | | 168 | | // |
| | | 169 | | // Security: We temporarily reset thread token to open the cert store under process account. |
| | | 170 | | // |
| | | 171 | | internal static X509Store OpenStore(StoreLocation storeLocation) |
| | 0 | 172 | | { |
| | 0 | 173 | | X509Store store = new X509Store(StoreName.My, storeLocation); |
| | | 174 | | |
| | | 175 | | // For app-compat We want to ensure the store is opened under the **process** account. |
| | | 176 | | try |
| | 0 | 177 | | { |
| | 0 | 178 | | using SafeAccessTokenHandle invalidHandle = SafeAccessTokenHandle.InvalidHandle; |
| | 0 | 179 | | WindowsIdentity.RunImpersonated(invalidHandle, () => |
| | 0 | 180 | | { |
| | 0 | 181 | | store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly); |
| | 0 | 182 | | }); |
| | 0 | 183 | | } |
| | 0 | 184 | | catch |
| | 0 | 185 | | { |
| | 0 | 186 | | throw; |
| | | 187 | | } |
| | | 188 | | |
| | 0 | 189 | | return store; |
| | 0 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |