Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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