x509_vfy.c 77 KB

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