| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Microsoft.Azure.KeyVault.Core; |
| | 10 | | using Microsoft.Azure.KeyVault.WebKey; |
| | 11 | | using Microsoft.Azure.KeyVault.Models; |
| | 12 | |
|
| | 13 | | namespace Microsoft.Azure.KeyVault |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// Key Vault key that performs cryptography operations at REST |
| | 17 | | /// </summary> |
| | 18 | | internal class KeyVaultKey : IKey |
| | 19 | | { |
| | 20 | | private readonly IKeyVaultClient _client; |
| | 21 | | private IKey _implementation; |
| | 22 | |
|
| 8 | 23 | | internal KeyVaultKey( IKeyVaultClient client, KeyBundle keyBundle ) |
| | 24 | | { |
| 8 | 25 | | switch ( keyBundle.Key.Kty ) |
| | 26 | | { |
| | 27 | | case JsonWebKeyType.Rsa: |
| 8 | 28 | | _implementation = new RsaKey( keyBundle.Key.Kid, keyBundle.Key.ToRSA() ); |
| 8 | 29 | | break; |
| | 30 | |
|
| | 31 | | case JsonWebKeyType.RsaHsm: |
| 0 | 32 | | _implementation = new RsaKey( keyBundle.Key.Kid, keyBundle.Key.ToRSA() ); |
| | 33 | | break; |
| | 34 | | } |
| | 35 | |
|
| 8 | 36 | | if ( _implementation == null ) |
| 0 | 37 | | throw new ArgumentException( string.Format( CultureInfo.InvariantCulture, "The key type \"{0}\" is not s |
| | 38 | |
|
| 8 | 39 | | _client = client; |
| 8 | 40 | | } |
| | 41 | |
|
| | 42 | | public string DefaultEncryptionAlgorithm |
| | 43 | | { |
| | 44 | | get |
| | 45 | | { |
| 0 | 46 | | if ( _implementation == null ) |
| 0 | 47 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 48 | |
|
| 0 | 49 | | return _implementation.DefaultEncryptionAlgorithm; |
| | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public string DefaultKeyWrapAlgorithm |
| | 54 | | { |
| | 55 | | get |
| | 56 | | { |
| 0 | 57 | | if ( _implementation == null ) |
| 0 | 58 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 59 | |
|
| 0 | 60 | | return _implementation.DefaultKeyWrapAlgorithm; |
| | 61 | | } |
| | 62 | | } |
| | 63 | |
|
| | 64 | | public string DefaultSignatureAlgorithm |
| | 65 | | { |
| | 66 | | get |
| | 67 | | { |
| 0 | 68 | | if ( _implementation == null ) |
| 0 | 69 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 70 | |
|
| 0 | 71 | | return _implementation.DefaultSignatureAlgorithm; |
| | 72 | | } |
| | 73 | | } |
| | 74 | |
|
| | 75 | | public string Kid |
| | 76 | | { |
| | 77 | | get |
| | 78 | | { |
| 8 | 79 | | if ( _implementation == null ) |
| 0 | 80 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 81 | |
|
| 8 | 82 | | return _implementation.Kid; |
| | 83 | | } |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public Task<byte[]> DecryptAsync( byte[] ciphertext, byte[] iv, byte[] authenticationData, byte[] authentication |
| | 87 | | { |
| 0 | 88 | | if ( _implementation == null ) |
| 0 | 89 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 90 | |
|
| 0 | 91 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| 0 | 92 | | algorithm = DefaultEncryptionAlgorithm; |
| | 93 | |
|
| | 94 | | // Never local |
| 0 | 95 | | return _client.DecryptAsync( _implementation.Kid, algorithm, ciphertext, token ) |
| 0 | 96 | | .ContinueWith( result => result.Result.Result, token ); |
| | 97 | | } |
| | 98 | |
|
| | 99 | | public Task<Tuple<byte[], byte[], string>> EncryptAsync( byte[] plaintext, byte[] iv, byte[] authenticationData, |
| | 100 | | { |
| 0 | 101 | | if ( _implementation == null ) |
| 0 | 102 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 103 | |
|
| 0 | 104 | | return _implementation.EncryptAsync( plaintext, iv, authenticationData, algorithm, token ); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | public Task<Tuple<byte[], string>> WrapKeyAsync( byte[] plaintext, string algorithm = null, CancellationToken to |
| | 108 | | { |
| 0 | 109 | | if ( _implementation == null ) |
| 0 | 110 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 111 | |
|
| 0 | 112 | | return _implementation.WrapKeyAsync( plaintext, algorithm, token ); |
| | 113 | | } |
| | 114 | |
|
| | 115 | | public Task<byte[]> UnwrapKeyAsync( byte[] ciphertext, string algorithm = null, CancellationToken token = defaul |
| | 116 | | { |
| 0 | 117 | | if ( _implementation == null ) |
| 0 | 118 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 119 | |
|
| 0 | 120 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| 0 | 121 | | algorithm = DefaultKeyWrapAlgorithm; |
| | 122 | |
|
| | 123 | | // Never local |
| 0 | 124 | | return _client.UnwrapKeyAsync( _implementation.Kid, algorithm, ciphertext, token ) |
| 0 | 125 | | .ContinueWith( result => result.Result.Result, token ); |
| | 126 | | } |
| | 127 | |
|
| | 128 | | public Task<Tuple<byte[], string>> SignAsync( byte[] digest, string algorithm = null, CancellationToken token = |
| | 129 | | { |
| 0 | 130 | | if ( _implementation == null ) |
| 0 | 131 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 132 | |
|
| 0 | 133 | | if ( string.IsNullOrWhiteSpace( algorithm ) ) |
| 0 | 134 | | algorithm = DefaultSignatureAlgorithm; |
| | 135 | |
|
| | 136 | | // Never local |
| 0 | 137 | | return _client.SignAsync( _implementation.Kid, algorithm, digest, token ) |
| 0 | 138 | | .ContinueWith( result => new Tuple<byte[], string>( result.Result.Result, algorithm ), token ); |
| | 139 | | } |
| | 140 | |
|
| | 141 | | public Task<bool> VerifyAsync( byte[] digest, byte[] signature, string algorithm = null, CancellationToken token |
| | 142 | | { |
| 0 | 143 | | if ( _implementation == null ) |
| 0 | 144 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 145 | |
|
| 0 | 146 | | return _implementation.VerifyAsync( digest, signature, algorithm, token ); |
| | 147 | | } |
| | 148 | |
|
| | 149 | | public void Dispose() |
| | 150 | | { |
| 4 | 151 | | if ( _implementation == null ) |
| 0 | 152 | | throw new ObjectDisposedException( "KeyVaultKey" ); |
| | 153 | |
|
| 4 | 154 | | Dispose( true ); |
| 4 | 155 | | GC.SuppressFinalize( this ); |
| 4 | 156 | | } |
| | 157 | |
|
| | 158 | | private void Dispose( bool disposing ) |
| | 159 | | { |
| 4 | 160 | | if ( disposing ) |
| | 161 | | { |
| 4 | 162 | | if ( _implementation != null ) |
| | 163 | | { |
| 4 | 164 | | _implementation.Dispose(); |
| 4 | 165 | | _implementation = null; |
| | 166 | | } |
| | 167 | | } |
| 4 | 168 | | } |
| | 169 | | } |
| | 170 | | } |