Commit 50c05e0f authored by Ivan Kuchin's avatar Ivan Kuchin
Browse files

convert certificate to human readable text using X509_print

parent 0c6b988f
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -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 −0
Original line number Diff line number Diff line
@@ -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 {
+26 −0
Original line number Diff line number Diff line
@@ -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
        );
    }
}