librasn / rasn

A Safe #[no_std] ASN.1 Codec Framework
Other
212 stars 50 forks source link

Missing `SetOf` implementation #31

Closed zaczkows closed 3 years ago

zaczkows commented 3 years ago

Hello,

I'm trying to implement very ASN.1 schema from RFC7030:

 CsrAttrs ::= SEQUENCE SIZE (0..MAX) OF AttrOrOID

   AttrOrOID ::= CHOICE (oid OBJECT IDENTIFIER, attribute Attribute }

   Attribute { ATTRIBUTE:IOSet } ::= SEQUENCE {
        type   ATTRIBUTE.&id({IOSet}),
        values SET SIZE(1..MAX) OF ATTRIBUTE.&Type({IOSet}{@type}) }

I created rust code which tries to encode/decode it:

#[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
struct CsrAttrs {
    attr_or_oid: Vec<AttrOrOid>,
}

#[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
#[rasn(choice)]
enum AttrOrOid {
    OID(rasn::types::ObjectIdentifier),
    ATTRIBUTE(Attribute),
}

#[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
struct Attribute {
    r#type: rasn::types::ObjectIdentifier,
    values: rasn::types::SetOf<rasn::types::Open>,
}

#[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
struct AttributeValue(Vec<u8>);

fn main() {}

However, I got compilation error, as it looks like the SetOf is not fully implemented:

error[E0277]: the trait bound `BTreeSet<Open>: Decode` is not satisfied
  --> src/main.rs:23:25
   |
23 | #[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
   |                         ^^^^^^^^^^^^ the trait `Decode` is not implemented for `BTreeSet<Open>`
   |
   = note: required by `decode_with_tag`
   = note: this error originates in the derive macro `rasn::Decode` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0599]: no method named `encode` found for struct `BTreeSet` in the current scope
  --> src/main.rs:23:39
   |
23 | #[derive(rasn::AsnType, rasn::Decode, rasn::Encode, Debug, PartialEq, Clone)]
   |                                       ^^^^^^^^^^^^ method not found in `BTreeSet<Open>`
   |
   = note: this error originates in the derive macro `rasn::Encode` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors
zaczkows commented 3 years ago

I took a look at the code and I'm not sure if SetOf should be set to BTreeSet. First of all it introduces strong dependency to Ord trait and forces values sorting. I know that DER or BER is only one of the representations, but according to warm-welcome-to-asn1-and-der:

In BER, a SET may be encoded in any order. In DER, a SET must be encoded in ascending order by tag.

A SET OF items is encoded the same way as a SET, including the tag byte of 0x31. For DER encoding, there is a similar requirement that the SET OF must be encoded in ascending order. Because all elements in the SET OF have the same type, ordering by tag is not sufficient. So the elements of a SET OF are sorted by their encoded values, with shorter values treated as if they were padded to the right with zeroes.

XAMPPRocky commented 3 years ago

Thank you for your issue! Yeah I added that type aliases but never finished the implementation, because SET/SET OF is quite unpopular, and in formats like PER they may or may not be encoded in ascending order, so I'm still figuring out what API would allow you to safely encode a set, without worrying about this encoding order.

XAMPPRocky commented 3 years ago

Also if you'd be interested, I'd be willing to accept adding any IETF ASN.1 modules related RFC7030 as crates to rasn (similar to SNMP) if you're interested in sharing the implementation.

zaczkows commented 3 years ago

Yeah, sure. I can try to add at least CSR attributes decoding from RFC7030 (the rest is just usual DER certificate). However, both examples in RFC requires implementation of the SET(OF).

XAMPPRocky commented 3 years ago

I've an idea on how to implement it SET encoding, I'll try to implement it soon.

XAMPPRocky commented 3 years ago

I've now implemented support for SET types, and I've also added rasn-pkix in standards which can decode CA certificates. Check it out, and I'll release it a few days once I've added documentation and release notes.