2 * X.509v3 certificate parsing and processing (RFC 3280 profile)
3 * Copyright (c) 2006-2015, 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 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);
58 os_free(cert->subject_dn);
64 * x509_certificate_free - Free an X.509 certificate chain
65 * @cert: Pointer to the first certificate in the chain
67 void x509_certificate_chain_free(struct x509_certificate *cert)
69 struct x509_certificate *next;
74 x509_certificate_free(cert);
80 static int x509_whitespace(char c)
82 return c == ' ' || c == '\t';
86 static void x509_str_strip_whitespace(char *a)
89 int remove_whitespace = 1;
94 if (remove_whitespace && x509_whitespace(*ipos))
97 remove_whitespace = x509_whitespace(*ipos);
103 if (opos > a && x509_whitespace(*opos))
108 static int x509_str_compare(const char *a, const char *b)
123 if (aa == NULL || bb == NULL) {
126 return os_strcasecmp(a, b);
129 x509_str_strip_whitespace(aa);
130 x509_str_strip_whitespace(bb);
132 ret = os_strcasecmp(aa, bb);
142 * x509_name_compare - Compare X.509 certificate names
143 * @a: Certificate name
144 * @b: Certificate name
145 * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
148 int x509_name_compare(struct x509_name *a, struct x509_name *b)
159 if (a->num_attr < b->num_attr)
161 if (a->num_attr > b->num_attr)
164 for (i = 0; i < a->num_attr; i++) {
165 if (a->attr[i].type < b->attr[i].type)
167 if (a->attr[i].type > b->attr[i].type)
169 res = x509_str_compare(a->attr[i].value, b->attr[i].value);
173 res = x509_str_compare(a->email, b->email);
181 int x509_parse_algorithm_identifier(const u8 *buf, size_t len,
182 struct x509_algorithm_identifier *id,
189 * AlgorithmIdentifier ::= SEQUENCE {
190 * algorithm OBJECT IDENTIFIER,
191 * parameters ANY DEFINED BY algorithm OPTIONAL
195 if (asn1_get_next(buf, len, &hdr) < 0 ||
196 hdr.class != ASN1_CLASS_UNIVERSAL ||
197 hdr.tag != ASN1_TAG_SEQUENCE) {
198 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
199 "(AlgorithmIdentifier) - found class %d tag 0x%x",
203 if (hdr.length > buf + len - hdr.payload)
206 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 (hdr.length > end - pos)
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_memdup(pos + 1, hdr.length - 1);
278 if (cert->public_key == NULL) {
279 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
283 cert->public_key_len = hdr.length - 1;
284 wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
285 cert->public_key, cert->public_key_len);
291 int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
295 const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
300 * Name ::= CHOICE { RDNSequence }
301 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
302 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
303 * AttributeTypeAndValue ::= SEQUENCE {
304 * type AttributeType,
305 * value AttributeValue
307 * AttributeType ::= OBJECT IDENTIFIER
308 * AttributeValue ::= ANY DEFINED BY AttributeType
311 if (asn1_get_next(buf, len, &hdr) < 0 ||
312 hdr.class != ASN1_CLASS_UNIVERSAL ||
313 hdr.tag != ASN1_TAG_SEQUENCE) {
314 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
315 "(Name / RDNSequencer) - found class %d tag 0x%x",
321 if (hdr.length > buf + len - pos)
324 end = *next = pos + hdr.length;
327 enum x509_name_attr_type type;
329 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
330 hdr.class != ASN1_CLASS_UNIVERSAL ||
331 hdr.tag != ASN1_TAG_SET) {
332 wpa_printf(MSG_DEBUG, "X509: Expected SET "
333 "(RelativeDistinguishedName) - found class "
334 "%d tag 0x%x", hdr.class, hdr.tag);
335 x509_free_name(name);
339 set_pos = hdr.payload;
340 pos = set_end = hdr.payload + hdr.length;
342 if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
343 hdr.class != ASN1_CLASS_UNIVERSAL ||
344 hdr.tag != ASN1_TAG_SEQUENCE) {
345 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
346 "(AttributeTypeAndValue) - found class %d "
347 "tag 0x%x", hdr.class, hdr.tag);
348 x509_free_name(name);
352 seq_pos = hdr.payload;
353 seq_end = hdr.payload + hdr.length;
355 if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
356 x509_free_name(name);
360 if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
361 hdr.class != ASN1_CLASS_UNIVERSAL) {
362 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
364 x509_free_name(name);
369 * MUST: country, organization, organizational-unit,
370 * distinguished name qualifier, state or province name,
371 * common name, serial number.
372 * SHOULD: locality, title, surname, given name, initials,
373 * pseudonym, generation qualifier.
374 * MUST: domainComponent (RFC 2247).
376 type = X509_NAME_ATTR_NOT_USED;
378 oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
379 /* id-at ::= 2.5.4 */
380 switch (oid.oid[3]) {
383 type = X509_NAME_ATTR_CN;
387 type = X509_NAME_ATTR_C;
391 type = X509_NAME_ATTR_L;
394 /* stateOrProvinceName */
395 type = X509_NAME_ATTR_ST;
398 /* organizationName */
399 type = X509_NAME_ATTR_O;
402 /* organizationalUnitName */
403 type = X509_NAME_ATTR_OU;
406 } else if (oid.len == 7 &&
407 oid.oid[0] == 1 && oid.oid[1] == 2 &&
408 oid.oid[2] == 840 && oid.oid[3] == 113549 &&
409 oid.oid[4] == 1 && oid.oid[5] == 9 &&
411 /* 1.2.840.113549.1.9.1 - e-mailAddress */
412 os_free(name->email);
413 name->email = os_malloc(hdr.length + 1);
414 if (name->email == NULL) {
415 x509_free_name(name);
418 os_memcpy(name->email, hdr.payload, hdr.length);
419 name->email[hdr.length] = '\0';
421 } else if (oid.len == 7 &&
422 oid.oid[0] == 0 && oid.oid[1] == 9 &&
423 oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
424 oid.oid[4] == 100 && oid.oid[5] == 1 &&
426 /* 0.9.2342.19200300.100.1.25 - domainComponent */
427 type = X509_NAME_ATTR_DC;
430 if (type == X509_NAME_ATTR_NOT_USED) {
431 wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
433 oid.len * sizeof(oid.oid[0]));
434 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
435 hdr.payload, hdr.length);
439 if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
440 wpa_printf(MSG_INFO, "X509: Too many Name attributes");
441 x509_free_name(name);
445 val = dup_binstr(hdr.payload, hdr.length);
447 x509_free_name(name);
450 if (os_strlen(val) != hdr.length) {
451 wpa_printf(MSG_INFO, "X509: Reject certificate with "
452 "embedded NUL byte in a string (%s[NUL])",
455 x509_free_name(name);
459 name->attr[name->num_attr].type = type;
460 name->attr[name->num_attr].value = val;
468 static char * x509_name_attr_str(enum x509_name_attr_type type)
471 case X509_NAME_ATTR_NOT_USED:
473 case X509_NAME_ATTR_DC:
475 case X509_NAME_ATTR_CN:
477 case X509_NAME_ATTR_C:
479 case X509_NAME_ATTR_L:
481 case X509_NAME_ATTR_ST:
483 case X509_NAME_ATTR_O:
485 case X509_NAME_ATTR_OU:
493 * x509_name_string - Convert an X.509 certificate name into a string
494 * @name: Name to convert
495 * @buf: Buffer for the string
496 * @len: Maximum buffer length
498 void x509_name_string(struct x509_name *name, char *buf, size_t len)
510 for (i = 0; i < name->num_attr; i++) {
511 ret = os_snprintf(pos, end - pos, "%s=%s, ",
512 x509_name_attr_str(name->attr[i].type),
513 name->attr[i].value);
514 if (os_snprintf_error(end - pos, ret))
519 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
527 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
529 if (os_snprintf_error(end - pos, ret))
541 int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag, os_time_t *val)
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 (plen > (size_t) (buf + len - pos))
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_any_ext_key_usage_oid(struct asn1_oid *oid)
726 return oid->len == 6 &&
727 x509_id_ce_oid(oid) &&
728 oid->oid[3] == 37 /* extKeyUsage */ &&
729 oid->oid[4] == 0 /* anyExtendedKeyUsage */;
733 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
734 const u8 *pos, size_t len)
739 * KeyUsage ::= BIT STRING {
740 * digitalSignature (0),
741 * nonRepudiation (1),
742 * keyEncipherment (2),
743 * dataEncipherment (3),
751 if (asn1_get_next(pos, len, &hdr) < 0 ||
752 hdr.class != ASN1_CLASS_UNIVERSAL ||
753 hdr.tag != ASN1_TAG_BITSTRING ||
755 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
756 "KeyUsage; found %d tag 0x%x len %d",
757 hdr.class, hdr.tag, hdr.length);
761 cert->extensions_present |= X509_EXT_KEY_USAGE;
762 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
764 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
770 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
771 const u8 *pos, size_t len)
778 * BasicConstraints ::= SEQUENCE {
779 * cA BOOLEAN DEFAULT FALSE,
780 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
783 if (asn1_get_next(pos, len, &hdr) < 0 ||
784 hdr.class != ASN1_CLASS_UNIVERSAL ||
785 hdr.tag != ASN1_TAG_SEQUENCE) {
786 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
787 "BasicConstraints; found %d tag 0x%x",
792 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
797 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
798 hdr.class != ASN1_CLASS_UNIVERSAL) {
799 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
804 if (hdr.tag == ASN1_TAG_BOOLEAN) {
805 if (hdr.length != 1) {
806 wpa_printf(MSG_DEBUG, "X509: Unexpected "
807 "Boolean length (%u) in BasicConstraints",
811 cert->ca = hdr.payload[0];
813 if (hdr.length == pos + len - hdr.payload) {
814 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
819 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
821 hdr.class != ASN1_CLASS_UNIVERSAL) {
822 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
828 if (hdr.tag != ASN1_TAG_INTEGER) {
829 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
830 "BasicConstraints; found class %d tag 0x%x",
844 cert->path_len_constraint = value;
845 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
847 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
848 "pathLenConstraint=%lu",
849 cert->ca, cert->path_len_constraint);
855 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
856 const u8 *pos, size_t len)
858 /* rfc822Name IA5String */
859 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
860 os_free(name->alt_email);
861 name->alt_email = os_zalloc(len + 1);
862 if (name->alt_email == NULL)
864 os_memcpy(name->alt_email, pos, len);
865 if (os_strlen(name->alt_email) != len) {
866 wpa_printf(MSG_INFO, "X509: Reject certificate with "
867 "embedded NUL byte in rfc822Name (%s[NUL])",
869 os_free(name->alt_email);
870 name->alt_email = NULL;
877 static int x509_parse_alt_name_dns(struct x509_name *name,
878 const u8 *pos, size_t len)
880 /* dNSName IA5String */
881 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
883 name->dns = os_zalloc(len + 1);
884 if (name->dns == NULL)
886 os_memcpy(name->dns, pos, len);
887 if (os_strlen(name->dns) != len) {
888 wpa_printf(MSG_INFO, "X509: Reject certificate with "
889 "embedded NUL byte in dNSName (%s[NUL])",
899 static int x509_parse_alt_name_uri(struct x509_name *name,
900 const u8 *pos, size_t len)
902 /* uniformResourceIdentifier IA5String */
903 wpa_hexdump_ascii(MSG_MSGDUMP,
904 "X509: altName - uniformResourceIdentifier",
907 name->uri = os_zalloc(len + 1);
908 if (name->uri == NULL)
910 os_memcpy(name->uri, pos, len);
911 if (os_strlen(name->uri) != len) {
912 wpa_printf(MSG_INFO, "X509: Reject certificate with "
913 "embedded NUL byte in uniformResourceIdentifier "
914 "(%s[NUL])", name->uri);
923 static int x509_parse_alt_name_ip(struct x509_name *name,
924 const u8 *pos, size_t len)
926 /* iPAddress OCTET STRING */
927 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
929 name->ip = os_memdup(pos, len);
930 if (name->ip == NULL)
937 static int x509_parse_alt_name_rid(struct x509_name *name,
938 const u8 *pos, size_t len)
942 /* registeredID OBJECT IDENTIFIER */
943 if (asn1_parse_oid(pos, len, &name->rid) < 0)
946 asn1_oid_to_str(&name->rid, buf, sizeof(buf));
947 wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
953 static int x509_parse_ext_alt_name(struct x509_name *name,
954 const u8 *pos, size_t len)
960 * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
962 * GeneralName ::= CHOICE {
963 * otherName [0] OtherName,
964 * rfc822Name [1] IA5String,
965 * dNSName [2] IA5String,
966 * x400Address [3] ORAddress,
967 * directoryName [4] Name,
968 * ediPartyName [5] EDIPartyName,
969 * uniformResourceIdentifier [6] IA5String,
970 * iPAddress [7] OCTET STRING,
971 * registeredID [8] OBJECT IDENTIFIER }
973 * OtherName ::= SEQUENCE {
974 * type-id OBJECT IDENTIFIER,
975 * value [0] EXPLICIT ANY DEFINED BY type-id }
977 * EDIPartyName ::= SEQUENCE {
978 * nameAssigner [0] DirectoryString OPTIONAL,
979 * partyName [1] DirectoryString }
982 for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
985 if (asn1_get_next(p, end - p, &hdr) < 0) {
986 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
987 "SubjectAltName item");
991 if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
996 res = x509_parse_alt_name_rfc8222(name, hdr.payload,
1000 res = x509_parse_alt_name_dns(name, hdr.payload,
1004 res = x509_parse_alt_name_uri(name, hdr.payload,
1008 res = x509_parse_alt_name_ip(name, hdr.payload,
1012 res = x509_parse_alt_name_rid(name, hdr.payload,
1015 case 0: /* TODO: otherName */
1016 case 3: /* TODO: x500Address */
1017 case 4: /* TODO: directoryName */
1018 case 5: /* TODO: ediPartyName */
1031 static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
1032 const u8 *pos, size_t len)
1034 struct asn1_hdr hdr;
1036 /* SubjectAltName ::= GeneralNames */
1038 if (asn1_get_next(pos, len, &hdr) < 0 ||
1039 hdr.class != ASN1_CLASS_UNIVERSAL ||
1040 hdr.tag != ASN1_TAG_SEQUENCE) {
1041 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1042 "SubjectAltName; found %d tag 0x%x",
1043 hdr.class, hdr.tag);
1047 wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
1048 cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
1050 if (hdr.length == 0)
1053 return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
1058 static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
1059 const u8 *pos, size_t len)
1061 struct asn1_hdr hdr;
1063 /* IssuerAltName ::= GeneralNames */
1065 if (asn1_get_next(pos, len, &hdr) < 0 ||
1066 hdr.class != ASN1_CLASS_UNIVERSAL ||
1067 hdr.tag != ASN1_TAG_SEQUENCE) {
1068 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
1069 "IssuerAltName; found %d tag 0x%x",
1070 hdr.class, hdr.tag);
1074 wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
1075 cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
1077 if (hdr.length == 0)
1080 return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
1085 static int x509_id_pkix_oid(struct asn1_oid *oid)
1087 return oid->len >= 7 &&
1088 oid->oid[0] == 1 /* iso */ &&
1089 oid->oid[1] == 3 /* identified-organization */ &&
1090 oid->oid[2] == 6 /* dod */ &&
1091 oid->oid[3] == 1 /* internet */ &&
1092 oid->oid[4] == 5 /* security */ &&
1093 oid->oid[5] == 5 /* mechanisms */ &&
1094 oid->oid[6] == 7 /* id-pkix */;
1098 static int x509_id_kp_oid(struct asn1_oid *oid)
1101 return oid->len >= 8 &&
1102 x509_id_pkix_oid(oid) &&
1103 oid->oid[7] == 3 /* id-kp */;
1107 static int x509_id_kp_server_auth_oid(struct asn1_oid *oid)
1110 return oid->len == 9 &&
1111 x509_id_kp_oid(oid) &&
1112 oid->oid[8] == 1 /* id-kp-serverAuth */;
1116 static int x509_id_kp_client_auth_oid(struct asn1_oid *oid)
1119 return oid->len == 9 &&
1120 x509_id_kp_oid(oid) &&
1121 oid->oid[8] == 2 /* id-kp-clientAuth */;
1125 static int x509_id_kp_ocsp_oid(struct asn1_oid *oid)
1128 return oid->len == 9 &&
1129 x509_id_kp_oid(oid) &&
1130 oid->oid[8] == 9 /* id-kp-OCSPSigning */;
1134 static int x509_parse_ext_ext_key_usage(struct x509_certificate *cert,
1135 const u8 *pos, size_t len)
1137 struct asn1_hdr hdr;
1139 struct asn1_oid oid;
1142 * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
1144 * KeyPurposeId ::= OBJECT IDENTIFIER
1147 if (asn1_get_next(pos, len, &hdr) < 0 ||
1148 hdr.class != ASN1_CLASS_UNIVERSAL ||
1149 hdr.tag != ASN1_TAG_SEQUENCE) {
1150 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1151 "(ExtKeyUsageSyntax) - found class %d tag 0x%x",
1152 hdr.class, hdr.tag);
1155 if (hdr.length > pos + len - hdr.payload)
1158 end = pos + hdr.length;
1160 wpa_hexdump(MSG_MSGDUMP, "X509: ExtKeyUsageSyntax", pos, end - pos);
1165 if (asn1_get_oid(pos, end - pos, &oid, &pos))
1167 if (x509_any_ext_key_usage_oid(&oid)) {
1168 os_strlcpy(buf, "anyExtendedKeyUsage", sizeof(buf));
1169 cert->ext_key_usage |= X509_EXT_KEY_USAGE_ANY;
1170 } else if (x509_id_kp_server_auth_oid(&oid)) {
1171 os_strlcpy(buf, "id-kp-serverAuth", sizeof(buf));
1172 cert->ext_key_usage |= X509_EXT_KEY_USAGE_SERVER_AUTH;
1173 } else if (x509_id_kp_client_auth_oid(&oid)) {
1174 os_strlcpy(buf, "id-kp-clientAuth", sizeof(buf));
1175 cert->ext_key_usage |= X509_EXT_KEY_USAGE_CLIENT_AUTH;
1176 } else if (x509_id_kp_ocsp_oid(&oid)) {
1177 os_strlcpy(buf, "id-kp-OCSPSigning", sizeof(buf));
1178 cert->ext_key_usage |= X509_EXT_KEY_USAGE_OCSP;
1180 asn1_oid_to_str(&oid, buf, sizeof(buf));
1182 wpa_printf(MSG_DEBUG, "ExtKeyUsage KeyPurposeId: %s", buf);
1185 cert->extensions_present |= X509_EXT_EXT_KEY_USAGE;
1191 static int x509_parse_extension_data(struct x509_certificate *cert,
1192 struct asn1_oid *oid,
1193 const u8 *pos, size_t len)
1195 if (!x509_id_ce_oid(oid))
1198 /* TODO: add other extensions required by RFC 3280, Ch 4.2:
1199 * certificate policies (section 4.2.1.5)
1200 * name constraints (section 4.2.1.11)
1201 * policy constraints (section 4.2.1.12)
1202 * inhibit any-policy (section 4.2.1.15)
1204 switch (oid->oid[3]) {
1205 case 15: /* id-ce-keyUsage */
1206 return x509_parse_ext_key_usage(cert, pos, len);
1207 case 17: /* id-ce-subjectAltName */
1208 return x509_parse_ext_subject_alt_name(cert, pos, len);
1209 case 18: /* id-ce-issuerAltName */
1210 return x509_parse_ext_issuer_alt_name(cert, pos, len);
1211 case 19: /* id-ce-basicConstraints */
1212 return x509_parse_ext_basic_constraints(cert, pos, len);
1213 case 37: /* id-ce-extKeyUsage */
1214 return x509_parse_ext_ext_key_usage(cert, pos, len);
1221 static int x509_parse_extension(struct x509_certificate *cert,
1222 const u8 *pos, size_t len, const u8 **next)
1225 struct asn1_hdr hdr;
1226 struct asn1_oid oid;
1227 int critical_ext = 0, res;
1231 * Extension ::= SEQUENCE {
1232 * extnID OBJECT IDENTIFIER,
1233 * critical BOOLEAN DEFAULT FALSE,
1234 * extnValue OCTET STRING
1238 if (asn1_get_next(pos, len, &hdr) < 0 ||
1239 hdr.class != ASN1_CLASS_UNIVERSAL ||
1240 hdr.tag != ASN1_TAG_SEQUENCE) {
1241 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1242 "Extensions: class %d tag 0x%x; expected SEQUENCE",
1243 hdr.class, hdr.tag);
1247 *next = end = pos + hdr.length;
1249 if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
1250 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
1251 "Extension (expected OID)");
1255 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1256 hdr.class != ASN1_CLASS_UNIVERSAL ||
1257 (hdr.tag != ASN1_TAG_BOOLEAN &&
1258 hdr.tag != ASN1_TAG_OCTETSTRING)) {
1259 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
1260 "Extensions: class %d tag 0x%x; expected BOOLEAN "
1261 "or OCTET STRING", hdr.class, hdr.tag);
1265 if (hdr.tag == ASN1_TAG_BOOLEAN) {
1266 if (hdr.length != 1) {
1267 wpa_printf(MSG_DEBUG, "X509: Unexpected "
1268 "Boolean length (%u)", hdr.length);
1271 critical_ext = hdr.payload[0];
1273 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1274 (hdr.class != ASN1_CLASS_UNIVERSAL &&
1275 hdr.class != ASN1_CLASS_PRIVATE) ||
1276 hdr.tag != ASN1_TAG_OCTETSTRING) {
1277 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
1278 "in Extensions: class %d tag 0x%x; "
1279 "expected OCTET STRING",
1280 hdr.class, hdr.tag);
1285 asn1_oid_to_str(&oid, buf, sizeof(buf));
1286 wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
1288 wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
1290 res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
1293 if (res == 1 && critical_ext) {
1294 wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
1303 static int x509_parse_extensions(struct x509_certificate *cert,
1304 const u8 *pos, size_t len)
1307 struct asn1_hdr hdr;
1309 /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
1311 if (asn1_get_next(pos, len, &hdr) < 0 ||
1312 hdr.class != ASN1_CLASS_UNIVERSAL ||
1313 hdr.tag != ASN1_TAG_SEQUENCE) {
1314 wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
1315 "for Extensions: class %d tag 0x%x; "
1316 "expected SEQUENCE", hdr.class, hdr.tag);
1321 end = pos + hdr.length;
1324 if (x509_parse_extension(cert, pos, end - pos, &pos)
1333 static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
1334 struct x509_certificate *cert,
1337 struct asn1_hdr hdr;
1338 const u8 *pos, *end;
1341 unsigned long value;
1342 const u8 *subject_dn;
1344 /* tbsCertificate TBSCertificate ::= SEQUENCE */
1345 if (asn1_get_next(buf, len, &hdr) < 0 ||
1346 hdr.class != ASN1_CLASS_UNIVERSAL ||
1347 hdr.tag != ASN1_TAG_SEQUENCE) {
1348 wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
1349 "with a valid SEQUENCE - found class %d tag 0x%x",
1350 hdr.class, hdr.tag);
1354 end = *next = pos + hdr.length;
1357 * version [0] EXPLICIT Version DEFAULT v1
1358 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
1360 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1364 if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
1365 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1368 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1369 hdr.tag != ASN1_TAG_INTEGER) {
1370 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1371 "version field - found class %d tag 0x%x",
1372 hdr.class, hdr.tag);
1375 if (hdr.length != 1) {
1376 wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
1377 "length %u (expected 1)", hdr.length);
1389 cert->version = value;
1390 if (cert->version != X509_CERT_V1 &&
1391 cert->version != X509_CERT_V2 &&
1392 cert->version != X509_CERT_V3) {
1393 wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
1398 if (asn1_get_next(pos, end - pos, &hdr) < 0)
1401 cert->version = X509_CERT_V1;
1402 wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
1404 /* serialNumber CertificateSerialNumber ::= INTEGER */
1405 if (hdr.class != ASN1_CLASS_UNIVERSAL ||
1406 hdr.tag != ASN1_TAG_INTEGER ||
1407 hdr.length < 1 || hdr.length > X509_MAX_SERIAL_NUM_LEN) {
1408 wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
1409 "serialNumber; class=%d tag=0x%x length=%u",
1410 hdr.class, hdr.tag, hdr.length);
1414 pos = hdr.payload + hdr.length;
1415 while (hdr.length > 0 && hdr.payload[0] == 0) {
1419 os_memcpy(cert->serial_number, hdr.payload, hdr.length);
1420 cert->serial_number_len = hdr.length;
1421 wpa_hexdump(MSG_MSGDUMP, "X509: serialNumber", cert->serial_number,
1422 cert->serial_number_len);
1424 /* signature AlgorithmIdentifier */
1425 if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
1430 if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
1432 x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
1433 wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
1435 /* validity Validity */
1436 if (x509_parse_validity(pos, end - pos, cert, &pos))
1441 if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
1443 cert->subject_dn = os_malloc(pos - subject_dn);
1444 if (!cert->subject_dn)
1446 cert->subject_dn_len = pos - subject_dn;
1447 os_memcpy(cert->subject_dn, subject_dn, cert->subject_dn_len);
1448 x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
1449 wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
1451 /* subjectPublicKeyInfo SubjectPublicKeyInfo */
1452 if (x509_parse_public_key(pos, end - pos, cert, &pos))
1458 if (cert->version == X509_CERT_V1)
1461 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1462 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1463 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1464 " tag to parse optional tbsCertificate "
1465 "field(s); parsed class %d tag 0x%x",
1466 hdr.class, hdr.tag);
1471 /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
1472 wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
1473 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1475 pos = hdr.payload + hdr.length;
1479 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1480 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1481 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1482 " tag to parse optional tbsCertificate "
1483 "field(s); parsed class %d tag 0x%x",
1484 hdr.class, hdr.tag);
1490 /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
1491 wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
1492 /* TODO: parse UniqueIdentifier ::= BIT STRING */
1494 pos = hdr.payload + hdr.length;
1498 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1499 hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
1500 wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
1501 " tag to parse optional tbsCertificate "
1502 "field(s); parsed class %d tag 0x%x",
1503 hdr.class, hdr.tag);
1509 wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
1510 "Context-Specific tag %d in optional "
1511 "tbsCertificate fields", hdr.tag);
1515 /* extensions [3] EXPLICIT Extensions OPTIONAL */
1517 if (cert->version != X509_CERT_V3) {
1518 wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
1519 "Extensions data which are only allowed for "
1520 "version 3", cert->version + 1);
1524 if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
1527 pos = hdr.payload + hdr.length;
1529 wpa_hexdump(MSG_DEBUG,
1530 "X509: Ignored extra tbsCertificate data",
1538 static int x509_rsadsi_oid(struct asn1_oid *oid)
1540 return oid->len >= 4 &&
1541 oid->oid[0] == 1 /* iso */ &&
1542 oid->oid[1] == 2 /* member-body */ &&
1543 oid->oid[2] == 840 /* us */ &&
1544 oid->oid[3] == 113549 /* rsadsi */;
1548 static int x509_pkcs_oid(struct asn1_oid *oid)
1550 return oid->len >= 5 &&
1551 x509_rsadsi_oid(oid) &&
1552 oid->oid[4] == 1 /* pkcs */;
1556 static int x509_digest_oid(struct asn1_oid *oid)
1558 return oid->len >= 5 &&
1559 x509_rsadsi_oid(oid) &&
1560 oid->oid[4] == 2 /* digestAlgorithm */;
1564 int x509_sha1_oid(struct asn1_oid *oid)
1566 return oid->len == 6 &&
1567 oid->oid[0] == 1 /* iso */ &&
1568 oid->oid[1] == 3 /* identified-organization */ &&
1569 oid->oid[2] == 14 /* oiw */ &&
1570 oid->oid[3] == 3 /* secsig */ &&
1571 oid->oid[4] == 2 /* algorithms */ &&
1572 oid->oid[5] == 26 /* id-sha1 */;
1576 static int x509_sha2_oid(struct asn1_oid *oid)
1578 return oid->len == 9 &&
1579 oid->oid[0] == 2 /* joint-iso-itu-t */ &&
1580 oid->oid[1] == 16 /* country */ &&
1581 oid->oid[2] == 840 /* us */ &&
1582 oid->oid[3] == 1 /* organization */ &&
1583 oid->oid[4] == 101 /* gov */ &&
1584 oid->oid[5] == 3 /* csor */ &&
1585 oid->oid[6] == 4 /* nistAlgorithm */ &&
1586 oid->oid[7] == 2 /* hashAlgs */;
1590 int x509_sha256_oid(struct asn1_oid *oid)
1592 return x509_sha2_oid(oid) &&
1593 oid->oid[8] == 1 /* sha256 */;
1597 int x509_sha384_oid(struct asn1_oid *oid)
1599 return x509_sha2_oid(oid) &&
1600 oid->oid[8] == 2 /* sha384 */;
1604 int x509_sha512_oid(struct asn1_oid *oid)
1606 return x509_sha2_oid(oid) &&
1607 oid->oid[8] == 3 /* sha512 */;
1612 * x509_certificate_parse - Parse a X.509 certificate in DER format
1613 * @buf: Pointer to the X.509 certificate in DER format
1614 * @len: Buffer length
1615 * Returns: Pointer to the parsed certificate or %NULL on failure
1617 * Caller is responsible for freeing the returned certificate by calling
1618 * x509_certificate_free().
1620 struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
1622 struct asn1_hdr hdr;
1623 const u8 *pos, *end, *hash_start;
1624 struct x509_certificate *cert;
1626 cert = os_zalloc(sizeof(*cert) + len);
1629 os_memcpy(cert + 1, buf, len);
1630 cert->cert_start = (u8 *) (cert + 1);
1631 cert->cert_len = len;
1636 /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
1638 /* Certificate ::= SEQUENCE */
1639 if (asn1_get_next(pos, len, &hdr) < 0 ||
1640 hdr.class != ASN1_CLASS_UNIVERSAL ||
1641 hdr.tag != ASN1_TAG_SEQUENCE) {
1642 wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
1643 "a valid SEQUENCE - found class %d tag 0x%x",
1644 hdr.class, hdr.tag);
1645 x509_certificate_free(cert);
1650 if (hdr.length > end - pos) {
1651 x509_certificate_free(cert);
1655 if (hdr.length < end - pos) {
1656 wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
1657 "encoded certificate",
1658 pos + hdr.length, end - (pos + hdr.length));
1659 end = pos + hdr.length;
1663 cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
1664 if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
1665 x509_certificate_free(cert);
1668 cert->tbs_cert_len = pos - hash_start;
1670 /* signatureAlgorithm AlgorithmIdentifier */
1671 if (x509_parse_algorithm_identifier(pos, end - pos,
1672 &cert->signature_alg, &pos)) {
1673 x509_certificate_free(cert);
1677 /* signatureValue BIT STRING */
1678 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1679 hdr.class != ASN1_CLASS_UNIVERSAL ||
1680 hdr.tag != ASN1_TAG_BITSTRING) {
1681 wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
1682 "(signatureValue) - found class %d tag 0x%x",
1683 hdr.class, hdr.tag);
1684 x509_certificate_free(cert);
1687 if (hdr.length < 1) {
1688 x509_certificate_free(cert);
1693 wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
1695 /* PKCS #1 v1.5 10.2.1:
1696 * It is an error if the length in bits of the signature S is
1697 * not a multiple of eight.
1699 x509_certificate_free(cert);
1702 os_free(cert->sign_value);
1703 cert->sign_value = os_memdup(pos + 1, hdr.length - 1);
1704 if (cert->sign_value == NULL) {
1705 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
1707 x509_certificate_free(cert);
1710 cert->sign_value_len = hdr.length - 1;
1711 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1712 cert->sign_value, cert->sign_value_len);
1719 * x509_certificate_check_signature - Verify certificate signature
1720 * @issuer: Issuer certificate
1721 * @cert: Certificate to be verified
1722 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1725 int x509_certificate_check_signature(struct x509_certificate *issuer,
1726 struct x509_certificate *cert)
1728 return x509_check_signature(issuer, &cert->signature,
1729 cert->sign_value, cert->sign_value_len,
1730 cert->tbs_cert_start, cert->tbs_cert_len);
1734 int x509_check_signature(struct x509_certificate *issuer,
1735 struct x509_algorithm_identifier *signature,
1736 const u8 *sign_value, size_t sign_value_len,
1737 const u8 *signed_data, size_t signed_data_len)
1739 struct crypto_public_key *pk;
1741 const u8 *pos, *end, *next, *da_end;
1743 struct asn1_hdr hdr;
1744 struct asn1_oid oid;
1747 const u8 *addr[1] = { signed_data };
1748 size_t len[1] = { signed_data_len };
1750 if (!x509_pkcs_oid(&signature->oid) ||
1751 signature->oid.len != 7 ||
1752 signature->oid.oid[5] != 1 /* pkcs-1 */) {
1753 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1758 pk = crypto_public_key_import(issuer->public_key,
1759 issuer->public_key_len);
1763 data_len = sign_value_len;
1764 data = os_malloc(data_len);
1766 crypto_public_key_free(pk);
1770 if (crypto_public_key_decrypt_pkcs1(pk, sign_value,
1771 sign_value_len, data,
1773 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1774 crypto_public_key_free(pk);
1778 crypto_public_key_free(pk);
1780 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1783 * PKCS #1 v1.5, 10.1.2:
1785 * DigestInfo ::= SEQUENCE {
1786 * digestAlgorithm DigestAlgorithmIdentifier,
1790 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1792 * Digest ::= OCTET STRING
1795 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1796 hdr.class != ASN1_CLASS_UNIVERSAL ||
1797 hdr.tag != ASN1_TAG_SEQUENCE) {
1798 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1799 "(DigestInfo) - found class %d tag 0x%x",
1800 hdr.class, hdr.tag);
1806 end = pos + hdr.length;
1810 * AlgorithmIdentifier ::= SEQUENCE {
1811 * algorithm OBJECT IDENTIFIER,
1812 * parameters ANY DEFINED BY algorithm OPTIONAL
1816 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1817 hdr.class != ASN1_CLASS_UNIVERSAL ||
1818 hdr.tag != ASN1_TAG_SEQUENCE) {
1819 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1820 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1821 hdr.class, hdr.tag);
1825 da_end = hdr.payload + hdr.length;
1827 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1828 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1833 if (x509_sha1_oid(&oid)) {
1834 if (signature->oid.oid[6] != 5 /* sha-1WithRSAEncryption */) {
1835 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1836 "does not match with certificate "
1837 "signatureAlgorithm (%lu)",
1838 signature->oid.oid[6]);
1842 goto skip_digest_oid;
1845 if (x509_sha256_oid(&oid)) {
1846 if (signature->oid.oid[6] !=
1847 11 /* sha2561WithRSAEncryption */) {
1848 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1849 "does not match with certificate "
1850 "signatureAlgorithm (%lu)",
1851 signature->oid.oid[6]);
1855 goto skip_digest_oid;
1858 if (x509_sha384_oid(&oid)) {
1859 if (signature->oid.oid[6] != 12 /* sha384WithRSAEncryption */) {
1860 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA384 "
1861 "does not match with certificate "
1862 "signatureAlgorithm (%lu)",
1863 signature->oid.oid[6]);
1867 goto skip_digest_oid;
1870 if (x509_sha512_oid(&oid)) {
1871 if (signature->oid.oid[6] != 13 /* sha512WithRSAEncryption */) {
1872 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA512 "
1873 "does not match with certificate "
1874 "signatureAlgorithm (%lu)",
1875 signature->oid.oid[6]);
1879 goto skip_digest_oid;
1882 if (!x509_digest_oid(&oid)) {
1883 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1887 switch (oid.oid[5]) {
1889 if (signature->oid.oid[6] != 4 /* md5WithRSAEncryption */) {
1890 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1891 "not match with certificate "
1892 "signatureAlgorithm (%lu)",
1893 signature->oid.oid[6]);
1901 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1902 "(%lu)", oid.oid[5]);
1908 /* Digest ::= OCTET STRING */
1910 end = data + data_len;
1912 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1913 hdr.class != ASN1_CLASS_UNIVERSAL ||
1914 hdr.tag != ASN1_TAG_OCTETSTRING) {
1915 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1916 "(Digest) - found class %d tag 0x%x",
1917 hdr.class, hdr.tag);
1921 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1922 hdr.payload, hdr.length);
1924 switch (signature->oid.oid[6]) {
1925 case 4: /* md5WithRSAEncryption */
1926 md5_vector(1, addr, len, hash);
1928 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1931 case 5: /* sha-1WithRSAEncryption */
1932 sha1_vector(1, addr, len, hash);
1934 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1937 case 11: /* sha256WithRSAEncryption */
1938 sha256_vector(1, addr, len, hash);
1940 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1943 case 12: /* sha384WithRSAEncryption */
1944 sha384_vector(1, addr, len, hash);
1946 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA384)",
1949 case 13: /* sha512WithRSAEncryption */
1950 sha512_vector(1, addr, len, hash);
1952 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA512)",
1955 case 2: /* md2WithRSAEncryption */
1957 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1958 "algorithm (%lu)", signature->oid.oid[6]);
1963 if (hdr.length != hash_len ||
1964 os_memcmp_const(hdr.payload, hash, hdr.length) != 0) {
1965 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1966 "with calculated tbsCertificate hash");
1971 if (hdr.payload + hdr.length < data + data_len) {
1972 wpa_hexdump(MSG_INFO,
1973 "X509: Extra data after certificate signature hash",
1974 hdr.payload + hdr.length,
1975 data + data_len - hdr.payload - hdr.length);
1982 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1983 "calculated tbsCertificate hash");
1989 static int x509_valid_issuer(const struct x509_certificate *cert)
1991 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1993 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1998 if (cert->version == X509_CERT_V3 &&
1999 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
2000 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
2001 "include BasicConstraints extension");
2005 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
2006 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
2007 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
2008 "keyCertSign bit in Key Usage");
2017 * x509_certificate_chain_validate - Validate X.509 certificate chain
2018 * @trusted: List of trusted certificates
2019 * @chain: Certificate chain to be validated (first chain must be issued by
2020 * signed by the second certificate in the chain and so on)
2021 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
2022 * Returns: 0 if chain is valid, -1 if not
2024 int x509_certificate_chain_validate(struct x509_certificate *trusted,
2025 struct x509_certificate *chain,
2026 int *reason, int disable_time_checks)
2029 int chain_trusted = 0;
2030 struct x509_certificate *cert, *trust;
2034 *reason = X509_VALIDATE_OK;
2036 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
2039 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
2040 cert->issuer_trusted = 0;
2041 x509_name_string(&cert->subject, buf, sizeof(buf));
2042 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
2047 if (!disable_time_checks &&
2048 ((unsigned long) now.sec <
2049 (unsigned long) cert->not_before ||
2050 (unsigned long) now.sec >
2051 (unsigned long) cert->not_after)) {
2052 wpa_printf(MSG_INFO, "X509: Certificate not valid "
2053 "(now=%lu not_before=%lu not_after=%lu)",
2054 now.sec, cert->not_before, cert->not_after);
2055 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
2060 if (x509_name_compare(&cert->issuer,
2061 &cert->next->subject) != 0) {
2062 wpa_printf(MSG_DEBUG, "X509: Certificate "
2063 "chain issuer name mismatch");
2064 x509_name_string(&cert->issuer, buf,
2066 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
2068 x509_name_string(&cert->next->subject, buf,
2070 wpa_printf(MSG_DEBUG, "X509: next cert "
2071 "subject: %s", buf);
2072 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
2076 if (x509_valid_issuer(cert->next) < 0) {
2077 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2081 if ((cert->next->extensions_present &
2082 X509_EXT_PATH_LEN_CONSTRAINT) &&
2083 idx > cert->next->path_len_constraint) {
2084 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
2085 " not met (idx=%lu issuer "
2086 "pathLenConstraint=%lu)", idx,
2087 cert->next->path_len_constraint);
2088 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2092 if (x509_certificate_check_signature(cert->next, cert)
2094 wpa_printf(MSG_DEBUG, "X509: Invalid "
2095 "certificate signature within "
2097 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2102 for (trust = trusted; trust; trust = trust->next) {
2103 if (x509_name_compare(&cert->issuer, &trust->subject)
2109 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
2110 "list of trusted certificates");
2111 if (x509_valid_issuer(trust) < 0) {
2112 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2116 if (x509_certificate_check_signature(trust, cert) < 0)
2118 wpa_printf(MSG_DEBUG, "X509: Invalid "
2119 "certificate signature");
2120 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2124 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
2125 "found to complete the chain");
2126 cert->issuer_trusted = 1;
2131 if (!chain_trusted) {
2132 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
2133 "from the list of trusted certificates");
2135 *reason = X509_VALIDATE_UNKNOWN_CA;
2138 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
2139 "disabled - ignore unknown CA issue");
2142 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
2149 * x509_certificate_get_subject - Get a certificate based on Subject name
2150 * @chain: Certificate chain to search through
2151 * @name: Subject name to search for
2152 * Returns: Pointer to the certificate with the given Subject name or
2155 struct x509_certificate *
2156 x509_certificate_get_subject(struct x509_certificate *chain,
2157 struct x509_name *name)
2159 struct x509_certificate *cert;
2161 for (cert = chain; cert; cert = cert->next) {
2162 if (x509_name_compare(&cert->subject, name) == 0)
2170 * x509_certificate_self_signed - Is the certificate self-signed?
2171 * @cert: Certificate
2172 * Returns: 1 if certificate is self-signed, 0 if not
2174 int x509_certificate_self_signed(struct x509_certificate *cert)
2176 return x509_name_compare(&cert->issuer, &cert->subject) == 0;