Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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