| | | 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.Cryptography |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// An algorithm used for signing and verification. |
| | | 12 | | /// </summary> |
| | | 13 | | public readonly struct SignatureAlgorithm : IEquatable<SignatureAlgorithm> |
| | | 14 | | { |
| | | 15 | | internal const string RS256Value = "RS256"; |
| | | 16 | | internal const string RS384Value = "RS384"; |
| | | 17 | | internal const string RS512Value = "RS512"; |
| | | 18 | | internal const string PS256Value = "PS256"; |
| | | 19 | | internal const string PS384Value = "PS384"; |
| | | 20 | | internal const string PS512Value = "PS512"; |
| | | 21 | | internal const string ES256Value = "ES256"; |
| | | 22 | | internal const string ES384Value = "ES384"; |
| | | 23 | | internal const string ES512Value = "ES512"; |
| | | 24 | | internal const string ES256KValue = "ES256K"; |
| | | 25 | | |
| | | 26 | | private readonly string _value; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="SignatureAlgorithm"/> structure. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="value">The string value of the instance.</param> |
| | | 32 | | public SignatureAlgorithm(string value) |
| | | 33 | | { |
| | 84 | 34 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| | 84 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets an RSA SHA-256 <see cref="SignatureAlgorithm"/>. |
| | | 39 | | /// </summary> |
| | 42 | 40 | | public static SignatureAlgorithm RS256 { get; } = new SignatureAlgorithm(RS256Value); |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets an RSA SHA-384 <see cref="SignatureAlgorithm"/>. |
| | | 44 | | /// </summary> |
| | 34 | 45 | | public static SignatureAlgorithm RS384 { get; } = new SignatureAlgorithm(RS384Value); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets an RSA SHA-512 <see cref="SignatureAlgorithm"/>. |
| | | 49 | | /// </summary> |
| | 34 | 50 | | public static SignatureAlgorithm RS512 { get; } = new SignatureAlgorithm(RS512Value); |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets an RSASSA-PSS using SHA-256 and MGF1 with SHA-256 <see cref="SignatureAlgorithm"/>. |
| | | 54 | | /// </summary> |
| | 54 | 55 | | public static SignatureAlgorithm PS256 { get; } = new SignatureAlgorithm(PS256Value); |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets an RSASSA-PSS using SHA-384 and MGF1 with SHA-384 <see cref="SignatureAlgorithm"/>. |
| | | 59 | | /// </summary> |
| | 50 | 60 | | public static SignatureAlgorithm PS384 { get; } = new SignatureAlgorithm(PS384Value); |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets an RSASSA-PSS using SHA-512 and MGF1 with SHA-512 <see cref="SignatureAlgorithm"/>. |
| | | 64 | | /// </summary> |
| | 50 | 65 | | public static SignatureAlgorithm PS512 { get; } = new SignatureAlgorithm(PS512Value); |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets an ECDSA with a P-256 curve <see cref="SignatureAlgorithm"/>. |
| | | 69 | | /// </summary> |
| | 46 | 70 | | public static SignatureAlgorithm ES256 { get; } = new SignatureAlgorithm(ES256Value); |
| | | 71 | | |
| | | 72 | | /// <summary> |
| | | 73 | | /// Gets an ECDSA with a P-384 curve <see cref="SignatureAlgorithm"/>. |
| | | 74 | | /// </summary> |
| | 46 | 75 | | public static SignatureAlgorithm ES384 { get; } = new SignatureAlgorithm(ES384Value); |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Gets an ECDSA with a P-521 curve <see cref="SignatureAlgorithm"/>. |
| | | 79 | | /// </summary> |
| | 46 | 80 | | public static SignatureAlgorithm ES512 { get; } = new SignatureAlgorithm(ES512Value); |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// Gets an ECDSA with a secp256k1 curve <see cref="SignatureAlgorithm"/>. |
| | | 84 | | /// </summary> |
| | 30 | 85 | | public static SignatureAlgorithm ES256K { get; } = new SignatureAlgorithm(ES256KValue); |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Determines if two <see cref="SignatureAlgorithm"/> values are the same. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="left">The first <see cref="SignatureAlgorithm"/> to compare.</param> |
| | | 91 | | /// <param name="right">The second <see cref="SignatureAlgorithm"/> to compare.</param> |
| | | 92 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur |
| | 0 | 93 | | public static bool operator ==(SignatureAlgorithm left, SignatureAlgorithm right) => left.Equals(right); |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Determines if two <see cref="SignatureAlgorithm"/> values are different. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="left">The first <see cref="SignatureAlgorithm"/> to compare.</param> |
| | | 99 | | /// <param name="right">The second <see cref="SignatureAlgorithm"/> to compare.</param> |
| | | 100 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu |
| | 0 | 101 | | public static bool operator !=(SignatureAlgorithm left, SignatureAlgorithm right) => !left.Equals(right); |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Converts a string to a <see cref="SignatureAlgorithm"/>. |
| | | 105 | | /// </summary> |
| | | 106 | | /// <param name="value">The string value to convert.</param> |
| | 64 | 107 | | public static implicit operator SignatureAlgorithm(string value) => new SignatureAlgorithm(value); |
| | | 108 | | |
| | | 109 | | /// <inheritdoc/> |
| | | 110 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 111 | | public override bool Equals(object obj) => obj is SignatureAlgorithm other && Equals(other); |
| | | 112 | | |
| | | 113 | | /// <inheritdoc/> |
| | 468 | 114 | | public bool Equals(SignatureAlgorithm other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | | 115 | | |
| | | 116 | | /// <inheritdoc/> |
| | | 117 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| | 0 | 118 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | | 119 | | |
| | | 120 | | /// <inheritdoc/> |
| | 1040 | 121 | | public override string ToString() => _value; |
| | | 122 | | |
| | | 123 | | internal HashAlgorithm GetHashAlgorithm() |
| | | 124 | | { |
| | 312 | 125 | | switch (_value) |
| | | 126 | | { |
| | | 127 | | case RS256Value: |
| | | 128 | | case PS256Value: |
| | | 129 | | case ES256Value: |
| | | 130 | | case ES256KValue: |
| | 120 | 131 | | return SHA256.Create(); |
| | | 132 | | |
| | | 133 | | case RS384Value: |
| | | 134 | | case PS384Value: |
| | | 135 | | case ES384Value: |
| | 96 | 136 | | return SHA384.Create(); |
| | | 137 | | |
| | | 138 | | case RS512Value: |
| | | 139 | | case PS512Value: |
| | | 140 | | case ES512Value: |
| | 96 | 141 | | return SHA512.Create(); |
| | | 142 | | |
| | | 143 | | default: |
| | 0 | 144 | | throw new InvalidOperationException("Invalid Algorithm"); |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | |
| | | 148 | | internal HashAlgorithmName GetHashAlgorithmName() |
| | | 149 | | { |
| | 56 | 150 | | switch (_value) |
| | | 151 | | { |
| | | 152 | | case RS256Value: |
| | | 153 | | case PS256Value: |
| | | 154 | | case ES256Value: |
| | | 155 | | case ES256KValue: |
| | 16 | 156 | | return HashAlgorithmName.SHA256; |
| | | 157 | | |
| | | 158 | | case RS384Value: |
| | | 159 | | case PS384Value: |
| | | 160 | | case ES384Value: |
| | 16 | 161 | | return HashAlgorithmName.SHA384; |
| | | 162 | | |
| | | 163 | | case RS512Value: |
| | | 164 | | case PS512Value: |
| | | 165 | | case ES512Value: |
| | 16 | 166 | | return HashAlgorithmName.SHA512; |
| | | 167 | | default: |
| | | 168 | | |
| | 8 | 169 | | return default; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | internal KeyCurveName GetEcKeyCurveName() |
| | | 174 | | { |
| | 90 | 175 | | switch (_value) |
| | | 176 | | { |
| | | 177 | | case ES256Value: |
| | 30 | 178 | | return KeyCurveName.P256; |
| | | 179 | | |
| | | 180 | | case ES256KValue: |
| | 12 | 181 | | return KeyCurveName.P256K; |
| | | 182 | | |
| | | 183 | | case ES384Value: |
| | 24 | 184 | | return KeyCurveName.P384; |
| | | 185 | | |
| | | 186 | | case ES512Value: |
| | 24 | 187 | | return KeyCurveName.P521; |
| | | 188 | | |
| | | 189 | | default: |
| | 0 | 190 | | return KeyCurveName.s_default; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | internal RSASignaturePadding GetRsaSignaturePadding() |
| | | 195 | | { |
| | 48 | 196 | | switch (_value) |
| | | 197 | | { |
| | | 198 | | case RS256Value: |
| | | 199 | | case RS384Value: |
| | | 200 | | case RS512Value: |
| | 24 | 201 | | return RSASignaturePadding.Pkcs1; |
| | | 202 | | |
| | | 203 | | case PS256Value: |
| | | 204 | | case PS384Value: |
| | | 205 | | case PS512Value: |
| | 24 | 206 | | return RSASignaturePadding.Pss; |
| | | 207 | | |
| | | 208 | | default: |
| | 0 | 209 | | return null; |
| | | 210 | | } |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |