Unverified Commit c38a86cc authored by Steven Fackler's avatar Steven Fackler Committed by GitHub
Browse files

Merge pull request #1341 from igorty/X509_version_getter

Add X509Ref::version
parents 75b0978b 484aa929
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -288,6 +288,8 @@ extern "C" {
    pub fn X509_get_pubkey(x: *mut X509) -> *mut EVP_PKEY;

    pub fn X509_set_version(x: *mut X509, version: c_long) -> c_int;
    #[cfg(ossl110)]
    pub fn X509_get_version(x: *const X509) -> c_long;
    pub fn X509_set_serialNumber(x: *mut X509, sn: *mut ASN1_INTEGER) -> c_int;
    pub fn X509_get_serialNumber(x: *mut X509) -> *mut ASN1_INTEGER;
    pub fn X509_set_issuer_name(x: *mut X509, name: *mut X509_NAME) -> c_int;
+48 −1
Original line number Diff line number Diff line
@@ -41,7 +41,40 @@ pub mod extension;
pub mod store;

#[cfg(test)]
mod tests;
mod tests {
    #[cfg(ossl110)]
    use x509::X509Builder;

    /// Tests `X509Ref::version` happy path.
    #[cfg(ossl110)]
    #[test]
    fn x509_ref_version() {
        let mut builder = X509Builder::new().unwrap();
        let expected_version = 2;
        builder
            .set_version(expected_version)
            .expect("Failed to set certificate version");
        let cert = builder.build();
        let actual_version = cert.version();
        assert_eq!(
            expected_version, actual_version,
            "Obtained certificate version is incorrect",
        );
    }

    /// Tests `X509Ref::version`. Checks case when no version has been set, so a default one is
    /// returned.
    #[cfg(ossl110)]
    #[test]
    fn x509_ref_version_no_version_set() {
        let cert = X509Builder::new().unwrap().build();
        let actual_version = cert.version();
        assert_eq!(
            0, actual_version,
            "Default certificate version is incorrect",
        );
    }
}

foreign_type_and_impl_send_sync! {
    type CType = ffi::X509_STORE_CTX;
@@ -532,6 +565,20 @@ impl X509Ref {
        }
    }

    /// Returns certificate version. If this certificate has no explicit version set, it defaults to
    /// version 1.
    ///
    /// Note that `0` return value stands for version 1, `1` for version 2 and so on.
    ///
    /// This corresponds to [`X509_get_version`].
    ///
    /// [`X509_get_version`]: https://www.openssl.org/docs/man1.1.1/man3/X509_get_version.html
    #[cfg(ossl110)]
    pub fn version(&self) -> i32 {
        // Covered with `x509_ref_version()`, `x509_ref_version_no_version_set()` tests
        unsafe { ffi::X509_get_version(self.as_ptr()) as i32 }
    }

    /// Check if the certificate is signed using the given public key.
    ///
    /// Only the signature is checked: no other checks (such as certificate chain validity)