| | | 1 | | namespace AsiBackbone.Signing.LocalDevelopment; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configures the local-development signing provider. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// This provider is intended for local development, samples, and tests. It is not a production managed-key provider and |
| | | 8 | | /// </remarks> |
| | | 9 | | public sealed class LocalDevelopmentSigningOptions |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Gets the minimum supported RSA key size for generated local-development keys. |
| | | 13 | | /// </summary> |
| | | 14 | | public const int MinimumKeySizeBits = 2048; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Gets the default provider descriptor returned in signing metadata. |
| | | 18 | | /// </summary> |
| | | 19 | | public const string DefaultProviderName = "local-development"; |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// Gets the default local-development key identifier. |
| | | 23 | | /// </summary> |
| | | 24 | | public const string DefaultKeyId = "local-dev-key"; |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the default local-development key version. |
| | | 28 | | /// </summary> |
| | | 29 | | public const string DefaultKeyVersion = "dev"; |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the default provider-neutral signature algorithm descriptor. |
| | | 33 | | /// </summary> |
| | | 34 | | public const string DefaultSignatureAlgorithm = "RSASSA-PSS-SHA256-LOCAL-DEV"; |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the default RSA key size for generated local-development keys. |
| | | 38 | | /// </summary> |
| | | 39 | | public const int DefaultKeySizeBits = MinimumKeySizeBits; |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets or sets the provider descriptor returned in signing metadata. |
| | | 43 | | /// </summary> |
| | 134 | 44 | | public string ProviderName { get; set; } = DefaultProviderName; |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets or sets the local-development key identifier returned in signing metadata. |
| | | 48 | | /// </summary> |
| | 154 | 49 | | public string KeyId { get; set; } = DefaultKeyId; |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Gets or sets the local-development key version returned in signing metadata. |
| | | 53 | | /// </summary> |
| | 154 | 54 | | public string KeyVersion { get; set; } = DefaultKeyVersion; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets or sets the signature algorithm descriptor returned in signing metadata. |
| | | 58 | | /// </summary> |
| | 104 | 59 | | public string SignatureAlgorithm { get; set; } = DefaultSignatureAlgorithm; |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets the generated RSA key size in bits. |
| | | 63 | | /// </summary> |
| | | 64 | | /// <remarks> |
| | | 65 | | /// Values below <see cref="MinimumKeySizeBits" /> are invalid. Omitted configuration uses |
| | | 66 | | /// <see cref="DefaultKeySizeBits" />. |
| | | 67 | | /// </remarks> |
| | 182 | 68 | | public int KeySizeBits { get; set; } = DefaultKeySizeBits; |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Gets or sets a value indicating whether signing failures should return unsigned metadata with explicit failure d |
| | | 72 | | /// </summary> |
| | 84 | 73 | | public bool ReturnUnsignedOnFailure { get; set; } = true; |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Validates the configured local-development signing options. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <exception cref="InvalidOperationException"> |
| | | 79 | | /// Thrown when <see cref="KeySizeBits" /> is below <see cref="MinimumKeySizeBits" />. |
| | | 80 | | /// </exception> |
| | | 81 | | public void Validate() |
| | | 82 | | { |
| | 50 | 83 | | if (KeySizeBits < MinimumKeySizeBits) |
| | | 84 | | { |
| | 22 | 85 | | throw new InvalidOperationException( |
| | 22 | 86 | | $"Local-development RSA key size must be at least {MinimumKeySizeBits} bits. Configured value: {KeySizeB |
| | | 87 | | } |
| | 28 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Creates options for the local-development signing provider. |
| | | 92 | | /// </summary> |
| | | 93 | | public static LocalDevelopmentSigningOptions Create( |
| | | 94 | | string? providerName = null, |
| | | 95 | | string? keyId = null, |
| | | 96 | | string? keyVersion = null, |
| | | 97 | | string? signatureAlgorithm = null, |
| | | 98 | | int keySizeBits = DefaultKeySizeBits, |
| | | 99 | | bool returnUnsignedOnFailure = true) |
| | | 100 | | { |
| | 40 | 101 | | return new LocalDevelopmentSigningOptions |
| | 40 | 102 | | { |
| | 40 | 103 | | ProviderName = string.IsNullOrWhiteSpace(providerName) |
| | 40 | 104 | | ? DefaultProviderName |
| | 40 | 105 | | : providerName.Trim(), |
| | 40 | 106 | | KeyId = string.IsNullOrWhiteSpace(keyId) |
| | 40 | 107 | | ? DefaultKeyId |
| | 40 | 108 | | : keyId.Trim(), |
| | 40 | 109 | | KeyVersion = string.IsNullOrWhiteSpace(keyVersion) |
| | 40 | 110 | | ? DefaultKeyVersion |
| | 40 | 111 | | : keyVersion.Trim(), |
| | 40 | 112 | | SignatureAlgorithm = string.IsNullOrWhiteSpace(signatureAlgorithm) |
| | 40 | 113 | | ? DefaultSignatureAlgorithm |
| | 40 | 114 | | : signatureAlgorithm.Trim(), |
| | 40 | 115 | | KeySizeBits = keySizeBits, |
| | 40 | 116 | | ReturnUnsignedOnFailure = returnUnsignedOnFailure |
| | 40 | 117 | | }; |
| | | 118 | | } |
| | | 119 | | } |