x509_vfy.c 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <string.h>
  57. #include <time.h>
  58. #include <openssl/asn1.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/err.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/obj.h>
  64. #include <openssl/thread.h>
  65. #include <openssl/x509.h>
  66. #include <openssl/x509v3.h>
  67. #include "vpm_int.h"
  68. #include "../internal.h"
  69. static CRYPTO_EX_DATA_CLASS g_ex_data_class =
  70. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  71. /* CRL score values */
  72. /* No unhandled critical extensions */
  73. #define CRL_SCORE_NOCRITICAL 0x100
  74. /* certificate is within CRL scope */
  75. #define CRL_SCORE_SCOPE 0x080
  76. /* CRL times valid */
  77. #define CRL_SCORE_TIME 0x040
  78. /* Issuer name matches certificate */
  79. #define CRL_SCORE_ISSUER_NAME 0x020
  80. /* If this score or above CRL is probably valid */
  81. #define CRL_SCORE_VALID (CRL_SCORE_NOCRITICAL|CRL_SCORE_TIME|CRL_SCORE_SCOPE)
  82. /* CRL issuer is certificate issuer */
  83. #define CRL_SCORE_ISSUER_CERT 0x018
  84. /* CRL issuer is on certificate path */
  85. #define CRL_SCORE_SAME_PATH 0x008
  86. /* CRL issuer matches CRL AKID */
  87. #define CRL_SCORE_AKID 0x004
  88. /* Have a delta CRL with valid times */
  89. #define CRL_SCORE_TIME_DELTA 0x002
  90. static int null_callback(int ok, X509_STORE_CTX *e);
  91. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  92. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  93. static int check_chain_extensions(X509_STORE_CTX *ctx);
  94. static int check_name_constraints(X509_STORE_CTX *ctx);
  95. static int check_id(X509_STORE_CTX *ctx);
  96. static int check_trust(X509_STORE_CTX *ctx);
  97. static int check_revocation(X509_STORE_CTX *ctx);
  98. static int check_cert(X509_STORE_CTX *ctx);
  99. static int check_policy(X509_STORE_CTX *ctx);
  100. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  101. unsigned int *preasons, X509_CRL *crl, X509 *x);
  102. static int get_crl_delta(X509_STORE_CTX *ctx,
  103. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x);
  104. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl,
  105. int *pcrl_score, X509_CRL *base,
  106. STACK_OF(X509_CRL) *crls);
  107. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer,
  108. int *pcrl_score);
  109. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  110. unsigned int *preasons);
  111. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x);
  112. static int check_crl_chain(X509_STORE_CTX *ctx,
  113. STACK_OF(X509) *cert_path,
  114. STACK_OF(X509) *crl_path);
  115. static int internal_verify(X509_STORE_CTX *ctx);
  116. static int null_callback(int ok, X509_STORE_CTX *e)
  117. {
  118. return ok;
  119. }
  120. /* Return 1 is a certificate is self signed */
  121. static int cert_self_signed(X509 *x)
  122. {
  123. X509_check_purpose(x, -1, 0);
  124. if (x->ex_flags & EXFLAG_SS)
  125. return 1;
  126. else
  127. return 0;
  128. }
  129. /* Given a certificate try and find an exact match in the store */
  130. static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x)
  131. {
  132. STACK_OF(X509) *certs;
  133. X509 *xtmp = NULL;
  134. size_t i;
  135. /* Lookup all certs with matching subject name */
  136. certs = ctx->lookup_certs(ctx, X509_get_subject_name(x));
  137. if (certs == NULL)
  138. return NULL;
  139. /* Look for exact match */
  140. for (i = 0; i < sk_X509_num(certs); i++) {
  141. xtmp = sk_X509_value(certs, i);
  142. if (!X509_cmp(xtmp, x))
  143. break;
  144. }
  145. if (i < sk_X509_num(certs))
  146. X509_up_ref(xtmp);
  147. else
  148. xtmp = NULL;
  149. sk_X509_pop_free(certs, X509_free);
  150. return xtmp;
  151. }
  152. int X509_verify_cert(X509_STORE_CTX *ctx)
  153. {
  154. X509 *x, *xtmp, *xtmp2, *chain_ss = NULL;
  155. int bad_chain = 0;
  156. X509_VERIFY_PARAM *param = ctx->param;
  157. int depth, i, ok = 0;
  158. int num, j, retry, trust;
  159. int (*cb) (int xok, X509_STORE_CTX *xctx);
  160. STACK_OF(X509) *sktmp = NULL;
  161. if (ctx->cert == NULL) {
  162. OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  163. ctx->error = X509_V_ERR_INVALID_CALL;
  164. return -1;
  165. }
  166. if (ctx->chain != NULL) {
  167. /*
  168. * This X509_STORE_CTX has already been used to verify a cert. We
  169. * cannot do another one.
  170. */
  171. OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  172. ctx->error = X509_V_ERR_INVALID_CALL;
  173. return -1;
  174. }
  175. cb = ctx->verify_cb;
  176. /*
  177. * first we make sure the chain we are going to build is present and that
  178. * the first entry is in place
  179. */
  180. ctx->chain = sk_X509_new_null();
  181. if (ctx->chain == NULL || !sk_X509_push(ctx->chain, ctx->cert)) {
  182. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  183. ctx->error = X509_V_ERR_OUT_OF_MEM;
  184. goto end;
  185. }
  186. X509_up_ref(ctx->cert);
  187. ctx->last_untrusted = 1;
  188. /* We use a temporary STACK so we can chop and hack at it.
  189. * sktmp = ctx->untrusted ++ ctx->ctx->additional_untrusted */
  190. if (ctx->untrusted != NULL
  191. && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
  192. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  193. ctx->error = X509_V_ERR_OUT_OF_MEM;
  194. goto end;
  195. }
  196. if (ctx->ctx->additional_untrusted != NULL) {
  197. if (sktmp == NULL) {
  198. sktmp = sk_X509_new_null();
  199. if (sktmp == NULL) {
  200. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  201. ctx->error = X509_V_ERR_OUT_OF_MEM;
  202. goto end;
  203. }
  204. }
  205. for (size_t k = 0; k < sk_X509_num(ctx->ctx->additional_untrusted);
  206. k++) {
  207. if (!sk_X509_push(sktmp,
  208. sk_X509_value(ctx->ctx->additional_untrusted,
  209. k))) {
  210. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  211. ctx->error = X509_V_ERR_OUT_OF_MEM;
  212. goto end;
  213. }
  214. }
  215. }
  216. num = sk_X509_num(ctx->chain);
  217. x = sk_X509_value(ctx->chain, num - 1);
  218. depth = param->depth;
  219. for (;;) {
  220. /* If we have enough, we break */
  221. if (depth < num)
  222. break; /* FIXME: If this happens, we should take
  223. * note of it and, if appropriate, use the
  224. * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
  225. * later. */
  226. /* If we are self signed, we break */
  227. if (cert_self_signed(x))
  228. break;
  229. /*
  230. * If asked see if we can find issuer in trusted store first
  231. */
  232. if (ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST) {
  233. ok = ctx->get_issuer(&xtmp, ctx, x);
  234. if (ok < 0) {
  235. ctx->error = X509_V_ERR_STORE_LOOKUP;
  236. goto end;
  237. }
  238. /*
  239. * If successful for now free up cert so it will be picked up
  240. * again later.
  241. */
  242. if (ok > 0) {
  243. X509_free(xtmp);
  244. break;
  245. }
  246. }
  247. /* If we were passed a cert chain, use it first */
  248. if (sktmp != NULL) {
  249. xtmp = find_issuer(ctx, sktmp, x);
  250. if (xtmp != NULL) {
  251. if (!sk_X509_push(ctx->chain, xtmp)) {
  252. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  253. ctx->error = X509_V_ERR_OUT_OF_MEM;
  254. ok = 0;
  255. goto end;
  256. }
  257. X509_up_ref(xtmp);
  258. (void)sk_X509_delete_ptr(sktmp, xtmp);
  259. ctx->last_untrusted++;
  260. x = xtmp;
  261. num++;
  262. /*
  263. * reparse the full chain for the next one
  264. */
  265. continue;
  266. }
  267. }
  268. break;
  269. }
  270. /* Remember how many untrusted certs we have */
  271. j = num;
  272. /*
  273. * at this point, chain should contain a list of untrusted certificates.
  274. * We now need to add at least one trusted one, if possible, otherwise we
  275. * complain.
  276. */
  277. do {
  278. /*
  279. * Examine last certificate in chain and see if it is self signed.
  280. */
  281. i = sk_X509_num(ctx->chain);
  282. x = sk_X509_value(ctx->chain, i - 1);
  283. if (cert_self_signed(x)) {
  284. /* we have a self signed certificate */
  285. if (sk_X509_num(ctx->chain) == 1) {
  286. /*
  287. * We have a single self signed certificate: see if we can
  288. * find it in the store. We must have an exact match to avoid
  289. * possible impersonation.
  290. */
  291. ok = ctx->get_issuer(&xtmp, ctx, x);
  292. if ((ok <= 0) || X509_cmp(x, xtmp)) {
  293. ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  294. ctx->current_cert = x;
  295. ctx->error_depth = i - 1;
  296. if (ok == 1)
  297. X509_free(xtmp);
  298. bad_chain = 1;
  299. ok = cb(0, ctx);
  300. if (!ok)
  301. goto end;
  302. } else {
  303. /*
  304. * We have a match: replace certificate with store
  305. * version so we get any trust settings.
  306. */
  307. X509_free(x);
  308. x = xtmp;
  309. (void)sk_X509_set(ctx->chain, i - 1, x);
  310. ctx->last_untrusted = 0;
  311. }
  312. } else {
  313. /*
  314. * extract and save self signed certificate for later use
  315. */
  316. chain_ss = sk_X509_pop(ctx->chain);
  317. ctx->last_untrusted--;
  318. num--;
  319. j--;
  320. x = sk_X509_value(ctx->chain, num - 1);
  321. }
  322. }
  323. /* We now lookup certs from the certificate store */
  324. for (;;) {
  325. /* If we have enough, we break */
  326. if (depth < num)
  327. break;
  328. /* If we are self signed, we break */
  329. if (cert_self_signed(x))
  330. break;
  331. ok = ctx->get_issuer(&xtmp, ctx, x);
  332. if (ok < 0) {
  333. ctx->error = X509_V_ERR_STORE_LOOKUP;
  334. goto end;
  335. }
  336. if (ok == 0)
  337. break;
  338. x = xtmp;
  339. if (!sk_X509_push(ctx->chain, x)) {
  340. X509_free(xtmp);
  341. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  342. ctx->error = X509_V_ERR_OUT_OF_MEM;
  343. ok = 0;
  344. goto end;
  345. }
  346. num++;
  347. }
  348. /* we now have our chain, lets check it... */
  349. trust = check_trust(ctx);
  350. /* If explicitly rejected error */
  351. if (trust == X509_TRUST_REJECTED) {
  352. ok = 0;
  353. goto end;
  354. }
  355. /*
  356. * If it's not explicitly trusted then check if there is an alternative
  357. * chain that could be used. We only do this if we haven't already
  358. * checked via TRUSTED_FIRST and the user hasn't switched off alternate
  359. * chain checking
  360. */
  361. retry = 0;
  362. if (trust != X509_TRUST_TRUSTED
  363. && !(ctx->param->flags & X509_V_FLAG_TRUSTED_FIRST)
  364. && !(ctx->param->flags & X509_V_FLAG_NO_ALT_CHAINS)) {
  365. while (j-- > 1) {
  366. xtmp2 = sk_X509_value(ctx->chain, j - 1);
  367. ok = ctx->get_issuer(&xtmp, ctx, xtmp2);
  368. if (ok < 0)
  369. goto end;
  370. /* Check if we found an alternate chain */
  371. if (ok > 0) {
  372. /*
  373. * Free up the found cert we'll add it again later
  374. */
  375. X509_free(xtmp);
  376. /*
  377. * Dump all the certs above this point - we've found an
  378. * alternate chain
  379. */
  380. while (num > j) {
  381. xtmp = sk_X509_pop(ctx->chain);
  382. X509_free(xtmp);
  383. num--;
  384. }
  385. ctx->last_untrusted = sk_X509_num(ctx->chain);
  386. retry = 1;
  387. break;
  388. }
  389. }
  390. }
  391. } while (retry);
  392. /*
  393. * If not explicitly trusted then indicate error unless it's a single
  394. * self signed certificate in which case we've indicated an error already
  395. * and set bad_chain == 1
  396. */
  397. if (trust != X509_TRUST_TRUSTED && !bad_chain) {
  398. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
  399. if (ctx->last_untrusted >= num)
  400. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  401. else
  402. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  403. ctx->current_cert = x;
  404. } else {
  405. sk_X509_push(ctx->chain, chain_ss);
  406. num++;
  407. ctx->last_untrusted = num;
  408. ctx->current_cert = chain_ss;
  409. ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  410. chain_ss = NULL;
  411. }
  412. ctx->error_depth = num - 1;
  413. bad_chain = 1;
  414. ok = cb(0, ctx);
  415. if (!ok)
  416. goto end;
  417. }
  418. /* We have the chain complete: now we need to check its purpose */
  419. ok = check_chain_extensions(ctx);
  420. if (!ok)
  421. goto end;
  422. ok = check_id(ctx);
  423. if (!ok)
  424. goto end;
  425. /*
  426. * Check revocation status: we do this after copying parameters because
  427. * they may be needed for CRL signature verification.
  428. */
  429. ok = ctx->check_revocation(ctx);
  430. if (!ok)
  431. goto end;
  432. int err = X509_chain_check_suiteb(&ctx->error_depth, NULL, ctx->chain,
  433. ctx->param->flags);
  434. if (err != X509_V_OK) {
  435. ctx->error = err;
  436. ctx->current_cert = sk_X509_value(ctx->chain, ctx->error_depth);
  437. ok = cb(0, ctx);
  438. if (!ok)
  439. goto end;
  440. }
  441. /* At this point, we have a chain and need to verify it */
  442. if (ctx->verify != NULL)
  443. ok = ctx->verify(ctx);
  444. else
  445. ok = internal_verify(ctx);
  446. if (!ok)
  447. goto end;
  448. /* Check name constraints */
  449. ok = check_name_constraints(ctx);
  450. if (!ok)
  451. goto end;
  452. /* If we get this far evaluate policies */
  453. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  454. ok = ctx->check_policy(ctx);
  455. end:
  456. if (sktmp != NULL)
  457. sk_X509_free(sktmp);
  458. if (chain_ss != NULL)
  459. X509_free(chain_ss);
  460. /* Safety net, error returns must set ctx->error */
  461. if (ok <= 0 && ctx->error == X509_V_OK)
  462. ctx->error = X509_V_ERR_UNSPECIFIED;
  463. return ok;
  464. }
  465. /*
  466. * Given a STACK_OF(X509) find the issuer of cert (if any)
  467. */
  468. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  469. {
  470. size_t i;
  471. X509 *issuer;
  472. for (i = 0; i < sk_X509_num(sk); i++) {
  473. issuer = sk_X509_value(sk, i);
  474. if (ctx->check_issued(ctx, x, issuer))
  475. return issuer;
  476. }
  477. return NULL;
  478. }
  479. /* Given a possible certificate and issuer check them */
  480. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  481. {
  482. int ret;
  483. ret = X509_check_issued(issuer, x);
  484. if (ret == X509_V_OK)
  485. return 1;
  486. /* If we haven't asked for issuer errors don't set ctx */
  487. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  488. return 0;
  489. ctx->error = ret;
  490. ctx->current_cert = x;
  491. ctx->current_issuer = issuer;
  492. return ctx->verify_cb(0, ctx);
  493. }
  494. /* Alternative lookup method: look from a STACK stored in other_ctx */
  495. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  496. {
  497. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  498. if (*issuer) {
  499. X509_up_ref(*issuer);
  500. return 1;
  501. } else
  502. return 0;
  503. }
  504. /*
  505. * Check a certificate chains extensions for consistency with the supplied
  506. * purpose
  507. */
  508. static int check_chain_extensions(X509_STORE_CTX *ctx)
  509. {
  510. int i, ok = 0, must_be_ca, plen = 0;
  511. X509 *x;
  512. int (*cb) (int xok, X509_STORE_CTX *xctx);
  513. int proxy_path_length = 0;
  514. int purpose;
  515. int allow_proxy_certs;
  516. cb = ctx->verify_cb;
  517. /*
  518. * must_be_ca can have 1 of 3 values: -1: we accept both CA and non-CA
  519. * certificates, to allow direct use of self-signed certificates (which
  520. * are marked as CA). 0: we only accept non-CA certificates. This is
  521. * currently not used, but the possibility is present for future
  522. * extensions. 1: we only accept CA certificates. This is currently used
  523. * for all certificates in the chain except the leaf certificate.
  524. */
  525. must_be_ca = -1;
  526. /* CRL path validation */
  527. if (ctx->parent) {
  528. allow_proxy_certs = 0;
  529. purpose = X509_PURPOSE_CRL_SIGN;
  530. } else {
  531. allow_proxy_certs =
  532. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  533. purpose = ctx->param->purpose;
  534. }
  535. /* Check all untrusted certificates */
  536. for (i = 0; i < ctx->last_untrusted; i++) {
  537. int ret;
  538. x = sk_X509_value(ctx->chain, i);
  539. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  540. && (x->ex_flags & EXFLAG_CRITICAL)) {
  541. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  542. ctx->error_depth = i;
  543. ctx->current_cert = x;
  544. ok = cb(0, ctx);
  545. if (!ok)
  546. goto end;
  547. }
  548. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  549. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  550. ctx->error_depth = i;
  551. ctx->current_cert = x;
  552. ok = cb(0, ctx);
  553. if (!ok)
  554. goto end;
  555. }
  556. ret = X509_check_ca(x);
  557. switch (must_be_ca) {
  558. case -1:
  559. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  560. && (ret != 1) && (ret != 0)) {
  561. ret = 0;
  562. ctx->error = X509_V_ERR_INVALID_CA;
  563. } else
  564. ret = 1;
  565. break;
  566. case 0:
  567. if (ret != 0) {
  568. ret = 0;
  569. ctx->error = X509_V_ERR_INVALID_NON_CA;
  570. } else
  571. ret = 1;
  572. break;
  573. default:
  574. if ((ret == 0)
  575. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  576. && (ret != 1))) {
  577. ret = 0;
  578. ctx->error = X509_V_ERR_INVALID_CA;
  579. } else
  580. ret = 1;
  581. break;
  582. }
  583. if (ret == 0) {
  584. ctx->error_depth = i;
  585. ctx->current_cert = x;
  586. ok = cb(0, ctx);
  587. if (!ok)
  588. goto end;
  589. }
  590. if (ctx->param->purpose > 0) {
  591. ret = X509_check_purpose(x, purpose, must_be_ca > 0);
  592. if ((ret == 0)
  593. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  594. && (ret != 1))) {
  595. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  596. ctx->error_depth = i;
  597. ctx->current_cert = x;
  598. ok = cb(0, ctx);
  599. if (!ok)
  600. goto end;
  601. }
  602. }
  603. /* Check pathlen if not self issued */
  604. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  605. && (x->ex_pathlen != -1)
  606. && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
  607. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  608. ctx->error_depth = i;
  609. ctx->current_cert = x;
  610. ok = cb(0, ctx);
  611. if (!ok)
  612. goto end;
  613. }
  614. /* Increment path length if not self issued */
  615. if (!(x->ex_flags & EXFLAG_SI))
  616. plen++;
  617. /*
  618. * If this certificate is a proxy certificate, the next certificate
  619. * must be another proxy certificate or a EE certificate. If not,
  620. * the next certificate must be a CA certificate.
  621. */
  622. if (x->ex_flags & EXFLAG_PROXY) {
  623. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
  624. ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  625. ctx->error_depth = i;
  626. ctx->current_cert = x;
  627. ok = cb(0, ctx);
  628. if (!ok)
  629. goto end;
  630. }
  631. proxy_path_length++;
  632. must_be_ca = 0;
  633. } else
  634. must_be_ca = 1;
  635. }
  636. ok = 1;
  637. end:
  638. return ok;
  639. }
  640. static int check_name_constraints(X509_STORE_CTX *ctx)
  641. {
  642. X509 *x;
  643. int i, j, rv;
  644. /* Check name constraints for all certificates */
  645. for (i = sk_X509_num(ctx->chain) - 1; i >= 0; i--) {
  646. x = sk_X509_value(ctx->chain, i);
  647. /* Ignore self issued certs unless last in chain */
  648. if (i && (x->ex_flags & EXFLAG_SI))
  649. continue;
  650. /*
  651. * Check against constraints for all certificates higher in chain
  652. * including trust anchor. Trust anchor not strictly speaking needed
  653. * but if it includes constraints it is to be assumed it expects them
  654. * to be obeyed.
  655. */
  656. for (j = sk_X509_num(ctx->chain) - 1; j > i; j--) {
  657. NAME_CONSTRAINTS *nc = sk_X509_value(ctx->chain, j)->nc;
  658. if (nc) {
  659. rv = NAME_CONSTRAINTS_check(x, nc);
  660. switch (rv) {
  661. case X509_V_OK:
  662. continue;
  663. case X509_V_ERR_OUT_OF_MEM:
  664. ctx->error = rv;
  665. return 0;
  666. default:
  667. ctx->error = rv;
  668. ctx->error_depth = i;
  669. ctx->current_cert = x;
  670. if (!ctx->verify_cb(0, ctx))
  671. return 0;
  672. break;
  673. }
  674. }
  675. }
  676. }
  677. return 1;
  678. }
  679. static int check_id_error(X509_STORE_CTX *ctx, int errcode)
  680. {
  681. ctx->error = errcode;
  682. ctx->current_cert = ctx->cert;
  683. ctx->error_depth = 0;
  684. return ctx->verify_cb(0, ctx);
  685. }
  686. static int check_hosts(X509 *x, X509_VERIFY_PARAM_ID *id)
  687. {
  688. size_t i;
  689. size_t n = sk_OPENSSL_STRING_num(id->hosts);
  690. char *name;
  691. if (id->peername != NULL) {
  692. OPENSSL_free(id->peername);
  693. id->peername = NULL;
  694. }
  695. for (i = 0; i < n; ++i) {
  696. name = sk_OPENSSL_STRING_value(id->hosts, i);
  697. if (X509_check_host(x, name, strlen(name), id->hostflags,
  698. &id->peername) > 0)
  699. return 1;
  700. }
  701. return n == 0;
  702. }
  703. static int check_id(X509_STORE_CTX *ctx)
  704. {
  705. X509_VERIFY_PARAM *vpm = ctx->param;
  706. X509_VERIFY_PARAM_ID *id = vpm->id;
  707. X509 *x = ctx->cert;
  708. if (id->poison) {
  709. if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL))
  710. return 0;
  711. }
  712. if (id->hosts && check_hosts(x, id) <= 0) {
  713. if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH))
  714. return 0;
  715. }
  716. if (id->email && X509_check_email(x, id->email, id->emaillen, 0) <= 0) {
  717. if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH))
  718. return 0;
  719. }
  720. if (id->ip && X509_check_ip(x, id->ip, id->iplen, 0) <= 0) {
  721. if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH))
  722. return 0;
  723. }
  724. return 1;
  725. }
  726. static int check_trust(X509_STORE_CTX *ctx)
  727. {
  728. size_t i;
  729. int ok;
  730. X509 *x = NULL;
  731. int (*cb) (int xok, X509_STORE_CTX *xctx);
  732. cb = ctx->verify_cb;
  733. /* Check all trusted certificates in chain */
  734. for (i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) {
  735. x = sk_X509_value(ctx->chain, i);
  736. ok = X509_check_trust(x, ctx->param->trust, 0);
  737. /* If explicitly trusted return trusted */
  738. if (ok == X509_TRUST_TRUSTED)
  739. return X509_TRUST_TRUSTED;
  740. /*
  741. * If explicitly rejected notify callback and reject if not
  742. * overridden.
  743. */
  744. if (ok == X509_TRUST_REJECTED) {
  745. ctx->error_depth = i;
  746. ctx->current_cert = x;
  747. ctx->error = X509_V_ERR_CERT_REJECTED;
  748. ok = cb(0, ctx);
  749. if (!ok)
  750. return X509_TRUST_REJECTED;
  751. }
  752. }
  753. /*
  754. * If we accept partial chains and have at least one trusted certificate
  755. * return success.
  756. */
  757. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  758. X509 *mx;
  759. if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain))
  760. return X509_TRUST_TRUSTED;
  761. x = sk_X509_value(ctx->chain, 0);
  762. mx = lookup_cert_match(ctx, x);
  763. if (mx) {
  764. (void)sk_X509_set(ctx->chain, 0, mx);
  765. X509_free(x);
  766. ctx->last_untrusted = 0;
  767. return X509_TRUST_TRUSTED;
  768. }
  769. }
  770. /*
  771. * If no trusted certs in chain at all return untrusted and allow
  772. * standard (no issuer cert) etc errors to be indicated.
  773. */
  774. return X509_TRUST_UNTRUSTED;
  775. }
  776. static int check_revocation(X509_STORE_CTX *ctx)
  777. {
  778. int i, last, ok;
  779. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  780. return 1;
  781. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  782. last = sk_X509_num(ctx->chain) - 1;
  783. else {
  784. /* If checking CRL paths this isn't the EE certificate */
  785. if (ctx->parent)
  786. return 1;
  787. last = 0;
  788. }
  789. for (i = 0; i <= last; i++) {
  790. ctx->error_depth = i;
  791. ok = check_cert(ctx);
  792. if (!ok)
  793. return ok;
  794. }
  795. return 1;
  796. }
  797. static int check_cert(X509_STORE_CTX *ctx)
  798. {
  799. X509_CRL *crl = NULL, *dcrl = NULL;
  800. X509 *x;
  801. int ok = 0, cnum;
  802. unsigned int last_reasons;
  803. cnum = ctx->error_depth;
  804. x = sk_X509_value(ctx->chain, cnum);
  805. ctx->current_cert = x;
  806. ctx->current_issuer = NULL;
  807. ctx->current_crl_score = 0;
  808. ctx->current_reasons = 0;
  809. while (ctx->current_reasons != CRLDP_ALL_REASONS) {
  810. last_reasons = ctx->current_reasons;
  811. /* Try to retrieve relevant CRL */
  812. if (ctx->get_crl)
  813. ok = ctx->get_crl(ctx, &crl, x);
  814. else
  815. ok = get_crl_delta(ctx, &crl, &dcrl, x);
  816. /*
  817. * If error looking up CRL, nothing we can do except notify callback
  818. */
  819. if (!ok) {
  820. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  821. ok = ctx->verify_cb(0, ctx);
  822. goto err;
  823. }
  824. ctx->current_crl = crl;
  825. ok = ctx->check_crl(ctx, crl);
  826. if (!ok)
  827. goto err;
  828. if (dcrl) {
  829. ok = ctx->check_crl(ctx, dcrl);
  830. if (!ok)
  831. goto err;
  832. ok = ctx->cert_crl(ctx, dcrl, x);
  833. if (!ok)
  834. goto err;
  835. } else
  836. ok = 1;
  837. /* Don't look in full CRL if delta reason is removefromCRL */
  838. if (ok != 2) {
  839. ok = ctx->cert_crl(ctx, crl, x);
  840. if (!ok)
  841. goto err;
  842. }
  843. X509_CRL_free(crl);
  844. X509_CRL_free(dcrl);
  845. crl = NULL;
  846. dcrl = NULL;
  847. /*
  848. * If reasons not updated we wont get anywhere by another iteration,
  849. * so exit loop.
  850. */
  851. if (last_reasons == ctx->current_reasons) {
  852. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  853. ok = ctx->verify_cb(0, ctx);
  854. goto err;
  855. }
  856. }
  857. err:
  858. X509_CRL_free(crl);
  859. X509_CRL_free(dcrl);
  860. ctx->current_crl = NULL;
  861. return ok;
  862. }
  863. /* Check CRL times against values in X509_STORE_CTX */
  864. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  865. {
  866. time_t *ptime;
  867. int i;
  868. if (notify)
  869. ctx->current_crl = crl;
  870. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  871. ptime = &ctx->param->check_time;
  872. else
  873. ptime = NULL;
  874. i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  875. if (i == 0) {
  876. if (!notify)
  877. return 0;
  878. ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  879. if (!ctx->verify_cb(0, ctx))
  880. return 0;
  881. }
  882. if (i > 0) {
  883. if (!notify)
  884. return 0;
  885. ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
  886. if (!ctx->verify_cb(0, ctx))
  887. return 0;
  888. }
  889. if (X509_CRL_get_nextUpdate(crl)) {
  890. i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  891. if (i == 0) {
  892. if (!notify)
  893. return 0;
  894. ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  895. if (!ctx->verify_cb(0, ctx))
  896. return 0;
  897. }
  898. /* Ignore expiry of base CRL is delta is valid */
  899. if ((i < 0) && !(ctx->current_crl_score & CRL_SCORE_TIME_DELTA)) {
  900. if (!notify)
  901. return 0;
  902. ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
  903. if (!ctx->verify_cb(0, ctx))
  904. return 0;
  905. }
  906. }
  907. if (notify)
  908. ctx->current_crl = NULL;
  909. return 1;
  910. }
  911. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509_CRL **pdcrl,
  912. X509 **pissuer, int *pscore, unsigned int *preasons,
  913. STACK_OF(X509_CRL) *crls)
  914. {
  915. int crl_score, best_score = *pscore;
  916. size_t i;
  917. unsigned int reasons, best_reasons = 0;
  918. X509 *x = ctx->current_cert;
  919. X509_CRL *crl, *best_crl = NULL;
  920. X509 *crl_issuer = NULL, *best_crl_issuer = NULL;
  921. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  922. crl = sk_X509_CRL_value(crls, i);
  923. reasons = *preasons;
  924. crl_score = get_crl_score(ctx, &crl_issuer, &reasons, crl, x);
  925. if (crl_score < best_score || crl_score == 0)
  926. continue;
  927. /* If current CRL is equivalent use it if it is newer */
  928. if (crl_score == best_score && best_crl != NULL) {
  929. int day, sec;
  930. if (ASN1_TIME_diff(&day, &sec, X509_CRL_get_lastUpdate(best_crl),
  931. X509_CRL_get_lastUpdate(crl)) == 0)
  932. continue;
  933. /*
  934. * ASN1_TIME_diff never returns inconsistent signs for |day|
  935. * and |sec|.
  936. */
  937. if (day <= 0 && sec <= 0)
  938. continue;
  939. }
  940. best_crl = crl;
  941. best_crl_issuer = crl_issuer;
  942. best_score = crl_score;
  943. best_reasons = reasons;
  944. }
  945. if (best_crl) {
  946. if (*pcrl)
  947. X509_CRL_free(*pcrl);
  948. *pcrl = best_crl;
  949. *pissuer = best_crl_issuer;
  950. *pscore = best_score;
  951. *preasons = best_reasons;
  952. X509_CRL_up_ref(best_crl);
  953. if (*pdcrl) {
  954. X509_CRL_free(*pdcrl);
  955. *pdcrl = NULL;
  956. }
  957. get_delta_sk(ctx, pdcrl, pscore, best_crl, crls);
  958. }
  959. if (best_score >= CRL_SCORE_VALID)
  960. return 1;
  961. return 0;
  962. }
  963. /*
  964. * Compare two CRL extensions for delta checking purposes. They should be
  965. * both present or both absent. If both present all fields must be identical.
  966. */
  967. static int crl_extension_match(X509_CRL *a, X509_CRL *b, int nid)
  968. {
  969. ASN1_OCTET_STRING *exta, *extb;
  970. int i;
  971. i = X509_CRL_get_ext_by_NID(a, nid, -1);
  972. if (i >= 0) {
  973. /* Can't have multiple occurrences */
  974. if (X509_CRL_get_ext_by_NID(a, nid, i) != -1)
  975. return 0;
  976. exta = X509_EXTENSION_get_data(X509_CRL_get_ext(a, i));
  977. } else
  978. exta = NULL;
  979. i = X509_CRL_get_ext_by_NID(b, nid, -1);
  980. if (i >= 0) {
  981. if (X509_CRL_get_ext_by_NID(b, nid, i) != -1)
  982. return 0;
  983. extb = X509_EXTENSION_get_data(X509_CRL_get_ext(b, i));
  984. } else
  985. extb = NULL;
  986. if (!exta && !extb)
  987. return 1;
  988. if (!exta || !extb)
  989. return 0;
  990. if (ASN1_OCTET_STRING_cmp(exta, extb))
  991. return 0;
  992. return 1;
  993. }
  994. /* See if a base and delta are compatible */
  995. static int check_delta_base(X509_CRL *delta, X509_CRL *base)
  996. {
  997. /* Delta CRL must be a delta */
  998. if (!delta->base_crl_number)
  999. return 0;
  1000. /* Base must have a CRL number */
  1001. if (!base->crl_number)
  1002. return 0;
  1003. /* Issuer names must match */
  1004. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(delta)))
  1005. return 0;
  1006. /* AKID and IDP must match */
  1007. if (!crl_extension_match(delta, base, NID_authority_key_identifier))
  1008. return 0;
  1009. if (!crl_extension_match(delta, base, NID_issuing_distribution_point))
  1010. return 0;
  1011. /* Delta CRL base number must not exceed Full CRL number. */
  1012. if (ASN1_INTEGER_cmp(delta->base_crl_number, base->crl_number) > 0)
  1013. return 0;
  1014. /* Delta CRL number must exceed full CRL number */
  1015. if (ASN1_INTEGER_cmp(delta->crl_number, base->crl_number) > 0)
  1016. return 1;
  1017. return 0;
  1018. }
  1019. /*
  1020. * For a given base CRL find a delta... maybe extend to delta scoring or
  1021. * retrieve a chain of deltas...
  1022. */
  1023. static void get_delta_sk(X509_STORE_CTX *ctx, X509_CRL **dcrl, int *pscore,
  1024. X509_CRL *base, STACK_OF(X509_CRL) *crls)
  1025. {
  1026. X509_CRL *delta;
  1027. size_t i;
  1028. if (!(ctx->param->flags & X509_V_FLAG_USE_DELTAS))
  1029. return;
  1030. if (!((ctx->current_cert->ex_flags | base->flags) & EXFLAG_FRESHEST))
  1031. return;
  1032. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  1033. delta = sk_X509_CRL_value(crls, i);
  1034. if (check_delta_base(delta, base)) {
  1035. if (check_crl_time(ctx, delta, 0))
  1036. *pscore |= CRL_SCORE_TIME_DELTA;
  1037. X509_CRL_up_ref(delta);
  1038. *dcrl = delta;
  1039. return;
  1040. }
  1041. }
  1042. *dcrl = NULL;
  1043. }
  1044. /*
  1045. * For a given CRL return how suitable it is for the supplied certificate
  1046. * 'x'. The return value is a mask of several criteria. If the issuer is not
  1047. * the certificate issuer this is returned in *pissuer. The reasons mask is
  1048. * also used to determine if the CRL is suitable: if no new reasons the CRL
  1049. * is rejected, otherwise reasons is updated.
  1050. */
  1051. static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer,
  1052. unsigned int *preasons, X509_CRL *crl, X509 *x)
  1053. {
  1054. int crl_score = 0;
  1055. unsigned int tmp_reasons = *preasons, crl_reasons;
  1056. /* First see if we can reject CRL straight away */
  1057. /* Invalid IDP cannot be processed */
  1058. if (crl->idp_flags & IDP_INVALID)
  1059. return 0;
  1060. /* Reason codes or indirect CRLs need extended CRL support */
  1061. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT)) {
  1062. if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS))
  1063. return 0;
  1064. } else if (crl->idp_flags & IDP_REASONS) {
  1065. /* If no new reasons reject */
  1066. if (!(crl->idp_reasons & ~tmp_reasons))
  1067. return 0;
  1068. }
  1069. /* Don't process deltas at this stage */
  1070. else if (crl->base_crl_number)
  1071. return 0;
  1072. /* If issuer name doesn't match certificate need indirect CRL */
  1073. if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) {
  1074. if (!(crl->idp_flags & IDP_INDIRECT))
  1075. return 0;
  1076. } else
  1077. crl_score |= CRL_SCORE_ISSUER_NAME;
  1078. if (!(crl->flags & EXFLAG_CRITICAL))
  1079. crl_score |= CRL_SCORE_NOCRITICAL;
  1080. /* Check expiry */
  1081. if (check_crl_time(ctx, crl, 0))
  1082. crl_score |= CRL_SCORE_TIME;
  1083. /* Check authority key ID and locate certificate issuer */
  1084. crl_akid_check(ctx, crl, pissuer, &crl_score);
  1085. /* If we can't locate certificate issuer at this point forget it */
  1086. if (!(crl_score & CRL_SCORE_AKID))
  1087. return 0;
  1088. /* Check cert for matching CRL distribution points */
  1089. if (crl_crldp_check(x, crl, crl_score, &crl_reasons)) {
  1090. /* If no new reasons reject */
  1091. if (!(crl_reasons & ~tmp_reasons))
  1092. return 0;
  1093. tmp_reasons |= crl_reasons;
  1094. crl_score |= CRL_SCORE_SCOPE;
  1095. }
  1096. *preasons = tmp_reasons;
  1097. return crl_score;
  1098. }
  1099. static void crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl,
  1100. X509 **pissuer, int *pcrl_score)
  1101. {
  1102. X509 *crl_issuer = NULL;
  1103. X509_NAME *cnm = X509_CRL_get_issuer(crl);
  1104. int cidx = ctx->error_depth;
  1105. size_t i;
  1106. if ((size_t)cidx != sk_X509_num(ctx->chain) - 1)
  1107. cidx++;
  1108. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1109. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1110. if (*pcrl_score & CRL_SCORE_ISSUER_NAME) {
  1111. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT;
  1112. *pissuer = crl_issuer;
  1113. return;
  1114. }
  1115. }
  1116. for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) {
  1117. crl_issuer = sk_X509_value(ctx->chain, cidx);
  1118. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1119. continue;
  1120. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1121. *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH;
  1122. *pissuer = crl_issuer;
  1123. return;
  1124. }
  1125. }
  1126. /* Anything else needs extended CRL support */
  1127. if (!(ctx->param->flags & X509_V_FLAG_EXTENDED_CRL_SUPPORT))
  1128. return;
  1129. /*
  1130. * Otherwise the CRL issuer is not on the path. Look for it in the set of
  1131. * untrusted certificates.
  1132. */
  1133. for (i = 0; i < sk_X509_num(ctx->untrusted); i++) {
  1134. crl_issuer = sk_X509_value(ctx->untrusted, i);
  1135. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1136. continue;
  1137. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1138. *pissuer = crl_issuer;
  1139. *pcrl_score |= CRL_SCORE_AKID;
  1140. return;
  1141. }
  1142. }
  1143. for (i = 0; i < sk_X509_num(ctx->ctx->additional_untrusted); i++) {
  1144. crl_issuer = sk_X509_value(ctx->ctx->additional_untrusted, i);
  1145. if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm))
  1146. continue;
  1147. if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) {
  1148. *pissuer = crl_issuer;
  1149. *pcrl_score |= CRL_SCORE_AKID;
  1150. return;
  1151. }
  1152. }
  1153. }
  1154. /*
  1155. * Check the path of a CRL issuer certificate. This creates a new
  1156. * X509_STORE_CTX and populates it with most of the parameters from the
  1157. * parent. This could be optimised somewhat since a lot of path checking will
  1158. * be duplicated by the parent, but this will rarely be used in practice.
  1159. */
  1160. static int check_crl_path(X509_STORE_CTX *ctx, X509 *x)
  1161. {
  1162. X509_STORE_CTX crl_ctx;
  1163. int ret;
  1164. /* Don't allow recursive CRL path validation */
  1165. if (ctx->parent)
  1166. return 0;
  1167. if (!X509_STORE_CTX_init(&crl_ctx, ctx->ctx, x, ctx->untrusted))
  1168. return -1;
  1169. crl_ctx.crls = ctx->crls;
  1170. /* Copy verify params across */
  1171. X509_STORE_CTX_set0_param(&crl_ctx, ctx->param);
  1172. crl_ctx.parent = ctx;
  1173. crl_ctx.verify_cb = ctx->verify_cb;
  1174. /* Verify CRL issuer */
  1175. ret = X509_verify_cert(&crl_ctx);
  1176. if (ret <= 0)
  1177. goto err;
  1178. /* Check chain is acceptable */
  1179. ret = check_crl_chain(ctx, ctx->chain, crl_ctx.chain);
  1180. err:
  1181. X509_STORE_CTX_cleanup(&crl_ctx);
  1182. return ret;
  1183. }
  1184. /*
  1185. * RFC3280 says nothing about the relationship between CRL path and
  1186. * certificate path, which could lead to situations where a certificate could
  1187. * be revoked or validated by a CA not authorised to do so. RFC5280 is more
  1188. * strict and states that the two paths must end in the same trust anchor,
  1189. * though some discussions remain... until this is resolved we use the
  1190. * RFC5280 version
  1191. */
  1192. static int check_crl_chain(X509_STORE_CTX *ctx,
  1193. STACK_OF(X509) *cert_path,
  1194. STACK_OF(X509) *crl_path)
  1195. {
  1196. X509 *cert_ta, *crl_ta;
  1197. cert_ta = sk_X509_value(cert_path, sk_X509_num(cert_path) - 1);
  1198. crl_ta = sk_X509_value(crl_path, sk_X509_num(crl_path) - 1);
  1199. if (!X509_cmp(cert_ta, crl_ta))
  1200. return 1;
  1201. return 0;
  1202. }
  1203. /*
  1204. * Check for match between two dist point names: three separate cases. 1.
  1205. * Both are relative names and compare X509_NAME types. 2. One full, one
  1206. * relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and
  1207. * compare two GENERAL_NAMES. 4. One is NULL: automatic match.
  1208. */
  1209. static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b)
  1210. {
  1211. X509_NAME *nm = NULL;
  1212. GENERAL_NAMES *gens = NULL;
  1213. GENERAL_NAME *gena, *genb;
  1214. size_t i, j;
  1215. if (!a || !b)
  1216. return 1;
  1217. if (a->type == 1) {
  1218. if (!a->dpname)
  1219. return 0;
  1220. /* Case 1: two X509_NAME */
  1221. if (b->type == 1) {
  1222. if (!b->dpname)
  1223. return 0;
  1224. if (!X509_NAME_cmp(a->dpname, b->dpname))
  1225. return 1;
  1226. else
  1227. return 0;
  1228. }
  1229. /* Case 2: set name and GENERAL_NAMES appropriately */
  1230. nm = a->dpname;
  1231. gens = b->name.fullname;
  1232. } else if (b->type == 1) {
  1233. if (!b->dpname)
  1234. return 0;
  1235. /* Case 2: set name and GENERAL_NAMES appropriately */
  1236. gens = a->name.fullname;
  1237. nm = b->dpname;
  1238. }
  1239. /* Handle case 2 with one GENERAL_NAMES and one X509_NAME */
  1240. if (nm) {
  1241. for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
  1242. gena = sk_GENERAL_NAME_value(gens, i);
  1243. if (gena->type != GEN_DIRNAME)
  1244. continue;
  1245. if (!X509_NAME_cmp(nm, gena->d.directoryName))
  1246. return 1;
  1247. }
  1248. return 0;
  1249. }
  1250. /* Else case 3: two GENERAL_NAMES */
  1251. for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) {
  1252. gena = sk_GENERAL_NAME_value(a->name.fullname, i);
  1253. for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) {
  1254. genb = sk_GENERAL_NAME_value(b->name.fullname, j);
  1255. if (!GENERAL_NAME_cmp(gena, genb))
  1256. return 1;
  1257. }
  1258. }
  1259. return 0;
  1260. }
  1261. static int crldp_check_crlissuer(DIST_POINT *dp, X509_CRL *crl, int crl_score)
  1262. {
  1263. size_t i;
  1264. X509_NAME *nm = X509_CRL_get_issuer(crl);
  1265. /* If no CRLissuer return is successful iff don't need a match */
  1266. if (!dp->CRLissuer)
  1267. return ! !(crl_score & CRL_SCORE_ISSUER_NAME);
  1268. for (i = 0; i < sk_GENERAL_NAME_num(dp->CRLissuer); i++) {
  1269. GENERAL_NAME *gen = sk_GENERAL_NAME_value(dp->CRLissuer, i);
  1270. if (gen->type != GEN_DIRNAME)
  1271. continue;
  1272. if (!X509_NAME_cmp(gen->d.directoryName, nm))
  1273. return 1;
  1274. }
  1275. return 0;
  1276. }
  1277. /* Check CRLDP and IDP */
  1278. static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score,
  1279. unsigned int *preasons)
  1280. {
  1281. size_t i;
  1282. if (crl->idp_flags & IDP_ONLYATTR)
  1283. return 0;
  1284. if (x->ex_flags & EXFLAG_CA) {
  1285. if (crl->idp_flags & IDP_ONLYUSER)
  1286. return 0;
  1287. } else {
  1288. if (crl->idp_flags & IDP_ONLYCA)
  1289. return 0;
  1290. }
  1291. *preasons = crl->idp_reasons;
  1292. for (i = 0; i < sk_DIST_POINT_num(x->crldp); i++) {
  1293. DIST_POINT *dp = sk_DIST_POINT_value(x->crldp, i);
  1294. if (crldp_check_crlissuer(dp, crl, crl_score)) {
  1295. if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)) {
  1296. *preasons &= dp->dp_reasons;
  1297. return 1;
  1298. }
  1299. }
  1300. }
  1301. if ((!crl->idp || !crl->idp->distpoint)
  1302. && (crl_score & CRL_SCORE_ISSUER_NAME))
  1303. return 1;
  1304. return 0;
  1305. }
  1306. /*
  1307. * Retrieve CRL corresponding to current certificate. If deltas enabled try
  1308. * to find a delta CRL too
  1309. */
  1310. static int get_crl_delta(X509_STORE_CTX *ctx,
  1311. X509_CRL **pcrl, X509_CRL **pdcrl, X509 *x)
  1312. {
  1313. int ok;
  1314. X509 *issuer = NULL;
  1315. int crl_score = 0;
  1316. unsigned int reasons;
  1317. X509_CRL *crl = NULL, *dcrl = NULL;
  1318. STACK_OF(X509_CRL) *skcrl;
  1319. X509_NAME *nm = X509_get_issuer_name(x);
  1320. reasons = ctx->current_reasons;
  1321. ok = get_crl_sk(ctx, &crl, &dcrl,
  1322. &issuer, &crl_score, &reasons, ctx->crls);
  1323. if (ok)
  1324. goto done;
  1325. /* Lookup CRLs from store */
  1326. skcrl = ctx->lookup_crls(ctx, nm);
  1327. /* If no CRLs found and a near match from get_crl_sk use that */
  1328. if (!skcrl && crl)
  1329. goto done;
  1330. get_crl_sk(ctx, &crl, &dcrl, &issuer, &crl_score, &reasons, skcrl);
  1331. sk_X509_CRL_pop_free(skcrl, X509_CRL_free);
  1332. done:
  1333. /* If we got any kind of CRL use it and return success */
  1334. if (crl) {
  1335. ctx->current_issuer = issuer;
  1336. ctx->current_crl_score = crl_score;
  1337. ctx->current_reasons = reasons;
  1338. *pcrl = crl;
  1339. *pdcrl = dcrl;
  1340. return 1;
  1341. }
  1342. return 0;
  1343. }
  1344. /* Check CRL validity */
  1345. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  1346. {
  1347. X509 *issuer = NULL;
  1348. EVP_PKEY *ikey = NULL;
  1349. int ok = 0, chnum, cnum;
  1350. cnum = ctx->error_depth;
  1351. chnum = sk_X509_num(ctx->chain) - 1;
  1352. /* if we have an alternative CRL issuer cert use that */
  1353. if (ctx->current_issuer)
  1354. issuer = ctx->current_issuer;
  1355. /*
  1356. * Else find CRL issuer: if not last certificate then issuer is next
  1357. * certificate in chain.
  1358. */
  1359. else if (cnum < chnum)
  1360. issuer = sk_X509_value(ctx->chain, cnum + 1);
  1361. else {
  1362. issuer = sk_X509_value(ctx->chain, chnum);
  1363. /* If not self signed, can't check signature */
  1364. if (!ctx->check_issued(ctx, issuer, issuer)) {
  1365. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  1366. ok = ctx->verify_cb(0, ctx);
  1367. if (!ok)
  1368. goto err;
  1369. }
  1370. }
  1371. if (issuer) {
  1372. /*
  1373. * Skip most tests for deltas because they have already been done
  1374. */
  1375. if (!crl->base_crl_number) {
  1376. /* Check for cRLSign bit if keyUsage present */
  1377. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  1378. !(issuer->ex_kusage & KU_CRL_SIGN)) {
  1379. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  1380. ok = ctx->verify_cb(0, ctx);
  1381. if (!ok)
  1382. goto err;
  1383. }
  1384. if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) {
  1385. ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE;
  1386. ok = ctx->verify_cb(0, ctx);
  1387. if (!ok)
  1388. goto err;
  1389. }
  1390. if (!(ctx->current_crl_score & CRL_SCORE_SAME_PATH)) {
  1391. if (check_crl_path(ctx, ctx->current_issuer) <= 0) {
  1392. ctx->error = X509_V_ERR_CRL_PATH_VALIDATION_ERROR;
  1393. ok = ctx->verify_cb(0, ctx);
  1394. if (!ok)
  1395. goto err;
  1396. }
  1397. }
  1398. if (crl->idp_flags & IDP_INVALID) {
  1399. ctx->error = X509_V_ERR_INVALID_EXTENSION;
  1400. ok = ctx->verify_cb(0, ctx);
  1401. if (!ok)
  1402. goto err;
  1403. }
  1404. }
  1405. if (!(ctx->current_crl_score & CRL_SCORE_TIME)) {
  1406. ok = check_crl_time(ctx, crl, 1);
  1407. if (!ok)
  1408. goto err;
  1409. }
  1410. /* Attempt to get issuer certificate public key */
  1411. ikey = X509_get_pubkey(issuer);
  1412. if (!ikey) {
  1413. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1414. ok = ctx->verify_cb(0, ctx);
  1415. if (!ok)
  1416. goto err;
  1417. } else {
  1418. int rv;
  1419. rv = X509_CRL_check_suiteb(crl, ikey, ctx->param->flags);
  1420. if (rv != X509_V_OK) {
  1421. ctx->error = rv;
  1422. ok = ctx->verify_cb(0, ctx);
  1423. if (!ok)
  1424. goto err;
  1425. }
  1426. /* Verify CRL signature */
  1427. if (X509_CRL_verify(crl, ikey) <= 0) {
  1428. ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
  1429. ok = ctx->verify_cb(0, ctx);
  1430. if (!ok)
  1431. goto err;
  1432. }
  1433. }
  1434. }
  1435. ok = 1;
  1436. err:
  1437. EVP_PKEY_free(ikey);
  1438. return ok;
  1439. }
  1440. /* Check certificate against CRL */
  1441. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  1442. {
  1443. int ok;
  1444. X509_REVOKED *rev;
  1445. /*
  1446. * The rules changed for this... previously if a CRL contained unhandled
  1447. * critical extensions it could still be used to indicate a certificate
  1448. * was revoked. This has since been changed since critical extension can
  1449. * change the meaning of CRL entries.
  1450. */
  1451. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  1452. && (crl->flags & EXFLAG_CRITICAL)) {
  1453. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  1454. ok = ctx->verify_cb(0, ctx);
  1455. if (!ok)
  1456. return 0;
  1457. }
  1458. /*
  1459. * Look for serial number of certificate in CRL If found make sure reason
  1460. * is not removeFromCRL.
  1461. */
  1462. if (X509_CRL_get0_by_cert(crl, &rev, x)) {
  1463. if (rev->reason == CRL_REASON_REMOVE_FROM_CRL)
  1464. return 2;
  1465. ctx->error = X509_V_ERR_CERT_REVOKED;
  1466. ok = ctx->verify_cb(0, ctx);
  1467. if (!ok)
  1468. return 0;
  1469. }
  1470. return 1;
  1471. }
  1472. static int check_policy(X509_STORE_CTX *ctx)
  1473. {
  1474. int ret;
  1475. if (ctx->parent)
  1476. return 1;
  1477. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  1478. ctx->param->policies, ctx->param->flags);
  1479. if (ret == 0) {
  1480. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1481. ctx->error = X509_V_ERR_OUT_OF_MEM;
  1482. return 0;
  1483. }
  1484. /* Invalid or inconsistent extensions */
  1485. if (ret == -1) {
  1486. /*
  1487. * Locate certificates with bad extensions and notify callback.
  1488. */
  1489. X509 *x;
  1490. size_t i;
  1491. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  1492. x = sk_X509_value(ctx->chain, i);
  1493. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  1494. continue;
  1495. ctx->current_cert = x;
  1496. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  1497. if (!ctx->verify_cb(0, ctx))
  1498. return 0;
  1499. }
  1500. return 1;
  1501. }
  1502. if (ret == -2) {
  1503. ctx->current_cert = NULL;
  1504. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  1505. return ctx->verify_cb(0, ctx);
  1506. }
  1507. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  1508. ctx->current_cert = NULL;
  1509. /*
  1510. * Verification errors need to be "sticky", a callback may have allowed
  1511. * an SSL handshake to continue despite an error, and we must then
  1512. * remain in an error state. Therefore, we MUST NOT clear earlier
  1513. * verification errors by setting the error to X509_V_OK.
  1514. */
  1515. if (!ctx->verify_cb(2, ctx))
  1516. return 0;
  1517. }
  1518. return 1;
  1519. }
  1520. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  1521. {
  1522. time_t *ptime;
  1523. int i;
  1524. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  1525. ptime = &ctx->param->check_time;
  1526. else
  1527. ptime = NULL;
  1528. i = X509_cmp_time(X509_get_notBefore(x), ptime);
  1529. if (i == 0) {
  1530. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  1531. ctx->current_cert = x;
  1532. if (!ctx->verify_cb(0, ctx))
  1533. return 0;
  1534. }
  1535. if (i > 0) {
  1536. ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
  1537. ctx->current_cert = x;
  1538. if (!ctx->verify_cb(0, ctx))
  1539. return 0;
  1540. }
  1541. i = X509_cmp_time(X509_get_notAfter(x), ptime);
  1542. if (i == 0) {
  1543. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  1544. ctx->current_cert = x;
  1545. if (!ctx->verify_cb(0, ctx))
  1546. return 0;
  1547. }
  1548. if (i < 0) {
  1549. ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
  1550. ctx->current_cert = x;
  1551. if (!ctx->verify_cb(0, ctx))
  1552. return 0;
  1553. }
  1554. return 1;
  1555. }
  1556. static int internal_verify(X509_STORE_CTX *ctx)
  1557. {
  1558. int ok = 0, n;
  1559. X509 *xs, *xi;
  1560. EVP_PKEY *pkey = NULL;
  1561. int (*cb) (int xok, X509_STORE_CTX *xctx);
  1562. cb = ctx->verify_cb;
  1563. n = sk_X509_num(ctx->chain);
  1564. ctx->error_depth = n - 1;
  1565. n--;
  1566. xi = sk_X509_value(ctx->chain, n);
  1567. if (ctx->check_issued(ctx, xi, xi))
  1568. xs = xi;
  1569. else {
  1570. if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) {
  1571. xs = xi;
  1572. goto check_cert;
  1573. }
  1574. if (n <= 0) {
  1575. ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  1576. ctx->current_cert = xi;
  1577. ok = cb(0, ctx);
  1578. goto end;
  1579. } else {
  1580. n--;
  1581. ctx->error_depth = n;
  1582. xs = sk_X509_value(ctx->chain, n);
  1583. }
  1584. }
  1585. /* ctx->error=0; not needed */
  1586. while (n >= 0) {
  1587. ctx->error_depth = n;
  1588. /*
  1589. * Skip signature check for self signed certificates unless
  1590. * explicitly asked for. It doesn't add any security and just wastes
  1591. * time.
  1592. */
  1593. if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) {
  1594. if ((pkey = X509_get_pubkey(xi)) == NULL) {
  1595. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  1596. ctx->current_cert = xi;
  1597. ok = (*cb) (0, ctx);
  1598. if (!ok)
  1599. goto end;
  1600. } else if (X509_verify(xs, pkey) <= 0) {
  1601. ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
  1602. ctx->current_cert = xs;
  1603. ok = (*cb) (0, ctx);
  1604. if (!ok) {
  1605. EVP_PKEY_free(pkey);
  1606. goto end;
  1607. }
  1608. }
  1609. EVP_PKEY_free(pkey);
  1610. pkey = NULL;
  1611. }
  1612. check_cert:
  1613. ok = check_cert_time(ctx, xs);
  1614. if (!ok)
  1615. goto end;
  1616. /* The last error (if any) is still in the error value */
  1617. ctx->current_issuer = xi;
  1618. ctx->current_cert = xs;
  1619. ok = (*cb) (1, ctx);
  1620. if (!ok)
  1621. goto end;
  1622. n--;
  1623. if (n >= 0) {
  1624. xi = xs;
  1625. xs = sk_X509_value(ctx->chain, n);
  1626. }
  1627. }
  1628. ok = 1;
  1629. end:
  1630. return ok;
  1631. }
  1632. int X509_cmp_current_time(const ASN1_TIME *ctm)
  1633. {
  1634. return X509_cmp_time(ctm, NULL);
  1635. }
  1636. int X509_cmp_time(const ASN1_TIME *ctm, time_t *cmp_time)
  1637. {
  1638. char *str;
  1639. ASN1_TIME atm;
  1640. long offset;
  1641. char buff1[24], buff2[24], *p;
  1642. int i, j, remaining;
  1643. p = buff1;
  1644. remaining = ctm->length;
  1645. str = (char *)ctm->data;
  1646. /*
  1647. * Note that the following (historical) code allows much more slack in
  1648. * the time format than RFC5280. In RFC5280, the representation is fixed:
  1649. * UTCTime: YYMMDDHHMMSSZ GeneralizedTime: YYYYMMDDHHMMSSZ
  1650. */
  1651. if (ctm->type == V_ASN1_UTCTIME) {
  1652. /* YYMMDDHHMM[SS]Z or YYMMDDHHMM[SS](+-)hhmm */
  1653. int min_length = sizeof("YYMMDDHHMMZ") - 1;
  1654. int max_length = sizeof("YYMMDDHHMMSS+hhmm") - 1;
  1655. if (remaining < min_length || remaining > max_length)
  1656. return 0;
  1657. OPENSSL_memcpy(p, str, 10);
  1658. p += 10;
  1659. str += 10;
  1660. remaining -= 10;
  1661. } else {
  1662. /*
  1663. * YYYYMMDDHHMM[SS[.fff]]Z or YYYYMMDDHHMM[SS[.f[f[f]]]](+-)hhmm
  1664. */
  1665. int min_length = sizeof("YYYYMMDDHHMMZ") - 1;
  1666. int max_length = sizeof("YYYYMMDDHHMMSS.fff+hhmm") - 1;
  1667. if (remaining < min_length || remaining > max_length)
  1668. return 0;
  1669. OPENSSL_memcpy(p, str, 12);
  1670. p += 12;
  1671. str += 12;
  1672. remaining -= 12;
  1673. }
  1674. if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
  1675. *(p++) = '0';
  1676. *(p++) = '0';
  1677. } else {
  1678. /* SS (seconds) */
  1679. if (remaining < 2)
  1680. return 0;
  1681. *(p++) = *(str++);
  1682. *(p++) = *(str++);
  1683. remaining -= 2;
  1684. /*
  1685. * Skip any (up to three) fractional seconds... TODO(emilia): in
  1686. * RFC5280, fractional seconds are forbidden. Can we just kill them
  1687. * altogether?
  1688. */
  1689. if (remaining && *str == '.') {
  1690. str++;
  1691. remaining--;
  1692. for (i = 0; i < 3 && remaining; i++, str++, remaining--) {
  1693. if (*str < '0' || *str > '9')
  1694. break;
  1695. }
  1696. }
  1697. }
  1698. *(p++) = 'Z';
  1699. *(p++) = '\0';
  1700. /* We now need either a terminating 'Z' or an offset. */
  1701. if (!remaining)
  1702. return 0;
  1703. if (*str == 'Z') {
  1704. if (remaining != 1)
  1705. return 0;
  1706. offset = 0;
  1707. } else {
  1708. /* (+-)HHMM */
  1709. if ((*str != '+') && (*str != '-'))
  1710. return 0;
  1711. /*
  1712. * Historical behaviour: the (+-)hhmm offset is forbidden in RFC5280.
  1713. */
  1714. if (remaining != 5)
  1715. return 0;
  1716. if (str[1] < '0' || str[1] > '9' || str[2] < '0' || str[2] > '9' ||
  1717. str[3] < '0' || str[3] > '9' || str[4] < '0' || str[4] > '9')
  1718. return 0;
  1719. offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
  1720. offset += (str[3] - '0') * 10 + (str[4] - '0');
  1721. if (*str == '-')
  1722. offset = -offset;
  1723. }
  1724. atm.type = ctm->type;
  1725. atm.flags = 0;
  1726. atm.length = sizeof(buff2);
  1727. atm.data = (unsigned char *)buff2;
  1728. if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
  1729. return 0;
  1730. if (ctm->type == V_ASN1_UTCTIME) {
  1731. i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
  1732. if (i < 50)
  1733. i += 100; /* cf. RFC 2459 */
  1734. j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
  1735. if (j < 50)
  1736. j += 100;
  1737. if (i < j)
  1738. return -1;
  1739. if (i > j)
  1740. return 1;
  1741. }
  1742. i = strcmp(buff1, buff2);
  1743. if (i == 0) /* wait a second then return younger :-) */
  1744. return -1;
  1745. else
  1746. return i;
  1747. }
  1748. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  1749. {
  1750. return X509_time_adj(s, adj, NULL);
  1751. }
  1752. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, time_t *in_tm)
  1753. {
  1754. return X509_time_adj_ex(s, 0, offset_sec, in_tm);
  1755. }
  1756. ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s,
  1757. int offset_day, long offset_sec, time_t *in_tm)
  1758. {
  1759. time_t t = 0;
  1760. if (in_tm)
  1761. t = *in_tm;
  1762. else
  1763. time(&t);
  1764. if (s && !(s->flags & ASN1_STRING_FLAG_MSTRING)) {
  1765. if (s->type == V_ASN1_UTCTIME)
  1766. return ASN1_UTCTIME_adj(s, t, offset_day, offset_sec);
  1767. if (s->type == V_ASN1_GENERALIZEDTIME)
  1768. return ASN1_GENERALIZEDTIME_adj(s, t, offset_day, offset_sec);
  1769. }
  1770. return ASN1_TIME_adj(s, t, offset_day, offset_sec);
  1771. }
  1772. /* Make a delta CRL as the diff between two full CRLs */
  1773. X509_CRL *X509_CRL_diff(X509_CRL *base, X509_CRL *newer,
  1774. EVP_PKEY *skey, const EVP_MD *md, unsigned int flags)
  1775. {
  1776. X509_CRL *crl = NULL;
  1777. int i;
  1778. size_t j;
  1779. STACK_OF(X509_REVOKED) *revs = NULL;
  1780. /* CRLs can't be delta already */
  1781. if (base->base_crl_number || newer->base_crl_number) {
  1782. OPENSSL_PUT_ERROR(X509, X509_R_CRL_ALREADY_DELTA);
  1783. return NULL;
  1784. }
  1785. /* Base and new CRL must have a CRL number */
  1786. if (!base->crl_number || !newer->crl_number) {
  1787. OPENSSL_PUT_ERROR(X509, X509_R_NO_CRL_NUMBER);
  1788. return NULL;
  1789. }
  1790. /* Issuer names must match */
  1791. if (X509_NAME_cmp(X509_CRL_get_issuer(base), X509_CRL_get_issuer(newer))) {
  1792. OPENSSL_PUT_ERROR(X509, X509_R_ISSUER_MISMATCH);
  1793. return NULL;
  1794. }
  1795. /* AKID and IDP must match */
  1796. if (!crl_extension_match(base, newer, NID_authority_key_identifier)) {
  1797. OPENSSL_PUT_ERROR(X509, X509_R_AKID_MISMATCH);
  1798. return NULL;
  1799. }
  1800. if (!crl_extension_match(base, newer, NID_issuing_distribution_point)) {
  1801. OPENSSL_PUT_ERROR(X509, X509_R_IDP_MISMATCH);
  1802. return NULL;
  1803. }
  1804. /* Newer CRL number must exceed full CRL number */
  1805. if (ASN1_INTEGER_cmp(newer->crl_number, base->crl_number) <= 0) {
  1806. OPENSSL_PUT_ERROR(X509, X509_R_NEWER_CRL_NOT_NEWER);
  1807. return NULL;
  1808. }
  1809. /* CRLs must verify */
  1810. if (skey && (X509_CRL_verify(base, skey) <= 0 ||
  1811. X509_CRL_verify(newer, skey) <= 0)) {
  1812. OPENSSL_PUT_ERROR(X509, X509_R_CRL_VERIFY_FAILURE);
  1813. return NULL;
  1814. }
  1815. /* Create new CRL */
  1816. crl = X509_CRL_new();
  1817. if (!crl || !X509_CRL_set_version(crl, 1))
  1818. goto memerr;
  1819. /* Set issuer name */
  1820. if (!X509_CRL_set_issuer_name(crl, X509_CRL_get_issuer(newer)))
  1821. goto memerr;
  1822. if (!X509_CRL_set_lastUpdate(crl, X509_CRL_get_lastUpdate(newer)))
  1823. goto memerr;
  1824. if (!X509_CRL_set_nextUpdate(crl, X509_CRL_get_nextUpdate(newer)))
  1825. goto memerr;
  1826. /* Set base CRL number: must be critical */
  1827. if (!X509_CRL_add1_ext_i2d(crl, NID_delta_crl, base->crl_number, 1, 0))
  1828. goto memerr;
  1829. /*
  1830. * Copy extensions across from newest CRL to delta: this will set CRL
  1831. * number to correct value too.
  1832. */
  1833. for (i = 0; i < X509_CRL_get_ext_count(newer); i++) {
  1834. X509_EXTENSION *ext;
  1835. ext = X509_CRL_get_ext(newer, i);
  1836. if (!X509_CRL_add_ext(crl, ext, -1))
  1837. goto memerr;
  1838. }
  1839. /* Go through revoked entries, copying as needed */
  1840. revs = X509_CRL_get_REVOKED(newer);
  1841. for (j = 0; j < sk_X509_REVOKED_num(revs); j++) {
  1842. X509_REVOKED *rvn, *rvtmp;
  1843. rvn = sk_X509_REVOKED_value(revs, j);
  1844. /*
  1845. * Add only if not also in base. TODO: need something cleverer here
  1846. * for some more complex CRLs covering multiple CAs.
  1847. */
  1848. if (!X509_CRL_get0_by_serial(base, &rvtmp, rvn->serialNumber)) {
  1849. rvtmp = X509_REVOKED_dup(rvn);
  1850. if (!rvtmp)
  1851. goto memerr;
  1852. if (!X509_CRL_add0_revoked(crl, rvtmp)) {
  1853. X509_REVOKED_free(rvtmp);
  1854. goto memerr;
  1855. }
  1856. }
  1857. }
  1858. /* TODO: optionally prune deleted entries */
  1859. if (skey && md && !X509_CRL_sign(crl, skey, md))
  1860. goto memerr;
  1861. return crl;
  1862. memerr:
  1863. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  1864. if (crl)
  1865. X509_CRL_free(crl);
  1866. return NULL;
  1867. }
  1868. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
  1869. CRYPTO_EX_unused * unused,
  1870. CRYPTO_EX_dup *dup_unused,
  1871. CRYPTO_EX_free *free_func)
  1872. {
  1873. /*
  1874. * This function is (usually) called only once, by
  1875. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
  1876. */
  1877. int index;
  1878. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp,
  1879. free_func)) {
  1880. return -1;
  1881. }
  1882. return index;
  1883. }
  1884. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1885. {
  1886. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1887. }
  1888. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1889. {
  1890. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1891. }
  1892. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1893. {
  1894. return ctx->error;
  1895. }
  1896. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1897. {
  1898. ctx->error = err;
  1899. }
  1900. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1901. {
  1902. return ctx->error_depth;
  1903. }
  1904. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1905. {
  1906. return ctx->current_cert;
  1907. }
  1908. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1909. {
  1910. return ctx->chain;
  1911. }
  1912. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1913. {
  1914. if (!ctx->chain)
  1915. return NULL;
  1916. return X509_chain_up_ref(ctx->chain);
  1917. }
  1918. X509 *X509_STORE_CTX_get0_current_issuer(X509_STORE_CTX *ctx)
  1919. {
  1920. return ctx->current_issuer;
  1921. }
  1922. X509_CRL *X509_STORE_CTX_get0_current_crl(X509_STORE_CTX *ctx)
  1923. {
  1924. return ctx->current_crl;
  1925. }
  1926. X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(X509_STORE_CTX *ctx)
  1927. {
  1928. return ctx->parent;
  1929. }
  1930. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1931. {
  1932. ctx->cert = x;
  1933. }
  1934. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1935. {
  1936. ctx->untrusted = sk;
  1937. }
  1938. STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(X509_STORE_CTX *ctx)
  1939. {
  1940. return ctx->untrusted;
  1941. }
  1942. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1943. {
  1944. ctx->crls = sk;
  1945. }
  1946. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1947. {
  1948. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1949. }
  1950. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1951. {
  1952. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1953. }
  1954. /*
  1955. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1956. * This is intended to be used when another structure has its own trust and
  1957. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1958. * set then we will usually have a default purpose in mind which should then
  1959. * be used to set the trust value. An example of this is SSL use: an SSL
  1960. * structure will have its own purpose and trust settings which the
  1961. * application can set: if they aren't set then we use the default of SSL
  1962. * client/server.
  1963. */
  1964. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1965. int purpose, int trust)
  1966. {
  1967. int idx;
  1968. /* If purpose not set use default */
  1969. if (!purpose)
  1970. purpose = def_purpose;
  1971. /* If we have a purpose then check it is valid */
  1972. if (purpose) {
  1973. X509_PURPOSE *ptmp;
  1974. idx = X509_PURPOSE_get_by_id(purpose);
  1975. if (idx == -1) {
  1976. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1977. return 0;
  1978. }
  1979. ptmp = X509_PURPOSE_get0(idx);
  1980. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1981. idx = X509_PURPOSE_get_by_id(def_purpose);
  1982. if (idx == -1) {
  1983. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID);
  1984. return 0;
  1985. }
  1986. ptmp = X509_PURPOSE_get0(idx);
  1987. }
  1988. /* If trust not set then get from purpose default */
  1989. if (!trust)
  1990. trust = ptmp->trust;
  1991. }
  1992. if (trust) {
  1993. idx = X509_TRUST_get_by_id(trust);
  1994. if (idx == -1) {
  1995. OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID);
  1996. return 0;
  1997. }
  1998. }
  1999. if (purpose && !ctx->param->purpose)
  2000. ctx->param->purpose = purpose;
  2001. if (trust && !ctx->param->trust)
  2002. ctx->param->trust = trust;
  2003. return 1;
  2004. }
  2005. X509_STORE_CTX *X509_STORE_CTX_new(void)
  2006. {
  2007. X509_STORE_CTX *ctx;
  2008. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  2009. if (!ctx) {
  2010. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  2011. return NULL;
  2012. }
  2013. X509_STORE_CTX_zero(ctx);
  2014. return ctx;
  2015. }
  2016. void X509_STORE_CTX_zero(X509_STORE_CTX *ctx)
  2017. {
  2018. OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
  2019. }
  2020. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  2021. {
  2022. if (ctx == NULL) {
  2023. return;
  2024. }
  2025. X509_STORE_CTX_cleanup(ctx);
  2026. OPENSSL_free(ctx);
  2027. }
  2028. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  2029. STACK_OF(X509) *chain)
  2030. {
  2031. int ret = 1;
  2032. X509_STORE_CTX_zero(ctx);
  2033. ctx->ctx = store;
  2034. ctx->cert = x509;
  2035. ctx->untrusted = chain;
  2036. CRYPTO_new_ex_data(&ctx->ex_data);
  2037. ctx->param = X509_VERIFY_PARAM_new();
  2038. if (!ctx->param)
  2039. goto err;
  2040. /*
  2041. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  2042. */
  2043. if (store)
  2044. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  2045. else
  2046. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  2047. if (store) {
  2048. ctx->verify_cb = store->verify_cb;
  2049. ctx->cleanup = store->cleanup;
  2050. } else
  2051. ctx->cleanup = 0;
  2052. if (ret)
  2053. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  2054. X509_VERIFY_PARAM_lookup("default"));
  2055. if (ret == 0)
  2056. goto err;
  2057. if (store && store->check_issued)
  2058. ctx->check_issued = store->check_issued;
  2059. else
  2060. ctx->check_issued = check_issued;
  2061. if (store && store->get_issuer)
  2062. ctx->get_issuer = store->get_issuer;
  2063. else
  2064. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  2065. if (store && store->verify_cb)
  2066. ctx->verify_cb = store->verify_cb;
  2067. else
  2068. ctx->verify_cb = null_callback;
  2069. if (store && store->verify)
  2070. ctx->verify = store->verify;
  2071. else
  2072. ctx->verify = internal_verify;
  2073. if (store && store->check_revocation)
  2074. ctx->check_revocation = store->check_revocation;
  2075. else
  2076. ctx->check_revocation = check_revocation;
  2077. if (store && store->get_crl)
  2078. ctx->get_crl = store->get_crl;
  2079. else
  2080. ctx->get_crl = NULL;
  2081. if (store && store->check_crl)
  2082. ctx->check_crl = store->check_crl;
  2083. else
  2084. ctx->check_crl = check_crl;
  2085. if (store && store->cert_crl)
  2086. ctx->cert_crl = store->cert_crl;
  2087. else
  2088. ctx->cert_crl = cert_crl;
  2089. if (store && store->lookup_certs)
  2090. ctx->lookup_certs = store->lookup_certs;
  2091. else
  2092. ctx->lookup_certs = X509_STORE_get1_certs;
  2093. if (store && store->lookup_crls)
  2094. ctx->lookup_crls = store->lookup_crls;
  2095. else
  2096. ctx->lookup_crls = X509_STORE_get1_crls;
  2097. ctx->check_policy = check_policy;
  2098. return 1;
  2099. err:
  2100. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &ctx->ex_data);
  2101. if (ctx->param != NULL) {
  2102. X509_VERIFY_PARAM_free(ctx->param);
  2103. }
  2104. OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX));
  2105. OPENSSL_PUT_ERROR(X509, ERR_R_MALLOC_FAILURE);
  2106. return 0;
  2107. }
  2108. /*
  2109. * Set alternative lookup method: just a STACK of trusted certificates. This
  2110. * avoids X509_STORE nastiness where it isn't needed.
  2111. */
  2112. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  2113. {
  2114. ctx->other_ctx = sk;
  2115. ctx->get_issuer = get_issuer_sk;
  2116. }
  2117. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  2118. {
  2119. /* We need to be idempotent because, unfortunately, |X509_STORE_CTX_free|
  2120. * also calls this function. */
  2121. if (ctx->cleanup != NULL) {
  2122. ctx->cleanup(ctx);
  2123. ctx->cleanup = NULL;
  2124. }
  2125. if (ctx->param != NULL) {
  2126. if (ctx->parent == NULL)
  2127. X509_VERIFY_PARAM_free(ctx->param);
  2128. ctx->param = NULL;
  2129. }
  2130. if (ctx->tree != NULL) {
  2131. X509_policy_tree_free(ctx->tree);
  2132. ctx->tree = NULL;
  2133. }
  2134. if (ctx->chain != NULL) {
  2135. sk_X509_pop_free(ctx->chain, X509_free);
  2136. ctx->chain = NULL;
  2137. }
  2138. CRYPTO_free_ex_data(&g_ex_data_class, ctx, &(ctx->ex_data));
  2139. OPENSSL_memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
  2140. }
  2141. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  2142. {
  2143. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2144. }
  2145. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  2146. {
  2147. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  2148. }
  2149. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  2150. time_t t)
  2151. {
  2152. X509_VERIFY_PARAM_set_time(ctx->param, t);
  2153. }
  2154. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  2155. int (*verify_cb) (int, X509_STORE_CTX *))
  2156. {
  2157. ctx->verify_cb = verify_cb;
  2158. }
  2159. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  2160. {
  2161. return ctx->tree;
  2162. }
  2163. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  2164. {
  2165. return ctx->explicit_policy;
  2166. }
  2167. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  2168. {
  2169. const X509_VERIFY_PARAM *param;
  2170. param = X509_VERIFY_PARAM_lookup(name);
  2171. if (!param)
  2172. return 0;
  2173. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  2174. }
  2175. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  2176. {
  2177. return ctx->param;
  2178. }
  2179. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  2180. {
  2181. if (ctx->param)
  2182. X509_VERIFY_PARAM_free(ctx->param);
  2183. ctx->param = param;
  2184. }
  2185. IMPLEMENT_ASN1_SET_OF(X509)
  2186. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)