Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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