x509_crt.c 106 KB

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