| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using Azure.Core; |
| | 5 | | using Azure.Core.Cryptography; |
| | 6 | | using Azure.Core.Pipeline; |
| | 7 | | using System; |
| | 8 | | using System.IO; |
| | 9 | | using System.Runtime.InteropServices; |
| | 10 | | using System.Security.Cryptography; |
| | 11 | | using System.Threading; |
| | 12 | | using System.Threading.Tasks; |
| | 13 | |
|
| | 14 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 15 | | { |
| | 16 | | /// <summary> |
| | 17 | | /// A client used to perform cryptographic operations with Azure Key Vault keys. |
| | 18 | | /// </summary> |
| | 19 | | public class CryptographyClient : IKeyEncryptionKey |
| | 20 | | { |
| | 21 | | private readonly Uri _keyId; |
| | 22 | | private readonly KeyVaultPipeline _pipeline; |
| | 23 | | private readonly RemoteCryptographyClient _remoteProvider; |
| | 24 | | private ICryptographyProvider _provider; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="CryptographyClient"/> class for mocking. |
| | 28 | | /// </summary> |
| 292 | 29 | | protected CryptographyClient() |
| | 30 | | { |
| 292 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Initializes a new instance of the <see cref="CryptographyClient"/> class. |
| | 35 | | /// </summary> |
| | 36 | | /// <param name="keyId">The <see cref="KeyProperties.Id"/> of the <see cref="KeyVaultKey"/> which will be used f |
| | 37 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, like De |
| | 38 | | /// <exception cref="ArgumentNullException"><paramref name="keyId"/> or <paramref name="credential"/> is null.</ |
| | 39 | | public CryptographyClient(Uri keyId, TokenCredential credential) |
| 12 | 40 | | : this(keyId, credential, null) |
| | 41 | | { |
| 4 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Initializes a new instance of the <see cref="CryptographyClient"/> class. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="keyId">The <see cref="KeyProperties.Id"/> of the <see cref="KeyVaultKey"/> which will be used f |
| | 48 | | /// <param name="credential">A <see cref="TokenCredential"/> used to authenticate requests to the vault, like De |
| | 49 | | /// <param name="options"><see cref="CryptographyClientOptions"/> that allow to configure the management of the |
| | 50 | | /// <exception cref="ArgumentNullException"><paramref name="keyId"/> or <paramref name="credential"/> is null.</ |
| | 51 | | /// <exception cref="NotSupportedException">The <see cref="CryptographyClientOptions.Version"/> is not supported |
| 12 | 52 | | public CryptographyClient(Uri keyId, TokenCredential credential, CryptographyClientOptions options) : this(keyId |
| | 53 | | { |
| 4 | 54 | | } |
| | 55 | |
|
| 152 | 56 | | internal CryptographyClient(Uri keyId, TokenCredential credential, CryptographyClientOptions options, bool force |
| | 57 | | { |
| 152 | 58 | | Argument.AssertNotNull(keyId, nameof(keyId)); |
| 148 | 59 | | Argument.AssertNotNull(credential, nameof(credential)); |
| | 60 | |
|
| 144 | 61 | | _keyId = keyId; |
| 144 | 62 | | options ??= new CryptographyClientOptions(); |
| | 63 | |
|
| 144 | 64 | | RemoteCryptographyClient remoteClient = new RemoteCryptographyClient(_keyId, credential, options); |
| | 65 | |
|
| 144 | 66 | | _pipeline = remoteClient.Pipeline; |
| 144 | 67 | | _remoteProvider = remoteClient; |
| | 68 | |
|
| 144 | 69 | | if (forceRemote) |
| | 70 | | { |
| 104 | 71 | | _provider = remoteClient; |
| | 72 | | } |
| 144 | 73 | | } |
| | 74 | |
|
| 140 | 75 | | internal CryptographyClient(KeyVaultKey key, TokenCredential credential, CryptographyClientOptions options, ICry |
| | 76 | | { |
| 140 | 77 | | Argument.AssertNotNull(key, nameof(key)); |
| 140 | 78 | | Argument.AssertNotNull(credential, nameof(credential)); |
| | 79 | |
|
| 140 | 80 | | JsonWebKey keyMaterial = key.Key; |
| 140 | 81 | | if (string.IsNullOrEmpty(keyMaterial?.Id)) |
| | 82 | | { |
| 0 | 83 | | throw new ArgumentException($"{nameof(key.Id)} is required", nameof(key)); |
| | 84 | | } |
| | 85 | |
|
| 140 | 86 | | _keyId = new Uri(keyMaterial.Id); |
| 140 | 87 | | options ??= new CryptographyClientOptions(); |
| | 88 | |
|
| 140 | 89 | | RemoteCryptographyClient remoteClient = new RemoteCryptographyClient(_keyId, credential, options); |
| | 90 | |
|
| 140 | 91 | | _pipeline = remoteClient.Pipeline; |
| 140 | 92 | | _remoteProvider = remoteClient; |
| 140 | 93 | | _provider = provider ?? LocalCryptographyProviderFactory.Create(key); |
| 140 | 94 | | } |
| | 95 | |
|
| 12 | 96 | | internal CryptographyClient(KeyVaultKey key, KeyVaultPipeline pipeline) |
| | 97 | | { |
| 12 | 98 | | Argument.AssertNotNull(key, nameof(key)); |
| | 99 | |
|
| 12 | 100 | | JsonWebKey keyMaterial = key.Key; |
| 12 | 101 | | if (string.IsNullOrEmpty(keyMaterial?.Id)) |
| | 102 | | { |
| 0 | 103 | | throw new ArgumentException($"{nameof(key.Id)} is required", nameof(key)); |
| | 104 | | } |
| | 105 | |
|
| 12 | 106 | | _keyId = new Uri(keyMaterial.Id); |
| | 107 | |
|
| 12 | 108 | | RemoteCryptographyClient remoteClient = new RemoteCryptographyClient(pipeline); |
| | 109 | |
|
| 12 | 110 | | _pipeline = pipeline; |
| 12 | 111 | | _remoteProvider = remoteClient; |
| 12 | 112 | | _provider = LocalCryptographyProviderFactory.Create(key); |
| 12 | 113 | | } |
| | 114 | |
|
| 4 | 115 | | internal CryptographyClient(Uri keyId, KeyVaultPipeline pipeline) |
| | 116 | | { |
| 4 | 117 | | Argument.AssertNotNull(keyId, nameof(keyId)); |
| | 118 | |
|
| 4 | 119 | | _keyId = keyId; |
| | 120 | |
|
| 4 | 121 | | RemoteCryptographyClient remoteClient = new RemoteCryptographyClient(pipeline); |
| | 122 | |
|
| 4 | 123 | | _pipeline = pipeline; |
| 4 | 124 | | _remoteProvider = remoteClient; |
| 4 | 125 | | _provider = remoteClient; |
| 4 | 126 | | } |
| | 127 | |
|
| 36 | 128 | | internal ICryptographyProvider RemoteClient => _remoteProvider; |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// Gets the <see cref="KeyVaultKey.Id"/> of the key used to perform cryptographic operations for the client. |
| | 132 | | /// </summary> |
| 0 | 133 | | public virtual string KeyId => _keyId.ToString(); |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Encrypts the specified plain text. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="algorithm">The <see cref="EncryptionAlgorithm"/> to use.</param> |
| | 139 | | /// <param name="plaintext">The data to encrypt.</param> |
| | 140 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 141 | | /// <returns> |
| | 142 | | /// The result of the encrypt operation. The returned <see cref="EncryptResult"/> contains the encrypted data |
| | 143 | | /// along with all other information needed to decrypt it. This information should be stored with the encrypted |
| | 144 | | /// </returns> |
| | 145 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 146 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 147 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 148 | | public virtual async Task<EncryptResult> EncryptAsync(EncryptionAlgorithm algorithm, byte[] plaintext, Cancellat |
| | 149 | | { |
| 12 | 150 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Encrypt)}"); |
| 12 | 151 | | scope.AddAttribute("key", _keyId); |
| 12 | 152 | | scope.Start(); |
| | 153 | |
|
| | 154 | | try |
| | 155 | | { |
| 12 | 156 | | if (_provider is null) |
| | 157 | | { |
| 2 | 158 | | await InitializeAsync(nameof(Encrypt), cancellationToken).ConfigureAwait(false); |
| | 159 | | } |
| | 160 | |
|
| 12 | 161 | | EncryptResult result = null; |
| 12 | 162 | | if (_provider.SupportsOperation(KeyOperation.Encrypt)) |
| | 163 | | { |
| | 164 | | try |
| | 165 | | { |
| 12 | 166 | | result = await _provider.EncryptAsync(algorithm, plaintext, cancellationToken).ConfigureAwait(fa |
| 10 | 167 | | } |
| 2 | 168 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 169 | | { |
| | 170 | | // Use the non-async name as we do for scope. |
| 2 | 171 | | KeysEventSource.Singleton.CryptographicException(nameof(Encrypt), ex); |
| 2 | 172 | | } |
| | 173 | | } |
| | 174 | |
|
| 12 | 175 | | if (result is null) |
| | 176 | | { |
| 4 | 177 | | result = await _remoteProvider.EncryptAsync(algorithm, plaintext, cancellationToken).ConfigureAwait( |
| | 178 | | } |
| | 179 | |
|
| 12 | 180 | | return result; |
| | 181 | | } |
| 0 | 182 | | catch (Exception e) |
| | 183 | | { |
| 0 | 184 | | scope.Failed(e); |
| 0 | 185 | | throw; |
| | 186 | | } |
| 12 | 187 | | } |
| | 188 | |
|
| | 189 | | /// <summary> |
| | 190 | | /// Encrypts the specified plain text. |
| | 191 | | /// </summary> |
| | 192 | | /// <param name="algorithm">The <see cref="EncryptionAlgorithm"/> to use.</param> |
| | 193 | | /// <param name="plaintext">The data to encrypt.</param> |
| | 194 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 195 | | /// <returns> |
| | 196 | | /// The result of the encrypt operation. The returned <see cref="EncryptResult"/> contains the encrypted data |
| | 197 | | /// along with all other information needed to decrypt it. This information should be stored with the encrypted |
| | 198 | | /// </returns> |
| | 199 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 200 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 201 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 202 | | public virtual EncryptResult Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToken cancella |
| | 203 | | { |
| 12 | 204 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Encrypt)}"); |
| 12 | 205 | | scope.AddAttribute("key", _keyId); |
| 12 | 206 | | scope.Start(); |
| | 207 | |
|
| | 208 | | try |
| | 209 | | { |
| 12 | 210 | | if (_provider is null) |
| | 211 | | { |
| 2 | 212 | | Initialize(nameof(Encrypt), cancellationToken); |
| | 213 | | } |
| | 214 | |
|
| 12 | 215 | | EncryptResult result = null; |
| 12 | 216 | | if (_provider.SupportsOperation(KeyOperation.Encrypt)) |
| | 217 | | { |
| | 218 | | try |
| | 219 | | { |
| 12 | 220 | | result = _provider.Encrypt(algorithm, plaintext, cancellationToken); |
| 10 | 221 | | } |
| 2 | 222 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 223 | | { |
| 2 | 224 | | KeysEventSource.Singleton.CryptographicException(nameof(Encrypt), ex); |
| 2 | 225 | | } |
| | 226 | | } |
| | 227 | |
|
| 12 | 228 | | if (result is null) |
| | 229 | | { |
| 4 | 230 | | result = _remoteProvider.Encrypt(algorithm, plaintext, cancellationToken); |
| | 231 | | } |
| | 232 | |
|
| 12 | 233 | | return result; |
| | 234 | | } |
| 0 | 235 | | catch (Exception e) |
| | 236 | | { |
| 0 | 237 | | scope.Failed(e); |
| 0 | 238 | | throw; |
| | 239 | | } |
| 12 | 240 | | } |
| | 241 | |
|
| | 242 | | /// <summary> |
| | 243 | | /// Decrypts the specified cipher text. |
| | 244 | | /// </summary> |
| | 245 | | /// <param name="algorithm">The <see cref="EncryptionAlgorithm"/> to use.</param> |
| | 246 | | /// <param name="ciphertext">The encrypted data to decrypt.</param> |
| | 247 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 248 | | /// <returns> |
| | 249 | | /// The result of the decrypt operation. The returned <see cref="DecryptResult"/> contains the encrypted data |
| | 250 | | /// along with information regarding the algorithm and key used to decrypt it. |
| | 251 | | /// </returns> |
| | 252 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 253 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 254 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 255 | | public virtual async Task<DecryptResult> DecryptAsync(EncryptionAlgorithm algorithm, byte[] ciphertext, Cancella |
| | 256 | | { |
| 14 | 257 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Decrypt)}"); |
| 14 | 258 | | scope.AddAttribute("key", _keyId); |
| 14 | 259 | | scope.Start(); |
| | 260 | |
|
| | 261 | | try |
| | 262 | | { |
| 14 | 263 | | if (_provider is null) |
| | 264 | | { |
| 2 | 265 | | await InitializeAsync(nameof(Decrypt), cancellationToken).ConfigureAwait(false); |
| | 266 | | } |
| | 267 | |
|
| 14 | 268 | | DecryptResult result = null; |
| 14 | 269 | | if (_provider.SupportsOperation(KeyOperation.Decrypt)) |
| | 270 | | { |
| | 271 | | try |
| | 272 | | { |
| 14 | 273 | | result = await _provider.DecryptAsync(algorithm, ciphertext, cancellationToken).ConfigureAwait(f |
| 12 | 274 | | } |
| 2 | 275 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 276 | | { |
| | 277 | | // Use the non-async name as we do for scope. |
| 2 | 278 | | KeysEventSource.Singleton.CryptographicException(nameof(Decrypt), ex); |
| 2 | 279 | | } |
| | 280 | | } |
| | 281 | |
|
| 14 | 282 | | if (result is null) |
| | 283 | | { |
| 6 | 284 | | result = await _remoteProvider.DecryptAsync(algorithm, ciphertext, cancellationToken).ConfigureAwait |
| | 285 | | } |
| | 286 | |
|
| 14 | 287 | | return result; |
| | 288 | | } |
| 0 | 289 | | catch (Exception e) |
| | 290 | | { |
| 0 | 291 | | scope.Failed(e); |
| 0 | 292 | | throw; |
| | 293 | | } |
| 14 | 294 | | } |
| | 295 | |
|
| | 296 | | /// <summary> |
| | 297 | | /// Decrypts the specified cipher text. |
| | 298 | | /// </summary> |
| | 299 | | /// <param name="algorithm">The <see cref="EncryptionAlgorithm"/> to use.</param> |
| | 300 | | /// <param name="ciphertext">The encrypted data to decrypt.</param> |
| | 301 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 302 | | /// <returns> |
| | 303 | | /// The result of the decrypt operation. The returned <see cref="DecryptResult"/> contains the encrypted data |
| | 304 | | /// along with information regarding the algorithm and key used to decrypt it. |
| | 305 | | /// </returns> |
| | 306 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 307 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 308 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 309 | | public virtual DecryptResult Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationToken cancell |
| | 310 | | { |
| 14 | 311 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Decrypt)}"); |
| 14 | 312 | | scope.AddAttribute("key", _keyId); |
| 14 | 313 | | scope.Start(); |
| | 314 | |
|
| | 315 | | try |
| | 316 | | { |
| 14 | 317 | | if (_provider is null) |
| | 318 | | { |
| 2 | 319 | | Initialize(nameof(Decrypt), cancellationToken); |
| | 320 | | } |
| | 321 | |
|
| 14 | 322 | | DecryptResult result = null; |
| 14 | 323 | | if (_provider.SupportsOperation(KeyOperation.Decrypt)) |
| | 324 | | { |
| | 325 | | try |
| | 326 | | { |
| 14 | 327 | | result = _provider.Decrypt(algorithm, ciphertext, cancellationToken); |
| 12 | 328 | | } |
| 2 | 329 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 330 | | { |
| 2 | 331 | | KeysEventSource.Singleton.CryptographicException(nameof(Decrypt), ex); |
| 2 | 332 | | } |
| | 333 | | } |
| | 334 | |
|
| 14 | 335 | | if (result is null) |
| | 336 | | { |
| 6 | 337 | | result = _remoteProvider.Decrypt(algorithm, ciphertext, cancellationToken); |
| | 338 | | } |
| | 339 | |
|
| 14 | 340 | | return result; |
| | 341 | | } |
| 0 | 342 | | catch (Exception e) |
| | 343 | | { |
| 0 | 344 | | scope.Failed(e); |
| 0 | 345 | | throw; |
| | 346 | | } |
| 14 | 347 | | } |
| | 348 | |
|
| | 349 | | /// <summary> |
| | 350 | | /// Encrypts the specified key. |
| | 351 | | /// </summary> |
| | 352 | | /// <param name="algorithm">The <see cref="KeyWrapAlgorithm"/> to use.</param> |
| | 353 | | /// <param name="key">The key to encrypt.</param> |
| | 354 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 355 | | /// <returns> |
| | 356 | | /// The result of the wrap operation. The returned <see cref="WrapResult"/> contains the wrapped key |
| | 357 | | /// along with all other information needed to unwrap it. This information should be stored with the wrapped key |
| | 358 | | /// </returns> |
| | 359 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 360 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 361 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 362 | | public virtual async Task<WrapResult> WrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken can |
| | 363 | | { |
| 18 | 364 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(WrapKey)}"); |
| 18 | 365 | | scope.AddAttribute("key", _keyId); |
| 18 | 366 | | scope.Start(); |
| | 367 | |
|
| | 368 | | try |
| | 369 | | { |
| 18 | 370 | | if (_provider is null) |
| | 371 | | { |
| 2 | 372 | | await InitializeAsync(nameof(WrapKey), cancellationToken).ConfigureAwait(false); |
| | 373 | | } |
| | 374 | |
|
| 18 | 375 | | WrapResult result = null; |
| 18 | 376 | | if (_provider.SupportsOperation(KeyOperation.WrapKey)) |
| | 377 | | { |
| | 378 | | try |
| | 379 | | { |
| 18 | 380 | | result = await _provider.WrapKeyAsync(algorithm, key, cancellationToken).ConfigureAwait(false); |
| 16 | 381 | | } |
| 2 | 382 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 383 | | { |
| | 384 | | // Use the non-async name as we do for scope. |
| 2 | 385 | | KeysEventSource.Singleton.CryptographicException(nameof(WrapKey), ex); |
| 2 | 386 | | } |
| | 387 | | } |
| | 388 | |
|
| 18 | 389 | | if (result is null) |
| | 390 | | { |
| 6 | 391 | | result = await _remoteProvider.WrapKeyAsync(algorithm, key, cancellationToken).ConfigureAwait(false) |
| | 392 | | } |
| | 393 | |
|
| 18 | 394 | | return result; |
| | 395 | | } |
| 0 | 396 | | catch (Exception e) |
| | 397 | | { |
| 0 | 398 | | scope.Failed(e); |
| 0 | 399 | | throw; |
| | 400 | | } |
| 18 | 401 | | } |
| | 402 | |
|
| | 403 | | /// <summary> |
| | 404 | | /// Encrypts the specified key. |
| | 405 | | /// </summary> |
| | 406 | | /// <param name="algorithm">The <see cref="KeyWrapAlgorithm"/> to use.</param> |
| | 407 | | /// <param name="key">The key to encrypt.</param> |
| | 408 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 409 | | /// <returns> |
| | 410 | | /// The result of the wrap operation. The returned <see cref="WrapResult"/> contains the wrapped key |
| | 411 | | /// along with all other information needed to unwrap it. This information should be stored with the wrapped key |
| | 412 | | /// </returns> |
| | 413 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 414 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 415 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 416 | | public virtual WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken = |
| | 417 | | { |
| 18 | 418 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(WrapKey)}"); |
| 18 | 419 | | scope.AddAttribute("key", _keyId); |
| 18 | 420 | | scope.Start(); |
| | 421 | |
|
| | 422 | | try |
| | 423 | | { |
| 18 | 424 | | if (_provider is null) |
| | 425 | | { |
| 2 | 426 | | Initialize(nameof(WrapKey), cancellationToken); |
| | 427 | | } |
| | 428 | |
|
| 18 | 429 | | WrapResult result = null; |
| 18 | 430 | | if (_provider.SupportsOperation(KeyOperation.WrapKey)) |
| | 431 | | { |
| | 432 | | try |
| | 433 | | { |
| 18 | 434 | | result = _provider.WrapKey(algorithm, key, cancellationToken); |
| 16 | 435 | | } |
| 2 | 436 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 437 | | { |
| 2 | 438 | | KeysEventSource.Singleton.CryptographicException(nameof(WrapKey), ex); |
| 2 | 439 | | } |
| | 440 | | } |
| | 441 | |
|
| 18 | 442 | | if (result is null) |
| | 443 | | { |
| 6 | 444 | | result = _remoteProvider.WrapKey(algorithm, key, cancellationToken); |
| | 445 | | } |
| | 446 | |
|
| 18 | 447 | | return result; |
| | 448 | | } |
| 0 | 449 | | catch (Exception e) |
| | 450 | | { |
| 0 | 451 | | scope.Failed(e); |
| 0 | 452 | | throw; |
| | 453 | | } |
| 18 | 454 | | } |
| | 455 | |
|
| | 456 | | /// <summary> |
| | 457 | | /// Decrypts the specified encrypted key. |
| | 458 | | /// </summary> |
| | 459 | | /// <param name="algorithm">The <see cref="KeyWrapAlgorithm"/> to use.</param> |
| | 460 | | /// <param name="encryptedKey">The encrypted key.</param> |
| | 461 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 462 | | /// <returns> |
| | 463 | | /// The result of the unwrap operation. The returned <see cref="UnwrapResult"/> contains the key |
| | 464 | | /// along with information regarding the algorithm and key used to unwrap it. |
| | 465 | | /// </returns> |
| | 466 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 467 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 468 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 469 | | public virtual async Task<UnwrapResult> UnwrapKeyAsync(KeyWrapAlgorithm algorithm, byte[] encryptedKey, Cancella |
| | 470 | | { |
| 24 | 471 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(UnwrapKey)}"); |
| 24 | 472 | | scope.AddAttribute("key", _keyId); |
| 24 | 473 | | scope.Start(); |
| | 474 | |
|
| | 475 | | try |
| | 476 | | { |
| 24 | 477 | | if (_provider is null) |
| | 478 | | { |
| 2 | 479 | | await InitializeAsync(nameof(UnwrapKey), cancellationToken).ConfigureAwait(false); |
| | 480 | | } |
| | 481 | |
|
| 24 | 482 | | UnwrapResult result = null; |
| 24 | 483 | | if (_provider.SupportsOperation(KeyOperation.UnwrapKey)) |
| | 484 | | { |
| | 485 | | try |
| | 486 | | { |
| 24 | 487 | | result = await _provider.UnwrapKeyAsync(algorithm, encryptedKey, cancellationToken).ConfigureAwa |
| 18 | 488 | | } |
| 2 | 489 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 490 | | { |
| | 491 | | // Use the non-async name as we do for scope. |
| 2 | 492 | | KeysEventSource.Singleton.CryptographicException(nameof(UnwrapKey), ex); |
| 2 | 493 | | } |
| | 494 | | } |
| | 495 | |
|
| 20 | 496 | | if (result is null) |
| | 497 | | { |
| 10 | 498 | | result = await _remoteProvider.UnwrapKeyAsync(algorithm, encryptedKey, cancellationToken).ConfigureA |
| | 499 | | } |
| | 500 | |
|
| 20 | 501 | | return result; |
| | 502 | | } |
| 4 | 503 | | catch (Exception e) |
| | 504 | | { |
| 4 | 505 | | scope.Failed(e); |
| 4 | 506 | | throw; |
| | 507 | | } |
| 20 | 508 | | } |
| | 509 | |
|
| | 510 | | /// <summary> |
| | 511 | | /// Decrypts the specified encrypted key. |
| | 512 | | /// </summary> |
| | 513 | | /// <param name="algorithm">The <see cref="KeyWrapAlgorithm"/> to use.</param> |
| | 514 | | /// <param name="encryptedKey">The encrypted key.</param> |
| | 515 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 516 | | /// <returns> |
| | 517 | | /// The result of the unwrap operation. The returned <see cref="UnwrapResult"/> contains the key |
| | 518 | | /// along with information regarding the algorithm and key used to unwrap it. |
| | 519 | | /// </returns> |
| | 520 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 521 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 522 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 523 | | public virtual UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancell |
| | 524 | | { |
| 20 | 525 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(UnwrapKey)}"); |
| 20 | 526 | | scope.AddAttribute("key", _keyId); |
| 20 | 527 | | scope.Start(); |
| | 528 | |
|
| | 529 | | try |
| | 530 | | { |
| 20 | 531 | | if (_provider is null) |
| | 532 | | { |
| 2 | 533 | | Initialize(nameof(UnwrapKey), cancellationToken); |
| | 534 | | } |
| | 535 | |
|
| 20 | 536 | | UnwrapResult result = null; |
| 20 | 537 | | if (_provider.SupportsOperation(KeyOperation.UnwrapKey)) |
| | 538 | | { |
| | 539 | | try |
| | 540 | | { |
| 20 | 541 | | result = _provider.UnwrapKey(algorithm, encryptedKey, cancellationToken); |
| 18 | 542 | | } |
| 2 | 543 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 544 | | { |
| 2 | 545 | | KeysEventSource.Singleton.CryptographicException(nameof(UnwrapKey), ex); |
| 2 | 546 | | } |
| | 547 | | } |
| | 548 | |
|
| 20 | 549 | | if (result is null) |
| | 550 | | { |
| 10 | 551 | | result = _remoteProvider.UnwrapKey(algorithm, encryptedKey, cancellationToken); |
| | 552 | | } |
| | 553 | |
|
| 20 | 554 | | return result; |
| | 555 | | } |
| 0 | 556 | | catch (Exception e) |
| | 557 | | { |
| 0 | 558 | | scope.Failed(e); |
| 0 | 559 | | throw; |
| | 560 | | } |
| 20 | 561 | | } |
| | 562 | |
|
| | 563 | | /// <summary> |
| | 564 | | /// Signs the specified digest. |
| | 565 | | /// </summary> |
| | 566 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 567 | | /// <param name="digest">The pre-hashed digest to sign. The hash algorithm used to compute the digest must be co |
| | 568 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 569 | | /// <returns> |
| | 570 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 571 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 572 | | /// </returns> |
| | 573 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 574 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 575 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 576 | | public virtual async Task<SignResult> SignAsync(SignatureAlgorithm algorithm, byte[] digest, CancellationToken c |
| | 577 | | { |
| 88 | 578 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Sign)}"); |
| 88 | 579 | | scope.AddAttribute("key", _keyId); |
| 88 | 580 | | scope.Start(); |
| | 581 | |
|
| | 582 | | try |
| | 583 | | { |
| 88 | 584 | | if (_provider is null) |
| | 585 | | { |
| 20 | 586 | | await InitializeAsync(nameof(Sign), cancellationToken).ConfigureAwait(false); |
| | 587 | | } |
| | 588 | |
|
| 88 | 589 | | SignResult result = null; |
| 88 | 590 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 591 | | { |
| | 592 | | try |
| | 593 | | { |
| 88 | 594 | | result = await _provider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false); |
| 86 | 595 | | } |
| 2 | 596 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 597 | | { |
| | 598 | | // Use the non-async name as we do for scope. |
| 2 | 599 | | KeysEventSource.Singleton.CryptographicException(nameof(Sign), ex); |
| 2 | 600 | | } |
| | 601 | | } |
| | 602 | |
|
| 88 | 603 | | if (result is null) |
| | 604 | | { |
| 28 | 605 | | result = await _remoteProvider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false) |
| | 606 | | } |
| | 607 | |
|
| 88 | 608 | | return result; |
| | 609 | | } |
| 0 | 610 | | catch (Exception e) |
| | 611 | | { |
| 0 | 612 | | scope.Failed(e); |
| 0 | 613 | | throw; |
| | 614 | | } |
| 88 | 615 | | } |
| | 616 | |
|
| | 617 | | /// <summary> |
| | 618 | | /// Signs the specified digest. |
| | 619 | | /// </summary> |
| | 620 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 621 | | /// <param name="digest">The pre-hashed digest to sign. The hash algorithm used to compute the digest must be co |
| | 622 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 623 | | /// <returns> |
| | 624 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 625 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 626 | | /// </returns> |
| | 627 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 628 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 629 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 630 | | public virtual SignResult Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellationToken |
| | 631 | | { |
| 88 | 632 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Sign)}"); |
| 88 | 633 | | scope.AddAttribute("key", _keyId); |
| 88 | 634 | | scope.Start(); |
| | 635 | |
|
| | 636 | | try |
| | 637 | | { |
| 88 | 638 | | if (_provider is null) |
| | 639 | | { |
| 20 | 640 | | Initialize(nameof(Sign), cancellationToken); |
| | 641 | | } |
| | 642 | |
|
| 88 | 643 | | SignResult result = null; |
| 88 | 644 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 645 | | { |
| | 646 | | try |
| | 647 | | { |
| 88 | 648 | | result = _provider.Sign(algorithm, digest, cancellationToken); |
| 86 | 649 | | } |
| 2 | 650 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 651 | | { |
| 2 | 652 | | KeysEventSource.Singleton.CryptographicException(nameof(Sign), ex); |
| 2 | 653 | | } |
| | 654 | | } |
| | 655 | |
|
| 88 | 656 | | if (result is null) |
| | 657 | | { |
| 28 | 658 | | result = _remoteProvider.Sign(algorithm, digest, cancellationToken); |
| | 659 | | } |
| | 660 | |
|
| 88 | 661 | | return result; |
| | 662 | | } |
| 0 | 663 | | catch (Exception e) |
| | 664 | | { |
| 0 | 665 | | scope.Failed(e); |
| 0 | 666 | | throw; |
| | 667 | | } |
| 88 | 668 | | } |
| | 669 | |
|
| | 670 | | /// <summary> |
| | 671 | | /// Verifies the specified signature. |
| | 672 | | /// </summary> |
| | 673 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 674 | | /// <param name="digest">The pre-hashed digest corresponding to the signature. The hash algorithm used to comput |
| | 675 | | /// <param name="signature">The signature to verify.</param> |
| | 676 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 677 | | /// <returns> |
| | 678 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 679 | | /// </returns> |
| | 680 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 681 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 682 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 683 | | public virtual async Task<VerifyResult> VerifyAsync(SignatureAlgorithm algorithm, byte[] digest, byte[] signatur |
| | 684 | | { |
| 66 | 685 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Verify)}"); |
| 66 | 686 | | scope.AddAttribute("key", _keyId); |
| 66 | 687 | | scope.Start(); |
| | 688 | |
|
| | 689 | | try |
| | 690 | | { |
| 66 | 691 | | if (_provider is null) |
| | 692 | | { |
| 2 | 693 | | await InitializeAsync(nameof(Verify), cancellationToken).ConfigureAwait(false); |
| | 694 | | } |
| | 695 | |
|
| 66 | 696 | | VerifyResult result = null; |
| 66 | 697 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 698 | | { |
| | 699 | | try |
| | 700 | | { |
| 66 | 701 | | result = await _provider.VerifyAsync(algorithm, digest, signature, cancellationToken).ConfigureA |
| 64 | 702 | | } |
| 2 | 703 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 704 | | { |
| | 705 | | // Use the non-async name as we do for scope. |
| 2 | 706 | | KeysEventSource.Singleton.CryptographicException(nameof(Verify), ex); |
| 2 | 707 | | } |
| | 708 | | } |
| | 709 | |
|
| 66 | 710 | | if (result is null) |
| | 711 | | { |
| 6 | 712 | | result = await _remoteProvider.VerifyAsync(algorithm, digest, signature, cancellationToken).Configur |
| | 713 | | } |
| | 714 | |
|
| 66 | 715 | | return result; |
| | 716 | | } |
| 0 | 717 | | catch (Exception e) |
| | 718 | | { |
| 0 | 719 | | scope.Failed(e); |
| 0 | 720 | | throw; |
| | 721 | | } |
| 66 | 722 | | } |
| | 723 | |
|
| | 724 | | /// <summary> |
| | 725 | | /// Verifies the specified signature. |
| | 726 | | /// </summary> |
| | 727 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 728 | | /// <param name="digest">The pre-hashed digest corresponding to the signature. The hash algorithm used to comput |
| | 729 | | /// <param name="signature">The signature to verify.</param> |
| | 730 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 731 | | /// <returns> |
| | 732 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 733 | | /// </returns> |
| | 734 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 735 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 736 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 737 | | public virtual VerifyResult Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, CancellationTo |
| | 738 | | { |
| 66 | 739 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Verify)}"); |
| 66 | 740 | | scope.AddAttribute("key", _keyId); |
| 66 | 741 | | scope.Start(); |
| | 742 | |
|
| | 743 | | try |
| | 744 | | { |
| 66 | 745 | | if (_provider is null) |
| | 746 | | { |
| 2 | 747 | | Initialize(nameof(Verify), cancellationToken); |
| | 748 | | } |
| | 749 | |
|
| 66 | 750 | | VerifyResult result = null; |
| 66 | 751 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 752 | | { |
| | 753 | | try |
| | 754 | | { |
| 66 | 755 | | result = _provider.Verify(algorithm, digest, signature, cancellationToken); |
| 64 | 756 | | } |
| 2 | 757 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 758 | | { |
| 2 | 759 | | KeysEventSource.Singleton.CryptographicException(nameof(Verify), ex); |
| 2 | 760 | | } |
| | 761 | | } |
| | 762 | |
|
| 66 | 763 | | if (result is null) |
| | 764 | | { |
| 6 | 765 | | result = _remoteProvider.Verify(algorithm, digest, signature, cancellationToken); |
| | 766 | | } |
| | 767 | |
|
| 66 | 768 | | return result; |
| | 769 | | } |
| 0 | 770 | | catch (Exception e) |
| | 771 | | { |
| 0 | 772 | | scope.Failed(e); |
| 0 | 773 | | throw; |
| | 774 | | } |
| 66 | 775 | | } |
| | 776 | |
|
| | 777 | | /// <summary> |
| | 778 | | /// Signs the specified data. |
| | 779 | | /// </summary> |
| | 780 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 781 | | /// <param name="data">The data to sign.</param> |
| | 782 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 783 | | /// <returns> |
| | 784 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 785 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 786 | | /// </returns> |
| | 787 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 788 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 789 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 790 | | public virtual async Task<SignResult> SignDataAsync(SignatureAlgorithm algorithm, byte[] data, CancellationToken |
| | 791 | | { |
| 22 | 792 | | Argument.AssertNotNull(data, nameof(data)); |
| | 793 | |
|
| 20 | 794 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(SignData)}"); |
| 20 | 795 | | scope.AddAttribute("key", _keyId); |
| 20 | 796 | | scope.Start(); |
| | 797 | |
|
| | 798 | | try |
| | 799 | | { |
| 20 | 800 | | byte[] digest = CreateDigest(algorithm, data); |
| | 801 | |
|
| 20 | 802 | | if (_provider is null) |
| | 803 | | { |
| 0 | 804 | | await InitializeAsync(nameof(SignData), cancellationToken).ConfigureAwait(false); |
| | 805 | | } |
| | 806 | |
|
| 20 | 807 | | SignResult result = null; |
| 20 | 808 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 809 | | { |
| | 810 | | try |
| | 811 | | { |
| 20 | 812 | | result = await _provider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false); |
| 20 | 813 | | } |
| 0 | 814 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 815 | | { |
| | 816 | | // Use the non-async name as we do for scope. |
| 0 | 817 | | KeysEventSource.Singleton.CryptographicException(nameof(SignData), ex); |
| 0 | 818 | | } |
| | 819 | | } |
| | 820 | |
|
| 20 | 821 | | if (result is null) |
| | 822 | | { |
| 0 | 823 | | result = await _remoteProvider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false) |
| | 824 | | } |
| | 825 | |
|
| 20 | 826 | | return result; |
| | 827 | | } |
| 0 | 828 | | catch (Exception e) |
| | 829 | | { |
| 0 | 830 | | scope.Failed(e); |
| 0 | 831 | | throw; |
| | 832 | | } |
| 20 | 833 | | } |
| | 834 | |
|
| | 835 | | /// <summary> |
| | 836 | | /// Signs the specified data. |
| | 837 | | /// </summary> |
| | 838 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 839 | | /// <param name="data">The data to sign.</param> |
| | 840 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 841 | | /// <returns> |
| | 842 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 843 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 844 | | /// </returns> |
| | 845 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 846 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 847 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 848 | | public virtual SignResult SignData(SignatureAlgorithm algorithm, byte[] data, CancellationToken cancellationToke |
| | 849 | | { |
| 22 | 850 | | Argument.AssertNotNull(data, nameof(data)); |
| | 851 | |
|
| 20 | 852 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(SignData)}"); |
| 20 | 853 | | scope.AddAttribute("key", _keyId); |
| 20 | 854 | | scope.Start(); |
| | 855 | |
|
| | 856 | | try |
| | 857 | | { |
| 20 | 858 | | byte[] digest = CreateDigest(algorithm, data); |
| | 859 | |
|
| 20 | 860 | | if (_provider is null) |
| | 861 | | { |
| 0 | 862 | | Initialize(nameof(SignData), cancellationToken); |
| | 863 | | } |
| | 864 | |
|
| 20 | 865 | | SignResult result = null; |
| 20 | 866 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 867 | | { |
| | 868 | | try |
| | 869 | | { |
| 20 | 870 | | result = _provider.Sign(algorithm, digest, cancellationToken); |
| 20 | 871 | | } |
| 0 | 872 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 873 | | { |
| | 874 | | // Use the non-async name as we do for scope. |
| 0 | 875 | | KeysEventSource.Singleton.CryptographicException(nameof(SignData), ex); |
| 0 | 876 | | } |
| | 877 | | } |
| | 878 | |
|
| 20 | 879 | | if (result is null) |
| | 880 | | { |
| 0 | 881 | | result = _remoteProvider.Sign(algorithm, digest, cancellationToken); |
| | 882 | | } |
| | 883 | |
|
| 20 | 884 | | return result; |
| | 885 | | } |
| 0 | 886 | | catch (Exception e) |
| | 887 | | { |
| 0 | 888 | | scope.Failed(e); |
| 0 | 889 | | throw; |
| | 890 | | } |
| 20 | 891 | | } |
| | 892 | |
|
| | 893 | | /// <summary> |
| | 894 | | /// Signs the specified data. |
| | 895 | | /// </summary> |
| | 896 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 897 | | /// <param name="data">The data to sign.</param> |
| | 898 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 899 | | /// <returns> |
| | 900 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 901 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 902 | | /// </returns> |
| | 903 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 904 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 905 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 906 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 907 | | public virtual async Task<SignResult> SignDataAsync(SignatureAlgorithm algorithm, Stream data, CancellationToken |
| | 908 | | { |
| 22 | 909 | | Argument.AssertNotNull(data, nameof(data)); |
| | 910 | |
|
| 20 | 911 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(SignData)}"); |
| 20 | 912 | | scope.AddAttribute("key", _keyId); |
| 20 | 913 | | scope.Start(); |
| | 914 | |
|
| | 915 | | try |
| | 916 | | { |
| 20 | 917 | | byte[] digest = CreateDigest(algorithm, data); |
| | 918 | |
|
| 20 | 919 | | if (_provider is null) |
| | 920 | | { |
| 0 | 921 | | await InitializeAsync(nameof(SignData), cancellationToken).ConfigureAwait(false); |
| | 922 | | } |
| | 923 | |
|
| 20 | 924 | | SignResult result = null; |
| 20 | 925 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 926 | | { |
| | 927 | | try |
| | 928 | | { |
| 20 | 929 | | result = await _provider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false); |
| 20 | 930 | | } |
| 0 | 931 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 932 | | { |
| | 933 | | // Use the non-async name as we do for scope. |
| 0 | 934 | | KeysEventSource.Singleton.CryptographicException(nameof(SignData), ex); |
| 0 | 935 | | } |
| | 936 | | } |
| | 937 | |
|
| 20 | 938 | | if (result is null) |
| | 939 | | { |
| 0 | 940 | | result = await _remoteProvider.SignAsync(algorithm, digest, cancellationToken).ConfigureAwait(false) |
| | 941 | | } |
| | 942 | |
|
| 20 | 943 | | return result; |
| | 944 | | } |
| 0 | 945 | | catch (Exception e) |
| | 946 | | { |
| 0 | 947 | | scope.Failed(e); |
| 0 | 948 | | throw; |
| | 949 | | } |
| 20 | 950 | | } |
| | 951 | |
|
| | 952 | | /// <summary> |
| | 953 | | /// Signs the specified data. |
| | 954 | | /// </summary> |
| | 955 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use.</param> |
| | 956 | | /// <param name="data">The data to sign.</param> |
| | 957 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 958 | | /// <returns> |
| | 959 | | /// The result of the sign operation. The returned <see cref="SignResult"/> contains the signature |
| | 960 | | /// along with all other information needed to verify it. This information should be stored with the signature. |
| | 961 | | /// </returns> |
| | 962 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 963 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 964 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 965 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 966 | | public virtual SignResult SignData(SignatureAlgorithm algorithm, Stream data, CancellationToken cancellationToke |
| | 967 | | { |
| 22 | 968 | | Argument.AssertNotNull(data, nameof(data)); |
| | 969 | |
|
| 20 | 970 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(SignData)}"); |
| 20 | 971 | | scope.AddAttribute("key", _keyId); |
| 20 | 972 | | scope.Start(); |
| | 973 | |
|
| | 974 | | try |
| | 975 | | { |
| 20 | 976 | | byte[] digest = CreateDigest(algorithm, data); |
| | 977 | |
|
| 20 | 978 | | if (_provider is null) |
| | 979 | | { |
| 0 | 980 | | Initialize(nameof(SignData), cancellationToken); |
| | 981 | | } |
| | 982 | |
|
| 20 | 983 | | SignResult result = null; |
| 20 | 984 | | if (_provider.SupportsOperation(KeyOperation.Sign)) |
| | 985 | | { |
| | 986 | | try |
| | 987 | | { |
| 20 | 988 | | result = _provider.Sign(algorithm, digest, cancellationToken); |
| 20 | 989 | | } |
| 0 | 990 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 991 | | { |
| 0 | 992 | | KeysEventSource.Singleton.CryptographicException(nameof(SignData), ex); |
| 0 | 993 | | } |
| | 994 | | } |
| | 995 | |
|
| 20 | 996 | | if (result is null) |
| | 997 | | { |
| 0 | 998 | | result = _remoteProvider.Sign(algorithm, digest, cancellationToken); |
| | 999 | | } |
| | 1000 | |
|
| 20 | 1001 | | return result; |
| | 1002 | | } |
| 0 | 1003 | | catch (Exception e) |
| | 1004 | | { |
| 0 | 1005 | | scope.Failed(e); |
| 0 | 1006 | | throw; |
| | 1007 | | } |
| 20 | 1008 | | } |
| | 1009 | |
|
| | 1010 | | /// <summary> |
| | 1011 | | /// Verifies the specified signature. |
| | 1012 | | /// </summary> |
| | 1013 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 1014 | | /// <param name="data">The data corresponding to the signature.</param> |
| | 1015 | | /// <param name="signature">The signature to verify.</param> |
| | 1016 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 1017 | | /// <returns> |
| | 1018 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 1019 | | /// </returns> |
| | 1020 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 1021 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 1022 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 1023 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 1024 | | public virtual async Task<VerifyResult> VerifyDataAsync(SignatureAlgorithm algorithm, byte[] data, byte[] signat |
| | 1025 | | { |
| 22 | 1026 | | Argument.AssertNotNull(data, nameof(data)); |
| | 1027 | |
|
| 20 | 1028 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(VerifyData)}"); |
| 20 | 1029 | | scope.AddAttribute("key", _keyId); |
| 20 | 1030 | | scope.Start(); |
| | 1031 | |
|
| | 1032 | | try |
| | 1033 | | { |
| 20 | 1034 | | byte[] digest = CreateDigest(algorithm, data); |
| | 1035 | |
|
| 20 | 1036 | | if (_provider is null) |
| | 1037 | | { |
| 0 | 1038 | | await InitializeAsync(nameof(VerifyData), cancellationToken).ConfigureAwait(false); |
| | 1039 | | } |
| | 1040 | |
|
| 20 | 1041 | | VerifyResult result = null; |
| 20 | 1042 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 1043 | | { |
| | 1044 | | try |
| | 1045 | | { |
| 20 | 1046 | | result = await _provider.VerifyAsync(algorithm, digest, signature, cancellationToken).ConfigureA |
| 20 | 1047 | | } |
| 0 | 1048 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 1049 | | { |
| | 1050 | | // Use the non-async name as we do for scope. |
| 0 | 1051 | | KeysEventSource.Singleton.CryptographicException(nameof(VerifyData), ex); |
| 0 | 1052 | | } |
| | 1053 | | } |
| | 1054 | |
|
| 20 | 1055 | | if (result is null) |
| | 1056 | | { |
| 0 | 1057 | | result = await _remoteProvider.VerifyAsync(algorithm, digest, signature, cancellationToken).Configur |
| | 1058 | | } |
| | 1059 | |
|
| 20 | 1060 | | return result; |
| | 1061 | | } |
| 0 | 1062 | | catch (Exception e) |
| | 1063 | | { |
| 0 | 1064 | | scope.Failed(e); |
| 0 | 1065 | | throw; |
| | 1066 | | } |
| 20 | 1067 | | } |
| | 1068 | |
|
| | 1069 | | /// <summary> |
| | 1070 | | /// Verifies the specified signature. |
| | 1071 | | /// </summary> |
| | 1072 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 1073 | | /// <param name="data">The data corresponding to the signature.</param> |
| | 1074 | | /// <param name="signature">The signature to verify.</param> |
| | 1075 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 1076 | | /// <returns> |
| | 1077 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 1078 | | /// </returns> |
| | 1079 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 1080 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 1081 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 1082 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 1083 | | public virtual VerifyResult VerifyData(SignatureAlgorithm algorithm, byte[] data, byte[] signature, Cancellation |
| | 1084 | | { |
| 22 | 1085 | | Argument.AssertNotNull(data, nameof(data)); |
| | 1086 | |
|
| 20 | 1087 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(VerifyData)}"); |
| 20 | 1088 | | scope.AddAttribute("key", _keyId); |
| 20 | 1089 | | scope.Start(); |
| | 1090 | |
|
| | 1091 | | try |
| | 1092 | | { |
| 20 | 1093 | | byte[] digest = CreateDigest(algorithm, data); |
| | 1094 | |
|
| 20 | 1095 | | if (_provider is null) |
| | 1096 | | { |
| 0 | 1097 | | Initialize(nameof(VerifyData), cancellationToken); |
| | 1098 | | } |
| | 1099 | |
|
| 20 | 1100 | | VerifyResult result = null; |
| 20 | 1101 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 1102 | | { |
| | 1103 | | try |
| | 1104 | | { |
| 20 | 1105 | | result = _provider.Verify(algorithm, digest, signature, cancellationToken); |
| 20 | 1106 | | } |
| 0 | 1107 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 1108 | | { |
| 0 | 1109 | | KeysEventSource.Singleton.CryptographicException(nameof(VerifyData), ex); |
| 0 | 1110 | | } |
| | 1111 | | } |
| | 1112 | |
|
| 20 | 1113 | | if (result is null) |
| | 1114 | | { |
| 0 | 1115 | | result = _remoteProvider.Verify(algorithm, digest, signature, cancellationToken); |
| | 1116 | | } |
| | 1117 | |
|
| 20 | 1118 | | return result; |
| | 1119 | | } |
| 0 | 1120 | | catch (Exception e) |
| | 1121 | | { |
| 0 | 1122 | | scope.Failed(e); |
| 0 | 1123 | | throw; |
| | 1124 | | } |
| 20 | 1125 | | } |
| | 1126 | |
|
| | 1127 | | /// <summary> |
| | 1128 | | /// Verifies the specified signature. |
| | 1129 | | /// </summary> |
| | 1130 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 1131 | | /// <param name="data">The data corresponding to the signature.</param> |
| | 1132 | | /// <param name="signature">The signature to verify.</param> |
| | 1133 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 1134 | | /// <returns> |
| | 1135 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 1136 | | /// </returns> |
| | 1137 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 1138 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 1139 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 1140 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 1141 | | public virtual async Task<VerifyResult> VerifyDataAsync(SignatureAlgorithm algorithm, Stream data, byte[] signat |
| | 1142 | | { |
| 22 | 1143 | | Argument.AssertNotNull(data, nameof(data)); |
| | 1144 | |
|
| 20 | 1145 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(VerifyData)}"); |
| 20 | 1146 | | scope.AddAttribute("key", _keyId); |
| 20 | 1147 | | scope.Start(); |
| | 1148 | |
|
| | 1149 | | try |
| | 1150 | | { |
| 20 | 1151 | | byte[] digest = CreateDigest(algorithm, data); |
| | 1152 | |
|
| 20 | 1153 | | if (_provider is null) |
| | 1154 | | { |
| 0 | 1155 | | await InitializeAsync(nameof(VerifyData), cancellationToken).ConfigureAwait(false); |
| | 1156 | | } |
| | 1157 | |
|
| 20 | 1158 | | VerifyResult result = null; |
| 20 | 1159 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 1160 | | { |
| | 1161 | | try |
| | 1162 | | { |
| 20 | 1163 | | result = await _provider.VerifyAsync(algorithm, digest, signature, cancellationToken).ConfigureA |
| 20 | 1164 | | } |
| 0 | 1165 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 1166 | | { |
| | 1167 | | // Use the non-async name as we do for scope. |
| 0 | 1168 | | KeysEventSource.Singleton.CryptographicException(nameof(VerifyData), ex); |
| 0 | 1169 | | } |
| | 1170 | | } |
| | 1171 | |
|
| 20 | 1172 | | if (result is null) |
| | 1173 | | { |
| 0 | 1174 | | result = await _remoteProvider.VerifyAsync(algorithm, digest, signature, cancellationToken).Configur |
| | 1175 | | } |
| | 1176 | |
|
| 20 | 1177 | | return result; |
| | 1178 | | } |
| 0 | 1179 | | catch (Exception e) |
| | 1180 | | { |
| 0 | 1181 | | scope.Failed(e); |
| 0 | 1182 | | throw; |
| | 1183 | | } |
| 20 | 1184 | | } |
| | 1185 | |
|
| | 1186 | | /// <summary> |
| | 1187 | | /// Verifies the specified signature. |
| | 1188 | | /// </summary> |
| | 1189 | | /// <param name="algorithm">The <see cref="SignatureAlgorithm"/> to use. This must be the same algorithm used to |
| | 1190 | | /// <param name="data">The data corresponding to the signature.</param> |
| | 1191 | | /// <param name="signature">The signature to verify.</param> |
| | 1192 | | /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the operation.</param> |
| | 1193 | | /// <returns> |
| | 1194 | | /// The result of the verify operation. If the signature is valid the <see cref="VerifyResult.IsValid"/> propert |
| | 1195 | | /// </returns> |
| | 1196 | | /// <exception cref="ArgumentException">The specified <paramref name="algorithm"/> does not match the key corres |
| | 1197 | | /// <exception cref="ArgumentNullException"><paramref name="data"/> is null.</exception> |
| | 1198 | | /// <exception cref="NotSupportedException">The operation is not supported with the specified key.</exception> |
| | 1199 | | /// <exception cref="RequestFailedException">The server returned an error. See <see cref="Exception.Message"/> f |
| | 1200 | | public virtual VerifyResult VerifyData(SignatureAlgorithm algorithm, Stream data, byte[] signature, Cancellation |
| | 1201 | | { |
| 22 | 1202 | | Argument.AssertNotNull(data, nameof(data)); |
| | 1203 | |
|
| 20 | 1204 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(VerifyData)}"); |
| 20 | 1205 | | scope.AddAttribute("key", _keyId); |
| 20 | 1206 | | scope.Start(); |
| | 1207 | |
|
| | 1208 | | try |
| | 1209 | | { |
| 20 | 1210 | | byte[] digest = CreateDigest(algorithm, data); |
| | 1211 | |
|
| 20 | 1212 | | if (_provider is null) |
| | 1213 | | { |
| 0 | 1214 | | Initialize(nameof(VerifyData), cancellationToken); |
| | 1215 | | } |
| | 1216 | |
|
| 20 | 1217 | | VerifyResult result = null; |
| 20 | 1218 | | if (_provider.SupportsOperation(KeyOperation.Verify)) |
| | 1219 | | { |
| | 1220 | | try |
| | 1221 | | { |
| 20 | 1222 | | result = _provider.Verify(algorithm, digest, signature, cancellationToken); |
| 20 | 1223 | | } |
| 0 | 1224 | | catch (CryptographicException ex) when (_provider.ShouldRemote) |
| | 1225 | | { |
| 0 | 1226 | | KeysEventSource.Singleton.CryptographicException(nameof(VerifyData), ex); |
| 0 | 1227 | | } |
| | 1228 | | } |
| | 1229 | |
|
| 20 | 1230 | | if (result is null) |
| | 1231 | | { |
| 0 | 1232 | | result = _remoteProvider.Verify(algorithm, digest, signature, cancellationToken); |
| | 1233 | | } |
| | 1234 | |
|
| 20 | 1235 | | return result; |
| | 1236 | | } |
| 0 | 1237 | | catch (Exception e) |
| | 1238 | | { |
| 0 | 1239 | | scope.Failed(e); |
| 0 | 1240 | | throw; |
| | 1241 | | } |
| 20 | 1242 | | } |
| | 1243 | |
|
| | 1244 | | /// <inheritdoc/> |
| | 1245 | | byte[] IKeyEncryptionKey.WrapKey(string algorithm, ReadOnlyMemory<byte> key, CancellationToken cancellationToken |
| | 1246 | | { |
| 0 | 1247 | | WrapResult result = WrapKey(new KeyWrapAlgorithm(algorithm), key.ToArray(), cancellationToken); |
| | 1248 | |
|
| 0 | 1249 | | return result.EncryptedKey; |
| | 1250 | | } |
| | 1251 | |
|
| | 1252 | | /// <inheritdoc/> |
| | 1253 | | async Task<byte[]> IKeyEncryptionKey.WrapKeyAsync(string algorithm, ReadOnlyMemory<byte> key, CancellationToken |
| | 1254 | | { |
| 0 | 1255 | | WrapResult result = await WrapKeyAsync(new KeyWrapAlgorithm(algorithm), key.ToArray(), cancellationToken).Co |
| | 1256 | |
|
| 0 | 1257 | | return result.EncryptedKey; |
| 0 | 1258 | | } |
| | 1259 | |
|
| | 1260 | | /// <inheritdoc/> |
| | 1261 | | byte[] IKeyEncryptionKey.UnwrapKey(string algorithm, ReadOnlyMemory<byte> encryptedKey, CancellationToken cancel |
| | 1262 | | { |
| 0 | 1263 | | byte[] buffer = MemoryMarshal.TryGetArray(encryptedKey, out ArraySegment<byte> segment) ? segment.Array : en |
| 0 | 1264 | | UnwrapResult result = UnwrapKey(new KeyWrapAlgorithm(algorithm), buffer, cancellationToken); |
| | 1265 | |
|
| 0 | 1266 | | return result.Key; |
| | 1267 | | } |
| | 1268 | |
|
| | 1269 | | /// <inheritdoc/> |
| | 1270 | | async Task<byte[]> IKeyEncryptionKey.UnwrapKeyAsync(string algorithm, ReadOnlyMemory<byte> encryptedKey, Cancell |
| | 1271 | | { |
| 0 | 1272 | | byte[] buffer = MemoryMarshal.TryGetArray(encryptedKey, out ArraySegment<byte> segment) ? segment.Array : en |
| 0 | 1273 | | UnwrapResult result = await UnwrapKeyAsync(new KeyWrapAlgorithm(algorithm), buffer, cancellationToken).Confi |
| | 1274 | |
|
| 0 | 1275 | | return result.Key; |
| 0 | 1276 | | } |
| | 1277 | |
|
| | 1278 | | private static byte[] CreateDigest(SignatureAlgorithm algorithm, byte[] data) |
| | 1279 | | { |
| 80 | 1280 | | using HashAlgorithm hashAlgo = algorithm.GetHashAlgorithm(); |
| 80 | 1281 | | return hashAlgo.ComputeHash(data); |
| 80 | 1282 | | } |
| | 1283 | |
|
| | 1284 | | private static byte[] CreateDigest(SignatureAlgorithm algorithm, Stream data) |
| | 1285 | | { |
| 80 | 1286 | | using HashAlgorithm hashAlgo = algorithm.GetHashAlgorithm(); |
| 80 | 1287 | | return hashAlgo.ComputeHash(data); |
| 80 | 1288 | | } |
| | 1289 | |
|
| | 1290 | | private async Task InitializeAsync(string operation, CancellationToken cancellationToken) |
| | 1291 | | { |
| 30 | 1292 | | if (_provider != null) |
| | 1293 | | { |
| 0 | 1294 | | return; |
| | 1295 | | } |
| | 1296 | |
|
| 30 | 1297 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Initialize)}"); |
| 30 | 1298 | | scope.AddAttribute("key", _keyId); |
| 30 | 1299 | | scope.Start(); |
| | 1300 | |
|
| | 1301 | | try |
| | 1302 | | { |
| 30 | 1303 | | Response<KeyVaultKey> key = await _remoteProvider.GetKeyAsync(cancellationToken).ConfigureAwait(false); |
| | 1304 | |
|
| 30 | 1305 | | _provider = LocalCryptographyProviderFactory.Create(key.Value); |
| 30 | 1306 | | if (_provider is null) |
| | 1307 | | { |
| 12 | 1308 | | KeysEventSource.Singleton.KeyTypeNotSupported(operation, key.Value); |
| | 1309 | |
|
| 12 | 1310 | | _provider = _remoteProvider; |
| 12 | 1311 | | return; |
| | 1312 | | } |
| 18 | 1313 | | } |
| 0 | 1314 | | catch (RequestFailedException e) when (e.Status == 403) |
| | 1315 | | { |
| 0 | 1316 | | scope.AddAttribute("status", e.Status); |
| | 1317 | |
|
| 0 | 1318 | | _provider = _remoteProvider; |
| 0 | 1319 | | } |
| 0 | 1320 | | catch (Exception e) |
| | 1321 | | { |
| 0 | 1322 | | scope.Failed(e); |
| 0 | 1323 | | throw; |
| | 1324 | | } |
| 30 | 1325 | | } |
| | 1326 | |
|
| | 1327 | | private void Initialize(string operation, CancellationToken cancellationToken) |
| | 1328 | | { |
| 30 | 1329 | | if (_provider != null) |
| | 1330 | | { |
| 0 | 1331 | | return; |
| | 1332 | | } |
| | 1333 | |
|
| 30 | 1334 | | using DiagnosticScope scope = _pipeline.CreateScope($"{nameof(CryptographyClient)}.{nameof(Initialize)}"); |
| 30 | 1335 | | scope.AddAttribute("key", _keyId); |
| 30 | 1336 | | scope.Start(); |
| | 1337 | |
|
| | 1338 | | try |
| | 1339 | | { |
| 30 | 1340 | | Response<KeyVaultKey> key = _remoteProvider.GetKey(cancellationToken); |
| | 1341 | |
|
| 30 | 1342 | | _provider = LocalCryptographyProviderFactory.Create(key.Value); |
| 30 | 1343 | | if (_provider is null) |
| | 1344 | | { |
| 12 | 1345 | | KeysEventSource.Singleton.KeyTypeNotSupported(operation, key.Value); |
| | 1346 | |
|
| 12 | 1347 | | _provider = _remoteProvider; |
| | 1348 | | return; |
| | 1349 | | } |
| 30 | 1350 | | } |
| 0 | 1351 | | catch (RequestFailedException e) when (e.Status == 403) |
| | 1352 | | { |
| 0 | 1353 | | scope.AddAttribute("status", e.Status); |
| | 1354 | |
|
| 0 | 1355 | | _provider = _remoteProvider; |
| 0 | 1356 | | } |
| 0 | 1357 | | catch (Exception e) |
| | 1358 | | { |
| 0 | 1359 | | scope.Failed(e); |
| 0 | 1360 | | throw; |
| | 1361 | | } |
| 30 | 1362 | | } |
| | 1363 | | } |
| | 1364 | | } |