x509_crt.c 105 KB

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