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

ssl_cert.c 27 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  114. #include <errno.h>
  115. #include <stdio.h>
  116. #include <string.h>
  117. #include <openssl/bio.h>
  118. #include <openssl/bn.h>
  119. #include <openssl/buf.h>
  120. #include <openssl/dh.h>
  121. #include <openssl/err.h>
  122. #include <openssl/mem.h>
  123. #include <openssl/obj.h>
  124. #include <openssl/pem.h>
  125. #include <openssl/x509v3.h>
  126. #include "../crypto/dh/internal.h"
  127. #include "../crypto/directory.h"
  128. #include "internal.h"
  129. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  130. static int ssl_x509_store_ctx_idx = -1;
  131. int got_write_lock = 0;
  132. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  133. if (ssl_x509_store_ctx_idx < 0) {
  134. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  135. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  136. got_write_lock = 1;
  137. if (ssl_x509_store_ctx_idx < 0) {
  138. ssl_x509_store_ctx_idx = X509_STORE_CTX_get_ex_new_index(
  139. 0, "SSL for verify callback", NULL, NULL, NULL);
  140. }
  141. }
  142. if (got_write_lock) {
  143. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  144. } else {
  145. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  146. }
  147. return ssl_x509_store_ctx_idx;
  148. }
  149. CERT *ssl_cert_new(void) {
  150. CERT *ret;
  151. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  152. if (ret == NULL) {
  153. OPENSSL_PUT_ERROR(SSL, ssl_cert_new, ERR_R_MALLOC_FAILURE);
  154. return NULL;
  155. }
  156. memset(ret, 0, sizeof(CERT));
  157. ret->key = &ret->pkeys[SSL_PKEY_RSA_ENC];
  158. return ret;
  159. }
  160. CERT *ssl_cert_dup(CERT *cert) {
  161. CERT *ret;
  162. int i;
  163. ret = (CERT *)OPENSSL_malloc(sizeof(CERT));
  164. if (ret == NULL) {
  165. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  166. return NULL;
  167. }
  168. memset(ret, 0, sizeof(CERT));
  169. ret->key = &ret->pkeys[cert->key - &cert->pkeys[0]];
  170. /* or ret->key = ret->pkeys + (cert->key - cert->pkeys), if you find that
  171. * more readable */
  172. ret->mask_k = cert->mask_k;
  173. ret->mask_a = cert->mask_a;
  174. if (cert->dh_tmp != NULL) {
  175. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  176. if (ret->dh_tmp == NULL) {
  177. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_DH_LIB);
  178. goto err;
  179. }
  180. if (cert->dh_tmp->priv_key) {
  181. BIGNUM *b = BN_dup(cert->dh_tmp->priv_key);
  182. if (!b) {
  183. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  184. goto err;
  185. }
  186. ret->dh_tmp->priv_key = b;
  187. }
  188. if (cert->dh_tmp->pub_key) {
  189. BIGNUM *b = BN_dup(cert->dh_tmp->pub_key);
  190. if (!b) {
  191. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_BN_LIB);
  192. goto err;
  193. }
  194. ret->dh_tmp->pub_key = b;
  195. }
  196. }
  197. ret->dh_tmp_cb = cert->dh_tmp_cb;
  198. ret->ecdh_nid = cert->ecdh_nid;
  199. ret->ecdh_tmp_cb = cert->ecdh_tmp_cb;
  200. for (i = 0; i < SSL_PKEY_NUM; i++) {
  201. CERT_PKEY *cpk = cert->pkeys + i;
  202. CERT_PKEY *rpk = ret->pkeys + i;
  203. if (cpk->x509 != NULL) {
  204. rpk->x509 = X509_up_ref(cpk->x509);
  205. }
  206. if (cpk->privatekey != NULL) {
  207. rpk->privatekey = EVP_PKEY_dup(cpk->privatekey);
  208. }
  209. if (cpk->chain) {
  210. rpk->chain = X509_chain_up_ref(cpk->chain);
  211. if (!rpk->chain) {
  212. OPENSSL_PUT_ERROR(SSL, ssl_cert_dup, ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. }
  216. }
  217. /* Copy over signature algorithm configuration. */
  218. if (cert->conf_sigalgs) {
  219. ret->conf_sigalgs = BUF_memdup(cert->conf_sigalgs, cert->conf_sigalgslen);
  220. if (!ret->conf_sigalgs) {
  221. goto err;
  222. }
  223. ret->conf_sigalgslen = cert->conf_sigalgslen;
  224. }
  225. if (cert->client_sigalgs) {
  226. ret->client_sigalgs = BUF_memdup(cert->client_sigalgs,
  227. cert->client_sigalgslen);
  228. if (!ret->client_sigalgs) {
  229. goto err;
  230. }
  231. ret->client_sigalgslen = cert->client_sigalgslen;
  232. }
  233. /* Copy any custom client certificate types */
  234. if (cert->client_certificate_types) {
  235. ret->client_certificate_types = BUF_memdup(
  236. cert->client_certificate_types, cert->num_client_certificate_types);
  237. if (!ret->client_certificate_types) {
  238. goto err;
  239. }
  240. ret->num_client_certificate_types = cert->num_client_certificate_types;
  241. }
  242. ret->cert_flags = cert->cert_flags;
  243. ret->cert_cb = cert->cert_cb;
  244. ret->cert_cb_arg = cert->cert_cb_arg;
  245. if (cert->verify_store) {
  246. CRYPTO_add(&cert->verify_store->references, 1, CRYPTO_LOCK_X509_STORE);
  247. ret->verify_store = cert->verify_store;
  248. }
  249. if (cert->chain_store) {
  250. CRYPTO_add(&cert->chain_store->references, 1, CRYPTO_LOCK_X509_STORE);
  251. ret->chain_store = cert->chain_store;
  252. }
  253. return ret;
  254. err:
  255. ssl_cert_free(ret);
  256. return NULL;
  257. }
  258. /* Free up and clear all certificates and chains */
  259. void ssl_cert_clear_certs(CERT *c) {
  260. int i;
  261. if (c == NULL) {
  262. return;
  263. }
  264. for (i = 0; i < SSL_PKEY_NUM; i++) {
  265. CERT_PKEY *cpk = c->pkeys + i;
  266. if (cpk->x509) {
  267. X509_free(cpk->x509);
  268. cpk->x509 = NULL;
  269. }
  270. if (cpk->privatekey) {
  271. EVP_PKEY_free(cpk->privatekey);
  272. cpk->privatekey = NULL;
  273. }
  274. if (cpk->chain) {
  275. sk_X509_pop_free(cpk->chain, X509_free);
  276. cpk->chain = NULL;
  277. }
  278. }
  279. }
  280. void ssl_cert_free(CERT *c) {
  281. if (c == NULL) {
  282. return;
  283. }
  284. if (c->dh_tmp) {
  285. DH_free(c->dh_tmp);
  286. }
  287. ssl_cert_clear_certs(c);
  288. if (c->peer_sigalgs) {
  289. OPENSSL_free(c->peer_sigalgs);
  290. }
  291. if (c->conf_sigalgs) {
  292. OPENSSL_free(c->conf_sigalgs);
  293. }
  294. if (c->client_sigalgs) {
  295. OPENSSL_free(c->client_sigalgs);
  296. }
  297. if (c->shared_sigalgs) {
  298. OPENSSL_free(c->shared_sigalgs);
  299. }
  300. if (c->client_certificate_types) {
  301. OPENSSL_free(c->client_certificate_types);
  302. }
  303. if (c->verify_store) {
  304. X509_STORE_free(c->verify_store);
  305. }
  306. if (c->chain_store) {
  307. X509_STORE_free(c->chain_store);
  308. }
  309. if (c->ciphers_raw) {
  310. OPENSSL_free(c->ciphers_raw);
  311. }
  312. OPENSSL_free(c);
  313. }
  314. int ssl_cert_set0_chain(CERT *c, STACK_OF(X509) * chain) {
  315. CERT_PKEY *cpk = c->key;
  316. if (!cpk) {
  317. return 0;
  318. }
  319. if (cpk->chain) {
  320. sk_X509_pop_free(cpk->chain, X509_free);
  321. }
  322. cpk->chain = chain;
  323. return 1;
  324. }
  325. int ssl_cert_set1_chain(CERT *c, STACK_OF(X509) * chain) {
  326. STACK_OF(X509) * dchain;
  327. if (!chain) {
  328. return ssl_cert_set0_chain(c, NULL);
  329. }
  330. dchain = X509_chain_up_ref(chain);
  331. if (!dchain) {
  332. return 0;
  333. }
  334. if (!ssl_cert_set0_chain(c, dchain)) {
  335. sk_X509_pop_free(dchain, X509_free);
  336. return 0;
  337. }
  338. return 1;
  339. }
  340. int ssl_cert_add0_chain_cert(CERT *c, X509 *x) {
  341. CERT_PKEY *cpk = c->key;
  342. if (!cpk) {
  343. return 0;
  344. }
  345. if (!cpk->chain) {
  346. cpk->chain = sk_X509_new_null();
  347. }
  348. if (!cpk->chain || !sk_X509_push(cpk->chain, x)) {
  349. return 0;
  350. }
  351. return 1;
  352. }
  353. int ssl_cert_add1_chain_cert(CERT *c, X509 *x) {
  354. if (!ssl_cert_add0_chain_cert(c, x)) {
  355. return 0;
  356. }
  357. X509_up_ref(x);
  358. return 1;
  359. }
  360. int ssl_cert_select_current(CERT *c, X509 *x) {
  361. int i;
  362. if (x == NULL) {
  363. return 0;
  364. }
  365. for (i = 0; i < SSL_PKEY_NUM; i++) {
  366. if (c->pkeys[i].x509 == x) {
  367. c->key = &c->pkeys[i];
  368. return 1;
  369. }
  370. }
  371. for (i = 0; i < SSL_PKEY_NUM; i++) {
  372. if (c->pkeys[i].x509 && !X509_cmp(c->pkeys[i].x509, x)) {
  373. c->key = &c->pkeys[i];
  374. return 1;
  375. }
  376. }
  377. return 0;
  378. }
  379. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  380. c->cert_cb = cb;
  381. c->cert_cb_arg = arg;
  382. }
  383. SESS_CERT *ssl_sess_cert_new(void) {
  384. SESS_CERT *ret;
  385. ret = OPENSSL_malloc(sizeof *ret);
  386. if (ret == NULL) {
  387. OPENSSL_PUT_ERROR(SSL, ssl_sess_cert_new, ERR_R_MALLOC_FAILURE);
  388. return NULL;
  389. }
  390. memset(ret, 0, sizeof *ret);
  391. ret->peer_key = &(ret->peer_pkeys[SSL_PKEY_RSA_ENC]);
  392. return ret;
  393. }
  394. void ssl_sess_cert_free(SESS_CERT *sc) {
  395. int i;
  396. if (sc == NULL) {
  397. return;
  398. }
  399. if (sc->cert_chain != NULL) {
  400. sk_X509_pop_free(sc->cert_chain, X509_free);
  401. }
  402. for (i = 0; i < SSL_PKEY_NUM; i++) {
  403. if (sc->peer_pkeys[i].x509 != NULL) {
  404. X509_free(sc->peer_pkeys[i].x509);
  405. }
  406. }
  407. if (sc->peer_dh_tmp != NULL) {
  408. DH_free(sc->peer_dh_tmp);
  409. }
  410. if (sc->peer_ecdh_tmp != NULL) {
  411. EC_KEY_free(sc->peer_ecdh_tmp);
  412. }
  413. OPENSSL_free(sc);
  414. }
  415. int ssl_set_peer_cert_type(SESS_CERT *sc, int type) {
  416. sc->peer_cert_type = type;
  417. return 1;
  418. }
  419. int ssl_verify_cert_chain(SSL *s, STACK_OF(X509) * sk) {
  420. X509 *x;
  421. int i;
  422. X509_STORE *verify_store;
  423. X509_STORE_CTX ctx;
  424. if (s->cert->verify_store) {
  425. verify_store = s->cert->verify_store;
  426. } else {
  427. verify_store = s->ctx->cert_store;
  428. }
  429. if (sk == NULL || sk_X509_num(sk) == 0) {
  430. return 0;
  431. }
  432. x = sk_X509_value(sk, 0);
  433. if (!X509_STORE_CTX_init(&ctx, verify_store, x, sk)) {
  434. OPENSSL_PUT_ERROR(SSL, ssl_verify_cert_chain, ERR_R_X509_LIB);
  435. return 0;
  436. }
  437. X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(), s);
  438. /* We need to inherit the verify parameters. These can be determined by the
  439. * context: if its a server it will verify SSL client certificates or vice
  440. * versa. */
  441. X509_STORE_CTX_set_default(&ctx, s->server ? "ssl_client" : "ssl_server");
  442. /* Anything non-default in "param" should overwrite anything in the ctx. */
  443. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), s->param);
  444. if (s->verify_callback) {
  445. X509_STORE_CTX_set_verify_cb(&ctx, s->verify_callback);
  446. }
  447. if (s->ctx->app_verify_callback != NULL) {
  448. i = s->ctx->app_verify_callback(&ctx, s->ctx->app_verify_arg);
  449. } else {
  450. i = X509_verify_cert(&ctx);
  451. }
  452. s->verify_result = ctx.error;
  453. X509_STORE_CTX_cleanup(&ctx);
  454. return i;
  455. }
  456. static void set_client_CA_list(STACK_OF(X509_NAME) * *ca_list,
  457. STACK_OF(X509_NAME) * name_list) {
  458. if (*ca_list != NULL) {
  459. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  460. }
  461. *ca_list = name_list;
  462. }
  463. STACK_OF(X509_NAME) * SSL_dup_CA_list(STACK_OF(X509_NAME) * sk) {
  464. size_t i;
  465. STACK_OF(X509_NAME) * ret;
  466. X509_NAME *name;
  467. ret = sk_X509_NAME_new_null();
  468. for (i = 0; i < sk_X509_NAME_num(sk); i++) {
  469. name = X509_NAME_dup(sk_X509_NAME_value(sk, i));
  470. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  471. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  472. return NULL;
  473. }
  474. }
  475. return ret;
  476. }
  477. void SSL_set_client_CA_list(SSL *s, STACK_OF(X509_NAME) * name_list) {
  478. set_client_CA_list(&(s->client_CA), name_list);
  479. }
  480. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) * name_list) {
  481. set_client_CA_list(&(ctx->client_CA), name_list);
  482. }
  483. STACK_OF(X509_NAME) * SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  484. return ctx->client_CA;
  485. }
  486. STACK_OF(X509_NAME) * SSL_get_client_CA_list(const SSL *s) {
  487. if (s->server) {
  488. if (s->client_CA != NULL) {
  489. return s->client_CA;
  490. } else {
  491. return s->ctx->client_CA;
  492. }
  493. } else {
  494. if ((s->version >> 8) == SSL3_VERSION_MAJOR && s->s3 != NULL) {
  495. return s->s3->tmp.ca_names;
  496. } else {
  497. return NULL;
  498. }
  499. }
  500. }
  501. static int add_client_CA(STACK_OF(X509_NAME) * *sk, X509 *x) {
  502. X509_NAME *name;
  503. if (x == NULL) {
  504. return 0;
  505. }
  506. if (*sk == NULL) {
  507. *sk = sk_X509_NAME_new_null();
  508. if (*sk == NULL) {
  509. return 0;
  510. }
  511. }
  512. name = X509_NAME_dup(X509_get_subject_name(x));
  513. if (name == NULL) {
  514. return 0;
  515. }
  516. if (!sk_X509_NAME_push(*sk, name)) {
  517. X509_NAME_free(name);
  518. return 0;
  519. }
  520. return 1;
  521. }
  522. int SSL_add_client_CA(SSL *ssl, X509 *x) {
  523. return add_client_CA(&(ssl->client_CA), x);
  524. }
  525. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x) {
  526. return add_client_CA(&(ctx->client_CA), x);
  527. }
  528. static int xname_cmp(const X509_NAME **a, const X509_NAME **b) {
  529. return X509_NAME_cmp(*a, *b);
  530. }
  531. /* Load CA certs from a file into a STACK. Note that it is somewhat misnamed;
  532. * it doesn't really have anything to do with clients (except that a common use
  533. * for a stack of CAs is to send it to the client). Actually, it doesn't have
  534. * much to do with CAs, either, since it will load any old cert.
  535. *
  536. * \param file the file containing one or more certs.
  537. * \return a ::STACK containing the certs. */
  538. STACK_OF(X509_NAME) * SSL_load_client_CA_file(const char *file) {
  539. BIO *in;
  540. X509 *x = NULL;
  541. X509_NAME *xn = NULL;
  542. STACK_OF(X509_NAME) *ret = NULL, *sk;
  543. sk = sk_X509_NAME_new(xname_cmp);
  544. in = BIO_new(BIO_s_file());
  545. if (sk == NULL || in == NULL) {
  546. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  547. goto err;
  548. }
  549. if (!BIO_read_filename(in, file)) {
  550. goto err;
  551. }
  552. for (;;) {
  553. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  554. break;
  555. }
  556. if (ret == NULL) {
  557. ret = sk_X509_NAME_new_null();
  558. if (ret == NULL) {
  559. OPENSSL_PUT_ERROR(SSL, SSL_load_client_CA_file, ERR_R_MALLOC_FAILURE);
  560. goto err;
  561. }
  562. }
  563. xn = X509_get_subject_name(x);
  564. if (xn == NULL) {
  565. goto err;
  566. }
  567. /* check for duplicates */
  568. xn = X509_NAME_dup(xn);
  569. if (xn == NULL) {
  570. goto err;
  571. }
  572. if (sk_X509_NAME_find(sk, NULL, xn)) {
  573. X509_NAME_free(xn);
  574. } else {
  575. sk_X509_NAME_push(sk, xn);
  576. sk_X509_NAME_push(ret, xn);
  577. }
  578. }
  579. if (0) {
  580. err:
  581. if (ret != NULL) {
  582. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  583. }
  584. ret = NULL;
  585. }
  586. if (sk != NULL) {
  587. sk_X509_NAME_free(sk);
  588. }
  589. if (in != NULL) {
  590. BIO_free(in);
  591. }
  592. if (x != NULL) {
  593. X509_free(x);
  594. }
  595. if (ret != NULL) {
  596. ERR_clear_error();
  597. }
  598. return ret;
  599. }
  600. /* Add a file of certs to a stack.
  601. *
  602. * \param stack the stack to add to.
  603. * \param file the file to add from. All certs in this file that are not
  604. * already in the stack will be added.
  605. * \return 1 for success, 0 for failure. Note that in the case of failure some
  606. * certs may have been added to \c stack. */
  607. int SSL_add_file_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
  608. const char *file) {
  609. BIO *in;
  610. X509 *x = NULL;
  611. X509_NAME *xn = NULL;
  612. int ret = 1;
  613. int (*oldcmp)(const X509_NAME **a, const X509_NAME **b);
  614. oldcmp = sk_X509_NAME_set_cmp_func(stack, xname_cmp);
  615. in = BIO_new(BIO_s_file());
  616. if (in == NULL) {
  617. OPENSSL_PUT_ERROR(SSL, SSL_add_file_cert_subjects_to_stack,
  618. ERR_R_MALLOC_FAILURE);
  619. goto err;
  620. }
  621. if (!BIO_read_filename(in, file)) {
  622. goto err;
  623. }
  624. for (;;) {
  625. if (PEM_read_bio_X509(in, &x, NULL, NULL) == NULL) {
  626. break;
  627. }
  628. xn = X509_get_subject_name(x);
  629. if (xn == NULL) {
  630. goto err;
  631. }
  632. xn = X509_NAME_dup(xn);
  633. if (xn == NULL) {
  634. goto err;
  635. }
  636. if (sk_X509_NAME_find(stack, NULL, xn)) {
  637. X509_NAME_free(xn);
  638. } else {
  639. sk_X509_NAME_push(stack, xn);
  640. }
  641. }
  642. ERR_clear_error();
  643. if (0) {
  644. err:
  645. ret = 0;
  646. }
  647. if (in != NULL) {
  648. BIO_free(in);
  649. }
  650. if (x != NULL) {
  651. X509_free(x);
  652. }
  653. (void) sk_X509_NAME_set_cmp_func(stack, oldcmp);
  654. return ret;
  655. }
  656. /* Add a directory of certs to a stack.
  657. *
  658. * \param stack the stack to append to.
  659. * \param dir the directory to append from. All files in this directory will be
  660. * examined as potential certs. Any that are acceptable to
  661. * SSL_add_dir_cert_subjects_to_stack() that are not already in the stack will
  662. * be included.
  663. * \return 1 for success, 0 for failure. Note that in the case of failure some
  664. * certs may have been added to \c stack. */
  665. int SSL_add_dir_cert_subjects_to_stack(STACK_OF(X509_NAME) * stack,
  666. const char *dir) {
  667. OPENSSL_DIR_CTX *d = NULL;
  668. const char *filename;
  669. int ret = 0;
  670. CRYPTO_w_lock(CRYPTO_LOCK_READDIR);
  671. /* Note that a side effect is that the CAs will be sorted by name */
  672. while ((filename = OPENSSL_DIR_read(&d, dir))) {
  673. char buf[1024];
  674. int r;
  675. if (strlen(dir) + strlen(filename) + 2 > sizeof(buf)) {
  676. OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack,
  677. SSL_R_PATH_TOO_LONG);
  678. goto err;
  679. }
  680. r = BIO_snprintf(buf, sizeof buf, "%s/%s", dir, filename);
  681. if (r <= 0 || r >= (int)sizeof(buf) ||
  682. !SSL_add_file_cert_subjects_to_stack(stack, buf)) {
  683. goto err;
  684. }
  685. }
  686. if (errno) {
  687. OPENSSL_PUT_ERROR(SSL, SSL_add_dir_cert_subjects_to_stack, ERR_R_SYS_LIB);
  688. ERR_add_error_data(3, "OPENSSL_DIR_read(&ctx, '", dir, "')");
  689. goto err;
  690. }
  691. ret = 1;
  692. err:
  693. if (d) {
  694. OPENSSL_DIR_end(&d);
  695. }
  696. CRYPTO_w_unlock(CRYPTO_LOCK_READDIR);
  697. return ret;
  698. }
  699. /* Add a certificate to a BUF_MEM structure */
  700. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
  701. int n;
  702. uint8_t *p;
  703. n = i2d_X509(x, NULL);
  704. if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  705. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_to_buf, ERR_R_BUF_LIB);
  706. return 0;
  707. }
  708. p = (uint8_t *)&(buf->data[*l]);
  709. l2n3(n, p);
  710. i2d_X509(x, &p);
  711. *l += n + 3;
  712. return 1;
  713. }
  714. /* Add certificate chain to internal SSL BUF_MEM structure. */
  715. int ssl_add_cert_chain(SSL *s, CERT_PKEY *cpk, unsigned long *l) {
  716. BUF_MEM *buf = s->init_buf;
  717. int no_chain = 0;
  718. size_t i;
  719. X509 *x = NULL;
  720. STACK_OF(X509) * extra_certs;
  721. X509_STORE *chain_store;
  722. if (cpk) {
  723. x = cpk->x509;
  724. }
  725. if (s->cert->chain_store) {
  726. chain_store = s->cert->chain_store;
  727. } else {
  728. chain_store = s->ctx->cert_store;
  729. }
  730. /* If we have a certificate specific chain use it, else use parent ctx. */
  731. if (cpk && cpk->chain) {
  732. extra_certs = cpk->chain;
  733. } else {
  734. extra_certs = s->ctx->extra_certs;
  735. }
  736. if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs) {
  737. no_chain = 1;
  738. }
  739. /* TLSv1 sends a chain with nothing in it, instead of an alert. */
  740. if (!BUF_MEM_grow_clean(buf, 10)) {
  741. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_BUF_LIB);
  742. return 0;
  743. }
  744. if (x != NULL) {
  745. if (no_chain) {
  746. if (!ssl_add_cert_to_buf(buf, l, x)) {
  747. return 0;
  748. }
  749. } else {
  750. X509_STORE_CTX xs_ctx;
  751. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, x, NULL)) {
  752. OPENSSL_PUT_ERROR(SSL, ssl_add_cert_chain, ERR_R_X509_LIB);
  753. return 0;
  754. }
  755. X509_verify_cert(&xs_ctx);
  756. /* Don't leave errors in the queue */
  757. ERR_clear_error();
  758. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  759. x = sk_X509_value(xs_ctx.chain, i);
  760. if (!ssl_add_cert_to_buf(buf, l, x)) {
  761. X509_STORE_CTX_cleanup(&xs_ctx);
  762. return 0;
  763. }
  764. }
  765. X509_STORE_CTX_cleanup(&xs_ctx);
  766. }
  767. }
  768. for (i = 0; i < sk_X509_num(extra_certs); i++) {
  769. x = sk_X509_value(extra_certs, i);
  770. if (!ssl_add_cert_to_buf(buf, l, x)) {
  771. return 0;
  772. }
  773. }
  774. return 1;
  775. }
  776. /* Build a certificate chain for current certificate */
  777. int ssl_build_cert_chain(CERT *c, X509_STORE *chain_store, int flags) {
  778. CERT_PKEY *cpk = c->key;
  779. X509_STORE_CTX xs_ctx;
  780. STACK_OF(X509) *chain = NULL, *untrusted = NULL;
  781. X509 *x;
  782. int i, rv = 0;
  783. unsigned long error;
  784. if (!cpk->x509) {
  785. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, SSL_R_NO_CERTIFICATE_SET);
  786. goto err;
  787. }
  788. /* Rearranging and check the chain: add everything to a store */
  789. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  790. size_t j;
  791. chain_store = X509_STORE_new();
  792. if (!chain_store) {
  793. goto err;
  794. }
  795. for (j = 0; j < sk_X509_num(cpk->chain); j++) {
  796. x = sk_X509_value(cpk->chain, j);
  797. if (!X509_STORE_add_cert(chain_store, x)) {
  798. error = ERR_peek_last_error();
  799. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  800. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  801. goto err;
  802. }
  803. ERR_clear_error();
  804. }
  805. }
  806. /* Add EE cert too: it might be self signed */
  807. if (!X509_STORE_add_cert(chain_store, cpk->x509)) {
  808. error = ERR_peek_last_error();
  809. if (ERR_GET_LIB(error) != ERR_LIB_X509 ||
  810. ERR_GET_REASON(error) != X509_R_CERT_ALREADY_IN_HASH_TABLE) {
  811. goto err;
  812. }
  813. ERR_clear_error();
  814. }
  815. } else {
  816. if (c->chain_store) {
  817. chain_store = c->chain_store;
  818. }
  819. if (flags & SSL_BUILD_CHAIN_FLAG_UNTRUSTED) {
  820. untrusted = cpk->chain;
  821. }
  822. }
  823. if (!X509_STORE_CTX_init(&xs_ctx, chain_store, cpk->x509, untrusted)) {
  824. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain, ERR_R_X509_LIB);
  825. goto err;
  826. }
  827. i = X509_verify_cert(&xs_ctx);
  828. if (i <= 0 && flags & SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR) {
  829. if (flags & SSL_BUILD_CHAIN_FLAG_CLEAR_ERROR) {
  830. ERR_clear_error();
  831. }
  832. i = 1;
  833. rv = 2;
  834. }
  835. if (i > 0) {
  836. chain = X509_STORE_CTX_get1_chain(&xs_ctx);
  837. }
  838. if (i <= 0) {
  839. OPENSSL_PUT_ERROR(SSL, ssl_build_cert_chain,
  840. SSL_R_CERTIFICATE_VERIFY_FAILED);
  841. i = X509_STORE_CTX_get_error(&xs_ctx);
  842. ERR_add_error_data(2, "Verify error:", X509_verify_cert_error_string(i));
  843. X509_STORE_CTX_cleanup(&xs_ctx);
  844. goto err;
  845. }
  846. X509_STORE_CTX_cleanup(&xs_ctx);
  847. if (cpk->chain) {
  848. sk_X509_pop_free(cpk->chain, X509_free);
  849. }
  850. /* Remove EE certificate from chain */
  851. x = sk_X509_shift(chain);
  852. X509_free(x);
  853. if (flags & SSL_BUILD_CHAIN_FLAG_NO_ROOT) {
  854. if (sk_X509_num(chain) > 0) {
  855. /* See if last cert is self signed */
  856. x = sk_X509_value(chain, sk_X509_num(chain) - 1);
  857. X509_check_purpose(x, -1, 0);
  858. if (x->ex_flags & EXFLAG_SS) {
  859. x = sk_X509_pop(chain);
  860. X509_free(x);
  861. }
  862. }
  863. }
  864. cpk->chain = chain;
  865. if (rv == 0) {
  866. rv = 1;
  867. }
  868. err:
  869. if (flags & SSL_BUILD_CHAIN_FLAG_CHECK) {
  870. X509_STORE_free(chain_store);
  871. }
  872. return rv;
  873. }
  874. int ssl_cert_set_cert_store(CERT *c, X509_STORE *store, int chain, int ref) {
  875. X509_STORE **pstore;
  876. if (chain) {
  877. pstore = &c->chain_store;
  878. } else {
  879. pstore = &c->verify_store;
  880. }
  881. if (*pstore) {
  882. X509_STORE_free(*pstore);
  883. }
  884. *pstore = store;
  885. if (ref && store) {
  886. CRYPTO_add(&store->references, 1, CRYPTO_LOCK_X509_STORE);
  887. }
  888. return 1;
  889. }