| | | 1 | | using System.Collections.ObjectModel; |
| | | 2 | | |
| | | 3 | | namespace AsiBackbone.Core.Signing; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a provider-neutral request to verify signing metadata against a precomputed artifact hash. |
| | | 7 | | /// </summary> |
| | | 8 | | public sealed class SignatureVerificationRequest |
| | | 9 | | { |
| | 2 | 10 | | private static readonly IReadOnlyDictionary<string, string> EmptyMetadata = |
| | 2 | 11 | | new ReadOnlyDictionary<string, string>( |
| | 2 | 12 | | new Dictionary<string, string>(StringComparer.Ordinal)); |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Initializes a new instance of the <see cref="SignatureVerificationRequest" /> class. |
| | | 16 | | /// </summary> |
| | 24 | 17 | | public SignatureVerificationRequest( |
| | 24 | 18 | | string signingHash, |
| | 24 | 19 | | SigningMetadata signingMetadata, |
| | 24 | 20 | | string? purpose = null, |
| | 24 | 21 | | IReadOnlyDictionary<string, string>? metadata = null) |
| | | 22 | | { |
| | 24 | 23 | | ArgumentException.ThrowIfNullOrWhiteSpace(signingHash); |
| | 24 | 24 | | ArgumentNullException.ThrowIfNull(signingMetadata); |
| | | 25 | | |
| | 24 | 26 | | SigningHash = signingHash.Trim(); |
| | 24 | 27 | | SigningMetadata = signingMetadata; |
| | 24 | 28 | | Purpose = NormalizeOptional(purpose); |
| | 24 | 29 | | Metadata = NormalizeMetadata(metadata); |
| | 24 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets the precomputed artifact hash expected to have been signed. |
| | | 34 | | /// </summary> |
| | 7 | 35 | | public string SigningHash { get; } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Gets the provider-neutral signing metadata to verify. |
| | | 39 | | /// </summary> |
| | 5 | 40 | | public SigningMetadata SigningMetadata { get; } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets the host-defined verification purpose, when supplied. |
| | | 44 | | /// </summary> |
| | 2 | 45 | | public string? Purpose { get; } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Gets additional provider-neutral request metadata. |
| | | 49 | | /// </summary> |
| | 8 | 50 | | public IReadOnlyDictionary<string, string> Metadata { get; } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets a value indicating whether metadata is present. |
| | | 54 | | /// </summary> |
| | 3 | 55 | | public bool HasMetadata => Metadata.Count > 0; |
| | | 56 | | |
| | | 57 | | private static string? NormalizeOptional(string? value) |
| | | 58 | | { |
| | 24 | 59 | | return string.IsNullOrWhiteSpace(value) |
| | 24 | 60 | | ? null |
| | 24 | 61 | | : value.Trim(); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | private static IReadOnlyDictionary<string, string> NormalizeMetadata( |
| | | 65 | | IReadOnlyDictionary<string, string>? metadata) |
| | | 66 | | { |
| | 24 | 67 | | if (metadata is null || metadata.Count == 0) |
| | | 68 | | { |
| | 22 | 69 | | return EmptyMetadata; |
| | | 70 | | } |
| | | 71 | | |
| | 2 | 72 | | Dictionary<string, string> normalizedMetadata = new(StringComparer.Ordinal); |
| | | 73 | | |
| | 12 | 74 | | foreach (KeyValuePair<string, string> item in metadata) |
| | | 75 | | { |
| | 4 | 76 | | if (string.IsNullOrWhiteSpace(item.Key)) |
| | | 77 | | { |
| | | 78 | | continue; |
| | | 79 | | } |
| | | 80 | | |
| | 2 | 81 | | normalizedMetadata[item.Key.Trim()] = item.Value?.Trim() ?? string.Empty; |
| | | 82 | | } |
| | | 83 | | |
| | 2 | 84 | | return normalizedMetadata.Count == 0 |
| | 2 | 85 | | ? EmptyMetadata |
| | 2 | 86 | | : new ReadOnlyDictionary<string, string>(normalizedMetadata); |
| | | 87 | | } |
| | | 88 | | } |