Skip to content

Commit cbb542d

Browse files
committed
Fix verify_chain test
1 parent 5719241 commit cbb542d

File tree

2 files changed

+22
-45
lines changed

2 files changed

+22
-45
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mbedtls/src/x509/certificate.rs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,58 +1012,35 @@ cYp0bH/RcPTC0Z+ZaqSWMtfxRrk63MJQF9EXpDCdvQRcTMD9D85DJrMKn8aumq0M
10121012
let c_int2 = Certificate::from_pem(C_INT2.as_bytes()).unwrap();
10131013
let mut c_root = Certificate::from_pem_multiple(C_ROOT.as_bytes()).unwrap();
10141014

1015-
{
1016-
let mut chain = MbedtlsList::<Certificate>::new();
1017-
chain.push(c_leaf.clone());
1018-
chain.push(c_int1.clone());
1019-
1020-
let err = Certificate::verify(&chain, &mut c_root, None, None).unwrap_err();
1021-
assert_eq!(err, Error::X509CertVerifyFailed);
1022-
1023-
// try again after fixing the chain
1024-
chain.push(c_int2.clone());
1025-
1026-
1027-
let mut err_str = String::new();
1028-
1029-
let verify_callback = |_crt: &Certificate, _depth: i32, verify_flags: &mut VerifyError| {
1030-
verify_flags.remove(VerifyError::CERT_EXPIRED);
1031-
Ok(())
1032-
};
1015+
// Certificate C_INT2 is missing at the beginning so the verification should fail at first
1016+
let mut chain = MbedtlsList::<Certificate>::new();
1017+
chain.push(c_leaf.clone());
1018+
chain.push(c_int1.clone());
10331019

1034-
Certificate::verify(&chain, &mut c_root, None, None).unwrap();
1035-
let res = Certificate::verify_with_callback(&chain, &mut c_root, None, Some(&mut err_str), verify_callback);
1020+
// The certificates used for this test are expired so we remove the CERT_EXPIRED flag with the callback
1021+
let verify_callback = |_crt: &Certificate, _depth: i32, verify_flags: &mut VerifyError| {
1022+
verify_flags.remove(VerifyError::CERT_EXPIRED);
1023+
Ok(())
1024+
};
10361025

1037-
match res {
1038-
Ok(()) => (),
1039-
Err(e) => assert!(false, "Failed to verify, error: {}, err_str: {}", e, err_str),
1040-
};
1026+
let res = Certificate::verify_with_callback(&chain, &mut c_root, None, None, verify_callback);
1027+
match res {
1028+
Ok(_) => panic!("Certificate chain verification should have failed, but it succeeded"),
1029+
Err(err) => assert_eq!(err, Error::X509CertVerifyFailed),
10411030
}
10421031

1043-
{
1044-
let mut chain = MbedtlsList::<Certificate>::new();
1045-
chain.push(c_leaf.clone());
1046-
chain.push(c_int1.clone());
1047-
chain.push(c_int2.clone());
1048-
1049-
Certificate::verify(&chain, &mut c_root, None, None).unwrap();
1032+
// try again after fixing the chain
1033+
chain.push(c_int2.clone());
10501034

1051-
let verify_callback = |_crt: &Certificate, _depth: i32, verify_flags: &mut VerifyError| {
1052-
verify_flags.remove(VerifyError::CERT_EXPIRED);
1053-
Ok(())
1054-
};
1035+
let mut err_str = String::new();
10551036

1056-
let mut err_str = String::new();
1057-
let res = Certificate::verify_with_callback(&chain, &mut c_root, None, Some(&mut err_str), verify_callback);
1037+
let res = Certificate::verify_with_callback(&chain, &mut c_root, None, Some(&mut err_str), verify_callback);
10581038

1059-
match res {
1060-
Ok(()) => (),
1061-
Err(e) => assert!(false, "Failed to verify, error: {}, err_str: {}", e, err_str),
1062-
};
1063-
}
1039+
match res {
1040+
Ok(()) => (),
1041+
Err(e) => panic!("Failed to verify, error: {}, err_str: {}", e, err_str),
1042+
};
10641043
}
1065-
1066-
10671044

10681045
#[test]
10691046
fn clone_test() {

0 commit comments

Comments
 (0)