x509_crt.c 70 KB

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