aria2-core 0.3.10

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
use super::pkcs12::{
    decrypt_pkcs12_aes_gcm, is_pkcs12_private_key_bag, passwordless_pkcs12_bags,
    pkcs5_algorithm_der, pkcs12_oid,
};
use super::test_support::{
    TEST_CHAIN_PEM, TEST_PRIVATE_KEY_PEM, TEST_ROOT_PEM, pem_certificates, pem_private_key,
    start_https_fixture, test_client_builder,
};
use super::{ClientTlsConfig, apply};

fn passwordless_pkcs12_identity(password: &p12_q3::BmpString) -> Vec<u8> {
    let certificates = pem_certificates(TEST_CHAIN_PEM);
    let private_key = pem_private_key(TEST_PRIVATE_KEY_PEM);
    let ca_certificates = certificates
        .iter()
        .skip(1)
        .map(|certificate| certificate.as_ref())
        .collect::<Vec<_>>();

    p12_q3::PFX::new_with_cas(
        certificates[0].as_ref(),
        private_key.secret_der(),
        &ca_certificates,
        password,
        "aria2-rust-test",
    )
    .expect("passwordless PKCS#12 fixture should be generated")
    .to_der()
}

fn passwordless_pkcs12_with_key_bag(key_bag: p12_q3::SafeBagKind) -> Vec<u8> {
    passwordless_pkcs12_with_key_bag_and_extras(key_bag, &[])
}

fn passwordless_pkcs12_with_key_bag_and_extras(
    key_bag: p12_q3::SafeBagKind,
    extra_bags: &[p12_q3::SafeBagKind],
) -> Vec<u8> {
    let certificates = pem_certificates(TEST_CHAIN_PEM);
    let password = p12_q3::BmpString::with_two_trailing_zeros("");
    let key_bag = p12_q3::SafeBag {
        bag: key_bag,
        attributes: vec![],
    };
    let cert_bag = p12_q3::SafeBag {
        bag: p12_q3::SafeBagKind::CertBag(p12_q3::CertBag::X509(certificates[0].as_ref().to_vec())),
        attributes: vec![],
    };
    let encrypted_certificates =
        p12_q3::EncryptedData::from_safe_bags(&[cert_bag], password.as_ref())
            .expect("certificate safe contents should encrypt");
    let authenticated_safe = yasna::construct_der(|writer| {
        writer.write_sequence_of(|writer| {
            p12_q3::ContentInfo::EncryptedData(encrypted_certificates).write(writer.next());
            p12_q3::ContentInfo::Data(yasna::construct_der(|writer| {
                writer.write_sequence_of(|writer| {
                    write_test_key_bag(writer.next(), &key_bag.bag);
                    for extra_bag in extra_bags {
                        write_test_key_bag(writer.next(), extra_bag);
                    }
                });
            }))
            .write(writer.next());
        });
    });

    p12_q3::PFX {
        version: 3,
        auth_safe: p12_q3::ContentInfo::Data(authenticated_safe),
        mac_data: None,
    }
    .to_der()
}

fn write_test_key_bag(writer: yasna::DERWriter, bag: &p12_q3::SafeBagKind) {
    writer.write_sequence(|writer| {
        writer.next().write_oid(&bag.oid());
        writer
            .next()
            .write_tagged(yasna::Tag::context(0), |writer| match bag {
                p12_q3::SafeBagKind::Pkcs8ShroudedKeyBag(key_bag) => {
                    let algorithm_der = pkcs5_algorithm_der(&key_bag.encryption_algorithm)
                        .unwrap_or_else(|| {
                            yasna::construct_der(|writer| {
                                key_bag.encryption_algorithm.write(writer)
                            })
                        });
                    writer.write_sequence(|writer| {
                        writer.next().write_der(&algorithm_der);
                        writer.next().write_bytes(&key_bag.encrypted_data);
                    });
                }
                _ => bag.write(writer),
            });
    });
}

fn aes_cbc_pkcs12_key_bag(algorithm_oid: &[u64]) -> p12_q3::SafeBagKind {
    aes_cbc_pkcs12_key_bag_with_prf(algorithm_oid, p12_q3::AlgorithmIdentifier::HmacWithSha256)
}

fn aes_cbc_pkcs12_key_bag_with_prf(
    algorithm_oid: &[u64],
    prf: p12_q3::AlgorithmIdentifier,
) -> p12_q3::SafeBagKind {
    let password = p12_q3::BmpString::with_two_trailing_zeros("");
    let iv = [7u8; 16];
    let algorithm = p12_q3::AlgorithmIdentifier::Pbes2(p12_q3::Pkcs12Pbes2Params {
        key_derivation_function: Box::new(p12_q3::AlgorithmIdentifier::Pbkdf2(
            p12_q3::Pbkdf2Params {
                salt: p12_q3::Pbkdf2Salt::Specified(b"rust-owned-salt".to_vec()),
                iteration_count: 1000,
                key_length: None,
                prf: Box::new(prf),
            },
        )),
        encryption_scheme: Box::new(p12_q3::AlgorithmIdentifier::OtherAlg(
            p12_q3::OtherAlgorithmIdentifier {
                algorithm_type: pkcs12_oid(algorithm_oid),
                params: Some(yasna::construct_der(|writer| writer.write_bytes(&iv))),
            },
        )),
    });
    let algorithm_der =
        pkcs5_algorithm_der(&algorithm).expect("test AES-CBC PBES2 algorithm should be supported");
    let scheme = pkcs5::EncryptionScheme::try_from(algorithm_der.as_slice())
        .expect("test AES-CBC PBES2 algorithm should parse");
    let private_key = pem_private_key(TEST_PRIVATE_KEY_PEM);
    let encrypted_data = scheme
        .encrypt(password.as_ref(), private_key.secret_der())
        .expect("test private key should encrypt");

    p12_q3::SafeBagKind::Pkcs8ShroudedKeyBag(p12_q3::EncryptedPrivateKeyInfo {
        encryption_algorithm: algorithm,
        encrypted_data,
    })
}

fn aes_gcm_pkcs12_key_bag(algorithm_oid: &[u64], tag_len: usize) -> p12_q3::SafeBagKind {
    use aes_gcm::aead::consts::U12;
    use aes_gcm::aead::{AeadInPlace, KeyInit};

    let password = p12_q3::BmpString::with_two_trailing_zeros("");
    let salt = b"rust-owned-gcm-salt";
    let nonce = [9u8; 12];
    let key_len = if algorithm_oid.last() == Some(&6) {
        16
    } else {
        32
    };
    let mut key = vec![0u8; key_len];
    pbkdf2::pbkdf2_hmac::<sha2::Sha256>(password.as_ref(), salt, 1000, &mut key);
    let private_key = pem_private_key(TEST_PRIVATE_KEY_PEM);
    let mut encrypted_data = private_key.secret_der().to_vec();
    let tag: Vec<u8> = if key_len == 16 && tag_len == 12 {
        let cipher = aes_gcm::AesGcm::<aes_gcm::aes::Aes128, U12, U12>::new_from_slice(&key)
            .expect("AES-128-GCM test key should have the right length");
        cipher
            .encrypt_in_place_detached(aes_gcm::Nonce::from_slice(&nonce), &[], &mut encrypted_data)
            .expect("AES-128-GCM test encryption should succeed")
            .to_vec()
    } else if key_len == 16 {
        let cipher = aes_gcm::Aes128Gcm::new_from_slice(&key)
            .expect("AES-128-GCM test key should have the right length");
        cipher
            .encrypt_in_place_detached(aes_gcm::Nonce::from_slice(&nonce), &[], &mut encrypted_data)
            .expect("AES-128-GCM test encryption should succeed")
            .to_vec()
    } else if tag_len == 12 {
        let cipher = aes_gcm::AesGcm::<aes_gcm::aes::Aes256, U12, U12>::new_from_slice(&key)
            .expect("AES-256-GCM test key should have the right length");
        cipher
            .encrypt_in_place_detached(aes_gcm::Nonce::from_slice(&nonce), &[], &mut encrypted_data)
            .expect("AES-256-GCM test encryption should succeed")
            .to_vec()
    } else {
        let cipher = aes_gcm::Aes256Gcm::new_from_slice(&key)
            .expect("AES-256-GCM test key should have the right length");
        cipher
            .encrypt_in_place_detached(aes_gcm::Nonce::from_slice(&nonce), &[], &mut encrypted_data)
            .expect("AES-256-GCM test encryption should succeed")
            .to_vec()
    };
    encrypted_data.extend_from_slice(&tag);

    let algorithm = p12_q3::AlgorithmIdentifier::Pbes2(p12_q3::Pkcs12Pbes2Params {
        key_derivation_function: Box::new(p12_q3::AlgorithmIdentifier::Pbkdf2(
            p12_q3::Pbkdf2Params {
                salt: p12_q3::Pbkdf2Salt::Specified(salt.to_vec()),
                iteration_count: 1000,
                key_length: None,
                prf: Box::new(p12_q3::AlgorithmIdentifier::HmacWithSha256),
            },
        )),
        encryption_scheme: Box::new(p12_q3::AlgorithmIdentifier::OtherAlg(
            p12_q3::OtherAlgorithmIdentifier {
                algorithm_type: pkcs12_oid(algorithm_oid),
                params: Some(yasna::construct_der(|writer| {
                    writer.write_sequence(|writer| {
                        writer.next().write_bytes(&nonce);
                        if tag_len != 12 {
                            writer.next().write_u64(tag_len as u64);
                        }
                    });
                })),
            },
        )),
    });

    p12_q3::SafeBagKind::Pkcs8ShroudedKeyBag(p12_q3::EncryptedPrivateKeyInfo {
        encryption_algorithm: algorithm,
        encrypted_data,
    })
}

#[test]
fn accepts_alternative_pbkdf2_prfs_for_pkcs12_private_keys() {
    crate::http::client_pool::ensure_rustls_provider();
    for (prf_oid, prf_name) in [
        ([1, 2, 840, 113549, 2, 8], "SHA-224"),
        ([1, 2, 840, 113549, 2, 10], "SHA-384"),
        ([1, 2, 840, 113549, 2, 11], "SHA-512"),
    ] {
        let prf = p12_q3::AlgorithmIdentifier::OtherAlg(p12_q3::OtherAlgorithmIdentifier {
            algorithm_type: pkcs12_oid(&prf_oid),
            params: Some(yasna::construct_der(|writer| writer.write_null())),
        });
        let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
        let certificate_path = directory.path().join("client.p12");
        let archive = passwordless_pkcs12_with_key_bag(aes_cbc_pkcs12_key_bag_with_prf(
            &[2, 16, 840, 1, 101, 3, 4, 1, 2],
            prf,
        ));
        std::fs::write(&certificate_path, archive).expect("write alternative-PRF PKCS#12 fixture");

        let builder = apply(
            reqwest::Client::builder(),
            &ClientTlsConfig {
                certificate: Some(certificate_path.to_string_lossy().into_owned()),
                ..ClientTlsConfig::default()
            },
        )
        .unwrap_or_else(|error| {
            panic!("PKCS#12 identity with PBKDF2 {prf_name} should configure: {error}")
        });
        builder
            .build()
            .unwrap_or_else(|error| panic!("PBKDF2 {prf_name} identity should build: {error}"));
    }
}

#[test]
fn accepts_aes128_and_aes192_cbc_pkcs12_private_keys() {
    crate::http::client_pool::ensure_rustls_provider();
    for algorithm_oid in [
        [2, 16, 840, 1, 101, 3, 4, 1, 2],
        [2, 16, 840, 1, 101, 3, 4, 1, 22],
    ] {
        let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
        let certificate_path = directory.path().join("client.p12");
        let archive = passwordless_pkcs12_with_key_bag(aes_cbc_pkcs12_key_bag(&algorithm_oid));
        std::fs::write(&certificate_path, archive).expect("write AES-CBC PKCS#12 fixture");
        let archive = std::fs::read(&certificate_path).expect("read AES-CBC PKCS#12 fixture");
        let pfx = p12_q3::PFX::parse(&archive).expect("AES-CBC PKCS#12 fixture should parse");
        let password = p12_q3::BmpString::with_two_trailing_zeros("");
        let bags = pfx
            .bags(&password)
            .expect("AES-CBC PKCS#12 bags should parse");
        let key_bag = bags
            .iter()
            .find_map(|bag| match &bag.bag {
                p12_q3::SafeBagKind::Pkcs8ShroudedKeyBag(key_bag) => Some(key_bag),
                _ => None,
            })
            .expect("AES-CBC PKCS#12 fixture should contain a key");
        let algorithm_der = pkcs5_algorithm_der(&key_bag.encryption_algorithm)
            .expect("AES-CBC PBES2 algorithm should convert to PKCS#5 DER");
        let scheme = pkcs5::EncryptionScheme::try_from(algorithm_der.as_slice())
            .expect("AES-CBC PBES2 algorithm should parse as PKCS#5");
        assert!(
            scheme
                .decrypt(password.as_ref(), &key_bag.encrypted_data)
                .is_ok()
        );

        let builder = apply(
            reqwest::Client::builder(),
            &ClientTlsConfig {
                certificate: Some(certificate_path.to_string_lossy().into_owned()),
                ..ClientTlsConfig::default()
            },
        )
        .expect("AES-128/192-CBC PKCS#12 identity should configure the client");
        builder
            .build()
            .expect("AES-128/192-CBC PKCS#12 client identity should build");
    }
}

#[test]
fn accepts_aes128_and_aes256_gcm_pkcs12_private_keys() {
    crate::http::client_pool::ensure_rustls_provider();
    for algorithm_oid in [
        [2, 16, 840, 1, 101, 3, 4, 1, 6],
        [2, 16, 840, 1, 101, 3, 4, 1, 46],
    ] {
        for tag_len in [12, 16] {
            let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
            let certificate_path = directory.path().join("client-gcm.p12");
            let archive =
                passwordless_pkcs12_with_key_bag(aes_gcm_pkcs12_key_bag(&algorithm_oid, tag_len));
            std::fs::write(&certificate_path, archive).expect("write AES-GCM PKCS#12 fixture");
            let archive = std::fs::read(&certificate_path).expect("read AES-GCM PKCS#12 fixture");
            let pfx = p12_q3::PFX::parse(&archive).expect("AES-GCM PKCS#12 fixture should parse");
            let password = p12_q3::BmpString::with_two_trailing_zeros("");
            let bags = passwordless_pkcs12_bags(&pfx, &password)
                .expect("AES-GCM PKCS#12 bags should parse");
            let key_bag = bags
                .iter()
                .find(|bag| is_pkcs12_private_key_bag(bag))
                .expect("AES-GCM PKCS#12 fixture should contain a key");
            let p12_q3::SafeBagKind::Pkcs8ShroudedKeyBag(key_bag) = &key_bag.bag else {
                panic!("expected shrouded key bag");
            };
            assert!(
                decrypt_pkcs12_aes_gcm(
                    &key_bag.encryption_algorithm,
                    &key_bag.encrypted_data,
                    &password,
                )
                .is_some(),
                "AES-GCM private key should decrypt"
            );

            apply(
                reqwest::Client::builder(),
                &ClientTlsConfig {
                    certificate: Some(certificate_path.to_string_lossy().into_owned()),
                    ..ClientTlsConfig::default()
                },
            )
            .expect("AES-GCM PKCS#12 identity should configure the client")
            .build()
            .expect("AES-GCM PKCS#12 client identity should build");
        }
    }
}

#[test]
fn accepts_a_plaintext_pkcs12_key_bag() {
    crate::http::client_pool::ensure_rustls_provider();
    let private_key = pem_private_key(TEST_PRIVATE_KEY_PEM);
    let key_bag = p12_q3::SafeBagKind::OtherBagKind(p12_q3::OtherBag {
        bag_id: pkcs12_oid(&[1, 2, 840, 113549, 1, 12, 10, 1, 1]),
        bag_value: private_key.secret_der().to_vec(),
    });
    let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
    let certificate_path = directory.path().join("client.p12");
    std::fs::write(&certificate_path, passwordless_pkcs12_with_key_bag(key_bag))
        .expect("write plaintext keyBag PKCS#12 fixture");

    let builder = apply(
        reqwest::Client::builder(),
        &ClientTlsConfig {
            certificate: Some(certificate_path.to_string_lossy().into_owned()),
            ..ClientTlsConfig::default()
        },
    )
    .expect("plaintext keyBag PKCS#12 identity should configure the client");
    builder
        .build()
        .expect("plaintext keyBag PKCS#12 client identity should build");
}

#[test]
fn ignores_unknown_pkcs12_bags_when_certificate_and_key_are_present() {
    crate::http::client_pool::ensure_rustls_provider();
    let unknown_bag = p12_q3::SafeBagKind::OtherBagKind(p12_q3::OtherBag {
        bag_id: pkcs12_oid(&[1, 2, 840, 113549, 1, 12, 10, 1, 5]),
        bag_value: yasna::construct_der(|writer| writer.write_bytes(b"ignored")),
    });
    let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
    let certificate_path = directory.path().join("client-with-unknown-bag.p12");
    let archive = passwordless_pkcs12_with_key_bag_and_extras(
        aes_cbc_pkcs12_key_bag(&[2, 16, 840, 1, 101, 3, 4, 1, 2]),
        &[unknown_bag],
    );
    std::fs::write(&certificate_path, archive).expect("write unknown-bag PKCS#12 fixture");

    apply(
        reqwest::Client::builder(),
        &ClientTlsConfig {
            certificate: Some(certificate_path.to_string_lossy().into_owned()),
            ..ClientTlsConfig::default()
        },
    )
    .expect("unknown PKCS#12 bags should be ignored when identity is usable")
    .build()
    .expect("PKCS#12 identity with an unknown bag should build");
}

#[tokio::test]
async fn passwordless_pkcs12_identity_completes_live_mutual_tls() {
    let fixture = start_https_fixture(true).await;
    let directory = tempfile::tempdir().expect("create temporary identity directory");
    let ca_path = directory.path().join("root.pem");
    let certificate_path = directory.path().join("client.p12");
    let password = p12_q3::BmpString::with_two_trailing_zeros("");
    std::fs::write(&ca_path, TEST_ROOT_PEM).expect("write test root certificate");
    std::fs::write(&certificate_path, passwordless_pkcs12_identity(&password))
        .expect("write passwordless PKCS#12 fixture");

    let config = ClientTlsConfig {
        ca_certificate: Some(ca_path.to_string_lossy().into_owned()),
        certificate: Some(certificate_path.to_string_lossy().into_owned()),
        ..ClientTlsConfig::default()
    };
    let client = apply(test_client_builder(fixture.address), &config)
        .expect("passwordless PKCS#12 identity should configure the client")
        .build()
        .expect("passwordless PKCS#12 client should build");

    let response = client
        .get(fixture.url())
        .send()
        .await
        .expect("passwordless PKCS#12 identity should complete mutual TLS");
    assert_eq!(response.status(), reqwest::StatusCode::OK);
    assert_eq!(
        response.bytes().await.expect("read HTTPS response"),
        bytes::Bytes::from_static(b"tls-ok")
    );
}

#[test]
fn accepts_empty_password_without_bmp_terminator() {
    crate::http::client_pool::ensure_rustls_provider();
    let directory = tempfile::tempdir().expect("create temporary PKCS#12 directory");
    let certificate_path = directory.path().join("client.p12");
    let password = p12_q3::BmpString::empty_without_trailing_zeros();
    std::fs::write(&certificate_path, passwordless_pkcs12_identity(&password))
        .expect("write passwordless PKCS#12 fixture");

    let builder = apply(
        reqwest::Client::builder(),
        &ClientTlsConfig {
            certificate: Some(certificate_path.to_string_lossy().into_owned()),
            ..ClientTlsConfig::default()
        },
    )
    .expect("empty password without a BMP terminator should be accepted");
    builder
        .build()
        .expect("passwordless PKCS#12 client should build");
}