2 * X.509v3 certificate parsing and processing (RFC 3280 profile)
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #ifdef CONFIG_INTERNAL_X509
26 static void x509_free_name(struct x509_name
*name
)
35 name
->cn
= name
->c
= name
->l
= name
->st
= name
->o
= name
->ou
= NULL
;
41 * x509_certificate_free - Free an X.509 certificate
42 * @cert: Certificate to be freed
44 void x509_certificate_free(struct x509_certificate
*cert
)
49 wpa_printf(MSG_DEBUG
, "X509: x509_certificate_free: cer=%p "
50 "was still on a list (next=%p)\n",
53 x509_free_name(&cert
->issuer
);
54 x509_free_name(&cert
->subject
);
55 os_free(cert
->public_key
);
56 os_free(cert
->sign_value
);
62 * x509_certificate_free - Free an X.509 certificate chain
63 * @cert: Pointer to the first certificate in the chain
65 void x509_certificate_chain_free(struct x509_certificate
*cert
)
67 struct x509_certificate
*next
;
72 x509_certificate_free(cert
);
78 static int x509_whitespace(char c
)
80 return c
== ' ' || c
== '\t';
84 static void x509_str_strip_whitespace(char *a
)
87 int remove_whitespace
= 1;
92 if (remove_whitespace
&& x509_whitespace(*ipos
))
95 remove_whitespace
= x509_whitespace(*ipos
);
101 if (opos
> a
&& x509_whitespace(*opos
))
106 static int x509_str_compare(const char *a
, const char *b
)
121 if (aa
== NULL
|| bb
== NULL
) {
124 return os_strcasecmp(a
, b
);
127 x509_str_strip_whitespace(aa
);
128 x509_str_strip_whitespace(bb
);
130 ret
= os_strcasecmp(aa
, bb
);
140 * x509_name_compare - Compare X.509 certificate names
141 * @a: Certificate name
142 * @b: Certificate name
143 * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
146 int x509_name_compare(struct x509_name
*a
, struct x509_name
*b
)
157 res
= x509_str_compare(a
->cn
, b
->cn
);
160 res
= x509_str_compare(a
->c
, b
->c
);
163 res
= x509_str_compare(a
->l
, b
->l
);
166 res
= x509_str_compare(a
->st
, b
->st
);
169 res
= x509_str_compare(a
->o
, b
->o
);
172 res
= x509_str_compare(a
->ou
, b
->ou
);
175 res
= x509_str_compare(a
->email
, b
->email
);
183 static int x509_parse_algorithm_identifier(
184 const u8
*buf
, size_t len
,
185 struct x509_algorithm_identifier
*id
, const u8
**next
)
191 * AlgorithmIdentifier ::= SEQUENCE {
192 * algorithm OBJECT IDENTIFIER,
193 * parameters ANY DEFINED BY algorithm OPTIONAL
197 if (asn1_get_next(buf
, len
, &hdr
) < 0 ||
198 hdr
.class != ASN1_CLASS_UNIVERSAL
||
199 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
200 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
201 "(AlgorithmIdentifier) - found class %d tag 0x%x",
206 end
= pos
+ hdr
.length
;
213 if (asn1_get_oid(pos
, end
- pos
, &id
->oid
, &pos
))
216 /* TODO: optional parameters */
222 static int x509_parse_public_key(const u8
*buf
, size_t len
,
223 struct x509_certificate
*cert
,
230 * SubjectPublicKeyInfo ::= SEQUENCE {
231 * algorithm AlgorithmIdentifier,
232 * subjectPublicKey BIT STRING
239 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
240 hdr
.class != ASN1_CLASS_UNIVERSAL
||
241 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
242 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
243 "(SubjectPublicKeyInfo) - found class %d tag 0x%x",
249 if (pos
+ hdr
.length
> end
)
251 end
= pos
+ hdr
.length
;
254 if (x509_parse_algorithm_identifier(pos
, end
- pos
,
255 &cert
->public_key_alg
, &pos
))
258 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
259 hdr
.class != ASN1_CLASS_UNIVERSAL
||
260 hdr
.tag
!= ASN1_TAG_BITSTRING
) {
261 wpa_printf(MSG_DEBUG
, "X509: Expected BITSTRING "
262 "(subjectPublicKey) - found class %d tag 0x%x",
270 wpa_printf(MSG_DEBUG
, "X509: BITSTRING - %d unused bits",
273 * TODO: should this be rejected? X.509 certificates are
274 * unlikely to use such a construction. Now we would end up
275 * including the extra bits in the buffer which may also be
279 os_free(cert
->public_key
);
280 cert
->public_key
= os_malloc(hdr
.length
- 1);
281 if (cert
->public_key
== NULL
) {
282 wpa_printf(MSG_DEBUG
, "X509: Failed to allocate memory for "
286 os_memcpy(cert
->public_key
, pos
+ 1, hdr
.length
- 1);
287 cert
->public_key_len
= hdr
.length
- 1;
288 wpa_hexdump(MSG_MSGDUMP
, "X509: subjectPublicKey",
289 cert
->public_key
, cert
->public_key_len
);
295 static int x509_parse_name(const u8
*buf
, size_t len
, struct x509_name
*name
,
299 const u8
*pos
, *end
, *set_pos
, *set_end
, *seq_pos
, *seq_end
;
304 * Name ::= CHOICE { RDNSequence }
305 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
306 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
307 * AttributeTypeAndValue ::= SEQUENCE {
308 * type AttributeType,
309 * value AttributeValue
311 * AttributeType ::= OBJECT IDENTIFIER
312 * AttributeValue ::= ANY DEFINED BY AttributeType
315 if (asn1_get_next(buf
, len
, &hdr
) < 0 ||
316 hdr
.class != ASN1_CLASS_UNIVERSAL
||
317 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
318 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
319 "(Name / RDNSequencer) - found class %d tag 0x%x",
325 if (pos
+ hdr
.length
> buf
+ len
)
328 end
= *next
= pos
+ hdr
.length
;
331 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
332 hdr
.class != ASN1_CLASS_UNIVERSAL
||
333 hdr
.tag
!= ASN1_TAG_SET
) {
334 wpa_printf(MSG_DEBUG
, "X509: Expected SET "
335 "(RelativeDistinguishedName) - found class "
336 "%d tag 0x%x", hdr
.class, hdr
.tag
);
337 x509_free_name(name
);
341 set_pos
= hdr
.payload
;
342 pos
= set_end
= hdr
.payload
+ hdr
.length
;
344 if (asn1_get_next(set_pos
, set_end
- set_pos
, &hdr
) < 0 ||
345 hdr
.class != ASN1_CLASS_UNIVERSAL
||
346 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
347 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
348 "(AttributeTypeAndValue) - found class %d "
349 "tag 0x%x", hdr
.class, hdr
.tag
);
350 x509_free_name(name
);
354 seq_pos
= hdr
.payload
;
355 seq_end
= hdr
.payload
+ hdr
.length
;
357 if (asn1_get_oid(seq_pos
, seq_end
- seq_pos
, &oid
, &seq_pos
)) {
358 x509_free_name(name
);
362 if (asn1_get_next(seq_pos
, seq_end
- seq_pos
, &hdr
) < 0 ||
363 hdr
.class != ASN1_CLASS_UNIVERSAL
) {
364 wpa_printf(MSG_DEBUG
, "X509: Failed to parse "
366 x509_free_name(name
);
371 * MUST: country, organization, organizational-unit,
372 * distinguished name qualifier, state or province name,
373 * common name, serial number.
374 * SHOULD: locality, title, surname, given name, initials,
375 * pseudonym, generation qualifier.
376 * MUST: domainComponent (RFC 2247).
380 oid
.oid
[0] == 2 && oid
.oid
[1] == 5 && oid
.oid
[2] == 4) {
381 /* id-at ::= 2.5.4 */
382 switch (oid
.oid
[3]) {
396 /* stateOrProvinceName */
400 /* organizationName */
404 /* organizationalUnitName */
408 } else if (oid
.len
== 7 &&
409 oid
.oid
[0] == 1 && oid
.oid
[1] == 2 &&
410 oid
.oid
[2] == 840 && oid
.oid
[3] == 113549 &&
411 oid
.oid
[4] == 1 && oid
.oid
[5] == 9 &&
413 /* 1.2.840.113549.1.9.1 - e-mailAddress */
414 fieldp
= &name
->email
;
417 if (fieldp
== NULL
) {
418 wpa_hexdump(MSG_DEBUG
, "X509: Unrecognized OID",
420 oid
.len
* sizeof(oid
.oid
[0]));
421 wpa_hexdump_ascii(MSG_MSGDUMP
, "X509: Attribute Data",
422 hdr
.payload
, hdr
.length
);
427 *fieldp
= os_malloc(hdr
.length
+ 1);
428 if (*fieldp
== NULL
) {
429 x509_free_name(name
);
432 os_memcpy(*fieldp
, hdr
.payload
, hdr
.length
);
433 (*fieldp
)[hdr
.length
] = '\0';
441 * x509_name_string - Convert an X.509 certificate name into a string
442 * @name: Name to convert
443 * @buf: Buffer for the string
444 * @len: Maximum buffer length
446 void x509_name_string(struct x509_name
*name
, char *buf
, size_t len
)
458 ret
= os_snprintf(pos
, end
- pos
, "C=%s, ", name
->c
);
459 if (ret
< 0 || ret
>= end
- pos
)
464 ret
= os_snprintf(pos
, end
- pos
, "ST=%s, ", name
->st
);
465 if (ret
< 0 || ret
>= end
- pos
)
470 ret
= os_snprintf(pos
, end
- pos
, "L=%s, ", name
->l
);
471 if (ret
< 0 || ret
>= end
- pos
)
476 ret
= os_snprintf(pos
, end
- pos
, "O=%s, ", name
->o
);
477 if (ret
< 0 || ret
>= end
- pos
)
482 ret
= os_snprintf(pos
, end
- pos
, "OU=%s, ", name
->ou
);
483 if (ret
< 0 || ret
>= end
- pos
)
488 ret
= os_snprintf(pos
, end
- pos
, "CN=%s, ", name
->cn
);
489 if (ret
< 0 || ret
>= end
- pos
)
494 if (pos
> buf
+ 1 && pos
[-1] == ' ' && pos
[-2] == ',') {
500 ret
= os_snprintf(pos
, end
- pos
, "/emailAddress=%s",
502 if (ret
< 0 || ret
>= end
- pos
)
512 static int x509_parse_time(const u8
*buf
, size_t len
, u8 asn1_tag
,
516 int year
, month
, day
, hour
, min
, sec
;
521 * generalTime GeneralizedTime
524 * UTCTime: YYMMDDHHMMSSZ
525 * GeneralizedTime: YYYYMMDDHHMMSSZ
528 pos
= (const char *) buf
;
531 case ASN1_TAG_UTCTIME
:
532 if (len
!= 13 || buf
[12] != 'Z') {
533 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Unrecognized "
534 "UTCTime format", buf
, len
);
537 if (sscanf(pos
, "%02d", &year
) != 1) {
538 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse "
539 "UTCTime year", buf
, len
);
548 case ASN1_TAG_GENERALIZEDTIME
:
549 if (len
!= 15 || buf
[14] != 'Z') {
550 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Unrecognized "
551 "GeneralizedTime format", buf
, len
);
554 if (sscanf(pos
, "%04d", &year
) != 1) {
555 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse "
556 "GeneralizedTime year", buf
, len
);
562 wpa_printf(MSG_DEBUG
, "X509: Expected UTCTime or "
563 "GeneralizedTime - found tag 0x%x", asn1_tag
);
567 if (sscanf(pos
, "%02d", &month
) != 1) {
568 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse Time "
569 "(month)", buf
, len
);
574 if (sscanf(pos
, "%02d", &day
) != 1) {
575 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse Time "
581 if (sscanf(pos
, "%02d", &hour
) != 1) {
582 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse Time "
588 if (sscanf(pos
, "%02d", &min
) != 1) {
589 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse Time "
595 if (sscanf(pos
, "%02d", &sec
) != 1) {
596 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse Time "
601 if (os_mktime(year
, month
, day
, hour
, min
, sec
, val
) < 0) {
602 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to convert Time",
606 * At least some test certificates have been configured
607 * to use dates prior to 1970. Set the date to
608 * beginning of 1970 to handle these case.
610 wpa_printf(MSG_DEBUG
, "X509: Year=%d before epoch - "
611 "assume epoch as the time", year
);
622 static int x509_parse_validity(const u8
*buf
, size_t len
,
623 struct x509_certificate
*cert
, const u8
**next
)
630 * Validity ::= SEQUENCE {
636 * CAs conforming to this profile MUST always encode certificate
637 * validity dates through the year 2049 as UTCTime; certificate
638 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
641 if (asn1_get_next(buf
, len
, &hdr
) < 0 ||
642 hdr
.class != ASN1_CLASS_UNIVERSAL
||
643 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
644 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
645 "(Validity) - found class %d tag 0x%x",
652 if (pos
+ plen
> buf
+ len
)
657 if (asn1_get_next(pos
, plen
, &hdr
) < 0 ||
658 hdr
.class != ASN1_CLASS_UNIVERSAL
||
659 x509_parse_time(hdr
.payload
, hdr
.length
, hdr
.tag
,
660 &cert
->not_before
) < 0) {
661 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse notBefore "
662 "Time", hdr
.payload
, hdr
.length
);
666 pos
= hdr
.payload
+ hdr
.length
;
669 if (asn1_get_next(pos
, plen
, &hdr
) < 0 ||
670 hdr
.class != ASN1_CLASS_UNIVERSAL
||
671 x509_parse_time(hdr
.payload
, hdr
.length
, hdr
.tag
,
672 &cert
->not_after
) < 0) {
673 wpa_hexdump_ascii(MSG_DEBUG
, "X509: Failed to parse notAfter "
674 "Time", hdr
.payload
, hdr
.length
);
678 wpa_printf(MSG_MSGDUMP
, "X509: Validity: notBefore: %lu notAfter: %lu",
679 (unsigned long) cert
->not_before
,
680 (unsigned long) cert
->not_after
);
686 static int x509_id_ce_oid(struct asn1_oid
*oid
)
688 /* id-ce arc from X.509 for standard X.509v3 extensions */
689 return oid
->len
>= 4 &&
690 oid
->oid
[0] == 2 /* joint-iso-ccitt */ &&
691 oid
->oid
[1] == 5 /* ds */ &&
692 oid
->oid
[2] == 29 /* id-ce */;
696 static int x509_parse_ext_key_usage(struct x509_certificate
*cert
,
697 const u8
*pos
, size_t len
)
702 * KeyUsage ::= BIT STRING {
703 * digitalSignature (0),
704 * nonRepudiation (1),
705 * keyEncipherment (2),
706 * dataEncipherment (3),
714 if (asn1_get_next(pos
, len
, &hdr
) < 0 ||
715 hdr
.class != ASN1_CLASS_UNIVERSAL
||
716 hdr
.tag
!= ASN1_TAG_BITSTRING
||
718 wpa_printf(MSG_DEBUG
, "X509: Expected BIT STRING in "
719 "KeyUsage; found %d tag 0x%x len %d",
720 hdr
.class, hdr
.tag
, hdr
.length
);
724 cert
->extensions_present
|= X509_EXT_KEY_USAGE
;
725 cert
->key_usage
= asn1_bit_string_to_long(hdr
.payload
, hdr
.length
);
727 wpa_printf(MSG_DEBUG
, "X509: KeyUsage 0x%lx", cert
->key_usage
);
733 static int x509_parse_ext_basic_constraints(struct x509_certificate
*cert
,
734 const u8
*pos
, size_t len
)
741 * BasicConstraints ::= SEQUENCE {
742 * cA BOOLEAN DEFAULT FALSE,
743 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
746 if (asn1_get_next(pos
, len
, &hdr
) < 0 ||
747 hdr
.class != ASN1_CLASS_UNIVERSAL
||
748 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
749 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE in "
750 "BasicConstraints; found %d tag 0x%x",
755 cert
->extensions_present
|= X509_EXT_BASIC_CONSTRAINTS
;
760 if (asn1_get_next(hdr
.payload
, hdr
.length
, &hdr
) < 0 ||
761 hdr
.class != ASN1_CLASS_UNIVERSAL
) {
762 wpa_printf(MSG_DEBUG
, "X509: Failed to parse "
767 if (hdr
.tag
== ASN1_TAG_BOOLEAN
) {
768 if (hdr
.length
!= 1) {
769 wpa_printf(MSG_DEBUG
, "X509: Unexpected "
770 "Boolean length (%u) in BasicConstraints",
774 cert
->ca
= hdr
.payload
[0];
776 if (hdr
.payload
+ hdr
.length
== pos
+ len
) {
777 wpa_printf(MSG_DEBUG
, "X509: BasicConstraints - cA=%d",
782 if (asn1_get_next(hdr
.payload
+ hdr
.length
, len
- hdr
.length
,
784 hdr
.class != ASN1_CLASS_UNIVERSAL
) {
785 wpa_printf(MSG_DEBUG
, "X509: Failed to parse "
791 if (hdr
.tag
!= ASN1_TAG_INTEGER
) {
792 wpa_printf(MSG_DEBUG
, "X509: Expected INTEGER in "
793 "BasicConstraints; found class %d tag 0x%x",
807 cert
->path_len_constraint
= value
;
808 cert
->extensions_present
|= X509_EXT_PATH_LEN_CONSTRAINT
;
810 wpa_printf(MSG_DEBUG
, "X509: BasicConstraints - cA=%d "
811 "pathLenConstraint=%lu",
812 cert
->ca
, cert
->path_len_constraint
);
818 static int x509_parse_extension_data(struct x509_certificate
*cert
,
819 struct asn1_oid
*oid
,
820 const u8
*pos
, size_t len
)
822 if (!x509_id_ce_oid(oid
))
825 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
826 * certificate policies (section 4.2.1.5)
827 * the subject alternative name (section 4.2.1.7)
828 * name constraints (section 4.2.1.11)
829 * policy constraints (section 4.2.1.12)
830 * extended key usage (section 4.2.1.13)
831 * inhibit any-policy (section 4.2.1.15)
833 switch (oid
->oid
[3]) {
834 case 15: /* id-ce-keyUsage */
835 return x509_parse_ext_key_usage(cert
, pos
, len
);
836 case 19: /* id-ce-basicConstraints */
837 return x509_parse_ext_basic_constraints(cert
, pos
, len
);
844 static int x509_parse_extension(struct x509_certificate
*cert
,
845 const u8
*pos
, size_t len
, const u8
**next
)
850 int critical_ext
= 0, res
;
854 * Extension ::= SEQUENCE {
855 * extnID OBJECT IDENTIFIER,
856 * critical BOOLEAN DEFAULT FALSE,
857 * extnValue OCTET STRING
861 if (asn1_get_next(pos
, len
, &hdr
) < 0 ||
862 hdr
.class != ASN1_CLASS_UNIVERSAL
||
863 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
864 wpa_printf(MSG_DEBUG
, "X509: Unexpected ASN.1 header in "
865 "Extensions: class %d tag 0x%x; expected SEQUENCE",
870 *next
= end
= pos
+ hdr
.length
;
872 if (asn1_get_oid(pos
, end
- pos
, &oid
, &pos
) < 0) {
873 wpa_printf(MSG_DEBUG
, "X509: Unexpected ASN.1 data for "
874 "Extension (expected OID)");
878 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
879 hdr
.class != ASN1_CLASS_UNIVERSAL
||
880 (hdr
.tag
!= ASN1_TAG_BOOLEAN
&&
881 hdr
.tag
!= ASN1_TAG_OCTETSTRING
)) {
882 wpa_printf(MSG_DEBUG
, "X509: Unexpected ASN.1 header in "
883 "Extensions: class %d tag 0x%x; expected BOOLEAN "
884 "or OCTET STRING", hdr
.class, hdr
.tag
);
888 if (hdr
.tag
== ASN1_TAG_BOOLEAN
) {
889 if (hdr
.length
!= 1) {
890 wpa_printf(MSG_DEBUG
, "X509: Unexpected "
891 "Boolean length (%u)", hdr
.length
);
894 critical_ext
= hdr
.payload
[0];
896 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
897 (hdr
.class != ASN1_CLASS_UNIVERSAL
&&
898 hdr
.class != ASN1_CLASS_PRIVATE
) ||
899 hdr
.tag
!= ASN1_TAG_OCTETSTRING
) {
900 wpa_printf(MSG_DEBUG
, "X509: Unexpected ASN.1 header "
901 "in Extensions: class %d tag 0x%x; "
902 "expected OCTET STRING",
908 asn1_oid_to_str(&oid
, buf
, sizeof(buf
));
909 wpa_printf(MSG_DEBUG
, "X509: Extension: extnID=%s critical=%d",
911 wpa_hexdump(MSG_MSGDUMP
, "X509: extnValue", hdr
.payload
, hdr
.length
);
913 res
= x509_parse_extension_data(cert
, &oid
, hdr
.payload
, hdr
.length
);
916 if (res
== 1 && critical_ext
) {
917 wpa_printf(MSG_INFO
, "X509: Unknown critical extension %s",
926 static int x509_parse_extensions(struct x509_certificate
*cert
,
927 const u8
*pos
, size_t len
)
932 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
934 if (asn1_get_next(pos
, len
, &hdr
) < 0 ||
935 hdr
.class != ASN1_CLASS_UNIVERSAL
||
936 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
937 wpa_printf(MSG_DEBUG
, "X509: Unexpected ASN.1 data "
938 "for Extensions: class %d tag 0x%x; "
939 "expected SEQUENCE", hdr
.class, hdr
.tag
);
944 end
= pos
+ hdr
.length
;
947 if (x509_parse_extension(cert
, pos
, end
- pos
, &pos
)
956 static int x509_parse_tbs_certificate(const u8
*buf
, size_t len
,
957 struct x509_certificate
*cert
,
966 /* tbsCertificate TBSCertificate ::= SEQUENCE */
967 if (asn1_get_next(buf
, len
, &hdr
) < 0 ||
968 hdr
.class != ASN1_CLASS_UNIVERSAL
||
969 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
970 wpa_printf(MSG_DEBUG
, "X509: tbsCertificate did not start "
971 "with a valid SEQUENCE - found class %d tag 0x%x",
976 end
= *next
= pos
+ hdr
.length
;
979 * version [0] EXPLICIT Version DEFAULT v1
980 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
982 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0)
986 if (hdr
.class == ASN1_CLASS_CONTEXT_SPECIFIC
) {
987 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0)
990 if (hdr
.class != ASN1_CLASS_UNIVERSAL
||
991 hdr
.tag
!= ASN1_TAG_INTEGER
) {
992 wpa_printf(MSG_DEBUG
, "X509: No INTEGER tag found for "
993 "version field - found class %d tag 0x%x",
997 if (hdr
.length
!= 1) {
998 wpa_printf(MSG_DEBUG
, "X509: Unexpected version field "
999 "length %u (expected 1)", hdr
.length
);
1011 cert
->version
= value
;
1012 if (cert
->version
!= X509_CERT_V1
&&
1013 cert
->version
!= X509_CERT_V2
&&
1014 cert
->version
!= X509_CERT_V3
) {
1015 wpa_printf(MSG_DEBUG
, "X509: Unsupported version %d",
1020 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0)
1023 cert
->version
= X509_CERT_V1
;
1024 wpa_printf(MSG_MSGDUMP
, "X509: Version X.509v%d", cert
->version
+ 1);
1026 /* serialNumber CertificateSerialNumber ::= INTEGER */
1027 if (hdr
.class != ASN1_CLASS_UNIVERSAL
||
1028 hdr
.tag
!= ASN1_TAG_INTEGER
) {
1029 wpa_printf(MSG_DEBUG
, "X509: No INTEGER tag found for "
1030 "serialNumber; class=%d tag=0x%x",
1031 hdr
.class, hdr
.tag
);
1038 cert
->serial_number
<<= 8;
1039 cert
->serial_number
|= *pos
++;
1042 wpa_printf(MSG_MSGDUMP
, "X509: serialNumber %lu", cert
->serial_number
);
1044 /* signature AlgorithmIdentifier */
1045 if (x509_parse_algorithm_identifier(pos
, end
- pos
, &cert
->signature
,
1050 if (x509_parse_name(pos
, end
- pos
, &cert
->issuer
, &pos
))
1052 x509_name_string(&cert
->issuer
, sbuf
, sizeof(sbuf
));
1053 wpa_printf(MSG_MSGDUMP
, "X509: issuer %s", sbuf
);
1055 /* validity Validity */
1056 if (x509_parse_validity(pos
, end
- pos
, cert
, &pos
))
1060 if (x509_parse_name(pos
, end
- pos
, &cert
->subject
, &pos
))
1062 x509_name_string(&cert
->subject
, sbuf
, sizeof(sbuf
));
1063 wpa_printf(MSG_MSGDUMP
, "X509: subject %s", sbuf
);
1065 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1066 if (x509_parse_public_key(pos
, end
- pos
, cert
, &pos
))
1072 if (cert
->version
== X509_CERT_V1
)
1075 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1076 hdr
.class != ASN1_CLASS_CONTEXT_SPECIFIC
) {
1077 wpa_printf(MSG_DEBUG
, "X509: Expected Context-Specific"
1078 " tag to parse optional tbsCertificate "
1079 "field(s); parsed class %d tag 0x%x",
1080 hdr
.class, hdr
.tag
);
1085 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1086 wpa_printf(MSG_DEBUG
, "X509: issuerUniqueID");
1087 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1089 if (hdr
.payload
+ hdr
.length
== end
)
1092 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1093 hdr
.class != ASN1_CLASS_CONTEXT_SPECIFIC
) {
1094 wpa_printf(MSG_DEBUG
, "X509: Expected Context-Specific"
1095 " tag to parse optional tbsCertificate "
1096 "field(s); parsed class %d tag 0x%x",
1097 hdr
.class, hdr
.tag
);
1103 /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
1104 wpa_printf(MSG_DEBUG
, "X509: subjectUniqueID");
1105 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1107 if (hdr
.payload
+ hdr
.length
== end
)
1110 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1111 hdr
.class != ASN1_CLASS_CONTEXT_SPECIFIC
) {
1112 wpa_printf(MSG_DEBUG
, "X509: Expected Context-Specific"
1113 " tag to parse optional tbsCertificate "
1114 "field(s); parsed class %d tag 0x%x",
1115 hdr
.class, hdr
.tag
);
1121 wpa_printf(MSG_DEBUG
, "X509: Ignored unexpected "
1122 "Context-Specific tag %d in optional "
1123 "tbsCertificate fields", hdr
.tag
);
1127 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1129 if (cert
->version
!= X509_CERT_V3
) {
1130 wpa_printf(MSG_DEBUG
, "X509: X.509%d certificate and "
1131 "Extensions data which are only allowed for "
1132 "version 3", cert
->version
+ 1);
1136 if (x509_parse_extensions(cert
, hdr
.payload
, hdr
.length
) < 0)
1139 pos
= hdr
.payload
+ hdr
.length
;
1141 wpa_hexdump(MSG_DEBUG
,
1142 "X509: Ignored extra tbsCertificate data",
1150 static int x509_rsadsi_oid(struct asn1_oid
*oid
)
1152 return oid
->len
>= 4 &&
1153 oid
->oid
[0] == 1 /* iso */ &&
1154 oid
->oid
[1] == 2 /* member-body */ &&
1155 oid
->oid
[2] == 840 /* us */ &&
1156 oid
->oid
[3] == 113549 /* rsadsi */;
1160 static int x509_pkcs_oid(struct asn1_oid
*oid
)
1162 return oid
->len
>= 5 &&
1163 x509_rsadsi_oid(oid
) &&
1164 oid
->oid
[4] == 1 /* pkcs */;
1168 static int x509_digest_oid(struct asn1_oid
*oid
)
1170 return oid
->len
>= 5 &&
1171 x509_rsadsi_oid(oid
) &&
1172 oid
->oid
[4] == 2 /* digestAlgorithm */;
1176 static int x509_sha1_oid(struct asn1_oid
*oid
)
1178 return oid
->len
== 6 &&
1179 oid
->oid
[0] == 1 /* iso */ &&
1180 oid
->oid
[1] == 3 /* identified-organization */ &&
1181 oid
->oid
[2] == 14 /* oiw */ &&
1182 oid
->oid
[3] == 3 /* secsig */ &&
1183 oid
->oid
[4] == 2 /* algorithms */ &&
1184 oid
->oid
[5] == 26 /* id-sha1 */;
1189 * x509_certificate_parse - Parse a X.509 certificate in DER format
1190 * @buf: Pointer to the X.509 certificate in DER format
1191 * @len: Buffer length
1192 * Returns: Pointer to the parsed certificate or %NULL on failure
1194 * Caller is responsible for freeing the returned certificate by calling
1195 * x509_certificate_free().
1197 struct x509_certificate
* x509_certificate_parse(const u8
*buf
, size_t len
)
1199 struct asn1_hdr hdr
;
1200 const u8
*pos
, *end
, *hash_start
;
1201 struct x509_certificate
*cert
;
1203 cert
= os_zalloc(sizeof(*cert
) + len
);
1206 os_memcpy(cert
+ 1, buf
, len
);
1207 cert
->cert_start
= (u8
*) (cert
+ 1);
1208 cert
->cert_len
= len
;
1213 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1215 /* Certificate ::= SEQUENCE */
1216 if (asn1_get_next(pos
, len
, &hdr
) < 0 ||
1217 hdr
.class != ASN1_CLASS_UNIVERSAL
||
1218 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
1219 wpa_printf(MSG_DEBUG
, "X509: Certificate did not start with "
1220 "a valid SEQUENCE - found class %d tag 0x%x",
1221 hdr
.class, hdr
.tag
);
1222 x509_certificate_free(cert
);
1227 if (pos
+ hdr
.length
> end
) {
1228 x509_certificate_free(cert
);
1232 if (pos
+ hdr
.length
< end
) {
1233 wpa_hexdump(MSG_MSGDUMP
, "X509: Ignoring extra data after DER "
1234 "encoded certificate",
1235 pos
+ hdr
.length
, end
- pos
+ hdr
.length
);
1236 end
= pos
+ hdr
.length
;
1240 cert
->tbs_cert_start
= cert
->cert_start
+ (hash_start
- buf
);
1241 if (x509_parse_tbs_certificate(pos
, end
- pos
, cert
, &pos
)) {
1242 x509_certificate_free(cert
);
1245 cert
->tbs_cert_len
= pos
- hash_start
;
1247 /* signatureAlgorithm AlgorithmIdentifier */
1248 if (x509_parse_algorithm_identifier(pos
, end
- pos
,
1249 &cert
->signature_alg
, &pos
)) {
1250 x509_certificate_free(cert
);
1254 /* signatureValue BIT STRING */
1255 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1256 hdr
.class != ASN1_CLASS_UNIVERSAL
||
1257 hdr
.tag
!= ASN1_TAG_BITSTRING
) {
1258 wpa_printf(MSG_DEBUG
, "X509: Expected BITSTRING "
1259 "(signatureValue) - found class %d tag 0x%x",
1260 hdr
.class, hdr
.tag
);
1261 x509_certificate_free(cert
);
1264 if (hdr
.length
< 1) {
1265 x509_certificate_free(cert
);
1270 wpa_printf(MSG_DEBUG
, "X509: BITSTRING - %d unused bits",
1272 /* PKCS #1 v1.5 10.2.1:
1273 * It is an error if the length in bits of the signature S is
1274 * not a multiple of eight.
1276 x509_certificate_free(cert
);
1279 os_free(cert
->sign_value
);
1280 cert
->sign_value
= os_malloc(hdr
.length
- 1);
1281 if (cert
->sign_value
== NULL
) {
1282 wpa_printf(MSG_DEBUG
, "X509: Failed to allocate memory for "
1284 x509_certificate_free(cert
);
1287 os_memcpy(cert
->sign_value
, pos
+ 1, hdr
.length
- 1);
1288 cert
->sign_value_len
= hdr
.length
- 1;
1289 wpa_hexdump(MSG_MSGDUMP
, "X509: signature",
1290 cert
->sign_value
, cert
->sign_value_len
);
1297 * x509_certificate_check_signature - Verify certificate signature
1298 * @issuer: Issuer certificate
1299 * @cert: Certificate to be verified
1300 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1303 int x509_certificate_check_signature(struct x509_certificate
*issuer
,
1304 struct x509_certificate
*cert
)
1306 struct crypto_public_key
*pk
;
1308 const u8
*pos
, *end
, *next
, *da_end
;
1310 struct asn1_hdr hdr
;
1311 struct asn1_oid oid
;
1315 if (!x509_pkcs_oid(&cert
->signature
.oid
) ||
1316 cert
->signature
.oid
.len
!= 7 ||
1317 cert
->signature
.oid
.oid
[5] != 1 /* pkcs-1 */) {
1318 wpa_printf(MSG_DEBUG
, "X509: Unrecognized signature "
1323 pk
= crypto_public_key_import(issuer
->public_key
,
1324 issuer
->public_key_len
);
1328 data_len
= cert
->sign_value_len
;
1329 data
= os_malloc(data_len
);
1331 crypto_public_key_free(pk
);
1335 if (crypto_public_key_decrypt_pkcs1(pk
, cert
->sign_value
,
1336 cert
->sign_value_len
, data
,
1338 wpa_printf(MSG_DEBUG
, "X509: Failed to decrypt signature");
1339 crypto_public_key_free(pk
);
1343 crypto_public_key_free(pk
);
1345 wpa_hexdump(MSG_MSGDUMP
, "X509: Signature data D", data
, data_len
);
1348 * PKCS #1 v1.5, 10.1.2:
1350 * DigestInfo ::= SEQUENCE {
1351 * digestAlgorithm DigestAlgorithmIdentifier,
1355 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1357 * Digest ::= OCTET STRING
1360 if (asn1_get_next(data
, data_len
, &hdr
) < 0 ||
1361 hdr
.class != ASN1_CLASS_UNIVERSAL
||
1362 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
1363 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
1364 "(DigestInfo) - found class %d tag 0x%x",
1365 hdr
.class, hdr
.tag
);
1371 end
= pos
+ hdr
.length
;
1375 * AlgorithmIdentifier ::= SEQUENCE {
1376 * algorithm OBJECT IDENTIFIER,
1377 * parameters ANY DEFINED BY algorithm OPTIONAL
1381 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1382 hdr
.class != ASN1_CLASS_UNIVERSAL
||
1383 hdr
.tag
!= ASN1_TAG_SEQUENCE
) {
1384 wpa_printf(MSG_DEBUG
, "X509: Expected SEQUENCE "
1385 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1386 hdr
.class, hdr
.tag
);
1390 da_end
= hdr
.payload
+ hdr
.length
;
1392 if (asn1_get_oid(hdr
.payload
, hdr
.length
, &oid
, &next
)) {
1393 wpa_printf(MSG_DEBUG
, "X509: Failed to parse digestAlgorithm");
1398 if (x509_sha1_oid(&oid
)) {
1399 if (cert
->signature
.oid
.oid
[6] !=
1400 5 /* sha-1WithRSAEncryption */) {
1401 wpa_printf(MSG_DEBUG
, "X509: digestAlgorithm SHA1 "
1402 "does not match with certificate "
1403 "signatureAlgorithm (%lu)",
1404 cert
->signature
.oid
.oid
[6]);
1408 goto skip_digest_oid
;
1411 if (!x509_digest_oid(&oid
)) {
1412 wpa_printf(MSG_DEBUG
, "X509: Unrecognized digestAlgorithm");
1416 switch (oid
.oid
[5]) {
1418 if (cert
->signature
.oid
.oid
[6] != 4 /* md5WithRSAEncryption */)
1420 wpa_printf(MSG_DEBUG
, "X509: digestAlgorithm MD5 does "
1421 "not match with certificate "
1422 "signatureAlgorithm (%lu)",
1423 cert
->signature
.oid
.oid
[6]);
1431 wpa_printf(MSG_DEBUG
, "X509: Unsupported digestAlgorithm "
1432 "(%lu)", oid
.oid
[5]);
1438 /* Digest ::= OCTET STRING */
1440 end
= data
+ data_len
;
1442 if (asn1_get_next(pos
, end
- pos
, &hdr
) < 0 ||
1443 hdr
.class != ASN1_CLASS_UNIVERSAL
||
1444 hdr
.tag
!= ASN1_TAG_OCTETSTRING
) {
1445 wpa_printf(MSG_DEBUG
, "X509: Expected OCTETSTRING "
1446 "(Digest) - found class %d tag 0x%x",
1447 hdr
.class, hdr
.tag
);
1451 wpa_hexdump(MSG_MSGDUMP
, "X509: Decrypted Digest",
1452 hdr
.payload
, hdr
.length
);
1454 switch (cert
->signature
.oid
.oid
[6]) {
1455 case 4: /* md5WithRSAEncryption */
1456 md5_vector(1, &cert
->tbs_cert_start
, &cert
->tbs_cert_len
,
1459 wpa_hexdump(MSG_MSGDUMP
, "X509: Certificate hash (MD5)",
1462 case 5: /* sha-1WithRSAEncryption */
1463 sha1_vector(1, &cert
->tbs_cert_start
, &cert
->tbs_cert_len
,
1466 wpa_hexdump(MSG_MSGDUMP
, "X509: Certificate hash (SHA1)",
1469 case 2: /* md2WithRSAEncryption */
1470 case 11: /* sha256WithRSAEncryption */
1471 case 12: /* sha384WithRSAEncryption */
1472 case 13: /* sha512WithRSAEncryption */
1474 wpa_printf(MSG_INFO
, "X509: Unsupported certificate signature "
1475 "algorithm (%lu)", cert
->signature
.oid
.oid
[6]);
1480 if (hdr
.length
!= hash_len
||
1481 os_memcmp(hdr
.payload
, hash
, hdr
.length
) != 0) {
1482 wpa_printf(MSG_INFO
, "X509: Certificate Digest does not match "
1483 "with calculated tbsCertificate hash");
1490 wpa_printf(MSG_DEBUG
, "X509: Certificate Digest matches with "
1491 "calculated tbsCertificate hash");
1497 static int x509_valid_issuer(const struct x509_certificate
*cert
)
1499 if ((cert
->extensions_present
& X509_EXT_BASIC_CONSTRAINTS
) &&
1501 wpa_printf(MSG_DEBUG
, "X509: Non-CA certificate used as an "
1506 if (cert
->version
== X509_CERT_V3
&&
1507 !(cert
->extensions_present
& X509_EXT_BASIC_CONSTRAINTS
)) {
1508 wpa_printf(MSG_DEBUG
, "X509: v3 CA certificate did not "
1509 "include BasicConstraints extension");
1513 if ((cert
->extensions_present
& X509_EXT_KEY_USAGE
) &&
1514 !(cert
->key_usage
& X509_KEY_USAGE_KEY_CERT_SIGN
)) {
1515 wpa_printf(MSG_DEBUG
, "X509: Issuer certificate did not have "
1516 "keyCertSign bit in Key Usage");
1525 * x509_certificate_chain_validate - Validate X.509 certificate chain
1526 * @trusted: List of trusted certificates
1527 * @chain: Certificate chain to be validated (first chain must be issued by
1528 * signed by the second certificate in the chain and so on)
1529 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
1530 * Returns: 0 if chain is valid, -1 if not
1532 int x509_certificate_chain_validate(struct x509_certificate
*trusted
,
1533 struct x509_certificate
*chain
,
1537 int chain_trusted
= 0;
1538 struct x509_certificate
*cert
, *trust
;
1542 *reason
= X509_VALIDATE_OK
;
1544 wpa_printf(MSG_DEBUG
, "X509: Validate certificate chain");
1547 for (cert
= chain
, idx
= 0; cert
; cert
= cert
->next
, idx
++) {
1548 x509_name_string(&cert
->subject
, buf
, sizeof(buf
));
1549 wpa_printf(MSG_DEBUG
, "X509: %lu: %s", idx
, buf
);
1554 if ((unsigned long) now
.sec
<
1555 (unsigned long) cert
->not_before
||
1556 (unsigned long) now
.sec
>
1557 (unsigned long) cert
->not_after
) {
1558 wpa_printf(MSG_INFO
, "X509: Certificate not valid "
1559 "(now=%lu not_before=%lu not_after=%lu)",
1560 now
.sec
, cert
->not_before
, cert
->not_after
);
1561 *reason
= X509_VALIDATE_CERTIFICATE_EXPIRED
;
1566 if (x509_name_compare(&cert
->issuer
,
1567 &cert
->next
->subject
) != 0) {
1568 wpa_printf(MSG_DEBUG
, "X509: Certificate "
1569 "chain issuer name mismatch");
1570 x509_name_string(&cert
->issuer
, buf
,
1572 wpa_printf(MSG_DEBUG
, "X509: cert issuer: %s",
1574 x509_name_string(&cert
->next
->subject
, buf
,
1576 wpa_printf(MSG_DEBUG
, "X509: next cert "
1577 "subject: %s", buf
);
1578 *reason
= X509_VALIDATE_CERTIFICATE_UNKNOWN
;
1582 if (x509_valid_issuer(cert
->next
) < 0) {
1583 *reason
= X509_VALIDATE_BAD_CERTIFICATE
;
1587 if ((cert
->next
->extensions_present
&
1588 X509_EXT_PATH_LEN_CONSTRAINT
) &&
1589 idx
> cert
->next
->path_len_constraint
) {
1590 wpa_printf(MSG_DEBUG
, "X509: pathLenConstraint"
1591 " not met (idx=%lu issuer "
1592 "pathLenConstraint=%lu)", idx
,
1593 cert
->next
->path_len_constraint
);
1594 *reason
= X509_VALIDATE_BAD_CERTIFICATE
;
1598 if (x509_certificate_check_signature(cert
->next
, cert
)
1600 wpa_printf(MSG_DEBUG
, "X509: Invalid "
1601 "certificate signature within "
1603 *reason
= X509_VALIDATE_BAD_CERTIFICATE
;
1608 for (trust
= trusted
; trust
; trust
= trust
->next
) {
1609 if (x509_name_compare(&cert
->issuer
, &trust
->subject
)
1615 wpa_printf(MSG_DEBUG
, "X509: Found issuer from the "
1616 "list of trusted certificates");
1617 if (x509_valid_issuer(trust
) < 0) {
1618 *reason
= X509_VALIDATE_BAD_CERTIFICATE
;
1622 if (x509_certificate_check_signature(trust
, cert
) < 0)
1624 wpa_printf(MSG_DEBUG
, "X509: Invalid "
1625 "certificate signature");
1626 *reason
= X509_VALIDATE_BAD_CERTIFICATE
;
1630 wpa_printf(MSG_DEBUG
, "X509: Trusted certificate "
1631 "found to complete the chain");
1636 if (!chain_trusted
) {
1637 wpa_printf(MSG_DEBUG
, "X509: Did not find any of the issuers "
1638 "from the list of trusted certificates");
1640 *reason
= X509_VALIDATE_UNKNOWN_CA
;
1643 wpa_printf(MSG_DEBUG
, "X509: Certificate chain validation "
1644 "disabled - ignore unknown CA issue");
1647 wpa_printf(MSG_DEBUG
, "X509: Certificate chain valid");
1654 * x509_certificate_get_subject - Get a certificate based on Subject name
1655 * @chain: Certificate chain to search through
1656 * @name: Subject name to search for
1657 * Returns: Pointer to the certificate with the given Subject name or
1660 struct x509_certificate
*
1661 x509_certificate_get_subject(struct x509_certificate
*chain
,
1662 struct x509_name
*name
)
1664 struct x509_certificate
*cert
;
1666 for (cert
= chain
; cert
; cert
= cert
->next
) {
1667 if (x509_name_compare(&cert
->subject
, name
) == 0)
1675 * x509_certificate_self_signed - Is the certificate self-signed?
1676 * @cert: Certificate
1677 * Returns: 1 if certificate is self-signed, 0 if not
1679 int x509_certificate_self_signed(struct x509_certificate
*cert
)
1681 return x509_name_compare(&cert
->issuer
, &cert
->subject
) == 0;
1684 #endif /* CONFIG_INTERNAL_X509 */