| | | 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.Globalization; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Security.Cryptography; |
| | | 10 | | using System.Text; |
| | | 11 | | using System.Xml; |
| | | 12 | | using CoreWCF.IdentityModel; |
| | | 13 | | using CoreWCF.IdentityModel.Tokens; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Security.Tokens |
| | | 16 | | { |
| | | 17 | | internal sealed class DerivedKeySecurityToken : SecurityToken |
| | | 18 | | { |
| | 0 | 19 | | private static readonly byte[] s_defaultLabel = new byte[] |
| | 0 | 20 | | { |
| | 0 | 21 | | (byte)'W', (byte)'S', (byte)'-', (byte)'S', (byte)'e', (byte)'c', (byte)'u', (byte)'r', (byte)'e', |
| | 0 | 22 | | (byte)'C', (byte)'o', (byte)'n', (byte)'v', (byte)'e', (byte)'r', (byte)'s', (byte)'a', (byte)'t', (byte |
| | 0 | 23 | | (byte)'W', (byte)'S', (byte)'-', (byte)'S', (byte)'e', (byte)'c', (byte)'u', (byte)'r', (byte)'e', |
| | 0 | 24 | | (byte)'C', (byte)'o', (byte)'n', (byte)'v', (byte)'e', (byte)'r', (byte)'s', (byte)'a', (byte)'t', (byte |
| | 0 | 25 | | }; |
| | | 26 | | |
| | | 27 | | public const int DefaultNonceLength = 16; |
| | | 28 | | public const int DefaultDerivedKeyLength = 32; |
| | | 29 | | private string _id; |
| | | 30 | | private byte[] _key; |
| | | 31 | | private ReadOnlyCollection<SecurityKey> _securityKeys; |
| | | 32 | | |
| | | 33 | | // create from scratch |
| | | 34 | | public DerivedKeySecurityToken(SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier, |
| | 0 | 35 | | : this(tokenToDerive, tokenToDeriveIdentifier, length, SecurityUtils.GenerateId()) |
| | | 36 | | { |
| | 0 | 37 | | } |
| | | 38 | | |
| | 0 | 39 | | internal DerivedKeySecurityToken(SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifie |
| | 0 | 40 | | int length, string id) |
| | | 41 | | { |
| | 0 | 42 | | if (length != 16 && length != 24 && length != 32) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Psha1KeyLen |
| | | 45 | | } |
| | | 46 | | |
| | 0 | 47 | | byte[] nonce = new byte[DefaultNonceLength]; |
| | 0 | 48 | | RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
| | 0 | 49 | | rng.GetBytes(nonce); |
| | | 50 | | |
| | 0 | 51 | | Initialize(id, -1, 0, length, null, nonce, tokenToDerive, tokenToDeriveIdentifier, SecurityAlgorithms.Psha1K |
| | 0 | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | internal DerivedKeySecurityToken(int generation, int offset, int length, |
| | 0 | 55 | | string label, int minNonceLength, SecurityToken tokenToDerive, |
| | 0 | 56 | | SecurityKeyIdentifierClause tokenToDeriveIdentifier, |
| | 0 | 57 | | string derivationAlgorithm, string id) |
| | | 58 | | { |
| | 0 | 59 | | byte[] nonce = new byte[minNonceLength]; |
| | 0 | 60 | | RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); |
| | 0 | 61 | | rng.GetBytes(nonce); |
| | | 62 | | |
| | 0 | 63 | | Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | // create from xml |
| | 0 | 67 | | internal DerivedKeySecurityToken(int generation, int offset, int length, |
| | 0 | 68 | | string label, byte[] nonce, SecurityToken tokenToDerive, |
| | 0 | 69 | | SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm, string id) |
| | | 70 | | { |
| | 0 | 71 | | Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA |
| | 0 | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | public override string Id => _id; |
| | | 75 | | |
| | 0 | 76 | | public override DateTime ValidFrom => TokenToDerive.ValidFrom; |
| | | 77 | | |
| | 0 | 78 | | public override DateTime ValidTo => TokenToDerive.ValidTo; |
| | | 79 | | |
| | 0 | 80 | | public string KeyDerivationAlgorithm { get; private set; } |
| | | 81 | | |
| | 0 | 82 | | public int Generation { get; private set; } = -1; |
| | | 83 | | |
| | 0 | 84 | | public string Label { get; private set; } |
| | | 85 | | |
| | 0 | 86 | | public int Length { get; private set; } = -1; |
| | | 87 | | |
| | 0 | 88 | | internal byte[] Nonce { get; private set; } |
| | | 89 | | |
| | 0 | 90 | | public int Offset { get; private set; } = -1; |
| | | 91 | | |
| | 0 | 92 | | internal SecurityToken TokenToDerive { get; private set; } |
| | | 93 | | |
| | 0 | 94 | | internal SecurityKeyIdentifierClause TokenToDeriveIdentifier { get; private set; } |
| | | 95 | | |
| | | 96 | | public override ReadOnlyCollection<SecurityKey> SecurityKeys |
| | | 97 | | { |
| | | 98 | | get |
| | | 99 | | { |
| | 0 | 100 | | if (_securityKeys == null) |
| | | 101 | | { |
| | 0 | 102 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.DerivedKe |
| | | 103 | | } |
| | 0 | 104 | | return _securityKeys; |
| | | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public byte[] GetKeyBytes() |
| | | 109 | | { |
| | 0 | 110 | | return SecurityUtils.CloneBuffer(_key); |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public byte[] GetNonce() |
| | | 114 | | { |
| | 0 | 115 | | return SecurityUtils.CloneBuffer(Nonce); |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | internal bool TryGetSecurityKeys(out ReadOnlyCollection<SecurityKey> keys) |
| | | 119 | | { |
| | 0 | 120 | | keys = _securityKeys; |
| | 0 | 121 | | return (keys != null); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public override string ToString() |
| | | 125 | | { |
| | 0 | 126 | | StringWriter writer = new StringWriter(CultureInfo.InvariantCulture); |
| | 0 | 127 | | writer.WriteLine("DerivedKeySecurityToken:"); |
| | 0 | 128 | | writer.WriteLine(" Generation: {0}", Generation); |
| | 0 | 129 | | writer.WriteLine(" Offset: {0}", Offset); |
| | 0 | 130 | | writer.WriteLine(" Length: {0}", Length); |
| | 0 | 131 | | writer.WriteLine(" Label: {0}", Label); |
| | 0 | 132 | | writer.WriteLine(" Nonce: {0}", Convert.ToBase64String(Nonce)); |
| | 0 | 133 | | writer.WriteLine(" TokenToDeriveFrom:"); |
| | 0 | 134 | | using (XmlTextWriter xmlWriter = new XmlTextWriter(writer)) |
| | | 135 | | { |
| | 0 | 136 | | xmlWriter.Formatting = Formatting.Indented; |
| | 0 | 137 | | SecurityStandardsManager.DefaultInstance.SecurityTokenSerializer.WriteKeyIdentifierClause(XmlDictionaryW |
| | 0 | 138 | | } |
| | 0 | 139 | | return writer.ToString(); |
| | | 140 | | } |
| | | 141 | | |
| | | 142 | | private void Initialize(string id, int generation, int offset, int length, string label, byte[] nonce, |
| | | 143 | | SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm |
| | | 144 | | { |
| | 0 | 145 | | Initialize(id, generation, offset, length, label, nonce, tokenToDerive, tokenToDeriveIdentifier, derivationA |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private void Initialize(string id, int generation, int offset, int length, string label, byte[] nonce, |
| | | 149 | | SecurityToken tokenToDerive, SecurityKeyIdentifierClause tokenToDeriveIdentifier, string derivationAlgorithm |
| | | 150 | | bool initializeDerivedKey) |
| | | 151 | | { |
| | 0 | 152 | | if (tokenToDerive == null) |
| | | 153 | | { |
| | 0 | 154 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenToDerive)); |
| | | 155 | | } |
| | | 156 | | |
| | 0 | 157 | | if (!SecurityUtils.IsSupportedAlgorithm(derivationAlgorithm, tokenToDerive)) |
| | | 158 | | { |
| | 0 | 159 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.DerivedKeyCannotDeriv |
| | | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | if (length == -1) |
| | | 163 | | { |
| | 0 | 164 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(length) |
| | | 165 | | } |
| | 0 | 166 | | if (offset == -1 && generation == -1) |
| | | 167 | | { |
| | 0 | 168 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyPosAndGenNotSpecified); |
| | | 169 | | } |
| | 0 | 170 | | if (offset >= 0 && generation >= 0) |
| | | 171 | | { |
| | 0 | 172 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyPosAndGenBothSpecified); |
| | | 173 | | } |
| | | 174 | | |
| | 0 | 175 | | _id = id ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id)); |
| | 0 | 176 | | Label = label; |
| | 0 | 177 | | Nonce = nonce ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(nonce)); |
| | 0 | 178 | | Length = length; |
| | 0 | 179 | | Offset = offset; |
| | 0 | 180 | | Generation = generation; |
| | 0 | 181 | | TokenToDerive = tokenToDerive; |
| | 0 | 182 | | TokenToDeriveIdentifier = tokenToDeriveIdentifier ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArg |
| | 0 | 183 | | KeyDerivationAlgorithm = derivationAlgorithm; |
| | | 184 | | |
| | 0 | 185 | | if (initializeDerivedKey) |
| | | 186 | | { |
| | 0 | 187 | | InitializeDerivedKey(Length); |
| | | 188 | | } |
| | 0 | 189 | | } |
| | | 190 | | |
| | | 191 | | internal void InitializeDerivedKey(int maxKeyLength) |
| | | 192 | | { |
| | 0 | 193 | | if (_key != null) |
| | | 194 | | { |
| | 0 | 195 | | return; |
| | | 196 | | } |
| | 0 | 197 | | if (Length > maxKeyLength) |
| | | 198 | | { |
| | 0 | 199 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.DerivedKeyLengthTooLong, Lengt |
| | | 200 | | } |
| | | 201 | | |
| | 0 | 202 | | _key = SecurityUtils.GenerateDerivedKey(TokenToDerive, KeyDerivationAlgorithm, |
| | 0 | 203 | | (Label != null ? Encoding.UTF8.GetBytes(Label) : s_defaultLabel), Nonce, Length * 8, |
| | 0 | 204 | | ((Offset >= 0) ? Offset : Generation * Length)); |
| | 0 | 205 | | if ((_key == null) || (_key.Length == 0)) |
| | | 206 | | { |
| | 0 | 207 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.DerivedKeyCannotDeriveFromSecret); |
| | | 208 | | } |
| | 0 | 209 | | List<SecurityKey> temp = new List<SecurityKey>(1) |
| | 0 | 210 | | { |
| | 0 | 211 | | new InMemorySymmetricSecurityKey(_key, false) |
| | 0 | 212 | | }; |
| | 0 | 213 | | _securityKeys = temp.AsReadOnly(); |
| | 0 | 214 | | } |
| | | 215 | | |
| | | 216 | | internal void InitializeDerivedKey(ReadOnlyCollection<SecurityKey> securityKeys) |
| | | 217 | | { |
| | 0 | 218 | | _key = ((SymmetricSecurityKey)securityKeys[0]).GetSymmetricKey(); |
| | 0 | 219 | | _securityKeys = securityKeys; |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | internal static void EnsureAcceptableOffset(int offset, int generation, int length, int maxOffset) |
| | | 223 | | { |
| | 0 | 224 | | if (offset != -1) |
| | | 225 | | { |
| | 0 | 226 | | if (offset > maxOffset) |
| | | 227 | | { |
| | 0 | 228 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 229 | | } |
| | | 230 | | } |
| | | 231 | | else |
| | | 232 | | { |
| | 0 | 233 | | int effectiveOffset = generation * length; |
| | 0 | 234 | | if ((effectiveOffset < generation && effectiveOffset < length) || effectiveOffset > maxOffset) |
| | | 235 | | { |
| | 0 | 236 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(S |
| | | 237 | | } |
| | | 238 | | } |
| | 0 | 239 | | } |
| | | 240 | | } |
| | | 241 | | } |