rusticata / asn1-rs

Parsers/Encoders for ASN.1 BER/DER data
Apache License 2.0
10 stars 14 forks source link

Large Tags are not encoded properly #43

Closed justtryingthingsout closed 3 months ago

justtryingthingsout commented 3 months ago

Hi, I have an issue regarding the encoding of large tags, particularly those that exceed the value of 0x80, where after serialization and deserialization, the tags don't match to the original.

The following code can be used to show this:

extern crate asn1_rs;
use asn1_rs::{Any, Error, FromDer, Implicit, Integer, Tag, ToDer};

fn main() {
   let tmp = Any::from_tag_and_data(Tag::from(0x41424344), &Integer::from(1).to_der_vec().unwrap())
        .to_der_vec()
        .unwrap();

    let expect = Tag::from(0x41424344);
    let actual = Any::from_der(&tmp).unwrap().1.tag();

    assert_eq!(expect, actual, "expected tag {expect}, found tag {actual}");
}

The above code results in the following output upon execution:

thread 'main' panicked at src/main.rs:12:5:
assertion `left == right` failed: expected tag Tag(1094861636 / 0x41424344), found tag Tag(1086473476 / 0x40c24504)
  left: Tag(1094861636)
 right: Tag(1086473476)
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

As I understand it, the tags are to remain the same during both the serialization and deserialization steps. If not, is there a correct way to store the tags so that they can be encoded onto the proper value?

Thanks in advance!

chifflier commented 3 months ago

Hi, I can confirm that the encoded value should be the same. I'll look into this

chifflier commented 3 months ago

Basically, the algorithm for encoding large tags was completely wrong. This is fixed in f9ea57f and will be included in the next release

Thanks