Struct yasna::DERWriter

source ·
pub struct DERWriter<'a> { /* private fields */ }
Expand description

A writer object that accepts an ASN.1 value.

The two main sources of DERWriterSeq are:

§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>

source

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]);
source

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]);
source

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]);
source

pub fn write_u64(self, val: u64)

Writes u64 as an ASN.1 INTEGER value.

source

pub fn write_i32(self, val: i32)

Writes i32 as an ASN.1 INTEGER value.

source

pub fn write_u32(self, val: u32)

Writes u32 as an ASN.1 INTEGER value.

source

pub fn write_i16(self, val: i16)

Writes i16 as an ASN.1 INTEGER value.

source

pub fn write_u16(self, val: u16)

Writes u16 as an ASN.1 INTEGER value.

source

pub fn write_i8(self, val: i8)

Writes i8 as an ASN.1 INTEGER value.

source

pub fn write_u8(self, val: u8)

Writes u8 as an ASN.1 INTEGER value.

source

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"] }
source

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]);
source

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"] }
source

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"] }
source

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]);
source

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]);
source

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]);
source

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]);
source

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]);
source

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]);
source

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.

source

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]);
source

pub fn write_sequence<T, F>(self, callback: F) -> T
where 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]);
source

pub fn write_sequence_of<T, F>(self, callback: F) -> T
where 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]);
source

pub fn write_set<T, F>(self, callback: F) -> T
where 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]);
source

pub fn write_set_of<T, F>(self, callback: F) -> T
where 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]);
source

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]);
source

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]);
source

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"] }
source

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"] }
source

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]);
source

pub fn write_tagged<T, F>(self, tag: Tag, callback: F) -> T
where F: FnOnce(DERWriter<'_>) -> 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]);
source

pub fn write_tagged_implicit<T, F>(self, tag: Tag, callback: F) -> T
where F: FnOnce(DERWriter<'_>) -> 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]);
source

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);
source

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);

Trait Implementations§

source§

impl<'a> Debug for DERWriter<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for DERWriter<'a>

§

impl<'a> RefUnwindSafe for DERWriter<'a>

§

impl<'a> Send for DERWriter<'a>

§

impl<'a> Sync for DERWriter<'a>

§

impl<'a> Unpin for DERWriter<'a>

§

impl<'a> !UnwindSafe for DERWriter<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.