wechat-protocol 2.6.0

Native WeChat transport, authentication and protocol codecs
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
//! Uploads whose plaintext stays in a file and is encrypted one chunk at a time.
//!
//! CDN objects use AES-128-ECB with PKCS#7 padding, so each 16-byte ciphertext
//! block depends only on the plaintext block at the same offset. A chunk that
//! starts on a block boundary is encrypted on its own from that plaintext
//! range, and the result is byte-for-byte the slice of the whole-object
//! ciphertext. A [`PreparedFileUpload`] is therefore read from disk as it is
//! sent instead of being encrypted into memory first, and every request is the
//! same as for the equivalent in-memory [`PreparedUpload`].
use super::*;
use std::{
    io::{Seek, SeekFrom},
    sync::{Arc, Mutex},
};
use zeroize::Zeroizing;

fn ciphertext_len(plaintext: usize) -> usize {
    (plaintext / 16 + 1) * 16
}

/// One object ready to upload from a private file. The file is read in steps
/// while the object is sent; its length, MD5 and SHA-256 are checked again on
/// the way, and the request that ends the object is only made once every byte
/// read matched. Persist the key and file key before the first request, as
/// for [`PreparedUpload`].
#[derive(Clone)]
pub struct PreparedFileUpload {
    /// CDN object type to upload as.
    pub kind: MediaQuality,
    /// AES-128 key the object is encrypted with.
    pub aes_key: Vec<u8>,
    /// Client file key that correlates the upload's requests.
    pub file_key: String,
    /// Size of the plaintext in bytes.
    pub plaintext_bytes: u32,
    /// Lowercase hex MD5 of the plaintext.
    pub md5: String,
    /// Lowercase hex SHA-256 of the plaintext.
    pub sha256: String,
    /// File name sent with the upload.
    pub file_name: String,
    /// Chat the upload is for.
    pub destination: String,
    file: Arc<Mutex<std::fs::File>>,
}
impl Drop for PreparedFileUpload {
    fn drop(&mut self) {
        self.aes_key.zeroize();
        self.file_key.zeroize();
    }
}
impl fmt::Debug for PreparedFileUpload {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PreparedFileUpload")
            .field("kind", &self.kind)
            .field("plaintext_bytes", &self.plaintext_bytes)
            .field("material", &"[redacted]")
            .finish()
    }
}
impl PreparedFileUpload {
    /// Prepare an upload of `file`. `length`, `md5` and `sha256` describe the
    /// file; the caller computed them while writing it. A file of another
    /// length is refused now, and changed bytes before the final request.
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        file: std::fs::File,
        length: u64,
        md5: &str,
        sha256: &str,
        kind: MediaQuality,
        file_name: &str,
        destination: &str,
        key: &[u8],
        file_key: &str,
    ) -> Result<Self> {
        let plaintext_bytes = usize::try_from(length)?;
        check_upload_profile(plaintext_bytes, kind, file_name, destination, key, file_key)?;
        ensure!(
            md5.len() == 32
                && md5.bytes().all(|b| b.is_ascii_hexdigit())
                && sha256.len() == 64
                && sha256.bytes().all(|b| b.is_ascii_hexdigit()),
            "Invalid upload source digest"
        );
        ensure!(file.metadata()?.len() == length, "Upload source changed");
        Ok(Self {
            kind,
            aes_key: key.to_vec(),
            file_key: file_key.to_string(),
            plaintext_bytes: u32::try_from(plaintext_bytes)?,
            md5: md5.to_ascii_lowercase(),
            sha256: sha256.to_ascii_lowercase(),
            file_name: file_name.into(),
            destination: destination.into(),
            file: Arc::new(Mutex::new(file)),
        })
    }
    /// Length of the encrypted object, including its padding block.
    pub fn encrypted_len(&self) -> usize {
        ciphertext_len(self.plaintext_bytes as usize)
    }
    /// The same upload held in memory, for transports that send one object per
    /// request. The file is read whole and checked against its digests.
    pub fn to_memory(&self) -> Result<PreparedUpload> {
        let mut raw = Zeroizing::new(vec![0u8; self.plaintext_bytes as usize]);
        self.read_at(0, &mut raw)?;
        let prepared = PreparedUpload::with_material(
            &raw,
            self.kind,
            &self.file_name,
            &self.destination,
            &self.aes_key,
            &self.file_key,
        )?;
        ensure!(
            prepared.md5 == self.md5 && prepared.sha256 == self.sha256,
            "Prepared upload digest mismatch"
        );
        Ok(prepared)
    }
    fn read_at(&self, offset: u64, buffer: &mut [u8]) -> Result<()> {
        let mut file = self
            .file
            .lock()
            .map_err(|_| anyhow::anyhow!("Upload source unavailable"))?;
        file.seek(SeekFrom::Start(offset))?;
        file.read_exact(buffer)
            .map_err(|_| anyhow::anyhow!("Upload source changed"))
    }
}

/// Checks shared by every way of preparing an upload.
pub(super) fn check_upload_profile(
    length: usize,
    kind: MediaQuality,
    file_name: &str,
    destination: &str,
    key: &[u8],
    file_key: &str,
) -> Result<()> {
    ensure!(
        length > 0
            && length <= upload_plaintext_limit(kind)
            && [
                MediaQuality::File,
                MediaQuality::LargeFile,
                MediaQuality::Original,
                MediaQuality::Video
            ]
            .contains(&kind),
        "Unsupported upload profile"
    );
    ensure!(
        !file_name.is_empty()
            && file_name.len() <= 255
            && file_name != "."
            && file_name != ".."
            && !file_name.contains(['/', '\\'])
            && !file_name.chars().any(|c| c.is_control()),
        "Invalid upload filename"
    );
    ensure!(
        key.len() == 16 && file_key.len() == 32 && file_key.bytes().all(|b| b.is_ascii_hexdigit()),
        "Invalid durable upload material"
    );
    encrypt_recipient(destination)?;
    Ok(())
}

/// Either kind of prepared upload, for the shared request builders.
#[derive(Clone, Copy)]
pub(super) enum UploadRef<'a> {
    Memory(&'a PreparedUpload),
    File(&'a PreparedFileUpload),
}
impl<'a> UploadRef<'a> {
    pub(super) fn kind(self) -> MediaQuality {
        match self {
            Self::Memory(p) => p.kind,
            Self::File(p) => p.kind,
        }
    }
    pub(super) fn aes_key(self) -> &'a [u8] {
        match self {
            Self::Memory(p) => &p.aes_key,
            Self::File(p) => &p.aes_key,
        }
    }
    pub(super) fn file_key(self) -> &'a str {
        match self {
            Self::Memory(p) => &p.file_key,
            Self::File(p) => &p.file_key,
        }
    }
    pub(super) fn plaintext_bytes(self) -> u32 {
        match self {
            Self::Memory(p) => p.plaintext_bytes,
            Self::File(p) => p.plaintext_bytes,
        }
    }
    pub(super) fn md5(self) -> &'a str {
        match self {
            Self::Memory(p) => &p.md5,
            Self::File(p) => &p.md5,
        }
    }
    pub(super) fn sha256(self) -> &'a str {
        match self {
            Self::Memory(p) => &p.sha256,
            Self::File(p) => &p.sha256,
        }
    }
    pub(super) fn file_name(self) -> &'a str {
        match self {
            Self::Memory(p) => &p.file_name,
            Self::File(p) => &p.file_name,
        }
    }
    pub(super) fn destination(self) -> &'a str {
        match self {
            Self::Memory(p) => &p.destination,
            Self::File(p) => &p.destination,
        }
    }
    pub(super) fn encrypted_len(self) -> usize {
        match self {
            Self::Memory(p) => p.encrypted.len(),
            Self::File(p) => p.encrypted_len(),
        }
    }
    /// Checks made before any request. An in-memory upload's ciphertext is
    /// fully checked; a file is checked while it is sent instead.
    pub(super) fn validate(self) -> Result<()> {
        match self {
            Self::Memory(p) => p.validate(),
            Self::File(p) => {
                ensure!(
                    p.aes_key.len() == 16
                        && p.file_key.len() == 32
                        && p.file_key.bytes().all(|b| b.is_ascii_hexdigit())
                        && p.plaintext_bytes > 0
                        && p.plaintext_bytes as usize <= upload_plaintext_limit(p.kind),
                    "Invalid prepared upload"
                );
                Ok(())
            }
        }
    }
    /// The encrypted object in order, one chunk at a time.
    pub(super) fn chunks(self) -> CiphertextChunks<'a> {
        CiphertextChunks {
            upload: self,
            position: 0,
            md5: None,
            sha256: None,
        }
    }
    /// The uploaded object's receipt for this upload.
    pub(super) fn uploaded(
        self,
        file_id: String,
        width: Option<u32>,
        height: Option<u32>,
    ) -> UploadedMedia {
        UploadedMedia {
            descriptor: MediaDescriptor {
                file_id,
                aes_key: self.aes_key().to_vec(),
                quality: self.kind(),
                expected_bytes: Some(self.plaintext_bytes() as u64),
                expected_md5: Some(self.md5().into()),
                strict_size: true,
                expires_at: None,
            },
            plaintext_bytes: self.plaintext_bytes(),
            md5: self.md5().into(),
            sha256: self.sha256().into(),
            file_name: self.file_name().into(),
            width,
            height,
        }
    }
}

/// Produces an upload's ciphertext from the start, in order. For a file, the
/// plaintext is hashed as it is read, and the chunk holding the end of the
/// object is returned only when the length and both digests match.
pub(super) struct CiphertextChunks<'a> {
    upload: UploadRef<'a>,
    position: usize,
    md5: Option<Hasher>,
    sha256: Option<Hasher>,
}
impl CiphertextChunks<'_> {
    /// Ciphertext offset of the next chunk.
    pub(super) fn position(&self) -> usize {
        self.position
    }
    /// The next `size` ciphertext bytes, or fewer at the end of the object.
    /// `size` must be a multiple of the 16-byte block size.
    pub(super) fn next(&mut self, size: usize) -> Result<Zeroizing<Vec<u8>>> {
        let total = self.upload.encrypted_len();
        ensure!(
            size > 0 && size.is_multiple_of(16) && self.position < total,
            "Invalid upload range"
        );
        let end = (self.position + size).min(total);
        let prepared = match self.upload {
            UploadRef::Memory(prepared) => {
                let chunk = Zeroizing::new(prepared.encrypted[self.position..end].to_vec());
                self.position = end;
                return Ok(chunk);
            }
            UploadRef::File(prepared) => prepared,
        };
        let plaintext = prepared.plaintext_bytes as usize;
        let read = end.min(plaintext).saturating_sub(self.position);
        let mut block = Zeroizing::new(vec![0u8; end - self.position]);
        prepared.read_at(self.position as u64, &mut block[..read])?;
        let md5 = match &mut self.md5 {
            Some(hasher) => hasher,
            None => self.md5.insert(Hasher::new(MessageDigest::md5())?),
        };
        md5.update(&block[..read])?;
        let sha256 = match &mut self.sha256 {
            Some(hasher) => hasher,
            None => self.sha256.insert(Hasher::new(MessageDigest::sha256())?),
        };
        sha256.update(&block[..read])?;
        if end == total {
            // PKCS#7: the final block is completed with copies of its pad length.
            let pad = (total - plaintext) as u8;
            block[read..].fill(pad);
            let md5 = hex::encode(self.md5.take().unwrap().finish()?);
            let sha256 = hex::encode(self.sha256.take().unwrap().finish()?);
            ensure!(
                md5 == prepared.md5 && sha256 == prepared.sha256,
                "Prepared upload digest mismatch"
            );
        }
        let mut crypter = Crypter::new(
            Cipher::aes_128_ecb(),
            Mode::Encrypt,
            &prepared.aes_key,
            None,
        )?;
        crypter.pad(false);
        let mut encrypted = Zeroizing::new(vec![0u8; block.len() + 16]);
        let count = crypter.update(&block, &mut encrypted)?;
        let rest = crypter.finalize(&mut encrypted[count..])?;
        encrypted.truncate(count + rest);
        ensure!(encrypted.len() == block.len(), "Invalid upload range");
        self.position = end;
        Ok(encrypted)
    }
}

#[cfg(test)]
pub(super) mod tests {
    use super::*;
    use std::io::Write;

    fn material() -> ([u8; 16], String) {
        ([9; 16], "a".repeat(32))
    }
    fn whole(upload: UploadRef<'_>, chunk: usize) -> Vec<u8> {
        let mut chunks = upload.chunks();
        let mut out = vec![];
        while chunks.position() < upload.encrypted_len() {
            out.extend_from_slice(&chunks.next(chunk).unwrap());
        }
        out
    }
    pub(in super::super) struct FileSource {
        _directory: wechat_platform::PrivateTempDir,
        pub(in super::super) path: std::path::PathBuf,
    }
    /// A file upload of `raw` with `kind`, using the given material.
    pub(in super::super) fn file_upload_with(
        raw: &[u8],
        kind: MediaQuality,
        key: &[u8],
        file_key: &str,
    ) -> (FileSource, PreparedFileUpload) {
        let directory = wechat_platform::private_tempdir("wechat-upload-source-test-").unwrap();
        let path = directory.path().join("source");
        std::fs::File::create(&path)
            .unwrap()
            .write_all(raw)
            .unwrap();
        let prepared = PreparedFileUpload::new(
            std::fs::File::open(&path).unwrap(),
            raw.len() as u64,
            &hex::encode(md5(raw)),
            &hex::encode(sha256(raw)),
            kind,
            "a.bin",
            "wxid_peer",
            key,
            file_key,
        )
        .unwrap();
        (
            FileSource {
                _directory: directory,
                path,
            },
            prepared,
        )
    }
    fn file_upload(raw: &[u8]) -> (FileSource, PreparedFileUpload) {
        let (key, file_key) = material();
        file_upload_with(raw, MediaQuality::File, &key, &file_key)
    }

    #[test]
    fn file_chunks_match_the_in_memory_upload_at_every_boundary() {
        let (key, file_key) = material();
        for length in [
            1,
            15,
            16,
            17,
            31,
            32,
            33,
            65535,
            65536,
            65537,
            131072 - 16,
            131072,
        ] {
            let raw: Vec<u8> = (0..length).map(|i| (i * 7 % 251) as u8).collect();
            let memory = PreparedUpload::with_material(
                &raw,
                MediaQuality::File,
                "a.bin",
                "wxid_peer",
                &key,
                &file_key,
            )
            .unwrap();
            let (_file, from_file) = file_upload(&raw);
            assert_eq!(
                from_file.encrypted_len(),
                memory.encrypted.len(),
                "length {length}"
            );
            for chunk in [16, 4096, UPLOAD_CHUNK] {
                assert_eq!(whole(UploadRef::Memory(&memory), chunk), memory.encrypted);
                assert_eq!(
                    whole(UploadRef::File(&from_file), chunk),
                    memory.encrypted,
                    "file {length}/{chunk}"
                );
            }
            let copied = from_file.to_memory().unwrap();
            assert_eq!(copied.encrypted, memory.encrypted);
            assert_eq!(
                (copied.md5.clone(), copied.sha256.clone()),
                (memory.md5.clone(), memory.sha256.clone())
            );
        }
    }

    #[test]
    fn a_changed_or_truncated_file_never_yields_the_final_chunk() {
        let raw: Vec<u8> = (0..70_000).map(|i| (i % 256) as u8).collect();
        let (file, prepared) = file_upload(&raw);
        // Changed after preparation: earlier chunks are produced, the last is refused.
        let mut changed = raw.clone();
        changed[69_999] ^= 1;
        std::fs::write(&file.path, &changed).unwrap();
        let mut chunks = UploadRef::File(&prepared).chunks();
        chunks.next(UPLOAD_CHUNK).unwrap();
        let error = chunks.next(UPLOAD_CHUNK).unwrap_err();
        assert_eq!(error.to_string(), "Prepared upload digest mismatch");
        assert!(prepared.to_memory().is_err());
        // Truncated: the read itself fails.
        std::fs::write(&file.path, &raw[..66_000]).unwrap();
        let mut chunks = UploadRef::File(&prepared).chunks();
        chunks.next(UPLOAD_CHUNK).unwrap();
        let error = chunks.next(UPLOAD_CHUNK).unwrap_err();
        assert_eq!(error.to_string(), "Upload source changed");
        // A file whose length differs is refused when prepared.
        let (key, file_key) = material();
        let error = PreparedFileUpload::new(
            std::fs::File::open(&file.path).unwrap(),
            raw.len() as u64,
            &hex::encode(md5(&raw)),
            &hex::encode(sha256(&raw)),
            MediaQuality::File,
            "a.bin",
            "wxid_peer",
            &key,
            &file_key,
        )
        .err()
        .unwrap();
        assert_eq!(error.to_string(), "Upload source changed");
    }
}