x509_vfy.c 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511
  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. static int null_callback(int ok, X509_STORE_CTX *e);
  71. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer);
  72. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x);
  73. static int check_chain_extensions(X509_STORE_CTX *ctx);
  74. static int check_trust(X509_STORE_CTX *ctx);
  75. static int check_revocation(X509_STORE_CTX *ctx);
  76. static int check_cert(X509_STORE_CTX *ctx);
  77. static int check_policy(X509_STORE_CTX *ctx);
  78. static int internal_verify(X509_STORE_CTX *ctx);
  79. const char X509_version[] = "X.509" OPENSSL_VERSION_PTEXT;
  80. static int null_callback(int ok, X509_STORE_CTX *e)
  81. {
  82. return ok;
  83. }
  84. #if 0
  85. static int x509_subject_cmp(X509 **a, X509 **b)
  86. {
  87. return X509_subject_name_cmp(*a, *b);
  88. }
  89. #endif
  90. int X509_verify_cert(X509_STORE_CTX *ctx)
  91. {
  92. X509 *x, *xtmp, *chain_ss = NULL;
  93. int bad_chain = 0;
  94. X509_VERIFY_PARAM *param = ctx->param;
  95. int depth, i, ok = 0;
  96. int num;
  97. int (*cb) (int xok, X509_STORE_CTX *xctx);
  98. STACK_OF(X509) *sktmp = NULL;
  99. if (ctx->cert == NULL) {
  100. X509err(X509_F_X509_VERIFY_CERT, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY);
  101. return -1;
  102. }
  103. cb = ctx->verify_cb;
  104. /*
  105. * first we make sure the chain we are going to build is present and that
  106. * the first entry is in place
  107. */
  108. if (ctx->chain == NULL) {
  109. if (((ctx->chain = sk_X509_new_null()) == NULL) ||
  110. (!sk_X509_push(ctx->chain, ctx->cert))) {
  111. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  112. goto end;
  113. }
  114. CRYPTO_add(&ctx->cert->references, 1, CRYPTO_LOCK_X509);
  115. ctx->last_untrusted = 1;
  116. }
  117. /* We use a temporary STACK so we can chop and hack at it */
  118. if (ctx->untrusted != NULL
  119. && (sktmp = sk_X509_dup(ctx->untrusted)) == NULL) {
  120. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  121. goto end;
  122. }
  123. num = sk_X509_num(ctx->chain);
  124. x = sk_X509_value(ctx->chain, num - 1);
  125. depth = param->depth;
  126. for (;;) {
  127. /* If we have enough, we break */
  128. if (depth < num)
  129. break; /* FIXME: If this happens, we should take
  130. * note of it and, if appropriate, use the
  131. * X509_V_ERR_CERT_CHAIN_TOO_LONG error code
  132. * later. */
  133. /* If we are self signed, we break */
  134. if (ctx->check_issued(ctx, x, x))
  135. break;
  136. /* If we were passed a cert chain, use it first */
  137. if (ctx->untrusted != NULL) {
  138. xtmp = find_issuer(ctx, sktmp, x);
  139. if (xtmp != NULL) {
  140. if (!sk_X509_push(ctx->chain, xtmp)) {
  141. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  142. goto end;
  143. }
  144. CRYPTO_add(&xtmp->references, 1, CRYPTO_LOCK_X509);
  145. (void)sk_X509_delete_ptr(sktmp, xtmp);
  146. ctx->last_untrusted++;
  147. x = xtmp;
  148. num++;
  149. /*
  150. * reparse the full chain for the next one
  151. */
  152. continue;
  153. }
  154. }
  155. break;
  156. }
  157. /*
  158. * at this point, chain should contain a list of untrusted certificates.
  159. * We now need to add at least one trusted one, if possible, otherwise we
  160. * complain.
  161. */
  162. /*
  163. * Examine last certificate in chain and see if it is self signed.
  164. */
  165. i = sk_X509_num(ctx->chain);
  166. x = sk_X509_value(ctx->chain, i - 1);
  167. if (ctx->check_issued(ctx, x, x)) {
  168. /* we have a self signed certificate */
  169. if (sk_X509_num(ctx->chain) == 1) {
  170. /*
  171. * We have a single self signed certificate: see if we can find
  172. * it in the store. We must have an exact match to avoid possible
  173. * impersonation.
  174. */
  175. ok = ctx->get_issuer(&xtmp, ctx, x);
  176. if ((ok <= 0) || X509_cmp(x, xtmp)) {
  177. ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT;
  178. ctx->current_cert = x;
  179. ctx->error_depth = i - 1;
  180. if (ok == 1)
  181. X509_free(xtmp);
  182. bad_chain = 1;
  183. ok = cb(0, ctx);
  184. if (!ok)
  185. goto end;
  186. } else {
  187. /*
  188. * We have a match: replace certificate with store version so
  189. * we get any trust settings.
  190. */
  191. X509_free(x);
  192. x = xtmp;
  193. (void)sk_X509_set(ctx->chain, i - 1, x);
  194. ctx->last_untrusted = 0;
  195. }
  196. } else {
  197. /*
  198. * extract and save self signed certificate for later use
  199. */
  200. chain_ss = sk_X509_pop(ctx->chain);
  201. ctx->last_untrusted--;
  202. num--;
  203. x = sk_X509_value(ctx->chain, num - 1);
  204. }
  205. }
  206. /* We now lookup certs from the certificate store */
  207. for (;;) {
  208. /* If we have enough, we break */
  209. if (depth < num)
  210. break;
  211. /* If we are self signed, we break */
  212. if (ctx->check_issued(ctx, x, x))
  213. break;
  214. ok = ctx->get_issuer(&xtmp, ctx, x);
  215. if (ok < 0)
  216. return ok;
  217. if (ok == 0)
  218. break;
  219. x = xtmp;
  220. if (!sk_X509_push(ctx->chain, x)) {
  221. X509_free(xtmp);
  222. X509err(X509_F_X509_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  223. return 0;
  224. }
  225. num++;
  226. }
  227. /* we now have our chain, lets check it... */
  228. /* Is last certificate looked up self signed? */
  229. if (!ctx->check_issued(ctx, x, x)) {
  230. if ((chain_ss == NULL) || !ctx->check_issued(ctx, x, chain_ss)) {
  231. if (ctx->last_untrusted >= num)
  232. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY;
  233. else
  234. ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;
  235. ctx->current_cert = x;
  236. } else {
  237. sk_X509_push(ctx->chain, chain_ss);
  238. num++;
  239. ctx->last_untrusted = num;
  240. ctx->current_cert = chain_ss;
  241. ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN;
  242. chain_ss = NULL;
  243. }
  244. ctx->error_depth = num - 1;
  245. bad_chain = 1;
  246. ok = cb(0, ctx);
  247. if (!ok)
  248. goto end;
  249. }
  250. /* We have the chain complete: now we need to check its purpose */
  251. ok = check_chain_extensions(ctx);
  252. if (!ok)
  253. goto end;
  254. /* The chain extensions are OK: check trust */
  255. if (param->trust > 0)
  256. ok = check_trust(ctx);
  257. if (!ok)
  258. goto end;
  259. /* We may as well copy down any DSA parameters that are required */
  260. X509_get_pubkey_parameters(NULL, ctx->chain);
  261. /*
  262. * Check revocation status: we do this after copying parameters because
  263. * they may be needed for CRL signature verification.
  264. */
  265. ok = ctx->check_revocation(ctx);
  266. if (!ok)
  267. goto end;
  268. /* At this point, we have a chain and need to verify it */
  269. if (ctx->verify != NULL)
  270. ok = ctx->verify(ctx);
  271. else
  272. ok = internal_verify(ctx);
  273. if (!ok)
  274. goto end;
  275. #ifndef OPENSSL_NO_RFC3779
  276. /* RFC 3779 path validation, now that CRL check has been done */
  277. ok = v3_asid_validate_path(ctx);
  278. if (!ok)
  279. goto end;
  280. ok = v3_addr_validate_path(ctx);
  281. if (!ok)
  282. goto end;
  283. #endif
  284. /* If we get this far evaluate policies */
  285. if (!bad_chain && (ctx->param->flags & X509_V_FLAG_POLICY_CHECK))
  286. ok = ctx->check_policy(ctx);
  287. if (!ok)
  288. goto end;
  289. if (0) {
  290. end:
  291. X509_get_pubkey_parameters(NULL, ctx->chain);
  292. }
  293. if (sktmp != NULL)
  294. sk_X509_free(sktmp);
  295. if (chain_ss != NULL)
  296. X509_free(chain_ss);
  297. return ok;
  298. }
  299. /*
  300. * Given a STACK_OF(X509) find the issuer of cert (if any)
  301. */
  302. static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x)
  303. {
  304. int i;
  305. X509 *issuer;
  306. for (i = 0; i < sk_X509_num(sk); i++) {
  307. issuer = sk_X509_value(sk, i);
  308. if (ctx->check_issued(ctx, x, issuer))
  309. return issuer;
  310. }
  311. return NULL;
  312. }
  313. /* Given a possible certificate and issuer check them */
  314. static int check_issued(X509_STORE_CTX *ctx, X509 *x, X509 *issuer)
  315. {
  316. int ret;
  317. ret = X509_check_issued(issuer, x);
  318. if (ret == X509_V_OK)
  319. return 1;
  320. /* If we haven't asked for issuer errors don't set ctx */
  321. if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK))
  322. return 0;
  323. ctx->error = ret;
  324. ctx->current_cert = x;
  325. ctx->current_issuer = issuer;
  326. return ctx->verify_cb(0, ctx);
  327. return 0;
  328. }
  329. /* Alternative lookup method: look from a STACK stored in other_ctx */
  330. static int get_issuer_sk(X509 **issuer, X509_STORE_CTX *ctx, X509 *x)
  331. {
  332. *issuer = find_issuer(ctx, ctx->other_ctx, x);
  333. if (*issuer) {
  334. CRYPTO_add(&(*issuer)->references, 1, CRYPTO_LOCK_X509);
  335. return 1;
  336. } else
  337. return 0;
  338. }
  339. /*
  340. * Check a certificate chains extensions for consistency with the supplied
  341. * purpose
  342. */
  343. static int check_chain_extensions(X509_STORE_CTX *ctx)
  344. {
  345. #ifdef OPENSSL_NO_CHAIN_VERIFY
  346. return 1;
  347. #else
  348. int i, ok = 0, must_be_ca, plen = 0;
  349. X509 *x;
  350. int (*cb) (int xok, X509_STORE_CTX *xctx);
  351. int proxy_path_length = 0;
  352. int allow_proxy_certs =
  353. ! !(ctx->param->flags & X509_V_FLAG_ALLOW_PROXY_CERTS);
  354. cb = ctx->verify_cb;
  355. /*-
  356. * must_be_ca can have 1 of 3 values:
  357. * -1: we accept both CA and non-CA certificates, to allow direct
  358. * use of self-signed certificates (which are marked as CA).
  359. * 0: we only accept non-CA certificates. This is currently not
  360. * used, but the possibility is present for future extensions.
  361. * 1: we only accept CA certificates. This is currently used for
  362. * all certificates in the chain except the leaf certificate.
  363. */
  364. must_be_ca = -1;
  365. /*
  366. * A hack to keep people who don't want to modify their software happy
  367. */
  368. if (getenv("OPENSSL_ALLOW_PROXY_CERTS"))
  369. allow_proxy_certs = 1;
  370. /* Check all untrusted certificates */
  371. for (i = 0; i < ctx->last_untrusted; i++) {
  372. int ret;
  373. x = sk_X509_value(ctx->chain, i);
  374. if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  375. && (x->ex_flags & EXFLAG_CRITICAL)) {
  376. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION;
  377. ctx->error_depth = i;
  378. ctx->current_cert = x;
  379. ok = cb(0, ctx);
  380. if (!ok)
  381. goto end;
  382. }
  383. if (!allow_proxy_certs && (x->ex_flags & EXFLAG_PROXY)) {
  384. ctx->error = X509_V_ERR_PROXY_CERTIFICATES_NOT_ALLOWED;
  385. ctx->error_depth = i;
  386. ctx->current_cert = x;
  387. ok = cb(0, ctx);
  388. if (!ok)
  389. goto end;
  390. }
  391. ret = X509_check_ca(x);
  392. switch (must_be_ca) {
  393. case -1:
  394. if ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  395. && (ret != 1) && (ret != 0)) {
  396. ret = 0;
  397. ctx->error = X509_V_ERR_INVALID_CA;
  398. } else
  399. ret = 1;
  400. break;
  401. case 0:
  402. if (ret != 0) {
  403. ret = 0;
  404. ctx->error = X509_V_ERR_INVALID_NON_CA;
  405. } else
  406. ret = 1;
  407. break;
  408. default:
  409. if ((ret == 0)
  410. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  411. && (ret != 1))) {
  412. ret = 0;
  413. ctx->error = X509_V_ERR_INVALID_CA;
  414. } else
  415. ret = 1;
  416. break;
  417. }
  418. if (ret == 0) {
  419. ctx->error_depth = i;
  420. ctx->current_cert = x;
  421. ok = cb(0, ctx);
  422. if (!ok)
  423. goto end;
  424. }
  425. if (ctx->param->purpose > 0) {
  426. ret = X509_check_purpose(x, ctx->param->purpose, must_be_ca > 0);
  427. if ((ret == 0)
  428. || ((ctx->param->flags & X509_V_FLAG_X509_STRICT)
  429. && (ret != 1))) {
  430. ctx->error = X509_V_ERR_INVALID_PURPOSE;
  431. ctx->error_depth = i;
  432. ctx->current_cert = x;
  433. ok = cb(0, ctx);
  434. if (!ok)
  435. goto end;
  436. }
  437. }
  438. /* Check pathlen if not self issued */
  439. if ((i > 1) && !(x->ex_flags & EXFLAG_SI)
  440. && (x->ex_pathlen != -1)
  441. && (plen > (x->ex_pathlen + proxy_path_length + 1))) {
  442. ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED;
  443. ctx->error_depth = i;
  444. ctx->current_cert = x;
  445. ok = cb(0, ctx);
  446. if (!ok)
  447. goto end;
  448. }
  449. /* Increment path length if not self issued */
  450. if (!(x->ex_flags & EXFLAG_SI))
  451. plen++;
  452. /*
  453. * If this certificate is a proxy certificate, the next certificate
  454. * must be another proxy certificate or a EE certificate. If not,
  455. * the next certificate must be a CA certificate.
  456. */
  457. if (x->ex_flags & EXFLAG_PROXY) {
  458. if (x->ex_pcpathlen != -1 && i > x->ex_pcpathlen) {
  459. ctx->error = X509_V_ERR_PROXY_PATH_LENGTH_EXCEEDED;
  460. ctx->error_depth = i;
  461. ctx->current_cert = x;
  462. ok = cb(0, ctx);
  463. if (!ok)
  464. goto end;
  465. }
  466. proxy_path_length++;
  467. must_be_ca = 0;
  468. } else
  469. must_be_ca = 1;
  470. }
  471. ok = 1;
  472. end:
  473. return ok;
  474. #endif
  475. }
  476. static int check_trust(X509_STORE_CTX *ctx)
  477. {
  478. #ifdef OPENSSL_NO_CHAIN_VERIFY
  479. return 1;
  480. #else
  481. int i, ok;
  482. X509 *x;
  483. int (*cb) (int xok, X509_STORE_CTX *xctx);
  484. cb = ctx->verify_cb;
  485. /* For now just check the last certificate in the chain */
  486. i = sk_X509_num(ctx->chain) - 1;
  487. x = sk_X509_value(ctx->chain, i);
  488. ok = X509_check_trust(x, ctx->param->trust, 0);
  489. if (ok == X509_TRUST_TRUSTED)
  490. return 1;
  491. ctx->error_depth = i;
  492. ctx->current_cert = x;
  493. if (ok == X509_TRUST_REJECTED)
  494. ctx->error = X509_V_ERR_CERT_REJECTED;
  495. else
  496. ctx->error = X509_V_ERR_CERT_UNTRUSTED;
  497. ok = cb(0, ctx);
  498. return ok;
  499. #endif
  500. }
  501. static int check_revocation(X509_STORE_CTX *ctx)
  502. {
  503. int i, last, ok;
  504. if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK))
  505. return 1;
  506. if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL)
  507. last = sk_X509_num(ctx->chain) - 1;
  508. else
  509. last = 0;
  510. for (i = 0; i <= last; i++) {
  511. ctx->error_depth = i;
  512. ok = check_cert(ctx);
  513. if (!ok)
  514. return ok;
  515. }
  516. return 1;
  517. }
  518. static int check_cert(X509_STORE_CTX *ctx)
  519. {
  520. X509_CRL *crl = NULL;
  521. X509 *x;
  522. int ok, cnum;
  523. cnum = ctx->error_depth;
  524. x = sk_X509_value(ctx->chain, cnum);
  525. ctx->current_cert = x;
  526. /* Try to retrieve relevant CRL */
  527. ok = ctx->get_crl(ctx, &crl, x);
  528. /*
  529. * If error looking up CRL, nothing we can do except notify callback
  530. */
  531. if (!ok) {
  532. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL;
  533. ok = ctx->verify_cb(0, ctx);
  534. goto err;
  535. }
  536. ctx->current_crl = crl;
  537. ok = ctx->check_crl(ctx, crl);
  538. if (!ok)
  539. goto err;
  540. ok = ctx->cert_crl(ctx, crl, x);
  541. err:
  542. ctx->current_crl = NULL;
  543. X509_CRL_free(crl);
  544. return ok;
  545. }
  546. /* Check CRL times against values in X509_STORE_CTX */
  547. static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify)
  548. {
  549. time_t *ptime;
  550. int i;
  551. ctx->current_crl = crl;
  552. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  553. ptime = &ctx->param->check_time;
  554. else
  555. ptime = NULL;
  556. i = X509_cmp_time(X509_CRL_get_lastUpdate(crl), ptime);
  557. if (i == 0) {
  558. ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD;
  559. if (!notify || !ctx->verify_cb(0, ctx))
  560. return 0;
  561. }
  562. if (i > 0) {
  563. ctx->error = X509_V_ERR_CRL_NOT_YET_VALID;
  564. if (!notify || !ctx->verify_cb(0, ctx))
  565. return 0;
  566. }
  567. if (X509_CRL_get_nextUpdate(crl)) {
  568. i = X509_cmp_time(X509_CRL_get_nextUpdate(crl), ptime);
  569. if (i == 0) {
  570. ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD;
  571. if (!notify || !ctx->verify_cb(0, ctx))
  572. return 0;
  573. }
  574. if (i < 0) {
  575. ctx->error = X509_V_ERR_CRL_HAS_EXPIRED;
  576. if (!notify || !ctx->verify_cb(0, ctx))
  577. return 0;
  578. }
  579. }
  580. ctx->current_crl = NULL;
  581. return 1;
  582. }
  583. /*
  584. * Lookup CRLs from the supplied list. Look for matching isser name and
  585. * validity. If we can't find a valid CRL return the last one with matching
  586. * name. This gives more meaningful error codes. Otherwise we'd get a CRL not
  587. * found error if a CRL existed with matching name but was invalid.
  588. */
  589. static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl,
  590. X509_NAME *nm, STACK_OF(X509_CRL) *crls)
  591. {
  592. int i;
  593. X509_CRL *crl, *best_crl = NULL;
  594. for (i = 0; i < sk_X509_CRL_num(crls); i++) {
  595. crl = sk_X509_CRL_value(crls, i);
  596. if (X509_NAME_cmp(nm, X509_CRL_get_issuer(crl)))
  597. continue;
  598. if (check_crl_time(ctx, crl, 0)) {
  599. *pcrl = crl;
  600. CRYPTO_add(&crl->references, 1, CRYPTO_LOCK_X509);
  601. return 1;
  602. }
  603. best_crl = crl;
  604. }
  605. if (best_crl) {
  606. *pcrl = best_crl;
  607. CRYPTO_add(&best_crl->references, 1, CRYPTO_LOCK_X509);
  608. }
  609. return 0;
  610. }
  611. /*
  612. * Retrieve CRL corresponding to certificate: currently just a subject
  613. * lookup: maybe use AKID later...
  614. */
  615. static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x)
  616. {
  617. int ok;
  618. X509_CRL *crl = NULL;
  619. X509_OBJECT xobj;
  620. X509_NAME *nm;
  621. nm = X509_get_issuer_name(x);
  622. ok = get_crl_sk(ctx, &crl, nm, ctx->crls);
  623. if (ok) {
  624. *pcrl = crl;
  625. return 1;
  626. }
  627. ok = X509_STORE_get_by_subject(ctx, X509_LU_CRL, nm, &xobj);
  628. if (!ok) {
  629. /* If we got a near match from get_crl_sk use that */
  630. if (crl) {
  631. *pcrl = crl;
  632. return 1;
  633. }
  634. return 0;
  635. }
  636. *pcrl = xobj.data.crl;
  637. if (crl)
  638. X509_CRL_free(crl);
  639. return 1;
  640. }
  641. /* Check CRL validity */
  642. static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl)
  643. {
  644. X509 *issuer = NULL;
  645. EVP_PKEY *ikey = NULL;
  646. int ok = 0, chnum, cnum;
  647. cnum = ctx->error_depth;
  648. chnum = sk_X509_num(ctx->chain) - 1;
  649. /*
  650. * Find CRL issuer: if not last certificate then issuer is next
  651. * certificate in chain.
  652. */
  653. if (cnum < chnum)
  654. issuer = sk_X509_value(ctx->chain, cnum + 1);
  655. else {
  656. issuer = sk_X509_value(ctx->chain, chnum);
  657. /* If not self signed, can't check signature */
  658. if (!ctx->check_issued(ctx, issuer, issuer)) {
  659. ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER;
  660. ok = ctx->verify_cb(0, ctx);
  661. if (!ok)
  662. goto err;
  663. }
  664. }
  665. if (issuer) {
  666. /* Check for cRLSign bit if keyUsage present */
  667. if ((issuer->ex_flags & EXFLAG_KUSAGE) &&
  668. !(issuer->ex_kusage & KU_CRL_SIGN)) {
  669. ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN;
  670. ok = ctx->verify_cb(0, ctx);
  671. if (!ok)
  672. goto err;
  673. }
  674. /* Attempt to get issuer certificate public key */
  675. ikey = X509_get_pubkey(issuer);
  676. if (!ikey) {
  677. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  678. ok = ctx->verify_cb(0, ctx);
  679. if (!ok)
  680. goto err;
  681. } else {
  682. /* Verify CRL signature */
  683. if (X509_CRL_verify(crl, ikey) <= 0) {
  684. ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE;
  685. ok = ctx->verify_cb(0, ctx);
  686. if (!ok)
  687. goto err;
  688. }
  689. }
  690. }
  691. ok = check_crl_time(ctx, crl, 1);
  692. if (!ok)
  693. goto err;
  694. ok = 1;
  695. err:
  696. EVP_PKEY_free(ikey);
  697. return ok;
  698. }
  699. /* Check certificate against CRL */
  700. static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x)
  701. {
  702. int idx, ok;
  703. X509_REVOKED rtmp;
  704. STACK_OF(X509_EXTENSION) *exts;
  705. X509_EXTENSION *ext;
  706. /* Look for serial number of certificate in CRL */
  707. rtmp.serialNumber = X509_get_serialNumber(x);
  708. /*
  709. * Sort revoked into serial number order if not already sorted. Do this
  710. * under a lock to avoid race condition.
  711. */
  712. if (!sk_X509_REVOKED_is_sorted(crl->crl->revoked)) {
  713. CRYPTO_w_lock(CRYPTO_LOCK_X509_CRL);
  714. sk_X509_REVOKED_sort(crl->crl->revoked);
  715. CRYPTO_w_unlock(CRYPTO_LOCK_X509_CRL);
  716. }
  717. idx = sk_X509_REVOKED_find(crl->crl->revoked, &rtmp);
  718. /*
  719. * If found assume revoked: want something cleverer than this to handle
  720. * entry extensions in V2 CRLs.
  721. */
  722. if (idx >= 0) {
  723. ctx->error = X509_V_ERR_CERT_REVOKED;
  724. ok = ctx->verify_cb(0, ctx);
  725. if (!ok)
  726. return 0;
  727. }
  728. if (ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL)
  729. return 1;
  730. /*
  731. * See if we have any critical CRL extensions: since we currently don't
  732. * handle any CRL extensions the CRL must be rejected. This code
  733. * accesses the X509_CRL structure directly: applications shouldn't do
  734. * this.
  735. */
  736. exts = crl->crl->extensions;
  737. for (idx = 0; idx < sk_X509_EXTENSION_num(exts); idx++) {
  738. ext = sk_X509_EXTENSION_value(exts, idx);
  739. if (ext->critical > 0) {
  740. ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION;
  741. ok = ctx->verify_cb(0, ctx);
  742. if (!ok)
  743. return 0;
  744. break;
  745. }
  746. }
  747. return 1;
  748. }
  749. static int check_policy(X509_STORE_CTX *ctx)
  750. {
  751. int ret;
  752. ret = X509_policy_check(&ctx->tree, &ctx->explicit_policy, ctx->chain,
  753. ctx->param->policies, ctx->param->flags);
  754. if (ret == 0) {
  755. X509err(X509_F_CHECK_POLICY, ERR_R_MALLOC_FAILURE);
  756. return 0;
  757. }
  758. /* Invalid or inconsistent extensions */
  759. if (ret == -1) {
  760. /*
  761. * Locate certificates with bad extensions and notify callback.
  762. */
  763. X509 *x;
  764. int i;
  765. for (i = 1; i < sk_X509_num(ctx->chain); i++) {
  766. x = sk_X509_value(ctx->chain, i);
  767. if (!(x->ex_flags & EXFLAG_INVALID_POLICY))
  768. continue;
  769. ctx->current_cert = x;
  770. ctx->error = X509_V_ERR_INVALID_POLICY_EXTENSION;
  771. ret = ctx->verify_cb(0, ctx);
  772. }
  773. return 1;
  774. }
  775. if (ret == -2) {
  776. ctx->current_cert = NULL;
  777. ctx->error = X509_V_ERR_NO_EXPLICIT_POLICY;
  778. return ctx->verify_cb(0, ctx);
  779. }
  780. if (ctx->param->flags & X509_V_FLAG_NOTIFY_POLICY) {
  781. ctx->current_cert = NULL;
  782. ctx->error = X509_V_OK;
  783. if (!ctx->verify_cb(2, ctx))
  784. return 0;
  785. }
  786. return 1;
  787. }
  788. static int check_cert_time(X509_STORE_CTX *ctx, X509 *x)
  789. {
  790. time_t *ptime;
  791. int i;
  792. if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME)
  793. ptime = &ctx->param->check_time;
  794. else
  795. ptime = NULL;
  796. i = X509_cmp_time(X509_get_notBefore(x), ptime);
  797. if (i == 0) {
  798. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD;
  799. ctx->current_cert = x;
  800. if (!ctx->verify_cb(0, ctx))
  801. return 0;
  802. }
  803. if (i > 0) {
  804. ctx->error = X509_V_ERR_CERT_NOT_YET_VALID;
  805. ctx->current_cert = x;
  806. if (!ctx->verify_cb(0, ctx))
  807. return 0;
  808. }
  809. i = X509_cmp_time(X509_get_notAfter(x), ptime);
  810. if (i == 0) {
  811. ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD;
  812. ctx->current_cert = x;
  813. if (!ctx->verify_cb(0, ctx))
  814. return 0;
  815. }
  816. if (i < 0) {
  817. ctx->error = X509_V_ERR_CERT_HAS_EXPIRED;
  818. ctx->current_cert = x;
  819. if (!ctx->verify_cb(0, ctx))
  820. return 0;
  821. }
  822. return 1;
  823. }
  824. static int internal_verify(X509_STORE_CTX *ctx)
  825. {
  826. int ok = 0, n;
  827. X509 *xs, *xi;
  828. EVP_PKEY *pkey = NULL;
  829. int (*cb) (int xok, X509_STORE_CTX *xctx);
  830. cb = ctx->verify_cb;
  831. n = sk_X509_num(ctx->chain);
  832. ctx->error_depth = n - 1;
  833. n--;
  834. xi = sk_X509_value(ctx->chain, n);
  835. if (ctx->check_issued(ctx, xi, xi))
  836. xs = xi;
  837. else {
  838. if (n <= 0) {
  839. ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE;
  840. ctx->current_cert = xi;
  841. ok = cb(0, ctx);
  842. goto end;
  843. } else {
  844. n--;
  845. ctx->error_depth = n;
  846. xs = sk_X509_value(ctx->chain, n);
  847. }
  848. }
  849. /* ctx->error=0; not needed */
  850. while (n >= 0) {
  851. ctx->error_depth = n;
  852. /*
  853. * Skip signature check for self signed certificates unless
  854. * explicitly asked for. It doesn't add any security and just wastes
  855. * time.
  856. */
  857. if (!xs->valid
  858. && (xs != xi
  859. || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE))) {
  860. if ((pkey = X509_get_pubkey(xi)) == NULL) {
  861. ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY;
  862. ctx->current_cert = xi;
  863. ok = (*cb) (0, ctx);
  864. if (!ok)
  865. goto end;
  866. } else if (X509_verify(xs, pkey) <= 0) {
  867. ctx->error = X509_V_ERR_CERT_SIGNATURE_FAILURE;
  868. ctx->current_cert = xs;
  869. ok = (*cb) (0, ctx);
  870. if (!ok) {
  871. EVP_PKEY_free(pkey);
  872. goto end;
  873. }
  874. }
  875. EVP_PKEY_free(pkey);
  876. pkey = NULL;
  877. }
  878. xs->valid = 1;
  879. ok = check_cert_time(ctx, xs);
  880. if (!ok)
  881. goto end;
  882. /* The last error (if any) is still in the error value */
  883. ctx->current_issuer = xi;
  884. ctx->current_cert = xs;
  885. ok = (*cb) (1, ctx);
  886. if (!ok)
  887. goto end;
  888. n--;
  889. if (n >= 0) {
  890. xi = xs;
  891. xs = sk_X509_value(ctx->chain, n);
  892. }
  893. }
  894. ok = 1;
  895. end:
  896. return ok;
  897. }
  898. int X509_cmp_current_time(ASN1_TIME *ctm)
  899. {
  900. return X509_cmp_time(ctm, NULL);
  901. }
  902. int X509_cmp_time(ASN1_TIME *ctm, time_t *cmp_time)
  903. {
  904. char *str;
  905. ASN1_TIME atm;
  906. long offset;
  907. char buff1[24], buff2[24], *p;
  908. int i, j;
  909. p = buff1;
  910. i = ctm->length;
  911. str = (char *)ctm->data;
  912. if (ctm->type == V_ASN1_UTCTIME) {
  913. if ((i < 11) || (i > 17))
  914. return 0;
  915. memcpy(p, str, 10);
  916. p += 10;
  917. str += 10;
  918. } else {
  919. if (i < 13)
  920. return 0;
  921. memcpy(p, str, 12);
  922. p += 12;
  923. str += 12;
  924. }
  925. if ((*str == 'Z') || (*str == '-') || (*str == '+')) {
  926. *(p++) = '0';
  927. *(p++) = '0';
  928. } else {
  929. *(p++) = *(str++);
  930. *(p++) = *(str++);
  931. /* Skip any fractional seconds... */
  932. if (*str == '.') {
  933. str++;
  934. while ((*str >= '0') && (*str <= '9'))
  935. str++;
  936. }
  937. }
  938. *(p++) = 'Z';
  939. *(p++) = '\0';
  940. if (*str == 'Z')
  941. offset = 0;
  942. else {
  943. if ((*str != '+') && (*str != '-'))
  944. return 0;
  945. offset = ((str[1] - '0') * 10 + (str[2] - '0')) * 60;
  946. offset += (str[3] - '0') * 10 + (str[4] - '0');
  947. if (*str == '-')
  948. offset = -offset;
  949. }
  950. atm.type = ctm->type;
  951. atm.length = sizeof(buff2);
  952. atm.data = (unsigned char *)buff2;
  953. if (X509_time_adj(&atm, offset * 60, cmp_time) == NULL)
  954. return 0;
  955. if (ctm->type == V_ASN1_UTCTIME) {
  956. i = (buff1[0] - '0') * 10 + (buff1[1] - '0');
  957. if (i < 50)
  958. i += 100; /* cf. RFC 2459 */
  959. j = (buff2[0] - '0') * 10 + (buff2[1] - '0');
  960. if (j < 50)
  961. j += 100;
  962. if (i < j)
  963. return -1;
  964. if (i > j)
  965. return 1;
  966. }
  967. i = strcmp(buff1, buff2);
  968. if (i == 0) /* wait a second then return younger :-) */
  969. return -1;
  970. else
  971. return i;
  972. }
  973. ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long adj)
  974. {
  975. return X509_time_adj(s, adj, NULL);
  976. }
  977. ASN1_TIME *X509_time_adj(ASN1_TIME *s, long adj, time_t *in_tm)
  978. {
  979. time_t t;
  980. int type = -1;
  981. if (in_tm)
  982. t = *in_tm;
  983. else
  984. time(&t);
  985. t += adj;
  986. if (s)
  987. type = s->type;
  988. if (type == V_ASN1_UTCTIME)
  989. return ASN1_UTCTIME_set(s, t);
  990. if (type == V_ASN1_GENERALIZEDTIME)
  991. return ASN1_GENERALIZEDTIME_set(s, t);
  992. return ASN1_TIME_set(s, t);
  993. }
  994. int X509_get_pubkey_parameters(EVP_PKEY *pkey, STACK_OF(X509) *chain)
  995. {
  996. EVP_PKEY *ktmp = NULL, *ktmp2;
  997. int i, j;
  998. if ((pkey != NULL) && !EVP_PKEY_missing_parameters(pkey))
  999. return 1;
  1000. for (i = 0; i < sk_X509_num(chain); i++) {
  1001. ktmp = X509_get_pubkey(sk_X509_value(chain, i));
  1002. if (ktmp == NULL) {
  1003. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1004. X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY);
  1005. return 0;
  1006. }
  1007. if (!EVP_PKEY_missing_parameters(ktmp))
  1008. break;
  1009. else {
  1010. EVP_PKEY_free(ktmp);
  1011. ktmp = NULL;
  1012. }
  1013. }
  1014. if (ktmp == NULL) {
  1015. X509err(X509_F_X509_GET_PUBKEY_PARAMETERS,
  1016. X509_R_UNABLE_TO_FIND_PARAMETERS_IN_CHAIN);
  1017. return 0;
  1018. }
  1019. /* first, populate the other certs */
  1020. for (j = i - 1; j >= 0; j--) {
  1021. ktmp2 = X509_get_pubkey(sk_X509_value(chain, j));
  1022. EVP_PKEY_copy_parameters(ktmp2, ktmp);
  1023. EVP_PKEY_free(ktmp2);
  1024. }
  1025. if (pkey != NULL)
  1026. EVP_PKEY_copy_parameters(pkey, ktmp);
  1027. EVP_PKEY_free(ktmp);
  1028. return 1;
  1029. }
  1030. int X509_STORE_CTX_get_ex_new_index(long argl, void *argp,
  1031. CRYPTO_EX_new *new_func,
  1032. CRYPTO_EX_dup *dup_func,
  1033. CRYPTO_EX_free *free_func)
  1034. {
  1035. /*
  1036. * This function is (usually) called only once, by
  1037. * SSL_get_ex_data_X509_STORE_CTX_idx (ssl/ssl_cert.c).
  1038. */
  1039. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_X509_STORE_CTX, argl, argp,
  1040. new_func, dup_func, free_func);
  1041. }
  1042. int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data)
  1043. {
  1044. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1045. }
  1046. void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx)
  1047. {
  1048. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1049. }
  1050. int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx)
  1051. {
  1052. return ctx->error;
  1053. }
  1054. void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err)
  1055. {
  1056. ctx->error = err;
  1057. }
  1058. int X509_STORE_CTX_get_error_depth(X509_STORE_CTX *ctx)
  1059. {
  1060. return ctx->error_depth;
  1061. }
  1062. X509 *X509_STORE_CTX_get_current_cert(X509_STORE_CTX *ctx)
  1063. {
  1064. return ctx->current_cert;
  1065. }
  1066. STACK_OF(X509) *X509_STORE_CTX_get_chain(X509_STORE_CTX *ctx)
  1067. {
  1068. return ctx->chain;
  1069. }
  1070. STACK_OF(X509) *X509_STORE_CTX_get1_chain(X509_STORE_CTX *ctx)
  1071. {
  1072. int i;
  1073. X509 *x;
  1074. STACK_OF(X509) *chain;
  1075. if (!ctx->chain || !(chain = sk_X509_dup(ctx->chain)))
  1076. return NULL;
  1077. for (i = 0; i < sk_X509_num(chain); i++) {
  1078. x = sk_X509_value(chain, i);
  1079. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  1080. }
  1081. return chain;
  1082. }
  1083. void X509_STORE_CTX_set_cert(X509_STORE_CTX *ctx, X509 *x)
  1084. {
  1085. ctx->cert = x;
  1086. }
  1087. void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1088. {
  1089. ctx->untrusted = sk;
  1090. }
  1091. void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk)
  1092. {
  1093. ctx->crls = sk;
  1094. }
  1095. int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose)
  1096. {
  1097. return X509_STORE_CTX_purpose_inherit(ctx, 0, purpose, 0);
  1098. }
  1099. int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust)
  1100. {
  1101. return X509_STORE_CTX_purpose_inherit(ctx, 0, 0, trust);
  1102. }
  1103. /*
  1104. * This function is used to set the X509_STORE_CTX purpose and trust values.
  1105. * This is intended to be used when another structure has its own trust and
  1106. * purpose values which (if set) will be inherited by the ctx. If they aren't
  1107. * set then we will usually have a default purpose in mind which should then
  1108. * be used to set the trust value. An example of this is SSL use: an SSL
  1109. * structure will have its own purpose and trust settings which the
  1110. * application can set: if they aren't set then we use the default of SSL
  1111. * client/server.
  1112. */
  1113. int X509_STORE_CTX_purpose_inherit(X509_STORE_CTX *ctx, int def_purpose,
  1114. int purpose, int trust)
  1115. {
  1116. int idx;
  1117. /* If purpose not set use default */
  1118. if (!purpose)
  1119. purpose = def_purpose;
  1120. /* If we have a purpose then check it is valid */
  1121. if (purpose) {
  1122. X509_PURPOSE *ptmp;
  1123. idx = X509_PURPOSE_get_by_id(purpose);
  1124. if (idx == -1) {
  1125. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1126. X509_R_UNKNOWN_PURPOSE_ID);
  1127. return 0;
  1128. }
  1129. ptmp = X509_PURPOSE_get0(idx);
  1130. if (ptmp->trust == X509_TRUST_DEFAULT) {
  1131. idx = X509_PURPOSE_get_by_id(def_purpose);
  1132. if (idx == -1) {
  1133. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1134. X509_R_UNKNOWN_PURPOSE_ID);
  1135. return 0;
  1136. }
  1137. ptmp = X509_PURPOSE_get0(idx);
  1138. }
  1139. /* If trust not set then get from purpose default */
  1140. if (!trust)
  1141. trust = ptmp->trust;
  1142. }
  1143. if (trust) {
  1144. idx = X509_TRUST_get_by_id(trust);
  1145. if (idx == -1) {
  1146. X509err(X509_F_X509_STORE_CTX_PURPOSE_INHERIT,
  1147. X509_R_UNKNOWN_TRUST_ID);
  1148. return 0;
  1149. }
  1150. }
  1151. if (purpose && !ctx->param->purpose)
  1152. ctx->param->purpose = purpose;
  1153. if (trust && !ctx->param->trust)
  1154. ctx->param->trust = trust;
  1155. return 1;
  1156. }
  1157. X509_STORE_CTX *X509_STORE_CTX_new(void)
  1158. {
  1159. X509_STORE_CTX *ctx;
  1160. ctx = (X509_STORE_CTX *)OPENSSL_malloc(sizeof(X509_STORE_CTX));
  1161. if (!ctx) {
  1162. X509err(X509_F_X509_STORE_CTX_NEW, ERR_R_MALLOC_FAILURE);
  1163. return NULL;
  1164. }
  1165. memset(ctx, 0, sizeof(X509_STORE_CTX));
  1166. return ctx;
  1167. }
  1168. void X509_STORE_CTX_free(X509_STORE_CTX *ctx)
  1169. {
  1170. X509_STORE_CTX_cleanup(ctx);
  1171. OPENSSL_free(ctx);
  1172. }
  1173. int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509,
  1174. STACK_OF(X509) *chain)
  1175. {
  1176. int ret = 1;
  1177. ctx->ctx = store;
  1178. ctx->current_method = 0;
  1179. ctx->cert = x509;
  1180. ctx->untrusted = chain;
  1181. ctx->crls = NULL;
  1182. ctx->last_untrusted = 0;
  1183. ctx->other_ctx = NULL;
  1184. ctx->valid = 0;
  1185. ctx->chain = NULL;
  1186. ctx->error = 0;
  1187. ctx->explicit_policy = 0;
  1188. ctx->error_depth = 0;
  1189. ctx->current_cert = NULL;
  1190. ctx->current_issuer = NULL;
  1191. ctx->tree = NULL;
  1192. ctx->param = X509_VERIFY_PARAM_new();
  1193. if (!ctx->param) {
  1194. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  1195. return 0;
  1196. }
  1197. /*
  1198. * Inherit callbacks and flags from X509_STORE if not set use defaults.
  1199. */
  1200. if (store)
  1201. ret = X509_VERIFY_PARAM_inherit(ctx->param, store->param);
  1202. else
  1203. ctx->param->inh_flags |= X509_VP_FLAG_DEFAULT | X509_VP_FLAG_ONCE;
  1204. if (store) {
  1205. ctx->verify_cb = store->verify_cb;
  1206. ctx->cleanup = store->cleanup;
  1207. } else
  1208. ctx->cleanup = 0;
  1209. if (ret)
  1210. ret = X509_VERIFY_PARAM_inherit(ctx->param,
  1211. X509_VERIFY_PARAM_lookup("default"));
  1212. if (ret == 0) {
  1213. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  1214. return 0;
  1215. }
  1216. if (store && store->check_issued)
  1217. ctx->check_issued = store->check_issued;
  1218. else
  1219. ctx->check_issued = check_issued;
  1220. if (store && store->get_issuer)
  1221. ctx->get_issuer = store->get_issuer;
  1222. else
  1223. ctx->get_issuer = X509_STORE_CTX_get1_issuer;
  1224. if (store && store->verify_cb)
  1225. ctx->verify_cb = store->verify_cb;
  1226. else
  1227. ctx->verify_cb = null_callback;
  1228. if (store && store->verify)
  1229. ctx->verify = store->verify;
  1230. else
  1231. ctx->verify = internal_verify;
  1232. if (store && store->check_revocation)
  1233. ctx->check_revocation = store->check_revocation;
  1234. else
  1235. ctx->check_revocation = check_revocation;
  1236. if (store && store->get_crl)
  1237. ctx->get_crl = store->get_crl;
  1238. else
  1239. ctx->get_crl = get_crl;
  1240. if (store && store->check_crl)
  1241. ctx->check_crl = store->check_crl;
  1242. else
  1243. ctx->check_crl = check_crl;
  1244. if (store && store->cert_crl)
  1245. ctx->cert_crl = store->cert_crl;
  1246. else
  1247. ctx->cert_crl = cert_crl;
  1248. ctx->check_policy = check_policy;
  1249. /*
  1250. * This memset() can't make any sense anyway, so it's removed. As
  1251. * X509_STORE_CTX_cleanup does a proper "free" on the ex_data, we put a
  1252. * corresponding "new" here and remove this bogus initialisation.
  1253. */
  1254. /* memset(&(ctx->ex_data),0,sizeof(CRYPTO_EX_DATA)); */
  1255. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx,
  1256. &(ctx->ex_data))) {
  1257. OPENSSL_free(ctx);
  1258. X509err(X509_F_X509_STORE_CTX_INIT, ERR_R_MALLOC_FAILURE);
  1259. return 0;
  1260. }
  1261. return 1;
  1262. }
  1263. /*
  1264. * Set alternative lookup method: just a STACK of trusted certificates. This
  1265. * avoids X509_STORE nastiness where it isn't needed.
  1266. */
  1267. void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk)
  1268. {
  1269. ctx->other_ctx = sk;
  1270. ctx->get_issuer = get_issuer_sk;
  1271. }
  1272. void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx)
  1273. {
  1274. if (ctx->cleanup)
  1275. ctx->cleanup(ctx);
  1276. if (ctx->param != NULL) {
  1277. X509_VERIFY_PARAM_free(ctx->param);
  1278. ctx->param = NULL;
  1279. }
  1280. if (ctx->tree != NULL) {
  1281. X509_policy_tree_free(ctx->tree);
  1282. ctx->tree = NULL;
  1283. }
  1284. if (ctx->chain != NULL) {
  1285. sk_X509_pop_free(ctx->chain, X509_free);
  1286. ctx->chain = NULL;
  1287. }
  1288. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_X509_STORE_CTX, ctx, &(ctx->ex_data));
  1289. memset(&ctx->ex_data, 0, sizeof(CRYPTO_EX_DATA));
  1290. }
  1291. void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth)
  1292. {
  1293. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1294. }
  1295. void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags)
  1296. {
  1297. X509_VERIFY_PARAM_set_flags(ctx->param, flags);
  1298. }
  1299. void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags,
  1300. time_t t)
  1301. {
  1302. X509_VERIFY_PARAM_set_time(ctx->param, t);
  1303. }
  1304. void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx,
  1305. int (*verify_cb) (int, X509_STORE_CTX *))
  1306. {
  1307. ctx->verify_cb = verify_cb;
  1308. }
  1309. X509_POLICY_TREE *X509_STORE_CTX_get0_policy_tree(X509_STORE_CTX *ctx)
  1310. {
  1311. return ctx->tree;
  1312. }
  1313. int X509_STORE_CTX_get_explicit_policy(X509_STORE_CTX *ctx)
  1314. {
  1315. return ctx->explicit_policy;
  1316. }
  1317. int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name)
  1318. {
  1319. const X509_VERIFY_PARAM *param;
  1320. param = X509_VERIFY_PARAM_lookup(name);
  1321. if (!param)
  1322. return 0;
  1323. return X509_VERIFY_PARAM_inherit(ctx->param, param);
  1324. }
  1325. X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx)
  1326. {
  1327. return ctx->param;
  1328. }
  1329. void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param)
  1330. {
  1331. if (ctx->param)
  1332. X509_VERIFY_PARAM_free(ctx->param);
  1333. ctx->param = param;
  1334. }
  1335. IMPLEMENT_STACK_OF(X509)
  1336. IMPLEMENT_ASN1_SET_OF(X509)
  1337. IMPLEMENT_STACK_OF(X509_NAME)
  1338. IMPLEMENT_STACK_OF(X509_ATTRIBUTE)
  1339. IMPLEMENT_ASN1_SET_OF(X509_ATTRIBUTE)