Skip to content

Commit 336416c

Browse files
Merge #59
59: Add ability to read arbitrary certificate extensions r=Goirad a=jack-fortanix mbedtls ignores extensions it doesn't know about, but the entire DER buffer is available in the v3_ext field so we can parse it ourselves to f.ex read the appid out of our custom extensions. Co-authored-by: Jack Lloyd <[email protected]>
2 parents d3e229c + 998aa65 commit 336416c

File tree

4 files changed

+73
-7
lines changed

4 files changed

+73
-7
lines changed

mbedtls/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ spin = { version = "0.4.0", default-features = false, optional = true }
2525
serde = { version = "1.0.7", default-features = false }
2626
serde_derive = "1.0.7"
2727
byteorder = "1.0.0"
28-
yasna = { version = "0.2", optional = true }
28+
yasna = "0.2"
2929
block-modes = { version = "0.3", optional = true }
3030
rc2 = { version = "0.3", optional = true }
3131

@@ -66,7 +66,7 @@ zlib = ["mbedtls-sys-auto/zlib"]
6666
time = ["mbedtls-sys-auto/time"]
6767
padlock = ["mbedtls-sys-auto/padlock"]
6868
legacy_protocols = ["mbedtls-sys-auto/legacy_protocols"]
69-
pkcs12 = ["yasna"]
69+
pkcs12 = []
7070
pkcs12_rc2 = ["pkcs12", "rc2", "block-modes"]
7171

7272
[[example]]

mbedtls/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ extern crate mbedtls_sys;
2626

2727
extern crate byteorder;
2828

29+
extern crate yasna;
30+
2931
extern crate serde;
3032
#[macro_use]
3133
extern crate serde_derive;
@@ -36,9 +38,6 @@ extern crate rs_libc;
3638
#[macro_use]
3739
mod wrapper_macros;
3840

39-
#[cfg(feature = "pkcs12")]
40-
extern crate yasna;
41-
4241
#[cfg(feature="pkcs12_rc2")]
4342
extern crate rc2;
4443
#[cfg(feature="pkcs12_rc2")]

mbedtls/src/pkcs12/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use crate::alloc_prelude::*;
1919

2020
use core::result::Result as StdResult;
2121

22-
extern crate yasna;
23-
2422
#[cfg(feature = "pkcs12_rc2")]
2523
extern crate block_modes;
2624
#[cfg(feature = "pkcs12_rc2")]

mbedtls/src/x509/certificate.rs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ use crate::alloc_prelude::*;
1717
use mbedtls_sys::types::raw_types::c_char;
1818
use mbedtls_sys::*;
1919

20+
use yasna::models::ObjectIdentifier;
21+
use yasna::{BERDecodable, BERReader, ASN1Result, ASN1Error, ASN1ErrorKind};
22+
2023
use crate::pk::Pk;
2124
use crate::error::{Error, IntoResult, Result};
2225
use crate::private::UnsafeFrom;
@@ -37,6 +40,24 @@ define!(
3740
const drop: fn(&mut Self) = x509_crt_free;
3841
);
3942

43+
#[derive(Debug, Clone, Eq, PartialEq)]
44+
pub struct Extension {
45+
pub oid: ObjectIdentifier,
46+
pub critical: bool,
47+
pub value: Vec<u8>,
48+
}
49+
50+
impl BERDecodable for Extension {
51+
fn decode_ber(reader: BERReader) -> ASN1Result<Self> {
52+
reader.read_sequence(|reader| {
53+
let oid = reader.next().read_oid()?;
54+
let critical = reader.read_optional(|r| r.read_bool())?.unwrap_or(false);
55+
let value = reader.next().read_bytes()?;
56+
Ok(Extension { oid, critical, value })
57+
})
58+
}
59+
}
60+
4061
impl Certificate {
4162
pub fn from_der(der: &[u8]) -> Result<Certificate> {
4263
let mut ret = Self::init();
@@ -217,6 +238,25 @@ impl LinkedCertificate {
217238
Ok(x509_buf_to_vec(&self.inner.v3_ext))
218239
}
219240

241+
pub fn extensions(&self) -> Result<Vec<Extension>> {
242+
let mut ext = Vec::new();
243+
244+
yasna::parse_der(&self.extensions_raw()?, |r| {
245+
r.read_sequence_of(|r| {
246+
if let Ok(data) = r.read_der() {
247+
let e: Extension = yasna::decode_der(&data)?;
248+
ext.push(e);
249+
return Ok(());
250+
} else {
251+
return Err(ASN1Error::new(ASN1ErrorKind::Eof));
252+
}
253+
})?;
254+
return Ok(());
255+
}).map_err(|_| Error::X509InvalidExtensions)?;
256+
257+
Ok(ext)
258+
}
259+
220260
pub fn signature(&self) -> Result<Vec<u8>> {
221261
Ok(x509_buf_to_vec(&self.inner.sig))
222262
}
@@ -804,6 +844,35 @@ cYp0bH/RcPTC0Z+ZaqSWMtfxRrk63MJQF9EXpDCdvQRcTMD9D85DJrMKn8aumq0M
804844
use crate::x509::Time;
805845
assert_eq!(cert.not_before().unwrap(), Time::new(2019,1,8,0,18,35).unwrap());
806846
assert_eq!(cert.not_after().unwrap(), Time::new(2029,1,5,0,18,35).unwrap());
847+
848+
let ext = cert.extensions().unwrap();
849+
assert_eq!(ext.len(), 5);
850+
851+
assert_eq!(ext[0], Extension {
852+
oid: ObjectIdentifier::from_slice(&[2,5,29,14]),
853+
critical: false,
854+
value: hex::decode("04186839FAD57E6544121CC6BC421953CC9620655C57CFAC0602").unwrap(),
855+
});
856+
assert_eq!(ext[1], Extension {
857+
oid: ObjectIdentifier::from_slice(&[2,5,29,17]),
858+
critical: false,
859+
value: hex::decode("302981117465737440666f7274616e69782e636f6d82146578616d706c652e666f7274616e69782e636f6d").unwrap()
860+
});
861+
assert_eq!(ext[2], Extension {
862+
oid: ObjectIdentifier::from_slice(&[2,5,29,19]),
863+
critical: true,
864+
value: hex::decode("3000").unwrap()
865+
});
866+
assert_eq!(ext[3], Extension {
867+
oid: ObjectIdentifier::from_slice(&[2,5,29,35]),
868+
critical: false,
869+
value: hex::decode("301a801879076BCC8DA0077E4116F84B8E4C9C5C6AF7EC4FA000D987").unwrap()
870+
});
871+
assert_eq!(ext[4], Extension {
872+
oid: ObjectIdentifier::from_slice(&[2,5,29,37]),
873+
critical: false,
874+
value: hex::decode("300a06082b06010505070302").unwrap(),
875+
});
807876
}
808877

809878
#[test]

0 commit comments

Comments
 (0)