diff --git a/openssl-sys/src/object.rs b/openssl-sys/src/object.rs index fbc459bca36c6b42d474b683fa9c98a7109980ba..99abdebc992b20c5184c1bf0b01dc3a4f62b896f 100644 --- a/openssl-sys/src/object.rs +++ b/openssl-sys/src/object.rs @@ -18,4 +18,8 @@ extern "C" { -> c_int; pub fn OBJ_sn2nid(sn: *const libc::c_char) -> libc::c_int; pub fn OBJ_txt2obj(s: *const libc::c_char, no_name: libc::c_int) -> *mut ASN1_OBJECT; + #[cfg(ossl111)] + pub fn OBJ_length(obj: *const ASN1_OBJECT) -> libc::size_t; + #[cfg(ossl111)] + pub fn OBJ_get0_data(obj: *const ASN1_OBJECT) -> *const c_uchar; } diff --git a/openssl/src/asn1.rs b/openssl/src/asn1.rs index 9c066c556c05d52324c41058b70217964954b7a8..5d7fd0fd6c884ba7a132241cec64594a8fd66bc9 100644 --- a/openssl/src/asn1.rs +++ b/openssl/src/asn1.rs @@ -626,6 +626,22 @@ impl Asn1Object { Ok(Asn1Object::from_ptr(obj)) } } + + /// Return the OID as an DER encoded array of bytes. This is the ASN.1 + /// value, not including tag or length. + /// + /// This corresponds to [`OBJ_get0_data`]. + /// + /// Requires OpenSSL 1.1.1 or newer. + /// + /// [`OBJ_get0_data`]: https://www.openssl.org/docs/man1.1.0/man3/OBJ_get0_data.html + #[cfg(ossl111)] + pub fn as_slice(&self) -> &[u8] { + unsafe { + let len = ffi::OBJ_length(self.as_ptr()); + slice::from_raw_parts(ffi::OBJ_get0_data(self.as_ptr()), len) + } + } } impl Asn1ObjectRef { @@ -765,4 +781,14 @@ mod tests { .map(|object| object.to_string()) .expect_err("parsing invalid OID should fail"); } + + #[test] + #[cfg(ossl111)] + fn object_to_slice() { + let object = Asn1Object::from_str("2.16.840.1.101.3.4.2.1").unwrap(); + assert_eq!( + object.as_slice(), + &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01], + ); + } }