Unverified Commit e59ab31f authored by Alex Gaynor's avatar Alex Gaynor Committed by GitHub
Browse files

Merge pull request #1893 from reaperhulk/asn1octetstring

add asn1octetstring creation support
parents a882c692 2ac0d838
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -51,9 +51,15 @@ extern "C" {
    #[cfg(any(all(ossl101, not(ossl110)), libressl))]
    pub fn ASN1_STRING_data(x: *mut ASN1_STRING) -> *mut c_uchar;
    pub fn ASN1_STRING_new() -> *mut ASN1_STRING;
    pub fn ASN1_OCTET_STRING_new() -> *mut ASN1_OCTET_STRING;
    pub fn ASN1_STRING_free(x: *mut ASN1_STRING);
    pub fn ASN1_STRING_length(x: *const ASN1_STRING) -> c_int;
    pub fn ASN1_STRING_set(x: *mut ASN1_STRING, data: *const c_void, len_in: c_int) -> c_int;
    pub fn ASN1_OCTET_STRING_set(
        x: *mut ASN1_OCTET_STRING,
        data: *const c_uchar,
        len_in: c_int,
    ) -> c_int;

    pub fn ASN1_BIT_STRING_free(x: *mut ASN1_BIT_STRING);
    pub fn ASN1_OCTET_STRING_free(x: *mut ASN1_OCTET_STRING);
+48 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ use cfg_if::cfg_if;
use foreign_types::{ForeignType, ForeignTypeRef};
use libc::{c_char, c_int, c_long, time_t};
use std::cmp::Ordering;
use std::convert::TryInto;
use std::ffi::CString;
use std::fmt;
use std::ptr;
@@ -611,6 +612,46 @@ impl Asn1BitStringRef {
    }
}

foreign_type_and_impl_send_sync! {
    type CType = ffi::ASN1_OCTET_STRING;
    fn drop = ffi::ASN1_OCTET_STRING_free;
    /// ASN.1 OCTET STRING type
    pub struct Asn1OctetString;
    /// A reference to an [`Asn1OctetString`].
    pub struct Asn1OctetStringRef;
}

impl Asn1OctetString {
    /// Creates an Asn1OctetString from bytes
    pub fn new_from_bytes(value: &[u8]) -> Result<Self, ErrorStack> {
        ffi::init();
        unsafe {
            let s = cvt_p(ffi::ASN1_OCTET_STRING_new())?;
            ffi::ASN1_OCTET_STRING_set(s, value.as_ptr(), value.len().try_into().unwrap());
            Ok(Self::from_ptr(s))
        }
    }
}

impl Asn1OctetStringRef {
    /// Returns the octet string as an array of bytes.
    #[corresponds(ASN1_STRING_get0_data)]
    pub fn as_slice(&self) -> &[u8] {
        unsafe { slice::from_raw_parts(ASN1_STRING_get0_data(self.as_ptr().cast()), self.len()) }
    }

    /// Returns the number of bytes in the octet string.
    #[corresponds(ASN1_STRING_length)]
    pub fn len(&self) -> usize {
        unsafe { ffi::ASN1_STRING_length(self.as_ptr().cast()) as usize }
    }

    /// Determines if the string is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

foreign_type_and_impl_send_sync! {
    type CType = ffi::ASN1_OBJECT;
    fn drop = ffi::ASN1_OBJECT_free;
@@ -859,4 +900,11 @@ mod tests {
            &[0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01],
        );
    }

    #[test]
    fn asn1_octet_string() {
        let octet_string = Asn1OctetString::new_from_bytes(b"hello world").unwrap();
        assert_eq!(octet_string.as_slice(), b"hello world");
        assert_eq!(octet_string.len(), 11);
    }
}