RustCrypto / formats

Cryptography-related format encoders/decoders: DER, PEM, PKCS, PKIX
228 stars 122 forks source link

unable to parse RecipientEncryptedKey #1350

Open knecht opened 4 months ago

knecht commented 4 months ago
  RecipientEncryptedKey ::= SEQUENCE {
      rid KeyAgreeRecipientIdentifier,
      encryptedKey EncryptedKey }
  KeyAgreeRecipientIdentifier ::= CHOICE {
      issuerAndSerialNumber IssuerAndSerialNumber,
      rKeyId [0] IMPLICIT RecipientKeyIdentifier }
  RecipientKeyIdentifier ::= SEQUENCE {
      subjectKeyIdentifier SubjectKeyIdentifier,
      date GeneralizedTime OPTIONAL,
      other OtherKeyAttribute OPTIONAL }
let dummy_rek = hex::decode("300ca00504034141410403424242").unwrap();
cms::enveloped_data::RecipientEncryptedKey::from_der(&dummy_rek).unwrap();

results in Error { kind: TagUnexpected { expected: None, actual: Tag(0xa0: CONTEXT-SPECIFIC [0] (constructed)) }, position: Some(Length(0)) }

Since RecipientKeyIdentifier is a sequence, 0xa0 seems to be be correct, though.

openssl output:

$ echo 300ca00504034141410403424242 | xxd -i -p -r |  openssl asn1parse -inform der -i
    0:d=0  hl=2 l=  12 cons: SEQUENCE          
    2:d=1  hl=2 l=   5 cons:  cont [ 0 ]        
    4:d=2  hl=2 l=   3 prim:   OCTET STRING      :AAA
    9:d=1  hl=2 l=   3 prim:  OCTET STRING      :BBB
carl-wallace commented 4 months ago

I think this is likely a duplicate of an issue that was fixed here: https://github.com/RustCrypto/formats/commit/5612e91286d0f2bc8d12372cd1c0258da1b4550f.

tarcieri commented 4 months ago

Yeah, looks like it, although unfortunately it seems the fix doesn't appear to have a stable release. I can potentially backport it.