您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

868 行
23 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. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  114. #include <errno.h>
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include <openssl/bio.h>
  118. #include <openssl/bn.h>
  119. #include <openssl/buf.h>
  120. #include <openssl/ec_key.h>
  121. #include <openssl/dh.h>
  122. #include <openssl/err.h>
  123. #include <openssl/mem.h>
  124. #include <openssl/obj.h>
  125. #include <openssl/pem.h>
  126. #include <openssl/x509.h>
  127. #include <openssl/x509v3.h>
  128. #include "../crypto/dh/internal.h"
  129. #include "../crypto/directory.h"
  130. #include "../crypto/internal.h"
  131. #include "internal.h"
  132. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  133. /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
  134. * reserved app_data slot. Before ex_data was introduced, app_data was used.
  135. * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
  136. * works. */
  137. return 0;
  138. }
  139. CERT *ssl_cert_new(void) {
  140. CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  141. if (ret == NULL) {
  142. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  143. return NULL;
  144. }
  145. memset(ret, 0, sizeof(CERT));
  146. return ret;
  147. }
  148. CERT *ssl_cert_dup(CERT *cert) {
  149. CERT *ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  150. if (ret == NULL) {
  151. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  152. return NULL;
  153. }
  154. memset(ret, 0, sizeof(CERT));
  155. ret->mask_k = cert->mask_k;
  156. ret->mask_a = cert->mask_a;
  157. if (cert->dh_tmp != NULL) {
  158. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  159. if (ret->dh_tmp == NULL) {
  160. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  161. goto err;
  162. }
  163. if (cert->dh_tmp->priv_key) {
  164. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  165. if (!b) {
  166. OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB);
  167. goto err;
  168. }
  169. ret->dh_tmp->priv_key = b;
  170. }
  171. if (cert->dh_tmp->pub_key) {
  172. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  173. if (!b) {
  174. OPENSSL_PUT_ERROR(SSL, ERR_R_BN_LIB);
  175. goto err;
  176. }
  177. ret->dh_tmp->pub_key = b;
  178. }
  179. }
  180. ret->dh_tmp_cb = cert->dh_tmp_cb;
  181. ret->ecdh_nid = cert->ecdh_nid;
  182. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  183. if (cert->x509 != NULL) {
  184. ret->x509 = X509_up_ref(cert->x509);
  185. }
  186. if (cert->privatekey != NULL) {
  187. ret->privatekey = EVP_PKEY_up_ref(cert->privatekey);
  188. }
  189. if (cert->chain) {
  190. ret->chain = X509_chain_up_ref(cert->chain);
  191. if (!ret->chain) {
  192. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  193. goto err;
  194. }
  195. }
  196. /* Copy over signature algorithm configuration. */
  197. if (cert->conf_sigalgs) {
  198. ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
  199. if (!ret->conf_sigalgs) {
  200. goto err;
  201. }
  202. ret->conf_sigalgslen = cert->conf_sigalgslen;
  203. }
  204. if (cert->client_sigalgs) {
  205. ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
  206. cert->client_sigalgslen);
  207. if (!ret->client_sigalgs) {
  208. goto err;
  209. }
  210. ret->client_sigalgslen = cert->client_sigalgslen;
  211. }
  212. /* Copy any custom client certificate types */
  213. if (cert->client_certificate_types) {
  214. ret->client_certificate_types = BUF_memdup(
  215. cert->client_certificate_types, cert->num_client_certificate_types);
  216. if (!ret->client_certificate_types) {
  217. goto err;
  218. }
  219. ret->num_client_certificate_types = cert->num_client_certificate_types;
  220. }
  221. ret->cert_cb = cert->cert_cb;
  222. ret->cert_cb_arg = cert->cert_cb_arg;
  223. if (cert->verify_store) {
  224. CRYPTO_refcount_inc(&cert->verify_store->references);
  225. ret->verify_store = cert->verify_store;
  226. }
  227. if (cert->chain_store) {
  228. CRYPTO_refcount_inc(&cert->chain_store->references);
  229. ret->chain_store = cert->chain_store;
  230. }
  231. return ret;
  232. err:
  233. ssl_cert_free(ret);
  234. return NULL;
  235. }
  236. /* Free up and clear all certificates and chains */
  237. void ssl_cert_clear_certs(CERT *cert) {
  238. if (cert == NULL) {
  239. return;
  240. }
  241. X509_free(cert->x509);
  242. cert->x509 = NULL;
  243. EVP_PKEY_free(cert->privatekey);
  244. cert->privatekey = NULL;
  245. sk_X509_pop_free(cert->chain, X509_free);
  246. cert->chain = NULL;
  247. cert->key_method = NULL;
  248. }
  249. void ssl_cert_free(CERT *c) {
  250. if (c == NULL) {
  251. return;
  252. }
  253. DH_free(c->dh_tmp);
  254. ssl_cert_clear_certs(c);
  255. OPENSSL_free(c->peer_sigalgs);
  256. OPENSSL_free(c->conf_sigalgs);
  257. OPENSSL_free(c->client_sigalgs);
  258. OPENSSL_free(c->shared_sigalgs);
  259. OPENSSL_free(c->client_certificate_types);
  260. X509_STORE_free(c->verify_store);
  261. X509_STORE_free(c->chain_store);
  262. OPENSSL_free(c);
  263. }
  264. int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  265. sk_X509_pop_free(cert->chain, X509_free);
  266. cert->chain = chain;
  267. return 1;
  268. }
  269. int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  270. STACK_OF(X509) *dchain;
  271. if (chain == NULL) {
  272. return ssl_cert_set0_chain(cert, NULL);
  273. }
  274. dchain = X509_chain_up_ref(chain);
  275. if (dchain == NULL) {
  276. return 0;
  277. }
  278. if (!ssl_cert_set0_chain(cert, dchain)) {
  279. sk_X509_pop_free(dchain, X509_free);
  280. return 0;
  281. }
  282. return 1;
  283. }
  284. int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  285. if (cert->chain == NULL) {
  286. cert->chain = sk_X509_new_null();
  287. }
  288. if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
  289. return 0;
  290. }
  291. return 1;
  292. }
  293. int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  294. if (!ssl_cert_add0_chain_cert(cert, x509)) {
  295. return 0;
  296. }
  297. X509_up_ref(x509);
  298. return 1;
  299. }
  300. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  301. c->cert_cb = cb;
  302. c->cert_cb_arg = arg;
  303. }
  304. SESS_CERT *ssl_sess_cert_new(void) {
  305. SESS_CERT *ret;
  306. ret = OPENSSL_malloc(sizeof *ret);
  307. if (ret == NULL) {
  308. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  309. return NULL;
  310. }
  311. memset(ret, 0, sizeof *ret);
  312. return ret;
  313. }
  314. SESS_CERT *ssl_sess_cert_dup(const SESS_CERT *sess_cert) {
  315. SESS_CERT *ret = ssl_sess_cert_new();
  316. if (ret == NULL) {
  317. return NULL;
  318. }
  319. if (sess_cert->cert_chain != NULL) {
  320. ret->cert_chain = X509_chain_up_ref(sess_cert->cert_chain);
  321. if (ret->cert_chain == NULL) {
  322. ssl_sess_cert_free(ret);
  323. return NULL;
  324. }
  325. }
  326. if (sess_cert->peer_cert != NULL) {
  327. ret->peer_cert = X509_up_ref(sess_cert->peer_cert);
  328. }
  329. if (sess_cert->peer_dh_tmp != NULL) {
  330. ret->peer_dh_tmp = sess_cert->peer_dh_tmp;
  331. DH_up_ref(ret->peer_dh_tmp);
  332. }
  333. if (sess_cert->peer_ecdh_tmp != NULL) {
  334. ret->peer_ecdh_tmp = sess_cert->peer_ecdh_tmp;
  335. EC_KEY_up_ref(ret->peer_ecdh_tmp);
  336. }
  337. return ret;
  338. }
  339. void ssl_sess_cert_free(SESS_CERT *sess_cert) {
  340. if (sess_cert == NULL) {
  341. return;
  342. }
  343. sk_X509_pop_free(sess_cert->cert_chain, X509_free);
  344. X509_free(sess_cert->peer_cert);
  345. DH_free(sess_cert->peer_dh_tmp);
  346. EC_KEY_free(sess_cert->peer_ecdh_tmp);
  347. OPENSSL_free(sess_cert);
  348. }
  349. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk) {
  350. X509 *x;
  351. int i;
  352. X509_STORE *verify_store;
  353. X509_STORE_CTX ctx;
  354. if (s->cert->verify_store) {
  355. verify_store = s->cert->verify_store;
  356. } else {
  357. verify_store = s->ctx->cert_store;
  358. }
  359. if (sk == NULL || sk_X509_num(sk) == 0) {
  360. return 0;
  361. }
  362. x = sk_X509_value(sk, 0);
  363. if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
  364. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  365. return 0;
  366. }
  367. X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
  368. /* We need to inherit the verify parameters. These can be determined by the
  369. * context: if its a server it will verify SSL client certificates or vice
  370. * versa. */
  371. X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
  372. /* Anything non-default in "param" should overwrite anything in the ctx. */
  373. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  374. if (s->verify_callback) {
  375. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  376. }
  377. if (s->ctx->app_verify_callback != NULL) {
  378. i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  379. } else {
  380. i = X509_verify_cert(&ctx);
  381. }
  382. s->verify_result = ctx.error;
  383. X509_STORE_CTX_cleanup(&ctx);
  384. return i;
  385. }
  386. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  387. STACK_OF(X509_NAME) *name_list) {
  388. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  389. *ca_list = name_list;
  390. }
  391. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk) {
  392. size_t i;
  393. STACK_OF(X509_NAME) *ret;
  394. X509_NAME *name;
  395. ret = sk_X509_NAME_new_null();
  396. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  397. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  398. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  399. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  400. return NULL;
  401. }
  402. }
  403. return ret;
  404. }
  405. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list) {
  406. set_client_CA_list(&(s->client_CA), name_list);
  407. }
  408. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  409. set_client_CA_list(&(ctx->client_CA), name_list);
  410. }
  411. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  412. return ctx->client_CA;
  413. }
  414. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s) {
  415. if (s->server) {
  416. if (s->client_CA != NULL) {
  417. return s->client_CA;
  418. } else {
  419. return s->ctx->client_CA;
  420. }
  421. } else {
  422. if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
  423. return s->s3->tmp.ca_names;
  424. } else {
  425. return NULL;
  426. }
  427. }
  428. }
  429. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x) {
  430. X509_NAME *name;
  431. if (x == NULL) {
  432. return 0;
  433. }
  434. if (*sk == NULL) {
  435. *sk = sk_X509_NAME_new_null();
  436. if (*sk == NULL) {
  437. return 0;
  438. }
  439. }
  440. name = X509_NAME_dup(X509_get_subject_name(x));
  441. if (name == NULL) {
  442. return 0;
  443. }
  444. if (!sk_X509_NAME_push(*sk, name)) {
  445. X509_NAME_free(name);
  446. return 0;
  447. }
  448. return 1;
  449. }
  450. int SSL_add_client_CA(SSL *ssl, X509 *x) {
  451. return add_client_CA(&(ssl->client_CA), x);
  452. }
  453. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
  454. return add_client_CA(&(ctx->client_CA), x);
  455. }
  456. static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
  457. return X509_NAME_cmp(*a, *b);
  458. }
  459. /* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
  460. * it doesn't really have anything to do with clients (except that a common use
  461. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  462. * much to do with CAs, either, since it will load any old cert.
  463. *
  464. * \param file the file containing one or more certs.
  465. * \return a ::STACK containing the certs. */
  466. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file) {
  467. BIO *in;
  468. X509 *x = NULL;
  469. X509_NAME *xn = NULL;
  470. STACK_OF(X509_NAME) *ret = NULL, *sk;
  471. sk = sk_X509_NAME_new(xname_cmp);
  472. in = BIO_new(BIO_s_file());
  473. if (sk == NULL || in == NULL) {
  474. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  475. goto err;
  476. }
  477. if (!BIO_read_filename(in, file)) {
  478. goto err;
  479. }
  480. for (;;) {
  481. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  482. break;
  483. }
  484. if (ret == NULL) {
  485. ret = sk_X509_NAME_new_null();
  486. if (ret == NULL) {
  487. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  488. goto err;
  489. }
  490. }
  491. xn = X509_get_subject_name(x);
  492. if (xn == NULL) {
  493. goto err;
  494. }
  495. /* check for duplicates */
  496. xn = X509_NAME_dup(xn);
  497. if (xn == NULL) {
  498. goto err;
  499. }
  500. if (sk_X509_NAME_find(sk, NULL, xn)) {
  501. X509_NAME_free(xn);
  502. } else {
  503. sk_X509_NAME_push(sk, xn);
  504. sk_X509_NAME_push(ret, xn);
  505. }
  506. }
  507. if (0) {
  508. err:
  509. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  510. ret = NULL;
  511. }
  512. sk_X509_NAME_free(sk);
  513. BIO_free(in);
  514. X509_free(x);
  515. if (ret != NULL) {
  516. ERR_clear_error();
  517. }
  518. return ret;
  519. }
  520. /* Add a file of certs to a stack.
  521. *
  522. * \param stack the stack to add to.
  523. * \param file the file to add from. All certs in this file that are not
  524. * already in the stack will be added.
  525. * \return 1 for success, 0 for failure. Note that in the case of failure some
  526. * certs may have been added to \c stack. */
  527. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  528. const char *file) {
  529. BIO *in;
  530. X509 *x = NULL;
  531. X509_NAME *xn = NULL;
  532. int ret = 1;
  533. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  534. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  535. in = BIO_new(BIO_s_file());
  536. if (in == NULL) {
  537. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  538. goto err;
  539. }
  540. if (!BIO_read_filename(in, file)) {
  541. goto err;
  542. }
  543. for (;;) {
  544. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  545. break;
  546. }
  547. xn = X509_get_subject_name(x);
  548. if (xn == NULL) {
  549. goto err;
  550. }
  551. xn = X509_NAME_dup(xn);
  552. if (xn == NULL) {
  553. goto err;
  554. }
  555. if (sk_X509_NAME_find(stack, NULL, xn)) {
  556. X509_NAME_free(xn);
  557. } else {
  558. sk_X509_NAME_push(stack, xn);
  559. }
  560. }
  561. ERR_clear_error();
  562. if (0) {
  563. err:
  564. ret = 0;
  565. }
  566. BIO_free(in);
  567. X509_free(x);
  568. (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
  569. return ret;
  570. }
  571. /* Add a directory of certs to a stack.
  572. *
  573. * \param stack the stack to append to.
  574. * \param dir the directory to append from. All files in this directory will be
  575. * examined as potential certs. Any that are acceptable to
  576. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
  577. * be included.
  578. * \return 1 for success, 0 for failure. Note that in the case of failure some
  579. * certs may have been added to \c stack. */
  580. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  581. const char *dir) {
  582. OPENSSL_DIR_CTX *d = NULL;
  583. const char *filename;
  584. int ret = 0;
  585. /* Note that a side effect is that the CAs will be sorted by name */
  586. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  587. char buf[1024];
  588. int r;
  589. if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
  590. OPENSSL_PUT_ERROR(SSL, SSL_R_PATH_TOO_LONG);
  591. goto err;
  592. }
  593. r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
  594. if (r <= 0 || r >= (int)sizeof(buf) ||
  595. !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
  596. goto err;
  597. }
  598. }
  599. if (errno) {
  600. OPENSSL_PUT_ERROR(SSL, ERR_R_SYS_LIB);
  601. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  602. goto err;
  603. }
  604. ret = 1;
  605. err:
  606. if (d) {
  607. OPENSSL_DIR_end(&d);
  608. }
  609. return ret;
  610. }
  611. /* Add a certificate to a BUF_MEM structure */
  612. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
  613. int n;
  614. uint8_t *p;
  615. n = i2d_X509(x, NULL);
  616. if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  617. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  618. return 0;
  619. }
  620. p = (uint8_t *)&(buf->data[*l]);
  621. l2n3(n, p);
  622. i2d_X509(x, &p);
  623. *l += n + 3;
  624. return 1;
  625. }
  626. /* Add certificate chain to internal SSL BUF_MEM structure. */
  627. int ssl_add_cert_chain(SSL *ssl, unsigned long *l) {
  628. CERT *cert = ssl->cert;
  629. BUF_MEM *buf = ssl->init_buf;
  630. int no_chain = 0;
  631. size_t i;
  632. X509 *x = cert->x509;
  633. STACK_OF(X509) *chain = cert->chain;
  634. X509_STORE *chain_store;
  635. if (x == NULL) {
  636. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  637. return 0;
  638. }
  639. if (ssl->cert->chain_store) {
  640. chain_store = ssl->cert->chain_store;
  641. } else {
  642. chain_store = ssl->ctx->cert_store;
  643. }
  644. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
  645. no_chain = 1;
  646. }
  647. if (no_chain) {
  648. if (!ssl_add_cert_to_buf(buf, l, x)) {
  649. return 0;
  650. }
  651. for (i = 0; i < sk_X509_num(chain); i++) {
  652. x = sk_X509_value(chain, i);
  653. if (!ssl_add_cert_to_buf(buf, l, x)) {
  654. return 0;
  655. }
  656. }
  657. } else {
  658. X509_STORE_CTX xs_ctx;
  659. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
  660. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  661. return 0;
  662. }
  663. X509_verify_cert(&xs_ctx);
  664. /* Don't leave errors in the queue */
  665. ERR_clear_error();
  666. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  667. x = sk_X509_value(xs_ctx.chain, i);
  668. if (!ssl_add_cert_to_buf(buf, l, x)) {
  669. X509_STORE_CTX_cleanup(&xs_ctx);
  670. return 0;
  671. }
  672. }
  673. X509_STORE_CTX_cleanup(&xs_ctx);
  674. }
  675. return 1;
  676. }
  677. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
  678. X509_STORE **pstore;
  679. if (chain) {
  680. pstore = &c->chain_store;
  681. } else {
  682. pstore = &c->verify_store;
  683. }
  684. X509_STORE_free(*pstore);
  685. *pstore = store;
  686. if (ref && store) {
  687. CRYPTO_refcount_inc(&store->references);
  688. }
  689. return 1;
  690. }
  691. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  692. return ssl_cert_set0_chain(ctx->cert, chain);
  693. }
  694. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  695. return ssl_cert_set1_chain(ctx->cert, chain);
  696. }
  697. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  698. return ssl_cert_set0_chain(ssl->cert, chain);
  699. }
  700. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  701. return ssl_cert_set1_chain(ssl->cert, chain);
  702. }
  703. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  704. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  705. }
  706. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  707. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  708. }
  709. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  710. return SSL_CTX_add0_chain_cert(ctx, x509);
  711. }
  712. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  713. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  714. }
  715. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  716. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  717. }
  718. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  719. return SSL_CTX_set0_chain(ctx, NULL);
  720. }
  721. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  722. return SSL_CTX_clear_chain_certs(ctx);
  723. }
  724. int SSL_clear_chain_certs(SSL *ssl) {
  725. return SSL_set0_chain(ssl, NULL);
  726. }
  727. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  728. *out_chain = ctx->cert->chain;
  729. return 1;
  730. }
  731. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  732. STACK_OF(X509) **out_chain) {
  733. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  734. }
  735. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  736. *out_chain = ssl->cert->chain;
  737. return 1;
  738. }