x509_crt.c 106 KB

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