x509_vfy.c 106 KB

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