| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading; |
| | 6 | | using System.Threading.Tasks; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 9 | | { |
| | 10 | | internal abstract class LocalCryptographyProvider : ICryptographyProvider |
| | 11 | | { |
| | 12 | | private readonly KeyVaultKey _key; |
| | 13 | |
|
| 250 | 14 | | public LocalCryptographyProvider(KeyVaultKey key) |
| | 15 | | { |
| 250 | 16 | | _key = key ?? throw new ArgumentNullException(nameof(key)); |
| | 17 | |
|
| 250 | 18 | | KeyMaterial = key.Key; |
| 250 | 19 | | } |
| | 20 | |
|
| 134 | 21 | | public bool ShouldRemote => KeyMaterial?.Id != null; |
| | 22 | |
|
| 1318 | 23 | | protected JsonWebKey KeyMaterial { get; set; } |
| | 24 | |
|
| 134 | 25 | | protected bool MustRemote => ShouldRemote && !KeyMaterial.HasPrivateKey; |
| | 26 | |
|
| | 27 | | public abstract bool SupportsOperation(KeyOperation operation); |
| | 28 | |
|
| | 29 | | public virtual DecryptResult Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationToken cancell |
| | 30 | | { |
| 0 | 31 | | throw new NotSupportedException(); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | public virtual Task<DecryptResult> DecryptAsync(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationTo |
| | 35 | | { |
| 4 | 36 | | DecryptResult result = Decrypt(algorithm, ciphertext, cancellationToken); |
| 4 | 37 | | return Task.FromResult(result); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | public virtual EncryptResult Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToken cancella |
| | 41 | | { |
| 0 | 42 | | throw new NotSupportedException(); |
| | 43 | | } |
| | 44 | |
|
| | 45 | | public virtual Task<EncryptResult> EncryptAsync(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationTok |
| | 46 | | { |
| 2 | 47 | | EncryptResult result = Encrypt(algorithm, plaintext, cancellationToken); |
| 2 | 48 | | return Task.FromResult(result); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public virtual SignResult Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellationToken |
| | 52 | | { |
| 0 | 53 | | throw new NotSupportedException(); |
| | 54 | | } |
| | 55 | |
|
| | 56 | | public virtual Task<SignResult> SignAsync(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancell |
| | 57 | | { |
| 48 | 58 | | SignResult result = Sign(algorithm, digest, cancellationToken); |
| 48 | 59 | | return Task.FromResult(result); |
| | 60 | | } |
| | 61 | |
|
| | 62 | | public virtual UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancell |
| | 63 | | { |
| 0 | 64 | | throw new NotSupportedException(); |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public virtual Task<UnwrapResult> UnwrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationTo |
| | 68 | | { |
| 10 | 69 | | UnwrapResult result = UnwrapKey(algorithm, encryptedKey, cancellationToken); |
| 10 | 70 | | return Task.FromResult(result); |
| | 71 | | } |
| | 72 | |
|
| | 73 | | public virtual VerifyResult Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, CancellationTo |
| | 74 | | { |
| 0 | 75 | | throw new NotSupportedException(); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | public virtual Task<VerifyResult> VerifyAsync(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, Can |
| | 79 | | { |
| 24 | 80 | | VerifyResult result = Verify(algorithm, digest, signature, cancellationToken); |
| 24 | 81 | | return Task.FromResult(result); |
| | 82 | | } |
| | 83 | |
|
| | 84 | | public virtual WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken = |
| | 85 | | { |
| 0 | 86 | | throw new NotSupportedException(); |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public virtual Task<WrapResult> WrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellat |
| | 90 | | { |
| 8 | 91 | | WrapResult result = WrapKey(algorithm, key, cancellationToken); |
| 8 | 92 | | return Task.FromResult(result); |
| | 93 | | } |
| | 94 | |
|
| | 95 | | protected void ThrowIfTimeInvalid() |
| | 96 | | { |
| 160 | 97 | | DateTimeOffset now = DateTimeOffset.Now; |
| 160 | 98 | | if (_key.Properties.NotBefore.HasValue && now < _key.Properties.NotBefore.Value) |
| | 99 | | { |
| 10 | 100 | | throw new InvalidOperationException($"The key \"{_key.Name}\" is not valid before {_key.Properties.NotBe |
| | 101 | | } |
| | 102 | |
|
| 150 | 103 | | if (_key.Properties.ExpiresOn.HasValue && now > _key.Properties.ExpiresOn.Value) |
| | 104 | | { |
| 10 | 105 | | throw new InvalidOperationException($"The key \"{_key.Name}\" is not valid after {_key.Properties.Expire |
| | 106 | | } |
| 140 | 107 | | } |
| | 108 | | } |
| | 109 | | } |