From d8f299fbbe7ebef5f8b5f6a3f48a652e9a273e94 Mon Sep 17 00:00:00 2001 From: Tim Pambor Date: Sat, 6 Nov 2021 12:50:53 +0100 Subject: [PATCH] Export OBJ_create --- openssl-sys/src/object.rs | 5 +++++ openssl/src/nid.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/openssl-sys/src/object.rs b/openssl-sys/src/object.rs index 99abdebc9..d2c525b80 100644 --- a/openssl-sys/src/object.rs +++ b/openssl-sys/src/object.rs @@ -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)] diff --git a/openssl/src/nid.rs b/openssl/src/nid.rs index 8545666cb..8d3a54cfb 100644 --- a/openssl/src/nid.rs +++ b/openssl/src/nid.rs @@ -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 { + 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" + ); + } } -- GitLab