| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Security.Cryptography; |
| | 5 | | using System.Threading; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 9 | | { |
| | 10 | | internal class RsaCryptographyProvider : LocalCryptographyProvider |
| | 11 | | { |
| 108 | 12 | | internal RsaCryptographyProvider(KeyVaultKey key) : base(key) |
| | 13 | | { |
| 108 | 14 | | } |
| | 15 | |
|
| | 16 | | public override bool SupportsOperation(KeyOperation operation) |
| | 17 | | { |
| 116 | 18 | | if (KeyMaterial != null) |
| | 19 | | { |
| 116 | 20 | | if (operation == KeyOperation.Encrypt || operation == KeyOperation.Decrypt || operation == KeyOperation. |
| | 21 | | { |
| 116 | 22 | | return KeyMaterial.SupportsOperation(operation); |
| | 23 | | } |
| | 24 | | } |
| | 25 | |
|
| 0 | 26 | | return false; |
| | 27 | | } |
| | 28 | |
|
| | 29 | | public override EncryptResult Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToken cancell |
| | 30 | | { |
| 8 | 31 | | Argument.AssertNotNull(plaintext, nameof(plaintext)); |
| | 32 | |
|
| 8 | 33 | | ThrowIfTimeInvalid(); |
| | 34 | |
|
| 4 | 35 | | RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding(); |
| 4 | 36 | | if (padding is null) |
| | 37 | | { |
| 4 | 38 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Encrypt), algorithm); |
| 4 | 39 | | return null; |
| | 40 | | } |
| | 41 | |
|
| 0 | 42 | | byte[] ciphertext = Encrypt(plaintext, padding); |
| 0 | 43 | | EncryptResult result = null; |
| | 44 | |
|
| 0 | 45 | | if (ciphertext != null) |
| | 46 | | { |
| 0 | 47 | | result = new EncryptResult |
| 0 | 48 | | { |
| 0 | 49 | | Algorithm = algorithm, |
| 0 | 50 | | Ciphertext = ciphertext, |
| 0 | 51 | | KeyId = KeyMaterial.Id, |
| 0 | 52 | | }; |
| | 53 | | } |
| | 54 | |
|
| 0 | 55 | | return result; |
| | 56 | | } |
| | 57 | |
|
| | 58 | | public override DecryptResult Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationToken cancel |
| | 59 | | { |
| 8 | 60 | | Argument.AssertNotNull(ciphertext, nameof(ciphertext)); |
| | 61 | |
|
| 8 | 62 | | if (MustRemote) |
| | 63 | | { |
| | 64 | | // A private key is required to decrypt. Send to the server. |
| 4 | 65 | | KeysEventSource.Singleton.PrivateKeyRequired(nameof(Decrypt)); |
| 4 | 66 | | return null; |
| | 67 | | } |
| | 68 | |
|
| 4 | 69 | | RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding(); |
| 4 | 70 | | if (padding is null) |
| | 71 | | { |
| 4 | 72 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Decrypt), algorithm); |
| 4 | 73 | | return null; |
| | 74 | | } |
| | 75 | |
|
| 0 | 76 | | byte[] plaintext = Decrypt(ciphertext, padding); |
| 0 | 77 | | DecryptResult result = null; |
| | 78 | |
|
| 0 | 79 | | if (plaintext != null) |
| | 80 | | { |
| 0 | 81 | | result = new DecryptResult |
| 0 | 82 | | { |
| 0 | 83 | | Algorithm = algorithm, |
| 0 | 84 | | KeyId = KeyMaterial.Id, |
| 0 | 85 | | Plaintext = plaintext, |
| 0 | 86 | | }; |
| | 87 | | } |
| | 88 | |
|
| 0 | 89 | | return result; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | public override SignResult Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellationToken |
| | 93 | | { |
| 60 | 94 | | Argument.AssertNotNull(digest, nameof(digest)); |
| | 95 | |
|
| 60 | 96 | | ThrowIfTimeInvalid(); |
| | 97 | |
|
| | 98 | | // A private key is required to sign. Send to the server. |
| 56 | 99 | | if (MustRemote) |
| | 100 | | { |
| 28 | 101 | | KeysEventSource.Singleton.PrivateKeyRequired(nameof(Sign)); |
| 28 | 102 | | return null; |
| | 103 | | } |
| | 104 | |
|
| 28 | 105 | | HashAlgorithmName hashAlgorithm = algorithm.GetHashAlgorithmName(); |
| 28 | 106 | | if (hashAlgorithm == default) |
| | 107 | | { |
| 4 | 108 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Sign), algorithm); |
| 4 | 109 | | return null; |
| | 110 | | } |
| | 111 | |
|
| 24 | 112 | | RSASignaturePadding padding = algorithm.GetRsaSignaturePadding(); |
| 24 | 113 | | if (padding is null) |
| | 114 | | { |
| 0 | 115 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Sign), algorithm); |
| 0 | 116 | | return null; |
| | 117 | | } |
| | 118 | |
|
| 24 | 119 | | using RSA rsa = KeyMaterial.ToRSA(true); |
| 24 | 120 | | byte[] signature = rsa.SignHash(digest, hashAlgorithm, padding); |
| | 121 | |
|
| 24 | 122 | | return new SignResult |
| 24 | 123 | | { |
| 24 | 124 | | Algorithm = algorithm, |
| 24 | 125 | | KeyId = KeyMaterial.Id, |
| 24 | 126 | | Signature = signature, |
| 24 | 127 | | }; |
| 24 | 128 | | } |
| | 129 | |
|
| | 130 | | public override VerifyResult Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, CancellationT |
| | 131 | | { |
| 28 | 132 | | Argument.AssertNotNull(digest, nameof(digest)); |
| 28 | 133 | | Argument.AssertNotNull(signature, nameof(signature)); |
| | 134 | |
|
| 28 | 135 | | HashAlgorithmName hashAlgorithm = algorithm.GetHashAlgorithmName(); |
| 28 | 136 | | if (hashAlgorithm == default) |
| | 137 | | { |
| 4 | 138 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Verify), algorithm); |
| 4 | 139 | | return null; |
| | 140 | | } |
| | 141 | |
|
| 24 | 142 | | RSASignaturePadding padding = algorithm.GetRsaSignaturePadding(); |
| 24 | 143 | | if (padding is null) |
| | 144 | | { |
| 0 | 145 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Verify), algorithm); |
| 0 | 146 | | return null; |
| | 147 | | } |
| | 148 | |
|
| 24 | 149 | | using RSA rsa = KeyMaterial.ToRSA(); |
| 24 | 150 | | bool isValid = rsa.VerifyHash(digest, signature, hashAlgorithm, padding); |
| | 151 | |
|
| 24 | 152 | | return new VerifyResult |
| 24 | 153 | | { |
| 24 | 154 | | Algorithm = algorithm, |
| 24 | 155 | | IsValid = isValid, |
| 24 | 156 | | KeyId = KeyMaterial.Id, |
| 24 | 157 | | }; |
| 24 | 158 | | } |
| | 159 | |
|
| | 160 | | public override WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken) |
| | 161 | | { |
| 12 | 162 | | Argument.AssertNotNull(key, nameof(key)); |
| | 163 | |
|
| 12 | 164 | | ThrowIfTimeInvalid(); |
| | 165 | |
|
| 8 | 166 | | RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding(); |
| 8 | 167 | | if (padding is null) |
| | 168 | | { |
| 4 | 169 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(WrapKey), algorithm); |
| 4 | 170 | | return null; |
| | 171 | | } |
| | 172 | |
|
| 4 | 173 | | byte[] encryptedKey = Encrypt(key, padding); |
| 4 | 174 | | WrapResult result = null; |
| | 175 | |
|
| 4 | 176 | | if (encryptedKey != null) |
| | 177 | | { |
| 4 | 178 | | result = new WrapResult |
| 4 | 179 | | { |
| 4 | 180 | | Algorithm = algorithm, |
| 4 | 181 | | EncryptedKey = encryptedKey, |
| 4 | 182 | | KeyId = KeyMaterial.Id, |
| 4 | 183 | | }; |
| | 184 | | } |
| | 185 | |
|
| 4 | 186 | | return result; |
| | 187 | | } |
| | 188 | |
|
| | 189 | | public override UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancel |
| | 190 | | { |
| 12 | 191 | | Argument.AssertNotNull(encryptedKey, nameof(encryptedKey)); |
| | 192 | |
|
| 12 | 193 | | if (MustRemote) |
| | 194 | | { |
| | 195 | | // A private key is required to decrypt. Send to the server. |
| 8 | 196 | | KeysEventSource.Singleton.PrivateKeyRequired(nameof(UnwrapKey)); |
| 8 | 197 | | return null; |
| | 198 | | } |
| | 199 | |
|
| 4 | 200 | | RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding(); |
| 4 | 201 | | if (padding is null) |
| | 202 | | { |
| 4 | 203 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(UnwrapKey), algorithm); |
| 4 | 204 | | return null; |
| | 205 | | } |
| | 206 | |
|
| 0 | 207 | | byte[] key = Decrypt(encryptedKey, padding); |
| 0 | 208 | | UnwrapResult result = null; |
| | 209 | |
|
| 0 | 210 | | if (key != null) |
| | 211 | | { |
| 0 | 212 | | result = new UnwrapResult |
| 0 | 213 | | { |
| 0 | 214 | | Algorithm = algorithm, |
| 0 | 215 | | Key = key, |
| 0 | 216 | | KeyId = KeyMaterial.Id, |
| 0 | 217 | | }; |
| | 218 | | } |
| | 219 | |
|
| 0 | 220 | | return result; |
| | 221 | | } |
| | 222 | |
|
| | 223 | | private byte[] Encrypt(byte[] data, RSAEncryptionPadding padding) |
| | 224 | | { |
| 4 | 225 | | using RSA rsa = KeyMaterial.ToRSA(true); |
| 4 | 226 | | return rsa.Encrypt(data, padding); |
| 4 | 227 | | } |
| | 228 | |
|
| | 229 | | private byte[] Decrypt(byte[] data, RSAEncryptionPadding padding) |
| | 230 | | { |
| 0 | 231 | | using RSA rsa = KeyMaterial.ToRSA(); |
| 0 | 232 | | return rsa.Decrypt(data, padding); |
| 0 | 233 | | } |
| | 234 | | } |
| | 235 | | } |