| | | 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; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Security.Claims; |
| | | 8 | | using System.Security.Cryptography; |
| | | 9 | | using System.Security.Cryptography.X509Certificates; |
| | | 10 | | using CoreWCF.IdentityModel.Selectors; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | using CoreWCF.Security; |
| | | 13 | | using Claim = System.Security.Claims.Claim; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.IdentityModel |
| | | 16 | | { |
| | | 17 | | internal static class X509Util |
| | | 18 | | { |
| | | 19 | | /// <summary> |
| | | 20 | | /// Creates an X509CertificateValidator using the given parameters. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <param name="certificateValidationMode">The certificate validation mode to use.</param> |
| | | 23 | | /// <param name="revocationMode">The revocation mode to use.</param> |
| | | 24 | | /// <param name="trustedStoreLocation">The store to use.</param> |
| | | 25 | | /// <returns>The X509CertificateValidator.</returns> |
| | | 26 | | /// <remarks>Due to a WCF bug, X509CertificateValidatorEx must be used rather than WCF's validators directly</re |
| | | 27 | | internal static X509CertificateValidator CreateCertificateValidator( |
| | | 28 | | X509CertificateValidationMode certificateValidationMode, |
| | | 29 | | X509RevocationMode revocationMode, |
| | | 30 | | StoreLocation trustedStoreLocation) |
| | | 31 | | { |
| | 2 | 32 | | return new X509CertificateValidatorEx(certificateValidationMode, revocationMode, trustedStoreLocation); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | internal static string GetCertificateId(X509Certificate2 certificate) |
| | | 36 | | { |
| | 0 | 37 | | if (certificate == null) |
| | | 38 | | { |
| | 0 | 39 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | string certificateId = certificate.SubjectName.Name; |
| | 0 | 43 | | if (string.IsNullOrEmpty(certificateId)) |
| | | 44 | | { |
| | 0 | 45 | | certificateId = certificate.Thumbprint; |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | return certificateId; |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | internal static string GetCertificateIssuerName(X509Certificate2 certificate, IssuerNameRegistry issuerNameRegis |
| | | 52 | | { |
| | 0 | 53 | | if (certificate == null) |
| | | 54 | | { |
| | 0 | 55 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (issuerNameRegistry == null) |
| | | 59 | | { |
| | 0 | 60 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(issuerNameRegistry)); |
| | | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | X509Chain chain = new X509Chain(); |
| | 0 | 64 | | chain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck; |
| | 0 | 65 | | chain.Build(certificate); |
| | 0 | 66 | | X509ChainElementCollection elements = chain.ChainElements; |
| | | 67 | | |
| | 0 | 68 | | string issuer = null; |
| | 0 | 69 | | if (elements.Count > 1) |
| | | 70 | | { |
| | 0 | 71 | | using (X509SecurityToken token = new X509SecurityToken(elements[1].Certificate)) |
| | | 72 | | { |
| | 0 | 73 | | issuer = issuerNameRegistry.GetIssuerName(token); |
| | 0 | 74 | | } |
| | | 75 | | } |
| | | 76 | | else |
| | | 77 | | { |
| | | 78 | | // This is a self-issued certificate. Use the thumbprint of the current certificate. |
| | 0 | 79 | | using (X509SecurityToken token = new X509SecurityToken(certificate)) |
| | | 80 | | { |
| | 0 | 81 | | issuer = issuerNameRegistry.GetIssuerName(token); |
| | 0 | 82 | | } |
| | | 83 | | } |
| | | 84 | | |
| | 0 | 85 | | for (int i = 1; i < elements.Count; ++i) |
| | | 86 | | { |
| | | 87 | | // Resets the state of the certificate and frees resources associated with it. |
| | 0 | 88 | | elements[i].Certificate.Reset(); |
| | | 89 | | } |
| | | 90 | | |
| | 0 | 91 | | return issuer; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public static IEnumerable<Claim> GetClaimsFromCertificate(X509Certificate2 certificate, string issuer) |
| | | 95 | | { |
| | 0 | 96 | | if (certificate == null) |
| | | 97 | | { |
| | 0 | 98 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | ICollection<Claim> claimsCollection = new Collection<Claim>(); |
| | | 102 | | |
| | 0 | 103 | | string thumbprint = Convert.ToBase64String(certificate.GetCertHash()); |
| | 0 | 104 | | claimsCollection.Add(new Claim(ClaimTypes.Thumbprint, thumbprint, ClaimValueTypes.Base64Binary, issuer)); |
| | | 105 | | |
| | 0 | 106 | | string value = certificate.SubjectName.Name; |
| | 0 | 107 | | if (!string.IsNullOrEmpty(value)) |
| | | 108 | | { |
| | 0 | 109 | | claimsCollection.Add(new Claim(ClaimTypes.X500DistinguishedName, value, ClaimValueTypes.String, issuer)) |
| | | 110 | | } |
| | | 111 | | |
| | 0 | 112 | | value = certificate.GetNameInfo(X509NameType.DnsName, false); |
| | 0 | 113 | | if (!string.IsNullOrEmpty(value)) |
| | | 114 | | { |
| | 0 | 115 | | claimsCollection.Add(new Claim(ClaimTypes.Dns, value, ClaimValueTypes.String, issuer)); |
| | | 116 | | } |
| | | 117 | | |
| | 0 | 118 | | value = certificate.GetNameInfo(X509NameType.SimpleName, false); |
| | 0 | 119 | | if (!string.IsNullOrEmpty(value)) |
| | | 120 | | { |
| | 0 | 121 | | claimsCollection.Add(new Claim(ClaimTypes.Name, value, ClaimValueTypes.String, issuer)); |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | value = certificate.GetNameInfo(X509NameType.EmailName, false); |
| | 0 | 125 | | if (!string.IsNullOrEmpty(value)) |
| | | 126 | | { |
| | 0 | 127 | | claimsCollection.Add(new Claim(ClaimTypes.Email, value, ClaimValueTypes.String, issuer)); |
| | | 128 | | } |
| | | 129 | | |
| | 0 | 130 | | value = certificate.GetNameInfo(X509NameType.UpnName, false); |
| | 0 | 131 | | if (!string.IsNullOrEmpty(value)) |
| | | 132 | | { |
| | 0 | 133 | | claimsCollection.Add(new Claim(ClaimTypes.Upn, value, ClaimValueTypes.String, issuer)); |
| | | 134 | | } |
| | | 135 | | |
| | 0 | 136 | | value = certificate.GetNameInfo(X509NameType.UrlName, false); |
| | 0 | 137 | | if (!string.IsNullOrEmpty(value)) |
| | | 138 | | { |
| | 0 | 139 | | claimsCollection.Add(new Claim(ClaimTypes.Uri, value, ClaimValueTypes.String, issuer)); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | RSA rsa; |
| | | 143 | | // if (LocalAppContextSwitches.DisableCngCertificates) |
| | | 144 | | { |
| | 0 | 145 | | rsa = certificate.PublicKey.Key as RSA; |
| | | 146 | | } |
| | | 147 | | //else |
| | | 148 | | //{ |
| | | 149 | | // rsa = CngLightup.GetRSAPublicKey(certificate); |
| | | 150 | | //} |
| | 0 | 151 | | if (rsa != null) |
| | | 152 | | { |
| | 0 | 153 | | claimsCollection.Add(new Claim(ClaimTypes.Rsa, rsa.ToXmlString(false), ClaimValueTypes.RsaKeyValue, issu |
| | | 154 | | } |
| | | 155 | | |
| | | 156 | | DSA dsa; |
| | | 157 | | //if (LocalAppContextSwitches.DisableCngCertificates) |
| | | 158 | | { |
| | 0 | 159 | | dsa = certificate.PublicKey.Key as DSA; |
| | | 160 | | } |
| | | 161 | | //else |
| | | 162 | | //{ |
| | | 163 | | // dsa = CngLightup.GetDSAPublicKey(certificate); |
| | | 164 | | //} |
| | 0 | 165 | | if (dsa != null) |
| | | 166 | | { |
| | 0 | 167 | | claimsCollection.Add(new Claim(ClaimTypes.Dsa, dsa.ToXmlString(false), ClaimValueTypes.DsaKeyValue, issu |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | value = certificate.SerialNumber; |
| | 0 | 171 | | if (!string.IsNullOrEmpty(value)) |
| | | 172 | | { |
| | 0 | 173 | | claimsCollection.Add(new Claim(ClaimTypes.SerialNumber, value, ClaimValueTypes.String, issuer)); |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | return claimsCollection; |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | } |