Skip to content

der: add new and derives to SequenceRef #1980

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kamulos
Copy link

@kamulos kamulos commented Aug 4, 2025

This is a reopening of #1977 which was closed because there was too little explaination. The motivation for adding the derives is that I cannot use SequenceRef in my structs that I want to output in the debug format or compare for equality.

The motivation for creating new SequenceRef is a bit harder to explain: I have something like X.509 ContentInfo that needs raw data access. When decoding I need the buffer data to validate a signature and I need to add a raw buffer which I have signed when compiling the data. I also want to use der_derive annotations to do all the validation for me.

Up until recently I was using AnyRef for this purpose, but this broke in the recent 0.8.0 release candidates (please see #1976 for more info). The thing is, instead of an explicit tag mode like in ContentInfo the format I am parsing is using the implicit tag mode and has the constructed bit set. AnyRef does not work with this anymore and I can understand the reasoning.

The next best thing we found in the linked case was (ab)using SequenceRef because it accepts having the implicit tag mode and the constructed bit set. And to be able to construct the type I need some kind of constructor for SequenceRef.

To be honest I would prefer having something that is more intentional for the purpose of raw data access, but still allows me to use the derive macro to specify the tag and tag mode implicit.

Tagging @dishmaker because we had a lengthy discussion in the linked case

Copying the example from the linked case. This used to work, but does not anymore:

use der::{AnyRef, Decode, Sequence, oid::ObjectIdentifier};

#[derive(Debug, Sequence)]
pub struct SomeType<'a> {
    pub oid: ObjectIdentifier,
    #[asn1(context_specific = "0", tag_mode = "IMPLICIT")]
    pub data: AnyRef<'a>,
}

fn main() {
    let bytes = b"\x30\x12\x06\x07\x2a\x86\x48\xce\x3d\x02\x01\xa0\x07\x63\x6f\x6e\x74\x65\x6e\x74";
    let decoded = SomeType::from_der(bytes).unwrap();
    println!("{:02x?}", decoded.data.value());
}

@baloo
Copy link
Member

baloo commented Aug 4, 2025

Apologies for the aggressive closure of the last PR. Too many AI slop PRs those days. I have misjudged the last PR.

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

Successfully merging this pull request may close these issues.

2 participants