pub struct Der { /* private fields */ }Expand description
Implementations
sourceimpl Der
impl Der
sourcepub fn new(id: &IdRef, contents: &ContentsRef) -> Self
pub fn new(id: &IdRef, contents: &ContentsRef) -> Self
Creates a new instance from id and contents.
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function accepts
such identifiers.
For example, ‘Octet String’ must be primitive in DER, but this function will construct a
new instance even if id represenets constructed ‘Octet String.’
Examples
use bsn1::{ContentsRef, Der, IdRef};
let id = IdRef::octet_string();
let contents = ContentsRef::from_bytes(&[10, 20, 30]);
let der = Der::new(id, contents);
assert_eq!(id, der.id());
assert_eq!(contents, der.contents());sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Error>
Parses bytes starting with DER octets and builds a new instance.
This function ignores extra octet(s) at the end of bytes if any.
This function is same to TryFrom::try_from .
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function accepts
such an identifier.
For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for
constructed Octet String DER.
Examples
use bsn1::Der;
let bytes: &[u8] = &[0x02, 0x01, 0x0a]; // Represents '10' as Integer.
let der0 = Der::from_bytes(bytes).unwrap();
// Extra octets at the end does not affect the result.
let bytes: &[u8] = &[0x02, 0x01, 0x0a, 0x01, 0x02];
let der1 = Der::from_bytes(bytes).unwrap();
assert_eq!(der0, der1);sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> Self
Builds a new instance holding bytes without any check.
bytes must not include any extra octet.
If it is not sure whether bytes are valid octets as an ‘DER’ or not, use TryFrom
implementation or from_bytes.
Safety
The behavior is undefined if bytes is not formatted as a DER.
Examples
use bsn1::Der;
let bytes: &[u8] = &[0x02, 0x01, 0x0a]; // Represents '10' as Integer.
let der = unsafe { Der::from_bytes_unchecked(bytes) };
assert_eq!(bytes, der.as_bytes());sourcepub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Selfwhere
I: Iterator + Clone,
I::Item: AsRef<[u8]>,
pub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Selfwhere
I: Iterator + Clone,
I::Item: AsRef<[u8]>,
Creates a new instance from id and the iterator of contents.
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function accepts
such an identifier.
For example, ‘Octet String’ must be primitive in DER, but this function will construct a
new instance even if id represenets constructed ‘Octet String.’
Examples
use bsn1::{ContentsRef, Der, IdRef};
let id = IdRef::sequence();
// Build instance using function 'from_id_iterator()'.
let contents: &[Der] = &[Der::utf8_string("foo"), Der::integer(29_i32)];
let der = Der::from_id_iterator(id, contents.iter());
// Build instance using function 'new()'.
let contents: Vec<u8> = contents.iter()
.map(|i| Vec::from(i.as_ref() as &[u8]))
.flatten().collect();
let contents = ContentsRef::from_bytes(&contents);
let expected = Der::new(id, contents);
// The result are same.
assert_eq!(expected, der);sourcepub fn boolean(val: bool) -> Self
pub fn boolean(val: bool) -> Self
Returns a new instance representing boolean.
Examples
use bsn1::{Der, IdRef};
let val = true;
let der = Der::boolean(val);
assert_eq!(IdRef::boolean(), der.id());
assert_eq!(val, der.contents().to_bool_der().unwrap());sourcepub fn integer<T>(val: T) -> Selfwhere
T: PrimInt,
pub fn integer<T>(val: T) -> Selfwhere
T: PrimInt,
Returns a new instance representing integer.
Type T should be the builtin primitive integer types (e.g., u8, u32, isize, i128, …)
Examples
use bsn1::{Der, IdRef};
let val = 39;
let der = Der::integer(val);
assert_eq!(IdRef::integer(), der.id());
assert_eq!(val, der.contents().to_integer().unwrap());sourcepub fn utf8_string(val: &str) -> Self
pub fn utf8_string(val: &str) -> Self
Returns a new instance representing utf8_string.
Examples
use bsn1::{Der, IdRef};
let val = "foo";
let der = Der::utf8_string(val);
assert_eq!(IdRef::utf8_string(), der.id());
assert_eq!(val.as_bytes(), der.contents().as_ref());sourcepub fn octet_string(val: &[u8]) -> Self
pub fn octet_string(val: &[u8]) -> Self
Returns a new instance representing octet_string.
Examples
use bsn1::{Der, IdRef};
let val = &[1, 2, 3];
let der = Der::octet_string(val);
assert_eq!(IdRef::octet_string(), der.id());
assert_eq!(val, der.contents().as_ref());Methods from Deref<Target = DerRef>
sourcepub fn id(&self) -> &IdRef
pub fn id(&self) -> &IdRef
Returns a reference to the IdRef of self .
Examples
use bsn1::{DerRef, IdRef};
let bytes: &[u8] = &[0x02, 0x01, 0x04]; // Represents '4' as Integer.
let der = DerRef::from_bytes(bytes).unwrap();
assert_eq!(IdRef::integer(), der.id());sourcepub fn length(&self) -> Length
pub fn length(&self) -> Length
Returns Length to represent the length of contents.
Note that DER does not allow indefinite Length.
The return value must be Length::Definite.
Warnings
Length stands for the length octets in DER; i.e. the length of the contents.
The total byte count of the DER is greater than the value.
Examples
use bsn1::{DerRef, Length};
let bytes: &[u8] = &[0x04, 0x02, 0x00, 0xff]; // Represents '[0x00, 0xff]' as Octet String
let der = DerRef::from_bytes(bytes).unwrap();
assert_eq!(Length::Definite(2), der.length());sourcepub fn contents(&self) -> &ContentsRef
pub fn contents(&self) -> &ContentsRef
Returns a reference to the contents octets of self .
Examples
use bsn1::{ContentsRef, DerRef};
let bytes: &[u8] = &[0x04, 0x02, 0x00, 0xff]; // Represents '[0x00, 0xff]' as Octet String
let der = DerRef::from_bytes(bytes).unwrap();
let contents = ContentsRef::from_bytes(&bytes[2..]);
assert_eq!(contents, der.contents());sourcepub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
pub fn as_bytes(&self) -> &[u8]ⓘNotable traits for &mut [u8]impl Write for &mut [u8]impl Read for &[u8]
Provides a reference to the inner slice.
Example
use bsn1::DerRef;
// This octets represents `3` as integer.
let bytes = vec![0x02, 0x01, 0x03];
let der = DerRef::from_bytes(&bytes).unwrap();
assert_eq!(&bytes, der.as_bytes());Trait Implementations
sourceimpl TryFrom<&[u8]> for Der
impl TryFrom<&[u8]> for Der
sourcefn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error>
Parses bytes starting with DER octets and creates a new instance.
This function ignores extra octet(s) at the end of bytes if any.
This function is same to from_bytes .
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function accepts
such an identifier.
For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for
constructed Octet String DER.