x509_crt.c 105 KB

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