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.
 
 
 
 
 
 

580 lines
17 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */
  114. #include <openssl/ssl.h>
  115. #include <string.h>
  116. #include <openssl/bn.h>
  117. #include <openssl/buf.h>
  118. #include <openssl/ec_key.h>
  119. #include <openssl/dh.h>
  120. #include <openssl/err.h>
  121. #include <openssl/mem.h>
  122. #include <openssl/x509.h>
  123. #include <openssl/x509v3.h>
  124. #include "../crypto/dh/internal.h"
  125. #include "../crypto/internal.h"
  126. #include "internal.h"
  127. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  128. /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
  129. * reserved app_data slot. Before ex_data was introduced, app_data was used.
  130. * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
  131. * works. */
  132. return 0;
  133. }
  134. CERT *ssl_cert_new(void) {
  135. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  136. if (ret == NULL) {
  137. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  138. return NULL;
  139. }
  140. memset(ret, 0, sizeof(CERT));
  141. return ret;
  142. }
  143. CERT *ssl_cert_dup(CERT *cert) {
  144. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  145. if (ret == NULL) {
  146. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  147. return NULL;
  148. }
  149. memset(ret, 0, sizeof(CERT));
  150. ret->mask_k = cert->mask_k;
  151. ret->mask_a = cert->mask_a;
  152. if (cert->dh_tmp != NULL) {
  153. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  154. if (ret->dh_tmp == NULL) {
  155. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  156. goto err;
  157. }
  158. }
  159. ret->dh_tmp_cb = cert->dh_tmp_cb;
  160. if (cert->x509 != NULL) {
  161. ret->x509 = X509_up_ref(cert->x509);
  162. }
  163. if (cert->privatekey != NULL) {
  164. ret->privatekey = EVP_PKEY_up_ref(cert->privatekey);
  165. }
  166. if (cert->chain) {
  167. ret->chain = X509_chain_up_ref(cert->chain);
  168. if (!ret->chain) {
  169. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  170. goto err;
  171. }
  172. }
  173. ret->key_method = cert->key_method;
  174. ret->cert_cb = cert->cert_cb;
  175. ret->cert_cb_arg = cert->cert_cb_arg;
  176. if (cert->verify_store != NULL) {
  177. X509_STORE_up_ref(cert->verify_store);
  178. ret->verify_store = cert->verify_store;
  179. }
  180. return ret;
  181. err:
  182. ssl_cert_free(ret);
  183. return NULL;
  184. }
  185. /* Free up and clear all certificates and chains */
  186. void ssl_cert_clear_certs(CERT *cert) {
  187. if (cert == NULL) {
  188. return;
  189. }
  190. X509_free(cert->x509);
  191. cert->x509 = NULL;
  192. EVP_PKEY_free(cert->privatekey);
  193. cert->privatekey = NULL;
  194. sk_X509_pop_free(cert->chain, X509_free);
  195. cert->chain = NULL;
  196. cert->key_method = NULL;
  197. }
  198. void ssl_cert_free(CERT *c) {
  199. if (c == NULL) {
  200. return;
  201. }
  202. DH_free(c->dh_tmp);
  203. ssl_cert_clear_certs(c);
  204. OPENSSL_free(c->peer_sigalgs);
  205. OPENSSL_free(c->digest_nids);
  206. X509_STORE_free(c->verify_store);
  207. OPENSSL_free(c);
  208. }
  209. int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  210. sk_X509_pop_free(cert->chain, X509_free);
  211. cert->chain = chain;
  212. return 1;
  213. }
  214. int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  215. STACK_OF(X509) *dchain;
  216. if (chain == NULL) {
  217. return ssl_cert_set0_chain(cert, NULL);
  218. }
  219. dchain = X509_chain_up_ref(chain);
  220. if (dchain == NULL) {
  221. return 0;
  222. }
  223. if (!ssl_cert_set0_chain(cert, dchain)) {
  224. sk_X509_pop_free(dchain, X509_free);
  225. return 0;
  226. }
  227. return 1;
  228. }
  229. int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  230. if (cert->chain == NULL) {
  231. cert->chain = sk_X509_new_null();
  232. }
  233. if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
  234. return 0;
  235. }
  236. return 1;
  237. }
  238. int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  239. if (!ssl_cert_add0_chain_cert(cert, x509)) {
  240. return 0;
  241. }
  242. X509_up_ref(x509);
  243. return 1;
  244. }
  245. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  246. c->cert_cb = cb;
  247. c->cert_cb_arg = arg;
  248. }
  249. int ssl_verify_cert_chain(SSL *ssl, STACK_OF(X509) *cert_chain) {
  250. if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
  251. return 0;
  252. }
  253. X509_STORE *verify_store = ssl->ctx->cert_store;
  254. if (ssl->cert->verify_store != NULL) {
  255. verify_store = ssl->cert->verify_store;
  256. }
  257. X509 *leaf = sk_X509_value(cert_chain, 0);
  258. int ret = 0;
  259. X509_STORE_CTX ctx;
  260. if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
  261. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  262. return 0;
  263. }
  264. if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
  265. ssl)) {
  266. goto err;
  267. }
  268. /* We need to inherit the verify parameters. These can be determined by the
  269. * context: if its a server it will verify SSL client certificates or vice
  270. * versa. */
  271. X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
  272. /* Anything non-default in "param" should overwrite anything in the ctx. */
  273. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
  274. if (ssl->verify_callback) {
  275. X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
  276. }
  277. if (ssl->ctx->app_verify_callback != NULL) {
  278. ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
  279. } else {
  280. ret = X509_verify_cert(&ctx);
  281. }
  282. ssl->verify_result = ctx.error;
  283. err:
  284. X509_STORE_CTX_cleanup(&ctx);
  285. return ret;
  286. }
  287. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  288. STACK_OF(X509_NAME) *name_list) {
  289. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  290. *ca_list = name_list;
  291. }
  292. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
  293. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
  294. if (ret == NULL) {
  295. return NULL;
  296. }
  297. size_t i;
  298. for (i = 0; i < sk_X509_NAME_num(list); i++) {
  299. X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
  300. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  301. X509_NAME_free(name);
  302. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  303. return NULL;
  304. }
  305. }
  306. return ret;
  307. }
  308. void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
  309. set_client_CA_list(&ssl->client_CA, name_list);
  310. }
  311. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  312. set_client_CA_list(&ctx->client_CA, name_list);
  313. }
  314. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  315. return ctx->client_CA;
  316. }
  317. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
  318. /* For historical reasons, this function is used both to query configuration
  319. * state on a server as well as handshake state on a client. However, whether
  320. * |ssl| is a client or server is not known until explicitly configured with
  321. * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
  322. * indeterminate mode and |ssl->server| is unset. */
  323. if (ssl->handshake_func != NULL && !ssl->server) {
  324. return ssl->s3->tmp.ca_names;
  325. }
  326. if (ssl->client_CA != NULL) {
  327. return ssl->client_CA;
  328. }
  329. return ssl->ctx->client_CA;
  330. }
  331. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
  332. X509_NAME *name;
  333. if (x509 == NULL) {
  334. return 0;
  335. }
  336. if (*sk == NULL) {
  337. *sk = sk_X509_NAME_new_null();
  338. if (*sk == NULL) {
  339. return 0;
  340. }
  341. }
  342. name = X509_NAME_dup(X509_get_subject_name(x509));
  343. if (name == NULL) {
  344. return 0;
  345. }
  346. if (!sk_X509_NAME_push(*sk, name)) {
  347. X509_NAME_free(name);
  348. return 0;
  349. }
  350. return 1;
  351. }
  352. int SSL_add_client_CA(SSL *ssl, X509 *x509) {
  353. return add_client_CA(&ssl->client_CA, x509);
  354. }
  355. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
  356. return add_client_CA(&ctx->client_CA, x509);
  357. }
  358. /* Add a certificate to a BUF_MEM structure */
  359. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
  360. int n;
  361. uint8_t *p;
  362. n = i2d_X509(x, NULL);
  363. if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  364. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  365. return 0;
  366. }
  367. p = (uint8_t *)&(buf->data[*l]);
  368. l2n3(n, p);
  369. i2d_X509(x, &p);
  370. *l += n + 3;
  371. return 1;
  372. }
  373. /* Add certificate chain to internal SSL BUF_MEM structure. */
  374. int ssl_add_cert_chain(SSL *ssl, unsigned long *l) {
  375. CERT *cert = ssl->cert;
  376. BUF_MEM *buf = ssl->init_buf;
  377. int no_chain = 0;
  378. size_t i;
  379. X509 *x = cert->x509;
  380. STACK_OF(X509) *chain = cert->chain;
  381. if (x == NULL) {
  382. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  383. return 0;
  384. }
  385. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
  386. no_chain = 1;
  387. }
  388. if (no_chain) {
  389. if (!ssl_add_cert_to_buf(buf, l, x)) {
  390. return 0;
  391. }
  392. for (i = 0; i < sk_X509_num(chain); i++) {
  393. x = sk_X509_value(chain, i);
  394. if (!ssl_add_cert_to_buf(buf, l, x)) {
  395. return 0;
  396. }
  397. }
  398. } else {
  399. X509_STORE_CTX xs_ctx;
  400. if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
  401. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  402. return 0;
  403. }
  404. X509_verify_cert(&xs_ctx);
  405. /* Don't leave errors in the queue */
  406. ERR_clear_error();
  407. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  408. x = sk_X509_value(xs_ctx.chain, i);
  409. if (!ssl_add_cert_to_buf(buf, l, x)) {
  410. X509_STORE_CTX_cleanup(&xs_ctx);
  411. return 0;
  412. }
  413. }
  414. X509_STORE_CTX_cleanup(&xs_ctx);
  415. }
  416. return 1;
  417. }
  418. static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
  419. X509_STORE_free(*store_ptr);
  420. *store_ptr = new_store;
  421. if (new_store != NULL && take_ref) {
  422. X509_STORE_up_ref(new_store);
  423. }
  424. return 1;
  425. }
  426. int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  427. return set_cert_store(&ctx->cert->verify_store, store, 0);
  428. }
  429. int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  430. return set_cert_store(&ctx->cert->verify_store, store, 1);
  431. }
  432. int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
  433. return set_cert_store(&ssl->cert->verify_store, store, 0);
  434. }
  435. int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
  436. return set_cert_store(&ssl->cert->verify_store, store, 1);
  437. }
  438. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  439. return ssl_cert_set0_chain(ctx->cert, chain);
  440. }
  441. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  442. return ssl_cert_set1_chain(ctx->cert, chain);
  443. }
  444. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  445. return ssl_cert_set0_chain(ssl->cert, chain);
  446. }
  447. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  448. return ssl_cert_set1_chain(ssl->cert, chain);
  449. }
  450. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  451. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  452. }
  453. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  454. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  455. }
  456. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  457. return SSL_CTX_add0_chain_cert(ctx, x509);
  458. }
  459. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  460. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  461. }
  462. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  463. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  464. }
  465. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  466. return SSL_CTX_set0_chain(ctx, NULL);
  467. }
  468. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  469. return SSL_CTX_clear_chain_certs(ctx);
  470. }
  471. int SSL_clear_chain_certs(SSL *ssl) {
  472. return SSL_set0_chain(ssl, NULL);
  473. }
  474. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  475. *out_chain = ctx->cert->chain;
  476. return 1;
  477. }
  478. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  479. STACK_OF(X509) **out_chain) {
  480. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  481. }
  482. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  483. *out_chain = ssl->cert->chain;
  484. return 1;
  485. }