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_malloc(hdr.length - 1);
278 if (cert->public_key == NULL) {
279 wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
283 os_memcpy(cert->public_key, pos + 1, hdr.length - 1);
284 cert->public_key_len = hdr.length - 1;
285 wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
286 cert->public_key, cert->public_key_len);
292 int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
296 const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
301 * Name ::= CHOICE { RDNSequence }
302 * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
303 * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
304 * AttributeTypeAndValue ::= SEQUENCE {
305 * type AttributeType,
306 * value AttributeValue
308 * AttributeType ::= OBJECT IDENTIFIER
309 * AttributeValue ::= ANY DEFINED BY AttributeType
312 if (asn1_get_next(buf, len, &hdr) < 0 ||
313 hdr.class != ASN1_CLASS_UNIVERSAL ||
314 hdr.tag != ASN1_TAG_SEQUENCE) {
315 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
316 "(Name / RDNSequencer) - found class %d tag 0x%x",
322 if (hdr.length > buf + len - pos)
325 end = *next = pos + hdr.length;
328 enum x509_name_attr_type type;
330 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
331 hdr.class != ASN1_CLASS_UNIVERSAL ||
332 hdr.tag != ASN1_TAG_SET) {
333 wpa_printf(MSG_DEBUG, "X509: Expected SET "
334 "(RelativeDistinguishedName) - found class "
335 "%d tag 0x%x", hdr.class, hdr.tag);
336 x509_free_name(name);
340 set_pos = hdr.payload;
341 pos = set_end = hdr.payload + hdr.length;
343 if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
344 hdr.class != ASN1_CLASS_UNIVERSAL ||
345 hdr.tag != ASN1_TAG_SEQUENCE) {
346 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
347 "(AttributeTypeAndValue) - found class %d "
348 "tag 0x%x", hdr.class, hdr.tag);
349 x509_free_name(name);
353 seq_pos = hdr.payload;
354 seq_end = hdr.payload + hdr.length;
356 if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
357 x509_free_name(name);
361 if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
362 hdr.class != ASN1_CLASS_UNIVERSAL) {
363 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
365 x509_free_name(name);
370 * MUST: country, organization, organizational-unit,
371 * distinguished name qualifier, state or province name,
372 * common name, serial number.
373 * SHOULD: locality, title, surname, given name, initials,
374 * pseudonym, generation qualifier.
375 * MUST: domainComponent (RFC 2247).
377 type = X509_NAME_ATTR_NOT_USED;
379 oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
380 /* id-at ::= 2.5.4 */
381 switch (oid.oid[3]) {
384 type = X509_NAME_ATTR_CN;
388 type = X509_NAME_ATTR_C;
392 type = X509_NAME_ATTR_L;
395 /* stateOrProvinceName */
396 type = X509_NAME_ATTR_ST;
399 /* organizationName */
400 type = X509_NAME_ATTR_O;
403 /* organizationalUnitName */
404 type = X509_NAME_ATTR_OU;
407 } else if (oid.len == 7 &&
408 oid.oid[0] == 1 && oid.oid[1] == 2 &&
409 oid.oid[2] == 840 && oid.oid[3] == 113549 &&
410 oid.oid[4] == 1 && oid.oid[5] == 9 &&
412 /* 1.2.840.113549.1.9.1 - e-mailAddress */
413 os_free(name->email);
414 name->email = os_malloc(hdr.length + 1);
415 if (name->email == NULL) {
416 x509_free_name(name);
419 os_memcpy(name->email, hdr.payload, hdr.length);
420 name->email[hdr.length] = '\0';
422 } else if (oid.len == 7 &&
423 oid.oid[0] == 0 && oid.oid[1] == 9 &&
424 oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
425 oid.oid[4] == 100 && oid.oid[5] == 1 &&
427 /* 0.9.2342.19200300.100.1.25 - domainComponent */
428 type = X509_NAME_ATTR_DC;
431 if (type == X509_NAME_ATTR_NOT_USED) {
432 wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
434 oid.len * sizeof(oid.oid[0]));
435 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
436 hdr.payload, hdr.length);
440 if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
441 wpa_printf(MSG_INFO, "X509: Too many Name attributes");
442 x509_free_name(name);
446 val = dup_binstr(hdr.payload, hdr.length);
448 x509_free_name(name);
451 if (os_strlen(val) != hdr.length) {
452 wpa_printf(MSG_INFO, "X509: Reject certificate with "
453 "embedded NUL byte in a string (%s[NUL])",
456 x509_free_name(name);
460 name->attr[name->num_attr].type = type;
461 name->attr[name->num_attr].value = val;
469 static char * x509_name_attr_str(enum x509_name_attr_type type)
472 case X509_NAME_ATTR_NOT_USED:
474 case X509_NAME_ATTR_DC:
476 case X509_NAME_ATTR_CN:
478 case X509_NAME_ATTR_C:
480 case X509_NAME_ATTR_L:
482 case X509_NAME_ATTR_ST:
484 case X509_NAME_ATTR_O:
486 case X509_NAME_ATTR_OU:
494 * x509_name_string - Convert an X.509 certificate name into a string
495 * @name: Name to convert
496 * @buf: Buffer for the string
497 * @len: Maximum buffer length
499 void x509_name_string(struct x509_name *name, char *buf, size_t len)
511 for (i = 0; i < name->num_attr; i++) {
512 ret = os_snprintf(pos, end - pos, "%s=%s, ",
513 x509_name_attr_str(name->attr[i].type),
514 name->attr[i].value);
515 if (os_snprintf_error(end - pos, ret))
520 if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
528 ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
530 if (os_snprintf_error(end - pos, ret))
540 int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag, os_time_t *val)
543 int year, month, day, hour, min, sec;
548 * generalTime GeneralizedTime
551 * UTCTime: YYMMDDHHMMSSZ
552 * GeneralizedTime: YYYYMMDDHHMMSSZ
555 pos = (const char *) buf;
558 case ASN1_TAG_UTCTIME:
559 if (len != 13 || buf[12] != 'Z') {
560 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
561 "UTCTime format", buf, len);
564 if (sscanf(pos, "%02d", &year) != 1) {
565 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
566 "UTCTime year", buf, len);
575 case ASN1_TAG_GENERALIZEDTIME:
576 if (len != 15 || buf[14] != 'Z') {
577 wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
578 "GeneralizedTime format", buf, len);
581 if (sscanf(pos, "%04d", &year) != 1) {
582 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
583 "GeneralizedTime year", buf, len);
589 wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
590 "GeneralizedTime - found tag 0x%x", asn1_tag);
594 if (sscanf(pos, "%02d", &month) != 1) {
595 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
596 "(month)", buf, len);
601 if (sscanf(pos, "%02d", &day) != 1) {
602 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
608 if (sscanf(pos, "%02d", &hour) != 1) {
609 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
615 if (sscanf(pos, "%02d", &min) != 1) {
616 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
622 if (sscanf(pos, "%02d", &sec) != 1) {
623 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
628 if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
629 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
633 * At least some test certificates have been configured
634 * to use dates prior to 1970. Set the date to
635 * beginning of 1970 to handle these case.
637 wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
638 "assume epoch as the time", year);
649 static int x509_parse_validity(const u8 *buf, size_t len,
650 struct x509_certificate *cert, const u8 **next)
657 * Validity ::= SEQUENCE {
663 * CAs conforming to this profile MUST always encode certificate
664 * validity dates through the year 2049 as UTCTime; certificate
665 * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
668 if (asn1_get_next(buf, len, &hdr) < 0 ||
669 hdr.class != ASN1_CLASS_UNIVERSAL ||
670 hdr.tag != ASN1_TAG_SEQUENCE) {
671 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
672 "(Validity) - found class %d tag 0x%x",
679 if (plen > (size_t) (buf + len - pos))
684 if (asn1_get_next(pos, plen, &hdr) < 0 ||
685 hdr.class != ASN1_CLASS_UNIVERSAL ||
686 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
687 &cert->not_before) < 0) {
688 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
689 "Time", hdr.payload, hdr.length);
693 pos = hdr.payload + hdr.length;
696 if (asn1_get_next(pos, plen, &hdr) < 0 ||
697 hdr.class != ASN1_CLASS_UNIVERSAL ||
698 x509_parse_time(hdr.payload, hdr.length, hdr.tag,
699 &cert->not_after) < 0) {
700 wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
701 "Time", hdr.payload, hdr.length);
705 wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
706 (unsigned long) cert->not_before,
707 (unsigned long) cert->not_after);
713 static int x509_id_ce_oid(struct asn1_oid *oid)
715 /* id-ce arc from X.509 for standard X.509v3 extensions */
716 return oid->len >= 4 &&
717 oid->oid[0] == 2 /* joint-iso-ccitt */ &&
718 oid->oid[1] == 5 /* ds */ &&
719 oid->oid[2] == 29 /* id-ce */;
723 static int x509_any_ext_key_usage_oid(struct asn1_oid *oid)
725 return oid->len == 6 &&
726 x509_id_ce_oid(oid) &&
727 oid->oid[3] == 37 /* extKeyUsage */ &&
728 oid->oid[4] == 0 /* anyExtendedKeyUsage */;
732 static int x509_parse_ext_key_usage(struct x509_certificate *cert,
733 const u8 *pos, size_t len)
738 * KeyUsage ::= BIT STRING {
739 * digitalSignature (0),
740 * nonRepudiation (1),
741 * keyEncipherment (2),
742 * dataEncipherment (3),
750 if (asn1_get_next(pos, len, &hdr) < 0 ||
751 hdr.class != ASN1_CLASS_UNIVERSAL ||
752 hdr.tag != ASN1_TAG_BITSTRING ||
754 wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
755 "KeyUsage; found %d tag 0x%x len %d",
756 hdr.class, hdr.tag, hdr.length);
760 cert->extensions_present |= X509_EXT_KEY_USAGE;
761 cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
763 wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
769 static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
770 const u8 *pos, size_t len)
777 * BasicConstraints ::= SEQUENCE {
778 * cA BOOLEAN DEFAULT FALSE,
779 * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
782 if (asn1_get_next(pos, len, &hdr) < 0 ||
783 hdr.class != ASN1_CLASS_UNIVERSAL ||
784 hdr.tag != ASN1_TAG_SEQUENCE) {
785 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
786 "BasicConstraints; found %d tag 0x%x",
791 cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
796 if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
797 hdr.class != ASN1_CLASS_UNIVERSAL) {
798 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
803 if (hdr.tag == ASN1_TAG_BOOLEAN) {
804 if (hdr.length != 1) {
805 wpa_printf(MSG_DEBUG, "X509: Unexpected "
806 "Boolean length (%u) in BasicConstraints",
810 cert->ca = hdr.payload[0];
812 if (hdr.length == pos + len - hdr.payload) {
813 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
818 if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
820 hdr.class != ASN1_CLASS_UNIVERSAL) {
821 wpa_printf(MSG_DEBUG, "X509: Failed to parse "
827 if (hdr.tag != ASN1_TAG_INTEGER) {
828 wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
829 "BasicConstraints; found class %d tag 0x%x",
843 cert->path_len_constraint = value;
844 cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
846 wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
847 "pathLenConstraint=%lu",
848 cert->ca, cert->path_len_constraint);
854 static int x509_parse_alt_name_rfc8222(struct x509_name *name,
855 const u8 *pos, size_t len)
857 /* rfc822Name IA5String */
858 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
859 os_free(name->alt_email);
860 name->alt_email = os_zalloc(len + 1);
861 if (name->alt_email == NULL)
863 os_memcpy(name->alt_email, pos, len);
864 if (os_strlen(name->alt_email) != len) {
865 wpa_printf(MSG_INFO, "X509: Reject certificate with "
866 "embedded NUL byte in rfc822Name (%s[NUL])",
868 os_free(name->alt_email);
869 name->alt_email = NULL;
876 static int x509_parse_alt_name_dns(struct x509_name *name,
877 const u8 *pos, size_t len)
879 /* dNSName IA5String */
880 wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
882 name->dns = os_zalloc(len + 1);
883 if (name->dns == NULL)
885 os_memcpy(name->dns, pos, len);
886 if (os_strlen(name->dns) != len) {
887 wpa_printf(MSG_INFO, "X509: Reject certificate with "
888 "embedded NUL byte in dNSName (%s[NUL])",
898 static int x509_parse_alt_name_uri(struct x509_name *name,
899 const u8 *pos, size_t len)
901 /* uniformResourceIdentifier IA5String */
902 wpa_hexdump_ascii(MSG_MSGDUMP,
903 "X509: altName - uniformResourceIdentifier",
906 name->uri = os_zalloc(len + 1);
907 if (name->uri == NULL)
909 os_memcpy(name->uri, pos, len);
910 if (os_strlen(name->uri) != len) {
911 wpa_printf(MSG_INFO, "X509: Reject certificate with "
912 "embedded NUL byte in uniformResourceIdentifier "
913 "(%s[NUL])", name->uri);
922 static int x509_parse_alt_name_ip(struct x509_name *name,
923 const u8 *pos, size_t len)
925 /* iPAddress OCTET STRING */
926 wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
928 name->ip = os_malloc(len);
929 if (name->ip == NULL)
931 os_memcpy(name->ip, pos, len);
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_malloc(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 os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
1711 cert->sign_value_len = hdr.length - 1;
1712 wpa_hexdump(MSG_MSGDUMP, "X509: signature",
1713 cert->sign_value, cert->sign_value_len);
1720 * x509_certificate_check_signature - Verify certificate signature
1721 * @issuer: Issuer certificate
1722 * @cert: Certificate to be verified
1723 * Returns: 0 if cert has a valid signature that was signed by the issuer,
1726 int x509_certificate_check_signature(struct x509_certificate *issuer,
1727 struct x509_certificate *cert)
1729 return x509_check_signature(issuer, &cert->signature,
1730 cert->sign_value, cert->sign_value_len,
1731 cert->tbs_cert_start, cert->tbs_cert_len);
1735 int x509_check_signature(struct x509_certificate *issuer,
1736 struct x509_algorithm_identifier *signature,
1737 const u8 *sign_value, size_t sign_value_len,
1738 const u8 *signed_data, size_t signed_data_len)
1740 struct crypto_public_key *pk;
1742 const u8 *pos, *end, *next, *da_end;
1744 struct asn1_hdr hdr;
1745 struct asn1_oid oid;
1748 const u8 *addr[1] = { signed_data };
1749 size_t len[1] = { signed_data_len };
1751 if (!x509_pkcs_oid(&signature->oid) ||
1752 signature->oid.len != 7 ||
1753 signature->oid.oid[5] != 1 /* pkcs-1 */) {
1754 wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
1759 pk = crypto_public_key_import(issuer->public_key,
1760 issuer->public_key_len);
1764 data_len = sign_value_len;
1765 data = os_malloc(data_len);
1767 crypto_public_key_free(pk);
1771 if (crypto_public_key_decrypt_pkcs1(pk, sign_value,
1772 sign_value_len, data,
1774 wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
1775 crypto_public_key_free(pk);
1779 crypto_public_key_free(pk);
1781 wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
1784 * PKCS #1 v1.5, 10.1.2:
1786 * DigestInfo ::= SEQUENCE {
1787 * digestAlgorithm DigestAlgorithmIdentifier,
1791 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
1793 * Digest ::= OCTET STRING
1796 if (asn1_get_next(data, data_len, &hdr) < 0 ||
1797 hdr.class != ASN1_CLASS_UNIVERSAL ||
1798 hdr.tag != ASN1_TAG_SEQUENCE) {
1799 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1800 "(DigestInfo) - found class %d tag 0x%x",
1801 hdr.class, hdr.tag);
1807 end = pos + hdr.length;
1811 * AlgorithmIdentifier ::= SEQUENCE {
1812 * algorithm OBJECT IDENTIFIER,
1813 * parameters ANY DEFINED BY algorithm OPTIONAL
1817 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1818 hdr.class != ASN1_CLASS_UNIVERSAL ||
1819 hdr.tag != ASN1_TAG_SEQUENCE) {
1820 wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
1821 "(AlgorithmIdentifier) - found class %d tag 0x%x",
1822 hdr.class, hdr.tag);
1826 da_end = hdr.payload + hdr.length;
1828 if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
1829 wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
1834 if (x509_sha1_oid(&oid)) {
1835 if (signature->oid.oid[6] != 5 /* sha-1WithRSAEncryption */) {
1836 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
1837 "does not match with certificate "
1838 "signatureAlgorithm (%lu)",
1839 signature->oid.oid[6]);
1843 goto skip_digest_oid;
1846 if (x509_sha256_oid(&oid)) {
1847 if (signature->oid.oid[6] !=
1848 11 /* sha2561WithRSAEncryption */) {
1849 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
1850 "does not match with certificate "
1851 "signatureAlgorithm (%lu)",
1852 signature->oid.oid[6]);
1856 goto skip_digest_oid;
1859 if (x509_sha384_oid(&oid)) {
1860 if (signature->oid.oid[6] != 12 /* sha384WithRSAEncryption */) {
1861 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA384 "
1862 "does not match with certificate "
1863 "signatureAlgorithm (%lu)",
1864 signature->oid.oid[6]);
1868 goto skip_digest_oid;
1871 if (x509_sha512_oid(&oid)) {
1872 if (signature->oid.oid[6] != 13 /* sha512WithRSAEncryption */) {
1873 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA512 "
1874 "does not match with certificate "
1875 "signatureAlgorithm (%lu)",
1876 signature->oid.oid[6]);
1880 goto skip_digest_oid;
1883 if (!x509_digest_oid(&oid)) {
1884 wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
1888 switch (oid.oid[5]) {
1890 if (signature->oid.oid[6] != 4 /* md5WithRSAEncryption */) {
1891 wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
1892 "not match with certificate "
1893 "signatureAlgorithm (%lu)",
1894 signature->oid.oid[6]);
1902 wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
1903 "(%lu)", oid.oid[5]);
1909 /* Digest ::= OCTET STRING */
1911 end = data + data_len;
1913 if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
1914 hdr.class != ASN1_CLASS_UNIVERSAL ||
1915 hdr.tag != ASN1_TAG_OCTETSTRING) {
1916 wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
1917 "(Digest) - found class %d tag 0x%x",
1918 hdr.class, hdr.tag);
1922 wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
1923 hdr.payload, hdr.length);
1925 switch (signature->oid.oid[6]) {
1926 case 4: /* md5WithRSAEncryption */
1927 md5_vector(1, addr, len, hash);
1929 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
1932 case 5: /* sha-1WithRSAEncryption */
1933 sha1_vector(1, addr, len, hash);
1935 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
1938 case 11: /* sha256WithRSAEncryption */
1939 sha256_vector(1, addr, len, hash);
1941 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
1944 case 12: /* sha384WithRSAEncryption */
1945 sha384_vector(1, addr, len, hash);
1947 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA384)",
1950 case 13: /* sha512WithRSAEncryption */
1951 sha512_vector(1, addr, len, hash);
1953 wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA512)",
1956 case 2: /* md2WithRSAEncryption */
1958 wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
1959 "algorithm (%lu)", signature->oid.oid[6]);
1964 if (hdr.length != hash_len ||
1965 os_memcmp_const(hdr.payload, hash, hdr.length) != 0) {
1966 wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
1967 "with calculated tbsCertificate hash");
1972 if (hdr.payload + hdr.length < data + data_len) {
1973 wpa_hexdump(MSG_INFO,
1974 "X509: Extra data after certificate signature hash",
1975 hdr.payload + hdr.length,
1976 data + data_len - hdr.payload - hdr.length);
1983 wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
1984 "calculated tbsCertificate hash");
1990 static int x509_valid_issuer(const struct x509_certificate *cert)
1992 if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
1994 wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
1999 if (cert->version == X509_CERT_V3 &&
2000 !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
2001 wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
2002 "include BasicConstraints extension");
2006 if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
2007 !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
2008 wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
2009 "keyCertSign bit in Key Usage");
2018 * x509_certificate_chain_validate - Validate X.509 certificate chain
2019 * @trusted: List of trusted certificates
2020 * @chain: Certificate chain to be validated (first chain must be issued by
2021 * signed by the second certificate in the chain and so on)
2022 * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
2023 * Returns: 0 if chain is valid, -1 if not
2025 int x509_certificate_chain_validate(struct x509_certificate *trusted,
2026 struct x509_certificate *chain,
2027 int *reason, int disable_time_checks)
2030 int chain_trusted = 0;
2031 struct x509_certificate *cert, *trust;
2035 *reason = X509_VALIDATE_OK;
2037 wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
2040 for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
2041 cert->issuer_trusted = 0;
2042 x509_name_string(&cert->subject, buf, sizeof(buf));
2043 wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
2048 if (!disable_time_checks &&
2049 ((unsigned long) now.sec <
2050 (unsigned long) cert->not_before ||
2051 (unsigned long) now.sec >
2052 (unsigned long) cert->not_after)) {
2053 wpa_printf(MSG_INFO, "X509: Certificate not valid "
2054 "(now=%lu not_before=%lu not_after=%lu)",
2055 now.sec, cert->not_before, cert->not_after);
2056 *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
2061 if (x509_name_compare(&cert->issuer,
2062 &cert->next->subject) != 0) {
2063 wpa_printf(MSG_DEBUG, "X509: Certificate "
2064 "chain issuer name mismatch");
2065 x509_name_string(&cert->issuer, buf,
2067 wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
2069 x509_name_string(&cert->next->subject, buf,
2071 wpa_printf(MSG_DEBUG, "X509: next cert "
2072 "subject: %s", buf);
2073 *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
2077 if (x509_valid_issuer(cert->next) < 0) {
2078 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2082 if ((cert->next->extensions_present &
2083 X509_EXT_PATH_LEN_CONSTRAINT) &&
2084 idx > cert->next->path_len_constraint) {
2085 wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
2086 " not met (idx=%lu issuer "
2087 "pathLenConstraint=%lu)", idx,
2088 cert->next->path_len_constraint);
2089 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2093 if (x509_certificate_check_signature(cert->next, cert)
2095 wpa_printf(MSG_DEBUG, "X509: Invalid "
2096 "certificate signature within "
2098 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2103 for (trust = trusted; trust; trust = trust->next) {
2104 if (x509_name_compare(&cert->issuer, &trust->subject)
2110 wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
2111 "list of trusted certificates");
2112 if (x509_valid_issuer(trust) < 0) {
2113 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2117 if (x509_certificate_check_signature(trust, cert) < 0)
2119 wpa_printf(MSG_DEBUG, "X509: Invalid "
2120 "certificate signature");
2121 *reason = X509_VALIDATE_BAD_CERTIFICATE;
2125 wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
2126 "found to complete the chain");
2127 cert->issuer_trusted = 1;
2132 if (!chain_trusted) {
2133 wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
2134 "from the list of trusted certificates");
2136 *reason = X509_VALIDATE_UNKNOWN_CA;
2139 wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
2140 "disabled - ignore unknown CA issue");
2143 wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
2150 * x509_certificate_get_subject - Get a certificate based on Subject name
2151 * @chain: Certificate chain to search through
2152 * @name: Subject name to search for
2153 * Returns: Pointer to the certificate with the given Subject name or
2156 struct x509_certificate *
2157 x509_certificate_get_subject(struct x509_certificate *chain,
2158 struct x509_name *name)
2160 struct x509_certificate *cert;
2162 for (cert = chain; cert; cert = cert->next) {
2163 if (x509_name_compare(&cert->subject, name) == 0)
2171 * x509_certificate_self_signed - Is the certificate self-signed?
2172 * @cert: Certificate
2173 * Returns: 1 if certificate is self-signed, 0 if not
2175 int x509_certificate_self_signed(struct x509_certificate *cert)
2177 return x509_name_compare(&cert->issuer, &cert->subject) == 0;