1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------
4 
5 namespace System.IdentityModel.Tokens
6 {
7     using System.IdentityModel.Selectors;
8     using System.Security.Cryptography;
9 
10     public class InMemorySymmetricSecurityKey : SymmetricSecurityKey
11     {
12         int keySize;
13         byte[] symmetricKey;
14 
InMemorySymmetricSecurityKey(byte[] symmetricKey)15         public InMemorySymmetricSecurityKey(byte[] symmetricKey)
16             : this(symmetricKey, true)
17         {
18         }
19 
InMemorySymmetricSecurityKey(byte[] symmetricKey, bool cloneBuffer)20         public InMemorySymmetricSecurityKey(byte[] symmetricKey, bool cloneBuffer)
21         {
22             if (symmetricKey == null)
23             {
24                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("symmetricKey"));
25             }
26 
27             if (symmetricKey.Length == 0)
28             {
29                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.SymmetricKeyLengthTooShort, symmetricKey.Length)));
30             }
31             this.keySize = symmetricKey.Length * 8;
32 
33             if (cloneBuffer)
34             {
35                 this.symmetricKey = new byte[symmetricKey.Length];
36                 Buffer.BlockCopy(symmetricKey, 0, this.symmetricKey, 0, symmetricKey.Length);
37             }
38             else
39             {
40                 this.symmetricKey = symmetricKey;
41             }
42         }
43 
44         public override int KeySize
45         {
46             get { return this.keySize; }
47         }
48 
DecryptKey(string algorithm, byte[] keyData)49         public override byte[] DecryptKey(string algorithm, byte[] keyData)
50         {
51             return CryptoHelper.UnwrapKey(this.symmetricKey, keyData, algorithm);
52         }
53 
EncryptKey(string algorithm, byte[] keyData)54         public override byte[] EncryptKey(string algorithm, byte[] keyData)
55         {
56             return CryptoHelper.WrapKey(this.symmetricKey, keyData, algorithm);
57         }
58 
GenerateDerivedKey(string algorithm, byte[] label, byte[] nonce, int derivedKeyLength, int offset)59         public override byte[] GenerateDerivedKey(string algorithm, byte[] label, byte[] nonce, int derivedKeyLength, int offset)
60         {
61             return CryptoHelper.GenerateDerivedKey(this.symmetricKey, algorithm, label, nonce, derivedKeyLength, offset);
62         }
63 
GetDecryptionTransform(string algorithm, byte[] iv)64         public override ICryptoTransform GetDecryptionTransform(string algorithm, byte[] iv)
65         {
66             return CryptoHelper.CreateDecryptor(this.symmetricKey, iv, algorithm);
67         }
68 
GetEncryptionTransform(string algorithm, byte[] iv)69         public override ICryptoTransform GetEncryptionTransform(string algorithm, byte[] iv)
70         {
71             return CryptoHelper.CreateEncryptor(this.symmetricKey, iv, algorithm);
72         }
73 
GetIVSize(string algorithm)74         public override int GetIVSize(string algorithm)
75         {
76             return CryptoHelper.GetIVSize(algorithm);
77         }
78 
GetKeyedHashAlgorithm(string algorithm)79         public override KeyedHashAlgorithm GetKeyedHashAlgorithm(string algorithm)
80         {
81             return CryptoHelper.CreateKeyedHashAlgorithm(this.symmetricKey, algorithm);
82         }
83 
GetSymmetricAlgorithm(string algorithm)84         public override SymmetricAlgorithm GetSymmetricAlgorithm(string algorithm)
85         {
86             return CryptoHelper.GetSymmetricAlgorithm(this.symmetricKey, algorithm);
87         }
88 
GetSymmetricKey()89         public override byte[] GetSymmetricKey()
90         {
91             byte[] local = new byte[this.symmetricKey.Length];
92             Buffer.BlockCopy(this.symmetricKey, 0, local, 0, this.symmetricKey.Length);
93 
94             return local;
95         }
96 
IsAsymmetricAlgorithm(string algorithm)97         public override bool IsAsymmetricAlgorithm(string algorithm)
98         {
99             return (CryptoHelper.IsAsymmetricAlgorithm(algorithm));
100         }
101 
IsSupportedAlgorithm(string algorithm)102         public override bool IsSupportedAlgorithm(string algorithm)
103         {
104             return (CryptoHelper.IsSymmetricSupportedAlgorithm(algorithm, this.KeySize));
105         }
106 
IsSymmetricAlgorithm(string algorithm)107         public override bool IsSymmetricAlgorithm(string algorithm)
108         {
109             return CryptoHelper.IsSymmetricAlgorithm(algorithm);
110         }
111     }
112 }
113