12

according to for example http://luca.ntop.org/Teaching/Appunti/asn1.html a sequence has the tag number 10 in hexadecimal. But why it is then DER encoded as 30 and not 10? An INTEGER with the tag number 02 in hexadecimal is also encoded as 02. Thanks.

e.g.

Sample ::= SEQUENCE {
    number 5
}

encoded as 30 03 02 01 05

Crypt32
  • 12,850
  • 2
  • 41
  • 70
Pete
  • 151
  • 1
  • 6

1 Answers1

15

This is because in X.509 formats, SET and SEQUENCE types are used in constructed form. As the result, 6th bit is set to 1. By setting 1 in 6th bit for SEQUENCE universal tag (0x10) you will get 0x30 and 0x31 for SET (0x11 and 6th bit to 1 = 0x31). The rest universal types are encoded in primitive forms (6th bit is set to 0).

Crypt32
  • 12,850
  • 2
  • 41
  • 70
  • I'm still struggling with this. Wouldn't setting the sixth bit make 0x14 for SEQUENCE, and 0x15 for SET? – Jibby Aug 28 '19 at 19:13
  • No. 6th bit makes primitive form constructed. By clearing this bit you can get primitive form back. – Crypt32 Aug 28 '19 at 19:16
  • What I'm asking is, how does 00010100 (0x10 for SEQUENCE, with 6th bit set to indicate constructed) equal 0x31, and not 0x14? – Jibby Aug 28 '19 at 19:22
  • Oh, I'm sorry, I'm counting bits in the wrong direction. This is with 6th bit set: 00110000, which is 0x30, as you said. Oops! – Jibby Aug 28 '19 at 19:30
  • 1
    Yes, bits are calculated in RTL direction. – Crypt32 Aug 28 '19 at 19:32