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

992 行
26 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/dh.h>
  121. #include <openssl/err.h>
  122. #include <openssl/mem.h>
  123. #include <openssl/obj.h>
  124. #include <openssl/pem.h>
  125. #include <openssl/x509v3.h>
  126. #include "../crypto/dh/internal.h"
  127. #include "../crypto/directory.h"
  128. #include "internal.h"
  129. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  130. static int ssl_x509_store_ctx_idx = -1;
  131. int got_write_lock = 0;
  132. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  133. if (ssl_x509_store_ctx_idx < 0) {
  134. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  135. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  136. got_write_lock = 1;
  137. if (ssl_x509_store_ctx_idx < 0) {
  138. ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(
  139. 0, "SSL for verify callback", NULL, NULL, NULL);
  140. }
  141. }
  142. if (got_write_lock) {
  143. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  144. } else {
  145. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  146. }
  147. return ssl_x509_store_ctx_idx;
  148. }
  149. CERT *ssl_cert_new(void) {
  150. CERT *ret;
  151. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  152. if (ret == NULL) {
  153. OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
  154. return NULL;
  155. }
  156. memset(ret, 0, sizeof(CERT));
  157. ret->key = &ret->pkeys[SSL_PKEY_RSA_ENC];
  158. return ret;
  159. }
  160. CERT *ssl_cert_dup(CERT *cert) {
  161. CERT *ret;
  162. int i;
  163. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  164. if (ret == NULL) {
  165. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  166. return NULL;
  167. }
  168. memset(ret, 0, sizeof(CERT));
  169. ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
  170. /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
  171. * more readable */
  172. ret->mask_k = cert->mask_k;
  173. ret->mask_a = cert->mask_a;
  174. if (cert->dh_tmp != NULL) {
  175. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  176. if (ret->dh_tmp == NULL) {
  177. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
  178. goto err;
  179. }
  180. if (cert->dh_tmp->priv_key) {
  181. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  182. if (!b) {
  183. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  184. goto err;
  185. }
  186. ret->dh_tmp->priv_key = b;
  187. }
  188. if (cert->dh_tmp->pub_key) {
  189. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  190. if (!b) {
  191. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  192. goto err;
  193. }
  194. ret->dh_tmp->pub_key = b;
  195. }
  196. }
  197. ret->dh_tmp_cb = cert->dh_tmp_cb;
  198. ret->ecdh_nid = cert->ecdh_nid;
  199. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  200. for (i = 0; i < SSL_PKEY_NUM; i++) {
  201. CERT_PKEY *cpk = cert->pkeys + i;
  202. CERT_PKEY *rpk = ret->pkeys + i;
  203. if (cpk->x509 != NULL) {
  204. rpk->x509 = X509_up_ref(cpk->x509);
  205. }
  206. if (cpk->privatekey != NULL) {
  207. rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
  208. }
  209. if (cpk->chain) {
  210. rpk->chain = X509_chain_up_ref(cpk->chain);
  211. if (!rpk->chain) {
  212. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. }
  216. }
  217. /* Copy over signature algorithm configuration. */
  218. if (cert->conf_sigalgs) {
  219. ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
  220. if (!ret->conf_sigalgs) {
  221. goto err;
  222. }
  223. ret->conf_sigalgslen = cert->conf_sigalgslen;
  224. }
  225. if (cert->client_sigalgs) {
  226. ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
  227. cert->client_sigalgslen);
  228. if (!ret->client_sigalgs) {
  229. goto err;
  230. }
  231. ret->client_sigalgslen = cert->client_sigalgslen;
  232. }
  233. /* Copy any custom client certificate types */
  234. if (cert->client_certificate_types) {
  235. ret->client_certificate_types = BUF_memdup(
  236. cert->client_certificate_types, cert->num_client_certificate_types);
  237. if (!ret->client_certificate_types) {
  238. goto err;
  239. }
  240. ret->num_client_certificate_types = cert->num_client_certificate_types;
  241. }
  242. ret->cert_cb = cert->cert_cb;
  243. ret->cert_cb_arg = cert->cert_cb_arg;
  244. if (cert->verify_store) {
  245. CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
  246. ret->verify_store = cert->verify_store;
  247. }
  248. if (cert->chain_store) {
  249. CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
  250. ret->chain_store = cert->chain_store;
  251. }
  252. return ret;
  253. err:
  254. ssl_cert_free(ret);
  255. return NULL;
  256. }
  257. /* Free up and clear all certificates and chains */
  258. void ssl_cert_clear_certs(CERT *c) {
  259. int i;
  260. if (c == NULL) {
  261. return;
  262. }
  263. for (i = 0; i < SSL_PKEY_NUM; i++) {
  264. CERT_PKEY *cpk = c->pkeys + i;
  265. if (cpk->x509) {
  266. X509_free(cpk->x509);
  267. cpk->x509 = NULL;
  268. }
  269. if (cpk->privatekey) {
  270. EVP_PKEY_free(cpk->privatekey);
  271. cpk->privatekey = NULL;
  272. }
  273. if (cpk->chain) {
  274. sk_X509_pop_free(cpk->chain, X509_free);
  275. cpk->chain = NULL;
  276. }
  277. }
  278. }
  279. void ssl_cert_free(CERT *c) {
  280. if (c == NULL) {
  281. return;
  282. }
  283. DH_free(c->dh_tmp);
  284. ssl_cert_clear_certs(c);
  285. OPENSSL_free(c->peer_sigalgs);
  286. OPENSSL_free(c->conf_sigalgs);
  287. OPENSSL_free(c->client_sigalgs);
  288. OPENSSL_free(c->shared_sigalgs);
  289. OPENSSL_free(c->client_certificate_types);
  290. X509_STORE_free(c->verify_store);
  291. X509_STORE_free(c->chain_store);
  292. OPENSSL_free(c->ciphers_raw);
  293. OPENSSL_free(c);
  294. }
  295. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) {
  296. CERT_PKEY *cpk = c->key;
  297. if (!cpk) {
  298. return 0;
  299. }
  300. sk_X509_pop_free(cpk->chain, X509_free);
  301. cpk->chain = chain;
  302. return 1;
  303. }
  304. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) {
  305. STACK_OF(X509) * dchain;
  306. if (!chain) {
  307. return ssl_cert_set0_chain(c, NULL);
  308. }
  309. dchain = X509_chain_up_ref(chain);
  310. if (!dchain) {
  311. return 0;
  312. }
  313. if (!ssl_cert_set0_chain(c, dchain)) {
  314. sk_X509_pop_free(dchain, X509_free);
  315. return 0;
  316. }
  317. return 1;
  318. }
  319. int ssl_cert_add0_chain_cert(CERT *c, X509 *x) {
  320. CERT_PKEY *cpk = c->key;
  321. if (!cpk) {
  322. return 0;
  323. }
  324. if (!cpk->chain) {
  325. cpk->chain = sk_X509_new_null();
  326. }
  327. if (!cpk->chain || !sk_X509_push(cpk->chain, x)) {
  328. return 0;
  329. }
  330. return 1;
  331. }
  332. int ssl_cert_add1_chain_cert(CERT *c, X509 *x) {
  333. if (!ssl_cert_add0_chain_cert(c, x)) {
  334. return 0;
  335. }
  336. X509_up_ref(x);
  337. return 1;
  338. }
  339. int ssl_cert_select_current(CERT *c, X509 *x) {
  340. int i;
  341. if (x == NULL) {
  342. return 0;
  343. }
  344. for (i = 0; i < SSL_PKEY_NUM; i++) {
  345. if (c->pkeys[i].x509 == x) {
  346. c->key = &c->pkeys[i];
  347. return 1;
  348. }
  349. }
  350. for (i = 0; i < SSL_PKEY_NUM; i++) {
  351. if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) {
  352. c->key = &c->pkeys[i];
  353. return 1;
  354. }
  355. }
  356. return 0;
  357. }
  358. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  359. c->cert_cb = cb;
  360. c->cert_cb_arg = arg;
  361. }
  362. SESS_CERT *ssl_sess_cert_new(void) {
  363. SESS_CERT *ret;
  364. ret = OPENSSL_malloc(sizeof *ret);
  365. if (ret == NULL) {
  366. OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
  367. return NULL;
  368. }
  369. memset(ret, 0, sizeof *ret);
  370. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  371. return ret;
  372. }
  373. void ssl_sess_cert_free(SESS_CERT *sc) {
  374. int i;
  375. if (sc == NULL) {
  376. return;
  377. }
  378. sk_X509_pop_free(sc->cert_chain, X509_free);
  379. for (i = 0; i < SSL_PKEY_NUM; i++) {
  380. X509_free(sc->peer_pkeys[i].x509);
  381. }
  382. DH_free(sc->peer_dh_tmp);
  383. EC_KEY_free(sc->peer_ecdh_tmp);
  384. OPENSSL_free(sc);
  385. }
  386. int ssl_set_peer_cert_type(SESS_CERT *sc, int type) {
  387. sc->peer_cert_type = type;
  388. return 1;
  389. }
  390. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) {
  391. X509 *x;
  392. int i;
  393. X509_STORE *verify_store;
  394. X509_STORE_CTX ctx;
  395. if (s->cert->verify_store) {
  396. verify_store = s->cert->verify_store;
  397. } else {
  398. verify_store = s->ctx->cert_store;
  399. }
  400. if (sk == NULL || sk_X509_num(sk) == 0) {
  401. return 0;
  402. }
  403. x = sk_X509_value(sk, 0);
  404. if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
  405. OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
  406. return 0;
  407. }
  408. X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
  409. /* We need to inherit the verify parameters. These can be determined by the
  410. * context: if its a server it will verify SSL client certificates or vice
  411. * versa. */
  412. X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
  413. /* Anything non-default in "param" should overwrite anything in the ctx. */
  414. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  415. if (s->verify_callback) {
  416. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  417. }
  418. if (s->ctx->app_verify_callback != NULL) {
  419. i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  420. } else {
  421. i = X509_verify_cert(&ctx);
  422. }
  423. s->verify_result = ctx.error;
  424. X509_STORE_CTX_cleanup(&ctx);
  425. return i;
  426. }
  427. static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list,
  428. STACK_OF(X509_NAME) * name_list) {
  429. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  430. *ca_list = name_list;
  431. }
  432. STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) {
  433. size_t i;
  434. STACK_OF(X509_NAME) * ret;
  435. X509_NAME *name;
  436. ret = sk_X509_NAME_new_null();
  437. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  438. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  439. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  440. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  441. return NULL;
  442. }
  443. }
  444. return ret;
  445. }
  446. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) {
  447. set_client_CA_list(&(s->client_CA), name_list);
  448. }
  449. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) {
  450. set_client_CA_list(&(ctx->client_CA), name_list);
  451. }
  452. STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  453. return ctx->client_CA;
  454. }
  455. STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) {
  456. if (s->server) {
  457. if (s->client_CA != NULL) {
  458. return s->client_CA;
  459. } else {
  460. return s->ctx->client_CA;
  461. }
  462. } else {
  463. if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
  464. return s->s3->tmp.ca_names;
  465. } else {
  466. return NULL;
  467. }
  468. }
  469. }
  470. static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) {
  471. X509_NAME *name;
  472. if (x == NULL) {
  473. return 0;
  474. }
  475. if (*sk == NULL) {
  476. *sk = sk_X509_NAME_new_null();
  477. if (*sk == NULL) {
  478. return 0;
  479. }
  480. }
  481. name = X509_NAME_dup(X509_get_subject_name(x));
  482. if (name == NULL) {
  483. return 0;
  484. }
  485. if (!sk_X509_NAME_push(*sk, name)) {
  486. X509_NAME_free(name);
  487. return 0;
  488. }
  489. return 1;
  490. }
  491. int SSL_add_client_CA(SSL *ssl, X509 *x) {
  492. return add_client_CA(&(ssl->client_CA), x);
  493. }
  494. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
  495. return add_client_CA(&(ctx->client_CA), x);
  496. }
  497. static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
  498. return X509_NAME_cmp(*a, *b);
  499. }
  500. /* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
  501. * it doesn't really have anything to do with clients (except that a common use
  502. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  503. * much to do with CAs, either, since it will load any old cert.
  504. *
  505. * \param file the file containing one or more certs.
  506. * \return a ::STACK containing the certs. */
  507. STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) {
  508. BIO *in;
  509. X509 *x = NULL;
  510. X509_NAME *xn = NULL;
  511. STACK_OF(X509_NAME) *ret = NULL, *sk;
  512. sk = sk_X509_NAME_new(xname_cmp);
  513. in = BIO_new(BIO_s_file());
  514. if (sk == NULL || in == NULL) {
  515. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  516. goto err;
  517. }
  518. if (!BIO_read_filename(in, file)) {
  519. goto err;
  520. }
  521. for (;;) {
  522. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  523. break;
  524. }
  525. if (ret == NULL) {
  526. ret = sk_X509_NAME_new_null();
  527. if (ret == NULL) {
  528. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  529. goto err;
  530. }
  531. }
  532. xn = X509_get_subject_name(x);
  533. if (xn == NULL) {
  534. goto err;
  535. }
  536. /* check for duplicates */
  537. xn = X509_NAME_dup(xn);
  538. if (xn == NULL) {
  539. goto err;
  540. }
  541. if (sk_X509_NAME_find(sk, NULL, xn)) {
  542. X509_NAME_free(xn);
  543. } else {
  544. sk_X509_NAME_push(sk, xn);
  545. sk_X509_NAME_push(ret, xn);
  546. }
  547. }
  548. if (0) {
  549. err:
  550. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  551. ret = NULL;
  552. }
  553. sk_X509_NAME_free(sk);
  554. BIO_free(in);
  555. X509_free(x);
  556. if (ret != NULL) {
  557. ERR_clear_error();
  558. }
  559. return ret;
  560. }
  561. /* Add a file of certs to a stack.
  562. *
  563. * \param stack the stack to add to.
  564. * \param file the file to add from. All certs in this file that are not
  565. * already in the stack will be added.
  566. * \return 1 for success, 0 for failure. Note that in the case of failure some
  567. * certs may have been added to \c stack. */
  568. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
  569. const char *file) {
  570. BIO *in;
  571. X509 *x = NULL;
  572. X509_NAME *xn = NULL;
  573. int ret = 1;
  574. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  575. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  576. in = BIO_new(BIO_s_file());
  577. if (in == NULL) {
  578. OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
  579. ERR_R_MALLOC_FAILURE);
  580. goto err;
  581. }
  582. if (!BIO_read_filename(in, file)) {
  583. goto err;
  584. }
  585. for (;;) {
  586. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  587. break;
  588. }
  589. xn = X509_get_subject_name(x);
  590. if (xn == NULL) {
  591. goto err;
  592. }
  593. xn = X509_NAME_dup(xn);
  594. if (xn == NULL) {
  595. goto err;
  596. }
  597. if (sk_X509_NAME_find(stack, NULL, xn)) {
  598. X509_NAME_free(xn);
  599. } else {
  600. sk_X509_NAME_push(stack, xn);
  601. }
  602. }
  603. ERR_clear_error();
  604. if (0) {
  605. err:
  606. ret = 0;
  607. }
  608. BIO_free(in);
  609. X509_free(x);
  610. (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
  611. return ret;
  612. }
  613. /* Add a directory of certs to a stack.
  614. *
  615. * \param stack the stack to append to.
  616. * \param dir the directory to append from. All files in this directory will be
  617. * examined as potential certs. Any that are acceptable to
  618. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
  619. * be included.
  620. * \return 1 for success, 0 for failure. Note that in the case of failure some
  621. * certs may have been added to \c stack. */
  622. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
  623. const char *dir) {
  624. OPENSSL_DIR_CTX *d = NULL;
  625. const char *filename;
  626. int ret = 0;
  627. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  628. /* Note that a side effect is that the CAs will be sorted by name */
  629. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  630. char buf[1024];
  631. int r;
  632. if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
  633. OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
  634. SSL_R_PATH_TOO_LONG);
  635. goto err;
  636. }
  637. r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
  638. if (r <= 0 || r >= (int)sizeof(buf) ||
  639. !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
  640. goto err;
  641. }
  642. }
  643. if (errno) {
  644. OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
  645. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  646. goto err;
  647. }
  648. ret = 1;
  649. err:
  650. if (d) {
  651. OPENSSL_DIR_end(&d);
  652. }
  653. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  654. return ret;
  655. }
  656. /* Add a certificate to a BUF_MEM structure */
  657. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
  658. int n;
  659. uint8_t *p;
  660. n = i2d_X509(x, NULL);
  661. if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  662. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
  663. return 0;
  664. }
  665. p = (uint8_t *)&(buf->data[*l]);
  666. l2n3(n, p);
  667. i2d_X509(x, &p);
  668. *l += n + 3;
  669. return 1;
  670. }
  671. /* Add certificate chain to internal SSL BUF_MEM structure. */
  672. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) {
  673. BUF_MEM *buf = s->init_buf;
  674. int no_chain = 0;
  675. size_t i;
  676. X509 *x = NULL;
  677. STACK_OF(X509) * extra_certs;
  678. X509_STORE *chain_store;
  679. if (cpk) {
  680. x = cpk->x509;
  681. }
  682. if (s->cert->chain_store) {
  683. chain_store = s->cert->chain_store;
  684. } else {
  685. chain_store = s->ctx->cert_store;
  686. }
  687. /* If we have a certificate specific chain use it, else use parent ctx. */
  688. if (cpk && cpk->chain) {
  689. extra_certs = cpk->chain;
  690. } else {
  691. extra_certs = s->ctx->extra_certs;
  692. }
  693. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) {
  694. no_chain = 1;
  695. }
  696. /* TLSv1 sends a chain with nothing in it, instead of an alert. */
  697. if (!BUF_MEM_grow_clean(buf, 10)) {
  698. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
  699. return 0;
  700. }
  701. if (x != NULL) {
  702. if (no_chain) {
  703. if (!ssl_add_cert_to_buf(buf, l, x)) {
  704. return 0;
  705. }
  706. } else {
  707. X509_STORE_CTX xs_ctx;
  708. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
  709. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
  710. return 0;
  711. }
  712. X509_verify_cert(&xs_ctx);
  713. /* Don't leave errors in the queue */
  714. ERR_clear_error();
  715. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  716. x = sk_X509_value(xs_ctx.chain, i);
  717. if (!ssl_add_cert_to_buf(buf, l, x)) {
  718. X509_STORE_CTX_cleanup(&xs_ctx);
  719. return 0;
  720. }
  721. }
  722. X509_STORE_CTX_cleanup(&xs_ctx);
  723. }
  724. }
  725. for (i = 0; i < sk_X509_num(extra_certs); i++) {
  726. x = sk_X509_value(extra_certs, i);
  727. if (!ssl_add_cert_to_buf(buf, l, x)) {
  728. return 0;
  729. }
  730. }
  731. return 1;
  732. }
  733. /* Build a certificate chain for current certificate */
  734. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) {
  735. CERT_PKEY *cpk = c->key;
  736. X509_STORE_CTX xs_ctx;
  737. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  738. X509 *x;
  739. int i, rv = 0;
  740. unsigned long error;
  741. if (!cpk->x509) {
  742. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
  743. goto err;
  744. }
  745. /* Rearranging and check the chain: add everything to a store */
  746. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  747. size_t j;
  748. chain_store = X509_STORE_new();
  749. if (!chain_store) {
  750. goto err;
  751. }
  752. for (j = 0; j < sk_X509_num(cpk->chain); j++) {
  753. x = sk_X509_value(cpk->chain, j);
  754. if (!X509_STORE_add_cert(chain_store, x)) {
  755. error = ERR_peek_last_error();
  756. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  757. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  758. goto err;
  759. }
  760. ERR_clear_error();
  761. }
  762. }
  763. /* Add EE cert too: it might be self signed */
  764. if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
  765. error = ERR_peek_last_error();
  766. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  767. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  768. goto err;
  769. }
  770. ERR_clear_error();
  771. }
  772. } else {
  773. if (c->chain_store) {
  774. chain_store = c->chain_store;
  775. }
  776. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
  777. untrusted = cpk->chain;
  778. }
  779. }
  780. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
  781. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
  782. goto err;
  783. }
  784. i = X509_verify_cert(&xs_ctx);
  785. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  786. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
  787. ERR_clear_error();
  788. }
  789. i = 1;
  790. rv = 2;
  791. }
  792. if (i > 0) {
  793. chain = X509_STORE_CTX_get1_chain(&xs_ctx);
  794. }
  795. if (i <= 0) {
  796. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
  797. SSL_R_CERTIFICATE_VERIFY_FAILED);
  798. i = X509_STORE_CTX_get_error(&xs_ctx);
  799. ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
  800. X509_STORE_CTX_cleanup(&xs_ctx);
  801. goto err;
  802. }
  803. X509_STORE_CTX_cleanup(&xs_ctx);
  804. if (cpk->chain) {
  805. sk_X509_pop_free(cpk->chain, X509_free);
  806. }
  807. /* Remove EE certificate from chain */
  808. x = sk_X509_shift(chain);
  809. X509_free(x);
  810. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  811. if (sk_X509_num(chain) > 0) {
  812. /* See if last cert is self signed */
  813. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  814. X509_check_purpose(x, -1, 0);
  815. if (x->ex_flags & EXFLAG_SS) {
  816. x = sk_X509_pop(chain);
  817. X509_free(x);
  818. }
  819. }
  820. }
  821. cpk->chain = chain;
  822. if (rv == 0) {
  823. rv = 1;
  824. }
  825. err:
  826. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  827. X509_STORE_free(chain_store);
  828. }
  829. return rv;
  830. }
  831. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
  832. X509_STORE **pstore;
  833. if (chain) {
  834. pstore = &c->chain_store;
  835. } else {
  836. pstore = &c->verify_store;
  837. }
  838. X509_STORE_free(*pstore);
  839. *pstore = store;
  840. if (ref && store) {
  841. CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
  842. }
  843. return 1;
  844. }