Struct yasna::BERReader

source ·
pub struct BERReader<'a, 'b>
where 'a: 'b,
{ /* private fields */ }
Expand description

A reader object for BER/DER-encoded ASN.1 data.

The two main sources of BERReaderSeq are:

§Examples

use yasna;
let data = &[2, 1, 10];
let asn = yasna::parse_der(data, |reader| {
    reader.read_i64()
}).unwrap();
assert_eq!(asn, 10);

Implementations§

source§

impl<'a, 'b> BERReader<'a, 'b>

source

pub fn mode(&self) -> BERMode

Tells which format we are parsing, BER or DER.

source

pub fn read_bool(self) -> ASN1Result<bool>

Reads an ASN.1 BOOLEAN value as bool.

§Examples
use yasna;
let data = &[1, 1, 255];
let asn = yasna::parse_der(data, |reader| {
    reader.read_bool()
}).unwrap();
assert_eq!(asn, true);
source

pub fn read_enum(self) -> ASN1Result<i64>

Reads an ASN.1 ENUMERATED value as i64.

§Examples
use yasna;
let data = &[10, 1, 13];
let asn = yasna::parse_der(data, |reader| {
    reader.read_enum()
}).unwrap();
assert_eq!(asn, 13);
§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_i64(self) -> ASN1Result<i64>

Reads an ASN.1 INTEGER value as i64.

§Examples
use yasna;
let data = &[2, 4, 73, 150, 2, 210];
let asn = yasna::parse_der(data, |reader| {
    reader.read_i64()
}).unwrap();
assert_eq!(asn, 1234567890);
§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_u64(self) -> ASN1Result<u64>

Reads an ASN.1 INTEGER value as u64.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_i32(self) -> ASN1Result<i32>

Reads an ASN.1 INTEGER value as i32.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_u32(self) -> ASN1Result<u32>

Reads an ASN.1 INTEGER value as u32.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_i16(self) -> ASN1Result<i16>

Reads an ASN.1 INTEGER value as i16.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_u16(self) -> ASN1Result<u16>

Reads an ASN.1 INTEGER value as u16.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_i8(self) -> ASN1Result<i8>

Reads an ASN.1 INTEGER value as i8.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_u8(self) -> ASN1Result<u8>

Reads an ASN.1 INTEGER value as u8.

§Errors

Except parse errors, it can raise integer overflow errors.

source

pub fn read_bigint(self) -> ASN1Result<BigInt>

Reads an ASN.1 INTEGER value as BigInt.

§Examples
use yasna;
use num_bigint::BigInt;
let data = &[2, 4, 73, 150, 2, 210];
let asn = yasna::parse_der(data, |reader| {
    reader.read_bigint()
}).unwrap();
assert_eq!(&asn, &BigInt::parse_bytes(b"1234567890", 10).unwrap());
§Features

This method is enabled by num feature.

[dependencies]
yasna = { version = "*", features = ["num"] }
source

pub fn read_bigint_bytes(self) -> ASN1Result<(Vec<u8>, bool)>

Reads an ASN.1 INTEGER value as Vec<u8> and a sign bit.

The number given is in big endian byte ordering, and in two’s complement.

The sign bit is true if the number is positive, and false if it is negative.

§Examples
use yasna;
let data = &[2, 4, 73, 150, 2, 210];
let (bytes, nonnegative) = yasna::parse_der(data, |reader| {
    reader.read_bigint_bytes()
}).unwrap();
assert_eq!(&bytes, &[73, 150, 2, 210]);
assert_eq!(nonnegative, true);
source

pub fn read_biguint(self) -> ASN1Result<BigUint>

Reads an ASN.1 INTEGER value as BigUint.

§Errors

Except parse errors, it can raise integer overflow errors.

§Features

This method is enabled by num feature.

[dependencies]
yasna = { version = "*", features = ["num"] }
source

pub fn read_bitvec(self) -> ASN1Result<BitVec>

Reads an ASN.1 BITSTRING value as BitVec.

§Examples
use yasna;
use bit_vec::BitVec;
let data = &[3, 5, 3, 206, 213, 116, 24];
let asn = yasna::parse_der(data, |reader| {
    reader.read_bitvec()
}).unwrap();
assert_eq!(
    asn.into_iter().map(|b| b as usize).collect::<Vec<_>>(),
    vec![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]);
§Features

This method is enabled by bit-vec feature.

[dependencies]
yasna = { version = "*", features = ["bit-vec"] }
source

pub fn read_bitvec_bytes(self) -> ASN1Result<(Vec<u8>, usize)>

Reads an ASN.1 BITSTRING value as (Vec<u8>, usize).

§Examples
use yasna;
let data = &[3, 4, 6, 117, 13, 64];
let asn = yasna::parse_der(data, |reader| {
    reader.read_bitvec_bytes()
}).unwrap();
assert_eq!(asn, (vec![117, 13, 64], 18));
§Features

This method is enabled by bit-vec feature.

[dependencies]
yasna = { version = "*", features = ["bit-vec"] }
source

pub fn read_bytes(self) -> ASN1Result<Vec<u8>>

Reads an ASN.1 OCTETSTRING value as Vec<u8>.

§Examples
use yasna;
let data = &[36, 128, 4, 2, 72, 101, 4, 4, 108, 108, 111, 33, 0, 0];
let asn = yasna::parse_ber(data, |reader| {
    reader.read_bytes()
}).unwrap();
assert_eq!(&asn, b"Hello!");
source

pub fn read_null(self) -> ASN1Result<()>

Reads the ASN.1 NULL value.

§Examples
use yasna;
let data = &[5, 0];
let asn = yasna::parse_der(data, |reader| {
    reader.read_null()
}).unwrap();
assert_eq!(asn, ());
source

pub fn read_oid(self) -> ASN1Result<ObjectIdentifier>

Reads an ASN.1 object identifier.

§Examples
use yasna;
let data = &[6, 8, 42, 134, 72, 134, 247, 13, 1, 1];
let asn = yasna::parse_der(data, |reader| {
    reader.read_oid()
}).unwrap();
assert_eq!(&*asn.components(), &[1, 2, 840, 113549, 1, 1]);
source

pub fn read_utf8string(self) -> ASN1Result<String>

Reads an ASN.1 UTF8String.

§Examples
use yasna;
let data = &[
    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];
let asn = yasna::parse_der(data, |reader| {
    reader.read_utf8string()
}).unwrap();
assert_eq!(&asn, "gnaw ροκανίζω 𪘂る");
source

pub fn read_sequence<T, F>(self, callback: F) -> ASN1Result<T>
where F: for<'c> FnOnce(&mut BERReaderSeq<'a, 'c>) -> ASN1Result<T>,

Reads an ASN.1 SEQUENCE value.

This function uses the loan pattern: callback is called back with a BERReaderSeq, from which the contents of the SEQUENCE is read.

§Examples
use yasna;
let data = &[48, 6, 2, 1, 10, 1, 1, 255];
let asn = yasna::parse_der(data, |reader| {
    reader.read_sequence(|reader| {
        let i = reader.next().read_i64()?;
        let b = reader.next().read_bool()?;
        return Ok((i, b));
    })
}).unwrap();
assert_eq!(asn, (10, true));
source

pub fn read_sequence_of<F>(self, callback: F) -> ASN1Result<()>
where F: for<'c> FnMut(BERReader<'a, 'c>) -> ASN1Result<()>,

Reads an ASN.1 SEQUENCE OF value.

This function uses the loan pattern: callback is called back with a BERReader, from which the contents of the SEQUENCE OF is read.

This function doesn’t return values. Instead, use mutable values to maintain read values. collect_set_of can be an alternative.

§Examples
use yasna;
let data = &[48, 7, 2, 1, 10, 2, 2, 255, 127];
let asn = yasna::parse_der(data, |reader| {
    let mut numbers = Vec::new();
    reader.read_sequence_of(|reader| {
        numbers.push(reader.read_i64()?);
        return Ok(());
    })?;
    return Ok(numbers);
}).unwrap();
assert_eq!(&asn, &[10, -129]);
source

pub fn collect_sequence_of<T, F>(self, callback: F) -> ASN1Result<Vec<T>>
where F: for<'c> FnMut(BERReader<'a, 'c>) -> ASN1Result<T>,

Collects an ASN.1 SEQUENCE OF value.

This function uses the loan pattern: callback is called back with a BERReader, from which the contents of the SEQUENCE OF is read.

If you don’t like Vec, you can use read_sequence_of instead.

§Examples
use yasna;
let data = &[48, 7, 2, 1, 10, 2, 2, 255, 127];
let asn = yasna::parse_der(data, |reader| {
    reader.collect_sequence_of(|reader| {
        reader.read_i64()
    })
}).unwrap();
assert_eq!(&asn, &[10, -129]);
source

pub fn read_set<T, F>(self, callback: F) -> ASN1Result<T>
where F: for<'c> FnOnce(&mut BERReaderSet<'a, 'c>) -> ASN1Result<T>,

Reads an ASN.1 SET value.

This function uses the loan pattern: callback is called back with a BERReaderSet, from which the contents of the SET are read.

For SET OF values, use read_set_of instead.

§Examples
use yasna;
use yasna::tags::{TAG_INTEGER,TAG_BOOLEAN};
let data = &[49, 6, 1, 1, 255, 2, 1, 10];
let asn = yasna::parse_der(data, |reader| {
    reader.read_set(|reader| {
        let i = reader.next(&[TAG_INTEGER])?.read_i64()?;
        let b = reader.next(&[TAG_BOOLEAN])?.read_bool()?;
        return Ok((i, b));
    })
}).unwrap();
assert_eq!(asn, (10, true));
source

pub fn read_set_of<F>(self, callback: F) -> ASN1Result<()>
where F: for<'c> FnMut(BERReader<'a, 'c>) -> ASN1Result<()>,

Reads an ASN.1 SET OF value.

This function uses the loan pattern: callback is called back with a BERReader, from which the contents of the SET OF are read.

This function doesn’t return values. Instead, use mutable values to maintain read values. collect_set_of can be an alternative.

This function doesn’t sort the elements. In DER, it is assumed that the elements occur in an order determined by DER encodings of them.

For SET values, use read_set instead.

§Examples
use yasna;
let data = &[49, 7, 2, 1, 10, 2, 2, 255, 127];
let asn = yasna::parse_der(data, |reader| {
    let mut numbers = Vec::new();
    reader.read_set_of(|reader| {
        numbers.push(reader.read_i64()?);
        return Ok(());
    })?;
    return Ok(numbers);
}).unwrap();
assert_eq!(asn, vec![10, -129]);
source

pub fn collect_set_of<T, F>(self, callback: F) -> ASN1Result<Vec<T>>
where F: for<'c> FnMut(BERReader<'a, 'c>) -> ASN1Result<T>,

Collects an ASN.1 SET OF value.

This function uses the loan pattern: callback is called back with a BERReader, from which the contents of the SET OF is read.

If you don’t like Vec, you can use read_set_of instead.

This function doesn’t sort the elements. In DER, it is assumed that the elements occur in an order determined by DER encodings of them.

§Examples
use yasna;
let data = &[49, 7, 2, 1, 10, 2, 2, 255, 127];
let asn = yasna::parse_der(data, |reader| {
    reader.collect_set_of(|reader| {
        reader.read_i64()
    })
}).unwrap();
assert_eq!(asn, vec![10, -129]);
source

pub fn read_numeric_string(self) -> ASN1Result<String>

Reads an ASN.1 NumericString.

§Examples
use yasna;
let data = &[18, 7, 49, 50, 56, 32, 50, 53, 54];
let asn = yasna::parse_der(data, |reader| {
    reader.read_numeric_string()
}).unwrap();
assert_eq!(&asn, "128 256");
source

pub fn read_printable_string(self) -> ASN1Result<String>

Reads an ASN.1 PrintableString.

§Examples
use yasna;
let data = &[19, 9, 67, 111, 46, 44, 32, 76, 116, 100, 46];
let asn = yasna::parse_der(data, |reader| {
    reader.read_printable_string()
}).unwrap();
assert_eq!(&asn, "Co., Ltd.");
source

pub fn read_ia5_string(self) -> ASN1Result<String>

Reads an ASN.1 IA5String.

§Examples
use yasna;
let data = &[22, 9, 0x41, 0x53, 0x43, 0x49, 0x49, 0x20, 0x70, 0x6C, 0x7A];
let asn = yasna::parse_der(data, |reader| {
    reader.read_ia5_string()
}).unwrap();
assert_eq!(&asn, "ASCII plz");
source

pub fn read_bmp_string(self) -> ASN1Result<String>

Reads an ASN.1 BMPString.

§Examples
use yasna;
let data = &[30, 14, 0x00, 0xA3, 0x03, 0xC0, 0x00, 0x20, 0x00, 0x71, 0x00, 0x75, 0x00, 0x75, 0x00, 0x78];
let asn = yasna::parse_der(data, |reader| {
    reader.read_bmp_string()
}).unwrap();
assert_eq!(&asn, "£π quux");
source

pub fn read_utctime(self) -> ASN1Result<UTCTime>

Reads an ASN.1 UTCTime.

§Examples
use yasna;
let data = &[
    23, 15, 56, 50, 48, 49, 48, 50, 48,
    55, 48, 48, 45, 48, 53, 48, 48];
let asn = yasna::parse_ber(data, |reader| {
    reader.read_utctime()
}).unwrap();
assert_eq!(asn.datetime().unix_timestamp(), 378820800);
§Features

This method is enabled by time feature.

[dependencies]
yasna = { version = "*", features = ["time"] }
source

pub fn read_generalized_time(self) -> ASN1Result<GeneralizedTime>

Reads an ASN.1 GeneralizedTime.

§Examples
use yasna;
let data = &[
    24, 17, 49, 57, 56, 53, 49, 49, 48, 54,
    50, 49, 46, 49, 52, 49, 53, 57, 90];
let asn = yasna::parse_ber(data, |reader| {
    reader.read_generalized_time()
}).unwrap();
assert_eq!(asn.datetime().unix_timestamp(), 500159309);
§Features

This method is enabled by time feature.

[dependencies]
yasna = { version = "*", features = ["time"] }
source

pub fn read_visible_string(self) -> ASN1Result<String>

Reads an ASN.1 VisibleString.

§Examples
use yasna;
let data = &[26, 3, 72, 105, 33];
let asn = yasna::parse_der(data, |reader| {
    reader.read_visible_string()
}).unwrap();
assert_eq!(&asn, "Hi!");
source

pub fn read_tagged<T, F>(self, tag: Tag, callback: F) -> ASN1Result<T>
where F: for<'c> FnOnce(BERReader<'a, 'c>) -> ASN1Result<T>,

Reads a (explicitly) tagged value.

§Examples
use yasna::{self,Tag};
let data = &[163, 3, 2, 1, 10];
let asn = yasna::parse_der(data, |reader| {
    reader.read_tagged(Tag::context(3), |reader| {
        reader.read_i64()
    })
}).unwrap();
assert_eq!(asn, 10);
source

pub fn read_tagged_implicit<T, F>(self, tag: Tag, callback: F) -> ASN1Result<T>
where F: for<'c> FnOnce(BERReader<'a, 'c>) -> ASN1Result<T>,

Reads an implicitly tagged value.

§Examples
use yasna::{self,Tag};
let data = &[131, 1, 10];
let asn = yasna::parse_der(data, |reader| {
    reader.read_tagged_implicit(Tag::context(3), |reader| {
        reader.read_i64()
    })
}).unwrap();
assert_eq!(asn, 10);
source

pub fn lookahead_tag(&self) -> ASN1Result<Tag>

Lookaheads the tag in the next value. Used to parse CHOICE values.

§Examples
use yasna;
use yasna::tags::*;
let data = &[48, 5, 2, 1, 10, 5, 0];
let asn = yasna::parse_der(data, |reader| {
    reader.collect_sequence_of(|reader| {
        let tag = reader.lookahead_tag()?;
        let choice;
        if tag == TAG_INTEGER {
            choice = Some(reader.read_i64()?);
        } else {
            reader.read_null()?;
            choice = None;
        }
        return Ok(choice);
    })
}).unwrap();
assert_eq!(&asn, &[Some(10), None]);
source

pub fn read_with_buffer<T, F>(self, callback: F) -> ASN1Result<(T, &'a [u8])>
where F: for<'c> FnOnce(BERReader<'a, 'c>) -> ASN1Result<T>,

source

pub fn read_tagged_der(self) -> ASN1Result<TaggedDerValue>

Read an arbitrary (tag, value) pair as a TaggedDerValue. The length is not included in the returned payload. If the payload has indefinite-length encoding, the EOC bytes are included in the returned payload.

§Examples
use yasna;
use yasna::models::TaggedDerValue;
use yasna::tags::TAG_OCTETSTRING;
let data = b"\x04\x06Hello!";
let res = yasna::parse_der(data, |reader| reader.read_tagged_der()).unwrap();
assert_eq!(res, TaggedDerValue::from_tag_and_bytes(TAG_OCTETSTRING, b"Hello!".to_vec()));
source

pub fn read_der(self) -> ASN1Result<Vec<u8>>

Reads a DER object as raw bytes. Tag and length are included in the returned buffer. For indefinite length encoding, EOC bytes are included in the returned buffer as well.

§Examples
use yasna;
let data = b"\x04\x06Hello!";
let res = yasna::parse_der(data, |reader| reader.read_der()).unwrap();
assert_eq!(res, data);

Trait Implementations§

source§

impl<'a, 'b> Debug for BERReader<'a, 'b>
where 'a: 'b,

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, 'b> Freeze for BERReader<'a, 'b>

§

impl<'a, 'b> RefUnwindSafe for BERReader<'a, 'b>

§

impl<'a, 'b> Send for BERReader<'a, 'b>

§

impl<'a, 'b> Sync for BERReader<'a, 'b>

§

impl<'a, 'b> Unpin for BERReader<'a, 'b>

§

impl<'a, 'b> !UnwindSafe for BERReader<'a, 'b>

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.