wacore 0.7.0

Core WhatsApp protocol implementation without runtime dependencies
Documentation
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! ADV (Advanced Device Verification) key index utilities.
//!
//! Decodes `ADVSignedKeyIndexList` protobuf from `key-index-list` elements
//! and filters device lists by `valid_indexes`.
//!
//! Reference: WAWebHandleAdvDeviceNotificationUtils.decodeSignedKeyIndexBytes()

use crate::libsignal::protocol::PublicKey;
use crate::store::traits::DeviceInfo;

// ADV signature prefixes (WAWebAdvSignatureConstants). The hosted ([6,5]/[6,6])
// variants apply to business-hosted companion devices.
const ADV_PREFIX_ACCOUNT_SIGNATURE: &[u8] = &[6, 0];
const ADV_PREFIX_DEVICE_SIGNATURE: &[u8] = &[6, 1];
const ADV_HOSTED_PREFIX_ACCOUNT_SIGNATURE: &[u8] = &[6, 5];
const ADV_HOSTED_PREFIX_DEVICE_SIGNATURE: &[u8] = &[6, 6];

/// Outcome of validating a fetched companion device's `ADVSignedDeviceIdentity`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdvValidation {
    /// Both signatures verified against an available account key — trusted.
    Valid,
    /// The blob is malformed, or the signatures failed to verify against an
    /// available account key. A relay swapping in a forged identity lands here;
    /// the caller must reject the bundle.
    Invalid,
    /// No account key was available: the blob omits `account_signature_key` and
    /// no trusted account identity was passed as a fallback, so the chain can't
    /// be checked at all. The caller decides what to do (we log and proceed,
    /// matching the existing "device-identity absent" handling).
    NoAccountKey,
}

/// Verify a fetched companion device's `ADVSignedDeviceIdentity` binds the
/// fetched identity key to the account's ADV chain, mirroring WA Web
/// `WAWebAdvSignatureApi.validateADVwithIdentityKey`.
///
/// Two checks must both hold under one prefix family: the account signature over
/// `prefix || details || identity`, and the device signature (made with the
/// fetched identity key) over `prefix || details || identity || accountKey`. The
/// device signature is what binds the fetched identity to the account, so a relay
/// that substitutes a fabricated identity key is rejected. We try the E2EE then
/// the hosted prefix set rather than replicating WA Web's `bizHostedDevicesEnabled`
/// gating: the prefix is only a domain separator, so accepting whichever the
/// signer used stays sound (an attacker still can't forge the account signature).
///
/// The account key is `blob.account_signature_key || account_identity_fallback`,
/// exactly as WA Web's `P`/`w` helpers (`e.accountSignatureKey || t`): the server
/// legitimately omits `account_signature_key` for a contact's companion device
/// because it's the contact's primary (device 0) identity the client already
/// holds, so the caller passes that stored identity as the fallback. When neither
/// source has a key the chain is unverifiable and we return `NoAccountKey` rather
/// than rejecting a bundle we simply lack the material to check.
pub fn validate_adv_with_identity_key(
    device_identity_bytes: &[u8],
    fetched_identity_key: &[u8; 32],
    account_identity_fallback: Option<&[u8; 32]>,
) -> AdvValidation {
    let Ok(signed) = waproto::codec::adv_signed_device_identity_decode(device_identity_bytes)
    else {
        return AdvValidation::Invalid;
    };
    let (Some(details), Some(account_sig), Some(device_sig)) = (
        signed.details.as_deref(),
        signed.account_signature.as_deref(),
        signed.device_signature.as_deref(),
    ) else {
        return AdvValidation::Invalid;
    };
    // WA Web `e.accountSignatureKey || t`: prefer the in-blob key, else the
    // caller-supplied trusted identity. An empty field counts as absent.
    let account_key: &[u8] = match signed.account_signature_key.as_deref() {
        Some(k) if !k.is_empty() => k,
        _ => match account_identity_fallback {
            Some(f) => f.as_slice(),
            None => return AdvValidation::NoAccountKey,
        },
    };
    let (Ok(account_pub), Ok(device_pub)) = (
        PublicKey::from_djb_public_key_bytes(account_key),
        PublicKey::from_djb_public_key_bytes(fetched_identity_key),
    ) else {
        return AdvValidation::Invalid;
    };

    let verified = [
        (ADV_PREFIX_ACCOUNT_SIGNATURE, ADV_PREFIX_DEVICE_SIGNATURE),
        (
            ADV_HOSTED_PREFIX_ACCOUNT_SIGNATURE,
            ADV_HOSTED_PREFIX_DEVICE_SIGNATURE,
        ),
    ]
    .into_iter()
    .any(|(account_prefix, device_prefix)| {
        let account_msg = [account_prefix, details, fetched_identity_key].concat();
        let device_msg = [device_prefix, details, fetched_identity_key, account_key].concat();
        account_pub.verify_signature(&account_msg, account_sig)
            && device_pub.verify_signature(&device_msg, device_sig)
    });

    if verified {
        AdvValidation::Valid
    } else {
        AdvValidation::Invalid
    }
}

/// Decoded fields from `ADVKeyIndexList` protobuf.
#[derive(Debug, Clone)]
pub struct DecodedKeyIndex {
    pub raw_id: u32,
    pub timestamp: u64,
    pub current_index: u32,
    pub valid_indexes: Vec<u32>,
}

/// Decode signed key index bytes from a `key-index-list` element.
///
/// The bytes are an `ADVSignedKeyIndexList` protobuf whose `details` field
/// contains a serialized `ADVKeyIndexList`. Signature verification is deferred
/// (the notification arrives over a Noise-encrypted connection, so content is
/// already authenticated).
pub fn decode_key_index_list(signed_bytes: &[u8]) -> Option<DecodedKeyIndex> {
    let signed = waproto::codec::adv_signed_key_index_list_decode(signed_bytes).ok()?;
    let details_bytes = signed.details.as_ref()?;
    let key_index = waproto::codec::adv_key_index_list_decode(details_bytes.as_slice()).ok()?;

    let raw_id = key_index.raw_id?;
    let timestamp = key_index.timestamp?;
    let current_index = key_index.current_index.unwrap_or(0);

    Some(DecodedKeyIndex {
        raw_id,
        timestamp,
        current_index,
        valid_indexes: key_index.valid_indexes,
    })
}

/// Filter a device list using `valid_indexes` and `current_index` from an
/// `ADVKeyIndexList`, matching WA Web's filtering algorithm.
///
/// Retention rules (from `AdvDeviceNotificationApi` and `AdvKeyIndexResultApi`):
/// - Primary device (id=0): **always kept**
/// - Device with `key_index ∈ valid_indexes`: kept
/// - Device with `key_index > current_index`: kept (newer than server knows)
/// - Everything else: removed
pub fn filter_devices_by_key_index(
    devices: &[DeviceInfo],
    decoded: &DecodedKeyIndex,
) -> Vec<DeviceInfo> {
    devices
        .iter()
        .filter(|device| should_retain_device(device, decoded))
        .cloned()
        .collect()
}

/// Filter a device list in place using the same ADV key-index rules as
/// [`filter_devices_by_key_index`]. This avoids allocating and copying a second
/// list when the caller already owns its device snapshot.
pub fn retain_devices_by_key_index(devices: &mut Vec<DeviceInfo>, decoded: &DecodedKeyIndex) {
    devices.retain(|device| should_retain_device(device, decoded));
}

fn should_retain_device(device: &DeviceInfo, decoded: &DecodedKeyIndex) -> bool {
    device.device_id == 0 || is_key_index_valid(device.key_index, decoded)
}

/// Check if a key_index is accepted by the decoded ADV list.
/// Used to validate a newly-notified device before adding it to the registry.
///
/// WA Web `AdvDeviceNotificationApi`: device added only if
/// `keyIndex != null && (validIndexes.has(keyIndex) || keyIndex > currentIndex)`
pub fn is_key_index_valid(key_index: Option<u32>, decoded: &DecodedKeyIndex) -> bool {
    match key_index {
        Some(ki) => decoded.valid_indexes.contains(&ki) || ki > decoded.current_index,
        None => false,
    }
}

#[cfg(test)]
#[allow(clippy::disallowed_methods)]
mod tests {
    use super::*;
    use buffa::Message;

    fn dev(id: u32, key_index: Option<u32>) -> DeviceInfo {
        DeviceInfo::new(id, key_index)
    }

    #[test]
    fn primary_device_always_kept() {
        let devices = vec![dev(0, None), dev(5, Some(3))];
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 10,
            valid_indexes: vec![], // empty — nothing valid
        };
        let result = filter_devices_by_key_index(&devices, &decoded);
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].device_id, 0);
    }

    #[test]
    fn valid_index_kept_invalid_removed() {
        let devices = vec![dev(0, None), dev(11, Some(5)), dev(12, Some(7))];
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 10,
            valid_indexes: vec![7], // only key_index=7 is valid
        };
        let result = filter_devices_by_key_index(&devices, &decoded);
        assert_eq!(result.len(), 2); // device 0 + device 12
        assert!(result.iter().any(|d| d.device_id == 0));
        assert!(result.iter().any(|d| d.device_id == 12));
        assert!(!result.iter().any(|d| d.device_id == 11));
    }

    #[test]
    fn device_newer_than_current_index_kept() {
        let devices = vec![dev(0, None), dev(15, Some(20))];
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 10,
            valid_indexes: vec![7],
        };
        let result = filter_devices_by_key_index(&devices, &decoded);
        assert_eq!(result.len(), 2); // device 0 + device 15 (key_index 20 > current 10)
    }

    #[test]
    fn device_without_key_index_removed() {
        // WA Web: h.has(null) → false, null > y → false → device removed
        let devices = vec![dev(0, None), dev(5, None)];
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 10,
            valid_indexes: vec![7],
        };
        let result = filter_devices_by_key_index(&devices, &decoded);
        assert_eq!(result.len(), 1); // only primary device kept
        assert_eq!(result[0].device_id, 0);
    }

    #[test]
    fn is_key_index_valid_in_valid_set() {
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 5,
            valid_indexes: vec![3, 7],
        };
        assert!(is_key_index_valid(Some(3), &decoded));
        assert!(is_key_index_valid(Some(7), &decoded));
    }

    #[test]
    fn is_key_index_valid_not_in_valid_set() {
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 5,
            valid_indexes: vec![3, 7],
        };
        assert!(!is_key_index_valid(Some(4), &decoded));
    }

    #[test]
    fn is_key_index_valid_newer_than_current() {
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 5,
            valid_indexes: vec![3],
        };
        assert!(is_key_index_valid(Some(10), &decoded));
    }

    #[test]
    fn is_key_index_valid_none_rejected() {
        let decoded = DecodedKeyIndex {
            raw_id: 1,
            timestamp: 100,
            current_index: 5,
            valid_indexes: vec![3, 7],
        };
        assert!(!is_key_index_valid(None, &decoded));
    }

    #[test]
    fn decode_roundtrip() {
        use buffa::Message;

        let key_index = waproto::whatsapp::ADVKeyIndexList {
            raw_id: Some(42),
            timestamp: Some(1000),
            current_index: Some(5),
            valid_indexes: vec![3, 5, 7],
            ..Default::default()
        };
        let details = key_index.encode_to_vec();

        let signed = waproto::whatsapp::ADVSignedKeyIndexList {
            details: Some(details),
            ..Default::default()
        };
        let bytes = signed.encode_to_vec();

        let decoded = decode_key_index_list(&bytes).unwrap();
        assert_eq!(decoded.raw_id, 42);
        assert_eq!(decoded.timestamp, 1000);
        assert_eq!(decoded.current_index, 5);
        assert_eq!(decoded.valid_indexes, vec![3, 5, 7]);
    }

    use crate::libsignal::protocol::KeyPair;

    /// Build a signed device-identity. When `include_account_key` is false the
    /// `account_signature_key` field is omitted (the trimmed shape the real
    /// server sends for a contact's companion device); the signatures are still
    /// made with the account key so a fallback can verify them.
    fn signed_identity_opts(
        account: &KeyPair,
        device: &KeyPair,
        details: &[u8],
        hosted: bool,
        include_account_key: bool,
    ) -> Vec<u8> {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let identity = device.public_key.public_key_bytes();
        let account_key = account.public_key.public_key_bytes();
        let (acct_prefix, dev_prefix): (&[u8], &[u8]) = if hosted {
            (
                ADV_HOSTED_PREFIX_ACCOUNT_SIGNATURE,
                ADV_HOSTED_PREFIX_DEVICE_SIGNATURE,
            )
        } else {
            (ADV_PREFIX_ACCOUNT_SIGNATURE, ADV_PREFIX_DEVICE_SIGNATURE)
        };
        let account_sig = account
            .private_key
            .calculate_signature(&[acct_prefix, details, identity].concat(), &mut rng)
            .unwrap()
            .to_vec();
        let device_sig = device
            .private_key
            .calculate_signature(
                &[dev_prefix, details, identity, account_key].concat(),
                &mut rng,
            )
            .unwrap()
            .to_vec();
        waproto::whatsapp::ADVSignedDeviceIdentity {
            details: Some(details.to_vec()),
            account_signature_key: include_account_key.then(|| account_key.to_vec()),
            account_signature: Some(account_sig),
            device_signature: Some(device_sig),
        }
        .encode_to_vec()
    }

    fn signed_identity(
        account: &KeyPair,
        device: &KeyPair,
        details: &[u8],
        hosted: bool,
    ) -> Vec<u8> {
        signed_identity_opts(account, device, details, hosted, true)
    }

    fn id32(kp: &KeyPair) -> [u8; 32] {
        kp.public_key.public_key_bytes().try_into().unwrap()
    }

    #[test]
    fn adv_chain_valid_accepted() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let bytes = signed_identity(&account, &device, b"details", false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), None),
            AdvValidation::Valid
        );
    }

    #[test]
    fn adv_chain_hosted_prefix_accepted() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let bytes = signed_identity(&account, &device, b"hosted-details", true);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), None),
            AdvValidation::Valid
        );
    }

    #[test]
    fn adv_chain_rejects_substituted_identity() {
        // A relay swaps the bundle's <identity> to its own key, but the signed
        // device-identity still binds the real one: validation must fail.
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let attacker = KeyPair::generate(&mut rng);
        let bytes = signed_identity(&account, &device, b"details", false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&attacker), None),
            AdvValidation::Invalid
        );
    }

    #[test]
    fn adv_chain_rejects_missing_device_signature() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let no_dev_sig = waproto::whatsapp::ADVSignedDeviceIdentity {
            details: Some(b"details".to_vec()),
            account_signature_key: Some(account.public_key.public_key_bytes().to_vec()),
            account_signature: Some(vec![0u8; 64]),
            device_signature: None,
        }
        .encode_to_vec();
        assert_eq!(
            validate_adv_with_identity_key(&no_dev_sig, &id32(&device), None),
            AdvValidation::Invalid
        );
    }

    #[test]
    fn adv_chain_rejects_garbage() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let device = KeyPair::generate(&mut rng);
        assert_eq!(
            validate_adv_with_identity_key(&[1, 2, 3, 4], &id32(&device), None),
            AdvValidation::Invalid
        );
    }

    // The real server omits account_signature_key for a contact's companion
    // device. With the contact's primary identity supplied as the fallback, the
    // chain verifies — this is the regression #772 broke (it rejected outright).
    #[test]
    fn adv_chain_missing_account_key_verifies_with_fallback() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let bytes = signed_identity_opts(&account, &device, b"details", false, false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), Some(&id32(&account))),
            AdvValidation::Valid
        );
    }

    // No in-blob key and no fallback: the chain is unverifiable, so we must NOT
    // reject (that would drop a legitimate device); the caller proceeds.
    #[test]
    fn adv_chain_missing_account_key_no_fallback_is_no_account_key() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let bytes = signed_identity_opts(&account, &device, b"details", false, false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), None),
            AdvValidation::NoAccountKey
        );
    }

    // A wrong fallback (relay-supplied account identity) must NOT verify: the
    // account signature was made by the real account key, so a mismatched
    // fallback yields Invalid, preserving the forgery protection.
    #[test]
    fn adv_chain_missing_account_key_wrong_fallback_is_invalid() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let attacker = KeyPair::generate(&mut rng);
        let bytes = signed_identity_opts(&account, &device, b"details", false, false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), Some(&id32(&attacker))),
            AdvValidation::Invalid
        );
    }

    // The in-blob key takes precedence over the fallback (WA Web `|| t`): a valid
    // full blob still verifies even if an unrelated fallback is passed.
    #[test]
    fn adv_chain_in_blob_key_takes_precedence_over_fallback() {
        let mut rng = rand::make_rng::<rand::rngs::StdRng>();
        let account = KeyPair::generate(&mut rng);
        let device = KeyPair::generate(&mut rng);
        let unrelated = KeyPair::generate(&mut rng);
        let bytes = signed_identity(&account, &device, b"details", false);
        assert_eq!(
            validate_adv_with_identity_key(&bytes, &id32(&device), Some(&id32(&unrelated))),
            AdvValidation::Valid
        );
    }
}