ssl_cert.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263
  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. }