x509_vfy.c 102 KB

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