| | | 1 | | using AsiBackbone.Core.Audit; |
| | | 2 | | using AsiBackbone.Core.CapabilityGrants; |
| | | 3 | | using AsiBackbone.Core.Emissions; |
| | | 4 | | using AsiBackbone.Core.Outbox; |
| | | 5 | | using AsiBackbone.Core.Serialization; |
| | | 6 | | |
| | | 7 | | namespace AsiBackbone.Core.Signing; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides provider-neutral helper methods for preparing and signing AsiBackbone governance artifacts. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// The helpers canonicalize and hash artifacts before optionally invoking <see cref="IGovernanceSigningService" />. |
| | | 14 | | /// They do not verify signatures, persist records, provide immutable storage, or make tamper-evidence claims. |
| | | 15 | | /// </remarks> |
| | | 16 | | public static class GovernanceArtifactSigner |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Creates an unsigned wrapper for decision receipt. |
| | | 20 | | /// </summary> |
| | | 21 | | public static SignedGovernanceArtifact<IDecisionReceipt> CreateUnsignedDecisionReceipt( |
| | | 22 | | IDecisionReceipt receipt, |
| | | 23 | | CanonicalPayloadOptions? options = null, |
| | | 24 | | string? hashAlgorithm = null) |
| | | 25 | | { |
| | 1 | 26 | | return CreateUnsigned(receipt, CanonicalPayloadBuilder.ForDecisionReceipt(receipt, options), hashAlgorithm); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Creates signing-ready metadata for decision receipt without invoking a signing provider. |
| | | 31 | | /// </summary> |
| | | 32 | | public static SignedGovernanceArtifact<IDecisionReceipt> CreateSigningReadyDecisionReceipt( |
| | | 33 | | IDecisionReceipt receipt, |
| | | 34 | | CanonicalPayloadOptions? options = null, |
| | | 35 | | string? hashAlgorithm = null, |
| | | 36 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 37 | | { |
| | 1 | 38 | | return CreateSigningReady(receipt, CanonicalPayloadBuilder.ForDecisionReceipt(receipt, options), hashAlgorithm, |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Signs decision receipt after canonical payload hashing. |
| | | 43 | | /// </summary> |
| | | 44 | | public static ValueTask<SignedGovernanceArtifact<IDecisionReceipt>> SignDecisionReceiptAsync( |
| | | 45 | | IDecisionReceipt receipt, |
| | | 46 | | IGovernanceSigningService signingService, |
| | | 47 | | CanonicalPayloadOptions? options = null, |
| | | 48 | | string? hashAlgorithm = null, |
| | | 49 | | string? keyId = null, |
| | | 50 | | string? keyVersion = null, |
| | | 51 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 52 | | bool requireSignature = true, |
| | | 53 | | CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 1 | 55 | | return SignAsync( |
| | 1 | 56 | | receipt, |
| | 1 | 57 | | CanonicalPayloadBuilder.ForDecisionReceipt(receipt, options), |
| | 1 | 58 | | signingService, |
| | 1 | 59 | | hashAlgorithm, |
| | 1 | 60 | | keyId, |
| | 1 | 61 | | keyVersion, |
| | 1 | 62 | | metadata, |
| | 1 | 63 | | requireSignature, |
| | 1 | 64 | | cancellationToken); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates an unsigned wrapper for a persistence-ready audit ledger record. |
| | | 69 | | /// </summary> |
| | | 70 | | public static SignedGovernanceArtifact<AuditLedgerRecord> CreateUnsignedAuditLedgerRecord( |
| | | 71 | | AuditLedgerRecord record, |
| | | 72 | | CanonicalPayloadOptions? options = null, |
| | | 73 | | string? hashAlgorithm = null) |
| | | 74 | | { |
| | 0 | 75 | | return CreateUnsigned(record, CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), hashAlgorithm); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Creates signing-ready metadata for a persistence-ready audit ledger record without invoking a signing provider. |
| | | 80 | | /// </summary> |
| | | 81 | | public static SignedGovernanceArtifact<AuditLedgerRecord> CreateSigningReadyAuditLedgerRecord( |
| | | 82 | | AuditLedgerRecord record, |
| | | 83 | | CanonicalPayloadOptions? options = null, |
| | | 84 | | string? hashAlgorithm = null, |
| | | 85 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 86 | | { |
| | 1 | 87 | | return CreateSigningReady(record, CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), hashAlgorithm, |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Signs a persistence-ready audit ledger record after canonical payload hashing. |
| | | 92 | | /// </summary> |
| | | 93 | | public static ValueTask<SignedGovernanceArtifact<AuditLedgerRecord>> SignAuditLedgerRecordAsync( |
| | | 94 | | AuditLedgerRecord record, |
| | | 95 | | IGovernanceSigningService signingService, |
| | | 96 | | CanonicalPayloadOptions? options = null, |
| | | 97 | | string? hashAlgorithm = null, |
| | | 98 | | string? keyId = null, |
| | | 99 | | string? keyVersion = null, |
| | | 100 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 101 | | bool requireSignature = true, |
| | | 102 | | CancellationToken cancellationToken = default) |
| | | 103 | | { |
| | 11 | 104 | | return SignAsync( |
| | 11 | 105 | | record, |
| | 11 | 106 | | CanonicalPayloadBuilder.ForAuditLedgerRecord(record, options), |
| | 11 | 107 | | signingService, |
| | 11 | 108 | | hashAlgorithm, |
| | 11 | 109 | | keyId, |
| | 11 | 110 | | keyVersion, |
| | 11 | 111 | | metadata, |
| | 11 | 112 | | requireSignature, |
| | 11 | 113 | | cancellationToken); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | /// <summary> |
| | | 117 | | /// Creates an unsigned wrapper for an decision receipt lifecycle event. |
| | | 118 | | /// </summary> |
| | | 119 | | public static SignedGovernanceArtifact<DecisionReceiptLifecycleEvent> CreateUnsignedDecisionReceiptLifecycleEvent( |
| | | 120 | | DecisionReceiptLifecycleEvent lifecycleEvent, |
| | | 121 | | CanonicalPayloadOptions? options = null, |
| | | 122 | | string? hashAlgorithm = null) |
| | | 123 | | { |
| | 1 | 124 | | return CreateUnsigned(lifecycleEvent, CanonicalPayloadBuilder.ForDecisionReceiptLifecycleEvent(lifecycleEvent, o |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Creates signing-ready metadata for an decision receipt lifecycle event without invoking a signing provider. |
| | | 129 | | /// </summary> |
| | | 130 | | public static SignedGovernanceArtifact<DecisionReceiptLifecycleEvent> CreateSigningReadyDecisionReceiptLifecycleEven |
| | | 131 | | DecisionReceiptLifecycleEvent lifecycleEvent, |
| | | 132 | | CanonicalPayloadOptions? options = null, |
| | | 133 | | string? hashAlgorithm = null, |
| | | 134 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 135 | | { |
| | 1 | 136 | | return CreateSigningReady(lifecycleEvent, CanonicalPayloadBuilder.ForDecisionReceiptLifecycleEvent(lifecycleEven |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Signs an decision receipt lifecycle event after canonical payload hashing. |
| | | 141 | | /// </summary> |
| | | 142 | | public static ValueTask<SignedGovernanceArtifact<DecisionReceiptLifecycleEvent>> SignDecisionReceiptLifecycleEventAs |
| | | 143 | | DecisionReceiptLifecycleEvent lifecycleEvent, |
| | | 144 | | IGovernanceSigningService signingService, |
| | | 145 | | CanonicalPayloadOptions? options = null, |
| | | 146 | | string? hashAlgorithm = null, |
| | | 147 | | string? keyId = null, |
| | | 148 | | string? keyVersion = null, |
| | | 149 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 150 | | bool requireSignature = true, |
| | | 151 | | CancellationToken cancellationToken = default) |
| | | 152 | | { |
| | 1 | 153 | | return SignAsync( |
| | 1 | 154 | | lifecycleEvent, |
| | 1 | 155 | | CanonicalPayloadBuilder.ForDecisionReceiptLifecycleEvent(lifecycleEvent, options), |
| | 1 | 156 | | signingService, |
| | 1 | 157 | | hashAlgorithm, |
| | 1 | 158 | | keyId, |
| | 1 | 159 | | keyVersion, |
| | 1 | 160 | | metadata, |
| | 1 | 161 | | requireSignature, |
| | 1 | 162 | | cancellationToken); |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | /// <summary> |
| | | 166 | | /// Creates an unsigned wrapper for a governance emission envelope. |
| | | 167 | | /// </summary> |
| | | 168 | | public static SignedGovernanceArtifact<GovernanceEmissionEnvelope> CreateUnsignedGovernanceEmissionEnvelope( |
| | | 169 | | GovernanceEmissionEnvelope envelope, |
| | | 170 | | CanonicalPayloadOptions? options = null, |
| | | 171 | | string? hashAlgorithm = null) |
| | | 172 | | { |
| | 1 | 173 | | return CreateUnsigned(envelope, CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), hashAl |
| | | 174 | | } |
| | | 175 | | |
| | | 176 | | /// <summary> |
| | | 177 | | /// Creates signing-ready metadata for a governance emission envelope without invoking a signing provider. |
| | | 178 | | /// </summary> |
| | | 179 | | public static SignedGovernanceArtifact<GovernanceEmissionEnvelope> CreateSigningReadyGovernanceEmissionEnvelope( |
| | | 180 | | GovernanceEmissionEnvelope envelope, |
| | | 181 | | CanonicalPayloadOptions? options = null, |
| | | 182 | | string? hashAlgorithm = null, |
| | | 183 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 184 | | { |
| | 1 | 185 | | return CreateSigningReady(envelope, CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), ha |
| | | 186 | | } |
| | | 187 | | |
| | | 188 | | /// <summary> |
| | | 189 | | /// Signs a governance emission envelope after canonical payload hashing. |
| | | 190 | | /// </summary> |
| | | 191 | | public static ValueTask<SignedGovernanceArtifact<GovernanceEmissionEnvelope>> SignGovernanceEmissionEnvelopeAsync( |
| | | 192 | | GovernanceEmissionEnvelope envelope, |
| | | 193 | | IGovernanceSigningService signingService, |
| | | 194 | | CanonicalPayloadOptions? options = null, |
| | | 195 | | string? hashAlgorithm = null, |
| | | 196 | | string? keyId = null, |
| | | 197 | | string? keyVersion = null, |
| | | 198 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 199 | | bool requireSignature = true, |
| | | 200 | | CancellationToken cancellationToken = default) |
| | | 201 | | { |
| | 1 | 202 | | return SignAsync( |
| | 1 | 203 | | envelope, |
| | 1 | 204 | | CanonicalPayloadBuilder.ForGovernanceEmissionEnvelope(envelope, options), |
| | 1 | 205 | | signingService, |
| | 1 | 206 | | hashAlgorithm, |
| | 1 | 207 | | keyId, |
| | 1 | 208 | | keyVersion, |
| | 1 | 209 | | metadata, |
| | 1 | 210 | | requireSignature, |
| | 1 | 211 | | cancellationToken); |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | /// <summary> |
| | | 215 | | /// Creates an unsigned wrapper for an outbox entry. |
| | | 216 | | /// </summary> |
| | | 217 | | public static SignedGovernanceArtifact<GovernanceOutboxEntry> CreateUnsignedGovernanceOutboxEntry( |
| | | 218 | | GovernanceOutboxEntry entry, |
| | | 219 | | CanonicalPayloadOptions? options = null, |
| | | 220 | | string? hashAlgorithm = null) |
| | | 221 | | { |
| | 1 | 222 | | return CreateUnsigned(entry, CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), hashAlgorithm); |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Creates signing-ready metadata for an outbox entry without invoking a signing provider. |
| | | 227 | | /// </summary> |
| | | 228 | | public static SignedGovernanceArtifact<GovernanceOutboxEntry> CreateSigningReadyGovernanceOutboxEntry( |
| | | 229 | | GovernanceOutboxEntry entry, |
| | | 230 | | CanonicalPayloadOptions? options = null, |
| | | 231 | | string? hashAlgorithm = null, |
| | | 232 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 233 | | { |
| | 1 | 234 | | return CreateSigningReady(entry, CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), hashAlgorithm |
| | | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <summary> |
| | | 238 | | /// Signs an outbox entry after canonical payload hashing. |
| | | 239 | | /// </summary> |
| | | 240 | | public static ValueTask<SignedGovernanceArtifact<GovernanceOutboxEntry>> SignGovernanceOutboxEntryAsync( |
| | | 241 | | GovernanceOutboxEntry entry, |
| | | 242 | | IGovernanceSigningService signingService, |
| | | 243 | | CanonicalPayloadOptions? options = null, |
| | | 244 | | string? hashAlgorithm = null, |
| | | 245 | | string? keyId = null, |
| | | 246 | | string? keyVersion = null, |
| | | 247 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 248 | | bool requireSignature = true, |
| | | 249 | | CancellationToken cancellationToken = default) |
| | | 250 | | { |
| | 1 | 251 | | return SignAsync( |
| | 1 | 252 | | entry, |
| | 1 | 253 | | CanonicalPayloadBuilder.ForGovernanceOutboxEntry(entry, options), |
| | 1 | 254 | | signingService, |
| | 1 | 255 | | hashAlgorithm, |
| | 1 | 256 | | keyId, |
| | 1 | 257 | | keyVersion, |
| | 1 | 258 | | metadata, |
| | 1 | 259 | | requireSignature, |
| | 1 | 260 | | cancellationToken); |
| | | 261 | | } |
| | | 262 | | |
| | | 263 | | /// <summary> |
| | | 264 | | /// Creates an unsigned wrapper for a capability grant. |
| | | 265 | | /// </summary> |
| | | 266 | | /// <remarks> |
| | | 267 | | /// The grant is wrapped under <paramref name="schemaVersion" />, which defaults to |
| | | 268 | | /// <see cref="GovernanceSchemaVersions.StableArtifactsV2" /> so <see cref="CapabilityGrant.MaxUseCount" /> is part |
| | | 269 | | /// the canonical payload. The returned <see cref="SignedGovernanceArtifact{TArtifact}.Artifact" /> carries that sch |
| | | 270 | | /// version and is the grant to transport. |
| | | 271 | | /// </remarks> |
| | | 272 | | /// <exception cref="ArgumentException"> |
| | | 273 | | /// <paramref name="schemaVersion" /> is <see cref="GovernanceSchemaVersions.StableArtifactsV1" /> and the grant car |
| | | 274 | | /// use limit that the version 1 payload cannot bind. |
| | | 275 | | /// </exception> |
| | | 276 | | public static SignedGovernanceArtifact<CapabilityGrant> CreateUnsignedCapabilityGrant( |
| | | 277 | | CapabilityGrant grant, |
| | | 278 | | CanonicalPayloadOptions? options = null, |
| | | 279 | | string? hashAlgorithm = null, |
| | | 280 | | string? schemaVersion = null) |
| | | 281 | | { |
| | 3 | 282 | | CapabilityGrant versionedGrant = ApplyCapabilityGrantSchemaVersion(grant, schemaVersion); |
| | 2 | 283 | | return CreateUnsigned(versionedGrant, CanonicalPayloadBuilder.ForCapabilityGrant(versionedGrant, options), hashA |
| | | 284 | | } |
| | | 285 | | |
| | | 286 | | /// <summary> |
| | | 287 | | /// Creates signing-ready metadata for a capability grant without invoking a signing provider. |
| | | 288 | | /// </summary> |
| | | 289 | | /// <remarks> |
| | | 290 | | /// The grant schema version is applied as described for <see cref="CreateUnsignedCapabilityGrant" />. |
| | | 291 | | /// </remarks> |
| | | 292 | | /// <exception cref="ArgumentException"> |
| | | 293 | | /// <paramref name="schemaVersion" /> is <see cref="GovernanceSchemaVersions.StableArtifactsV1" /> and the grant car |
| | | 294 | | /// use limit that the version 1 payload cannot bind. |
| | | 295 | | /// </exception> |
| | | 296 | | public static SignedGovernanceArtifact<CapabilityGrant> CreateSigningReadyCapabilityGrant( |
| | | 297 | | CapabilityGrant grant, |
| | | 298 | | CanonicalPayloadOptions? options = null, |
| | | 299 | | string? hashAlgorithm = null, |
| | | 300 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 301 | | string? schemaVersion = null) |
| | | 302 | | { |
| | 1 | 303 | | CapabilityGrant versionedGrant = ApplyCapabilityGrantSchemaVersion(grant, schemaVersion); |
| | 1 | 304 | | return CreateSigningReady(versionedGrant, CanonicalPayloadBuilder.ForCapabilityGrant(versionedGrant, options), h |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | /// <summary> |
| | | 308 | | /// Signs a capability grant after canonical payload hashing. |
| | | 309 | | /// </summary> |
| | | 310 | | /// <remarks> |
| | | 311 | | /// <para> |
| | | 312 | | /// The grant is signed under <paramref name="schemaVersion" />, which defaults to |
| | | 313 | | /// <see cref="GovernanceSchemaVersions.StableArtifactsV2" /> so the issuer-bound |
| | | 314 | | /// <see cref="CapabilityGrant.MaxUseCount" /> is covered by the signature. A grant created with another schema vers |
| | | 315 | | /// is re-created under the requested one, and the returned |
| | | 316 | | /// <see cref="SignedGovernanceArtifact{TArtifact}.Artifact" /> is the grant that was signed. |
| | | 317 | | /// </para> |
| | | 318 | | /// <para> |
| | | 319 | | /// Verifiers rebuild the payload with <see cref="CapabilityGrantValidationOptions.ProofPayloadOptions" />, so |
| | | 320 | | /// <paramref name="options" /> must match what the relying party configures. Use |
| | | 321 | | /// <see cref="CapabilityGrantToken" /> to carry the result to a relying party. |
| | | 322 | | /// </para> |
| | | 323 | | /// </remarks> |
| | | 324 | | /// <exception cref="ArgumentException"> |
| | | 325 | | /// <paramref name="schemaVersion" /> is <see cref="GovernanceSchemaVersions.StableArtifactsV1" /> and the grant car |
| | | 326 | | /// use limit that the version 1 payload cannot bind. |
| | | 327 | | /// </exception> |
| | | 328 | | /// <exception cref="InvalidOperationException"> |
| | | 329 | | /// <paramref name="requireSignature" /> is <see langword="true" /> and the signing provider returned no signature. |
| | | 330 | | /// </exception> |
| | | 331 | | public static ValueTask<SignedGovernanceArtifact<CapabilityGrant>> SignCapabilityGrantAsync( |
| | | 332 | | CapabilityGrant grant, |
| | | 333 | | IGovernanceSigningService signingService, |
| | | 334 | | CanonicalPayloadOptions? options = null, |
| | | 335 | | string? hashAlgorithm = null, |
| | | 336 | | string? keyId = null, |
| | | 337 | | string? keyVersion = null, |
| | | 338 | | IReadOnlyDictionary<string, string>? metadata = null, |
| | | 339 | | bool requireSignature = true, |
| | | 340 | | string? schemaVersion = null, |
| | | 341 | | CancellationToken cancellationToken = default) |
| | | 342 | | { |
| | 69 | 343 | | CapabilityGrant versionedGrant = ApplyCapabilityGrantSchemaVersion(grant, schemaVersion); |
| | | 344 | | |
| | 68 | 345 | | return SignAsync( |
| | 68 | 346 | | versionedGrant, |
| | 68 | 347 | | CanonicalPayloadBuilder.ForCapabilityGrant(versionedGrant, options), |
| | 68 | 348 | | signingService, |
| | 68 | 349 | | hashAlgorithm, |
| | 68 | 350 | | keyId, |
| | 68 | 351 | | keyVersion, |
| | 68 | 352 | | metadata, |
| | 68 | 353 | | requireSignature, |
| | 68 | 354 | | cancellationToken); |
| | | 355 | | } |
| | | 356 | | |
| | | 357 | | /// <summary> |
| | | 358 | | /// Creates a signing request from canonical payload hash metadata. |
| | | 359 | | /// </summary> |
| | | 360 | | public static SigningRequest CreateSigningRequest( |
| | | 361 | | CanonicalPayloadHash canonicalHash, |
| | | 362 | | string? keyId = null, |
| | | 363 | | string? keyVersion = null, |
| | | 364 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 365 | | { |
| | 92 | 366 | | ArgumentNullException.ThrowIfNull(canonicalHash); |
| | | 367 | | |
| | 92 | 368 | | var signingReadyMetadata = canonicalHash.ToSigningMetadata(metadata); |
| | | 369 | | |
| | 92 | 370 | | return new SigningRequest( |
| | 92 | 371 | | canonicalHash.HashValue, |
| | 92 | 372 | | canonicalHash.HashAlgorithm, |
| | 92 | 373 | | purpose: canonicalHash.ArtifactType, |
| | 92 | 374 | | keyId: keyId, |
| | 92 | 375 | | keyVersion: keyVersion, |
| | 92 | 376 | | metadata: signingReadyMetadata.Metadata) |
| | 92 | 377 | | { |
| | 92 | 378 | | SignatureInput = GovernanceSignatureInput.CreateV1(canonicalHash, signingReadyMetadata.Metadata) |
| | 92 | 379 | | }; |
| | | 380 | | } |
| | | 381 | | |
| | | 382 | | private static CapabilityGrant ApplyCapabilityGrantSchemaVersion(CapabilityGrant grant, string? schemaVersion) |
| | | 383 | | { |
| | 73 | 384 | | ArgumentNullException.ThrowIfNull(grant); |
| | | 385 | | |
| | 73 | 386 | | string effectiveSchemaVersion = string.IsNullOrWhiteSpace(schemaVersion) |
| | 73 | 387 | | ? GovernanceSchemaVersions.StableArtifactsV2 |
| | 73 | 388 | | : schemaVersion.Trim(); |
| | | 389 | | |
| | | 390 | | // The version 1 payload omits the use limit, so signing a limited grant under it would leave the limit outside |
| | | 391 | | // proof while the validator still enforces it. Refuse rather than produce a grant whose limit a relying party |
| | | 392 | | // could not trust. |
| | 73 | 393 | | return string.Equals(effectiveSchemaVersion, GovernanceSchemaVersions.StableArtifactsV1, StringComparison.Ordina |
| | 73 | 394 | | && grant.MaxUseCount.HasValue |
| | 73 | 395 | | ? throw new ArgumentException( |
| | 73 | 396 | | $"A capability grant with a use limit cannot be signed under schema version {GovernanceSchemaVersions.St |
| | 73 | 397 | | nameof(schemaVersion)) |
| | 73 | 398 | | : grant.WithSchemaVersion(effectiveSchemaVersion); |
| | | 399 | | } |
| | | 400 | | |
| | | 401 | | private static SignedGovernanceArtifact<TArtifact> CreateUnsigned<TArtifact>( |
| | | 402 | | TArtifact artifact, |
| | | 403 | | CanonicalPayload payload, |
| | | 404 | | string? hashAlgorithm) |
| | | 405 | | { |
| | 6 | 406 | | return SignedGovernanceArtifacts.WithoutSignature( |
| | 6 | 407 | | artifact, |
| | 6 | 408 | | payload, |
| | 6 | 409 | | CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm)); |
| | | 410 | | } |
| | | 411 | | |
| | | 412 | | private static SignedGovernanceArtifact<TArtifact> CreateSigningReady<TArtifact>( |
| | | 413 | | TArtifact artifact, |
| | | 414 | | CanonicalPayload payload, |
| | | 415 | | string? hashAlgorithm, |
| | | 416 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 417 | | { |
| | 6 | 418 | | return SignedGovernanceArtifacts.SigningReady( |
| | 6 | 419 | | artifact, |
| | 6 | 420 | | payload, |
| | 6 | 421 | | CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm), |
| | 6 | 422 | | metadata); |
| | | 423 | | } |
| | | 424 | | |
| | | 425 | | private static async ValueTask<SignedGovernanceArtifact<TArtifact>> SignAsync<TArtifact>( |
| | | 426 | | TArtifact artifact, |
| | | 427 | | CanonicalPayload payload, |
| | | 428 | | IGovernanceSigningService signingService, |
| | | 429 | | string? hashAlgorithm, |
| | | 430 | | string? keyId, |
| | | 431 | | string? keyVersion, |
| | | 432 | | IReadOnlyDictionary<string, string>? metadata, |
| | | 433 | | bool requireSignature, |
| | | 434 | | CancellationToken cancellationToken) |
| | | 435 | | { |
| | 83 | 436 | | ArgumentNullException.ThrowIfNull(signingService); |
| | 83 | 437 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 438 | | |
| | 83 | 439 | | CanonicalPayloadHash hash = CanonicalPayloadHasher.ComputeHash(payload, hashAlgorithm); |
| | 83 | 440 | | SigningRequest signingRequest = CreateSigningRequest(hash, keyId, keyVersion, metadata); |
| | 83 | 441 | | SigningResult signingResult = await signingService |
| | 83 | 442 | | .SignAsync(signingRequest, cancellationToken) |
| | 83 | 443 | | .ConfigureAwait(false); |
| | | 444 | | |
| | | 445 | | // A provider returning a failure or no-signature result previously produced an artifact with IsSigned false and |
| | | 446 | | // no exception, so a caller that asked to sign could carry on holding an unsigned artifact. Callers that want t |
| | | 447 | | // unsigned result back pass requireSignature: false and inspect IsSigned themselves. |
| | 83 | 448 | | return requireSignature && !signingResult.Metadata.IsSigned |
| | 83 | 449 | | ? throw new InvalidOperationException( |
| | 83 | 450 | | $"The signing provider returned no signature for artifact type '{payload.ArtifactType}'. Pass requireSig |
| | 83 | 451 | | : SignedGovernanceArtifacts.FromSigningMetadata( |
| | 83 | 452 | | artifact, |
| | 83 | 453 | | payload, |
| | 83 | 454 | | hash, |
| | 83 | 455 | | BindSignedPolicyContext(signingResult.Metadata, signingRequest.Metadata)); |
| | 82 | 456 | | } |
| | | 457 | | |
| | | 458 | | /// <summary> |
| | | 459 | | /// Restores the signing policy context that was bound into the signature input. |
| | | 460 | | /// </summary> |
| | | 461 | | /// <remarks> |
| | | 462 | | /// The version 1 signature input binds the policy version and policy hash the signer was asked to sign. A provider |
| | | 463 | | /// dropped, altered, or added either key in its returned metadata would otherwise produce an artifact whose recorde |
| | | 464 | | /// policy context no longer rebuilds the signed input: it would fail verification, or carry a label that differs fr |
| | | 465 | | /// what was signed. |
| | | 466 | | /// </remarks> |
| | | 467 | | private static SigningMetadata BindSignedPolicyContext( |
| | | 468 | | SigningMetadata providerMetadata, |
| | | 469 | | IReadOnlyDictionary<string, string> requestMetadata) |
| | | 470 | | { |
| | 82 | 471 | | Dictionary<string, string> metadata = new(providerMetadata.Metadata, StringComparer.Ordinal); |
| | 82 | 472 | | CopyBoundValue(requestMetadata, metadata, GovernanceSignatureInput.PolicyVersionMetadataKey); |
| | 82 | 473 | | CopyBoundValue(requestMetadata, metadata, GovernanceSignatureInput.PolicyHashMetadataKey); |
| | | 474 | | |
| | 82 | 475 | | return SigningMetadata.Create( |
| | 82 | 476 | | signingHash: providerMetadata.SigningHash, |
| | 82 | 477 | | hashAlgorithm: providerMetadata.HashAlgorithm, |
| | 82 | 478 | | signature: providerMetadata.Signature, |
| | 82 | 479 | | signatureAlgorithm: providerMetadata.SignatureAlgorithm, |
| | 82 | 480 | | keyId: providerMetadata.KeyId, |
| | 82 | 481 | | keyVersion: providerMetadata.KeyVersion, |
| | 82 | 482 | | provider: providerMetadata.Provider, |
| | 82 | 483 | | signedUtc: providerMetadata.SignedUtc, |
| | 82 | 484 | | metadata: metadata); |
| | | 485 | | } |
| | | 486 | | |
| | | 487 | | private static void CopyBoundValue( |
| | | 488 | | IReadOnlyDictionary<string, string> source, |
| | | 489 | | Dictionary<string, string> target, |
| | | 490 | | string key) |
| | | 491 | | { |
| | 164 | 492 | | if (source.TryGetValue(key, out string? value)) |
| | | 493 | | { |
| | 10 | 494 | | target[key] = value; |
| | | 495 | | } |
| | | 496 | | else |
| | | 497 | | { |
| | 154 | 498 | | _ = target.Remove(key); |
| | | 499 | | } |
| | 154 | 500 | | } |
| | | 501 | | } |