x509_crt.c 87 KB

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