rusticata / der-parser

BER/DER parser written in pure Rust. Fast, zero-copy, safe.
Apache License 2.0
84 stars 29 forks source link

Concatenate a relative OID onto a base OID #44

Open lilyball opened 3 years ago

lilyball commented 3 years ago

I would really like to be able to concatenate relative OIDs onto base OIDs at compile-time. I'm not sure if this is actually possible, but I want to be able to write something like

const OID_BASE: Oid = oid!(1.2.840.1234);
const OID_SUB_1: Oid = oid!(OID_BASE 10);
const OID_SUB_2: Oid = oid!(OID_SUB_1 15);

Or some alternative syntax that achieves the same thing. I don't know if there's any way to achieve this without heap allocation. I really wish Rust had built-in syntax for "please flatten this array into the enclosing array literal".

The goal here is of course to be able to represent a tree of OIDs using a similar structure to how they're declared in ASN.1, instead of having to repeat the OID base everywhere.

lilyball commented 3 years ago

After looking into proc macros some more, I'm pretty sure this isn't possible.

What should be possible though is writing an oid_tree!() macro that lets you define a whole set of OIDs, where base OIDs can have children that are prefixed by the base. I'm currently working on such a macro for my own project, but I'd love to see this crate offer it directly.

zotho commented 3 years ago

@lilyball We can probably achieve this with changes like https://github.com/rusticata/der-parser/pull/45

chifflier commented 3 years ago

I would love to see that at build time, but I don't think there is a way to resolve a symbol value in a proc-macro (or macro). Maybe #45 is a solution (see the PR for discussion).