pub struct DerRef { /* private fields */ }Expand description
DerRef is a wrapper of [u8] and represents DER.
This struct is ‘Unsized’, and user will usually uses a reference.
Implementations
sourceimpl DerRef
impl DerRef
sourcepub fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
pub fn from_bytes(bytes: &[u8]) -> Result<&Self, Error>
Parses bytes starting with octets of ‘ASN.1 DER’ and returns a reference to DerRef.
This function ignores extra octet(s) at the end of bytes if any.
This function is same to <&DerRef>::try_from.
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function will accept
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::DerRef;
// Represents '8' as Integer.
let bytes0: &[u8] = &[0x02, 0x01, 0x08];
let der0 = DerRef::from_bytes(bytes0).unwrap();
// The result is not changed even if extra octets are added to the end.
let bytes1: &[u8] = &[0x02, 0x01, 0x08, 0x00, 0xff];
let der1 = DerRef::from_bytes(bytes1).unwrap();
assert_eq!(der0, der1);sourcepub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
pub unsafe fn from_bytes_unchecked(bytes: &[u8]) -> &Self
Provides a reference from bytes without any check.
bytes must not include any extra octet.
If it is not sure whether bytes is 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::DerRef;
let bytes: &[u8] = &[0x02, 0x01, 0x08]; // Represents '8' as Integer.
let _der: &DerRef = unsafe { DerRef::from_bytes_unchecked(bytes) };sourceimpl DerRef
impl 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 ToOwned for DerRef
impl ToOwned for DerRef
sourceimpl<'a> TryFrom<&'a [u8]> for &'a DerRef
impl<'a> TryFrom<&'a [u8]> for &'a DerRef
sourcefn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error>
fn try_from(bytes: &'a [u8]) -> Result<Self, Self::Error>
Parses bytes starting with octets of ‘ASN.1 DER’ and returns a reference to DerRef .
This function ignores extra octet(s) at the end of bytes if any.
This function is same to DerRef::from_bytes.
Warnings
ASN.1 does not allow some universal identifier for DER, however, this function will accept
such an identifier.
For example, ‘Octet String’ must be primitive in DER, but this function returns Ok for
constructed Octet String DER.