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 = dup_binstr(hdr.payload, hdr.length);
448 x509_free_name(name);
451 if (os_strlen(val) != hdr.length) {
452 wpa_printf(MSG_INFO, "X509: Reject certificate with "
453 "embedded NUL byte in a string (%s[NUL])",
456 x509_free_name(name);
460 name->attr[name->num_attr].type = type;
461 name->attr[name->num_attr].value = val;
469 static char * x509_name_attr_str(enum x509_name_attr_type type)
472 case X509_NAME_ATTR_NOT_USED:
474 case X509_NAME_ATTR_DC:
476 case X509_NAME_ATTR_CN:
478 case X509_NAME_ATTR_C:
480 case X509_NAME_ATTR_L:
482 case X509_NAME_ATTR_ST:
484 case X509_NAME_ATTR_O:
486 case X509_NAME_ATTR_OU:
494 * x509_name_string - Convert an X.509 certificate name into a string
495 * @name: Name to convert
496 * @buf: Buffer for the string
497 * @len: Maximum buffer length
499 void x509_name_string(struct x509_name *name, char *buf, size_t len)
511 for (i = 0; i < name->num_attr; i++) {
512 ret = os_snprintf(pos, end - pos, "%s=%s, ",
513 x509_name_attr_str(name->attr[i].type),
514 name->attr[i].value);
515 if (os_snprintf_error(end - pos, ret))
520 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
528 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
530 if (os_snprintf_error(end - pos, ret))
540 static int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag,
544 int year, month, day, hour, min, sec;
549 * generalTime GeneralizedTime
552 * UTCTime: YYMMDDHHMMSSZ
553 * GeneralizedTime: YYYYMMDDHHMMSSZ
556 pos = (const char *) buf;
559 case ASN1_TAG_UTCTIME:
560 if (len != 13 || buf[12] != 'Z') {
561 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
562 "UTCTime format", buf, len);
565 if (sscanf(pos, "%02d", &year) != 1) {
566 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
567 "UTCTime year", buf, len);
576 case ASN1_TAG_GENERALIZEDTIME:
577 if (len != 15 || buf[14] != 'Z') {
578 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
579 "GeneralizedTime format", buf, len);
582 if (sscanf(pos, "%04d", &year) != 1) {
583 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
584 "GeneralizedTime year", buf, len);
590 wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
591 "GeneralizedTime - found tag 0x%x", asn1_tag);
595 if (sscanf(pos, "%02d", &month) != 1) {
596 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
597 "(month)", buf, len);
602 if (sscanf(pos, "%02d", &day) != 1) {
603 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
609 if (sscanf(pos, "%02d", &hour) != 1) {
610 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
616 if (sscanf(pos, "%02d", &min) != 1) {
617 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
623 if (sscanf(pos, "%02d", &sec) != 1) {
624 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
629 if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
630 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
634 * At least some test certificates have been configured
635 * to use dates prior to 1970. Set the date to
636 * beginning of 1970 to handle these case.
638 wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
639 "assume epoch as the time", year);
650 static int x509_parse_validity(const u8 *buf, size_t len,
651 struct x509_certificate *cert, const u8 **next)
658 * Validity ::= SEQUENCE {
664 * CAs conforming to this profile MUST always encode certificate
665 * validity dates through the year 2049 as UTCTime; certificate
666 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
669 if (asn1_get_next(buf, len, &hdr) < 0 ||
670 hdr.class != ASN1_CLASS_UNIVERSAL ||
671 hdr.tag != ASN1_TAG_SEQUENCE) {
672 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
673 "(Validity) - found class %d tag 0x%x",
680 if (pos + plen > buf + len)
685 if (asn1_get_next(pos, plen, &hdr) < 0 ||
686 hdr.class != ASN1_CLASS_UNIVERSAL ||
687 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
688 &cert->not_before) < 0) {
689 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
690 "Time", hdr.payload, hdr.length);
694 pos = hdr.payload + hdr.length;
697 if (asn1_get_next(pos, plen, &hdr) < 0 ||
698 hdr.class != ASN1_CLASS_UNIVERSAL ||
699 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
700 &cert->not_after) < 0) {
701 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
702 "Time", hdr.payload, hdr.length);
706 wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
707 (unsigned long) cert->not_before,
708 (unsigned long) cert->not_after);
714 static int x509_id_ce_oid(struct asn1_oid *oid)
716 /* id-ce arc from X.509 for standard X.509v3 extensions */
717 return oid->len >= 4 &&
718 oid->oid[0] == 2 /* joint-iso-ccitt */ &&
719 oid->oid[1] == 5 /* ds */ &&
720 oid->oid[2] == 29 /* id-ce */;
724 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
725 const u8 *pos, size_t len)
730 * KeyUsage ::= BIT STRING {
731 * digitalSignature (0),
732 * nonRepudiation (1),
733 * keyEncipherment (2),
734 * dataEncipherment (3),
742 if (asn1_get_next(pos, len, &hdr) < 0 ||
743 hdr.class != ASN1_CLASS_UNIVERSAL ||
744 hdr.tag != ASN1_TAG_BITSTRING ||
746 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
747 "KeyUsage; found %d tag 0x%x len %d",
748 hdr.class, hdr.tag, hdr.length);
752 cert->extensions_present |= X509_EXT_KEY_USAGE;
753 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
755 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
761 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
762 const u8 *pos, size_t len)
769 * BasicConstraints ::= SEQUENCE {
770 * cA BOOLEAN DEFAULT FALSE,
771 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
774 if (asn1_get_next(pos, len, &hdr) < 0 ||
775 hdr.class != ASN1_CLASS_UNIVERSAL ||
776 hdr.tag != ASN1_TAG_SEQUENCE) {
777 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
778 "BasicConstraints; found %d tag 0x%x",
783 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
788 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
789 hdr.class != ASN1_CLASS_UNIVERSAL) {
790 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
795 if (hdr.tag == ASN1_TAG_BOOLEAN) {
796 if (hdr.length != 1) {
797 wpa_printf(MSG_DEBUG, "X509: Unexpected "
798 "Boolean length (%u) in BasicConstraints",
802 cert->ca = hdr.payload[0];
804 if (hdr.payload + hdr.length == pos + len) {
805 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
810 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
812 hdr.class != ASN1_CLASS_UNIVERSAL) {
813 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
819 if (hdr.tag != ASN1_TAG_INTEGER) {
820 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
821 "BasicConstraints; found class %d tag 0x%x",
835 cert->path_len_constraint = value;
836 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
838 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
839 "pathLenConstraint=%lu",
840 cert->ca, cert->path_len_constraint);
846 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
847 const u8 *pos, size_t len)
849 /* rfc822Name IA5String */
850 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
851 os_free(name->alt_email);
852 name->alt_email = os_zalloc(len + 1);
853 if (name->alt_email == NULL)
855 os_memcpy(name->alt_email, pos, len);
856 if (os_strlen(name->alt_email) != len) {
857 wpa_printf(MSG_INFO, "X509: Reject certificate with "
858 "embedded NUL byte in rfc822Name (%s[NUL])",
860 os_free(name->alt_email);
861 name->alt_email = NULL;
868 static int x509_parse_alt_name_dns(struct x509_name *name,
869 const u8 *pos, size_t len)
871 /* dNSName IA5String */
872 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
874 name->dns = os_zalloc(len + 1);
875 if (name->dns == NULL)
877 os_memcpy(name->dns, pos, len);
878 if (os_strlen(name->dns) != len) {
879 wpa_printf(MSG_INFO, "X509: Reject certificate with "
880 "embedded NUL byte in dNSName (%s[NUL])",
890 static int x509_parse_alt_name_uri(struct x509_name *name,
891 const u8 *pos, size_t len)
893 /* uniformResourceIdentifier IA5String */
894 wpa_hexdump_ascii(MSG_MSGDUMP,
895 "X509: altName - uniformResourceIdentifier",
898 name->uri = os_zalloc(len + 1);
899 if (name->uri == NULL)
901 os_memcpy(name->uri, pos, len);
902 if (os_strlen(name->uri) != len) {
903 wpa_printf(MSG_INFO, "X509: Reject certificate with "
904 "embedded NUL byte in uniformResourceIdentifier "
905 "(%s[NUL])", name->uri);
914 static int x509_parse_alt_name_ip(struct x509_name *name,
915 const u8 *pos, size_t len)
917 /* iPAddress OCTET STRING */
918 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
920 name->ip = os_malloc(len);
921 if (name->ip == NULL)
923 os_memcpy(name->ip, pos, len);
929 static int x509_parse_alt_name_rid(struct x509_name *name,
930 const u8 *pos, size_t len)
934 /* registeredID OBJECT IDENTIFIER */
935 if (asn1_parse_oid(pos, len, &name->rid) < 0)
938 asn1_oid_to_str(&name->rid, buf, sizeof(buf));
939 wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
945 static int x509_parse_ext_alt_name(struct x509_name *name,
946 const u8 *pos, size_t len)
952 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
954 * GeneralName ::= CHOICE {
955 * otherName [0] OtherName,
956 * rfc822Name [1] IA5String,
957 * dNSName [2] IA5String,
958 * x400Address [3] ORAddress,
959 * directoryName [4] Name,
960 * ediPartyName [5] EDIPartyName,
961 * uniformResourceIdentifier [6] IA5String,
962 * iPAddress [7] OCTET STRING,
963 * registeredID [8] OBJECT IDENTIFIER }
965 * OtherName ::= SEQUENCE {
966 * type-id OBJECT IDENTIFIER,
967 * value [0] EXPLICIT ANY DEFINED BY type-id }
969 * EDIPartyName ::= SEQUENCE {
970 * nameAssigner [0] DirectoryString OPTIONAL,
971 * partyName [1] DirectoryString }
974 for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
977 if (asn1_get_next(p, end - p, &hdr) < 0) {
978 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
979 "SubjectAltName item");
983 if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
988 res = x509_parse_alt_name_rfc8222(name, hdr.payload,
992 res = x509_parse_alt_name_dns(name, hdr.payload,
996 res = x509_parse_alt_name_uri(name, hdr.payload,
1000 res = x509_parse_alt_name_ip(name, hdr.payload,
1004 res = x509_parse_alt_name_rid(name, hdr.payload,
1007 case 0: /* TODO: otherName */
1008 case 3: /* TODO: x500Address */
1009 case 4: /* TODO: directoryName */
1010 case 5: /* TODO: ediPartyName */
1023 static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
1024 const u8 *pos, size_t len)
1026 struct asn1_hdr hdr;
1028 /* SubjectAltName ::= GeneralNames */
1030 if (asn1_get_next(pos, len, &hdr) < 0 ||
1031 hdr.class != ASN1_CLASS_UNIVERSAL ||
1032 hdr.tag != ASN1_TAG_SEQUENCE) {
1033 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1034 "SubjectAltName; found %d tag 0x%x",
1035 hdr.class, hdr.tag);
1039 wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
1040 cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
1042 if (hdr.length == 0)
1045 return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
1050 static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
1051 const u8 *pos, size_t len)
1053 struct asn1_hdr hdr;
1055 /* IssuerAltName ::= GeneralNames */
1057 if (asn1_get_next(pos, len, &hdr) < 0 ||
1058 hdr.class != ASN1_CLASS_UNIVERSAL ||
1059 hdr.tag != ASN1_TAG_SEQUENCE) {
1060 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1061 "IssuerAltName; found %d tag 0x%x",
1062 hdr.class, hdr.tag);
1066 wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
1067 cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
1069 if (hdr.length == 0)
1072 return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
1077 static int x509_parse_extension_data(struct x509_certificate *cert,
1078 struct asn1_oid *oid,
1079 const u8 *pos, size_t len)
1081 if (!x509_id_ce_oid(oid))
1084 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
1085 * certificate policies (section 4.2.1.5)
1086 * name constraints (section 4.2.1.11)
1087 * policy constraints (section 4.2.1.12)
1088 * extended key usage (section 4.2.1.13)
1089 * inhibit any-policy (section 4.2.1.15)
1091 switch (oid->oid[3]) {
1092 case 15: /* id-ce-keyUsage */
1093 return x509_parse_ext_key_usage(cert, pos, len);
1094 case 17: /* id-ce-subjectAltName */
1095 return x509_parse_ext_subject_alt_name(cert, pos, len);
1096 case 18: /* id-ce-issuerAltName */
1097 return x509_parse_ext_issuer_alt_name(cert, pos, len);
1098 case 19: /* id-ce-basicConstraints */
1099 return x509_parse_ext_basic_constraints(cert, pos, len);
1106 static int x509_parse_extension(struct x509_certificate *cert,
1107 const u8 *pos, size_t len, const u8 **next)
1110 struct asn1_hdr hdr;
1111 struct asn1_oid oid;
1112 int critical_ext = 0, res;
1116 * Extension ::= SEQUENCE {
1117 * extnID OBJECT IDENTIFIER,
1118 * critical BOOLEAN DEFAULT FALSE,
1119 * extnValue OCTET STRING
1123 if (asn1_get_next(pos, len, &hdr) < 0 ||
1124 hdr.class != ASN1_CLASS_UNIVERSAL ||
1125 hdr.tag != ASN1_TAG_SEQUENCE) {
1126 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1127 "Extensions: class %d tag 0x%x; expected SEQUENCE",
1128 hdr.class, hdr.tag);
1132 *next = end = pos + hdr.length;
1134 if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
1135 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
1136 "Extension (expected OID)");
1140 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1141 hdr.class != ASN1_CLASS_UNIVERSAL ||
1142 (hdr.tag != ASN1_TAG_BOOLEAN &&
1143 hdr.tag != ASN1_TAG_OCTETSTRING)) {
1144 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1145 "Extensions: class %d tag 0x%x; expected BOOLEAN "
1146 "or OCTET STRING", hdr.class, hdr.tag);
1150 if (hdr.tag == ASN1_TAG_BOOLEAN) {
1151 if (hdr.length != 1) {
1152 wpa_printf(MSG_DEBUG, "X509: Unexpected "
1153 "Boolean length (%u)", hdr.length);
1156 critical_ext = hdr.payload[0];
1158 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1159 (hdr.class != ASN1_CLASS_UNIVERSAL &&
1160 hdr.class != ASN1_CLASS_PRIVATE) ||
1161 hdr.tag != ASN1_TAG_OCTETSTRING) {
1162 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
1163 "in Extensions: class %d tag 0x%x; "
1164 "expected OCTET STRING",
1165 hdr.class, hdr.tag);
1170 asn1_oid_to_str(&oid, buf, sizeof(buf));
1171 wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
1173 wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
1175 res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
1178 if (res == 1 && critical_ext) {
1179 wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
1188 static int x509_parse_extensions(struct x509_certificate *cert,
1189 const u8 *pos, size_t len)
1192 struct asn1_hdr hdr;
1194 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
1196 if (asn1_get_next(pos, len, &hdr) < 0 ||
1197 hdr.class != ASN1_CLASS_UNIVERSAL ||
1198 hdr.tag != ASN1_TAG_SEQUENCE) {
1199 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
1200 "for Extensions: class %d tag 0x%x; "
1201 "expected SEQUENCE", hdr.class, hdr.tag);
1206 end = pos + hdr.length;
1209 if (x509_parse_extension(cert, pos, end - pos, &pos)
1218 static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
1219 struct x509_certificate *cert,
1222 struct asn1_hdr hdr;
1223 const u8 *pos, *end;
1226 unsigned long value;
1228 /* tbsCertificate TBSCertificate ::= SEQUENCE */
1229 if (asn1_get_next(buf, len, &hdr) < 0 ||
1230 hdr.class != ASN1_CLASS_UNIVERSAL ||
1231 hdr.tag != ASN1_TAG_SEQUENCE) {
1232 wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
1233 "with a valid SEQUENCE - found class %d tag 0x%x",
1234 hdr.class, hdr.tag);
1238 end = *next = pos + hdr.length;
1241 * version [0] EXPLICIT Version DEFAULT v1
1242 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1244 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1248 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
1249 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1252 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1253 hdr.tag != ASN1_TAG_INTEGER) {
1254 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1255 "version field - found class %d tag 0x%x",
1256 hdr.class, hdr.tag);
1259 if (hdr.length != 1) {
1260 wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
1261 "length %u (expected 1)", hdr.length);
1273 cert->version = value;
1274 if (cert->version != X509_CERT_V1 &&
1275 cert->version != X509_CERT_V2 &&
1276 cert->version != X509_CERT_V3) {
1277 wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
1282 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1285 cert->version = X509_CERT_V1;
1286 wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
1288 /* serialNumber CertificateSerialNumber ::= INTEGER */
1289 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1290 hdr.tag != ASN1_TAG_INTEGER) {
1291 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1292 "serialNumber; class=%d tag=0x%x",
1293 hdr.class, hdr.tag);
1300 cert->serial_number <<= 8;
1301 cert->serial_number |= *pos++;
1304 wpa_printf(MSG_MSGDUMP, "X509: serialNumber %lu", cert->serial_number);
1306 /* signature AlgorithmIdentifier */
1307 if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
1312 if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
1314 x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
1315 wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
1317 /* validity Validity */
1318 if (x509_parse_validity(pos, end - pos, cert, &pos))
1322 if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
1324 x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
1325 wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
1327 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1328 if (x509_parse_public_key(pos, end - pos, cert, &pos))
1334 if (cert->version == X509_CERT_V1)
1337 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1338 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1339 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1340 " tag to parse optional tbsCertificate "
1341 "field(s); parsed class %d tag 0x%x",
1342 hdr.class, hdr.tag);
1347 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1348 wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
1349 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1351 pos = hdr.payload + hdr.length;
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 pos = hdr.payload + hdr.length;
1374 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1375 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1376 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1377 " tag to parse optional tbsCertificate "
1378 "field(s); parsed class %d tag 0x%x",
1379 hdr.class, hdr.tag);
1385 wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
1386 "Context-Specific tag %d in optional "
1387 "tbsCertificate fields", hdr.tag);
1391 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1393 if (cert->version != X509_CERT_V3) {
1394 wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
1395 "Extensions data which are only allowed for "
1396 "version 3", cert->version + 1);
1400 if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
1403 pos = hdr.payload + hdr.length;
1405 wpa_hexdump(MSG_DEBUG,
1406 "X509: Ignored extra tbsCertificate data",
1414 static int x509_rsadsi_oid(struct asn1_oid *oid)
1416 return oid->len >= 4 &&
1417 oid->oid[0] == 1 /* iso */ &&
1418 oid->oid[1] == 2 /* member-body */ &&
1419 oid->oid[2] == 840 /* us */ &&
1420 oid->oid[3] == 113549 /* rsadsi */;
1424 static int x509_pkcs_oid(struct asn1_oid *oid)
1426 return oid->len >= 5 &&
1427 x509_rsadsi_oid(oid) &&
1428 oid->oid[4] == 1 /* pkcs */;
1432 static int x509_digest_oid(struct asn1_oid *oid)
1434 return oid->len >= 5 &&
1435 x509_rsadsi_oid(oid) &&
1436 oid->oid[4] == 2 /* digestAlgorithm */;
1440 static int x509_sha1_oid(struct asn1_oid *oid)
1442 return oid->len == 6 &&
1443 oid->oid[0] == 1 /* iso */ &&
1444 oid->oid[1] == 3 /* identified-organization */ &&
1445 oid->oid[2] == 14 /* oiw */ &&
1446 oid->oid[3] == 3 /* secsig */ &&
1447 oid->oid[4] == 2 /* algorithms */ &&
1448 oid->oid[5] == 26 /* id-sha1 */;
1452 static int x509_sha256_oid(struct asn1_oid *oid)
1454 return oid->len == 9 &&
1455 oid->oid[0] == 2 /* joint-iso-itu-t */ &&
1456 oid->oid[1] == 16 /* country */ &&
1457 oid->oid[2] == 840 /* us */ &&
1458 oid->oid[3] == 1 /* organization */ &&
1459 oid->oid[4] == 101 /* gov */ &&
1460 oid->oid[5] == 3 /* csor */ &&
1461 oid->oid[6] == 4 /* nistAlgorithm */ &&
1462 oid->oid[7] == 2 /* hashAlgs */ &&
1463 oid->oid[8] == 1 /* sha256 */;
1468 * x509_certificate_parse - Parse a X.509 certificate in DER format
1469 * @buf: Pointer to the X.509 certificate in DER format
1470 * @len: Buffer length
1471 * Returns: Pointer to the parsed certificate or %NULL on failure
1473 * Caller is responsible for freeing the returned certificate by calling
1474 * x509_certificate_free().
1476 struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
1478 struct asn1_hdr hdr;
1479 const u8 *pos, *end, *hash_start;
1480 struct x509_certificate *cert;
1482 cert = os_zalloc(sizeof(*cert) + len);
1485 os_memcpy(cert + 1, buf, len);
1486 cert->cert_start = (u8 *) (cert + 1);
1487 cert->cert_len = len;
1492 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1494 /* Certificate ::= SEQUENCE */
1495 if (asn1_get_next(pos, len, &hdr) < 0 ||
1496 hdr.class != ASN1_CLASS_UNIVERSAL ||
1497 hdr.tag != ASN1_TAG_SEQUENCE) {
1498 wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
1499 "a valid SEQUENCE - found class %d tag 0x%x",
1500 hdr.class, hdr.tag);
1501 x509_certificate_free(cert);
1506 if (pos + hdr.length > end) {
1507 x509_certificate_free(cert);
1511 if (pos + hdr.length < end) {
1512 wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
1513 "encoded certificate",
1514 pos + hdr.length, end - (pos + hdr.length));
1515 end = pos + hdr.length;
1519 cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
1520 if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
1521 x509_certificate_free(cert);
1524 cert->tbs_cert_len = pos - hash_start;
1526 /* signatureAlgorithm AlgorithmIdentifier */
1527 if (x509_parse_algorithm_identifier(pos, end - pos,
1528 &cert->signature_alg, &pos)) {
1529 x509_certificate_free(cert);
1533 /* signatureValue BIT STRING */
1534 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1535 hdr.class != ASN1_CLASS_UNIVERSAL ||
1536 hdr.tag != ASN1_TAG_BITSTRING) {
1537 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
1538 "(signatureValue) - found class %d tag 0x%x",
1539 hdr.class, hdr.tag);
1540 x509_certificate_free(cert);
1543 if (hdr.length < 1) {
1544 x509_certificate_free(cert);
1549 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
1551 /* PKCS #1 v1.5 10.2.1:
1552 * It is an error if the length in bits of the signature S is
1553 * not a multiple of eight.
1555 x509_certificate_free(cert);
1558 os_free(cert->sign_value);
1559 cert->sign_value = os_malloc(hdr.length - 1);
1560 if (cert->sign_value == NULL) {
1561 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
1563 x509_certificate_free(cert);
1566 os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
1567 cert->sign_value_len = hdr.length - 1;
1568 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1569 cert->sign_value, cert->sign_value_len);
1576 * x509_certificate_check_signature - Verify certificate signature
1577 * @issuer: Issuer certificate
1578 * @cert: Certificate to be verified
1579 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1582 int x509_certificate_check_signature(struct x509_certificate *issuer,
1583 struct x509_certificate *cert)
1585 struct crypto_public_key *pk;
1587 const u8 *pos, *end, *next, *da_end;
1589 struct asn1_hdr hdr;
1590 struct asn1_oid oid;
1594 if (!x509_pkcs_oid(&cert->signature.oid) ||
1595 cert->signature.oid.len != 7 ||
1596 cert->signature.oid.oid[5] != 1 /* pkcs-1 */) {
1597 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1602 pk = crypto_public_key_import(issuer->public_key,
1603 issuer->public_key_len);
1607 data_len = cert->sign_value_len;
1608 data = os_malloc(data_len);
1610 crypto_public_key_free(pk);
1614 if (crypto_public_key_decrypt_pkcs1(pk, cert->sign_value,
1615 cert->sign_value_len, data,
1617 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1618 crypto_public_key_free(pk);
1622 crypto_public_key_free(pk);
1624 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1627 * PKCS #1 v1.5, 10.1.2:
1629 * DigestInfo ::= SEQUENCE {
1630 * digestAlgorithm DigestAlgorithmIdentifier,
1634 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1636 * Digest ::= OCTET STRING
1639 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1640 hdr.class != ASN1_CLASS_UNIVERSAL ||
1641 hdr.tag != ASN1_TAG_SEQUENCE) {
1642 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1643 "(DigestInfo) - found class %d tag 0x%x",
1644 hdr.class, hdr.tag);
1650 end = pos + hdr.length;
1654 * AlgorithmIdentifier ::= SEQUENCE {
1655 * algorithm OBJECT IDENTIFIER,
1656 * parameters ANY DEFINED BY algorithm OPTIONAL
1660 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1661 hdr.class != ASN1_CLASS_UNIVERSAL ||
1662 hdr.tag != ASN1_TAG_SEQUENCE) {
1663 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1664 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1665 hdr.class, hdr.tag);
1669 da_end = hdr.payload + hdr.length;
1671 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1672 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1677 if (x509_sha1_oid(&oid)) {
1678 if (cert->signature.oid.oid[6] !=
1679 5 /* sha-1WithRSAEncryption */) {
1680 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1681 "does not match with certificate "
1682 "signatureAlgorithm (%lu)",
1683 cert->signature.oid.oid[6]);
1687 goto skip_digest_oid;
1690 if (x509_sha256_oid(&oid)) {
1691 if (cert->signature.oid.oid[6] !=
1692 11 /* sha2561WithRSAEncryption */) {
1693 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1694 "does not match with certificate "
1695 "signatureAlgorithm (%lu)",
1696 cert->signature.oid.oid[6]);
1700 goto skip_digest_oid;
1703 if (!x509_digest_oid(&oid)) {
1704 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1708 switch (oid.oid[5]) {
1710 if (cert->signature.oid.oid[6] != 4 /* md5WithRSAEncryption */)
1712 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1713 "not match with certificate "
1714 "signatureAlgorithm (%lu)",
1715 cert->signature.oid.oid[6]);
1723 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1724 "(%lu)", oid.oid[5]);
1730 /* Digest ::= OCTET STRING */
1732 end = data + data_len;
1734 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1735 hdr.class != ASN1_CLASS_UNIVERSAL ||
1736 hdr.tag != ASN1_TAG_OCTETSTRING) {
1737 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1738 "(Digest) - found class %d tag 0x%x",
1739 hdr.class, hdr.tag);
1743 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1744 hdr.payload, hdr.length);
1746 switch (cert->signature.oid.oid[6]) {
1747 case 4: /* md5WithRSAEncryption */
1748 md5_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1751 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1754 case 5: /* sha-1WithRSAEncryption */
1755 sha1_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1758 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1761 case 11: /* sha256WithRSAEncryption */
1762 sha256_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1765 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1768 case 2: /* md2WithRSAEncryption */
1769 case 12: /* sha384WithRSAEncryption */
1770 case 13: /* sha512WithRSAEncryption */
1772 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1773 "algorithm (%lu)", cert->signature.oid.oid[6]);
1778 if (hdr.length != hash_len ||
1779 os_memcmp_const(hdr.payload, hash, hdr.length) != 0) {
1780 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1781 "with calculated tbsCertificate hash");
1786 if (hdr.payload + hdr.length < data + data_len) {
1787 wpa_hexdump(MSG_INFO,
1788 "X509: Extra data after certificate signature hash",
1789 hdr.payload + hdr.length,
1790 data + data_len - hdr.payload - hdr.length);
1797 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1798 "calculated tbsCertificate hash");
1804 static int x509_valid_issuer(const struct x509_certificate *cert)
1806 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1808 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1813 if (cert->version == X509_CERT_V3 &&
1814 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
1815 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
1816 "include BasicConstraints extension");
1820 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
1821 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
1822 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
1823 "keyCertSign bit in Key Usage");
1832 * x509_certificate_chain_validate - Validate X.509 certificate chain
1833 * @trusted: List of trusted certificates
1834 * @chain: Certificate chain to be validated (first chain must be issued by
1835 * signed by the second certificate in the chain and so on)
1836 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
1837 * Returns: 0 if chain is valid, -1 if not
1839 int x509_certificate_chain_validate(struct x509_certificate *trusted,
1840 struct x509_certificate *chain,
1841 int *reason, int disable_time_checks)
1844 int chain_trusted = 0;
1845 struct x509_certificate *cert, *trust;
1849 *reason = X509_VALIDATE_OK;
1851 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
1854 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
1855 x509_name_string(&cert->subject, buf, sizeof(buf));
1856 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
1861 if (!disable_time_checks &&
1862 ((unsigned long) now.sec <
1863 (unsigned long) cert->not_before ||
1864 (unsigned long) now.sec >
1865 (unsigned long) cert->not_after)) {
1866 wpa_printf(MSG_INFO, "X509: Certificate not valid "
1867 "(now=%lu not_before=%lu not_after=%lu)",
1868 now.sec, cert->not_before, cert->not_after);
1869 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
1874 if (x509_name_compare(&cert->issuer,
1875 &cert->next->subject) != 0) {
1876 wpa_printf(MSG_DEBUG, "X509: Certificate "
1877 "chain issuer name mismatch");
1878 x509_name_string(&cert->issuer, buf,
1880 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
1882 x509_name_string(&cert->next->subject, buf,
1884 wpa_printf(MSG_DEBUG, "X509: next cert "
1885 "subject: %s", buf);
1886 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
1890 if (x509_valid_issuer(cert->next) < 0) {
1891 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1895 if ((cert->next->extensions_present &
1896 X509_EXT_PATH_LEN_CONSTRAINT) &&
1897 idx > cert->next->path_len_constraint) {
1898 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
1899 " not met (idx=%lu issuer "
1900 "pathLenConstraint=%lu)", idx,
1901 cert->next->path_len_constraint);
1902 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1906 if (x509_certificate_check_signature(cert->next, cert)
1908 wpa_printf(MSG_DEBUG, "X509: Invalid "
1909 "certificate signature within "
1911 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1916 for (trust = trusted; trust; trust = trust->next) {
1917 if (x509_name_compare(&cert->issuer, &trust->subject)
1923 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
1924 "list of trusted certificates");
1925 if (x509_valid_issuer(trust) < 0) {
1926 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1930 if (x509_certificate_check_signature(trust, cert) < 0)
1932 wpa_printf(MSG_DEBUG, "X509: Invalid "
1933 "certificate signature");
1934 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1938 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
1939 "found to complete the chain");
1944 if (!chain_trusted) {
1945 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
1946 "from the list of trusted certificates");
1948 *reason = X509_VALIDATE_UNKNOWN_CA;
1951 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
1952 "disabled - ignore unknown CA issue");
1955 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
1962 * x509_certificate_get_subject - Get a certificate based on Subject name
1963 * @chain: Certificate chain to search through
1964 * @name: Subject name to search for
1965 * Returns: Pointer to the certificate with the given Subject name or
1968 struct x509_certificate *
1969 x509_certificate_get_subject(struct x509_certificate *chain,
1970 struct x509_name *name)
1972 struct x509_certificate *cert;
1974 for (cert = chain; cert; cert = cert->next) {
1975 if (x509_name_compare(&cert->subject, name) == 0)
1983 * x509_certificate_self_signed - Is the certificate self-signed?
1984 * @cert: Certificate
1985 * Returns: 1 if certificate is self-signed, 0 if not
1987 int x509_certificate_self_signed(struct x509_certificate *cert)
1989 return x509_name_compare(&cert->issuer, &cert->subject) == 0;