x509_crt.c 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391
  1. /*
  2. * X.509 certificate parsing and verification
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. /*
  20. * The ITU-T X.509 standard defines a certificate format for PKI.
  21. *
  22. * http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
  23. * http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
  24. * http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
  25. *
  26. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
  27. * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
  28. *
  29. * [SIRO] https://cabforum.org/wp-content/uploads/Chunghwatelecom201503cabforumV4.pdf
  30. */
  31. #include "common.h"
  32. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  33. #include "mbedtls/x509_crt.h"
  34. #include "mbedtls/error.h"
  35. #include "mbedtls/oid.h"
  36. #include "mbedtls/platform_util.h"
  37. #include <string.h>
  38. #if defined(MBEDTLS_PEM_PARSE_C)
  39. #include "mbedtls/pem.h"
  40. #endif
  41. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  42. #include "psa/crypto.h"
  43. #include "mbedtls/psa_util.h"
  44. #endif
  45. #if defined(MBEDTLS_PLATFORM_C)
  46. #include "mbedtls/platform.h"
  47. #else
  48. #include <stdio.h>
  49. #include <stdlib.h>
  50. #define mbedtls_free free
  51. #define mbedtls_calloc calloc
  52. #define mbedtls_snprintf snprintf
  53. #endif
  54. #if defined(MBEDTLS_THREADING_C)
  55. #include "mbedtls/threading.h"
  56. #endif
  57. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  58. #include <windows.h>
  59. #else
  60. #include <time.h>
  61. #endif
  62. #if defined(MBEDTLS_FS_IO)
  63. #include <stdio.h>
  64. #if !defined(_WIN32) || defined(EFIX64) || defined(EFI32)
  65. #include <sys/types.h>
  66. #include <sys/stat.h>
  67. #if defined(__MBED__)
  68. #include <platform/mbed_retarget.h>
  69. #else
  70. #include <dirent.h>
  71. #endif /* __MBED__ */
  72. #endif /* !_WIN32 || EFIX64 || EFI32 */
  73. #endif
  74. /*
  75. * Item in a verification chain: cert and flags for it
  76. */
  77. typedef struct {
  78. mbedtls_x509_crt *crt;
  79. uint32_t flags;
  80. } x509_crt_verify_chain_item;
  81. /*
  82. * Max size of verification chain: end-entity + intermediates + trusted root
  83. */
  84. #define X509_MAX_VERIFY_CHAIN_SIZE ( MBEDTLS_X509_MAX_INTERMEDIATE_CA + 2 )
  85. /* Default profile. Do not remove items unless there are serious security
  86. * concerns. */
  87. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_default =
  88. {
  89. /* Hashes from SHA-256 and above. Note that this selection
  90. * should be aligned with ssl_preset_default_hashes in ssl_tls.c. */
  91. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  92. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  93. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  94. 0xFFFFFFF, /* Any PK alg */
  95. #if defined(MBEDTLS_ECP_C)
  96. /* Curves at or above 128-bit security level. Note that this selection
  97. * should be aligned with ssl_preset_default_curves in ssl_tls.c. */
  98. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  99. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
  100. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
  101. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
  102. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
  103. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
  104. 0,
  105. #else
  106. 0,
  107. #endif
  108. 2048,
  109. };
  110. /* Next-generation profile. Currently identical to the default, but may
  111. * be tightened at any time. */
  112. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_next =
  113. {
  114. /* Hashes from SHA-256 and above. */
  115. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  116. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ) |
  117. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA512 ),
  118. 0xFFFFFFF, /* Any PK alg */
  119. #if defined(MBEDTLS_ECP_C)
  120. /* Curves at or above 128-bit security level. */
  121. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  122. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ) |
  123. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP521R1 ) |
  124. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP256R1 ) |
  125. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP384R1 ) |
  126. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_BP512R1 ) |
  127. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256K1 ),
  128. #else
  129. 0,
  130. #endif
  131. 2048,
  132. };
  133. /*
  134. * NSA Suite B Profile
  135. */
  136. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_suiteb =
  137. {
  138. /* Only SHA-256 and 384 */
  139. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA256 ) |
  140. MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA384 ),
  141. /* Only ECDSA */
  142. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECDSA ) |
  143. MBEDTLS_X509_ID_FLAG( MBEDTLS_PK_ECKEY ),
  144. #if defined(MBEDTLS_ECP_C)
  145. /* Only NIST P-256 and P-384 */
  146. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP256R1 ) |
  147. MBEDTLS_X509_ID_FLAG( MBEDTLS_ECP_DP_SECP384R1 ),
  148. #else
  149. 0,
  150. #endif
  151. 0,
  152. };
  153. /*
  154. * Empty / all-forbidden profile
  155. */
  156. const mbedtls_x509_crt_profile mbedtls_x509_crt_profile_none =
  157. {
  158. 0,
  159. 0,
  160. 0,
  161. (uint32_t) -1,
  162. };
  163. /*
  164. * Check md_alg against profile
  165. * Return 0 if md_alg is acceptable for this profile, -1 otherwise
  166. */
  167. static int x509_profile_check_md_alg( const mbedtls_x509_crt_profile *profile,
  168. mbedtls_md_type_t md_alg )
  169. {
  170. if( md_alg == MBEDTLS_MD_NONE )
  171. return( -1 );
  172. if( ( profile->allowed_mds & MBEDTLS_X509_ID_FLAG( md_alg ) ) != 0 )
  173. return( 0 );
  174. return( -1 );
  175. }
  176. /*
  177. * Check pk_alg against profile
  178. * Return 0 if pk_alg is acceptable for this profile, -1 otherwise
  179. */
  180. static int x509_profile_check_pk_alg( const mbedtls_x509_crt_profile *profile,
  181. mbedtls_pk_type_t pk_alg )
  182. {
  183. if( pk_alg == MBEDTLS_PK_NONE )
  184. return( -1 );
  185. if( ( profile->allowed_pks & MBEDTLS_X509_ID_FLAG( pk_alg ) ) != 0 )
  186. return( 0 );
  187. return( -1 );
  188. }
  189. /*
  190. * Check key against profile
  191. * Return 0 if pk is acceptable for this profile, -1 otherwise
  192. */
  193. static int x509_profile_check_key( const mbedtls_x509_crt_profile *profile,
  194. const mbedtls_pk_context *pk )
  195. {
  196. const mbedtls_pk_type_t pk_alg = mbedtls_pk_get_type( pk );
  197. #if defined(MBEDTLS_RSA_C)
  198. if( pk_alg == MBEDTLS_PK_RSA || pk_alg == MBEDTLS_PK_RSASSA_PSS )
  199. {
  200. if( mbedtls_pk_get_bitlen( pk ) >= profile->rsa_min_bitlen )
  201. return( 0 );
  202. return( -1 );
  203. }
  204. #endif
  205. #if defined(MBEDTLS_ECP_C)
  206. if( pk_alg == MBEDTLS_PK_ECDSA ||
  207. pk_alg == MBEDTLS_PK_ECKEY ||
  208. pk_alg == MBEDTLS_PK_ECKEY_DH )
  209. {
  210. const mbedtls_ecp_group_id gid = mbedtls_pk_ec( *pk )->grp.id;
  211. if( gid == MBEDTLS_ECP_DP_NONE )
  212. return( -1 );
  213. if( ( profile->allowed_curves & MBEDTLS_X509_ID_FLAG( gid ) ) != 0 )
  214. return( 0 );
  215. return( -1 );
  216. }
  217. #endif
  218. return( -1 );
  219. }
  220. /*
  221. * Like memcmp, but case-insensitive and always returns -1 if different
  222. */
  223. static int x509_memcasecmp( const void *s1, const void *s2, size_t len )
  224. {
  225. size_t i;
  226. unsigned char diff;
  227. const unsigned char *n1 = s1, *n2 = s2;
  228. for( i = 0; i < len; i++ )
  229. {
  230. diff = n1[i] ^ n2[i];
  231. if( diff == 0 )
  232. continue;
  233. if( diff == 32 &&
  234. ( ( n1[i] >= 'a' && n1[i] <= 'z' ) ||
  235. ( n1[i] >= 'A' && n1[i] <= 'Z' ) ) )
  236. {
  237. continue;
  238. }
  239. return( -1 );
  240. }
  241. return( 0 );
  242. }
  243. /*
  244. * Return 0 if name matches wildcard, -1 otherwise
  245. */
  246. static int x509_check_wildcard( const char *cn, const mbedtls_x509_buf *name )
  247. {
  248. size_t i;
  249. size_t cn_idx = 0, cn_len = strlen( cn );
  250. /* We can't have a match if there is no wildcard to match */
  251. if( name->len < 3 || name->p[0] != '*' || name->p[1] != '.' )
  252. return( -1 );
  253. for( i = 0; i < cn_len; ++i )
  254. {
  255. if( cn[i] == '.' )
  256. {
  257. cn_idx = i;
  258. break;
  259. }
  260. }
  261. if( cn_idx == 0 )
  262. return( -1 );
  263. if( cn_len - cn_idx == name->len - 1 &&
  264. x509_memcasecmp( name->p + 1, cn + cn_idx, name->len - 1 ) == 0 )
  265. {
  266. return( 0 );
  267. }
  268. return( -1 );
  269. }
  270. /*
  271. * Compare two X.509 strings, case-insensitive, and allowing for some encoding
  272. * variations (but not all).
  273. *
  274. * Return 0 if equal, -1 otherwise.
  275. */
  276. static int x509_string_cmp( const mbedtls_x509_buf *a, const mbedtls_x509_buf *b )
  277. {
  278. if( a->tag == b->tag &&
  279. a->len == b->len &&
  280. memcmp( a->p, b->p, b->len ) == 0 )
  281. {
  282. return( 0 );
  283. }
  284. if( ( a->tag == MBEDTLS_ASN1_UTF8_STRING || a->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  285. ( b->tag == MBEDTLS_ASN1_UTF8_STRING || b->tag == MBEDTLS_ASN1_PRINTABLE_STRING ) &&
  286. a->len == b->len &&
  287. x509_memcasecmp( a->p, b->p, b->len ) == 0 )
  288. {
  289. return( 0 );
  290. }
  291. return( -1 );
  292. }
  293. /*
  294. * Compare two X.509 Names (aka rdnSequence).
  295. *
  296. * See RFC 5280 section 7.1, though we don't implement the whole algorithm:
  297. * we sometimes return unequal when the full algorithm would return equal,
  298. * but never the other way. (In particular, we don't do Unicode normalisation
  299. * or space folding.)
  300. *
  301. * Return 0 if equal, -1 otherwise.
  302. */
  303. static int x509_name_cmp( const mbedtls_x509_name *a, const mbedtls_x509_name *b )
  304. {
  305. /* Avoid recursion, it might not be optimised by the compiler */
  306. while( a != NULL || b != NULL )
  307. {
  308. if( a == NULL || b == NULL )
  309. return( -1 );
  310. /* type */
  311. if( a->oid.tag != b->oid.tag ||
  312. a->oid.len != b->oid.len ||
  313. memcmp( a->oid.p, b->oid.p, b->oid.len ) != 0 )
  314. {
  315. return( -1 );
  316. }
  317. /* value */
  318. if( x509_string_cmp( &a->val, &b->val ) != 0 )
  319. return( -1 );
  320. /* structure of the list of sets */
  321. if( a->next_merged != b->next_merged )
  322. return( -1 );
  323. a = a->next;
  324. b = b->next;
  325. }
  326. /* a == NULL == b */
  327. return( 0 );
  328. }
  329. /*
  330. * Reset (init or clear) a verify_chain
  331. */
  332. static void x509_crt_verify_chain_reset(
  333. mbedtls_x509_crt_verify_chain *ver_chain )
  334. {
  335. size_t i;
  336. for( i = 0; i < MBEDTLS_X509_MAX_VERIFY_CHAIN_SIZE; i++ )
  337. {
  338. ver_chain->items[i].crt = NULL;
  339. ver_chain->items[i].flags = (uint32_t) -1;
  340. }
  341. ver_chain->len = 0;
  342. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  343. ver_chain->trust_ca_cb_result = NULL;
  344. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  345. }
  346. /*
  347. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  348. */
  349. static int x509_get_version( unsigned char **p,
  350. const unsigned char *end,
  351. int *ver )
  352. {
  353. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  354. size_t len;
  355. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  356. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ) != 0 )
  357. {
  358. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  359. {
  360. *ver = 0;
  361. return( 0 );
  362. }
  363. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
  364. }
  365. end = *p + len;
  366. if( ( ret = mbedtls_asn1_get_int( p, end, ver ) ) != 0 )
  367. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION, ret ) );
  368. if( *p != end )
  369. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_VERSION,
  370. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  371. return( 0 );
  372. }
  373. /*
  374. * Validity ::= SEQUENCE {
  375. * notBefore Time,
  376. * notAfter Time }
  377. */
  378. static int x509_get_dates( unsigned char **p,
  379. const unsigned char *end,
  380. mbedtls_x509_time *from,
  381. mbedtls_x509_time *to )
  382. {
  383. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  384. size_t len;
  385. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  386. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  387. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE, ret ) );
  388. end = *p + len;
  389. if( ( ret = mbedtls_x509_get_time( p, end, from ) ) != 0 )
  390. return( ret );
  391. if( ( ret = mbedtls_x509_get_time( p, end, to ) ) != 0 )
  392. return( ret );
  393. if( *p != end )
  394. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_DATE,
  395. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  396. return( 0 );
  397. }
  398. /*
  399. * X.509 v2/v3 unique identifier (not parsed)
  400. */
  401. static int x509_get_uid( unsigned char **p,
  402. const unsigned char *end,
  403. mbedtls_x509_buf *uid, int n )
  404. {
  405. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  406. if( *p == end )
  407. return( 0 );
  408. uid->tag = **p;
  409. if( ( ret = mbedtls_asn1_get_tag( p, end, &uid->len,
  410. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | n ) ) != 0 )
  411. {
  412. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  413. return( 0 );
  414. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
  415. }
  416. uid->p = *p;
  417. *p += uid->len;
  418. return( 0 );
  419. }
  420. static int x509_get_basic_constraints( unsigned char **p,
  421. const unsigned char *end,
  422. int *ca_istrue,
  423. int *max_pathlen )
  424. {
  425. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  426. size_t len;
  427. /*
  428. * BasicConstraints ::= SEQUENCE {
  429. * cA BOOLEAN DEFAULT FALSE,
  430. * pathLenConstraint INTEGER (0..MAX) OPTIONAL }
  431. */
  432. *ca_istrue = 0; /* DEFAULT FALSE */
  433. *max_pathlen = 0; /* endless */
  434. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  435. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  436. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  437. if( *p == end )
  438. return( 0 );
  439. if( ( ret = mbedtls_asn1_get_bool( p, end, ca_istrue ) ) != 0 )
  440. {
  441. if( ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG )
  442. ret = mbedtls_asn1_get_int( p, end, ca_istrue );
  443. if( ret != 0 )
  444. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  445. if( *ca_istrue != 0 )
  446. *ca_istrue = 1;
  447. }
  448. if( *p == end )
  449. return( 0 );
  450. if( ( ret = mbedtls_asn1_get_int( p, end, max_pathlen ) ) != 0 )
  451. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  452. if( *p != end )
  453. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  454. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  455. /* Do not accept max_pathlen equal to INT_MAX to avoid a signed integer
  456. * overflow, which is an undefined behavior. */
  457. if( *max_pathlen == INT_MAX )
  458. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  459. MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
  460. (*max_pathlen)++;
  461. return( 0 );
  462. }
  463. static int x509_get_ns_cert_type( unsigned char **p,
  464. const unsigned char *end,
  465. unsigned char *ns_cert_type)
  466. {
  467. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  468. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  469. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  470. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  471. if( bs.len != 1 )
  472. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  473. MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
  474. /* Get actual bitstring */
  475. *ns_cert_type = *bs.p;
  476. return( 0 );
  477. }
  478. static int x509_get_key_usage( unsigned char **p,
  479. const unsigned char *end,
  480. unsigned int *key_usage)
  481. {
  482. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  483. size_t i;
  484. mbedtls_x509_bitstring bs = { 0, 0, NULL };
  485. if( ( ret = mbedtls_asn1_get_bitstring( p, end, &bs ) ) != 0 )
  486. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  487. if( bs.len < 1 )
  488. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  489. MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
  490. /* Get actual bitstring */
  491. *key_usage = 0;
  492. for( i = 0; i < bs.len && i < sizeof( unsigned int ); i++ )
  493. {
  494. *key_usage |= (unsigned int) bs.p[i] << (8*i);
  495. }
  496. return( 0 );
  497. }
  498. /*
  499. * ExtKeyUsageSyntax ::= SEQUENCE SIZE (1..MAX) OF KeyPurposeId
  500. *
  501. * KeyPurposeId ::= OBJECT IDENTIFIER
  502. */
  503. static int x509_get_ext_key_usage( unsigned char **p,
  504. const unsigned char *end,
  505. mbedtls_x509_sequence *ext_key_usage)
  506. {
  507. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  508. if( ( ret = mbedtls_asn1_get_sequence_of( p, end, ext_key_usage, MBEDTLS_ASN1_OID ) ) != 0 )
  509. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  510. /* Sequence length must be >= 1 */
  511. if( ext_key_usage->buf.p == NULL )
  512. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  513. MBEDTLS_ERR_ASN1_INVALID_LENGTH ) );
  514. return( 0 );
  515. }
  516. /*
  517. * SubjectAltName ::= GeneralNames
  518. *
  519. * GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
  520. *
  521. * GeneralName ::= CHOICE {
  522. * otherName [0] OtherName,
  523. * rfc822Name [1] IA5String,
  524. * dNSName [2] IA5String,
  525. * x400Address [3] ORAddress,
  526. * directoryName [4] Name,
  527. * ediPartyName [5] EDIPartyName,
  528. * uniformResourceIdentifier [6] IA5String,
  529. * iPAddress [7] OCTET STRING,
  530. * registeredID [8] OBJECT IDENTIFIER }
  531. *
  532. * OtherName ::= SEQUENCE {
  533. * type-id OBJECT IDENTIFIER,
  534. * value [0] EXPLICIT ANY DEFINED BY type-id }
  535. *
  536. * EDIPartyName ::= SEQUENCE {
  537. * nameAssigner [0] DirectoryString OPTIONAL,
  538. * partyName [1] DirectoryString }
  539. *
  540. * NOTE: we list all types, but only use dNSName and otherName
  541. * of type HwModuleName, as defined in RFC 4108, at this point.
  542. */
  543. static int x509_get_subject_alt_name( unsigned char **p,
  544. const unsigned char *end,
  545. mbedtls_x509_sequence *subject_alt_name )
  546. {
  547. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  548. size_t len, tag_len;
  549. mbedtls_asn1_buf *buf;
  550. unsigned char tag;
  551. mbedtls_asn1_sequence *cur = subject_alt_name;
  552. /* Get main sequence tag */
  553. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  554. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  555. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  556. if( *p + len != end )
  557. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  558. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  559. while( *p < end )
  560. {
  561. mbedtls_x509_subject_alternative_name dummy_san_buf;
  562. memset( &dummy_san_buf, 0, sizeof( dummy_san_buf ) );
  563. tag = **p;
  564. (*p)++;
  565. if( ( ret = mbedtls_asn1_get_len( p, end, &tag_len ) ) != 0 )
  566. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  567. if( ( tag & MBEDTLS_ASN1_TAG_CLASS_MASK ) !=
  568. MBEDTLS_ASN1_CONTEXT_SPECIFIC )
  569. {
  570. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  571. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  572. }
  573. /*
  574. * Check that the SAN is structured correctly.
  575. */
  576. ret = mbedtls_x509_parse_subject_alt_name( &(cur->buf), &dummy_san_buf );
  577. /*
  578. * In case the extension is malformed, return an error,
  579. * and clear the allocated sequences.
  580. */
  581. if( ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
  582. {
  583. mbedtls_x509_sequence *seq_cur = subject_alt_name->next;
  584. mbedtls_x509_sequence *seq_prv;
  585. while( seq_cur != NULL )
  586. {
  587. seq_prv = seq_cur;
  588. seq_cur = seq_cur->next;
  589. mbedtls_platform_zeroize( seq_prv,
  590. sizeof( mbedtls_x509_sequence ) );
  591. mbedtls_free( seq_prv );
  592. }
  593. subject_alt_name->next = NULL;
  594. return( ret );
  595. }
  596. /* Allocate and assign next pointer */
  597. if( cur->buf.p != NULL )
  598. {
  599. if( cur->next != NULL )
  600. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  601. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
  602. if( cur->next == NULL )
  603. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  604. MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
  605. cur = cur->next;
  606. }
  607. buf = &(cur->buf);
  608. buf->tag = tag;
  609. buf->p = *p;
  610. buf->len = tag_len;
  611. *p += buf->len;
  612. }
  613. /* Set final sequence entry's next pointer to NULL */
  614. cur->next = NULL;
  615. if( *p != end )
  616. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  617. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  618. return( 0 );
  619. }
  620. /*
  621. * id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
  622. *
  623. * anyPolicy OBJECT IDENTIFIER ::= { id-ce-certificatePolicies 0 }
  624. *
  625. * certificatePolicies ::= SEQUENCE SIZE (1..MAX) OF PolicyInformation
  626. *
  627. * PolicyInformation ::= SEQUENCE {
  628. * policyIdentifier CertPolicyId,
  629. * policyQualifiers SEQUENCE SIZE (1..MAX) OF
  630. * PolicyQualifierInfo OPTIONAL }
  631. *
  632. * CertPolicyId ::= OBJECT IDENTIFIER
  633. *
  634. * PolicyQualifierInfo ::= SEQUENCE {
  635. * policyQualifierId PolicyQualifierId,
  636. * qualifier ANY DEFINED BY policyQualifierId }
  637. *
  638. * -- policyQualifierIds for Internet policy qualifiers
  639. *
  640. * id-qt OBJECT IDENTIFIER ::= { id-pkix 2 }
  641. * id-qt-cps OBJECT IDENTIFIER ::= { id-qt 1 }
  642. * id-qt-unotice OBJECT IDENTIFIER ::= { id-qt 2 }
  643. *
  644. * PolicyQualifierId ::= OBJECT IDENTIFIER ( id-qt-cps | id-qt-unotice )
  645. *
  646. * Qualifier ::= CHOICE {
  647. * cPSuri CPSuri,
  648. * userNotice UserNotice }
  649. *
  650. * CPSuri ::= IA5String
  651. *
  652. * UserNotice ::= SEQUENCE {
  653. * noticeRef NoticeReference OPTIONAL,
  654. * explicitText DisplayText OPTIONAL }
  655. *
  656. * NoticeReference ::= SEQUENCE {
  657. * organization DisplayText,
  658. * noticeNumbers SEQUENCE OF INTEGER }
  659. *
  660. * DisplayText ::= CHOICE {
  661. * ia5String IA5String (SIZE (1..200)),
  662. * visibleString VisibleString (SIZE (1..200)),
  663. * bmpString BMPString (SIZE (1..200)),
  664. * utf8String UTF8String (SIZE (1..200)) }
  665. *
  666. * NOTE: we only parse and use anyPolicy without qualifiers at this point
  667. * as defined in RFC 5280.
  668. */
  669. static int x509_get_certificate_policies( unsigned char **p,
  670. const unsigned char *end,
  671. mbedtls_x509_sequence *certificate_policies )
  672. {
  673. int ret, parse_ret = 0;
  674. size_t len;
  675. mbedtls_asn1_buf *buf;
  676. mbedtls_asn1_sequence *cur = certificate_policies;
  677. /* Get main sequence tag */
  678. ret = mbedtls_asn1_get_tag( p, end, &len,
  679. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE );
  680. if( ret != 0 )
  681. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  682. if( *p + len != end )
  683. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  684. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  685. /*
  686. * Cannot be an empty sequence.
  687. */
  688. if( len == 0 )
  689. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  690. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  691. while( *p < end )
  692. {
  693. mbedtls_x509_buf policy_oid;
  694. const unsigned char *policy_end;
  695. /*
  696. * Get the policy sequence
  697. */
  698. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  699. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  700. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  701. policy_end = *p + len;
  702. if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
  703. MBEDTLS_ASN1_OID ) ) != 0 )
  704. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  705. policy_oid.tag = MBEDTLS_ASN1_OID;
  706. policy_oid.len = len;
  707. policy_oid.p = *p;
  708. /*
  709. * Only AnyPolicy is currently supported when enforcing policy.
  710. */
  711. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_POLICY, &policy_oid ) != 0 )
  712. {
  713. /*
  714. * Set the parsing return code but continue parsing, in case this
  715. * extension is critical.
  716. */
  717. parse_ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
  718. }
  719. /* Allocate and assign next pointer */
  720. if( cur->buf.p != NULL )
  721. {
  722. if( cur->next != NULL )
  723. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  724. cur->next = mbedtls_calloc( 1, sizeof( mbedtls_asn1_sequence ) );
  725. if( cur->next == NULL )
  726. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  727. MBEDTLS_ERR_ASN1_ALLOC_FAILED ) );
  728. cur = cur->next;
  729. }
  730. buf = &( cur->buf );
  731. buf->tag = policy_oid.tag;
  732. buf->p = policy_oid.p;
  733. buf->len = policy_oid.len;
  734. *p += len;
  735. /*
  736. * If there is an optional qualifier, then *p < policy_end
  737. * Check the Qualifier len to verify it doesn't exceed policy_end.
  738. */
  739. if( *p < policy_end )
  740. {
  741. if( ( ret = mbedtls_asn1_get_tag( p, policy_end, &len,
  742. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  743. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  744. /*
  745. * Skip the optional policy qualifiers.
  746. */
  747. *p += len;
  748. }
  749. if( *p != policy_end )
  750. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  751. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  752. }
  753. /* Set final sequence entry's next pointer to NULL */
  754. cur->next = NULL;
  755. if( *p != end )
  756. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  757. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  758. return( parse_ret );
  759. }
  760. /*
  761. * X.509 v3 extensions
  762. *
  763. */
  764. static int x509_get_crt_ext( unsigned char **p,
  765. const unsigned char *end,
  766. mbedtls_x509_crt *crt,
  767. mbedtls_x509_crt_ext_cb_t cb,
  768. void *p_ctx )
  769. {
  770. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  771. size_t len;
  772. unsigned char *end_ext_data, *start_ext_octet, *end_ext_octet;
  773. if( *p == end )
  774. return( 0 );
  775. if( ( ret = mbedtls_x509_get_ext( p, end, &crt->v3_ext, 3 ) ) != 0 )
  776. return( ret );
  777. end = crt->v3_ext.p + crt->v3_ext.len;
  778. while( *p < end )
  779. {
  780. /*
  781. * Extension ::= SEQUENCE {
  782. * extnID OBJECT IDENTIFIER,
  783. * critical BOOLEAN DEFAULT FALSE,
  784. * extnValue OCTET STRING }
  785. */
  786. mbedtls_x509_buf extn_oid = {0, 0, NULL};
  787. int is_critical = 0; /* DEFAULT FALSE */
  788. int ext_type = 0;
  789. if( ( ret = mbedtls_asn1_get_tag( p, end, &len,
  790. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  791. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  792. end_ext_data = *p + len;
  793. /* Get extension ID */
  794. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &extn_oid.len,
  795. MBEDTLS_ASN1_OID ) ) != 0 )
  796. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  797. extn_oid.tag = MBEDTLS_ASN1_OID;
  798. extn_oid.p = *p;
  799. *p += extn_oid.len;
  800. /* Get optional critical */
  801. if( ( ret = mbedtls_asn1_get_bool( p, end_ext_data, &is_critical ) ) != 0 &&
  802. ( ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) )
  803. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  804. /* Data should be octet string type */
  805. if( ( ret = mbedtls_asn1_get_tag( p, end_ext_data, &len,
  806. MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  807. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  808. start_ext_octet = *p;
  809. end_ext_octet = *p + len;
  810. if( end_ext_octet != end_ext_data )
  811. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  812. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  813. /*
  814. * Detect supported extensions
  815. */
  816. ret = mbedtls_oid_get_x509_ext_type( &extn_oid, &ext_type );
  817. if( ret != 0 )
  818. {
  819. /* Give the callback (if any) a chance to handle the extension */
  820. if( cb != NULL )
  821. {
  822. ret = cb( p_ctx, crt, &extn_oid, is_critical, *p, end_ext_octet );
  823. if( ret != 0 && is_critical )
  824. return( ret );
  825. *p = end_ext_octet;
  826. continue;
  827. }
  828. /* No parser found, skip extension */
  829. *p = end_ext_octet;
  830. if( is_critical )
  831. {
  832. /* Data is marked as critical: fail */
  833. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  834. MBEDTLS_ERR_ASN1_UNEXPECTED_TAG ) );
  835. }
  836. continue;
  837. }
  838. /* Forbid repeated extensions */
  839. if( ( crt->ext_types & ext_type ) != 0 )
  840. return( MBEDTLS_ERR_X509_INVALID_EXTENSIONS );
  841. crt->ext_types |= ext_type;
  842. switch( ext_type )
  843. {
  844. case MBEDTLS_X509_EXT_BASIC_CONSTRAINTS:
  845. /* Parse basic constraints */
  846. if( ( ret = x509_get_basic_constraints( p, end_ext_octet,
  847. &crt->ca_istrue, &crt->max_pathlen ) ) != 0 )
  848. return( ret );
  849. break;
  850. case MBEDTLS_X509_EXT_KEY_USAGE:
  851. /* Parse key usage */
  852. if( ( ret = x509_get_key_usage( p, end_ext_octet,
  853. &crt->key_usage ) ) != 0 )
  854. return( ret );
  855. break;
  856. case MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE:
  857. /* Parse extended key usage */
  858. if( ( ret = x509_get_ext_key_usage( p, end_ext_octet,
  859. &crt->ext_key_usage ) ) != 0 )
  860. return( ret );
  861. break;
  862. case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
  863. /* Parse subject alt name */
  864. if( ( ret = x509_get_subject_alt_name( p, end_ext_octet,
  865. &crt->subject_alt_names ) ) != 0 )
  866. return( ret );
  867. break;
  868. case MBEDTLS_X509_EXT_NS_CERT_TYPE:
  869. /* Parse netscape certificate type */
  870. if( ( ret = x509_get_ns_cert_type( p, end_ext_octet,
  871. &crt->ns_cert_type ) ) != 0 )
  872. return( ret );
  873. break;
  874. case MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES:
  875. /* Parse certificate policies type */
  876. if( ( ret = x509_get_certificate_policies( p, end_ext_octet,
  877. &crt->certificate_policies ) ) != 0 )
  878. {
  879. /* Give the callback (if any) a chance to handle the extension
  880. * if it contains unsupported policies */
  881. if( ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE && cb != NULL &&
  882. cb( p_ctx, crt, &extn_oid, is_critical,
  883. start_ext_octet, end_ext_octet ) == 0 )
  884. break;
  885. if( is_critical )
  886. return( ret );
  887. else
  888. /*
  889. * If MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE is returned, then we
  890. * cannot interpret or enforce the policy. However, it is up to
  891. * the user to choose how to enforce the policies,
  892. * unless the extension is critical.
  893. */
  894. if( ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
  895. return( ret );
  896. }
  897. break;
  898. default:
  899. /*
  900. * If this is a non-critical extension, which the oid layer
  901. * supports, but there isn't an x509 parser for it,
  902. * skip the extension.
  903. */
  904. if( is_critical )
  905. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  906. else
  907. *p = end_ext_octet;
  908. }
  909. }
  910. if( *p != end )
  911. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  912. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  913. return( 0 );
  914. }
  915. /*
  916. * Parse and fill a single X.509 certificate in DER format
  917. */
  918. static int x509_crt_parse_der_core( mbedtls_x509_crt *crt,
  919. const unsigned char *buf,
  920. size_t buflen,
  921. int make_copy,
  922. mbedtls_x509_crt_ext_cb_t cb,
  923. void *p_ctx )
  924. {
  925. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  926. size_t len;
  927. unsigned char *p, *end, *crt_end;
  928. mbedtls_x509_buf sig_params1, sig_params2, sig_oid2;
  929. memset( &sig_params1, 0, sizeof( mbedtls_x509_buf ) );
  930. memset( &sig_params2, 0, sizeof( mbedtls_x509_buf ) );
  931. memset( &sig_oid2, 0, sizeof( mbedtls_x509_buf ) );
  932. /*
  933. * Check for valid input
  934. */
  935. if( crt == NULL || buf == NULL )
  936. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  937. /* Use the original buffer until we figure out actual length. */
  938. p = (unsigned char*) buf;
  939. len = buflen;
  940. end = p + len;
  941. /*
  942. * Certificate ::= SEQUENCE {
  943. * tbsCertificate TBSCertificate,
  944. * signatureAlgorithm AlgorithmIdentifier,
  945. * signatureValue BIT STRING }
  946. */
  947. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  948. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  949. {
  950. mbedtls_x509_crt_free( crt );
  951. return( MBEDTLS_ERR_X509_INVALID_FORMAT );
  952. }
  953. end = crt_end = p + len;
  954. crt->raw.len = crt_end - buf;
  955. if( make_copy != 0 )
  956. {
  957. /* Create and populate a new buffer for the raw field. */
  958. crt->raw.p = p = mbedtls_calloc( 1, crt->raw.len );
  959. if( crt->raw.p == NULL )
  960. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  961. memcpy( crt->raw.p, buf, crt->raw.len );
  962. crt->own_buffer = 1;
  963. p += crt->raw.len - len;
  964. end = crt_end = p + len;
  965. }
  966. else
  967. {
  968. crt->raw.p = (unsigned char*) buf;
  969. crt->own_buffer = 0;
  970. }
  971. /*
  972. * TBSCertificate ::= SEQUENCE {
  973. */
  974. crt->tbs.p = p;
  975. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  976. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  977. {
  978. mbedtls_x509_crt_free( crt );
  979. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
  980. }
  981. end = p + len;
  982. crt->tbs.len = end - crt->tbs.p;
  983. /*
  984. * Version ::= INTEGER { v1(0), v2(1), v3(2) }
  985. *
  986. * CertificateSerialNumber ::= INTEGER
  987. *
  988. * signature AlgorithmIdentifier
  989. */
  990. if( ( ret = x509_get_version( &p, end, &crt->version ) ) != 0 ||
  991. ( ret = mbedtls_x509_get_serial( &p, end, &crt->serial ) ) != 0 ||
  992. ( ret = mbedtls_x509_get_alg( &p, end, &crt->sig_oid,
  993. &sig_params1 ) ) != 0 )
  994. {
  995. mbedtls_x509_crt_free( crt );
  996. return( ret );
  997. }
  998. if( crt->version < 0 || crt->version > 2 )
  999. {
  1000. mbedtls_x509_crt_free( crt );
  1001. return( MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  1002. }
  1003. crt->version++;
  1004. if( ( ret = mbedtls_x509_get_sig_alg( &crt->sig_oid, &sig_params1,
  1005. &crt->sig_md, &crt->sig_pk,
  1006. &crt->sig_opts ) ) != 0 )
  1007. {
  1008. mbedtls_x509_crt_free( crt );
  1009. return( ret );
  1010. }
  1011. /*
  1012. * issuer Name
  1013. */
  1014. crt->issuer_raw.p = p;
  1015. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1016. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  1017. {
  1018. mbedtls_x509_crt_free( crt );
  1019. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
  1020. }
  1021. if( ( ret = mbedtls_x509_get_name( &p, p + len, &crt->issuer ) ) != 0 )
  1022. {
  1023. mbedtls_x509_crt_free( crt );
  1024. return( ret );
  1025. }
  1026. crt->issuer_raw.len = p - crt->issuer_raw.p;
  1027. /*
  1028. * Validity ::= SEQUENCE {
  1029. * notBefore Time,
  1030. * notAfter Time }
  1031. *
  1032. */
  1033. if( ( ret = x509_get_dates( &p, end, &crt->valid_from,
  1034. &crt->valid_to ) ) != 0 )
  1035. {
  1036. mbedtls_x509_crt_free( crt );
  1037. return( ret );
  1038. }
  1039. /*
  1040. * subject Name
  1041. */
  1042. crt->subject_raw.p = p;
  1043. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1044. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  1045. {
  1046. mbedtls_x509_crt_free( crt );
  1047. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT, ret ) );
  1048. }
  1049. if( len && ( ret = mbedtls_x509_get_name( &p, p + len, &crt->subject ) ) != 0 )
  1050. {
  1051. mbedtls_x509_crt_free( crt );
  1052. return( ret );
  1053. }
  1054. crt->subject_raw.len = p - crt->subject_raw.p;
  1055. /*
  1056. * SubjectPublicKeyInfo
  1057. */
  1058. crt->pk_raw.p = p;
  1059. if( ( ret = mbedtls_pk_parse_subpubkey( &p, end, &crt->pk ) ) != 0 )
  1060. {
  1061. mbedtls_x509_crt_free( crt );
  1062. return( ret );
  1063. }
  1064. crt->pk_raw.len = p - crt->pk_raw.p;
  1065. /*
  1066. * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
  1067. * -- If present, version shall be v2 or v3
  1068. * subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
  1069. * -- If present, version shall be v2 or v3
  1070. * extensions [3] EXPLICIT Extensions OPTIONAL
  1071. * -- If present, version shall be v3
  1072. */
  1073. if( crt->version == 2 || crt->version == 3 )
  1074. {
  1075. ret = x509_get_uid( &p, end, &crt->issuer_id, 1 );
  1076. if( ret != 0 )
  1077. {
  1078. mbedtls_x509_crt_free( crt );
  1079. return( ret );
  1080. }
  1081. }
  1082. if( crt->version == 2 || crt->version == 3 )
  1083. {
  1084. ret = x509_get_uid( &p, end, &crt->subject_id, 2 );
  1085. if( ret != 0 )
  1086. {
  1087. mbedtls_x509_crt_free( crt );
  1088. return( ret );
  1089. }
  1090. }
  1091. if( crt->version == 3 )
  1092. {
  1093. ret = x509_get_crt_ext( &p, end, crt, cb, p_ctx );
  1094. if( ret != 0 )
  1095. {
  1096. mbedtls_x509_crt_free( crt );
  1097. return( ret );
  1098. }
  1099. }
  1100. if( p != end )
  1101. {
  1102. mbedtls_x509_crt_free( crt );
  1103. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
  1104. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  1105. }
  1106. end = crt_end;
  1107. /*
  1108. * }
  1109. * -- end of TBSCertificate
  1110. *
  1111. * signatureAlgorithm AlgorithmIdentifier,
  1112. * signatureValue BIT STRING
  1113. */
  1114. if( ( ret = mbedtls_x509_get_alg( &p, end, &sig_oid2, &sig_params2 ) ) != 0 )
  1115. {
  1116. mbedtls_x509_crt_free( crt );
  1117. return( ret );
  1118. }
  1119. if( crt->sig_oid.len != sig_oid2.len ||
  1120. memcmp( crt->sig_oid.p, sig_oid2.p, crt->sig_oid.len ) != 0 ||
  1121. sig_params1.tag != sig_params2.tag ||
  1122. sig_params1.len != sig_params2.len ||
  1123. ( sig_params1.len != 0 &&
  1124. memcmp( sig_params1.p, sig_params2.p, sig_params1.len ) != 0 ) )
  1125. {
  1126. mbedtls_x509_crt_free( crt );
  1127. return( MBEDTLS_ERR_X509_SIG_MISMATCH );
  1128. }
  1129. if( ( ret = mbedtls_x509_get_sig( &p, end, &crt->sig ) ) != 0 )
  1130. {
  1131. mbedtls_x509_crt_free( crt );
  1132. return( ret );
  1133. }
  1134. if( p != end )
  1135. {
  1136. mbedtls_x509_crt_free( crt );
  1137. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_FORMAT,
  1138. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  1139. }
  1140. return( 0 );
  1141. }
  1142. /*
  1143. * Parse one X.509 certificate in DER format from a buffer and add them to a
  1144. * chained list
  1145. */
  1146. static int mbedtls_x509_crt_parse_der_internal( mbedtls_x509_crt *chain,
  1147. const unsigned char *buf,
  1148. size_t buflen,
  1149. int make_copy,
  1150. mbedtls_x509_crt_ext_cb_t cb,
  1151. void *p_ctx )
  1152. {
  1153. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1154. mbedtls_x509_crt *crt = chain, *prev = NULL;
  1155. /*
  1156. * Check for valid input
  1157. */
  1158. if( crt == NULL || buf == NULL )
  1159. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1160. while( crt->version != 0 && crt->next != NULL )
  1161. {
  1162. prev = crt;
  1163. crt = crt->next;
  1164. }
  1165. /*
  1166. * Add new certificate on the end of the chain if needed.
  1167. */
  1168. if( crt->version != 0 && crt->next == NULL )
  1169. {
  1170. crt->next = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  1171. if( crt->next == NULL )
  1172. return( MBEDTLS_ERR_X509_ALLOC_FAILED );
  1173. prev = crt;
  1174. mbedtls_x509_crt_init( crt->next );
  1175. crt = crt->next;
  1176. }
  1177. ret = x509_crt_parse_der_core( crt, buf, buflen, make_copy, cb, p_ctx );
  1178. if( ret != 0 )
  1179. {
  1180. if( prev )
  1181. prev->next = NULL;
  1182. if( crt != chain )
  1183. mbedtls_free( crt );
  1184. return( ret );
  1185. }
  1186. return( 0 );
  1187. }
  1188. int mbedtls_x509_crt_parse_der_nocopy( mbedtls_x509_crt *chain,
  1189. const unsigned char *buf,
  1190. size_t buflen )
  1191. {
  1192. return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 0, NULL, NULL ) );
  1193. }
  1194. int mbedtls_x509_crt_parse_der_with_ext_cb( mbedtls_x509_crt *chain,
  1195. const unsigned char *buf,
  1196. size_t buflen,
  1197. int make_copy,
  1198. mbedtls_x509_crt_ext_cb_t cb,
  1199. void *p_ctx )
  1200. {
  1201. return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, make_copy, cb, p_ctx ) );
  1202. }
  1203. int mbedtls_x509_crt_parse_der( mbedtls_x509_crt *chain,
  1204. const unsigned char *buf,
  1205. size_t buflen )
  1206. {
  1207. return( mbedtls_x509_crt_parse_der_internal( chain, buf, buflen, 1, NULL, NULL ) );
  1208. }
  1209. /*
  1210. * Parse one or more PEM certificates from a buffer and add them to the chained
  1211. * list
  1212. */
  1213. int mbedtls_x509_crt_parse( mbedtls_x509_crt *chain,
  1214. const unsigned char *buf,
  1215. size_t buflen )
  1216. {
  1217. #if defined(MBEDTLS_PEM_PARSE_C)
  1218. int success = 0, first_error = 0, total_failed = 0;
  1219. int buf_format = MBEDTLS_X509_FORMAT_DER;
  1220. #endif
  1221. /*
  1222. * Check for valid input
  1223. */
  1224. if( chain == NULL || buf == NULL )
  1225. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1226. /*
  1227. * Determine buffer content. Buffer contains either one DER certificate or
  1228. * one or more PEM certificates.
  1229. */
  1230. #if defined(MBEDTLS_PEM_PARSE_C)
  1231. if( buflen != 0 && buf[buflen - 1] == '\0' &&
  1232. strstr( (const char *) buf, "-----BEGIN CERTIFICATE-----" ) != NULL )
  1233. {
  1234. buf_format = MBEDTLS_X509_FORMAT_PEM;
  1235. }
  1236. if( buf_format == MBEDTLS_X509_FORMAT_DER )
  1237. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  1238. #else
  1239. return mbedtls_x509_crt_parse_der( chain, buf, buflen );
  1240. #endif
  1241. #if defined(MBEDTLS_PEM_PARSE_C)
  1242. if( buf_format == MBEDTLS_X509_FORMAT_PEM )
  1243. {
  1244. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1245. mbedtls_pem_context pem;
  1246. /* 1 rather than 0 since the terminating NULL byte is counted in */
  1247. while( buflen > 1 )
  1248. {
  1249. size_t use_len;
  1250. mbedtls_pem_init( &pem );
  1251. /* If we get there, we know the string is null-terminated */
  1252. ret = mbedtls_pem_read_buffer( &pem,
  1253. "-----BEGIN CERTIFICATE-----",
  1254. "-----END CERTIFICATE-----",
  1255. buf, NULL, 0, &use_len );
  1256. if( ret == 0 )
  1257. {
  1258. /*
  1259. * Was PEM encoded
  1260. */
  1261. buflen -= use_len;
  1262. buf += use_len;
  1263. }
  1264. else if( ret == MBEDTLS_ERR_PEM_BAD_INPUT_DATA )
  1265. {
  1266. return( ret );
  1267. }
  1268. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  1269. {
  1270. mbedtls_pem_free( &pem );
  1271. /*
  1272. * PEM header and footer were found
  1273. */
  1274. buflen -= use_len;
  1275. buf += use_len;
  1276. if( first_error == 0 )
  1277. first_error = ret;
  1278. total_failed++;
  1279. continue;
  1280. }
  1281. else
  1282. break;
  1283. ret = mbedtls_x509_crt_parse_der( chain, pem.buf, pem.buflen );
  1284. mbedtls_pem_free( &pem );
  1285. if( ret != 0 )
  1286. {
  1287. /*
  1288. * Quit parsing on a memory error
  1289. */
  1290. if( ret == MBEDTLS_ERR_X509_ALLOC_FAILED )
  1291. return( ret );
  1292. if( first_error == 0 )
  1293. first_error = ret;
  1294. total_failed++;
  1295. continue;
  1296. }
  1297. success = 1;
  1298. }
  1299. }
  1300. if( success )
  1301. return( total_failed );
  1302. else if( first_error )
  1303. return( first_error );
  1304. else
  1305. return( MBEDTLS_ERR_X509_CERT_UNKNOWN_FORMAT );
  1306. #endif /* MBEDTLS_PEM_PARSE_C */
  1307. }
  1308. #if defined(MBEDTLS_FS_IO)
  1309. /*
  1310. * Load one or more certificates and add them to the chained list
  1311. */
  1312. int mbedtls_x509_crt_parse_file( mbedtls_x509_crt *chain, const char *path )
  1313. {
  1314. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1315. size_t n;
  1316. unsigned char *buf;
  1317. if( ( ret = mbedtls_pk_load_file( path, &buf, &n ) ) != 0 )
  1318. return( ret );
  1319. ret = mbedtls_x509_crt_parse( chain, buf, n );
  1320. mbedtls_platform_zeroize( buf, n );
  1321. mbedtls_free( buf );
  1322. return( ret );
  1323. }
  1324. int mbedtls_x509_crt_parse_path( mbedtls_x509_crt *chain, const char *path )
  1325. {
  1326. int ret = 0;
  1327. #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
  1328. int w_ret;
  1329. WCHAR szDir[MAX_PATH];
  1330. char filename[MAX_PATH];
  1331. char *p;
  1332. size_t len = strlen( path );
  1333. WIN32_FIND_DATAW file_data;
  1334. HANDLE hFind;
  1335. if( len > MAX_PATH - 3 )
  1336. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1337. memset( szDir, 0, sizeof(szDir) );
  1338. memset( filename, 0, MAX_PATH );
  1339. memcpy( filename, path, len );
  1340. filename[len++] = '\\';
  1341. p = filename + len;
  1342. filename[len++] = '*';
  1343. w_ret = MultiByteToWideChar( CP_ACP, 0, filename, (int)len, szDir,
  1344. MAX_PATH - 3 );
  1345. if( w_ret == 0 )
  1346. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1347. hFind = FindFirstFileW( szDir, &file_data );
  1348. if( hFind == INVALID_HANDLE_VALUE )
  1349. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  1350. len = MAX_PATH - len;
  1351. do
  1352. {
  1353. memset( p, 0, len );
  1354. if( file_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
  1355. continue;
  1356. w_ret = WideCharToMultiByte( CP_ACP, 0, file_data.cFileName,
  1357. lstrlenW( file_data.cFileName ),
  1358. p, (int) len - 1,
  1359. NULL, NULL );
  1360. if( w_ret == 0 )
  1361. {
  1362. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  1363. goto cleanup;
  1364. }
  1365. w_ret = mbedtls_x509_crt_parse_file( chain, filename );
  1366. if( w_ret < 0 )
  1367. ret++;
  1368. else
  1369. ret += w_ret;
  1370. }
  1371. while( FindNextFileW( hFind, &file_data ) != 0 );
  1372. if( GetLastError() != ERROR_NO_MORE_FILES )
  1373. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  1374. cleanup:
  1375. FindClose( hFind );
  1376. #else /* _WIN32 */
  1377. int t_ret;
  1378. int snp_ret;
  1379. struct stat sb;
  1380. struct dirent *entry;
  1381. char entry_name[MBEDTLS_X509_MAX_FILE_PATH_LEN];
  1382. DIR *dir = opendir( path );
  1383. if( dir == NULL )
  1384. return( MBEDTLS_ERR_X509_FILE_IO_ERROR );
  1385. #if defined(MBEDTLS_THREADING_C)
  1386. if( ( ret = mbedtls_mutex_lock( &mbedtls_threading_readdir_mutex ) ) != 0 )
  1387. {
  1388. closedir( dir );
  1389. return( ret );
  1390. }
  1391. #endif /* MBEDTLS_THREADING_C */
  1392. memset( &sb, 0, sizeof( sb ) );
  1393. while( ( entry = readdir( dir ) ) != NULL )
  1394. {
  1395. snp_ret = mbedtls_snprintf( entry_name, sizeof entry_name,
  1396. "%s/%s", path, entry->d_name );
  1397. if( snp_ret < 0 || (size_t)snp_ret >= sizeof entry_name )
  1398. {
  1399. ret = MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
  1400. goto cleanup;
  1401. }
  1402. else if( stat( entry_name, &sb ) == -1 )
  1403. {
  1404. ret = MBEDTLS_ERR_X509_FILE_IO_ERROR;
  1405. goto cleanup;
  1406. }
  1407. if( !S_ISREG( sb.st_mode ) )
  1408. continue;
  1409. // Ignore parse errors
  1410. //
  1411. t_ret = mbedtls_x509_crt_parse_file( chain, entry_name );
  1412. if( t_ret < 0 )
  1413. ret++;
  1414. else
  1415. ret += t_ret;
  1416. }
  1417. cleanup:
  1418. closedir( dir );
  1419. #if defined(MBEDTLS_THREADING_C)
  1420. if( mbedtls_mutex_unlock( &mbedtls_threading_readdir_mutex ) != 0 )
  1421. ret = MBEDTLS_ERR_THREADING_MUTEX_ERROR;
  1422. #endif /* MBEDTLS_THREADING_C */
  1423. #endif /* _WIN32 */
  1424. return( ret );
  1425. }
  1426. #endif /* MBEDTLS_FS_IO */
  1427. /*
  1428. * OtherName ::= SEQUENCE {
  1429. * type-id OBJECT IDENTIFIER,
  1430. * value [0] EXPLICIT ANY DEFINED BY type-id }
  1431. *
  1432. * HardwareModuleName ::= SEQUENCE {
  1433. * hwType OBJECT IDENTIFIER,
  1434. * hwSerialNum OCTET STRING }
  1435. *
  1436. * NOTE: we currently only parse and use otherName of type HwModuleName,
  1437. * as defined in RFC 4108.
  1438. */
  1439. static int x509_get_other_name( const mbedtls_x509_buf *subject_alt_name,
  1440. mbedtls_x509_san_other_name *other_name )
  1441. {
  1442. int ret = 0;
  1443. size_t len;
  1444. unsigned char *p = subject_alt_name->p;
  1445. const unsigned char *end = p + subject_alt_name->len;
  1446. mbedtls_x509_buf cur_oid;
  1447. if( ( subject_alt_name->tag &
  1448. ( MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK ) ) !=
  1449. ( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ) )
  1450. {
  1451. /*
  1452. * The given subject alternative name is not of type "othername".
  1453. */
  1454. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1455. }
  1456. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1457. MBEDTLS_ASN1_OID ) ) != 0 )
  1458. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  1459. cur_oid.tag = MBEDTLS_ASN1_OID;
  1460. cur_oid.p = p;
  1461. cur_oid.len = len;
  1462. /*
  1463. * Only HwModuleName is currently supported.
  1464. */
  1465. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid ) != 0 )
  1466. {
  1467. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  1468. }
  1469. if( p + len >= end )
  1470. {
  1471. mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
  1472. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1473. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  1474. }
  1475. p += len;
  1476. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1477. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC ) ) != 0 )
  1478. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  1479. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1480. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  1481. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  1482. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len, MBEDTLS_ASN1_OID ) ) != 0 )
  1483. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  1484. other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
  1485. other_name->value.hardware_module_name.oid.p = p;
  1486. other_name->value.hardware_module_name.oid.len = len;
  1487. if( p + len >= end )
  1488. {
  1489. mbedtls_platform_zeroize( other_name, sizeof( *other_name ) );
  1490. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1491. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  1492. }
  1493. p += len;
  1494. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  1495. MBEDTLS_ASN1_OCTET_STRING ) ) != 0 )
  1496. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret ) );
  1497. other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
  1498. other_name->value.hardware_module_name.val.p = p;
  1499. other_name->value.hardware_module_name.val.len = len;
  1500. p += len;
  1501. if( p != end )
  1502. {
  1503. mbedtls_platform_zeroize( other_name,
  1504. sizeof( *other_name ) );
  1505. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
  1506. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH ) );
  1507. }
  1508. return( 0 );
  1509. }
  1510. int mbedtls_x509_parse_subject_alt_name( const mbedtls_x509_buf *san_buf,
  1511. mbedtls_x509_subject_alternative_name *san )
  1512. {
  1513. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1514. switch( san_buf->tag &
  1515. ( MBEDTLS_ASN1_TAG_CLASS_MASK |
  1516. MBEDTLS_ASN1_TAG_VALUE_MASK ) )
  1517. {
  1518. /*
  1519. * otherName
  1520. */
  1521. case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME ):
  1522. {
  1523. mbedtls_x509_san_other_name other_name;
  1524. ret = x509_get_other_name( san_buf, &other_name );
  1525. if( ret != 0 )
  1526. return( ret );
  1527. memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
  1528. san->type = MBEDTLS_X509_SAN_OTHER_NAME;
  1529. memcpy( &san->san.other_name,
  1530. &other_name, sizeof( other_name ) );
  1531. }
  1532. break;
  1533. /*
  1534. * dNSName
  1535. */
  1536. case( MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME ):
  1537. {
  1538. memset( san, 0, sizeof( mbedtls_x509_subject_alternative_name ) );
  1539. san->type = MBEDTLS_X509_SAN_DNS_NAME;
  1540. memcpy( &san->san.unstructured_name,
  1541. san_buf, sizeof( *san_buf ) );
  1542. }
  1543. break;
  1544. /*
  1545. * Type not supported
  1546. */
  1547. default:
  1548. return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
  1549. }
  1550. return( 0 );
  1551. }
  1552. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  1553. static int x509_info_subject_alt_name( char **buf, size_t *size,
  1554. const mbedtls_x509_sequence
  1555. *subject_alt_name,
  1556. const char *prefix )
  1557. {
  1558. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1559. size_t n = *size;
  1560. char *p = *buf;
  1561. const mbedtls_x509_sequence *cur = subject_alt_name;
  1562. mbedtls_x509_subject_alternative_name san;
  1563. int parse_ret;
  1564. while( cur != NULL )
  1565. {
  1566. memset( &san, 0, sizeof( san ) );
  1567. parse_ret = mbedtls_x509_parse_subject_alt_name( &cur->buf, &san );
  1568. if( parse_ret != 0 )
  1569. {
  1570. if( parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE )
  1571. {
  1572. ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
  1573. MBEDTLS_X509_SAFE_SNPRINTF;
  1574. }
  1575. else
  1576. {
  1577. ret = mbedtls_snprintf( p, n, "\n%s <malformed>", prefix );
  1578. MBEDTLS_X509_SAFE_SNPRINTF;
  1579. }
  1580. cur = cur->next;
  1581. continue;
  1582. }
  1583. switch( san.type )
  1584. {
  1585. /*
  1586. * otherName
  1587. */
  1588. case MBEDTLS_X509_SAN_OTHER_NAME:
  1589. {
  1590. mbedtls_x509_san_other_name *other_name = &san.san.other_name;
  1591. ret = mbedtls_snprintf( p, n, "\n%s otherName :", prefix );
  1592. MBEDTLS_X509_SAFE_SNPRINTF;
  1593. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ON_HW_MODULE_NAME,
  1594. &other_name->value.hardware_module_name.oid ) != 0 )
  1595. {
  1596. ret = mbedtls_snprintf( p, n, "\n%s hardware module name :", prefix );
  1597. MBEDTLS_X509_SAFE_SNPRINTF;
  1598. ret = mbedtls_snprintf( p, n, "\n%s hardware type : ", prefix );
  1599. MBEDTLS_X509_SAFE_SNPRINTF;
  1600. ret = mbedtls_oid_get_numeric_string( p, n, &other_name->value.hardware_module_name.oid );
  1601. MBEDTLS_X509_SAFE_SNPRINTF;
  1602. ret = mbedtls_snprintf( p, n, "\n%s hardware serial number : ", prefix );
  1603. MBEDTLS_X509_SAFE_SNPRINTF;
  1604. if( other_name->value.hardware_module_name.val.len >= n )
  1605. {
  1606. *p = '\0';
  1607. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  1608. }
  1609. memcpy( p, other_name->value.hardware_module_name.val.p,
  1610. other_name->value.hardware_module_name.val.len );
  1611. p += other_name->value.hardware_module_name.val.len;
  1612. n -= other_name->value.hardware_module_name.val.len;
  1613. }/* MBEDTLS_OID_ON_HW_MODULE_NAME */
  1614. }
  1615. break;
  1616. /*
  1617. * dNSName
  1618. */
  1619. case MBEDTLS_X509_SAN_DNS_NAME:
  1620. {
  1621. ret = mbedtls_snprintf( p, n, "\n%s dNSName : ", prefix );
  1622. MBEDTLS_X509_SAFE_SNPRINTF;
  1623. if( san.san.unstructured_name.len >= n )
  1624. {
  1625. *p = '\0';
  1626. return( MBEDTLS_ERR_X509_BUFFER_TOO_SMALL );
  1627. }
  1628. memcpy( p, san.san.unstructured_name.p, san.san.unstructured_name.len );
  1629. p += san.san.unstructured_name.len;
  1630. n -= san.san.unstructured_name.len;
  1631. }
  1632. break;
  1633. /*
  1634. * Type not supported, skip item.
  1635. */
  1636. default:
  1637. ret = mbedtls_snprintf( p, n, "\n%s <unsupported>", prefix );
  1638. MBEDTLS_X509_SAFE_SNPRINTF;
  1639. break;
  1640. }
  1641. cur = cur->next;
  1642. }
  1643. *p = '\0';
  1644. *size = n;
  1645. *buf = p;
  1646. return( 0 );
  1647. }
  1648. #define PRINT_ITEM(i) \
  1649. { \
  1650. ret = mbedtls_snprintf( p, n, "%s" i, sep ); \
  1651. MBEDTLS_X509_SAFE_SNPRINTF; \
  1652. sep = ", "; \
  1653. }
  1654. #define CERT_TYPE(type,name) \
  1655. if( ns_cert_type & (type) ) \
  1656. PRINT_ITEM( name );
  1657. static int x509_info_cert_type( char **buf, size_t *size,
  1658. unsigned char ns_cert_type )
  1659. {
  1660. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1661. size_t n = *size;
  1662. char *p = *buf;
  1663. const char *sep = "";
  1664. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client" );
  1665. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server" );
  1666. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email" );
  1667. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing" );
  1668. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved" );
  1669. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA" );
  1670. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA" );
  1671. CERT_TYPE( MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA" );
  1672. *size = n;
  1673. *buf = p;
  1674. return( 0 );
  1675. }
  1676. #define KEY_USAGE(code,name) \
  1677. if( key_usage & (code) ) \
  1678. PRINT_ITEM( name );
  1679. static int x509_info_key_usage( char **buf, size_t *size,
  1680. unsigned int key_usage )
  1681. {
  1682. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1683. size_t n = *size;
  1684. char *p = *buf;
  1685. const char *sep = "";
  1686. KEY_USAGE( MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature" );
  1687. KEY_USAGE( MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation" );
  1688. KEY_USAGE( MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment" );
  1689. KEY_USAGE( MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment" );
  1690. KEY_USAGE( MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement" );
  1691. KEY_USAGE( MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign" );
  1692. KEY_USAGE( MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign" );
  1693. KEY_USAGE( MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only" );
  1694. KEY_USAGE( MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only" );
  1695. *size = n;
  1696. *buf = p;
  1697. return( 0 );
  1698. }
  1699. static int x509_info_ext_key_usage( char **buf, size_t *size,
  1700. const mbedtls_x509_sequence *extended_key_usage )
  1701. {
  1702. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1703. const char *desc;
  1704. size_t n = *size;
  1705. char *p = *buf;
  1706. const mbedtls_x509_sequence *cur = extended_key_usage;
  1707. const char *sep = "";
  1708. while( cur != NULL )
  1709. {
  1710. if( mbedtls_oid_get_extended_key_usage( &cur->buf, &desc ) != 0 )
  1711. desc = "???";
  1712. ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
  1713. MBEDTLS_X509_SAFE_SNPRINTF;
  1714. sep = ", ";
  1715. cur = cur->next;
  1716. }
  1717. *size = n;
  1718. *buf = p;
  1719. return( 0 );
  1720. }
  1721. static int x509_info_cert_policies( char **buf, size_t *size,
  1722. const mbedtls_x509_sequence *certificate_policies )
  1723. {
  1724. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1725. const char *desc;
  1726. size_t n = *size;
  1727. char *p = *buf;
  1728. const mbedtls_x509_sequence *cur = certificate_policies;
  1729. const char *sep = "";
  1730. while( cur != NULL )
  1731. {
  1732. if( mbedtls_oid_get_certificate_policies( &cur->buf, &desc ) != 0 )
  1733. desc = "???";
  1734. ret = mbedtls_snprintf( p, n, "%s%s", sep, desc );
  1735. MBEDTLS_X509_SAFE_SNPRINTF;
  1736. sep = ", ";
  1737. cur = cur->next;
  1738. }
  1739. *size = n;
  1740. *buf = p;
  1741. return( 0 );
  1742. }
  1743. /*
  1744. * Return an informational string about the certificate.
  1745. */
  1746. #define BEFORE_COLON 18
  1747. #define BC "18"
  1748. int mbedtls_x509_crt_info( char *buf, size_t size, const char *prefix,
  1749. const mbedtls_x509_crt *crt )
  1750. {
  1751. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1752. size_t n;
  1753. char *p;
  1754. char key_size_str[BEFORE_COLON];
  1755. p = buf;
  1756. n = size;
  1757. if( NULL == crt )
  1758. {
  1759. ret = mbedtls_snprintf( p, n, "\nCertificate is uninitialised!\n" );
  1760. MBEDTLS_X509_SAFE_SNPRINTF;
  1761. return( (int) ( size - n ) );
  1762. }
  1763. ret = mbedtls_snprintf( p, n, "%scert. version : %d\n",
  1764. prefix, crt->version );
  1765. MBEDTLS_X509_SAFE_SNPRINTF;
  1766. ret = mbedtls_snprintf( p, n, "%sserial number : ",
  1767. prefix );
  1768. MBEDTLS_X509_SAFE_SNPRINTF;
  1769. ret = mbedtls_x509_serial_gets( p, n, &crt->serial );
  1770. MBEDTLS_X509_SAFE_SNPRINTF;
  1771. ret = mbedtls_snprintf( p, n, "\n%sissuer name : ", prefix );
  1772. MBEDTLS_X509_SAFE_SNPRINTF;
  1773. ret = mbedtls_x509_dn_gets( p, n, &crt->issuer );
  1774. MBEDTLS_X509_SAFE_SNPRINTF;
  1775. ret = mbedtls_snprintf( p, n, "\n%ssubject name : ", prefix );
  1776. MBEDTLS_X509_SAFE_SNPRINTF;
  1777. ret = mbedtls_x509_dn_gets( p, n, &crt->subject );
  1778. MBEDTLS_X509_SAFE_SNPRINTF;
  1779. ret = mbedtls_snprintf( p, n, "\n%sissued on : " \
  1780. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1781. crt->valid_from.year, crt->valid_from.mon,
  1782. crt->valid_from.day, crt->valid_from.hour,
  1783. crt->valid_from.min, crt->valid_from.sec );
  1784. MBEDTLS_X509_SAFE_SNPRINTF;
  1785. ret = mbedtls_snprintf( p, n, "\n%sexpires on : " \
  1786. "%04d-%02d-%02d %02d:%02d:%02d", prefix,
  1787. crt->valid_to.year, crt->valid_to.mon,
  1788. crt->valid_to.day, crt->valid_to.hour,
  1789. crt->valid_to.min, crt->valid_to.sec );
  1790. MBEDTLS_X509_SAFE_SNPRINTF;
  1791. ret = mbedtls_snprintf( p, n, "\n%ssigned using : ", prefix );
  1792. MBEDTLS_X509_SAFE_SNPRINTF;
  1793. ret = mbedtls_x509_sig_alg_gets( p, n, &crt->sig_oid, crt->sig_pk,
  1794. crt->sig_md, crt->sig_opts );
  1795. MBEDTLS_X509_SAFE_SNPRINTF;
  1796. /* Key size */
  1797. if( ( ret = mbedtls_x509_key_size_helper( key_size_str, BEFORE_COLON,
  1798. mbedtls_pk_get_name( &crt->pk ) ) ) != 0 )
  1799. {
  1800. return( ret );
  1801. }
  1802. ret = mbedtls_snprintf( p, n, "\n%s%-" BC "s: %d bits", prefix, key_size_str,
  1803. (int) mbedtls_pk_get_bitlen( &crt->pk ) );
  1804. MBEDTLS_X509_SAFE_SNPRINTF;
  1805. /*
  1806. * Optional extensions
  1807. */
  1808. if( crt->ext_types & MBEDTLS_X509_EXT_BASIC_CONSTRAINTS )
  1809. {
  1810. ret = mbedtls_snprintf( p, n, "\n%sbasic constraints : CA=%s", prefix,
  1811. crt->ca_istrue ? "true" : "false" );
  1812. MBEDTLS_X509_SAFE_SNPRINTF;
  1813. if( crt->max_pathlen > 0 )
  1814. {
  1815. ret = mbedtls_snprintf( p, n, ", max_pathlen=%d", crt->max_pathlen - 1 );
  1816. MBEDTLS_X509_SAFE_SNPRINTF;
  1817. }
  1818. }
  1819. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  1820. {
  1821. ret = mbedtls_snprintf( p, n, "\n%ssubject alt name :", prefix );
  1822. MBEDTLS_X509_SAFE_SNPRINTF;
  1823. if( ( ret = x509_info_subject_alt_name( &p, &n,
  1824. &crt->subject_alt_names,
  1825. prefix ) ) != 0 )
  1826. return( ret );
  1827. }
  1828. if( crt->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE )
  1829. {
  1830. ret = mbedtls_snprintf( p, n, "\n%scert. type : ", prefix );
  1831. MBEDTLS_X509_SAFE_SNPRINTF;
  1832. if( ( ret = x509_info_cert_type( &p, &n, crt->ns_cert_type ) ) != 0 )
  1833. return( ret );
  1834. }
  1835. if( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE )
  1836. {
  1837. ret = mbedtls_snprintf( p, n, "\n%skey usage : ", prefix );
  1838. MBEDTLS_X509_SAFE_SNPRINTF;
  1839. if( ( ret = x509_info_key_usage( &p, &n, crt->key_usage ) ) != 0 )
  1840. return( ret );
  1841. }
  1842. if( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE )
  1843. {
  1844. ret = mbedtls_snprintf( p, n, "\n%sext key usage : ", prefix );
  1845. MBEDTLS_X509_SAFE_SNPRINTF;
  1846. if( ( ret = x509_info_ext_key_usage( &p, &n,
  1847. &crt->ext_key_usage ) ) != 0 )
  1848. return( ret );
  1849. }
  1850. if( crt->ext_types & MBEDTLS_OID_X509_EXT_CERTIFICATE_POLICIES )
  1851. {
  1852. ret = mbedtls_snprintf( p, n, "\n%scertificate policies : ", prefix );
  1853. MBEDTLS_X509_SAFE_SNPRINTF;
  1854. if( ( ret = x509_info_cert_policies( &p, &n,
  1855. &crt->certificate_policies ) ) != 0 )
  1856. return( ret );
  1857. }
  1858. ret = mbedtls_snprintf( p, n, "\n" );
  1859. MBEDTLS_X509_SAFE_SNPRINTF;
  1860. return( (int) ( size - n ) );
  1861. }
  1862. struct x509_crt_verify_string {
  1863. int code;
  1864. const char *string;
  1865. };
  1866. #define X509_CRT_ERROR_INFO( err, err_str, info ) { err, info },
  1867. static const struct x509_crt_verify_string x509_crt_verify_strings[] = {
  1868. MBEDTLS_X509_CRT_ERROR_INFO_LIST
  1869. { 0, NULL }
  1870. };
  1871. #undef X509_CRT_ERROR_INFO
  1872. int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix,
  1873. uint32_t flags )
  1874. {
  1875. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1876. const struct x509_crt_verify_string *cur;
  1877. char *p = buf;
  1878. size_t n = size;
  1879. for( cur = x509_crt_verify_strings; cur->string != NULL ; cur++ )
  1880. {
  1881. if( ( flags & cur->code ) == 0 )
  1882. continue;
  1883. ret = mbedtls_snprintf( p, n, "%s%s\n", prefix, cur->string );
  1884. MBEDTLS_X509_SAFE_SNPRINTF;
  1885. flags ^= cur->code;
  1886. }
  1887. if( flags != 0 )
  1888. {
  1889. ret = mbedtls_snprintf( p, n, "%sUnknown reason "
  1890. "(this should not happen)\n", prefix );
  1891. MBEDTLS_X509_SAFE_SNPRINTF;
  1892. }
  1893. return( (int) ( size - n ) );
  1894. }
  1895. #endif /* MBEDTLS_X509_REMOVE_INFO */
  1896. int mbedtls_x509_crt_check_key_usage( const mbedtls_x509_crt *crt,
  1897. unsigned int usage )
  1898. {
  1899. unsigned int usage_must, usage_may;
  1900. unsigned int may_mask = MBEDTLS_X509_KU_ENCIPHER_ONLY
  1901. | MBEDTLS_X509_KU_DECIPHER_ONLY;
  1902. if( ( crt->ext_types & MBEDTLS_X509_EXT_KEY_USAGE ) == 0 )
  1903. return( 0 );
  1904. usage_must = usage & ~may_mask;
  1905. if( ( ( crt->key_usage & ~may_mask ) & usage_must ) != usage_must )
  1906. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1907. usage_may = usage & may_mask;
  1908. if( ( ( crt->key_usage & may_mask ) | usage_may ) != usage_may )
  1909. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1910. return( 0 );
  1911. }
  1912. int mbedtls_x509_crt_check_extended_key_usage( const mbedtls_x509_crt *crt,
  1913. const char *usage_oid,
  1914. size_t usage_len )
  1915. {
  1916. const mbedtls_x509_sequence *cur;
  1917. /* Extension is not mandatory, absent means no restriction */
  1918. if( ( crt->ext_types & MBEDTLS_X509_EXT_EXTENDED_KEY_USAGE ) == 0 )
  1919. return( 0 );
  1920. /*
  1921. * Look for the requested usage (or wildcard ANY) in our list
  1922. */
  1923. for( cur = &crt->ext_key_usage; cur != NULL; cur = cur->next )
  1924. {
  1925. const mbedtls_x509_buf *cur_oid = &cur->buf;
  1926. if( cur_oid->len == usage_len &&
  1927. memcmp( cur_oid->p, usage_oid, usage_len ) == 0 )
  1928. {
  1929. return( 0 );
  1930. }
  1931. if( MBEDTLS_OID_CMP( MBEDTLS_OID_ANY_EXTENDED_KEY_USAGE, cur_oid ) == 0 )
  1932. return( 0 );
  1933. }
  1934. return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
  1935. }
  1936. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  1937. /*
  1938. * Return 1 if the certificate is revoked, or 0 otherwise.
  1939. */
  1940. int mbedtls_x509_crt_is_revoked( const mbedtls_x509_crt *crt, const mbedtls_x509_crl *crl )
  1941. {
  1942. const mbedtls_x509_crl_entry *cur = &crl->entry;
  1943. while( cur != NULL && cur->serial.len != 0 )
  1944. {
  1945. if( crt->serial.len == cur->serial.len &&
  1946. memcmp( crt->serial.p, cur->serial.p, crt->serial.len ) == 0 )
  1947. {
  1948. return( 1 );
  1949. }
  1950. cur = cur->next;
  1951. }
  1952. return( 0 );
  1953. }
  1954. /*
  1955. * Check that the given certificate is not revoked according to the CRL.
  1956. * Skip validation if no CRL for the given CA is present.
  1957. */
  1958. static int x509_crt_verifycrl( mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
  1959. mbedtls_x509_crl *crl_list,
  1960. const mbedtls_x509_crt_profile *profile )
  1961. {
  1962. int flags = 0;
  1963. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  1964. const mbedtls_md_info_t *md_info;
  1965. if( ca == NULL )
  1966. return( flags );
  1967. while( crl_list != NULL )
  1968. {
  1969. if( crl_list->version == 0 ||
  1970. x509_name_cmp( &crl_list->issuer, &ca->subject ) != 0 )
  1971. {
  1972. crl_list = crl_list->next;
  1973. continue;
  1974. }
  1975. /*
  1976. * Check if the CA is configured to sign CRLs
  1977. */
  1978. if( mbedtls_x509_crt_check_key_usage( ca,
  1979. MBEDTLS_X509_KU_CRL_SIGN ) != 0 )
  1980. {
  1981. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  1982. break;
  1983. }
  1984. /*
  1985. * Check if CRL is correctly signed by the trusted CA
  1986. */
  1987. if( x509_profile_check_md_alg( profile, crl_list->sig_md ) != 0 )
  1988. flags |= MBEDTLS_X509_BADCRL_BAD_MD;
  1989. if( x509_profile_check_pk_alg( profile, crl_list->sig_pk ) != 0 )
  1990. flags |= MBEDTLS_X509_BADCRL_BAD_PK;
  1991. md_info = mbedtls_md_info_from_type( crl_list->sig_md );
  1992. if( mbedtls_md( md_info, crl_list->tbs.p, crl_list->tbs.len, hash ) != 0 )
  1993. {
  1994. /* Note: this can't happen except after an internal error */
  1995. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  1996. break;
  1997. }
  1998. if( x509_profile_check_key( profile, &ca->pk ) != 0 )
  1999. flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  2000. if( mbedtls_pk_verify_ext( crl_list->sig_pk, crl_list->sig_opts, &ca->pk,
  2001. crl_list->sig_md, hash, mbedtls_md_get_size( md_info ),
  2002. crl_list->sig.p, crl_list->sig.len ) != 0 )
  2003. {
  2004. flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
  2005. break;
  2006. }
  2007. /*
  2008. * Check for validity of CRL (Do not drop out)
  2009. */
  2010. if( mbedtls_x509_time_is_past( &crl_list->next_update ) )
  2011. flags |= MBEDTLS_X509_BADCRL_EXPIRED;
  2012. if( mbedtls_x509_time_is_future( &crl_list->this_update ) )
  2013. flags |= MBEDTLS_X509_BADCRL_FUTURE;
  2014. /*
  2015. * Check if certificate is revoked
  2016. */
  2017. if( mbedtls_x509_crt_is_revoked( crt, crl_list ) )
  2018. {
  2019. flags |= MBEDTLS_X509_BADCERT_REVOKED;
  2020. break;
  2021. }
  2022. crl_list = crl_list->next;
  2023. }
  2024. return( flags );
  2025. }
  2026. #endif /* MBEDTLS_X509_CRL_PARSE_C */
  2027. /*
  2028. * Check the signature of a certificate by its parent
  2029. */
  2030. static int x509_crt_check_signature( const mbedtls_x509_crt *child,
  2031. mbedtls_x509_crt *parent,
  2032. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2033. {
  2034. unsigned char hash[MBEDTLS_MD_MAX_SIZE];
  2035. size_t hash_len;
  2036. #if !defined(MBEDTLS_USE_PSA_CRYPTO)
  2037. const mbedtls_md_info_t *md_info;
  2038. md_info = mbedtls_md_info_from_type( child->sig_md );
  2039. hash_len = mbedtls_md_get_size( md_info );
  2040. /* Note: hash errors can happen only after an internal error */
  2041. if( mbedtls_md( md_info, child->tbs.p, child->tbs.len, hash ) != 0 )
  2042. return( -1 );
  2043. #else
  2044. psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
  2045. psa_algorithm_t hash_alg = mbedtls_psa_translate_md( child->sig_md );
  2046. if( psa_hash_setup( &hash_operation, hash_alg ) != PSA_SUCCESS )
  2047. return( -1 );
  2048. if( psa_hash_update( &hash_operation, child->tbs.p, child->tbs.len )
  2049. != PSA_SUCCESS )
  2050. {
  2051. return( -1 );
  2052. }
  2053. if( psa_hash_finish( &hash_operation, hash, sizeof( hash ), &hash_len )
  2054. != PSA_SUCCESS )
  2055. {
  2056. return( -1 );
  2057. }
  2058. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2059. /* Skip expensive computation on obvious mismatch */
  2060. if( ! mbedtls_pk_can_do( &parent->pk, child->sig_pk ) )
  2061. return( -1 );
  2062. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2063. if( rs_ctx != NULL && child->sig_pk == MBEDTLS_PK_ECDSA )
  2064. {
  2065. return( mbedtls_pk_verify_restartable( &parent->pk,
  2066. child->sig_md, hash, hash_len,
  2067. child->sig.p, child->sig.len, &rs_ctx->pk ) );
  2068. }
  2069. #else
  2070. (void) rs_ctx;
  2071. #endif
  2072. return( mbedtls_pk_verify_ext( child->sig_pk, child->sig_opts, &parent->pk,
  2073. child->sig_md, hash, hash_len,
  2074. child->sig.p, child->sig.len ) );
  2075. }
  2076. /*
  2077. * Check if 'parent' is a suitable parent (signing CA) for 'child'.
  2078. * Return 0 if yes, -1 if not.
  2079. *
  2080. * top means parent is a locally-trusted certificate
  2081. */
  2082. static int x509_crt_check_parent( const mbedtls_x509_crt *child,
  2083. const mbedtls_x509_crt *parent,
  2084. int top )
  2085. {
  2086. int need_ca_bit;
  2087. /* Parent must be the issuer */
  2088. if( x509_name_cmp( &child->issuer, &parent->subject ) != 0 )
  2089. return( -1 );
  2090. /* Parent must have the basicConstraints CA bit set as a general rule */
  2091. need_ca_bit = 1;
  2092. /* Exception: v1/v2 certificates that are locally trusted. */
  2093. if( top && parent->version < 3 )
  2094. need_ca_bit = 0;
  2095. if( need_ca_bit && ! parent->ca_istrue )
  2096. return( -1 );
  2097. if( need_ca_bit &&
  2098. mbedtls_x509_crt_check_key_usage( parent, MBEDTLS_X509_KU_KEY_CERT_SIGN ) != 0 )
  2099. {
  2100. return( -1 );
  2101. }
  2102. return( 0 );
  2103. }
  2104. /*
  2105. * Find a suitable parent for child in candidates, or return NULL.
  2106. *
  2107. * Here suitable is defined as:
  2108. * 1. subject name matches child's issuer
  2109. * 2. if necessary, the CA bit is set and key usage allows signing certs
  2110. * 3. for trusted roots, the signature is correct
  2111. * (for intermediates, the signature is checked and the result reported)
  2112. * 4. pathlen constraints are satisfied
  2113. *
  2114. * If there's a suitable candidate which is also time-valid, return the first
  2115. * such. Otherwise, return the first suitable candidate (or NULL if there is
  2116. * none).
  2117. *
  2118. * The rationale for this rule is that someone could have a list of trusted
  2119. * roots with two versions on the same root with different validity periods.
  2120. * (At least one user reported having such a list and wanted it to just work.)
  2121. * The reason we don't just require time-validity is that generally there is
  2122. * only one version, and if it's expired we want the flags to state that
  2123. * rather than NOT_TRUSTED, as would be the case if we required it here.
  2124. *
  2125. * The rationale for rule 3 (signature for trusted roots) is that users might
  2126. * have two versions of the same CA with different keys in their list, and the
  2127. * way we select the correct one is by checking the signature (as we don't
  2128. * rely on key identifier extensions). (This is one way users might choose to
  2129. * handle key rollover, another relies on self-issued certs, see [SIRO].)
  2130. *
  2131. * Arguments:
  2132. * - [in] child: certificate for which we're looking for a parent
  2133. * - [in] candidates: chained list of potential parents
  2134. * - [out] r_parent: parent found (or NULL)
  2135. * - [out] r_signature_is_good: 1 if child signature by parent is valid, or 0
  2136. * - [in] top: 1 if candidates consists of trusted roots, ie we're at the top
  2137. * of the chain, 0 otherwise
  2138. * - [in] path_cnt: number of intermediates seen so far
  2139. * - [in] self_cnt: number of self-signed intermediates seen so far
  2140. * (will never be greater than path_cnt)
  2141. * - [in-out] rs_ctx: context for restarting operations
  2142. *
  2143. * Return value:
  2144. * - 0 on success
  2145. * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
  2146. */
  2147. static int x509_crt_find_parent_in(
  2148. mbedtls_x509_crt *child,
  2149. mbedtls_x509_crt *candidates,
  2150. mbedtls_x509_crt **r_parent,
  2151. int *r_signature_is_good,
  2152. int top,
  2153. unsigned path_cnt,
  2154. unsigned self_cnt,
  2155. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2156. {
  2157. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2158. mbedtls_x509_crt *parent, *fallback_parent;
  2159. int signature_is_good = 0, fallback_signature_is_good;
  2160. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2161. /* did we have something in progress? */
  2162. if( rs_ctx != NULL && rs_ctx->parent != NULL )
  2163. {
  2164. /* restore saved state */
  2165. parent = rs_ctx->parent;
  2166. fallback_parent = rs_ctx->fallback_parent;
  2167. fallback_signature_is_good = rs_ctx->fallback_signature_is_good;
  2168. /* clear saved state */
  2169. rs_ctx->parent = NULL;
  2170. rs_ctx->fallback_parent = NULL;
  2171. rs_ctx->fallback_signature_is_good = 0;
  2172. /* resume where we left */
  2173. goto check_signature;
  2174. }
  2175. #endif
  2176. fallback_parent = NULL;
  2177. fallback_signature_is_good = 0;
  2178. for( parent = candidates; parent != NULL; parent = parent->next )
  2179. {
  2180. /* basic parenting skills (name, CA bit, key usage) */
  2181. if( x509_crt_check_parent( child, parent, top ) != 0 )
  2182. continue;
  2183. /* +1 because stored max_pathlen is 1 higher that the actual value */
  2184. if( parent->max_pathlen > 0 &&
  2185. (size_t) parent->max_pathlen < 1 + path_cnt - self_cnt )
  2186. {
  2187. continue;
  2188. }
  2189. /* Signature */
  2190. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2191. check_signature:
  2192. #endif
  2193. ret = x509_crt_check_signature( child, parent, rs_ctx );
  2194. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2195. if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2196. {
  2197. /* save state */
  2198. rs_ctx->parent = parent;
  2199. rs_ctx->fallback_parent = fallback_parent;
  2200. rs_ctx->fallback_signature_is_good = fallback_signature_is_good;
  2201. return( ret );
  2202. }
  2203. #else
  2204. (void) ret;
  2205. #endif
  2206. signature_is_good = ret == 0;
  2207. if( top && ! signature_is_good )
  2208. continue;
  2209. /* optional time check */
  2210. if( mbedtls_x509_time_is_past( &parent->valid_to ) ||
  2211. mbedtls_x509_time_is_future( &parent->valid_from ) )
  2212. {
  2213. if( fallback_parent == NULL )
  2214. {
  2215. fallback_parent = parent;
  2216. fallback_signature_is_good = signature_is_good;
  2217. }
  2218. continue;
  2219. }
  2220. *r_parent = parent;
  2221. *r_signature_is_good = signature_is_good;
  2222. break;
  2223. }
  2224. if( parent == NULL )
  2225. {
  2226. *r_parent = fallback_parent;
  2227. *r_signature_is_good = fallback_signature_is_good;
  2228. }
  2229. return( 0 );
  2230. }
  2231. /*
  2232. * Find a parent in trusted CAs or the provided chain, or return NULL.
  2233. *
  2234. * Searches in trusted CAs first, and return the first suitable parent found
  2235. * (see find_parent_in() for definition of suitable).
  2236. *
  2237. * Arguments:
  2238. * - [in] child: certificate for which we're looking for a parent, followed
  2239. * by a chain of possible intermediates
  2240. * - [in] trust_ca: list of locally trusted certificates
  2241. * - [out] parent: parent found (or NULL)
  2242. * - [out] parent_is_trusted: 1 if returned `parent` is trusted, or 0
  2243. * - [out] signature_is_good: 1 if child signature by parent is valid, or 0
  2244. * - [in] path_cnt: number of links in the chain so far (EE -> ... -> child)
  2245. * - [in] self_cnt: number of self-signed certs in the chain so far
  2246. * (will always be no greater than path_cnt)
  2247. * - [in-out] rs_ctx: context for restarting operations
  2248. *
  2249. * Return value:
  2250. * - 0 on success
  2251. * - MBEDTLS_ERR_ECP_IN_PROGRESS otherwise
  2252. */
  2253. static int x509_crt_find_parent(
  2254. mbedtls_x509_crt *child,
  2255. mbedtls_x509_crt *trust_ca,
  2256. mbedtls_x509_crt **parent,
  2257. int *parent_is_trusted,
  2258. int *signature_is_good,
  2259. unsigned path_cnt,
  2260. unsigned self_cnt,
  2261. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2262. {
  2263. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2264. mbedtls_x509_crt *search_list;
  2265. *parent_is_trusted = 1;
  2266. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2267. /* restore then clear saved state if we have some stored */
  2268. if( rs_ctx != NULL && rs_ctx->parent_is_trusted != -1 )
  2269. {
  2270. *parent_is_trusted = rs_ctx->parent_is_trusted;
  2271. rs_ctx->parent_is_trusted = -1;
  2272. }
  2273. #endif
  2274. while( 1 ) {
  2275. search_list = *parent_is_trusted ? trust_ca : child->next;
  2276. ret = x509_crt_find_parent_in( child, search_list,
  2277. parent, signature_is_good,
  2278. *parent_is_trusted,
  2279. path_cnt, self_cnt, rs_ctx );
  2280. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2281. if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2282. {
  2283. /* save state */
  2284. rs_ctx->parent_is_trusted = *parent_is_trusted;
  2285. return( ret );
  2286. }
  2287. #else
  2288. (void) ret;
  2289. #endif
  2290. /* stop here if found or already in second iteration */
  2291. if( *parent != NULL || *parent_is_trusted == 0 )
  2292. break;
  2293. /* prepare second iteration */
  2294. *parent_is_trusted = 0;
  2295. }
  2296. /* extra precaution against mistakes in the caller */
  2297. if( *parent == NULL )
  2298. {
  2299. *parent_is_trusted = 0;
  2300. *signature_is_good = 0;
  2301. }
  2302. return( 0 );
  2303. }
  2304. /*
  2305. * Check if an end-entity certificate is locally trusted
  2306. *
  2307. * Currently we require such certificates to be self-signed (actually only
  2308. * check for self-issued as self-signatures are not checked)
  2309. */
  2310. static int x509_crt_check_ee_locally_trusted(
  2311. mbedtls_x509_crt *crt,
  2312. mbedtls_x509_crt *trust_ca )
  2313. {
  2314. mbedtls_x509_crt *cur;
  2315. /* must be self-issued */
  2316. if( x509_name_cmp( &crt->issuer, &crt->subject ) != 0 )
  2317. return( -1 );
  2318. /* look for an exact match with trusted cert */
  2319. for( cur = trust_ca; cur != NULL; cur = cur->next )
  2320. {
  2321. if( crt->raw.len == cur->raw.len &&
  2322. memcmp( crt->raw.p, cur->raw.p, crt->raw.len ) == 0 )
  2323. {
  2324. return( 0 );
  2325. }
  2326. }
  2327. /* too bad */
  2328. return( -1 );
  2329. }
  2330. /*
  2331. * Build and verify a certificate chain
  2332. *
  2333. * Given a peer-provided list of certificates EE, C1, ..., Cn and
  2334. * a list of trusted certs R1, ... Rp, try to build and verify a chain
  2335. * EE, Ci1, ... Ciq [, Rj]
  2336. * such that every cert in the chain is a child of the next one,
  2337. * jumping to a trusted root as early as possible.
  2338. *
  2339. * Verify that chain and return it with flags for all issues found.
  2340. *
  2341. * Special cases:
  2342. * - EE == Rj -> return a one-element list containing it
  2343. * - EE, Ci1, ..., Ciq cannot be continued with a trusted root
  2344. * -> return that chain with NOT_TRUSTED set on Ciq
  2345. *
  2346. * Tests for (aspects of) this function should include at least:
  2347. * - trusted EE
  2348. * - EE -> trusted root
  2349. * - EE -> intermediate CA -> trusted root
  2350. * - if relevant: EE untrusted
  2351. * - if relevant: EE -> intermediate, untrusted
  2352. * with the aspect under test checked at each relevant level (EE, int, root).
  2353. * For some aspects longer chains are required, but usually length 2 is
  2354. * enough (but length 1 is not in general).
  2355. *
  2356. * Arguments:
  2357. * - [in] crt: the cert list EE, C1, ..., Cn
  2358. * - [in] trust_ca: the trusted list R1, ..., Rp
  2359. * - [in] ca_crl, profile: as in verify_with_profile()
  2360. * - [out] ver_chain: the built and verified chain
  2361. * Only valid when return value is 0, may contain garbage otherwise!
  2362. * Restart note: need not be the same when calling again to resume.
  2363. * - [in-out] rs_ctx: context for restarting operations
  2364. *
  2365. * Return value:
  2366. * - non-zero if the chain could not be fully built and examined
  2367. * - 0 is the chain was successfully built and examined,
  2368. * even if it was found to be invalid
  2369. */
  2370. static int x509_crt_verify_chain(
  2371. mbedtls_x509_crt *crt,
  2372. mbedtls_x509_crt *trust_ca,
  2373. mbedtls_x509_crl *ca_crl,
  2374. mbedtls_x509_crt_ca_cb_t f_ca_cb,
  2375. void *p_ca_cb,
  2376. const mbedtls_x509_crt_profile *profile,
  2377. mbedtls_x509_crt_verify_chain *ver_chain,
  2378. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2379. {
  2380. /* Don't initialize any of those variables here, so that the compiler can
  2381. * catch potential issues with jumping ahead when restarting */
  2382. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2383. uint32_t *flags;
  2384. mbedtls_x509_crt_verify_chain_item *cur;
  2385. mbedtls_x509_crt *child;
  2386. mbedtls_x509_crt *parent;
  2387. int parent_is_trusted;
  2388. int child_is_trusted;
  2389. int signature_is_good;
  2390. unsigned self_cnt;
  2391. mbedtls_x509_crt *cur_trust_ca = NULL;
  2392. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2393. /* resume if we had an operation in progress */
  2394. if( rs_ctx != NULL && rs_ctx->in_progress == x509_crt_rs_find_parent )
  2395. {
  2396. /* restore saved state */
  2397. *ver_chain = rs_ctx->ver_chain; /* struct copy */
  2398. self_cnt = rs_ctx->self_cnt;
  2399. /* restore derived state */
  2400. cur = &ver_chain->items[ver_chain->len - 1];
  2401. child = cur->crt;
  2402. flags = &cur->flags;
  2403. goto find_parent;
  2404. }
  2405. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  2406. child = crt;
  2407. self_cnt = 0;
  2408. parent_is_trusted = 0;
  2409. child_is_trusted = 0;
  2410. while( 1 ) {
  2411. /* Add certificate to the verification chain */
  2412. cur = &ver_chain->items[ver_chain->len];
  2413. cur->crt = child;
  2414. cur->flags = 0;
  2415. ver_chain->len++;
  2416. flags = &cur->flags;
  2417. /* Check time-validity (all certificates) */
  2418. if( mbedtls_x509_time_is_past( &child->valid_to ) )
  2419. *flags |= MBEDTLS_X509_BADCERT_EXPIRED;
  2420. if( mbedtls_x509_time_is_future( &child->valid_from ) )
  2421. *flags |= MBEDTLS_X509_BADCERT_FUTURE;
  2422. /* Stop here for trusted roots (but not for trusted EE certs) */
  2423. if( child_is_trusted )
  2424. return( 0 );
  2425. /* Check signature algorithm: MD & PK algs */
  2426. if( x509_profile_check_md_alg( profile, child->sig_md ) != 0 )
  2427. *flags |= MBEDTLS_X509_BADCERT_BAD_MD;
  2428. if( x509_profile_check_pk_alg( profile, child->sig_pk ) != 0 )
  2429. *flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  2430. /* Special case: EE certs that are locally trusted */
  2431. if( ver_chain->len == 1 &&
  2432. x509_crt_check_ee_locally_trusted( child, trust_ca ) == 0 )
  2433. {
  2434. return( 0 );
  2435. }
  2436. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2437. find_parent:
  2438. #endif
  2439. /* Obtain list of potential trusted signers from CA callback,
  2440. * or use statically provided list. */
  2441. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2442. if( f_ca_cb != NULL )
  2443. {
  2444. mbedtls_x509_crt_free( ver_chain->trust_ca_cb_result );
  2445. mbedtls_free( ver_chain->trust_ca_cb_result );
  2446. ver_chain->trust_ca_cb_result = NULL;
  2447. ret = f_ca_cb( p_ca_cb, child, &ver_chain->trust_ca_cb_result );
  2448. if( ret != 0 )
  2449. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  2450. cur_trust_ca = ver_chain->trust_ca_cb_result;
  2451. }
  2452. else
  2453. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  2454. {
  2455. ((void) f_ca_cb);
  2456. ((void) p_ca_cb);
  2457. cur_trust_ca = trust_ca;
  2458. }
  2459. /* Look for a parent in trusted CAs or up the chain */
  2460. ret = x509_crt_find_parent( child, cur_trust_ca, &parent,
  2461. &parent_is_trusted, &signature_is_good,
  2462. ver_chain->len - 1, self_cnt, rs_ctx );
  2463. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2464. if( rs_ctx != NULL && ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
  2465. {
  2466. /* save state */
  2467. rs_ctx->in_progress = x509_crt_rs_find_parent;
  2468. rs_ctx->self_cnt = self_cnt;
  2469. rs_ctx->ver_chain = *ver_chain; /* struct copy */
  2470. return( ret );
  2471. }
  2472. #else
  2473. (void) ret;
  2474. #endif
  2475. /* No parent? We're done here */
  2476. if( parent == NULL )
  2477. {
  2478. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  2479. return( 0 );
  2480. }
  2481. /* Count intermediate self-issued (not necessarily self-signed) certs.
  2482. * These can occur with some strategies for key rollover, see [SIRO],
  2483. * and should be excluded from max_pathlen checks. */
  2484. if( ver_chain->len != 1 &&
  2485. x509_name_cmp( &child->issuer, &child->subject ) == 0 )
  2486. {
  2487. self_cnt++;
  2488. }
  2489. /* path_cnt is 0 for the first intermediate CA,
  2490. * and if parent is trusted it's not an intermediate CA */
  2491. if( ! parent_is_trusted &&
  2492. ver_chain->len > MBEDTLS_X509_MAX_INTERMEDIATE_CA )
  2493. {
  2494. /* return immediately to avoid overflow the chain array */
  2495. return( MBEDTLS_ERR_X509_FATAL_ERROR );
  2496. }
  2497. /* signature was checked while searching parent */
  2498. if( ! signature_is_good )
  2499. *flags |= MBEDTLS_X509_BADCERT_NOT_TRUSTED;
  2500. /* check size of signing key */
  2501. if( x509_profile_check_key( profile, &parent->pk ) != 0 )
  2502. *flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  2503. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  2504. /* Check trusted CA's CRL for the given crt */
  2505. *flags |= x509_crt_verifycrl( child, parent, ca_crl, profile );
  2506. #else
  2507. (void) ca_crl;
  2508. #endif
  2509. /* prepare for next iteration */
  2510. child = parent;
  2511. parent = NULL;
  2512. child_is_trusted = parent_is_trusted;
  2513. signature_is_good = 0;
  2514. }
  2515. }
  2516. /*
  2517. * Check for CN match
  2518. */
  2519. static int x509_crt_check_cn( const mbedtls_x509_buf *name,
  2520. const char *cn, size_t cn_len )
  2521. {
  2522. /* try exact match */
  2523. if( name->len == cn_len &&
  2524. x509_memcasecmp( cn, name->p, cn_len ) == 0 )
  2525. {
  2526. return( 0 );
  2527. }
  2528. /* try wildcard match */
  2529. if( x509_check_wildcard( cn, name ) == 0 )
  2530. {
  2531. return( 0 );
  2532. }
  2533. return( -1 );
  2534. }
  2535. /*
  2536. * Check for SAN match, see RFC 5280 Section 4.2.1.6
  2537. */
  2538. static int x509_crt_check_san( const mbedtls_x509_buf *name,
  2539. const char *cn, size_t cn_len )
  2540. {
  2541. const unsigned char san_type = (unsigned char) name->tag &
  2542. MBEDTLS_ASN1_TAG_VALUE_MASK;
  2543. /* dNSName */
  2544. if( san_type == MBEDTLS_X509_SAN_DNS_NAME )
  2545. return( x509_crt_check_cn( name, cn, cn_len ) );
  2546. /* (We may handle other types here later.) */
  2547. /* Unrecognized type */
  2548. return( -1 );
  2549. }
  2550. /*
  2551. * Verify the requested CN - only call this if cn is not NULL!
  2552. */
  2553. static void x509_crt_verify_name( const mbedtls_x509_crt *crt,
  2554. const char *cn,
  2555. uint32_t *flags )
  2556. {
  2557. const mbedtls_x509_name *name;
  2558. const mbedtls_x509_sequence *cur;
  2559. size_t cn_len = strlen( cn );
  2560. if( crt->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME )
  2561. {
  2562. for( cur = &crt->subject_alt_names; cur != NULL; cur = cur->next )
  2563. {
  2564. if( x509_crt_check_san( &cur->buf, cn, cn_len ) == 0 )
  2565. break;
  2566. }
  2567. if( cur == NULL )
  2568. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  2569. }
  2570. else
  2571. {
  2572. for( name = &crt->subject; name != NULL; name = name->next )
  2573. {
  2574. if( MBEDTLS_OID_CMP( MBEDTLS_OID_AT_CN, &name->oid ) == 0 &&
  2575. x509_crt_check_cn( &name->val, cn, cn_len ) == 0 )
  2576. {
  2577. break;
  2578. }
  2579. }
  2580. if( name == NULL )
  2581. *flags |= MBEDTLS_X509_BADCERT_CN_MISMATCH;
  2582. }
  2583. }
  2584. /*
  2585. * Merge the flags for all certs in the chain, after calling callback
  2586. */
  2587. static int x509_crt_merge_flags_with_cb(
  2588. uint32_t *flags,
  2589. const mbedtls_x509_crt_verify_chain *ver_chain,
  2590. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2591. void *p_vrfy )
  2592. {
  2593. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2594. unsigned i;
  2595. uint32_t cur_flags;
  2596. const mbedtls_x509_crt_verify_chain_item *cur;
  2597. for( i = ver_chain->len; i != 0; --i )
  2598. {
  2599. cur = &ver_chain->items[i-1];
  2600. cur_flags = cur->flags;
  2601. if( NULL != f_vrfy )
  2602. if( ( ret = f_vrfy( p_vrfy, cur->crt, (int) i-1, &cur_flags ) ) != 0 )
  2603. return( ret );
  2604. *flags |= cur_flags;
  2605. }
  2606. return( 0 );
  2607. }
  2608. /*
  2609. * Verify the certificate validity, with profile, restartable version
  2610. *
  2611. * This function:
  2612. * - checks the requested CN (if any)
  2613. * - checks the type and size of the EE cert's key,
  2614. * as that isn't done as part of chain building/verification currently
  2615. * - builds and verifies the chain
  2616. * - then calls the callback and merges the flags
  2617. *
  2618. * The parameters pairs `trust_ca`, `ca_crl` and `f_ca_cb`, `p_ca_cb`
  2619. * are mutually exclusive: If `f_ca_cb != NULL`, it will be used by the
  2620. * verification routine to search for trusted signers, and CRLs will
  2621. * be disabled. Otherwise, `trust_ca` will be used as the static list
  2622. * of trusted signers, and `ca_crl` will be use as the static list
  2623. * of CRLs.
  2624. */
  2625. static int x509_crt_verify_restartable_ca_cb( mbedtls_x509_crt *crt,
  2626. mbedtls_x509_crt *trust_ca,
  2627. mbedtls_x509_crl *ca_crl,
  2628. mbedtls_x509_crt_ca_cb_t f_ca_cb,
  2629. void *p_ca_cb,
  2630. const mbedtls_x509_crt_profile *profile,
  2631. const char *cn, uint32_t *flags,
  2632. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2633. void *p_vrfy,
  2634. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2635. {
  2636. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  2637. mbedtls_pk_type_t pk_type;
  2638. mbedtls_x509_crt_verify_chain ver_chain;
  2639. uint32_t ee_flags;
  2640. *flags = 0;
  2641. ee_flags = 0;
  2642. x509_crt_verify_chain_reset( &ver_chain );
  2643. if( profile == NULL )
  2644. {
  2645. ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA;
  2646. goto exit;
  2647. }
  2648. /* check name if requested */
  2649. if( cn != NULL )
  2650. x509_crt_verify_name( crt, cn, &ee_flags );
  2651. /* Check the type and size of the key */
  2652. pk_type = mbedtls_pk_get_type( &crt->pk );
  2653. if( x509_profile_check_pk_alg( profile, pk_type ) != 0 )
  2654. ee_flags |= MBEDTLS_X509_BADCERT_BAD_PK;
  2655. if( x509_profile_check_key( profile, &crt->pk ) != 0 )
  2656. ee_flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
  2657. /* Check the chain */
  2658. ret = x509_crt_verify_chain( crt, trust_ca, ca_crl,
  2659. f_ca_cb, p_ca_cb, profile,
  2660. &ver_chain, rs_ctx );
  2661. if( ret != 0 )
  2662. goto exit;
  2663. /* Merge end-entity flags */
  2664. ver_chain.items[0].flags |= ee_flags;
  2665. /* Build final flags, calling callback on the way if any */
  2666. ret = x509_crt_merge_flags_with_cb( flags, &ver_chain, f_vrfy, p_vrfy );
  2667. exit:
  2668. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2669. mbedtls_x509_crt_free( ver_chain.trust_ca_cb_result );
  2670. mbedtls_free( ver_chain.trust_ca_cb_result );
  2671. ver_chain.trust_ca_cb_result = NULL;
  2672. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  2673. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2674. if( rs_ctx != NULL && ret != MBEDTLS_ERR_ECP_IN_PROGRESS )
  2675. mbedtls_x509_crt_restart_free( rs_ctx );
  2676. #endif
  2677. /* prevent misuse of the vrfy callback - VERIFY_FAILED would be ignored by
  2678. * the SSL module for authmode optional, but non-zero return from the
  2679. * callback means a fatal error so it shouldn't be ignored */
  2680. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  2681. ret = MBEDTLS_ERR_X509_FATAL_ERROR;
  2682. if( ret != 0 )
  2683. {
  2684. *flags = (uint32_t) -1;
  2685. return( ret );
  2686. }
  2687. if( *flags != 0 )
  2688. return( MBEDTLS_ERR_X509_CERT_VERIFY_FAILED );
  2689. return( 0 );
  2690. }
  2691. /*
  2692. * Verify the certificate validity (default profile, not restartable)
  2693. */
  2694. int mbedtls_x509_crt_verify( mbedtls_x509_crt *crt,
  2695. mbedtls_x509_crt *trust_ca,
  2696. mbedtls_x509_crl *ca_crl,
  2697. const char *cn, uint32_t *flags,
  2698. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2699. void *p_vrfy )
  2700. {
  2701. return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
  2702. NULL, NULL,
  2703. &mbedtls_x509_crt_profile_default,
  2704. cn, flags,
  2705. f_vrfy, p_vrfy, NULL ) );
  2706. }
  2707. /*
  2708. * Verify the certificate validity (user-chosen profile, not restartable)
  2709. */
  2710. int mbedtls_x509_crt_verify_with_profile( mbedtls_x509_crt *crt,
  2711. mbedtls_x509_crt *trust_ca,
  2712. mbedtls_x509_crl *ca_crl,
  2713. const mbedtls_x509_crt_profile *profile,
  2714. const char *cn, uint32_t *flags,
  2715. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2716. void *p_vrfy )
  2717. {
  2718. return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
  2719. NULL, NULL,
  2720. profile, cn, flags,
  2721. f_vrfy, p_vrfy, NULL ) );
  2722. }
  2723. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2724. /*
  2725. * Verify the certificate validity (user-chosen profile, CA callback,
  2726. * not restartable).
  2727. */
  2728. int mbedtls_x509_crt_verify_with_ca_cb( mbedtls_x509_crt *crt,
  2729. mbedtls_x509_crt_ca_cb_t f_ca_cb,
  2730. void *p_ca_cb,
  2731. const mbedtls_x509_crt_profile *profile,
  2732. const char *cn, uint32_t *flags,
  2733. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2734. void *p_vrfy )
  2735. {
  2736. return( x509_crt_verify_restartable_ca_cb( crt, NULL, NULL,
  2737. f_ca_cb, p_ca_cb,
  2738. profile, cn, flags,
  2739. f_vrfy, p_vrfy, NULL ) );
  2740. }
  2741. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  2742. int mbedtls_x509_crt_verify_restartable( mbedtls_x509_crt *crt,
  2743. mbedtls_x509_crt *trust_ca,
  2744. mbedtls_x509_crl *ca_crl,
  2745. const mbedtls_x509_crt_profile *profile,
  2746. const char *cn, uint32_t *flags,
  2747. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  2748. void *p_vrfy,
  2749. mbedtls_x509_crt_restart_ctx *rs_ctx )
  2750. {
  2751. return( x509_crt_verify_restartable_ca_cb( crt, trust_ca, ca_crl,
  2752. NULL, NULL,
  2753. profile, cn, flags,
  2754. f_vrfy, p_vrfy, rs_ctx ) );
  2755. }
  2756. /*
  2757. * Initialize a certificate chain
  2758. */
  2759. void mbedtls_x509_crt_init( mbedtls_x509_crt *crt )
  2760. {
  2761. memset( crt, 0, sizeof(mbedtls_x509_crt) );
  2762. }
  2763. /*
  2764. * Unallocate all certificate data
  2765. */
  2766. void mbedtls_x509_crt_free( mbedtls_x509_crt *crt )
  2767. {
  2768. mbedtls_x509_crt *cert_cur = crt;
  2769. mbedtls_x509_crt *cert_prv;
  2770. mbedtls_x509_name *name_cur;
  2771. mbedtls_x509_name *name_prv;
  2772. mbedtls_x509_sequence *seq_cur;
  2773. mbedtls_x509_sequence *seq_prv;
  2774. if( crt == NULL )
  2775. return;
  2776. do
  2777. {
  2778. mbedtls_pk_free( &cert_cur->pk );
  2779. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  2780. mbedtls_free( cert_cur->sig_opts );
  2781. #endif
  2782. name_cur = cert_cur->issuer.next;
  2783. while( name_cur != NULL )
  2784. {
  2785. name_prv = name_cur;
  2786. name_cur = name_cur->next;
  2787. mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  2788. mbedtls_free( name_prv );
  2789. }
  2790. name_cur = cert_cur->subject.next;
  2791. while( name_cur != NULL )
  2792. {
  2793. name_prv = name_cur;
  2794. name_cur = name_cur->next;
  2795. mbedtls_platform_zeroize( name_prv, sizeof( mbedtls_x509_name ) );
  2796. mbedtls_free( name_prv );
  2797. }
  2798. seq_cur = cert_cur->ext_key_usage.next;
  2799. while( seq_cur != NULL )
  2800. {
  2801. seq_prv = seq_cur;
  2802. seq_cur = seq_cur->next;
  2803. mbedtls_platform_zeroize( seq_prv,
  2804. sizeof( mbedtls_x509_sequence ) );
  2805. mbedtls_free( seq_prv );
  2806. }
  2807. seq_cur = cert_cur->subject_alt_names.next;
  2808. while( seq_cur != NULL )
  2809. {
  2810. seq_prv = seq_cur;
  2811. seq_cur = seq_cur->next;
  2812. mbedtls_platform_zeroize( seq_prv,
  2813. sizeof( mbedtls_x509_sequence ) );
  2814. mbedtls_free( seq_prv );
  2815. }
  2816. seq_cur = cert_cur->certificate_policies.next;
  2817. while( seq_cur != NULL )
  2818. {
  2819. seq_prv = seq_cur;
  2820. seq_cur = seq_cur->next;
  2821. mbedtls_platform_zeroize( seq_prv,
  2822. sizeof( mbedtls_x509_sequence ) );
  2823. mbedtls_free( seq_prv );
  2824. }
  2825. if( cert_cur->raw.p != NULL && cert_cur->own_buffer )
  2826. {
  2827. mbedtls_platform_zeroize( cert_cur->raw.p, cert_cur->raw.len );
  2828. mbedtls_free( cert_cur->raw.p );
  2829. }
  2830. cert_cur = cert_cur->next;
  2831. }
  2832. while( cert_cur != NULL );
  2833. cert_cur = crt;
  2834. do
  2835. {
  2836. cert_prv = cert_cur;
  2837. cert_cur = cert_cur->next;
  2838. mbedtls_platform_zeroize( cert_prv, sizeof( mbedtls_x509_crt ) );
  2839. if( cert_prv != crt )
  2840. mbedtls_free( cert_prv );
  2841. }
  2842. while( cert_cur != NULL );
  2843. }
  2844. #if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
  2845. /*
  2846. * Initialize a restart context
  2847. */
  2848. void mbedtls_x509_crt_restart_init( mbedtls_x509_crt_restart_ctx *ctx )
  2849. {
  2850. mbedtls_pk_restart_init( &ctx->pk );
  2851. ctx->parent = NULL;
  2852. ctx->fallback_parent = NULL;
  2853. ctx->fallback_signature_is_good = 0;
  2854. ctx->parent_is_trusted = -1;
  2855. ctx->in_progress = x509_crt_rs_none;
  2856. ctx->self_cnt = 0;
  2857. x509_crt_verify_chain_reset( &ctx->ver_chain );
  2858. }
  2859. /*
  2860. * Free the components of a restart context
  2861. */
  2862. void mbedtls_x509_crt_restart_free( mbedtls_x509_crt_restart_ctx *ctx )
  2863. {
  2864. if( ctx == NULL )
  2865. return;
  2866. mbedtls_pk_restart_free( &ctx->pk );
  2867. mbedtls_x509_crt_restart_init( ctx );
  2868. }
  2869. #endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
  2870. #endif /* MBEDTLS_X509_CRT_PARSE_C */