| | | 1 | | using AsiBackbone.Core.Signing; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Integrity; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Verifies provider-neutral append-only audit integrity chains. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class AuditIntegrityVerifier |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Verifies that the supplied links form one continuous append-only chain in the supplied order. |
| | | 12 | | /// </summary> |
| | | 13 | | public static AuditIntegrityVerificationResult Verify( |
| | | 14 | | IEnumerable<AuditIntegrityLink> links, |
| | | 15 | | string? expectedChainId = null, |
| | | 16 | | bool requireGenesis = true) |
| | | 17 | | { |
| | 10 | 18 | | ArgumentNullException.ThrowIfNull(links); |
| | | 19 | | |
| | 10 | 20 | | List<AuditIntegrityLink> orderedLinks = [.. links]; |
| | | 21 | | |
| | 10 | 22 | | if (orderedLinks.Count == 0) |
| | | 23 | | { |
| | 1 | 24 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 25 | | AuditIntegrityVerificationCategory.EmptyChain, |
| | 1 | 26 | | "integrity.chain-empty", |
| | 1 | 27 | | "No integrity links were supplied."); |
| | | 28 | | } |
| | | 29 | | |
| | 9 | 30 | | string chainId = string.IsNullOrWhiteSpace(expectedChainId) |
| | 9 | 31 | | ? orderedLinks[0].ChainId |
| | 9 | 32 | | : expectedChainId.Trim(); |
| | 9 | 33 | | HashSet<long> observedSequences = []; |
| | 9 | 34 | | string expectedPreviousHash = string.Empty; |
| | 9 | 35 | | long expectedSequence = requireGenesis ? 1 : orderedLinks[0].Sequence; |
| | | 36 | | |
| | 41 | 37 | | foreach (AuditIntegrityLink link in orderedLinks) |
| | | 38 | | { |
| | 15 | 39 | | AuditIntegrityVerificationResult? result = VerifyLink( |
| | 15 | 40 | | link, |
| | 15 | 41 | | chainId, |
| | 15 | 42 | | expectedSequence, |
| | 15 | 43 | | expectedPreviousHash, |
| | 15 | 44 | | observedSequences, |
| | 15 | 45 | | requireGenesis); |
| | | 46 | | |
| | 15 | 47 | | if (result is not null) |
| | | 48 | | { |
| | 7 | 49 | | return result; |
| | | 50 | | } |
| | | 51 | | |
| | 8 | 52 | | _ = observedSequences.Add(link.Sequence); |
| | 8 | 53 | | expectedPreviousHash = link.LinkHash; |
| | 8 | 54 | | expectedSequence = link.Sequence + 1; |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | AuditIntegrityLink tip = orderedLinks[^1]; |
| | 2 | 58 | | return AuditIntegrityVerificationResult.Valid(chainId, orderedLinks.Count, tip.LinkHash); |
| | 7 | 59 | | } |
| | | 60 | | |
| | | 61 | | private static AuditIntegrityVerificationResult? VerifyLink( |
| | | 62 | | AuditIntegrityLink link, |
| | | 63 | | string expectedChainId, |
| | | 64 | | long expectedSequence, |
| | | 65 | | string expectedPreviousHash, |
| | | 66 | | HashSet<long> observedSequences, |
| | | 67 | | bool requireGenesis) |
| | | 68 | | { |
| | 15 | 69 | | if (!string.Equals(link.HashAlgorithm, CanonicalPayloadOptions.DefaultHashAlgorithm, StringComparison.Ordinal)) |
| | | 70 | | { |
| | 1 | 71 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 72 | | AuditIntegrityVerificationCategory.UnsupportedAlgorithm, |
| | 1 | 73 | | "integrity.hash-algorithm-unsupported", |
| | 1 | 74 | | "The integrity link uses an unsupported hash algorithm.", |
| | 1 | 75 | | link); |
| | | 76 | | } |
| | | 77 | | |
| | 14 | 78 | | if (!string.Equals(link.ChainId, expectedChainId, StringComparison.Ordinal)) |
| | | 79 | | { |
| | 1 | 80 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 81 | | AuditIntegrityVerificationCategory.WrongChain, |
| | 1 | 82 | | "integrity.chain-id-mismatch", |
| | 1 | 83 | | "The integrity link belongs to a different chain.", |
| | 1 | 84 | | link); |
| | | 85 | | } |
| | | 86 | | |
| | 13 | 87 | | if (!observedSequences.Add(link.Sequence)) |
| | | 88 | | { |
| | 1 | 89 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 90 | | AuditIntegrityVerificationCategory.ForkedChain, |
| | 1 | 91 | | "integrity.sequence-duplicate", |
| | 1 | 92 | | "Multiple links claim the same chain sequence.", |
| | 1 | 93 | | link); |
| | | 94 | | } |
| | | 95 | | |
| | 12 | 96 | | _ = observedSequences.Remove(link.Sequence); |
| | | 97 | | |
| | 12 | 98 | | if (link.Sequence != expectedSequence) |
| | | 99 | | { |
| | 1 | 100 | | AuditIntegrityVerificationCategory category = link.Sequence > expectedSequence |
| | 1 | 101 | | ? AuditIntegrityVerificationCategory.MissingRecord |
| | 1 | 102 | | : AuditIntegrityVerificationCategory.ReorderedRecord; |
| | | 103 | | |
| | 1 | 104 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 105 | | category, |
| | 1 | 106 | | category is AuditIntegrityVerificationCategory.MissingRecord |
| | 1 | 107 | | ? "integrity.sequence-missing" |
| | 1 | 108 | | : "integrity.sequence-reordered", |
| | 1 | 109 | | "The integrity link sequence is not continuous in the supplied order.", |
| | 1 | 110 | | link, |
| | 1 | 111 | | new Dictionary<string, string>(StringComparer.Ordinal) |
| | 1 | 112 | | { |
| | 1 | 113 | | ["expected_sequence"] = expectedSequence.ToString(System.Globalization.CultureInfo.InvariantCulture) |
| | 1 | 114 | | ["actual_sequence"] = link.Sequence.ToString(System.Globalization.CultureInfo.InvariantCulture) |
| | 1 | 115 | | }); |
| | | 116 | | } |
| | | 117 | | |
| | 11 | 118 | | if (requireGenesis && link.Sequence == 1 && link.PreviousLinkHash.Length != 0) |
| | | 119 | | { |
| | 1 | 120 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 121 | | AuditIntegrityVerificationCategory.HashMismatch, |
| | 1 | 122 | | "integrity.genesis-previous-hash-present", |
| | 1 | 123 | | "The genesis link must not point to a previous link hash.", |
| | 1 | 124 | | link); |
| | | 125 | | } |
| | | 126 | | |
| | 10 | 127 | | if (!string.Equals(link.PreviousLinkHash, expectedPreviousHash, StringComparison.Ordinal)) |
| | | 128 | | { |
| | 1 | 129 | | return AuditIntegrityVerificationResult.Failed( |
| | 1 | 130 | | AuditIntegrityVerificationCategory.HashMismatch, |
| | 1 | 131 | | "integrity.previous-link-hash-mismatch", |
| | 1 | 132 | | "The integrity link does not point to the previous link hash.", |
| | 1 | 133 | | link, |
| | 1 | 134 | | new Dictionary<string, string>(StringComparer.Ordinal) |
| | 1 | 135 | | { |
| | 1 | 136 | | ["expected_previous_hash"] = expectedPreviousHash, |
| | 1 | 137 | | ["actual_previous_hash"] = link.PreviousLinkHash |
| | 1 | 138 | | }); |
| | | 139 | | } |
| | | 140 | | |
| | 9 | 141 | | string expectedLinkHash = link.ComputeExpectedLinkHash(); |
| | 9 | 142 | | return !string.Equals(link.LinkHash, expectedLinkHash, StringComparison.Ordinal) |
| | 9 | 143 | | ? AuditIntegrityVerificationResult.Failed( |
| | 9 | 144 | | AuditIntegrityVerificationCategory.ModifiedRecord, |
| | 9 | 145 | | "integrity.link-hash-mismatch", |
| | 9 | 146 | | "The integrity link hash no longer matches its canonical fields.", |
| | 9 | 147 | | link, |
| | 9 | 148 | | new Dictionary<string, string>(StringComparer.Ordinal) |
| | 9 | 149 | | { |
| | 9 | 150 | | ["expected_link_hash"] = expectedLinkHash, |
| | 9 | 151 | | ["actual_link_hash"] = link.LinkHash |
| | 9 | 152 | | }) |
| | 9 | 153 | | : null; |
| | | 154 | | } |
| | | 155 | | } |