| | | 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.Security.Cryptography.X509Certificates; |
| | | 6 | | using CoreWCF.IdentityModel.Selectors; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Token Resolver that can resolve X509SecurityTokens against a given X.509 Certificate Store. |
| | | 12 | | /// </summary> |
| | | 13 | | public class X509CertificateStoreTokenResolver : SecurityTokenResolver |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/> |
| | | 17 | | /// </summary> |
| | | 18 | | public X509CertificateStoreTokenResolver() |
| | 0 | 19 | | : this(System.Security.Cryptography.X509Certificates.StoreName.My, StoreLocation.LocalMachine) |
| | | 20 | | { |
| | 0 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/> |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="storeName">StoreName of the X.509 Certificate Store.</param> |
| | | 27 | | /// <param name="storeLocation">StoreLocation of the X.509 Certificate store.</param> |
| | | 28 | | public X509CertificateStoreTokenResolver(StoreName storeName, StoreLocation storeLocation) |
| | 2 | 29 | | : this(Enum.GetName(typeof(System.Security.Cryptography.X509Certificates.StoreName), storeName), storeLocati |
| | | 30 | | { |
| | 2 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Initializes an instance of <see cref="X509CertificateStoreTokenResolver"/> |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="storeName">StoreName of the X.509 Certificate Store.</param> |
| | | 37 | | /// <param name="storeLocation">StoreLocation of the X.509 Certificate store.</param> |
| | 2 | 38 | | public X509CertificateStoreTokenResolver(string storeName, StoreLocation storeLocation) |
| | | 39 | | { |
| | 2 | 40 | | if (string.IsNullOrEmpty(storeName)) |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(storeName)); |
| | | 43 | | } |
| | | 44 | | |
| | 2 | 45 | | StoreName = storeName; |
| | 2 | 46 | | StoreLocation = storeLocation; |
| | 2 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Gets the StoreName used by this TokenResolver. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public string StoreName { get; } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the StoreLocation used by this TokenResolver. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public StoreLocation StoreLocation { get; } |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Resolves the given SecurityKeyIdentifierClause to a SecurityKey. |
| | | 61 | | /// </summary> |
| | | 62 | | /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to resolve</param> |
| | | 63 | | /// <param name="key">The resolved SecurityKey.</param> |
| | | 64 | | /// <returns>True if successfully resolved.</returns> |
| | | 65 | | /// <exception cref="ArgumentNullException">The input argument 'keyIdentifierClause' is null.</exception> |
| | | 66 | | protected override bool TryResolveSecurityKeyCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityK |
| | | 67 | | { |
| | 0 | 68 | | if (keyIdentifierClause == null) |
| | | 69 | | { |
| | 0 | 70 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | key = null; |
| | 0 | 74 | | if (keyIdentifierClause is EncryptedKeyIdentifierClause encryptedKeyIdentifierClause) |
| | | 75 | | { |
| | 0 | 76 | | SecurityKeyIdentifier keyIdentifier = encryptedKeyIdentifierClause.EncryptingKeyIdentifier; |
| | 0 | 77 | | if (keyIdentifier != null && keyIdentifier.Count > 0) |
| | | 78 | | { |
| | 0 | 79 | | for (int i = 0; i < keyIdentifier.Count; i++) |
| | | 80 | | { |
| | 0 | 81 | | if (TryResolveSecurityKey(keyIdentifier[i], out SecurityKey unwrappingSecurityKey)) |
| | | 82 | | { |
| | 0 | 83 | | byte[] wrappedKey = encryptedKeyIdentifierClause.GetEncryptedKey(); |
| | 0 | 84 | | string wrappingAlgorithm = encryptedKeyIdentifierClause.EncryptionMethod; |
| | 0 | 85 | | byte[] unwrappedKey = unwrappingSecurityKey.DecryptKey(wrappingAlgorithm, wrappedKey); |
| | 0 | 86 | | key = new InMemorySymmetricSecurityKey(unwrappedKey, false); |
| | 0 | 87 | | return true; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 0 | 94 | | SecurityToken token = null; |
| | 0 | 95 | | if (TryResolveToken(keyIdentifierClause, out token)) |
| | | 96 | | { |
| | 0 | 97 | | if (token.SecurityKeys.Count > 0) |
| | | 98 | | { |
| | 0 | 99 | | key = token.SecurityKeys[0]; |
| | 0 | 100 | | return true; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Resolves the given SecurityKeyIdentifier to a SecurityToken. |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="keyIdentifier">SecurityKeyIdentifier to resolve.</param> |
| | | 112 | | /// <param name="token">The resolved SecurityToken.</param> |
| | | 113 | | /// <returns>True if successfully resolved.</returns> |
| | | 114 | | /// <exception cref="ArgumentNullException">The input argument 'keyIdentifier' is null.</exception> |
| | | 115 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifier keyIdentifier, out SecurityToken token) |
| | | 116 | | { |
| | 0 | 117 | | if (keyIdentifier == null) |
| | | 118 | | { |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier)); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | token = null; |
| | 0 | 123 | | foreach (SecurityKeyIdentifierClause clause in keyIdentifier) |
| | | 124 | | { |
| | 0 | 125 | | if (TryResolveToken(clause, out token)) |
| | | 126 | | { |
| | 0 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | return false; |
| | 0 | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Resolves the given SecurityKeyIdentifierClause to a SecurityToken. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="keyIdentifierClause">SecurityKeyIdentifierClause to resolve.</param> |
| | | 138 | | /// <param name="token">The resolved SecurityToken.</param> |
| | | 139 | | /// <returns>True if successfully resolved.</returns> |
| | | 140 | | /// <exception cref="ArgumentNullException">The input argument 'keyIdentifierClause' is null.</exception> |
| | | 141 | | protected override bool TryResolveTokenCore(SecurityKeyIdentifierClause keyIdentifierClause, out SecurityToken t |
| | | 142 | | { |
| | 0 | 143 | | if (keyIdentifierClause == null) |
| | | 144 | | { |
| | 0 | 145 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifierClause)); |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | token = null; |
| | 0 | 149 | | X509Store store = null; |
| | 0 | 150 | | X509Certificate2Collection certs = null; |
| | | 151 | | try |
| | | 152 | | { |
| | 0 | 153 | | store = new X509Store(StoreName, StoreLocation); |
| | 0 | 154 | | store.Open(OpenFlags.ReadOnly); |
| | 0 | 155 | | certs = store.Certificates; |
| | 0 | 156 | | foreach (X509Certificate2 cert in certs) |
| | | 157 | | { |
| | 0 | 158 | | if (keyIdentifierClause is X509ThumbprintKeyIdentifierClause thumbprintKeyIdentifierClause && thumbp |
| | | 159 | | { |
| | 0 | 160 | | token = new X509SecurityToken(cert); |
| | 0 | 161 | | return true; |
| | | 162 | | } |
| | | 163 | | |
| | 0 | 164 | | if (keyIdentifierClause is X509IssuerSerialKeyIdentifierClause issuerSerialKeyIdentifierClause && is |
| | | 165 | | { |
| | 0 | 166 | | token = new X509SecurityToken(cert); |
| | 0 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | if (keyIdentifierClause is X509SubjectKeyIdentifierClause subjectKeyIdentifierClause && subjectKeyId |
| | | 171 | | { |
| | 0 | 172 | | token = new X509SecurityToken(cert); |
| | 0 | 173 | | return true; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | if (keyIdentifierClause is X509RawDataKeyIdentifierClause rawDataKeyIdentifierClause && rawDataKeyId |
| | | 177 | | { |
| | 0 | 178 | | token = new X509SecurityToken(cert); |
| | 0 | 179 | | return true; |
| | | 180 | | } |
| | | 181 | | } |
| | 0 | 182 | | } |
| | | 183 | | finally |
| | | 184 | | { |
| | 0 | 185 | | if (certs != null) |
| | | 186 | | { |
| | 0 | 187 | | for (int i = 0; i < certs.Count; i++) |
| | | 188 | | { |
| | 0 | 189 | | certs[i].Reset(); |
| | | 190 | | } |
| | | 191 | | } |
| | | 192 | | |
| | 0 | 193 | | if (store != null) |
| | | 194 | | { |
| | 0 | 195 | | store.Close(); |
| | | 196 | | } |
| | 0 | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | return false; |
| | 0 | 200 | | } |
| | | 201 | | } |
| | | 202 | | } |