x509_crt.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019
  1. /*
  2. * X.509 certificate parsing and verification
  3. *
  4. * Copyright (C) 2006-2014, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * The ITU-T X.509 standard defines a certificate format for PKI.
  27. *
  28. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  29. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  30. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  31. *
  32. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  33. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  34. */
  35. #if !defined(POLARSSL_CONFIG_FILE)
  36. #include "polarssl/config.h"
  37. #else
  38. #include POLARSSL_CONFIG_FILE
  39. #endif
  40. #if defined(POLARSSL_X509_CRT_PARSE_C)
  41. #include "polarssl/x509_crt.h"
  42. #include "polarssl/oid.h"
  43. #if defined(POLARSSL_PEM_PARSE_C)
  44. #include "polarssl/pem.h"
  45. #endif
  46. #if defined(POLARSSL_PLATFORM_C)
  47. #include "polarssl/platform.h"
  48. #else
  49. #define polarssl_malloc malloc
  50. #define polarssl_free free
  51. #endif
  52. #if defined(POLARSSL_THREADING_C)
  53. #include "polarssl/threading.h"
  54. #endif
  55. #include <string.h>
  56. #include <stdlib.h>
  57. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  58. #include <windows.h>
  59. #else
  60. #include <time.h>
  61. #endif
  62. #if defined(EFIX64) || defined(EFI32)
  63. #include <stdio.h>
  64. #endif
  65. #if defined(POLARSSL_FS_IO)
  66. #include <stdio.h>
  67. #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
  68. #include <sys/types.h>
  69. #include <sys/stat.h>
  70. #include <dirent.h>
  71. #endif
  72. #endif
  73. /* Implementation that should never be optimized out by the compiler */
  74. static void polarssl_zeroize( void *v, size_t n ) {
  75. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  76. }
  77. /*
  78. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  79. */
  80. static int x509_get_version( unsigned char **p,
  81. const unsigned char *end,
  82. int *ver )
  83. {
  84. int ret;
  85. size_t len;
  86. if( ( ret = asn1_get_tag( p, end, &len,
  87. ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ) != 0 )
  88. {
  89. if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
  90. {
  91. *ver = 0;
  92. return( 0 );
  93. }
  94. return( ret );
  95. }
  96. end = *p + len;
  97. if( ( ret = asn1_get_int( p, end, ver ) ) != 0 )
  98. return( POLARSSL_ERR_X509_INVALID_VERSION + ret );
  99. if( *p != end )
  100. return( POLARSSL_ERR_X509_INVALID_VERSION +
  101. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  102. return( 0 );
  103. }
  104. /*
  105. * Validity ::= SEQUENCE {
  106. * notBefore Time,
  107. * notAfter Time }
  108. */
  109. static int x509_get_dates( unsigned char **p,
  110. const unsigned char *end,
  111. x509_time *from,
  112. x509_time *to )
  113. {
  114. int ret;
  115. size_t len;
  116. if( ( ret = asn1_get_tag( p, end, &len,
  117. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  118. return( POLARSSL_ERR_X509_INVALID_DATE + ret );
  119. end = *p + len;
  120. if( ( ret = x509_get_time( p, end, from ) ) != 0 )
  121. return( ret );
  122. if( ( ret = x509_get_time( p, end, to ) ) != 0 )
  123. return( ret );
  124. if( *p != end )
  125. return( POLARSSL_ERR_X509_INVALID_DATE +
  126. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  127. return( 0 );
  128. }
  129. /*
  130. * X.509 v2/v3 unique identifier (not parsed)
  131. */
  132. static int x509_get_uid( unsigned char **p,
  133. const unsigned char *end,
  134. x509_buf *uid, int n )
  135. {
  136. int ret;
  137. if( *p == end )
  138. return( 0 );
  139. uid->tag = **p;
  140. if( ( ret = asn1_get_tag( p, end, &uid->len,
  141. ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | n ) ) != 0 )
  142. {
  143. if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
  144. return( 0 );
  145. return( ret );
  146. }
  147. uid->p = *p;
  148. *p += uid->len;
  149. return( 0 );
  150. }
  151. static int x509_get_basic_constraints( unsigned char **p,
  152. const unsigned char *end,
  153. int *ca_istrue,
  154. int *max_pathlen )
  155. {
  156. int ret;
  157. size_t len;
  158. /*
  159. * BasicConstraints ::= SEQUENCE {
  160. * cA BOOLEAN DEFAULT FALSE,
  161. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  162. */
  163. *ca_istrue = 0; /* DEFAULT FALSE */
  164. *max_pathlen = 0; /* endless */
  165. if( ( ret = asn1_get_tag( p, end, &len,
  166. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  167. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  168. if( *p == end )
  169. return( 0 );
  170. if( ( ret = asn1_get_bool( p, end, ca_istrue ) ) != 0 )
  171. {
  172. if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
  173. ret = asn1_get_int( p, end, ca_istrue );
  174. if( ret != 0 )
  175. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  176. if( *ca_istrue != 0 )
  177. *ca_istrue = 1;
  178. }
  179. if( *p == end )
  180. return( 0 );
  181. if( ( ret = asn1_get_int( p, end, max_pathlen ) ) != 0 )
  182. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  183. if( *p != end )
  184. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  185. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  186. (*max_pathlen)++;
  187. return( 0 );
  188. }
  189. static int x509_get_ns_cert_type( unsigned char **p,
  190. const unsigned char *end,
  191. unsigned char *ns_cert_type)
  192. {
  193. int ret;
  194. x509_bitstring bs = { 0, 0, NULL };
  195. if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
  196. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  197. if( bs.len != 1 )
  198. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  199. POLARSSL_ERR_ASN1_INVALID_LENGTH );
  200. /* Get actual bitstring */
  201. *ns_cert_type = *bs.p;
  202. return( 0 );
  203. }
  204. static int x509_get_key_usage( unsigned char **p,
  205. const unsigned char *end,
  206. unsigned char *key_usage)
  207. {
  208. int ret;
  209. x509_bitstring bs = { 0, 0, NULL };
  210. if( ( ret = asn1_get_bitstring( p, end, &bs ) ) != 0 )
  211. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  212. if( bs.len < 1 )
  213. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  214. POLARSSL_ERR_ASN1_INVALID_LENGTH );
  215. /* Get actual bitstring */
  216. *key_usage = *bs.p;
  217. return( 0 );
  218. }
  219. /*
  220. * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
  221. *
  222. * KeyPurposeId ::= OBJECT IDENTIFIER
  223. */
  224. static int x509_get_ext_key_usage( unsigned char **p,
  225. const unsigned char *end,
  226. x509_sequence *ext_key_usage)
  227. {
  228. int ret;
  229. if( ( ret = asn1_get_sequence_of( p, end, ext_key_usage, ASN1_OID ) ) != 0 )
  230. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  231. /* Sequence length must be >= 1 */
  232. if( ext_key_usage->buf.p == NULL )
  233. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  234. POLARSSL_ERR_ASN1_INVALID_LENGTH );
  235. return( 0 );
  236. }
  237. /*
  238. * SubjectAltName ::= GeneralNames
  239. *
  240. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  241. *
  242. * GeneralName ::= CHOICE {
  243. * otherName [0] OtherName,
  244. * rfc822Name [1] IA5String,
  245. * dNSName [2] IA5String,
  246. * x400Address [3] ORAddress,
  247. * directoryName [4] Name,
  248. * ediPartyName [5] EDIPartyName,
  249. * uniformResourceIdentifier [6] IA5String,
  250. * iPAddress [7] OCTET STRING,
  251. * registeredID [8] OBJECT IDENTIFIER }
  252. *
  253. * OtherName ::= SEQUENCE {
  254. * type-id OBJECT IDENTIFIER,
  255. * value [0] EXPLICIT ANY DEFINED BY type-id }
  256. *
  257. * EDIPartyName ::= SEQUENCE {
  258. * nameAssigner [0] DirectoryString OPTIONAL,
  259. * partyName [1] DirectoryString }
  260. *
  261. * NOTE: PolarSSL only parses and uses dNSName at this point.
  262. */
  263. static int x509_get_subject_alt_name( unsigned char **p,
  264. const unsigned char *end,
  265. x509_sequence *subject_alt_name )
  266. {
  267. int ret;
  268. size_t len, tag_len;
  269. asn1_buf *buf;
  270. unsigned char tag;
  271. asn1_sequence *cur = subject_alt_name;
  272. /* Get main sequence tag */
  273. if( ( ret = asn1_get_tag( p, end, &len,
  274. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  275. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  276. if( *p + len != end )
  277. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  278. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  279. while( *p < end )
  280. {
  281. if( ( end - *p ) < 1 )
  282. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  283. POLARSSL_ERR_ASN1_OUT_OF_DATA );
  284. tag = **p;
  285. (*p)++;
  286. if( ( ret = asn1_get_len( p, end, &tag_len ) ) != 0 )
  287. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  288. if( ( tag & ASN1_CONTEXT_SPECIFIC ) != ASN1_CONTEXT_SPECIFIC )
  289. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  290. POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
  291. /* Skip everything but DNS name */
  292. if( tag != ( ASN1_CONTEXT_SPECIFIC | 2 ) )
  293. {
  294. *p += tag_len;
  295. continue;
  296. }
  297. /* Allocate and assign next pointer */
  298. if( cur->buf.p != NULL )
  299. {
  300. cur->next = (asn1_sequence *) polarssl_malloc(
  301. sizeof( asn1_sequence ) );
  302. if( cur->next == NULL )
  303. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  304. POLARSSL_ERR_ASN1_MALLOC_FAILED );
  305. memset( cur->next, 0, sizeof( asn1_sequence ) );
  306. cur = cur->next;
  307. }
  308. buf = &(cur->buf);
  309. buf->tag = tag;
  310. buf->p = *p;
  311. buf->len = tag_len;
  312. *p += buf->len;
  313. }
  314. /* Set final sequence entry's next pointer to NULL */
  315. cur->next = NULL;
  316. if( *p != end )
  317. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  318. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  319. return( 0 );
  320. }
  321. /*
  322. * X.509 v3 extensions
  323. *
  324. * TODO: Perform all of the basic constraints tests required by the RFC
  325. * TODO: Set values for undetected extensions to a sane default?
  326. *
  327. */
  328. static int x509_get_crt_ext( unsigned char **p,
  329. const unsigned char *end,
  330. x509_crt *crt )
  331. {
  332. int ret;
  333. size_t len;
  334. unsigned char *end_ext_data, *end_ext_octet;
  335. if( ( ret = x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
  336. {
  337. if( ret == POLARSSL_ERR_ASN1_UNEXPECTED_TAG )
  338. return( 0 );
  339. return( ret );
  340. }
  341. while( *p < end )
  342. {
  343. /*
  344. * Extension ::= SEQUENCE {
  345. * extnID OBJECT IDENTIFIER,
  346. * critical BOOLEAN DEFAULT FALSE,
  347. * extnValue OCTET STRING }
  348. */
  349. x509_buf extn_oid = {0, 0, NULL};
  350. int is_critical = 0; /* DEFAULT FALSE */
  351. int ext_type = 0;
  352. if( ( ret = asn1_get_tag( p, end, &len,
  353. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  354. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  355. end_ext_data = *p + len;
  356. /* Get extension ID */
  357. extn_oid.tag = **p;
  358. if( ( ret = asn1_get_tag( p, end, &extn_oid.len, ASN1_OID ) ) != 0 )
  359. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  360. extn_oid.p = *p;
  361. *p += extn_oid.len;
  362. if( ( end - *p ) < 1 )
  363. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  364. POLARSSL_ERR_ASN1_OUT_OF_DATA );
  365. /* Get optional critical */
  366. if( ( ret = asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
  367. ( ret != POLARSSL_ERR_ASN1_UNEXPECTED_TAG ) )
  368. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  369. /* Data should be octet string type */
  370. if( ( ret = asn1_get_tag( p, end_ext_data, &len,
  371. ASN1_OCTET_STRING ) ) != 0 )
  372. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS + ret );
  373. end_ext_octet = *p + len;
  374. if( end_ext_octet != end_ext_data )
  375. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  376. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  377. /*
  378. * Detect supported extensions
  379. */
  380. ret = oid_get_x509_ext_type( &extn_oid, &ext_type );
  381. if( ret != 0 )
  382. {
  383. /* No parser found, skip extension */
  384. *p = end_ext_octet;
  385. #if !defined(POLARSSL_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
  386. if( is_critical )
  387. {
  388. /* Data is marked as critical: fail */
  389. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  390. POLARSSL_ERR_ASN1_UNEXPECTED_TAG );
  391. }
  392. #endif
  393. continue;
  394. }
  395. crt->ext_types |= ext_type;
  396. switch( ext_type )
  397. {
  398. case EXT_BASIC_CONSTRAINTS:
  399. /* Parse basic constraints */
  400. if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
  401. &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
  402. return( ret );
  403. break;
  404. case EXT_KEY_USAGE:
  405. /* Parse key usage */
  406. if( ( ret = x509_get_key_usage( p, end_ext_octet,
  407. &crt->key_usage ) ) != 0 )
  408. return( ret );
  409. break;
  410. case EXT_EXTENDED_KEY_USAGE:
  411. /* Parse extended key usage */
  412. if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
  413. &crt->ext_key_usage ) ) != 0 )
  414. return( ret );
  415. break;
  416. case EXT_SUBJECT_ALT_NAME:
  417. /* Parse subject alt name */
  418. if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
  419. &crt->subject_alt_names ) ) != 0 )
  420. return( ret );
  421. break;
  422. case EXT_NS_CERT_TYPE:
  423. /* Parse netscape certificate type */
  424. if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
  425. &crt->ns_cert_type ) ) != 0 )
  426. return( ret );
  427. break;
  428. default:
  429. return( POLARSSL_ERR_X509_FEATURE_UNAVAILABLE );
  430. }
  431. }
  432. if( *p != end )
  433. return( POLARSSL_ERR_X509_INVALID_EXTENSIONS +
  434. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  435. return( 0 );
  436. }
  437. /*
  438. * Parse and fill a single X.509 certificate in DER format
  439. */
  440. static int x509_crt_parse_der_core( x509_crt *crt, const unsigned char *buf,
  441. size_t buflen )
  442. {
  443. int ret;
  444. size_t len;
  445. unsigned char *p, *end, *crt_end;
  446. x509_buf sig_params1, sig_params2;
  447. memset( &sig_params1, 0, sizeof( x509_buf ) );
  448. memset( &sig_params2, 0, sizeof( x509_buf ) );
  449. /*
  450. * Check for valid input
  451. */
  452. if( crt == NULL || buf == NULL )
  453. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  454. p = (unsigned char *) polarssl_malloc( len = buflen );
  455. if( p == NULL )
  456. return( POLARSSL_ERR_X509_MALLOC_FAILED );
  457. memcpy( p, buf, buflen );
  458. crt->raw.p = p;
  459. crt->raw.len = len;
  460. end = p + len;
  461. /*
  462. * Certificate ::= SEQUENCE {
  463. * tbsCertificate TBSCertificate,
  464. * signatureAlgorithm AlgorithmIdentifier,
  465. * signatureValue BIT STRING }
  466. */
  467. if( ( ret = asn1_get_tag( &p, end, &len,
  468. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  469. {
  470. x509_crt_free( crt );
  471. return( POLARSSL_ERR_X509_INVALID_FORMAT );
  472. }
  473. if( len > (size_t) ( end - p ) )
  474. {
  475. x509_crt_free( crt );
  476. return( POLARSSL_ERR_X509_INVALID_FORMAT +
  477. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  478. }
  479. crt_end = p + len;
  480. /*
  481. * TBSCertificate ::= SEQUENCE {
  482. */
  483. crt->tbs.p = p;
  484. if( ( ret = asn1_get_tag( &p, end, &len,
  485. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  486. {
  487. x509_crt_free( crt );
  488. return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
  489. }
  490. end = p + len;
  491. crt->tbs.len = end - crt->tbs.p;
  492. /*
  493. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  494. *
  495. * CertificateSerialNumber ::= INTEGER
  496. *
  497. * signature AlgorithmIdentifier
  498. */
  499. if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
  500. ( ret = x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
  501. ( ret = x509_get_alg( &p, end, &crt->sig_oid1,
  502. &sig_params1 ) ) != 0 )
  503. {
  504. x509_crt_free( crt );
  505. return( ret );
  506. }
  507. crt->version++;
  508. if( crt->version > 3 )
  509. {
  510. x509_crt_free( crt );
  511. return( POLARSSL_ERR_X509_UNKNOWN_VERSION );
  512. }
  513. if( ( ret = x509_get_sig_alg( &crt->sig_oid1, &sig_params1,
  514. &crt->sig_md, &crt->sig_pk,
  515. &crt->sig_opts ) ) != 0 )
  516. {
  517. x509_crt_free( crt );
  518. return( ret );
  519. }
  520. /*
  521. * issuer Name
  522. */
  523. crt->issuer_raw.p = p;
  524. if( ( ret = asn1_get_tag( &p, end, &len,
  525. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  526. {
  527. x509_crt_free( crt );
  528. return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
  529. }
  530. if( ( ret = x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
  531. {
  532. x509_crt_free( crt );
  533. return( ret );
  534. }
  535. crt->issuer_raw.len = p - crt->issuer_raw.p;
  536. /*
  537. * Validity ::= SEQUENCE {
  538. * notBefore Time,
  539. * notAfter Time }
  540. *
  541. */
  542. if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
  543. &crt->valid_to ) ) != 0 )
  544. {
  545. x509_crt_free( crt );
  546. return( ret );
  547. }
  548. /*
  549. * subject Name
  550. */
  551. crt->subject_raw.p = p;
  552. if( ( ret = asn1_get_tag( &p, end, &len,
  553. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  554. {
  555. x509_crt_free( crt );
  556. return( POLARSSL_ERR_X509_INVALID_FORMAT + ret );
  557. }
  558. if( len && ( ret = x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
  559. {
  560. x509_crt_free( crt );
  561. return( ret );
  562. }
  563. crt->subject_raw.len = p - crt->subject_raw.p;
  564. /*
  565. * SubjectPublicKeyInfo
  566. */
  567. if( ( ret = pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
  568. {
  569. x509_crt_free( crt );
  570. return( ret );
  571. }
  572. /*
  573. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  574. * -- If present, version shall be v2 or v3
  575. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  576. * -- If present, version shall be v2 or v3
  577. * extensions [3] EXPLICIT Extensions OPTIONAL
  578. * -- If present, version shall be v3
  579. */
  580. if( crt->version == 2 || crt->version == 3 )
  581. {
  582. ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
  583. if( ret != 0 )
  584. {
  585. x509_crt_free( crt );
  586. return( ret );
  587. }
  588. }
  589. if( crt->version == 2 || crt->version == 3 )
  590. {
  591. ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
  592. if( ret != 0 )
  593. {
  594. x509_crt_free( crt );
  595. return( ret );
  596. }
  597. }
  598. #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
  599. if( crt->version == 3 )
  600. {
  601. #endif
  602. ret = x509_get_crt_ext( &p, end, crt );
  603. if( ret != 0 )
  604. {
  605. x509_crt_free( crt );
  606. return( ret );
  607. }
  608. #if !defined(POLARSSL_X509_ALLOW_EXTENSIONS_NON_V3)
  609. }
  610. #endif
  611. if( p != end )
  612. {
  613. x509_crt_free( crt );
  614. return( POLARSSL_ERR_X509_INVALID_FORMAT +
  615. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  616. }
  617. end = crt_end;
  618. /*
  619. * }
  620. * -- end of TBSCertificate
  621. *
  622. * signatureAlgorithm AlgorithmIdentifier,
  623. * signatureValue BIT STRING
  624. */
  625. if( ( ret = x509_get_alg( &p, end, &crt->sig_oid2, &sig_params2 ) ) != 0 )
  626. {
  627. x509_crt_free( crt );
  628. return( ret );
  629. }
  630. if( crt->sig_oid1.len != crt->sig_oid2.len ||
  631. memcmp( crt->sig_oid1.p, crt->sig_oid2.p, crt->sig_oid1.len ) != 0 ||
  632. sig_params1.len != sig_params2.len ||
  633. memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 )
  634. {
  635. x509_crt_free( crt );
  636. return( POLARSSL_ERR_X509_SIG_MISMATCH );
  637. }
  638. if( ( ret = x509_get_sig( &p, end, &crt->sig ) ) != 0 )
  639. {
  640. x509_crt_free( crt );
  641. return( ret );
  642. }
  643. if( p != end )
  644. {
  645. x509_crt_free( crt );
  646. return( POLARSSL_ERR_X509_INVALID_FORMAT +
  647. POLARSSL_ERR_ASN1_LENGTH_MISMATCH );
  648. }
  649. return( 0 );
  650. }
  651. /*
  652. * Parse one X.509 certificate in DER format from a buffer and add them to a
  653. * chained list
  654. */
  655. int x509_crt_parse_der( x509_crt *chain, const unsigned char *buf,
  656. size_t buflen )
  657. {
  658. int ret;
  659. x509_crt *crt = chain, *prev = NULL;
  660. /*
  661. * Check for valid input
  662. */
  663. if( crt == NULL || buf == NULL )
  664. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  665. while( crt->version != 0 && crt->next != NULL )
  666. {
  667. prev = crt;
  668. crt = crt->next;
  669. }
  670. /*
  671. * Add new certificate on the end of the chain if needed.
  672. */
  673. if( crt->version != 0 && crt->next == NULL )
  674. {
  675. crt->next = (x509_crt *) polarssl_malloc( sizeof( x509_crt ) );
  676. if( crt->next == NULL )
  677. return( POLARSSL_ERR_X509_MALLOC_FAILED );
  678. prev = crt;
  679. crt = crt->next;
  680. x509_crt_init( crt );
  681. }
  682. if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
  683. {
  684. if( prev )
  685. prev->next = NULL;
  686. if( crt != chain )
  687. polarssl_free( crt );
  688. return( ret );
  689. }
  690. return( 0 );
  691. }
  692. /*
  693. * Parse one or more PEM certificates from a buffer and add them to the chained
  694. * list
  695. */
  696. int x509_crt_parse( x509_crt *chain, const unsigned char *buf, size_t buflen )
  697. {
  698. int success = 0, first_error = 0, total_failed = 0;
  699. int buf_format = X509_FORMAT_DER;
  700. /*
  701. * Check for valid input
  702. */
  703. if( chain == NULL || buf == NULL )
  704. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  705. /*
  706. * Determine buffer content. Buffer contains either one DER certificate or
  707. * one or more PEM certificates.
  708. */
  709. #if defined(POLARSSL_PEM_PARSE_C)
  710. if( strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
  711. buf_format = X509_FORMAT_PEM;
  712. #endif
  713. if( buf_format == X509_FORMAT_DER )
  714. return x509_crt_parse_der( chain, buf, buflen );
  715. #if defined(POLARSSL_PEM_PARSE_C)
  716. if( buf_format == X509_FORMAT_PEM )
  717. {
  718. int ret;
  719. pem_context pem;
  720. while( buflen > 0 )
  721. {
  722. size_t use_len;
  723. pem_init( &pem );
  724. ret = pem_read_buffer( &pem,
  725. "-----BEGIN CERTIFICATE-----",
  726. "-----END CERTIFICATE-----",
  727. buf, NULL, 0, &use_len );
  728. if( ret == 0 )
  729. {
  730. /*
  731. * Was PEM encoded
  732. */
  733. buflen -= use_len;
  734. buf += use_len;
  735. }
  736. else if( ret == POLARSSL_ERR_PEM_BAD_INPUT_DATA )
  737. {
  738. return( ret );
  739. }
  740. else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  741. {
  742. pem_free( &pem );
  743. /*
  744. * PEM header and footer were found
  745. */
  746. buflen -= use_len;
  747. buf += use_len;
  748. if( first_error == 0 )
  749. first_error = ret;
  750. continue;
  751. }
  752. else
  753. break;
  754. ret = x509_crt_parse_der( chain, pem.buf, pem.buflen );
  755. pem_free( &pem );
  756. if( ret != 0 )
  757. {
  758. /*
  759. * Quit parsing on a memory error
  760. */
  761. if( ret == POLARSSL_ERR_X509_MALLOC_FAILED )
  762. return( ret );
  763. if( first_error == 0 )
  764. first_error = ret;
  765. total_failed++;
  766. continue;
  767. }
  768. success = 1;
  769. }
  770. }
  771. #endif /* POLARSSL_PEM_PARSE_C */
  772. if( success )
  773. return( total_failed );
  774. else if( first_error )
  775. return( first_error );
  776. else
  777. return( POLARSSL_ERR_X509_CERT_UNKNOWN_FORMAT );
  778. }
  779. #if defined(POLARSSL_FS_IO)
  780. /*
  781. * Load one or more certificates and add them to the chained list
  782. */
  783. int x509_crt_parse_file( x509_crt *chain, const char *path )
  784. {
  785. int ret;
  786. size_t n;
  787. unsigned char *buf;
  788. if( ( ret = x509_load_file( path, &buf, &n ) ) != 0 )
  789. return( ret );
  790. ret = x509_crt_parse( chain, buf, n );
  791. polarssl_zeroize( buf, n + 1 );
  792. polarssl_free( buf );
  793. return( ret );
  794. }
  795. #if defined(POLARSSL_THREADING_PTHREAD)
  796. static threading_mutex_t readdir_mutex = PTHREAD_MUTEX_INITIALIZER;
  797. #endif
  798. int x509_crt_parse_path( x509_crt *chain, const char *path )
  799. {
  800. int ret = 0;
  801. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  802. int w_ret;
  803. WCHAR szDir[MAX_PATH];
  804. char filename[MAX_PATH];
  805. char *p;
  806. int len = (int) strlen( path );
  807. WIN32_FIND_DATAW file_data;
  808. HANDLE hFind;
  809. if( len > MAX_PATH - 3 )
  810. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  811. memset( szDir, 0, sizeof(szDir) );
  812. memset( filename, 0, MAX_PATH );
  813. memcpy( filename, path, len );
  814. filename[len++] = '\\';
  815. p = filename + len;
  816. filename[len++] = '*';
  817. w_ret = MultiByteToWideChar( CP_ACP, 0, filename, len, szDir,
  818. MAX_PATH - 3 );
  819. hFind = FindFirstFileW( szDir, &file_data );
  820. if( hFind == INVALID_HANDLE_VALUE )
  821. return( POLARSSL_ERR_X509_FILE_IO_ERROR );
  822. len = MAX_PATH - len;
  823. do
  824. {
  825. memset( p, 0, len );
  826. if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  827. continue;
  828. w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
  829. lstrlenW( file_data.cFileName ),
  830. p, len - 1,
  831. NULL, NULL );
  832. w_ret = x509_crt_parse_file( chain, filename );
  833. if( w_ret < 0 )
  834. ret++;
  835. else
  836. ret += w_ret;
  837. }
  838. while( FindNextFileW( hFind, &file_data ) != 0 );
  839. if( GetLastError() != ERROR_NO_MORE_FILES )
  840. ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
  841. FindClose( hFind );
  842. #else /* _WIN32 */
  843. int t_ret;
  844. struct stat sb;
  845. struct dirent *entry;
  846. char entry_name[255];
  847. DIR *dir = opendir( path );
  848. if( dir == NULL )
  849. return( POLARSSL_ERR_X509_FILE_IO_ERROR );
  850. #if defined(POLARSSL_THREADING_PTHREAD)
  851. if( ( ret = polarssl_mutex_lock( &readdir_mutex ) ) != 0 )
  852. return( ret );
  853. #endif
  854. while( ( entry = readdir( dir ) ) != NULL )
  855. {
  856. snprintf( entry_name, sizeof entry_name, "%s/%s", path, entry->d_name );
  857. if( stat( entry_name, &sb ) == -1 )
  858. {
  859. closedir( dir );
  860. ret = POLARSSL_ERR_X509_FILE_IO_ERROR;
  861. goto cleanup;
  862. }
  863. if( !S_ISREG( sb.st_mode ) )
  864. continue;
  865. // Ignore parse errors
  866. //
  867. t_ret = x509_crt_parse_file( chain, entry_name );
  868. if( t_ret < 0 )
  869. ret++;
  870. else
  871. ret += t_ret;
  872. }
  873. closedir( dir );
  874. cleanup:
  875. #if defined(POLARSSL_THREADING_PTHREAD)
  876. if( polarssl_mutex_unlock( &readdir_mutex ) != 0 )
  877. ret = POLARSSL_ERR_THREADING_MUTEX_ERROR;
  878. #endif
  879. #endif /* _WIN32 */
  880. return( ret );
  881. }
  882. #endif /* POLARSSL_FS_IO */
  883. #if defined(_MSC_VER) && !defined snprintf && !defined(EFIX64) && \
  884. !defined(EFI32)
  885. #include <stdarg.h>
  886. #if !defined vsnprintf
  887. #define vsnprintf _vsnprintf
  888. #endif // vsnprintf
  889. /*
  890. * Windows _snprintf and _vsnprintf are not compatible to linux versions.
  891. * Result value is not size of buffer needed, but -1 if no fit is possible.
  892. *
  893. * This fuction tries to 'fix' this by at least suggesting enlarging the
  894. * size by 20.
  895. */
  896. static int compat_snprintf( char *str, size_t size, const char *format, ... )
  897. {
  898. va_list ap;
  899. int res = -1;
  900. va_start( ap, format );
  901. res = vsnprintf( str, size, format, ap );
  902. va_end( ap );
  903. // No quick fix possible
  904. if( res < 0 )
  905. return( (int) size + 20 );
  906. return( res );
  907. }
  908. #define snprintf compat_snprintf
  909. #endif /* _MSC_VER && !snprintf && !EFIX64 && !EFI32 */
  910. #define POLARSSL_ERR_DEBUG_BUF_TOO_SMALL -2
  911. #define SAFE_SNPRINTF() \
  912. { \
  913. if( ret == -1 ) \
  914. return( -1 ); \
  915. \
  916. if( (unsigned int) ret > n ) { \
  917. p[n - 1] = '\0'; \
  918. return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL ); \
  919. } \
  920. \
  921. n -= (unsigned int) ret; \
  922. p += (unsigned int) ret; \
  923. }
  924. static int x509_info_subject_alt_name( char **buf, size_t *size,
  925. const x509_sequence *subject_alt_name )
  926. {
  927. size_t i;
  928. size_t n = *size;
  929. char *p = *buf;
  930. const x509_sequence *cur = subject_alt_name;
  931. const char *sep = "";
  932. size_t sep_len = 0;
  933. while( cur != NULL )
  934. {
  935. if( cur->buf.len + sep_len >= n )
  936. {
  937. *p = '\0';
  938. return( POLARSSL_ERR_DEBUG_BUF_TOO_SMALL );
  939. }
  940. n -= cur->buf.len + sep_len;
  941. for( i = 0; i < sep_len; i++ )
  942. *p++ = sep[i];
  943. for( i = 0; i < cur->buf.len; i++ )
  944. *p++ = cur->buf.p[i];
  945. sep = ", ";
  946. sep_len = 2;
  947. cur = cur->next;
  948. }
  949. *p = '\0';
  950. *size = n;
  951. *buf = p;
  952. return( 0 );
  953. }
  954. #define PRINT_ITEM(i) \
  955. { \
  956. ret = snprintf( p, n, "%s" i, sep ); \
  957. SAFE_SNPRINTF(); \
  958. sep = ", "; \
  959. }
  960. #define CERT_TYPE(type,name) \
  961. if( ns_cert_type & type ) \
  962. PRINT_ITEM( name );
  963. static int x509_info_cert_type( char **buf, size_t *size,
  964. unsigned char ns_cert_type )
  965. {
  966. int ret;
  967. size_t n = *size;
  968. char *p = *buf;
  969. const char *sep = "";
  970. CERT_TYPE( NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
  971. CERT_TYPE( NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
  972. CERT_TYPE( NS_CERT_TYPE_EMAIL, "Email" );
  973. CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
  974. CERT_TYPE( NS_CERT_TYPE_RESERVED, "Reserved" );
  975. CERT_TYPE( NS_CERT_TYPE_SSL_CA, "SSL CA" );
  976. CERT_TYPE( NS_CERT_TYPE_EMAIL_CA, "Email CA" );
  977. CERT_TYPE( NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
  978. *size = n;
  979. *buf = p;
  980. return( 0 );
  981. }
  982. #define KEY_USAGE(code,name) \
  983. if( key_usage & code ) \
  984. PRINT_ITEM( name );
  985. static int x509_info_key_usage( char **buf, size_t *size,
  986. unsigned char key_usage )
  987. {
  988. int ret;
  989. size_t n = *size;
  990. char *p = *buf;
  991. const char *sep = "";
  992. KEY_USAGE( KU_DIGITAL_SIGNATURE, "Digital Signature" );
  993. KEY_USAGE( KU_NON_REPUDIATION, "Non Repudiation" );
  994. KEY_USAGE( KU_KEY_ENCIPHERMENT, "Key Encipherment" );
  995. KEY_USAGE( KU_DATA_ENCIPHERMENT, "Data Encipherment" );
  996. KEY_USAGE( KU_KEY_AGREEMENT, "Key Agreement" );
  997. KEY_USAGE( KU_KEY_CERT_SIGN, "Key Cert Sign" );
  998. KEY_USAGE( KU_CRL_SIGN, "CRL Sign" );
  999. *size = n;
  1000. *buf = p;
  1001. return( 0 );
  1002. }
  1003. static int x509_info_ext_key_usage( char **buf, size_t *size,
  1004. const x509_sequence *extended_key_usage )
  1005. {
  1006. int ret;
  1007. const char *desc;
  1008. size_t n = *size;
  1009. char *p = *buf;
  1010. const x509_sequence *cur = extended_key_usage;
  1011. const char *sep = "";
  1012. while( cur != NULL )
  1013. {
  1014. if( oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
  1015. desc = "???";
  1016. ret = snprintf( p, n, "%s%s", sep, desc );
  1017. SAFE_SNPRINTF();
  1018. sep = ", ";
  1019. cur = cur->next;
  1020. }
  1021. *size = n;
  1022. *buf = p;
  1023. return( 0 );
  1024. }
  1025. /*
  1026. * Return an informational string about the certificate.
  1027. */
  1028. #define BEFORE_COLON 18
  1029. #define BC "18"
  1030. int x509_crt_info( char *buf, size_t size, const char *prefix,
  1031. const x509_crt *crt )
  1032. {
  1033. int ret;
  1034. size_t n;
  1035. char *p;
  1036. char key_size_str[BEFORE_COLON];
  1037. p = buf;
  1038. n = size;
  1039. ret = snprintf( p, n, "%scert. version : %d\n",
  1040. prefix, crt->version );
  1041. SAFE_SNPRINTF();
  1042. ret = snprintf( p, n, "%sserial number : ",
  1043. prefix );
  1044. SAFE_SNPRINTF();
  1045. ret = x509_serial_gets( p, n, &crt->serial );
  1046. SAFE_SNPRINTF();
  1047. ret = snprintf( p, n, "\n%sissuer name : ", prefix );
  1048. SAFE_SNPRINTF();
  1049. ret = x509_dn_gets( p, n, &crt->issuer );
  1050. SAFE_SNPRINTF();
  1051. ret = snprintf( p, n, "\n%ssubject name : ", prefix );
  1052. SAFE_SNPRINTF();
  1053. ret = x509_dn_gets( p, n, &crt->subject );
  1054. SAFE_SNPRINTF();
  1055. ret = snprintf( p, n, "\n%sissued on : " \
  1056. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1057. crt->valid_from.year, crt->valid_from.mon,
  1058. crt->valid_from.day, crt->valid_from.hour,
  1059. crt->valid_from.min, crt->valid_from.sec );
  1060. SAFE_SNPRINTF();
  1061. ret = snprintf( p, n, "\n%sexpires on : " \
  1062. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1063. crt->valid_to.year, crt->valid_to.mon,
  1064. crt->valid_to.day, crt->valid_to.hour,
  1065. crt->valid_to.min, crt->valid_to.sec );
  1066. SAFE_SNPRINTF();
  1067. ret = snprintf( p, n, "\n%ssigned using : ", prefix );
  1068. SAFE_SNPRINTF();
  1069. ret = x509_sig_alg_gets( p, n, &crt->sig_oid1, crt->sig_pk,
  1070. crt->sig_md, crt->sig_opts );
  1071. SAFE_SNPRINTF();
  1072. /* Key size */
  1073. if( ( ret = x509_key_size_helper( key_size_str, BEFORE_COLON,
  1074. pk_get_name( &crt->pk ) ) ) != 0 )
  1075. {
  1076. return( ret );
  1077. }
  1078. ret = snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
  1079. (int) pk_get_size( &crt->pk ) );
  1080. SAFE_SNPRINTF();
  1081. /*
  1082. * Optional extensions
  1083. */
  1084. if( crt->ext_types & EXT_BASIC_CONSTRAINTS )
  1085. {
  1086. ret = snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
  1087. crt->ca_istrue ? "true" : "false" );
  1088. SAFE_SNPRINTF();
  1089. if( crt->max_pathlen > 0 )
  1090. {
  1091. ret = snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
  1092. SAFE_SNPRINTF();
  1093. }
  1094. }
  1095. if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
  1096. {
  1097. ret = snprintf( p, n, "\n%ssubject alt name : ", prefix );
  1098. SAFE_SNPRINTF();
  1099. if( ( ret = x509_info_subject_alt_name( &p, &n,
  1100. &crt->subject_alt_names ) ) != 0 )
  1101. return( ret );
  1102. }
  1103. if( crt->ext_types & EXT_NS_CERT_TYPE )
  1104. {
  1105. ret = snprintf( p, n, "\n%scert. type : ", prefix );
  1106. SAFE_SNPRINTF();
  1107. if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
  1108. return( ret );
  1109. }
  1110. if( crt->ext_types & EXT_KEY_USAGE )
  1111. {
  1112. ret = snprintf( p, n, "\n%skey usage : ", prefix );
  1113. SAFE_SNPRINTF();
  1114. if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
  1115. return( ret );
  1116. }
  1117. if( crt->ext_types & EXT_EXTENDED_KEY_USAGE )
  1118. {
  1119. ret = snprintf( p, n, "\n%sext key usage : ", prefix );
  1120. SAFE_SNPRINTF();
  1121. if( ( ret = x509_info_ext_key_usage( &p, &n,
  1122. &crt->ext_key_usage ) ) != 0 )
  1123. return( ret );
  1124. }
  1125. ret = snprintf( p, n, "\n" );
  1126. SAFE_SNPRINTF();
  1127. return( (int) ( size - n ) );
  1128. }
  1129. #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
  1130. int x509_crt_check_key_usage( const x509_crt *crt, int usage )
  1131. {
  1132. if( ( crt->ext_types & EXT_KEY_USAGE ) != 0 &&
  1133. ( crt->key_usage & usage ) != usage )
  1134. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  1135. return( 0 );
  1136. }
  1137. #endif
  1138. #if defined(POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE)
  1139. int x509_crt_check_extended_key_usage( const x509_crt *crt,
  1140. const char *usage_oid,
  1141. size_t usage_len )
  1142. {
  1143. const x509_sequence *cur;
  1144. /* Extension is not mandatory, absent means no restriction */
  1145. if( ( crt->ext_types & EXT_EXTENDED_KEY_USAGE ) == 0 )
  1146. return( 0 );
  1147. /*
  1148. * Look for the requested usage (or wildcard ANY) in our list
  1149. */
  1150. for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
  1151. {
  1152. const x509_buf *cur_oid = &cur->buf;
  1153. if( cur_oid->len == usage_len &&
  1154. memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
  1155. {
  1156. return( 0 );
  1157. }
  1158. if( OID_CMP( OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) )
  1159. return( 0 );
  1160. }
  1161. return( POLARSSL_ERR_X509_BAD_INPUT_DATA );
  1162. }
  1163. #endif /* POLARSSL_X509_CHECK_EXTENDED_KEY_USAGE */
  1164. #if defined(POLARSSL_X509_CRL_PARSE_C)
  1165. /*
  1166. * Return 1 if the certificate is revoked, or 0 otherwise.
  1167. */
  1168. int x509_crt_revoked( const x509_crt *crt, const x509_crl *crl )
  1169. {
  1170. const x509_crl_entry *cur = &crl->entry;
  1171. while( cur != NULL && cur->serial.len != 0 )
  1172. {
  1173. if( crt->serial.len == cur->serial.len &&
  1174. memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
  1175. {
  1176. if( x509_time_expired( &cur->revocation_date ) )
  1177. return( 1 );
  1178. }
  1179. cur = cur->next;
  1180. }
  1181. return( 0 );
  1182. }
  1183. /*
  1184. * Check that the given certificate is valid according to the CRL.
  1185. */
  1186. static int x509_crt_verifycrl( x509_crt *crt, x509_crt *ca,
  1187. x509_crl *crl_list)
  1188. {
  1189. int flags = 0;
  1190. unsigned char hash[POLARSSL_MD_MAX_SIZE];
  1191. const md_info_t *md_info;
  1192. if( ca == NULL )
  1193. return( flags );
  1194. /*
  1195. * TODO: What happens if no CRL is present?
  1196. * Suggestion: Revocation state should be unknown if no CRL is present.
  1197. * For backwards compatibility this is not yet implemented.
  1198. */
  1199. while( crl_list != NULL )
  1200. {
  1201. if( crl_list->version == 0 ||
  1202. crl_list->issuer_raw.len != ca->subject_raw.len ||
  1203. memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
  1204. crl_list->issuer_raw.len ) != 0 )
  1205. {
  1206. crl_list = crl_list->next;
  1207. continue;
  1208. }
  1209. /*
  1210. * Check if the CA is configured to sign CRLs
  1211. */
  1212. #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
  1213. if( x509_crt_check_key_usage( ca, KU_CRL_SIGN ) != 0 )
  1214. {
  1215. flags |= BADCRL_NOT_TRUSTED;
  1216. break;
  1217. }
  1218. #endif
  1219. /*
  1220. * Check if CRL is correctly signed by the trusted CA
  1221. */
  1222. md_info = md_info_from_type( crl_list->sig_md );
  1223. if( md_info == NULL )
  1224. {
  1225. /*
  1226. * Cannot check 'unknown' hash
  1227. */
  1228. flags |= BADCRL_NOT_TRUSTED;
  1229. break;
  1230. }
  1231. md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
  1232. if( pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
  1233. crl_list->sig_md, hash, md_info->size,
  1234. crl_list->sig.p, crl_list->sig.len ) != 0 )
  1235. {
  1236. flags |= BADCRL_NOT_TRUSTED;
  1237. break;
  1238. }
  1239. /*
  1240. * Check for validity of CRL (Do not drop out)
  1241. */
  1242. if( x509_time_expired( &crl_list->next_update ) )
  1243. flags |= BADCRL_EXPIRED;
  1244. if( x509_time_future( &crl_list->this_update ) )
  1245. flags |= BADCRL_FUTURE;
  1246. /*
  1247. * Check if certificate is revoked
  1248. */
  1249. if( x509_crt_revoked( crt, crl_list ) )
  1250. {
  1251. flags |= BADCERT_REVOKED;
  1252. break;
  1253. }
  1254. crl_list = crl_list->next;
  1255. }
  1256. return( flags );
  1257. }
  1258. #endif /* POLARSSL_X509_CRL_PARSE_C */
  1259. // Equal == 0, inequal == 1
  1260. static int x509_name_cmp( const void *s1, const void *s2, size_t len )
  1261. {
  1262. size_t i;
  1263. unsigned char diff;
  1264. const unsigned char *n1 = s1, *n2 = s2;
  1265. for( i = 0; i < len; i++ )
  1266. {
  1267. diff = n1[i] ^ n2[i];
  1268. if( diff == 0 )
  1269. continue;
  1270. if( diff == 32 &&
  1271. ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
  1272. ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
  1273. {
  1274. continue;
  1275. }
  1276. return( 1 );
  1277. }
  1278. return( 0 );
  1279. }
  1280. static int x509_wildcard_verify( const char *cn, x509_buf *name )
  1281. {
  1282. size_t i;
  1283. size_t cn_idx = 0, cn_len = strlen( cn );
  1284. if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
  1285. return( 0 );
  1286. for( i = 0; i < cn_len; ++i )
  1287. {
  1288. if( cn[i] == '.' )
  1289. {
  1290. cn_idx = i;
  1291. break;
  1292. }
  1293. }
  1294. if( cn_idx == 0 )
  1295. return( 0 );
  1296. if( cn_len - cn_idx == name->len - 1 &&
  1297. x509_name_cmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
  1298. {
  1299. return( 1 );
  1300. }
  1301. return( 0 );
  1302. }
  1303. /*
  1304. * Check if 'parent' is a suitable parent (signing CA) for 'child'.
  1305. * Return 0 if yes, -1 if not.
  1306. *
  1307. * top means parent is a locally-trusted certificate
  1308. * bottom means child is the end entity cert
  1309. */
  1310. static int x509_crt_check_parent( const x509_crt *child,
  1311. const x509_crt *parent,
  1312. int top, int bottom )
  1313. {
  1314. int need_ca_bit;
  1315. /* Parent must be the issuer */
  1316. if( child->issuer_raw.len != parent->subject_raw.len ||
  1317. memcmp( child->issuer_raw.p, parent->subject_raw.p,
  1318. child->issuer_raw.len ) != 0 )
  1319. {
  1320. return( -1 );
  1321. }
  1322. /* Parent must have the basicConstraints CA bit set as a general rule */
  1323. need_ca_bit = 1;
  1324. /* Exception: v1/v2 certificates that are locally trusted. */
  1325. if( top && parent->version < 3 )
  1326. need_ca_bit = 0;
  1327. /* Exception: self-signed end-entity certs that are locally trusted. */
  1328. if( top && bottom &&
  1329. child->raw.len == parent->raw.len &&
  1330. memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
  1331. {
  1332. need_ca_bit = 0;
  1333. }
  1334. if( need_ca_bit && ! parent->ca_istrue )
  1335. return( -1 );
  1336. #if defined(POLARSSL_X509_CHECK_KEY_USAGE)
  1337. if( need_ca_bit &&
  1338. x509_crt_check_key_usage( parent, KU_KEY_CERT_SIGN ) != 0 )
  1339. {
  1340. return( -1 );
  1341. }
  1342. #endif
  1343. return( 0 );
  1344. }
  1345. static int x509_crt_verify_top(
  1346. x509_crt *child, x509_crt *trust_ca,
  1347. x509_crl *ca_crl, int path_cnt, int *flags,
  1348. int (*f_vrfy)(void *, x509_crt *, int, int *),
  1349. void *p_vrfy )
  1350. {
  1351. int ret;
  1352. int ca_flags = 0, check_path_cnt = path_cnt + 1;
  1353. unsigned char hash[POLARSSL_MD_MAX_SIZE];
  1354. const md_info_t *md_info;
  1355. if( x509_time_expired( &child->valid_to ) )
  1356. *flags |= BADCERT_EXPIRED;
  1357. if( x509_time_future( &child->valid_from ) )
  1358. *flags |= BADCERT_FUTURE;
  1359. /*
  1360. * Child is the top of the chain. Check against the trust_ca list.
  1361. */
  1362. *flags |= BADCERT_NOT_TRUSTED;
  1363. md_info = md_info_from_type( child->sig_md );
  1364. if( md_info == NULL )
  1365. {
  1366. /*
  1367. * Cannot check 'unknown', no need to try any CA
  1368. */
  1369. trust_ca = NULL;
  1370. }
  1371. else
  1372. md( md_info, child->tbs.p, child->tbs.len, hash );
  1373. for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
  1374. {
  1375. if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
  1376. continue;
  1377. /*
  1378. * Reduce path_len to check against if top of the chain is
  1379. * the same as the trusted CA
  1380. */
  1381. if( child->subject_raw.len == trust_ca->subject_raw.len &&
  1382. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  1383. child->issuer_raw.len ) == 0 )
  1384. {
  1385. check_path_cnt--;
  1386. }
  1387. if( trust_ca->max_pathlen > 0 &&
  1388. trust_ca->max_pathlen < check_path_cnt )
  1389. {
  1390. continue;
  1391. }
  1392. if( pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
  1393. child->sig_md, hash, md_info->size,
  1394. child->sig.p, child->sig.len ) != 0 )
  1395. {
  1396. continue;
  1397. }
  1398. /*
  1399. * Top of chain is signed by a trusted CA
  1400. */
  1401. *flags &= ~BADCERT_NOT_TRUSTED;
  1402. break;
  1403. }
  1404. /*
  1405. * If top of chain is not the same as the trusted CA send a verify request
  1406. * to the callback for any issues with validity and CRL presence for the
  1407. * trusted CA certificate.
  1408. */
  1409. if( trust_ca != NULL &&
  1410. ( child->subject_raw.len != trust_ca->subject_raw.len ||
  1411. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  1412. child->issuer_raw.len ) != 0 ) )
  1413. {
  1414. #if defined(POLARSSL_X509_CRL_PARSE_C)
  1415. /* Check trusted CA's CRL for the chain's top crt */
  1416. *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl );
  1417. #else
  1418. ((void) ca_crl);
  1419. #endif
  1420. if( x509_time_expired( &trust_ca->valid_to ) )
  1421. ca_flags |= BADCERT_EXPIRED;
  1422. if( x509_time_future( &trust_ca->valid_from ) )
  1423. ca_flags |= BADCERT_FUTURE;
  1424. if( NULL != f_vrfy )
  1425. {
  1426. if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
  1427. &ca_flags ) ) != 0 )
  1428. {
  1429. return( ret );
  1430. }
  1431. }
  1432. }
  1433. /* Call callback on top cert */
  1434. if( NULL != f_vrfy )
  1435. {
  1436. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  1437. return( ret );
  1438. }
  1439. *flags |= ca_flags;
  1440. return( 0 );
  1441. }
  1442. static int x509_crt_verify_child(
  1443. x509_crt *child, x509_crt *parent, x509_crt *trust_ca,
  1444. x509_crl *ca_crl, int path_cnt, int *flags,
  1445. int (*f_vrfy)(void *, x509_crt *, int, int *),
  1446. void *p_vrfy )
  1447. {
  1448. int ret;
  1449. int parent_flags = 0;
  1450. unsigned char hash[POLARSSL_MD_MAX_SIZE];
  1451. x509_crt *grandparent;
  1452. const md_info_t *md_info;
  1453. if( x509_time_expired( &child->valid_to ) )
  1454. *flags |= BADCERT_EXPIRED;
  1455. if( x509_time_future( &child->valid_from ) )
  1456. *flags |= BADCERT_FUTURE;
  1457. md_info = md_info_from_type( child->sig_md );
  1458. if( md_info == NULL )
  1459. {
  1460. /*
  1461. * Cannot check 'unknown' hash
  1462. */
  1463. *flags |= BADCERT_NOT_TRUSTED;
  1464. }
  1465. else
  1466. {
  1467. md( md_info, child->tbs.p, child->tbs.len, hash );
  1468. if( pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
  1469. child->sig_md, hash, md_info->size,
  1470. child->sig.p, child->sig.len ) != 0 )
  1471. {
  1472. *flags |= BADCERT_NOT_TRUSTED;
  1473. }
  1474. }
  1475. #if defined(POLARSSL_X509_CRL_PARSE_C)
  1476. /* Check trusted CA's CRL for the given crt */
  1477. *flags |= x509_crt_verifycrl(child, parent, ca_crl);
  1478. #endif
  1479. /* Look for a grandparent upwards the chain */
  1480. for( grandparent = parent->next;
  1481. grandparent != NULL;
  1482. grandparent = grandparent->next )
  1483. {
  1484. if( x509_crt_check_parent( parent, grandparent,
  1485. 0, path_cnt == 0 ) == 0 )
  1486. break;
  1487. }
  1488. /* Is our parent part of the chain or at the top? */
  1489. if( grandparent != NULL )
  1490. {
  1491. ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
  1492. path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
  1493. if( ret != 0 )
  1494. return( ret );
  1495. }
  1496. else
  1497. {
  1498. ret = x509_crt_verify_top( parent, trust_ca, ca_crl,
  1499. path_cnt + 1, &parent_flags, f_vrfy, p_vrfy );
  1500. if( ret != 0 )
  1501. return( ret );
  1502. }
  1503. /* child is verified to be a child of the parent, call verify callback */
  1504. if( NULL != f_vrfy )
  1505. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  1506. return( ret );
  1507. *flags |= parent_flags;
  1508. return( 0 );
  1509. }
  1510. /*
  1511. * Verify the certificate validity
  1512. */
  1513. int x509_crt_verify( x509_crt *crt,
  1514. x509_crt *trust_ca,
  1515. x509_crl *ca_crl,
  1516. const char *cn, int *flags,
  1517. int (*f_vrfy)(void *, x509_crt *, int, int *),
  1518. void *p_vrfy )
  1519. {
  1520. size_t cn_len;
  1521. int ret;
  1522. int pathlen = 0;
  1523. x509_crt *parent;
  1524. x509_name *name;
  1525. x509_sequence *cur = NULL;
  1526. *flags = 0;
  1527. if( cn != NULL )
  1528. {
  1529. name = &crt->subject;
  1530. cn_len = strlen( cn );
  1531. if( crt->ext_types & EXT_SUBJECT_ALT_NAME )
  1532. {
  1533. cur = &crt->subject_alt_names;
  1534. while( cur != NULL )
  1535. {
  1536. if( cur->buf.len == cn_len &&
  1537. x509_name_cmp( cn, cur->buf.p, cn_len ) == 0 )
  1538. break;
  1539. if( cur->buf.len > 2 &&
  1540. memcmp( cur->buf.p, "*.", 2 ) == 0 &&
  1541. x509_wildcard_verify( cn, &cur->buf ) )
  1542. break;
  1543. cur = cur->next;
  1544. }
  1545. if( cur == NULL )
  1546. *flags |= BADCERT_CN_MISMATCH;
  1547. }
  1548. else
  1549. {
  1550. while( name != NULL )
  1551. {
  1552. if( OID_CMP( OID_AT_CN, &name->oid ) )
  1553. {
  1554. if( name->val.len == cn_len &&
  1555. x509_name_cmp( name->val.p, cn, cn_len ) == 0 )
  1556. break;
  1557. if( name->val.len > 2 &&
  1558. memcmp( name->val.p, "*.", 2 ) == 0 &&
  1559. x509_wildcard_verify( cn, &name->val ) )
  1560. break;
  1561. }
  1562. name = name->next;
  1563. }
  1564. if( name == NULL )
  1565. *flags |= BADCERT_CN_MISMATCH;
  1566. }
  1567. }
  1568. /* Look for a parent upwards the chain */
  1569. for( parent = crt->next; parent != NULL; parent = parent->next )
  1570. {
  1571. if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
  1572. break;
  1573. }
  1574. /* Are we part of the chain or at the top? */
  1575. if( parent != NULL )
  1576. {
  1577. ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl,
  1578. pathlen, flags, f_vrfy, p_vrfy );
  1579. if( ret != 0 )
  1580. return( ret );
  1581. }
  1582. else
  1583. {
  1584. ret = x509_crt_verify_top( crt, trust_ca, ca_crl,
  1585. pathlen, flags, f_vrfy, p_vrfy );
  1586. if( ret != 0 )
  1587. return( ret );
  1588. }
  1589. if( *flags != 0 )
  1590. return( POLARSSL_ERR_X509_CERT_VERIFY_FAILED );
  1591. return( 0 );
  1592. }
  1593. /*
  1594. * Initialize a certificate chain
  1595. */
  1596. void x509_crt_init( x509_crt *crt )
  1597. {
  1598. memset( crt, 0, sizeof(x509_crt) );
  1599. }
  1600. /*
  1601. * Unallocate all certificate data
  1602. */
  1603. void x509_crt_free( x509_crt *crt )
  1604. {
  1605. x509_crt *cert_cur = crt;
  1606. x509_crt *cert_prv;
  1607. x509_name *name_cur;
  1608. x509_name *name_prv;
  1609. x509_sequence *seq_cur;
  1610. x509_sequence *seq_prv;
  1611. if( crt == NULL )
  1612. return;
  1613. do
  1614. {
  1615. pk_free( &cert_cur->pk );
  1616. #if defined(POLARSSL_X509_RSASSA_PSS_SUPPORT)
  1617. polarssl_free( cert_cur->sig_opts );
  1618. #endif
  1619. name_cur = cert_cur->issuer.next;
  1620. while( name_cur != NULL )
  1621. {
  1622. name_prv = name_cur;
  1623. name_cur = name_cur->next;
  1624. polarssl_zeroize( name_prv, sizeof( x509_name ) );
  1625. polarssl_free( name_prv );
  1626. }
  1627. name_cur = cert_cur->subject.next;
  1628. while( name_cur != NULL )
  1629. {
  1630. name_prv = name_cur;
  1631. name_cur = name_cur->next;
  1632. polarssl_zeroize( name_prv, sizeof( x509_name ) );
  1633. polarssl_free( name_prv );
  1634. }
  1635. seq_cur = cert_cur->ext_key_usage.next;
  1636. while( seq_cur != NULL )
  1637. {
  1638. seq_prv = seq_cur;
  1639. seq_cur = seq_cur->next;
  1640. polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
  1641. polarssl_free( seq_prv );
  1642. }
  1643. seq_cur = cert_cur->subject_alt_names.next;
  1644. while( seq_cur != NULL )
  1645. {
  1646. seq_prv = seq_cur;
  1647. seq_cur = seq_cur->next;
  1648. polarssl_zeroize( seq_prv, sizeof( x509_sequence ) );
  1649. polarssl_free( seq_prv );
  1650. }
  1651. if( cert_cur->raw.p != NULL )
  1652. {
  1653. polarssl_zeroize( cert_cur->raw.p, cert_cur->raw.len );
  1654. polarssl_free( cert_cur->raw.p );
  1655. }
  1656. cert_cur = cert_cur->next;
  1657. }
  1658. while( cert_cur != NULL );
  1659. cert_cur = crt;
  1660. do
  1661. {
  1662. cert_prv = cert_cur;
  1663. cert_cur = cert_cur->next;
  1664. polarssl_zeroize( cert_prv, sizeof( x509_crt ) );
  1665. if( cert_prv != crt )
  1666. polarssl_free( cert_prv );
  1667. }
  1668. while( cert_cur != NULL );
  1669. }
  1670. #endif /* POLARSSL_X509_CRT_PARSE_C */