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.
 
 
 
 
 
 

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