Use constants rather than constructors for Nid

This commit is contained in:
Steven Fackler 2016-10-22 15:58:06 -07:00
parent 3c50c74444
commit 787cad3c82
4 changed files with 970 additions and 3822 deletions

View File

@ -6,7 +6,7 @@ use ffi;
/// This operation takes an amount of time dependent on the length of the two /// This operation takes an amount of time dependent on the length of the two
/// arrays given, but is independent of the contents of a and b. /// arrays given, but is independent of the contents of a and b.
/// ///
/// # Failure /// # Panics
/// ///
/// This function will panic the current task if `a` and `b` do not have the same /// This function will panic the current task if `a` and `b` do not have the same
/// length. /// length.

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
use std::fmt; use std::fmt;
use nid::Nid; use nid::{self, Nid};
/// Type-only version of the `Extension` enum. /// Type-only version of the `Extension` enum.
/// ///
@ -71,10 +71,10 @@ impl Extension {
impl ExtensionType { impl ExtensionType {
pub fn get_nid(&self) -> Option<Nid> { pub fn get_nid(&self) -> Option<Nid> {
match self { match self {
&ExtensionType::KeyUsage => Some(Nid::key_usage()), &ExtensionType::KeyUsage => Some(nid::KEY_USAGE),
&ExtensionType::ExtKeyUsage => Some(Nid::ext_key_usage()), &ExtensionType::ExtKeyUsage => Some(nid::EXT_KEY_USAGE),
&ExtensionType::SubjectAltName => Some(Nid::subject_alt_name()), &ExtensionType::SubjectAltName => Some(nid::SUBJECT_ALT_NAME),
&ExtensionType::IssuerAltName => Some(Nid::issuer_alt_name()), &ExtensionType::IssuerAltName => Some(nid::ISSUER_ALT_NAME),
&ExtensionType::OtherNid(nid) => Some(nid), &ExtensionType::OtherNid(nid) => Some(nid),
&ExtensionType::OtherStr(_) => None, &ExtensionType::OtherStr(_) => None,
} }

View File

@ -8,7 +8,7 @@ use x509::extension::Extension::{KeyUsage, ExtKeyUsage, SubjectAltName, OtherNid
use x509::extension::AltNameOption as SAN; use x509::extension::AltNameOption as SAN;
use x509::extension::KeyUsageOption::{DigitalSignature, KeyEncipherment}; use x509::extension::KeyUsageOption::{DigitalSignature, KeyEncipherment};
use x509::extension::ExtKeyUsageOption::{self, ClientAuth, ServerAuth}; use x509::extension::ExtKeyUsageOption::{self, ClientAuth, ServerAuth};
use nid::Nid; use nid;
fn get_generator() -> X509Generator { fn get_generator() -> X509Generator {
X509Generator::new() X509Generator::new()
@ -20,7 +20,7 @@ fn get_generator() -> X509Generator {
ServerAuth, ServerAuth,
ExtKeyUsageOption::Other("2.999.1".to_owned())])) ExtKeyUsageOption::Other("2.999.1".to_owned())]))
.add_extension(SubjectAltName(vec![(SAN::DNS, "example.com".to_owned())])) .add_extension(SubjectAltName(vec![(SAN::DNS, "example.com".to_owned())]))
.add_extension(OtherNid(Nid::basic_constraints(), "critical,CA:TRUE".to_owned())) .add_extension(OtherNid(nid::BASIC_CONSTRAINTS, "critical,CA:TRUE".to_owned()))
.add_extension(OtherStr("2.999.2".to_owned(), "ASN1:UTF8:example value".to_owned())) .add_extension(OtherStr("2.999.2".to_owned(), "ASN1:UTF8:example value".to_owned()))
} }
@ -48,8 +48,8 @@ fn test_cert_gen() {
fn test_cert_gen_extension_ordering() { fn test_cert_gen_extension_ordering() {
let pkey = pkey(); let pkey = pkey();
get_generator() get_generator()
.add_extension(OtherNid(Nid::subject_key_identifier(), "hash".to_owned())) .add_extension(OtherNid(nid::SUBJECT_KEY_IDENTIFIER, "hash".to_owned()))
.add_extension(OtherNid(Nid::authority_key_identifier(), "keyid:always".to_owned())) .add_extension(OtherNid(nid::AUTHORITY_KEY_IDENTIFIER, "keyid:always".to_owned()))
.sign(&pkey) .sign(&pkey)
.expect("Failed to generate cert with order-dependent extensions"); .expect("Failed to generate cert with order-dependent extensions");
} }
@ -60,9 +60,9 @@ fn test_cert_gen_extension_ordering() {
fn test_cert_gen_extension_bad_ordering() { fn test_cert_gen_extension_bad_ordering() {
let pkey = pkey(); let pkey = pkey();
let result = get_generator() let result = get_generator()
.add_extension(OtherNid(Nid::authority_key_identifier(), .add_extension(OtherNid(nid::AUTHORITY_KEY_IDENTIFIER,
"keyid:always".to_owned())) "keyid:always".to_owned()))
.add_extension(OtherNid(Nid::subject_key_identifier(), "hash".to_owned())) .add_extension(OtherNid(nid::SUBJECT_KEY_IDENTIFIER, "hash".to_owned()))
.sign(&pkey); .sign(&pkey);
assert!(result.is_err()); assert!(result.is_err());
@ -116,7 +116,7 @@ fn test_subject_read_cn() {
let cert = include_bytes!("../../test/cert.pem"); let cert = include_bytes!("../../test/cert.pem");
let cert = X509::from_pem(cert).ok().expect("Failed to load PEM"); let cert = X509::from_pem(cert).ok().expect("Failed to load PEM");
let subject = cert.subject_name(); let subject = cert.subject_name();
let cn = match subject.text_by_nid(Nid::commonName()) { let cn = match subject.text_by_nid(nid::COMMONNAME) {
Some(x) => x, Some(x) => x,
None => panic!("Failed to read CN from cert"), None => panic!("Failed to read CN from cert"),
}; };
@ -130,19 +130,19 @@ fn test_nid_values() {
let cert = X509::from_pem(cert).ok().expect("Failed to load PEM"); let cert = X509::from_pem(cert).ok().expect("Failed to load PEM");
let subject = cert.subject_name(); let subject = cert.subject_name();
let cn = match subject.text_by_nid(Nid::commonName()) { let cn = match subject.text_by_nid(nid::COMMONNAME) {
Some(x) => x, Some(x) => x,
None => panic!("Failed to read CN from cert"), None => panic!("Failed to read CN from cert"),
}; };
assert_eq!(&cn as &str, "example.com"); assert_eq!(&cn as &str, "example.com");
let email = match subject.text_by_nid(Nid::pkcs9_emailAddress()) { let email = match subject.text_by_nid(nid::PKCS9_EMAILADDRESS) {
Some(x) => x, Some(x) => x,
None => panic!("Failed to read subject email address from cert"), None => panic!("Failed to read subject email address from cert"),
}; };
assert_eq!(&email as &str, "test@example.com"); assert_eq!(&email as &str, "test@example.com");
let friendly = match subject.text_by_nid(Nid::friendlyName()) { let friendly = match subject.text_by_nid(nid::FRIENDLYNAME) {
Some(x) => x, Some(x) => x,
None => panic!("Failed to read subject friendly name from cert"), None => panic!("Failed to read subject friendly name from cert"),
}; };
@ -155,7 +155,7 @@ fn test_nid_uid_value() {
let cert = X509::from_pem(cert).ok().expect("Failed to load PEM"); let cert = X509::from_pem(cert).ok().expect("Failed to load PEM");
let subject = cert.subject_name(); let subject = cert.subject_name();
let cn = match subject.text_by_nid(Nid::userId()) { let cn = match subject.text_by_nid(nid::USERID) {
Some(x) => x, Some(x) => x,
None => panic!("Failed to read UID from cert"), None => panic!("Failed to read UID from cert"),
}; };