25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

845 lines
24 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 <assert.h>
  116. #include <limits.h>
  117. #include <string.h>
  118. #include <openssl/bn.h>
  119. #include <openssl/buf.h>
  120. #include <openssl/ec_key.h>
  121. #include <openssl/dh.h>
  122. #include <openssl/err.h>
  123. #include <openssl/mem.h>
  124. #include <openssl/sha.h>
  125. #include <openssl/x509.h>
  126. #include <openssl/x509v3.h>
  127. #include "../crypto/internal.h"
  128. #include "internal.h"
  129. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  130. /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
  131. * reserved app_data slot. Before ex_data was introduced, app_data was used.
  132. * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
  133. * works. */
  134. return 0;
  135. }
  136. CERT *ssl_cert_new(void) {
  137. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  138. if (ret == NULL) {
  139. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  140. return NULL;
  141. }
  142. memset(ret, 0, sizeof(CERT));
  143. return ret;
  144. }
  145. CERT *ssl_cert_dup(CERT *cert) {
  146. CERT *ret = OPENSSL_malloc(sizeof(CERT));
  147. if (ret == NULL) {
  148. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  149. return NULL;
  150. }
  151. memset(ret, 0, sizeof(CERT));
  152. if (cert->x509_leaf != NULL) {
  153. X509_up_ref(cert->x509_leaf);
  154. ret->x509_leaf = cert->x509_leaf;
  155. }
  156. if (cert->privatekey != NULL) {
  157. EVP_PKEY_up_ref(cert->privatekey);
  158. ret->privatekey = cert->privatekey;
  159. }
  160. if (cert->x509_chain) {
  161. ret->x509_chain = X509_chain_up_ref(cert->x509_chain);
  162. if (!ret->x509_chain) {
  163. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  164. goto err;
  165. }
  166. }
  167. ret->key_method = cert->key_method;
  168. ret->mask_k = cert->mask_k;
  169. ret->mask_a = cert->mask_a;
  170. if (cert->dh_tmp != NULL) {
  171. ret->dh_tmp = DHparams_dup(cert->dh_tmp);
  172. if (ret->dh_tmp == NULL) {
  173. OPENSSL_PUT_ERROR(SSL, ERR_R_DH_LIB);
  174. goto err;
  175. }
  176. }
  177. ret->dh_tmp_cb = cert->dh_tmp_cb;
  178. if (cert->sigalgs != NULL) {
  179. ret->sigalgs =
  180. BUF_memdup(cert->sigalgs, cert->num_sigalgs * sizeof(cert->sigalgs[0]));
  181. if (ret->sigalgs == NULL) {
  182. goto err;
  183. }
  184. }
  185. ret->num_sigalgs = cert->num_sigalgs;
  186. ret->cert_cb = cert->cert_cb;
  187. ret->cert_cb_arg = cert->cert_cb_arg;
  188. if (cert->verify_store != NULL) {
  189. X509_STORE_up_ref(cert->verify_store);
  190. ret->verify_store = cert->verify_store;
  191. }
  192. return ret;
  193. err:
  194. ssl_cert_free(ret);
  195. return NULL;
  196. }
  197. /* Free up and clear all certificates and chains */
  198. void ssl_cert_clear_certs(CERT *cert) {
  199. if (cert == NULL) {
  200. return;
  201. }
  202. X509_free(cert->x509_leaf);
  203. cert->x509_leaf = NULL;
  204. EVP_PKEY_free(cert->privatekey);
  205. cert->privatekey = NULL;
  206. sk_X509_pop_free(cert->x509_chain, X509_free);
  207. cert->x509_chain = NULL;
  208. cert->key_method = NULL;
  209. }
  210. void ssl_cert_free(CERT *c) {
  211. if (c == NULL) {
  212. return;
  213. }
  214. DH_free(c->dh_tmp);
  215. ssl_cert_clear_certs(c);
  216. OPENSSL_free(c->sigalgs);
  217. X509_STORE_free(c->verify_store);
  218. OPENSSL_free(c);
  219. }
  220. int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  221. sk_X509_pop_free(cert->x509_chain, X509_free);
  222. cert->x509_chain = chain;
  223. return 1;
  224. }
  225. int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  226. STACK_OF(X509) *dchain;
  227. if (chain == NULL) {
  228. return ssl_cert_set0_chain(cert, NULL);
  229. }
  230. dchain = X509_chain_up_ref(chain);
  231. if (dchain == NULL) {
  232. return 0;
  233. }
  234. if (!ssl_cert_set0_chain(cert, dchain)) {
  235. sk_X509_pop_free(dchain, X509_free);
  236. return 0;
  237. }
  238. return 1;
  239. }
  240. int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  241. if (cert->x509_chain == NULL) {
  242. cert->x509_chain = sk_X509_new_null();
  243. }
  244. if (cert->x509_chain == NULL || !sk_X509_push(cert->x509_chain, x509)) {
  245. return 0;
  246. }
  247. return 1;
  248. }
  249. int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  250. if (!ssl_cert_add0_chain_cert(cert, x509)) {
  251. return 0;
  252. }
  253. X509_up_ref(x509);
  254. return 1;
  255. }
  256. void ssl_cert_set_cert_cb(CERT *c, int (*cb)(SSL *ssl, void *arg), void *arg) {
  257. c->cert_cb = cb;
  258. c->cert_cb_arg = arg;
  259. }
  260. int ssl_verify_cert_chain(SSL *ssl, long *out_verify_result,
  261. STACK_OF(X509) *cert_chain) {
  262. if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
  263. return 0;
  264. }
  265. X509_STORE *verify_store = ssl->ctx->cert_store;
  266. if (ssl->cert->verify_store != NULL) {
  267. verify_store = ssl->cert->verify_store;
  268. }
  269. X509 *leaf = sk_X509_value(cert_chain, 0);
  270. int ret = 0;
  271. X509_STORE_CTX ctx;
  272. if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
  273. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  274. return 0;
  275. }
  276. if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
  277. ssl)) {
  278. goto err;
  279. }
  280. /* We need to inherit the verify parameters. These can be determined by the
  281. * context: if its a server it will verify SSL client certificates or vice
  282. * versa. */
  283. X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
  284. /* Anything non-default in "param" should overwrite anything in the ctx. */
  285. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
  286. if (ssl->verify_callback) {
  287. X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
  288. }
  289. int verify_ret;
  290. if (ssl->ctx->app_verify_callback != NULL) {
  291. verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
  292. } else {
  293. verify_ret = X509_verify_cert(&ctx);
  294. }
  295. *out_verify_result = ctx.error;
  296. /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
  297. if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
  298. ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
  299. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
  300. goto err;
  301. }
  302. ERR_clear_error();
  303. ret = 1;
  304. err:
  305. X509_STORE_CTX_cleanup(&ctx);
  306. return ret;
  307. }
  308. static void set_client_CA_list(STACK_OF(X509_NAME) **ca_list,
  309. STACK_OF(X509_NAME) *name_list) {
  310. sk_X509_NAME_pop_free(*ca_list, X509_NAME_free);
  311. *ca_list = name_list;
  312. }
  313. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
  314. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new_null();
  315. if (ret == NULL) {
  316. return NULL;
  317. }
  318. for (size_t i = 0; i < sk_X509_NAME_num(list); i++) {
  319. X509_NAME *name = X509_NAME_dup(sk_X509_NAME_value(list, i));
  320. if (name == NULL || !sk_X509_NAME_push(ret, name)) {
  321. X509_NAME_free(name);
  322. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  323. return NULL;
  324. }
  325. }
  326. return ret;
  327. }
  328. void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
  329. set_client_CA_list(&ssl->client_CA, name_list);
  330. }
  331. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  332. set_client_CA_list(&ctx->client_CA, name_list);
  333. }
  334. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  335. return ctx->client_CA;
  336. }
  337. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
  338. /* For historical reasons, this function is used both to query configuration
  339. * state on a server as well as handshake state on a client. However, whether
  340. * |ssl| is a client or server is not known until explicitly configured with
  341. * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
  342. * indeterminate mode and |ssl->server| is unset. */
  343. if (ssl->handshake_func != NULL && !ssl->server) {
  344. if (ssl->s3->hs != NULL) {
  345. return ssl->s3->hs->ca_names;
  346. }
  347. return NULL;
  348. }
  349. if (ssl->client_CA != NULL) {
  350. return ssl->client_CA;
  351. }
  352. return ssl->ctx->client_CA;
  353. }
  354. static int add_client_CA(STACK_OF(X509_NAME) **sk, X509 *x509) {
  355. X509_NAME *name;
  356. if (x509 == NULL) {
  357. return 0;
  358. }
  359. if (*sk == NULL) {
  360. *sk = sk_X509_NAME_new_null();
  361. if (*sk == NULL) {
  362. return 0;
  363. }
  364. }
  365. name = X509_NAME_dup(X509_get_subject_name(x509));
  366. if (name == NULL) {
  367. return 0;
  368. }
  369. if (!sk_X509_NAME_push(*sk, name)) {
  370. X509_NAME_free(name);
  371. return 0;
  372. }
  373. return 1;
  374. }
  375. int SSL_add_client_CA(SSL *ssl, X509 *x509) {
  376. return add_client_CA(&ssl->client_CA, x509);
  377. }
  378. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
  379. return add_client_CA(&ctx->client_CA, x509);
  380. }
  381. int ssl_has_certificate(const SSL *ssl) {
  382. return ssl->cert->x509_leaf != NULL && ssl_has_private_key(ssl);
  383. }
  384. X509 *ssl_parse_x509(CBS *cbs) {
  385. if (CBS_len(cbs) > LONG_MAX) {
  386. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  387. return NULL;
  388. }
  389. const uint8_t *ptr = CBS_data(cbs);
  390. X509 *ret = d2i_X509(NULL, &ptr, (long)CBS_len(cbs));
  391. if (ret == NULL) {
  392. return NULL;
  393. }
  394. CBS_skip(cbs, ptr - CBS_data(cbs));
  395. return ret;
  396. }
  397. STACK_OF(X509) *ssl_parse_cert_chain(SSL *ssl, uint8_t *out_alert,
  398. uint8_t *out_leaf_sha256, CBS *cbs) {
  399. STACK_OF(X509) *ret = sk_X509_new_null();
  400. if (ret == NULL) {
  401. *out_alert = SSL_AD_INTERNAL_ERROR;
  402. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  403. return NULL;
  404. }
  405. X509 *x = NULL;
  406. CBS certificate_list;
  407. if (!CBS_get_u24_length_prefixed(cbs, &certificate_list)) {
  408. *out_alert = SSL_AD_DECODE_ERROR;
  409. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  410. goto err;
  411. }
  412. while (CBS_len(&certificate_list) > 0) {
  413. CBS certificate;
  414. if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) {
  415. *out_alert = SSL_AD_DECODE_ERROR;
  416. OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
  417. goto err;
  418. }
  419. /* Retain the hash of the leaf certificate if requested. */
  420. if (sk_X509_num(ret) == 0 && out_leaf_sha256 != NULL) {
  421. SHA256(CBS_data(&certificate), CBS_len(&certificate), out_leaf_sha256);
  422. }
  423. x = ssl_parse_x509(&certificate);
  424. if (x == NULL || CBS_len(&certificate) != 0) {
  425. *out_alert = SSL_AD_DECODE_ERROR;
  426. goto err;
  427. }
  428. if (!sk_X509_push(ret, x)) {
  429. *out_alert = SSL_AD_INTERNAL_ERROR;
  430. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  431. goto err;
  432. }
  433. x = NULL;
  434. }
  435. return ret;
  436. err:
  437. X509_free(x);
  438. sk_X509_pop_free(ret, X509_free);
  439. return NULL;
  440. }
  441. int ssl_add_cert_to_cbb(CBB *cbb, X509 *x509) {
  442. int len = i2d_X509(x509, NULL);
  443. if (len < 0) {
  444. return 0;
  445. }
  446. uint8_t *buf;
  447. if (!CBB_add_space(cbb, &buf, len)) {
  448. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  449. return 0;
  450. }
  451. if (buf != NULL && i2d_X509(x509, &buf) < 0) {
  452. return 0;
  453. }
  454. return 1;
  455. }
  456. static int ssl_add_cert_with_length(CBB *cbb, X509 *x509) {
  457. CBB child;
  458. return CBB_add_u24_length_prefixed(cbb, &child) &&
  459. ssl_add_cert_to_cbb(&child, x509) &&
  460. CBB_flush(cbb);
  461. }
  462. int ssl_add_cert_chain(SSL *ssl, CBB *cbb) {
  463. if (!ssl_has_certificate(ssl)) {
  464. return CBB_add_u24(cbb, 0);
  465. }
  466. CERT *cert = ssl->cert;
  467. X509 *x = cert->x509_leaf;
  468. CBB child;
  469. if (!CBB_add_u24_length_prefixed(cbb, &child)) {
  470. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  471. return 0;
  472. }
  473. int no_chain = 0;
  474. STACK_OF(X509) *chain = cert->x509_chain;
  475. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) || chain != NULL) {
  476. no_chain = 1;
  477. }
  478. if (no_chain) {
  479. if (!ssl_add_cert_with_length(&child, x)) {
  480. return 0;
  481. }
  482. for (size_t i = 0; i < sk_X509_num(chain); i++) {
  483. x = sk_X509_value(chain, i);
  484. if (!ssl_add_cert_with_length(&child, x)) {
  485. return 0;
  486. }
  487. }
  488. } else {
  489. X509_STORE_CTX xs_ctx;
  490. if (!X509_STORE_CTX_init(&xs_ctx, ssl->ctx->cert_store, x, NULL)) {
  491. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  492. return 0;
  493. }
  494. X509_verify_cert(&xs_ctx);
  495. /* Don't leave errors in the queue */
  496. ERR_clear_error();
  497. for (size_t i = 0; i < sk_X509_num(xs_ctx.chain); i++) {
  498. x = sk_X509_value(xs_ctx.chain, i);
  499. if (!ssl_add_cert_with_length(&child, x)) {
  500. X509_STORE_CTX_cleanup(&xs_ctx);
  501. return 0;
  502. }
  503. }
  504. X509_STORE_CTX_cleanup(&xs_ctx);
  505. }
  506. return CBB_flush(cbb);
  507. }
  508. static int ca_dn_cmp(const X509_NAME **a, const X509_NAME **b) {
  509. return X509_NAME_cmp(*a, *b);
  510. }
  511. STACK_OF(X509_NAME) *
  512. ssl_parse_client_CA_list(SSL *ssl, uint8_t *out_alert, CBS *cbs) {
  513. STACK_OF(X509_NAME) *ret = sk_X509_NAME_new(ca_dn_cmp);
  514. X509_NAME *name = NULL;
  515. if (ret == NULL) {
  516. *out_alert = SSL_AD_INTERNAL_ERROR;
  517. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  518. return NULL;
  519. }
  520. CBS child;
  521. if (!CBS_get_u16_length_prefixed(cbs, &child)) {
  522. *out_alert = SSL_AD_DECODE_ERROR;
  523. OPENSSL_PUT_ERROR(SSL, SSL_R_LENGTH_MISMATCH);
  524. goto err;
  525. }
  526. while (CBS_len(&child) > 0) {
  527. CBS distinguished_name;
  528. if (!CBS_get_u16_length_prefixed(&child, &distinguished_name)) {
  529. *out_alert = SSL_AD_DECODE_ERROR;
  530. OPENSSL_PUT_ERROR(SSL, SSL_R_CA_DN_TOO_LONG);
  531. goto err;
  532. }
  533. const uint8_t *ptr = CBS_data(&distinguished_name);
  534. /* A u16 length cannot overflow a long. */
  535. name = d2i_X509_NAME(NULL, &ptr, (long)CBS_len(&distinguished_name));
  536. if (name == NULL ||
  537. ptr != CBS_data(&distinguished_name) + CBS_len(&distinguished_name)) {
  538. *out_alert = SSL_AD_DECODE_ERROR;
  539. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  540. goto err;
  541. }
  542. if (!sk_X509_NAME_push(ret, name)) {
  543. *out_alert = SSL_AD_INTERNAL_ERROR;
  544. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  545. goto err;
  546. }
  547. name = NULL;
  548. }
  549. return ret;
  550. err:
  551. X509_NAME_free(name);
  552. sk_X509_NAME_pop_free(ret, X509_NAME_free);
  553. return NULL;
  554. }
  555. int ssl_add_client_CA_list(SSL *ssl, CBB *cbb) {
  556. CBB child, name_cbb;
  557. if (!CBB_add_u16_length_prefixed(cbb, &child)) {
  558. return 0;
  559. }
  560. STACK_OF(X509_NAME) *sk = SSL_get_client_CA_list(ssl);
  561. if (sk == NULL) {
  562. return CBB_flush(cbb);
  563. }
  564. for (size_t i = 0; i < sk_X509_NAME_num(sk); i++) {
  565. X509_NAME *name = sk_X509_NAME_value(sk, i);
  566. int len = i2d_X509_NAME(name, NULL);
  567. if (len < 0) {
  568. return 0;
  569. }
  570. uint8_t *ptr;
  571. if (!CBB_add_u16_length_prefixed(&child, &name_cbb) ||
  572. !CBB_add_space(&name_cbb, &ptr, (size_t)len) ||
  573. (len > 0 && i2d_X509_NAME(name, &ptr) < 0)) {
  574. return 0;
  575. }
  576. }
  577. return CBB_flush(cbb);
  578. }
  579. int ssl_do_client_cert_cb(SSL *ssl, int *out_should_retry) {
  580. if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
  581. return 1;
  582. }
  583. X509 *x509 = NULL;
  584. EVP_PKEY *pkey = NULL;
  585. int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
  586. if (ret < 0) {
  587. *out_should_retry = 1;
  588. return 0;
  589. }
  590. if (ret != 0) {
  591. if (!SSL_use_certificate(ssl, x509) ||
  592. !SSL_use_PrivateKey(ssl, pkey)) {
  593. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
  594. *out_should_retry = 0;
  595. return 0;
  596. }
  597. }
  598. X509_free(x509);
  599. EVP_PKEY_free(pkey);
  600. return 1;
  601. }
  602. static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store, int take_ref) {
  603. X509_STORE_free(*store_ptr);
  604. *store_ptr = new_store;
  605. if (new_store != NULL && take_ref) {
  606. X509_STORE_up_ref(new_store);
  607. }
  608. return 1;
  609. }
  610. int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  611. return set_cert_store(&ctx->cert->verify_store, store, 0);
  612. }
  613. int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  614. return set_cert_store(&ctx->cert->verify_store, store, 1);
  615. }
  616. int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
  617. return set_cert_store(&ssl->cert->verify_store, store, 0);
  618. }
  619. int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
  620. return set_cert_store(&ssl->cert->verify_store, store, 1);
  621. }
  622. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  623. return ssl_cert_set0_chain(ctx->cert, chain);
  624. }
  625. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  626. return ssl_cert_set1_chain(ctx->cert, chain);
  627. }
  628. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  629. return ssl_cert_set0_chain(ssl->cert, chain);
  630. }
  631. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  632. return ssl_cert_set1_chain(ssl->cert, chain);
  633. }
  634. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  635. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  636. }
  637. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  638. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  639. }
  640. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  641. return SSL_CTX_add0_chain_cert(ctx, x509);
  642. }
  643. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  644. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  645. }
  646. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  647. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  648. }
  649. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  650. return SSL_CTX_set0_chain(ctx, NULL);
  651. }
  652. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  653. return SSL_CTX_clear_chain_certs(ctx);
  654. }
  655. int SSL_clear_chain_certs(SSL *ssl) {
  656. return SSL_set0_chain(ssl, NULL);
  657. }
  658. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  659. *out_chain = ctx->cert->x509_chain;
  660. return 1;
  661. }
  662. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  663. STACK_OF(X509) **out_chain) {
  664. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  665. }
  666. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  667. *out_chain = ssl->cert->x509_chain;
  668. return 1;
  669. }
  670. int ssl_check_leaf_certificate(SSL *ssl, X509 *leaf) {
  671. assert(ssl3_protocol_version(ssl) < TLS1_3_VERSION);
  672. int ret = 0;
  673. EVP_PKEY *pkey = X509_get_pubkey(leaf);
  674. if (pkey == NULL) {
  675. goto err;
  676. }
  677. /* Check the certificate's type matches the cipher. */
  678. const SSL_CIPHER *cipher = ssl->s3->tmp.new_cipher;
  679. int expected_type = ssl_cipher_get_key_type(cipher);
  680. assert(expected_type != EVP_PKEY_NONE);
  681. if (pkey->type != expected_type) {
  682. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CERTIFICATE_TYPE);
  683. goto err;
  684. }
  685. if (cipher->algorithm_auth & SSL_aECDSA) {
  686. /* TODO(davidben): This behavior is preserved from upstream. Should key
  687. * usages be checked in other cases as well? */
  688. /* This call populates the ex_flags field correctly */
  689. X509_check_purpose(leaf, -1, 0);
  690. if ((leaf->ex_flags & EXFLAG_KUSAGE) &&
  691. !(leaf->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)) {
  692. OPENSSL_PUT_ERROR(SSL, SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  693. goto err;
  694. }
  695. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(pkey);
  696. if (ec_key == NULL) {
  697. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  698. goto err;
  699. }
  700. /* Check the key's group and point format are acceptable. */
  701. uint16_t group_id;
  702. if (!ssl_nid_to_group_id(
  703. &group_id, EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key))) ||
  704. !tls1_check_group_id(ssl, group_id) ||
  705. EC_KEY_get_conv_form(ec_key) != POINT_CONVERSION_UNCOMPRESSED) {
  706. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_ECC_CERT);
  707. goto err;
  708. }
  709. }
  710. ret = 1;
  711. err:
  712. EVP_PKEY_free(pkey);
  713. return ret;
  714. }