x509v3.c 49 KB

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