I'm trying to decode an X.509 certificate and have a question regarding the decoding of the extensions. In particular, the octet string corresponding to id-ce-keyUsage (OID 2.5.29.15) is as follows:
03020106
03 is the tag. A BIT STRING. 02 is the length. The value is 0106. Here's what http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf says about the encoding of bitstring's:
"The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit, the number of
unused bits in the final subsequent octet. The number shall be in the range zero to seven."
Here's the ASN.1 definition for this particular BIT STRING (from https://www.rfc-editor.org/rfc/rfc5280#appendix-A.1):
-- key usage extension OID and syntax
id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
KeyUsage ::= BIT STRING {
digitalSignature (0),
nonRepudiation (1), -- recent editions of X.509 have
-- renamed this bit to contentCommitment
keyEncipherment (2),
dataEncipherment (3),
keyAgreement (4),
keyCertSign (5),
cRLSign (6),
encipherOnly (7),
decipherOnly (8) }
My question is... why does the final bitstring have one unused byte? My expectation would be that it'd have no unused bits since the ASN.1 definition defines values for all eight bits.