x509_vfy.c 106 KB

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