x509v3.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990
  1. /*
  2. * X.509v3 certificate parsing and processing (RFC 3280 profile)
  3. * Copyright (c) 2006-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "includes.h"
  9. #include "common.h"
  10. #include "crypto/crypto.h"
  11. #include "asn1.h"
  12. #include "x509v3.h"
  13. static void x509_free_name(struct x509_name *name)
  14. {
  15. size_t i;
  16. for (i = 0; i < name->num_attr; i++) {
  17. os_free(name->attr[i].value);
  18. name->attr[i].value = NULL;
  19. name->attr[i].type = X509_NAME_ATTR_NOT_USED;
  20. }
  21. name->num_attr = 0;
  22. os_free(name->email);
  23. name->email = NULL;
  24. os_free(name->alt_email);
  25. os_free(name->dns);
  26. os_free(name->uri);
  27. os_free(name->ip);
  28. name->alt_email = name->dns = name->uri = NULL;
  29. name->ip = NULL;
  30. name->ip_len = 0;
  31. os_memset(&name->rid, 0, sizeof(name->rid));
  32. }
  33. /**
  34. * x509_certificate_free - Free an X.509 certificate
  35. * @cert: Certificate to be freed
  36. */
  37. void x509_certificate_free(struct x509_certificate *cert)
  38. {
  39. if (cert == NULL)
  40. return;
  41. if (cert->next) {
  42. wpa_printf(MSG_DEBUG, "X509: x509_certificate_free: cer=%p "
  43. "was still on a list (next=%p)\n",
  44. cert, cert->next);
  45. }
  46. x509_free_name(&cert->issuer);
  47. x509_free_name(&cert->subject);
  48. os_free(cert->public_key);
  49. os_free(cert->sign_value);
  50. os_free(cert);
  51. }
  52. /**
  53. * x509_certificate_free - Free an X.509 certificate chain
  54. * @cert: Pointer to the first certificate in the chain
  55. */
  56. void x509_certificate_chain_free(struct x509_certificate *cert)
  57. {
  58. struct x509_certificate *next;
  59. while (cert) {
  60. next = cert->next;
  61. cert->next = NULL;
  62. x509_certificate_free(cert);
  63. cert = next;
  64. }
  65. }
  66. static int x509_whitespace(char c)
  67. {
  68. return c == ' ' || c == '\t';
  69. }
  70. static void x509_str_strip_whitespace(char *a)
  71. {
  72. char *ipos, *opos;
  73. int remove_whitespace = 1;
  74. ipos = opos = a;
  75. while (*ipos) {
  76. if (remove_whitespace && x509_whitespace(*ipos))
  77. ipos++;
  78. else {
  79. remove_whitespace = x509_whitespace(*ipos);
  80. *opos++ = *ipos++;
  81. }
  82. }
  83. *opos-- = '\0';
  84. if (opos > a && x509_whitespace(*opos))
  85. *opos = '\0';
  86. }
  87. static int x509_str_compare(const char *a, const char *b)
  88. {
  89. char *aa, *bb;
  90. int ret;
  91. if (!a && b)
  92. return -1;
  93. if (a && !b)
  94. return 1;
  95. if (!a && !b)
  96. return 0;
  97. aa = os_strdup(a);
  98. bb = os_strdup(b);
  99. if (aa == NULL || bb == NULL) {
  100. os_free(aa);
  101. os_free(bb);
  102. return os_strcasecmp(a, b);
  103. }
  104. x509_str_strip_whitespace(aa);
  105. x509_str_strip_whitespace(bb);
  106. ret = os_strcasecmp(aa, bb);
  107. os_free(aa);
  108. os_free(bb);
  109. return ret;
  110. }
  111. /**
  112. * x509_name_compare - Compare X.509 certificate names
  113. * @a: Certificate name
  114. * @b: Certificate name
  115. * Returns: <0, 0, or >0 based on whether a is less than, equal to, or
  116. * greater than b
  117. */
  118. int x509_name_compare(struct x509_name *a, struct x509_name *b)
  119. {
  120. int res;
  121. size_t i;
  122. if (!a && b)
  123. return -1;
  124. if (a && !b)
  125. return 1;
  126. if (!a && !b)
  127. return 0;
  128. if (a->num_attr < b->num_attr)
  129. return -1;
  130. if (a->num_attr > b->num_attr)
  131. return 1;
  132. for (i = 0; i < a->num_attr; i++) {
  133. if (a->attr[i].type < b->attr[i].type)
  134. return -1;
  135. if (a->attr[i].type > b->attr[i].type)
  136. return -1;
  137. res = x509_str_compare(a->attr[i].value, b->attr[i].value);
  138. if (res)
  139. return res;
  140. }
  141. res = x509_str_compare(a->email, b->email);
  142. if (res)
  143. return res;
  144. return 0;
  145. }
  146. static int x509_parse_algorithm_identifier(
  147. const u8 *buf, size_t len,
  148. struct x509_algorithm_identifier *id, const u8 **next)
  149. {
  150. struct asn1_hdr hdr;
  151. const u8 *pos, *end;
  152. /*
  153. * AlgorithmIdentifier ::= SEQUENCE {
  154. * algorithm OBJECT IDENTIFIER,
  155. * parameters ANY DEFINED BY algorithm OPTIONAL
  156. * }
  157. */
  158. if (asn1_get_next(buf, len, &hdr) < 0 ||
  159. hdr.class != ASN1_CLASS_UNIVERSAL ||
  160. hdr.tag != ASN1_TAG_SEQUENCE) {
  161. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  162. "(AlgorithmIdentifier) - found class %d tag 0x%x",
  163. hdr.class, hdr.tag);
  164. return -1;
  165. }
  166. pos = hdr.payload;
  167. end = pos + hdr.length;
  168. if (end > buf + len)
  169. return -1;
  170. *next = end;
  171. if (asn1_get_oid(pos, end - pos, &id->oid, &pos))
  172. return -1;
  173. /* TODO: optional parameters */
  174. return 0;
  175. }
  176. static int x509_parse_public_key(const u8 *buf, size_t len,
  177. struct x509_certificate *cert,
  178. const u8 **next)
  179. {
  180. struct asn1_hdr hdr;
  181. const u8 *pos, *end;
  182. /*
  183. * SubjectPublicKeyInfo ::= SEQUENCE {
  184. * algorithm AlgorithmIdentifier,
  185. * subjectPublicKey BIT STRING
  186. * }
  187. */
  188. pos = buf;
  189. end = buf + len;
  190. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  191. hdr.class != ASN1_CLASS_UNIVERSAL ||
  192. hdr.tag != ASN1_TAG_SEQUENCE) {
  193. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  194. "(SubjectPublicKeyInfo) - found class %d tag 0x%x",
  195. hdr.class, hdr.tag);
  196. return -1;
  197. }
  198. pos = hdr.payload;
  199. if (pos + hdr.length > end)
  200. return -1;
  201. end = pos + hdr.length;
  202. *next = end;
  203. if (x509_parse_algorithm_identifier(pos, end - pos,
  204. &cert->public_key_alg, &pos))
  205. return -1;
  206. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  207. hdr.class != ASN1_CLASS_UNIVERSAL ||
  208. hdr.tag != ASN1_TAG_BITSTRING) {
  209. wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
  210. "(subjectPublicKey) - found class %d tag 0x%x",
  211. hdr.class, hdr.tag);
  212. return -1;
  213. }
  214. if (hdr.length < 1)
  215. return -1;
  216. pos = hdr.payload;
  217. if (*pos) {
  218. wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
  219. *pos);
  220. /*
  221. * TODO: should this be rejected? X.509 certificates are
  222. * unlikely to use such a construction. Now we would end up
  223. * including the extra bits in the buffer which may also be
  224. * ok.
  225. */
  226. }
  227. os_free(cert->public_key);
  228. cert->public_key = os_malloc(hdr.length - 1);
  229. if (cert->public_key == NULL) {
  230. wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
  231. "public key");
  232. return -1;
  233. }
  234. os_memcpy(cert->public_key, pos + 1, hdr.length - 1);
  235. cert->public_key_len = hdr.length - 1;
  236. wpa_hexdump(MSG_MSGDUMP, "X509: subjectPublicKey",
  237. cert->public_key, cert->public_key_len);
  238. return 0;
  239. }
  240. static int x509_parse_name(const u8 *buf, size_t len, struct x509_name *name,
  241. const u8 **next)
  242. {
  243. struct asn1_hdr hdr;
  244. const u8 *pos, *end, *set_pos, *set_end, *seq_pos, *seq_end;
  245. struct asn1_oid oid;
  246. char *val;
  247. /*
  248. * Name ::= CHOICE { RDNSequence }
  249. * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  250. * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
  251. * AttributeTypeAndValue ::= SEQUENCE {
  252. * type AttributeType,
  253. * value AttributeValue
  254. * }
  255. * AttributeType ::= OBJECT IDENTIFIER
  256. * AttributeValue ::= ANY DEFINED BY AttributeType
  257. */
  258. if (asn1_get_next(buf, len, &hdr) < 0 ||
  259. hdr.class != ASN1_CLASS_UNIVERSAL ||
  260. hdr.tag != ASN1_TAG_SEQUENCE) {
  261. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  262. "(Name / RDNSequencer) - found class %d tag 0x%x",
  263. hdr.class, hdr.tag);
  264. return -1;
  265. }
  266. pos = hdr.payload;
  267. if (pos + hdr.length > buf + len)
  268. return -1;
  269. end = *next = pos + hdr.length;
  270. while (pos < end) {
  271. enum x509_name_attr_type type;
  272. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  273. hdr.class != ASN1_CLASS_UNIVERSAL ||
  274. hdr.tag != ASN1_TAG_SET) {
  275. wpa_printf(MSG_DEBUG, "X509: Expected SET "
  276. "(RelativeDistinguishedName) - found class "
  277. "%d tag 0x%x", hdr.class, hdr.tag);
  278. x509_free_name(name);
  279. return -1;
  280. }
  281. set_pos = hdr.payload;
  282. pos = set_end = hdr.payload + hdr.length;
  283. if (asn1_get_next(set_pos, set_end - set_pos, &hdr) < 0 ||
  284. hdr.class != ASN1_CLASS_UNIVERSAL ||
  285. hdr.tag != ASN1_TAG_SEQUENCE) {
  286. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  287. "(AttributeTypeAndValue) - found class %d "
  288. "tag 0x%x", hdr.class, hdr.tag);
  289. x509_free_name(name);
  290. return -1;
  291. }
  292. seq_pos = hdr.payload;
  293. seq_end = hdr.payload + hdr.length;
  294. if (asn1_get_oid(seq_pos, seq_end - seq_pos, &oid, &seq_pos)) {
  295. x509_free_name(name);
  296. return -1;
  297. }
  298. if (asn1_get_next(seq_pos, seq_end - seq_pos, &hdr) < 0 ||
  299. hdr.class != ASN1_CLASS_UNIVERSAL) {
  300. wpa_printf(MSG_DEBUG, "X509: Failed to parse "
  301. "AttributeValue");
  302. x509_free_name(name);
  303. return -1;
  304. }
  305. /* RFC 3280:
  306. * MUST: country, organization, organizational-unit,
  307. * distinguished name qualifier, state or province name,
  308. * common name, serial number.
  309. * SHOULD: locality, title, surname, given name, initials,
  310. * pseudonym, generation qualifier.
  311. * MUST: domainComponent (RFC 2247).
  312. */
  313. type = X509_NAME_ATTR_NOT_USED;
  314. if (oid.len == 4 &&
  315. oid.oid[0] == 2 && oid.oid[1] == 5 && oid.oid[2] == 4) {
  316. /* id-at ::= 2.5.4 */
  317. switch (oid.oid[3]) {
  318. case 3:
  319. /* commonName */
  320. type = X509_NAME_ATTR_CN;
  321. break;
  322. case 6:
  323. /* countryName */
  324. type = X509_NAME_ATTR_C;
  325. break;
  326. case 7:
  327. /* localityName */
  328. type = X509_NAME_ATTR_L;
  329. break;
  330. case 8:
  331. /* stateOrProvinceName */
  332. type = X509_NAME_ATTR_ST;
  333. break;
  334. case 10:
  335. /* organizationName */
  336. type = X509_NAME_ATTR_O;
  337. break;
  338. case 11:
  339. /* organizationalUnitName */
  340. type = X509_NAME_ATTR_OU;
  341. break;
  342. }
  343. } else if (oid.len == 7 &&
  344. oid.oid[0] == 1 && oid.oid[1] == 2 &&
  345. oid.oid[2] == 840 && oid.oid[3] == 113549 &&
  346. oid.oid[4] == 1 && oid.oid[5] == 9 &&
  347. oid.oid[6] == 1) {
  348. /* 1.2.840.113549.1.9.1 - e-mailAddress */
  349. os_free(name->email);
  350. name->email = os_malloc(hdr.length + 1);
  351. if (name->email == NULL) {
  352. x509_free_name(name);
  353. return -1;
  354. }
  355. os_memcpy(name->email, hdr.payload, hdr.length);
  356. name->email[hdr.length] = '\0';
  357. continue;
  358. } else if (oid.len == 7 &&
  359. oid.oid[0] == 0 && oid.oid[1] == 9 &&
  360. oid.oid[2] == 2342 && oid.oid[3] == 19200300 &&
  361. oid.oid[4] == 100 && oid.oid[5] == 1 &&
  362. oid.oid[6] == 25) {
  363. /* 0.9.2342.19200300.100.1.25 - domainComponent */
  364. type = X509_NAME_ATTR_DC;
  365. }
  366. if (type == X509_NAME_ATTR_NOT_USED) {
  367. wpa_hexdump(MSG_DEBUG, "X509: Unrecognized OID",
  368. (u8 *) oid.oid,
  369. oid.len * sizeof(oid.oid[0]));
  370. wpa_hexdump_ascii(MSG_MSGDUMP, "X509: Attribute Data",
  371. hdr.payload, hdr.length);
  372. continue;
  373. }
  374. if (name->num_attr == X509_MAX_NAME_ATTRIBUTES) {
  375. wpa_printf(MSG_INFO, "X509: Too many Name attributes");
  376. x509_free_name(name);
  377. return -1;
  378. }
  379. val = dup_binstr(hdr.payload, hdr.length);
  380. if (val == NULL) {
  381. x509_free_name(name);
  382. return -1;
  383. }
  384. if (os_strlen(val) != hdr.length) {
  385. wpa_printf(MSG_INFO, "X509: Reject certificate with "
  386. "embedded NUL byte in a string (%s[NUL])",
  387. val);
  388. os_free(val);
  389. x509_free_name(name);
  390. return -1;
  391. }
  392. name->attr[name->num_attr].type = type;
  393. name->attr[name->num_attr].value = val;
  394. name->num_attr++;
  395. }
  396. return 0;
  397. }
  398. static char * x509_name_attr_str(enum x509_name_attr_type type)
  399. {
  400. switch (type) {
  401. case X509_NAME_ATTR_NOT_USED:
  402. return "[N/A]";
  403. case X509_NAME_ATTR_DC:
  404. return "DC";
  405. case X509_NAME_ATTR_CN:
  406. return "CN";
  407. case X509_NAME_ATTR_C:
  408. return "C";
  409. case X509_NAME_ATTR_L:
  410. return "L";
  411. case X509_NAME_ATTR_ST:
  412. return "ST";
  413. case X509_NAME_ATTR_O:
  414. return "O";
  415. case X509_NAME_ATTR_OU:
  416. return "OU";
  417. }
  418. return "?";
  419. }
  420. /**
  421. * x509_name_string - Convert an X.509 certificate name into a string
  422. * @name: Name to convert
  423. * @buf: Buffer for the string
  424. * @len: Maximum buffer length
  425. */
  426. void x509_name_string(struct x509_name *name, char *buf, size_t len)
  427. {
  428. char *pos, *end;
  429. int ret;
  430. size_t i;
  431. if (len == 0)
  432. return;
  433. pos = buf;
  434. end = buf + len;
  435. for (i = 0; i < name->num_attr; i++) {
  436. ret = os_snprintf(pos, end - pos, "%s=%s, ",
  437. x509_name_attr_str(name->attr[i].type),
  438. name->attr[i].value);
  439. if (os_snprintf_error(end - pos, ret))
  440. goto done;
  441. pos += ret;
  442. }
  443. if (pos > buf + 1 && pos[-1] == ' ' && pos[-2] == ',') {
  444. pos--;
  445. *pos = '\0';
  446. pos--;
  447. *pos = '\0';
  448. }
  449. if (name->email) {
  450. ret = os_snprintf(pos, end - pos, "/emailAddress=%s",
  451. name->email);
  452. if (os_snprintf_error(end - pos, ret))
  453. goto done;
  454. pos += ret;
  455. }
  456. done:
  457. end[-1] = '\0';
  458. }
  459. static int x509_parse_time(const u8 *buf, size_t len, u8 asn1_tag,
  460. os_time_t *val)
  461. {
  462. const char *pos;
  463. int year, month, day, hour, min, sec;
  464. /*
  465. * Time ::= CHOICE {
  466. * utcTime UTCTime,
  467. * generalTime GeneralizedTime
  468. * }
  469. *
  470. * UTCTime: YYMMDDHHMMSSZ
  471. * GeneralizedTime: YYYYMMDDHHMMSSZ
  472. */
  473. pos = (const char *) buf;
  474. switch (asn1_tag) {
  475. case ASN1_TAG_UTCTIME:
  476. if (len != 13 || buf[12] != 'Z') {
  477. wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
  478. "UTCTime format", buf, len);
  479. return -1;
  480. }
  481. if (sscanf(pos, "%02d", &year) != 1) {
  482. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
  483. "UTCTime year", buf, len);
  484. return -1;
  485. }
  486. if (year < 50)
  487. year += 2000;
  488. else
  489. year += 1900;
  490. pos += 2;
  491. break;
  492. case ASN1_TAG_GENERALIZEDTIME:
  493. if (len != 15 || buf[14] != 'Z') {
  494. wpa_hexdump_ascii(MSG_DEBUG, "X509: Unrecognized "
  495. "GeneralizedTime format", buf, len);
  496. return -1;
  497. }
  498. if (sscanf(pos, "%04d", &year) != 1) {
  499. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse "
  500. "GeneralizedTime year", buf, len);
  501. return -1;
  502. }
  503. pos += 4;
  504. break;
  505. default:
  506. wpa_printf(MSG_DEBUG, "X509: Expected UTCTime or "
  507. "GeneralizedTime - found tag 0x%x", asn1_tag);
  508. return -1;
  509. }
  510. if (sscanf(pos, "%02d", &month) != 1) {
  511. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
  512. "(month)", buf, len);
  513. return -1;
  514. }
  515. pos += 2;
  516. if (sscanf(pos, "%02d", &day) != 1) {
  517. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
  518. "(day)", buf, len);
  519. return -1;
  520. }
  521. pos += 2;
  522. if (sscanf(pos, "%02d", &hour) != 1) {
  523. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
  524. "(hour)", buf, len);
  525. return -1;
  526. }
  527. pos += 2;
  528. if (sscanf(pos, "%02d", &min) != 1) {
  529. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
  530. "(min)", buf, len);
  531. return -1;
  532. }
  533. pos += 2;
  534. if (sscanf(pos, "%02d", &sec) != 1) {
  535. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse Time "
  536. "(sec)", buf, len);
  537. return -1;
  538. }
  539. if (os_mktime(year, month, day, hour, min, sec, val) < 0) {
  540. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to convert Time",
  541. buf, len);
  542. if (year < 1970) {
  543. /*
  544. * At least some test certificates have been configured
  545. * to use dates prior to 1970. Set the date to
  546. * beginning of 1970 to handle these case.
  547. */
  548. wpa_printf(MSG_DEBUG, "X509: Year=%d before epoch - "
  549. "assume epoch as the time", year);
  550. *val = 0;
  551. return 0;
  552. }
  553. return -1;
  554. }
  555. return 0;
  556. }
  557. static int x509_parse_validity(const u8 *buf, size_t len,
  558. struct x509_certificate *cert, const u8 **next)
  559. {
  560. struct asn1_hdr hdr;
  561. const u8 *pos;
  562. size_t plen;
  563. /*
  564. * Validity ::= SEQUENCE {
  565. * notBefore Time,
  566. * notAfter Time
  567. * }
  568. *
  569. * RFC 3280, 4.1.2.5:
  570. * CAs conforming to this profile MUST always encode certificate
  571. * validity dates through the year 2049 as UTCTime; certificate
  572. * validity dates in 2050 or later MUST be encoded as GeneralizedTime.
  573. */
  574. if (asn1_get_next(buf, len, &hdr) < 0 ||
  575. hdr.class != ASN1_CLASS_UNIVERSAL ||
  576. hdr.tag != ASN1_TAG_SEQUENCE) {
  577. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  578. "(Validity) - found class %d tag 0x%x",
  579. hdr.class, hdr.tag);
  580. return -1;
  581. }
  582. pos = hdr.payload;
  583. plen = hdr.length;
  584. if (pos + plen > buf + len)
  585. return -1;
  586. *next = pos + plen;
  587. if (asn1_get_next(pos, plen, &hdr) < 0 ||
  588. hdr.class != ASN1_CLASS_UNIVERSAL ||
  589. x509_parse_time(hdr.payload, hdr.length, hdr.tag,
  590. &cert->not_before) < 0) {
  591. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notBefore "
  592. "Time", hdr.payload, hdr.length);
  593. return -1;
  594. }
  595. pos = hdr.payload + hdr.length;
  596. plen = *next - pos;
  597. if (asn1_get_next(pos, plen, &hdr) < 0 ||
  598. hdr.class != ASN1_CLASS_UNIVERSAL ||
  599. x509_parse_time(hdr.payload, hdr.length, hdr.tag,
  600. &cert->not_after) < 0) {
  601. wpa_hexdump_ascii(MSG_DEBUG, "X509: Failed to parse notAfter "
  602. "Time", hdr.payload, hdr.length);
  603. return -1;
  604. }
  605. wpa_printf(MSG_MSGDUMP, "X509: Validity: notBefore: %lu notAfter: %lu",
  606. (unsigned long) cert->not_before,
  607. (unsigned long) cert->not_after);
  608. return 0;
  609. }
  610. static int x509_id_ce_oid(struct asn1_oid *oid)
  611. {
  612. /* id-ce arc from X.509 for standard X.509v3 extensions */
  613. return oid->len >= 4 &&
  614. oid->oid[0] == 2 /* joint-iso-ccitt */ &&
  615. oid->oid[1] == 5 /* ds */ &&
  616. oid->oid[2] == 29 /* id-ce */;
  617. }
  618. static int x509_parse_ext_key_usage(struct x509_certificate *cert,
  619. const u8 *pos, size_t len)
  620. {
  621. struct asn1_hdr hdr;
  622. /*
  623. * KeyUsage ::= BIT STRING {
  624. * digitalSignature (0),
  625. * nonRepudiation (1),
  626. * keyEncipherment (2),
  627. * dataEncipherment (3),
  628. * keyAgreement (4),
  629. * keyCertSign (5),
  630. * cRLSign (6),
  631. * encipherOnly (7),
  632. * decipherOnly (8) }
  633. */
  634. if (asn1_get_next(pos, len, &hdr) < 0 ||
  635. hdr.class != ASN1_CLASS_UNIVERSAL ||
  636. hdr.tag != ASN1_TAG_BITSTRING ||
  637. hdr.length < 1) {
  638. wpa_printf(MSG_DEBUG, "X509: Expected BIT STRING in "
  639. "KeyUsage; found %d tag 0x%x len %d",
  640. hdr.class, hdr.tag, hdr.length);
  641. return -1;
  642. }
  643. cert->extensions_present |= X509_EXT_KEY_USAGE;
  644. cert->key_usage = asn1_bit_string_to_long(hdr.payload, hdr.length);
  645. wpa_printf(MSG_DEBUG, "X509: KeyUsage 0x%lx", cert->key_usage);
  646. return 0;
  647. }
  648. static int x509_parse_ext_basic_constraints(struct x509_certificate *cert,
  649. const u8 *pos, size_t len)
  650. {
  651. struct asn1_hdr hdr;
  652. unsigned long value;
  653. size_t left;
  654. /*
  655. * BasicConstraints ::= SEQUENCE {
  656. * cA BOOLEAN DEFAULT FALSE,
  657. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  658. */
  659. if (asn1_get_next(pos, len, &hdr) < 0 ||
  660. hdr.class != ASN1_CLASS_UNIVERSAL ||
  661. hdr.tag != ASN1_TAG_SEQUENCE) {
  662. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
  663. "BasicConstraints; found %d tag 0x%x",
  664. hdr.class, hdr.tag);
  665. return -1;
  666. }
  667. cert->extensions_present |= X509_EXT_BASIC_CONSTRAINTS;
  668. if (hdr.length == 0)
  669. return 0;
  670. if (asn1_get_next(hdr.payload, hdr.length, &hdr) < 0 ||
  671. hdr.class != ASN1_CLASS_UNIVERSAL) {
  672. wpa_printf(MSG_DEBUG, "X509: Failed to parse "
  673. "BasicConstraints");
  674. return -1;
  675. }
  676. if (hdr.tag == ASN1_TAG_BOOLEAN) {
  677. if (hdr.length != 1) {
  678. wpa_printf(MSG_DEBUG, "X509: Unexpected "
  679. "Boolean length (%u) in BasicConstraints",
  680. hdr.length);
  681. return -1;
  682. }
  683. cert->ca = hdr.payload[0];
  684. if (hdr.payload + hdr.length == pos + len) {
  685. wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d",
  686. cert->ca);
  687. return 0;
  688. }
  689. if (asn1_get_next(hdr.payload + hdr.length, len - hdr.length,
  690. &hdr) < 0 ||
  691. hdr.class != ASN1_CLASS_UNIVERSAL) {
  692. wpa_printf(MSG_DEBUG, "X509: Failed to parse "
  693. "BasicConstraints");
  694. return -1;
  695. }
  696. }
  697. if (hdr.tag != ASN1_TAG_INTEGER) {
  698. wpa_printf(MSG_DEBUG, "X509: Expected INTEGER in "
  699. "BasicConstraints; found class %d tag 0x%x",
  700. hdr.class, hdr.tag);
  701. return -1;
  702. }
  703. pos = hdr.payload;
  704. left = hdr.length;
  705. value = 0;
  706. while (left) {
  707. value <<= 8;
  708. value |= *pos++;
  709. left--;
  710. }
  711. cert->path_len_constraint = value;
  712. cert->extensions_present |= X509_EXT_PATH_LEN_CONSTRAINT;
  713. wpa_printf(MSG_DEBUG, "X509: BasicConstraints - cA=%d "
  714. "pathLenConstraint=%lu",
  715. cert->ca, cert->path_len_constraint);
  716. return 0;
  717. }
  718. static int x509_parse_alt_name_rfc8222(struct x509_name *name,
  719. const u8 *pos, size_t len)
  720. {
  721. /* rfc822Name IA5String */
  722. wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - rfc822Name", pos, len);
  723. os_free(name->alt_email);
  724. name->alt_email = os_zalloc(len + 1);
  725. if (name->alt_email == NULL)
  726. return -1;
  727. os_memcpy(name->alt_email, pos, len);
  728. if (os_strlen(name->alt_email) != len) {
  729. wpa_printf(MSG_INFO, "X509: Reject certificate with "
  730. "embedded NUL byte in rfc822Name (%s[NUL])",
  731. name->alt_email);
  732. os_free(name->alt_email);
  733. name->alt_email = NULL;
  734. return -1;
  735. }
  736. return 0;
  737. }
  738. static int x509_parse_alt_name_dns(struct x509_name *name,
  739. const u8 *pos, size_t len)
  740. {
  741. /* dNSName IA5String */
  742. wpa_hexdump_ascii(MSG_MSGDUMP, "X509: altName - dNSName", pos, len);
  743. os_free(name->dns);
  744. name->dns = os_zalloc(len + 1);
  745. if (name->dns == NULL)
  746. return -1;
  747. os_memcpy(name->dns, pos, len);
  748. if (os_strlen(name->dns) != len) {
  749. wpa_printf(MSG_INFO, "X509: Reject certificate with "
  750. "embedded NUL byte in dNSName (%s[NUL])",
  751. name->dns);
  752. os_free(name->dns);
  753. name->dns = NULL;
  754. return -1;
  755. }
  756. return 0;
  757. }
  758. static int x509_parse_alt_name_uri(struct x509_name *name,
  759. const u8 *pos, size_t len)
  760. {
  761. /* uniformResourceIdentifier IA5String */
  762. wpa_hexdump_ascii(MSG_MSGDUMP,
  763. "X509: altName - uniformResourceIdentifier",
  764. pos, len);
  765. os_free(name->uri);
  766. name->uri = os_zalloc(len + 1);
  767. if (name->uri == NULL)
  768. return -1;
  769. os_memcpy(name->uri, pos, len);
  770. if (os_strlen(name->uri) != len) {
  771. wpa_printf(MSG_INFO, "X509: Reject certificate with "
  772. "embedded NUL byte in uniformResourceIdentifier "
  773. "(%s[NUL])", name->uri);
  774. os_free(name->uri);
  775. name->uri = NULL;
  776. return -1;
  777. }
  778. return 0;
  779. }
  780. static int x509_parse_alt_name_ip(struct x509_name *name,
  781. const u8 *pos, size_t len)
  782. {
  783. /* iPAddress OCTET STRING */
  784. wpa_hexdump(MSG_MSGDUMP, "X509: altName - iPAddress", pos, len);
  785. os_free(name->ip);
  786. name->ip = os_malloc(len);
  787. if (name->ip == NULL)
  788. return -1;
  789. os_memcpy(name->ip, pos, len);
  790. name->ip_len = len;
  791. return 0;
  792. }
  793. static int x509_parse_alt_name_rid(struct x509_name *name,
  794. const u8 *pos, size_t len)
  795. {
  796. char buf[80];
  797. /* registeredID OBJECT IDENTIFIER */
  798. if (asn1_parse_oid(pos, len, &name->rid) < 0)
  799. return -1;
  800. asn1_oid_to_str(&name->rid, buf, sizeof(buf));
  801. wpa_printf(MSG_MSGDUMP, "X509: altName - registeredID: %s", buf);
  802. return 0;
  803. }
  804. static int x509_parse_ext_alt_name(struct x509_name *name,
  805. const u8 *pos, size_t len)
  806. {
  807. struct asn1_hdr hdr;
  808. const u8 *p, *end;
  809. /*
  810. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  811. *
  812. * GeneralName ::= CHOICE {
  813. * otherName [0] OtherName,
  814. * rfc822Name [1] IA5String,
  815. * dNSName [2] IA5String,
  816. * x400Address [3] ORAddress,
  817. * directoryName [4] Name,
  818. * ediPartyName [5] EDIPartyName,
  819. * uniformResourceIdentifier [6] IA5String,
  820. * iPAddress [7] OCTET STRING,
  821. * registeredID [8] OBJECT IDENTIFIER }
  822. *
  823. * OtherName ::= SEQUENCE {
  824. * type-id OBJECT IDENTIFIER,
  825. * value [0] EXPLICIT ANY DEFINED BY type-id }
  826. *
  827. * EDIPartyName ::= SEQUENCE {
  828. * nameAssigner [0] DirectoryString OPTIONAL,
  829. * partyName [1] DirectoryString }
  830. */
  831. for (p = pos, end = pos + len; p < end; p = hdr.payload + hdr.length) {
  832. int res;
  833. if (asn1_get_next(p, end - p, &hdr) < 0) {
  834. wpa_printf(MSG_DEBUG, "X509: Failed to parse "
  835. "SubjectAltName item");
  836. return -1;
  837. }
  838. if (hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC)
  839. continue;
  840. switch (hdr.tag) {
  841. case 1:
  842. res = x509_parse_alt_name_rfc8222(name, hdr.payload,
  843. hdr.length);
  844. break;
  845. case 2:
  846. res = x509_parse_alt_name_dns(name, hdr.payload,
  847. hdr.length);
  848. break;
  849. case 6:
  850. res = x509_parse_alt_name_uri(name, hdr.payload,
  851. hdr.length);
  852. break;
  853. case 7:
  854. res = x509_parse_alt_name_ip(name, hdr.payload,
  855. hdr.length);
  856. break;
  857. case 8:
  858. res = x509_parse_alt_name_rid(name, hdr.payload,
  859. hdr.length);
  860. break;
  861. case 0: /* TODO: otherName */
  862. case 3: /* TODO: x500Address */
  863. case 4: /* TODO: directoryName */
  864. case 5: /* TODO: ediPartyName */
  865. default:
  866. res = 0;
  867. break;
  868. }
  869. if (res < 0)
  870. return res;
  871. }
  872. return 0;
  873. }
  874. static int x509_parse_ext_subject_alt_name(struct x509_certificate *cert,
  875. const u8 *pos, size_t len)
  876. {
  877. struct asn1_hdr hdr;
  878. /* SubjectAltName ::= GeneralNames */
  879. if (asn1_get_next(pos, len, &hdr) < 0 ||
  880. hdr.class != ASN1_CLASS_UNIVERSAL ||
  881. hdr.tag != ASN1_TAG_SEQUENCE) {
  882. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
  883. "SubjectAltName; found %d tag 0x%x",
  884. hdr.class, hdr.tag);
  885. return -1;
  886. }
  887. wpa_printf(MSG_DEBUG, "X509: SubjectAltName");
  888. cert->extensions_present |= X509_EXT_SUBJECT_ALT_NAME;
  889. if (hdr.length == 0)
  890. return 0;
  891. return x509_parse_ext_alt_name(&cert->subject, hdr.payload,
  892. hdr.length);
  893. }
  894. static int x509_parse_ext_issuer_alt_name(struct x509_certificate *cert,
  895. const u8 *pos, size_t len)
  896. {
  897. struct asn1_hdr hdr;
  898. /* IssuerAltName ::= GeneralNames */
  899. if (asn1_get_next(pos, len, &hdr) < 0 ||
  900. hdr.class != ASN1_CLASS_UNIVERSAL ||
  901. hdr.tag != ASN1_TAG_SEQUENCE) {
  902. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE in "
  903. "IssuerAltName; found %d tag 0x%x",
  904. hdr.class, hdr.tag);
  905. return -1;
  906. }
  907. wpa_printf(MSG_DEBUG, "X509: IssuerAltName");
  908. cert->extensions_present |= X509_EXT_ISSUER_ALT_NAME;
  909. if (hdr.length == 0)
  910. return 0;
  911. return x509_parse_ext_alt_name(&cert->issuer, hdr.payload,
  912. hdr.length);
  913. }
  914. static int x509_parse_extension_data(struct x509_certificate *cert,
  915. struct asn1_oid *oid,
  916. const u8 *pos, size_t len)
  917. {
  918. if (!x509_id_ce_oid(oid))
  919. return 1;
  920. /* TODO: add other extensions required by RFC 3280, Ch 4.2:
  921. * certificate policies (section 4.2.1.5)
  922. * name constraints (section 4.2.1.11)
  923. * policy constraints (section 4.2.1.12)
  924. * extended key usage (section 4.2.1.13)
  925. * inhibit any-policy (section 4.2.1.15)
  926. */
  927. switch (oid->oid[3]) {
  928. case 15: /* id-ce-keyUsage */
  929. return x509_parse_ext_key_usage(cert, pos, len);
  930. case 17: /* id-ce-subjectAltName */
  931. return x509_parse_ext_subject_alt_name(cert, pos, len);
  932. case 18: /* id-ce-issuerAltName */
  933. return x509_parse_ext_issuer_alt_name(cert, pos, len);
  934. case 19: /* id-ce-basicConstraints */
  935. return x509_parse_ext_basic_constraints(cert, pos, len);
  936. default:
  937. return 1;
  938. }
  939. }
  940. static int x509_parse_extension(struct x509_certificate *cert,
  941. const u8 *pos, size_t len, const u8 **next)
  942. {
  943. const u8 *end;
  944. struct asn1_hdr hdr;
  945. struct asn1_oid oid;
  946. int critical_ext = 0, res;
  947. char buf[80];
  948. /*
  949. * Extension ::= SEQUENCE {
  950. * extnID OBJECT IDENTIFIER,
  951. * critical BOOLEAN DEFAULT FALSE,
  952. * extnValue OCTET STRING
  953. * }
  954. */
  955. if (asn1_get_next(pos, len, &hdr) < 0 ||
  956. hdr.class != ASN1_CLASS_UNIVERSAL ||
  957. hdr.tag != ASN1_TAG_SEQUENCE) {
  958. wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
  959. "Extensions: class %d tag 0x%x; expected SEQUENCE",
  960. hdr.class, hdr.tag);
  961. return -1;
  962. }
  963. pos = hdr.payload;
  964. *next = end = pos + hdr.length;
  965. if (asn1_get_oid(pos, end - pos, &oid, &pos) < 0) {
  966. wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data for "
  967. "Extension (expected OID)");
  968. return -1;
  969. }
  970. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  971. hdr.class != ASN1_CLASS_UNIVERSAL ||
  972. (hdr.tag != ASN1_TAG_BOOLEAN &&
  973. hdr.tag != ASN1_TAG_OCTETSTRING)) {
  974. wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header in "
  975. "Extensions: class %d tag 0x%x; expected BOOLEAN "
  976. "or OCTET STRING", hdr.class, hdr.tag);
  977. return -1;
  978. }
  979. if (hdr.tag == ASN1_TAG_BOOLEAN) {
  980. if (hdr.length != 1) {
  981. wpa_printf(MSG_DEBUG, "X509: Unexpected "
  982. "Boolean length (%u)", hdr.length);
  983. return -1;
  984. }
  985. critical_ext = hdr.payload[0];
  986. pos = hdr.payload;
  987. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  988. (hdr.class != ASN1_CLASS_UNIVERSAL &&
  989. hdr.class != ASN1_CLASS_PRIVATE) ||
  990. hdr.tag != ASN1_TAG_OCTETSTRING) {
  991. wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 header "
  992. "in Extensions: class %d tag 0x%x; "
  993. "expected OCTET STRING",
  994. hdr.class, hdr.tag);
  995. return -1;
  996. }
  997. }
  998. asn1_oid_to_str(&oid, buf, sizeof(buf));
  999. wpa_printf(MSG_DEBUG, "X509: Extension: extnID=%s critical=%d",
  1000. buf, critical_ext);
  1001. wpa_hexdump(MSG_MSGDUMP, "X509: extnValue", hdr.payload, hdr.length);
  1002. res = x509_parse_extension_data(cert, &oid, hdr.payload, hdr.length);
  1003. if (res < 0)
  1004. return res;
  1005. if (res == 1 && critical_ext) {
  1006. wpa_printf(MSG_INFO, "X509: Unknown critical extension %s",
  1007. buf);
  1008. return -1;
  1009. }
  1010. return 0;
  1011. }
  1012. static int x509_parse_extensions(struct x509_certificate *cert,
  1013. const u8 *pos, size_t len)
  1014. {
  1015. const u8 *end;
  1016. struct asn1_hdr hdr;
  1017. /* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension */
  1018. if (asn1_get_next(pos, len, &hdr) < 0 ||
  1019. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1020. hdr.tag != ASN1_TAG_SEQUENCE) {
  1021. wpa_printf(MSG_DEBUG, "X509: Unexpected ASN.1 data "
  1022. "for Extensions: class %d tag 0x%x; "
  1023. "expected SEQUENCE", hdr.class, hdr.tag);
  1024. return -1;
  1025. }
  1026. pos = hdr.payload;
  1027. end = pos + hdr.length;
  1028. while (pos < end) {
  1029. if (x509_parse_extension(cert, pos, end - pos, &pos)
  1030. < 0)
  1031. return -1;
  1032. }
  1033. return 0;
  1034. }
  1035. static int x509_parse_tbs_certificate(const u8 *buf, size_t len,
  1036. struct x509_certificate *cert,
  1037. const u8 **next)
  1038. {
  1039. struct asn1_hdr hdr;
  1040. const u8 *pos, *end;
  1041. size_t left;
  1042. char sbuf[128];
  1043. unsigned long value;
  1044. /* tbsCertificate TBSCertificate ::= SEQUENCE */
  1045. if (asn1_get_next(buf, len, &hdr) < 0 ||
  1046. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1047. hdr.tag != ASN1_TAG_SEQUENCE) {
  1048. wpa_printf(MSG_DEBUG, "X509: tbsCertificate did not start "
  1049. "with a valid SEQUENCE - found class %d tag 0x%x",
  1050. hdr.class, hdr.tag);
  1051. return -1;
  1052. }
  1053. pos = hdr.payload;
  1054. end = *next = pos + hdr.length;
  1055. /*
  1056. * version [0] EXPLICIT Version DEFAULT v1
  1057. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  1058. */
  1059. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  1060. return -1;
  1061. pos = hdr.payload;
  1062. if (hdr.class == ASN1_CLASS_CONTEXT_SPECIFIC) {
  1063. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  1064. return -1;
  1065. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  1066. hdr.tag != ASN1_TAG_INTEGER) {
  1067. wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
  1068. "version field - found class %d tag 0x%x",
  1069. hdr.class, hdr.tag);
  1070. return -1;
  1071. }
  1072. if (hdr.length != 1) {
  1073. wpa_printf(MSG_DEBUG, "X509: Unexpected version field "
  1074. "length %u (expected 1)", hdr.length);
  1075. return -1;
  1076. }
  1077. pos = hdr.payload;
  1078. left = hdr.length;
  1079. value = 0;
  1080. while (left) {
  1081. value <<= 8;
  1082. value |= *pos++;
  1083. left--;
  1084. }
  1085. cert->version = value;
  1086. if (cert->version != X509_CERT_V1 &&
  1087. cert->version != X509_CERT_V2 &&
  1088. cert->version != X509_CERT_V3) {
  1089. wpa_printf(MSG_DEBUG, "X509: Unsupported version %d",
  1090. cert->version + 1);
  1091. return -1;
  1092. }
  1093. if (asn1_get_next(pos, end - pos, &hdr) < 0)
  1094. return -1;
  1095. } else
  1096. cert->version = X509_CERT_V1;
  1097. wpa_printf(MSG_MSGDUMP, "X509: Version X.509v%d", cert->version + 1);
  1098. /* serialNumber CertificateSerialNumber ::= INTEGER */
  1099. if (hdr.class != ASN1_CLASS_UNIVERSAL ||
  1100. hdr.tag != ASN1_TAG_INTEGER) {
  1101. wpa_printf(MSG_DEBUG, "X509: No INTEGER tag found for "
  1102. "serialNumber; class=%d tag=0x%x",
  1103. hdr.class, hdr.tag);
  1104. return -1;
  1105. }
  1106. pos = hdr.payload;
  1107. left = hdr.length;
  1108. while (left) {
  1109. cert->serial_number <<= 8;
  1110. cert->serial_number |= *pos++;
  1111. left--;
  1112. }
  1113. wpa_printf(MSG_MSGDUMP, "X509: serialNumber %lu", cert->serial_number);
  1114. /* signature AlgorithmIdentifier */
  1115. if (x509_parse_algorithm_identifier(pos, end - pos, &cert->signature,
  1116. &pos))
  1117. return -1;
  1118. /* issuer Name */
  1119. if (x509_parse_name(pos, end - pos, &cert->issuer, &pos))
  1120. return -1;
  1121. x509_name_string(&cert->issuer, sbuf, sizeof(sbuf));
  1122. wpa_printf(MSG_MSGDUMP, "X509: issuer %s", sbuf);
  1123. /* validity Validity */
  1124. if (x509_parse_validity(pos, end - pos, cert, &pos))
  1125. return -1;
  1126. /* subject Name */
  1127. if (x509_parse_name(pos, end - pos, &cert->subject, &pos))
  1128. return -1;
  1129. x509_name_string(&cert->subject, sbuf, sizeof(sbuf));
  1130. wpa_printf(MSG_MSGDUMP, "X509: subject %s", sbuf);
  1131. /* subjectPublicKeyInfo SubjectPublicKeyInfo */
  1132. if (x509_parse_public_key(pos, end - pos, cert, &pos))
  1133. return -1;
  1134. if (pos == end)
  1135. return 0;
  1136. if (cert->version == X509_CERT_V1)
  1137. return 0;
  1138. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1139. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
  1140. wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
  1141. " tag to parse optional tbsCertificate "
  1142. "field(s); parsed class %d tag 0x%x",
  1143. hdr.class, hdr.tag);
  1144. return -1;
  1145. }
  1146. if (hdr.tag == 1) {
  1147. /* issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL */
  1148. wpa_printf(MSG_DEBUG, "X509: issuerUniqueID");
  1149. /* TODO: parse UniqueIdentifier ::= BIT STRING */
  1150. pos = hdr.payload + hdr.length;
  1151. if (pos == end)
  1152. return 0;
  1153. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1154. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
  1155. wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
  1156. " tag to parse optional tbsCertificate "
  1157. "field(s); parsed class %d tag 0x%x",
  1158. hdr.class, hdr.tag);
  1159. return -1;
  1160. }
  1161. }
  1162. if (hdr.tag == 2) {
  1163. /* subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL */
  1164. wpa_printf(MSG_DEBUG, "X509: subjectUniqueID");
  1165. /* TODO: parse UniqueIdentifier ::= BIT STRING */
  1166. pos = hdr.payload + hdr.length;
  1167. if (pos == end)
  1168. return 0;
  1169. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1170. hdr.class != ASN1_CLASS_CONTEXT_SPECIFIC) {
  1171. wpa_printf(MSG_DEBUG, "X509: Expected Context-Specific"
  1172. " tag to parse optional tbsCertificate "
  1173. "field(s); parsed class %d tag 0x%x",
  1174. hdr.class, hdr.tag);
  1175. return -1;
  1176. }
  1177. }
  1178. if (hdr.tag != 3) {
  1179. wpa_printf(MSG_DEBUG, "X509: Ignored unexpected "
  1180. "Context-Specific tag %d in optional "
  1181. "tbsCertificate fields", hdr.tag);
  1182. return 0;
  1183. }
  1184. /* extensions [3] EXPLICIT Extensions OPTIONAL */
  1185. if (cert->version != X509_CERT_V3) {
  1186. wpa_printf(MSG_DEBUG, "X509: X.509%d certificate and "
  1187. "Extensions data which are only allowed for "
  1188. "version 3", cert->version + 1);
  1189. return -1;
  1190. }
  1191. if (x509_parse_extensions(cert, hdr.payload, hdr.length) < 0)
  1192. return -1;
  1193. pos = hdr.payload + hdr.length;
  1194. if (pos < end) {
  1195. wpa_hexdump(MSG_DEBUG,
  1196. "X509: Ignored extra tbsCertificate data",
  1197. pos, end - pos);
  1198. }
  1199. return 0;
  1200. }
  1201. static int x509_rsadsi_oid(struct asn1_oid *oid)
  1202. {
  1203. return oid->len >= 4 &&
  1204. oid->oid[0] == 1 /* iso */ &&
  1205. oid->oid[1] == 2 /* member-body */ &&
  1206. oid->oid[2] == 840 /* us */ &&
  1207. oid->oid[3] == 113549 /* rsadsi */;
  1208. }
  1209. static int x509_pkcs_oid(struct asn1_oid *oid)
  1210. {
  1211. return oid->len >= 5 &&
  1212. x509_rsadsi_oid(oid) &&
  1213. oid->oid[4] == 1 /* pkcs */;
  1214. }
  1215. static int x509_digest_oid(struct asn1_oid *oid)
  1216. {
  1217. return oid->len >= 5 &&
  1218. x509_rsadsi_oid(oid) &&
  1219. oid->oid[4] == 2 /* digestAlgorithm */;
  1220. }
  1221. static int x509_sha1_oid(struct asn1_oid *oid)
  1222. {
  1223. return oid->len == 6 &&
  1224. oid->oid[0] == 1 /* iso */ &&
  1225. oid->oid[1] == 3 /* identified-organization */ &&
  1226. oid->oid[2] == 14 /* oiw */ &&
  1227. oid->oid[3] == 3 /* secsig */ &&
  1228. oid->oid[4] == 2 /* algorithms */ &&
  1229. oid->oid[5] == 26 /* id-sha1 */;
  1230. }
  1231. static int x509_sha256_oid(struct asn1_oid *oid)
  1232. {
  1233. return oid->len == 9 &&
  1234. oid->oid[0] == 2 /* joint-iso-itu-t */ &&
  1235. oid->oid[1] == 16 /* country */ &&
  1236. oid->oid[2] == 840 /* us */ &&
  1237. oid->oid[3] == 1 /* organization */ &&
  1238. oid->oid[4] == 101 /* gov */ &&
  1239. oid->oid[5] == 3 /* csor */ &&
  1240. oid->oid[6] == 4 /* nistAlgorithm */ &&
  1241. oid->oid[7] == 2 /* hashAlgs */ &&
  1242. oid->oid[8] == 1 /* sha256 */;
  1243. }
  1244. /**
  1245. * x509_certificate_parse - Parse a X.509 certificate in DER format
  1246. * @buf: Pointer to the X.509 certificate in DER format
  1247. * @len: Buffer length
  1248. * Returns: Pointer to the parsed certificate or %NULL on failure
  1249. *
  1250. * Caller is responsible for freeing the returned certificate by calling
  1251. * x509_certificate_free().
  1252. */
  1253. struct x509_certificate * x509_certificate_parse(const u8 *buf, size_t len)
  1254. {
  1255. struct asn1_hdr hdr;
  1256. const u8 *pos, *end, *hash_start;
  1257. struct x509_certificate *cert;
  1258. cert = os_zalloc(sizeof(*cert) + len);
  1259. if (cert == NULL)
  1260. return NULL;
  1261. os_memcpy(cert + 1, buf, len);
  1262. cert->cert_start = (u8 *) (cert + 1);
  1263. cert->cert_len = len;
  1264. pos = buf;
  1265. end = buf + len;
  1266. /* RFC 3280 - X.509 v3 certificate / ASN.1 DER */
  1267. /* Certificate ::= SEQUENCE */
  1268. if (asn1_get_next(pos, len, &hdr) < 0 ||
  1269. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1270. hdr.tag != ASN1_TAG_SEQUENCE) {
  1271. wpa_printf(MSG_DEBUG, "X509: Certificate did not start with "
  1272. "a valid SEQUENCE - found class %d tag 0x%x",
  1273. hdr.class, hdr.tag);
  1274. x509_certificate_free(cert);
  1275. return NULL;
  1276. }
  1277. pos = hdr.payload;
  1278. if (pos + hdr.length > end) {
  1279. x509_certificate_free(cert);
  1280. return NULL;
  1281. }
  1282. if (pos + hdr.length < end) {
  1283. wpa_hexdump(MSG_MSGDUMP, "X509: Ignoring extra data after DER "
  1284. "encoded certificate",
  1285. pos + hdr.length, end - (pos + hdr.length));
  1286. end = pos + hdr.length;
  1287. }
  1288. hash_start = pos;
  1289. cert->tbs_cert_start = cert->cert_start + (hash_start - buf);
  1290. if (x509_parse_tbs_certificate(pos, end - pos, cert, &pos)) {
  1291. x509_certificate_free(cert);
  1292. return NULL;
  1293. }
  1294. cert->tbs_cert_len = pos - hash_start;
  1295. /* signatureAlgorithm AlgorithmIdentifier */
  1296. if (x509_parse_algorithm_identifier(pos, end - pos,
  1297. &cert->signature_alg, &pos)) {
  1298. x509_certificate_free(cert);
  1299. return NULL;
  1300. }
  1301. /* signatureValue BIT STRING */
  1302. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1303. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1304. hdr.tag != ASN1_TAG_BITSTRING) {
  1305. wpa_printf(MSG_DEBUG, "X509: Expected BITSTRING "
  1306. "(signatureValue) - found class %d tag 0x%x",
  1307. hdr.class, hdr.tag);
  1308. x509_certificate_free(cert);
  1309. return NULL;
  1310. }
  1311. if (hdr.length < 1) {
  1312. x509_certificate_free(cert);
  1313. return NULL;
  1314. }
  1315. pos = hdr.payload;
  1316. if (*pos) {
  1317. wpa_printf(MSG_DEBUG, "X509: BITSTRING - %d unused bits",
  1318. *pos);
  1319. /* PKCS #1 v1.5 10.2.1:
  1320. * It is an error if the length in bits of the signature S is
  1321. * not a multiple of eight.
  1322. */
  1323. x509_certificate_free(cert);
  1324. return NULL;
  1325. }
  1326. os_free(cert->sign_value);
  1327. cert->sign_value = os_malloc(hdr.length - 1);
  1328. if (cert->sign_value == NULL) {
  1329. wpa_printf(MSG_DEBUG, "X509: Failed to allocate memory for "
  1330. "signatureValue");
  1331. x509_certificate_free(cert);
  1332. return NULL;
  1333. }
  1334. os_memcpy(cert->sign_value, pos + 1, hdr.length - 1);
  1335. cert->sign_value_len = hdr.length - 1;
  1336. wpa_hexdump(MSG_MSGDUMP, "X509: signature",
  1337. cert->sign_value, cert->sign_value_len);
  1338. return cert;
  1339. }
  1340. /**
  1341. * x509_certificate_check_signature - Verify certificate signature
  1342. * @issuer: Issuer certificate
  1343. * @cert: Certificate to be verified
  1344. * Returns: 0 if cert has a valid signature that was signed by the issuer,
  1345. * -1 if not
  1346. */
  1347. int x509_certificate_check_signature(struct x509_certificate *issuer,
  1348. struct x509_certificate *cert)
  1349. {
  1350. struct crypto_public_key *pk;
  1351. u8 *data;
  1352. const u8 *pos, *end, *next, *da_end;
  1353. size_t data_len;
  1354. struct asn1_hdr hdr;
  1355. struct asn1_oid oid;
  1356. u8 hash[32];
  1357. size_t hash_len;
  1358. if (!x509_pkcs_oid(&cert->signature.oid) ||
  1359. cert->signature.oid.len != 7 ||
  1360. cert->signature.oid.oid[5] != 1 /* pkcs-1 */) {
  1361. wpa_printf(MSG_DEBUG, "X509: Unrecognized signature "
  1362. "algorithm");
  1363. return -1;
  1364. }
  1365. pk = crypto_public_key_import(issuer->public_key,
  1366. issuer->public_key_len);
  1367. if (pk == NULL)
  1368. return -1;
  1369. data_len = cert->sign_value_len;
  1370. data = os_malloc(data_len);
  1371. if (data == NULL) {
  1372. crypto_public_key_free(pk);
  1373. return -1;
  1374. }
  1375. if (crypto_public_key_decrypt_pkcs1(pk, cert->sign_value,
  1376. cert->sign_value_len, data,
  1377. &data_len) < 0) {
  1378. wpa_printf(MSG_DEBUG, "X509: Failed to decrypt signature");
  1379. crypto_public_key_free(pk);
  1380. os_free(data);
  1381. return -1;
  1382. }
  1383. crypto_public_key_free(pk);
  1384. wpa_hexdump(MSG_MSGDUMP, "X509: Signature data D", data, data_len);
  1385. /*
  1386. * PKCS #1 v1.5, 10.1.2:
  1387. *
  1388. * DigestInfo ::= SEQUENCE {
  1389. * digestAlgorithm DigestAlgorithmIdentifier,
  1390. * digest Digest
  1391. * }
  1392. *
  1393. * DigestAlgorithmIdentifier ::= AlgorithmIdentifier
  1394. *
  1395. * Digest ::= OCTET STRING
  1396. *
  1397. */
  1398. if (asn1_get_next(data, data_len, &hdr) < 0 ||
  1399. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1400. hdr.tag != ASN1_TAG_SEQUENCE) {
  1401. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  1402. "(DigestInfo) - found class %d tag 0x%x",
  1403. hdr.class, hdr.tag);
  1404. os_free(data);
  1405. return -1;
  1406. }
  1407. pos = hdr.payload;
  1408. end = pos + hdr.length;
  1409. /*
  1410. * X.509:
  1411. * AlgorithmIdentifier ::= SEQUENCE {
  1412. * algorithm OBJECT IDENTIFIER,
  1413. * parameters ANY DEFINED BY algorithm OPTIONAL
  1414. * }
  1415. */
  1416. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1417. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1418. hdr.tag != ASN1_TAG_SEQUENCE) {
  1419. wpa_printf(MSG_DEBUG, "X509: Expected SEQUENCE "
  1420. "(AlgorithmIdentifier) - found class %d tag 0x%x",
  1421. hdr.class, hdr.tag);
  1422. os_free(data);
  1423. return -1;
  1424. }
  1425. da_end = hdr.payload + hdr.length;
  1426. if (asn1_get_oid(hdr.payload, hdr.length, &oid, &next)) {
  1427. wpa_printf(MSG_DEBUG, "X509: Failed to parse digestAlgorithm");
  1428. os_free(data);
  1429. return -1;
  1430. }
  1431. if (x509_sha1_oid(&oid)) {
  1432. if (cert->signature.oid.oid[6] !=
  1433. 5 /* sha-1WithRSAEncryption */) {
  1434. wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA1 "
  1435. "does not match with certificate "
  1436. "signatureAlgorithm (%lu)",
  1437. cert->signature.oid.oid[6]);
  1438. os_free(data);
  1439. return -1;
  1440. }
  1441. goto skip_digest_oid;
  1442. }
  1443. if (x509_sha256_oid(&oid)) {
  1444. if (cert->signature.oid.oid[6] !=
  1445. 11 /* sha2561WithRSAEncryption */) {
  1446. wpa_printf(MSG_DEBUG, "X509: digestAlgorithm SHA256 "
  1447. "does not match with certificate "
  1448. "signatureAlgorithm (%lu)",
  1449. cert->signature.oid.oid[6]);
  1450. os_free(data);
  1451. return -1;
  1452. }
  1453. goto skip_digest_oid;
  1454. }
  1455. if (!x509_digest_oid(&oid)) {
  1456. wpa_printf(MSG_DEBUG, "X509: Unrecognized digestAlgorithm");
  1457. os_free(data);
  1458. return -1;
  1459. }
  1460. switch (oid.oid[5]) {
  1461. case 5: /* md5 */
  1462. if (cert->signature.oid.oid[6] != 4 /* md5WithRSAEncryption */)
  1463. {
  1464. wpa_printf(MSG_DEBUG, "X509: digestAlgorithm MD5 does "
  1465. "not match with certificate "
  1466. "signatureAlgorithm (%lu)",
  1467. cert->signature.oid.oid[6]);
  1468. os_free(data);
  1469. return -1;
  1470. }
  1471. break;
  1472. case 2: /* md2 */
  1473. case 4: /* md4 */
  1474. default:
  1475. wpa_printf(MSG_DEBUG, "X509: Unsupported digestAlgorithm "
  1476. "(%lu)", oid.oid[5]);
  1477. os_free(data);
  1478. return -1;
  1479. }
  1480. skip_digest_oid:
  1481. /* Digest ::= OCTET STRING */
  1482. pos = da_end;
  1483. end = data + data_len;
  1484. if (asn1_get_next(pos, end - pos, &hdr) < 0 ||
  1485. hdr.class != ASN1_CLASS_UNIVERSAL ||
  1486. hdr.tag != ASN1_TAG_OCTETSTRING) {
  1487. wpa_printf(MSG_DEBUG, "X509: Expected OCTETSTRING "
  1488. "(Digest) - found class %d tag 0x%x",
  1489. hdr.class, hdr.tag);
  1490. os_free(data);
  1491. return -1;
  1492. }
  1493. wpa_hexdump(MSG_MSGDUMP, "X509: Decrypted Digest",
  1494. hdr.payload, hdr.length);
  1495. switch (cert->signature.oid.oid[6]) {
  1496. case 4: /* md5WithRSAEncryption */
  1497. md5_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
  1498. hash);
  1499. hash_len = 16;
  1500. wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (MD5)",
  1501. hash, hash_len);
  1502. break;
  1503. case 5: /* sha-1WithRSAEncryption */
  1504. sha1_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
  1505. hash);
  1506. hash_len = 20;
  1507. wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA1)",
  1508. hash, hash_len);
  1509. break;
  1510. case 11: /* sha256WithRSAEncryption */
  1511. sha256_vector(1, &cert->tbs_cert_start, &cert->tbs_cert_len,
  1512. hash);
  1513. hash_len = 32;
  1514. wpa_hexdump(MSG_MSGDUMP, "X509: Certificate hash (SHA256)",
  1515. hash, hash_len);
  1516. break;
  1517. case 2: /* md2WithRSAEncryption */
  1518. case 12: /* sha384WithRSAEncryption */
  1519. case 13: /* sha512WithRSAEncryption */
  1520. default:
  1521. wpa_printf(MSG_INFO, "X509: Unsupported certificate signature "
  1522. "algorithm (%lu)", cert->signature.oid.oid[6]);
  1523. os_free(data);
  1524. return -1;
  1525. }
  1526. if (hdr.length != hash_len ||
  1527. os_memcmp_const(hdr.payload, hash, hdr.length) != 0) {
  1528. wpa_printf(MSG_INFO, "X509: Certificate Digest does not match "
  1529. "with calculated tbsCertificate hash");
  1530. os_free(data);
  1531. return -1;
  1532. }
  1533. if (hdr.payload + hdr.length < data + data_len) {
  1534. wpa_hexdump(MSG_INFO,
  1535. "X509: Extra data after certificate signature hash",
  1536. hdr.payload + hdr.length,
  1537. data + data_len - hdr.payload - hdr.length);
  1538. os_free(data);
  1539. return -1;
  1540. }
  1541. os_free(data);
  1542. wpa_printf(MSG_DEBUG, "X509: Certificate Digest matches with "
  1543. "calculated tbsCertificate hash");
  1544. return 0;
  1545. }
  1546. static int x509_valid_issuer(const struct x509_certificate *cert)
  1547. {
  1548. if ((cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS) &&
  1549. !cert->ca) {
  1550. wpa_printf(MSG_DEBUG, "X509: Non-CA certificate used as an "
  1551. "issuer");
  1552. return -1;
  1553. }
  1554. if (cert->version == X509_CERT_V3 &&
  1555. !(cert->extensions_present & X509_EXT_BASIC_CONSTRAINTS)) {
  1556. wpa_printf(MSG_DEBUG, "X509: v3 CA certificate did not "
  1557. "include BasicConstraints extension");
  1558. return -1;
  1559. }
  1560. if ((cert->extensions_present & X509_EXT_KEY_USAGE) &&
  1561. !(cert->key_usage & X509_KEY_USAGE_KEY_CERT_SIGN)) {
  1562. wpa_printf(MSG_DEBUG, "X509: Issuer certificate did not have "
  1563. "keyCertSign bit in Key Usage");
  1564. return -1;
  1565. }
  1566. return 0;
  1567. }
  1568. /**
  1569. * x509_certificate_chain_validate - Validate X.509 certificate chain
  1570. * @trusted: List of trusted certificates
  1571. * @chain: Certificate chain to be validated (first chain must be issued by
  1572. * signed by the second certificate in the chain and so on)
  1573. * @reason: Buffer for returning failure reason (X509_VALIDATE_*)
  1574. * Returns: 0 if chain is valid, -1 if not
  1575. */
  1576. int x509_certificate_chain_validate(struct x509_certificate *trusted,
  1577. struct x509_certificate *chain,
  1578. int *reason, int disable_time_checks)
  1579. {
  1580. long unsigned idx;
  1581. int chain_trusted = 0;
  1582. struct x509_certificate *cert, *trust;
  1583. char buf[128];
  1584. struct os_time now;
  1585. *reason = X509_VALIDATE_OK;
  1586. wpa_printf(MSG_DEBUG, "X509: Validate certificate chain");
  1587. os_get_time(&now);
  1588. for (cert = chain, idx = 0; cert; cert = cert->next, idx++) {
  1589. x509_name_string(&cert->subject, buf, sizeof(buf));
  1590. wpa_printf(MSG_DEBUG, "X509: %lu: %s", idx, buf);
  1591. if (chain_trusted)
  1592. continue;
  1593. if (!disable_time_checks &&
  1594. ((unsigned long) now.sec <
  1595. (unsigned long) cert->not_before ||
  1596. (unsigned long) now.sec >
  1597. (unsigned long) cert->not_after)) {
  1598. wpa_printf(MSG_INFO, "X509: Certificate not valid "
  1599. "(now=%lu not_before=%lu not_after=%lu)",
  1600. now.sec, cert->not_before, cert->not_after);
  1601. *reason = X509_VALIDATE_CERTIFICATE_EXPIRED;
  1602. return -1;
  1603. }
  1604. if (cert->next) {
  1605. if (x509_name_compare(&cert->issuer,
  1606. &cert->next->subject) != 0) {
  1607. wpa_printf(MSG_DEBUG, "X509: Certificate "
  1608. "chain issuer name mismatch");
  1609. x509_name_string(&cert->issuer, buf,
  1610. sizeof(buf));
  1611. wpa_printf(MSG_DEBUG, "X509: cert issuer: %s",
  1612. buf);
  1613. x509_name_string(&cert->next->subject, buf,
  1614. sizeof(buf));
  1615. wpa_printf(MSG_DEBUG, "X509: next cert "
  1616. "subject: %s", buf);
  1617. *reason = X509_VALIDATE_CERTIFICATE_UNKNOWN;
  1618. return -1;
  1619. }
  1620. if (x509_valid_issuer(cert->next) < 0) {
  1621. *reason = X509_VALIDATE_BAD_CERTIFICATE;
  1622. return -1;
  1623. }
  1624. if ((cert->next->extensions_present &
  1625. X509_EXT_PATH_LEN_CONSTRAINT) &&
  1626. idx > cert->next->path_len_constraint) {
  1627. wpa_printf(MSG_DEBUG, "X509: pathLenConstraint"
  1628. " not met (idx=%lu issuer "
  1629. "pathLenConstraint=%lu)", idx,
  1630. cert->next->path_len_constraint);
  1631. *reason = X509_VALIDATE_BAD_CERTIFICATE;
  1632. return -1;
  1633. }
  1634. if (x509_certificate_check_signature(cert->next, cert)
  1635. < 0) {
  1636. wpa_printf(MSG_DEBUG, "X509: Invalid "
  1637. "certificate signature within "
  1638. "chain");
  1639. *reason = X509_VALIDATE_BAD_CERTIFICATE;
  1640. return -1;
  1641. }
  1642. }
  1643. for (trust = trusted; trust; trust = trust->next) {
  1644. if (x509_name_compare(&cert->issuer, &trust->subject)
  1645. == 0)
  1646. break;
  1647. }
  1648. if (trust) {
  1649. wpa_printf(MSG_DEBUG, "X509: Found issuer from the "
  1650. "list of trusted certificates");
  1651. if (x509_valid_issuer(trust) < 0) {
  1652. *reason = X509_VALIDATE_BAD_CERTIFICATE;
  1653. return -1;
  1654. }
  1655. if (x509_certificate_check_signature(trust, cert) < 0)
  1656. {
  1657. wpa_printf(MSG_DEBUG, "X509: Invalid "
  1658. "certificate signature");
  1659. *reason = X509_VALIDATE_BAD_CERTIFICATE;
  1660. return -1;
  1661. }
  1662. wpa_printf(MSG_DEBUG, "X509: Trusted certificate "
  1663. "found to complete the chain");
  1664. chain_trusted = 1;
  1665. }
  1666. }
  1667. if (!chain_trusted) {
  1668. wpa_printf(MSG_DEBUG, "X509: Did not find any of the issuers "
  1669. "from the list of trusted certificates");
  1670. if (trusted) {
  1671. *reason = X509_VALIDATE_UNKNOWN_CA;
  1672. return -1;
  1673. }
  1674. wpa_printf(MSG_DEBUG, "X509: Certificate chain validation "
  1675. "disabled - ignore unknown CA issue");
  1676. }
  1677. wpa_printf(MSG_DEBUG, "X509: Certificate chain valid");
  1678. return 0;
  1679. }
  1680. /**
  1681. * x509_certificate_get_subject - Get a certificate based on Subject name
  1682. * @chain: Certificate chain to search through
  1683. * @name: Subject name to search for
  1684. * Returns: Pointer to the certificate with the given Subject name or
  1685. * %NULL on failure
  1686. */
  1687. struct x509_certificate *
  1688. x509_certificate_get_subject(struct x509_certificate *chain,
  1689. struct x509_name *name)
  1690. {
  1691. struct x509_certificate *cert;
  1692. for (cert = chain; cert; cert = cert->next) {
  1693. if (x509_name_compare(&cert->subject, name) == 0)
  1694. return cert;
  1695. }
  1696. return NULL;
  1697. }
  1698. /**
  1699. * x509_certificate_self_signed - Is the certificate self-signed?
  1700. * @cert: Certificate
  1701. * Returns: 1 if certificate is self-signed, 0 if not
  1702. */
  1703. int x509_certificate_self_signed(struct x509_certificate *cert)
  1704. {
  1705. return x509_name_compare(&cert->issuer, &cert->subject) == 0;
  1706. }