| | | 1 | | using AsiBackbone.Core.Audit; |
| | | 2 | | using AsiBackbone.Core.Emissions; |
| | | 3 | | using AsiBackbone.Core.Outbox; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.Signing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Provides provider-neutral helper methods for preparing and signing AsiBackbone governance artifacts. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <remarks> |
| | | 11 | | /// The helpers canonicalize and hash artifacts before optionally invoking <see cref="IAsiBackboneSigningService" />. |
| | | 12 | | /// They do not verify signatures, persist records, provide immutable storage, or make tamper-evidence claims. |
| | | 13 | | /// </remarks> |
| | | 14 | | public static class GovernanceArtifactSigner |
| | | 15 | | { |
| | | 16 | | /// <summary> |
| | | 17 | | /// Creates an unsigned wrapper for audit residue. |
| | | 18 | | /// </summary> |
| | | 19 | | public static SignedGovernanceArtifact<IAsiBackboneAuditResidue> CreateUnsignedAuditResidue( |
| | | 20 | | IAsiBackboneAuditResidue residue, |
| | | 21 | | CanonicalPayloadOptions? options = null, |
| | | 22 | | string? hashAlgorithm = null) |
| | | 23 | | { |
| | 0 | 24 | | return CreateUnsigned(residue, CanonicalPayloadBuilder.ForAuditResidue(residue, options), hashAlgorithm); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Creates signing-ready metadata for audit residue without invoking a signing provider. |
| | | 29 | | /// </summary> |
| | | 30 | | public static SignedGovernanceArtifact<IAsiBackboneAuditResidue> CreateSigningReadyAuditResidue( |
| | | 31 | | IAsiBackboneAuditResidue residue, |
| | | 32 | | CanonicalPayloadOptions? options = null, |
| | | 33 | | string? hashAlgorithm = null, |
| | | 34 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 35 | | { |
| | 0 | 36 | | return CreateSigningReady(residue, CanonicalPayloadBuilder.ForAuditResidue(residue, options), hashAlgorithm, met |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Signs audit residue after canonical payload hashing. |
| | | 41 | | /// </summary> |
| | | 42 | | public static ValueTask<SignedGovernanceArtifact<IAsiBackboneAuditResidue>> SignAuditResidueAsync( |
| | | 43 | | IAsiBackboneAuditResidue residue, |
| | | 44 | | IAsiBackboneSigningService signingService, |
| | | 45 | | CanonicalPayloadOptions? options = null, |
| | | 46 | | string? hashAlgorithm = null, |
| | | 47 | | string? keyId = null, |
| | | 48 | | string? keyVersion = null, |
| | | 49 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 50 | | CancellationToken cancellationToken = default) |
| | | 51 | | { |
| | 0 | 52 | | return SignAsync( |
| | 0 | 53 | | residue, |
| | 0 | 54 | | CanonicalPayloadBuilder.ForAuditResidue(residue, options), |
| | 0 | 55 | | signingService, |
| | 0 | 56 | | hashAlgorithm, |
| | 0 | 57 | | keyId, |
| | 0 | 58 | | keyVersion, |
| | 0 | 59 | | metadata, |
| | 0 | 60 | | cancellationToken); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Creates an unsigned wrapper for a persistence-ready audit ledger record. |
| | | 65 | | /// </summary> |
| | | 66 | | public static SignedGovernanceArtifact<AuditLedgerRecord> CreateUnsignedAuditLedgerRecord( |
| | | 67 | | AuditLedgerRecord record, |
| | | 68 | | CanonicalPayloadOptions? options = null, |
| | | 69 | | string? hashAlgorithm = null) |
| | | 70 | | { |
| | 0 | 71 | | return CreateUnsigned(record, CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), hashAlgorithm); |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Creates signing-ready metadata for a persistence-ready audit ledger record without invoking a signing provider. |
| | | 76 | | /// </summary> |
| | | 77 | | public static SignedGovernanceArtifact<AuditLedgerRecord> CreateSigningReadyAuditLedgerRecord( |
| | | 78 | | AuditLedgerRecord record, |
| | | 79 | | CanonicalPayloadOptions? options = null, |
| | | 80 | | string? hashAlgorithm = null, |
| | | 81 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 82 | | { |
| | 1 | 83 | | return CreateSigningReady(record, CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), hashAlgorithm, |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Signs a persistence-ready audit ledger record after canonical payload hashing. |
| | | 88 | | /// </summary> |
| | | 89 | | public static ValueTask<SignedGovernanceArtifact<AuditLedgerRecord>> SignAuditLedgerRecordAsync( |
| | | 90 | | AuditLedgerRecord record, |
| | | 91 | | IAsiBackboneSigningService signingService, |
| | | 92 | | CanonicalPayloadOptions? options = null, |
| | | 93 | | string? hashAlgorithm = null, |
| | | 94 | | string? keyId = null, |
| | | 95 | | string? keyVersion = null, |
| | | 96 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 97 | | CancellationToken cancellationToken = default) |
| | | 98 | | { |
| | 1 | 99 | | return SignAsync( |
| | 1 | 100 | | record, |
| | 1 | 101 | | CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), |
| | 1 | 102 | | signingService, |
| | 1 | 103 | | hashAlgorithm, |
| | 1 | 104 | | keyId, |
| | 1 | 105 | | keyVersion, |
| | 1 | 106 | | metadata, |
| | 1 | 107 | | cancellationToken); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Creates an unsigned wrapper for an audit residue lifecycle event. |
| | | 112 | | /// </summary> |
| | | 113 | | public static SignedGovernanceArtifact<AuditResidueLifecycleEvent> CreateUnsignedAuditResidueLifecycleEvent( |
| | | 114 | | AuditResidueLifecycleEvent lifecycleEvent, |
| | | 115 | | CanonicalPayloadOptions? options = null, |
| | | 116 | | string? hashAlgorithm = null) |
| | | 117 | | { |
| | 0 | 118 | | return CreateUnsigned(lifecycleEvent, CanonicalPayloadBuilder.ForAuditResidueLifecycleEvent(lifecycleEvent, opti |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | /// <summary> |
| | | 122 | | /// Creates signing-ready metadata for an audit residue lifecycle event without invoking a signing provider. |
| | | 123 | | /// </summary> |
| | | 124 | | public static SignedGovernanceArtifact<AuditResidueLifecycleEvent> CreateSigningReadyAuditResidueLifecycleEvent( |
| | | 125 | | AuditResidueLifecycleEvent lifecycleEvent, |
| | | 126 | | CanonicalPayloadOptions? options = null, |
| | | 127 | | string? hashAlgorithm = null, |
| | | 128 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 129 | | { |
| | 0 | 130 | | return CreateSigningReady(lifecycleEvent, CanonicalPayloadBuilder.ForAuditResidueLifecycleEvent(lifecycleEvent, |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | /// <summary> |
| | | 134 | | /// Signs an audit residue lifecycle event after canonical payload hashing. |
| | | 135 | | /// </summary> |
| | | 136 | | public static ValueTask<SignedGovernanceArtifact<AuditResidueLifecycleEvent>> SignAuditResidueLifecycleEventAsync( |
| | | 137 | | AuditResidueLifecycleEvent lifecycleEvent, |
| | | 138 | | IAsiBackboneSigningService signingService, |
| | | 139 | | CanonicalPayloadOptions? options = null, |
| | | 140 | | string? hashAlgorithm = null, |
| | | 141 | | string? keyId = null, |
| | | 142 | | string? keyVersion = null, |
| | | 143 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 144 | | CancellationToken cancellationToken = default) |
| | | 145 | | { |
| | 0 | 146 | | return SignAsync( |
| | 0 | 147 | | lifecycleEvent, |
| | 0 | 148 | | CanonicalPayloadBuilder.ForAuditResidueLifecycleEvent(lifecycleEvent, options), |
| | 0 | 149 | | signingService, |
| | 0 | 150 | | hashAlgorithm, |
| | 0 | 151 | | keyId, |
| | 0 | 152 | | keyVersion, |
| | 0 | 153 | | metadata, |
| | 0 | 154 | | cancellationToken); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Creates an unsigned wrapper for a governance emission envelope. |
| | | 159 | | /// </summary> |
| | | 160 | | public static SignedGovernanceArtifact<GovernanceEmissionEnvelope> CreateUnsignedGovernanceEmissionEnvelope( |
| | | 161 | | GovernanceEmissionEnvelope envelope, |
| | | 162 | | CanonicalPayloadOptions? options = null, |
| | | 163 | | string? hashAlgorithm = null) |
| | | 164 | | { |
| | 0 | 165 | | return CreateUnsigned(envelope, CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), hashAl |
| | | 166 | | } |
| | | 167 | | |
| | | 168 | | /// <summary> |
| | | 169 | | /// Creates signing-ready metadata for a governance emission envelope without invoking a signing provider. |
| | | 170 | | /// </summary> |
| | | 171 | | public static SignedGovernanceArtifact<GovernanceEmissionEnvelope> CreateSigningReadyGovernanceEmissionEnvelope( |
| | | 172 | | GovernanceEmissionEnvelope envelope, |
| | | 173 | | CanonicalPayloadOptions? options = null, |
| | | 174 | | string? hashAlgorithm = null, |
| | | 175 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 176 | | { |
| | 0 | 177 | | return CreateSigningReady(envelope, CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), ha |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | /// <summary> |
| | | 181 | | /// Signs a governance emission envelope after canonical payload hashing. |
| | | 182 | | /// </summary> |
| | | 183 | | public static ValueTask<SignedGovernanceArtifact<GovernanceEmissionEnvelope>> SignGovernanceEmissionEnvelopeAsync( |
| | | 184 | | GovernanceEmissionEnvelope envelope, |
| | | 185 | | IAsiBackboneSigningService signingService, |
| | | 186 | | CanonicalPayloadOptions? options = null, |
| | | 187 | | string? hashAlgorithm = null, |
| | | 188 | | string? keyId = null, |
| | | 189 | | string? keyVersion = null, |
| | | 190 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 191 | | CancellationToken cancellationToken = default) |
| | | 192 | | { |
| | 0 | 193 | | return SignAsync( |
| | 0 | 194 | | envelope, |
| | 0 | 195 | | CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), |
| | 0 | 196 | | signingService, |
| | 0 | 197 | | hashAlgorithm, |
| | 0 | 198 | | keyId, |
| | 0 | 199 | | keyVersion, |
| | 0 | 200 | | metadata, |
| | 0 | 201 | | cancellationToken); |
| | | 202 | | } |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Creates an unsigned wrapper for a governance outbox entry. |
| | | 206 | | /// </summary> |
| | | 207 | | public static SignedGovernanceArtifact<GovernanceOutboxEntry> CreateUnsignedGovernanceOutboxEntry( |
| | | 208 | | GovernanceOutboxEntry entry, |
| | | 209 | | CanonicalPayloadOptions? options = null, |
| | | 210 | | string? hashAlgorithm = null) |
| | | 211 | | { |
| | 1 | 212 | | return CreateUnsigned(entry, CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), hashAlgorithm); |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Creates signing-ready metadata for a governance outbox entry without invoking a signing provider. |
| | | 217 | | /// </summary> |
| | | 218 | | public static SignedGovernanceArtifact<GovernanceOutboxEntry> CreateSigningReadyGovernanceOutboxEntry( |
| | | 219 | | GovernanceOutboxEntry entry, |
| | | 220 | | CanonicalPayloadOptions? options = null, |
| | | 221 | | string? hashAlgorithm = null, |
| | | 222 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 223 | | { |
| | 0 | 224 | | return CreateSigningReady(entry, CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), hashAlgorithm |
| | | 225 | | } |
| | | 226 | | |
| | | 227 | | /// <summary> |
| | | 228 | | /// Signs a governance outbox entry after canonical payload hashing. |
| | | 229 | | /// </summary> |
| | | 230 | | public static ValueTask<SignedGovernanceArtifact<GovernanceOutboxEntry>> SignGovernanceOutboxEntryAsync( |
| | | 231 | | GovernanceOutboxEntry entry, |
| | | 232 | | IAsiBackboneSigningService signingService, |
| | | 233 | | CanonicalPayloadOptions? options = null, |
| | | 234 | | string? hashAlgorithm = null, |
| | | 235 | | string? keyId = null, |
| | | 236 | | string? keyVersion = null, |
| | | 237 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 238 | | CancellationToken cancellationToken = default) |
| | | 239 | | { |
| | 1 | 240 | | return SignAsync( |
| | 1 | 241 | | entry, |
| | 1 | 242 | | CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), |
| | 1 | 243 | | signingService, |
| | 1 | 244 | | hashAlgorithm, |
| | 1 | 245 | | keyId, |
| | 1 | 246 | | keyVersion, |
| | 1 | 247 | | metadata, |
| | 1 | 248 | | cancellationToken); |
| | | 249 | | } |
| | | 250 | | |
| | | 251 | | /// <summary> |
| | | 252 | | /// Creates a signing request from canonical payload hash metadata. |
| | | 253 | | /// </summary> |
| | | 254 | | public static SigningRequest CreateSigningRequest( |
| | | 255 | | CanonicalPayloadHash canonicalHash, |
| | | 256 | | string? keyId = null, |
| | | 257 | | string? keyVersion = null, |
| | | 258 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 259 | | { |
| | 2 | 260 | | ArgumentNullException.ThrowIfNull(canonicalHash); |
| | | 261 | | |
| | 2 | 262 | | var signingReadyMetadata = canonicalHash.ToSigningMetadata(metadata); |
| | | 263 | | |
| | 2 | 264 | | return new SigningRequest( |
| | 2 | 265 | | canonicalHash.HashValue, |
| | 2 | 266 | | canonicalHash.HashAlgorithm, |
| | 2 | 267 | | purpose: canonicalHash.ArtifactType, |
| | 2 | 268 | | keyId: keyId, |
| | 2 | 269 | | keyVersion: keyVersion, |
| | 2 | 270 | | metadata: signingReadyMetadata.Metadata); |
| | | 271 | | } |
| | | 272 | | |
| | | 273 | | private static SignedGovernanceArtifact<TArtifact> CreateUnsigned<TArtifact>( |
| | | 274 | | TArtifact artifact, |
| | | 275 | | CanonicalPayload payload, |
| | | 276 | | string? hashAlgorithm) |
| | | 277 | | { |
| | 1 | 278 | | return SignedGovernanceArtifacts.WithoutSignature( |
| | 1 | 279 | | artifact, |
| | 1 | 280 | | payload, |
| | 1 | 281 | | CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm)); |
| | | 282 | | } |
| | | 283 | | |
| | | 284 | | private static SignedGovernanceArtifact<TArtifact> CreateSigningReady<TArtifact>( |
| | | 285 | | TArtifact artifact, |
| | | 286 | | CanonicalPayload payload, |
| | | 287 | | string? hashAlgorithm, |
| | | 288 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 289 | | { |
| | 1 | 290 | | return SignedGovernanceArtifacts.SigningReady( |
| | 1 | 291 | | artifact, |
| | 1 | 292 | | payload, |
| | 1 | 293 | | CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm), |
| | 1 | 294 | | metadata); |
| | | 295 | | } |
| | | 296 | | |
| | | 297 | | private static async ValueTask<SignedGovernanceArtifact<TArtifact>> SignAsync<TArtifact>( |
| | | 298 | | TArtifact artifact, |
| | | 299 | | CanonicalPayload payload, |
| | | 300 | | IAsiBackboneSigningService signingService, |
| | | 301 | | string? hashAlgorithm, |
| | | 302 | | string? keyId, |
| | | 303 | | string? keyVersion, |
| | | 304 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 305 | | CancellationToken cancellationToken) |
| | | 306 | | { |
| | 2 | 307 | | ArgumentNullException.ThrowIfNull(signingService); |
| | 2 | 308 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 309 | | |
| | 2 | 310 | | CanonicalPayloadHash hash = CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm); |
| | 2 | 311 | | SigningResult signingResult = await signingService |
| | 2 | 312 | | .SignAsync(CreateSigningRequest(hash, keyId, keyVersion, metadata), cancellationToken) |
| | 2 | 313 | | .ConfigureAwait(false); |
| | | 314 | | |
| | 2 | 315 | | return SignedGovernanceArtifacts.FromSigningMetadata( |
| | 2 | 316 | | artifact, |
| | 2 | 317 | | payload, |
| | 2 | 318 | | hash, |
| | 2 | 319 | | signingResult.Metadata); |
| | 2 | 320 | | } |
| | | 321 | | } |