2 * X.509v3 certificate parsing and processing (RFC 3280 profile)
3 * Copyright (c) 2006-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #ifdef CONFIG_INTERNAL_X509
26 static void x509_free_name(struct x509_name *name)
35 name->cn = name->c = name->l = name->st = name->o = name->ou = NULL;
38 os_free(name->alt_email);
42 name->alt_email = name->dns = name->uri = NULL;
45 os_memset(&name->rid, 0, sizeof(name->rid));
50 * x509_certificate_free - Free an X.509 certificate
51 * @cert: Certificate to be freed
53 void x509_certificate_free(struct x509_certificate *cert)
58 wpa_printf(MSG_DEBUG, "X509: x509_certificate_free: cer=%p "
59 "was still on a list (next=%p)\n",
62 x509_free_name(&cert->issuer);
63 x509_free_name(&cert->subject);
64 os_free(cert->public_key);
65 os_free(cert->sign_value);
71 * x509_certificate_free - Free an X.509 certificate chain
72 * @cert: Pointer to the first certificate in the chain
74 void x509_certificate_chain_free(struct x509_certificate *cert)
76 struct x509_certificate *next;
81 x509_certificate_free(cert);
87 static int x509_whitespace(char c)
89 return c == ' ' || c == '\t';
93 static void x509_str_strip_whitespace(char *a)
96 int remove_whitespace = 1;
101 if (remove_whitespace && x509_whitespace(*ipos))
104 remove_whitespace = x509_whitespace(*ipos);
110 if (opos > a && x509_whitespace(*opos))
115 static int x509_str_compare(const char *a, const char *b)
130 if (aa == NULL || bb == NULL) {
133 return os_strcasecmp(a, b);
136 x509_str_strip_whitespace(aa);
137 x509_str_strip_whitespace(bb);
139 ret = os_strcasecmp(aa, bb);
149 * x509_name_compare - Compare X.509 certificate names
150 * @a: Certificate name
151 * @b: Certificate name
152 * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
155 int x509_name_compare(struct x509_name *a, struct x509_name *b)
166 res = x509_str_compare(a->cn, b->cn);
169 res = x509_str_compare(a->c, b->c);
172 res = x509_str_compare(a->l, b->l);
175 res = x509_str_compare(a->st, b->st);
178 res = x509_str_compare(a->o, b->o);
181 res = x509_str_compare(a->ou, b->ou);
184 res = x509_str_compare(a->email, b->email);
192 static int x509_parse_algorithm_identifier(
193 const u8 *buf, size_t len,
194 struct x509_algorithm_identifier *id, const u8 **next)
200 * AlgorithmIdentifier ::= SEQUENCE {
201 * algorithm OBJECT IDENTIFIER,
202 * parameters ANY DEFINED BY algorithm OPTIONAL
206 if (asn1_get_next(buf, len, &hdr) < 0 ||
207 hdr.class != ASN1_CLASS_UNIVERSAL ||
208 hdr.tag != ASN1_TAG_SEQUENCE) {
209 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
210 "(AlgorithmIdentifier) - found class %d tag 0x%x",
215 end = pos + hdr.length;
222 if (asn1_get_oid(pos, end - pos, &id->oid, &pos))
225 /* TODO: optional parameters */
231 static int x509_parse_public_key(const u8 *buf, size_t len,
232 struct x509_certificate *cert,
239 * SubjectPublicKeyInfo ::= SEQUENCE {
240 * algorithm AlgorithmIdentifier,
241 * subjectPublicKey BIT STRING
248 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
249 hdr.class != ASN1_CLASS_UNIVERSAL ||
250 hdr.tag != ASN1_TAG_SEQUENCE) {
251 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
252 "(SubjectPublicKeyInfo) - found class %d tag 0x%x",
258 if (pos + hdr.length > end)
260 end = pos + hdr.length;
263 if (x509_parse_algorithm_identifier(pos, end - pos,
264 &cert->public_key_alg, &pos))
267 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
268 hdr.class != ASN1_CLASS_UNIVERSAL ||
269 hdr.tag != ASN1_TAG_BITSTRING) {
270 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
271 "(subjectPublicKey) - found class %d tag 0x%x",
279 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
282 * TODO: should this be rejected? X.509 certificates are
283 * unlikely to use such a construction. Now we would end up
284 * including the extra bits in the buffer which may also be
288 os_free(cert->public_key);
289 cert->public_key = os_malloc(hdr.length - 1);
290 if (cert->public_key == NULL) {
291 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
295 os_memcpy(cert->public_key, pos + 1, hdr.length - 1);
296 cert->public_key_len = hdr.length - 1;
297 wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
298 cert->public_key, cert->public_key_len);
304 static int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
308 const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
313 * Name ::= CHOICE { RDNSequence }
314 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
315 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
316 * AttributeTypeAndValue ::= SEQUENCE {
317 * type AttributeType,
318 * value AttributeValue
320 * AttributeType ::= OBJECT IDENTIFIER
321 * AttributeValue ::= ANY DEFINED BY AttributeType
324 if (asn1_get_next(buf, len, &hdr) < 0 ||
325 hdr.class != ASN1_CLASS_UNIVERSAL ||
326 hdr.tag != ASN1_TAG_SEQUENCE) {
327 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
328 "(Name / RDNSequencer) - found class %d tag 0x%x",
334 if (pos + hdr.length > buf + len)
337 end = *next = pos + hdr.length;
340 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
341 hdr.class != ASN1_CLASS_UNIVERSAL ||
342 hdr.tag != ASN1_TAG_SET) {
343 wpa_printf(MSG_DEBUG, "X509: Expected SET "
344 "(RelativeDistinguishedName) - found class "
345 "%d tag 0x%x", hdr.class, hdr.tag);
346 x509_free_name(name);
350 set_pos = hdr.payload;
351 pos = set_end = hdr.payload + hdr.length;
353 if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
354 hdr.class != ASN1_CLASS_UNIVERSAL ||
355 hdr.tag != ASN1_TAG_SEQUENCE) {
356 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
357 "(AttributeTypeAndValue) - found class %d "
358 "tag 0x%x", hdr.class, hdr.tag);
359 x509_free_name(name);
363 seq_pos = hdr.payload;
364 seq_end = hdr.payload + hdr.length;
366 if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
367 x509_free_name(name);
371 if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
372 hdr.class != ASN1_CLASS_UNIVERSAL) {
373 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
375 x509_free_name(name);
380 * MUST: country, organization, organizational-unit,
381 * distinguished name qualifier, state or province name,
382 * common name, serial number.
383 * SHOULD: locality, title, surname, given name, initials,
384 * pseudonym, generation qualifier.
385 * MUST: domainComponent (RFC 2247).
389 oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
390 /* id-at ::= 2.5.4 */
391 switch (oid.oid[3]) {
405 /* stateOrProvinceName */
409 /* organizationName */
413 /* organizationalUnitName */
417 } else if (oid.len == 7 &&
418 oid.oid[0] == 1 && oid.oid[1] == 2 &&
419 oid.oid[2] == 840 && oid.oid[3] == 113549 &&
420 oid.oid[4] == 1 && oid.oid[5] == 9 &&
422 /* 1.2.840.113549.1.9.1 - e-mailAddress */
423 fieldp = &name->email;
426 if (fieldp == NULL) {
427 wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
429 oid.len * sizeof(oid.oid[0]));
430 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
431 hdr.payload, hdr.length);
436 *fieldp = os_malloc(hdr.length + 1);
437 if (*fieldp == NULL) {
438 x509_free_name(name);
441 os_memcpy(*fieldp, hdr.payload, hdr.length);
442 (*fieldp)[hdr.length] = '\0';
450 * x509_name_string - Convert an X.509 certificate name into a string
451 * @name: Name to convert
452 * @buf: Buffer for the string
453 * @len: Maximum buffer length
455 void x509_name_string(struct x509_name *name, char *buf, size_t len)
467 ret = os_snprintf(pos, end - pos, "C=%s, ", name->c);
468 if (ret < 0 || ret >= end - pos)
473 ret = os_snprintf(pos, end - pos, "ST=%s, ", name->st);
474 if (ret < 0 || ret >= end - pos)
479 ret = os_snprintf(pos, end - pos, "L=%s, ", name->l);
480 if (ret < 0 || ret >= end - pos)
485 ret = os_snprintf(pos, end - pos, "O=%s, ", name->o);
486 if (ret < 0 || ret >= end - pos)
491 ret = os_snprintf(pos, end - pos, "OU=%s, ", name->ou);
492 if (ret < 0 || ret >= end - pos)
497 ret = os_snprintf(pos, end - pos, "CN=%s, ", name->cn);
498 if (ret < 0 || ret >= end - pos)
503 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
509 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
511 if (ret < 0 || ret >= end - pos)
521 static int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag,
525 int year, month, day, hour, min, sec;
530 * generalTime GeneralizedTime
533 * UTCTime: YYMMDDHHMMSSZ
534 * GeneralizedTime: YYYYMMDDHHMMSSZ
537 pos = (const char *) buf;
540 case ASN1_TAG_UTCTIME:
541 if (len != 13 || buf[12] != 'Z') {
542 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
543 "UTCTime format", buf, len);
546 if (sscanf(pos, "%02d", &year) != 1) {
547 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
548 "UTCTime year", buf, len);
557 case ASN1_TAG_GENERALIZEDTIME:
558 if (len != 15 || buf[14] != 'Z') {
559 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
560 "GeneralizedTime format", buf, len);
563 if (sscanf(pos, "%04d", &year) != 1) {
564 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
565 "GeneralizedTime year", buf, len);
571 wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
572 "GeneralizedTime - found tag 0x%x", asn1_tag);
576 if (sscanf(pos, "%02d", &month) != 1) {
577 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
578 "(month)", buf, len);
583 if (sscanf(pos, "%02d", &day) != 1) {
584 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
590 if (sscanf(pos, "%02d", &hour) != 1) {
591 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
597 if (sscanf(pos, "%02d", &min) != 1) {
598 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
604 if (sscanf(pos, "%02d", &sec) != 1) {
605 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
610 if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
611 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
615 * At least some test certificates have been configured
616 * to use dates prior to 1970. Set the date to
617 * beginning of 1970 to handle these case.
619 wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
620 "assume epoch as the time", year);
631 static int x509_parse_validity(const u8 *buf, size_t len,
632 struct x509_certificate *cert, const u8 **next)
639 * Validity ::= SEQUENCE {
645 * CAs conforming to this profile MUST always encode certificate
646 * validity dates through the year 2049 as UTCTime; certificate
647 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
650 if (asn1_get_next(buf, len, &hdr) < 0 ||
651 hdr.class != ASN1_CLASS_UNIVERSAL ||
652 hdr.tag != ASN1_TAG_SEQUENCE) {
653 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
654 "(Validity) - found class %d tag 0x%x",
661 if (pos + plen > buf + len)
666 if (asn1_get_next(pos, plen, &hdr) < 0 ||
667 hdr.class != ASN1_CLASS_UNIVERSAL ||
668 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
669 &cert->not_before) < 0) {
670 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
671 "Time", hdr.payload, hdr.length);
675 pos = hdr.payload + hdr.length;
678 if (asn1_get_next(pos, plen, &hdr) < 0 ||
679 hdr.class != ASN1_CLASS_UNIVERSAL ||
680 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
681 &cert->not_after) < 0) {
682 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
683 "Time", hdr.payload, hdr.length);
687 wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
688 (unsigned long) cert->not_before,
689 (unsigned long) cert->not_after);
695 static int x509_id_ce_oid(struct asn1_oid *oid)
697 /* id-ce arc from X.509 for standard X.509v3 extensions */
698 return oid->len >= 4 &&
699 oid->oid[0] == 2 /* joint-iso-ccitt */ &&
700 oid->oid[1] == 5 /* ds */ &&
701 oid->oid[2] == 29 /* id-ce */;
705 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
706 const u8 *pos, size_t len)
711 * KeyUsage ::= BIT STRING {
712 * digitalSignature (0),
713 * nonRepudiation (1),
714 * keyEncipherment (2),
715 * dataEncipherment (3),
723 if (asn1_get_next(pos, len, &hdr) < 0 ||
724 hdr.class != ASN1_CLASS_UNIVERSAL ||
725 hdr.tag != ASN1_TAG_BITSTRING ||
727 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
728 "KeyUsage; found %d tag 0x%x len %d",
729 hdr.class, hdr.tag, hdr.length);
733 cert->extensions_present |= X509_EXT_KEY_USAGE;
734 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
736 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
742 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
743 const u8 *pos, size_t len)
750 * BasicConstraints ::= SEQUENCE {
751 * cA BOOLEAN DEFAULT FALSE,
752 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
755 if (asn1_get_next(pos, len, &hdr) < 0 ||
756 hdr.class != ASN1_CLASS_UNIVERSAL ||
757 hdr.tag != ASN1_TAG_SEQUENCE) {
758 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
759 "BasicConstraints; found %d tag 0x%x",
764 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
769 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
770 hdr.class != ASN1_CLASS_UNIVERSAL) {
771 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
776 if (hdr.tag == ASN1_TAG_BOOLEAN) {
777 if (hdr.length != 1) {
778 wpa_printf(MSG_DEBUG, "X509: Unexpected "
779 "Boolean length (%u) in BasicConstraints",
783 cert->ca = hdr.payload[0];
785 if (hdr.payload + hdr.length == pos + len) {
786 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
791 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
793 hdr.class != ASN1_CLASS_UNIVERSAL) {
794 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
800 if (hdr.tag != ASN1_TAG_INTEGER) {
801 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
802 "BasicConstraints; found class %d tag 0x%x",
816 cert->path_len_constraint = value;
817 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
819 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
820 "pathLenConstraint=%lu",
821 cert->ca, cert->path_len_constraint);
827 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
828 const u8 *pos, size_t len)
830 /* rfc822Name IA5String */
831 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
832 os_free(name->alt_email);
833 name->alt_email = os_zalloc(len + 1);
834 if (name->alt_email == NULL)
836 os_memcpy(name->alt_email, pos, len);
841 static int x509_parse_alt_name_dns(struct x509_name *name,
842 const u8 *pos, size_t len)
844 /* dNSName IA5String */
845 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
847 name->dns = os_zalloc(len + 1);
848 if (name->dns == NULL)
850 os_memcpy(name->dns, pos, len);
855 static int x509_parse_alt_name_uri(struct x509_name *name,
856 const u8 *pos, size_t len)
858 /* uniformResourceIdentifier IA5String */
859 wpa_hexdump_ascii(MSG_MSGDUMP,
860 "X509: altName - uniformResourceIdentifier",
863 name->uri = os_zalloc(len + 1);
864 if (name->uri == NULL)
866 os_memcpy(name->uri, pos, len);
871 static int x509_parse_alt_name_ip(struct x509_name *name,
872 const u8 *pos, size_t len)
874 /* iPAddress OCTET STRING */
875 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
877 name->ip = os_malloc(len);
878 if (name->ip == NULL)
880 os_memcpy(name->ip, pos, len);
886 static int x509_parse_alt_name_rid(struct x509_name *name,
887 const u8 *pos, size_t len)
891 /* registeredID OBJECT IDENTIFIER */
892 if (asn1_parse_oid(pos, len, &name->rid) < 0)
895 asn1_oid_to_str(&name->rid, buf, sizeof(buf));
896 wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
902 static int x509_parse_ext_alt_name(struct x509_name *name,
903 const u8 *pos, size_t len)
909 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
911 * GeneralName ::= CHOICE {
912 * otherName [0] OtherName,
913 * rfc822Name [1] IA5String,
914 * dNSName [2] IA5String,
915 * x400Address [3] ORAddress,
916 * directoryName [4] Name,
917 * ediPartyName [5] EDIPartyName,
918 * uniformResourceIdentifier [6] IA5String,
919 * iPAddress [7] OCTET STRING,
920 * registeredID [8] OBJECT IDENTIFIER }
922 * OtherName ::= SEQUENCE {
923 * type-id OBJECT IDENTIFIER,
924 * value [0] EXPLICIT ANY DEFINED BY type-id }
926 * EDIPartyName ::= SEQUENCE {
927 * nameAssigner [0] DirectoryString OPTIONAL,
928 * partyName [1] DirectoryString }
931 for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
934 if (asn1_get_next(p, end - p, &hdr) < 0) {
935 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
936 "SubjectAltName item");
940 if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
945 res = x509_parse_alt_name_rfc8222(name, hdr.payload,
949 res = x509_parse_alt_name_dns(name, hdr.payload,
953 res = x509_parse_alt_name_uri(name, hdr.payload,
957 res = x509_parse_alt_name_ip(name, hdr.payload,
961 res = x509_parse_alt_name_rid(name, hdr.payload,
964 case 0: /* TODO: otherName */
965 case 3: /* TODO: x500Address */
966 case 4: /* TODO: directoryName */
967 case 5: /* TODO: ediPartyName */
980 static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
981 const u8 *pos, size_t len)
985 /* SubjectAltName ::= GeneralNames */
987 if (asn1_get_next(pos, len, &hdr) < 0 ||
988 hdr.class != ASN1_CLASS_UNIVERSAL ||
989 hdr.tag != ASN1_TAG_SEQUENCE) {
990 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
991 "SubjectAltName; found %d tag 0x%x",
996 wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
997 cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
1002 return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
1007 static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
1008 const u8 *pos, size_t len)
1010 struct asn1_hdr hdr;
1012 /* IssuerAltName ::= GeneralNames */
1014 if (asn1_get_next(pos, len, &hdr) < 0 ||
1015 hdr.class != ASN1_CLASS_UNIVERSAL ||
1016 hdr.tag != ASN1_TAG_SEQUENCE) {
1017 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1018 "IssuerAltName; found %d tag 0x%x",
1019 hdr.class, hdr.tag);
1023 wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
1024 cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
1026 if (hdr.length == 0)
1029 return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
1034 static int x509_parse_extension_data(struct x509_certificate *cert,
1035 struct asn1_oid *oid,
1036 const u8 *pos, size_t len)
1038 if (!x509_id_ce_oid(oid))
1041 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
1042 * certificate policies (section 4.2.1.5)
1043 * name constraints (section 4.2.1.11)
1044 * policy constraints (section 4.2.1.12)
1045 * extended key usage (section 4.2.1.13)
1046 * inhibit any-policy (section 4.2.1.15)
1048 switch (oid->oid[3]) {
1049 case 15: /* id-ce-keyUsage */
1050 return x509_parse_ext_key_usage(cert, pos, len);
1051 case 17: /* id-ce-subjectAltName */
1052 return x509_parse_ext_subject_alt_name(cert, pos, len);
1053 case 18: /* id-ce-issuerAltName */
1054 return x509_parse_ext_issuer_alt_name(cert, pos, len);
1055 case 19: /* id-ce-basicConstraints */
1056 return x509_parse_ext_basic_constraints(cert, pos, len);
1063 static int x509_parse_extension(struct x509_certificate *cert,
1064 const u8 *pos, size_t len, const u8 **next)
1067 struct asn1_hdr hdr;
1068 struct asn1_oid oid;
1069 int critical_ext = 0, res;
1073 * Extension ::= SEQUENCE {
1074 * extnID OBJECT IDENTIFIER,
1075 * critical BOOLEAN DEFAULT FALSE,
1076 * extnValue OCTET STRING
1080 if (asn1_get_next(pos, len, &hdr) < 0 ||
1081 hdr.class != ASN1_CLASS_UNIVERSAL ||
1082 hdr.tag != ASN1_TAG_SEQUENCE) {
1083 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1084 "Extensions: class %d tag 0x%x; expected SEQUENCE",
1085 hdr.class, hdr.tag);
1089 *next = end = pos + hdr.length;
1091 if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
1092 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
1093 "Extension (expected OID)");
1097 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1098 hdr.class != ASN1_CLASS_UNIVERSAL ||
1099 (hdr.tag != ASN1_TAG_BOOLEAN &&
1100 hdr.tag != ASN1_TAG_OCTETSTRING)) {
1101 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1102 "Extensions: class %d tag 0x%x; expected BOOLEAN "
1103 "or OCTET STRING", hdr.class, hdr.tag);
1107 if (hdr.tag == ASN1_TAG_BOOLEAN) {
1108 if (hdr.length != 1) {
1109 wpa_printf(MSG_DEBUG, "X509: Unexpected "
1110 "Boolean length (%u)", hdr.length);
1113 critical_ext = hdr.payload[0];
1115 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1116 (hdr.class != ASN1_CLASS_UNIVERSAL &&
1117 hdr.class != ASN1_CLASS_PRIVATE) ||
1118 hdr.tag != ASN1_TAG_OCTETSTRING) {
1119 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
1120 "in Extensions: class %d tag 0x%x; "
1121 "expected OCTET STRING",
1122 hdr.class, hdr.tag);
1127 asn1_oid_to_str(&oid, buf, sizeof(buf));
1128 wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
1130 wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
1132 res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
1135 if (res == 1 && critical_ext) {
1136 wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
1145 static int x509_parse_extensions(struct x509_certificate *cert,
1146 const u8 *pos, size_t len)
1149 struct asn1_hdr hdr;
1151 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
1153 if (asn1_get_next(pos, len, &hdr) < 0 ||
1154 hdr.class != ASN1_CLASS_UNIVERSAL ||
1155 hdr.tag != ASN1_TAG_SEQUENCE) {
1156 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
1157 "for Extensions: class %d tag 0x%x; "
1158 "expected SEQUENCE", hdr.class, hdr.tag);
1163 end = pos + hdr.length;
1166 if (x509_parse_extension(cert, pos, end - pos, &pos)
1175 static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
1176 struct x509_certificate *cert,
1179 struct asn1_hdr hdr;
1180 const u8 *pos, *end;
1183 unsigned long value;
1185 /* tbsCertificate TBSCertificate ::= SEQUENCE */
1186 if (asn1_get_next(buf, len, &hdr) < 0 ||
1187 hdr.class != ASN1_CLASS_UNIVERSAL ||
1188 hdr.tag != ASN1_TAG_SEQUENCE) {
1189 wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
1190 "with a valid SEQUENCE - found class %d tag 0x%x",
1191 hdr.class, hdr.tag);
1195 end = *next = pos + hdr.length;
1198 * version [0] EXPLICIT Version DEFAULT v1
1199 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1201 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1205 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
1206 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1209 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1210 hdr.tag != ASN1_TAG_INTEGER) {
1211 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1212 "version field - found class %d tag 0x%x",
1213 hdr.class, hdr.tag);
1216 if (hdr.length != 1) {
1217 wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
1218 "length %u (expected 1)", hdr.length);
1230 cert->version = value;
1231 if (cert->version != X509_CERT_V1 &&
1232 cert->version != X509_CERT_V2 &&
1233 cert->version != X509_CERT_V3) {
1234 wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
1239 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1242 cert->version = X509_CERT_V1;
1243 wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
1245 /* serialNumber CertificateSerialNumber ::= INTEGER */
1246 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1247 hdr.tag != ASN1_TAG_INTEGER) {
1248 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1249 "serialNumber; class=%d tag=0x%x",
1250 hdr.class, hdr.tag);
1257 cert->serial_number <<= 8;
1258 cert->serial_number |= *pos++;
1261 wpa_printf(MSG_MSGDUMP, "X509: serialNumber %lu", cert->serial_number);
1263 /* signature AlgorithmIdentifier */
1264 if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
1269 if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
1271 x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
1272 wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
1274 /* validity Validity */
1275 if (x509_parse_validity(pos, end - pos, cert, &pos))
1279 if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
1281 x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
1282 wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
1284 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1285 if (x509_parse_public_key(pos, end - pos, cert, &pos))
1291 if (cert->version == X509_CERT_V1)
1294 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1295 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1296 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1297 " tag to parse optional tbsCertificate "
1298 "field(s); parsed class %d tag 0x%x",
1299 hdr.class, hdr.tag);
1304 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1305 wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
1306 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1308 if (hdr.payload + hdr.length == end)
1311 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1312 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1313 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1314 " tag to parse optional tbsCertificate "
1315 "field(s); parsed class %d tag 0x%x",
1316 hdr.class, hdr.tag);
1322 /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
1323 wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
1324 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1326 if (hdr.payload + hdr.length == end)
1329 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1330 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1331 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1332 " tag to parse optional tbsCertificate "
1333 "field(s); parsed class %d tag 0x%x",
1334 hdr.class, hdr.tag);
1340 wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
1341 "Context-Specific tag %d in optional "
1342 "tbsCertificate fields", hdr.tag);
1346 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1348 if (cert->version != X509_CERT_V3) {
1349 wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
1350 "Extensions data which are only allowed for "
1351 "version 3", cert->version + 1);
1355 if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
1358 pos = hdr.payload + hdr.length;
1360 wpa_hexdump(MSG_DEBUG,
1361 "X509: Ignored extra tbsCertificate data",
1369 static int x509_rsadsi_oid(struct asn1_oid *oid)
1371 return oid->len >= 4 &&
1372 oid->oid[0] == 1 /* iso */ &&
1373 oid->oid[1] == 2 /* member-body */ &&
1374 oid->oid[2] == 840 /* us */ &&
1375 oid->oid[3] == 113549 /* rsadsi */;
1379 static int x509_pkcs_oid(struct asn1_oid *oid)
1381 return oid->len >= 5 &&
1382 x509_rsadsi_oid(oid) &&
1383 oid->oid[4] == 1 /* pkcs */;
1387 static int x509_digest_oid(struct asn1_oid *oid)
1389 return oid->len >= 5 &&
1390 x509_rsadsi_oid(oid) &&
1391 oid->oid[4] == 2 /* digestAlgorithm */;
1395 static int x509_sha1_oid(struct asn1_oid *oid)
1397 return oid->len == 6 &&
1398 oid->oid[0] == 1 /* iso */ &&
1399 oid->oid[1] == 3 /* identified-organization */ &&
1400 oid->oid[2] == 14 /* oiw */ &&
1401 oid->oid[3] == 3 /* secsig */ &&
1402 oid->oid[4] == 2 /* algorithms */ &&
1403 oid->oid[5] == 26 /* id-sha1 */;
1407 static int x509_sha256_oid(struct asn1_oid *oid)
1409 return oid->len == 9 &&
1410 oid->oid[0] == 2 /* joint-iso-itu-t */ &&
1411 oid->oid[1] == 16 /* country */ &&
1412 oid->oid[2] == 840 /* us */ &&
1413 oid->oid[3] == 1 /* organization */ &&
1414 oid->oid[4] == 101 /* gov */ &&
1415 oid->oid[5] == 3 /* csor */ &&
1416 oid->oid[6] == 4 /* nistAlgorithm */ &&
1417 oid->oid[7] == 2 /* hashAlgs */ &&
1418 oid->oid[8] == 1 /* sha256 */;
1423 * x509_certificate_parse - Parse a X.509 certificate in DER format
1424 * @buf: Pointer to the X.509 certificate in DER format
1425 * @len: Buffer length
1426 * Returns: Pointer to the parsed certificate or %NULL on failure
1428 * Caller is responsible for freeing the returned certificate by calling
1429 * x509_certificate_free().
1431 struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
1433 struct asn1_hdr hdr;
1434 const u8 *pos, *end, *hash_start;
1435 struct x509_certificate *cert;
1437 cert = os_zalloc(sizeof(*cert) + len);
1440 os_memcpy(cert + 1, buf, len);
1441 cert->cert_start = (u8 *) (cert + 1);
1442 cert->cert_len = len;
1447 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1449 /* Certificate ::= SEQUENCE */
1450 if (asn1_get_next(pos, len, &hdr) < 0 ||
1451 hdr.class != ASN1_CLASS_UNIVERSAL ||
1452 hdr.tag != ASN1_TAG_SEQUENCE) {
1453 wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
1454 "a valid SEQUENCE - found class %d tag 0x%x",
1455 hdr.class, hdr.tag);
1456 x509_certificate_free(cert);
1461 if (pos + hdr.length > end) {
1462 x509_certificate_free(cert);
1466 if (pos + hdr.length < end) {
1467 wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
1468 "encoded certificate",
1469 pos + hdr.length, end - pos + hdr.length);
1470 end = pos + hdr.length;
1474 cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
1475 if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
1476 x509_certificate_free(cert);
1479 cert->tbs_cert_len = pos - hash_start;
1481 /* signatureAlgorithm AlgorithmIdentifier */
1482 if (x509_parse_algorithm_identifier(pos, end - pos,
1483 &cert->signature_alg, &pos)) {
1484 x509_certificate_free(cert);
1488 /* signatureValue BIT STRING */
1489 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1490 hdr.class != ASN1_CLASS_UNIVERSAL ||
1491 hdr.tag != ASN1_TAG_BITSTRING) {
1492 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
1493 "(signatureValue) - found class %d tag 0x%x",
1494 hdr.class, hdr.tag);
1495 x509_certificate_free(cert);
1498 if (hdr.length < 1) {
1499 x509_certificate_free(cert);
1504 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
1506 /* PKCS #1 v1.5 10.2.1:
1507 * It is an error if the length in bits of the signature S is
1508 * not a multiple of eight.
1510 x509_certificate_free(cert);
1513 os_free(cert->sign_value);
1514 cert->sign_value = os_malloc(hdr.length - 1);
1515 if (cert->sign_value == NULL) {
1516 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
1518 x509_certificate_free(cert);
1521 os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
1522 cert->sign_value_len = hdr.length - 1;
1523 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1524 cert->sign_value, cert->sign_value_len);
1531 * x509_certificate_check_signature - Verify certificate signature
1532 * @issuer: Issuer certificate
1533 * @cert: Certificate to be verified
1534 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1537 int x509_certificate_check_signature(struct x509_certificate *issuer,
1538 struct x509_certificate *cert)
1540 struct crypto_public_key *pk;
1542 const u8 *pos, *end, *next, *da_end;
1544 struct asn1_hdr hdr;
1545 struct asn1_oid oid;
1549 if (!x509_pkcs_oid(&cert->signature.oid) ||
1550 cert->signature.oid.len != 7 ||
1551 cert->signature.oid.oid[5] != 1 /* pkcs-1 */) {
1552 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1557 pk = crypto_public_key_import(issuer->public_key,
1558 issuer->public_key_len);
1562 data_len = cert->sign_value_len;
1563 data = os_malloc(data_len);
1565 crypto_public_key_free(pk);
1569 if (crypto_public_key_decrypt_pkcs1(pk, cert->sign_value,
1570 cert->sign_value_len, data,
1572 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1573 crypto_public_key_free(pk);
1577 crypto_public_key_free(pk);
1579 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1582 * PKCS #1 v1.5, 10.1.2:
1584 * DigestInfo ::= SEQUENCE {
1585 * digestAlgorithm DigestAlgorithmIdentifier,
1589 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1591 * Digest ::= OCTET STRING
1594 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1595 hdr.class != ASN1_CLASS_UNIVERSAL ||
1596 hdr.tag != ASN1_TAG_SEQUENCE) {
1597 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1598 "(DigestInfo) - found class %d tag 0x%x",
1599 hdr.class, hdr.tag);
1605 end = pos + hdr.length;
1609 * AlgorithmIdentifier ::= SEQUENCE {
1610 * algorithm OBJECT IDENTIFIER,
1611 * parameters ANY DEFINED BY algorithm OPTIONAL
1615 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1616 hdr.class != ASN1_CLASS_UNIVERSAL ||
1617 hdr.tag != ASN1_TAG_SEQUENCE) {
1618 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1619 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1620 hdr.class, hdr.tag);
1624 da_end = hdr.payload + hdr.length;
1626 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1627 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1632 if (x509_sha1_oid(&oid)) {
1633 if (cert->signature.oid.oid[6] !=
1634 5 /* sha-1WithRSAEncryption */) {
1635 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1636 "does not match with certificate "
1637 "signatureAlgorithm (%lu)",
1638 cert->signature.oid.oid[6]);
1642 goto skip_digest_oid;
1645 if (x509_sha256_oid(&oid)) {
1646 if (cert->signature.oid.oid[6] !=
1647 11 /* sha2561WithRSAEncryption */) {
1648 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1649 "does not match with certificate "
1650 "signatureAlgorithm (%lu)",
1651 cert->signature.oid.oid[6]);
1655 goto skip_digest_oid;
1658 if (!x509_digest_oid(&oid)) {
1659 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1663 switch (oid.oid[5]) {
1665 if (cert->signature.oid.oid[6] != 4 /* md5WithRSAEncryption */)
1667 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1668 "not match with certificate "
1669 "signatureAlgorithm (%lu)",
1670 cert->signature.oid.oid[6]);
1678 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1679 "(%lu)", oid.oid[5]);
1685 /* Digest ::= OCTET STRING */
1687 end = data + data_len;
1689 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1690 hdr.class != ASN1_CLASS_UNIVERSAL ||
1691 hdr.tag != ASN1_TAG_OCTETSTRING) {
1692 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1693 "(Digest) - found class %d tag 0x%x",
1694 hdr.class, hdr.tag);
1698 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1699 hdr.payload, hdr.length);
1701 switch (cert->signature.oid.oid[6]) {
1702 case 4: /* md5WithRSAEncryption */
1703 md5_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1706 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1709 case 5: /* sha-1WithRSAEncryption */
1710 sha1_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1713 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1716 case 11: /* sha256WithRSAEncryption */
1718 sha256_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
1721 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1724 #else /* NEED_SHA256 */
1725 wpa_printf(MSG_INFO, "X509: SHA256 support disabled");
1728 #endif /* NEED_SHA256 */
1729 case 2: /* md2WithRSAEncryption */
1730 case 12: /* sha384WithRSAEncryption */
1731 case 13: /* sha512WithRSAEncryption */
1733 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1734 "algorithm (%lu)", cert->signature.oid.oid[6]);
1739 if (hdr.length != hash_len ||
1740 os_memcmp(hdr.payload, hash, hdr.length) != 0) {
1741 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1742 "with calculated tbsCertificate hash");
1749 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1750 "calculated tbsCertificate hash");
1756 static int x509_valid_issuer(const struct x509_certificate *cert)
1758 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1760 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1765 if (cert->version == X509_CERT_V3 &&
1766 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
1767 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
1768 "include BasicConstraints extension");
1772 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
1773 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
1774 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
1775 "keyCertSign bit in Key Usage");
1784 * x509_certificate_chain_validate - Validate X.509 certificate chain
1785 * @trusted: List of trusted certificates
1786 * @chain: Certificate chain to be validated (first chain must be issued by
1787 * signed by the second certificate in the chain and so on)
1788 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
1789 * Returns: 0 if chain is valid, -1 if not
1791 int x509_certificate_chain_validate(struct x509_certificate *trusted,
1792 struct x509_certificate *chain,
1796 int chain_trusted = 0;
1797 struct x509_certificate *cert, *trust;
1801 *reason = X509_VALIDATE_OK;
1803 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
1806 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
1807 x509_name_string(&cert->subject, buf, sizeof(buf));
1808 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
1813 if ((unsigned long) now.sec <
1814 (unsigned long) cert->not_before ||
1815 (unsigned long) now.sec >
1816 (unsigned long) cert->not_after) {
1817 wpa_printf(MSG_INFO, "X509: Certificate not valid "
1818 "(now=%lu not_before=%lu not_after=%lu)",
1819 now.sec, cert->not_before, cert->not_after);
1820 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
1825 if (x509_name_compare(&cert->issuer,
1826 &cert->next->subject) != 0) {
1827 wpa_printf(MSG_DEBUG, "X509: Certificate "
1828 "chain issuer name mismatch");
1829 x509_name_string(&cert->issuer, buf,
1831 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
1833 x509_name_string(&cert->next->subject, buf,
1835 wpa_printf(MSG_DEBUG, "X509: next cert "
1836 "subject: %s", buf);
1837 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
1841 if (x509_valid_issuer(cert->next) < 0) {
1842 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1846 if ((cert->next->extensions_present &
1847 X509_EXT_PATH_LEN_CONSTRAINT) &&
1848 idx > cert->next->path_len_constraint) {
1849 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
1850 " not met (idx=%lu issuer "
1851 "pathLenConstraint=%lu)", idx,
1852 cert->next->path_len_constraint);
1853 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1857 if (x509_certificate_check_signature(cert->next, cert)
1859 wpa_printf(MSG_DEBUG, "X509: Invalid "
1860 "certificate signature within "
1862 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1867 for (trust = trusted; trust; trust = trust->next) {
1868 if (x509_name_compare(&cert->issuer, &trust->subject)
1874 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
1875 "list of trusted certificates");
1876 if (x509_valid_issuer(trust) < 0) {
1877 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1881 if (x509_certificate_check_signature(trust, cert) < 0)
1883 wpa_printf(MSG_DEBUG, "X509: Invalid "
1884 "certificate signature");
1885 *reason = X509_VALIDATE_BAD_CERTIFICATE;
1889 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
1890 "found to complete the chain");
1895 if (!chain_trusted) {
1896 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
1897 "from the list of trusted certificates");
1899 *reason = X509_VALIDATE_UNKNOWN_CA;
1902 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
1903 "disabled - ignore unknown CA issue");
1906 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
1913 * x509_certificate_get_subject - Get a certificate based on Subject name
1914 * @chain: Certificate chain to search through
1915 * @name: Subject name to search for
1916 * Returns: Pointer to the certificate with the given Subject name or
1919 struct x509_certificate *
1920 x509_certificate_get_subject(struct x509_certificate *chain,
1921 struct x509_name *name)
1923 struct x509_certificate *cert;
1925 for (cert = chain; cert; cert = cert->next) {
1926 if (x509_name_compare(&cert->subject, name) == 0)
1934 * x509_certificate_self_signed - Is the certificate self-signed?
1935 * @cert: Certificate
1936 * Returns: 1 if certificate is self-signed, 0 if not
1938 int x509_certificate_self_signed(struct x509_certificate *cert)
1940 return x509_name_compare(&cert->issuer, &cert->subject) == 0;
1943 #endif /* CONFIG_INTERNAL_X509 */