Skip to content

Commit

Permalink
Merge pull request #1756 from steffen-eiden/x509Name
Browse files Browse the repository at this point in the history
Add X509_NAME_add_entry binding
  • Loading branch information
sfackler authored Dec 21, 2022
2 parents 7df5686 + 263c7ce commit 88b46c9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions openssl-sys/src/handwritten/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ const_ptr_api! {
pub fn X509_NAME_dup(x: #[const_ptr_if(ossl300)] X509_NAME) -> *mut X509_NAME;
#[cfg(any(ossl110, libressl270))]
pub fn X509_dup(x: #[const_ptr_if(ossl300)] X509) -> *mut X509;
#[cfg(any(ossl101, libressl350))]
pub fn X509_NAME_add_entry(
name: *mut X509_NAME,
ne: #[const_ptr_if(any(ossl110, libressl))] X509_NAME_ENTRY,
loc: c_int,
set: c_int,
) -> c_int;
}
}
extern "C" {
Expand Down
15 changes: 15 additions & 0 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,21 @@ impl X509NameBuilder {
}
}

/// Add a name entry
#[corresponds(X509_NAME_add_entry)]
#[cfg(any(ossl101, libressl350))]
pub fn append_entry(&mut self, ne: &X509NameEntryRef) -> std::result::Result<(), ErrorStack> {
unsafe {
cvt(ffi::X509_NAME_add_entry(
self.0.as_ptr(),
ne.as_ptr(),
-1,
0,
))
.map(|_| ())
}
}

/// Add a field entry by str.
///
/// This corresponds to [`X509_NAME_add_entry_by_txt`].
Expand Down
18 changes: 18 additions & 0 deletions openssl/src/x509/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,3 +693,21 @@ fn test_load_cert_file() {
.init(&store, &cert, &chain, |c| c.verify_cert())
.unwrap());
}

#[test]
#[cfg(any(ossl101, libressl350))]
fn test_add_name_entry() {
let cert = include_bytes!("../../test/cert.pem");
let cert = X509::from_pem(cert).unwrap();
let inp_name = cert.subject_name().entries().next().unwrap();

let mut names = X509Name::builder().unwrap();
names.append_entry(inp_name).unwrap();
let names = names.build();

let mut entries = names.entries();
let outp_name = entries.next().unwrap();
assert_eq!(outp_name.object().nid(), inp_name.object().nid());
assert_eq!(outp_name.data().as_slice(), inp_name.data().as_slice());
assert!(entries.next().is_none());
}

0 comments on commit 88b46c9

Please sign in to comment.