x509_crt.c 105 KB

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