x509_crt.c 69 KB

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