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.
 
 
 
 
 
 

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