| | | 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; |
| | | 6 | | |
| | | 7 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 8 | | { |
| | | 9 | | internal class InMemorySymmetricSecurityKey : SymmetricSecurityKey |
| | | 10 | | { |
| | | 11 | | private readonly int _keySize; |
| | | 12 | | private readonly byte[] _symmetricKey; |
| | | 13 | | |
| | | 14 | | public InMemorySymmetricSecurityKey(byte[] symmetricKey) |
| | 2 | 15 | | : this(symmetricKey, true) |
| | | 16 | | { |
| | 2 | 17 | | } |
| | | 18 | | |
| | 42 | 19 | | public InMemorySymmetricSecurityKey(byte[] symmetricKey, bool cloneBuffer) |
| | | 20 | | { |
| | 42 | 21 | | if (symmetricKey == null) |
| | | 22 | | { |
| | 0 | 23 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(symmetricKey) |
| | | 24 | | } |
| | | 25 | | |
| | 42 | 26 | | if (symmetricKey.Length == 0) |
| | | 27 | | { |
| | 0 | 28 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format("SymmetricKeyL |
| | | 29 | | } |
| | 42 | 30 | | _keySize = symmetricKey.Length * 8; |
| | | 31 | | |
| | 42 | 32 | | if (cloneBuffer) |
| | | 33 | | { |
| | 2 | 34 | | _symmetricKey = new byte[symmetricKey.Length]; |
| | 2 | 35 | | Buffer.BlockCopy(symmetricKey, 0, _symmetricKey, 0, symmetricKey.Length); |
| | | 36 | | } |
| | | 37 | | else |
| | | 38 | | { |
| | 40 | 39 | | _symmetricKey = symmetricKey; |
| | | 40 | | } |
| | 40 | 41 | | } |
| | | 42 | | |
| | | 43 | | public override int KeySize |
| | | 44 | | { |
| | 40 | 45 | | get { return _keySize; } |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public override byte[] DecryptKey(string algorithm, byte[] keyData) |
| | | 49 | | { |
| | 0 | 50 | | return CryptoHelper.UnwrapKey(_symmetricKey, keyData, algorithm); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public override byte[] EncryptKey(string algorithm, byte[] keyData) |
| | | 54 | | { |
| | 0 | 55 | | return CryptoHelper.WrapKey(_symmetricKey, keyData, algorithm); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | public override byte[] GenerateDerivedKey(string algorithm, byte[] label, byte[] nonce, int derivedKeyLength, in |
| | | 59 | | { |
| | 0 | 60 | | return CryptoHelper.GenerateDerivedKey(_symmetricKey, algorithm, label, nonce, derivedKeyLength, offset); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | public override ICryptoTransform GetDecryptionTransform(string algorithm, byte[] iv) |
| | | 64 | | { |
| | 0 | 65 | | return CryptoHelper.CreateDecryptor(_symmetricKey, iv, algorithm); |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | public override ICryptoTransform GetEncryptionTransform(string algorithm, byte[] iv) |
| | | 69 | | { |
| | 0 | 70 | | return CryptoHelper.CreateEncryptor(_symmetricKey, iv, algorithm); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | public override int GetIVSize(string algorithm) |
| | | 74 | | { |
| | 0 | 75 | | return CryptoHelper.GetIVSize(algorithm); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | public override KeyedHashAlgorithm GetKeyedHashAlgorithm(string algorithm) |
| | | 79 | | { |
| | 20 | 80 | | return CryptoHelper.CreateKeyedHashAlgorithm(_symmetricKey, algorithm); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | public override SymmetricAlgorithm GetSymmetricAlgorithm(string algorithm) |
| | | 84 | | { |
| | 0 | 85 | | return CryptoHelper.GetSymmetricAlgorithm(_symmetricKey, algorithm); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | public override byte[] GetSymmetricKey() |
| | | 89 | | { |
| | 2 | 90 | | byte[] local = new byte[_symmetricKey.Length]; |
| | 2 | 91 | | Buffer.BlockCopy(_symmetricKey, 0, local, 0, _symmetricKey.Length); |
| | | 92 | | |
| | 2 | 93 | | return local; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | public override bool IsSupportedAlgorithm(string algorithm) |
| | | 97 | | { |
| | 20 | 98 | | return (CryptoHelper.IsSymmetricSupportedAlgorithm(algorithm, KeySize)); |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |