unidpp 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dfa7997ad4401b81ce1947c9e24c6ce0aced00b28c6059ddabd659d4038f8def
4
- data.tar.gz: 8bf56c4dc67440a63202c3450c798eb5970cbf80285673adaea46729522c2afa
3
+ metadata.gz: 8876b82d2e3f787f0167abf60fcacf277b48b7c59810882077d326eaf45fc42a
4
+ data.tar.gz: 439c77c3d134d6e131e7c1669a0ffc8edd1ada21acb86f48af2de056d049ae74
5
5
  SHA512:
6
- metadata.gz: 866c37ebf615688158480c4fde6bc5a2e9fc8543a0528e53c4cbbee688b1f66ef9af101db0833c8e7941b1543e70ed4138ecd6a25a570786769430d38bf452d1
7
- data.tar.gz: b9b623d7eb7752edd943d8f0fd3b8489cd0bc9e9bdeed6128b5ca0f3d3d708ce2feb01db1a343c3fe4a60053648fdffd7d7cd54bd56e09b7f79494938e35115c
6
+ metadata.gz: 26ad525525101510dc34c3bcc2ba0c47b45d88c0883472294404486beb6cc88e282ce15c81bee5d4b971286fcf6c8adc3aeec20df5d39c897d7c6046287ea6a0
7
+ data.tar.gz: 815ee0e83390f53406cfdfe81de7b4021ef80dadadb447b7fc1d4d2f4d58a494e3d0d0cbc78a7d194e0a867c2957d92da618d658c4304d7ae3e2f99dd70459ea
data/lib/unidpp/s13.rb ADDED
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ed25519"
4
+
5
+ module Unidpp
6
+ # The S13 trust layer (XB-6/7): verify the seam's signed envelopes
7
+ # offline — the requester's, the custodian's and the authority's
8
+ # signatures all ride the S13-MESSAGE domain over canonical bytes,
9
+ # and the choreography journal replays from the envelopes alone.
10
+ # The graph is the caller's projection: node id → that node's
11
+ # anchored keys ({ key_id => raw public bytes }), the same
12
+ # resolution discipline the native TrustGraph applies — a valid
13
+ # signature from any other node's key is not this envelope.
14
+ module S13
15
+ module_function
16
+
17
+ DOMAIN = "UNIDPP-SIGNATIF/S13-MESSAGE"
18
+
19
+ # The signatif suite codes (Annex B): the one byte a key id's
20
+ # digest rides over, ahead of the public key bytes.
21
+ SUITE_CODES = {
22
+ "ecdsa-p256" => 1,
23
+ "sm2" => 2,
24
+ "ml-dsa-44" => 3,
25
+ "ml-dsa-65" => 4,
26
+ "ml-dsa-87" => 5,
27
+ "ed25519" => 6,
28
+ "slh-dsa-128s" => 7,
29
+ "slh-dsa-192s" => 8
30
+ }.freeze
31
+
32
+ def key_id(suite, public_key)
33
+ code = SUITE_CODES[suite]
34
+ raise Error, "unknown suite `#{suite}`" unless code
35
+ digest = AnnexB.sha256([code].pack("C") + public_key)
36
+ "k-#{digest.unpack1("H16")}"
37
+ end
38
+
39
+ def authority_payload(response, sequence)
40
+ AnnexB.canonical_fields(
41
+ [
42
+ AnnexB.as_bytes(response["request_digest"]),
43
+ AnnexB.le_u64(sequence),
44
+ response["governing_policy"],
45
+ AnnexB.le_u64(response["governing_policy_version"])
46
+ ]
47
+ )
48
+ end
49
+
50
+ def domain_framed(payload)
51
+ "#{DOMAIN}\x00".b + payload
52
+ end
53
+
54
+ def verify_request(signed, graph)
55
+ verify_under(graph, signed["requester"], signed["signature"],
56
+ domain_framed(AnnexB.request_canonical(signed["request"])))
57
+ end
58
+
59
+ def verify_response(signed, graph)
60
+ verify_under(graph, signed["response"]["custodian"], signed["signature"],
61
+ domain_framed(AnnexB.response_canonical(signed["response"])))
62
+ end
63
+
64
+ # The non-repudiable answer (XB-7): the custodian's signature AND
65
+ # the authority's counter-signature over the answer's identity and
66
+ # place, at the exact expected sequence (a stale sequence is a
67
+ # replayed answer).
68
+ def verify_non_repudiable(countersigned, graph, expected_sequence)
69
+ custodian = verify_response(countersigned["signed"], graph)
70
+ return custodian unless custodian[:ok]
71
+
72
+ if countersigned["sequence"] != expected_sequence
73
+ return { ok: false,
74
+ why: "s13 non-repudiation: stale sequence #{countersigned['sequence']} (expected #{expected_sequence}) — a replayed answer" }
75
+ end
76
+
77
+ verify_under(
78
+ graph,
79
+ countersigned["authority"],
80
+ countersigned["authority_signature"],
81
+ domain_framed(authority_payload(countersigned["signed"]["response"], countersigned["sequence"]))
82
+ )
83
+ end
84
+
85
+ # Replay the choreography from the journal alone (XB-6): every
86
+ # entry verifies under the graph, every answer binds to a
87
+ # journaled request's digest, and every counter-signature sits at
88
+ # its exact monotone place per (subject, custodian), from 1. The
89
+ # decisions come back in journal sequence.
90
+ def replay(journal, graph)
91
+ decisions = []
92
+ subjects = {}
93
+ sequences = Hash.new(0)
94
+ index = 0
95
+ journal["entries"].each do |entry|
96
+ index += 1
97
+ if entry.key?("request")
98
+ check = verify_request(entry, graph)
99
+ return { ok: false, why: "entry #{index}: #{check[:why]}" } unless check[:ok]
100
+ subjects[AnnexB.sha256(AnnexB.request_canonical(entry["request"])).unpack1("H32")] =
101
+ entry["request"]["subject"]
102
+ elsif entry.key?("response")
103
+ check = verify_response(entry, graph)
104
+ return { ok: false, why: "entry #{index}: #{check[:why]}" } unless check[:ok]
105
+ digest = digest_hex(entry["response"]["request_digest"])
106
+ return { ok: false, why: "entry #{index}: an answer is bound to no journaled request" } unless subjects.key?(digest)
107
+ decisions << decision_of(entry["response"])
108
+ else
109
+ digest = digest_hex(entry["signed"]["response"]["request_digest"])
110
+ return { ok: false, why: "entry #{index}: an answer is bound to no journaled request" } unless subjects.key?(digest)
111
+ subject = subjects[digest]
112
+ place = "#{subject}|#{entry['signed']['response']['custodian']}"
113
+ expected = sequences[place] + 1
114
+ check = verify_non_repudiable(entry, graph, expected)
115
+ return { ok: false, why: "entry #{index}: #{check[:why]}" } unless check[:ok]
116
+ sequences[place] = entry["sequence"]
117
+ decisions << decision_of(entry["signed"]["response"])
118
+ end
119
+ end
120
+ { ok: true, decisions: decisions }
121
+ end
122
+
123
+ def verify_under(graph, node, slot, payload)
124
+ keys = graph[node]
125
+ return { ok: false, why: "s13 node `#{node}` has no such keys" } unless keys
126
+ key_bytes = keys[slot["key_id"]]
127
+ return { ok: false, why: "s13 node `#{node}` has no such key `#{slot['key_id']}`" } unless key_bytes
128
+ signature = slot["signature"]
129
+ return { ok: false, why: "the slot is framed-only (nothing to verify)" } unless signature
130
+ signature = signature.pack("C*") if signature.is_a?(Array)
131
+ begin
132
+ Ed25519::VerifyKey.new(key_bytes).verify(signature, payload)
133
+ { ok: true }
134
+ rescue Ed25519::VerifyError, ArgumentError
135
+ { ok: false, why: "the signature does not verify under `#{node}`'s key `#{slot['key_id']}`" }
136
+ end
137
+ end
138
+
139
+ # The digest rides JSON as either its hex form or the byte array.
140
+ def digest_hex(value)
141
+ return value if value.is_a?(String) && value.match?(/\A[0-9a-f]{64}\z/)
142
+ bytes = value.is_a?(Array) ? value.pack("C*") : value
143
+ bytes.unpack1("H32")
144
+ end
145
+
146
+ def decision_of(response)
147
+ {
148
+ request_digest_hex: digest_hex(response["request_digest"]),
149
+ outcome: response["outcome"]["outcome"],
150
+ governing_policy: response["governing_policy"],
151
+ governing_policy_version: response["governing_policy_version"]
152
+ }
153
+ end
154
+ end
155
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Unidpp
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
data/lib/unidpp.rb CHANGED
@@ -15,6 +15,7 @@ module Unidpp
15
15
  autoload :VERSION, "unidpp/version"
16
16
  autoload :CanonicalJson, "unidpp/canonical_json"
17
17
  autoload :AnnexB, "unidpp/annexb"
18
+ autoload :S13, "unidpp/s13"
18
19
  autoload :Gs1, "unidpp/gs1"
19
20
  autoload :ProductIdentifier, "unidpp/identifier"
20
21
  autoload :ProfileManifest, "unidpp/manifest"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: unidpp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - UniDPP
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - "~>"
24
24
  - !ruby/object:Gem::Version
25
25
  version: '0.8'
26
+ - !ruby/object:Gem::Dependency
27
+ name: ed25519
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - "~>"
31
+ - !ruby/object:Gem::Version
32
+ version: '1.3'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.3'
26
40
  - !ruby/object:Gem::Dependency
27
41
  name: rake
28
42
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +95,7 @@ files:
81
95
  - lib/unidpp/registry/item_version.rb
82
96
  - lib/unidpp/registry/register.rb
83
97
  - lib/unidpp/registry/store.rb
98
+ - lib/unidpp/s13.rb
84
99
  - lib/unidpp/verdict.rb
85
100
  - lib/unidpp/version.rb
86
101
  homepage: https://github.com/unidpp/unidpp-rb