2 * X.509v3 certificate parsing and processing (RFC 3280 profile)
3 * Copyright (c) 2006-2011, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/crypto.h"
17 static void x509_free_name(struct x509_name *name)
21 for (i = 0; i < name->num_attr; i++) {
22 os_free(name->attr[i].value);
23 name->attr[i].value = NULL;
24 name->attr[i].type = X509_NAME_ATTR_NOT_USED;
30 os_free(name->alt_email);
34 name->alt_email = name->dns = name->uri = NULL;
37 os_memset(&name->rid, 0, sizeof(name->rid));
42 * x509_certificate_free - Free an X.509 certificate
43 * @cert: Certificate to be freed
45 void x509_certificate_free(struct x509_certificate *cert)
50 wpa_printf(MSG_DEBUG, "X509: x509_certificate_free: cer=%p "
51 "was still on a list (next=%p)\n",
54 x509_free_name(&cert->issuer);
55 x509_free_name(&cert->subject);
56 os_free(cert->public_key);
57 os_free(cert->sign_value);
63 * x509_certificate_free - Free an X.509 certificate chain
64 * @cert: Pointer to the first certificate in the chain
66 void x509_certificate_chain_free(struct x509_certificate *cert)
68 struct x509_certificate *next;
73 x509_certificate_free(cert);
79 static int x509_whitespace(char c)
81 return c == ' ' || c == '\t';
85 static void x509_str_strip_whitespace(char *a)
88 int remove_whitespace = 1;
93 if (remove_whitespace && x509_whitespace(*ipos))
96 remove_whitespace = x509_whitespace(*ipos);
102 if (opos > a && x509_whitespace(*opos))
107 static int x509_str_compare(const char *a, const char *b)
122 if (aa == NULL || bb == NULL) {
125 return os_strcasecmp(a, b);
128 x509_str_strip_whitespace(aa);
129 x509_str_strip_whitespace(bb);
131 ret = os_strcasecmp(aa, bb);
141 * x509_name_compare - Compare X.509 certificate names
142 * @a: Certificate name
143 * @b: Certificate name
144 * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
147 int x509_name_compare(struct x509_name *a, struct x509_name *b)
158 if (a->num_attr < b->num_attr)
160 if (a->num_attr > b->num_attr)
163 for (i = 0; i < a->num_attr; i++) {
164 if (a->attr[i].type < b->attr[i].type)
166 if (a->attr[i].type > b->attr[i].type)
168 res = x509_str_compare(a->attr[i].value, b->attr[i].value);
172 res = x509_str_compare(a->email, b->email);
180 static int x509_parse_algorithm_identifier(
181 const u8 *buf, size_t len,
182 struct x509_algorithm_identifier *id, const u8 **next)
188 * AlgorithmIdentifier ::= SEQUENCE {
189 * algorithm OBJECT IDENTIFIER,
190 * parameters ANY DEFINED BY algorithm OPTIONAL
194 if (asn1_get_next(buf, len, &hdr) < 0 ||
195 hdr.class != ASN1_CLASS_UNIVERSAL ||
196 hdr.tag != ASN1_TAG_SEQUENCE) {
197 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
198 "(AlgorithmIdentifier) - found class %d tag 0x%x",
203 end = pos + hdr.length;
210 if (asn1_get_oid(pos, end - pos, &id->oid, &pos))
213 /* TODO: optional parameters */
219 static int x509_parse_public_key(const u8 *buf, size_t len,
220 struct x509_certificate *cert,
227 * SubjectPublicKeyInfo ::= SEQUENCE {
228 * algorithm AlgorithmIdentifier,
229 * subjectPublicKey BIT STRING
236 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
237 hdr.class != ASN1_CLASS_UNIVERSAL ||
238 hdr.tag != ASN1_TAG_SEQUENCE) {
239 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
240 "(SubjectPublicKeyInfo) - found class %d tag 0x%x",
246 if (pos + hdr.length > end)
248 end = pos + hdr.length;
251 if (x509_parse_algorithm_identifier(pos, end - pos,
252 &cert->public_key_alg, &pos))
255 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
256 hdr.class != ASN1_CLASS_UNIVERSAL ||
257 hdr.tag != ASN1_TAG_BITSTRING) {
258 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
259 "(subjectPublicKey) - found class %d tag 0x%x",
267 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
270 * TODO: should this be rejected? X.509 certificates are
271 * unlikely to use such a construction. Now we would end up
272 * including the extra bits in the buffer which may also be
276 os_free(cert->public_key);
277 cert->public_key = os_malloc(hdr.length - 1);
278 if (cert->public_key == NULL) {
279 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
283 os_memcpy(cert->public_key, pos + 1, hdr.length - 1);
284 cert->public_key_len = hdr.length - 1;
285 wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
286 cert->public_key, cert->public_key_len);
292 static int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
296 const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
301 * Name ::= CHOICE { RDNSequence }
302 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
303 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
304 * AttributeTypeAndValue ::= SEQUENCE {
305 * type AttributeType,
306 * value AttributeValue
308 * AttributeType ::= OBJECT IDENTIFIER
309 * AttributeValue ::= ANY DEFINED BY AttributeType
312 if (asn1_get_next(buf, len, &hdr) < 0 ||
313 hdr.class != ASN1_CLASS_UNIVERSAL ||
314 hdr.tag != ASN1_TAG_SEQUENCE) {
315 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
316 "(Name / RDNSequencer) - found class %d tag 0x%x",
322 if (pos + hdr.length > buf + len)
325 end = *next = pos + hdr.length;
328 enum x509_name_attr_type type;
330 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
331 hdr.class != ASN1_CLASS_UNIVERSAL ||
332 hdr.tag != ASN1_TAG_SET) {
333 wpa_printf(MSG_DEBUG, "X509: Expected SET "
334 "(RelativeDistinguishedName) - found class "
335 "%d tag 0x%x", hdr.class, hdr.tag);
336 x509_free_name(name);
340 set_pos = hdr.payload;
341 pos = set_end = hdr.payload + hdr.length;
343 if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
344 hdr.class != ASN1_CLASS_UNIVERSAL ||
345 hdr.tag != ASN1_TAG_SEQUENCE) {
346 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
347 "(AttributeTypeAndValue) - found class %d "
348 "tag 0x%x", hdr.class, hdr.tag);
349 x509_free_name(name);
353 seq_pos = hdr.payload;
354 seq_end = hdr.payload + hdr.length;
356 if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
357 x509_free_name(name);
361 if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
362 hdr.class != ASN1_CLASS_UNIVERSAL) {
363 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
365 x509_free_name(name);
370 * MUST: country, organization, organizational-unit,
371 * distinguished name qualifier, state or province name,
372 * common name, serial number.
373 * SHOULD: locality, title, surname, given name, initials,
374 * pseudonym, generation qualifier.
375 * MUST: domainComponent (RFC 2247).
377 type = X509_NAME_ATTR_NOT_USED;
379 oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
380 /* id-at ::= 2.5.4 */
381 switch (oid.oid[3]) {
384 type = X509_NAME_ATTR_CN;
388 type = X509_NAME_ATTR_C;
392 type = X509_NAME_ATTR_L;
395 /* stateOrProvinceName */
396 type = X509_NAME_ATTR_ST;
399 /* organizationName */
400 type = X509_NAME_ATTR_O;
403 /* organizationalUnitName */
404 type = X509_NAME_ATTR_OU;
407 } else if (oid.len == 7 &&
408 oid.oid[0] == 1 && oid.oid[1] == 2 &&
409 oid.oid[2] == 840 && oid.oid[3] == 113549 &&
410 oid.oid[4] == 1 && oid.oid[5] == 9 &&
412 /* 1.2.840.113549.1.9.1 - e-mailAddress */
413 os_free(name->email);
414 name->email = os_malloc(hdr.length + 1);
415 if (name->email == NULL) {
416 x509_free_name(name);
419 os_memcpy(name->email, hdr.payload, hdr.length);
420 name->email[hdr.length] = '\0';
422 } else if (oid.len == 7 &&
423 oid.oid[0] == 0 && oid.oid[1] == 9 &&
424 oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
425 oid.oid[4] == 100 && oid.oid[5] == 1 &&
427 /* 0.9.2342.19200300.100.1.25 - domainComponent */
428 type = X509_NAME_ATTR_DC;
431 if (type == X509_NAME_ATTR_NOT_USED) {
432 wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
434 oid.len * sizeof(oid.oid[0]));
435 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
436 hdr.payload, hdr.length);
440 if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
441 wpa_printf(MSG_INFO, "X509: Too many Name attributes");
442 x509_free_name(name);
446 val = os_malloc(hdr.length + 1);
448 x509_free_name(name);
451 os_memcpy(val, hdr.payload, hdr.length);
452 val[hdr.length] = '\0';
453 if (os_strlen(val) != hdr.length) {
454 wpa_printf(MSG_INFO, "X509: Reject certificate with "
455 "embedded NUL byte in a string (%s[NUL])",
457 x509_free_name(name);
461 name->attr[name->num_attr].type = type;
462 name->attr[name->num_attr].value = val;
470 static char * x509_name_attr_str(enum x509_name_attr_type type)
473 case X509_NAME_ATTR_NOT_USED:
475 case X509_NAME_ATTR_DC:
477 case X509_NAME_ATTR_CN:
479 case X509_NAME_ATTR_C:
481 case X509_NAME_ATTR_L:
483 case X509_NAME_ATTR_ST:
485 case X509_NAME_ATTR_O:
487 case X509_NAME_ATTR_OU:
495 * x509_name_string - Convert an X.509 certificate name into a string
496 * @name: Name to convert
497 * @buf: Buffer for the string
498 * @len: Maximum buffer length
500 void x509_name_string(struct x509_name *name, char *buf, size_t len)
512 for (i = 0; i < name->num_attr; i++) {
513 ret = os_snprintf(pos, end - pos, "%s=%s, ",
514 x509_name_attr_str(name->attr[i].type),
515 name->attr[i].value);
516 if (ret < 0 || ret >= end - pos)
521 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
529 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
531 if (ret < 0 || ret >= end - pos)
541 static int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag,
545 int year, month, day, hour, min, sec;
550 * generalTime GeneralizedTime
553 * UTCTime: YYMMDDHHMMSSZ
554 * GeneralizedTime: YYYYMMDDHHMMSSZ
557 pos = (const char *) buf;
560 case ASN1_TAG_UTCTIME:
561 if (len != 13 || buf[12] != 'Z') {
562 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
563 "UTCTime format", buf, len);
566 if (sscanf(pos, "%02d", &year) != 1) {
567 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
568 "UTCTime year", buf, len);
577 case ASN1_TAG_GENERALIZEDTIME:
578 if (len != 15 || buf[14] != 'Z') {
579 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
580 "GeneralizedTime format", buf, len);
583 if (sscanf(pos, "%04d", &year) != 1) {
584 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
585 "GeneralizedTime year", buf, len);
591 wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
592 "GeneralizedTime - found tag 0x%x", asn1_tag);
596 if (sscanf(pos, "%02d", &month) != 1) {
597 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
598 "(month)", buf, len);
603 if (sscanf(pos, "%02d", &day) != 1) {
604 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
610 if (sscanf(pos, "%02d", &hour) != 1) {
611 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
617 if (sscanf(pos, "%02d", &min) != 1) {
618 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
624 if (sscanf(pos, "%02d", &sec) != 1) {
625 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
630 if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
631 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
635 * At least some test certificates have been configured
636 * to use dates prior to 1970. Set the date to
637 * beginning of 1970 to handle these case.
639 wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
640 "assume epoch as the time", year);
651 static int x509_parse_validity(const u8 *buf, size_t len,
652 struct x509_certificate *cert, const u8 **next)
659 * Validity ::= SEQUENCE {
665 * CAs conforming to this profile MUST always encode certificate
666 * validity dates through the year 2049 as UTCTime; certificate
667 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
670 if (asn1_get_next(buf, len, &hdr) < 0 ||
671 hdr.class != ASN1_CLASS_UNIVERSAL ||
672 hdr.tag != ASN1_TAG_SEQUENCE) {
673 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
674 "(Validity) - found class %d tag 0x%x",
681 if (pos + plen > buf + len)
686 if (asn1_get_next(pos, plen, &hdr) < 0 ||
687 hdr.class != ASN1_CLASS_UNIVERSAL ||
688 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
689 &cert->not_before) < 0) {
690 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
691 "Time", hdr.payload, hdr.length);
695 pos = hdr.payload + hdr.length;
698 if (asn1_get_next(pos, plen, &hdr) < 0 ||
699 hdr.class != ASN1_CLASS_UNIVERSAL ||
700 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
701 &cert->not_after) < 0) {
702 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
703 "Time", hdr.payload, hdr.length);
707 wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
708 (unsigned long) cert->not_before,
709 (unsigned long) cert->not_after);
715 static int x509_id_ce_oid(struct asn1_oid *oid)
717 /* id-ce arc from X.509 for standard X.509v3 extensions */
718 return oid->len >= 4 &&
719 oid->oid[0] == 2 /* joint-iso-ccitt */ &&
720 oid->oid[1] == 5 /* ds */ &&
721 oid->oid[2] == 29 /* id-ce */;
725 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
726 const u8 *pos, size_t len)
731 * KeyUsage ::= BIT STRING {
732 * digitalSignature (0),
733 * nonRepudiation (1),
734 * keyEncipherment (2),
735 * dataEncipherment (3),
743 if (asn1_get_next(pos, len, &hdr) < 0 ||
744 hdr.class != ASN1_CLASS_UNIVERSAL ||
745 hdr.tag != ASN1_TAG_BITSTRING ||
747 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
748 "KeyUsage; found %d tag 0x%x len %d",
749 hdr.class, hdr.tag, hdr.length);
753 cert->extensions_present |= X509_EXT_KEY_USAGE;
754 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
756 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
762 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
763 const u8 *pos, size_t len)
770 * BasicConstraints ::= SEQUENCE {
771 * cA BOOLEAN DEFAULT FALSE,
772 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
775 if (asn1_get_next(pos, len, &hdr) < 0 ||
776 hdr.class != ASN1_CLASS_UNIVERSAL ||
777 hdr.tag != ASN1_TAG_SEQUENCE) {
778 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
779 "BasicConstraints; found %d tag 0x%x",
784 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
789 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
790 hdr.class != ASN1_CLASS_UNIVERSAL) {
791 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
796 if (hdr.tag == ASN1_TAG_BOOLEAN) {
797 if (hdr.length != 1) {
798 wpa_printf(MSG_DEBUG, "X509: Unexpected "
799 "Boolean length (%u) in BasicConstraints",
803 cert->ca = hdr.payload[0];
805 if (hdr.payload + hdr.length == pos + len) {
806 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
811 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
813 hdr.class != ASN1_CLASS_UNIVERSAL) {
814 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
820 if (hdr.tag != ASN1_TAG_INTEGER) {
821 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
822 "BasicConstraints; found class %d tag 0x%x",
836 cert->path_len_constraint = value;
837 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
839 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
840 "pathLenConstraint=%lu",
841 cert->ca, cert->path_len_constraint);
847 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
848 const u8 *pos, size_t len)
850 /* rfc822Name IA5String */
851 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
852 os_free(name->alt_email);
853 name->alt_email = os_zalloc(len + 1);
854 if (name->alt_email == NULL)
856 os_memcpy(name->alt_email, pos, len);
857 if (os_strlen(name->alt_email) != len) {
858 wpa_printf(MSG_INFO, "X509: Reject certificate with "
859 "embedded NUL byte in rfc822Name (%s[NUL])",
861 os_free(name->alt_email);
862 name->alt_email = NULL;
869 static int x509_parse_alt_name_dns(struct x509_name *name,
870 const u8 *pos, size_t len)
872 /* dNSName IA5String */
873 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
875 name->dns = os_zalloc(len + 1);
876 if (name->dns == NULL)
878 os_memcpy(name->dns, pos, len);
879 if (os_strlen(name->dns) != len) {
880 wpa_printf(MSG_INFO, "X509: Reject certificate with "
881 "embedded NUL byte in dNSName (%s[NUL])",
891 static int x509_parse_alt_name_uri(struct x509_name *name,
892 const u8 *pos, size_t len)
894 /* uniformResourceIdentifier IA5String */
895 wpa_hexdump_ascii(MSG_MSGDUMP,
896 "X509: altName - uniformResourceIdentifier",
899 name->uri = os_zalloc(len + 1);
900 if (name->uri == NULL)
902 os_memcpy(name->uri, pos, len);
903 if (os_strlen(name->uri) != len) {
904 wpa_printf(MSG_INFO, "X509: Reject certificate with "
905 "embedded NUL byte in uniformResourceIdentifier "
906 "(%s[NUL])", name->uri);
915 static int x509_parse_alt_name_ip(struct x509_name *name,
916 const u8 *pos, size_t len)
918 /* iPAddress OCTET STRING */
919 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
921 name->ip = os_malloc(len);
922 if (name->ip == NULL)
924 os_memcpy(name->ip, pos, len);
930 static int x509_parse_alt_name_rid(struct x509_name *name,
931 const u8 *pos, size_t len)
935 /* registeredID OBJECT IDENTIFIER */
936 if (asn1_parse_oid(pos, len, &name->rid) < 0)
939 asn1_oid_to_str(&name->rid, buf, sizeof(buf));
940 wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
946 static int x509_parse_ext_alt_name(struct x509_name *name,
947 const u8 *pos, size_t len)
953 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
955 * GeneralName ::= CHOICE {
956 * otherName [0] OtherName,
957 * rfc822Name [1] IA5String,
958 * dNSName [2] IA5String,
959 * x400Address [3] ORAddress,
960 * directoryName [4] Name,
961 * ediPartyName [5] EDIPartyName,
962 * uniformResourceIdentifier [6] IA5String,
963 * iPAddress [7] OCTET STRING,
964 * registeredID [8] OBJECT IDENTIFIER }
966 * OtherName ::= SEQUENCE {
967 * type-id OBJECT IDENTIFIER,
968 * value [0] EXPLICIT ANY DEFINED BY type-id }
970 * EDIPartyName ::= SEQUENCE {
971 * nameAssigner [0] DirectoryString OPTIONAL,
972 * partyName [1] DirectoryString }
975 for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
978 if (asn1_get_next(p, end - p, &hdr) < 0) {
979 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
980 "SubjectAltName item");
984 if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
989 res = x509_parse_alt_name_rfc8222(name, hdr.payload,
993 res = x509_parse_alt_name_dns(name, hdr.payload,
997 res = x509_parse_alt_name_uri(name, hdr.payload,
1001 res = x509_parse_alt_name_ip(name, hdr.payload,
1005 res = x509_parse_alt_name_rid(name, hdr.payload,
1008 case 0: /* TODO: otherName */
1009 case 3: /* TODO: x500Address */
1010 case 4: /* TODO: directoryName */
1011 case 5: /* TODO: ediPartyName */
1024 static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
1025 const u8 *pos, size_t len)
1027 struct asn1_hdr hdr;
1029 /* SubjectAltName ::= GeneralNames */
1031 if (asn1_get_next(pos, len, &hdr) < 0 ||
1032 hdr.class != ASN1_CLASS_UNIVERSAL ||
1033 hdr.tag != ASN1_TAG_SEQUENCE) {
1034 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1035 "SubjectAltName; found %d tag 0x%x",
1036 hdr.class, hdr.tag);
1040 wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
1041 cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
1043 if (hdr.length == 0)
1046 return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
1051 static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
1052 const u8 *pos, size_t len)
1054 struct asn1_hdr hdr;
1056 /* IssuerAltName ::= GeneralNames */
1058 if (asn1_get_next(pos, len, &hdr) < 0 ||
1059 hdr.class != ASN1_CLASS_UNIVERSAL ||
1060 hdr.tag != ASN1_TAG_SEQUENCE) {
1061 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1062 "IssuerAltName; found %d tag 0x%x",
1063 hdr.class, hdr.tag);
1067 wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
1068 cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
1070 if (hdr.length == 0)
1073 return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
1078 static int x509_parse_extension_data(struct x509_certificate *cert,
1079 struct asn1_oid *oid,
1080 const u8 *pos, size_t len)
1082 if (!x509_id_ce_oid(oid))
1085 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
1086 * certificate policies (section 4.2.1.5)
1087 * name constraints (section 4.2.1.11)
1088 * policy constraints (section 4.2.1.12)
1089 * extended key usage (section 4.2.1.13)
1090 * inhibit any-policy (section 4.2.1.15)
1092 switch (oid->oid[3]) {
1093 case 15: /* id-ce-keyUsage */
1094 return x509_parse_ext_key_usage(cert, pos, len);
1095 case 17: /* id-ce-subjectAltName */
1096 return x509_parse_ext_subject_alt_name(cert, pos, len);
1097 case 18: /* id-ce-issuerAltName */
1098 return x509_parse_ext_issuer_alt_name(cert, pos, len);
1099 case 19: /* id-ce-basicConstraints */
1100 return x509_parse_ext_basic_constraints(cert, pos, len);
1107 static int x509_parse_extension(struct x509_certificate *cert,
1108 const u8 *pos, size_t len, const u8 **next)
1111 struct asn1_hdr hdr;
1112 struct asn1_oid oid;
1113 int critical_ext = 0, res;
1117 * Extension ::= SEQUENCE {
1118 * extnID OBJECT IDENTIFIER,
1119 * critical BOOLEAN DEFAULT FALSE,
1120 * extnValue OCTET STRING
1124 if (asn1_get_next(pos, len, &hdr) < 0 ||
1125 hdr.class != ASN1_CLASS_UNIVERSAL ||
1126 hdr.tag != ASN1_TAG_SEQUENCE) {
1127 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1128 "Extensions: class %d tag 0x%x; expected SEQUENCE",
1129 hdr.class, hdr.tag);
1133 *next = end = pos + hdr.length;
1135 if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
1136 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
1137 "Extension (expected OID)");
1141 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1142 hdr.class != ASN1_CLASS_UNIVERSAL ||
1143 (hdr.tag != ASN1_TAG_BOOLEAN &&
1144 hdr.tag != ASN1_TAG_OCTETSTRING)) {
1145 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1146 "Extensions: class %d tag 0x%x; expected BOOLEAN "
1147 "or OCTET STRING", hdr.class, hdr.tag);
1151 if (hdr.tag == ASN1_TAG_BOOLEAN) {
1152 if (hdr.length != 1) {
1153 wpa_printf(MSG_DEBUG, "X509: Unexpected "
1154 "Boolean length (%u)", hdr.length);
1157 critical_ext = hdr.payload[0];
1159 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1160 (hdr.class != ASN1_CLASS_UNIVERSAL &&
1161 hdr.class != ASN1_CLASS_PRIVATE) ||
1162 hdr.tag != ASN1_TAG_OCTETSTRING) {
1163 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
1164 "in Extensions: class %d tag 0x%x; "
1165 "expected OCTET STRING",
1166 hdr.class, hdr.tag);
1171 asn1_oid_to_str(&oid, buf, sizeof(buf));
1172 wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
1174 wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
1176 res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
1179 if (res == 1 && critical_ext) {
1180 wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
1189 static int x509_parse_extensions(struct x509_certificate *cert,
1190 const u8 *pos, size_t len)
1193 struct asn1_hdr hdr;
1195 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
1197 if (asn1_get_next(pos, len, &hdr) < 0 ||
1198 hdr.class != ASN1_CLASS_UNIVERSAL ||
1199 hdr.tag != ASN1_TAG_SEQUENCE) {
1200 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
1201 "for Extensions: class %d tag 0x%x; "
1202 "expected SEQUENCE", hdr.class, hdr.tag);
1207 end = pos + hdr.length;
1210 if (x509_parse_extension(cert, pos, end - pos, &pos)
1219 static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
1220 struct x509_certificate *cert,
1223 struct asn1_hdr hdr;
1224 const u8 *pos, *end;
1227 unsigned long value;
1229 /* tbsCertificate TBSCertificate ::= SEQUENCE */
1230 if (asn1_get_next(buf, len, &hdr) < 0 ||
1231 hdr.class != ASN1_CLASS_UNIVERSAL ||
1232 hdr.tag != ASN1_TAG_SEQUENCE) {
1233 wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
1234 "with a valid SEQUENCE - found class %d tag 0x%x",
1235 hdr.class, hdr.tag);
1239 end = *next = pos + hdr.length;
1242 * version [0] EXPLICIT Version DEFAULT v1
1243 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1245 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1249 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
1250 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1253 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1254 hdr.tag != ASN1_TAG_INTEGER) {
1255 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1256 "version field - found class %d tag 0x%x",
1257 hdr.class, hdr.tag);
1260 if (hdr.length != 1) {
1261 wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
1262 "length %u (expected 1)", hdr.length);
1274 cert->version = value;
1275 if (cert->version != X509_CERT_V1 &&
1276 cert->version != X509_CERT_V2 &&
1277 cert->version != X509_CERT_V3) {
1278 wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
1283 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1286 cert->version = X509_CERT_V1;
1287 wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
1289 /* serialNumber CertificateSerialNumber ::= INTEGER */
1290 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1291 hdr.tag != ASN1_TAG_INTEGER) {
1292 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1293 "serialNumber; class=%d tag=0x%x",
1294 hdr.class, hdr.tag);
1301 cert->serial_number <<= 8;
1302 cert->serial_number |= *pos++;
1305 wpa_printf(MSG_MSGDUMP, "X509: serialNumber %lu", cert->serial_number);
1307 /* signature AlgorithmIdentifier */
1308 if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
1313 if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
1315 x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
1316 wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
1318 /* validity Validity */
1319 if (x509_parse_validity(pos, end - pos, cert, &pos))
1323 if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
1325 x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
1326 wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
1328 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1329 if (x509_parse_public_key(pos, end - pos, cert, &pos))
1335 if (cert->version == X509_CERT_V1)
1338 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1339 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1340 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1341 " tag to parse optional tbsCertificate "
1342 "field(s); parsed class %d tag 0x%x",
1343 hdr.class, hdr.tag);
1348 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1349 wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
1350 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1352 if (hdr.payload + hdr.length == end)
1355 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1356 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1357 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1358 " tag to parse optional tbsCertificate "
1359 "field(s); parsed class %d tag 0x%x",
1360 hdr.class, hdr.tag);
1366 /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
1367 wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
1368 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1370 if (hdr.payload + hdr.length == end)
1373 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1374 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1375 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1376 " tag to parse optional tbsCertificate "
1377 "field(s); parsed class %d tag 0x%x",
1378 hdr.class, hdr.tag);
1384 wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
1385 "Context-Specific tag %d in optional "
1386 "tbsCertificate fields", hdr.tag);
1390 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1392 if (cert->version != X509_CERT_V3) {
1393 wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
1394 "Extensions data which are only allowed for "
1395 "version 3", cert->version + 1);
1399 if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
1402 pos = hdr.payload + hdr.length;
1404 wpa_hexdump(MSG_DEBUG,
1405 "X509: Ignored extra tbsCertificate data",
1413 static int x509_rsadsi_oid(struct asn1_oid *oid)
1415 return oid->len >= 4 &&
1416 oid->oid[0] == 1 /* iso */ &&
1417 oid->oid[1] == 2 /* member-body */ &&
1418 oid->oid[2] == 840 /* us */ &&
1419 oid->oid[3] == 113549 /* rsadsi */;
1423 static int x509_pkcs_oid(struct asn1_oid *oid)
1425 return oid->len >= 5 &&
1426 x509_rsadsi_oid(oid) &&
1427 oid->oid[4] == 1 /* pkcs */;
1431 static int x509_digest_oid(struct asn1_oid *oid)
1433 return oid->len >= 5 &&
1434 x509_rsadsi_oid(oid) &&
1435 oid->oid[4] == 2 /* digestAlgorithm */;
1439 static int x509_sha1_oid(struct asn1_oid *oid)
1441 return oid->len == 6 &&
1442 oid->oid[0] == 1 /* iso */ &&
1443 oid->oid[1] == 3 /* identified-organization */ &&
1444 oid->oid[2] == 14 /* oiw */ &&
1445 oid->oid[3] == 3 /* secsig */ &&
1446 oid->oid[4] == 2 /* algorithms */ &&
1447 oid->oid[5] == 26 /* id-sha1 */;
1451 static int x509_sha256_oid(struct asn1_oid *oid)
1453 return oid->len == 9 &&
1454 oid->oid[0] == 2 /* joint-iso-itu-t */ &&
1455 oid->oid[1] == 16 /* country */ &&
1456 oid->oid[2] == 840 /* us */ &&
1457 oid->oid[3] == 1 /* organization */ &&
1458 oid->oid[4] == 101 /* gov */ &&
1459 oid->oid[5] == 3 /* csor */ &&
1460 oid->oid[6] == 4 /* nistAlgorithm */ &&
1461 oid->oid[7] == 2 /* hashAlgs */ &&
1462 oid->oid[8] == 1 /* sha256 */;
1467 * x509_certificate_parse - Parse a X.509 certificate in DER format
1468 * @buf: Pointer to the X.509 certificate in DER format
1469 * @len: Buffer length
1470 * Returns: Pointer to the parsed certificate or %NULL on failure
1472 * Caller is responsible for freeing the returned certificate by calling
1473 * x509_certificate_free().
1475 struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
1477 struct asn1_hdr hdr;
1478 const u8 *pos, *end, *hash_start;
1479 struct x509_certificate *cert;
1481 cert = os_zalloc(sizeof(*cert) + len);
1484 os_memcpy(cert + 1, buf, len);
1485 cert->cert_start = (u8 *) (cert + 1);
1486 cert->cert_len = len;
1491 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1493 /* Certificate ::= SEQUENCE */
1494 if (asn1_get_next(pos, len, &hdr) < 0 ||
1495 hdr.class != ASN1_CLASS_UNIVERSAL ||
1496 hdr.tag != ASN1_TAG_SEQUENCE) {
1497 wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
1498 "a valid SEQUENCE - found class %d tag 0x%x",
1499 hdr.class, hdr.tag);
1500 x509_certificate_free(cert);
1505 if (pos + hdr.length > end) {
1506 x509_certificate_free(cert);
1510 if (pos + hdr.length < end) {
1511 wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
1512 "encoded certificate",
1513 pos + hdr.length, end - pos + hdr.length);
1514 end = pos + hdr.length;
1518 cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
1519 if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
1520 x509_certificate_free(cert);
1523 cert->tbs_cert_len = pos - hash_start;
1525 /* signatureAlgorithm AlgorithmIdentifier */
1526 if (x509_parse_algorithm_identifier(pos, end - pos,
1527 &cert->signature_alg, &pos)) {
1528 x509_certificate_free(cert);
1532 /* signatureValue BIT STRING */
1533 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1534 hdr.class != ASN1_CLASS_UNIVERSAL ||
1535 hdr.tag != ASN1_TAG_BITSTRING) {
1536 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
1537 "(signatureValue) - found class %d tag 0x%x",
1538 hdr.class, hdr.tag);
1539 x509_certificate_free(cert);
1542 if (hdr.length < 1) {
1543 x509_certificate_free(cert);
1548 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
1550 /* PKCS #1 v1.5 10.2.1:
1551 * It is an error if the length in bits of the signature S is
1552 * not a multiple of eight.
1554 x509_certificate_free(cert);
1557 os_free(cert->sign_value);
1558 cert->sign_value = os_malloc(hdr.length - 1);
1559 if (cert->sign_value == NULL) {
1560 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
1562 x509_certificate_free(cert);
1565 os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
1566 cert->sign_value_len = hdr.length - 1;
1567 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1568 cert->sign_value, cert->sign_value_len);
1575 * x509_certificate_check_signature - Verify certificate signature
1576 * @issuer: Issuer certificate
1577 * @cert: Certificate to be verified
1578 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1581 int x509_certificate_check_signature(struct x509_certificate *issuer,
1582 struct x509_certificate *cert)
1584 struct crypto_public_key *pk;
1586 const u8 *pos, *end, *next, *da_end;
1588 struct asn1_hdr hdr;
1589 struct asn1_oid oid;
1593 if (!x509_pkcs_oid(&cert->signature.oid) ||
1594 cert->signature.oid.len != 7 ||
1595 cert->signature.oid.oid[5] != 1 /* pkcs-1 */) {
1596 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1601 pk = crypto_public_key_import(issuer->public_key,
1602 issuer->public_key_len);
1606 data_len = cert->sign_value_len;
1607 data = os_malloc(data_len);
1609 crypto_public_key_free(pk);
1613 if (crypto_public_key_decrypt_pkcs1(pk, cert->sign_value,
1614 cert->sign_value_len, data,
1616 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1617 crypto_public_key_free(pk);
1621 crypto_public_key_free(pk);
1623 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1626 * PKCS #1 v1.5, 10.1.2:
1628 * DigestInfo ::= SEQUENCE {
1629 * digestAlgorithm DigestAlgorithmIdentifier,
1633 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1635 * Digest ::= OCTET STRING
1638 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1639 hdr.class != ASN1_CLASS_UNIVERSAL ||
1640 hdr.tag != ASN1_TAG_SEQUENCE) {
1641 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1642 "(DigestInfo) - found class %d tag 0x%x",
1643 hdr.class, hdr.tag);
1649 end = pos + hdr.length;
1653 * AlgorithmIdentifier ::= SEQUENCE {
1654 * algorithm OBJECT IDENTIFIER,
1655 * parameters ANY DEFINED BY algorithm OPTIONAL
1659 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1660 hdr.class != ASN1_CLASS_UNIVERSAL ||
1661 hdr.tag != ASN1_TAG_SEQUENCE) {
1662 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1663 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1664 hdr.class, hdr.tag);
1668 da_end = hdr.payload + hdr.length;
1670 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1671 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1676 if (x509_sha1_oid(&oid)) {
1677 if (cert->signature.oid.oid[6] !=
1678 5 /* sha-1WithRSAEncryption */) {
1679 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1680 "does not match with certificate "
1681 "signatureAlgorithm (%lu)",
1682 cert->signature.oid.oid[6]);
1686 goto skip_digest_oid;
1689 if (x509_sha256_oid(&oid)) {
1690 if (cert->signature.oid.oid[6] !=
1691 11 /* sha2561WithRSAEncryption */) {
1692 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1693 "does not match with certificate "
1694 "signatureAlgorithm (%lu)",
1695 cert->signature.oid.oid[6]);
1699 goto skip_digest_oid;
1702 if (!x509_digest_oid(&oid)) {
1703 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1707 switch (oid.oid[5]) {
1709 if (cert->signature.oid.oid[6] != 4 /* md5WithRSAEncryption */)
1711 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1712 "not match with certificate "
1713 "signatureAlgorithm (%lu)",
1714 cert->signature.oid.oid[6]);
1722 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1723 "(%lu)", oid.oid[5]);
1729 /* Digest ::= OCTET STRING */
1731 end = data + data_len;
1733 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1734 hdr.class != ASN1_CLASS_UNIVERSAL ||
1735 hdr.tag != ASN1_TAG_OCTETSTRING) {
1736 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1737 "(Digest) - found class %d tag 0x%x",
1738 hdr.class, hdr.tag);
1742 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1743 hdr.payload, hdr.length);
1745 switch (cert->signature.oid.oid[6]) {
1746 case 4: /* md5WithRSAEncryption */
1747 md5_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1750 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1753 case 5: /* sha-1WithRSAEncryption */
1754 sha1_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1757 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1760 case 11: /* sha256WithRSAEncryption */
1761 sha256_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1764 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1767 case 2: /* md2WithRSAEncryption */
1768 case 12: /* sha384WithRSAEncryption */
1769 case 13: /* sha512WithRSAEncryption */
1771 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1772 "algorithm (%lu)", cert->signature.oid.oid[6]);
1777 if (hdr.length != hash_len ||
1778 os_memcmp(hdr.payload, hash, hdr.length) != 0) {
1779 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1780 "with calculated tbsCertificate hash");
1787 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1788 "calculated tbsCertificate hash");
1794 static int x509_valid_issuer(const struct x509_certificate *cert)
1796 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1798 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1803 if (cert->version == X509_CERT_V3 &&
1804 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
1805 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
1806 "include BasicConstraints extension");
1810 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
1811 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
1812 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
1813 "keyCertSign bit in Key Usage");
1822 * x509_certificate_chain_validate - Validate X.509 certificate chain
1823 * @trusted: List of trusted certificates
1824 * @chain: Certificate chain to be validated (first chain must be issued by
1825 * signed by the second certificate in the chain and so on)
1826 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
1827 * Returns: 0 if chain is valid, -1 if not
1829 int x509_certificate_chain_validate(struct x509_certificate *trusted,
1830 struct x509_certificate *chain,
1831 int *reason, int disable_time_checks)
1834 int chain_trusted = 0;
1835 struct x509_certificate *cert, *trust;
1839 *reason = X509_VALIDATE_OK;
1841 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
1844 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
1845 x509_name_string(&cert->subject, buf, sizeof(buf));
1846 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
1851 if (!disable_time_checks &&
1852 ((unsigned long) now.sec <
1853 (unsigned long) cert->not_before ||
1854 (unsigned long) now.sec >
1855 (unsigned long) cert->not_after)) {
1856 wpa_printf(MSG_INFO, "X509: Certificate not valid "
1857 "(now=%lu not_before=%lu not_after=%lu)",
1858 now.sec, cert->not_before, cert->not_after);
1859 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
1864 if (x509_name_compare(&cert->issuer,
1865 &cert->next->subject) != 0) {
1866 wpa_printf(MSG_DEBUG, "X509: Certificate "
1867 "chain issuer name mismatch");
1868 x509_name_string(&cert->issuer, buf,
1870 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
1872 x509_name_string(&cert->next->subject, buf,
1874 wpa_printf(MSG_DEBUG, "X509: next cert "
1875 "subject: %s", buf);
1876 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
1880 if (x509_valid_issuer(cert->next) < 0) {
1881 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1885 if ((cert->next->extensions_present &
1886 X509_EXT_PATH_LEN_CONSTRAINT) &&
1887 idx > cert->next->path_len_constraint) {
1888 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
1889 " not met (idx=%lu issuer "
1890 "pathLenConstraint=%lu)", idx,
1891 cert->next->path_len_constraint);
1892 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1896 if (x509_certificate_check_signature(cert->next, cert)
1898 wpa_printf(MSG_DEBUG, "X509: Invalid "
1899 "certificate signature within "
1901 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1906 for (trust = trusted; trust; trust = trust->next) {
1907 if (x509_name_compare(&cert->issuer, &trust->subject)
1913 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
1914 "list of trusted certificates");
1915 if (x509_valid_issuer(trust) < 0) {
1916 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1920 if (x509_certificate_check_signature(trust, cert) < 0)
1922 wpa_printf(MSG_DEBUG, "X509: Invalid "
1923 "certificate signature");
1924 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1928 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
1929 "found to complete the chain");
1934 if (!chain_trusted) {
1935 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
1936 "from the list of trusted certificates");
1938 *reason = X509_VALIDATE_UNKNOWN_CA;
1941 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
1942 "disabled - ignore unknown CA issue");
1945 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
1952 * x509_certificate_get_subject - Get a certificate based on Subject name
1953 * @chain: Certificate chain to search through
1954 * @name: Subject name to search for
1955 * Returns: Pointer to the certificate with the given Subject name or
1958 struct x509_certificate *
1959 x509_certificate_get_subject(struct x509_certificate *chain,
1960 struct x509_name *name)
1962 struct x509_certificate *cert;
1964 for (cert = chain; cert; cert = cert->next) {
1965 if (x509_name_compare(&cert->subject, name) == 0)
1973 * x509_certificate_self_signed - Is the certificate self-signed?
1974 * @cert: Certificate
1975 * Returns: 1 if certificate is self-signed, 0 if not
1977 int x509_certificate_self_signed(struct x509_certificate *cert)
1979 return x509_name_compare(&cert->issuer, &cert->subject) == 0;