aboutsummaryrefslogtreecommitdiff
path: root/openssl/src
diff options
context:
space:
mode:
authorSteven Fackler <[email protected]>2018-05-28 15:02:56 -0700
committerGitHub <[email protected]>2018-05-28 15:02:56 -0700
commita523219f6cdf761019d0125bf4818acb90c47477 (patch)
tree753b37cb91d948ee15df7eef25b6c14ff9f92d06 /openssl/src
parentMerge pull request #934 from sfackler/digest-algo (diff)
parentrewrite Nid::{long_name,short_name} to return Results instead of Options (diff)
downloadrust-openssl-a523219f6cdf761019d0125bf4818acb90c47477.tar.xz
rust-openssl-a523219f6cdf761019d0125bf4818acb90c47477.zip
Merge pull request #929 from marcoh00/nid-names
Get Nid string representations
Diffstat (limited to 'openssl/src')
-rw-r--r--openssl/src/nid.rs64
1 files changed, 63 insertions, 1 deletions
diff --git a/openssl/src/nid.rs b/openssl/src/nid.rs
index 78ffac96..23068406 100644
--- a/openssl/src/nid.rs
+++ b/openssl/src/nid.rs
@@ -1,8 +1,14 @@
//! A collection of numerical identifiers for OpenSSL objects.
use ffi;
-use libc::c_int;
+use libc::{c_int,c_char};
use std::ptr;
+use std::ffi::CStr;
+use std::str;
+
+use error::ErrorStack;
+use cvt_p;
+
/// A numerical identifier for an OpenSSL object.
///
/// Objects in OpenSSL can have a short name, a long name, and
@@ -57,6 +63,28 @@ impl Nid {
}
}
+ /// Return the string representation of a `Nid` (long)
+ /// This corresponds to [`OBJ_nid2ln`]
+ ///
+ /// [`OBJ_nid2ln`]: https://www.openssl.org/docs/man1.1.0/crypto/OBJ_nid2ln.html
+ pub fn long_name(&self) -> Result<&'static str, ErrorStack> {
+ unsafe {
+ cvt_p(ffi::OBJ_nid2ln(self.0) as *mut c_char)
+ .map(|nameptr| str::from_utf8(CStr::from_ptr(nameptr).to_bytes()).unwrap())
+ }
+ }
+
+ /// Return the string representation of a `Nid` (short)
+ /// This corresponds to [`OBJ_nid2sn`]
+ ///
+ /// [`OBJ_nid2sn`]: https://www.openssl.org/docs/man1.1.0/crypto/OBJ_nid2sn.html
+ pub fn short_name(&self) -> Result<&'static str, ErrorStack> {
+ unsafe {
+ cvt_p(ffi::OBJ_nid2sn(self.0) as *mut c_char)
+ .map(|nameptr| str::from_utf8(CStr::from_ptr(nameptr).to_bytes()).unwrap())
+ }
+ }
+
pub const UNDEF: Nid = Nid(ffi::NID_undef);
pub const ITU_T: Nid = Nid(ffi::NID_itu_t);
pub const CCITT: Nid = Nid(ffi::NID_ccitt);
@@ -1018,4 +1046,38 @@ mod test {
Some(Nid::SHA256)
);
}
+
+ #[test]
+ fn test_long_name_conversion() {
+ let common_name = Nid::COMMONNAME;
+ let organizational_unit_name = Nid::ORGANIZATIONALUNITNAME;
+ let aes256_cbc_hmac_sha1 = Nid::AES_256_CBC_HMAC_SHA1;
+ let id_cmc_lrapopwitness = Nid::ID_CMC_LRAPOPWITNESS;
+ let ms_ctl_sign = Nid::MS_CTL_SIGN;
+ let undefined_nid = Nid::from_raw(118);
+
+ assert_eq!(common_name.long_name().unwrap(), "commonName");
+ assert_eq!(organizational_unit_name.long_name().unwrap(), "organizationalUnitName");
+ assert_eq!(aes256_cbc_hmac_sha1.long_name().unwrap(), "aes-256-cbc-hmac-sha1");
+ assert_eq!(id_cmc_lrapopwitness.long_name().unwrap(), "id-cmc-lraPOPWitness");
+ assert_eq!(ms_ctl_sign.long_name().unwrap(), "Microsoft Trust List Signing");
+ assert!(undefined_nid.long_name().is_err(), "undefined_nid should not return a valid value");
+ }
+
+ #[test]
+ fn test_short_name_conversion() {
+ let common_name = Nid::COMMONNAME;
+ let organizational_unit_name = Nid::ORGANIZATIONALUNITNAME;
+ let aes256_cbc_hmac_sha1 = Nid::AES_256_CBC_HMAC_SHA1;
+ let id_cmc_lrapopwitness = Nid::ID_CMC_LRAPOPWITNESS;
+ let ms_ctl_sign = Nid::MS_CTL_SIGN;
+ let undefined_nid = Nid::from_raw(118);
+
+ assert_eq!(common_name.short_name().unwrap(), "CN");
+ assert_eq!(organizational_unit_name.short_name().unwrap(), "OU");
+ assert_eq!(aes256_cbc_hmac_sha1.short_name().unwrap(), "AES-256-CBC-HMAC-SHA1");
+ assert_eq!(id_cmc_lrapopwitness.short_name().unwrap(), "id-cmc-lraPOPWitness");
+ assert_eq!(ms_ctl_sign.short_name().unwrap(), "msCTLSign");
+ assert!(undefined_nid.short_name().is_err(), "undefined_nid should not return a valid value");
+ }
}