x509_crt.c 69 KB

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