x509_vfy.c 106 KB

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