x509_vfy.c 65 KB

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