Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to peek into DER and stop reading early? #71

Open
zugzwang opened this issue May 9, 2023 · 1 comment
Open

How to peek into DER and stop reading early? #71

zugzwang opened this issue May 9, 2023 · 1 comment

Comments

@zugzwang
Copy link

zugzwang commented May 9, 2023

In some scenarios, I want to peek into a DER, parse an early element (think of an OID), and return it. It appears that I cannot do that with BERReader without reading the whole DER.

My best try was to ignore the remainder with the following

fn extract_oid(der: &[u8]) {
    let oid = yasna::parse_der(der, |r| {
        // Let's say we have a SEQUENCE
        r.read_sequence(|r| {
            // The OID I want is embedded inside an inner SEQUENCE:
            let oid = r.next().read_sequence(|r| {
                let oid = r.next().read_oid()?;
                let _remainder = r.next().read_der(); // Ignore the remainder of that sequence
                Ok(oid)
            })?;
            let _remainder = r.next().read_der(); // Ignore the rest of the sequence
            Ok(oid)
        })
    });
    println!("extracted OID {:?}", oid);
}

Without checking the remainder, yasna fails with Err(ASN1Error { kind: Extra }), and I would like to ignore this error.

@gaetanww
Copy link

Did you find a way to do that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants