| | | 1 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 2 | | |
| | | 3 | | /// <summary>Configures public-key signature verification.</summary> |
| | | 4 | | public sealed class KeySignatureVerificationOptions |
| | | 5 | | { |
| | 5 | 6 | | private readonly List<GovernanceVerificationKey> keys = []; |
| | 5 | 7 | | private readonly HashSet<string> allowedAlgorithms = new(StringComparer.Ordinal) |
| | 5 | 8 | | { |
| | 5 | 9 | | ManagedKeySignatureAlgorithms.RsaPssSha256, |
| | 5 | 10 | | ManagedKeySignatureAlgorithms.EcdsaP256Sha256 |
| | 5 | 11 | | }; |
| | | 12 | | |
| | | 13 | | /// <summary>Gets the configured trusted key versions.</summary> |
| | 9 | 14 | | public IList<GovernanceVerificationKey> Keys => keys; |
| | | 15 | | |
| | | 16 | | /// <summary>Gets the explicit signature algorithm allow-list.</summary> |
| | 10 | 17 | | public ISet<string> AllowedAlgorithms => allowedAlgorithms; |
| | | 18 | | |
| | | 19 | | /// <summary>Gets or sets whether pre-6.0 hash-only signature input is accepted.</summary> |
| | | 20 | | public bool AllowLegacySignatureInput { get; set; } |
| | | 21 | | |
| | | 22 | | /// <summary>Adds an SPKI PEM public key.</summary> |
| | | 23 | | public void AddPemKey( |
| | | 24 | | string keyId, |
| | | 25 | | string keyVersion, |
| | | 26 | | string signatureAlgorithm, |
| | | 27 | | string publicKeyPem, |
| | | 28 | | bool isRevoked = false, |
| | | 29 | | int minimumRsaKeySize = 2048) |
| | | 30 | | { |
| | 5 | 31 | | keys.Add(GovernanceVerificationKey.FromPem( |
| | 5 | 32 | | keyId, keyVersion, signatureAlgorithm, publicKeyPem, isRevoked, minimumRsaKeySize)); |
| | 5 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <summary>Adds an RSA or EC public JWK.</summary> |
| | | 36 | | public void AddJwkKey( |
| | | 37 | | string keyId, |
| | | 38 | | string keyVersion, |
| | | 39 | | string signatureAlgorithm, |
| | | 40 | | string publicJwk, |
| | | 41 | | bool isRevoked = false, |
| | | 42 | | int minimumRsaKeySize = 2048) |
| | | 43 | | { |
| | 1 | 44 | | keys.Add(GovernanceVerificationKey.FromJwk( |
| | 1 | 45 | | keyId, keyVersion, signatureAlgorithm, publicJwk, isRevoked, minimumRsaKeySize)); |
| | 1 | 46 | | } |
| | | 47 | | |
| | | 48 | | internal void Validate() |
| | | 49 | | { |
| | 10 | 50 | | if (allowedAlgorithms.Count == 0 || allowedAlgorithms.Any(string.IsNullOrWhiteSpace)) |
| | | 51 | | { |
| | 0 | 52 | | throw new InvalidOperationException("At least one non-empty signature algorithm must be allowed."); |
| | | 53 | | } |
| | | 54 | | |
| | 10 | 55 | | if (keys.GroupBy(key => (key.KeyId, key.KeyVersion)).Any(group => group.Count() > 1)) |
| | | 56 | | { |
| | 0 | 57 | | throw new InvalidOperationException("Each configured verification key ID and version pair must be unique."); |
| | | 58 | | } |
| | 10 | 59 | | } |
| | | 60 | | } |