x509_crt.c 72 KB

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