Skip to content

Commit 5851aff

Browse files
jxwufanherbertx
authored andcommitted
KEYS: X.509: Fix Basic Constraints CA flag parsing
Fix the X.509 Basic Constraints CA flag parsing to correctly handle the ASN.1 DER encoded structure. The parser was incorrectly treating the length field as the boolean value. Per RFC 5280 section 4.1, X.509 certificates must use ASN.1 DER encoding. According to ITU-T X.690, a DER-encoded BOOLEAN is represented as: Tag (0x01), Length (0x01), Value (0x00 for FALSE, 0xFF for TRUE) The basicConstraints extension with CA:TRUE is encoded as: SEQUENCE (0x30) | Length | BOOLEAN (0x01) | Length (0x01) | Value (0xFF) ^-- v[2] ^-- v[3] ^-- v[4] The parser was checking v[3] (the length field, always 0x01) instead of v[4] (the actual boolean value, 0xFF for TRUE in DER encoding). Also handle the case where the extension is an empty SEQUENCE (30 00), which is valid for CA:FALSE when the default value is omitted as required by DER encoding rules (X.690 section 11.5). Per ITU-T X.690-0207: - Section 11.5: Default values must be omitted in DER - Section 11.1: DER requires TRUE to be encoded as 0xFF Link: https://datatracker.ietf.org/doc/html/rfc5280 Link: https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf Fixes: 30eae2b ("KEYS: X.509: Parse Basic Constraints for CA") Signed-off-by: Fan Wu <wufan@kernel.org> Reviewed-by: Lukas Wunner <lukas@wunner.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b73f28d commit 5851aff

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

crypto/asymmetric_keys/x509_cert_parser.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,14 @@ int x509_process_extension(void *context, size_t hdrlen,
610610
/*
611611
* Get hold of the basicConstraints
612612
* v[1] is the encoding size
613-
* (Expect 0x2 or greater, making it 1 or more bytes)
613+
* (Expect 0x00 for empty SEQUENCE with CA:FALSE, or
614+
* 0x03 or greater for non-empty SEQUENCE)
614615
* v[2] is the encoding type
615616
* (Expect an ASN1_BOOL for the CA)
616-
* v[3] is the contents of the ASN1_BOOL
617-
* (Expect 1 if the CA is TRUE)
617+
* v[3] is the length of the ASN1_BOOL
618+
* (Expect 1 for a single byte boolean)
619+
* v[4] is the contents of the ASN1_BOOL
620+
* (Expect 0xFF if the CA is TRUE)
618621
* vlen should match the entire extension size
619622
*/
620623
if (v[0] != (ASN1_CONS_BIT | ASN1_SEQ))
@@ -623,8 +626,13 @@ int x509_process_extension(void *context, size_t hdrlen,
623626
return -EBADMSG;
624627
if (v[1] != vlen - 2)
625628
return -EBADMSG;
626-
if (vlen >= 4 && v[1] != 0 && v[2] == ASN1_BOOL && v[3] == 1)
629+
/* Empty SEQUENCE means CA:FALSE (default value omitted per DER) */
630+
if (v[1] == 0)
631+
return 0;
632+
if (vlen >= 5 && v[2] == ASN1_BOOL && v[3] == 1 && v[4] == 0xFF)
627633
ctx->cert->pub->key_eflags |= 1 << KEY_EFLAG_CA;
634+
else
635+
return -EBADMSG;
628636
return 0;
629637
}
630638

0 commit comments

Comments
 (0)