pub struct DERWriter<'a> { /* private fields */ }
Expand description
A writer object that accepts an ASN.1 value.
The two main sources of DERWriterSeq
are:
- The
construct_der
function, the starting point of DER serialization. - The
next
method ofDERWriterSeq
.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_i64(10)
});
assert_eq!(der, vec![2, 1, 10]);
Implementations§
source§impl<'a> DERWriter<'a>
impl<'a> DERWriter<'a>
sourcepub fn write_bool(self, val: bool)
pub fn write_bool(self, val: bool)
Writes bool
as an ASN.1 BOOLEAN value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_bool(true)
});
assert_eq!(der, vec![1, 1, 255]);
sourcepub fn write_enum(self, val: i64)
pub fn write_enum(self, val: i64)
Writes i64
as an ASN.1 ENUMERATED value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_enum(2)
});
assert_eq!(der, vec![10, 1, 2]);
sourcepub fn write_i64(self, val: i64)
pub fn write_i64(self, val: i64)
Writes i64
as an ASN.1 INTEGER value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_i64(1234567890)
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
sourcepub fn write_bigint(self, val: &BigInt)
pub fn write_bigint(self, val: &BigInt)
Writes BigInt
as an ASN.1 INTEGER value.
§Examples
use yasna;
use num_bigint::BigInt;
let der = yasna::construct_der(|writer| {
writer.write_bigint(
&BigInt::parse_bytes(b"1234567890", 10).unwrap())
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
§Features
This method is enabled by num
feature.
[dependencies]
yasna = { version = "*", features = ["num-bigint"] }
sourcepub fn write_bigint_bytes(self, bytes: &[u8], positive: bool)
pub fn write_bigint_bytes(self, bytes: &[u8], positive: bool)
Writes &[u8]
and bool
as an ASN.1 INTEGER value.
The first parameter encodes the bytes of the integer, in big-endian byte ordering and two’s complement format. The second parameter encodes the sign, and is true if the number is non-negative, and false if it is negative. Zero is encoded by passing an empty slice.
The number is expected to be in two’s complement format, so for example
1
is encoded as [1]
and -1
as [255]
.
You don’t have to worry about leading bits, meaning that if the leading bit of a positive number is not zero, a leading zero byte will be encoded by the function. Similarly, if the leading bit of a negative number is not one, a leading byte will be added by the function as well.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
// Encodes 1234567890
writer.write_bigint_bytes(&[73, 150, 2, 210], true)
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
sourcepub fn write_biguint(self, val: &BigUint)
pub fn write_biguint(self, val: &BigUint)
Writes BigUint
as an ASN.1 INTEGER value.
§Examples
use yasna;
use num_bigint::BigUint;
let der = yasna::construct_der(|writer| {
writer.write_biguint(
&BigUint::parse_bytes(b"1234567890", 10).unwrap())
});
assert_eq!(der, vec![2, 4, 73, 150, 2, 210]);
§Features
This method is enabled by num
feature.
[dependencies]
yasna = { version = "*", features = ["num-bigint"] }
sourcepub fn write_bitvec(self, bitvec: &BitVec)
pub fn write_bitvec(self, bitvec: &BitVec)
Writes BitVec
as an ASN.1 BITSTRING value.
§Examples
use yasna;
use bit_vec::BitVec;
let der = yasna::construct_der(|writer| {
writer.write_bitvec(&
[1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1,
0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 1]
.iter().map(|&i| i != 0).collect())
});
assert_eq!(&der, &[3, 5, 3, 206, 213, 116, 24]);
§Features
This method is enabled by bit-vec
feature.
[dependencies]
yasna = { version = "*", features = ["bit-vec"] }
sourcepub fn write_bitvec_bytes(self, bytes: &[u8], len: usize)
pub fn write_bitvec_bytes(self, bytes: &[u8], len: usize)
Writes &[u8]
and usize
as an ASN.1 BITSTRING value.
The len
parameter represents the number of bits to be encoded.
This function is similar to write_bitvec
, with to_bytes
applied to
the bitvec, but is available even if the bit-vec
feature is disabled.
§Examples
use yasna;
let der_1 = yasna::construct_der(|writer| {
writer.write_bitvec_bytes(&[117, 13, 64], 18)
});
let der_2 = yasna::construct_der(|writer| {
writer.write_bitvec_bytes(&[117, 13, 65], 18)
});
assert_eq!(&der_1, &[3, 4, 6, 117, 13, 64]);
assert_eq!(&der_2, &[3, 4, 6, 117, 13, 64]);
sourcepub fn write_bytes(self, bytes: &[u8])
pub fn write_bytes(self, bytes: &[u8])
Writes &[u8]
as an ASN.1 OCTETSTRING value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_bytes(b"Hello!")
});
assert_eq!(der, vec![4, 6, 72, 101, 108, 108, 111, 33]);
sourcepub fn write_utf8_string(self, string: &str)
pub fn write_utf8_string(self, string: &str)
Writes &str
as an ASN.1 UTF8String value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_utf8_string("Hello!")
});
assert_eq!(der, vec![12, 6, 72, 101, 108, 108, 111, 33]);
sourcepub fn write_ia5_string(self, string: &str)
pub fn write_ia5_string(self, string: &str)
Writes &str
as an ASN.1 IA5String value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_ia5_string("Hello!")
});
assert_eq!(der, vec![22, 6, 72, 101, 108, 108, 111, 33]);
sourcepub fn write_bmp_string(self, string: &str)
pub fn write_bmp_string(self, string: &str)
Writes &str
as an ASN.1 BMPString value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_bmp_string("❤πü2?")
});
assert_eq!(der, vec![30, 10, 39, 100, 3, 192, 0, 252, 0, 50, 0, 63]);
sourcepub fn write_null(self)
pub fn write_null(self)
Writes the ASN.1 NULL value.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_null()
});
assert_eq!(der, vec![5, 0]);
sourcepub fn write_oid(self, oid: &ObjectIdentifier)
pub fn write_oid(self, oid: &ObjectIdentifier)
Writes an ASN.1 object identifier.
§Examples
use yasna;
use yasna::models::ObjectIdentifier;
let der = yasna::construct_der(|writer| {
writer.write_oid(&ObjectIdentifier::from_slice(
&[1, 2, 840, 113549, 1, 1]))
});
assert_eq!(&der, &[6, 8, 42, 134, 72, 134, 247, 13, 1, 1]);
§Panics
It panics when the OID cannot be canonically encoded in BER.
sourcepub fn write_utf8string(self, string: &str)
pub fn write_utf8string(self, string: &str)
Writes an ASN.1 UTF8String.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_utf8string("gnaw ροκανίζω 𪘂る")
});
assert_eq!(&der, &[
12, 29, 103, 110, 97, 119, 32, 207, 129, 206, 191, 206,
186, 206, 177, 206, 189, 206, 175, 206, 182, 207,
137, 32, 240, 170, 152, 130, 227, 130, 139]);
sourcepub fn write_sequence<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSeq<'_>) -> T,
pub fn write_sequence<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSeq<'_>) -> T,
Writes ASN.1 SEQUENCE.
This function uses the loan pattern: callback
is called back with
a DERWriterSeq
, to which the contents of the
SEQUENCE is written.
This is equivalent to write_sequence_of
in behavior.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_sequence(|writer| {
writer.next().write_i64(10);
writer.next().write_bool(true);
})
});
assert_eq!(der, vec![48, 6, 2, 1, 10, 1, 1, 255]);
sourcepub fn write_sequence_of<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSeq<'_>) -> T,
pub fn write_sequence_of<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSeq<'_>) -> T,
Writes ASN.1 SEQUENCE OF.
This function uses the loan pattern: callback
is called back with
a DERWriterSeq
, to which the contents of the
SEQUENCE OF are written.
This is equivalent to write_sequence
in
behavior.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_sequence_of(|writer| {
for &i in &[10, -129] {
writer.next().write_i64(i);
}
})
});
assert_eq!(der, vec![48, 7, 2, 1, 10, 2, 2, 255, 127]);
sourcepub fn write_set<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSet<'_>) -> T,
pub fn write_set<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSet<'_>) -> T,
Writes ASN.1 SET.
This function uses the loan pattern: callback
is called back with
a DERWriterSet
, to which the contents of the
SET are written.
For SET OF values, use write_set_of
instead.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_set(|writer| {
writer.next().write_i64(10);
writer.next().write_bool(true);
})
});
assert_eq!(der, vec![49, 6, 1, 1, 255, 2, 1, 10]);
sourcepub fn write_set_of<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSet<'_>) -> T,
pub fn write_set_of<T, F>(self, callback: F) -> Twhere
F: FnOnce(&mut DERWriterSet<'_>) -> T,
Writes ASN.1 SET OF.
This function uses the loan pattern: callback
is called back with
a DERWriterSet
, to which the contents of the
SET OF are written.
For SET values, use write_set
instead.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_set_of(|writer| {
for &i in &[10, -129] {
writer.next().write_i64(i);
}
})
});
assert_eq!(der, vec![49, 7, 2, 1, 10, 2, 2, 255, 127]);
sourcepub fn write_numeric_string(self, string: &str)
pub fn write_numeric_string(self, string: &str)
Writes an ASN.1 NumericString.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_numeric_string("128 256")
});
assert_eq!(&der, &[18, 7, 49, 50, 56, 32, 50, 53, 54]);
sourcepub fn write_printable_string(self, string: &str)
pub fn write_printable_string(self, string: &str)
Writes an ASN.1 PrintableString.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_printable_string("Co., Ltd.")
});
assert_eq!(&der, &[19, 9, 67, 111, 46, 44, 32, 76, 116, 100, 46]);
sourcepub fn write_utctime(self, datetime: &UTCTime)
pub fn write_utctime(self, datetime: &UTCTime)
Writes an ASN.1 UTCTime.
§Examples
use yasna;
use yasna::models::UTCTime;
use time::OffsetDateTime;
let der = yasna::construct_der(|writer| {
writer.write_utctime(
&UTCTime::from_datetime(
OffsetDateTime::from_unix_timestamp(378820800).unwrap()))
});
assert_eq!(&der, &[
23, 13, 56, 50, 48, 49, 48, 50, 49, 50, 48, 48, 48, 48, 90]);
§Features
This method is enabled by time
feature.
[dependencies]
yasna = { version = "*", features = ["time"] }
sourcepub fn write_generalized_time(self, datetime: &GeneralizedTime)
pub fn write_generalized_time(self, datetime: &GeneralizedTime)
Writes an ASN.1 GeneralizedTime.
§Examples
use yasna;
use yasna::models::GeneralizedTime;
use time::OffsetDateTime;
let der = yasna::construct_der(|writer| {
writer.write_generalized_time(
&GeneralizedTime::from_datetime(
OffsetDateTime::from_unix_timestamp_nanos(
500_159_309_724_000_000).unwrap()))
});
assert_eq!(&der, &[
24, 19, 49, 57, 56, 53, 49, 49, 48, 54, 50,
49, 48, 56, 50, 57, 46, 55, 50, 52, 90]);
§Features
This method is enabled by time
feature.
[dependencies]
yasna = { version = "*", features = ["time"] }
sourcepub fn write_visible_string(self, string: &str)
pub fn write_visible_string(self, string: &str)
Writes an ASN.1 VisibleString.
§Examples
use yasna;
let der = yasna::construct_der(|writer| {
writer.write_visible_string("Hi!")
});
assert_eq!(&der, &[26, 3, 72, 105, 33]);
sourcepub fn write_tagged<T, F>(self, tag: Tag, callback: F) -> T
pub fn write_tagged<T, F>(self, tag: Tag, callback: F) -> T
Writes an (explicitly) tagged value.
§Examples
use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
writer.write_tagged(Tag::context(3), |writer| {
writer.write_i64(10)
})
});
assert_eq!(der, vec![163, 3, 2, 1, 10]);
Note: you can achieve the same using
write_tagged_implicit
:
use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
writer.write_tagged_implicit(Tag::context(3), |writer| {
writer.write_sequence(|writer| {
let writer = writer.next();
writer.write_i64(10)
})
})
});
assert_eq!(der, vec![163, 3, 2, 1, 10]);
sourcepub fn write_tagged_implicit<T, F>(self, tag: Tag, callback: F) -> T
pub fn write_tagged_implicit<T, F>(self, tag: Tag, callback: F) -> T
Writes an implicitly tagged value.
§Examples
use yasna::{self,Tag};
let der = yasna::construct_der(|writer| {
writer.write_tagged_implicit(Tag::context(3), |writer| {
writer.write_i64(10)
})
});
assert_eq!(der, vec![131, 1, 10]);
sourcepub fn write_tagged_der(self, der: &TaggedDerValue)
pub fn write_tagged_der(self, der: &TaggedDerValue)
Writes the arbitrary tagged DER value in der
.
§Examples
use yasna;
use yasna::models::TaggedDerValue;
use yasna::tags::TAG_OCTETSTRING;
let tagged_der_value = TaggedDerValue::from_tag_and_bytes(TAG_OCTETSTRING, b"Hello!".to_vec());
let der1 = yasna::construct_der(|writer| {
writer.write_tagged_der(&tagged_der_value)
});
let der2 = yasna::construct_der(|writer| {
writer.write_bytes(b"Hello!")
});
assert_eq!(der1, der2);
sourcepub fn write_der(self, der: &[u8])
pub fn write_der(self, der: &[u8])
Writes &[u8]
into the DER output buffer directly. Properly encoded tag
and length must be included at the start of the passed buffer.
§Examples
use yasna;
let raw_der = yasna::construct_der(|writer| {
writer.write_der(b"\x04\x06Hello!")
});
let der = yasna::construct_der(|writer| {
writer.write_bytes(b"Hello!")
});
assert_eq!(raw_der, der);