Coder Social home page Coder Social logo

simple_asn1's Introduction

Overview

This is a simple library for parsing DER-encoded data.

In particular, this library automates the process of understanding the DER encoded objects in an ASN.1 data stream. These tokens can then be parsed by your library, based on the ASN.1 description in your format.

For convenience, we create the traits ToASN1 and FromASN to abstract the ability to decode a type from an ASN.1 token stream. If your type implements one of these traits, your program or library can then use the convenience functions der_encode and der_decode to do all the parsing work in one action.

Patches welcome!

simple_asn1's People

Contributors

acw avatar c410-f3r avatar drahnr avatar flakebi avatar marco-neumann-by avatar mehcode avatar nmathewson avatar randombit avatar rib avatar seinlin avatar stanislav-tkach avatar tsoutsman avatar twjang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

simple_asn1's Issues

Decoding output a bit unintuitive

In the below script, the function test_encode prints:

a: [Explicit(ContextSpecific, 0, BigUint { data: [] }, Unknown(ContextSpecific, false, 2, BigUint { data: [] }, [0]))]
b: [Unknown(ContextSpecific, true, 0, BigUint { data: [] }, [128, 1, 0, 129, 1, 1])]

This is unexpected as the structure is the same in both occasions, and it appears to relate to this. I would expect the output to be as it is in b both times. I'm not exactly sure if the encoding is being done in the best way, but it conforms to the ASN model I am working with so I can't change it. For reference, the Haskell library asn1-encoding produces the following:

[Start (Container Context 0),Other Context 0 "\NUL",End (Container Context 0)]
[Start (Container Context 0),Other Context 0 "\NUL",Other Context 1 "\SOH",End (Container Context 0)]

Test script:

use num_traits::cast::{FromPrimitive};
use num_bigint::{BigInt, BigUint};
use simple_asn1::*;

#[test]
fn test_encode() {
    let a = encode_structure(0, &vec![vec![0]]);
    let b = encode_structure(0, &vec![vec![0], vec![1]]);
    println!("a: {:?}\nb: {:?}", from_der(&a).unwrap(), from_der(&b).unwrap());
}

fn encode_structure(type_id: u8, bufs: &[Vec<u8>]) -> Vec<u8> {
    let mut body = Vec::new();
    for (i, buf) in bufs.iter().enumerate() {
        let mut der = to_der(&ASN1Block::Unknown(ASN1Class::ContextSpecific, false, 0, BigUint::from_usize(i).unwrap(), buf.to_vec())).unwrap();
        body.append(&mut der);
    }
    to_der(&ASN1Block::Unknown(ASN1Class::ContextSpecific, true, 0, BigUint::from_u8(type_id).unwrap(), body)).unwrap()
}

Why is OID not `Eq`?

OID impls PartialEq but not Eq even though (AFAICT) OID equality satisfies Eq - is this just an oversight? Would you accept a patch adding Eq?

Release new version?

Latest published version on https://crates.io/crates/simple_asn1 0.1.0 is from Dec 26th, 2017, could you publish a new version from the git repo?

Looking for a version with this commit #3 - upgrading the dependencies, because it has num 0.2.0 instead of num 0.1.0 (and the same minor version of the crate must be used in order to use num in the application and the dependency, allowing interoperability). For now I'm using the git version in my Cargo.toml:

simple_asn1 = { git = "https://github.com/acw/simple_asn1", rev = "823fc31f6f9ba0636207226901f46d50eb1fdd01" }

Panic when decoding an invalid UTCTime

Hello, I hope this is the right place to report this; I didn't find any documentation for a preferred method for reporting security issues.

The following code panics when trying to parse an invalid UTCTime object:

fn main() {
    let input =  [55, 13, 13, 133, 13, 13, 50, 13, 13, 133, 13, 13, 50, 13, 133];
    let output = simple_asn1::from_der(&input);
    println!("{:?}", output);
}

The panic occurs because of these line in lib.rs:

                let v = String::from_iter(body.iter().map(|x| *x as char));
                let y = &v[0..2];

If the string is constructed in such a way that the first two bytes do not end on a character boundary, the slice operation will panic.

Found by fuzzing a downstream library.

I'll submit a patch ASAP.

OID as bytes

I am currently looking into parsing some ASN1 strcuts with varyin OCTET STRING sub parts.

It would be super awesome if one could compare against byte slices directly for equality or get a to_vec() method to yield just OID part of the particular ASN1 block to be able to compare them.

If either of them would be a desired addition, I am happy to extend OID a little bit :)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.