Skip to content

Commit 22b3281

Browse files
bors[bot]Taowyoo
andauthored
Merge #310
310: [back-port][v0.8] fix: return error when verify empty cert chain r=xinyufort a=Taowyoo back-port #308 to 0.8.X Co-authored-by: Yuxiang Cao <[email protected]>
2 parents 551110f + 8659874 commit 22b3281

File tree

6 files changed

+84
-15
lines changed

6 files changed

+84
-15
lines changed

.travis.yml

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ branches:
66
- trying
77
# Not really necessary, just to get a green badge on “master”
88
- master
9+
- v0.8
910
language: rust
1011
os: linux
1112
dist: focal
@@ -18,24 +19,34 @@ addons:
1819
- clang-11
1920
- cmake
2021
- qemu-user
21-
rust:
22-
- stable
22+
before_script:
23+
- printenv
24+
- whereis clang && clang --version
25+
# remove clang-16 path from PATH
26+
- export PATH=$(echo $PATH | sed -e 's|:/usr/local/clang-16.0.0/bin||')
27+
# setup clang-11 as default clang
28+
- sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-11 100
29+
- whereis clang && clang --version
30+
2331
env:
24-
jobs:
25-
# Matrix build of 3 targets against Rust stable
26-
- TARGET=x86_64-unknown-linux-gnu ZLIB_INSTALLED=true AES_NI_SUPPORT=true
27-
- TARGET=aarch64-unknown-linux-musl
28-
- TARGET=x86_64-fortanix-unknown-sgx
2932
global:
3033
- RUST_BACKTRACE=1
3134
# Pinned to this particular nightly version because of core_io. This can be
3235
# re-pinned whenever core_io is updated to the latest nightly.
3336
- CORE_IO_NIGHTLY=nightly-2021-03-25
3437
jobs:
3538
include:
36-
# Test additional Rust toolchains on x86_64
37-
- rust: beta
38-
- rust: nightly
39-
- rust: nightly-2021-03-25
39+
- env: TARGET=x86_64-unknown-linux-gnu ZLIB_INSTALLED=true AES_NI_SUPPORT=true
40+
rust: nightly-2021-03-25
41+
- env: TARGET=x86_64-fortanix-unknown-sgx
42+
rust: stable
43+
- env: TARGET=aarch64-unknown-linux-musl
44+
rust: stable
45+
- env: TARGET=x86_64-unknown-linux-gnu ZLIB_INSTALLED=true AES_NI_SUPPORT=true
46+
rust: nightly
47+
- env: TARGET=x86_64-unknown-linux-gnu ZLIB_INSTALLED=true AES_NI_SUPPORT=true
48+
rust: beta
49+
- env: TARGET=x86_64-unknown-linux-gnu ZLIB_INSTALLED=true AES_NI_SUPPORT=true
50+
rust: stable
4051
script:
4152
- ./ct.sh

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/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "mbedtls"
3-
version = "0.8.3"
3+
version = "0.8.4"
44
authors = ["Jethro Beekman <[email protected]>"]
55
build = "build.rs"
66
edition = "2018"

mbedtls/src/ssl/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@ impl Config {
252252
}
253253

254254
pub fn push_cert(&mut self, own_cert: Arc<MbedtlsList<Certificate>>, own_pk: Arc<Pk>) -> Result<()> {
255+
if own_cert.is_empty() {
256+
return Err(Error::SslBadInputData);
257+
}
255258
// Need to ensure own_cert/pk_key outlive the config.
256259
self.own_cert.push(own_cert.clone());
257260
self.own_pk.push(own_pk.clone());

mbedtls/src/ssl/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<'ctx> HandshakeContext<'ctx> {
350350
key: Arc<Pk>,
351351
) -> Result<()> {
352352
// mbedtls_ssl_set_hs_own_cert does not check for NULL handshake.
353-
if self.context.inner.handshake as *const _ == ::core::ptr::null() {
353+
if self.context.inner.handshake as *const _ == ::core::ptr::null() || chain.is_empty() {
354354
return Err(Error::SslBadInputData);
355355
}
356356

mbedtls/src/x509/certificate.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,9 @@ impl Certificate {
228228
where
229229
F: VerifyCallback + 'static,
230230
{
231+
if chain.is_empty() {
232+
return Err(Error::X509BadInputData);
233+
}
231234
let (f_vrfy, p_vrfy): (Option<unsafe extern "C" fn(_, _, _, _) -> _>, _) = if let Some(cb) = cb.as_ref() {
232235
(Some(x509::verify_callback::<F>),
233236
cb as *const _ as *mut c_void)
@@ -1034,7 +1037,59 @@ cYp0bH/RcPTC0Z+ZaqSWMtfxRrk63MJQF9EXpDCdvQRcTMD9D85DJrMKn8aumq0M
10341037
Err(e) => panic!("Failed to verify, error: {}, err_str: {}", e, err_str),
10351038
};
10361039
}
1037-
1040+
1041+
#[test]
1042+
fn empty_cert_chain_test() {
1043+
const C_LEAF: &'static str = concat!(include_str!("../../tests/data/chain-leaf.crt"),"\0");
1044+
const C_INT1: &'static str = concat!(include_str!("../../tests/data/chain-int1.crt"),"\0");
1045+
const C_INT2: &'static str = concat!(include_str!("../../tests/data/chain-int2.crt"),"\0");
1046+
const C_ROOT: &'static str = concat!(include_str!("../../tests/data/chain-root.crt"),"\0");
1047+
1048+
let c_leaf = Certificate::from_pem(C_LEAF.as_bytes()).unwrap();
1049+
let c_int1 = Certificate::from_pem(C_INT1.as_bytes()).unwrap();
1050+
let c_int2 = Certificate::from_pem(C_INT2.as_bytes()).unwrap();
1051+
let c_root = Certificate::from_pem_multiple(C_ROOT.as_bytes()).unwrap();
1052+
1053+
// Certificate C_INT2 is missing at the beginning so the verification should fail at first
1054+
let mut chain = MbedtlsList::<Certificate>::new();
1055+
chain.push(c_leaf.clone());
1056+
chain.push(c_int1.clone());
1057+
chain.push(c_int2.clone());
1058+
1059+
// The certificates used for this test are expired so we remove the CERT_EXPIRED flag with the callback
1060+
let verify_callback = |_crt: &Certificate, _depth: i32, verify_flags: &mut VerifyError| {
1061+
verify_flags.remove(VerifyError::CERT_EXPIRED);
1062+
Ok(())
1063+
};
1064+
1065+
let mut err_str = String::new();
1066+
1067+
let res = Certificate::verify_with_callback(&chain, &c_root, Some(&mut err_str), verify_callback);
1068+
1069+
match res {
1070+
Ok(()) => (),
1071+
Err(e) => panic!("Failed to verify, error: {}, err_str: {}", e, err_str),
1072+
};
1073+
1074+
let empty_certs = MbedtlsList::<Certificate>::new();
1075+
assert_eq!(
1076+
Certificate::verify(&empty_certs, &empty_certs, None).unwrap_err(),
1077+
Error::X509BadInputData
1078+
);
1079+
assert_eq!(
1080+
Certificate::verify_with_callback(&empty_certs, &empty_certs, Some(&mut err_str), verify_callback).unwrap_err(),
1081+
Error::X509BadInputData
1082+
);
1083+
assert_eq!(
1084+
Certificate::verify_with_callback(&chain, &empty_certs, Some(&mut err_str), verify_callback).unwrap_err(),
1085+
Error::X509CertVerifyFailed
1086+
);
1087+
assert_eq!(
1088+
Certificate::verify_with_callback(&empty_certs, &c_root, Some(&mut err_str), verify_callback).unwrap_err(),
1089+
Error::X509BadInputData
1090+
);
1091+
}
1092+
10381093
#[test]
10391094
fn clone_test() {
10401095
let cert_chain = Certificate::from_pem(TEST_CERT_PEM.as_bytes()).unwrap();

0 commit comments

Comments
 (0)