| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Security.Cryptography; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Elliptic Curve Cryptography (ECC) curve names. |
| | 12 | | /// </summary> |
| | 13 | | public readonly struct KeyCurveName : IEquatable<KeyCurveName> |
| | 14 | | { |
| | 15 | | private const string P256Value = "P-256"; |
| | 16 | | private const string P256KValue = "P-256K"; |
| | 17 | | private const string P384Value = "P-384"; |
| | 18 | | private const string P521Value = "P-521"; |
| | 19 | |
|
| | 20 | | private const string P256OidValue = "1.2.840.10045.3.1.7"; |
| | 21 | | private const string P256KOidValue = "1.3.132.0.10"; |
| | 22 | | private const string P384OidValue = "1.3.132.0.34"; |
| | 23 | | private const string P521OidValue = "1.3.132.0.35"; |
| | 24 | |
|
| | 25 | | private readonly string _value; |
| | 26 | |
|
| | 27 | | /// <summary> |
| | 28 | | /// Initializes a new instance of the <see cref="KeyCurveName"/> structure. |
| | 29 | | /// </summary> |
| | 30 | | /// <param name="value">The string value of the instance.</param> |
| | 31 | | public KeyCurveName(string value) |
| | 32 | | { |
| 526 | 33 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| 526 | 34 | | } |
| | 35 | |
|
| | 36 | | /// <summary> |
| | 37 | | /// Gets the NIST P-256 elliptic curve, AKA SECG curve SECP256R1 |
| | 38 | | /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types" |
| | 39 | | /// </summary> |
| 110 | 40 | | public static KeyCurveName P256 { get; } = new KeyCurveName(P256Value); |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the SECG SECP256K1 elliptic curve. |
| | 44 | | /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types" |
| | 45 | | /// </summary> |
| 58 | 46 | | public static KeyCurveName P256K { get; } = new KeyCurveName(P256KValue); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets the NIST P-384 elliptic curve, AKA SECG curve SECP384R1. |
| | 50 | | /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types" |
| | 51 | | /// </summary> |
| 82 | 52 | | public static KeyCurveName P384 { get; } = new KeyCurveName(P384Value); |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Gets the NIST P-521 elliptic curve, AKA SECG curve SECP521R1. |
| | 56 | | /// For more information, see <see href="https://docs.microsoft.com/azure/key-vault/keys/about-keys#curve-types" |
| | 57 | | /// </summary> |
| 108 | 58 | | public static KeyCurveName P521 { get; } = new KeyCurveName(P521Value); |
| | 59 | |
|
| 2 | 60 | | internal static readonly KeyCurveName s_default = default; |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Determines if two <see cref="KeyCurveName"/> values are the same. |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="left">The first <see cref="KeyCurveName"/> to compare.</param> |
| | 66 | | /// <param name="right">The second <see cref="KeyCurveName"/> to compare.</param> |
| | 67 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur |
| 52 | 68 | | public static bool operator ==(KeyCurveName left, KeyCurveName right) => left.Equals(right); |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// Determines if two <see cref="KeyCurveName"/> values are different. |
| | 72 | | /// </summary> |
| | 73 | | /// <param name="left">The first <see cref="KeyCurveName"/> to compare.</param> |
| | 74 | | /// <param name="right">The second <see cref="KeyCurveName"/> to compare.</param> |
| | 75 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu |
| 38 | 76 | | public static bool operator !=(KeyCurveName left, KeyCurveName right) => !left.Equals(right); |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Converts a string to a <see cref="KeyCurveName"/>. |
| | 80 | | /// </summary> |
| | 81 | | /// <param name="value">The string value to convert.</param> |
| 518 | 82 | | public static implicit operator KeyCurveName(string value) => new KeyCurveName(value); |
| | 83 | |
|
| | 84 | | /// <inheritdoc/> |
| | 85 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 86 | | public override bool Equals(object obj) => obj is KeyCurveName other && Equals(other); |
| | 87 | |
|
| | 88 | | /// <inheritdoc/> |
| 198 | 89 | | public bool Equals(KeyCurveName other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | 90 | |
|
| | 91 | | /// <inheritdoc/> |
| | 92 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 93 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | 94 | |
|
| | 95 | | /// <inheritdoc/> |
| 308 | 96 | | public override string ToString() => _value; |
| | 97 | |
|
| | 98 | | internal static KeyCurveName FromOid(Oid oid, int keySize = 0) |
| | 99 | | { |
| 128 | 100 | | if (!string.IsNullOrEmpty(oid?.Value)) |
| | 101 | | { |
| 10 | 102 | | if (string.Equals(oid.Value, P521OidValue, StringComparison.Ordinal)) |
| | 103 | | { |
| 2 | 104 | | return P521; |
| | 105 | | } |
| | 106 | |
|
| 8 | 107 | | if (string.Equals(oid.Value, P384OidValue, StringComparison.Ordinal)) |
| | 108 | | { |
| 2 | 109 | | return P384; |
| | 110 | | } |
| | 111 | |
|
| 6 | 112 | | if (string.Equals(oid.Value, P256KOidValue, StringComparison.Ordinal)) |
| | 113 | | { |
| 2 | 114 | | return P256K; |
| | 115 | | } |
| | 116 | |
|
| 4 | 117 | | if (string.Equals(oid.Value, P256OidValue, StringComparison.Ordinal)) |
| | 118 | | { |
| 2 | 119 | | return P256; |
| | 120 | | } |
| | 121 | | } |
| | 122 | |
|
| 120 | 123 | | if (!string.IsNullOrEmpty(oid?.FriendlyName)) |
| | 124 | | { |
| 114 | 125 | | switch (keySize) |
| | 126 | | { |
| 48 | 127 | | case 521 when string.Equals(oid.FriendlyName, "nistP521", StringComparison.OrdinalIgnoreCase) |
| 48 | 128 | | || string.Equals(oid.FriendlyName, "secp521r1", StringComparison.OrdinalIgnoreCase) |
| 48 | 129 | | || string.Equals(oid.FriendlyName, "ECDSA_P521", StringComparison.OrdinalIgnoreCase): |
| 48 | 130 | | return P521; |
| | 131 | |
|
| 22 | 132 | | case 384 when string.Equals(oid.FriendlyName, "nistP384", StringComparison.OrdinalIgnoreCase) |
| 22 | 133 | | || string.Equals(oid.FriendlyName, "secp384r1", StringComparison.OrdinalIgnoreCase) |
| 22 | 134 | | || string.Equals(oid.FriendlyName, "ECDSA_P384", StringComparison.OrdinalIgnoreCase): |
| 22 | 135 | | return P384; |
| | 136 | |
|
| 42 | 137 | | case 256 when string.Equals(oid.FriendlyName, "secp256k1", StringComparison.OrdinalIgnoreCase): |
| 14 | 138 | | return P256K; |
| | 139 | |
|
| 28 | 140 | | case 256 when string.Equals(oid.FriendlyName, "nistP256", StringComparison.OrdinalIgnoreCase) |
| 28 | 141 | | || string.Equals(oid.FriendlyName, "secp256r1", StringComparison.OrdinalIgnoreCase) |
| 28 | 142 | | || string.Equals(oid.FriendlyName, "ECDSA_P256", StringComparison.OrdinalIgnoreCase): |
| 28 | 143 | | return P256; |
| | 144 | | } |
| | 145 | | } |
| | 146 | |
|
| 8 | 147 | | return s_default; |
| | 148 | | } |
| | 149 | |
|
| | 150 | | internal bool IsSupported |
| | 151 | | { |
| | 152 | | get |
| | 153 | | { |
| 124 | 154 | | switch (_value) |
| | 155 | | { |
| | 156 | | case P256Value: |
| | 157 | | case P256KValue: |
| | 158 | | case P384Value: |
| | 159 | | case P521Value: |
| 108 | 160 | | return true; |
| | 161 | |
|
| | 162 | | default: |
| 16 | 163 | | return false; |
| | 164 | | } |
| | 165 | | } |
| | 166 | | } |
| | 167 | |
|
| 82 | 168 | | internal int KeyParameterSize => _value switch |
| 82 | 169 | | { |
| 106 | 170 | | P256Value => 32, |
| 88 | 171 | | P256KValue => 32, |
| 96 | 172 | | P384Value => 48, |
| 98 | 173 | | P521Value => 66, |
| 104 | 174 | | _ => 0, |
| 82 | 175 | | }; |
| 262 | 176 | | internal int KeySize => _value switch |
| 262 | 177 | | { |
| 332 | 178 | | P256Value => 256, |
| 304 | 179 | | P256KValue => 256, |
| 328 | 180 | | P384Value => 384, |
| 328 | 181 | | P521Value => 521, |
| 280 | 182 | | _ => 0, |
| 262 | 183 | | }; |
| | 184 | |
|
| 90 | 185 | | internal Oid Oid => _value switch |
| 90 | 186 | | { |
| 118 | 187 | | P256Value => new Oid(P256OidValue), |
| 96 | 188 | | P256KValue => new Oid(P256KOidValue), |
| 108 | 189 | | P384Value => new Oid(P384OidValue), |
| 110 | 190 | | P521Value => new Oid(P521OidValue), |
| 108 | 191 | | _ => null, |
| 90 | 192 | | }; |
| | 193 | |
|
| | 194 | | } |
| | 195 | | } |