1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 DeRec Alliance. All rights reserved.
use crate::derec_message::current_timestamp;
use crate::primitives::pairing::PairingError;
use crate::types::ChannelId;
use derec_cryptography::pairing::PairingContactMessageMaterial;
use derec_proto::{ContactMessage, ContactMode, TransportProtocol};
/// Extension trait that attaches structural validation as a method on
/// the proto [`ContactMessage`].
///
/// The proto type lives in [`derec_proto`], so an inherent `impl` is
/// impossible from this crate — this trait sidesteps the orphan rule
/// the same way [`TransportProtocolExt`](super::transport_protocol::TransportProtocolExt) does for
/// [`derec_proto::TransportProtocol`]. Bring the trait into scope
/// (`use crate::extensions::contact_message::ContactMessageExt as _;`) at every parse
/// boundary that ingests an untrusted `ContactMessage` and call
/// `contact.validate()?` before handing it to protocol code.
pub(crate) trait ContactMessageExt {
/// Structural validator for a decoded [`ContactMessage`]. Enforces
/// the per-mode field-presence invariants the proto schema
/// documents but cannot itself express:
///
/// - The declared `contact_mode` must be a known [`ContactMode`]
/// value.
/// - [`ContactMode::InlineKeys`]: `mlkem_encapsulation_key` and
/// `ecies_public_key` MUST be present and non-empty;
/// `contact_binding_hash` MUST be absent (would mean the contact
/// is misshaped — keys present AND a commitment).
/// - [`ContactMode::HashedKeys`]: both inline-key fields MUST be
/// absent, and `contact_binding_hash` MUST be present with
/// exactly 48 bytes (SHA-384 digest size).
/// - [`ContactMode::NoKeys`]: all three of
/// `mlkem_encapsulation_key`, `ecies_public_key`, and
/// `contact_binding_hash` MUST be absent. The contact carries
/// only `channel_id`, `nonce`, and `transport_protocol`; any
/// key-shaped field on a NoKeys contact is a protocol misuse.
///
/// The bidirectional check matters most for `HashedKeys` and
/// `NoKeys`: a malformed `HashedKeys` contact that carried inline
/// keys could have its keys consumed downstream without the
/// binding-hash recomputation that gives the mode its MITM
/// resistance; a malformed `NoKeys` contact that carried
/// unauthenticated key material would silently upgrade the flow to
/// a mode the OOB channel wasn't trusted for.
fn validate(&self) -> Result<(), crate::Error>;
/// Returns `true` when this contact's mode requires a `PrePairRequest`
/// round-trip before the responder can produce a `PairRequestMessage`.
///
/// - [`ContactMode::HashedKeys`] and [`ContactMode::NoKeys`] return
/// `true` — both require the responder to fetch key material via
/// `PrePair` before the encrypted pairing handshake can begin.
/// - [`ContactMode::InlineKeys`] returns `false` — the keys are
/// already carried in the contact itself.
/// - Unknown enum values return `false`; downstream validation
/// (e.g. [`Self::validate`]) is responsible for rejecting them.
fn requires_pre_pair(&self) -> bool;
/// Build a [`ContactMode::InlineKeys`] [`ContactMessage`] carrying
/// the given ML-KEM + ECIES public key material verbatim.
///
/// `own` is every endpoint the contact creator serves, in its own
/// preference order. It fills `supported_transports` wholesale, and
/// its first entry also fills the legacy singular
/// `transport_protocol` so peers predating the offer list still find
/// an endpoint. Deriving the singular field here rather than taking
/// it as a second argument is what keeps the two from disagreeing.
/// An empty `own` yields an absent singular field; callers reject
/// that case before constructing a contact.
///
/// Timestamp is stamped with the current wall-clock; callers that
/// need deterministic timestamps must construct the proto struct
/// directly.
fn inline_keys(
channel_id: ChannelId,
nonce: u64,
own: Vec<TransportProtocol>,
pk: PairingContactMessageMaterial,
) -> ContactMessage;
/// Build a [`ContactMode::HashedKeys`] [`ContactMessage`]: no key
/// material inlined, but a SHA-384 commitment (`contact_binding_hash`)
/// over `(mlkem_encapsulation_key || ecies_public_key || u64_be(nonce)
/// || u64_be(channel_id))` so the scanner can verify keys received
/// later via `PrePair` against the commitment.
///
/// `own` is outside the binding hash, exactly like the singular
/// field derived from it.
///
/// Timestamp is stamped with the current wall-clock.
fn hashed_keys(
channel_id: ChannelId,
nonce: u64,
own: Vec<TransportProtocol>,
pk: &PairingContactMessageMaterial,
) -> ContactMessage;
/// Build a [`ContactMode::NoKeys`] [`ContactMessage`]: no key
/// material and no commitment — only `channel_id`, `nonce`, and
/// `transport_protocol`. The contact creator generates key material
/// on the fly when the corresponding `PrePairRequest` arrives; the
/// scanner accepts the returned keys without cryptographic
/// verification.
///
/// Trust rests entirely on the out-of-band delivery channel being
/// fully trusted. Applications MUST rate-limit inbound
/// `PrePairRequest`s per channel and expire outstanding NoKeys
/// contacts on a short timer.
///
/// Timestamp is stamped with the current wall-clock.
fn no_keys(channel_id: ChannelId, nonce: u64, own: Vec<TransportProtocol>) -> ContactMessage;
}
impl ContactMessageExt for ContactMessage {
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn validate(&self) -> Result<(), crate::Error> {
let mode = ContactMode::try_from(self.contact_mode).map_err(|_| {
#[cfg(feature = "logging")]
tracing::warn!(
contact_mode = self.contact_mode,
"unknown contact_mode value"
);
PairingError::InvalidContactMessage("unknown contact_mode value")
})?;
let mlkem_present = self
.mlkem_encapsulation_key
.as_ref()
.is_some_and(|v| !v.is_empty());
let ecies_present = self
.ecies_public_key
.as_ref()
.is_some_and(|v| !v.is_empty());
let hash_present = self
.contact_binding_hash
.as_ref()
.is_some_and(|v| !v.is_empty());
match mode {
ContactMode::InlineKeys => {
if !mlkem_present {
return Err(PairingError::InvalidContactMessage(
"inline_keys contact missing mlkem_encapsulation_key",
)
.into());
}
if !ecies_present {
return Err(PairingError::InvalidContactMessage(
"inline_keys contact missing ecies_public_key",
)
.into());
}
if hash_present {
return Err(PairingError::InvalidContactMessage(
"inline_keys contact must not carry contact_binding_hash",
)
.into());
}
}
ContactMode::HashedKeys => {
if mlkem_present || ecies_present {
return Err(PairingError::InvalidContactMessage(
"hashed_keys contact must not carry inline keys",
)
.into());
}
const BINDING_HASH_LEN: usize = 48;
let hash = self.contact_binding_hash.as_ref().ok_or(
PairingError::InvalidContactMessage(
"hashed_keys contact missing contact_binding_hash",
),
)?;
if hash.is_empty() {
return Err(PairingError::InvalidContactMessage(
"hashed_keys contact missing contact_binding_hash",
)
.into());
}
if hash.len() != BINDING_HASH_LEN {
return Err(PairingError::InvalidContactMessage(
"hashed_keys contact_binding_hash is not a SHA-384 digest",
)
.into());
}
}
ContactMode::NoKeys => {
if mlkem_present || ecies_present {
return Err(PairingError::InvalidContactMessage(
"no_keys contact must not carry inline keys",
)
.into());
}
if hash_present {
return Err(PairingError::InvalidContactMessage(
"no_keys contact must not carry contact_binding_hash",
)
.into());
}
}
}
// A contact naming no endpoint at all gives the scanner nowhere to
// send the pair request. Either spelling satisfies this: the list,
// or the deprecated singular field a peer predating it fills.
let has_offers = !self.supported_transports.is_empty();
let has_singular = self
.transport_protocol
.as_ref()
.is_some_and(|tp| !tp.uri.trim().is_empty());
if !has_offers && !has_singular {
#[cfg(feature = "logging")]
tracing::warn!("contact advertises no usable transport endpoint");
return Err(PairingError::EmptyTransportUri.into());
}
Ok(())
}
fn requires_pre_pair(&self) -> bool {
self.contact_mode == ContactMode::HashedKeys as i32
|| self.contact_mode == ContactMode::NoKeys as i32
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn inline_keys(
channel_id: ChannelId,
nonce: u64,
own: Vec<TransportProtocol>,
pk: PairingContactMessageMaterial,
) -> ContactMessage {
ContactMessage {
channel_id: channel_id.into(),
transport_protocol: own.first().cloned(),
contact_mode: ContactMode::InlineKeys as i32,
mlkem_encapsulation_key: Some(pk.mlkem_encapsulation_key),
ecies_public_key: Some(pk.ecies_public_key),
contact_binding_hash: None,
nonce,
timestamp: Some(current_timestamp()),
supported_transports: own,
}
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn hashed_keys(
channel_id: ChannelId,
nonce: u64,
own: Vec<TransportProtocol>,
pk: &PairingContactMessageMaterial,
) -> ContactMessage {
let binding_hash = derec_cryptography::pairing::contact_binding_hash(
&pk.mlkem_encapsulation_key,
&pk.ecies_public_key,
nonce,
channel_id.into(),
);
ContactMessage {
channel_id: channel_id.into(),
transport_protocol: own.first().cloned(),
contact_mode: ContactMode::HashedKeys as i32,
mlkem_encapsulation_key: None,
ecies_public_key: None,
contact_binding_hash: Some(binding_hash.to_vec()),
nonce,
timestamp: Some(current_timestamp()),
supported_transports: own,
}
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn no_keys(channel_id: ChannelId, nonce: u64, own: Vec<TransportProtocol>) -> ContactMessage {
ContactMessage {
channel_id: channel_id.into(),
transport_protocol: own.first().cloned(),
contact_mode: ContactMode::NoKeys as i32,
mlkem_encapsulation_key: None,
ecies_public_key: None,
contact_binding_hash: None,
nonce,
timestamp: Some(current_timestamp()),
supported_transports: own,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Error;
use derec_proto::Protocol;
use prost_types::Timestamp;
fn ts(seconds: i64) -> Timestamp {
Timestamp { seconds, nanos: 0 }
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn well_formed_inline_keys_contact() -> ContactMessage {
ContactMessage {
channel_id: 42,
transport_protocol: Some(TransportProtocol {
uri: "https://relay.example/alice".to_owned(),
protocol: Protocol::Https.into(),
}),
contact_mode: ContactMode::InlineKeys as i32,
mlkem_encapsulation_key: Some(vec![1; 1184]),
ecies_public_key: Some(vec![2; 33]),
contact_binding_hash: None,
nonce: 0xCAFE_BABE,
timestamp: Some(ts(1_700_000_000)),
supported_transports: Vec::new(),
}
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn well_formed_hashed_keys_contact() -> ContactMessage {
ContactMessage {
channel_id: 42,
transport_protocol: Some(TransportProtocol {
uri: "https://relay.example/alice/ephemeral".to_owned(),
protocol: Protocol::Https.into(),
}),
contact_mode: ContactMode::HashedKeys as i32,
mlkem_encapsulation_key: None,
ecies_public_key: None,
contact_binding_hash: Some(vec![0xAB; 48]),
nonce: 0xDEAD_BEEF,
timestamp: Some(ts(1_700_000_000)),
supported_transports: Vec::new(),
}
}
// Compatibility, not oversight — see the `transport` module docs.
#[allow(deprecated)]
fn well_formed_no_keys_contact() -> ContactMessage {
ContactMessage {
channel_id: 1234,
transport_protocol: Some(TransportProtocol {
uri: "https://institution.example/pair".to_owned(),
protocol: Protocol::Https.into(),
}),
contact_mode: ContactMode::NoKeys as i32,
mlkem_encapsulation_key: None,
ecies_public_key: None,
contact_binding_hash: None,
nonce: 4321,
timestamp: Some(ts(1_700_000_000)),
supported_transports: Vec::new(),
}
}
fn assert_invalid_contact(result: Result<(), crate::Error>, expected: &'static str) {
match result {
Err(Error::Pairing(PairingError::InvalidContactMessage(m))) => {
assert_eq!(m, expected, "wrong InvalidContactMessage payload")
}
other => panic!("expected InvalidContactMessage({expected:?}), got {other:?}"),
}
}
#[test]
fn validate_accepts_well_formed_inline_keys() {
well_formed_inline_keys_contact()
.validate()
.expect("well-formed inline_keys must pass");
}
#[test]
fn validate_accepts_well_formed_hashed_keys() {
well_formed_hashed_keys_contact()
.validate()
.expect("well-formed hashed_keys must pass");
}
#[test]
fn validate_accepts_well_formed_no_keys() {
well_formed_no_keys_contact()
.validate()
.expect("well-formed no_keys must pass");
}
#[test]
fn validate_rejects_unknown_contact_mode() {
let mut c = well_formed_inline_keys_contact();
c.contact_mode = 7;
assert_invalid_contact(c.validate(), "unknown contact_mode value");
}
#[test]
fn validate_rejects_inline_missing_mlkem_key() {
let mut c = well_formed_inline_keys_contact();
c.mlkem_encapsulation_key = None;
assert_invalid_contact(
c.validate(),
"inline_keys contact missing mlkem_encapsulation_key",
);
}
#[test]
fn validate_rejects_inline_missing_ecies_key() {
let mut c = well_formed_inline_keys_contact();
c.ecies_public_key = None;
assert_invalid_contact(c.validate(), "inline_keys contact missing ecies_public_key");
}
#[test]
fn validate_rejects_inline_with_binding_hash() {
let mut c = well_formed_inline_keys_contact();
c.contact_binding_hash = Some(vec![0xCD; 48]);
assert_invalid_contact(
c.validate(),
"inline_keys contact must not carry contact_binding_hash",
);
}
#[test]
fn validate_rejects_hashed_with_inline_keys() {
let mut c = well_formed_hashed_keys_contact();
c.mlkem_encapsulation_key = Some(vec![1; 1184]);
assert_invalid_contact(
c.validate(),
"hashed_keys contact must not carry inline keys",
);
}
#[test]
fn validate_rejects_hashed_missing_binding_hash() {
let mut c = well_formed_hashed_keys_contact();
c.contact_binding_hash = None;
assert_invalid_contact(
c.validate(),
"hashed_keys contact missing contact_binding_hash",
);
}
#[test]
fn validate_rejects_hashed_empty_binding_hash() {
let mut c = well_formed_hashed_keys_contact();
c.contact_binding_hash = Some(Vec::new());
assert_invalid_contact(
c.validate(),
"hashed_keys contact missing contact_binding_hash",
);
}
#[test]
fn validate_rejects_hashed_wrong_hash_length() {
let mut c = well_formed_hashed_keys_contact();
c.contact_binding_hash = Some(vec![0xAB; 32]);
assert_invalid_contact(
c.validate(),
"hashed_keys contact_binding_hash is not a SHA-384 digest",
);
}
#[test]
fn validate_rejects_no_keys_with_mlkem_key() {
let mut c = well_formed_no_keys_contact();
c.mlkem_encapsulation_key = Some(vec![1; 1184]);
assert_invalid_contact(c.validate(), "no_keys contact must not carry inline keys");
}
#[test]
fn validate_rejects_no_keys_with_ecies_key() {
let mut c = well_formed_no_keys_contact();
c.ecies_public_key = Some(vec![2; 33]);
assert_invalid_contact(c.validate(), "no_keys contact must not carry inline keys");
}
#[test]
fn validate_rejects_no_keys_with_binding_hash() {
let mut c = well_formed_no_keys_contact();
c.contact_binding_hash = Some(vec![0xAB; 48]);
assert_invalid_contact(
c.validate(),
"no_keys contact must not carry contact_binding_hash",
);
}
}