Skip to content

Commit c30d4fd

Browse files
authored
Merge pull request #1618 from toy/certificate-to-text
Convert certificate to human readable text using X509_print
2 parents 9fb189c + 50c05e0 commit c30d4fd

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

openssl-sys/src/handwritten/x509.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,7 @@ extern "C" {
633633
pub fn X509_CRL_cmp(a: *const X509_CRL, b: *const X509_CRL) -> c_int;
634634
pub fn X509_CRL_match(a: *const X509_CRL, b: *const X509_CRL) -> c_int;
635635
}
636+
637+
extern "C" {
638+
pub fn X509_print(bio: *mut BIO, x509: *mut X509) -> c_int;
639+
}

openssl/src/x509/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,13 @@ impl X509Ref {
580580
to_der,
581581
ffi::i2d_X509
582582
}
583+
584+
to_pem! {
585+
/// Converts the certificate to human readable text.
586+
#[corresponds(X509_print)]
587+
to_text,
588+
ffi::X509_print
589+
}
583590
}
584591

585592
impl ToOwned for X509Ref {

openssl/src/x509/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,3 +476,29 @@ fn test_load_subject_der() {
476476
];
477477
X509Name::from_der(SUBJECT_DER).unwrap();
478478
}
479+
480+
#[test]
481+
fn test_convert_to_text() {
482+
let cert = include_bytes!("../../test/cert.pem");
483+
let cert = X509::from_pem(cert).unwrap();
484+
485+
const SUBSTRINGS: &[&str] = &[
486+
"Certificate:\n",
487+
"Serial Number:",
488+
"Signature Algorithm:",
489+
"Issuer: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd\n",
490+
"Subject: C=AU, ST=Some-State, O=Internet Widgits Pty Ltd, CN=foobar.com\n",
491+
"Subject Public Key Info:",
492+
];
493+
494+
let text = String::from_utf8(cert.to_text().unwrap()).unwrap();
495+
496+
for substring in SUBSTRINGS {
497+
assert!(
498+
text.contains(substring),
499+
"{:?} not found inside {}",
500+
substring,
501+
text
502+
);
503+
}
504+
}

0 commit comments

Comments
 (0)