Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

540 řádky
16 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 = (CERT *)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 = (CERT *)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->cert_cb = cert->cert_cb;
  174. ret->cert_cb_arg = cert->cert_cb_arg;
  175. return ret;
  176. err:
  177. ssl_cert_free(ret);
  178. return NULL;
  179. }
  180. /* Free up and clear all certificates and chains */
  181. void ssl_cert_clear_certs(CERT *cert) {
  182. if (cert == NULL) {
  183. return;
  184. }
  185. X509_free(cert->x509);
  186. cert->x509 = NULL;
  187. EVP_PKEY_free(cert->privatekey);
  188. cert->privatekey = NULL;
  189. sk_X509_pop_free(cert->chain, X509_free);
  190. cert->chain = NULL;
  191. cert->key_method = NULL;
  192. }
  193. void ssl_cert_free(CERT *c) {
  194. if (c == NULL) {
  195. return;
  196. }
  197. DH_free(c->dh_tmp);
  198. ssl_cert_clear_certs(c);
  199. OPENSSL_free(c->peer_sigalgs);
  200. OPENSSL_free(c->digest_nids);
  201. OPENSSL_free(c);
  202. }
  203. int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  204. sk_X509_pop_free(cert->chain, X509_free);
  205. cert->chain = chain;
  206. return 1;
  207. }
  208. int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  209. STACK_OF(X509) *dchain;
  210. if (chain == NULL) {
  211. return ssl_cert_set0_chain(cert, NULL);
  212. }
  213. dchain = X509_chain_up_ref(chain);
  214. if (dchain == NULL) {
  215. return 0;
  216. }
  217. if (!ssl_cert_set0_chain(cert, dchain)) {
  218. sk_X509_pop_free(dchain, X509_free);
  219. return 0;
  220. }
  221. return 1;
  222. }
  223. int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  224. if (cert->chain == NULL) {
  225. cert->chain = sk_X509_new_null();
  226. }
  227. if (cert->chain == NULL || !sk_X509_push(cert->chain, x509)) {
  228. return 0;
  229. }
  230. return 1;
  231. }
  232. int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  233. if (!ssl_cert_add0_chain_cert(cert, x509)) {
  234. return 0;
  235. }
  236. X509_up_ref(x509);
  237. return 1;
  238. }
  239. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  240. c->cert_cb = cb;
  241. c->cert_cb_arg = arg;
  242. }
  243. int ssl_verify_cert_chain(SSL *ssl, STACK_OF(X509) *cert_chain) {
  244. if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
  245. return 0;
  246. }
  247. X509 *leaf = sk_X509_value(cert_chain, 0);
  248. int ret = 0;
  249. X509_STORE_CTX ctx;
  250. if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, cert_chain)) {
  251. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  252. return 0;
  253. }
  254. if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
  255. ssl)) {
  256. goto err;
  257. }
  258. /* We need to inherit the verify parameters. These can be determined by the
  259. * context: if its a server it will verify SSL client certificates or vice
  260. * versa. */
  261. X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
  262. /* Anything non-default in "param" should overwrite anything in the ctx. */
  263. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
  264. if (ssl->verify_callback) {
  265. X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
  266. }
  267. if (ssl->ctx->app_verify_callback != NULL) {
  268. ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
  269. } else {
  270. ret = X509_verify_cert(&ctx);
  271. }
  272. ssl->verify_result = ctx.error;
  273. err:
  274. X509_STORE_CTX_cleanup(&ctx);
  275. return ret;
  276. }
  277. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  278. STACK_OF(X509_NAME) *name_list) {
  279. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  280. *ca_list = name_list;
  281. }
  282. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
  283. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
  284. if (ret == NULL) {
  285. return NULL;
  286. }
  287. size_t i;
  288. for (i = 0; i < sk_X509_NAME_num(list); i++) {
  289. X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
  290. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  291. X509_NAME_free(name);
  292. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  293. return NULL;
  294. }
  295. }
  296. return ret;
  297. }
  298. void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
  299. set_client_CA_list(&ssl->client_CA, name_list);
  300. }
  301. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  302. set_client_CA_list(&ctx->client_CA, name_list);
  303. }
  304. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  305. return ctx->client_CA;
  306. }
  307. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
  308. /* For historical reasons, this function is used both to query configuration
  309. * state on a server as well as handshake state on a client. However, whether
  310. * |ssl| is a client or server is not known until explicitly configured with
  311. * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
  312. * indeterminate mode and |ssl->server| is unset. */
  313. if (ssl->handshake_func != NULL && !ssl->server) {
  314. return ssl->s3->tmp.ca_names;
  315. }
  316. if (ssl->client_CA != NULL) {
  317. return ssl->client_CA;
  318. }
  319. return ssl->ctx->client_CA;
  320. }
  321. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
  322. X509_NAME *name;
  323. if (x509 == NULL) {
  324. return 0;
  325. }
  326. if (*sk == NULL) {
  327. *sk = sk_X509_NAME_new_null();
  328. if (*sk == NULL) {
  329. return 0;
  330. }
  331. }
  332. name = X509_NAME_dup(X509_get_subject_name(x509));
  333. if (name == NULL) {
  334. return 0;
  335. }
  336. if (!sk_X509_NAME_push(*sk, name)) {
  337. X509_NAME_free(name);
  338. return 0;
  339. }
  340. return 1;
  341. }
  342. int SSL_add_client_CA(SSL *ssl, X509 *x509) {
  343. return add_client_CA(&ssl->client_CA, x509);
  344. }
  345. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
  346. return add_client_CA(&ctx->client_CA, x509);
  347. }
  348. /* Add a certificate to a BUF_MEM structure */
  349. static int ssl_add_cert_to_buf(BUF_MEM *buf, unsigned long *l, X509 *x) {
  350. int n;
  351. uint8_t *p;
  352. n = i2d_X509(x, NULL);
  353. if (!BUF_MEM_grow_clean(buf, (int)(n + (*l) + 3))) {
  354. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  355. return 0;
  356. }
  357. p = (uint8_t *)&(buf->data[*l]);
  358. l2n3(n, p);
  359. i2d_X509(x, &p);
  360. *l += n + 3;
  361. return 1;
  362. }
  363. /* Add certificate chain to internal SSL BUF_MEM structure. */
  364. int ssl_add_cert_chain(SSL *ssl, unsigned long *l) {
  365. CERT *cert = ssl->cert;
  366. BUF_MEM *buf = ssl->init_buf;
  367. int no_chain = 0;
  368. size_t i;
  369. X509 *x = cert->x509;
  370. STACK_OF(X509) *chain = cert->chain;
  371. if (x == NULL) {
  372. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET);
  373. return 0;
  374. }
  375. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
  376. no_chain = 1;
  377. }
  378. if (no_chain) {
  379. if (!ssl_add_cert_to_buf(buf, l, x)) {
  380. return 0;
  381. }
  382. for (i = 0; i < sk_X509_num(chain); i++) {
  383. x = sk_X509_value(chain, i);
  384. if (!ssl_add_cert_to_buf(buf, l, x)) {
  385. return 0;
  386. }
  387. }
  388. } else {
  389. X509_STORE_CTX xs_ctx;
  390. if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
  391. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  392. return 0;
  393. }
  394. X509_verify_cert(&xs_ctx);
  395. /* Don't leave errors in the queue */
  396. ERR_clear_error();
  397. for (i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  398. x = sk_X509_value(xs_ctx.chain, i);
  399. if (!ssl_add_cert_to_buf(buf, l, x)) {
  400. X509_STORE_CTX_cleanup(&xs_ctx);
  401. return 0;
  402. }
  403. }
  404. X509_STORE_CTX_cleanup(&xs_ctx);
  405. }
  406. return 1;
  407. }
  408. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  409. return ssl_cert_set0_chain(ctx->cert, chain);
  410. }
  411. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  412. return ssl_cert_set1_chain(ctx->cert, chain);
  413. }
  414. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  415. return ssl_cert_set0_chain(ssl->cert, chain);
  416. }
  417. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  418. return ssl_cert_set1_chain(ssl->cert, chain);
  419. }
  420. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  421. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  422. }
  423. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  424. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  425. }
  426. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  427. return SSL_CTX_add0_chain_cert(ctx, x509);
  428. }
  429. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  430. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  431. }
  432. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  433. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  434. }
  435. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  436. return SSL_CTX_set0_chain(ctx, NULL);
  437. }
  438. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  439. return SSL_CTX_clear_chain_certs(ctx);
  440. }
  441. int SSL_clear_chain_certs(SSL *ssl) {
  442. return SSL_set0_chain(ssl, NULL);
  443. }
  444. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  445. *out_chain = ctx->cert->chain;
  446. return 1;
  447. }
  448. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  449. STACK_OF(X509) **out_chain) {
  450. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  451. }
  452. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  453. *out_chain = ssl->cert->chain;
  454. return 1;
  455. }