| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text.Json; |
| | | 3 | | |
| | | 4 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 5 | | |
| | | 6 | | /// <summary>Represents one trusted public verification-key version.</summary> |
| | | 7 | | public sealed class GovernanceVerificationKey |
| | | 8 | | { |
| | 6 | 9 | | private GovernanceVerificationKey( |
| | 6 | 10 | | string keyId, |
| | 6 | 11 | | string keyVersion, |
| | 6 | 12 | | string signatureAlgorithm, |
| | 6 | 13 | | byte[] subjectPublicKeyInfo, |
| | 6 | 14 | | bool isRevoked) |
| | | 15 | | { |
| | 6 | 16 | | KeyId = keyId; |
| | 6 | 17 | | KeyVersion = keyVersion; |
| | 6 | 18 | | SignatureAlgorithm = signatureAlgorithm; |
| | 6 | 19 | | SubjectPublicKeyInfo = subjectPublicKeyInfo; |
| | 6 | 20 | | IsRevoked = isRevoked; |
| | 6 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary>Gets the stable key identifier.</summary> |
| | | 24 | | public string KeyId { get; } |
| | | 25 | | |
| | | 26 | | /// <summary>Gets the stable key version.</summary> |
| | | 27 | | public string KeyVersion { get; } |
| | | 28 | | |
| | | 29 | | /// <summary>Gets the required signature algorithm descriptor.</summary> |
| | | 30 | | public string SignatureAlgorithm { get; } |
| | | 31 | | |
| | | 32 | | /// <summary>Gets a value indicating whether this version is revoked.</summary> |
| | | 33 | | public bool IsRevoked { get; } |
| | | 34 | | |
| | | 35 | | internal byte[] SubjectPublicKeyInfo { get; } |
| | | 36 | | |
| | | 37 | | /// <summary>Creates a trusted key from an SPKI public-key PEM.</summary> |
| | | 38 | | public static GovernanceVerificationKey FromPem( |
| | | 39 | | string keyId, |
| | | 40 | | string keyVersion, |
| | | 41 | | string signatureAlgorithm, |
| | | 42 | | string publicKeyPem, |
| | | 43 | | bool isRevoked = false, |
| | | 44 | | int minimumRsaKeySize = 2048) |
| | | 45 | | { |
| | 7 | 46 | | ValidateIdentity(keyId, keyVersion, signatureAlgorithm); |
| | 7 | 47 | | ArgumentException.ThrowIfNullOrWhiteSpace(publicKeyPem); |
| | | 48 | | |
| | 7 | 49 | | if (publicKeyPem.Contains("PRIVATE KEY", StringComparison.Ordinal)) |
| | | 50 | | { |
| | 1 | 51 | | throw new ArgumentException("Trusted verification material must not contain a private key.", nameof(publicKe |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | try |
| | | 55 | | { |
| | 6 | 56 | | return signatureAlgorithm switch |
| | 6 | 57 | | { |
| | 5 | 58 | | ManagedKeySignatureAlgorithms.RsaPssSha256 => FromRsaPem( |
| | 5 | 59 | | keyId.Trim(), keyVersion.Trim(), signatureAlgorithm, publicKeyPem, isRevoked, minimumRsaKeySize), |
| | 1 | 60 | | ManagedKeySignatureAlgorithms.EcdsaP256Sha256 => FromEcdsaPem( |
| | 1 | 61 | | keyId.Trim(), keyVersion.Trim(), signatureAlgorithm, publicKeyPem, isRevoked), |
| | 0 | 62 | | _ => throw new ArgumentException("The verification-key signature algorithm is unsupported.", nameof(sign |
| | 6 | 63 | | }; |
| | | 64 | | } |
| | 0 | 65 | | catch (CryptographicException exception) |
| | | 66 | | { |
| | 0 | 67 | | throw new ArgumentException("The verification key is not valid public-key material.", nameof(publicKeyPem), |
| | | 68 | | } |
| | 5 | 69 | | } |
| | | 70 | | |
| | | 71 | | /// <summary>Creates a trusted key from an RSA or EC public JWK.</summary> |
| | | 72 | | public static GovernanceVerificationKey FromJwk( |
| | | 73 | | string keyId, |
| | | 74 | | string keyVersion, |
| | | 75 | | string signatureAlgorithm, |
| | | 76 | | string publicJwk, |
| | | 77 | | bool isRevoked = false, |
| | | 78 | | int minimumRsaKeySize = 2048) |
| | | 79 | | { |
| | 1 | 80 | | ValidateIdentity(keyId, keyVersion, signatureAlgorithm); |
| | 1 | 81 | | ArgumentException.ThrowIfNullOrWhiteSpace(publicJwk); |
| | | 82 | | |
| | 1 | 83 | | using var document = JsonDocument.Parse(publicJwk); |
| | 1 | 84 | | JsonElement root = document.RootElement; |
| | 1 | 85 | | if (root.TryGetProperty("d", out _) |
| | 1 | 86 | | || root.TryGetProperty("p", out _) |
| | 1 | 87 | | || root.TryGetProperty("q", out _) |
| | 1 | 88 | | || root.TryGetProperty("dp", out _) |
| | 1 | 89 | | || root.TryGetProperty("dq", out _) |
| | 1 | 90 | | || root.TryGetProperty("qi", out _)) |
| | | 91 | | { |
| | 0 | 92 | | throw new ArgumentException("Trusted verification material must not contain a private key.", nameof(publicJw |
| | | 93 | | } |
| | | 94 | | |
| | 1 | 95 | | string kty = RequiredString(root, "kty", nameof(publicJwk)); |
| | | 96 | | try |
| | | 97 | | { |
| | 1 | 98 | | if (string.Equals(kty, "RSA", StringComparison.Ordinal) |
| | 1 | 99 | | && signatureAlgorithm == ManagedKeySignatureAlgorithms.RsaPssSha256) |
| | | 100 | | { |
| | 1 | 101 | | using var rsa = RSA.Create(); |
| | 1 | 102 | | rsa.ImportParameters(new RSAParameters |
| | 1 | 103 | | { |
| | 1 | 104 | | Modulus = DecodeBase64Url(RequiredString(root, "n", nameof(publicJwk))), |
| | 1 | 105 | | Exponent = DecodeBase64Url(RequiredString(root, "e", nameof(publicJwk))) |
| | 1 | 106 | | }); |
| | 1 | 107 | | EnsureRsaSize(rsa, minimumRsaKeySize); |
| | 1 | 108 | | return new(keyId.Trim(), keyVersion.Trim(), signatureAlgorithm, rsa.ExportSubjectPublicKeyInfo(), isRevo |
| | | 109 | | } |
| | | 110 | | |
| | 0 | 111 | | if (string.Equals(kty, "EC", StringComparison.Ordinal) |
| | 0 | 112 | | && signatureAlgorithm == ManagedKeySignatureAlgorithms.EcdsaP256Sha256 |
| | 0 | 113 | | && string.Equals(RequiredString(root, "crv", nameof(publicJwk)), "P-256", StringComparison.Ordinal)) |
| | | 114 | | { |
| | 0 | 115 | | using var ecdsa = ECDsa.Create(new ECParameters |
| | 0 | 116 | | { |
| | 0 | 117 | | Curve = ECCurve.NamedCurves.nistP256, |
| | 0 | 118 | | Q = new ECPoint |
| | 0 | 119 | | { |
| | 0 | 120 | | X = DecodeBase64Url(RequiredString(root, "x", nameof(publicJwk))), |
| | 0 | 121 | | Y = DecodeBase64Url(RequiredString(root, "y", nameof(publicJwk))) |
| | 0 | 122 | | } |
| | 0 | 123 | | }); |
| | 0 | 124 | | return new(keyId.Trim(), keyVersion.Trim(), signatureAlgorithm, ecdsa.ExportSubjectPublicKeyInfo(), isRe |
| | | 125 | | } |
| | 0 | 126 | | } |
| | 0 | 127 | | catch (CryptographicException exception) |
| | | 128 | | { |
| | 0 | 129 | | throw new ArgumentException("The JWK is not valid public-key material.", nameof(publicJwk), exception); |
| | | 130 | | } |
| | | 131 | | |
| | 0 | 132 | | throw new ArgumentException("The JWK key type, curve, or signature algorithm is unsupported.", nameof(publicJwk) |
| | 1 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static GovernanceVerificationKey FromRsaPem( |
| | | 136 | | string keyId, string keyVersion, string algorithm, string pem, bool revoked, int minimumRsaKeySize) |
| | | 137 | | { |
| | 5 | 138 | | using var rsa = RSA.Create(); |
| | 5 | 139 | | rsa.ImportFromPem(pem); |
| | 5 | 140 | | EnsureRsaSize(rsa, minimumRsaKeySize); |
| | 4 | 141 | | return new(keyId, keyVersion, algorithm, rsa.ExportSubjectPublicKeyInfo(), revoked); |
| | 4 | 142 | | } |
| | | 143 | | |
| | | 144 | | private static GovernanceVerificationKey FromEcdsaPem( |
| | | 145 | | string keyId, string keyVersion, string algorithm, string pem, bool revoked) |
| | | 146 | | { |
| | 1 | 147 | | using var ecdsa = ECDsa.Create(); |
| | 1 | 148 | | ecdsa.ImportFromPem(pem); |
| | 1 | 149 | | return ecdsa.KeySize != 256 |
| | 1 | 150 | | ? throw new ArgumentException("ECDSA verification keys must use the P-256 curve.", nameof(pem)) |
| | 1 | 151 | | : new(keyId, keyVersion, algorithm, ecdsa.ExportSubjectPublicKeyInfo(), revoked); |
| | 1 | 152 | | } |
| | | 153 | | |
| | | 154 | | private static void EnsureRsaSize(RSA rsa, int minimumRsaKeySize) |
| | | 155 | | { |
| | 6 | 156 | | ArgumentOutOfRangeException.ThrowIfLessThan(minimumRsaKeySize, 2048); |
| | 6 | 157 | | if (rsa.KeySize < minimumRsaKeySize) |
| | | 158 | | { |
| | 1 | 159 | | throw new ArgumentException($"RSA verification keys must be at least {minimumRsaKeySize} bits."); |
| | | 160 | | } |
| | 5 | 161 | | } |
| | | 162 | | |
| | | 163 | | private static void ValidateIdentity(string keyId, string keyVersion, string signatureAlgorithm) |
| | | 164 | | { |
| | 8 | 165 | | ArgumentException.ThrowIfNullOrWhiteSpace(keyId); |
| | 8 | 166 | | ArgumentException.ThrowIfNullOrWhiteSpace(keyVersion); |
| | 8 | 167 | | ArgumentException.ThrowIfNullOrWhiteSpace(signatureAlgorithm); |
| | 8 | 168 | | } |
| | | 169 | | |
| | | 170 | | private static string RequiredString(JsonElement root, string name, string parameterName) |
| | | 171 | | { |
| | 3 | 172 | | return !root.TryGetProperty(name, out JsonElement value) || value.ValueKind != JsonValueKind.String |
| | 3 | 173 | | || string.IsNullOrWhiteSpace(value.GetString()) |
| | 3 | 174 | | ? throw new ArgumentException($"The JWK requires a non-empty '{name}' member.", parameterName) |
| | 3 | 175 | | : value.GetString()!; |
| | | 176 | | } |
| | | 177 | | |
| | | 178 | | private static byte[] DecodeBase64Url(string value) |
| | | 179 | | { |
| | 2 | 180 | | string padded = value.Replace('-', '+').Replace('_', '/'); |
| | 2 | 181 | | padded = padded.PadRight(padded.Length + ((4 - (padded.Length % 4)) % 4), '='); |
| | 2 | 182 | | return Convert.FromBase64String(padded); |
| | | 183 | | } |
| | | 184 | | } |