Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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