x509_vfy.c 106 KB

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