ssl_cert.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. /*
  2. * ! \file ssl/ssl_cert.c
  3. */
  4. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  5. * All rights reserved.
  6. *
  7. * This package is an SSL implementation written
  8. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  110. * Hudson ([email protected]).
  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. return NULL;
  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. #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_ECDH)
  358. err:
  359. #endif
  360. #ifndef OPENSSL_NO_RSA
  361. if (ret->rsa_tmp != NULL)
  362. RSA_free(ret->rsa_tmp);
  363. #endif
  364. #ifndef OPENSSL_NO_DH
  365. if (ret->dh_tmp != NULL)
  366. DH_free(ret->dh_tmp);
  367. #endif
  368. #ifndef OPENSSL_NO_ECDH
  369. if (ret->ecdh_tmp != NULL)
  370. EC_KEY_free(ret->ecdh_tmp);
  371. #endif
  372. #ifndef OPENSSL_NO_TLSEXT
  373. custom_exts_free(&ret->cli_ext);
  374. custom_exts_free(&ret->srv_ext);
  375. #endif
  376. ssl_cert_clear_certs(ret);
  377. return NULL;
  378. }
  379. /* Free up and clear all certificates and chains */
  380. void ssl_cert_clear_certs(CERT *c)
  381. {
  382. int i;
  383. if (c == NULL)
  384. return;
  385. for (i = 0; i < SSL_PKEY_NUM; i++) {
  386. CERT_PKEY *cpk = c->pkeys + i;
  387. if (cpk->x509) {
  388. X509_free(cpk->x509);
  389. cpk->x509 = NULL;
  390. }
  391. if (cpk->privatekey) {
  392. EVP_PKEY_free(cpk->privatekey);
  393. cpk->privatekey = NULL;
  394. }
  395. if (cpk->chain) {
  396. sk_X509_pop_free(cpk->chain, X509_free);
  397. cpk->chain = NULL;
  398. }
  399. #ifndef OPENSSL_NO_TLSEXT
  400. if (cpk->serverinfo) {
  401. OPENSSL_free(cpk->serverinfo);
  402. cpk->serverinfo = NULL;
  403. cpk->serverinfo_length = 0;
  404. }
  405. #endif
  406. /* Clear all flags apart from explicit sign */
  407. cpk->valid_flags &= CERT_PKEY_EXPLICIT_SIGN;
  408. }
  409. }
  410. void ssl_cert_free(CERT *c)
  411. {
  412. int i;
  413. if (c == NULL)
  414. return;
  415. i = CRYPTO_add(&c->references, -1, CRYPTO_LOCK_SSL_CERT);
  416. #ifdef REF_PRINT
  417. REF_PRINT("CERT", c);
  418. #endif
  419. if (i > 0)
  420. return;
  421. #ifdef REF_CHECK
  422. if (i < 0) {
  423. fprintf(stderr, "ssl_cert_free, bad reference count\n");
  424. abort(); /* ok */
  425. }
  426. #endif
  427. #ifndef OPENSSL_NO_RSA
  428. if (c->rsa_tmp)
  429. RSA_free(c->rsa_tmp);
  430. #endif
  431. #ifndef OPENSSL_NO_DH
  432. if (c->dh_tmp)
  433. DH_free(c->dh_tmp);
  434. #endif
  435. #ifndef OPENSSL_NO_ECDH
  436. if (c->ecdh_tmp)
  437. EC_KEY_free(c->ecdh_tmp);
  438. #endif
  439. ssl_cert_clear_certs(c);
  440. if (c->peer_sigalgs)
  441. OPENSSL_free(c->peer_sigalgs);
  442. if (c->conf_sigalgs)
  443. OPENSSL_free(c->conf_sigalgs);
  444. if (c->client_sigalgs)
  445. OPENSSL_free(c->client_sigalgs);
  446. if (c->shared_sigalgs)
  447. OPENSSL_free(c->shared_sigalgs);
  448. if (c->ctypes)
  449. OPENSSL_free(c->ctypes);
  450. if (c->verify_store)
  451. X509_STORE_free(c->verify_store);
  452. if (c->chain_store)
  453. X509_STORE_free(c->chain_store);
  454. if (c->ciphers_raw)
  455. OPENSSL_free(c->ciphers_raw);
  456. #ifndef OPENSSL_NO_TLSEXT
  457. custom_exts_free(&c->cli_ext);
  458. custom_exts_free(&c->srv_ext);
  459. if (c->alpn_proposed)
  460. OPENSSL_free(c->alpn_proposed);
  461. #endif
  462. OPENSSL_free(c);
  463. }
  464. int ssl_cert_inst(CERT **o)
  465. {
  466. /*
  467. * Create a CERT if there isn't already one (which cannot really happen,
  468. * as it is initially created in SSL_CTX_new; but the earlier code
  469. * usually allows for that one being non-existant, so we follow that
  470. * behaviour, as it might turn out that there actually is a reason for it
  471. * -- but I'm not sure that *all* of the existing code could cope with
  472. * s->cert being NULL, otherwise we could do without the initialization
  473. * in SSL_CTX_new).
  474. */
  475. if (o == NULL) {
  476. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_PASSED_NULL_PARAMETER);
  477. return (0);
  478. }
  479. if (*o == NULL) {
  480. if ((*o = ssl_cert_new()) == NULL) {
  481. SSLerr(SSL_F_SSL_CERT_INST, ERR_R_MALLOC_FAILURE);
  482. return (0);
  483. }
  484. }
  485. return (1);
  486. }
  487. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) *chain)
  488. {
  489. CERT_PKEY *cpk = c->key;
  490. if (!cpk)
  491. return 0;
  492. if (cpk->chain)
  493. sk_X509_pop_free(cpk->chain, X509_free);
  494. cpk->chain = chain;
  495. return 1;
  496. }
  497. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) *chain)
  498. {
  499. STACK_OF(X509) *dchain;
  500. if (!chain)
  501. return ssl_cert_set0_chain(c, NULL);
  502. dchain = X509_chain_up_ref(chain);
  503. if (!dchain)
  504. return 0;
  505. if (!ssl_cert_set0_chain(c, dchain)) {
  506. sk_X509_pop_free(dchain, X509_free);
  507. return 0;
  508. }
  509. return 1;
  510. }
  511. int ssl_cert_add0_chain_cert(CERT *c, X509 *x)
  512. {
  513. CERT_PKEY *cpk = c->key;
  514. if (!cpk)
  515. return 0;
  516. if (!cpk->chain)
  517. cpk->chain = sk_X509_new_null();
  518. if (!cpk->chain || !sk_X509_push(cpk->chain, x))
  519. return 0;
  520. return 1;
  521. }
  522. int ssl_cert_add1_chain_cert(CERT *c, X509 *x)
  523. {
  524. if (!ssl_cert_add0_chain_cert(c, x))
  525. return 0;
  526. CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
  527. return 1;
  528. }
  529. int ssl_cert_select_current(CERT *c, X509 *x)
  530. {
  531. int i;
  532. if (x == NULL)
  533. return 0;
  534. for (i = 0; i < SSL_PKEY_NUM; i++) {
  535. CERT_PKEY *cpk = c->pkeys + i;
  536. if (cpk->x509 == x && cpk->privatekey) {
  537. c->key = cpk;
  538. return 1;
  539. }
  540. }
  541. for (i = 0; i < SSL_PKEY_NUM; i++) {
  542. CERT_PKEY *cpk = c->pkeys + i;
  543. if (cpk->privatekey && cpk->x509 && !X509_cmp(cpk->x509, x)) {
  544. c->key = cpk;
  545. return 1;
  546. }
  547. }
  548. return 0;
  549. }
  550. int ssl_cert_set_current(CERT *c, long op)
  551. {
  552. int i, idx;
  553. if (!c)
  554. return 0;
  555. if (op == SSL_CERT_SET_FIRST)
  556. idx = 0;
  557. else if (op == SSL_CERT_SET_NEXT) {
  558. idx = (int)(c->key - c->pkeys + 1);
  559. if (idx >= SSL_PKEY_NUM)
  560. return 0;
  561. } else
  562. return 0;
  563. for (i = idx; i < SSL_PKEY_NUM; i++) {
  564. CERT_PKEY *cpk = c->pkeys + i;
  565. if (cpk->x509 && cpk->privatekey) {
  566. c->key = cpk;
  567. return 1;
  568. }
  569. }
  570. return 0;
  571. }
  572. void ssl_cert_set_cert_cb(CERT *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  573. {
  574. c->cert_cb = cb;
  575. c->cert_cb_arg = arg;
  576. }
  577. SESS_CERT *ssl_sess_cert_new(void)
  578. {
  579. SESS_CERT *ret;
  580. ret = OPENSSL_malloc(sizeof *ret);
  581. if (ret == NULL) {
  582. SSLerr(SSL_F_SSL_SESS_CERT_NEW, ERR_R_MALLOC_FAILURE);
  583. return NULL;
  584. }
  585. memset(ret, 0, sizeof *ret);
  586. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  587. ret->references = 1;
  588. return ret;
  589. }
  590. void ssl_sess_cert_free(SESS_CERT *sc)
  591. {
  592. int i;
  593. if (sc == NULL)
  594. return;
  595. i = CRYPTO_add(&sc->references, -1, CRYPTO_LOCK_SSL_SESS_CERT);
  596. #ifdef REF_PRINT
  597. REF_PRINT("SESS_CERT", sc);
  598. #endif
  599. if (i > 0)
  600. return;
  601. #ifdef REF_CHECK
  602. if (i < 0) {
  603. fprintf(stderr, "ssl_sess_cert_free, bad reference count\n");
  604. abort(); /* ok */
  605. }
  606. #endif
  607. /* i == 0 */
  608. if (sc->cert_chain != NULL)
  609. sk_X509_pop_free(sc->cert_chain, X509_free);
  610. for (i = 0; i < SSL_PKEY_NUM; i++) {
  611. if (sc->peer_pkeys[i].x509 != NULL)
  612. X509_free(sc->peer_pkeys[i].x509);
  613. #if 0 /* We don't have the peer's private key.
  614. * These lines are just * here as a reminder
  615. * that we're still using a
  616. * not-quite-appropriate * data structure. */
  617. if (sc->peer_pkeys[i].privatekey != NULL)
  618. EVP_PKEY_free(sc->peer_pkeys[i].privatekey);
  619. #endif
  620. }
  621. #ifndef OPENSSL_NO_RSA
  622. if (sc->peer_rsa_tmp != NULL)
  623. RSA_free(sc->peer_rsa_tmp);
  624. #endif
  625. #ifndef OPENSSL_NO_DH
  626. if (sc->peer_dh_tmp != NULL)
  627. DH_free(sc->peer_dh_tmp);
  628. #endif
  629. #ifndef OPENSSL_NO_ECDH
  630. if (sc->peer_ecdh_tmp != NULL)
  631. EC_KEY_free(sc->peer_ecdh_tmp);
  632. #endif
  633. OPENSSL_free(sc);
  634. }
  635. int ssl_set_peer_cert_type(SESS_CERT *sc, int type)
  636. {
  637. sc->peer_cert_type = type;
  638. return (1);
  639. }
  640. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) *sk)
  641. {
  642. X509 *x;
  643. int i;
  644. X509_STORE *verify_store;
  645. X509_STORE_CTX ctx;
  646. if (s->cert->verify_store)
  647. verify_store = s->cert->verify_store;
  648. else
  649. verify_store = s->ctx->cert_store;
  650. if ((sk == NULL) || (sk_X509_num(sk) == 0))
  651. return (0);
  652. x = sk_X509_value(sk, 0);
  653. if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
  654. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, ERR_R_X509_LIB);
  655. return (0);
  656. }
  657. /* Set suite B flags if needed */
  658. X509_STORE_CTX_set_flags(&ctx, tls1_suiteb(s));
  659. #if 0
  660. if (SSL_get_verify_depth(s) >= 0)
  661. X509_STORE_CTX_set_depth(&ctx, SSL_get_verify_depth(s));
  662. #endif
  663. X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
  664. /*
  665. * We need to inherit the verify parameters. These can be determined by
  666. * the context: if its a server it will verify SSL client certificates or
  667. * vice versa.
  668. */
  669. X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
  670. /*
  671. * Anything non-default in "param" should overwrite anything in the ctx.
  672. */
  673. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  674. if (s->verify_callback)
  675. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  676. if (s->ctx->app_verify_callback != NULL)
  677. #if 1 /* new with OpenSSL 0.9.7 */
  678. i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  679. #else
  680. i = s->ctx->app_verify_callback(&ctx); /* should pass app_verify_arg */
  681. #endif
  682. else {
  683. #ifndef OPENSSL_NO_X509_VERIFY
  684. i = X509_verify_cert(&ctx);
  685. #else
  686. i = 0;
  687. ctx.error = X509_V_ERR_APPLICATION_VERIFICATION;
  688. SSLerr(SSL_F_SSL_VERIFY_CERT_CHAIN, SSL_R_NO_VERIFY_CALLBACK);
  689. #endif
  690. }
  691. s->verify_result = ctx.error;
  692. X509_STORE_CTX_cleanup(&ctx);
  693. return (i);
  694. }
  695. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  696. STACK_OF(X509_NAME) *name_list)
  697. {
  698. if (*ca_list != NULL)
  699. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  700. *ca_list = name_list;
  701. }
  702. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *sk)
  703. {
  704. int i;
  705. STACK_OF(X509_NAME) *ret;
  706. X509_NAME *name;
  707. ret = sk_X509_NAME_new_null();
  708. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  709. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  710. if ((name == NULL) || !sk_X509_NAME_push(ret, name)) {
  711. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  712. return (NULL);
  713. }
  714. }
  715. return (ret);
  716. }
  717. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) *name_list)
  718. {
  719. set_client_CA_list(&(s->client_CA), name_list);
  720. }
  721. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list)
  722. {
  723. set_client_CA_list(&(ctx->client_CA), name_list);
  724. }
  725. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx)
  726. {
  727. return (ctx->client_CA);
  728. }
  729. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *s)
  730. {
  731. if (s->type == SSL_ST_CONNECT) { /* we are in the client */
  732. if (((s->version >> 8) == SSL3_VERSION_MAJOR) && (s->s3 != NULL))
  733. return (s->s3->tmp.ca_names);
  734. else
  735. return (NULL);
  736. } else {
  737. if (s->client_CA != NULL)
  738. return (s->client_CA);
  739. else
  740. return (s->ctx->client_CA);
  741. }
  742. }
  743. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x)
  744. {
  745. X509_NAME *name;
  746. if (x == NULL)
  747. return (0);
  748. if ((*sk == NULL) && ((*sk = sk_X509_NAME_new_null()) == NULL))
  749. return (0);
  750. if ((name = X509_NAME_dup(X509_get_subject_name(x))) == NULL)
  751. return (0);
  752. if (!sk_X509_NAME_push(*sk, name)) {
  753. X509_NAME_free(name);
  754. return (0);
  755. }
  756. return (1);
  757. }
  758. int SSL_add_client_CA(SSL *ssl, X509 *x)
  759. {
  760. return (add_client_CA(&(ssl->client_CA), x));
  761. }
  762. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x)
  763. {
  764. return (add_client_CA(&(ctx->client_CA), x));
  765. }
  766. static int xname_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
  767. {
  768. return (X509_NAME_cmp(*a, *b));
  769. }
  770. #ifndef OPENSSL_NO_STDIO
  771. /**
  772. * Load CA certs from a file into a ::STACK. Note that it is somewhat misnamed;
  773. * it doesn't really have anything to do with clients (except that a common use
  774. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  775. * much to do with CAs, either, since it will load any old cert.
  776. * \param file the file containing one or more certs.
  777. * \return a ::STACK containing the certs.
  778. */
  779. STACK_OF(X509_NAME) *SSL_load_client_CA_file(const char *file)
  780. {
  781. BIO *in;
  782. X509 *x = NULL;
  783. X509_NAME *xn = NULL;
  784. STACK_OF(X509_NAME) *ret = NULL, *sk;
  785. sk = sk_X509_NAME_new(xname_cmp);
  786. in = BIO_new(BIO_s_file_internal());
  787. if ((sk == NULL) || (in == NULL)) {
  788. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  789. goto err;
  790. }
  791. if (!BIO_read_filename(in, file))
  792. goto err;
  793. for (;;) {
  794. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  795. break;
  796. if (ret == NULL) {
  797. ret = sk_X509_NAME_new_null();
  798. if (ret == NULL) {
  799. SSLerr(SSL_F_SSL_LOAD_CLIENT_CA_FILE, ERR_R_MALLOC_FAILURE);
  800. goto err;
  801. }
  802. }
  803. if ((xn = X509_get_subject_name(x)) == NULL)
  804. goto err;
  805. /* check for duplicates */
  806. xn = X509_NAME_dup(xn);
  807. if (xn == NULL)
  808. goto err;
  809. if (sk_X509_NAME_find(sk, xn) >= 0)
  810. X509_NAME_free(xn);
  811. else {
  812. sk_X509_NAME_push(sk, xn);
  813. sk_X509_NAME_push(ret, xn);
  814. }
  815. }
  816. if (0) {
  817. err:
  818. if (ret != NULL)
  819. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  820. ret = NULL;
  821. }
  822. if (sk != NULL)
  823. sk_X509_NAME_free(sk);
  824. if (in != NULL)
  825. BIO_free(in);
  826. if (x != NULL)
  827. X509_free(x);
  828. if (ret != NULL)
  829. ERR_clear_error();
  830. return (ret);
  831. }
  832. #endif
  833. /**
  834. * Add a file of certs to a stack.
  835. * \param stack the stack to add to.
  836. * \param file the file to add from. All certs in this file that are not
  837. * already in the stack will be added.
  838. * \return 1 for success, 0 for failure. Note that in the case of failure some
  839. * certs may have been added to \c stack.
  840. */
  841. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  842. const char *file)
  843. {
  844. BIO *in;
  845. X509 *x = NULL;
  846. X509_NAME *xn = NULL;
  847. int ret = 1;
  848. int (*oldcmp) (const X509_NAME *const *a, const X509_NAME *const *b);
  849. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  850. in = BIO_new(BIO_s_file_internal());
  851. if (in == NULL) {
  852. SSLerr(SSL_F_SSL_ADD_FILE_CERT_SUBJECTS_TO_STACK,
  853. ERR_R_MALLOC_FAILURE);
  854. goto err;
  855. }
  856. if (!BIO_read_filename(in, file))
  857. goto err;
  858. for (;;) {
  859. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL)
  860. break;
  861. if ((xn = X509_get_subject_name(x)) == NULL)
  862. goto err;
  863. xn = X509_NAME_dup(xn);
  864. if (xn == NULL)
  865. goto err;
  866. if (sk_X509_NAME_find(stack, xn) >= 0)
  867. X509_NAME_free(xn);
  868. else
  869. sk_X509_NAME_push(stack, xn);
  870. }
  871. ERR_clear_error();
  872. if (0) {
  873. err:
  874. ret = 0;
  875. }
  876. if (in != NULL)
  877. BIO_free(in);
  878. if (x != NULL)
  879. X509_free(x);
  880. (void)sk_X509_NAME_set_cmp_func(stack, oldcmp);
  881. return ret;
  882. }
  883. /**
  884. * Add a directory of certs to a stack.
  885. * \param stack the stack to append to.
  886. * \param dir the directory to append from. All files in this directory will be
  887. * examined as potential certs. Any that are acceptable to
  888. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will be
  889. * included.
  890. * \return 1 for success, 0 for failure. Note that in the case of failure some
  891. * certs may have been added to \c stack.
  892. */
  893. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) *stack,
  894. const char *dir)
  895. {
  896. OPENSSL_DIR_CTX *d = NULL;
  897. const char *filename;
  898. int ret = 0;
  899. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  900. /* Note that a side effect is that the CAs will be sorted by name */
  901. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  902. char buf[1024];
  903. int r;
  904. if (strlen(dir) + strlen(filename) + 2 > sizeof buf) {
  905. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK,
  906. SSL_R_PATH_TOO_LONG);
  907. goto err;
  908. }
  909. #ifdef OPENSSL_SYS_VMS
  910. r = BIO_snprintf(buf, sizeof buf, "%s%s", dir, filename);
  911. #else
  912. r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
  913. #endif
  914. if (r <= 0 || r >= (int)sizeof(buf))
  915. goto err;
  916. if (!SSL_add_file_cert_subjects_to_stack(stack, buf))
  917. goto err;
  918. }
  919. if (errno) {
  920. SYSerr(SYS_F_OPENDIR, get_last_sys_error());
  921. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  922. SSLerr(SSL_F_SSL_ADD_DIR_CERT_SUBJECTS_TO_STACK, ERR_R_SYS_LIB);
  923. goto err;
  924. }
  925. ret = 1;
  926. err:
  927. if (d)
  928. OPENSSL_DIR_end(&d);
  929. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  930. return ret;
  931. }
  932. /* Add a certificate to a BUF_MEM structure */
  933. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x)
  934. {
  935. int n;
  936. unsigned char *p;
  937. n = i2d_X509(x, NULL);
  938. if (n < 0 || !BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  939. SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
  940. return 0;
  941. }
  942. p = (unsigned char *)&(buf->data[*l]);
  943. l2n3(n, p);
  944. n = i2d_X509(x, &p);
  945. if (n < 0) {
  946. /* Shouldn't happen */
  947. SSLerr(SSL_F_SSL_ADD_CERT_TO_BUF, ERR_R_BUF_LIB);
  948. return 0;
  949. }
  950. *l += n + 3;
  951. return 1;
  952. }
  953. /* Add certificate chain to internal SSL BUF_MEM strcuture */
  954. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l)
  955. {
  956. BUF_MEM *buf = s->init_buf;
  957. int no_chain;
  958. int i;
  959. X509 *x;
  960. STACK_OF(X509) *extra_certs;
  961. X509_STORE *chain_store;
  962. if (cpk)
  963. x = cpk->x509;
  964. else
  965. x = NULL;
  966. if (s->cert->chain_store)
  967. chain_store = s->cert->chain_store;
  968. else
  969. chain_store = s->ctx->cert_store;
  970. /*
  971. * If we have a certificate specific chain use it, else use parent ctx.
  972. */
  973. if (cpk && cpk->chain)
  974. extra_certs = cpk->chain;
  975. else
  976. extra_certs = s->ctx->extra_certs;
  977. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
  978. no_chain = 1;
  979. else
  980. no_chain = 0;
  981. /* TLSv1 sends a chain with nothing in it, instead of an alert */
  982. if (!BUF_MEM_grow_clean(buf, 10)) {
  983. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_BUF_LIB);
  984. return 0;
  985. }
  986. if (x != NULL) {
  987. if (no_chain) {
  988. if (!ssl_add_cert_to_buf(buf, l, x))
  989. return 0;
  990. } else {
  991. X509_STORE_CTX xs_ctx;
  992. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
  993. SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, ERR_R_X509_LIB);
  994. return (0);
  995. }
  996. X509_verify_cert(&xs_ctx);
  997. /* Don't leave errors in the queue */
  998. ERR_clear_error();
  999. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  1000. x = sk_X509_value(xs_ctx.chain, i);
  1001. if (!ssl_add_cert_to_buf(buf, l, x)) {
  1002. X509_STORE_CTX_cleanup(&xs_ctx);
  1003. return 0;
  1004. }
  1005. }
  1006. X509_STORE_CTX_cleanup(&xs_ctx);
  1007. }
  1008. }
  1009. for (i = 0; i < sk_X509_num(extra_certs); i++) {
  1010. x = sk_X509_value(extra_certs, i);
  1011. if (!ssl_add_cert_to_buf(buf, l, x))
  1012. return 0;
  1013. }
  1014. return 1;
  1015. }
  1016. /* Build a certificate chain for current certificate */
  1017. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags)
  1018. {
  1019. CERT_PKEY *cpk = c->key;
  1020. X509_STORE_CTX xs_ctx;
  1021. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  1022. X509 *x;
  1023. int i, rv = 0;
  1024. unsigned long error;
  1025. if (!cpk->x509) {
  1026. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_NO_CERTIFICATE_SET);
  1027. goto err;
  1028. }
  1029. /* Rearranging and check the chain: add everything to a store */
  1030. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  1031. chain_store = X509_STORE_new();
  1032. if (!chain_store)
  1033. goto err;
  1034. for (i = 0; i < sk_X509_num(cpk->chain); i++) {
  1035. x = sk_X509_value(cpk->chain, i);
  1036. if (!X509_STORE_add_cert(chain_store, x)) {
  1037. error = ERR_peek_last_error();
  1038. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  1039. ERR_GET_REASON(error) !=
  1040. X509_R_CERT_ALREADY_IN_HASH_TABLE)
  1041. goto err;
  1042. ERR_clear_error();
  1043. }
  1044. }
  1045. /* Add EE cert too: it might be self signed */
  1046. if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
  1047. error = ERR_peek_last_error();
  1048. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  1049. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE)
  1050. goto err;
  1051. ERR_clear_error();
  1052. }
  1053. } else {
  1054. if (c->chain_store)
  1055. chain_store = c->chain_store;
  1056. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED)
  1057. untrusted = cpk->chain;
  1058. }
  1059. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
  1060. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, ERR_R_X509_LIB);
  1061. goto err;
  1062. }
  1063. /* Set suite B flags if needed */
  1064. X509_STORE_CTX_set_flags(&xs_ctx,
  1065. c->cert_flags & SSL_CERT_FLAG_SUITEB_128_LOS);
  1066. i = X509_verify_cert(&xs_ctx);
  1067. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  1068. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR)
  1069. ERR_clear_error();
  1070. i = 1;
  1071. rv = 2;
  1072. }
  1073. if (i > 0)
  1074. chain = X509_STORE_CTX_get1_chain(&xs_ctx);
  1075. if (i <= 0) {
  1076. SSLerr(SSL_F_SSL_BUILD_CERT_CHAIN, SSL_R_CERTIFICATE_VERIFY_FAILED);
  1077. i = X509_STORE_CTX_get_error(&xs_ctx);
  1078. ERR_add_error_data(2, "Verify error:",
  1079. X509_verify_cert_error_string(i));
  1080. X509_STORE_CTX_cleanup(&xs_ctx);
  1081. goto err;
  1082. }
  1083. X509_STORE_CTX_cleanup(&xs_ctx);
  1084. if (cpk->chain)
  1085. sk_X509_pop_free(cpk->chain, X509_free);
  1086. /* Remove EE certificate from chain */
  1087. x = sk_X509_shift(chain);
  1088. X509_free(x);
  1089. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  1090. if (sk_X509_num(chain) > 0) {
  1091. /* See if last cert is self signed */
  1092. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  1093. X509_check_purpose(x, -1, 0);
  1094. if (x->ex_flags & EXFLAG_SS) {
  1095. x = sk_X509_pop(chain);
  1096. X509_free(x);
  1097. }
  1098. }
  1099. }
  1100. cpk->chain = chain;
  1101. if (rv == 0)
  1102. rv = 1;
  1103. err:
  1104. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK)
  1105. X509_STORE_free(chain_store);
  1106. return rv;
  1107. }
  1108. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref)
  1109. {
  1110. X509_STORE **pstore;
  1111. if (chain)
  1112. pstore = &c->chain_store;
  1113. else
  1114. pstore = &c->verify_store;
  1115. if (*pstore)
  1116. X509_STORE_free(*pstore);
  1117. *pstore = store;
  1118. if (ref && store)
  1119. CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
  1120. return 1;
  1121. }