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

Missing SetOf implementation #31

Closed
zaczkows opened this issue Aug 13, 2021 · 6 comments
Closed

Missing SetOf implementation #31

zaczkows opened this issue Aug 13, 2021 · 6 comments
Assignees
Labels
kind/enhancement New feature or request

Comments

@zaczkows
Copy link
Contributor

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
Copy link
Contributor Author

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
Copy link
Collaborator

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
Copy link
Collaborator

XAMPPRocky commented Aug 14, 2021

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.

@XAMPPRocky XAMPPRocky added the kind/enhancement New feature or request label Aug 14, 2021
@XAMPPRocky XAMPPRocky self-assigned this Aug 14, 2021
@zaczkows
Copy link
Contributor Author

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
Copy link
Collaborator

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

@XAMPPRocky
Copy link
Collaborator

XAMPPRocky commented Sep 12, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants