No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

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