Skip to content

Commit 21f9b42

Browse files
landairemvertescher
authored andcommitted
Add support for reading extended key usage
- Add support for getting authority key id (sfackler#373) - Add new test for reading X.509 extensions from a well-known cert - Fix documentation for X509::authority_keyid - Add support for key usage and subject keyid extensions - Attempt to fix build errors on older versions of OpenSSL - Rewrite X509::key_usage() to hopefully work on older versions of OpenSSL/LibreSSL - Fix copy/paste error resulting in build failure on older OpenSSL/LibreSSL versions - Import std::cmp and use mem::size_of instead of FQDN - Add methods to X509 for enumerating generic extensions - Fix issues raised during PR review
1 parent 79d6d1f commit 21f9b42

File tree

6 files changed

+455
-3
lines changed

6 files changed

+455
-3
lines changed

openssl-sys/src/x509.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,10 @@ extern "C" {
321321
) -> c_int;
322322

323323
pub fn X509_add_ext(x: *mut X509, ext: *mut X509_EXTENSION, loc: c_int) -> c_int;
324+
325+
pub fn X509_EXTENSION_get_data(ext: *mut X509_EXTENSION) -> *mut ASN1_STRING;
326+
327+
pub fn X509_EXTENSION_get_object(ext: *mut X509_EXTENSION) -> *mut ASN1_OBJECT;
324328
}
325329
cfg_if! {
326330
if #[cfg(any(ossl110, libressl280))] {
@@ -331,6 +335,14 @@ cfg_if! {
331335
crit: *mut c_int,
332336
idx: *mut c_int,
333337
) -> *mut c_void;
338+
339+
pub fn X509_get_ext_by_NID(ext: *const X509, nid: c_int, last_pos: c_int) -> c_int;
340+
341+
pub fn X509_get_ext(ext: *const X509, loc: c_int) -> *mut X509_EXTENSION;
342+
343+
pub fn X509_EXTENSION_get_critical(ext: *const X509_EXTENSION) -> c_int;
344+
345+
pub fn X509_get_ext_count(ext: *const X509) -> c_int;
334346
}
335347
} else {
336348
extern "C" {
@@ -340,6 +352,14 @@ cfg_if! {
340352
crit: *mut c_int,
341353
idx: *mut c_int,
342354
) -> *mut c_void;
355+
356+
pub fn X509_EXTENSION_get_critical(ext: *mut X509_EXTENSION) -> c_int;
357+
358+
pub fn X509_get_ext_by_NID(ext: *mut X509, nid: c_int, last_pos: c_int) -> c_int;
359+
360+
pub fn X509_get_ext(ext: *mut X509, loc: c_int) -> *mut X509_EXTENSION;
361+
362+
pub fn X509_get_ext_count(ext: *mut X509) -> c_int;
343363
}
344364
}
345365
}

openssl-sys/src/x509v3.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ extern "C" {
2727
pub fn GENERAL_NAME_free(name: *mut GENERAL_NAME);
2828
}
2929

30+
#[repr(C)]
31+
pub struct AUTHORITY_KEYID {
32+
pub keyid: *mut ASN1_STRING,
33+
pub issuer: *mut stack_st_GENERAL_NAME,
34+
pub serial: *mut ASN1_INTEGER,
35+
}
36+
37+
extern "C" {
38+
pub fn AUTHORITY_KEYID_free(akid: *mut AUTHORITY_KEYID);
39+
}
40+
3041
#[cfg(any(ossl102, libressl261))]
3142
pub const X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT: c_uint = 0x1;
3243
#[cfg(any(ossl102, libressl261))]

openssl/src/asn1.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ use bio::MemBio;
3737
use bn::{BigNum, BigNumRef};
3838
use error::ErrorStack;
3939
use nid::Nid;
40+
use stack::Stackable;
4041
use string::OpensslString;
4142
use {cvt, cvt_p};
4243

@@ -342,18 +343,36 @@ impl fmt::Display for Asn1ObjectRef {
342343
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
343344
unsafe {
344345
let mut buf = [0; 80];
346+
// NOTE: `len` may be greater than the size of the buffer we provided, which would
347+
// indicate that the result is truncated. See docs here:
348+
// https://www.openssl.org/docs/man1.0.2/man3/OBJ_obj2txt.html
345349
let len = ffi::OBJ_obj2txt(
346350
buf.as_mut_ptr() as *mut _,
347351
buf.len() as c_int,
348352
self.as_ptr(),
349353
0,
350354
);
351-
let s = str::from_utf8(&buf[..len as usize]).map_err(|_| fmt::Error)?;
355+
356+
if len < 0 {
357+
return Err(fmt::Error {});
358+
}
359+
360+
let len = if len as usize > buf.len() {
361+
buf.len()
362+
} else {
363+
len as usize
364+
};
365+
366+
let s = str::from_utf8(&buf[..len]).map_err(|_| fmt::Error)?;
352367
fmt.write_str(s)
353368
}
354369
}
355370
}
356371

372+
impl Stackable for Asn1Object {
373+
type StackType = ffi::stack_st_ASN1_OBJECT;
374+
}
375+
357376
cfg_if! {
358377
if #[cfg(any(ossl110, libressl273))] {
359378
use ffi::ASN1_STRING_get0_data;

0 commit comments

Comments
 (0)