x509_vfy.c 73 KB

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