Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

2425 lignes
57 KiB

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