| | | 1 | | using System.Security.Cryptography; |
| | | 2 | | using System.Text; |
| | | 3 | | using AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 6 | | |
| | | 7 | | /// <summary>Verifies governance signatures with trusted public keys only.</summary> |
| | | 8 | | public sealed class PublicKeySignatureVerificationService : IGovernanceSignatureVerificationService |
| | | 9 | | { |
| | | 10 | | private readonly KeySignatureVerificationOptions options; |
| | | 11 | | private readonly IGovernanceVerificationKeyResolver resolver; |
| | | 12 | | |
| | | 13 | | /// <summary>Initializes a public-key verifier.</summary> |
| | 5 | 14 | | public PublicKeySignatureVerificationService( |
| | 5 | 15 | | KeySignatureVerificationOptions options, |
| | 5 | 16 | | IGovernanceVerificationKeyResolver resolver) |
| | | 17 | | { |
| | 5 | 18 | | ArgumentNullException.ThrowIfNull(options); |
| | 5 | 19 | | ArgumentNullException.ThrowIfNull(resolver); |
| | 5 | 20 | | options.Validate(); |
| | 5 | 21 | | this.options = options; |
| | 5 | 22 | | this.resolver = resolver; |
| | 5 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async ValueTask<SignatureVerificationResult> VerifyAsync( |
| | | 27 | | SignatureVerificationRequest request, |
| | | 28 | | CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 8 | 30 | | ArgumentNullException.ThrowIfNull(request); |
| | 8 | 31 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 32 | | |
| | 8 | 33 | | SigningMetadata metadata = request.SigningMetadata; |
| | 8 | 34 | | if (!metadata.HasSignature) |
| | | 35 | | { |
| | 0 | 36 | | return SignatureVerificationResult.MissingSignature(); |
| | | 37 | | } |
| | | 38 | | |
| | 8 | 39 | | if (metadata.SigningHash is null || !FixedTimeEquals(request.SigningHash, metadata.SigningHash)) |
| | | 40 | | { |
| | 0 | 41 | | return Fail("signature.hash-mismatch", SignatureVerificationCategory.HashMismatch); |
| | | 42 | | } |
| | | 43 | | |
| | 8 | 44 | | if (request.UsesLegacySignatureInput && !options.AllowLegacySignatureInput) |
| | | 45 | | { |
| | 0 | 46 | | return Fail("signature.legacy-input-not-allowed", SignatureVerificationCategory.UntrustedSigningContext); |
| | | 47 | | } |
| | | 48 | | |
| | 8 | 49 | | if (metadata.SignatureAlgorithm is null || !options.AllowedAlgorithms.Contains(metadata.SignatureAlgorithm)) |
| | | 50 | | { |
| | 1 | 51 | | return Fail("signature.algorithm-unsupported", SignatureVerificationCategory.UnsupportedAlgorithm); |
| | | 52 | | } |
| | | 53 | | |
| | 7 | 54 | | if (metadata.KeyId is null) |
| | | 55 | | { |
| | 0 | 56 | | return Fail("signature.key-unknown", SignatureVerificationCategory.UnknownKeyVersion); |
| | | 57 | | } |
| | | 58 | | |
| | 7 | 59 | | if (metadata.KeyVersion is null) |
| | | 60 | | { |
| | 0 | 61 | | return Fail("signature.key-version-unknown", SignatureVerificationCategory.UnknownKeyVersion); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | GovernanceVerificationKey? key; |
| | | 65 | | try |
| | | 66 | | { |
| | 7 | 67 | | key = await resolver.ResolveAsync(metadata.KeyId, metadata.KeyVersion, cancellationToken).ConfigureAwait(fal |
| | 7 | 68 | | } |
| | 0 | 69 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 70 | | { |
| | 0 | 71 | | throw; |
| | | 72 | | } |
| | 0 | 73 | | catch (Exception exception) when (exception is InvalidOperationException or TimeoutException) |
| | | 74 | | { |
| | 0 | 75 | | return SignatureVerificationResult.Failed( |
| | 0 | 76 | | "signature.key-provider-unavailable", |
| | 0 | 77 | | SignatureVerificationCategory.ProviderUnavailable, |
| | 0 | 78 | | "The verification key resolver could not answer the request."); |
| | | 79 | | } |
| | | 80 | | |
| | 7 | 81 | | if (key is null) |
| | | 82 | | { |
| | | 83 | | bool knownKey; |
| | | 84 | | try |
| | | 85 | | { |
| | 2 | 86 | | knownKey = await resolver.IsKnownKeyAsync(metadata.KeyId, cancellationToken).ConfigureAwait(false); |
| | 2 | 87 | | } |
| | 0 | 88 | | catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested) |
| | | 89 | | { |
| | 0 | 90 | | throw; |
| | | 91 | | } |
| | 0 | 92 | | catch (Exception exception) when (exception is InvalidOperationException or TimeoutException) |
| | | 93 | | { |
| | 0 | 94 | | return SignatureVerificationResult.Failed( |
| | 0 | 95 | | "signature.key-provider-unavailable", |
| | 0 | 96 | | SignatureVerificationCategory.ProviderUnavailable, |
| | 0 | 97 | | "The verification key resolver could not answer the request."); |
| | | 98 | | } |
| | | 99 | | |
| | 2 | 100 | | return Fail( |
| | 2 | 101 | | knownKey ? "signature.key-version-unknown" : "signature.key-unknown", |
| | 2 | 102 | | SignatureVerificationCategory.UnknownKeyVersion); |
| | | 103 | | } |
| | | 104 | | |
| | 5 | 105 | | if (key.IsRevoked) |
| | | 106 | | { |
| | 1 | 107 | | return Fail("signature.key-revoked", SignatureVerificationCategory.RevokedKey); |
| | | 108 | | } |
| | | 109 | | |
| | 4 | 110 | | if (!string.Equals(key.SignatureAlgorithm, metadata.SignatureAlgorithm, StringComparison.Ordinal)) |
| | | 111 | | { |
| | 0 | 112 | | return Fail("signature.algorithm-unsupported", SignatureVerificationCategory.UnsupportedAlgorithm); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | byte[] signature; |
| | | 116 | | try |
| | | 117 | | { |
| | 4 | 118 | | signature = Convert.FromBase64String(metadata.Signature!); |
| | 4 | 119 | | } |
| | 0 | 120 | | catch (FormatException) |
| | | 121 | | { |
| | 0 | 122 | | return Fail("signature.invalid", SignatureVerificationCategory.InvalidSignature); |
| | | 123 | | } |
| | | 124 | | |
| | | 125 | | bool verified; |
| | | 126 | | try |
| | | 127 | | { |
| | 4 | 128 | | verified = Verify(key, request.SignatureInput.Span, signature); |
| | 4 | 129 | | } |
| | 0 | 130 | | catch (CryptographicException) |
| | | 131 | | { |
| | 0 | 132 | | verified = false; |
| | 0 | 133 | | } |
| | | 134 | | |
| | 4 | 135 | | return verified |
| | 4 | 136 | | ? SignatureVerificationResult.Verified() |
| | 4 | 137 | | : Fail("signature.invalid", SignatureVerificationCategory.InvalidSignature); |
| | 8 | 138 | | } |
| | | 139 | | |
| | | 140 | | private static bool Verify(GovernanceVerificationKey key, ReadOnlySpan<byte> input, ReadOnlySpan<byte> signature) |
| | | 141 | | { |
| | 4 | 142 | | if (key.SignatureAlgorithm == ManagedKeySignatureAlgorithms.RsaPssSha256) |
| | | 143 | | { |
| | 3 | 144 | | using var rsa = RSA.Create(); |
| | 3 | 145 | | rsa.ImportSubjectPublicKeyInfo(key.SubjectPublicKeyInfo, out _); |
| | 3 | 146 | | return rsa.VerifyData(input, signature, HashAlgorithmName.SHA256, RSASignaturePadding.Pss); |
| | | 147 | | } |
| | | 148 | | |
| | 1 | 149 | | using var ecdsa = ECDsa.Create(); |
| | 1 | 150 | | ecdsa.ImportSubjectPublicKeyInfo(key.SubjectPublicKeyInfo, out _); |
| | 1 | 151 | | return ecdsa.VerifyData(input, signature, HashAlgorithmName.SHA256); |
| | 4 | 152 | | } |
| | | 153 | | |
| | | 154 | | private static bool FixedTimeEquals(string left, string right) |
| | | 155 | | { |
| | 8 | 156 | | byte[] leftBytes = Encoding.UTF8.GetBytes(left); |
| | 8 | 157 | | byte[] rightBytes = Encoding.UTF8.GetBytes(right); |
| | 8 | 158 | | return leftBytes.Length == rightBytes.Length |
| | 8 | 159 | | && CryptographicOperations.FixedTimeEquals(leftBytes, rightBytes); |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | private static SignatureVerificationResult Fail(string code, SignatureVerificationCategory category) |
| | | 163 | | { |
| | 4 | 164 | | return SignatureVerificationResult.Failed(code, category); |
| | | 165 | | } |
| | | 166 | | } |