Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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