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.
18 #include "crypto/crypto.h"
23 static void x509_free_name(struct x509_name *name)
27 for (i = 0; i < name->num_attr; i++) {
28 os_free(name->attr[i].value);
29 name->attr[i].value = NULL;
30 name->attr[i].type = X509_NAME_ATTR_NOT_USED;
36 os_free(name->alt_email);
40 name->alt_email = name->dns = name->uri = NULL;
43 os_memset(&name->rid, 0, sizeof(name->rid));
48 * x509_certificate_free - Free an X.509 certificate
49 * @cert: Certificate to be freed
51 void x509_certificate_free(struct x509_certificate *cert)
56 wpa_printf(MSG_DEBUG, "X509: x509_certificate_free: cer=%p "
57 "was still on a list (next=%p)\n",
60 x509_free_name(&cert->issuer);
61 x509_free_name(&cert->subject);
62 os_free(cert->public_key);
63 os_free(cert->sign_value);
69 * x509_certificate_free - Free an X.509 certificate chain
70 * @cert: Pointer to the first certificate in the chain
72 void x509_certificate_chain_free(struct x509_certificate *cert)
74 struct x509_certificate *next;
79 x509_certificate_free(cert);
85 static int x509_whitespace(char c)
87 return c == ' ' || c == '\t';
91 static void x509_str_strip_whitespace(char *a)
94 int remove_whitespace = 1;
99 if (remove_whitespace && x509_whitespace(*ipos))
102 remove_whitespace = x509_whitespace(*ipos);
108 if (opos > a && x509_whitespace(*opos))
113 static int x509_str_compare(const char *a, const char *b)
128 if (aa == NULL || bb == NULL) {
131 return os_strcasecmp(a, b);
134 x509_str_strip_whitespace(aa);
135 x509_str_strip_whitespace(bb);
137 ret = os_strcasecmp(aa, bb);
147 * x509_name_compare - Compare X.509 certificate names
148 * @a: Certificate name
149 * @b: Certificate name
150 * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
153 int x509_name_compare(struct x509_name *a, struct x509_name *b)
164 if (a->num_attr < b->num_attr)
166 if (a->num_attr > b->num_attr)
169 for (i = 0; i < a->num_attr; i++) {
170 if (a->attr[i].type < b->attr[i].type)
172 if (a->attr[i].type > b->attr[i].type)
174 res = x509_str_compare(a->attr[i].value, b->attr[i].value);
178 res = x509_str_compare(a->email, b->email);
186 static int x509_parse_algorithm_identifier(
187 const u8 *buf, size_t len,
188 struct x509_algorithm_identifier *id, const u8 **next)
194 * AlgorithmIdentifier ::= SEQUENCE {
195 * algorithm OBJECT IDENTIFIER,
196 * parameters ANY DEFINED BY algorithm OPTIONAL
200 if (asn1_get_next(buf, len, &hdr) < 0 ||
201 hdr.class != ASN1_CLASS_UNIVERSAL ||
202 hdr.tag != ASN1_TAG_SEQUENCE) {
203 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
204 "(AlgorithmIdentifier) - found class %d tag 0x%x",
209 end = pos + hdr.length;
216 if (asn1_get_oid(pos, end - pos, &id->oid, &pos))
219 /* TODO: optional parameters */
225 static int x509_parse_public_key(const u8 *buf, size_t len,
226 struct x509_certificate *cert,
233 * SubjectPublicKeyInfo ::= SEQUENCE {
234 * algorithm AlgorithmIdentifier,
235 * subjectPublicKey BIT STRING
242 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
243 hdr.class != ASN1_CLASS_UNIVERSAL ||
244 hdr.tag != ASN1_TAG_SEQUENCE) {
245 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
246 "(SubjectPublicKeyInfo) - found class %d tag 0x%x",
252 if (pos + hdr.length > end)
254 end = pos + hdr.length;
257 if (x509_parse_algorithm_identifier(pos, end - pos,
258 &cert->public_key_alg, &pos))
261 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
262 hdr.class != ASN1_CLASS_UNIVERSAL ||
263 hdr.tag != ASN1_TAG_BITSTRING) {
264 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
265 "(subjectPublicKey) - found class %d tag 0x%x",
273 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
276 * TODO: should this be rejected? X.509 certificates are
277 * unlikely to use such a construction. Now we would end up
278 * including the extra bits in the buffer which may also be
282 os_free(cert->public_key);
283 cert->public_key = os_malloc(hdr.length - 1);
284 if (cert->public_key == NULL) {
285 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
289 os_memcpy(cert->public_key, pos + 1, hdr.length - 1);
290 cert->public_key_len = hdr.length - 1;
291 wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
292 cert->public_key, cert->public_key_len);
298 static int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
302 const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
307 * Name ::= CHOICE { RDNSequence }
308 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
309 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
310 * AttributeTypeAndValue ::= SEQUENCE {
311 * type AttributeType,
312 * value AttributeValue
314 * AttributeType ::= OBJECT IDENTIFIER
315 * AttributeValue ::= ANY DEFINED BY AttributeType
318 if (asn1_get_next(buf, len, &hdr) < 0 ||
319 hdr.class != ASN1_CLASS_UNIVERSAL ||
320 hdr.tag != ASN1_TAG_SEQUENCE) {
321 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
322 "(Name / RDNSequencer) - found class %d tag 0x%x",
328 if (pos + hdr.length > buf + len)
331 end = *next = pos + hdr.length;
334 enum x509_name_attr_type type;
336 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
337 hdr.class != ASN1_CLASS_UNIVERSAL ||
338 hdr.tag != ASN1_TAG_SET) {
339 wpa_printf(MSG_DEBUG, "X509: Expected SET "
340 "(RelativeDistinguishedName) - found class "
341 "%d tag 0x%x", hdr.class, hdr.tag);
342 x509_free_name(name);
346 set_pos = hdr.payload;
347 pos = set_end = hdr.payload + hdr.length;
349 if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
350 hdr.class != ASN1_CLASS_UNIVERSAL ||
351 hdr.tag != ASN1_TAG_SEQUENCE) {
352 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
353 "(AttributeTypeAndValue) - found class %d "
354 "tag 0x%x", hdr.class, hdr.tag);
355 x509_free_name(name);
359 seq_pos = hdr.payload;
360 seq_end = hdr.payload + hdr.length;
362 if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
363 x509_free_name(name);
367 if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
368 hdr.class != ASN1_CLASS_UNIVERSAL) {
369 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
371 x509_free_name(name);
376 * MUST: country, organization, organizational-unit,
377 * distinguished name qualifier, state or province name,
378 * common name, serial number.
379 * SHOULD: locality, title, surname, given name, initials,
380 * pseudonym, generation qualifier.
381 * MUST: domainComponent (RFC 2247).
383 type = X509_NAME_ATTR_NOT_USED;
385 oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
386 /* id-at ::= 2.5.4 */
387 switch (oid.oid[3]) {
390 type = X509_NAME_ATTR_CN;
394 type = X509_NAME_ATTR_C;
398 type = X509_NAME_ATTR_L;
401 /* stateOrProvinceName */
402 type = X509_NAME_ATTR_ST;
405 /* organizationName */
406 type = X509_NAME_ATTR_O;
409 /* organizationalUnitName */
410 type = X509_NAME_ATTR_OU;
413 } else if (oid.len == 7 &&
414 oid.oid[0] == 1 && oid.oid[1] == 2 &&
415 oid.oid[2] == 840 && oid.oid[3] == 113549 &&
416 oid.oid[4] == 1 && oid.oid[5] == 9 &&
418 /* 1.2.840.113549.1.9.1 - e-mailAddress */
419 os_free(name->email);
420 name->email = os_malloc(hdr.length + 1);
421 if (name->email == NULL) {
422 x509_free_name(name);
425 os_memcpy(name->email, hdr.payload, hdr.length);
426 name->email[hdr.length] = '\0';
428 } else if (oid.len == 7 &&
429 oid.oid[0] == 0 && oid.oid[1] == 9 &&
430 oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
431 oid.oid[4] == 100 && oid.oid[5] == 1 &&
433 /* 0.9.2342.19200300.100.1.25 - domainComponent */
434 type = X509_NAME_ATTR_DC;
437 if (type == X509_NAME_ATTR_NOT_USED) {
438 wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
440 oid.len * sizeof(oid.oid[0]));
441 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
442 hdr.payload, hdr.length);
446 if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
447 wpa_printf(MSG_INFO, "X509: Too many Name attributes");
448 x509_free_name(name);
452 val = os_malloc(hdr.length + 1);
454 x509_free_name(name);
457 os_memcpy(val, hdr.payload, hdr.length);
458 val[hdr.length] = '\0';
459 if (os_strlen(val) != hdr.length) {
460 wpa_printf(MSG_INFO, "X509: Reject certificate with "
461 "embedded NUL byte in a string (%s[NUL])",
463 x509_free_name(name);
467 name->attr[name->num_attr].type = type;
468 name->attr[name->num_attr].value = val;
476 static char * x509_name_attr_str(enum x509_name_attr_type type)
479 case X509_NAME_ATTR_NOT_USED:
481 case X509_NAME_ATTR_DC:
483 case X509_NAME_ATTR_CN:
485 case X509_NAME_ATTR_C:
487 case X509_NAME_ATTR_L:
489 case X509_NAME_ATTR_ST:
491 case X509_NAME_ATTR_O:
493 case X509_NAME_ATTR_OU:
501 * x509_name_string - Convert an X.509 certificate name into a string
502 * @name: Name to convert
503 * @buf: Buffer for the string
504 * @len: Maximum buffer length
506 void x509_name_string(struct x509_name *name, char *buf, size_t len)
518 for (i = 0; i < name->num_attr; i++) {
519 ret = os_snprintf(pos, end - pos, "%s=%s, ",
520 x509_name_attr_str(name->attr[i].type),
521 name->attr[i].value);
522 if (ret < 0 || ret >= end - pos)
527 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
535 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
537 if (ret < 0 || ret >= end - pos)
547 static int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag,
551 int year, month, day, hour, min, sec;
556 * generalTime GeneralizedTime
559 * UTCTime: YYMMDDHHMMSSZ
560 * GeneralizedTime: YYYYMMDDHHMMSSZ
563 pos = (const char *) buf;
566 case ASN1_TAG_UTCTIME:
567 if (len != 13 || buf[12] != 'Z') {
568 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
569 "UTCTime format", buf, len);
572 if (sscanf(pos, "%02d", &year) != 1) {
573 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
574 "UTCTime year", buf, len);
583 case ASN1_TAG_GENERALIZEDTIME:
584 if (len != 15 || buf[14] != 'Z') {
585 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
586 "GeneralizedTime format", buf, len);
589 if (sscanf(pos, "%04d", &year) != 1) {
590 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
591 "GeneralizedTime year", buf, len);
597 wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
598 "GeneralizedTime - found tag 0x%x", asn1_tag);
602 if (sscanf(pos, "%02d", &month) != 1) {
603 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
604 "(month)", buf, len);
609 if (sscanf(pos, "%02d", &day) != 1) {
610 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
616 if (sscanf(pos, "%02d", &hour) != 1) {
617 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
623 if (sscanf(pos, "%02d", &min) != 1) {
624 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
630 if (sscanf(pos, "%02d", &sec) != 1) {
631 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
636 if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
637 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
641 * At least some test certificates have been configured
642 * to use dates prior to 1970. Set the date to
643 * beginning of 1970 to handle these case.
645 wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
646 "assume epoch as the time", year);
657 static int x509_parse_validity(const u8 *buf, size_t len,
658 struct x509_certificate *cert, const u8 **next)
665 * Validity ::= SEQUENCE {
671 * CAs conforming to this profile MUST always encode certificate
672 * validity dates through the year 2049 as UTCTime; certificate
673 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
676 if (asn1_get_next(buf, len, &hdr) < 0 ||
677 hdr.class != ASN1_CLASS_UNIVERSAL ||
678 hdr.tag != ASN1_TAG_SEQUENCE) {
679 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
680 "(Validity) - found class %d tag 0x%x",
687 if (pos + plen > buf + len)
692 if (asn1_get_next(pos, plen, &hdr) < 0 ||
693 hdr.class != ASN1_CLASS_UNIVERSAL ||
694 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
695 &cert->not_before) < 0) {
696 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
697 "Time", hdr.payload, hdr.length);
701 pos = hdr.payload + hdr.length;
704 if (asn1_get_next(pos, plen, &hdr) < 0 ||
705 hdr.class != ASN1_CLASS_UNIVERSAL ||
706 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
707 &cert->not_after) < 0) {
708 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
709 "Time", hdr.payload, hdr.length);
713 wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
714 (unsigned long) cert->not_before,
715 (unsigned long) cert->not_after);
721 static int x509_id_ce_oid(struct asn1_oid *oid)
723 /* id-ce arc from X.509 for standard X.509v3 extensions */
724 return oid->len >= 4 &&
725 oid->oid[0] == 2 /* joint-iso-ccitt */ &&
726 oid->oid[1] == 5 /* ds */ &&
727 oid->oid[2] == 29 /* id-ce */;
731 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
732 const u8 *pos, size_t len)
737 * KeyUsage ::= BIT STRING {
738 * digitalSignature (0),
739 * nonRepudiation (1),
740 * keyEncipherment (2),
741 * dataEncipherment (3),
749 if (asn1_get_next(pos, len, &hdr) < 0 ||
750 hdr.class != ASN1_CLASS_UNIVERSAL ||
751 hdr.tag != ASN1_TAG_BITSTRING ||
753 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
754 "KeyUsage; found %d tag 0x%x len %d",
755 hdr.class, hdr.tag, hdr.length);
759 cert->extensions_present |= X509_EXT_KEY_USAGE;
760 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
762 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
768 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
769 const u8 *pos, size_t len)
776 * BasicConstraints ::= SEQUENCE {
777 * cA BOOLEAN DEFAULT FALSE,
778 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
781 if (asn1_get_next(pos, len, &hdr) < 0 ||
782 hdr.class != ASN1_CLASS_UNIVERSAL ||
783 hdr.tag != ASN1_TAG_SEQUENCE) {
784 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
785 "BasicConstraints; found %d tag 0x%x",
790 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
795 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
796 hdr.class != ASN1_CLASS_UNIVERSAL) {
797 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
802 if (hdr.tag == ASN1_TAG_BOOLEAN) {
803 if (hdr.length != 1) {
804 wpa_printf(MSG_DEBUG, "X509: Unexpected "
805 "Boolean length (%u) in BasicConstraints",
809 cert->ca = hdr.payload[0];
811 if (hdr.payload + hdr.length == pos + len) {
812 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
817 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
819 hdr.class != ASN1_CLASS_UNIVERSAL) {
820 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
826 if (hdr.tag != ASN1_TAG_INTEGER) {
827 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
828 "BasicConstraints; found class %d tag 0x%x",
842 cert->path_len_constraint = value;
843 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
845 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
846 "pathLenConstraint=%lu",
847 cert->ca, cert->path_len_constraint);
853 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
854 const u8 *pos, size_t len)
856 /* rfc822Name IA5String */
857 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
858 os_free(name->alt_email);
859 name->alt_email = os_zalloc(len + 1);
860 if (name->alt_email == NULL)
862 os_memcpy(name->alt_email, pos, len);
863 if (os_strlen(name->alt_email) != len) {
864 wpa_printf(MSG_INFO, "X509: Reject certificate with "
865 "embedded NUL byte in rfc822Name (%s[NUL])",
867 os_free(name->alt_email);
868 name->alt_email = NULL;
875 static int x509_parse_alt_name_dns(struct x509_name *name,
876 const u8 *pos, size_t len)
878 /* dNSName IA5String */
879 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
881 name->dns = os_zalloc(len + 1);
882 if (name->dns == NULL)
884 os_memcpy(name->dns, pos, len);
885 if (os_strlen(name->dns) != len) {
886 wpa_printf(MSG_INFO, "X509: Reject certificate with "
887 "embedded NUL byte in dNSName (%s[NUL])",
897 static int x509_parse_alt_name_uri(struct x509_name *name,
898 const u8 *pos, size_t len)
900 /* uniformResourceIdentifier IA5String */
901 wpa_hexdump_ascii(MSG_MSGDUMP,
902 "X509: altName - uniformResourceIdentifier",
905 name->uri = os_zalloc(len + 1);
906 if (name->uri == NULL)
908 os_memcpy(name->uri, pos, len);
909 if (os_strlen(name->uri) != len) {
910 wpa_printf(MSG_INFO, "X509: Reject certificate with "
911 "embedded NUL byte in uniformResourceIdentifier "
912 "(%s[NUL])", name->uri);
921 static int x509_parse_alt_name_ip(struct x509_name *name,
922 const u8 *pos, size_t len)
924 /* iPAddress OCTET STRING */
925 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
927 name->ip = os_malloc(len);
928 if (name->ip == NULL)
930 os_memcpy(name->ip, pos, len);
936 static int x509_parse_alt_name_rid(struct x509_name *name,
937 const u8 *pos, size_t len)
941 /* registeredID OBJECT IDENTIFIER */
942 if (asn1_parse_oid(pos, len, &name->rid) < 0)
945 asn1_oid_to_str(&name->rid, buf, sizeof(buf));
946 wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
952 static int x509_parse_ext_alt_name(struct x509_name *name,
953 const u8 *pos, size_t len)
959 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
961 * GeneralName ::= CHOICE {
962 * otherName [0] OtherName,
963 * rfc822Name [1] IA5String,
964 * dNSName [2] IA5String,
965 * x400Address [3] ORAddress,
966 * directoryName [4] Name,
967 * ediPartyName [5] EDIPartyName,
968 * uniformResourceIdentifier [6] IA5String,
969 * iPAddress [7] OCTET STRING,
970 * registeredID [8] OBJECT IDENTIFIER }
972 * OtherName ::= SEQUENCE {
973 * type-id OBJECT IDENTIFIER,
974 * value [0] EXPLICIT ANY DEFINED BY type-id }
976 * EDIPartyName ::= SEQUENCE {
977 * nameAssigner [0] DirectoryString OPTIONAL,
978 * partyName [1] DirectoryString }
981 for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
984 if (asn1_get_next(p, end - p, &hdr) < 0) {
985 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
986 "SubjectAltName item");
990 if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
995 res = x509_parse_alt_name_rfc8222(name, hdr.payload,
999 res = x509_parse_alt_name_dns(name, hdr.payload,
1003 res = x509_parse_alt_name_uri(name, hdr.payload,
1007 res = x509_parse_alt_name_ip(name, hdr.payload,
1011 res = x509_parse_alt_name_rid(name, hdr.payload,
1014 case 0: /* TODO: otherName */
1015 case 3: /* TODO: x500Address */
1016 case 4: /* TODO: directoryName */
1017 case 5: /* TODO: ediPartyName */
1030 static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
1031 const u8 *pos, size_t len)
1033 struct asn1_hdr hdr;
1035 /* SubjectAltName ::= GeneralNames */
1037 if (asn1_get_next(pos, len, &hdr) < 0 ||
1038 hdr.class != ASN1_CLASS_UNIVERSAL ||
1039 hdr.tag != ASN1_TAG_SEQUENCE) {
1040 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1041 "SubjectAltName; found %d tag 0x%x",
1042 hdr.class, hdr.tag);
1046 wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
1047 cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
1049 if (hdr.length == 0)
1052 return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
1057 static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
1058 const u8 *pos, size_t len)
1060 struct asn1_hdr hdr;
1062 /* IssuerAltName ::= GeneralNames */
1064 if (asn1_get_next(pos, len, &hdr) < 0 ||
1065 hdr.class != ASN1_CLASS_UNIVERSAL ||
1066 hdr.tag != ASN1_TAG_SEQUENCE) {
1067 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1068 "IssuerAltName; found %d tag 0x%x",
1069 hdr.class, hdr.tag);
1073 wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
1074 cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
1076 if (hdr.length == 0)
1079 return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
1084 static int x509_parse_extension_data(struct x509_certificate *cert,
1085 struct asn1_oid *oid,
1086 const u8 *pos, size_t len)
1088 if (!x509_id_ce_oid(oid))
1091 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
1092 * certificate policies (section 4.2.1.5)
1093 * name constraints (section 4.2.1.11)
1094 * policy constraints (section 4.2.1.12)
1095 * extended key usage (section 4.2.1.13)
1096 * inhibit any-policy (section 4.2.1.15)
1098 switch (oid->oid[3]) {
1099 case 15: /* id-ce-keyUsage */
1100 return x509_parse_ext_key_usage(cert, pos, len);
1101 case 17: /* id-ce-subjectAltName */
1102 return x509_parse_ext_subject_alt_name(cert, pos, len);
1103 case 18: /* id-ce-issuerAltName */
1104 return x509_parse_ext_issuer_alt_name(cert, pos, len);
1105 case 19: /* id-ce-basicConstraints */
1106 return x509_parse_ext_basic_constraints(cert, pos, len);
1113 static int x509_parse_extension(struct x509_certificate *cert,
1114 const u8 *pos, size_t len, const u8 **next)
1117 struct asn1_hdr hdr;
1118 struct asn1_oid oid;
1119 int critical_ext = 0, res;
1123 * Extension ::= SEQUENCE {
1124 * extnID OBJECT IDENTIFIER,
1125 * critical BOOLEAN DEFAULT FALSE,
1126 * extnValue OCTET STRING
1130 if (asn1_get_next(pos, len, &hdr) < 0 ||
1131 hdr.class != ASN1_CLASS_UNIVERSAL ||
1132 hdr.tag != ASN1_TAG_SEQUENCE) {
1133 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1134 "Extensions: class %d tag 0x%x; expected SEQUENCE",
1135 hdr.class, hdr.tag);
1139 *next = end = pos + hdr.length;
1141 if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
1142 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
1143 "Extension (expected OID)");
1147 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1148 hdr.class != ASN1_CLASS_UNIVERSAL ||
1149 (hdr.tag != ASN1_TAG_BOOLEAN &&
1150 hdr.tag != ASN1_TAG_OCTETSTRING)) {
1151 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1152 "Extensions: class %d tag 0x%x; expected BOOLEAN "
1153 "or OCTET STRING", hdr.class, hdr.tag);
1157 if (hdr.tag == ASN1_TAG_BOOLEAN) {
1158 if (hdr.length != 1) {
1159 wpa_printf(MSG_DEBUG, "X509: Unexpected "
1160 "Boolean length (%u)", hdr.length);
1163 critical_ext = hdr.payload[0];
1165 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1166 (hdr.class != ASN1_CLASS_UNIVERSAL &&
1167 hdr.class != ASN1_CLASS_PRIVATE) ||
1168 hdr.tag != ASN1_TAG_OCTETSTRING) {
1169 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
1170 "in Extensions: class %d tag 0x%x; "
1171 "expected OCTET STRING",
1172 hdr.class, hdr.tag);
1177 asn1_oid_to_str(&oid, buf, sizeof(buf));
1178 wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
1180 wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
1182 res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
1185 if (res == 1 && critical_ext) {
1186 wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
1195 static int x509_parse_extensions(struct x509_certificate *cert,
1196 const u8 *pos, size_t len)
1199 struct asn1_hdr hdr;
1201 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
1203 if (asn1_get_next(pos, len, &hdr) < 0 ||
1204 hdr.class != ASN1_CLASS_UNIVERSAL ||
1205 hdr.tag != ASN1_TAG_SEQUENCE) {
1206 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
1207 "for Extensions: class %d tag 0x%x; "
1208 "expected SEQUENCE", hdr.class, hdr.tag);
1213 end = pos + hdr.length;
1216 if (x509_parse_extension(cert, pos, end - pos, &pos)
1225 static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
1226 struct x509_certificate *cert,
1229 struct asn1_hdr hdr;
1230 const u8 *pos, *end;
1233 unsigned long value;
1235 /* tbsCertificate TBSCertificate ::= SEQUENCE */
1236 if (asn1_get_next(buf, len, &hdr) < 0 ||
1237 hdr.class != ASN1_CLASS_UNIVERSAL ||
1238 hdr.tag != ASN1_TAG_SEQUENCE) {
1239 wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
1240 "with a valid SEQUENCE - found class %d tag 0x%x",
1241 hdr.class, hdr.tag);
1245 end = *next = pos + hdr.length;
1248 * version [0] EXPLICIT Version DEFAULT v1
1249 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1251 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1255 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
1256 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1259 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1260 hdr.tag != ASN1_TAG_INTEGER) {
1261 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1262 "version field - found class %d tag 0x%x",
1263 hdr.class, hdr.tag);
1266 if (hdr.length != 1) {
1267 wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
1268 "length %u (expected 1)", hdr.length);
1280 cert->version = value;
1281 if (cert->version != X509_CERT_V1 &&
1282 cert->version != X509_CERT_V2 &&
1283 cert->version != X509_CERT_V3) {
1284 wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
1289 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1292 cert->version = X509_CERT_V1;
1293 wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
1295 /* serialNumber CertificateSerialNumber ::= INTEGER */
1296 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1297 hdr.tag != ASN1_TAG_INTEGER) {
1298 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1299 "serialNumber; class=%d tag=0x%x",
1300 hdr.class, hdr.tag);
1307 cert->serial_number <<= 8;
1308 cert->serial_number |= *pos++;
1311 wpa_printf(MSG_MSGDUMP, "X509: serialNumber %lu", cert->serial_number);
1313 /* signature AlgorithmIdentifier */
1314 if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
1319 if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
1321 x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
1322 wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
1324 /* validity Validity */
1325 if (x509_parse_validity(pos, end - pos, cert, &pos))
1329 if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
1331 x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
1332 wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
1334 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1335 if (x509_parse_public_key(pos, end - pos, cert, &pos))
1341 if (cert->version == X509_CERT_V1)
1344 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1345 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1346 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1347 " tag to parse optional tbsCertificate "
1348 "field(s); parsed class %d tag 0x%x",
1349 hdr.class, hdr.tag);
1354 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1355 wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
1356 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1358 if (hdr.payload + hdr.length == end)
1361 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1362 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1363 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1364 " tag to parse optional tbsCertificate "
1365 "field(s); parsed class %d tag 0x%x",
1366 hdr.class, hdr.tag);
1372 /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
1373 wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
1374 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1376 if (hdr.payload + hdr.length == end)
1379 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1380 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1381 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1382 " tag to parse optional tbsCertificate "
1383 "field(s); parsed class %d tag 0x%x",
1384 hdr.class, hdr.tag);
1390 wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
1391 "Context-Specific tag %d in optional "
1392 "tbsCertificate fields", hdr.tag);
1396 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1398 if (cert->version != X509_CERT_V3) {
1399 wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
1400 "Extensions data which are only allowed for "
1401 "version 3", cert->version + 1);
1405 if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
1408 pos = hdr.payload + hdr.length;
1410 wpa_hexdump(MSG_DEBUG,
1411 "X509: Ignored extra tbsCertificate data",
1419 static int x509_rsadsi_oid(struct asn1_oid *oid)
1421 return oid->len >= 4 &&
1422 oid->oid[0] == 1 /* iso */ &&
1423 oid->oid[1] == 2 /* member-body */ &&
1424 oid->oid[2] == 840 /* us */ &&
1425 oid->oid[3] == 113549 /* rsadsi */;
1429 static int x509_pkcs_oid(struct asn1_oid *oid)
1431 return oid->len >= 5 &&
1432 x509_rsadsi_oid(oid) &&
1433 oid->oid[4] == 1 /* pkcs */;
1437 static int x509_digest_oid(struct asn1_oid *oid)
1439 return oid->len >= 5 &&
1440 x509_rsadsi_oid(oid) &&
1441 oid->oid[4] == 2 /* digestAlgorithm */;
1445 static int x509_sha1_oid(struct asn1_oid *oid)
1447 return oid->len == 6 &&
1448 oid->oid[0] == 1 /* iso */ &&
1449 oid->oid[1] == 3 /* identified-organization */ &&
1450 oid->oid[2] == 14 /* oiw */ &&
1451 oid->oid[3] == 3 /* secsig */ &&
1452 oid->oid[4] == 2 /* algorithms */ &&
1453 oid->oid[5] == 26 /* id-sha1 */;
1457 static int x509_sha256_oid(struct asn1_oid *oid)
1459 return oid->len == 9 &&
1460 oid->oid[0] == 2 /* joint-iso-itu-t */ &&
1461 oid->oid[1] == 16 /* country */ &&
1462 oid->oid[2] == 840 /* us */ &&
1463 oid->oid[3] == 1 /* organization */ &&
1464 oid->oid[4] == 101 /* gov */ &&
1465 oid->oid[5] == 3 /* csor */ &&
1466 oid->oid[6] == 4 /* nistAlgorithm */ &&
1467 oid->oid[7] == 2 /* hashAlgs */ &&
1468 oid->oid[8] == 1 /* sha256 */;
1473 * x509_certificate_parse - Parse a X.509 certificate in DER format
1474 * @buf: Pointer to the X.509 certificate in DER format
1475 * @len: Buffer length
1476 * Returns: Pointer to the parsed certificate or %NULL on failure
1478 * Caller is responsible for freeing the returned certificate by calling
1479 * x509_certificate_free().
1481 struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
1483 struct asn1_hdr hdr;
1484 const u8 *pos, *end, *hash_start;
1485 struct x509_certificate *cert;
1487 cert = os_zalloc(sizeof(*cert) + len);
1490 os_memcpy(cert + 1, buf, len);
1491 cert->cert_start = (u8 *) (cert + 1);
1492 cert->cert_len = len;
1497 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1499 /* Certificate ::= SEQUENCE */
1500 if (asn1_get_next(pos, len, &hdr) < 0 ||
1501 hdr.class != ASN1_CLASS_UNIVERSAL ||
1502 hdr.tag != ASN1_TAG_SEQUENCE) {
1503 wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
1504 "a valid SEQUENCE - found class %d tag 0x%x",
1505 hdr.class, hdr.tag);
1506 x509_certificate_free(cert);
1511 if (pos + hdr.length > end) {
1512 x509_certificate_free(cert);
1516 if (pos + hdr.length < end) {
1517 wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
1518 "encoded certificate",
1519 pos + hdr.length, end - pos + hdr.length);
1520 end = pos + hdr.length;
1524 cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
1525 if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
1526 x509_certificate_free(cert);
1529 cert->tbs_cert_len = pos - hash_start;
1531 /* signatureAlgorithm AlgorithmIdentifier */
1532 if (x509_parse_algorithm_identifier(pos, end - pos,
1533 &cert->signature_alg, &pos)) {
1534 x509_certificate_free(cert);
1538 /* signatureValue BIT STRING */
1539 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1540 hdr.class != ASN1_CLASS_UNIVERSAL ||
1541 hdr.tag != ASN1_TAG_BITSTRING) {
1542 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
1543 "(signatureValue) - found class %d tag 0x%x",
1544 hdr.class, hdr.tag);
1545 x509_certificate_free(cert);
1548 if (hdr.length < 1) {
1549 x509_certificate_free(cert);
1554 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
1556 /* PKCS #1 v1.5 10.2.1:
1557 * It is an error if the length in bits of the signature S is
1558 * not a multiple of eight.
1560 x509_certificate_free(cert);
1563 os_free(cert->sign_value);
1564 cert->sign_value = os_malloc(hdr.length - 1);
1565 if (cert->sign_value == NULL) {
1566 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
1568 x509_certificate_free(cert);
1571 os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
1572 cert->sign_value_len = hdr.length - 1;
1573 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1574 cert->sign_value, cert->sign_value_len);
1581 * x509_certificate_check_signature - Verify certificate signature
1582 * @issuer: Issuer certificate
1583 * @cert: Certificate to be verified
1584 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1587 int x509_certificate_check_signature(struct x509_certificate *issuer,
1588 struct x509_certificate *cert)
1590 struct crypto_public_key *pk;
1592 const u8 *pos, *end, *next, *da_end;
1594 struct asn1_hdr hdr;
1595 struct asn1_oid oid;
1599 if (!x509_pkcs_oid(&cert->signature.oid) ||
1600 cert->signature.oid.len != 7 ||
1601 cert->signature.oid.oid[5] != 1 /* pkcs-1 */) {
1602 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1607 pk = crypto_public_key_import(issuer->public_key,
1608 issuer->public_key_len);
1612 data_len = cert->sign_value_len;
1613 data = os_malloc(data_len);
1615 crypto_public_key_free(pk);
1619 if (crypto_public_key_decrypt_pkcs1(pk, cert->sign_value,
1620 cert->sign_value_len, data,
1622 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1623 crypto_public_key_free(pk);
1627 crypto_public_key_free(pk);
1629 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1632 * PKCS #1 v1.5, 10.1.2:
1634 * DigestInfo ::= SEQUENCE {
1635 * digestAlgorithm DigestAlgorithmIdentifier,
1639 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1641 * Digest ::= OCTET STRING
1644 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1645 hdr.class != ASN1_CLASS_UNIVERSAL ||
1646 hdr.tag != ASN1_TAG_SEQUENCE) {
1647 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1648 "(DigestInfo) - found class %d tag 0x%x",
1649 hdr.class, hdr.tag);
1655 end = pos + hdr.length;
1659 * AlgorithmIdentifier ::= SEQUENCE {
1660 * algorithm OBJECT IDENTIFIER,
1661 * parameters ANY DEFINED BY algorithm OPTIONAL
1665 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1666 hdr.class != ASN1_CLASS_UNIVERSAL ||
1667 hdr.tag != ASN1_TAG_SEQUENCE) {
1668 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1669 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1670 hdr.class, hdr.tag);
1674 da_end = hdr.payload + hdr.length;
1676 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1677 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1682 if (x509_sha1_oid(&oid)) {
1683 if (cert->signature.oid.oid[6] !=
1684 5 /* sha-1WithRSAEncryption */) {
1685 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1686 "does not match with certificate "
1687 "signatureAlgorithm (%lu)",
1688 cert->signature.oid.oid[6]);
1692 goto skip_digest_oid;
1695 if (x509_sha256_oid(&oid)) {
1696 if (cert->signature.oid.oid[6] !=
1697 11 /* sha2561WithRSAEncryption */) {
1698 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1699 "does not match with certificate "
1700 "signatureAlgorithm (%lu)",
1701 cert->signature.oid.oid[6]);
1705 goto skip_digest_oid;
1708 if (!x509_digest_oid(&oid)) {
1709 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1713 switch (oid.oid[5]) {
1715 if (cert->signature.oid.oid[6] != 4 /* md5WithRSAEncryption */)
1717 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1718 "not match with certificate "
1719 "signatureAlgorithm (%lu)",
1720 cert->signature.oid.oid[6]);
1728 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1729 "(%lu)", oid.oid[5]);
1735 /* Digest ::= OCTET STRING */
1737 end = data + data_len;
1739 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1740 hdr.class != ASN1_CLASS_UNIVERSAL ||
1741 hdr.tag != ASN1_TAG_OCTETSTRING) {
1742 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1743 "(Digest) - found class %d tag 0x%x",
1744 hdr.class, hdr.tag);
1748 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1749 hdr.payload, hdr.length);
1751 switch (cert->signature.oid.oid[6]) {
1752 case 4: /* md5WithRSAEncryption */
1753 md5_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1756 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1759 case 5: /* sha-1WithRSAEncryption */
1760 sha1_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1763 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1766 case 11: /* sha256WithRSAEncryption */
1767 sha256_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1770 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1773 case 2: /* md2WithRSAEncryption */
1774 case 12: /* sha384WithRSAEncryption */
1775 case 13: /* sha512WithRSAEncryption */
1777 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1778 "algorithm (%lu)", cert->signature.oid.oid[6]);
1783 if (hdr.length != hash_len ||
1784 os_memcmp(hdr.payload, hash, hdr.length) != 0) {
1785 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1786 "with calculated tbsCertificate hash");
1793 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1794 "calculated tbsCertificate hash");
1800 static int x509_valid_issuer(const struct x509_certificate *cert)
1802 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1804 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1809 if (cert->version == X509_CERT_V3 &&
1810 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
1811 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
1812 "include BasicConstraints extension");
1816 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
1817 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
1818 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
1819 "keyCertSign bit in Key Usage");
1828 * x509_certificate_chain_validate - Validate X.509 certificate chain
1829 * @trusted: List of trusted certificates
1830 * @chain: Certificate chain to be validated (first chain must be issued by
1831 * signed by the second certificate in the chain and so on)
1832 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
1833 * Returns: 0 if chain is valid, -1 if not
1835 int x509_certificate_chain_validate(struct x509_certificate *trusted,
1836 struct x509_certificate *chain,
1840 int chain_trusted = 0;
1841 struct x509_certificate *cert, *trust;
1845 *reason = X509_VALIDATE_OK;
1847 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
1850 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
1851 x509_name_string(&cert->subject, buf, sizeof(buf));
1852 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
1857 if ((unsigned long) now.sec <
1858 (unsigned long) cert->not_before ||
1859 (unsigned long) now.sec >
1860 (unsigned long) cert->not_after) {
1861 wpa_printf(MSG_INFO, "X509: Certificate not valid "
1862 "(now=%lu not_before=%lu not_after=%lu)",
1863 now.sec, cert->not_before, cert->not_after);
1864 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
1869 if (x509_name_compare(&cert->issuer,
1870 &cert->next->subject) != 0) {
1871 wpa_printf(MSG_DEBUG, "X509: Certificate "
1872 "chain issuer name mismatch");
1873 x509_name_string(&cert->issuer, buf,
1875 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
1877 x509_name_string(&cert->next->subject, buf,
1879 wpa_printf(MSG_DEBUG, "X509: next cert "
1880 "subject: %s", buf);
1881 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
1885 if (x509_valid_issuer(cert->next) < 0) {
1886 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1890 if ((cert->next->extensions_present &
1891 X509_EXT_PATH_LEN_CONSTRAINT) &&
1892 idx > cert->next->path_len_constraint) {
1893 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
1894 " not met (idx=%lu issuer "
1895 "pathLenConstraint=%lu)", idx,
1896 cert->next->path_len_constraint);
1897 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1901 if (x509_certificate_check_signature(cert->next, cert)
1903 wpa_printf(MSG_DEBUG, "X509: Invalid "
1904 "certificate signature within "
1906 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1911 for (trust = trusted; trust; trust = trust->next) {
1912 if (x509_name_compare(&cert->issuer, &trust->subject)
1918 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
1919 "list of trusted certificates");
1920 if (x509_valid_issuer(trust) < 0) {
1921 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1925 if (x509_certificate_check_signature(trust, cert) < 0)
1927 wpa_printf(MSG_DEBUG, "X509: Invalid "
1928 "certificate signature");
1929 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1933 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
1934 "found to complete the chain");
1939 if (!chain_trusted) {
1940 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
1941 "from the list of trusted certificates");
1943 *reason = X509_VALIDATE_UNKNOWN_CA;
1946 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
1947 "disabled - ignore unknown CA issue");
1950 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
1957 * x509_certificate_get_subject - Get a certificate based on Subject name
1958 * @chain: Certificate chain to search through
1959 * @name: Subject name to search for
1960 * Returns: Pointer to the certificate with the given Subject name or
1963 struct x509_certificate *
1964 x509_certificate_get_subject(struct x509_certificate *chain,
1965 struct x509_name *name)
1967 struct x509_certificate *cert;
1969 for (cert = chain; cert; cert = cert->next) {
1970 if (x509_name_compare(&cert->subject, name) == 0)
1978 * x509_certificate_self_signed - Is the certificate self-signed?
1979 * @cert: Certificate
1980 * Returns: 1 if certificate is self-signed, 0 if not
1982 int x509_certificate_self_signed(struct x509_certificate *cert)
1984 return x509_name_compare(&cert->issuer, &cert->subject) == 0;