| | | 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.ObjectModel; |
| | | 6 | | using System.Security.Cryptography; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.IdentityModel; |
| | | 9 | | using CoreWCF.IdentityModel.Tokens; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Security.Tokens |
| | | 12 | | { |
| | | 13 | | public class WrappedKeySecurityToken : SecurityToken |
| | | 14 | | { |
| | | 15 | | private readonly string _id; |
| | | 16 | | private readonly DateTime _effectiveTime; |
| | | 17 | | private readonly ReadOnlyCollection<SecurityKey> _securityKey; |
| | | 18 | | private readonly byte[] _wrappedKey; |
| | | 19 | | private readonly bool _serializeCarriedKeyName; |
| | | 20 | | private byte[] _wrappedKeyHash; |
| | | 21 | | private readonly XmlDictionaryString _wrappingAlgorithmDictionaryString; |
| | | 22 | | |
| | | 23 | | internal WrappedKeySecurityToken(string id, byte[] keyToWrap, ISspiNegotiation wrappingSspiContext) |
| | 0 | 24 | | : this(id, keyToWrap, (wrappingSspiContext != null) ? (wrappingSspiContext.KeyEncryptionAlgorithm) : null, w |
| | | 25 | | { |
| | | 26 | | |
| | 0 | 27 | | } |
| | | 28 | | |
| | | 29 | | internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, ISspiNegotiation wrappin |
| | 0 | 30 | | : this(id, keyToWrap, wrappingAlgorithm, null) |
| | | 31 | | { |
| | 0 | 32 | | if (wrappingSspiContext == null) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wrappingSspiContext)); |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | if (wrappedKey == null) |
| | | 38 | | { |
| | 0 | 39 | | _wrappedKey = wrappingSspiContext.Encrypt(keyToWrap); |
| | | 40 | | } |
| | | 41 | | else |
| | | 42 | | { |
| | 0 | 43 | | _wrappedKey = wrappedKey; |
| | | 44 | | } |
| | 0 | 45 | | _serializeCarriedKeyName = false; |
| | 0 | 46 | | } |
| | | 47 | | |
| | | 48 | | internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrap |
| | 0 | 49 | | : this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString, wrappingToken, wrappingTokenRefe |
| | | 50 | | { |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | internal WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrap |
| | 0 | 54 | | : this(id, keyToWrap, wrappingAlgorithm, wrappingAlgorithmDictionaryString) |
| | | 55 | | { |
| | 0 | 56 | | WrappingToken = wrappingToken ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(wra |
| | 0 | 57 | | WrappingTokenReference = wrappingTokenReference; |
| | 0 | 58 | | if (wrappedKey == null) |
| | | 59 | | { |
| | 0 | 60 | | _wrappedKey = SecurityUtils.EncryptKey(wrappingToken, wrappingAlgorithm, keyToWrap); |
| | | 61 | | } |
| | | 62 | | else |
| | | 63 | | { |
| | 0 | 64 | | _wrappedKey = wrappedKey; |
| | | 65 | | } |
| | 0 | 66 | | WrappingSecurityKey = wrappingSecurityKey; |
| | 0 | 67 | | _serializeCarriedKeyName = true; |
| | 0 | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | private WrappedKeySecurityToken(string id, byte[] keyToWrap, string wrappingAlgorithm, XmlDictionaryString wrapp |
| | | 71 | | { |
| | 0 | 72 | | if (keyToWrap == null) |
| | | 73 | | { |
| | 0 | 74 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyToWrap)); |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id)); |
| | 0 | 78 | | _effectiveTime = DateTime.UtcNow; |
| | 0 | 79 | | _securityKey = SecurityUtils.CreateSymmetricSecurityKeys(keyToWrap); |
| | 0 | 80 | | WrappingAlgorithm = wrappingAlgorithm ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(na |
| | 0 | 81 | | _wrappingAlgorithmDictionaryString = wrappingAlgorithmDictionaryString; |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | internal WrappedKeySecurityToken( |
| | | 85 | | string id, |
| | | 86 | | byte[] keyToWrap, |
| | | 87 | | string wrappingAlgorithm, |
| | | 88 | | SecurityToken wrappingToken, |
| | | 89 | | SecurityKeyIdentifier wrappingTokenReference, |
| | | 90 | | byte[] wrappedKey, |
| | | 91 | | SecurityKey wrappingSecurityKey) |
| | 0 | 92 | | : this(id, keyToWrap, wrappingAlgorithm, (XmlDictionaryString)null, wrappingToken, wrappingTokenReference, wra |
| | | 93 | | { |
| | 0 | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | public override string Id => _id; |
| | | 97 | | |
| | 0 | 98 | | public override DateTime ValidFrom => _effectiveTime; |
| | | 99 | | |
| | 0 | 100 | | public override DateTime ValidTo => DateTime.MaxValue; |
| | | 101 | | |
| | 0 | 102 | | internal EncryptedKey EncryptedKey { get; set; } |
| | | 103 | | |
| | 0 | 104 | | internal ReferenceList ReferenceList => EncryptedKey?.ReferenceList; |
| | | 105 | | |
| | 0 | 106 | | public string WrappingAlgorithm { get; } |
| | | 107 | | |
| | 0 | 108 | | internal SecurityKey WrappingSecurityKey { get; } |
| | | 109 | | |
| | 0 | 110 | | public SecurityToken WrappingToken { get; } |
| | | 111 | | |
| | 0 | 112 | | public SecurityKeyIdentifier WrappingTokenReference { get; } |
| | | 113 | | |
| | 0 | 114 | | internal string CarriedKeyName => null; |
| | | 115 | | |
| | 0 | 116 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys => _securityKey; |
| | | 117 | | |
| | | 118 | | internal byte[] GetHash() |
| | | 119 | | { |
| | 0 | 120 | | if (_wrappedKeyHash == null) |
| | | 121 | | { |
| | 0 | 122 | | EnsureEncryptedKeySetUp(); |
| | 0 | 123 | | using (HashAlgorithm hash = CryptoHelper.NewSha1HashAlgorithm()) |
| | | 124 | | { |
| | 0 | 125 | | _wrappedKeyHash = hash.ComputeHash(EncryptedKey.GetWrappedKey()); |
| | 0 | 126 | | } |
| | | 127 | | } |
| | 0 | 128 | | return _wrappedKeyHash; |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public byte[] GetWrappedKey() |
| | | 132 | | { |
| | 0 | 133 | | return SecurityUtils.CloneBuffer(_wrappedKey); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | internal void EnsureEncryptedKeySetUp() |
| | | 137 | | { |
| | 0 | 138 | | if (EncryptedKey == null) |
| | | 139 | | { |
| | 0 | 140 | | EncryptedKey ek = new EncryptedKey |
| | 0 | 141 | | { |
| | 0 | 142 | | Id = Id |
| | 0 | 143 | | }; |
| | 0 | 144 | | if (_serializeCarriedKeyName) |
| | | 145 | | { |
| | 0 | 146 | | ek.CarriedKeyName = CarriedKeyName; |
| | | 147 | | } |
| | | 148 | | else |
| | | 149 | | { |
| | 0 | 150 | | ek.CarriedKeyName = null; |
| | | 151 | | } |
| | 0 | 152 | | ek.EncryptionMethod = WrappingAlgorithm; |
| | 0 | 153 | | ek.EncryptionMethodDictionaryString = _wrappingAlgorithmDictionaryString; |
| | 0 | 154 | | ek.SetUpKeyWrap(_wrappedKey); |
| | 0 | 155 | | if (WrappingTokenReference != null) |
| | | 156 | | { |
| | 0 | 157 | | ek.KeyIdentifier = WrappingTokenReference; |
| | | 158 | | } |
| | 0 | 159 | | EncryptedKey = ek; |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | |
| | | 163 | | public override bool CanCreateKeyIdentifierClause<T>() |
| | | 164 | | { |
| | 0 | 165 | | if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause)) |
| | | 166 | | { |
| | 0 | 167 | | return true; |
| | | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | return base.CanCreateKeyIdentifierClause<T>(); |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | public override T CreateKeyIdentifierClause<T>() |
| | | 174 | | { |
| | 0 | 175 | | if (typeof(T) == typeof(EncryptedKeyHashIdentifierClause)) |
| | | 176 | | { |
| | 0 | 177 | | return new EncryptedKeyHashIdentifierClause(GetHash()) as T; |
| | | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | return base.CreateKeyIdentifierClause<T>(); |
| | | 181 | | } |
| | | 182 | | |
| | | 183 | | public override bool MatchesKeyIdentifierClause(SecurityKeyIdentifierClause keyIdentifierClause) |
| | | 184 | | { |
| | 0 | 185 | | if (keyIdentifierClause is EncryptedKeyHashIdentifierClause encKeyIdentifierClause) |
| | | 186 | | { |
| | 0 | 187 | | return encKeyIdentifierClause.Matches(GetHash()); |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | return base.MatchesKeyIdentifierClause(keyIdentifierClause); |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | } |