Commit 33e91420 authored by Andrew Scull's avatar Andrew Scull
Browse files

Add X509Name::to_owned()

The X509_NAME_dup() function can fail but that isn't compatible with the
ToOwned trait. Follow the pattern used in BigNum to add a custom,
fallible to_owned() function.
parent 09f672f9
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -1064,6 +1064,13 @@ impl X509NameRef {
        Ok(cmp.cmp(&0))
    }

    /// Copies the name to a new `X509Name`.
    #[corresponds(X509_NAME_dup)]
    #[cfg(any(boringssl, ossl110, libressl270))]
    pub fn to_owned(&self) -> Result<X509Name, ErrorStack> {
        unsafe { cvt_p(ffi::X509_NAME_dup(self.as_ptr())).map(|n| X509Name::from_ptr(n)) }
    }

    to_der! {
        /// Serializes the certificate into a DER-encoded X509 name structure.
        ///
+10 −0
Original line number Diff line number Diff line
@@ -615,6 +615,16 @@ fn test_name_cmp() {
    assert_eq!(Ordering::Greater, subject.try_cmp(issuer).unwrap());
}

#[test]
#[cfg(any(boringssl, ossl110, libressl270))]
fn test_name_to_owned() {
    let cert = include_bytes!("../../test/cert.pem");
    let cert = X509::from_pem(cert).unwrap();
    let name = cert.subject_name();
    let copied_name = name.to_owned().unwrap();
    assert_eq!(Ordering::Equal, name.try_cmp(&copied_name).unwrap());
}

#[test]
#[cfg(any(ossl102, libressl261))]
fn test_verify_param_set_time_fails_verification() {