Skip to content

Commit

Permalink
simplify converting asn1string
Browse files Browse the repository at this point in the history
in combination with the previous commit this makes it easier to get an
asn1string from a `&str` and convert it to a der encoding with a asn1
type tag.
  • Loading branch information
huettner94 committed May 9, 2023
1 parent e17420a commit 77c0850
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions openssl/src/asn1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,30 @@ foreign_type_and_impl_send_sync! {
pub struct Asn1StringRef;
}

impl Asn1String {
/// Converts a `&str` to an `Asn1String`.
pub fn from_str(data: &str) -> Result<Self, ErrorStack> {
unsafe {
let s = cvt_p(ffi::ASN1_STRING_type_new(Asn1Type::IA5STRING.as_raw()))?;
ffi::ASN1_STRING_set(s, data.as_ptr().cast(), data.len().try_into().unwrap());
Ok(Self::from_ptr(s))
}
}

/// Converts the Asn1String to the ASN.1 representation with a IA5String tag
pub fn as_asn1type_der(&self) -> Result<Vec<u8>, ErrorStack> {
unsafe {
let typ = cvt_p(ffi::ASN1_TYPE_new()).unwrap();
ffi::ASN1_TYPE_set(typ, ffi::V_ASN1_IA5STRING, self.0.cast());

let len = cvt(ffi::i2d_ASN1_TYPE(typ, ptr::null_mut()))?;
let mut buf = vec![0; len as usize];
crate::cvt(ffi::i2d_ASN1_TYPE(typ, &mut buf.as_mut_ptr()))?;
Ok(buf)
}
}
}

impl Asn1StringRef {
/// Converts the ASN.1 underlying format to UTF8
///
Expand Down

0 comments on commit 77c0850

Please sign in to comment.