x509_vfy.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <time.h>
  11. #include <errno.h>
  12. #include <limits.h>
  13. #include "internal/ctype.h"
  14. #include "internal/cryptlib.h"
  15. #include <openssl/crypto.h>
  16. #include <openssl/buffer.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/asn1.h>
  19. #include <openssl/x509.h>
  20. #include <openssl/x509v3.h>
  21. #include <openssl/objects.h>
  22. #include "internal/dane.h"
  23. #include "internal/x509_int.h"
  24. #include "x509_lcl.h"
  25. /* CRL score values */
  26. /* No unhandled critical extensions */
  27. #define CRL_SCORE_NOCRITICAL 0x100
  28. /* certificate is within CRL scope */
  29. #define CRL_SCORE_SCOPE 0x080
  30. /* CRL times valid */
  31. #define CRL_SCORE_TIME 0x040
  32. /* Issuer name matches certificate */
  33. #define CRL_SCORE_ISSUER_NAME 0x020
  34. /* If this score or above CRL is probably valid */
  35. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  36. /* CRL issuer is certificate issuer */
  37. #define CRL_SCORE_ISSUER_CERT 0x018
  38. /* CRL issuer is on certificate path */
  39. #define CRL_SCORE_SAME_PATH 0x008
  40. /* CRL issuer matches CRL AKID */
  41. #define CRL_SCORE_AKID 0x004
  42. /* Have a delta CRL with valid times */
  43. #define CRL_SCORE_TIME_DELTA 0x002
  44. static int build_chain(X509_STORE_CTX *ctx);
  45. static int verify_chain(X509_STORE_CTX *ctx);
  46. static int dane_verify(X509_STORE_CTX *ctx);
  47. static int null_callback(int ok, X509_STORE_CTX *e);
  48. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  49. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  50. static int check_chain_extensions(X509_STORE_CTX *ctx);
  51. static int check_name_constraints(X509_STORE_CTX *ctx);
  52. static int check_id(X509_STORE_CTX *ctx);
  53. static int check_trust(X509_STORE_CTX *ctx, int num_untrusted);
  54. static int check_revocation(X509_STORE_CTX *ctx);
  55. static int check_cert(X509_STORE_CTX *ctx);
  56. static int check_policy(X509_STORE_CTX *ctx);
  57. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x);
  58. static int check_dane_issuer(X509_STORE_CTX *ctx, int depth);
  59. static int check_key_level(X509_STORE_CTX *ctx, X509 *cert);
  60. static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert);
  61. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  62. unsigned int *preasons, X509_CRL *crl, X509 *x);
  63. static int get_crl_delta(X509_STORE_CTX *ctx,
  64. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  65. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
  66. int *pcrl_score, X509_CRL *base,
  67. STACK_OF(X509_CRL) *crls);
  68. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
  69. int *pcrl_score);
  70. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  71. unsigned int *preasons);
  72. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  73. static int check_crl_chain(X509_STORE_CTX *ctx,
  74. STACK_OF(X509) *cert_path,
  75. STACK_OF(X509) *crl_path);
  76. static int internal_verify(X509_STORE_CTX *ctx);
  77. static int null_callback(int ok, X509_STORE_CTX *e)
  78. {
  79. return ok;
  80. }
  81. /* Return 1 is a certificate is self signed */
  82. static int cert_self_signed(X509 *x)
  83. {
  84. /*
  85. * FIXME: x509v3_cache_extensions() needs to detect more failures and not
  86. * set EXFLAG_SET when that happens. Especially, if the failures are
  87. * parse errors, rather than memory pressure!
  88. */
  89. X509_check_purpose(x, -1, 0);
  90. if (x->ex_flags & EXFLAG_SS)
  91. return 1;
  92. else
  93. return 0;
  94. }
  95. /* Given a certificate try and find an exact match in the store */
  96. static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
  97. {
  98. STACK_OF(X509) *certs;
  99. X509 *xtmp = NULL;
  100. int i;
  101. /* Lookup all certs with matching subject name */
  102. certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
  103. if (certs == NULL)
  104. return NULL;
  105. /* Look for exact match */
  106. for (i = 0; i < sk_X509_num(certs); i++) {
  107. xtmp = sk_X509_value(certs, i);
  108. if (!X509_cmp(xtmp, x))
  109. break;
  110. }
  111. if (i < sk_X509_num(certs))
  112. X509_up_ref(xtmp);
  113. else
  114. xtmp = NULL;
  115. sk_X509_pop_free(certs, X509_free);
  116. return xtmp;
  117. }
  118. /*-
  119. * Inform the verify callback of an error.
  120. * If B<x> is not NULL it is the error cert, otherwise use the chain cert at
  121. * B<depth>.
  122. * If B<err> is not X509_V_OK, that's the error value, otherwise leave
  123. * unchanged (presumably set by the caller).
  124. *
  125. * Returns 0 to abort verification with an error, non-zero to continue.
  126. */
  127. static int verify_cb_cert(X509_STORE_CTX *ctx, X509 *x, int depth, int err)
  128. {
  129. ctx->error_depth = depth;
  130. ctx->current_cert = (x != NULL) ? x : sk_X509_value(ctx->chain, depth);
  131. if (err != X509_V_OK)
  132. ctx->error = err;
  133. return ctx->verify_cb(0, ctx);
  134. }
  135. /*-
  136. * Inform the verify callback of an error, CRL-specific variant. Here, the
  137. * error depth and certificate are already set, we just specify the error
  138. * number.
  139. *
  140. * Returns 0 to abort verification with an error, non-zero to continue.
  141. */
  142. static int verify_cb_crl(X509_STORE_CTX *ctx, int err)
  143. {
  144. ctx->error = err;
  145. return ctx->verify_cb(0, ctx);
  146. }
  147. static int check_auth_level(X509_STORE_CTX *ctx)
  148. {
  149. int i;
  150. int num = sk_X509_num(ctx->chain);
  151. if (ctx->param->auth_level <= 0)
  152. return 1;
  153. for (i = 0; i < num; ++i) {
  154. X509 *cert = sk_X509_value(ctx->chain, i);
  155. /*
  156. * We've already checked the security of the leaf key, so here we only
  157. * check the security of issuer keys.
  158. */
  159. if (i > 0 && !check_key_level(ctx, cert) &&
  160. verify_cb_cert(ctx, cert, i, X509_V_ERR_CA_KEY_TOO_SMALL) == 0)
  161. return 0;
  162. /*
  163. * We also check the signature algorithm security of all certificates
  164. * except those of the trust anchor at index num-1.
  165. */
  166. if (i < num - 1 && !check_sig_level(ctx, cert) &&
  167. verify_cb_cert(ctx, cert, i, X509_V_ERR_CA_MD_TOO_WEAK) == 0)
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. static int verify_chain(X509_STORE_CTX *ctx)
  173. {
  174. int err;
  175. int ok;
  176. /*
  177. * Before either returning with an error, or continuing with CRL checks,
  178. * instantiate chain public key parameters.
  179. */
  180. if ((ok = build_chain(ctx)) == 0 ||
  181. (ok = check_chain_extensions(ctx)) == 0 ||
  182. (ok = check_auth_level(ctx)) == 0 ||
  183. (ok = check_id(ctx)) == 0 || 1)
  184. X509_get_pubkey_parameters(NULL, ctx->chain);
  185. if (ok == 0 || (ok = ctx->check_revocation(ctx)) == 0)
  186. return ok;
  187. err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
  188. ctx->param->flags);
  189. if (err != X509_V_OK) {
  190. if ((ok = verify_cb_cert(ctx, NULL, ctx->error_depth, err)) == 0)
  191. return ok;
  192. }
  193. /* Verify chain signatures and expiration times */
  194. ok = (ctx->verify != NULL) ? ctx->verify(ctx) : internal_verify(ctx);
  195. if (!ok)
  196. return ok;
  197. if ((ok = check_name_constraints(ctx)) == 0)
  198. return ok;
  199. #ifndef OPENSSL_NO_RFC3779
  200. /* RFC 3779 path validation, now that CRL check has been done */
  201. if ((ok = X509v3_asid_validate_path(ctx)) == 0)
  202. return ok;
  203. if ((ok = X509v3_addr_validate_path(ctx)) == 0)
  204. return ok;
  205. #endif
  206. /* If we get this far evaluate policies */
  207. if (ctx->param->flags & X509_V_FLAG_POLICY_CHECK)
  208. ok = ctx->check_policy(ctx);
  209. return ok;
  210. }
  211. int X509_verify_cert(X509_STORE_CTX *ctx)
  212. {
  213. SSL_DANE *dane = ctx->dane;
  214. int ret;
  215. if (ctx->cert == NULL) {
  216. X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  217. ctx->error = X509_V_ERR_INVALID_CALL;
  218. return -1;
  219. }
  220. if (ctx->chain != NULL) {
  221. /*
  222. * This X509_STORE_CTX has already been used to verify a cert. We
  223. * cannot do another one.
  224. */
  225. X509err(X509_F_X509_VERIFY_CERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  226. ctx->error = X509_V_ERR_INVALID_CALL;
  227. return -1;
  228. }
  229. /*
  230. * first we make sure the chain we are going to build is present and that
  231. * the first entry is in place
  232. */
  233. if (((ctx->chain = sk_X509_new_null()) == NULL) ||
  234. (!sk_X509_push(ctx->chain, ctx->cert))) {
  235. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  236. ctx->error = X509_V_ERR_OUT_OF_MEM;
  237. return -1;
  238. }
  239. X509_up_ref(ctx->cert);
  240. ctx->num_untrusted = 1;
  241. /* If the peer's public key is too weak, we can stop early. */
  242. if (!check_key_level(ctx, ctx->cert) &&
  243. !verify_cb_cert(ctx, ctx->cert, 0, X509_V_ERR_EE_KEY_TOO_SMALL))
  244. return 0;
  245. if (DANETLS_ENABLED(dane))
  246. ret = dane_verify(ctx);
  247. else
  248. ret = verify_chain(ctx);
  249. /*
  250. * Safety-net. If we are returning an error, we must also set ctx->error,
  251. * so that the chain is not considered verified should the error be ignored
  252. * (e.g. TLS with SSL_VERIFY_NONE).
  253. */
  254. if (ret <= 0 && ctx->error == X509_V_OK)
  255. ctx->error = X509_V_ERR_UNSPECIFIED;
  256. return ret;
  257. }
  258. /*
  259. * Given a STACK_OF(X509) find the issuer of cert (if any)
  260. */
  261. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  262. {
  263. int i;
  264. X509 *issuer, *rv = NULL;
  265. for (i = 0; i < sk_X509_num(sk); i++) {
  266. issuer = sk_X509_value(sk, i);
  267. if (ctx->check_issued(ctx, x, issuer)) {
  268. rv = issuer;
  269. if (x509_check_cert_time(ctx, rv, -1))
  270. break;
  271. }
  272. }
  273. return rv;
  274. }
  275. /* Given a possible certificate and issuer check them */
  276. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  277. {
  278. int ret;
  279. if (x == issuer)
  280. return cert_self_signed(x);
  281. ret = X509_check_issued(issuer, x);
  282. if (ret == X509_V_OK) {
  283. int i;
  284. X509 *ch;
  285. /* Special case: single self signed certificate */
  286. if (cert_self_signed(x) && sk_X509_num(ctx->chain) == 1)
  287. return 1;
  288. for (i = 0; i < sk_X509_num(ctx->chain); i++) {
  289. ch = sk_X509_value(ctx->chain, i);
  290. if (ch == issuer || !X509_cmp(ch, issuer)) {
  291. ret = X509_V_ERR_PATH_LOOP;
  292. break;
  293. }
  294. }
  295. }
  296. return (ret == X509_V_OK);
  297. }
  298. /* Alternative lookup method: look from a STACK stored in other_ctx */
  299. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  300. {
  301. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  302. if (*issuer) {
  303. X509_up_ref(*issuer);
  304. return 1;
  305. } else
  306. return 0;
  307. }
  308. static STACK_OF(X509) *lookup_certs_sk(X509_STORE_CTX *ctx, X509_NAME *nm)
  309. {
  310. STACK_OF(X509) *sk = NULL;
  311. X509 *x;
  312. int i;
  313. for (i = 0; i < sk_X509_num(ctx->other_ctx); i++) {
  314. x = sk_X509_value(ctx->other_ctx, i);
  315. if (X509_NAME_cmp(nm, X509_get_subject_name(x)) == 0) {
  316. if (sk == NULL)
  317. sk = sk_X509_new_null();
  318. if (sk == NULL || sk_X509_push(sk, x) == 0) {
  319. sk_X509_pop_free(sk, X509_free);
  320. X509err(X509_F_LOOKUP_CERTS_SK, ERR_R_MALLOC_FAILURE);
  321. ctx->error = X509_V_ERR_OUT_OF_MEM;
  322. return NULL;
  323. }
  324. X509_up_ref(x);
  325. }
  326. }
  327. return sk;
  328. }
  329. /*
  330. * Check EE or CA certificate purpose. For trusted certificates explicit local
  331. * auxiliary trust can be used to override EKU-restrictions.
  332. */
  333. static int check_purpose(X509_STORE_CTX *ctx, X509 *x, int purpose, int depth,
  334. int must_be_ca)
  335. {
  336. int tr_ok = X509_TRUST_UNTRUSTED;
  337. /*
  338. * For trusted certificates we want to see whether any auxiliary trust
  339. * settings trump the purpose constraints.
  340. *
  341. * This is complicated by the fact that the trust ordinals in
  342. * ctx->param->trust are entirely independent of the purpose ordinals in
  343. * ctx->param->purpose!
  344. *
  345. * What connects them is their mutual initialization via calls from
  346. * X509_STORE_CTX_set_default() into X509_VERIFY_PARAM_lookup() which sets
  347. * related values of both param->trust and param->purpose. It is however
  348. * typically possible to infer associated trust values from a purpose value
  349. * via the X509_PURPOSE API.
  350. *
  351. * Therefore, we can only check for trust overrides when the purpose we're
  352. * checking is the same as ctx->param->purpose and ctx->param->trust is
  353. * also set.
  354. */
  355. if (depth >= ctx->num_untrusted && purpose == ctx->param->purpose)
  356. tr_ok = X509_check_trust(x, ctx->param->trust, X509_TRUST_NO_SS_COMPAT);
  357. switch (tr_ok) {
  358. case X509_TRUST_TRUSTED:
  359. return 1;
  360. case X509_TRUST_REJECTED:
  361. break;
  362. default:
  363. switch (X509_check_purpose(x, purpose, must_be_ca > 0)) {
  364. case 1:
  365. return 1;
  366. case 0:
  367. break;
  368. default:
  369. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT) == 0)
  370. return 1;
  371. }
  372. break;
  373. }
  374. return verify_cb_cert(ctx, x, depth, X509_V_ERR_INVALID_PURPOSE);
  375. }
  376. /*
  377. * Check a certificate chains extensions for consistency with the supplied
  378. * purpose
  379. */
  380. static int check_chain_extensions(X509_STORE_CTX *ctx)
  381. {
  382. int i, must_be_ca, plen = 0;
  383. X509 *x;
  384. int proxy_path_length = 0;
  385. int purpose;
  386. int allow_proxy_certs;
  387. int num = sk_X509_num(ctx->chain);
  388. /*-
  389. * must_be_ca can have 1 of 3 values:
  390. * -1: we accept both CA and non-CA certificates, to allow direct
  391. * use of self-signed certificates (which are marked as CA).
  392. * 0: we only accept non-CA certificates. This is currently not
  393. * used, but the possibility is present for future extensions.
  394. * 1: we only accept CA certificates. This is currently used for
  395. * all certificates in the chain except the leaf certificate.
  396. */
  397. must_be_ca = -1;
  398. /* CRL path validation */
  399. if (ctx->parent) {
  400. allow_proxy_certs = 0;
  401. purpose = X509_PURPOSE_CRL_SIGN;
  402. } else {
  403. allow_proxy_certs =
  404. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  405. purpose = ctx->param->purpose;
  406. }
  407. for (i = 0; i < num; i++) {
  408. int ret;
  409. x = sk_X509_value(ctx->chain, i);
  410. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  411. && (x->ex_flags & EXFLAG_CRITICAL)) {
  412. if (!verify_cb_cert(ctx, x, i,
  413. X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION))
  414. return 0;
  415. }
  416. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  417. if (!verify_cb_cert(ctx, x, i,
  418. X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED))
  419. return 0;
  420. }
  421. ret = X509_check_ca(x);
  422. switch (must_be_ca) {
  423. case -1:
  424. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  425. && (ret != 1) && (ret != 0)) {
  426. ret = 0;
  427. ctx->error = X509_V_ERR_INVALID_CA;
  428. } else
  429. ret = 1;
  430. break;
  431. case 0:
  432. if (ret != 0) {
  433. ret = 0;
  434. ctx->error = X509_V_ERR_INVALID_NON_CA;
  435. } else
  436. ret = 1;
  437. break;
  438. default:
  439. /* X509_V_FLAG_X509_STRICT is implicit for intermediate CAs */
  440. if ((ret == 0)
  441. || ((i + 1 < num || ctx->param->flags & X509_V_FLAG_X509_STRICT)
  442. && (ret != 1))) {
  443. ret = 0;
  444. ctx->error = X509_V_ERR_INVALID_CA;
  445. } else
  446. ret = 1;
  447. break;
  448. }
  449. if (ret == 0 && !verify_cb_cert(ctx, x, i, X509_V_OK))
  450. return 0;
  451. /* check_purpose() makes the callback as needed */
  452. if (purpose > 0 && !check_purpose(ctx, x, purpose, i, must_be_ca))
  453. return 0;
  454. /* Check pathlen */
  455. if ((i > 1) && (x->ex_pathlen != -1)
  456. && (plen > (x->ex_pathlen + proxy_path_length))) {
  457. if (!verify_cb_cert(ctx, x, i, X509_V_ERR_PATH_LENGTH_EXCEEDED))
  458. return 0;
  459. }
  460. /* Increment path length if not a self issued intermediate CA */
  461. if (i > 0 && (x->ex_flags & EXFLAG_SI) == 0)
  462. plen++;
  463. /*
  464. * If this certificate is a proxy certificate, the next certificate
  465. * must be another proxy certificate or a EE certificate. If not,
  466. * the next certificate must be a CA certificate.
  467. */
  468. if (x->ex_flags & EXFLAG_PROXY) {
  469. /*
  470. * RFC3820, 4.1.3 (b)(1) stipulates that if pCPathLengthConstraint
  471. * is less than max_path_length, the former should be copied to
  472. * the latter, and 4.1.4 (a) stipulates that max_path_length
  473. * should be verified to be larger than zero and decrement it.
  474. *
  475. * Because we're checking the certs in the reverse order, we start
  476. * with verifying that proxy_path_length isn't larger than pcPLC,
  477. * and copy the latter to the former if it is, and finally,
  478. * increment proxy_path_length.
  479. */
  480. if (x->ex_pcpathlen != -1) {
  481. if (proxy_path_length > x->ex_pcpathlen) {
  482. if (!verify_cb_cert(ctx, x, i,
  483. X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED))
  484. return 0;
  485. }
  486. proxy_path_length = x->ex_pcpathlen;
  487. }
  488. proxy_path_length++;
  489. must_be_ca = 0;
  490. } else
  491. must_be_ca = 1;
  492. }
  493. return 1;
  494. }
  495. static int has_san_id(X509 *x, int gtype)
  496. {
  497. int i;
  498. int ret = 0;
  499. GENERAL_NAMES *gs = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
  500. if (gs == NULL)
  501. return 0;
  502. for (i = 0; i < sk_GENERAL_NAME_num(gs); i++) {
  503. GENERAL_NAME *g = sk_GENERAL_NAME_value(gs, i);
  504. if (g->type == gtype) {
  505. ret = 1;
  506. break;
  507. }
  508. }
  509. GENERAL_NAMES_free(gs);
  510. return ret;
  511. }
  512. static int check_name_constraints(X509_STORE_CTX *ctx)
  513. {
  514. int i;
  515. /* Check name constraints for all certificates */
  516. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
  517. X509 *x = sk_X509_value(ctx->chain, i);
  518. int j;
  519. /* Ignore self issued certs unless last in chain */
  520. if (i && (x->ex_flags & EXFLAG_SI))
  521. continue;
  522. /*
  523. * Proxy certificates policy has an extra constraint, where the
  524. * certificate subject MUST be the issuer with a single CN entry
  525. * added.
  526. * (RFC 3820: 3.4, 4.1.3 (a)(4))
  527. */
  528. if (x->ex_flags & EXFLAG_PROXY) {
  529. X509_NAME *tmpsubject = X509_get_subject_name(x);
  530. X509_NAME *tmpissuer = X509_get_issuer_name(x);
  531. X509_NAME_ENTRY *tmpentry = NULL;
  532. int last_object_nid = 0;
  533. int err = X509_V_OK;
  534. int last_object_loc = X509_NAME_entry_count(tmpsubject) - 1;
  535. /* Check that there are at least two RDNs */
  536. if (last_object_loc < 1) {
  537. err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
  538. goto proxy_name_done;
  539. }
  540. /*
  541. * Check that there is exactly one more RDN in subject as
  542. * there is in issuer.
  543. */
  544. if (X509_NAME_entry_count(tmpsubject)
  545. != X509_NAME_entry_count(tmpissuer) + 1) {
  546. err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
  547. goto proxy_name_done;
  548. }
  549. /*
  550. * Check that the last subject component isn't part of a
  551. * multivalued RDN
  552. */
  553. if (X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject,
  554. last_object_loc))
  555. == X509_NAME_ENTRY_set(X509_NAME_get_entry(tmpsubject,
  556. last_object_loc - 1))) {
  557. err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
  558. goto proxy_name_done;
  559. }
  560. /*
  561. * Check that the last subject RDN is a commonName, and that
  562. * all the previous RDNs match the issuer exactly
  563. */
  564. tmpsubject = X509_NAME_dup(tmpsubject);
  565. if (tmpsubject == NULL) {
  566. X509err(X509_F_CHECK_NAME_CONSTRAINTS, ERR_R_MALLOC_FAILURE);
  567. ctx->error = X509_V_ERR_OUT_OF_MEM;
  568. return 0;
  569. }
  570. tmpentry =
  571. X509_NAME_delete_entry(tmpsubject, last_object_loc);
  572. last_object_nid =
  573. OBJ_obj2nid(X509_NAME_ENTRY_get_object(tmpentry));
  574. if (last_object_nid != NID_commonName
  575. || X509_NAME_cmp(tmpsubject, tmpissuer) != 0) {
  576. err = X509_V_ERR_PROXY_SUBJECT_NAME_VIOLATION;
  577. }
  578. X509_NAME_ENTRY_free(tmpentry);
  579. X509_NAME_free(tmpsubject);
  580. proxy_name_done:
  581. if (err != X509_V_OK
  582. && !verify_cb_cert(ctx, x, i, err))
  583. return 0;
  584. }
  585. /*
  586. * Check against constraints for all certificates higher in chain
  587. * including trust anchor. Trust anchor not strictly speaking needed
  588. * but if it includes constraints it is to be assumed it expects them
  589. * to be obeyed.
  590. */
  591. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
  592. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  593. if (nc) {
  594. int rv = NAME_CONSTRAINTS_check(x, nc);
  595. /* If EE certificate check commonName too */
  596. if (rv == X509_V_OK && i == 0
  597. && (ctx->param->hostflags
  598. & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT) == 0
  599. && ((ctx->param->hostflags
  600. & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT) != 0
  601. || !has_san_id(x, GEN_DNS)))
  602. rv = NAME_CONSTRAINTS_check_CN(x, nc);
  603. switch (rv) {
  604. case X509_V_OK:
  605. break;
  606. case X509_V_ERR_OUT_OF_MEM:
  607. return 0;
  608. default:
  609. if (!verify_cb_cert(ctx, x, i, rv))
  610. return 0;
  611. break;
  612. }
  613. }
  614. }
  615. }
  616. return 1;
  617. }
  618. static int check_id_error(X509_STORE_CTX *ctx, int errcode)
  619. {
  620. return verify_cb_cert(ctx, ctx->cert, 0, errcode);
  621. }
  622. static int check_hosts(X509 *x, X509_VERIFY_PARAM *vpm)
  623. {
  624. int i;
  625. int n = sk_OPENSSL_STRING_num(vpm->hosts);
  626. char *name;
  627. if (vpm->peername != NULL) {
  628. OPENSSL_free(vpm->peername);
  629. vpm->peername = NULL;
  630. }
  631. for (i = 0; i < n; ++i) {
  632. name = sk_OPENSSL_STRING_value(vpm->hosts, i);
  633. if (X509_check_host(x, name, 0, vpm->hostflags, &vpm->peername) > 0)
  634. return 1;
  635. }
  636. return n == 0;
  637. }
  638. static int check_id(X509_STORE_CTX *ctx)
  639. {
  640. X509_VERIFY_PARAM *vpm = ctx->param;
  641. X509 *x = ctx->cert;
  642. if (vpm->hosts && check_hosts(x, vpm) <= 0) {
  643. if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
  644. return 0;
  645. }
  646. if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) {
  647. if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
  648. return 0;
  649. }
  650. if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) {
  651. if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
  652. return 0;
  653. }
  654. return 1;
  655. }
  656. static int check_trust(X509_STORE_CTX *ctx, int num_untrusted)
  657. {
  658. int i;
  659. X509 *x = NULL;
  660. X509 *mx;
  661. SSL_DANE *dane = ctx->dane;
  662. int num = sk_X509_num(ctx->chain);
  663. int trust;
  664. /*
  665. * Check for a DANE issuer at depth 1 or greater, if it is a DANE-TA(2)
  666. * match, we're done, otherwise we'll merely record the match depth.
  667. */
  668. if (DANETLS_HAS_TA(dane) && num_untrusted > 0 && num_untrusted < num) {
  669. switch (trust = check_dane_issuer(ctx, num_untrusted)) {
  670. case X509_TRUST_TRUSTED:
  671. case X509_TRUST_REJECTED:
  672. return trust;
  673. }
  674. }
  675. /*
  676. * Check trusted certificates in chain at depth num_untrusted and up.
  677. * Note, that depths 0..num_untrusted-1 may also contain trusted
  678. * certificates, but the caller is expected to have already checked those,
  679. * and wants to incrementally check just any added since.
  680. */
  681. for (i = num_untrusted; i < num; i++) {
  682. x = sk_X509_value(ctx->chain, i);
  683. trust = X509_check_trust(x, ctx->param->trust, 0);
  684. /* If explicitly trusted return trusted */
  685. if (trust == X509_TRUST_TRUSTED)
  686. goto trusted;
  687. if (trust == X509_TRUST_REJECTED)
  688. goto rejected;
  689. }
  690. /*
  691. * If we are looking at a trusted certificate, and accept partial chains,
  692. * the chain is PKIX trusted.
  693. */
  694. if (num_untrusted < num) {
  695. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN)
  696. goto trusted;
  697. return X509_TRUST_UNTRUSTED;
  698. }
  699. if (num_untrusted == num && ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  700. /*
  701. * Last-resort call with no new trusted certificates, check the leaf
  702. * for a direct trust store match.
  703. */
  704. i = 0;
  705. x = sk_X509_value(ctx->chain, i);
  706. mx = lookup_cert_match(ctx, x);
  707. if (!mx)
  708. return X509_TRUST_UNTRUSTED;
  709. /*
  710. * Check explicit auxiliary trust/reject settings. If none are set,
  711. * we'll accept X509_TRUST_UNTRUSTED when not self-signed.
  712. */
  713. trust = X509_check_trust(mx, ctx->param->trust, 0);
  714. if (trust == X509_TRUST_REJECTED) {
  715. X509_free(mx);
  716. goto rejected;
  717. }
  718. /* Replace leaf with trusted match */
  719. (void) sk_X509_set(ctx->chain, 0, mx);
  720. X509_free(x);
  721. ctx->num_untrusted = 0;
  722. goto trusted;
  723. }
  724. /*
  725. * If no trusted certs in chain at all return untrusted and allow
  726. * standard (no issuer cert) etc errors to be indicated.
  727. */
  728. return X509_TRUST_UNTRUSTED;
  729. rejected:
  730. if (!verify_cb_cert(ctx, x, i, X509_V_ERR_CERT_REJECTED))
  731. return X509_TRUST_REJECTED;
  732. return X509_TRUST_UNTRUSTED;
  733. trusted:
  734. if (!DANETLS_ENABLED(dane))
  735. return X509_TRUST_TRUSTED;
  736. if (dane->pdpth < 0)
  737. dane->pdpth = num_untrusted;
  738. /* With DANE, PKIX alone is not trusted until we have both */
  739. if (dane->mdpth >= 0)
  740. return X509_TRUST_TRUSTED;
  741. return X509_TRUST_UNTRUSTED;
  742. }
  743. static int check_revocation(X509_STORE_CTX *ctx)
  744. {
  745. int i = 0, last = 0, ok = 0;
  746. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  747. return 1;
  748. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  749. last = sk_X509_num(ctx->chain) - 1;
  750. else {
  751. /* If checking CRL paths this isn't the EE certificate */
  752. if (ctx->parent)
  753. return 1;
  754. last = 0;
  755. }
  756. for (i = 0; i <= last; i++) {
  757. ctx->error_depth = i;
  758. ok = check_cert(ctx);
  759. if (!ok)
  760. return ok;
  761. }
  762. return 1;
  763. }
  764. static int check_cert(X509_STORE_CTX *ctx)
  765. {
  766. X509_CRL *crl = NULL, *dcrl = NULL;
  767. int ok = 0;
  768. int cnum = ctx->error_depth;
  769. X509 *x = sk_X509_value(ctx->chain, cnum);
  770. ctx->current_cert = x;
  771. ctx->current_issuer = NULL;
  772. ctx->current_crl_score = 0;
  773. ctx->current_reasons = 0;
  774. if (x->ex_flags & EXFLAG_PROXY)
  775. return 1;
  776. while (ctx->current_reasons != CRLDP_ALL_REASONS) {
  777. unsigned int last_reasons = ctx->current_reasons;
  778. /* Try to retrieve relevant CRL */
  779. if (ctx->get_crl)
  780. ok = ctx->get_crl(ctx, &crl, x);
  781. else
  782. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  783. /*
  784. * If error looking up CRL, nothing we can do except notify callback
  785. */
  786. if (!ok) {
  787. ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);
  788. goto done;
  789. }
  790. ctx->current_crl = crl;
  791. ok = ctx->check_crl(ctx, crl);
  792. if (!ok)
  793. goto done;
  794. if (dcrl) {
  795. ok = ctx->check_crl(ctx, dcrl);
  796. if (!ok)
  797. goto done;
  798. ok = ctx->cert_crl(ctx, dcrl, x);
  799. if (!ok)
  800. goto done;
  801. } else
  802. ok = 1;
  803. /* Don't look in full CRL if delta reason is removefromCRL */
  804. if (ok != 2) {
  805. ok = ctx->cert_crl(ctx, crl, x);
  806. if (!ok)
  807. goto done;
  808. }
  809. X509_CRL_free(crl);
  810. X509_CRL_free(dcrl);
  811. crl = NULL;
  812. dcrl = NULL;
  813. /*
  814. * If reasons not updated we won't get anywhere by another iteration,
  815. * so exit loop.
  816. */
  817. if (last_reasons == ctx->current_reasons) {
  818. ok = verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL);
  819. goto done;
  820. }
  821. }
  822. done:
  823. X509_CRL_free(crl);
  824. X509_CRL_free(dcrl);
  825. ctx->current_crl = NULL;
  826. return ok;
  827. }
  828. /* Check CRL times against values in X509_STORE_CTX */
  829. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  830. {
  831. time_t *ptime;
  832. int i;
  833. if (notify)
  834. ctx->current_crl = crl;
  835. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  836. ptime = &ctx->param->check_time;
  837. else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
  838. return 1;
  839. else
  840. ptime = NULL;
  841. i = X509_cmp_time(X509_CRL_get0_lastUpdate(crl), ptime);
  842. if (i == 0) {
  843. if (!notify)
  844. return 0;
  845. if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD))
  846. return 0;
  847. }
  848. if (i > 0) {
  849. if (!notify)
  850. return 0;
  851. if (!verify_cb_crl(ctx, X509_V_ERR_CRL_NOT_YET_VALID))
  852. return 0;
  853. }
  854. if (X509_CRL_get0_nextUpdate(crl)) {
  855. i = X509_cmp_time(X509_CRL_get0_nextUpdate(crl), ptime);
  856. if (i == 0) {
  857. if (!notify)
  858. return 0;
  859. if (!verify_cb_crl(ctx, X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD))
  860. return 0;
  861. }
  862. /* Ignore expiry of base CRL is delta is valid */
  863. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
  864. if (!notify)
  865. return 0;
  866. if (!verify_cb_crl(ctx, X509_V_ERR_CRL_HAS_EXPIRED))
  867. return 0;
  868. }
  869. }
  870. if (notify)
  871. ctx->current_crl = NULL;
  872. return 1;
  873. }
  874. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  875. X509 **pissuer, int *pscore, unsigned int *preasons,
  876. STACK_OF(X509_CRL) *crls)
  877. {
  878. int i, crl_score, best_score = *pscore;
  879. unsigned int reasons, best_reasons = 0;
  880. X509 *x = ctx->current_cert;
  881. X509_CRL *crl, *best_crl = NULL;
  882. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  883. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  884. crl = sk_X509_CRL_value(crls, i);
  885. reasons = *preasons;
  886. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  887. if (crl_score < best_score || crl_score == 0)
  888. continue;
  889. /* If current CRL is equivalent use it if it is newer */
  890. if (crl_score == best_score && best_crl != NULL) {
  891. int day, sec;
  892. if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl),
  893. X509_CRL_get0_lastUpdate(crl)) == 0)
  894. continue;
  895. /*
  896. * ASN1_TIME_diff never returns inconsistent signs for |day|
  897. * and |sec|.
  898. */
  899. if (day <= 0 && sec <= 0)
  900. continue;
  901. }
  902. best_crl = crl;
  903. best_crl_issuer = crl_issuer;
  904. best_score = crl_score;
  905. best_reasons = reasons;
  906. }
  907. if (best_crl) {
  908. X509_CRL_free(*pcrl);
  909. *pcrl = best_crl;
  910. *pissuer = best_crl_issuer;
  911. *pscore = best_score;
  912. *preasons = best_reasons;
  913. X509_CRL_up_ref(best_crl);
  914. X509_CRL_free(*pdcrl);
  915. *pdcrl = NULL;
  916. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  917. }
  918. if (best_score >= CRL_SCORE_VALID)
  919. return 1;
  920. return 0;
  921. }
  922. /*
  923. * Compare two CRL extensions for delta checking purposes. They should be
  924. * both present or both absent. If both present all fields must be identical.
  925. */
  926. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  927. {
  928. ASN1_OCTET_STRING *exta, *extb;
  929. int i;
  930. i = X509_CRL_get_ext_by_NID(a, nid, -1);
  931. if (i >= 0) {
  932. /* Can't have multiple occurrences */
  933. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  934. return 0;
  935. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  936. } else
  937. exta = NULL;
  938. i = X509_CRL_get_ext_by_NID(b, nid, -1);
  939. if (i >= 0) {
  940. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  941. return 0;
  942. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  943. } else
  944. extb = NULL;
  945. if (!exta && !extb)
  946. return 1;
  947. if (!exta || !extb)
  948. return 0;
  949. if (ASN1_OCTET_STRING_cmp(exta, extb))
  950. return 0;
  951. return 1;
  952. }
  953. /* See if a base and delta are compatible */
  954. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  955. {
  956. /* Delta CRL must be a delta */
  957. if (!delta->base_crl_number)
  958. return 0;
  959. /* Base must have a CRL number */
  960. if (!base->crl_number)
  961. return 0;
  962. /* Issuer names must match */
  963. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
  964. return 0;
  965. /* AKID and IDP must match */
  966. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  967. return 0;
  968. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  969. return 0;
  970. /* Delta CRL base number must not exceed Full CRL number. */
  971. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  972. return 0;
  973. /* Delta CRL number must exceed full CRL number */
  974. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  975. return 1;
  976. return 0;
  977. }
  978. /*
  979. * For a given base CRL find a delta... maybe extend to delta scoring or
  980. * retrieve a chain of deltas...
  981. */
  982. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  983. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  984. {
  985. X509_CRL *delta;
  986. int i;
  987. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  988. return;
  989. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  990. return;
  991. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  992. delta = sk_X509_CRL_value(crls, i);
  993. if (check_delta_base(delta, base)) {
  994. if (check_crl_time(ctx, delta, 0))
  995. *pscore |= CRL_SCORE_TIME_DELTA;
  996. X509_CRL_up_ref(delta);
  997. *dcrl = delta;
  998. return;
  999. }
  1000. }
  1001. *dcrl = NULL;
  1002. }
  1003. /*
  1004. * For a given CRL return how suitable it is for the supplied certificate
  1005. * 'x'. The return value is a mask of several criteria. If the issuer is not
  1006. * the certificate issuer this is returned in *pissuer. The reasons mask is
  1007. * also used to determine if the CRL is suitable: if no new reasons the CRL
  1008. * is rejected, otherwise reasons is updated.
  1009. */
  1010. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  1011. unsigned int *preasons, X509_CRL *crl, X509 *x)
  1012. {
  1013. int crl_score = 0;
  1014. unsigned int tmp_reasons = *preasons, crl_reasons;
  1015. /* First see if we can reject CRL straight away */
  1016. /* Invalid IDP cannot be processed */
  1017. if (crl->idp_flags & IDP_INVALID)
  1018. return 0;
  1019. /* Reason codes or indirect CRLs need extended CRL support */
  1020. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
  1021. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  1022. return 0;
  1023. } else if (crl->idp_flags & IDP_REASONS) {
  1024. /* If no new reasons reject */
  1025. if (!(crl->idp_reasons & ~tmp_reasons))
  1026. return 0;
  1027. }
  1028. /* Don't process deltas at this stage */
  1029. else if (crl->base_crl_number)
  1030. return 0;
  1031. /* If issuer name doesn't match certificate need indirect CRL */
  1032. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
  1033. if (!(crl->idp_flags & IDP_INDIRECT))
  1034. return 0;
  1035. } else
  1036. crl_score |= CRL_SCORE_ISSUER_NAME;
  1037. if (!(crl->flags & EXFLAG_CRITICAL))
  1038. crl_score |= CRL_SCORE_NOCRITICAL;
  1039. /* Check expiry */
  1040. if (check_crl_time(ctx, crl, 0))
  1041. crl_score |= CRL_SCORE_TIME;
  1042. /* Check authority key ID and locate certificate issuer */
  1043. crl_akid_check(ctx, crl, pissuer, &crl_score);
  1044. /* If we can't locate certificate issuer at this point forget it */
  1045. if (!(crl_score & CRL_SCORE_AKID))
  1046. return 0;
  1047. /* Check cert for matching CRL distribution points */
  1048. if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
  1049. /* If no new reasons reject */
  1050. if (!(crl_reasons & ~tmp_reasons))
  1051. return 0;
  1052. tmp_reasons |= crl_reasons;
  1053. crl_score |= CRL_SCORE_SCOPE;
  1054. }
  1055. *preasons = tmp_reasons;
  1056. return crl_score;
  1057. }
  1058. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  1059. X509 **pissuer, int *pcrl_score)
  1060. {
  1061. X509 *crl_issuer = NULL;
  1062. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  1063. int cidx = ctx->error_depth;
  1064. int i;
  1065. if (cidx != sk_X509_num(ctx->chain) - 1)
  1066. cidx++;
  1067. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1068. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1069. if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
  1070. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
  1071. *pissuer = crl_issuer;
  1072. return;
  1073. }
  1074. }
  1075. for (cidx++; cidx < sk_X509_num(ctx->chain); cidx++) {
  1076. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1077. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1078. continue;
  1079. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1080. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
  1081. *pissuer = crl_issuer;
  1082. return;
  1083. }
  1084. }
  1085. /* Anything else needs extended CRL support */
  1086. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  1087. return;
  1088. /*
  1089. * Otherwise the CRL issuer is not on the path. Look for it in the set of
  1090. * untrusted certificates.
  1091. */
  1092. for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
  1093. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1094. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1095. continue;
  1096. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1097. *pissuer = crl_issuer;
  1098. *pcrl_score |= CRL_SCORE_AKID;
  1099. return;
  1100. }
  1101. }
  1102. }
  1103. /*
  1104. * Check the path of a CRL issuer certificate. This creates a new
  1105. * X509_STORE_CTX and populates it with most of the parameters from the
  1106. * parent. This could be optimised somewhat since a lot of path checking will
  1107. * be duplicated by the parent, but this will rarely be used in practice.
  1108. */
  1109. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1110. {
  1111. X509_STORE_CTX crl_ctx;
  1112. int ret;
  1113. /* Don't allow recursive CRL path validation */
  1114. if (ctx->parent)
  1115. return 0;
  1116. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1117. return -1;
  1118. crl_ctx.crls = ctx->crls;
  1119. /* Copy verify params across */
  1120. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1121. crl_ctx.parent = ctx;
  1122. crl_ctx.verify_cb = ctx->verify_cb;
  1123. /* Verify CRL issuer */
  1124. ret = X509_verify_cert(&crl_ctx);
  1125. if (ret <= 0)
  1126. goto err;
  1127. /* Check chain is acceptable */
  1128. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1129. err:
  1130. X509_STORE_CTX_cleanup(&crl_ctx);
  1131. return ret;
  1132. }
  1133. /*
  1134. * RFC3280 says nothing about the relationship between CRL path and
  1135. * certificate path, which could lead to situations where a certificate could
  1136. * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  1137. * strict and states that the two paths must end in the same trust anchor,
  1138. * though some discussions remain... until this is resolved we use the
  1139. * RFC5280 version
  1140. */
  1141. static int check_crl_chain(X509_STORE_CTX *ctx,
  1142. STACK_OF(X509) *cert_path,
  1143. STACK_OF(X509) *crl_path)
  1144. {
  1145. X509 *cert_ta, *crl_ta;
  1146. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1147. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1148. if (!X509_cmp(cert_ta, crl_ta))
  1149. return 1;
  1150. return 0;
  1151. }
  1152. /*-
  1153. * Check for match between two dist point names: three separate cases.
  1154. * 1. Both are relative names and compare X509_NAME types.
  1155. * 2. One full, one relative. Compare X509_NAME to GENERAL_NAMES.
  1156. * 3. Both are full names and compare two GENERAL_NAMES.
  1157. * 4. One is NULL: automatic match.
  1158. */
  1159. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1160. {
  1161. X509_NAME *nm = NULL;
  1162. GENERAL_NAMES *gens = NULL;
  1163. GENERAL_NAME *gena, *genb;
  1164. int i, j;
  1165. if (!a || !b)
  1166. return 1;
  1167. if (a->type == 1) {
  1168. if (!a->dpname)
  1169. return 0;
  1170. /* Case 1: two X509_NAME */
  1171. if (b->type == 1) {
  1172. if (!b->dpname)
  1173. return 0;
  1174. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1175. return 1;
  1176. else
  1177. return 0;
  1178. }
  1179. /* Case 2: set name and GENERAL_NAMES appropriately */
  1180. nm = a->dpname;
  1181. gens = b->name.fullname;
  1182. } else if (b->type == 1) {
  1183. if (!b->dpname)
  1184. return 0;
  1185. /* Case 2: set name and GENERAL_NAMES appropriately */
  1186. gens = a->name.fullname;
  1187. nm = b->dpname;
  1188. }
  1189. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1190. if (nm) {
  1191. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  1192. gena = sk_GENERAL_NAME_value(gens, i);
  1193. if (gena->type != GEN_DIRNAME)
  1194. continue;
  1195. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1196. return 1;
  1197. }
  1198. return 0;
  1199. }
  1200. /* Else case 3: two GENERAL_NAMES */
  1201. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
  1202. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1203. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
  1204. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1205. if (!GENERAL_NAME_cmp(gena, genb))
  1206. return 1;
  1207. }
  1208. }
  1209. return 0;
  1210. }
  1211. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1212. {
  1213. int i;
  1214. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1215. /* If no CRLissuer return is successful iff don't need a match */
  1216. if (!dp->CRLissuer)
  1217. return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
  1218. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  1219. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1220. if (gen->type != GEN_DIRNAME)
  1221. continue;
  1222. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1223. return 1;
  1224. }
  1225. return 0;
  1226. }
  1227. /* Check CRLDP and IDP */
  1228. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1229. unsigned int *preasons)
  1230. {
  1231. int i;
  1232. if (crl->idp_flags & IDP_ONLYATTR)
  1233. return 0;
  1234. if (x->ex_flags & EXFLAG_CA) {
  1235. if (crl->idp_flags & IDP_ONLYUSER)
  1236. return 0;
  1237. } else {
  1238. if (crl->idp_flags & IDP_ONLYCA)
  1239. return 0;
  1240. }
  1241. *preasons = crl->idp_reasons;
  1242. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  1243. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1244. if (crldp_check_crlissuer(dp, crl, crl_score)) {
  1245. if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
  1246. *preasons &= dp->dp_reasons;
  1247. return 1;
  1248. }
  1249. }
  1250. }
  1251. if ((!crl->idp || !crl->idp->distpoint)
  1252. && (crl_score & CRL_SCORE_ISSUER_NAME))
  1253. return 1;
  1254. return 0;
  1255. }
  1256. /*
  1257. * Retrieve CRL corresponding to current certificate. If deltas enabled try
  1258. * to find a delta CRL too
  1259. */
  1260. static int get_crl_delta(X509_STORE_CTX *ctx,
  1261. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1262. {
  1263. int ok;
  1264. X509 *issuer = NULL;
  1265. int crl_score = 0;
  1266. unsigned int reasons;
  1267. X509_CRL *crl = NULL, *dcrl = NULL;
  1268. STACK_OF(X509_CRL) *skcrl;
  1269. X509_NAME *nm = X509_get_issuer_name(x);
  1270. reasons = ctx->current_reasons;
  1271. ok = get_crl_sk(ctx, &crl, &dcrl,
  1272. &issuer, &crl_score, &reasons, ctx->crls);
  1273. if (ok)
  1274. goto done;
  1275. /* Lookup CRLs from store */
  1276. skcrl = ctx->lookup_crls(ctx, nm);
  1277. /* If no CRLs found and a near match from get_crl_sk use that */
  1278. if (!skcrl && crl)
  1279. goto done;
  1280. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1281. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1282. done:
  1283. /* If we got any kind of CRL use it and return success */
  1284. if (crl) {
  1285. ctx->current_issuer = issuer;
  1286. ctx->current_crl_score = crl_score;
  1287. ctx->current_reasons = reasons;
  1288. *pcrl = crl;
  1289. *pdcrl = dcrl;
  1290. return 1;
  1291. }
  1292. return 0;
  1293. }
  1294. /* Check CRL validity */
  1295. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1296. {
  1297. X509 *issuer = NULL;
  1298. EVP_PKEY *ikey = NULL;
  1299. int cnum = ctx->error_depth;
  1300. int chnum = sk_X509_num(ctx->chain) - 1;
  1301. /* if we have an alternative CRL issuer cert use that */
  1302. if (ctx->current_issuer)
  1303. issuer = ctx->current_issuer;
  1304. /*
  1305. * Else find CRL issuer: if not last certificate then issuer is next
  1306. * certificate in chain.
  1307. */
  1308. else if (cnum < chnum)
  1309. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1310. else {
  1311. issuer = sk_X509_value(ctx->chain, chnum);
  1312. /* If not self signed, can't check signature */
  1313. if (!ctx->check_issued(ctx, issuer, issuer) &&
  1314. !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER))
  1315. return 0;
  1316. }
  1317. if (issuer == NULL)
  1318. return 1;
  1319. /*
  1320. * Skip most tests for deltas because they have already been done
  1321. */
  1322. if (!crl->base_crl_number) {
  1323. /* Check for cRLSign bit if keyUsage present */
  1324. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1325. !(issuer->ex_kusage & KU_CRL_SIGN) &&
  1326. !verify_cb_crl(ctx, X509_V_ERR_KEYUSAGE_NO_CRL_SIGN))
  1327. return 0;
  1328. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE) &&
  1329. !verify_cb_crl(ctx, X509_V_ERR_DIFFERENT_CRL_SCOPE))
  1330. return 0;
  1331. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH) &&
  1332. check_crl_path(ctx, ctx->current_issuer) <= 0 &&
  1333. !verify_cb_crl(ctx, X509_V_ERR_CRL_PATH_VALIDATION_ERROR))
  1334. return 0;
  1335. if ((crl->idp_flags & IDP_INVALID) &&
  1336. !verify_cb_crl(ctx, X509_V_ERR_INVALID_EXTENSION))
  1337. return 0;
  1338. }
  1339. if (!(ctx->current_crl_score & CRL_SCORE_TIME) &&
  1340. !check_crl_time(ctx, crl, 1))
  1341. return 0;
  1342. /* Attempt to get issuer certificate public key */
  1343. ikey = X509_get0_pubkey(issuer);
  1344. if (!ikey &&
  1345. !verify_cb_crl(ctx, X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY))
  1346. return 0;
  1347. if (ikey) {
  1348. int rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
  1349. if (rv != X509_V_OK && !verify_cb_crl(ctx, rv))
  1350. return 0;
  1351. /* Verify CRL signature */
  1352. if (X509_CRL_verify(crl, ikey) <= 0 &&
  1353. !verify_cb_crl(ctx, X509_V_ERR_CRL_SIGNATURE_FAILURE))
  1354. return 0;
  1355. }
  1356. return 1;
  1357. }
  1358. /* Check certificate against CRL */
  1359. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1360. {
  1361. X509_REVOKED *rev;
  1362. /*
  1363. * The rules changed for this... previously if a CRL contained unhandled
  1364. * critical extensions it could still be used to indicate a certificate
  1365. * was revoked. This has since been changed since critical extensions can
  1366. * change the meaning of CRL entries.
  1367. */
  1368. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1369. && (crl->flags & EXFLAG_CRITICAL) &&
  1370. !verify_cb_crl(ctx, X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION))
  1371. return 0;
  1372. /*
  1373. * Look for serial number of certificate in CRL. If found, make sure
  1374. * reason is not removeFromCRL.
  1375. */
  1376. if (X509_CRL_get0_by_cert(crl, &rev, x)) {
  1377. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1378. return 2;
  1379. if (!verify_cb_crl(ctx, X509_V_ERR_CERT_REVOKED))
  1380. return 0;
  1381. }
  1382. return 1;
  1383. }
  1384. static int check_policy(X509_STORE_CTX *ctx)
  1385. {
  1386. int ret;
  1387. if (ctx->parent)
  1388. return 1;
  1389. /*
  1390. * With DANE, the trust anchor might be a bare public key, not a
  1391. * certificate! In that case our chain does not have the trust anchor
  1392. * certificate as a top-most element. This comports well with RFC5280
  1393. * chain verification, since there too, the trust anchor is not part of the
  1394. * chain to be verified. In particular, X509_policy_check() does not look
  1395. * at the TA cert, but assumes that it is present as the top-most chain
  1396. * element. We therefore temporarily push a NULL cert onto the chain if it
  1397. * was verified via a bare public key, and pop it off right after the
  1398. * X509_policy_check() call.
  1399. */
  1400. if (ctx->bare_ta_signed && !sk_X509_push(ctx->chain, NULL)) {
  1401. X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
  1402. ctx->error = X509_V_ERR_OUT_OF_MEM;
  1403. return 0;
  1404. }
  1405. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1406. ctx->param->policies, ctx->param->flags);
  1407. if (ctx->bare_ta_signed)
  1408. sk_X509_pop(ctx->chain);
  1409. if (ret == X509_PCY_TREE_INTERNAL) {
  1410. X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
  1411. ctx->error = X509_V_ERR_OUT_OF_MEM;
  1412. return 0;
  1413. }
  1414. /* Invalid or inconsistent extensions */
  1415. if (ret == X509_PCY_TREE_INVALID) {
  1416. int i;
  1417. /* Locate certificates with bad extensions and notify callback. */
  1418. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  1419. X509 *x = sk_X509_value(ctx->chain, i);
  1420. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1421. continue;
  1422. if (!verify_cb_cert(ctx, x, i,
  1423. X509_V_ERR_INVALID_POLICY_EXTENSION))
  1424. return 0;
  1425. }
  1426. return 1;
  1427. }
  1428. if (ret == X509_PCY_TREE_FAILURE) {
  1429. ctx->current_cert = NULL;
  1430. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1431. return ctx->verify_cb(0, ctx);
  1432. }
  1433. if (ret != X509_PCY_TREE_VALID) {
  1434. X509err(X509_F_CHECK_POLICY, ERR_R_INTERNAL_ERROR);
  1435. return 0;
  1436. }
  1437. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  1438. ctx->current_cert = NULL;
  1439. /*
  1440. * Verification errors need to be "sticky", a callback may have allowed
  1441. * an SSL handshake to continue despite an error, and we must then
  1442. * remain in an error state. Therefore, we MUST NOT clear earlier
  1443. * verification errors by setting the error to X509_V_OK.
  1444. */
  1445. if (!ctx->verify_cb(2, ctx))
  1446. return 0;
  1447. }
  1448. return 1;
  1449. }
  1450. /*-
  1451. * Check certificate validity times.
  1452. * If depth >= 0, invoke verification callbacks on error, otherwise just return
  1453. * the validation status.
  1454. *
  1455. * Return 1 on success, 0 otherwise.
  1456. */
  1457. int x509_check_cert_time(X509_STORE_CTX *ctx, X509 *x, int depth)
  1458. {
  1459. time_t *ptime;
  1460. int i;
  1461. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1462. ptime = &ctx->param->check_time;
  1463. else if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME)
  1464. return 1;
  1465. else
  1466. ptime = NULL;
  1467. i = X509_cmp_time(X509_get0_notBefore(x), ptime);
  1468. if (i >= 0 && depth < 0)
  1469. return 0;
  1470. if (i == 0 && !verify_cb_cert(ctx, x, depth,
  1471. X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD))
  1472. return 0;
  1473. if (i > 0 && !verify_cb_cert(ctx, x, depth, X509_V_ERR_CERT_NOT_YET_VALID))
  1474. return 0;
  1475. i = X509_cmp_time(X509_get0_notAfter(x), ptime);
  1476. if (i <= 0 && depth < 0)
  1477. return 0;
  1478. if (i == 0 && !verify_cb_cert(ctx, x, depth,
  1479. X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD))
  1480. return 0;
  1481. if (i < 0 && !verify_cb_cert(ctx, x, depth, X509_V_ERR_CERT_HAS_EXPIRED))
  1482. return 0;
  1483. return 1;
  1484. }
  1485. static int internal_verify(X509_STORE_CTX *ctx)
  1486. {
  1487. int n = sk_X509_num(ctx->chain) - 1;
  1488. X509 *xi = sk_X509_value(ctx->chain, n);
  1489. X509 *xs;
  1490. /*
  1491. * With DANE-verified bare public key TA signatures, it remains only to
  1492. * check the timestamps of the top certificate. We report the issuer as
  1493. * NULL, since all we have is a bare key.
  1494. */
  1495. if (ctx->bare_ta_signed) {
  1496. xs = xi;
  1497. xi = NULL;
  1498. goto check_cert;
  1499. }
  1500. if (ctx->check_issued(ctx, xi, xi))
  1501. xs = xi;
  1502. else {
  1503. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  1504. xs = xi;
  1505. goto check_cert;
  1506. }
  1507. if (n <= 0)
  1508. return verify_cb_cert(ctx, xi, 0,
  1509. X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE);
  1510. n--;
  1511. ctx->error_depth = n;
  1512. xs = sk_X509_value(ctx->chain, n);
  1513. }
  1514. /*
  1515. * Do not clear ctx->error=0, it must be "sticky", only the user's callback
  1516. * is allowed to reset errors (at its own peril).
  1517. */
  1518. while (n >= 0) {
  1519. EVP_PKEY *pkey;
  1520. /*
  1521. * Skip signature check for self signed certificates unless explicitly
  1522. * asked for. It doesn't add any security and just wastes time. If
  1523. * the issuer's public key is unusable, report the issuer certificate
  1524. * and its depth (rather than the depth of the subject).
  1525. */
  1526. if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
  1527. if ((pkey = X509_get0_pubkey(xi)) == NULL) {
  1528. if (!verify_cb_cert(ctx, xi, xi != xs ? n+1 : n,
  1529. X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY))
  1530. return 0;
  1531. } else if (X509_verify(xs, pkey) <= 0) {
  1532. if (!verify_cb_cert(ctx, xs, n,
  1533. X509_V_ERR_CERT_SIGNATURE_FAILURE))
  1534. return 0;
  1535. }
  1536. }
  1537. check_cert:
  1538. /* Calls verify callback as needed */
  1539. if (!x509_check_cert_time(ctx, xs, n))
  1540. return 0;
  1541. /*
  1542. * Signal success at this depth. However, the previous error (if any)
  1543. * is retained.
  1544. */
  1545. ctx->current_issuer = xi;
  1546. ctx->current_cert = xs;
  1547. ctx->error_depth = n;
  1548. if (!ctx->verify_cb(1, ctx))
  1549. return 0;
  1550. if (--n >= 0) {
  1551. xi = xs;
  1552. xs = sk_X509_value(ctx->chain, n);
  1553. }
  1554. }
  1555. return 1;
  1556. }
  1557. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1558. {
  1559. return X509_cmp_time(ctm, NULL);
  1560. }
  1561. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1562. {
  1563. static const size_t utctime_length = sizeof("YYMMDDHHMMSSZ") - 1;
  1564. static const size_t generalizedtime_length = sizeof("YYYYMMDDHHMMSSZ") - 1;
  1565. ASN1_TIME *asn1_cmp_time = NULL;
  1566. int i, day, sec, ret = 0;
  1567. /*
  1568. * Note that ASN.1 allows much more slack in the time format than RFC5280.
  1569. * In RFC5280, the representation is fixed:
  1570. * UTCTime: YYMMDDHHMMSSZ
  1571. * GeneralizedTime: YYYYMMDDHHMMSSZ
  1572. *
  1573. * We do NOT currently enforce the following RFC 5280 requirement:
  1574. * "CAs conforming to this profile MUST always encode certificate
  1575. * validity dates through the year 2049 as UTCTime; certificate validity
  1576. * dates in 2050 or later MUST be encoded as GeneralizedTime."
  1577. */
  1578. switch (ctm->type) {
  1579. case V_ASN1_UTCTIME:
  1580. if (ctm->length != (int)(utctime_length))
  1581. return 0;
  1582. break;
  1583. case V_ASN1_GENERALIZEDTIME:
  1584. if (ctm->length != (int)(generalizedtime_length))
  1585. return 0;
  1586. break;
  1587. default:
  1588. return 0;
  1589. }
  1590. /**
  1591. * Verify the format: the ASN.1 functions we use below allow a more
  1592. * flexible format than what's mandated by RFC 5280.
  1593. * Digit and date ranges will be verified in the conversion methods.
  1594. */
  1595. for (i = 0; i < ctm->length - 1; i++) {
  1596. if (!ossl_isdigit(ctm->data[i]))
  1597. return 0;
  1598. }
  1599. if (ctm->data[ctm->length - 1] != 'Z')
  1600. return 0;
  1601. /*
  1602. * There is ASN1_UTCTIME_cmp_time_t but no
  1603. * ASN1_GENERALIZEDTIME_cmp_time_t or ASN1_TIME_cmp_time_t,
  1604. * so we go through ASN.1
  1605. */
  1606. asn1_cmp_time = X509_time_adj(NULL, 0, cmp_time);
  1607. if (asn1_cmp_time == NULL)
  1608. goto err;
  1609. if (!ASN1_TIME_diff(&day, &sec, ctm, asn1_cmp_time))
  1610. goto err;
  1611. /*
  1612. * X509_cmp_time comparison is <=.
  1613. * The return value 0 is reserved for errors.
  1614. */
  1615. ret = (day >= 0 && sec >= 0) ? -1 : 1;
  1616. err:
  1617. ASN1_TIME_free(asn1_cmp_time);
  1618. return ret;
  1619. }
  1620. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1621. {
  1622. return X509_time_adj(s, adj, NULL);
  1623. }
  1624. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1625. {
  1626. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1627. }
  1628. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1629. int offset_day, long offset_sec, time_t *in_tm)
  1630. {
  1631. time_t t;
  1632. if (in_tm)
  1633. t = *in_tm;
  1634. else
  1635. time(&t);
  1636. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
  1637. if (s->type == V_ASN1_UTCTIME)
  1638. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  1639. if (s->type == V_ASN1_GENERALIZEDTIME)
  1640. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  1641. }
  1642. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1643. }
  1644. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  1645. {
  1646. EVP_PKEY *ktmp = NULL, *ktmp2;
  1647. int i, j;
  1648. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
  1649. return 1;
  1650. for (i = 0; i < sk_X509_num(chain); i++) {
  1651. ktmp = X509_get0_pubkey(sk_X509_value(chain, i));
  1652. if (ktmp == NULL) {
  1653. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1654. X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1655. return 0;
  1656. }
  1657. if (!EVP_PKEY_missing_parameters(ktmp))
  1658. break;
  1659. }
  1660. if (ktmp == NULL) {
  1661. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1662. X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1663. return 0;
  1664. }
  1665. /* first, populate the other certs */
  1666. for (j = i - 1; j >= 0; j--) {
  1667. ktmp2 = X509_get0_pubkey(sk_X509_value(chain, j));
  1668. EVP_PKEY_copy_parameters(ktmp2, ktmp);
  1669. }
  1670. if (pkey != NULL)
  1671. EVP_PKEY_copy_parameters(pkey, ktmp);
  1672. return 1;
  1673. }
  1674. /* Make a delta CRL as the diff between two full CRLs */
  1675. X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
  1676. EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
  1677. {
  1678. X509_CRL *crl = NULL;
  1679. int i;
  1680. STACK_OF(X509_REVOKED) *revs = NULL;
  1681. /* CRLs can't be delta already */
  1682. if (base->base_crl_number || newer->base_crl_number) {
  1683. X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_ALREADY_DELTA);
  1684. return NULL;
  1685. }
  1686. /* Base and new CRL must have a CRL number */
  1687. if (!base->crl_number || !newer->crl_number) {
  1688. X509err(X509_F_X509_CRL_DIFF, X509_R_NO_CRL_NUMBER);
  1689. return NULL;
  1690. }
  1691. /* Issuer names must match */
  1692. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
  1693. X509err(X509_F_X509_CRL_DIFF, X509_R_ISSUER_MISMATCH);
  1694. return NULL;
  1695. }
  1696. /* AKID and IDP must match */
  1697. if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
  1698. X509err(X509_F_X509_CRL_DIFF, X509_R_AKID_MISMATCH);
  1699. return NULL;
  1700. }
  1701. if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
  1702. X509err(X509_F_X509_CRL_DIFF, X509_R_IDP_MISMATCH);
  1703. return NULL;
  1704. }
  1705. /* Newer CRL number must exceed full CRL number */
  1706. if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
  1707. X509err(X509_F_X509_CRL_DIFF, X509_R_NEWER_CRL_NOT_NEWER);
  1708. return NULL;
  1709. }
  1710. /* CRLs must verify */
  1711. if (skey && (X509_CRL_verify(base, skey) <= 0 ||
  1712. X509_CRL_verify(newer, skey) <= 0)) {
  1713. X509err(X509_F_X509_CRL_DIFF, X509_R_CRL_VERIFY_FAILURE);
  1714. return NULL;
  1715. }
  1716. /* Create new CRL */
  1717. crl = X509_CRL_new();
  1718. if (crl == NULL || !X509_CRL_set_version(crl, 1))
  1719. goto memerr;
  1720. /* Set issuer name */
  1721. if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
  1722. goto memerr;
  1723. if (!X509_CRL_set1_lastUpdate(crl, X509_CRL_get0_lastUpdate(newer)))
  1724. goto memerr;
  1725. if (!X509_CRL_set1_nextUpdate(crl, X509_CRL_get0_nextUpdate(newer)))
  1726. goto memerr;
  1727. /* Set base CRL number: must be critical */
  1728. if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
  1729. goto memerr;
  1730. /*
  1731. * Copy extensions across from newest CRL to delta: this will set CRL
  1732. * number to correct value too.
  1733. */
  1734. for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
  1735. X509_EXTENSION *ext;
  1736. ext = X509_CRL_get_ext(newer, i);
  1737. if (!X509_CRL_add_ext(crl, ext, -1))
  1738. goto memerr;
  1739. }
  1740. /* Go through revoked entries, copying as needed */
  1741. revs = X509_CRL_get_REVOKED(newer);
  1742. for (i = 0; i < sk_X509_REVOKED_num(revs); i++) {
  1743. X509_REVOKED *rvn, *rvtmp;
  1744. rvn = sk_X509_REVOKED_value(revs, i);
  1745. /*
  1746. * Add only if not also in base. TODO: need something cleverer here
  1747. * for some more complex CRLs covering multiple CAs.
  1748. */
  1749. if (!X509_CRL_get0_by_serial(base, &rvtmp, &rvn->serialNumber)) {
  1750. rvtmp = X509_REVOKED_dup(rvn);
  1751. if (!rvtmp)
  1752. goto memerr;
  1753. if (!X509_CRL_add0_revoked(crl, rvtmp)) {
  1754. X509_REVOKED_free(rvtmp);
  1755. goto memerr;
  1756. }
  1757. }
  1758. }
  1759. /* TODO: optionally prune deleted entries */
  1760. if (skey && md && !X509_CRL_sign(crl, skey, md))
  1761. goto memerr;
  1762. return crl;
  1763. memerr:
  1764. X509err(X509_F_X509_CRL_DIFF, ERR_R_MALLOC_FAILURE);
  1765. X509_CRL_free(crl);
  1766. return NULL;
  1767. }
  1768. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1769. {
  1770. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1771. }
  1772. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1773. {
  1774. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1775. }
  1776. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1777. {
  1778. return ctx->error;
  1779. }
  1780. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1781. {
  1782. ctx->error = err;
  1783. }
  1784. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1785. {
  1786. return ctx->error_depth;
  1787. }
  1788. void X509_STORE_CTX_set_error_depth(X509_STORE_CTX *ctx, int depth)
  1789. {
  1790. ctx->error_depth = depth;
  1791. }
  1792. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1793. {
  1794. return ctx->current_cert;
  1795. }
  1796. void X509_STORE_CTX_set_current_cert(X509_STORE_CTX *ctx, X509 *x)
  1797. {
  1798. ctx->current_cert = x;
  1799. }
  1800. STACK_OF(X509) *X509_STORE_CTX_get0_chain(X509_STORE_CTX *ctx)
  1801. {
  1802. return ctx->chain;
  1803. }
  1804. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1805. {
  1806. if (!ctx->chain)
  1807. return NULL;
  1808. return X509_chain_up_ref(ctx->chain);
  1809. }
  1810. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1811. {
  1812. return ctx->current_issuer;
  1813. }
  1814. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1815. {
  1816. return ctx->current_crl;
  1817. }
  1818. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1819. {
  1820. return ctx->parent;
  1821. }
  1822. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1823. {
  1824. ctx->cert = x;
  1825. }
  1826. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1827. {
  1828. ctx->crls = sk;
  1829. }
  1830. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1831. {
  1832. /*
  1833. * XXX: Why isn't this function always used to set the associated trust?
  1834. * Should there even be a VPM->trust field at all? Or should the trust
  1835. * always be inferred from the purpose by X509_STORE_CTX_init().
  1836. */
  1837. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1838. }
  1839. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1840. {
  1841. /*
  1842. * XXX: See above, this function would only be needed when the default
  1843. * trust for the purpose needs an override in a corner case.
  1844. */
  1845. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1846. }
  1847. /*
  1848. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1849. * This is intended to be used when another structure has its own trust and
  1850. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1851. * set then we will usually have a default purpose in mind which should then
  1852. * be used to set the trust value. An example of this is SSL use: an SSL
  1853. * structure will have its own purpose and trust settings which the
  1854. * application can set: if they aren't set then we use the default of SSL
  1855. * client/server.
  1856. */
  1857. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1858. int purpose, int trust)
  1859. {
  1860. int idx;
  1861. /* If purpose not set use default */
  1862. if (!purpose)
  1863. purpose = def_purpose;
  1864. /* If we have a purpose then check it is valid */
  1865. if (purpose) {
  1866. X509_PURPOSE *ptmp;
  1867. idx = X509_PURPOSE_get_by_id(purpose);
  1868. if (idx == -1) {
  1869. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1870. X509_R_UNKNOWN_PURPOSE_ID);
  1871. return 0;
  1872. }
  1873. ptmp = X509_PURPOSE_get0(idx);
  1874. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1875. idx = X509_PURPOSE_get_by_id(def_purpose);
  1876. /*
  1877. * XXX: In the two callers above def_purpose is always 0, which is
  1878. * not a known value, so idx will always be -1. How is the
  1879. * X509_TRUST_DEFAULT case actually supposed to be handled?
  1880. */
  1881. if (idx == -1) {
  1882. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1883. X509_R_UNKNOWN_PURPOSE_ID);
  1884. return 0;
  1885. }
  1886. ptmp = X509_PURPOSE_get0(idx);
  1887. }
  1888. /* If trust not set then get from purpose default */
  1889. if (!trust)
  1890. trust = ptmp->trust;
  1891. }
  1892. if (trust) {
  1893. idx = X509_TRUST_get_by_id(trust);
  1894. if (idx == -1) {
  1895. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1896. X509_R_UNKNOWN_TRUST_ID);
  1897. return 0;
  1898. }
  1899. }
  1900. if (purpose && !ctx->param->purpose)
  1901. ctx->param->purpose = purpose;
  1902. if (trust && !ctx->param->trust)
  1903. ctx->param->trust = trust;
  1904. return 1;
  1905. }
  1906. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1907. {
  1908. X509_STORE_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  1909. if (ctx == NULL) {
  1910. X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
  1911. return NULL;
  1912. }
  1913. return ctx;
  1914. }
  1915. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1916. {
  1917. if (ctx == NULL)
  1918. return;
  1919. X509_STORE_CTX_cleanup(ctx);
  1920. OPENSSL_free(ctx);
  1921. }
  1922. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1923. STACK_OF(X509) *chain)
  1924. {
  1925. int ret = 1;
  1926. ctx->ctx = store;
  1927. ctx->cert = x509;
  1928. ctx->untrusted = chain;
  1929. ctx->crls = NULL;
  1930. ctx->num_untrusted = 0;
  1931. ctx->other_ctx = NULL;
  1932. ctx->valid = 0;
  1933. ctx->chain = NULL;
  1934. ctx->error = 0;
  1935. ctx->explicit_policy = 0;
  1936. ctx->error_depth = 0;
  1937. ctx->current_cert = NULL;
  1938. ctx->current_issuer = NULL;
  1939. ctx->current_crl = NULL;
  1940. ctx->current_crl_score = 0;
  1941. ctx->current_reasons = 0;
  1942. ctx->tree = NULL;
  1943. ctx->parent = NULL;
  1944. ctx->dane = NULL;
  1945. ctx->bare_ta_signed = 0;
  1946. /* Zero ex_data to make sure we're cleanup-safe */
  1947. memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
  1948. /* store->cleanup is always 0 in OpenSSL, if set must be idempotent */
  1949. if (store)
  1950. ctx->cleanup = store->cleanup;
  1951. else
  1952. ctx->cleanup = 0;
  1953. if (store && store->check_issued)
  1954. ctx->check_issued = store->check_issued;
  1955. else
  1956. ctx->check_issued = check_issued;
  1957. if (store && store->get_issuer)
  1958. ctx->get_issuer = store->get_issuer;
  1959. else
  1960. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1961. if (store && store->verify_cb)
  1962. ctx->verify_cb = store->verify_cb;
  1963. else
  1964. ctx->verify_cb = null_callback;
  1965. if (store && store->verify)
  1966. ctx->verify = store->verify;
  1967. else
  1968. ctx->verify = internal_verify;
  1969. if (store && store->check_revocation)
  1970. ctx->check_revocation = store->check_revocation;
  1971. else
  1972. ctx->check_revocation = check_revocation;
  1973. if (store && store->get_crl)
  1974. ctx->get_crl = store->get_crl;
  1975. else
  1976. ctx->get_crl = NULL;
  1977. if (store && store->check_crl)
  1978. ctx->check_crl = store->check_crl;
  1979. else
  1980. ctx->check_crl = check_crl;
  1981. if (store && store->cert_crl)
  1982. ctx->cert_crl = store->cert_crl;
  1983. else
  1984. ctx->cert_crl = cert_crl;
  1985. if (store && store->check_policy)
  1986. ctx->check_policy = store->check_policy;
  1987. else
  1988. ctx->check_policy = check_policy;
  1989. if (store && store->lookup_certs)
  1990. ctx->lookup_certs = store->lookup_certs;
  1991. else
  1992. ctx->lookup_certs = X509_STORE_CTX_get1_certs;
  1993. if (store && store->lookup_crls)
  1994. ctx->lookup_crls = store->lookup_crls;
  1995. else
  1996. ctx->lookup_crls = X509_STORE_CTX_get1_crls;
  1997. ctx->param = X509_VERIFY_PARAM_new();
  1998. if (ctx->param == NULL) {
  1999. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2000. goto err;
  2001. }
  2002. /*
  2003. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  2004. */
  2005. if (store)
  2006. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  2007. else
  2008. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  2009. if (ret)
  2010. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  2011. X509_VERIFY_PARAM_lookup("default"));
  2012. if (ret == 0) {
  2013. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2014. goto err;
  2015. }
  2016. /*
  2017. * XXX: For now, continue to inherit trust from VPM, but infer from the
  2018. * purpose if this still yields the default value.
  2019. */
  2020. if (ctx->param->trust == X509_TRUST_DEFAULT) {
  2021. int idx = X509_PURPOSE_get_by_id(ctx->param->purpose);
  2022. X509_PURPOSE *xp = X509_PURPOSE_get0(idx);
  2023. if (xp != NULL)
  2024. ctx->param->trust = X509_PURPOSE_get_trust(xp);
  2025. }
  2026. if (CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  2027. &ctx->ex_data))
  2028. return 1;
  2029. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  2030. err:
  2031. /*
  2032. * On error clean up allocated storage, if the store context was not
  2033. * allocated with X509_STORE_CTX_new() this is our last chance to do so.
  2034. */
  2035. X509_STORE_CTX_cleanup(ctx);
  2036. return 0;
  2037. }
  2038. /*
  2039. * Set alternative lookup method: just a STACK of trusted certificates. This
  2040. * avoids X509_STORE nastiness where it isn't needed.
  2041. */
  2042. void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2043. {
  2044. ctx->other_ctx = sk;
  2045. ctx->get_issuer = get_issuer_sk;
  2046. ctx->lookup_certs = lookup_certs_sk;
  2047. }
  2048. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  2049. {
  2050. /*
  2051. * We need to be idempotent because, unfortunately, free() also calls
  2052. * cleanup(), so the natural call sequence new(), init(), cleanup(), free()
  2053. * calls cleanup() for the same object twice! Thus we must zero the
  2054. * pointers below after they're freed!
  2055. */
  2056. /* Seems to always be 0 in OpenSSL, do this at most once. */
  2057. if (ctx->cleanup != NULL) {
  2058. ctx->cleanup(ctx);
  2059. ctx->cleanup = NULL;
  2060. }
  2061. if (ctx->param != NULL) {
  2062. if (ctx->parent == NULL)
  2063. X509_VERIFY_PARAM_free(ctx->param);
  2064. ctx->param = NULL;
  2065. }
  2066. X509_policy_tree_free(ctx->tree);
  2067. ctx->tree = NULL;
  2068. sk_X509_pop_free(ctx->chain, X509_free);
  2069. ctx->chain = NULL;
  2070. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  2071. memset(&ctx->ex_data, 0, sizeof(ctx->ex_data));
  2072. }
  2073. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  2074. {
  2075. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2076. }
  2077. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  2078. {
  2079. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  2080. }
  2081. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  2082. time_t t)
  2083. {
  2084. X509_VERIFY_PARAM_set_time(ctx->param, t);
  2085. }
  2086. X509 *X509_STORE_CTX_get0_cert(X509_STORE_CTX *ctx)
  2087. {
  2088. return ctx->cert;
  2089. }
  2090. STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
  2091. {
  2092. return ctx->untrusted;
  2093. }
  2094. void X509_STORE_CTX_set0_untrusted(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2095. {
  2096. ctx->untrusted = sk;
  2097. }
  2098. void X509_STORE_CTX_set0_verified_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2099. {
  2100. sk_X509_pop_free(ctx->chain, X509_free);
  2101. ctx->chain = sk;
  2102. }
  2103. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  2104. X509_STORE_CTX_verify_cb verify_cb)
  2105. {
  2106. ctx->verify_cb = verify_cb;
  2107. }
  2108. X509_STORE_CTX_verify_cb X509_STORE_CTX_get_verify_cb(X509_STORE_CTX *ctx)
  2109. {
  2110. return ctx->verify_cb;
  2111. }
  2112. void X509_STORE_CTX_set_verify(X509_STORE_CTX *ctx,
  2113. X509_STORE_CTX_verify_fn verify)
  2114. {
  2115. ctx->verify = verify;
  2116. }
  2117. X509_STORE_CTX_verify_fn X509_STORE_CTX_get_verify(X509_STORE_CTX *ctx)
  2118. {
  2119. return ctx->verify;
  2120. }
  2121. X509_STORE_CTX_get_issuer_fn X509_STORE_CTX_get_get_issuer(X509_STORE_CTX *ctx)
  2122. {
  2123. return ctx->get_issuer;
  2124. }
  2125. X509_STORE_CTX_check_issued_fn X509_STORE_CTX_get_check_issued(X509_STORE_CTX *ctx)
  2126. {
  2127. return ctx->check_issued;
  2128. }
  2129. X509_STORE_CTX_check_revocation_fn X509_STORE_CTX_get_check_revocation(X509_STORE_CTX *ctx)
  2130. {
  2131. return ctx->check_revocation;
  2132. }
  2133. X509_STORE_CTX_get_crl_fn X509_STORE_CTX_get_get_crl(X509_STORE_CTX *ctx)
  2134. {
  2135. return ctx->get_crl;
  2136. }
  2137. X509_STORE_CTX_check_crl_fn X509_STORE_CTX_get_check_crl(X509_STORE_CTX *ctx)
  2138. {
  2139. return ctx->check_crl;
  2140. }
  2141. X509_STORE_CTX_cert_crl_fn X509_STORE_CTX_get_cert_crl(X509_STORE_CTX *ctx)
  2142. {
  2143. return ctx->cert_crl;
  2144. }
  2145. X509_STORE_CTX_check_policy_fn X509_STORE_CTX_get_check_policy(X509_STORE_CTX *ctx)
  2146. {
  2147. return ctx->check_policy;
  2148. }
  2149. X509_STORE_CTX_lookup_certs_fn X509_STORE_CTX_get_lookup_certs(X509_STORE_CTX *ctx)
  2150. {
  2151. return ctx->lookup_certs;
  2152. }
  2153. X509_STORE_CTX_lookup_crls_fn X509_STORE_CTX_get_lookup_crls(X509_STORE_CTX *ctx)
  2154. {
  2155. return ctx->lookup_crls;
  2156. }
  2157. X509_STORE_CTX_cleanup_fn X509_STORE_CTX_get_cleanup(X509_STORE_CTX *ctx)
  2158. {
  2159. return ctx->cleanup;
  2160. }
  2161. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  2162. {
  2163. return ctx->tree;
  2164. }
  2165. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  2166. {
  2167. return ctx->explicit_policy;
  2168. }
  2169. int X509_STORE_CTX_get_num_untrusted(X509_STORE_CTX *ctx)
  2170. {
  2171. return ctx->num_untrusted;
  2172. }
  2173. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  2174. {
  2175. const X509_VERIFY_PARAM *param;
  2176. param = X509_VERIFY_PARAM_lookup(name);
  2177. if (!param)
  2178. return 0;
  2179. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  2180. }
  2181. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  2182. {
  2183. return ctx->param;
  2184. }
  2185. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  2186. {
  2187. X509_VERIFY_PARAM_free(ctx->param);
  2188. ctx->param = param;
  2189. }
  2190. void X509_STORE_CTX_set0_dane(X509_STORE_CTX *ctx, SSL_DANE *dane)
  2191. {
  2192. ctx->dane = dane;
  2193. }
  2194. static unsigned char *dane_i2d(
  2195. X509 *cert,
  2196. uint8_t selector,
  2197. unsigned int *i2dlen)
  2198. {
  2199. unsigned char *buf = NULL;
  2200. int len;
  2201. /*
  2202. * Extract ASN.1 DER form of certificate or public key.
  2203. */
  2204. switch (selector) {
  2205. case DANETLS_SELECTOR_CERT:
  2206. len = i2d_X509(cert, &buf);
  2207. break;
  2208. case DANETLS_SELECTOR_SPKI:
  2209. len = i2d_X509_PUBKEY(X509_get_X509_PUBKEY(cert), &buf);
  2210. break;
  2211. default:
  2212. X509err(X509_F_DANE_I2D, X509_R_BAD_SELECTOR);
  2213. return NULL;
  2214. }
  2215. if (len < 0 || buf == NULL) {
  2216. X509err(X509_F_DANE_I2D, ERR_R_MALLOC_FAILURE);
  2217. return NULL;
  2218. }
  2219. *i2dlen = (unsigned int)len;
  2220. return buf;
  2221. }
  2222. #define DANETLS_NONE 256 /* impossible uint8_t */
  2223. static int dane_match(X509_STORE_CTX *ctx, X509 *cert, int depth)
  2224. {
  2225. SSL_DANE *dane = ctx->dane;
  2226. unsigned usage = DANETLS_NONE;
  2227. unsigned selector = DANETLS_NONE;
  2228. unsigned ordinal = DANETLS_NONE;
  2229. unsigned mtype = DANETLS_NONE;
  2230. unsigned char *i2dbuf = NULL;
  2231. unsigned int i2dlen = 0;
  2232. unsigned char mdbuf[EVP_MAX_MD_SIZE];
  2233. unsigned char *cmpbuf = NULL;
  2234. unsigned int cmplen = 0;
  2235. int i;
  2236. int recnum;
  2237. int matched = 0;
  2238. danetls_record *t = NULL;
  2239. uint32_t mask;
  2240. mask = (depth == 0) ? DANETLS_EE_MASK : DANETLS_TA_MASK;
  2241. /*
  2242. * The trust store is not applicable with DANE-TA(2)
  2243. */
  2244. if (depth >= ctx->num_untrusted)
  2245. mask &= DANETLS_PKIX_MASK;
  2246. /*
  2247. * If we've previously matched a PKIX-?? record, no need to test any
  2248. * further PKIX-?? records, it remains to just build the PKIX chain.
  2249. * Had the match been a DANE-?? record, we'd be done already.
  2250. */
  2251. if (dane->mdpth >= 0)
  2252. mask &= ~DANETLS_PKIX_MASK;
  2253. /*-
  2254. * https://tools.ietf.org/html/rfc7671#section-5.1
  2255. * https://tools.ietf.org/html/rfc7671#section-5.2
  2256. * https://tools.ietf.org/html/rfc7671#section-5.3
  2257. * https://tools.ietf.org/html/rfc7671#section-5.4
  2258. *
  2259. * We handle DANE-EE(3) records first as they require no chain building
  2260. * and no expiration or hostname checks. We also process digests with
  2261. * higher ordinals first and ignore lower priorities except Full(0) which
  2262. * is always processed (last). If none match, we then process PKIX-EE(1).
  2263. *
  2264. * NOTE: This relies on DANE usages sorting before the corresponding PKIX
  2265. * usages in SSL_dane_tlsa_add(), and also on descending sorting of digest
  2266. * priorities. See twin comment in ssl/ssl_lib.c.
  2267. *
  2268. * We expect that most TLSA RRsets will have just a single usage, so we
  2269. * don't go out of our way to cache multiple selector-specific i2d buffers
  2270. * across usages, but if the selector happens to remain the same as switch
  2271. * usages, that's OK. Thus, a set of "3 1 1", "3 0 1", "1 1 1", "1 0 1",
  2272. * records would result in us generating each of the certificate and public
  2273. * key DER forms twice, but more typically we'd just see multiple "3 1 1"
  2274. * or multiple "3 0 1" records.
  2275. *
  2276. * As soon as we find a match at any given depth, we stop, because either
  2277. * we've matched a DANE-?? record and the peer is authenticated, or, after
  2278. * exhausting all DANE-?? records, we've matched a PKIX-?? record, which is
  2279. * sufficient for DANE, and what remains to do is ordinary PKIX validation.
  2280. */
  2281. recnum = (dane->umask & mask) ? sk_danetls_record_num(dane->trecs) : 0;
  2282. for (i = 0; matched == 0 && i < recnum; ++i) {
  2283. t = sk_danetls_record_value(dane->trecs, i);
  2284. if ((DANETLS_USAGE_BIT(t->usage) & mask) == 0)
  2285. continue;
  2286. if (t->usage != usage) {
  2287. usage = t->usage;
  2288. /* Reset digest agility for each usage/selector pair */
  2289. mtype = DANETLS_NONE;
  2290. ordinal = dane->dctx->mdord[t->mtype];
  2291. }
  2292. if (t->selector != selector) {
  2293. selector = t->selector;
  2294. /* Update per-selector state */
  2295. OPENSSL_free(i2dbuf);
  2296. i2dbuf = dane_i2d(cert, selector, &i2dlen);
  2297. if (i2dbuf == NULL)
  2298. return -1;
  2299. /* Reset digest agility for each usage/selector pair */
  2300. mtype = DANETLS_NONE;
  2301. ordinal = dane->dctx->mdord[t->mtype];
  2302. } else if (t->mtype != DANETLS_MATCHING_FULL) {
  2303. /*-
  2304. * Digest agility:
  2305. *
  2306. * <https://tools.ietf.org/html/rfc7671#section-9>
  2307. *
  2308. * For a fixed selector, after processing all records with the
  2309. * highest mtype ordinal, ignore all mtypes with lower ordinals
  2310. * other than "Full".
  2311. */
  2312. if (dane->dctx->mdord[t->mtype] < ordinal)
  2313. continue;
  2314. }
  2315. /*
  2316. * Each time we hit a (new selector or) mtype, re-compute the relevant
  2317. * digest, more complex caching is not worth the code space.
  2318. */
  2319. if (t->mtype != mtype) {
  2320. const EVP_MD *md = dane->dctx->mdevp[mtype = t->mtype];
  2321. cmpbuf = i2dbuf;
  2322. cmplen = i2dlen;
  2323. if (md != NULL) {
  2324. cmpbuf = mdbuf;
  2325. if (!EVP_Digest(i2dbuf, i2dlen, cmpbuf, &cmplen, md, 0)) {
  2326. matched = -1;
  2327. break;
  2328. }
  2329. }
  2330. }
  2331. /*
  2332. * Squirrel away the certificate and depth if we have a match. Any
  2333. * DANE match is dispositive, but with PKIX we still need to build a
  2334. * full chain.
  2335. */
  2336. if (cmplen == t->dlen &&
  2337. memcmp(cmpbuf, t->data, cmplen) == 0) {
  2338. if (DANETLS_USAGE_BIT(usage) & DANETLS_DANE_MASK)
  2339. matched = 1;
  2340. if (matched || dane->mdpth < 0) {
  2341. dane->mdpth = depth;
  2342. dane->mtlsa = t;
  2343. OPENSSL_free(dane->mcert);
  2344. dane->mcert = cert;
  2345. X509_up_ref(cert);
  2346. }
  2347. break;
  2348. }
  2349. }
  2350. /* Clear the one-element DER cache */
  2351. OPENSSL_free(i2dbuf);
  2352. return matched;
  2353. }
  2354. static int check_dane_issuer(X509_STORE_CTX *ctx, int depth)
  2355. {
  2356. SSL_DANE *dane = ctx->dane;
  2357. int matched = 0;
  2358. X509 *cert;
  2359. if (!DANETLS_HAS_TA(dane) || depth == 0)
  2360. return X509_TRUST_UNTRUSTED;
  2361. /*
  2362. * Record any DANE trust-anchor matches, for the first depth to test, if
  2363. * there's one at that depth. (This'll be false for length 1 chains looking
  2364. * for an exact match for the leaf certificate).
  2365. */
  2366. cert = sk_X509_value(ctx->chain, depth);
  2367. if (cert != NULL && (matched = dane_match(ctx, cert, depth)) < 0)
  2368. return X509_TRUST_REJECTED;
  2369. if (matched > 0) {
  2370. ctx->num_untrusted = depth - 1;
  2371. return X509_TRUST_TRUSTED;
  2372. }
  2373. return X509_TRUST_UNTRUSTED;
  2374. }
  2375. static int check_dane_pkeys(X509_STORE_CTX *ctx)
  2376. {
  2377. SSL_DANE *dane = ctx->dane;
  2378. danetls_record *t;
  2379. int num = ctx->num_untrusted;
  2380. X509 *cert = sk_X509_value(ctx->chain, num - 1);
  2381. int recnum = sk_danetls_record_num(dane->trecs);
  2382. int i;
  2383. for (i = 0; i < recnum; ++i) {
  2384. t = sk_danetls_record_value(dane->trecs, i);
  2385. if (t->usage != DANETLS_USAGE_DANE_TA ||
  2386. t->selector != DANETLS_SELECTOR_SPKI ||
  2387. t->mtype != DANETLS_MATCHING_FULL ||
  2388. X509_verify(cert, t->spki) <= 0)
  2389. continue;
  2390. /* Clear any PKIX-?? matches that failed to extend to a full chain */
  2391. X509_free(dane->mcert);
  2392. dane->mcert = NULL;
  2393. /* Record match via a bare TA public key */
  2394. ctx->bare_ta_signed = 1;
  2395. dane->mdpth = num - 1;
  2396. dane->mtlsa = t;
  2397. /* Prune any excess chain certificates */
  2398. num = sk_X509_num(ctx->chain);
  2399. for (; num > ctx->num_untrusted; --num)
  2400. X509_free(sk_X509_pop(ctx->chain));
  2401. return X509_TRUST_TRUSTED;
  2402. }
  2403. return X509_TRUST_UNTRUSTED;
  2404. }
  2405. static void dane_reset(SSL_DANE *dane)
  2406. {
  2407. /*
  2408. * Reset state to verify another chain, or clear after failure.
  2409. */
  2410. X509_free(dane->mcert);
  2411. dane->mcert = NULL;
  2412. dane->mtlsa = NULL;
  2413. dane->mdpth = -1;
  2414. dane->pdpth = -1;
  2415. }
  2416. static int check_leaf_suiteb(X509_STORE_CTX *ctx, X509 *cert)
  2417. {
  2418. int err = X509_chain_check_suiteb(NULL, cert, NULL, ctx->param->flags);
  2419. if (err == X509_V_OK)
  2420. return 1;
  2421. return verify_cb_cert(ctx, cert, 0, err);
  2422. }
  2423. static int dane_verify(X509_STORE_CTX *ctx)
  2424. {
  2425. X509 *cert = ctx->cert;
  2426. SSL_DANE *dane = ctx->dane;
  2427. int matched;
  2428. int done;
  2429. dane_reset(dane);
  2430. /*-
  2431. * When testing the leaf certificate, if we match a DANE-EE(3) record,
  2432. * dane_match() returns 1 and we're done. If however we match a PKIX-EE(1)
  2433. * record, the match depth and matching TLSA record are recorded, but the
  2434. * return value is 0, because we still need to find a PKIX trust-anchor.
  2435. * Therefore, when DANE authentication is enabled (required), we're done
  2436. * if:
  2437. * + matched < 0, internal error.
  2438. * + matched == 1, we matched a DANE-EE(3) record
  2439. * + matched == 0, mdepth < 0 (no PKIX-EE match) and there are no
  2440. * DANE-TA(2) or PKIX-TA(0) to test.
  2441. */
  2442. matched = dane_match(ctx, ctx->cert, 0);
  2443. done = matched != 0 || (!DANETLS_HAS_TA(dane) && dane->mdpth < 0);
  2444. if (done)
  2445. X509_get_pubkey_parameters(NULL, ctx->chain);
  2446. if (matched > 0) {
  2447. /* Callback invoked as needed */
  2448. if (!check_leaf_suiteb(ctx, cert))
  2449. return 0;
  2450. /* Callback invoked as needed */
  2451. if ((dane->flags & DANE_FLAG_NO_DANE_EE_NAMECHECKS) == 0 &&
  2452. !check_id(ctx))
  2453. return 0;
  2454. /* Bypass internal_verify(), issue depth 0 success callback */
  2455. ctx->error_depth = 0;
  2456. ctx->current_cert = cert;
  2457. return ctx->verify_cb(1, ctx);
  2458. }
  2459. if (matched < 0) {
  2460. ctx->error_depth = 0;
  2461. ctx->current_cert = cert;
  2462. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2463. return -1;
  2464. }
  2465. if (done) {
  2466. /* Fail early, TA-based success is not possible */
  2467. if (!check_leaf_suiteb(ctx, cert))
  2468. return 0;
  2469. return verify_cb_cert(ctx, cert, 0, X509_V_ERR_DANE_NO_MATCH);
  2470. }
  2471. /*
  2472. * Chain verification for usages 0/1/2. TLSA record matching of depth > 0
  2473. * certificates happens in-line with building the rest of the chain.
  2474. */
  2475. return verify_chain(ctx);
  2476. }
  2477. /* Get issuer, without duplicate suppression */
  2478. static int get_issuer(X509 **issuer, X509_STORE_CTX *ctx, X509 *cert)
  2479. {
  2480. STACK_OF(X509) *saved_chain = ctx->chain;
  2481. int ok;
  2482. ctx->chain = NULL;
  2483. ok = ctx->get_issuer(issuer, ctx, cert);
  2484. ctx->chain = saved_chain;
  2485. return ok;
  2486. }
  2487. static int build_chain(X509_STORE_CTX *ctx)
  2488. {
  2489. SSL_DANE *dane = ctx->dane;
  2490. int num = sk_X509_num(ctx->chain);
  2491. X509 *cert = sk_X509_value(ctx->chain, num - 1);
  2492. int ss = cert_self_signed(cert);
  2493. STACK_OF(X509) *sktmp = NULL;
  2494. unsigned int search;
  2495. int may_trusted = 0;
  2496. int may_alternate = 0;
  2497. int trust = X509_TRUST_UNTRUSTED;
  2498. int alt_untrusted = 0;
  2499. int depth;
  2500. int ok = 0;
  2501. int i;
  2502. /* Our chain starts with a single untrusted element. */
  2503. if (!ossl_assert(num == 1 && ctx->num_untrusted == num)) {
  2504. X509err(X509_F_BUILD_CHAIN, ERR_R_INTERNAL_ERROR);
  2505. ctx->error = X509_V_ERR_UNSPECIFIED;
  2506. return 0;
  2507. }
  2508. #define S_DOUNTRUSTED (1 << 0) /* Search untrusted chain */
  2509. #define S_DOTRUSTED (1 << 1) /* Search trusted store */
  2510. #define S_DOALTERNATE (1 << 2) /* Retry with pruned alternate chain */
  2511. /*
  2512. * Set up search policy, untrusted if possible, trusted-first if enabled.
  2513. * If we're doing DANE and not doing PKIX-TA/PKIX-EE, we never look in the
  2514. * trust_store, otherwise we might look there first. If not trusted-first,
  2515. * and alternate chains are not disabled, try building an alternate chain
  2516. * if no luck with untrusted first.
  2517. */
  2518. search = (ctx->untrusted != NULL) ? S_DOUNTRUSTED : 0;
  2519. if (DANETLS_HAS_PKIX(dane) || !DANETLS_HAS_DANE(dane)) {
  2520. if (search == 0 || ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
  2521. search |= S_DOTRUSTED;
  2522. else if (!(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS))
  2523. may_alternate = 1;
  2524. may_trusted = 1;
  2525. }
  2526. /*
  2527. * Shallow-copy the stack of untrusted certificates (with TLS, this is
  2528. * typically the content of the peer's certificate message) so can make
  2529. * multiple passes over it, while free to remove elements as we go.
  2530. */
  2531. if (ctx->untrusted && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
  2532. X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
  2533. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2534. return 0;
  2535. }
  2536. /*
  2537. * If we got any "DANE-TA(2) Cert(0) Full(0)" trust-anchors from DNS, add
  2538. * them to our working copy of the untrusted certificate stack. Since the
  2539. * caller of X509_STORE_CTX_init() may have provided only a leaf cert with
  2540. * no corresponding stack of untrusted certificates, we may need to create
  2541. * an empty stack first. [ At present only the ssl library provides DANE
  2542. * support, and ssl_verify_cert_chain() always provides a non-null stack
  2543. * containing at least the leaf certificate, but we must be prepared for
  2544. * this to change. ]
  2545. */
  2546. if (DANETLS_ENABLED(dane) && dane->certs != NULL) {
  2547. if (sktmp == NULL && (sktmp = sk_X509_new_null()) == NULL) {
  2548. X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
  2549. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2550. return 0;
  2551. }
  2552. for (i = 0; i < sk_X509_num(dane->certs); ++i) {
  2553. if (!sk_X509_push(sktmp, sk_X509_value(dane->certs, i))) {
  2554. sk_X509_free(sktmp);
  2555. X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
  2556. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2557. return 0;
  2558. }
  2559. }
  2560. }
  2561. /*
  2562. * Still absurdly large, but arithmetically safe, a lower hard upper bound
  2563. * might be reasonable.
  2564. */
  2565. if (ctx->param->depth > INT_MAX/2)
  2566. ctx->param->depth = INT_MAX/2;
  2567. /*
  2568. * Try to Extend the chain until we reach an ultimately trusted issuer.
  2569. * Build chains up to one longer the limit, later fail if we hit the limit,
  2570. * with an X509_V_ERR_CERT_CHAIN_TOO_LONG error code.
  2571. */
  2572. depth = ctx->param->depth + 1;
  2573. while (search != 0) {
  2574. X509 *x;
  2575. X509 *xtmp = NULL;
  2576. /*
  2577. * Look in the trust store if enabled for first lookup, or we've run
  2578. * out of untrusted issuers and search here is not disabled. When we
  2579. * reach the depth limit, we stop extending the chain, if by that point
  2580. * we've not found a trust-anchor, any trusted chain would be too long.
  2581. *
  2582. * The error reported to the application verify callback is at the
  2583. * maximal valid depth with the current certificate equal to the last
  2584. * not ultimately-trusted issuer. For example, with verify_depth = 0,
  2585. * the callback will report errors at depth=1 when the immediate issuer
  2586. * of the leaf certificate is not a trust anchor. No attempt will be
  2587. * made to locate an issuer for that certificate, since such a chain
  2588. * would be a-priori too long.
  2589. */
  2590. if ((search & S_DOTRUSTED) != 0) {
  2591. i = num = sk_X509_num(ctx->chain);
  2592. if ((search & S_DOALTERNATE) != 0) {
  2593. /*
  2594. * As high up the chain as we can, look for an alternative
  2595. * trusted issuer of an untrusted certificate that currently
  2596. * has an untrusted issuer. We use the alt_untrusted variable
  2597. * to track how far up the chain we find the first match. It
  2598. * is only if and when we find a match, that we prune the chain
  2599. * and reset ctx->num_untrusted to the reduced count of
  2600. * untrusted certificates. While we're searching for such a
  2601. * match (which may never be found), it is neither safe nor
  2602. * wise to preemptively modify either the chain or
  2603. * ctx->num_untrusted.
  2604. *
  2605. * Note, like ctx->num_untrusted, alt_untrusted is a count of
  2606. * untrusted certificates, not a "depth".
  2607. */
  2608. i = alt_untrusted;
  2609. }
  2610. x = sk_X509_value(ctx->chain, i-1);
  2611. ok = (depth < num) ? 0 : get_issuer(&xtmp, ctx, x);
  2612. if (ok < 0) {
  2613. trust = X509_TRUST_REJECTED;
  2614. ctx->error = X509_V_ERR_STORE_LOOKUP;
  2615. search = 0;
  2616. continue;
  2617. }
  2618. if (ok > 0) {
  2619. /*
  2620. * Alternative trusted issuer for a mid-chain untrusted cert?
  2621. * Pop the untrusted cert's successors and retry. We might now
  2622. * be able to complete a valid chain via the trust store. Note
  2623. * that despite the current trust-store match we might still
  2624. * fail complete the chain to a suitable trust-anchor, in which
  2625. * case we may prune some more untrusted certificates and try
  2626. * again. Thus the S_DOALTERNATE bit may yet be turned on
  2627. * again with an even shorter untrusted chain!
  2628. *
  2629. * If in the process we threw away our matching PKIX-TA trust
  2630. * anchor, reset DANE trust. We might find a suitable trusted
  2631. * certificate among the ones from the trust store.
  2632. */
  2633. if ((search & S_DOALTERNATE) != 0) {
  2634. if (!ossl_assert(num > i && i > 0 && ss == 0)) {
  2635. X509err(X509_F_BUILD_CHAIN, ERR_R_INTERNAL_ERROR);
  2636. X509_free(xtmp);
  2637. trust = X509_TRUST_REJECTED;
  2638. ctx->error = X509_V_ERR_UNSPECIFIED;
  2639. search = 0;
  2640. continue;
  2641. }
  2642. search &= ~S_DOALTERNATE;
  2643. for (; num > i; --num)
  2644. X509_free(sk_X509_pop(ctx->chain));
  2645. ctx->num_untrusted = num;
  2646. if (DANETLS_ENABLED(dane) &&
  2647. dane->mdpth >= ctx->num_untrusted) {
  2648. dane->mdpth = -1;
  2649. X509_free(dane->mcert);
  2650. dane->mcert = NULL;
  2651. }
  2652. if (DANETLS_ENABLED(dane) &&
  2653. dane->pdpth >= ctx->num_untrusted)
  2654. dane->pdpth = -1;
  2655. }
  2656. /*
  2657. * Self-signed untrusted certificates get replaced by their
  2658. * trusted matching issuer. Otherwise, grow the chain.
  2659. */
  2660. if (ss == 0) {
  2661. if (!sk_X509_push(ctx->chain, x = xtmp)) {
  2662. X509_free(xtmp);
  2663. X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
  2664. trust = X509_TRUST_REJECTED;
  2665. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2666. search = 0;
  2667. continue;
  2668. }
  2669. ss = cert_self_signed(x);
  2670. } else if (num == ctx->num_untrusted) {
  2671. /*
  2672. * We have a self-signed certificate that has the same
  2673. * subject name (and perhaps keyid and/or serial number) as
  2674. * a trust-anchor. We must have an exact match to avoid
  2675. * possible impersonation via key substitution etc.
  2676. */
  2677. if (X509_cmp(x, xtmp) != 0) {
  2678. /* Self-signed untrusted mimic. */
  2679. X509_free(xtmp);
  2680. ok = 0;
  2681. } else {
  2682. X509_free(x);
  2683. ctx->num_untrusted = --num;
  2684. (void) sk_X509_set(ctx->chain, num, x = xtmp);
  2685. }
  2686. }
  2687. /*
  2688. * We've added a new trusted certificate to the chain, recheck
  2689. * trust. If not done, and not self-signed look deeper.
  2690. * Whether or not we're doing "trusted first", we no longer
  2691. * look for untrusted certificates from the peer's chain.
  2692. *
  2693. * At this point ctx->num_trusted and num must reflect the
  2694. * correct number of untrusted certificates, since the DANE
  2695. * logic in check_trust() depends on distinguishing CAs from
  2696. * "the wire" from CAs from the trust store. In particular, the
  2697. * certificate at depth "num" should be the new trusted
  2698. * certificate with ctx->num_untrusted <= num.
  2699. */
  2700. if (ok) {
  2701. if (!ossl_assert(ctx->num_untrusted <= num)) {
  2702. X509err(X509_F_BUILD_CHAIN, ERR_R_INTERNAL_ERROR);
  2703. trust = X509_TRUST_REJECTED;
  2704. ctx->error = X509_V_ERR_UNSPECIFIED;
  2705. search = 0;
  2706. continue;
  2707. }
  2708. search &= ~S_DOUNTRUSTED;
  2709. switch (trust = check_trust(ctx, num)) {
  2710. case X509_TRUST_TRUSTED:
  2711. case X509_TRUST_REJECTED:
  2712. search = 0;
  2713. continue;
  2714. }
  2715. if (ss == 0)
  2716. continue;
  2717. }
  2718. }
  2719. /*
  2720. * No dispositive decision, and either self-signed or no match, if
  2721. * we were doing untrusted-first, and alt-chains are not disabled,
  2722. * do that, by repeatedly losing one untrusted element at a time,
  2723. * and trying to extend the shorted chain.
  2724. */
  2725. if ((search & S_DOUNTRUSTED) == 0) {
  2726. /* Continue search for a trusted issuer of a shorter chain? */
  2727. if ((search & S_DOALTERNATE) != 0 && --alt_untrusted > 0)
  2728. continue;
  2729. /* Still no luck and no fallbacks left? */
  2730. if (!may_alternate || (search & S_DOALTERNATE) != 0 ||
  2731. ctx->num_untrusted < 2)
  2732. break;
  2733. /* Search for a trusted issuer of a shorter chain */
  2734. search |= S_DOALTERNATE;
  2735. alt_untrusted = ctx->num_untrusted - 1;
  2736. ss = 0;
  2737. }
  2738. }
  2739. /*
  2740. * Extend chain with peer-provided certificates
  2741. */
  2742. if ((search & S_DOUNTRUSTED) != 0) {
  2743. num = sk_X509_num(ctx->chain);
  2744. if (!ossl_assert(num == ctx->num_untrusted)) {
  2745. X509err(X509_F_BUILD_CHAIN, ERR_R_INTERNAL_ERROR);
  2746. trust = X509_TRUST_REJECTED;
  2747. ctx->error = X509_V_ERR_UNSPECIFIED;
  2748. search = 0;
  2749. continue;
  2750. }
  2751. x = sk_X509_value(ctx->chain, num-1);
  2752. /*
  2753. * Once we run out of untrusted issuers, we stop looking for more
  2754. * and start looking only in the trust store if enabled.
  2755. */
  2756. xtmp = (ss || depth < num) ? NULL : find_issuer(ctx, sktmp, x);
  2757. if (xtmp == NULL) {
  2758. search &= ~S_DOUNTRUSTED;
  2759. if (may_trusted)
  2760. search |= S_DOTRUSTED;
  2761. continue;
  2762. }
  2763. /* Drop this issuer from future consideration */
  2764. (void) sk_X509_delete_ptr(sktmp, xtmp);
  2765. if (!sk_X509_push(ctx->chain, xtmp)) {
  2766. X509err(X509_F_BUILD_CHAIN, ERR_R_MALLOC_FAILURE);
  2767. trust = X509_TRUST_REJECTED;
  2768. ctx->error = X509_V_ERR_OUT_OF_MEM;
  2769. search = 0;
  2770. continue;
  2771. }
  2772. X509_up_ref(x = xtmp);
  2773. ++ctx->num_untrusted;
  2774. ss = cert_self_signed(xtmp);
  2775. /*
  2776. * Check for DANE-TA trust of the topmost untrusted certificate.
  2777. */
  2778. switch (trust = check_dane_issuer(ctx, ctx->num_untrusted - 1)) {
  2779. case X509_TRUST_TRUSTED:
  2780. case X509_TRUST_REJECTED:
  2781. search = 0;
  2782. continue;
  2783. }
  2784. }
  2785. }
  2786. sk_X509_free(sktmp);
  2787. /*
  2788. * Last chance to make a trusted chain, either bare DANE-TA public-key
  2789. * signers, or else direct leaf PKIX trust.
  2790. */
  2791. num = sk_X509_num(ctx->chain);
  2792. if (num <= depth) {
  2793. if (trust == X509_TRUST_UNTRUSTED && DANETLS_HAS_DANE_TA(dane))
  2794. trust = check_dane_pkeys(ctx);
  2795. if (trust == X509_TRUST_UNTRUSTED && num == ctx->num_untrusted)
  2796. trust = check_trust(ctx, num);
  2797. }
  2798. switch (trust) {
  2799. case X509_TRUST_TRUSTED:
  2800. return 1;
  2801. case X509_TRUST_REJECTED:
  2802. /* Callback already issued */
  2803. return 0;
  2804. case X509_TRUST_UNTRUSTED:
  2805. default:
  2806. num = sk_X509_num(ctx->chain);
  2807. if (num > depth)
  2808. return verify_cb_cert(ctx, NULL, num-1,
  2809. X509_V_ERR_CERT_CHAIN_TOO_LONG);
  2810. if (DANETLS_ENABLED(dane) &&
  2811. (!DANETLS_HAS_PKIX(dane) || dane->pdpth >= 0))
  2812. return verify_cb_cert(ctx, NULL, num-1, X509_V_ERR_DANE_NO_MATCH);
  2813. if (ss && sk_X509_num(ctx->chain) == 1)
  2814. return verify_cb_cert(ctx, NULL, num-1,
  2815. X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT);
  2816. if (ss)
  2817. return verify_cb_cert(ctx, NULL, num-1,
  2818. X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN);
  2819. if (ctx->num_untrusted < num)
  2820. return verify_cb_cert(ctx, NULL, num-1,
  2821. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT);
  2822. return verify_cb_cert(ctx, NULL, num-1,
  2823. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY);
  2824. }
  2825. }
  2826. static const int minbits_table[] = { 80, 112, 128, 192, 256 };
  2827. static const int NUM_AUTH_LEVELS = OSSL_NELEM(minbits_table);
  2828. /*
  2829. * Check whether the public key of ``cert`` meets the security level of
  2830. * ``ctx``.
  2831. *
  2832. * Returns 1 on success, 0 otherwise.
  2833. */
  2834. static int check_key_level(X509_STORE_CTX *ctx, X509 *cert)
  2835. {
  2836. EVP_PKEY *pkey = X509_get0_pubkey(cert);
  2837. int level = ctx->param->auth_level;
  2838. /*
  2839. * At security level zero, return without checking for a supported public
  2840. * key type. Some engines support key types not understood outside the
  2841. * engine, and we only need to understand the key when enforcing a security
  2842. * floor.
  2843. */
  2844. if (level <= 0)
  2845. return 1;
  2846. /* Unsupported or malformed keys are not secure */
  2847. if (pkey == NULL)
  2848. return 0;
  2849. if (level > NUM_AUTH_LEVELS)
  2850. level = NUM_AUTH_LEVELS;
  2851. return EVP_PKEY_security_bits(pkey) >= minbits_table[level - 1];
  2852. }
  2853. /*
  2854. * Check whether the signature digest algorithm of ``cert`` meets the security
  2855. * level of ``ctx``. Should not be checked for trust anchors (whether
  2856. * self-signed or otherwise).
  2857. *
  2858. * Returns 1 on success, 0 otherwise.
  2859. */
  2860. static int check_sig_level(X509_STORE_CTX *ctx, X509 *cert)
  2861. {
  2862. int secbits = -1;
  2863. int level = ctx->param->auth_level;
  2864. if (level <= 0)
  2865. return 1;
  2866. if (level > NUM_AUTH_LEVELS)
  2867. level = NUM_AUTH_LEVELS;
  2868. if (!X509_get_signature_info(cert, NULL, NULL, &secbits, NULL))
  2869. return 0;
  2870. return secbits >= minbits_table[level - 1];
  2871. }