You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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