x509_vfy.c 119 KB

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