Skip to content

Commit

Permalink
Always provide an X509V3Context in X509Extension::new because OpenSSL…
Browse files Browse the repository at this point in the history
… requires it for some extensions (and segfaults without)
  • Loading branch information
alex committed Mar 24, 2023
1 parent 332311b commit 78aa9aa
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
40 changes: 36 additions & 4 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,14 +816,30 @@ impl X509Extension {
) -> Result<X509Extension, ErrorStack> {
let name = CString::new(name).unwrap();
let value = CString::new(value).unwrap();
let mut ctx;
unsafe {
ffi::init();
let conf = conf.map_or(ptr::null_mut(), ConfRef::as_ptr);
let context = context.map_or(ptr::null_mut(), X509v3Context::as_ptr);
let context_ptr = match context {
Some(c) => c.as_ptr(),
None => {
ctx = mem::zeroed();

ffi::X509V3_set_ctx(
&mut ctx,
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
0,
);
&mut ctx
}
};
let name = name.as_ptr() as *mut _;
let value = value.as_ptr() as *mut _;

cvt_p(ffi::X509V3_EXT_nconf(conf, context, name, value)).map(X509Extension)
cvt_p(ffi::X509V3_EXT_nconf(conf, context_ptr, name, value)).map(X509Extension)
}
}

Expand All @@ -841,14 +857,30 @@ impl X509Extension {
value: &str,
) -> Result<X509Extension, ErrorStack> {
let value = CString::new(value).unwrap();
let mut ctx;
unsafe {
ffi::init();
let conf = conf.map_or(ptr::null_mut(), ConfRef::as_ptr);
let context = context.map_or(ptr::null_mut(), X509v3Context::as_ptr);
let context_ptr = match context {
Some(c) => c.as_ptr(),
None => {
ctx = mem::zeroed();

ffi::X509V3_set_ctx(
&mut ctx,
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
ptr::null_mut(),
0,
);
&mut ctx
}
};
let name = name.as_raw();
let value = value.as_ptr() as *mut _;

cvt_p(ffi::X509V3_EXT_nconf_nid(conf, context, name, value)).map(X509Extension)
cvt_p(ffi::X509V3_EXT_nconf_nid(conf, context_ptr, name, value)).map(X509Extension)
}
}

Expand Down
10 changes: 9 additions & 1 deletion openssl/src/x509/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use crate::x509::X509PurposeId;
#[cfg(any(ossl102, libressl261))]
use crate::x509::X509PurposeRef;
use crate::x509::{
CrlStatus, X509Crl, X509Name, X509Req, X509StoreContext, X509VerifyResult, X509,
CrlStatus, X509Crl, X509Extension, X509Name, X509Req, X509StoreContext, X509VerifyResult, X509,
};
use hex::{self, FromHex};
#[cfg(any(ossl102, libressl261))]
Expand Down Expand Up @@ -287,6 +287,14 @@ fn x509_builder() {
assert_eq!(serial, x509.serial_number().to_bn().unwrap());
}

#[test]
fn x509_extension_new() {
assert!(X509Extension::new(None, None, "crlDistributionPoints", "section").is_err());
assert!(X509Extension::new(None, None, "proxyCertInfo", "").is_err());
assert!(X509Extension::new(None, None, "certificatePolicies", "").is_err());
assert!(X509Extension::new(None, None, "subjectAltName", "dirName:section").is_err());
}

#[test]
fn x509_extension_to_der() {
let builder = X509::builder().unwrap();
Expand Down

0 comments on commit 78aa9aa

Please sign in to comment.