melos 0.1.0 → 0.9.0
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 +4 -4
- data/README.md +18 -10
- data/docs/roadmap.md +302 -0
- data/docs/validation.md +22 -18
- data/lib/melos/capabilities.rb +5 -2
- data/lib/melos/client.rb +184 -14
- data/lib/melos/credential/x509.rb +91 -0
- data/lib/melos/crypto.rb +25 -0
- data/lib/melos/epoch_secrets.rb +61 -8
- data/lib/melos/errors.rb +18 -0
- data/lib/melos/external_sender.rb +78 -0
- data/lib/melos/group/validation.rb +123 -50
- data/lib/melos/group.rb +597 -104
- data/lib/melos/key_package_bundle.rb +8 -0
- data/lib/melos/persistence.rb +45 -0
- data/lib/melos/private_tree.rb +23 -4
- data/lib/melos/ratchet_tree.rb +6 -6
- data/lib/melos/secret_tree.rb +87 -28
- data/lib/melos/struct/structs.rb +45 -2
- data/lib/melos/vec.rb +6 -6
- data/lib/melos/version.rb +1 -1
- data/lib/melos.rb +4 -0
- metadata +6 -17
- data/test_vectors/crypto-basics.json +0 -303
- data/test_vectors/deserialization.json +0 -58
- data/test_vectors/key-schedule.json +0 -926
- data/test_vectors/message-protection.json +0 -142
- data/test_vectors/messages.json +0 -5702
- data/test_vectors/passive-client-handling-commit.json +0 -2683
- data/test_vectors/passive-client-random.json +0 -2657
- data/test_vectors/passive-client-welcome.json +0 -814
- data/test_vectors/psk_secret.json +0 -2382
- data/test_vectors/secret-tree.json +0 -4846
- data/test_vectors/transcript-hashes.json +0 -58
- data/test_vectors/tree-math.json +0 -8276
- data/test_vectors/tree-operations.json +0 -47
- data/test_vectors/tree-validation.json +0 -11839
- data/test_vectors/treekem.json +0 -14877
- data/test_vectors/welcome.json +0 -51
data/lib/melos/client.rb
CHANGED
|
@@ -1,21 +1,41 @@
|
|
|
1
|
+
require 'securerandom'
|
|
1
2
|
require_relative 'crypto'
|
|
2
3
|
require_relative 'key_package_bundle'
|
|
3
4
|
require_relative 'group'
|
|
5
|
+
require_relative 'external_sender'
|
|
4
6
|
require_relative 'struct/structs'
|
|
5
7
|
|
|
6
8
|
# A client (RFC 9420 Section 5.3): a long-lived identity, i.e. a signature key pair and a
|
|
7
9
|
# credential, plus what a client keeps across groups: the external PSKs it knows (8.4) and the
|
|
8
|
-
# KeyPackages it has published and may still be welcomed into a group with (10)
|
|
10
|
+
# KeyPackages it has published and may still be welcomed into a group with (10), and the
|
|
11
|
+
# resumption PSKs of groups it has replaced (8.6), for groups that resume them.
|
|
9
12
|
class Melos::Client
|
|
10
13
|
attr_reader :cipher_suite, :credential, :external_psks, :key_packages, :groups
|
|
11
14
|
|
|
12
|
-
|
|
15
|
+
# identity: for a basic credential
|
|
16
|
+
# credential: any other credential (Melos::Credential::X509.create_credential, say); its key is then
|
|
17
|
+
# signature_private_key, the private key of the certified public key, as MLS serializes it
|
|
18
|
+
# credential_types: the credential types this client's leaves claim to support (13.3), by default basic
|
|
19
|
+
# and its own; members of a group must all support every type in use
|
|
20
|
+
def initialize(cipher_suite_id, identity: '', credential: nil, signature_private_key: nil, credential_types: nil)
|
|
13
21
|
@cipher_suite = Melos::Crypto::CipherSuite.new(cipher_suite_id)
|
|
14
|
-
@signature_key_pair = Melos::Crypto.generate_signature_key_pair(@cipher_suite)
|
|
15
|
-
@credential = Melos::Struct::Credential.create_basic_credential(identity)
|
|
22
|
+
@signature_key_pair = signature_private_key ? @cipher_suite.pkey.deserialize_private_signature_key(signature_private_key) : Melos::Crypto.generate_signature_key_pair(@cipher_suite)
|
|
23
|
+
@credential = credential || Melos::Struct::Credential.create_basic_credential(identity)
|
|
24
|
+
@credential_types = credential_types || ([Melos::Constants::CredentialType::BASIC] | [@credential.credential_type])
|
|
16
25
|
@external_psks = {} # psk_id -> psk; shared by reference with the groups this client is in
|
|
17
26
|
@key_packages = {} # KeyPackageRef -> Melos::KeyPackageBundle, until used to join a group
|
|
18
27
|
@groups = {} # group_id -> Melos::Group this client created or joined
|
|
28
|
+
@resumption_psks = {} # [group_id, epoch] -> resumption_psk, of groups no longer in @groups (8.6)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# 7.2: the Capabilities this client's leaves carry unless told otherwise
|
|
32
|
+
def default_capabilities
|
|
33
|
+
Melos::Struct::Capabilities.import(Melos::Capabilities.new(credentials: @credential_types))
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# 8.6: the resumption PSK of a group this client is or was in, or nil
|
|
37
|
+
def resumption_psk(group_id, epoch)
|
|
38
|
+
@groups[group_id]&.resumption_psks&.[](epoch) || @resumption_psks[[group_id, epoch]]
|
|
19
39
|
end
|
|
20
40
|
|
|
21
41
|
def signature_private_key
|
|
@@ -35,51 +55,201 @@ class Melos::Client
|
|
|
35
55
|
def create_key_package(capabilities: nil, lifetime: nil, extensions: [])
|
|
36
56
|
bundle = Melos::KeyPackageBundle.generate(
|
|
37
57
|
@cipher_suite, signature_private_key: signature_private_key, signature_public_key: signature_public_key,
|
|
38
|
-
credential: @credential, capabilities: capabilities, lifetime: lifetime, extensions: extensions
|
|
58
|
+
credential: @credential, capabilities: capabilities || default_capabilities, lifetime: lifetime, extensions: extensions
|
|
39
59
|
)
|
|
40
60
|
@key_packages[bundle.ref] = bundle
|
|
41
61
|
bundle
|
|
42
62
|
end
|
|
43
63
|
|
|
64
|
+
# 12.1.8: ask to join a group by proposing our own Add, as a new_member_proposal signed with the
|
|
65
|
+
# KeyPackage's key. A member who commits it sends a Welcome addressed to that KeyPackage, which
|
|
66
|
+
# #join_group then accepts. group_id and epoch are the group's current ones (from a GroupInfo, say).
|
|
67
|
+
# Returns [MLSMessage, KeyPackageBundle].
|
|
68
|
+
def create_new_member_proposal(group_id:, epoch:, authenticated_data: '', **key_package_options)
|
|
69
|
+
bundle = create_key_package(**key_package_options)
|
|
70
|
+
add = Melos::Struct::Proposal.create(proposal_type: Melos::Constants::ProposalType::ADD, add: Melos::Struct::Add.create(key_package: bundle.key_package))
|
|
71
|
+
sender = Melos::Struct::Sender.create_new_member(Melos::Constants::SenderType::NEW_MEMBER_PROPOSAL)
|
|
72
|
+
message = Melos::ExternalSender.sign_proposal(@cipher_suite, signature_private_key, add, sender,
|
|
73
|
+
group_id: group_id, epoch: epoch, authenticated_data: authenticated_data)
|
|
74
|
+
[message, bundle]
|
|
75
|
+
end
|
|
76
|
+
|
|
44
77
|
# 11: create a group with this client as its only member.
|
|
45
|
-
# options are passed to Melos::Group (credential_validator:, max_leaf_node_lifetime
|
|
78
|
+
# options are passed to Melos::Group (credential_validator:, max_leaf_node_lifetime:, ...)
|
|
46
79
|
def create_group(group_id, extensions: [], **options)
|
|
47
80
|
bundle = Melos::KeyPackageBundle.generate(
|
|
48
|
-
@cipher_suite, signature_private_key: signature_private_key, signature_public_key: signature_public_key, credential: @credential
|
|
81
|
+
@cipher_suite, signature_private_key: signature_private_key, signature_public_key: signature_public_key, credential: @credential,
|
|
82
|
+
capabilities: default_capabilities
|
|
49
83
|
)
|
|
50
84
|
ensure_group_id_free!(group_id)
|
|
51
|
-
|
|
85
|
+
store_group(Melos::Group.create(
|
|
52
86
|
@cipher_suite, group_id, bundle.key_package.leaf_node,
|
|
53
87
|
encryption_private_key: bundle.encryption_private_key, signature_private_key: signature_private_key,
|
|
54
|
-
extensions: extensions, external_psks: @external_psks, **options
|
|
88
|
+
extensions: extensions, external_psks: @external_psks, resumption_psk_lookup: method(:resumption_psk), **options
|
|
89
|
+
))
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
# 11.2: create the group that replaces old_group after a ReInit was committed in it, with the ReInit's
|
|
93
|
+
# group_id, cipher suite (this client's) and extensions, and with the same members, given as fresh
|
|
94
|
+
# KeyPackages of every other member. Returns [new group, Welcome]; the new group is already in epoch 1.
|
|
95
|
+
# member_matcher: ->(old_leaf, new_leaf) { true/false }, whether two leaves are the same member
|
|
96
|
+
def reinit_group(old_group, key_packages, member_matcher: Melos::Group::DEFAULT_MEMBER_MATCHER, **options)
|
|
97
|
+
reinit = old_group.pending_reinit
|
|
98
|
+
raise Melos::StateError.new('no ReInit has been committed in this group') if reinit.nil?
|
|
99
|
+
raise ArgumentError.new("the ReInit is for cipher suite #{reinit.cipher_suite}; use a client of that suite") unless reinit.cipher_suite == @cipher_suite.suite_id
|
|
100
|
+
raise Melos::UnsupportedError.new("unsupported MLS version #{reinit.version}") unless reinit.version == Melos::Constants::Version::MLS10
|
|
101
|
+
new_leaves = key_packages.map { leaf_of(_1) } + [own_leaf]
|
|
102
|
+
old_group.ratchet_tree.each_leaf_node do |old_leaf, _index|
|
|
103
|
+
next if new_leaves.any? { member_matcher.call(old_leaf, _1) }
|
|
104
|
+
raise ArgumentError.new('every member of the old group needs a KeyPackage in the new one')
|
|
105
|
+
end
|
|
106
|
+
resume(old_group, key_packages, Melos::Constants::ResumptionPSKUsage::REINIT, reinit.group_id, reinit.extensions, options)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# 11.3: branch a subgroup of old_group's members, given as fresh KeyPackages, with a new group_id and
|
|
110
|
+
# the same cipher suite. Returns [new group, Welcome]; the new group is already in epoch 1.
|
|
111
|
+
def branch_group(old_group, key_packages, group_id:, extensions: old_group.extensions, member_matcher: Melos::Group::DEFAULT_MEMBER_MATCHER, **options)
|
|
112
|
+
raise ArgumentError.new('a branch keeps the cipher suite of its group') unless old_group.suite.suite_id == @cipher_suite.suite_id
|
|
113
|
+
old_leaves = []
|
|
114
|
+
old_group.ratchet_tree.each_leaf_node { |leaf_node, _index| old_leaves << leaf_node }
|
|
115
|
+
(key_packages.map { leaf_of(_1) } + [own_leaf]).each do |new_leaf|
|
|
116
|
+
next if old_leaves.any? { member_matcher.call(_1, new_leaf) }
|
|
117
|
+
raise ArgumentError.new('every member of a branch must be a member of the group it branches from')
|
|
118
|
+
end
|
|
119
|
+
resume(old_group, key_packages, Melos::Constants::ResumptionPSKUsage::BRANCH, group_id, extensions, options)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# 12.4.3.2: join a group by external commit, from a GroupInfo with an external_pub extension (see
|
|
123
|
+
# Melos::Group#group_info). The new group is merged and stored right away; send the commit, and if the
|
|
124
|
+
# Delivery Service rejects it, drop the group (groups.delete(group.group_id)) and try again with a fresh GroupInfo.
|
|
125
|
+
# remove: the leaf index of this client's earlier appearance in the group, to replace it (a "resync")
|
|
126
|
+
# psk_ids: external PSKs (added with #add_external_psk) to inject
|
|
127
|
+
# Returns [group, commit MLSMessage].
|
|
128
|
+
def join_by_external_commit(group_info, ratchet_tree: nil, remove: nil, psk_ids: [], authenticated_data: '', **options)
|
|
129
|
+
bundle = Melos::KeyPackageBundle.generate(
|
|
130
|
+
@cipher_suite, signature_private_key: signature_private_key, signature_public_key: signature_public_key, credential: @credential,
|
|
131
|
+
capabilities: default_capabilities
|
|
55
132
|
)
|
|
133
|
+
group, result = Melos::Group.join_by_external_commit(
|
|
134
|
+
@cipher_suite, group_info, signature_private_key: signature_private_key, leaf_node: bundle.key_package.leaf_node,
|
|
135
|
+
ratchet_tree: ratchet_tree, remove: remove, psk_ids: psk_ids, external_psks: @external_psks, authenticated_data: authenticated_data,
|
|
136
|
+
resumption_psk_lookup: method(:resumption_psk), **options
|
|
137
|
+
)
|
|
138
|
+
# a resync replaces this client's own earlier state of the group, which may still be here
|
|
139
|
+
ensure_group_id_free!(group.group_id) unless remove
|
|
140
|
+
group.merge_commit(result.staged)
|
|
141
|
+
store_group(group)
|
|
142
|
+
[group, result.commit]
|
|
56
143
|
end
|
|
57
144
|
|
|
58
145
|
# 12.4.3.1: join the group a Welcome adds us to, using whichever of our KeyPackages it is addressed to.
|
|
59
146
|
# That KeyPackage is then forgotten: its init key is single-use.
|
|
60
|
-
|
|
147
|
+
# resumed_groups: groups held elsewhere (by a client of another cipher suite, say) that a reinit or
|
|
148
|
+
# branch Welcome may resume; this client's own groups are always looked in
|
|
149
|
+
# member_matcher: as for #reinit_group, for the checks of 11.2 and 11.3
|
|
150
|
+
def join_group(welcome, ratchet_tree: nil, resumed_groups: [], member_matcher: Melos::Group::DEFAULT_MEMBER_MATCHER, **options)
|
|
61
151
|
welcome_struct = welcome.is_a?(Melos::Struct::MLSMessage) ? welcome.welcome : welcome
|
|
62
152
|
raise ArgumentError.new('not a Welcome message') if welcome_struct.nil?
|
|
63
153
|
bundle = welcome_struct.secrets.map(&:new_member).filter_map { @key_packages[_1] }.first
|
|
64
|
-
raise
|
|
154
|
+
raise Melos::ProtocolError.new('Welcome is not addressed to any KeyPackage of this client') if bundle.nil?
|
|
65
155
|
group = Melos::Group.from_welcome(
|
|
66
156
|
@cipher_suite, welcome,
|
|
67
157
|
key_package: bundle.key_package, init_private_key: bundle.init_private_key,
|
|
68
158
|
encryption_private_key: bundle.encryption_private_key, signature_private_key: signature_private_key,
|
|
69
|
-
external_psks: @external_psks, ratchet_tree: ratchet_tree,
|
|
159
|
+
external_psks: @external_psks, ratchet_tree: ratchet_tree,
|
|
160
|
+
resumption_psk_lookup: ->(group_id, epoch) { resumed_groups.find { _1.group_id == group_id }&.resumption_psks&.[](epoch) || resumption_psk(group_id, epoch) },
|
|
161
|
+
resumed_group_lookup: ->(group_id) { resumed_groups.find { _1.group_id == group_id } || @groups[group_id] },
|
|
162
|
+
member_matcher: member_matcher, **options
|
|
70
163
|
)
|
|
71
164
|
ensure_group_id_free!(group.group_id)
|
|
72
165
|
@key_packages.delete(bundle.ref)
|
|
73
|
-
|
|
166
|
+
store_group(group)
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# version of the Hash produced by #to_h
|
|
170
|
+
STATE_FORMAT = 1
|
|
171
|
+
|
|
172
|
+
# The client's whole state, its groups included, in the form Melos::Group#to_h describes.
|
|
173
|
+
# Private keys are in it: store it encrypted.
|
|
174
|
+
def to_h
|
|
175
|
+
{
|
|
176
|
+
format: STATE_FORMAT,
|
|
177
|
+
cipher_suite: @cipher_suite.suite_id,
|
|
178
|
+
signature_private_key: signature_private_key,
|
|
179
|
+
credential: @credential.raw, credential_types: @credential_types,
|
|
180
|
+
external_psks: @external_psks.to_a,
|
|
181
|
+
key_packages: @key_packages.map { |ref, bundle| [ref, bundle.to_h] },
|
|
182
|
+
groups: @groups.map { |group_id, group| [group_id, group.to_h(include_external_psks: false)] },
|
|
183
|
+
resumption_psks: @resumption_psks.map { |(group_id, epoch), psk| [group_id, epoch, psk] }
|
|
184
|
+
}
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# A client from #to_h. group_options (credential_validator:, external_proposal_validator:) go to every group.
|
|
188
|
+
def self.from_h(hash, **group_options)
|
|
189
|
+
raise ArgumentError.new("unknown client state format #{hash[:format]}") unless hash[:format] == STATE_FORMAT
|
|
190
|
+
client = allocate
|
|
191
|
+
client.send(:restore_state, hash, group_options)
|
|
192
|
+
client
|
|
74
193
|
end
|
|
75
194
|
|
|
76
195
|
private
|
|
77
196
|
|
|
197
|
+
def restore_state(hash, group_options)
|
|
198
|
+
@cipher_suite = Melos::Crypto::CipherSuite.new(hash[:cipher_suite])
|
|
199
|
+
@signature_key_pair = @cipher_suite.pkey.deserialize_private_signature_key(hash[:signature_private_key])
|
|
200
|
+
@credential = Melos::Struct::Credential.new(hash[:credential])
|
|
201
|
+
@credential_types = hash[:credential_types] || [Melos::Constants::CredentialType::BASIC]
|
|
202
|
+
@external_psks = hash[:external_psks].to_h
|
|
203
|
+
@key_packages = hash[:key_packages].to_h { |ref, bundle| [ref, Melos::KeyPackageBundle.from_h(@cipher_suite, bundle)] }
|
|
204
|
+
@groups = hash[:groups].to_h do |group_id, group|
|
|
205
|
+
[group_id, Melos::Group.from_h(group, external_psks: @external_psks, resumption_psk_lookup: method(:resumption_psk), **group_options)]
|
|
206
|
+
end
|
|
207
|
+
@resumption_psks = (hash[:resumption_psks] || []).to_h { |group_id, epoch, psk| [[group_id, epoch], psk] }
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
# a group goes into @groups; one it replaces (removed or reinitialized, see #ensure_group_id_free!)
|
|
211
|
+
# leaves its resumption PSKs behind for groups that resume it (8.6)
|
|
212
|
+
def store_group(group)
|
|
213
|
+
if (replaced = @groups[group.group_id])
|
|
214
|
+
replaced.resumption_psks.each { |epoch, psk| @resumption_psks[[group.group_id, epoch]] = psk }
|
|
215
|
+
end
|
|
216
|
+
@groups[group.group_id] = group
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
# the options of #reinit_group and #branch_group that go to the new group's first commit rather than to the group
|
|
220
|
+
RESUMPTION_COMMIT_OPTIONS = %i[wire_format include_ratchet_tree path].freeze
|
|
221
|
+
|
|
222
|
+
# 11.2, 11.3: a new group of this client resuming old_group: created in epoch 0, then a first commit adding
|
|
223
|
+
# key_packages with the resumption PSK of old_group's current epoch, merged at once
|
|
224
|
+
def resume(old_group, key_packages, usage, group_id, extensions, options)
|
|
225
|
+
psk_id = Melos::Struct::PreSharedKeyID.create_resumption(
|
|
226
|
+
usage: usage, psk_group_id: old_group.group_id, psk_epoch: old_group.epoch, psk_nonce: SecureRandom.random_bytes(@cipher_suite.kdf.n_h)
|
|
227
|
+
)
|
|
228
|
+
psk = old_group.resumption_psks.fetch(old_group.epoch)
|
|
229
|
+
commit_options = options.slice(*RESUMPTION_COMMIT_OPTIONS)
|
|
230
|
+
group = create_group(group_id, extensions: extensions, **options.except(*RESUMPTION_COMMIT_OPTIONS))
|
|
231
|
+
result = group.send(:create_resumption_commit, key_packages, psk_id, psk, **commit_options)
|
|
232
|
+
group.merge_commit(result.staged)
|
|
233
|
+
[group, result.welcome]
|
|
234
|
+
end
|
|
235
|
+
|
|
236
|
+
def leaf_of(key_package)
|
|
237
|
+
(key_package.is_a?(Melos::Struct::MLSMessage) ? key_package.key_package : key_package).leaf_node
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
# a leaf standing for this client, for the membership checks of #reinit_group and #branch_group
|
|
241
|
+
def own_leaf
|
|
242
|
+
Melos::Struct::LeafNode.create(
|
|
243
|
+
encryption_key: '', signature_key: signature_public_key, credential: @credential, capabilities: nil,
|
|
244
|
+
leaf_node_source: nil, lifetime: nil, parent_hash: nil, extensions: [], signature: nil
|
|
245
|
+
)
|
|
246
|
+
end
|
|
247
|
+
|
|
78
248
|
# 12.4.3: a group_id MUST be unique among the groups this client is participating in;
|
|
79
249
|
# a group this client has been removed from no longer counts
|
|
80
250
|
def ensure_group_id_free!(group_id)
|
|
81
251
|
existing = @groups[group_id]
|
|
82
252
|
return if existing.nil? || !existing.active?
|
|
83
|
-
raise
|
|
253
|
+
raise Melos::StateError.new('this client is already in a group with this group_id')
|
|
84
254
|
end
|
|
85
255
|
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
require 'openssl'
|
|
2
|
+
require_relative '../constants'
|
|
3
|
+
require_relative '../struct/structs'
|
|
4
|
+
|
|
5
|
+
module Melos::Credential; end
|
|
6
|
+
|
|
7
|
+
# X.509 credentials (RFC 9420 5.3, 5.3.1): building them, and an Authentication Service check that a
|
|
8
|
+
# group can use as its credential_validator:
|
|
9
|
+
#
|
|
10
|
+
# validator = Melos::Credential::X509.new(store: store) # an OpenSSL::X509::Store with the trusted roots
|
|
11
|
+
# client.create_group(group_id, credential_validator: validator)
|
|
12
|
+
#
|
|
13
|
+
# For a leaf (or external sender) with an X.509 credential, it checks that the end-entity certificate's
|
|
14
|
+
# public key is the holder's signature_key (5.3), that the chain verifies against the store at the
|
|
15
|
+
# given time (the current time by default; this covers validity dates), and, if given, the
|
|
16
|
+
# application's identity check: which presented identifiers it accepts for this member (5.3.1).
|
|
17
|
+
# Whether a new credential is a valid successor of the one it replaces is also the application's call,
|
|
18
|
+
# through that check. Basic credentials are rejected unless accept_basic: is true.
|
|
19
|
+
class Melos::Credential::X509
|
|
20
|
+
attr_reader :last_error
|
|
21
|
+
|
|
22
|
+
# certificates: the chain, end-entity first, as OpenSSL::X509::Certificate or DER strings
|
|
23
|
+
def self.create_credential(certificates)
|
|
24
|
+
Melos::Struct::Credential.create(
|
|
25
|
+
credential_type: Melos::Constants::CredentialType::X509, identity: nil,
|
|
26
|
+
certificates: certificates.map { Melos::Struct::Certificate.create(cert_data: _1.is_a?(OpenSSL::X509::Certificate) ? _1.to_der : _1) }
|
|
27
|
+
)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# the certificates of an X.509 credential, end-entity first
|
|
31
|
+
def self.certificates(credential)
|
|
32
|
+
credential.certificates.map { OpenSSL::X509::Certificate.new(_1.cert_data) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# a public key as MLS encodes a SignaturePublicKey: the raw key for Ed25519 and Ed448, the
|
|
36
|
+
# uncompressed point for ECDSA
|
|
37
|
+
def self.signature_key_bytes(public_key)
|
|
38
|
+
if public_key.is_a?(OpenSSL::PKey::EC)
|
|
39
|
+
public_key.public_key.to_bn.to_s(2)
|
|
40
|
+
else
|
|
41
|
+
public_key.raw_public_key
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# store: OpenSSL::X509::Store holding the trust anchors (and any intermediates peers may omit)
|
|
46
|
+
# time: the time to verify the chain at, or nil for now
|
|
47
|
+
# identity_check: ->(end_entity_certificate, holder) { true/false }, the application's reference identifiers
|
|
48
|
+
# accept_basic: whether basic credentials pass (they carry nothing to check)
|
|
49
|
+
def initialize(store:, time: nil, identity_check: nil, accept_basic: false)
|
|
50
|
+
@store = store
|
|
51
|
+
@time = time
|
|
52
|
+
@identity_check = identity_check
|
|
53
|
+
@accept_basic = accept_basic
|
|
54
|
+
@last_error = nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# the credential_validator interface: holder is the LeafNode or ExternalSender carrying the credential
|
|
58
|
+
def call(credential, holder)
|
|
59
|
+
validate!(credential, holder)
|
|
60
|
+
@last_error = nil
|
|
61
|
+
true
|
|
62
|
+
rescue Melos::ProtocolError, OpenSSL::X509::CertificateError, OpenSSL::PKey::PKeyError => e
|
|
63
|
+
@last_error = e.message
|
|
64
|
+
false
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# like #call, but raises Melos::ProtocolError with the reason
|
|
68
|
+
def validate!(credential, holder)
|
|
69
|
+
case credential.credential_type
|
|
70
|
+
when Melos::Constants::CredentialType::BASIC
|
|
71
|
+
return if @accept_basic
|
|
72
|
+
raise Melos::ProtocolError.new('basic credentials are not accepted')
|
|
73
|
+
when Melos::Constants::CredentialType::X509
|
|
74
|
+
certificates = self.class.certificates(credential)
|
|
75
|
+
raise Melos::ProtocolError.new('X.509 credential has no certificates') if certificates.empty?
|
|
76
|
+
end_entity, *chain = certificates
|
|
77
|
+
# 5.3: the end-entity certificate's key is the holder's signature key
|
|
78
|
+
unless self.class.signature_key_bytes(end_entity.public_key) == holder.signature_key
|
|
79
|
+
raise Melos::ProtocolError.new('end-entity certificate key differs from the signature key')
|
|
80
|
+
end
|
|
81
|
+
context = OpenSSL::X509::StoreContext.new(@store, end_entity, chain)
|
|
82
|
+
context.time = @time if @time
|
|
83
|
+
raise Melos::ProtocolError.new("certificate chain does not verify: #{context.error_string}") unless context.verify
|
|
84
|
+
if @identity_check && !@identity_check.call(end_entity, holder)
|
|
85
|
+
raise Melos::ProtocolError.new('certificate identity not accepted for this member')
|
|
86
|
+
end
|
|
87
|
+
else
|
|
88
|
+
raise Melos::ProtocolError.new("unsupported credential type #{credential.credential_type}")
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
data/lib/melos/crypto.rb
CHANGED
|
@@ -202,6 +202,8 @@ class Melos::Crypto
|
|
|
202
202
|
@hpke = HPKE.new(HPKE::DHKEM_P384_HKDF_SHA384, HPKE::HKDF_SHA384, HPKE::AES_256_GCM)
|
|
203
203
|
@kdf = @hpke.hkdf
|
|
204
204
|
@pkey = Melos::Crypto::CipherSuite::P384
|
|
205
|
+
else
|
|
206
|
+
raise Melos::UnsupportedError.new("unsupported cipher suite #{suite_id}")
|
|
205
207
|
end
|
|
206
208
|
end
|
|
207
209
|
end
|
|
@@ -267,6 +269,26 @@ class Melos::Crypto
|
|
|
267
269
|
ctx.open(aad, ct)
|
|
268
270
|
end
|
|
269
271
|
|
|
272
|
+
# 8.3: the external key pair of an epoch, [private, public], from its external_secret
|
|
273
|
+
def self.external_key_pair(suite, external_secret)
|
|
274
|
+
derive_key_pair(suite, external_secret)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# 8.3 (joiner): kem_output, context = SetupBaseS(external_pub, ""); init_secret = context.export("MLS 1.0 external init secret", KDF.Nh)
|
|
278
|
+
# Returns [kem_output, init_secret].
|
|
279
|
+
def self.external_init_sender(suite, external_pub)
|
|
280
|
+
context = suite.hpke.setup_base_s(suite.pkey.deserialize_public_encapsulation_key(external_pub), '')
|
|
281
|
+
[context[:enc], context[:context_s].export('MLS 1.0 external init secret', suite.kdf.n_h)]
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# 8.3 (members): context = SetupBaseR(kem_output, external_priv, ""); the same init_secret
|
|
285
|
+
def self.external_init_receiver(suite, external_private_key, kem_output)
|
|
286
|
+
context = suite.hpke.setup_base_r(kem_output, suite.pkey.deserialize_private_encapsulation_key(external_private_key), '')
|
|
287
|
+
context.export('MLS 1.0 external init secret', suite.kdf.n_h)
|
|
288
|
+
rescue OpenSSL::PKey::PKeyError, ArgumentError => e
|
|
289
|
+
raise Melos::ProtocolError.new("ExternalInit kem_output is invalid: #{e.message}")
|
|
290
|
+
end
|
|
291
|
+
|
|
270
292
|
def self.encrypt_with_label(suite, public_key, label, context, plaintext)
|
|
271
293
|
encrypt_context = Melos::Vec.string_to_vec("MLS 1.0 " + label) + Melos::Vec.string_to_vec(context)
|
|
272
294
|
pkey = suite.pkey.deserialize_public_encapsulation_key(public_key)
|
|
@@ -303,8 +325,11 @@ class Melos::Crypto
|
|
|
303
325
|
suite.hpke.aead_encrypt(key, nonce, aad, plaintext)
|
|
304
326
|
end
|
|
305
327
|
|
|
328
|
+
# a ciphertext that does not authenticate is input to reject (Melos::ProtocolError)
|
|
306
329
|
def self.aead_decrypt(suite, key, nonce, aad, ciphertext)
|
|
307
330
|
suite.hpke.aead_decrypt(key, nonce, aad, ciphertext)
|
|
331
|
+
rescue OpenSSL::Cipher::CipherError => e
|
|
332
|
+
raise Melos::ProtocolError.new("AEAD decryption failed: #{e.message}")
|
|
308
333
|
end
|
|
309
334
|
|
|
310
335
|
def self.sender_data_key(suite, sender_data_secret, ciphertext)
|
data/lib/melos/epoch_secrets.rb
CHANGED
|
@@ -8,7 +8,7 @@ require_relative 'secret_tree'
|
|
|
8
8
|
# A new epoch comes from #next with the commit secret, PSK secret and new GroupContext,
|
|
9
9
|
# which uses this epoch's init_secret as Section 8 describes.
|
|
10
10
|
class Melos::EpochSecrets
|
|
11
|
-
attr_reader :suite, :epoch_secret, :joiner_secret, :psk_secret, :n_leaves
|
|
11
|
+
attr_reader :suite, :epoch_secret, :joiner_secret, :psk_secret, :n_leaves, :secret_tree_options
|
|
12
12
|
|
|
13
13
|
# Table 4, plus init_secret which is derived the same way
|
|
14
14
|
DERIVED_SECRETS = {
|
|
@@ -29,8 +29,15 @@ class Melos::EpochSecrets
|
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# whether the derived secret name can be had: it is held, or epoch_secret is there to derive it
|
|
33
|
+
def available?(name)
|
|
34
|
+
@derived.key?(name) || !@epoch_secret.nil?
|
|
35
|
+
end
|
|
36
|
+
|
|
32
37
|
# derived: secrets known without an epoch_secret (see .from_init_secret and .from_components)
|
|
33
|
-
|
|
38
|
+
# secret_tree_options: passed to Melos::SecretTree (max_forward_distance:, out_of_order_tolerance:);
|
|
39
|
+
# every epoch made by #next inherits them
|
|
40
|
+
def initialize(suite, epoch_secret: nil, joiner_secret: nil, psk_secret: nil, n_leaves: nil, derived: {}, secret_tree_options: {})
|
|
34
41
|
unknown = derived.keys - DERIVED_SECRETS.keys
|
|
35
42
|
raise ArgumentError.new("unknown secrets: #{unknown.join(', ')}") unless unknown.empty?
|
|
36
43
|
@suite = suite
|
|
@@ -39,6 +46,7 @@ class Melos::EpochSecrets
|
|
|
39
46
|
@psk_secret = psk_secret
|
|
40
47
|
@n_leaves = n_leaves
|
|
41
48
|
@derived = derived.dup
|
|
49
|
+
@secret_tree_options = secret_tree_options
|
|
42
50
|
end
|
|
43
51
|
|
|
44
52
|
def self.from_epoch_secret(suite, epoch_secret, n_leaves: nil)
|
|
@@ -47,16 +55,17 @@ class Melos::EpochSecrets
|
|
|
47
55
|
|
|
48
56
|
# 8: epoch_secret = ExpandWithLabel(Extract(joiner_secret, psk_secret), "epoch", GroupContext)
|
|
49
57
|
# This is how a new member joins an epoch from a Welcome (12.4.3.1).
|
|
50
|
-
def self.from_joiner(suite, joiner_secret, psk_secret, group_context, n_leaves: nil)
|
|
58
|
+
def self.from_joiner(suite, joiner_secret, psk_secret, group_context, n_leaves: nil, secret_tree_options: {})
|
|
51
59
|
member_secret = Melos::Crypto.kdf_extract(suite, joiner_secret, psk_secret)
|
|
52
60
|
epoch_secret = Melos::Crypto.expand_with_label(suite, member_secret, "epoch", group_context.raw, suite.kdf.n_h)
|
|
53
|
-
new(suite, epoch_secret: epoch_secret, joiner_secret: joiner_secret, psk_secret: psk_secret, n_leaves: n_leaves
|
|
61
|
+
new(suite, epoch_secret: epoch_secret, joiner_secret: joiner_secret, psk_secret: psk_secret, n_leaves: n_leaves,
|
|
62
|
+
secret_tree_options: secret_tree_options)
|
|
54
63
|
end
|
|
55
64
|
|
|
56
65
|
# 8: the state before an epoch exists, when only init_secret is known (a fresh random
|
|
57
66
|
# value for a new group). Only #next is meaningful on it.
|
|
58
|
-
def self.from_init_secret(suite, init_secret)
|
|
59
|
-
new(suite, derived: { init_secret: init_secret })
|
|
67
|
+
def self.from_init_secret(suite, init_secret, secret_tree_options: {})
|
|
68
|
+
new(suite, derived: { init_secret: init_secret }, secret_tree_options: secret_tree_options)
|
|
60
69
|
end
|
|
61
70
|
|
|
62
71
|
# an epoch given by some of its derived secrets rather than by epoch_secret; for test vectors
|
|
@@ -92,13 +101,57 @@ class Melos::EpochSecrets
|
|
|
92
101
|
joiner_secret = Melos::Crypto.expand_with_label(
|
|
93
102
|
@suite, Melos::Crypto.kdf_extract(@suite, init_secret, commit_secret), "joiner", group_context.raw, @suite.kdf.n_h
|
|
94
103
|
)
|
|
95
|
-
self.class.from_joiner(@suite, joiner_secret, psk_secret, group_context, n_leaves: n_leaves)
|
|
104
|
+
self.class.from_joiner(@suite, joiner_secret, psk_secret, group_context, n_leaves: n_leaves, secret_tree_options: @secret_tree_options)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# 8.5: MLS-Exporter(label, context, length) = ExpandWithLabel(DeriveSecret(exporter_secret, label), "exported", Hash(context), length)
|
|
108
|
+
def export(label, context, length)
|
|
109
|
+
Melos::Crypto.expand_with_label(@suite, Melos::Crypto.derive_secret(@suite, exporter_secret, label), "exported", Melos::Crypto.hash(@suite, context), length)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# 9.2: once this epoch is the current one, the secrets that produced it have been consumed. Derive
|
|
113
|
+
# everything the epoch still needs (the Table 4 secrets and the secret tree), then drop epoch_secret,
|
|
114
|
+
# joiner_secret, psk_secret and encryption_secret. init_secret stays until the next epoch exists.
|
|
115
|
+
# Ruby cannot zeroize strings; this drops the references so they can be collected.
|
|
116
|
+
def discard_consumed!
|
|
117
|
+
return self if @epoch_secret.nil?
|
|
118
|
+
DERIVED_SECRETS.each_key { send(_1) }
|
|
119
|
+
secret_tree
|
|
120
|
+
@derived.delete(:encryption_secret)
|
|
121
|
+
@epoch_secret = nil
|
|
122
|
+
@joiner_secret = nil
|
|
123
|
+
@psk_secret = nil
|
|
124
|
+
self
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# 9.1, 15.3: what an epoch that is no longer current keeps so that its late application messages
|
|
128
|
+
# can still be read: the secret tree and the sender data secret. Everything else, init_secret
|
|
129
|
+
# included (consumed by the next epoch, 9.2), is dropped.
|
|
130
|
+
def retain_for_past_messages!
|
|
131
|
+
discard_consumed!
|
|
132
|
+
@derived.select! { |name, _| name == :sender_data_secret }
|
|
133
|
+
self
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
# the epoch's remaining secrets and its secret tree's state, as a Hash of binary strings and integers
|
|
137
|
+
def to_h
|
|
138
|
+
{
|
|
139
|
+
epoch_secret: @epoch_secret, joiner_secret: @joiner_secret, psk_secret: @psk_secret, n_leaves: @n_leaves,
|
|
140
|
+
derived: @derived.dup, secret_tree_options: @secret_tree_options, secret_tree: @secret_tree&.to_h
|
|
141
|
+
}
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def self.from_h(suite, hash)
|
|
145
|
+
epoch_secrets = new(suite, epoch_secret: hash[:epoch_secret], joiner_secret: hash[:joiner_secret], psk_secret: hash[:psk_secret],
|
|
146
|
+
n_leaves: hash[:n_leaves], derived: hash[:derived], secret_tree_options: hash[:secret_tree_options] || {})
|
|
147
|
+
epoch_secrets.instance_variable_set(:@secret_tree, Melos::SecretTree.from_h(suite, hash[:secret_tree])) if hash[:secret_tree]
|
|
148
|
+
epoch_secrets
|
|
96
149
|
end
|
|
97
150
|
|
|
98
151
|
# 9: this epoch's secret tree, built on first use
|
|
99
152
|
def secret_tree
|
|
100
153
|
raise ArgumentError.new('n_leaves is needed to build the secret tree') if @n_leaves.nil?
|
|
101
|
-
@secret_tree ||= Melos::SecretTree.new(@suite, @n_leaves, encryption_secret)
|
|
154
|
+
@secret_tree ||= Melos::SecretTree.new(@suite, @n_leaves, encryption_secret, **@secret_tree_options)
|
|
102
155
|
end
|
|
103
156
|
|
|
104
157
|
private
|
data/lib/melos/errors.rb
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module Melos
|
|
2
|
+
# Base class of everything melos raises on purpose. Plain ArgumentError is still used for
|
|
3
|
+
# mistakes in how the library is called (wrong object type, missing option, internal misuse).
|
|
4
|
+
class Error < StandardError; end
|
|
5
|
+
|
|
6
|
+
# Input that RFC 9420 says MUST be rejected: a malformed encoding, a signature, MAC or hash that
|
|
7
|
+
# does not verify, a proposal or commit that breaks the protocol's rules, a tree that fails its
|
|
8
|
+
# integrity checks. The message is not to be acted on.
|
|
9
|
+
class ProtocolError < Error; end
|
|
10
|
+
|
|
11
|
+
# An operation this group or client cannot perform in its current state: sending after being
|
|
12
|
+
# removed, sending application data while proposals are pending, a PSK or key we do not hold,
|
|
13
|
+
# a group_id already in use.
|
|
14
|
+
class StateError < Error; end
|
|
15
|
+
|
|
16
|
+
# A part of RFC 9420 this implementation does not provide yet. See docs/roadmap.md.
|
|
17
|
+
class UnsupportedError < Error; end
|
|
18
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
require 'securerandom'
|
|
2
|
+
require_relative 'crypto'
|
|
3
|
+
require_relative 'constants'
|
|
4
|
+
require_relative 'struct/structs'
|
|
5
|
+
|
|
6
|
+
# A party outside a group that may send it proposals (RFC 9420 Section 12.1.8): a signature key pair
|
|
7
|
+
# and a credential, listed in the group's external_senders GroupContext extension. It holds no group
|
|
8
|
+
# state; the caller supplies the group_id and epoch (known from a GroupInfo, say) and its index in
|
|
9
|
+
# the extension. Proposals go out as PublicMessages, since an external sender has no epoch secrets.
|
|
10
|
+
class Melos::ExternalSender
|
|
11
|
+
attr_reader :suite, :credential
|
|
12
|
+
|
|
13
|
+
# signature_private_key: an existing key, or nil to generate one
|
|
14
|
+
def initialize(cipher_suite_id, identity: '', credential: nil, signature_private_key: nil)
|
|
15
|
+
@suite = Melos::Crypto::CipherSuite.new(cipher_suite_id)
|
|
16
|
+
@signature_key_pair = signature_private_key ? @suite.pkey.deserialize_private_signature_key(signature_private_key) : Melos::Crypto.generate_signature_key_pair(@suite)
|
|
17
|
+
@credential = credential || Melos::Struct::Credential.create_basic_credential(identity)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def signature_private_key
|
|
21
|
+
Melos::Crypto.serialize_private_key(@suite, @signature_key_pair)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def signature_public_key
|
|
25
|
+
Melos::Crypto.serialize_public_key(@suite, @signature_key_pair)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# 12.1.8.1: this sender's entry in an external_senders extension
|
|
29
|
+
def external_sender
|
|
30
|
+
Melos::Struct::ExternalSender.create(signature_key: signature_public_key, credential: @credential)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# 12.1.8: sign and frame a Proposal from this sender, as an MLSMessage carrying a PublicMessage.
|
|
34
|
+
# sender_index: this sender's position in the group's external_senders extension
|
|
35
|
+
def create_proposal(proposal, group_id:, epoch:, sender_index:, authenticated_data: '')
|
|
36
|
+
unless Melos::Group::EXTERNAL_PROPOSAL_TYPES.include?(proposal.proposal_type)
|
|
37
|
+
raise ArgumentError.new("an external sender cannot send a proposal of type #{proposal.proposal_type}")
|
|
38
|
+
end
|
|
39
|
+
self.class.sign_proposal(@suite, signature_private_key, proposal, Melos::Struct::Sender.create_external(sender_index),
|
|
40
|
+
group_id: group_id, epoch: epoch, authenticated_data: authenticated_data)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# 12.1.1: propose adding a member by its KeyPackage (or the MLSMessage carrying it)
|
|
44
|
+
def create_add_proposal(key_package, **options)
|
|
45
|
+
key_package = key_package.key_package if key_package.is_a?(Melos::Struct::MLSMessage)
|
|
46
|
+
create_proposal(Melos::Struct::Proposal.create(proposal_type: Melos::Constants::ProposalType::ADD, add: Melos::Struct::Add.create(key_package: key_package)), **options)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# 12.1.3: propose removing the member at removed_leaf_index
|
|
50
|
+
def create_remove_proposal(removed_leaf_index, **options)
|
|
51
|
+
create_proposal(Melos::Struct::Proposal.create(proposal_type: Melos::Constants::ProposalType::REMOVE, remove: Melos::Struct::Remove.create(removed: removed_leaf_index)), **options)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# 12.1.4: propose injecting an external PSK the members know
|
|
55
|
+
def create_psk_proposal(psk_id, **options)
|
|
56
|
+
psk = Melos::Struct::PreSharedKey.create(
|
|
57
|
+
psk: Melos::Struct::PreSharedKeyID.create_external(psk_id: psk_id, psk_nonce: SecureRandom.random_bytes(@suite.kdf.n_h))
|
|
58
|
+
)
|
|
59
|
+
create_proposal(Melos::Struct::Proposal.create(proposal_type: Melos::Constants::ProposalType::PSK, psk: psk), **options)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# 12.1.7: propose replacing the GroupContext extensions
|
|
63
|
+
def create_group_context_extensions_proposal(extensions, **options)
|
|
64
|
+
gce = Melos::Struct::GroupContextExtensions.create(extensions: extensions)
|
|
65
|
+
create_proposal(Melos::Struct::Proposal.create(proposal_type: Melos::Constants::ProposalType::GROUP_CONTEXT_EXTENSIONS, group_context_extensions: gce), **options)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# 6.1: a proposal from outside the group, signed without a GroupContext, as a PublicMessage (no membership tag)
|
|
69
|
+
def self.sign_proposal(suite, signature_private_key, proposal, sender, group_id:, epoch:, authenticated_data: '')
|
|
70
|
+
framed_content = Melos::Struct::FramedContent.create(
|
|
71
|
+
group_id: group_id, epoch: epoch, sender: sender, authenticated_data: authenticated_data,
|
|
72
|
+
content_type: Melos::Constants::ContentType::PROPOSAL, content: proposal
|
|
73
|
+
)
|
|
74
|
+
authenticated_content = Melos::Struct::AuthenticatedContent.create(wire_format: Melos::Constants::WireFormat::MLS_PUBLIC_MESSAGE, content: framed_content, auth: nil)
|
|
75
|
+
authenticated_content.sign(suite, signature_private_key, nil)
|
|
76
|
+
Melos::Struct::MLSMessage.create(public_message: Melos::Struct::PublicMessage.protect(authenticated_content, suite, nil, nil))
|
|
77
|
+
end
|
|
78
|
+
end
|