Skip to content

Commit

Permalink
Merge pull request sfackler#1371 from JoshuaNitschke/master
Browse files Browse the repository at this point in the history
Add additional function so that x509 name with specific type can be added
  • Loading branch information
sfackler authored Dec 25, 2020
2 parents 8f746ec + efddba6 commit 1dd71ad
Show file tree
Hide file tree
Showing 3 changed files with 176 additions and 1 deletion.
27 changes: 27 additions & 0 deletions openssl-sys/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,35 @@ use libc::*;

use *;

// ASN.1 tag values
pub const V_ASN1_EOC: c_int = 0;
pub const V_ASN1_BOOLEAN: c_int = 1;
pub const V_ASN1_INTEGER: c_int = 2;
pub const V_ASN1_BIT_STRING: c_int = 3;
pub const V_ASN1_OCTET_STRING: c_int = 4;
pub const V_ASN1_NULL: c_int = 5;
pub const V_ASN1_OBJECT: c_int = 6;
pub const V_ASN1_OBJECT_DESCRIPTOR: c_int = 7;
pub const V_ASN1_EXTERNAL: c_int = 8;
pub const V_ASN1_REAL: c_int = 9;
pub const V_ASN1_ENUMERATED: c_int = 10;
pub const V_ASN1_UTF8STRING: c_int = 12;
pub const V_ASN1_SEQUENCE: c_int = 16;
pub const V_ASN1_SET: c_int = 17;
pub const V_ASN1_NUMERICSTRING: c_int = 18;
pub const V_ASN1_PRINTABLESTRING: c_int = 19;
pub const V_ASN1_T61STRING: c_int = 20;
pub const V_ASN1_TELETEXSTRING: c_int = 20; // alias
pub const V_ASN1_VIDEOTEXSTRING: c_int = 21;
pub const V_ASN1_IA5STRING: c_int = 22;
pub const V_ASN1_UTCTIME: c_int = 23;
pub const V_ASN1_GENERALIZEDTIME: c_int = 24;
pub const V_ASN1_GRAPHICSTRING: c_int = 25;
pub const V_ASN1_ISO64STRING: c_int = 26;
pub const V_ASN1_VISIBLESTRING: c_int = 26; // alias
pub const V_ASN1_GENERALSTRING: c_int = 27;
pub const V_ASN1_UNIVERSALSTRING: c_int = 28;
pub const V_ASN1_BMPSTRING: c_int = 30;

pub const MBSTRING_FLAG: c_int = 0x1000;
pub const MBSTRING_UTF8: c_int = MBSTRING_FLAG;
Expand Down
95 changes: 95 additions & 0 deletions openssl/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,101 @@ impl fmt::Display for Asn1GeneralizedTimeRef {
}
}

/// An ASN.1 tag value
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct Asn1Tag(c_int);

impl Asn1Tag {
/// Return the underlying c_int value
pub fn as_c_int(&self) -> c_int {
self.0
}

/// Equivalent to OpenSSL Constant: V_ASN1_EOC
pub const EOC: Asn1Tag = Asn1Tag(ffi::V_ASN1_EOC);

/// Equivalent to OpenSSL Constant: V_ASN1_BOOLEAN
pub const BOOLEAN: Asn1Tag = Asn1Tag(ffi::V_ASN1_BOOLEAN);

/// Equivalent to OpenSSL Constant: V_ASN1_INTEGER
pub const INTEGER: Asn1Tag = Asn1Tag(ffi::V_ASN1_INTEGER);

/// Equivalent to OpenSSL Constant: V_ASN1_BIT_STRING
pub const BIT_STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_BIT_STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_OCTET_STRING
pub const OCTET_STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_OCTET_STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_NULL
pub const NULL: Asn1Tag = Asn1Tag(ffi::V_ASN1_NULL);

/// Equivalent to OpenSSL Constant: V_ASN1_OBJECT
pub const OBJECT: Asn1Tag = Asn1Tag(ffi::V_ASN1_OBJECT);

/// Equivalent to OpenSSL Constant: V_ASN1_OBJECT_DESCRIPTOR
pub const OBJECT_DESCRIPTOR: Asn1Tag = Asn1Tag(ffi::V_ASN1_OBJECT_DESCRIPTOR);

/// Equivalent to OpenSSL Constant: V_ASN1_EXTERNAL
pub const EXTERNAL: Asn1Tag = Asn1Tag(ffi::V_ASN1_EXTERNAL);

/// Equivalent to OpenSSL Constant: V_ASN1_REAL
pub const REAL: Asn1Tag = Asn1Tag(ffi::V_ASN1_REAL);

/// Equivalent to OpenSSL Constant: V_ASN1_ENUMERATED
pub const ENUMERATED: Asn1Tag = Asn1Tag(ffi::V_ASN1_ENUMERATED);

/// Equivalent to OpenSSL Constant: V_ASN1_UTF8STRING
pub const UTF8STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_UTF8STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_SEQUENCE
pub const SEQUENCE: Asn1Tag = Asn1Tag(ffi::V_ASN1_SEQUENCE);

/// Equivalent to OpenSSL Constant: V_ASN1_SET
pub const SET: Asn1Tag = Asn1Tag(ffi::V_ASN1_SET);

/// Equivalent to OpenSSL Constant: V_ASN1_NUMERICSTRING
pub const NUMERICSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_NUMERICSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_PRINTABLESTRING
pub const PRINTABLESTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_PRINTABLESTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_T61STRING
pub const T61STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_T61STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_TELETEXSTRING
pub const TELETEXSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_TELETEXSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_VIDEOTEXSTRING
pub const VIDEOTEXSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_VIDEOTEXSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_IA5STRING
pub const IA5STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_IA5STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_UTCTIME
pub const UTCTIME: Asn1Tag = Asn1Tag(ffi::V_ASN1_UTCTIME);

/// Equivalent to OpenSSL Constant: V_ASN1_GENERALIZEDTIME
pub const GENERALIZEDTIME: Asn1Tag = Asn1Tag(ffi::V_ASN1_GENERALIZEDTIME);

/// Equivalent to OpenSSL Constant: V_ASN1_GRAPHICSTRING
pub const GRAPHICSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_GRAPHICSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_ISO64STRING
pub const ISO64STRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_ISO64STRING);

/// Equivalent to OpenSSL Constant: V_ASN1_VISIBLESTRING
pub const VISIBLESTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_VISIBLESTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_GENERALSTRING
pub const GENERALSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_GENERALSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_UNIVERSALSTRING
pub const UNIVERSALSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_UNIVERSALSTRING);

/// Equivalent to OpenSSL Constant: V_ASN1_BMPSTRING
pub const BMPSTRING: Asn1Tag = Asn1Tag(ffi::V_ASN1_BMPSTRING);
}

/// Difference between two ASN1 times.
///
/// This `struct` is created by the [`diff`] method on [`Asn1TimeRef`]. See its
Expand Down
55 changes: 54 additions & 1 deletion openssl/src/x509/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::ptr;
use std::slice;
use std::str;

use asn1::{Asn1BitStringRef, Asn1IntegerRef, Asn1ObjectRef, Asn1StringRef, Asn1TimeRef};
use asn1::{Asn1BitStringRef, Asn1IntegerRef, Asn1ObjectRef, Asn1StringRef, Asn1Tag, Asn1TimeRef};
use bio::MemBioSlice;
use conf::ConfRef;
use error::ErrorStack;
Expand Down Expand Up @@ -824,6 +824,33 @@ impl X509NameBuilder {
}
}

/// Add a field entry by str with a specific type. See [Asn1Tag](openssl::asn1::Asn1Tag)
///
/// This corresponds to [`X509_NAME_add_entry_by_txt`].
///
/// [`X509_NAME_add_entry_by_txt`]: https://www.openssl.org/docs/man1.1.0/crypto/X509_NAME_add_entry_by_txt.html
pub fn append_entry_by_text_with_type(
&mut self,
field: &str,
value: &str,
ty: Asn1Tag,
) -> Result<(), ErrorStack> {
unsafe {
let field = CString::new(field).unwrap();
assert!(value.len() <= c_int::max_value() as usize);
cvt(ffi::X509_NAME_add_entry_by_txt(
self.0.as_ptr(),
field.as_ptr() as *mut _,
ty.as_c_int(),
value.as_ptr(),
value.len() as c_int,
-1,
0,
))
.map(|_| ())
}
}

/// Add a field entry by NID.
///
/// This corresponds to [`X509_NAME_add_entry_by_NID`].
Expand All @@ -845,6 +872,32 @@ impl X509NameBuilder {
}
}

/// Add a field entry by NID with a specific type. See [Asn1Tag](openssl::asn1::Asn1Tag)
///
/// This corresponds to [`X509_NAME_add_entry_by_NID`].
///
/// [`X509_NAME_add_entry_by_NID`]: https://www.openssl.org/docs/man1.1.0/crypto/X509_NAME_add_entry_by_NID.html
pub fn append_entry_by_nid_with_type(
&mut self,
field: Nid,
value: &str,
ty: Asn1Tag,
) -> Result<(), ErrorStack> {
unsafe {
assert!(value.len() <= c_int::max_value() as usize);
cvt(ffi::X509_NAME_add_entry_by_NID(
self.0.as_ptr(),
field.as_raw(),
ty.as_c_int(),
value.as_ptr() as *mut _,
value.len() as c_int,
-1,
0,
))
.map(|_| ())
}
}

/// Return an `X509Name`.
pub fn build(self) -> X509Name {
self.0
Expand Down

0 comments on commit 1dd71ad

Please sign in to comment.