x509_vfy.c 101 KB

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