x509_vfy.c 106 KB

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