| | | 1 | | // |
| | | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 3 | | // Licensed under the MIT License. See License.txt in the project root for |
| | | 4 | | // license information. |
| | | 5 | | // |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Security.Cryptography; |
| | | 9 | | using System.Threading; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | using Microsoft.Azure.KeyVault.Core; |
| | | 12 | | using Microsoft.Azure.KeyVault.Cryptography; |
| | | 13 | | using Microsoft.Azure.KeyVault.Cryptography.Algorithms; |
| | | 14 | | |
| | | 15 | | #if NETSTANDARD |
| | | 16 | | using TaskException = System.Threading.Tasks.Task; |
| | | 17 | | #endif |
| | | 18 | | |
| | | 19 | | namespace Microsoft.Azure.KeyVault |
| | | 20 | | { |
| | | 21 | | /// <summary> |
| | | 22 | | /// A simple Symmetric Key |
| | | 23 | | /// </summary> |
| | | 24 | | public class SymmetricKey : IKey |
| | | 25 | | { |
| | | 26 | | public const int KeySize128 = 128 >> 3; |
| | | 27 | | public const int KeySize192 = 192 >> 3; |
| | | 28 | | public const int KeySize256 = 256 >> 3; |
| | | 29 | | public const int KeySize384 = 384 >> 3; |
| | | 30 | | public const int KeySize512 = 512 >> 3; |
| | | 31 | | |
| | 0 | 32 | | private static readonly int DefaultKeySize = KeySize256; |
| | 0 | 33 | | private static readonly RandomNumberGenerator Rng = RandomNumberGenerator.Create(); |
| | | 34 | | |
| | | 35 | | private byte[] _key; |
| | | 36 | | private bool _isDisposed; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Default constructor |
| | | 40 | | /// </summary> |
| | | 41 | | public SymmetricKey() |
| | 0 | 42 | | : this( Guid.NewGuid().ToString( "N" ), DefaultKeySize ) |
| | | 43 | | { |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Constructor. |
| | | 48 | | /// </summary> |
| | | 49 | | /// <param name="kid">The key identifier to use</param> |
| | | 50 | | public SymmetricKey( string kid ) |
| | 0 | 51 | | : this( kid, DefaultKeySize ) |
| | | 52 | | { |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Constructor |
| | | 57 | | /// </summary> |
| | | 58 | | /// <param name="kid">The key identifier to use</param> |
| | | 59 | | /// <param name="keySize">The key size in bytes</param> |
| | 0 | 60 | | public SymmetricKey( string kid, int keySize ) |
| | | 61 | | { |
| | 0 | 62 | | if ( string.IsNullOrWhiteSpace( kid ) ) |
| | 0 | 63 | | throw new ArgumentNullException( "kid" ); |
| | | 64 | | |
| | 0 | 65 | | if ( keySize != KeySize128 && keySize != KeySize192 && keySize != KeySize256 && keySize != KeySize384 && key |
| | 0 | 66 | | throw new ArgumentOutOfRangeException( "keySize", "The key size must be 128, 192, 256, 384 or 512 bits o |
| | | 67 | | |
| | 0 | 68 | | Kid = kid; |
| | 0 | 69 | | _key = new byte[keySize]; |
| | | 70 | | |
| | 0 | 71 | | Rng.GetBytes( _key ); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Constructor |
| | | 76 | | /// </summary> |
| | | 77 | | /// <param name="kid">The key identifier</param> |
| | | 78 | | /// <param name="keyBytes">The key material</param> |
| | 36 | 79 | | public SymmetricKey( string kid, byte[] keyBytes ) |
| | | 80 | | { |
| | 36 | 81 | | if ( string.IsNullOrWhiteSpace( kid ) ) |
| | 0 | 82 | | throw new ArgumentNullException( "kid" ); |
| | | 83 | | |
| | 36 | 84 | | if ( keyBytes == null ) |
| | 0 | 85 | | throw new ArgumentNullException( "keyBytes" ); |
| | | 86 | | |
| | 36 | 87 | | if ( keyBytes.Length != KeySize128 && keyBytes.Length != KeySize192 && keyBytes.Length != KeySize256 && keyB |
| | 0 | 88 | | throw new ArgumentOutOfRangeException( "keyBytes", "The key material must be 128, 192, 256, 384 or 512 b |
| | | 89 | | |
| | 36 | 90 | | Kid = kid; |
| | 36 | 91 | | _key = keyBytes; |
| | 36 | 92 | | } |
| | | 93 | | |
| | | 94 | | #region IKey Implementation |
| | | 95 | | |
| | 48 | 96 | | public string Kid { get; protected set; } |
| | | 97 | | |
| | | 98 | | public string DefaultEncryptionAlgorithm |
| | | 99 | | { |
| | | 100 | | get |
| | | 101 | | { |
| | 0 | 102 | | switch ( _key.Length ) |
| | | 103 | | { |
| | | 104 | | case KeySize128: |
| | 0 | 105 | | return Aes128Cbc.AlgorithmName; |
| | | 106 | | |
| | | 107 | | case KeySize192: |
| | 0 | 108 | | return Aes192Cbc.AlgorithmName; |
| | | 109 | | |
| | | 110 | | case KeySize256: |
| | 0 | 111 | | return Aes128CbcHmacSha256.AlgorithmName; |
| | | 112 | | |
| | | 113 | | case KeySize384: |
| | 0 | 114 | | return Aes192CbcHmacSha384.AlgorithmName; |
| | | 115 | | |
| | | 116 | | case KeySize512: |
| | 0 | 117 | | return Aes256CbcHmacSha512.AlgorithmName; |
| | | 118 | | } |
| | | 119 | | |
| | 0 | 120 | | return null; |
| | | 121 | | } |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public string DefaultKeyWrapAlgorithm |
| | | 125 | | { |
| | | 126 | | get |
| | | 127 | | { |
| | 0 | 128 | | switch ( _key.Length ) |
| | | 129 | | { |
| | | 130 | | case KeySize128: |
| | 0 | 131 | | return AesKw128.AlgorithmName; |
| | | 132 | | |
| | | 133 | | case KeySize192: |
| | 0 | 134 | | return AesKw192.AlgorithmName; |
| | | 135 | | |
| | | 136 | | case KeySize256: |
| | 0 | 137 | | return AesKw256.AlgorithmName; |
| | | 138 | | |
| | | 139 | | case KeySize384: |
| | | 140 | | // Default to longest allowed key length for wrap |
| | 0 | 141 | | return AesKw256.AlgorithmName; |
| | | 142 | | |
| | | 143 | | case KeySize512: |
| | | 144 | | // Default to longest allowed key length for wrap |
| | 0 | 145 | | return AesKw256.AlgorithmName; |
| | | 146 | | } |
| | | 147 | | |
| | 0 | 148 | | return null; |
| | | 149 | | } |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | public string DefaultSignatureAlgorithm |
| | | 153 | | { |
| | 0 | 154 | | get { return null; } |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | |
| | | 158 | | public Task<byte[]> DecryptAsync( byte[] ciphertext, byte[] iv, byte[] authenticationData = null, byte[] authent |
| | | 159 | | { |
| | 0 | 160 | | if ( _isDisposed ) |
| | 0 | 161 | | throw new ObjectDisposedException( string.Format( "SymmetricKey {0} is disposed", Kid ) ); |
| | | 162 | | |
| | 0 | 163 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| | 0 | 164 | | algorithm = DefaultEncryptionAlgorithm; |
| | | 165 | | |
| | 0 | 166 | | if ( ciphertext == null ) |
| | 0 | 167 | | throw new ArgumentNullException( "ciphertext" ); |
| | | 168 | | |
| | 0 | 169 | | if ( iv == null ) |
| | 0 | 170 | | throw new ArgumentNullException( "iv" ); |
| | | 171 | | |
| | 0 | 172 | | var algo = AlgorithmResolver.Default[algorithm] as SymmetricEncryptionAlgorithm; |
| | | 173 | | |
| | 0 | 174 | | if ( algo == null ) |
| | 0 | 175 | | throw new NotSupportedException( algorithm ); |
| | | 176 | | |
| | | 177 | | try |
| | | 178 | | { |
| | 0 | 179 | | using ( var encryptor = algo.CreateDecryptor( _key, iv, authenticationData, authenticationTag ) ) |
| | | 180 | | { |
| | 0 | 181 | | return Task.FromResult( encryptor.TransformFinalBlock( ciphertext, 0, ciphertext.Length ) ); |
| | | 182 | | } |
| | | 183 | | } |
| | | 184 | | catch ( Exception ex ) |
| | | 185 | | { |
| | 0 | 186 | | return TaskException.FromException<byte[]>( ex ); |
| | | 187 | | } |
| | 0 | 188 | | } |
| | | 189 | | |
| | | 190 | | public Task<Tuple<byte[], byte[], string>> EncryptAsync( byte[] plaintext, byte[] iv, byte[] authenticationData |
| | | 191 | | { |
| | 0 | 192 | | if ( _isDisposed ) |
| | 0 | 193 | | throw new ObjectDisposedException( string.Format( "SymmetricKey {0} is disposed", Kid ) ); |
| | | 194 | | |
| | 0 | 195 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| | 0 | 196 | | algorithm = DefaultEncryptionAlgorithm; |
| | | 197 | | |
| | 0 | 198 | | if ( plaintext == null ) |
| | 0 | 199 | | throw new ArgumentNullException( "plaintext" ); |
| | | 200 | | |
| | 0 | 201 | | if ( iv == null ) |
| | 0 | 202 | | throw new ArgumentNullException( "iv" ); |
| | | 203 | | |
| | 0 | 204 | | var algo = AlgorithmResolver.Default[algorithm] as SymmetricEncryptionAlgorithm; |
| | | 205 | | |
| | 0 | 206 | | if ( algo == null ) |
| | 0 | 207 | | throw new NotSupportedException( algorithm ); |
| | | 208 | | |
| | | 209 | | try |
| | | 210 | | { |
| | 0 | 211 | | using ( var encryptor = algo.CreateEncryptor( _key, iv, authenticationData ) ) |
| | | 212 | | { |
| | 0 | 213 | | var cipherText = encryptor.TransformFinalBlock( plaintext, 0, plaintext.Length ); |
| | 0 | 214 | | byte[] authenticationTag = null; |
| | 0 | 215 | | var transform = encryptor as IAuthenticatedCryptoTransform; |
| | | 216 | | |
| | 0 | 217 | | if ( transform != null ) |
| | | 218 | | { |
| | 0 | 219 | | authenticationTag = transform.Tag.Clone() as byte[]; |
| | | 220 | | } |
| | | 221 | | |
| | 0 | 222 | | var result = new Tuple<byte[], byte[], string>( cipherText, authenticationTag, algorithm ); |
| | | 223 | | |
| | 0 | 224 | | return Task.FromResult( result ); |
| | | 225 | | } |
| | | 226 | | } |
| | | 227 | | catch ( Exception ex ) |
| | | 228 | | { |
| | 0 | 229 | | return TaskException.FromException<Tuple<byte[], byte[], string>>( ex ); |
| | | 230 | | } |
| | 0 | 231 | | } |
| | | 232 | | |
| | | 233 | | public Task<Tuple<byte[], string>> WrapKeyAsync( byte[] key, string algorithm = null, CancellationToken token = |
| | | 234 | | { |
| | 12 | 235 | | if ( _isDisposed ) |
| | 0 | 236 | | throw new ObjectDisposedException( string.Format( "SymmetricKey {0} is disposed", Kid ) ); |
| | | 237 | | |
| | 12 | 238 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| | 0 | 239 | | algorithm = DefaultKeyWrapAlgorithm; |
| | | 240 | | |
| | 12 | 241 | | if ( key == null || key.Length == 0 ) |
| | 0 | 242 | | throw new ArgumentNullException( "key" ); |
| | | 243 | | |
| | 12 | 244 | | var algo = AlgorithmResolver.Default[algorithm] as KeyWrapAlgorithm; |
| | | 245 | | |
| | 12 | 246 | | if ( algo == null ) |
| | 0 | 247 | | throw new NotSupportedException( algorithm ); |
| | | 248 | | |
| | | 249 | | try |
| | | 250 | | { |
| | 12 | 251 | | using ( var encryptor = algo.CreateEncryptor( _key, null ) ) |
| | | 252 | | { |
| | 12 | 253 | | var result = new Tuple<byte[], string>( encryptor.TransformFinalBlock( key, 0, key.Length ), alg |
| | | 254 | | |
| | 12 | 255 | | return Task.FromResult( result ); |
| | | 256 | | } |
| | | 257 | | } |
| | | 258 | | catch ( Exception ex ) |
| | | 259 | | { |
| | 0 | 260 | | return TaskException.FromException<Tuple<byte[], string>>( ex ); |
| | | 261 | | } |
| | 12 | 262 | | } |
| | | 263 | | |
| | | 264 | | public Task<byte[]> UnwrapKeyAsync( byte[] encryptedKey, string algorithm = null, CancellationToken token = defa |
| | | 265 | | { |
| | 12 | 266 | | if ( _isDisposed ) |
| | 0 | 267 | | throw new ObjectDisposedException( string.Format( "SymmetricKey {0} is disposed", Kid ) ); |
| | | 268 | | |
| | 12 | 269 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| | 0 | 270 | | algorithm = DefaultKeyWrapAlgorithm; |
| | | 271 | | |
| | 12 | 272 | | if ( encryptedKey == null || encryptedKey.Length == 0 ) |
| | 0 | 273 | | throw new ArgumentNullException( "encryptedKey" ); |
| | | 274 | | |
| | 12 | 275 | | var algo = AlgorithmResolver.Default[algorithm] as KeyWrapAlgorithm; |
| | | 276 | | |
| | 12 | 277 | | if ( algo == null ) |
| | 0 | 278 | | throw new NotSupportedException( algorithm ); |
| | | 279 | | |
| | | 280 | | try |
| | | 281 | | { |
| | 12 | 282 | | using ( var encryptor = algo.CreateDecryptor( _key, null ) ) |
| | | 283 | | { |
| | 12 | 284 | | var result = encryptor.TransformFinalBlock( encryptedKey, 0, encryptedKey.Length ); |
| | | 285 | | |
| | 12 | 286 | | return Task.FromResult( result ); |
| | | 287 | | } |
| | | 288 | | } |
| | | 289 | | catch ( Exception ex ) |
| | | 290 | | { |
| | 0 | 291 | | return TaskException.FromException<byte[]>( ex ); |
| | | 292 | | } |
| | 12 | 293 | | } |
| | | 294 | | |
| | | 295 | | public Task<Tuple<byte[], string>> SignAsync( byte[] digest, string algorithm = null, CancellationToken token = |
| | | 296 | | { |
| | 0 | 297 | | return TaskException.FromException<Tuple<byte[], string>>( new NotImplementedException() ); |
| | | 298 | | } |
| | | 299 | | |
| | | 300 | | public Task<bool> VerifyAsync( byte[] digest, byte[] signature, string algorithm = null, CancellationToken token |
| | | 301 | | { |
| | 0 | 302 | | return TaskException.FromException<bool>( new NotImplementedException() ); |
| | | 303 | | } |
| | | 304 | | |
| | | 305 | | #endregion |
| | | 306 | | |
| | | 307 | | public void Dispose() |
| | | 308 | | { |
| | 12 | 309 | | Dispose( true ); |
| | 12 | 310 | | GC.SuppressFinalize( this ); |
| | 12 | 311 | | } |
| | | 312 | | |
| | | 313 | | protected virtual void Dispose( bool disposing ) |
| | | 314 | | { |
| | 12 | 315 | | if ( disposing ) |
| | | 316 | | { |
| | 12 | 317 | | if ( !_isDisposed ) |
| | | 318 | | { |
| | 12 | 319 | | _isDisposed = true; |
| | 12 | 320 | | _key.Zero(); |
| | | 321 | | } |
| | | 322 | | } |
| | 12 | 323 | | } |
| | | 324 | | } |
| | | 325 | | } |