x509_vfy.c 78 KB

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