Skip to content

Commit

Permalink
Merge pull request #1618 from toy/certificate-to-text
Browse files Browse the repository at this point in the history
Convert certificate to human readable text using X509_print
  • Loading branch information
sfackler authored Apr 10, 2022
2 parents 9fb189c + 50c05e0 commit c30d4fd
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions openssl-sys/src/handwritten/x509.rs
Original file line number Diff line number Diff line change
Expand Up @@ -633,3 +633,7 @@ extern "C" {
pub fn X509_CRL_cmp(a: *const X509_CRL, b: *const X509_CRL) -> c_int;
pub fn X509_CRL_match(a: *const X509_CRL, b: *const X509_CRL) -> c_int;
}

extern "C" {
pub fn X509_print(bio: *mut BIO, x509: *mut X509) -> c_int;
}
7 changes: 7 additions & 0 deletions openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,13 @@ impl X509Ref {
to_der,
ffi::i2d_X509
}

to_pem! {
/// Converts the certificate to human readable text.
#[corresponds(X509_print)]
to_text,
ffi::X509_print
}
}

impl ToOwned for X509Ref {
Expand Down
26 changes: 26 additions & 0 deletions openssl/src/x509/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,29 @@ fn test_load_subject_der() {
];
X509Name::from_der(SUBJECT_DER).unwrap();
}

#[test]
fn test_convert_to_text() {
let cert = include_bytes!("../../test/cert.pem");
let cert = X509::from_pem(cert).unwrap();

const SUBSTRINGS: &[&str] = &[
"Certificate:\n",
"Serial Number:",
"Signature Algorithm:",
"Issuer: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd\n",
"Subject: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd, CN=foobar.com\n",
"Subject Public Key Info:",
];

let text = String::from_utf8(cert.to_text().unwrap()).unwrap();

for substring in SUBSTRINGS {
assert!(
text.contains(substring),
"{:?} not found inside {}",
substring,
text
);
}
}

0 comments on commit c30d4fd

Please sign in to comment.