| | | 1 | | using System.Buffers; |
| | | 2 | | using System.Text; |
| | | 3 | | using System.Text.Json; |
| | | 4 | | |
| | | 5 | | namespace AsiBackbone.Core.Signing; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Represents a deterministic, provider-neutral payload that can be hashed and later signed by a host or provider packa |
| | | 9 | | /// </summary> |
| | | 10 | | public sealed class CanonicalPayload |
| | | 11 | | { |
| | | 12 | | private CanonicalPayload( |
| | | 13 | | string artifactType, |
| | | 14 | | string artifactId, |
| | | 15 | | string payloadSchemaVersion, |
| | | 16 | | string canonicalizationVersion, |
| | | 17 | | string canonicalJson) |
| | | 18 | | { |
| | | 19 | | ArtifactType = artifactType; |
| | | 20 | | ArtifactId = artifactId; |
| | | 21 | | PayloadSchemaVersion = payloadSchemaVersion; |
| | | 22 | | CanonicalizationVersion = canonicalizationVersion; |
| | | 23 | | CanonicalJson = canonicalJson; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets the stable artifact type bound into the canonical payload. |
| | | 28 | | /// </summary> |
| | | 29 | | public string ArtifactType { get; } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Gets the stable artifact identifier bound into the canonical payload. |
| | | 33 | | /// </summary> |
| | | 34 | | public string ArtifactId { get; } |
| | | 35 | | |
| | | 36 | | /// <summary> |
| | | 37 | | /// Gets the payload schema version bound into the canonical payload. |
| | | 38 | | /// </summary> |
| | | 39 | | public string PayloadSchemaVersion { get; } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// Gets the canonicalization version used to create the payload bytes. |
| | | 43 | | /// </summary> |
| | | 44 | | public string CanonicalizationVersion { get; } |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the deterministic JSON payload to hash or sign. |
| | | 48 | | /// </summary> |
| | | 49 | | public string CanonicalJson { get; } |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Creates a canonical payload envelope around deterministic artifact content. |
| | | 53 | | /// </summary> |
| | | 54 | | public static CanonicalPayload Create( |
| | | 55 | | string artifactType, |
| | | 56 | | string artifactId, |
| | | 57 | | string payloadSchemaVersion, |
| | | 58 | | string canonicalizationVersion, |
| | | 59 | | IReadOnlyDictionary<string, object?> content) |
| | | 60 | | { |
| | | 61 | | ArgumentException.ThrowIfNullOrWhiteSpace(artifactType); |
| | | 62 | | ArgumentException.ThrowIfNullOrWhiteSpace(artifactId); |
| | | 63 | | ArgumentException.ThrowIfNullOrWhiteSpace(payloadSchemaVersion); |
| | | 64 | | ArgumentException.ThrowIfNullOrWhiteSpace(canonicalizationVersion); |
| | | 65 | | ArgumentNullException.ThrowIfNull(content); |
| | | 66 | | |
| | | 67 | | string normalizedArtifactType = artifactType.Trim(); |
| | | 68 | | string normalizedArtifactId = artifactId.Trim(); |
| | | 69 | | string normalizedPayloadSchemaVersion = payloadSchemaVersion.Trim(); |
| | | 70 | | string normalizedCanonicalizationVersion = canonicalizationVersion.Trim(); |
| | | 71 | | |
| | | 72 | | SortedDictionary<string, object?> envelope = new(StringComparer.Ordinal) |
| | | 73 | | { |
| | | 74 | | ["artifactId"] = normalizedArtifactId, |
| | | 75 | | ["artifactType"] = normalizedArtifactType, |
| | | 76 | | ["canonicalizationVersion"] = normalizedCanonicalizationVersion, |
| | | 77 | | ["content"] = content, |
| | | 78 | | ["payloadSchemaVersion"] = normalizedPayloadSchemaVersion |
| | | 79 | | }; |
| | | 80 | | |
| | | 81 | | return new CanonicalPayload( |
| | | 82 | | normalizedArtifactType, |
| | | 83 | | normalizedArtifactId, |
| | | 84 | | normalizedPayloadSchemaVersion, |
| | | 85 | | normalizedCanonicalizationVersion, |
| | | 86 | | CanonicalPayloadJson.Serialize(envelope)); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <summary> |
| | | 90 | | /// Gets the canonical JSON payload as UTF-8 bytes. |
| | | 91 | | /// </summary> |
| | | 92 | | public byte[] ToUtf8Bytes() |
| | | 93 | | { |
| | | 94 | | return Encoding.UTF8.GetBytes(CanonicalJson); |
| | | 95 | | } |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | internal static class CanonicalPayloadJson |
| | | 99 | | { |
| | 2 | 100 | | private static readonly JsonWriterOptions WriterOptions = new() |
| | 2 | 101 | | { |
| | 2 | 102 | | Indented = false, |
| | 2 | 103 | | SkipValidation = false |
| | 2 | 104 | | }; |
| | | 105 | | |
| | | 106 | | public static string Serialize(IReadOnlyDictionary<string, object?> value) |
| | | 107 | | { |
| | 145 | 108 | | ArrayBufferWriter<byte> buffer = new(); |
| | 145 | 109 | | using (Utf8JsonWriter writer = new(buffer, WriterOptions)) |
| | | 110 | | { |
| | 145 | 111 | | WriteDictionary(writer, value); |
| | 141 | 112 | | } |
| | | 113 | | |
| | 141 | 114 | | return Encoding.UTF8.GetString(buffer.WrittenSpan); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | private static void WriteValue(Utf8JsonWriter writer, object? value) |
| | | 118 | | { |
| | | 119 | | switch (value) |
| | | 120 | | { |
| | | 121 | | case null: |
| | 245 | 122 | | writer.WriteNullValue(); |
| | 245 | 123 | | break; |
| | | 124 | | case string stringValue: |
| | 1580 | 125 | | writer.WriteStringValue(stringValue); |
| | 1580 | 126 | | break; |
| | | 127 | | case bool boolValue: |
| | 4 | 128 | | writer.WriteBooleanValue(boolValue); |
| | 4 | 129 | | break; |
| | | 130 | | case int intValue: |
| | 32 | 131 | | writer.WriteNumberValue(intValue); |
| | 32 | 132 | | break; |
| | | 133 | | case long longValue: |
| | 58 | 134 | | writer.WriteNumberValue(longValue); |
| | 58 | 135 | | break; |
| | | 136 | | case double doubleValue: |
| | 13 | 137 | | if (double.IsNaN(doubleValue) || double.IsInfinity(doubleValue)) |
| | | 138 | | { |
| | 3 | 139 | | throw new ArgumentOutOfRangeException(nameof(value), value, "Canonical payload numbers must be finit |
| | | 140 | | } |
| | | 141 | | |
| | 10 | 142 | | writer.WriteNumberValue(doubleValue); |
| | 10 | 143 | | break; |
| | | 144 | | case IReadOnlyDictionary<string, object?> dictionaryValue: |
| | 211 | 145 | | WriteDictionary(writer, dictionaryValue); |
| | 207 | 146 | | break; |
| | | 147 | | case IReadOnlyDictionary<string, string> stringDictionaryValue: |
| | 1 | 148 | | WriteStringDictionary(writer, stringDictionaryValue); |
| | 1 | 149 | | break; |
| | | 150 | | case IEnumerable<string> stringValues: |
| | 47 | 151 | | writer.WriteStartArray(); |
| | 206 | 152 | | foreach (string item in stringValues) |
| | | 153 | | { |
| | 56 | 154 | | writer.WriteStringValue(item); |
| | | 155 | | } |
| | | 156 | | |
| | 47 | 157 | | writer.WriteEndArray(); |
| | 47 | 158 | | break; |
| | | 159 | | case IEnumerable<object?> objectValues: |
| | 1 | 160 | | writer.WriteStartArray(); |
| | 10 | 161 | | foreach (object? item in objectValues) |
| | | 162 | | { |
| | 4 | 163 | | WriteValue(writer, item); |
| | | 164 | | } |
| | | 165 | | |
| | 1 | 166 | | writer.WriteEndArray(); |
| | 1 | 167 | | break; |
| | | 168 | | default: |
| | 1 | 169 | | throw new NotSupportedException($"Canonical payload value type '{value.GetType().FullName}' is not suppo |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | |
| | | 173 | | private static void WriteDictionary(Utf8JsonWriter writer, IReadOnlyDictionary<string, object?> dictionary) |
| | | 174 | | { |
| | 356 | 175 | | writer.WriteStartObject(); |
| | | 176 | | |
| | 7275 | 177 | | foreach (KeyValuePair<string, object?> item in dictionary.OrderBy(item => item.Key, StringComparer.Ordinal)) |
| | | 178 | | { |
| | 2189 | 179 | | writer.WritePropertyName(item.Key); |
| | 2189 | 180 | | WriteValue(writer, item.Value); |
| | | 181 | | } |
| | | 182 | | |
| | 348 | 183 | | writer.WriteEndObject(); |
| | 348 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static void WriteStringDictionary(Utf8JsonWriter writer, IReadOnlyDictionary<string, string> dictionary) |
| | | 187 | | { |
| | 1 | 188 | | writer.WriteStartObject(); |
| | | 189 | | |
| | 8 | 190 | | foreach (KeyValuePair<string, string> item in dictionary.OrderBy(item => item.Key, StringComparer.Ordinal)) |
| | | 191 | | { |
| | 2 | 192 | | writer.WriteString(item.Key, item.Value); |
| | | 193 | | } |
| | | 194 | | |
| | 1 | 195 | | writer.WriteEndObject(); |
| | 1 | 196 | | } |
| | | 197 | | } |