x509_crt.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /*
  2. * X.509 certificate parsing and verification
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. /*
  24. * The ITU-T X.509 standard defines a certificate format for PKI.
  25. *
  26. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  27. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  28. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  29. *
  30. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  31. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  32. */
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "mbedtls/config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  39. #include "mbedtls/x509_crt.h"
  40. #include "mbedtls/oid.h"
  41. #include <stdio.h>
  42. #include <string.h>
  43. #if defined(MBEDTLS_PEM_PARSE_C)
  44. #include "mbedtls/pem.h"
  45. #endif
  46. #if defined(MBEDTLS_PLATFORM_C)
  47. #include "mbedtls/platform.h"
  48. #else
  49. #include <stdlib.h>
  50. #define mbedtls_free free
  51. #define mbedtls_calloc calloc
  52. #define mbedtls_snprintf snprintf
  53. #endif
  54. #if defined(MBEDTLS_THREADING_C)
  55. #include "mbedtls/threading.h"
  56. #endif
  57. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  58. #include <windows.h>
  59. #else
  60. #include <time.h>
  61. #endif
  62. #if defined(MBEDTLS_FS_IO)
  63. #include <stdio.h>
  64. #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #include <dirent.h>
  68. #endif /* !_WIN32 || EFIX64 || EFI32 */
  69. #endif
  70. /* Implementation that should never be optimized out by the compiler */
  71. static void mbedtls_zeroize( void *v, size_t n ) {
  72. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  73. }
  74. /*
  75. * Default profile
  76. */
  77. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
  78. {
  79. /* Hashes from SHA-1 and above */
  80. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 ) |
  81. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_RIPEMD160 ) |
  82. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA224 ) |
  83. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  84. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  85. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  86. 0xFFFFFFF, /* Any PK alg */
  87. 0xFFFFFFF, /* Any curve */
  88. 2048,
  89. };
  90. /*
  91. * Next-default profile
  92. */
  93. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
  94. {
  95. /* Hashes from SHA-256 and above */
  96. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  97. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  98. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  99. 0xFFFFFFF, /* Any PK alg */
  100. #if defined(MBEDTLS_ECP_C)
  101. /* Curves at or above 128-bit security level */
  102. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  103. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
  104. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
  105. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
  106. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
  107. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
  108. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
  109. #else
  110. 0,
  111. #endif
  112. 2048,
  113. };
  114. /*
  115. * NSA Suite B Profile
  116. */
  117. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
  118. {
  119. /* Only SHA-256 and 384 */
  120. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  121. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
  122. /* Only ECDSA */
  123. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ),
  124. #if defined(MBEDTLS_ECP_C)
  125. /* Only NIST P-256 and P-384 */
  126. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  127. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
  128. #else
  129. 0,
  130. #endif
  131. 0,
  132. };
  133. /*
  134. * Check md_alg against profile
  135. * Return 0 if md_alg acceptable for this profile, -1 otherwise
  136. */
  137. static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
  138. mbedtls_md_type_t md_alg )
  139. {
  140. if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
  141. return( 0 );
  142. return( -1 );
  143. }
  144. /*
  145. * Check pk_alg against profile
  146. * Return 0 if pk_alg acceptable for this profile, -1 otherwise
  147. */
  148. static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
  149. mbedtls_pk_type_t pk_alg )
  150. {
  151. if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
  152. return( 0 );
  153. return( -1 );
  154. }
  155. /*
  156. * Check key against profile
  157. * Return 0 if pk_alg acceptable for this profile, -1 otherwise
  158. */
  159. static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
  160. mbedtls_pk_type_t pk_alg,
  161. const mbedtls_pk_context *pk )
  162. {
  163. #if defined(MBEDTLS_RSA_C)
  164. if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
  165. {
  166. if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
  167. return( 0 );
  168. return( -1 );
  169. }
  170. #endif
  171. #if defined(MBEDTLS_ECP_C)
  172. if( pk_alg == MBEDTLS_PK_ECDSA ||
  173. pk_alg == MBEDTLS_PK_ECKEY ||
  174. pk_alg == MBEDTLS_PK_ECKEY_DH )
  175. {
  176. mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
  177. if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
  178. return( 0 );
  179. return( -1 );
  180. }
  181. #endif
  182. return( -1 );
  183. }
  184. /*
  185. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  186. */
  187. static int x509_get_version( unsigned char **p,
  188. const unsigned char *end,
  189. int *ver )
  190. {
  191. int ret;
  192. size_t len;
  193. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  194. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
  195. {
  196. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  197. {
  198. *ver = 0;
  199. return( 0 );
  200. }
  201. return( ret );
  202. }
  203. end = *p + len;
  204. if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
  205. return( MBEDTLS_ERR_X509_INVALID_VERSION + ret );
  206. if( *p != end )
  207. return( MBEDTLS_ERR_X509_INVALID_VERSION +
  208. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  209. return( 0 );
  210. }
  211. /*
  212. * Validity ::= SEQUENCE {
  213. * notBefore Time,
  214. * notAfter Time }
  215. */
  216. static int x509_get_dates( unsigned char **p,
  217. const unsigned char *end,
  218. mbedtls_x509_time *from,
  219. mbedtls_x509_time *to )
  220. {
  221. int ret;
  222. size_t len;
  223. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  224. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  225. return( MBEDTLS_ERR_X509_INVALID_DATE + ret );
  226. end = *p + len;
  227. if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
  228. return( ret );
  229. if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
  230. return( ret );
  231. if( *p != end )
  232. return( MBEDTLS_ERR_X509_INVALID_DATE +
  233. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  234. return( 0 );
  235. }
  236. /*
  237. * X.509 v2/v3 unique identifier (not parsed)
  238. */
  239. static int x509_get_uid( unsigned char **p,
  240. const unsigned char *end,
  241. mbedtls_x509_buf *uid, int n )
  242. {
  243. int ret;
  244. if( *p == end )
  245. return( 0 );
  246. uid->tag = **p;
  247. if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
  248. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
  249. {
  250. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  251. return( 0 );
  252. return( ret );
  253. }
  254. uid->p = *p;
  255. *p += uid->len;
  256. return( 0 );
  257. }
  258. static int x509_get_basic_constraints( unsigned char **p,
  259. const unsigned char *end,
  260. int *ca_istrue,
  261. int *max_pathlen )
  262. {
  263. int ret;
  264. size_t len;
  265. /*
  266. * BasicConstraints ::= SEQUENCE {
  267. * cA BOOLEAN DEFAULT FALSE,
  268. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  269. */
  270. *ca_istrue = 0; /* DEFAULT FALSE */
  271. *max_pathlen = 0; /* endless */
  272. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  273. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  274. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  275. if( *p == end )
  276. return( 0 );
  277. if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
  278. {
  279. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  280. ret = mbedtls_asn1_get_int( p, end, ca_istrue );
  281. if( ret != 0 )
  282. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  283. if( *ca_istrue != 0 )
  284. *ca_istrue = 1;
  285. }
  286. if( *p == end )
  287. return( 0 );
  288. if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
  289. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  290. if( *p != end )
  291. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  292. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  293. (*max_pathlen)++;
  294. return( 0 );
  295. }
  296. static int x509_get_ns_cert_type( unsigned char **p,
  297. const unsigned char *end,
  298. unsigned char *ns_cert_type)
  299. {
  300. int ret;
  301. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  302. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  303. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  304. if( bs.len != 1 )
  305. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  306. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  307. /* Get actual bitstring */
  308. *ns_cert_type = *bs.p;
  309. return( 0 );
  310. }
  311. static int x509_get_key_usage( unsigned char **p,
  312. const unsigned char *end,
  313. unsigned int *key_usage)
  314. {
  315. int ret;
  316. size_t i;
  317. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  318. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  319. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  320. if( bs.len < 1 )
  321. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  322. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  323. /* Get actual bitstring */
  324. *key_usage = 0;
  325. for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
  326. {
  327. *key_usage |= (unsigned int) bs.p[i] << (8*i);
  328. }
  329. return( 0 );
  330. }
  331. /*
  332. * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
  333. *
  334. * KeyPurposeId ::= OBJECT IDENTIFIER
  335. */
  336. static int x509_get_ext_key_usage( unsigned char **p,
  337. const unsigned char *end,
  338. mbedtls_x509_sequence *ext_key_usage)
  339. {
  340. int ret;
  341. if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
  342. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  343. /* Sequence length must be >= 1 */
  344. if( ext_key_usage->buf.p == NULL )
  345. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  346. MBEDTLS_ERR_ASN1_INVALID_LENGTH );
  347. return( 0 );
  348. }
  349. /*
  350. * SubjectAltName ::= GeneralNames
  351. *
  352. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  353. *
  354. * GeneralName ::= CHOICE {
  355. * otherName [0] OtherName,
  356. * rfc822Name [1] IA5String,
  357. * dNSName [2] IA5String,
  358. * x400Address [3] ORAddress,
  359. * directoryName [4] Name,
  360. * ediPartyName [5] EDIPartyName,
  361. * uniformResourceIdentifier [6] IA5String,
  362. * iPAddress [7] OCTET STRING,
  363. * registeredID [8] OBJECT IDENTIFIER }
  364. *
  365. * OtherName ::= SEQUENCE {
  366. * type-id OBJECT IDENTIFIER,
  367. * value [0] EXPLICIT ANY DEFINED BY type-id }
  368. *
  369. * EDIPartyName ::= SEQUENCE {
  370. * nameAssigner [0] DirectoryString OPTIONAL,
  371. * partyName [1] DirectoryString }
  372. *
  373. * NOTE: we only parse and use dNSName at this point.
  374. */
  375. static int x509_get_subject_alt_name( unsigned char **p,
  376. const unsigned char *end,
  377. mbedtls_x509_sequence *subject_alt_name )
  378. {
  379. int ret;
  380. size_t len, tag_len;
  381. mbedtls_asn1_buf *buf;
  382. unsigned char tag;
  383. mbedtls_asn1_sequence *cur = subject_alt_name;
  384. /* Get main sequence tag */
  385. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  386. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  387. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  388. if( *p + len != end )
  389. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  390. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  391. while( *p < end )
  392. {
  393. if( ( end - *p ) < 1 )
  394. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  395. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  396. tag = **p;
  397. (*p)++;
  398. if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
  399. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  400. if( ( tag & MBEDTLS_ASN1_CONTEXT_SPECIFIC ) != MBEDTLS_ASN1_CONTEXT_SPECIFIC )
  401. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  402. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  403. /* Skip everything but DNS name */
  404. if( tag != ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | 2 ) )
  405. {
  406. *p += tag_len;
  407. continue;
  408. }
  409. /* Allocate and assign next pointer */
  410. if( cur->buf.p != NULL )
  411. {
  412. if( cur->next != NULL )
  413. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  414. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
  415. if( cur->next == NULL )
  416. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  417. MBEDTLS_ERR_ASN1_ALLOC_FAILED );
  418. cur = cur->next;
  419. }
  420. buf = &(cur->buf);
  421. buf->tag = tag;
  422. buf->p = *p;
  423. buf->len = tag_len;
  424. *p += buf->len;
  425. }
  426. /* Set final sequence entry's next pointer to NULL */
  427. cur->next = NULL;
  428. if( *p != end )
  429. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  430. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  431. return( 0 );
  432. }
  433. /*
  434. * X.509 v3 extensions
  435. *
  436. */
  437. static int x509_get_crt_ext( unsigned char **p,
  438. const unsigned char *end,
  439. mbedtls_x509_crt *crt )
  440. {
  441. int ret;
  442. size_t len;
  443. unsigned char *end_ext_data, *end_ext_octet;
  444. if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
  445. {
  446. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  447. return( 0 );
  448. return( ret );
  449. }
  450. while( *p < end )
  451. {
  452. /*
  453. * Extension ::= SEQUENCE {
  454. * extnID OBJECT IDENTIFIER,
  455. * critical BOOLEAN DEFAULT FALSE,
  456. * extnValue OCTET STRING }
  457. */
  458. mbedtls_x509_buf extn_oid = {0, 0, NULL};
  459. int is_critical = 0; /* DEFAULT FALSE */
  460. int ext_type = 0;
  461. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  462. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  463. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  464. end_ext_data = *p + len;
  465. /* Get extension ID */
  466. extn_oid.tag = **p;
  467. if( ( ret = mbedtls_asn1_get_tag( p, end, &extn_oid.len, MBEDTLS_ASN1_OID ) ) != 0 )
  468. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  469. extn_oid.p = *p;
  470. *p += extn_oid.len;
  471. if( ( end - *p ) < 1 )
  472. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  473. MBEDTLS_ERR_ASN1_OUT_OF_DATA );
  474. /* Get optional critical */
  475. if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
  476. ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  477. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  478. /* Data should be octet string type */
  479. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
  480. MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  481. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS + ret );
  482. end_ext_octet = *p + len;
  483. if( end_ext_octet != end_ext_data )
  484. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  485. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  486. /*
  487. * Detect supported extensions
  488. */
  489. ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
  490. if( ret != 0 )
  491. {
  492. /* No parser found, skip extension */
  493. *p = end_ext_octet;
  494. #if !defined(MBEDTLS_X509_ALLOW_UNSUPPORTED_CRITICAL_EXTENSION)
  495. if( is_critical )
  496. {
  497. /* Data is marked as critical: fail */
  498. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  499. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG );
  500. }
  501. #endif
  502. continue;
  503. }
  504. /* Forbid repeated extensions */
  505. if( ( crt->ext_types & ext_type ) != 0 )
  506. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  507. crt->ext_types |= ext_type;
  508. switch( ext_type )
  509. {
  510. case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
  511. /* Parse basic constraints */
  512. if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
  513. &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
  514. return( ret );
  515. break;
  516. case MBEDTLS_X509_EXT_KEY_USAGE:
  517. /* Parse key usage */
  518. if( ( ret = x509_get_key_usage( p, end_ext_octet,
  519. &crt->key_usage ) ) != 0 )
  520. return( ret );
  521. break;
  522. case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
  523. /* Parse extended key usage */
  524. if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
  525. &crt->ext_key_usage ) ) != 0 )
  526. return( ret );
  527. break;
  528. case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
  529. /* Parse subject alt name */
  530. if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
  531. &crt->subject_alt_names ) ) != 0 )
  532. return( ret );
  533. break;
  534. case MBEDTLS_X509_EXT_NS_CERT_TYPE:
  535. /* Parse netscape certificate type */
  536. if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
  537. &crt->ns_cert_type ) ) != 0 )
  538. return( ret );
  539. break;
  540. default:
  541. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  542. }
  543. }
  544. if( *p != end )
  545. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS +
  546. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  547. return( 0 );
  548. }
  549. /*
  550. * Parse and fill a single X.509 certificate in DER format
  551. */
  552. static int x509_crt_parse_der_core( mbedtls_x509_crt *crt, const unsigned char *buf,
  553. size_t buflen )
  554. {
  555. int ret;
  556. size_t len;
  557. unsigned char *p, *end, *crt_end;
  558. mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
  559. memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
  560. memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
  561. memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
  562. /*
  563. * Check for valid input
  564. */
  565. if( crt == NULL || buf == NULL )
  566. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  567. // Use the original buffer until we figure out actual length
  568. p = (unsigned char*) buf;
  569. len = buflen;
  570. end = p + len;
  571. /*
  572. * Certificate ::= SEQUENCE {
  573. * tbsCertificate TBSCertificate,
  574. * signatureAlgorithm AlgorithmIdentifier,
  575. * signatureValue BIT STRING }
  576. */
  577. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  578. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  579. {
  580. mbedtls_x509_crt_free( crt );
  581. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  582. }
  583. if( len > (size_t) ( end - p ) )
  584. {
  585. mbedtls_x509_crt_free( crt );
  586. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  587. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  588. }
  589. crt_end = p + len;
  590. // Create and populate a new buffer for the raw field
  591. crt->raw.len = crt_end - buf;
  592. crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
  593. if( p == NULL )
  594. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  595. memcpy( p, buf, crt->raw.len );
  596. // Direct pointers to the new buffer
  597. p += crt->raw.len - len;
  598. end = crt_end = p + len;
  599. /*
  600. * TBSCertificate ::= SEQUENCE {
  601. */
  602. crt->tbs.p = p;
  603. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  604. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  605. {
  606. mbedtls_x509_crt_free( crt );
  607. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  608. }
  609. end = p + len;
  610. crt->tbs.len = end - crt->tbs.p;
  611. /*
  612. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  613. *
  614. * CertificateSerialNumber ::= INTEGER
  615. *
  616. * signature AlgorithmIdentifier
  617. */
  618. if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
  619. ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
  620. ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
  621. &sig_params1 ) ) != 0 )
  622. {
  623. mbedtls_x509_crt_free( crt );
  624. return( ret );
  625. }
  626. crt->version++;
  627. if( crt->version > 3 )
  628. {
  629. mbedtls_x509_crt_free( crt );
  630. return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  631. }
  632. if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
  633. &crt->sig_md, &crt->sig_pk,
  634. &crt->sig_opts ) ) != 0 )
  635. {
  636. mbedtls_x509_crt_free( crt );
  637. return( ret );
  638. }
  639. /*
  640. * issuer Name
  641. */
  642. crt->issuer_raw.p = p;
  643. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  644. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  645. {
  646. mbedtls_x509_crt_free( crt );
  647. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  648. }
  649. if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
  650. {
  651. mbedtls_x509_crt_free( crt );
  652. return( ret );
  653. }
  654. crt->issuer_raw.len = p - crt->issuer_raw.p;
  655. /*
  656. * Validity ::= SEQUENCE {
  657. * notBefore Time,
  658. * notAfter Time }
  659. *
  660. */
  661. if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
  662. &crt->valid_to ) ) != 0 )
  663. {
  664. mbedtls_x509_crt_free( crt );
  665. return( ret );
  666. }
  667. /*
  668. * subject Name
  669. */
  670. crt->subject_raw.p = p;
  671. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  672. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  673. {
  674. mbedtls_x509_crt_free( crt );
  675. return( MBEDTLS_ERR_X509_INVALID_FORMAT + ret );
  676. }
  677. if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
  678. {
  679. mbedtls_x509_crt_free( crt );
  680. return( ret );
  681. }
  682. crt->subject_raw.len = p - crt->subject_raw.p;
  683. /*
  684. * SubjectPublicKeyInfo
  685. */
  686. if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
  687. {
  688. mbedtls_x509_crt_free( crt );
  689. return( ret );
  690. }
  691. /*
  692. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  693. * -- If present, version shall be v2 or v3
  694. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  695. * -- If present, version shall be v2 or v3
  696. * extensions [3] EXPLICIT Extensions OPTIONAL
  697. * -- If present, version shall be v3
  698. */
  699. if( crt->version == 2 || crt->version == 3 )
  700. {
  701. ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
  702. if( ret != 0 )
  703. {
  704. mbedtls_x509_crt_free( crt );
  705. return( ret );
  706. }
  707. }
  708. if( crt->version == 2 || crt->version == 3 )
  709. {
  710. ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
  711. if( ret != 0 )
  712. {
  713. mbedtls_x509_crt_free( crt );
  714. return( ret );
  715. }
  716. }
  717. #if !defined(MBEDTLS_X509_ALLOW_EXTENSIONS_NON_V3)
  718. if( crt->version == 3 )
  719. #endif
  720. {
  721. ret = x509_get_crt_ext( &p, end, crt );
  722. if( ret != 0 )
  723. {
  724. mbedtls_x509_crt_free( crt );
  725. return( ret );
  726. }
  727. }
  728. if( p != end )
  729. {
  730. mbedtls_x509_crt_free( crt );
  731. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  732. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  733. }
  734. end = crt_end;
  735. /*
  736. * }
  737. * -- end of TBSCertificate
  738. *
  739. * signatureAlgorithm AlgorithmIdentifier,
  740. * signatureValue BIT STRING
  741. */
  742. if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
  743. {
  744. mbedtls_x509_crt_free( crt );
  745. return( ret );
  746. }
  747. if( crt->sig_oid.len != sig_oid2.len ||
  748. memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
  749. sig_params1.len != sig_params2.len ||
  750. ( sig_params1.len != 0 &&
  751. memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
  752. {
  753. mbedtls_x509_crt_free( crt );
  754. return( MBEDTLS_ERR_X509_SIG_MISMATCH );
  755. }
  756. if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
  757. {
  758. mbedtls_x509_crt_free( crt );
  759. return( ret );
  760. }
  761. if( p != end )
  762. {
  763. mbedtls_x509_crt_free( crt );
  764. return( MBEDTLS_ERR_X509_INVALID_FORMAT +
  765. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  766. }
  767. return( 0 );
  768. }
  769. /*
  770. * Parse one X.509 certificate in DER format from a buffer and add them to a
  771. * chained list
  772. */
  773. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain, const unsigned char *buf,
  774. size_t buflen )
  775. {
  776. int ret;
  777. mbedtls_x509_crt *crt = chain, *prev = NULL;
  778. /*
  779. * Check for valid input
  780. */
  781. if( crt == NULL || buf == NULL )
  782. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  783. while( crt->version != 0 && crt->next != NULL )
  784. {
  785. prev = crt;
  786. crt = crt->next;
  787. }
  788. /*
  789. * Add new certificate on the end of the chain if needed.
  790. */
  791. if( crt->version != 0 && crt->next == NULL )
  792. {
  793. crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  794. if( crt->next == NULL )
  795. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  796. prev = crt;
  797. mbedtls_x509_crt_init( crt->next );
  798. crt = crt->next;
  799. }
  800. if( ( ret = x509_crt_parse_der_core( crt, buf, buflen ) ) != 0 )
  801. {
  802. if( prev )
  803. prev->next = NULL;
  804. if( crt != chain )
  805. mbedtls_free( crt );
  806. return( ret );
  807. }
  808. return( 0 );
  809. }
  810. /*
  811. * Parse one or more PEM certificates from a buffer and add them to the chained
  812. * list
  813. */
  814. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain, const unsigned char *buf, size_t buflen )
  815. {
  816. #if defined(MBEDTLS_PEM_PARSE_C)
  817. int success = 0, first_error = 0, total_failed = 0;
  818. int buf_format = MBEDTLS_X509_FORMAT_DER;
  819. #endif
  820. /*
  821. * Check for valid input
  822. */
  823. if( chain == NULL || buf == NULL )
  824. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  825. /*
  826. * Determine buffer content. Buffer contains either one DER certificate or
  827. * one or more PEM certificates.
  828. */
  829. #if defined(MBEDTLS_PEM_PARSE_C)
  830. if( buflen != 0 && buf[buflen - 1] == '\0' &&
  831. strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
  832. {
  833. buf_format = MBEDTLS_X509_FORMAT_PEM;
  834. }
  835. if( buf_format == MBEDTLS_X509_FORMAT_DER )
  836. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  837. #else
  838. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  839. #endif
  840. #if defined(MBEDTLS_PEM_PARSE_C)
  841. if( buf_format == MBEDTLS_X509_FORMAT_PEM )
  842. {
  843. int ret;
  844. mbedtls_pem_context pem;
  845. /* 1 rather than 0 since the terminating NULL byte is counted in */
  846. while( buflen > 1 )
  847. {
  848. size_t use_len;
  849. mbedtls_pem_init( &pem );
  850. /* If we get there, we know the string is null-terminated */
  851. ret = mbedtls_pem_read_buffer( &pem,
  852. "-----BEGIN CERTIFICATE-----",
  853. "-----END CERTIFICATE-----",
  854. buf, NULL, 0, &use_len );
  855. if( ret == 0 )
  856. {
  857. /*
  858. * Was PEM encoded
  859. */
  860. buflen -= use_len;
  861. buf += use_len;
  862. }
  863. else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
  864. {
  865. return( ret );
  866. }
  867. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  868. {
  869. mbedtls_pem_free( &pem );
  870. /*
  871. * PEM header and footer were found
  872. */
  873. buflen -= use_len;
  874. buf += use_len;
  875. if( first_error == 0 )
  876. first_error = ret;
  877. total_failed++;
  878. continue;
  879. }
  880. else
  881. break;
  882. ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
  883. mbedtls_pem_free( &pem );
  884. if( ret != 0 )
  885. {
  886. /*
  887. * Quit parsing on a memory error
  888. */
  889. if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
  890. return( ret );
  891. if( first_error == 0 )
  892. first_error = ret;
  893. total_failed++;
  894. continue;
  895. }
  896. success = 1;
  897. }
  898. }
  899. if( success )
  900. return( total_failed );
  901. else if( first_error )
  902. return( first_error );
  903. else
  904. return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
  905. #endif /* MBEDTLS_PEM_PARSE_C */
  906. }
  907. #if defined(MBEDTLS_FS_IO)
  908. /*
  909. * Load one or more certificates and add them to the chained list
  910. */
  911. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
  912. {
  913. int ret;
  914. size_t n;
  915. unsigned char *buf;
  916. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  917. return( ret );
  918. ret = mbedtls_x509_crt_parse( chain, buf, n );
  919. mbedtls_zeroize( buf, n );
  920. mbedtls_free( buf );
  921. return( ret );
  922. }
  923. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  924. {
  925. int ret = 0;
  926. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  927. int w_ret;
  928. WCHAR szDir[MAX_PATH];
  929. char filename[MAX_PATH];
  930. char *p;
  931. size_t len = strlen( path );
  932. WIN32_FIND_DATAW file_data;
  933. HANDLE hFind;
  934. if( len > MAX_PATH - 3 )
  935. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  936. memset( szDir, 0, sizeof(szDir) );
  937. memset( filename, 0, MAX_PATH );
  938. memcpy( filename, path, len );
  939. filename[len++] = '\\';
  940. p = filename + len;
  941. filename[len++] = '*';
  942. w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
  943. MAX_PATH - 3 );
  944. if( w_ret == 0 )
  945. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  946. hFind = FindFirstFileW( szDir, &file_data );
  947. if( hFind == INVALID_HANDLE_VALUE )
  948. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  949. len = MAX_PATH - len;
  950. do
  951. {
  952. memset( p, 0, len );
  953. if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  954. continue;
  955. w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
  956. lstrlenW( file_data.cFileName ),
  957. p, (int) len - 1,
  958. NULL, NULL );
  959. if( w_ret == 0 )
  960. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  961. w_ret = mbedtls_x509_crt_parse_file( chain, filename );
  962. if( w_ret < 0 )
  963. ret++;
  964. else
  965. ret += w_ret;
  966. }
  967. while( FindNextFileW( hFind, &file_data ) != 0 );
  968. if( GetLastError() != ERROR_NO_MORE_FILES )
  969. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  970. FindClose( hFind );
  971. #else /* _WIN32 */
  972. int t_ret;
  973. int snp_ret;
  974. struct stat sb;
  975. struct dirent *entry;
  976. char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
  977. DIR *dir = opendir( path );
  978. if( dir == NULL )
  979. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  980. #if defined(MBEDTLS_THREADING_PTHREAD)
  981. if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
  982. {
  983. closedir( dir );
  984. return( ret );
  985. }
  986. #endif
  987. while( ( entry = readdir( dir ) ) != NULL )
  988. {
  989. snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
  990. "%s/%s", path, entry->d_name );
  991. if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
  992. {
  993. ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  994. goto cleanup;
  995. }
  996. else if( stat( entry_name, &sb ) == -1 )
  997. {
  998. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  999. goto cleanup;
  1000. }
  1001. if( !S_ISREG( sb.st_mode ) )
  1002. continue;
  1003. // Ignore parse errors
  1004. //
  1005. t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
  1006. if( t_ret < 0 )
  1007. ret++;
  1008. else
  1009. ret += t_ret;
  1010. }
  1011. cleanup:
  1012. closedir( dir );
  1013. #if defined(MBEDTLS_THREADING_PTHREAD)
  1014. if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
  1015. ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  1016. #endif
  1017. #endif /* _WIN32 */
  1018. return( ret );
  1019. }
  1020. #endif /* MBEDTLS_FS_IO */
  1021. static int x509_info_subject_alt_name( char **buf, size_t *size,
  1022. const mbedtls_x509_sequence *subject_alt_name )
  1023. {
  1024. size_t i;
  1025. size_t n = *size;
  1026. char *p = *buf;
  1027. const mbedtls_x509_sequence *cur = subject_alt_name;
  1028. const char *sep = "";
  1029. size_t sep_len = 0;
  1030. while( cur != NULL )
  1031. {
  1032. if( cur->buf.len + sep_len >= n )
  1033. {
  1034. *p = '\0';
  1035. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  1036. }
  1037. n -= cur->buf.len + sep_len;
  1038. for( i = 0; i < sep_len; i++ )
  1039. *p++ = sep[i];
  1040. for( i = 0; i < cur->buf.len; i++ )
  1041. *p++ = cur->buf.p[i];
  1042. sep = ", ";
  1043. sep_len = 2;
  1044. cur = cur->next;
  1045. }
  1046. *p = '\0';
  1047. *size = n;
  1048. *buf = p;
  1049. return( 0 );
  1050. }
  1051. #define PRINT_ITEM(i) \
  1052. { \
  1053. ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
  1054. MBEDTLS_X509_SAFE_SNPRINTF; \
  1055. sep = ", "; \
  1056. }
  1057. #define CERT_TYPE(type,name) \
  1058. if( ns_cert_type & type ) \
  1059. PRINT_ITEM( name );
  1060. static int x509_info_cert_type( char **buf, size_t *size,
  1061. unsigned char ns_cert_type )
  1062. {
  1063. int ret;
  1064. size_t n = *size;
  1065. char *p = *buf;
  1066. const char *sep = "";
  1067. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
  1068. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
  1069. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
  1070. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
  1071. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
  1072. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
  1073. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
  1074. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
  1075. *size = n;
  1076. *buf = p;
  1077. return( 0 );
  1078. }
  1079. #define KEY_USAGE(code,name) \
  1080. if( key_usage & code ) \
  1081. PRINT_ITEM( name );
  1082. static int x509_info_key_usage( char **buf, size_t *size,
  1083. unsigned int key_usage )
  1084. {
  1085. int ret;
  1086. size_t n = *size;
  1087. char *p = *buf;
  1088. const char *sep = "";
  1089. KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
  1090. KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
  1091. KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
  1092. KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
  1093. KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
  1094. KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
  1095. KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
  1096. KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
  1097. KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
  1098. *size = n;
  1099. *buf = p;
  1100. return( 0 );
  1101. }
  1102. static int x509_info_ext_key_usage( char **buf, size_t *size,
  1103. const mbedtls_x509_sequence *extended_key_usage )
  1104. {
  1105. int ret;
  1106. const char *desc;
  1107. size_t n = *size;
  1108. char *p = *buf;
  1109. const mbedtls_x509_sequence *cur = extended_key_usage;
  1110. const char *sep = "";
  1111. while( cur != NULL )
  1112. {
  1113. if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
  1114. desc = "???";
  1115. ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
  1116. MBEDTLS_X509_SAFE_SNPRINTF;
  1117. sep = ", ";
  1118. cur = cur->next;
  1119. }
  1120. *size = n;
  1121. *buf = p;
  1122. return( 0 );
  1123. }
  1124. /*
  1125. * Return an informational string about the certificate.
  1126. */
  1127. #define BEFORE_COLON 18
  1128. #define BC "18"
  1129. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  1130. const mbedtls_x509_crt *crt )
  1131. {
  1132. int ret;
  1133. size_t n;
  1134. char *p;
  1135. char key_size_str[BEFORE_COLON];
  1136. p = buf;
  1137. n = size;
  1138. if( NULL == crt )
  1139. {
  1140. ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
  1141. MBEDTLS_X509_SAFE_SNPRINTF;
  1142. return( (int) ( size - n ) );
  1143. }
  1144. ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
  1145. prefix, crt->version );
  1146. MBEDTLS_X509_SAFE_SNPRINTF;
  1147. ret = mbedtls_snprintf( p, n, "%sserial number : ",
  1148. prefix );
  1149. MBEDTLS_X509_SAFE_SNPRINTF;
  1150. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  1151. MBEDTLS_X509_SAFE_SNPRINTF;
  1152. ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
  1153. MBEDTLS_X509_SAFE_SNPRINTF;
  1154. ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
  1155. MBEDTLS_X509_SAFE_SNPRINTF;
  1156. ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
  1157. MBEDTLS_X509_SAFE_SNPRINTF;
  1158. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  1159. MBEDTLS_X509_SAFE_SNPRINTF;
  1160. ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
  1161. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1162. crt->valid_from.year, crt->valid_from.mon,
  1163. crt->valid_from.day, crt->valid_from.hour,
  1164. crt->valid_from.min, crt->valid_from.sec );
  1165. MBEDTLS_X509_SAFE_SNPRINTF;
  1166. ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
  1167. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1168. crt->valid_to.year, crt->valid_to.mon,
  1169. crt->valid_to.day, crt->valid_to.hour,
  1170. crt->valid_to.min, crt->valid_to.sec );
  1171. MBEDTLS_X509_SAFE_SNPRINTF;
  1172. ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
  1173. MBEDTLS_X509_SAFE_SNPRINTF;
  1174. ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
  1175. crt->sig_md, crt->sig_opts );
  1176. MBEDTLS_X509_SAFE_SNPRINTF;
  1177. /* Key size */
  1178. if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
  1179. mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
  1180. {
  1181. return( ret );
  1182. }
  1183. ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
  1184. (int) mbedtls_pk_get_bitlen( &crt->pk ) );
  1185. MBEDTLS_X509_SAFE_SNPRINTF;
  1186. /*
  1187. * Optional extensions
  1188. */
  1189. if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
  1190. {
  1191. ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
  1192. crt->ca_istrue ? "true" : "false" );
  1193. MBEDTLS_X509_SAFE_SNPRINTF;
  1194. if( crt->max_pathlen > 0 )
  1195. {
  1196. ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
  1197. MBEDTLS_X509_SAFE_SNPRINTF;
  1198. }
  1199. }
  1200. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  1201. {
  1202. ret = mbedtls_snprintf( p, n, "\n%ssubject alt name : ", prefix );
  1203. MBEDTLS_X509_SAFE_SNPRINTF;
  1204. if( ( ret = x509_info_subject_alt_name( &p, &n,
  1205. &crt->subject_alt_names ) ) != 0 )
  1206. return( ret );
  1207. }
  1208. if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
  1209. {
  1210. ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
  1211. MBEDTLS_X509_SAFE_SNPRINTF;
  1212. if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
  1213. return( ret );
  1214. }
  1215. if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
  1216. {
  1217. ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
  1218. MBEDTLS_X509_SAFE_SNPRINTF;
  1219. if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
  1220. return( ret );
  1221. }
  1222. if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
  1223. {
  1224. ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
  1225. MBEDTLS_X509_SAFE_SNPRINTF;
  1226. if( ( ret = x509_info_ext_key_usage( &p, &n,
  1227. &crt->ext_key_usage ) ) != 0 )
  1228. return( ret );
  1229. }
  1230. ret = mbedtls_snprintf( p, n, "\n" );
  1231. MBEDTLS_X509_SAFE_SNPRINTF;
  1232. return( (int) ( size - n ) );
  1233. }
  1234. struct x509_crt_verify_string {
  1235. int code;
  1236. const char *string;
  1237. };
  1238. static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
  1239. { MBEDTLS_X509_BADCERT_EXPIRED, "The certificate validity has expired" },
  1240. { MBEDTLS_X509_BADCERT_REVOKED, "The certificate has been revoked (is on a CRL)" },
  1241. { MBEDTLS_X509_BADCERT_CN_MISMATCH, "The certificate Common Name (CN) does not match with the expected CN" },
  1242. { MBEDTLS_X509_BADCERT_NOT_TRUSTED, "The certificate is not correctly signed by the trusted CA" },
  1243. { MBEDTLS_X509_BADCRL_NOT_TRUSTED, "The CRL is not correctly signed by the trusted CA" },
  1244. { MBEDTLS_X509_BADCRL_EXPIRED, "The CRL is expired" },
  1245. { MBEDTLS_X509_BADCERT_MISSING, "Certificate was missing" },
  1246. { MBEDTLS_X509_BADCERT_SKIP_VERIFY, "Certificate verification was skipped" },
  1247. { MBEDTLS_X509_BADCERT_OTHER, "Other reason (can be used by verify callback)" },
  1248. { MBEDTLS_X509_BADCERT_FUTURE, "The certificate validity starts in the future" },
  1249. { MBEDTLS_X509_BADCRL_FUTURE, "The CRL is from the future" },
  1250. { MBEDTLS_X509_BADCERT_KEY_USAGE, "Usage does not match the keyUsage extension" },
  1251. { MBEDTLS_X509_BADCERT_EXT_KEY_USAGE, "Usage does not match the extendedKeyUsage extension" },
  1252. { MBEDTLS_X509_BADCERT_NS_CERT_TYPE, "Usage does not match the nsCertType extension" },
  1253. { MBEDTLS_X509_BADCERT_BAD_MD, "The certificate is signed with an unacceptable hash." },
  1254. { MBEDTLS_X509_BADCERT_BAD_PK, "The certificate is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
  1255. { MBEDTLS_X509_BADCERT_BAD_KEY, "The certificate is signed with an unacceptable key (eg bad curve, RSA too short)." },
  1256. { MBEDTLS_X509_BADCRL_BAD_MD, "The CRL is signed with an unacceptable hash." },
  1257. { MBEDTLS_X509_BADCRL_BAD_PK, "The CRL is signed with an unacceptable PK alg (eg RSA vs ECDSA)." },
  1258. { MBEDTLS_X509_BADCRL_BAD_KEY, "The CRL is signed with an unacceptable key (eg bad curve, RSA too short)." },
  1259. { 0, NULL }
  1260. };
  1261. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  1262. uint32_t flags )
  1263. {
  1264. int ret;
  1265. const struct x509_crt_verify_string *cur;
  1266. char *p = buf;
  1267. size_t n = size;
  1268. for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
  1269. {
  1270. if( ( flags & cur->code ) == 0 )
  1271. continue;
  1272. ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
  1273. MBEDTLS_X509_SAFE_SNPRINTF;
  1274. flags ^= cur->code;
  1275. }
  1276. if( flags != 0 )
  1277. {
  1278. ret = mbedtls_snprintf( p, n, "%sUnknown reason "
  1279. "(this should not happen)\n", prefix );
  1280. MBEDTLS_X509_SAFE_SNPRINTF;
  1281. }
  1282. return( (int) ( size - n ) );
  1283. }
  1284. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  1285. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  1286. unsigned int usage )
  1287. {
  1288. unsigned int usage_must, usage_may;
  1289. unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
  1290. | MBEDTLS_X509_KU_DECIPHER_ONLY;
  1291. if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
  1292. return( 0 );
  1293. usage_must = usage & ~may_mask;
  1294. if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
  1295. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1296. usage_may = usage & may_mask;
  1297. if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
  1298. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1299. return( 0 );
  1300. }
  1301. #endif
  1302. #if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
  1303. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  1304. const char *usage_oid,
  1305. size_t usage_len )
  1306. {
  1307. const mbedtls_x509_sequence *cur;
  1308. /* Extension is not mandatory, absent means no restriction */
  1309. if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
  1310. return( 0 );
  1311. /*
  1312. * Look for the requested usage (or wildcard ANY) in our list
  1313. */
  1314. for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
  1315. {
  1316. const mbedtls_x509_buf *cur_oid = &cur->buf;
  1317. if( cur_oid->len == usage_len &&
  1318. memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
  1319. {
  1320. return( 0 );
  1321. }
  1322. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
  1323. return( 0 );
  1324. }
  1325. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1326. }
  1327. #endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
  1328. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  1329. /*
  1330. * Return 1 if the certificate is revoked, or 0 otherwise.
  1331. */
  1332. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
  1333. {
  1334. const mbedtls_x509_crl_entry *cur = &crl->entry;
  1335. while( cur != NULL && cur->serial.len != 0 )
  1336. {
  1337. if( crt->serial.len == cur->serial.len &&
  1338. memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
  1339. {
  1340. if( mbedtls_x509_time_is_past( &cur->revocation_date ) )
  1341. return( 1 );
  1342. }
  1343. cur = cur->next;
  1344. }
  1345. return( 0 );
  1346. }
  1347. /*
  1348. * Check that the given certificate is not revoked according to the CRL.
  1349. * Skip validation is no CRL for the given CA is present.
  1350. */
  1351. static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
  1352. mbedtls_x509_crl *crl_list,
  1353. const mbedtls_x509_crt_profile *profile )
  1354. {
  1355. int flags = 0;
  1356. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  1357. const mbedtls_md_info_t *md_info;
  1358. if( ca == NULL )
  1359. return( flags );
  1360. while( crl_list != NULL )
  1361. {
  1362. if( crl_list->version == 0 ||
  1363. crl_list->issuer_raw.len != ca->subject_raw.len ||
  1364. memcmp( crl_list->issuer_raw.p, ca->subject_raw.p,
  1365. crl_list->issuer_raw.len ) != 0 )
  1366. {
  1367. crl_list = crl_list->next;
  1368. continue;
  1369. }
  1370. /*
  1371. * Check if the CA is configured to sign CRLs
  1372. */
  1373. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  1374. if( mbedtls_x509_crt_check_key_usage( ca, MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
  1375. {
  1376. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  1377. break;
  1378. }
  1379. #endif
  1380. /*
  1381. * Check if CRL is correctly signed by the trusted CA
  1382. */
  1383. if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
  1384. flags |= MBEDTLS_X509_BADCRL_BAD_MD;
  1385. if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
  1386. flags |= MBEDTLS_X509_BADCRL_BAD_PK;
  1387. md_info = mbedtls_md_info_from_type( crl_list->sig_md );
  1388. if( md_info == NULL )
  1389. {
  1390. /*
  1391. * Cannot check 'unknown' hash
  1392. */
  1393. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  1394. break;
  1395. }
  1396. mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash );
  1397. if( x509_profile_check_key( profile, crl_list->sig_pk, &ca->pk ) != 0 )
  1398. flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  1399. if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
  1400. crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
  1401. crl_list->sig.p, crl_list->sig.len ) != 0 )
  1402. {
  1403. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  1404. break;
  1405. }
  1406. /*
  1407. * Check for validity of CRL (Do not drop out)
  1408. */
  1409. if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
  1410. flags |= MBEDTLS_X509_BADCRL_EXPIRED;
  1411. if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
  1412. flags |= MBEDTLS_X509_BADCRL_FUTURE;
  1413. /*
  1414. * Check if certificate is revoked
  1415. */
  1416. if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
  1417. {
  1418. flags |= MBEDTLS_X509_BADCERT_REVOKED;
  1419. break;
  1420. }
  1421. crl_list = crl_list->next;
  1422. }
  1423. return( flags );
  1424. }
  1425. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  1426. /*
  1427. * Like memcmp, but case-insensitive and always returns -1 if different
  1428. */
  1429. static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
  1430. {
  1431. size_t i;
  1432. unsigned char diff;
  1433. const unsigned char *n1 = s1, *n2 = s2;
  1434. for( i = 0; i < len; i++ )
  1435. {
  1436. diff = n1[i] ^ n2[i];
  1437. if( diff == 0 )
  1438. continue;
  1439. if( diff == 32 &&
  1440. ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
  1441. ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
  1442. {
  1443. continue;
  1444. }
  1445. return( -1 );
  1446. }
  1447. return( 0 );
  1448. }
  1449. /*
  1450. * Return 0 if name matches wildcard, -1 otherwise
  1451. */
  1452. static int x509_check_wildcard( const char *cn, mbedtls_x509_buf *name )
  1453. {
  1454. size_t i;
  1455. size_t cn_idx = 0, cn_len = strlen( cn );
  1456. if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
  1457. return( 0 );
  1458. for( i = 0; i < cn_len; ++i )
  1459. {
  1460. if( cn[i] == '.' )
  1461. {
  1462. cn_idx = i;
  1463. break;
  1464. }
  1465. }
  1466. if( cn_idx == 0 )
  1467. return( -1 );
  1468. if( cn_len - cn_idx == name->len - 1 &&
  1469. x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
  1470. {
  1471. return( 0 );
  1472. }
  1473. return( -1 );
  1474. }
  1475. /*
  1476. * Compare two X.509 strings, case-insensitive, and allowing for some encoding
  1477. * variations (but not all).
  1478. *
  1479. * Return 0 if equal, -1 otherwise.
  1480. */
  1481. static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
  1482. {
  1483. if( a->tag == b->tag &&
  1484. a->len == b->len &&
  1485. memcmp( a->p, b->p, b->len ) == 0 )
  1486. {
  1487. return( 0 );
  1488. }
  1489. if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  1490. ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  1491. a->len == b->len &&
  1492. x509_memcasecmp( a->p, b->p, b->len ) == 0 )
  1493. {
  1494. return( 0 );
  1495. }
  1496. return( -1 );
  1497. }
  1498. /*
  1499. * Compare two X.509 Names (aka rdnSequence).
  1500. *
  1501. * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
  1502. * we sometimes return unequal when the full algorithm would return equal,
  1503. * but never the other way. (In particular, we don't do Unicode normalisation
  1504. * or space folding.)
  1505. *
  1506. * Return 0 if equal, -1 otherwise.
  1507. */
  1508. static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
  1509. {
  1510. /* Avoid recursion, it might not be optimised by the compiler */
  1511. while( a != NULL || b != NULL )
  1512. {
  1513. if( a == NULL || b == NULL )
  1514. return( -1 );
  1515. /* type */
  1516. if( a->oid.tag != b->oid.tag ||
  1517. a->oid.len != b->oid.len ||
  1518. memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
  1519. {
  1520. return( -1 );
  1521. }
  1522. /* value */
  1523. if( x509_string_cmp( &a->val, &b->val ) != 0 )
  1524. return( -1 );
  1525. /* structure of the list of sets */
  1526. if( a->next_merged != b->next_merged )
  1527. return( -1 );
  1528. a = a->next;
  1529. b = b->next;
  1530. }
  1531. /* a == NULL == b */
  1532. return( 0 );
  1533. }
  1534. /*
  1535. * Check if 'parent' is a suitable parent (signing CA) for 'child'.
  1536. * Return 0 if yes, -1 if not.
  1537. *
  1538. * top means parent is a locally-trusted certificate
  1539. * bottom means child is the end entity cert
  1540. */
  1541. static int x509_crt_check_parent( const mbedtls_x509_crt *child,
  1542. const mbedtls_x509_crt *parent,
  1543. int top, int bottom )
  1544. {
  1545. int need_ca_bit;
  1546. /* Parent must be the issuer */
  1547. if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
  1548. return( -1 );
  1549. /* Parent must have the basicConstraints CA bit set as a general rule */
  1550. need_ca_bit = 1;
  1551. /* Exception: v1/v2 certificates that are locally trusted. */
  1552. if( top && parent->version < 3 )
  1553. need_ca_bit = 0;
  1554. /* Exception: self-signed end-entity certs that are locally trusted. */
  1555. if( top && bottom &&
  1556. child->raw.len == parent->raw.len &&
  1557. memcmp( child->raw.p, parent->raw.p, child->raw.len ) == 0 )
  1558. {
  1559. need_ca_bit = 0;
  1560. }
  1561. if( need_ca_bit && ! parent->ca_istrue )
  1562. return( -1 );
  1563. #if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
  1564. if( need_ca_bit &&
  1565. mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
  1566. {
  1567. return( -1 );
  1568. }
  1569. #endif
  1570. return( 0 );
  1571. }
  1572. static int x509_crt_verify_top(
  1573. mbedtls_x509_crt *child, mbedtls_x509_crt *trust_ca,
  1574. mbedtls_x509_crl *ca_crl,
  1575. const mbedtls_x509_crt_profile *profile,
  1576. int path_cnt, int self_cnt, uint32_t *flags,
  1577. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1578. void *p_vrfy )
  1579. {
  1580. int ret;
  1581. uint32_t ca_flags = 0;
  1582. int check_path_cnt;
  1583. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  1584. const mbedtls_md_info_t *md_info;
  1585. mbedtls_x509_crt *future_past_ca = NULL;
  1586. if( mbedtls_x509_time_is_past( &child->valid_to ) )
  1587. *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  1588. if( mbedtls_x509_time_is_future( &child->valid_from ) )
  1589. *flags |= MBEDTLS_X509_BADCERT_FUTURE;
  1590. if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
  1591. *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
  1592. if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
  1593. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  1594. /*
  1595. * Child is the top of the chain. Check against the trust_ca list.
  1596. */
  1597. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  1598. md_info = mbedtls_md_info_from_type( child->sig_md );
  1599. if( md_info == NULL )
  1600. {
  1601. /*
  1602. * Cannot check 'unknown', no need to try any CA
  1603. */
  1604. trust_ca = NULL;
  1605. }
  1606. else
  1607. mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
  1608. for( /* trust_ca */ ; trust_ca != NULL; trust_ca = trust_ca->next )
  1609. {
  1610. if( x509_crt_check_parent( child, trust_ca, 1, path_cnt == 0 ) != 0 )
  1611. continue;
  1612. check_path_cnt = path_cnt + 1;
  1613. /*
  1614. * Reduce check_path_cnt to check against if top of the chain is
  1615. * the same as the trusted CA
  1616. */
  1617. if( child->subject_raw.len == trust_ca->subject_raw.len &&
  1618. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  1619. child->issuer_raw.len ) == 0 )
  1620. {
  1621. check_path_cnt--;
  1622. }
  1623. /* Self signed certificates do not count towards the limit */
  1624. if( trust_ca->max_pathlen > 0 &&
  1625. trust_ca->max_pathlen < check_path_cnt - self_cnt )
  1626. {
  1627. continue;
  1628. }
  1629. if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &trust_ca->pk,
  1630. child->sig_md, hash, mbedtls_md_get_size( md_info ),
  1631. child->sig.p, child->sig.len ) != 0 )
  1632. {
  1633. continue;
  1634. }
  1635. if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) ||
  1636. mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
  1637. {
  1638. if ( future_past_ca == NULL )
  1639. future_past_ca = trust_ca;
  1640. continue;
  1641. }
  1642. break;
  1643. }
  1644. if( trust_ca != NULL || ( trust_ca = future_past_ca ) != NULL )
  1645. {
  1646. /*
  1647. * Top of chain is signed by a trusted CA
  1648. */
  1649. *flags &= ~MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  1650. if( x509_profile_check_key( profile, child->sig_pk, &trust_ca->pk ) != 0 )
  1651. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  1652. }
  1653. /*
  1654. * If top of chain is not the same as the trusted CA send a verify request
  1655. * to the callback for any issues with validity and CRL presence for the
  1656. * trusted CA certificate.
  1657. */
  1658. if( trust_ca != NULL &&
  1659. ( child->subject_raw.len != trust_ca->subject_raw.len ||
  1660. memcmp( child->subject_raw.p, trust_ca->subject_raw.p,
  1661. child->issuer_raw.len ) != 0 ) )
  1662. {
  1663. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  1664. /* Check trusted CA's CRL for the chain's top crt */
  1665. *flags |= x509_crt_verifycrl( child, trust_ca, ca_crl, profile );
  1666. #else
  1667. ((void) ca_crl);
  1668. #endif
  1669. if( mbedtls_x509_time_is_past( &trust_ca->valid_to ) )
  1670. ca_flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  1671. if( mbedtls_x509_time_is_future( &trust_ca->valid_from ) )
  1672. ca_flags |= MBEDTLS_X509_BADCERT_FUTURE;
  1673. if( NULL != f_vrfy )
  1674. {
  1675. if( ( ret = f_vrfy( p_vrfy, trust_ca, path_cnt + 1,
  1676. &ca_flags ) ) != 0 )
  1677. {
  1678. return( ret );
  1679. }
  1680. }
  1681. }
  1682. /* Call callback on top cert */
  1683. if( NULL != f_vrfy )
  1684. {
  1685. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  1686. return( ret );
  1687. }
  1688. *flags |= ca_flags;
  1689. return( 0 );
  1690. }
  1691. static int x509_crt_verify_child(
  1692. mbedtls_x509_crt *child, mbedtls_x509_crt *parent,
  1693. mbedtls_x509_crt *trust_ca, mbedtls_x509_crl *ca_crl,
  1694. const mbedtls_x509_crt_profile *profile,
  1695. int path_cnt, int self_cnt, uint32_t *flags,
  1696. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1697. void *p_vrfy )
  1698. {
  1699. int ret;
  1700. uint32_t parent_flags = 0;
  1701. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  1702. mbedtls_x509_crt *grandparent;
  1703. const mbedtls_md_info_t *md_info;
  1704. /* Counting intermediate self signed certificates */
  1705. if( ( path_cnt != 0 ) && x509_name_cmp( &child->issuer, &child->subject ) == 0 )
  1706. self_cnt++;
  1707. /* path_cnt is 0 for the first intermediate CA */
  1708. if( 1 + path_cnt > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
  1709. {
  1710. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  1711. return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
  1712. }
  1713. if( mbedtls_x509_time_is_past( &child->valid_to ) )
  1714. *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  1715. if( mbedtls_x509_time_is_future( &child->valid_from ) )
  1716. *flags |= MBEDTLS_X509_BADCERT_FUTURE;
  1717. if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
  1718. *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
  1719. if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
  1720. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  1721. md_info = mbedtls_md_info_from_type( child->sig_md );
  1722. if( md_info == NULL )
  1723. {
  1724. /*
  1725. * Cannot check 'unknown' hash
  1726. */
  1727. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  1728. }
  1729. else
  1730. {
  1731. mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash );
  1732. if( x509_profile_check_key( profile, child->sig_pk, &parent->pk ) != 0 )
  1733. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  1734. if( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
  1735. child->sig_md, hash, mbedtls_md_get_size( md_info ),
  1736. child->sig.p, child->sig.len ) != 0 )
  1737. {
  1738. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  1739. }
  1740. }
  1741. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  1742. /* Check trusted CA's CRL for the given crt */
  1743. *flags |= x509_crt_verifycrl(child, parent, ca_crl, profile );
  1744. #endif
  1745. /* Look for a grandparent in trusted CAs */
  1746. for( grandparent = trust_ca;
  1747. grandparent != NULL;
  1748. grandparent = grandparent->next )
  1749. {
  1750. if( x509_crt_check_parent( parent, grandparent,
  1751. 0, path_cnt == 0 ) == 0 )
  1752. break;
  1753. }
  1754. if( grandparent != NULL )
  1755. {
  1756. ret = x509_crt_verify_top( parent, grandparent, ca_crl, profile,
  1757. path_cnt + 1, self_cnt, &parent_flags, f_vrfy, p_vrfy );
  1758. if( ret != 0 )
  1759. return( ret );
  1760. }
  1761. else
  1762. {
  1763. /* Look for a grandparent upwards the chain */
  1764. for( grandparent = parent->next;
  1765. grandparent != NULL;
  1766. grandparent = grandparent->next )
  1767. {
  1768. /* +2 because the current step is not yet accounted for
  1769. * and because max_pathlen is one higher than it should be.
  1770. * Also self signed certificates do not count to the limit. */
  1771. if( grandparent->max_pathlen > 0 &&
  1772. grandparent->max_pathlen < 2 + path_cnt - self_cnt )
  1773. {
  1774. continue;
  1775. }
  1776. if( x509_crt_check_parent( parent, grandparent,
  1777. 0, path_cnt == 0 ) == 0 )
  1778. break;
  1779. }
  1780. /* Is our parent part of the chain or at the top? */
  1781. if( grandparent != NULL )
  1782. {
  1783. ret = x509_crt_verify_child( parent, grandparent, trust_ca, ca_crl,
  1784. profile, path_cnt + 1, self_cnt, &parent_flags,
  1785. f_vrfy, p_vrfy );
  1786. if( ret != 0 )
  1787. return( ret );
  1788. }
  1789. else
  1790. {
  1791. ret = x509_crt_verify_top( parent, trust_ca, ca_crl, profile,
  1792. path_cnt + 1, self_cnt, &parent_flags,
  1793. f_vrfy, p_vrfy );
  1794. if( ret != 0 )
  1795. return( ret );
  1796. }
  1797. }
  1798. /* child is verified to be a child of the parent, call verify callback */
  1799. if( NULL != f_vrfy )
  1800. if( ( ret = f_vrfy( p_vrfy, child, path_cnt, flags ) ) != 0 )
  1801. return( ret );
  1802. *flags |= parent_flags;
  1803. return( 0 );
  1804. }
  1805. /*
  1806. * Verify the certificate validity
  1807. */
  1808. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  1809. mbedtls_x509_crt *trust_ca,
  1810. mbedtls_x509_crl *ca_crl,
  1811. const char *cn, uint32_t *flags,
  1812. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1813. void *p_vrfy )
  1814. {
  1815. return( mbedtls_x509_crt_verify_with_profile( crt, trust_ca, ca_crl,
  1816. &mbedtls_x509_crt_profile_default, cn, flags, f_vrfy, p_vrfy ) );
  1817. }
  1818. /*
  1819. * Verify the certificate validity, with profile
  1820. */
  1821. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  1822. mbedtls_x509_crt *trust_ca,
  1823. mbedtls_x509_crl *ca_crl,
  1824. const mbedtls_x509_crt_profile *profile,
  1825. const char *cn, uint32_t *flags,
  1826. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1827. void *p_vrfy )
  1828. {
  1829. size_t cn_len;
  1830. int ret;
  1831. int pathlen = 0, selfsigned = 0;
  1832. mbedtls_x509_crt *parent;
  1833. mbedtls_x509_name *name;
  1834. mbedtls_x509_sequence *cur = NULL;
  1835. mbedtls_pk_type_t pk_type;
  1836. if( profile == NULL )
  1837. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1838. *flags = 0;
  1839. if( cn != NULL )
  1840. {
  1841. name = &crt->subject;
  1842. cn_len = strlen( cn );
  1843. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  1844. {
  1845. cur = &crt->subject_alt_names;
  1846. while( cur != NULL )
  1847. {
  1848. if( cur->buf.len == cn_len &&
  1849. x509_memcasecmp( cn, cur->buf.p, cn_len ) == 0 )
  1850. break;
  1851. if( cur->buf.len > 2 &&
  1852. memcmp( cur->buf.p, "*.", 2 ) == 0 &&
  1853. x509_check_wildcard( cn, &cur->buf ) == 0 )
  1854. {
  1855. break;
  1856. }
  1857. cur = cur->next;
  1858. }
  1859. if( cur == NULL )
  1860. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  1861. }
  1862. else
  1863. {
  1864. while( name != NULL )
  1865. {
  1866. if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 )
  1867. {
  1868. if( name->val.len == cn_len &&
  1869. x509_memcasecmp( name->val.p, cn, cn_len ) == 0 )
  1870. break;
  1871. if( name->val.len > 2 &&
  1872. memcmp( name->val.p, "*.", 2 ) == 0 &&
  1873. x509_check_wildcard( cn, &name->val ) == 0 )
  1874. break;
  1875. }
  1876. name = name->next;
  1877. }
  1878. if( name == NULL )
  1879. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  1880. }
  1881. }
  1882. /* Check the type and size of the key */
  1883. pk_type = mbedtls_pk_get_type( &crt->pk );
  1884. if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
  1885. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  1886. if( x509_profile_check_key( profile, pk_type, &crt->pk ) != 0 )
  1887. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  1888. /* Look for a parent in trusted CAs */
  1889. for( parent = trust_ca; parent != NULL; parent = parent->next )
  1890. {
  1891. if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
  1892. break;
  1893. }
  1894. if( parent != NULL )
  1895. {
  1896. ret = x509_crt_verify_top( crt, parent, ca_crl, profile,
  1897. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  1898. if( ret != 0 )
  1899. return( ret );
  1900. }
  1901. else
  1902. {
  1903. /* Look for a parent upwards the chain */
  1904. for( parent = crt->next; parent != NULL; parent = parent->next )
  1905. if( x509_crt_check_parent( crt, parent, 0, pathlen == 0 ) == 0 )
  1906. break;
  1907. /* Are we part of the chain or at the top? */
  1908. if( parent != NULL )
  1909. {
  1910. ret = x509_crt_verify_child( crt, parent, trust_ca, ca_crl, profile,
  1911. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  1912. if( ret != 0 )
  1913. return( ret );
  1914. }
  1915. else
  1916. {
  1917. ret = x509_crt_verify_top( crt, trust_ca, ca_crl, profile,
  1918. pathlen, selfsigned, flags, f_vrfy, p_vrfy );
  1919. if( ret != 0 )
  1920. return( ret );
  1921. }
  1922. }
  1923. if( *flags != 0 )
  1924. return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
  1925. return( 0 );
  1926. }
  1927. /*
  1928. * Initialize a certificate chain
  1929. */
  1930. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
  1931. {
  1932. memset( crt, 0, sizeof(mbedtls_x509_crt) );
  1933. }
  1934. /*
  1935. * Unallocate all certificate data
  1936. */
  1937. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
  1938. {
  1939. mbedtls_x509_crt *cert_cur = crt;
  1940. mbedtls_x509_crt *cert_prv;
  1941. mbedtls_x509_name *name_cur;
  1942. mbedtls_x509_name *name_prv;
  1943. mbedtls_x509_sequence *seq_cur;
  1944. mbedtls_x509_sequence *seq_prv;
  1945. if( crt == NULL )
  1946. return;
  1947. do
  1948. {
  1949. mbedtls_pk_free( &cert_cur->pk );
  1950. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  1951. mbedtls_free( cert_cur->sig_opts );
  1952. #endif
  1953. name_cur = cert_cur->issuer.next;
  1954. while( name_cur != NULL )
  1955. {
  1956. name_prv = name_cur;
  1957. name_cur = name_cur->next;
  1958. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  1959. mbedtls_free( name_prv );
  1960. }
  1961. name_cur = cert_cur->subject.next;
  1962. while( name_cur != NULL )
  1963. {
  1964. name_prv = name_cur;
  1965. name_cur = name_cur->next;
  1966. mbedtls_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  1967. mbedtls_free( name_prv );
  1968. }
  1969. seq_cur = cert_cur->ext_key_usage.next;
  1970. while( seq_cur != NULL )
  1971. {
  1972. seq_prv = seq_cur;
  1973. seq_cur = seq_cur->next;
  1974. mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
  1975. mbedtls_free( seq_prv );
  1976. }
  1977. seq_cur = cert_cur->subject_alt_names.next;
  1978. while( seq_cur != NULL )
  1979. {
  1980. seq_prv = seq_cur;
  1981. seq_cur = seq_cur->next;
  1982. mbedtls_zeroize( seq_prv, sizeof( mbedtls_x509_sequence ) );
  1983. mbedtls_free( seq_prv );
  1984. }
  1985. if( cert_cur->raw.p != NULL )
  1986. {
  1987. mbedtls_zeroize( cert_cur->raw.p, cert_cur->raw.len );
  1988. mbedtls_free( cert_cur->raw.p );
  1989. }
  1990. cert_cur = cert_cur->next;
  1991. }
  1992. while( cert_cur != NULL );
  1993. cert_cur = crt;
  1994. do
  1995. {
  1996. cert_prv = cert_cur;
  1997. cert_cur = cert_cur->next;
  1998. mbedtls_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
  1999. if( cert_prv != crt )
  2000. mbedtls_free( cert_prv );
  2001. }
  2002. while( cert_cur != NULL );
  2003. }
  2004. #endif /* MBEDTLS_X509_CRT_PARSE_C */