pub struct Der { /* private fields */ }
Expand description
Implementations§
source§impl 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 identifiers 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
represents constructed ‘Octet String.’
§Panics
Panics if the total length of the return value exceeds isize::MAX
.
§Examples
use bsn1::{ContentsRef, Der, IdRef};
let id = IdRef::octet_string();
let contents = <&ContentsRef>::from(&[10, 20, 30]);
let der = Der::new(id, contents);
assert_eq!(id, der.id());
assert_eq!(contents, der.contents());
sourcepub fn with_id_length(id: &IdRef, length: usize) -> Self
pub fn with_id_length(id: &IdRef, length: usize) -> Self
Creates a new instance with id
and ‘definite length
’.
The ‘contents octets’ of the return value holds length
bytes,
but they are not initialized.
Use mut_contents
via DerefMut
implementation to initialize them.
§Warnings
ASN.1 does not allow some universal identifiers 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
represents constructed ‘Octet String.’
§Panics
Panics if the total bytes exceed isize::MAX
.
§Examples
use bsn1::{Der, IdRef, Length};
let mut der = Der::with_id_length(IdRef::octet_string(), 5);
assert_eq!(der.id(), IdRef::octet_string());
assert_eq!(der.length(), Length::Definite(5));
assert_eq!(der.contents().len(), 5);
der.mut_contents().as_mut().copy_from_slice(&[1, 2, 3, 4, 5]);
assert_eq!(der.contents().as_ref(), &[1, 2, 3, 4, 5]);
sourcepub fn parse<R>(readable: &mut R) -> Result<Self, Error>
pub fn parse<R>(readable: &mut R) -> Result<Self, Error>
Parses readable
starting with DER octets and builds a new instance.
This function ignores extra octet(s) at the end of bytes
if any.
On error, the state of readable
is unspecified;
otherwise, readable
is advanced to the end of the DER octets.
§Performance
This function is not so efficient compared with DerRef::parse
.
If you have a slice of serialized DER, use DerRef::parse
and then call Der::from
instead.
§Warnings
ASN.1 does not allow some universal identifiers 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, DerRef};
// Serialize DER of '10' as Integer.
let der = Der::from(10_i32);
let mut serialized = Vec::from(der.as_ref() as &[u8]);
// Deserialize.
let deserialized = Der::parse(&mut &serialized[..]).unwrap();
assert_eq!(der, deserialized);
// Extra octets at the end does not affect the result.
serialized.push(0x00);
serialized.push(0x01);
let deserialized = Der::parse(&mut &serialized[..]).unwrap();
assert_eq!(der, deserialized);
// We can access to the inner slice of `serialized`.
// We can use `DerRef::parse` instead of this function.
// (`DerRef::parse` is more efficient than this function.)
let deserialized: Der = DerRef::parse(&mut &serialized[..]).unwrap().into();
assert_eq!(der, deserialized);
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’, use parse
instead.
§Safety
The behaviour is undefined if bytes
is not formatted as a DER.
§Examples
use bsn1::Der;
let der = Der::from(10_i32);
let serialized: &[u8] = der.as_ref();
let deserialized = unsafe { Der::from_bytes_unchecked(&serialized) };
assert_eq!(der, deserialized);
sourcepub unsafe fn from_vec_unchecked(bytes: Vec<u8>) -> Self
pub unsafe fn from_vec_unchecked(bytes: Vec<u8>) -> Self
Builds a new instance holding bytes
without any check.
bytes
must not includ any extra octet.
If it is not sure whether bytes
are valid octets as an ‘DER’, use parse
instead.
§Safety
The behavior is undefined if bytes
is not formatted as a DER.
§Examples
use bsn1::Der;
let der = Der::from(5_i32);
let serialized = der.clone().into_vec();
let deserialized = unsafe { Der::from_vec_unchecked(serialized) };
assert_eq!(der, deserialized);
sourcepub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Self
pub fn from_id_iterator<I>(id: &IdRef, contents: I) -> Self
Creates a new instance containing concatenated contents
.
§Warnings
ASN.1 does not allow some universal identifiers 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
represents constructed ‘Octet String.’
§Panics
Panics if the total length of the return value exceeds isize::MAX
.
§Examples
use bsn1::{ContentsRef, Der, IdRef};
// Build an sequence DER containing 2 other DERs.
let contents0 = vec![Der::from("foo"), Der::from(29_i32)];
let der0 = Der::from_id_iterator(IdRef::sequence(), contents0.iter());
let mut contents1: Vec<u8> = Der::from("foo").into_vec();
contents1.extend_from_slice(&Der::from(29_i32).into_vec());
let der1 = Der::new(IdRef::sequence(), <&ContentsRef>::from(&contents1[..]));
assert_eq!(der0, der1);
// Build an utf8-string DER using function `from_id_iterator()`.
let contents = vec!["Foo", "Bar"];
let der = Der::from_id_iterator(
IdRef::utf8_string(), contents.iter().map(|s| s.as_bytes()));
assert_eq!(der, Der::from("FooBar"));
sourcepub fn into_vec(self) -> Vec<u8>
pub fn into_vec(self) -> Vec<u8>
Consumes self
, returning Vec
.
§Examples
use bsn1::Der;
let der = Der::from("foo");
let v = der.clone().into_vec();
assert_eq!(der.as_ref() as &[u8], &v);
sourcepub fn push(&mut self, byte: u8)
pub fn push(&mut self, byte: u8)
Appends byte
to the end of the ‘contents octets’.
Note that this method may shift the ‘contents octets’,
and the performance is O(n)
where n
is the byte count of ‘contents octets’
in the worst-case,
because the byte count of ‘length octets’ may change.
(DER is composed of ‘identifier octets’, ‘length octets’ and ‘contents octets’
in this order.)
§Examples
use bsn1::{Der, IdRef, Length};
let bytes: Vec<u8> = (0..10).collect();
let mut der = Der::from(&bytes[..9]);
der.push(bytes[9]);
assert_eq!(der.id(), IdRef::octet_string());
assert_eq!(der.length(), Length::Definite(bytes.len()));
assert_eq!(der.contents().as_ref(), &bytes[..]);
sourcepub fn extend_from_slice<T>(&mut self, bytes: &T)
pub fn extend_from_slice<T>(&mut self, bytes: &T)
Appends bytes
to the end of the ‘contents octets’.
Note that this method may shift the ‘contents octets’,
and the performance is O(n)
where n
is the byte count of ‘contents octets’
in the worst-case,
because the byte count of ‘length octets’ may change.
(DER is composed of ‘identifier octets’, ‘length octets’ and ‘contents octets’
in this order.)
§Examples
use bsn1::{Der, IdRef, Length};
let bytes: Vec<u8> = (0..10).collect();
let mut der = Der::from(&bytes[..5]);
der.extend_from_slice(&bytes[5..]);
assert_eq!(der.id(), IdRef::octet_string());
assert_eq!(der.length(), Length::Definite(bytes.len()));
assert_eq!(der.contents().as_ref(), &bytes[..]);
sourcepub fn truncate(&mut self, new_length: usize)
pub fn truncate(&mut self, new_length: usize)
Enshorten the contents
, keeping the first new_length
and discarding the rest
if new_length
is less than the length of the current ‘contents octets’;
otherwise, does nothing.
Note that this method may shift the ‘contents octets’,
and the performance is O(n)
where n
is the byte count of ‘contents octets’
in the worst-case,
because the byte count of ‘length octets’ may change.
(DER is composed of ‘identifier octets’, ‘length octets’ and ‘contents octets’
in this order.)
§Warnings
new_length
specifies the length of the ‘contents octets’ after this method returns.
The total length of self
will be greater than new_length
.
§Examples
use bsn1::{Der, IdRef, Length};
// Create a DER of '0..=255' as Octet String.
let bytes: Vec<u8> = (0..=255).collect();
let mut der = Der::from(&bytes[..]);
// Truncate the length
der.truncate(100);
assert_eq!(der.id(), IdRef::octet_string()); // The identifier is not changed.
assert_eq!(der.length(), Length::Definite(100)); // The length is changed.
assert_eq!(der.contents().as_ref(), &bytes[..100]); // The contents is changed.
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::{Der, DerRef, IdRef};
let der = Der::from(4_i32);
let der: &DerRef = der.as_ref();
assert_eq!(IdRef::integer(), der.id());
sourcepub fn mut_id(&mut self) -> &mut IdRef
pub fn mut_id(&mut self) -> &mut IdRef
Returns a mutable reference to the IdRef
of self
.
§Examples
use bsn1::{ClassTag, Der, DerRef, PCTag};
let mut der = Der::from(4_i32);
let der: &mut DerRef = der.as_mut();
assert_eq!(der.id().class(), ClassTag::Universal);
der.mut_id().set_class(ClassTag::Private);
assert_eq!(der.id().class(), ClassTag::Private);
assert_eq!(der.id().pc(), PCTag::Primitive);
der.mut_id().set_pc(PCTag::Constructed);
assert_eq!(der.id().pc(), PCTag::Constructed);
sourcepub fn length(&self) -> Length
pub fn length(&self) -> Length
Returns Length
of self
.
Note that DER does not allow indefinite Length.
The return value is always Length::Definite
.
§Warnings
Length
stands for the length octets in DER: i.e. the length of the ‘contents octets’.
The total byte count of the DER is greater than the value.
§Examples
use bsn1::{Der, DerRef, Length};
let der = Der::from("Foo");
let der: &DerRef = der.as_ref();
assert_eq!(Length::Definite("Foo".len()), der.length());
sourcepub fn contents(&self) -> &ContentsRef
pub fn contents(&self) -> &ContentsRef
Returns a reference to the contents octets of self
.
§Examples
use bsn1::{Der, DerRef};
let der = Der::from("Foo");
let der: &DerRef = der.as_ref();
assert_eq!(der.contents().as_ref(), "Foo".as_bytes());
sourcepub fn mut_contents(&mut self) -> &mut ContentsRef
pub fn mut_contents(&mut self) -> &mut ContentsRef
Returns a mutable reference to the ‘contents octets’ of self
.
§Examples
use bsn1::{Der, DerRef};
let mut der = Der::from("Foo");
let der: &mut DerRef = der.as_mut();
assert_eq!(der.contents().as_ref(), "Foo".as_bytes());
der.mut_contents()[0] = 'B' as u8;
assert_eq!(der.contents().as_ref(), "Boo".as_bytes());
sourcepub fn disassemble(&self) -> (&IdRef, Length, &ContentsRef)
pub fn disassemble(&self) -> (&IdRef, Length, &ContentsRef)
Returns references to IdRef
, Length
, and ContentsRef
of self
.
§Examples
use bsn1::{Der, DerRef, IdRef};
let der = Der::from("Foo");
let der: &DerRef = der.as_ref();
let (id, length, contents) = der.disassemble();
assert_eq!(id, IdRef::utf8_string());
assert_eq!(length.definite().unwrap(), "Foo".len());
assert_eq!(contents.as_ref(), "Foo".as_bytes());
sourcepub fn disassemble_mut(&mut self) -> (&mut IdRef, Length, &mut ContentsRef)
pub fn disassemble_mut(&mut self) -> (&mut IdRef, Length, &mut ContentsRef)
Returns mutable references to IdRef
, Length
, and ContentsRef
of self
.
§Examples
use bsn1::{Der, DerRef, IdRef};
let mut der = Der::from("Foo");
let der: &mut DerRef = der.as_mut();
let (id, length, contents) = der.disassemble_mut();
assert_eq!(id, IdRef::utf8_string());
assert_eq!(length.definite().unwrap(), "Foo".len());
assert_eq!(contents.as_ref(), "Foo".as_bytes());
contents[0] = 'B' as u8;
assert_eq!(der.contents().as_ref() as &[u8], "Boo".as_bytes());