ssl_cert.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * ! \file ssl/ssl_cert.c
  3. */
  4. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  5. * All rights reserved.
  6. *
  7. * This package is an SSL implementation written
  8. * by Eric Young (eay@cryptsoft.com).
  9. * The implementation was written so as to conform with Netscapes SSL.
  10. *
  11. * This library is free for commercial and non-commercial use as long as
  12. * the following conditions are aheared to. The following conditions
  13. * apply to all code found in this distribution, be it the RC4, RSA,
  14. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  15. * included with this distribution is covered by the same copyright terms
  16. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  17. *
  18. * Copyright remains Eric Young's, and as such any Copyright notices in
  19. * the code are not to be removed.
  20. * If this package is used in a product, Eric Young should be given attribution
  21. * as the author of the parts of the library used.
  22. * This can be in the form of a textual message at program startup or
  23. * in documentation (online or textual) provided with the package.
  24. *
  25. * Redistribution and use in source and binary forms, with or without
  26. * modification, are permitted provided that the following conditions
  27. * are met:
  28. * 1. Redistributions of source code must retain the copyright
  29. * notice, this list of conditions and the following disclaimer.
  30. * 2. Redistributions in binary form must reproduce the above copyright
  31. * notice, this list of conditions and the following disclaimer in the
  32. * documentation and/or other materials provided with the distribution.
  33. * 3. All advertising materials mentioning features or use of this software
  34. * must display the following acknowledgement:
  35. * "This product includes cryptographic software written by
  36. * Eric Young (eay@cryptsoft.com)"
  37. * The word 'cryptographic' can be left out if the rouines from the library
  38. * being used are not cryptographic related :-).
  39. * 4. If you include any Windows specific code (or a derivative thereof) from
  40. * the apps directory (application code) you must include an acknowledgement:
  41. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  42. *
  43. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  44. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  45. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  46. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  47. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  48. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  49. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  50. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  51. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  52. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  53. * SUCH DAMAGE.
  54. *
  55. * The licence and distribution terms for any publically available version or
  56. * derivative of this code cannot be changed. i.e. this code cannot simply be
  57. * copied and put under another distribution licence
  58. * [including the GNU Public Licence.]
  59. */
  60. /* ====================================================================
  61. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  62. *
  63. * Redistribution and use in source and binary forms, with or without
  64. * modification, are permitted provided that the following conditions
  65. * are met:
  66. *
  67. * 1. Redistributions of source code must retain the above copyright
  68. * notice, this list of conditions and the following disclaimer.
  69. *
  70. * 2. Redistributions in binary form must reproduce the above copyright
  71. * notice, this list of conditions and the following disclaimer in
  72. * the documentation and/or other materials provided with the
  73. * distribution.
  74. *
  75. * 3. All advertising materials mentioning features or use of this
  76. * software must display the following acknowledgment:
  77. * "This product includes software developed by the OpenSSL Project
  78. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  79. *
  80. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  81. * endorse or promote products derived from this software without
  82. * prior written permission. For written permission, please contact
  83. * openssl-core@openssl.org.
  84. *
  85. * 5. Products derived from this software may not be called "OpenSSL"
  86. * nor may "OpenSSL" appear in their names without prior written
  87. * permission of the OpenSSL Project.
  88. *
  89. * 6. Redistributions of any form whatsoever must retain the following
  90. * acknowledgment:
  91. * "This product includes software developed by the OpenSSL Project
  92. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  93. *
  94. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  95. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  96. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  97. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  98. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  99. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  100. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  101. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  102. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  103. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  104. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  105. * OF THE POSSIBILITY OF SUCH DAMAGE.
  106. * ====================================================================
  107. *
  108. * This product includes cryptographic software written by Eric Young
  109. * (eay@cryptsoft.com). This product includes software written by Tim
  110. * Hudson (tjh@cryptsoft.com).
  111. *
  112. */
  113. /* ====================================================================
  114. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  115. * ECC cipher suite support in OpenSSL originally developed by
  116. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  117. */
  118. #include <stdio.h>
  119. #include "e_os.h"
  120. #ifndef NO_SYS_TYPES_H
  121. # include <sys/types.h>
  122. #endif
  123. #include "o_dir.h"
  124. #include <openssl/objects.h>
  125. #include <openssl/bio.h>
  126. #include <openssl/pem.h>
  127. #include <openssl/x509v3.h>
  128. #ifndef OPENSSL_NO_DH
  129. # include <openssl/dh.h>
  130. #endif
  131. #include <openssl/bn.h>
  132. #include "ssl_locl.h"
  133. int SSL_get_ex_data_X509_STORE_CTX_idx(void)
  134. {
  135. static volatile int ssl_x509_store_ctx_idx = -1;
  136. int got_write_lock = 0;
  137. if (((size_t)&ssl_x509_store_ctx_idx &
  138. (sizeof(ssl_x509_store_ctx_idx) - 1))
  139. == 0) { /* check alignment, practically always true */
  140. int ret;
  141. if ((ret = ssl_x509_store_ctx_idx) < 0) {
  142. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  143. if ((ret = ssl_x509_store_ctx_idx) < 0) {
  144. ret = ssl_x509_store_ctx_idx =
  145. X509_STORE_CTX_get_ex_new_index(0,
  146. "SSL for verify callback",
  147. NULL, NULL, NULL);
  148. }
  149. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  150. }
  151. return ret;
  152. } else { /* commonly eliminated */
  153. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  154. if (ssl_x509_store_ctx_idx < 0) {
  155. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  156. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  157. got_write_lock = 1;
  158. if (ssl_x509_store_ctx_idx < 0) {
  159. ssl_x509_store_ctx_idx =
  160. X509_STORE_CTX_get_ex_new_index(0,
  161. "SSL for verify callback",
  162. NULL, NULL, NULL);
  163. }
  164. }
  165. if (got_write_lock)
  166. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  167. else
  168. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  169. return ssl_x509_store_ctx_idx;
  170. }
  171. }
  172. void ssl_cert_set_default_md(CERT *cert)
  173. {
  174. /* Set digest values to defaults */
  175. #ifndef OPENSSL_NO_DSA
  176. cert->pkeys[SSL_PKEY_DSA_SIGN].digest = EVP_sha1();
  177. #endif
  178. #ifndef OPENSSL_NO_RSA
  179. cert->pkeys[SSL_PKEY_RSA_SIGN].digest = EVP_sha1();
  180. cert->pkeys[SSL_PKEY_RSA_ENC].digest = EVP_sha1();
  181. #endif
  182. #ifndef OPENSSL_NO_ECDSA
  183. cert->pkeys[SSL_PKEY_ECC].digest = EVP_sha1();
  184. #endif
  185. }
  186. CERT *ssl_cert_new(void)
  187. {
  188. CERT *ret;
  189. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  190. if (ret == NULL) {
  191. SSLerr(SSL_F_SSL_CERT_NEW, ERR_R_MALLOC_FAILURE);
  192. return (NULL);
  193. }
  194. memset(ret, 0, sizeof(CERT));
  195. ret->key = &(ret->pkeys[SSL_PKEY_RSA_ENC]);
  196. ret->references = 1;
  197. ssl_cert_set_default_md(ret);
  198. return (ret);
  199. }
  200. CERT *ssl_cert_dup(CERT *cert)
  201. {
  202. CERT *ret;
  203. int i;
  204. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  205. if (ret == NULL) {
  206. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
  207. return (NULL);
  208. }
  209. memset(ret, 0, sizeof(CERT));
  210. ret->references = 1;
  211. ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
  212. /*
  213. * or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
  214. * more readable
  215. */
  216. ret->valid = cert->valid;
  217. ret->mask_k = cert->mask_k;
  218. ret->mask_a = cert->mask_a;
  219. ret->export_mask_k = cert->export_mask_k;
  220. ret->export_mask_a = cert->export_mask_a;
  221. #ifndef OPENSSL_NO_RSA
  222. if (cert->rsa_tmp != NULL) {
  223. RSA_up_ref(cert->rsa_tmp);
  224. ret->rsa_tmp = cert->rsa_tmp;
  225. }
  226. ret->rsa_tmp_cb = cert->rsa_tmp_cb;
  227. #endif
  228. #ifndef OPENSSL_NO_DH
  229. if (cert->dh_tmp != NULL) {
  230. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  231. if (ret->dh_tmp == NULL) {
  232. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_DH_LIB);
  233. goto err;
  234. }
  235. if (cert->dh_tmp->priv_key) {
  236. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  237. if (!b) {
  238. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
  239. goto err;
  240. }
  241. ret->dh_tmp->priv_key = b;
  242. }
  243. if (cert->dh_tmp->pub_key) {
  244. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  245. if (!b) {
  246. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_BN_LIB);
  247. goto err;
  248. }
  249. ret->dh_tmp->pub_key = b;
  250. }
  251. }
  252. ret->dh_tmp_cb = cert->dh_tmp_cb;
  253. #endif
  254. #ifndef OPENSSL_NO_ECDH
  255. if (cert->ecdh_tmp) {
  256. ret->ecdh_tmp = EC_KEY_dup(cert->ecdh_tmp);
  257. if (ret->ecdh_tmp == NULL) {
  258. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_EC_LIB);
  259. goto err;
  260. }
  261. }
  262. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  263. ret->ecdh_tmp_auto = cert->ecdh_tmp_auto;
  264. #endif
  265. for (i = 0; i < SSL_PKEY_NUM; i++) {
  266. CERT_PKEY *cpk = cert->pkeys + i;
  267. CERT_PKEY *rpk = ret->pkeys + i;
  268. if (cpk->x509 != NULL) {
  269. rpk->x509 = cpk->x509;
  270. CRYPTO_add(&rpk->x509->references, 1, CRYPTO_LOCK_X509);
  271. }
  272. if (cpk->privatekey != NULL) {
  273. rpk->privatekey = cpk->privatekey;
  274. CRYPTO_add(&cpk->privatekey->references, 1, CRYPTO_LOCK_EVP_PKEY);
  275. }
  276. if (cpk->chain) {
  277. rpk->chain = X509_chain_up_ref(cpk->chain);
  278. if (!rpk->chain) {
  279. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
  280. goto err;
  281. }
  282. }
  283. rpk->valid_flags = 0;
  284. #ifndef OPENSSL_NO_TLSEXT
  285. if (cert->pkeys[i].serverinfo != NULL) {
  286. /* Just copy everything. */
  287. ret->pkeys[i].serverinfo =
  288. OPENSSL_malloc(cert->pkeys[i].serverinfo_length);
  289. if (ret->pkeys[i].serverinfo == NULL) {
  290. SSLerr(SSL_F_SSL_CERT_DUP, ERR_R_MALLOC_FAILURE);
  291. goto err;
  292. }
  293. ret->pkeys[i].serverinfo_length =
  294. cert->pkeys[i].serverinfo_length;
  295. memcpy(ret->pkeys[i].serverinfo,
  296. cert->pkeys[i].serverinfo,
  297. cert->pkeys[i].serverinfo_length);
  298. }
  299. #endif
  300. }
  301. /*
  302. * Set digests to defaults. NB: we don't copy existing values as they
  303. * will be set during handshake.
  304. */
  305. ssl_cert_set_default_md(ret);
  306. /* Peer sigalgs set to NULL as we get these from handshake too */
  307. ret->peer_sigalgs = NULL;
  308. ret->peer_sigalgslen = 0;
  309. /* Configured sigalgs however we copy across */
  310. if (cert->conf_sigalgs) {
  311. ret->conf_sigalgs = OPENSSL_malloc(cert->conf_sigalgslen);
  312. if (!ret->conf_sigalgs)
  313. goto err;
  314. memcpy(ret->conf_sigalgs, cert->conf_sigalgs, cert->conf_sigalgslen);
  315. ret->conf_sigalgslen = cert->conf_sigalgslen;
  316. } else
  317. ret->conf_sigalgs = NULL;
  318. if (cert->client_sigalgs) {
  319. ret->client_sigalgs = OPENSSL_malloc(cert->client_sigalgslen);
  320. if (!ret->client_sigalgs)
  321. goto err;
  322. memcpy(ret->client_sigalgs, cert->client_sigalgs,
  323. cert->client_sigalgslen);
  324. ret->client_sigalgslen = cert->client_sigalgslen;
  325. } else
  326. ret->client_sigalgs = NULL;
  327. /* Shared sigalgs also NULL */
  328. ret->shared_sigalgs = NULL;
  329. /* Copy any custom client certificate types */
  330. if (cert->ctypes) {
  331. ret->ctypes = OPENSSL_malloc(cert->ctype_num);
  332. if (!ret->ctypes)
  333. goto err;
  334. memcpy(ret->ctypes, cert->ctypes, cert->ctype_num);
  335. ret->ctype_num = cert->ctype_num;
  336. }
  337. ret->cert_flags = cert->cert_flags;
  338. ret->cert_cb = cert->cert_cb;
  339. ret->cert_cb_arg = cert->cert_cb_arg;
  340. if (cert->verify_store) {
  341. CRYPTO_add(&cert->verify_store->references, 1,
  342. CRYPTO_LOCK_X509_STORE);
  343. ret->verify_store = cert->verify_store;
  344. }
  345. if (cert->chain_store) {
  346. CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
  347. ret->chain_store = cert->chain_store;
  348. }
  349. ret->ciphers_raw = NULL;
  350. #ifndef OPENSSL_NO_TLSEXT
  351. if (!custom_exts_copy(&ret->cli_ext, &cert->cli_ext))
  352. goto err;
  353. if (!custom_exts_copy(&ret->srv_ext, &cert->srv_ext))
  354. goto err;
  355. #endif
  356. return (ret);
  357. err:
  358. #ifndef OPENSSL_NO_RSA
  359. if (ret->rsa_tmp != NULL)
  360. RSA_free(ret->rsa_tmp);
  361. #endif
  362. #ifndef OPENSSL_NO_DH
  363. if (ret->dh_tmp != NULL)
  364. DH_free(ret->dh_tmp);
  365. #endif
  366. #ifndef OPENSSL_NO_ECDH
  367. if (ret->ecdh_tmp != NULL)
  368. EC_KEY_free(ret->ecdh_tmp);
  369. #endif
  370. #ifndef OPENSSL_NO_TLSEXT
  371. custom_exts_free(&ret->cli_ext);
  372. custom_exts_free(&ret->srv_ext);
  373. #endif
  374. ssl_cert_clear_certs(ret);
  375. OPENSSL_free(ret);
  376. return NULL;
  377. }
  378. /* Free up and clear all certificates and chains */
  379. void ssl_cert_clear_certs(CERT *c)
  380. {
  381. int i;
  382. if (c == NULL)
  383. return;
  384. for (i = 0; i < SSL_PKEY_NUM; i++) {
  385. CERT_PKEY *cpk = c->pkeys + i;
  386. if (cpk->x509) {
  387. X509_free(cpk->x509);
  388. cpk->x509 = NULL;
  389. }
  390. if (cpk->privatekey) {
  391. EVP_PKEY_free(cpk->privatekey);
  392. cpk->privatekey = NULL;
  393. }
  394. if (cpk->chain) {
  395. sk_X509_pop_free(cpk->chain, X509_free);
  396. cpk->chain = NULL;
  397. }
  398. #ifndef OPENSSL_NO_TLSEXT
  399. if (cpk->serverinfo) {
  400. OPENSSL_free(cpk->serverinfo);
  401. cpk->serverinfo = NULL;
  402. cpk->serverinfo_length = 0;
  403. }
  404. #endif
  405. /* Clear all flags apart from explicit sign */
  406. cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
  407. }
  408. }
  409. void ssl_cert_free(CERT *c)
  410. {
  411. int i;
  412. if (c == NULL)
  413. return;
  414. i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT);
  415. #ifdef REF_PRINT
  416. REF_PRINT("CERT", c);
  417. #endif
  418. if (i > 0)
  419. return;
  420. #ifdef REF_CHECK
  421. if (i < 0) {
  422. fprintf(stderr, "ssl_cert_free, bad reference count\n");
  423. abort(); /* ok */
  424. }
  425. #endif
  426. #ifndef OPENSSL_NO_RSA
  427. if (c->rsa_tmp)
  428. RSA_free(c->rsa_tmp);
  429. #endif
  430. #ifndef OPENSSL_NO_DH
  431. if (c->dh_tmp)
  432. DH_free(c->dh_tmp);
  433. #endif
  434. #ifndef OPENSSL_NO_ECDH
  435. if (c->ecdh_tmp)
  436. EC_KEY_free(c->ecdh_tmp);
  437. #endif
  438. ssl_cert_clear_certs(c);
  439. if (c->peer_sigalgs)
  440. OPENSSL_free(c->peer_sigalgs);
  441. if (c->conf_sigalgs)
  442. OPENSSL_free(c->conf_sigalgs);
  443. if (c->client_sigalgs)
  444. OPENSSL_free(c->client_sigalgs);
  445. if (c->shared_sigalgs)
  446. OPENSSL_free(c->shared_sigalgs);
  447. if (c->ctypes)
  448. OPENSSL_free(c->ctypes);
  449. if (c->verify_store)
  450. X509_STORE_free(c->verify_store);
  451. if (c->chain_store)
  452. X509_STORE_free(c->chain_store);
  453. if (c->ciphers_raw)
  454. OPENSSL_free(c->ciphers_raw);
  455. #ifndef OPENSSL_NO_TLSEXT
  456. custom_exts_free(&c->cli_ext);
  457. custom_exts_free(&c->srv_ext);
  458. if (c->alpn_proposed)
  459. OPENSSL_free(c->alpn_proposed);
  460. #endif
  461. OPENSSL_free(c);
  462. }
  463. int ssl_cert_inst(CERT **o)
  464. {
  465. /*
  466. * Create a CERT if there isn't already one (which cannot really happen,
  467. * as it is initially created in SSL_CTX_new; but the earlier code
  468. * usually allows for that one being non-existant, so we follow that
  469. * behaviour, as it might turn out that there actually is a reason for it
  470. * -- but I'm not sure that *all* of the existing code could cope with
  471. * s->cert being NULL, otherwise we could do without the initialization
  472. * in SSL_CTX_new).
  473. */
  474. if (o == NULL) {
  475. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
  476. return (0);
  477. }
  478. if (*o == NULL) {
  479. if ((*o = ssl_cert_new()) == NULL) {
  480. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
  481. return (0);
  482. }
  483. }
  484. return (1);
  485. }
  486. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
  487. {
  488. CERT_PKEY *cpk = c->key;
  489. if (!cpk)
  490. return 0;
  491. if (cpk->chain)
  492. sk_X509_pop_free(cpk->chain, X509_free);
  493. cpk->chain = chain;
  494. return 1;
  495. }
  496. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
  497. {
  498. STACK_OF(X509) *dchain;
  499. if (!chain)
  500. return ssl_cert_set0_chain(c, NULL);
  501. dchain = X509_chain_up_ref(chain);
  502. if (!dchain)
  503. return 0;
  504. if (!ssl_cert_set0_chain(c, dchain)) {
  505. sk_X509_pop_free(dchain, X509_free);
  506. return 0;
  507. }
  508. return 1;
  509. }
  510. int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
  511. {
  512. CERT_PKEY *cpk = c->key;
  513. if (!cpk)
  514. return 0;
  515. if (!cpk->chain)
  516. cpk->chain = sk_X509_new_null();
  517. if (!cpk->chain || !sk_X509_push(cpk->chain, x))
  518. return 0;
  519. return 1;
  520. }
  521. int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
  522. {
  523. if (!ssl_cert_add0_chain_cert(c, x))
  524. return 0;
  525. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  526. return 1;
  527. }
  528. int ssl_cert_select_current(CERT *c, X509 *x)
  529. {
  530. int i;
  531. if (x == NULL)
  532. return 0;
  533. for (i = 0; i < SSL_PKEY_NUM; i++) {
  534. CERT_PKEY *cpk = c->pkeys + i;
  535. if (cpk->x509 == x && cpk->privatekey) {
  536. c->key = cpk;
  537. return 1;
  538. }
  539. }
  540. for (i = 0; i < SSL_PKEY_NUM; i++) {
  541. CERT_PKEY *cpk = c->pkeys + i;
  542. if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
  543. c->key = cpk;
  544. return 1;
  545. }
  546. }
  547. return 0;
  548. }
  549. int ssl_cert_set_current(CERT *c, long op)
  550. {
  551. int i, idx;
  552. if (!c)
  553. return 0;
  554. if (op == SSL_CERT_SET_FIRST)
  555. idx = 0;
  556. else if (op == SSL_CERT_SET_NEXT) {
  557. idx = (int)(c->key - c->pkeys + 1);
  558. if (idx >= SSL_PKEY_NUM)
  559. return 0;
  560. } else
  561. return 0;
  562. for (i = idx; i < SSL_PKEY_NUM; i++) {
  563. CERT_PKEY *cpk = c->pkeys + i;
  564. if (cpk->x509 && cpk->privatekey) {
  565. c->key = cpk;
  566. return 1;
  567. }
  568. }
  569. return 0;
  570. }
  571. void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  572. {
  573. c->cert_cb = cb;
  574. c->cert_cb_arg = arg;
  575. }
  576. SESS_CERT *ssl_sess_cert_new(void)
  577. {
  578. SESS_CERT *ret;
  579. ret = OPENSSL_malloc(sizeof *ret);
  580. if (ret == NULL) {
  581. SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
  582. return NULL;
  583. }
  584. memset(ret, 0, sizeof *ret);
  585. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  586. ret->references = 1;
  587. return ret;
  588. }
  589. void ssl_sess_cert_free(SESS_CERT *sc)
  590. {
  591. int i;
  592. if (sc == NULL)
  593. return;
  594. i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
  595. #ifdef REF_PRINT
  596. REF_PRINT("SESS_CERT", sc);
  597. #endif
  598. if (i > 0)
  599. return;
  600. #ifdef REF_CHECK
  601. if (i < 0) {
  602. fprintf(stderr, "ssl_sess_cert_free, bad reference count\n");
  603. abort(); /* ok */
  604. }
  605. #endif
  606. /* i == 0 */
  607. if (sc->cert_chain != NULL)
  608. sk_X509_pop_free(sc->cert_chain, X509_free);
  609. for (i = 0; i < SSL_PKEY_NUM; i++) {
  610. if (sc->peer_pkeys[i].x509 != NULL)
  611. X509_free(sc->peer_pkeys[i].x509);
  612. #if 0 /* We don't have the peer's private key.
  613. * These lines are just * here as a reminder
  614. * that we're still using a
  615. * not-quite-appropriate * data structure. */
  616. if (sc->peer_pkeys[i].privatekey != NULL)
  617. EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
  618. #endif
  619. }
  620. #ifndef OPENSSL_NO_RSA
  621. if (sc->peer_rsa_tmp != NULL)
  622. RSA_free(sc->peer_rsa_tmp);
  623. #endif
  624. #ifndef OPENSSL_NO_DH
  625. if (sc->peer_dh_tmp != NULL)
  626. DH_free(sc->peer_dh_tmp);
  627. #endif
  628. #ifndef OPENSSL_NO_ECDH
  629. if (sc->peer_ecdh_tmp != NULL)
  630. EC_KEY_free(sc->peer_ecdh_tmp);
  631. #endif
  632. OPENSSL_free(sc);
  633. }
  634. int ssl_set_peer_cert_type(SESS_CERT *sc, int type)
  635. {
  636. sc->peer_cert_type = type;
  637. return (1);
  638. }
  639. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
  640. {
  641. X509 *x;
  642. int i;
  643. X509_STORE *verify_store;
  644. X509_STORE_CTX ctx;
  645. if (s->cert->verify_store)
  646. verify_store = s->cert->verify_store;
  647. else
  648. verify_store = s->ctx->cert_store;
  649. if ((sk == NULL) || (sk_X509_num(sk) == 0))
  650. return (0);
  651. x = sk_X509_value(sk, 0);
  652. if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
  653. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
  654. return (0);
  655. }
  656. /* Set suite B flags if needed */
  657. X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
  658. #if 0
  659. if (SSL_get_verify_depth(s) >= 0)
  660. X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
  661. #endif
  662. X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
  663. /*
  664. * We need to inherit the verify parameters. These can be determined by
  665. * the context: if its a server it will verify SSL client certificates or
  666. * vice versa.
  667. */
  668. X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
  669. /*
  670. * Anything non-default in "param" should overwrite anything in the ctx.
  671. */
  672. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  673. if (s->verify_callback)
  674. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  675. if (s->ctx->app_verify_callback != NULL)
  676. #if 1 /* new with OpenSSL 0.9.7 */
  677. i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  678. #else
  679. i = s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
  680. #endif
  681. else {
  682. #ifndef OPENSSL_NO_X509_VERIFY
  683. i = X509_verify_cert(&ctx);
  684. #else
  685. i = 0;
  686. ctx.error = X509_V_ERR_APPLICATION_VERIFICATION;
  687. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_NO_VERIFY_CALLBACK);
  688. #endif
  689. }
  690. s->verify_result = ctx.error;
  691. X509_STORE_CTX_cleanup(&ctx);
  692. return (i);
  693. }
  694. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  695. STACK_OF(X509_NAME) *name_list)
  696. {
  697. if (*ca_list != NULL)
  698. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  699. *ca_list = name_list;
  700. }
  701. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
  702. {
  703. int i;
  704. STACK_OF(X509_NAME) *ret;
  705. X509_NAME *name;
  706. ret = sk_X509_NAME_new_null();
  707. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  708. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  709. if ((name == NULL) || !sk_X509_NAME_push(ret, name)) {
  710. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  711. return (NULL);
  712. }
  713. }
  714. return (ret);
  715. }
  716. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
  717. {
  718. set_client_CA_list(&(s->client_CA), name_list);
  719. }
  720. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
  721. {
  722. set_client_CA_list(&(ctx->client_CA), name_list);
  723. }
  724. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
  725. {
  726. return (ctx->client_CA);
  727. }
  728. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  729. {
  730. if (s->type == SSL_ST_CONNECT) { /* we are in the client */
  731. if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL))
  732. return (s->s3->tmp.ca_names);
  733. else
  734. return (NULL);
  735. } else {
  736. if (s->client_CA != NULL)
  737. return (s->client_CA);
  738. else
  739. return (s->ctx->client_CA);
  740. }
  741. }
  742. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x)
  743. {
  744. X509_NAME *name;
  745. if (x == NULL)
  746. return (0);
  747. if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL))
  748. return (0);
  749. if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
  750. return (0);
  751. if (!sk_X509_NAME_push(*sk, name)) {
  752. X509_NAME_free(name);
  753. return (0);
  754. }
  755. return (1);
  756. }
  757. int SSL_add_client_CA(SSL *ssl, X509 *x)
  758. {
  759. return (add_client_CA(&(ssl->client_CA), x));
  760. }
  761. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  762. {
  763. return (add_client_CA(&(ctx->client_CA), x));
  764. }
  765. static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  766. {
  767. return (X509_NAME_cmp(*a, *b));
  768. }
  769. #ifndef OPENSSL_NO_STDIO
  770. /**
  771. * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
  772. * it doesn't really have anything to do with clients (except that a common use
  773. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  774. * much to do with CAs, either, since it will load any old cert.
  775. * \param file the file containing one or more certs.
  776. * \return a ::STACK containing the certs.
  777. */
  778. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  779. {
  780. BIO *in;
  781. X509 *x = NULL;
  782. X509_NAME *xn = NULL;
  783. STACK_OF(X509_NAME) *ret = NULL, *sk;
  784. sk = sk_X509_NAME_new(xname_cmp);
  785. in = BIO_new(BIO_s_file_internal());
  786. if ((sk == NULL) || (in == NULL)) {
  787. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  788. goto err;
  789. }
  790. if (!BIO_read_filename(in, file))
  791. goto err;
  792. for (;;) {
  793. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  794. break;
  795. if (ret == NULL) {
  796. ret = sk_X509_NAME_new_null();
  797. if (ret == NULL) {
  798. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  799. goto err;
  800. }
  801. }
  802. if ((xn = X509_get_subject_name(x)) == NULL)
  803. goto err;
  804. /* check for duplicates */
  805. xn = X509_NAME_dup(xn);
  806. if (xn == NULL)
  807. goto err;
  808. if (sk_X509_NAME_find(sk, xn) >= 0)
  809. X509_NAME_free(xn);
  810. else {
  811. sk_X509_NAME_push(sk, xn);
  812. sk_X509_NAME_push(ret, xn);
  813. }
  814. }
  815. if (0) {
  816. err:
  817. if (ret != NULL)
  818. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  819. ret = NULL;
  820. }
  821. if (sk != NULL)
  822. sk_X509_NAME_free(sk);
  823. if (in != NULL)
  824. BIO_free(in);
  825. if (x != NULL)
  826. X509_free(x);
  827. if (ret != NULL)
  828. ERR_clear_error();
  829. return (ret);
  830. }
  831. #endif
  832. /**
  833. * Add a file of certs to a stack.
  834. * \param stack the stack to add to.
  835. * \param file the file to add from. All certs in this file that are not
  836. * already in the stack will be added.
  837. * \return 1 for success, 0 for failure. Note that in the case of failure some
  838. * certs may have been added to \c stack.
  839. */
  840. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  841. const char *file)
  842. {
  843. BIO *in;
  844. X509 *x = NULL;
  845. X509_NAME *xn = NULL;
  846. int ret = 1;
  847. int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
  848. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  849. in = BIO_new(BIO_s_file_internal());
  850. if (in == NULL) {
  851. SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,
  852. ERR_R_MALLOC_FAILURE);
  853. goto err;
  854. }
  855. if (!BIO_read_filename(in, file))
  856. goto err;
  857. for (;;) {
  858. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  859. break;
  860. if ((xn = X509_get_subject_name(x)) == NULL)
  861. goto err;
  862. xn = X509_NAME_dup(xn);
  863. if (xn == NULL)
  864. goto err;
  865. if (sk_X509_NAME_find(stack, xn) >= 0)
  866. X509_NAME_free(xn);
  867. else
  868. sk_X509_NAME_push(stack, xn);
  869. }
  870. ERR_clear_error();
  871. if (0) {
  872. err:
  873. ret = 0;
  874. }
  875. if (in != NULL)
  876. BIO_free(in);
  877. if (x != NULL)
  878. X509_free(x);
  879. (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
  880. return ret;
  881. }
  882. /**
  883. * Add a directory of certs to a stack.
  884. * \param stack the stack to append to.
  885. * \param dir the directory to append from. All files in this directory will be
  886. * examined as potential certs. Any that are acceptable to
  887. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
  888. * included.
  889. * \return 1 for success, 0 for failure. Note that in the case of failure some
  890. * certs may have been added to \c stack.
  891. */
  892. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  893. const char *dir)
  894. {
  895. OPENSSL_DIR_CTX *d = NULL;
  896. const char *filename;
  897. int ret = 0;
  898. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  899. /* Note that a side effect is that the CAs will be sorted by name */
  900. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  901. char buf[1024];
  902. int r;
  903. if (strlen(dir) + strlen(filename) + 2 > sizeof buf) {
  904. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
  905. SSL_R_PATH_TOO_LONG);
  906. goto err;
  907. }
  908. #ifdef OPENSSL_SYS_VMS
  909. r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename);
  910. #else
  911. r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
  912. #endif
  913. if (r <= 0 || r >= (int)sizeof(buf))
  914. goto err;
  915. if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
  916. goto err;
  917. }
  918. if (errno) {
  919. SYSerr(SYS_F_OPENDIR, get_last_sys_error());
  920. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  921. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
  922. goto err;
  923. }
  924. ret = 1;
  925. err:
  926. if (d)
  927. OPENSSL_DIR_end(&d);
  928. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  929. return ret;
  930. }
  931. /* Add a certificate to a BUF_MEM structure */
  932. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
  933. {
  934. int n;
  935. unsigned char *p;
  936. n = i2d_X509(x, NULL);
  937. if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  938. SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
  939. return 0;
  940. }
  941. p = (unsigned char *)&(buf->data[*l]);
  942. l2n3(n, p);
  943. n = i2d_X509(x, &p);
  944. if (n < 0) {
  945. /* Shouldn't happen */
  946. SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
  947. return 0;
  948. }
  949. *l += n + 3;
  950. return 1;
  951. }
  952. /* Add certificate chain to internal SSL BUF_MEM strcuture */
  953. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
  954. {
  955. BUF_MEM *buf = s->init_buf;
  956. int no_chain;
  957. int i;
  958. X509 *x;
  959. STACK_OF(X509) *extra_certs;
  960. X509_STORE *chain_store;
  961. if (cpk)
  962. x = cpk->x509;
  963. else
  964. x = NULL;
  965. if (s->cert->chain_store)
  966. chain_store = s->cert->chain_store;
  967. else
  968. chain_store = s->ctx->cert_store;
  969. /*
  970. * If we have a certificate specific chain use it, else use parent ctx.
  971. */
  972. if (cpk && cpk->chain)
  973. extra_certs = cpk->chain;
  974. else
  975. extra_certs = s->ctx->extra_certs;
  976. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
  977. no_chain = 1;
  978. else
  979. no_chain = 0;
  980. /* TLSv1 sends a chain with nothing in it, instead of an alert */
  981. if (!BUF_MEM_grow_clean(buf, 10)) {
  982. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
  983. return 0;
  984. }
  985. if (x != NULL) {
  986. if (no_chain) {
  987. if (!ssl_add_cert_to_buf(buf, l, x))
  988. return 0;
  989. } else {
  990. X509_STORE_CTX xs_ctx;
  991. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
  992. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
  993. return (0);
  994. }
  995. X509_verify_cert(&xs_ctx);
  996. /* Don't leave errors in the queue */
  997. ERR_clear_error();
  998. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  999. x = sk_X509_value(xs_ctx.chain, i);
  1000. if (!ssl_add_cert_to_buf(buf, l, x)) {
  1001. X509_STORE_CTX_cleanup(&xs_ctx);
  1002. return 0;
  1003. }
  1004. }
  1005. X509_STORE_CTX_cleanup(&xs_ctx);
  1006. }
  1007. }
  1008. for (i = 0; i < sk_X509_num(extra_certs); i++) {
  1009. x = sk_X509_value(extra_certs, i);
  1010. if (!ssl_add_cert_to_buf(buf, l, x))
  1011. return 0;
  1012. }
  1013. return 1;
  1014. }
  1015. /* Build a certificate chain for current certificate */
  1016. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
  1017. {
  1018. CERT_PKEY *cpk = c->key;
  1019. X509_STORE_CTX xs_ctx;
  1020. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  1021. X509 *x;
  1022. int i, rv = 0;
  1023. unsigned long error;
  1024. if (!cpk->x509) {
  1025. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
  1026. goto err;
  1027. }
  1028. /* Rearranging and check the chain: add everything to a store */
  1029. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  1030. chain_store = X509_STORE_new();
  1031. if (!chain_store)
  1032. goto err;
  1033. for (i = 0; i < sk_X509_num(cpk->chain); i++) {
  1034. x = sk_X509_value(cpk->chain, i);
  1035. if (!X509_STORE_add_cert(chain_store, x)) {
  1036. error = ERR_peek_last_error();
  1037. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  1038. ERR_GET_REASON(error) !=
  1039. X509_R_CERT_ALREADY_IN_HASH_TABLE)
  1040. goto err;
  1041. ERR_clear_error();
  1042. }
  1043. }
  1044. /* Add EE cert too: it might be self signed */
  1045. if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
  1046. error = ERR_peek_last_error();
  1047. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  1048. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
  1049. goto err;
  1050. ERR_clear_error();
  1051. }
  1052. } else {
  1053. if (c->chain_store)
  1054. chain_store = c->chain_store;
  1055. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
  1056. untrusted = cpk->chain;
  1057. }
  1058. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
  1059. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
  1060. goto err;
  1061. }
  1062. /* Set suite B flags if needed */
  1063. X509_STORE_CTX_set_flags(&xs_ctx,
  1064. c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
  1065. i = X509_verify_cert(&xs_ctx);
  1066. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  1067. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
  1068. ERR_clear_error();
  1069. i = 1;
  1070. rv = 2;
  1071. }
  1072. if (i > 0)
  1073. chain = X509_STORE_CTX_get1_chain(&xs_ctx);
  1074. if (i <= 0) {
  1075. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
  1076. i = X509_STORE_CTX_get_error(&xs_ctx);
  1077. ERR_add_error_data(2, "Verify error:",
  1078. X509_verify_cert_error_string(i));
  1079. X509_STORE_CTX_cleanup(&xs_ctx);
  1080. goto err;
  1081. }
  1082. X509_STORE_CTX_cleanup(&xs_ctx);
  1083. if (cpk->chain)
  1084. sk_X509_pop_free(cpk->chain, X509_free);
  1085. /* Remove EE certificate from chain */
  1086. x = sk_X509_shift(chain);
  1087. X509_free(x);
  1088. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  1089. if (sk_X509_num(chain) > 0) {
  1090. /* See if last cert is self signed */
  1091. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  1092. X509_check_purpose(x, -1, 0);
  1093. if (x->ex_flags & EXFLAG_SS) {
  1094. x = sk_X509_pop(chain);
  1095. X509_free(x);
  1096. }
  1097. }
  1098. }
  1099. cpk->chain = chain;
  1100. if (rv == 0)
  1101. rv = 1;
  1102. err:
  1103. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  1104. X509_STORE_free(chain_store);
  1105. return rv;
  1106. }
  1107. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
  1108. {
  1109. X509_STORE **pstore;
  1110. if (chain)
  1111. pstore = &c->chain_store;
  1112. else
  1113. pstore = &c->verify_store;
  1114. if (*pstore)
  1115. X509_STORE_free(*pstore);
  1116. *pstore = store;
  1117. if (ref && store)
  1118. CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
  1119. return 1;
  1120. }