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

Merge pull request #1554 from tpambor/nid-create

Export OBJ_create
parents db2b0d79 d8f299fb
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -18,6 +18,11 @@ extern "C" {
        -> c_int;
    pub fn OBJ_sn2nid(sn: *const libc::c_char) -> libc::c_int;
    pub fn OBJ_txt2obj(s: *const libc::c_char, no_name: libc::c_int) -> *mut ASN1_OBJECT;
    pub fn OBJ_create(
        oid: *const libc::c_char,
        sn: *const libc::c_char,
        ln: *const libc::c_char,
    ) -> c_int;
    #[cfg(ossl111)]
    pub fn OBJ_length(obj: *const ASN1_OBJECT) -> libc::size_t;
    #[cfg(ossl111)]
+32 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@
use libc::{c_char, c_int};

use std::ffi::CStr;
use std::ffi::CString;
use std::str;

use crate::cvt_p;
@@ -59,6 +60,24 @@ impl Nid {
        self.0
    }

    /// Creates a new `Nid` for the `oid` with short name `sn` and long name `ln`.
    ///
    /// This corresponds to `OBJ_create`
    pub fn create(oid: &str, sn: &str, ln: &str) -> Result<Nid, ErrorStack> {
        unsafe {
            ffi::init();
            let oid = CString::new(oid).unwrap();
            let sn = CString::new(sn).unwrap();
            let ln = CString::new(ln).unwrap();
            let raw = ffi::OBJ_create(oid.as_ptr(), sn.as_ptr(), ln.as_ptr());
            if raw == ffi::NID_undef {
                Err(ErrorStack::get())
            } else {
                Ok(Nid(raw))
            }
        }
    }

    /// Returns the `Nid`s of the digest and public key algorithms associated with a signature ID.
    ///
    /// This corresponds to `OBJ_find_sigid_algs`.
@@ -1120,4 +1139,17 @@ mod test {
            "undefined_nid should not return a valid value"
        );
    }

    #[test]
    fn test_create() {
        let nid = Nid::create("1.2.3.4", "foo", "foobar").unwrap();
        assert_eq!(nid.short_name().unwrap(), "foo");
        assert_eq!(nid.long_name().unwrap(), "foobar");

        let invalid_oid = Nid::create("invalid_oid", "invalid", "invalid");
        assert!(
            invalid_oid.is_err(),
            "invalid_oid should not return a valid value"
        );
    }
}