x509_vfy.c 113 KB

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