25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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