| | | 1 | | namespace AsiBackbone.Signing.ManagedKey; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Configures the managed-key signing provider. |
| | | 5 | | /// </summary> |
| | | 6 | | /// <remarks> |
| | | 7 | | /// The options carry provider-neutral key references and operational behavior. They must not contain private keys, |
| | | 8 | | /// credentials, connection strings, client secrets, or managed identity tokens. |
| | | 9 | | /// </remarks> |
| | | 10 | | public sealed class ManagedKeySigningOptions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Gets the default provider descriptor returned in signing metadata. |
| | | 14 | | /// </summary> |
| | | 15 | | public const string DefaultProviderName = "managed-key"; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Gets the default provider-neutral signature algorithm descriptor. |
| | | 19 | | /// </summary> |
| | | 20 | | public const string DefaultSignatureAlgorithm = "RSASSA-PSS-SHA256-MANAGED-KEY"; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Gets the default supported hash algorithm descriptor. |
| | | 24 | | /// </summary> |
| | | 25 | | public const string DefaultHashAlgorithm = "SHA-256"; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Gets the default maximum retry delay. |
| | | 29 | | /// </summary> |
| | 110 | 30 | | public static TimeSpan DefaultMaxRetryDelay { get; } = TimeSpan.FromSeconds(5); |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets or sets the provider descriptor returned in signing metadata. |
| | | 34 | | /// </summary> |
| | 452 | 35 | | public string ProviderName { get; set; } = DefaultProviderName; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets or sets the managed key identifier or key URI reference. |
| | | 39 | | /// </summary> |
| | 460 | 40 | | public string KeyId { get; set; } = string.Empty; |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets or sets the managed key version expected for signing. |
| | | 44 | | /// </summary> |
| | 174 | 45 | | public string? KeyVersion { get; set; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets or sets the provider-neutral signature algorithm descriptor requested from the managed-key client. |
| | | 49 | | /// </summary> |
| | 482 | 50 | | public string SignatureAlgorithm { get; set; } = DefaultSignatureAlgorithm; |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets or sets the hash algorithm expected on incoming signing requests. |
| | | 54 | | /// </summary> |
| | 428 | 55 | | public string HashAlgorithm { get; set; } = DefaultHashAlgorithm; |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Gets or sets a value indicating whether signing requests must specify or resolve a key version. |
| | | 59 | | /// </summary> |
| | 230 | 60 | | public bool RequireKeyVersion { get; set; } = true; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Gets or sets a value indicating whether signing failures should return unsigned failure metadata instead of thro |
| | | 64 | | /// </summary> |
| | | 65 | | /// <remarks> |
| | | 66 | | /// The production-oriented default is <see langword="false" /> so signing failures fail closed unless a host explic |
| | | 67 | | /// opts into unsigned failure metadata for local validation, samples, or policy-routed fallback behavior. |
| | | 68 | | /// </remarks> |
| | 134 | 69 | | public bool ReturnUnsignedOnFailure { get; set; } |
| | | 70 | | |
| | | 71 | | /// <summary> |
| | | 72 | | /// Gets or sets the maximum number of retry attempts after the initial managed-key signing call. |
| | | 73 | | /// </summary> |
| | 464 | 74 | | public int MaxRetryAttempts { get; set; } = 2; |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Gets or sets the base delay used by the exponential retry backoff calculation. |
| | | 78 | | /// </summary> |
| | | 79 | | /// <remarks> |
| | | 80 | | /// A zero value keeps retries immediate and disables jittered waiting. |
| | | 81 | | /// </remarks> |
| | 646 | 82 | | public TimeSpan RetryDelay { get; set; } = TimeSpan.FromMilliseconds(200); |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Gets or sets the maximum delay applied before any single retry attempt. |
| | | 86 | | /// </summary> |
| | 542 | 87 | | public TimeSpan MaxRetryDelay { get; set; } = DefaultMaxRetryDelay; |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Creates production-oriented managed-key signing options that fail closed by default when signing cannot complete |
| | | 91 | | /// </summary> |
| | | 92 | | public static ManagedKeySigningOptions Create( |
| | | 93 | | string keyId, |
| | | 94 | | string? keyVersion = null, |
| | | 95 | | string? providerName = null, |
| | | 96 | | string? signatureAlgorithm = null, |
| | | 97 | | string? hashAlgorithm = null, |
| | | 98 | | bool requireKeyVersion = true, |
| | | 99 | | bool returnUnsignedOnFailure = false, |
| | | 100 | | int maxRetryAttempts = 2, |
| | | 101 | | TimeSpan? retryDelay = null) |
| | | 102 | | { |
| | 68 | 103 | | var options = new ManagedKeySigningOptions |
| | 68 | 104 | | { |
| | 68 | 105 | | KeyId = NormalizeRequired(keyId, string.Empty), |
| | 68 | 106 | | KeyVersion = NormalizeOptional(keyVersion), |
| | 68 | 107 | | ProviderName = NormalizeRequired(providerName, DefaultProviderName), |
| | 68 | 108 | | SignatureAlgorithm = NormalizeRequired(signatureAlgorithm, DefaultSignatureAlgorithm), |
| | 68 | 109 | | HashAlgorithm = NormalizeRequired(hashAlgorithm, DefaultHashAlgorithm), |
| | 68 | 110 | | RequireKeyVersion = requireKeyVersion, |
| | 68 | 111 | | ReturnUnsignedOnFailure = returnUnsignedOnFailure, |
| | 68 | 112 | | MaxRetryAttempts = maxRetryAttempts, |
| | 68 | 113 | | RetryDelay = retryDelay ?? TimeSpan.FromMilliseconds(200) |
| | 68 | 114 | | }; |
| | | 115 | | |
| | 68 | 116 | | options.Validate(); |
| | 68 | 117 | | return options; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Creates local-validation managed-key signing options that return unsigned failure metadata instead of throwing. |
| | | 122 | | /// </summary> |
| | | 123 | | /// <remarks> |
| | | 124 | | /// Use this only for samples, tests, diagnostics, or hosts that explicitly route unsigned failure metadata through |
| | | 125 | | /// Production signing paths should prefer <see cref="Create" /> or set <see cref="ReturnUnsignedOnFailure" /> to |
| | | 126 | | /// <see langword="false" />. |
| | | 127 | | /// </remarks> |
| | | 128 | | public static ManagedKeySigningOptions CreateLocalValidation( |
| | | 129 | | string keyId, |
| | | 130 | | string? keyVersion = null, |
| | | 131 | | string? providerName = null, |
| | | 132 | | string? signatureAlgorithm = null, |
| | | 133 | | string? hashAlgorithm = null, |
| | | 134 | | bool requireKeyVersion = true, |
| | | 135 | | int maxRetryAttempts = 2, |
| | | 136 | | TimeSpan? retryDelay = null) |
| | | 137 | | { |
| | 10 | 138 | | return Create( |
| | 10 | 139 | | keyId, |
| | 10 | 140 | | keyVersion, |
| | 10 | 141 | | providerName, |
| | 10 | 142 | | signatureAlgorithm, |
| | 10 | 143 | | hashAlgorithm, |
| | 10 | 144 | | requireKeyVersion, |
| | 10 | 145 | | returnUnsignedOnFailure: true, |
| | 10 | 146 | | maxRetryAttempts: maxRetryAttempts, |
| | 10 | 147 | | retryDelay: retryDelay); |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | /// <summary> |
| | | 151 | | /// Validates the managed-key signing options. |
| | | 152 | | /// </summary> |
| | | 153 | | public void Validate() |
| | | 154 | | { |
| | 184 | 155 | | if (string.IsNullOrWhiteSpace(ProviderName)) |
| | | 156 | | { |
| | 2 | 157 | | throw new InvalidOperationException("Managed-key signing provider name is required."); |
| | | 158 | | } |
| | | 159 | | |
| | 182 | 160 | | if (string.IsNullOrWhiteSpace(KeyId)) |
| | | 161 | | { |
| | 4 | 162 | | throw new InvalidOperationException("Managed-key signing key ID is required."); |
| | | 163 | | } |
| | | 164 | | |
| | 178 | 165 | | if (string.IsNullOrWhiteSpace(SignatureAlgorithm)) |
| | | 166 | | { |
| | 2 | 167 | | throw new InvalidOperationException("Managed-key signing signature algorithm is required."); |
| | | 168 | | } |
| | | 169 | | |
| | 176 | 170 | | if (string.IsNullOrWhiteSpace(HashAlgorithm)) |
| | | 171 | | { |
| | 2 | 172 | | throw new InvalidOperationException("Managed-key signing hash algorithm is required."); |
| | | 173 | | } |
| | | 174 | | |
| | 174 | 175 | | if (MaxRetryAttempts < 0) |
| | | 176 | | { |
| | 2 | 177 | | throw new InvalidOperationException("Managed-key signing retry attempts must be greater than or equal to zer |
| | | 178 | | } |
| | | 179 | | |
| | 172 | 180 | | if (RetryDelay < TimeSpan.Zero) |
| | | 181 | | { |
| | 2 | 182 | | throw new InvalidOperationException("Managed-key signing retry delay must be greater than or equal to zero." |
| | | 183 | | } |
| | | 184 | | |
| | 170 | 185 | | if (MaxRetryDelay < TimeSpan.Zero) |
| | | 186 | | { |
| | 2 | 187 | | throw new InvalidOperationException("Managed-key signing maximum retry delay must be greater than or equal t |
| | | 188 | | } |
| | | 189 | | |
| | 168 | 190 | | if (MaxRetryDelay < RetryDelay) |
| | | 191 | | { |
| | 2 | 192 | | throw new InvalidOperationException("Managed-key signing maximum retry delay must be greater than or equal t |
| | | 193 | | } |
| | 166 | 194 | | } |
| | | 195 | | |
| | | 196 | | private static string NormalizeRequired(string? value, string fallback) |
| | | 197 | | { |
| | 272 | 198 | | return string.IsNullOrWhiteSpace(value) ? fallback : value.Trim(); |
| | | 199 | | } |
| | | 200 | | |
| | | 201 | | private static string? NormalizeOptional(string? value) |
| | | 202 | | { |
| | 68 | 203 | | return string.IsNullOrWhiteSpace(value) ? null : value.Trim(); |
| | | 204 | | } |
| | | 205 | | } |