| | | 1 | | namespace AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Provides provider-neutral helpers for verifying signed governance artifacts and applying verification policy. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// The verifier wrapper does not resolve provider-specific keys in Core and does not imply legal evidence, compliance c |
| | | 8 | | /// </remarks> |
| | | 9 | | public static class GovernanceArtifactVerifier |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Verifies a signed governance artifact and maps the result to a host-facing policy outcome. |
| | | 13 | | /// </summary> |
| | | 14 | | public static async ValueTask<VerificationPolicyOutcome> VerifyAsync<TArtifact>( |
| | | 15 | | SignedGovernanceArtifact<TArtifact> artifact, |
| | | 16 | | IAsiBackboneSignatureVerificationService verificationService, |
| | | 17 | | VerificationPolicyOptions? options = null, |
| | | 18 | | VerificationPolicyContext? context = null, |
| | | 19 | | CancellationToken cancellationToken = default) |
| | | 20 | | { |
| | 32 | 21 | | ArgumentNullException.ThrowIfNull(artifact); |
| | 32 | 22 | | ArgumentNullException.ThrowIfNull(verificationService); |
| | 32 | 23 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 24 | | |
| | 32 | 25 | | VerificationPolicyContext effectiveContext = context ?? VerificationPolicyContext.Default; |
| | 32 | 26 | | SignatureVerificationResult? preflightResult = ValidateBeforeProvider(artifact, effectiveContext); |
| | | 27 | | |
| | 32 | 28 | | if (preflightResult is not null) |
| | | 29 | | { |
| | 15 | 30 | | return VerificationPolicyEvaluator.Evaluate(artifact, preflightResult, options); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | try |
| | | 34 | | { |
| | 17 | 35 | | SignatureVerificationResult verificationResult = await verificationService |
| | 17 | 36 | | .VerifyAsync( |
| | 17 | 37 | | new SignatureVerificationRequest( |
| | 17 | 38 | | artifact.SigningHash, |
| | 17 | 39 | | artifact.SigningMetadata, |
| | 17 | 40 | | purpose: effectiveContext.Purpose ?? artifact.ArtifactType, |
| | 17 | 41 | | metadata: effectiveContext.Metadata), |
| | 17 | 42 | | cancellationToken) |
| | 17 | 43 | | .ConfigureAwait(false); |
| | | 44 | | |
| | 13 | 45 | | return VerificationPolicyEvaluator.Evaluate(artifact, verificationResult, options); |
| | | 46 | | } |
| | 2 | 47 | | catch (InvalidOperationException exception) |
| | | 48 | | { |
| | 2 | 49 | | return CreateProviderUnavailableOutcome(artifact, options, exception); |
| | | 50 | | } |
| | 1 | 51 | | catch (NotSupportedException exception) |
| | | 52 | | { |
| | 1 | 53 | | return CreateProviderUnavailableOutcome(artifact, options, exception); |
| | | 54 | | } |
| | 1 | 55 | | catch (TimeoutException exception) |
| | | 56 | | { |
| | 1 | 57 | | return CreateProviderUnavailableOutcome(artifact, options, exception); |
| | | 58 | | } |
| | 32 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static VerificationPolicyOutcome CreateProviderUnavailableOutcome<TArtifact>( |
| | | 62 | | SignedGovernanceArtifact<TArtifact> artifact, |
| | | 63 | | VerificationPolicyOptions? options, |
| | | 64 | | Exception exception) |
| | | 65 | | { |
| | 4 | 66 | | var providerUnavailableResult = SignatureVerificationResult.Failed( |
| | 4 | 67 | | "signature.provider-unavailable", |
| | 4 | 68 | | exception.GetType().Name); |
| | | 69 | | |
| | 4 | 70 | | return VerificationPolicyEvaluator.Evaluate(artifact, providerUnavailableResult, options); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | private static SignatureVerificationResult? ValidateBeforeProvider<TArtifact>( |
| | | 74 | | SignedGovernanceArtifact<TArtifact> artifact, |
| | | 75 | | VerificationPolicyContext context) |
| | | 76 | | { |
| | 32 | 77 | | SigningMetadata metadata = artifact.SigningMetadata; |
| | | 78 | | |
| | 32 | 79 | | return artifact.HasNoSignature || !metadata.HasSignature |
| | 32 | 80 | | ? SignatureVerificationResult.MissingSignature("The governance artifact does not carry signature metadata.") |
| | 32 | 81 | | : string.IsNullOrWhiteSpace(metadata.SigningHash) |
| | 32 | 82 | | ? SignatureVerificationResult.MissingSignature("The governance artifact does not carry the hash that was sig |
| | 32 | 83 | | : !string.Equals(metadata.SigningHash, artifact.SigningHash, StringComparison.Ordinal) |
| | 32 | 84 | | ? SignatureVerificationResult.Failed( |
| | 32 | 85 | | "signature.hash-mismatch", |
| | 32 | 86 | | "The signing metadata hash does not match the canonical artifact hash.") |
| | 32 | 87 | | : metadata.HashAlgorithm is not null |
| | 32 | 88 | | && !string.Equals(metadata.HashAlgorithm, artifact.HashAlgorithm, StringComparison.OrdinalIgnoreCase) |
| | 32 | 89 | | ? SignatureVerificationResult.Failed( |
| | 32 | 90 | | "signature.hash-algorithm-unsupported", |
| | 32 | 91 | | "The signing metadata hash algorithm does not match the canonical artifact hash algorithm.") |
| | 32 | 92 | | : context.RequiredHashAlgorithm is not null |
| | 32 | 93 | | && !string.Equals(context.RequiredHashAlgorithm, artifact.HashAlgorithm, StringComparison.OrdinalIgnoreCase) |
| | 32 | 94 | | ? SignatureVerificationResult.Failed( |
| | 32 | 95 | | "signature.hash-algorithm-unsupported", |
| | 32 | 96 | | "The canonical artifact hash algorithm does not match the required verification policy algorithm.") |
| | 32 | 97 | | : !MatchesCanonicalMetadata(metadata, "artifact_id", artifact.ArtifactId) |
| | 32 | 98 | | || !MatchesCanonicalMetadata(metadata, "artifact_type", artifact.ArtifactType) |
| | 32 | 99 | | || !MatchesCanonicalMetadata(metadata, "canonicalization_version", artifact.CanonicalHash.CanonicalizationVe |
| | 32 | 100 | | || !MatchesCanonicalMetadata(metadata, "payload_schema_version", artifact.CanonicalHash.PayloadSchemaVersion |
| | 32 | 101 | | ? SignatureVerificationResult.Failed( |
| | 32 | 102 | | "signature.canonicalization-mismatch", |
| | 32 | 103 | | "The signing metadata canonical artifact descriptors do not match the artifact being verified.") |
| | 32 | 104 | | : context.ExpectedKeyId is not null |
| | 32 | 105 | | && !string.Equals(context.ExpectedKeyId, metadata.KeyId, StringComparison.Ordinal) |
| | 32 | 106 | | ? SignatureVerificationResult.Failed( |
| | 32 | 107 | | "signature.key-version-unknown", |
| | 32 | 108 | | "The signing key identifier does not match the verification policy expectation.") |
| | 32 | 109 | | : context.ExpectedKeyVersion is not null |
| | 32 | 110 | | && !string.Equals(context.ExpectedKeyVersion, metadata.KeyVersion, StringComparison.Ordinal) |
| | 32 | 111 | | ? SignatureVerificationResult.Failed( |
| | 32 | 112 | | "signature.key-version-unknown", |
| | 32 | 113 | | "The signing key version does not match the verification policy expectation.") |
| | 32 | 114 | | : context.RequiredProvider is not null |
| | 32 | 115 | | && !string.Equals(context.RequiredProvider, metadata.Provider, StringComparison.Ordinal) |
| | 32 | 116 | | ? SignatureVerificationResult.Failed( |
| | 32 | 117 | | "signature.provider-unavailable", |
| | 32 | 118 | | "The signing provider does not match the required verification policy provider.") |
| | 32 | 119 | | : !MatchesOptionalPolicyMetadata(metadata, "policy_version", context.ExpectedPolicyVersion) |
| | 32 | 120 | | || !MatchesOptionalPolicyMetadata(metadata, "policy_hash", context.ExpectedPolicyHash) |
| | 32 | 121 | | ? SignatureVerificationResult.Failed( |
| | 32 | 122 | | "signature.canonicalization-mismatch", |
| | 32 | 123 | | "The signing metadata policy context does not match the verification policy expectation.") |
| | 32 | 124 | | : null; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | private static bool MatchesCanonicalMetadata(SigningMetadata metadata, string key, string expectedValue) |
| | | 128 | | { |
| | 98 | 129 | | return !metadata.Metadata.TryGetValue(key, out string? value) |
| | 98 | 130 | | || string.Equals(value, expectedValue, StringComparison.Ordinal); |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | private static bool MatchesOptionalPolicyMetadata(SigningMetadata metadata, string key, string? expectedValue) |
| | | 134 | | { |
| | 37 | 135 | | return expectedValue is null |
| | 37 | 136 | | || (metadata.Metadata.TryGetValue(key, out string? value) |
| | 37 | 137 | | && string.Equals(value, expectedValue, StringComparison.Ordinal)); |
| | | 138 | | } |
| | | 139 | | } |