x509_vfy.c 101 KB

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