| | | 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.Collections.Generic; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | |
| | | 9 | | namespace Microsoft.Azure.KeyVault.WebKey |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Because the current version of ECC is not supporting some of the operations needed for WebKey, |
| | | 13 | | /// those operations are added as ECC extension. |
| | | 14 | | /// </summary> |
| | | 15 | | public static class EccExtension |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Exports EC parameters from a CNG object. |
| | | 19 | | /// </summary> |
| | | 20 | | /// <param name="ecdsa">The CNG object initialized with desired key</param> |
| | | 21 | | /// <param name="includePrivateParameters">Determines whether the private key part is to be exported.</param> |
| | | 22 | | /// <returns></returns> |
| | | 23 | | public static ECParameters ExportParameters( this ECDsa ecdsa, bool includePrivateParameters ) |
| | | 24 | | { |
| | 44 | 25 | | var ecdsaCng = GetEcdsaCng( ecdsa ); |
| | 44 | 26 | | return ECParameters.FromEcdsa( ecdsaCng, includePrivateParameters ); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | public static string[] GetKeyOperations( this ECDsa ecdsa ) |
| | | 30 | | { |
| | 44 | 31 | | var keyUsage = GetEcdsaCng( ecdsa ).Key.KeyUsage; |
| | | 32 | | |
| | 44 | 33 | | if ( !_cngOperations.ContainsKey( keyUsage ) ) |
| | 0 | 34 | | throw new CryptographicException( $"Unknown key usage {keyUsage}" ); |
| | | 35 | | |
| | 44 | 36 | | return (string[]) _cngOperations[keyUsage].Clone(); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | private static ECDsaCng GetEcdsaCng(ECDsa ecdsa ) |
| | | 40 | | { |
| | 88 | 41 | | var ecdsaCng = ecdsa as ECDsaCng; |
| | 88 | 42 | | if ( ecdsaCng == null ) |
| | 0 | 43 | | throw new NotSupportedException( $"This version requires a CNG object." ); |
| | 88 | 44 | | return ecdsaCng; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | private static readonly Dictionary<CngKeyUsages, string[]> _cngOperations; |
| | | 48 | | |
| | | 49 | | static EccExtension() |
| | | 50 | | { |
| | 2 | 51 | | _cngOperations = new Dictionary<CngKeyUsages, string[]> |
| | 2 | 52 | | { |
| | 2 | 53 | | {CngKeyUsages.None, new string[0]}, |
| | 2 | 54 | | {CngKeyUsages.Signing, new[] {JsonWebKeyOperation.Sign, JsonWebKeyOperation.Verify}}, |
| | 2 | 55 | | {CngKeyUsages.Decryption, new[] {JsonWebKeyOperation.Encrypt, JsonWebKeyOperation.Decrypt, JsonWebKeyOpe |
| | 2 | 56 | | {CngKeyUsages.AllUsages, JsonWebKeyOperation.AllOperations} |
| | 2 | 57 | | }; |
| | 2 | 58 | | } |
| | | 59 | | } |
| | | 60 | | } |