You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1345 lines
40 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. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <openssl/asn1.h>
  143. #include <openssl/bytestring.h>
  144. #include <openssl/err.h>
  145. #include <openssl/pem.h>
  146. #include <openssl/stack.h>
  147. #include <openssl/x509.h>
  148. #include <openssl/x509v3.h>
  149. #include <openssl/x509_vfy.h>
  150. #include "internal.h"
  151. #include "../crypto/internal.h"
  152. /* check_ssl_x509_method asserts that |ssl| has the X509-based method
  153. * installed. Calling an X509-based method on an |ssl| with a different method
  154. * will likely misbehave and possibly crash or leak memory. */
  155. static void check_ssl_x509_method(const SSL *ssl) {
  156. assert(ssl == NULL || ssl->ctx->x509_method == &ssl_crypto_x509_method);
  157. }
  158. /* check_ssl_ctx_x509_method acts like |check_ssl_x509_method|, but for an
  159. * |SSL_CTX|. */
  160. static void check_ssl_ctx_x509_method(const SSL_CTX *ctx) {
  161. assert(ctx == NULL || ctx->x509_method == &ssl_crypto_x509_method);
  162. }
  163. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  164. check_ssl_x509_method(ssl);
  165. if (ssl == NULL) {
  166. return NULL;
  167. }
  168. SSL_SESSION *session = SSL_get_session(ssl);
  169. if (session == NULL || session->x509_peer == NULL) {
  170. return NULL;
  171. }
  172. X509_up_ref(session->x509_peer);
  173. return session->x509_peer;
  174. }
  175. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  176. check_ssl_x509_method(ssl);
  177. if (ssl == NULL) {
  178. return NULL;
  179. }
  180. SSL_SESSION *session = SSL_get_session(ssl);
  181. if (session == NULL ||
  182. session->x509_chain == NULL) {
  183. return NULL;
  184. }
  185. if (!ssl->server) {
  186. return session->x509_chain;
  187. }
  188. /* OpenSSL historically didn't include the leaf certificate in the returned
  189. * certificate chain, but only for servers. */
  190. if (session->x509_chain_without_leaf == NULL) {
  191. session->x509_chain_without_leaf = sk_X509_new_null();
  192. if (session->x509_chain_without_leaf == NULL) {
  193. return NULL;
  194. }
  195. for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) {
  196. X509 *cert = sk_X509_value(session->x509_chain, i);
  197. if (!sk_X509_push(session->x509_chain_without_leaf, cert)) {
  198. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  199. session->x509_chain_without_leaf = NULL;
  200. return NULL;
  201. }
  202. X509_up_ref(cert);
  203. }
  204. }
  205. return session->x509_chain_without_leaf;
  206. }
  207. STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
  208. check_ssl_x509_method(ssl);
  209. SSL_SESSION *session = SSL_get_session(ssl);
  210. if (session == NULL) {
  211. return NULL;
  212. }
  213. return session->x509_chain;
  214. }
  215. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  216. check_ssl_ctx_x509_method(ctx);
  217. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  218. }
  219. int SSL_set_purpose(SSL *ssl, int purpose) {
  220. check_ssl_x509_method(ssl);
  221. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  222. }
  223. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  224. check_ssl_ctx_x509_method(ctx);
  225. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  226. }
  227. int SSL_set_trust(SSL *ssl, int trust) {
  228. check_ssl_x509_method(ssl);
  229. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  230. }
  231. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  232. check_ssl_ctx_x509_method(ctx);
  233. return X509_VERIFY_PARAM_set1(ctx->param, param);
  234. }
  235. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  236. check_ssl_x509_method(ssl);
  237. return X509_VERIFY_PARAM_set1(ssl->param, param);
  238. }
  239. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) {
  240. check_ssl_ctx_x509_method(ctx);
  241. return ctx->param;
  242. }
  243. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) {
  244. check_ssl_x509_method(ssl);
  245. return ssl->param;
  246. }
  247. int SSL_get_verify_depth(const SSL *ssl) {
  248. check_ssl_x509_method(ssl);
  249. return X509_VERIFY_PARAM_get_depth(ssl->param);
  250. }
  251. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  252. check_ssl_x509_method(ssl);
  253. return ssl->verify_callback;
  254. }
  255. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) {
  256. check_ssl_ctx_x509_method(ctx);
  257. return ctx->verify_mode;
  258. }
  259. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  260. check_ssl_ctx_x509_method(ctx);
  261. return X509_VERIFY_PARAM_get_depth(ctx->param);
  262. }
  263. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  264. int ok, X509_STORE_CTX *store_ctx) {
  265. check_ssl_ctx_x509_method(ctx);
  266. return ctx->default_verify_callback;
  267. }
  268. void SSL_set_verify(SSL *ssl, int mode,
  269. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  270. check_ssl_x509_method(ssl);
  271. ssl->verify_mode = mode;
  272. if (callback != NULL) {
  273. ssl->verify_callback = callback;
  274. }
  275. }
  276. void SSL_set_verify_depth(SSL *ssl, int depth) {
  277. check_ssl_x509_method(ssl);
  278. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  279. }
  280. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  281. int (*cb)(X509_STORE_CTX *store_ctx,
  282. void *arg),
  283. void *arg) {
  284. check_ssl_ctx_x509_method(ctx);
  285. ctx->app_verify_callback = cb;
  286. ctx->app_verify_arg = arg;
  287. }
  288. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  289. int (*cb)(int, X509_STORE_CTX *)) {
  290. check_ssl_ctx_x509_method(ctx);
  291. ctx->verify_mode = mode;
  292. ctx->default_verify_callback = cb;
  293. }
  294. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  295. check_ssl_ctx_x509_method(ctx);
  296. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  297. }
  298. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  299. check_ssl_ctx_x509_method(ctx);
  300. return X509_STORE_set_default_paths(ctx->cert_store);
  301. }
  302. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  303. const char *ca_dir) {
  304. check_ssl_ctx_x509_method(ctx);
  305. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  306. }
  307. void SSL_set_verify_result(SSL *ssl, long result) {
  308. check_ssl_x509_method(ssl);
  309. if (result != X509_V_OK) {
  310. abort();
  311. }
  312. }
  313. long SSL_get_verify_result(const SSL *ssl) {
  314. check_ssl_x509_method(ssl);
  315. SSL_SESSION *session = SSL_get_session(ssl);
  316. if (session == NULL) {
  317. return X509_V_ERR_INVALID_CALL;
  318. }
  319. return session->verify_result;
  320. }
  321. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  322. check_ssl_ctx_x509_method(ctx);
  323. return ctx->cert_store;
  324. }
  325. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  326. check_ssl_ctx_x509_method(ctx);
  327. X509_STORE_free(ctx->cert_store);
  328. ctx->cert_store = store;
  329. }
  330. /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
  331. * contents of |x509|. */
  332. static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) {
  333. uint8_t *buf = NULL;
  334. int cert_len = i2d_X509(x509, &buf);
  335. if (cert_len <= 0) {
  336. return 0;
  337. }
  338. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL);
  339. OPENSSL_free(buf);
  340. return buffer;
  341. }
  342. /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */
  343. static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) {
  344. STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null();
  345. if (chain == NULL) {
  346. return NULL;
  347. }
  348. if (!sk_CRYPTO_BUFFER_push(chain, NULL)) {
  349. sk_CRYPTO_BUFFER_free(chain);
  350. return NULL;
  351. }
  352. return chain;
  353. }
  354. /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised
  355. * forms of elements of |chain|. It returns one on success or zero on error, in
  356. * which case no change to |cert->chain| is made. It preverses the existing
  357. * leaf from |cert->chain|, if any. */
  358. static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) {
  359. STACK_OF(CRYPTO_BUFFER) *new_chain = NULL;
  360. if (cert->chain != NULL) {
  361. new_chain = sk_CRYPTO_BUFFER_new_null();
  362. if (new_chain == NULL) {
  363. return 0;
  364. }
  365. CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
  366. if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) {
  367. goto err;
  368. }
  369. /* |leaf| might be NULL if it's a “leafless” chain. */
  370. if (leaf != NULL) {
  371. CRYPTO_BUFFER_up_ref(leaf);
  372. }
  373. }
  374. for (size_t i = 0; i < sk_X509_num(chain); i++) {
  375. if (new_chain == NULL) {
  376. new_chain = new_leafless_chain();
  377. if (new_chain == NULL) {
  378. goto err;
  379. }
  380. }
  381. CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i));
  382. if (buffer == NULL ||
  383. !sk_CRYPTO_BUFFER_push(new_chain, buffer)) {
  384. CRYPTO_BUFFER_free(buffer);
  385. goto err;
  386. }
  387. }
  388. sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
  389. cert->chain = new_chain;
  390. return 1;
  391. err:
  392. sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free);
  393. return 0;
  394. }
  395. static void ssl_crypto_x509_cert_flush_cached_leaf(CERT *cert) {
  396. X509_free(cert->x509_leaf);
  397. cert->x509_leaf = NULL;
  398. }
  399. static void ssl_crypto_x509_cert_flush_cached_chain(CERT *cert) {
  400. sk_X509_pop_free(cert->x509_chain, X509_free);
  401. cert->x509_chain = NULL;
  402. }
  403. static int ssl_crypto_x509_check_client_CA_list(
  404. STACK_OF(CRYPTO_BUFFER) *names) {
  405. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
  406. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i);
  407. const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
  408. X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer));
  409. const int ok = name != NULL && inp == CRYPTO_BUFFER_data(buffer) +
  410. CRYPTO_BUFFER_len(buffer);
  411. X509_NAME_free(name);
  412. if (!ok) {
  413. return 0;
  414. }
  415. }
  416. return 1;
  417. }
  418. static void ssl_crypto_x509_cert_clear(CERT *cert) {
  419. ssl_crypto_x509_cert_flush_cached_leaf(cert);
  420. ssl_crypto_x509_cert_flush_cached_chain(cert);
  421. X509_free(cert->x509_stash);
  422. cert->x509_stash = NULL;
  423. }
  424. static void ssl_crypto_x509_cert_free(CERT *cert) {
  425. ssl_crypto_x509_cert_clear(cert);
  426. X509_STORE_free(cert->verify_store);
  427. }
  428. static void ssl_crypto_x509_cert_dup(CERT *new_cert, const CERT *cert) {
  429. if (cert->verify_store != NULL) {
  430. X509_STORE_up_ref(cert->verify_store);
  431. new_cert->verify_store = cert->verify_store;
  432. }
  433. }
  434. static int ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) {
  435. STACK_OF(X509) *chain = NULL;
  436. const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs);
  437. if (num_certs > 0) {
  438. chain = sk_X509_new_null();
  439. if (chain == NULL) {
  440. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  441. goto err;
  442. }
  443. }
  444. X509 *leaf = NULL;
  445. for (size_t i = 0; i < num_certs; i++) {
  446. X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i));
  447. if (x509 == NULL) {
  448. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  449. goto err;
  450. }
  451. if (!sk_X509_push(chain, x509)) {
  452. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  453. X509_free(x509);
  454. goto err;
  455. }
  456. if (i == 0) {
  457. leaf = x509;
  458. }
  459. }
  460. sk_X509_pop_free(sess->x509_chain, X509_free);
  461. sess->x509_chain = chain;
  462. sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free);
  463. sess->x509_chain_without_leaf = NULL;
  464. X509_free(sess->x509_peer);
  465. if (leaf != NULL) {
  466. X509_up_ref(leaf);
  467. }
  468. sess->x509_peer = leaf;
  469. return 1;
  470. err:
  471. sk_X509_pop_free(chain, X509_free);
  472. return 0;
  473. }
  474. static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session,
  475. const SSL_SESSION *session) {
  476. if (session->x509_peer != NULL) {
  477. X509_up_ref(session->x509_peer);
  478. new_session->x509_peer = session->x509_peer;
  479. }
  480. if (session->x509_chain != NULL) {
  481. new_session->x509_chain = X509_chain_up_ref(session->x509_chain);
  482. if (new_session->x509_chain == NULL) {
  483. return 0;
  484. }
  485. }
  486. return 1;
  487. }
  488. static void ssl_crypto_x509_session_clear(SSL_SESSION *session) {
  489. X509_free(session->x509_peer);
  490. session->x509_peer = NULL;
  491. sk_X509_pop_free(session->x509_chain, X509_free);
  492. session->x509_chain = NULL;
  493. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  494. session->x509_chain_without_leaf = NULL;
  495. }
  496. static int ssl_verify_alarm_type(long type) {
  497. switch (type) {
  498. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
  499. case X509_V_ERR_UNABLE_TO_GET_CRL:
  500. case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
  501. return SSL_AD_UNKNOWN_CA;
  502. case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
  503. case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
  504. case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
  505. case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
  506. case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
  507. case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
  508. case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
  509. case X509_V_ERR_CERT_NOT_YET_VALID:
  510. case X509_V_ERR_CRL_NOT_YET_VALID:
  511. case X509_V_ERR_CERT_UNTRUSTED:
  512. case X509_V_ERR_CERT_REJECTED:
  513. case X509_V_ERR_HOSTNAME_MISMATCH:
  514. case X509_V_ERR_EMAIL_MISMATCH:
  515. case X509_V_ERR_IP_ADDRESS_MISMATCH:
  516. return SSL_AD_BAD_CERTIFICATE;
  517. case X509_V_ERR_CERT_SIGNATURE_FAILURE:
  518. case X509_V_ERR_CRL_SIGNATURE_FAILURE:
  519. return SSL_AD_DECRYPT_ERROR;
  520. case X509_V_ERR_CERT_HAS_EXPIRED:
  521. case X509_V_ERR_CRL_HAS_EXPIRED:
  522. return SSL_AD_CERTIFICATE_EXPIRED;
  523. case X509_V_ERR_CERT_REVOKED:
  524. return SSL_AD_CERTIFICATE_REVOKED;
  525. case X509_V_ERR_UNSPECIFIED:
  526. case X509_V_ERR_OUT_OF_MEM:
  527. case X509_V_ERR_INVALID_CALL:
  528. case X509_V_ERR_STORE_LOOKUP:
  529. return SSL_AD_INTERNAL_ERROR;
  530. case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
  531. case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
  532. case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
  533. case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
  534. case X509_V_ERR_CERT_CHAIN_TOO_LONG:
  535. case X509_V_ERR_PATH_LENGTH_EXCEEDED:
  536. case X509_V_ERR_INVALID_CA:
  537. return SSL_AD_UNKNOWN_CA;
  538. case X509_V_ERR_APPLICATION_VERIFICATION:
  539. return SSL_AD_HANDSHAKE_FAILURE;
  540. case X509_V_ERR_INVALID_PURPOSE:
  541. return SSL_AD_UNSUPPORTED_CERTIFICATE;
  542. default:
  543. return SSL_AD_CERTIFICATE_UNKNOWN;
  544. }
  545. }
  546. static int ssl_crypto_x509_session_verify_cert_chain(SSL_SESSION *session,
  547. SSL *ssl) {
  548. STACK_OF(X509) *const cert_chain = session->x509_chain;
  549. if (cert_chain == NULL || sk_X509_num(cert_chain) == 0) {
  550. return 0;
  551. }
  552. X509_STORE *verify_store = ssl->ctx->cert_store;
  553. if (ssl->cert->verify_store != NULL) {
  554. verify_store = ssl->cert->verify_store;
  555. }
  556. X509 *leaf = sk_X509_value(cert_chain, 0);
  557. int ret = 0;
  558. X509_STORE_CTX ctx;
  559. if (!X509_STORE_CTX_init(&ctx, verify_store, leaf, cert_chain)) {
  560. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  561. return 0;
  562. }
  563. if (!X509_STORE_CTX_set_ex_data(&ctx, SSL_get_ex_data_X509_STORE_CTX_idx(),
  564. ssl)) {
  565. goto err;
  566. }
  567. /* We need to inherit the verify parameters. These can be determined by the
  568. * context: if its a server it will verify SSL client certificates or vice
  569. * versa. */
  570. X509_STORE_CTX_set_default(&ctx, ssl->server ? "ssl_client" : "ssl_server");
  571. /* Anything non-default in "param" should overwrite anything in the ctx. */
  572. X509_VERIFY_PARAM_set1(X509_STORE_CTX_get0_param(&ctx), ssl->param);
  573. if (ssl->verify_callback) {
  574. X509_STORE_CTX_set_verify_cb(&ctx, ssl->verify_callback);
  575. }
  576. int verify_ret;
  577. if (ssl->ctx->app_verify_callback != NULL) {
  578. verify_ret = ssl->ctx->app_verify_callback(&ctx, ssl->ctx->app_verify_arg);
  579. } else {
  580. verify_ret = X509_verify_cert(&ctx);
  581. }
  582. session->verify_result = ctx.error;
  583. /* If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. */
  584. if (verify_ret <= 0 && ssl->verify_mode != SSL_VERIFY_NONE) {
  585. ssl3_send_alert(ssl, SSL3_AL_FATAL, ssl_verify_alarm_type(ctx.error));
  586. OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED);
  587. goto err;
  588. }
  589. ERR_clear_error();
  590. ret = 1;
  591. err:
  592. X509_STORE_CTX_cleanup(&ctx);
  593. return ret;
  594. }
  595. static void ssl_crypto_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {
  596. sk_X509_NAME_pop_free(hs->cached_x509_ca_names, X509_NAME_free);
  597. hs->cached_x509_ca_names = NULL;
  598. }
  599. static int ssl_crypto_x509_ssl_new(SSL *ssl) {
  600. ssl->param = X509_VERIFY_PARAM_new();
  601. if (ssl->param == NULL) {
  602. return 0;
  603. }
  604. X509_VERIFY_PARAM_inherit(ssl->param, ssl->ctx->param);
  605. return 1;
  606. }
  607. static void ssl_crypto_x509_ssl_flush_cached_client_CA(SSL *ssl) {
  608. sk_X509_NAME_pop_free(ssl->cached_x509_client_CA, X509_NAME_free);
  609. ssl->cached_x509_client_CA = NULL;
  610. }
  611. static void ssl_crypto_x509_ssl_free(SSL *ssl) {
  612. ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
  613. X509_VERIFY_PARAM_free(ssl->param);
  614. }
  615. static int ssl_crypto_x509_ssl_auto_chain_if_needed(SSL *ssl) {
  616. /* Only build a chain if there are no intermediates configured and the feature
  617. * isn't disabled. */
  618. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
  619. !ssl_has_certificate(ssl) ||
  620. ssl->cert->chain == NULL ||
  621. sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
  622. return 1;
  623. }
  624. X509 *leaf =
  625. X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
  626. if (!leaf) {
  627. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  628. return 0;
  629. }
  630. X509_STORE_CTX ctx;
  631. if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
  632. X509_free(leaf);
  633. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  634. return 0;
  635. }
  636. /* Attempt to build a chain, ignoring the result. */
  637. X509_verify_cert(&ctx);
  638. X509_free(leaf);
  639. ERR_clear_error();
  640. /* Remove the leaf from the generated chain. */
  641. X509_free(sk_X509_shift(ctx.chain));
  642. const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
  643. X509_STORE_CTX_cleanup(&ctx);
  644. if (!ok) {
  645. return 0;
  646. }
  647. ssl_crypto_x509_cert_flush_cached_chain(ssl->cert);
  648. return 1;
  649. }
  650. static void ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {
  651. sk_X509_NAME_pop_free(ctx->cached_x509_client_CA, X509_NAME_free);
  652. ctx->cached_x509_client_CA = NULL;
  653. }
  654. static int ssl_crypto_x509_ssl_ctx_new(SSL_CTX *ctx) {
  655. ctx->cert_store = X509_STORE_new();
  656. ctx->param = X509_VERIFY_PARAM_new();
  657. return (ctx->cert_store != NULL && ctx->param != NULL);
  658. }
  659. static void ssl_crypto_x509_ssl_ctx_free(SSL_CTX *ctx) {
  660. ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
  661. X509_VERIFY_PARAM_free(ctx->param);
  662. X509_STORE_free(ctx->cert_store);
  663. }
  664. const SSL_X509_METHOD ssl_crypto_x509_method = {
  665. ssl_crypto_x509_check_client_CA_list,
  666. ssl_crypto_x509_cert_clear,
  667. ssl_crypto_x509_cert_free,
  668. ssl_crypto_x509_cert_dup,
  669. ssl_crypto_x509_cert_flush_cached_chain,
  670. ssl_crypto_x509_cert_flush_cached_leaf,
  671. ssl_crypto_x509_session_cache_objects,
  672. ssl_crypto_x509_session_dup,
  673. ssl_crypto_x509_session_clear,
  674. ssl_crypto_x509_session_verify_cert_chain,
  675. ssl_crypto_x509_hs_flush_cached_ca_names,
  676. ssl_crypto_x509_ssl_new,
  677. ssl_crypto_x509_ssl_free,
  678. ssl_crypto_x509_ssl_flush_cached_client_CA,
  679. ssl_crypto_x509_ssl_auto_chain_if_needed,
  680. ssl_crypto_x509_ssl_ctx_new,
  681. ssl_crypto_x509_ssl_ctx_free,
  682. ssl_crypto_x509_ssl_ctx_flush_cached_client_CA,
  683. };
  684. static int ssl_use_certificate(CERT *cert, X509 *x) {
  685. if (x == NULL) {
  686. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  687. return 0;
  688. }
  689. CRYPTO_BUFFER *buffer = x509_to_buffer(x);
  690. if (buffer == NULL) {
  691. return 0;
  692. }
  693. const int ok = ssl_set_cert(cert, buffer);
  694. CRYPTO_BUFFER_free(buffer);
  695. return ok;
  696. }
  697. int SSL_use_certificate(SSL *ssl, X509 *x) {
  698. check_ssl_x509_method(ssl);
  699. return ssl_use_certificate(ssl->cert, x);
  700. }
  701. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  702. check_ssl_ctx_x509_method(ctx);
  703. return ssl_use_certificate(ctx->cert, x);
  704. }
  705. /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
  706. * first element of |cert->chain|. */
  707. static int ssl_cert_cache_leaf_cert(CERT *cert) {
  708. assert(cert->x509_method);
  709. if (cert->x509_leaf != NULL ||
  710. cert->chain == NULL) {
  711. return 1;
  712. }
  713. CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
  714. if (!leaf) {
  715. return 1;
  716. }
  717. cert->x509_leaf = X509_parse_from_buffer(leaf);
  718. return cert->x509_leaf != NULL;
  719. }
  720. static X509 *ssl_cert_get0_leaf(CERT *cert) {
  721. if (cert->x509_leaf == NULL &&
  722. !ssl_cert_cache_leaf_cert(cert)) {
  723. return NULL;
  724. }
  725. return cert->x509_leaf;
  726. }
  727. X509 *SSL_get_certificate(const SSL *ssl) {
  728. check_ssl_x509_method(ssl);
  729. return ssl_cert_get0_leaf(ssl->cert);
  730. }
  731. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  732. check_ssl_ctx_x509_method(ctx);
  733. CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
  734. X509 *ret = ssl_cert_get0_leaf(ctx->cert);
  735. CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
  736. return ret;
  737. }
  738. static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  739. if (!ssl_cert_set_chain(cert, chain)) {
  740. return 0;
  741. }
  742. sk_X509_pop_free(chain, X509_free);
  743. ssl_crypto_x509_cert_flush_cached_chain(cert);
  744. return 1;
  745. }
  746. static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  747. if (!ssl_cert_set_chain(cert, chain)) {
  748. return 0;
  749. }
  750. ssl_crypto_x509_cert_flush_cached_chain(cert);
  751. return 1;
  752. }
  753. static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
  754. assert(cert->x509_method);
  755. CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
  756. if (buffer == NULL) {
  757. return 0;
  758. }
  759. if (cert->chain != NULL) {
  760. if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  761. CRYPTO_BUFFER_free(buffer);
  762. return 0;
  763. }
  764. return 1;
  765. }
  766. cert->chain = new_leafless_chain();
  767. if (cert->chain == NULL ||
  768. !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  769. CRYPTO_BUFFER_free(buffer);
  770. sk_CRYPTO_BUFFER_free(cert->chain);
  771. cert->chain = NULL;
  772. return 0;
  773. }
  774. return 1;
  775. }
  776. static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  777. if (!ssl_cert_append_cert(cert, x509)) {
  778. return 0;
  779. }
  780. X509_free(cert->x509_stash);
  781. cert->x509_stash = x509;
  782. ssl_crypto_x509_cert_flush_cached_chain(cert);
  783. return 1;
  784. }
  785. static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  786. if (!ssl_cert_append_cert(cert, x509)) {
  787. return 0;
  788. }
  789. ssl_crypto_x509_cert_flush_cached_chain(cert);
  790. return 1;
  791. }
  792. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  793. check_ssl_ctx_x509_method(ctx);
  794. return ssl_cert_set0_chain(ctx->cert, chain);
  795. }
  796. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  797. check_ssl_ctx_x509_method(ctx);
  798. return ssl_cert_set1_chain(ctx->cert, chain);
  799. }
  800. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  801. check_ssl_x509_method(ssl);
  802. return ssl_cert_set0_chain(ssl->cert, chain);
  803. }
  804. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  805. check_ssl_x509_method(ssl);
  806. return ssl_cert_set1_chain(ssl->cert, chain);
  807. }
  808. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  809. check_ssl_ctx_x509_method(ctx);
  810. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  811. }
  812. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  813. check_ssl_ctx_x509_method(ctx);
  814. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  815. }
  816. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  817. check_ssl_ctx_x509_method(ctx);
  818. return SSL_CTX_add0_chain_cert(ctx, x509);
  819. }
  820. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  821. check_ssl_x509_method(ssl);
  822. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  823. }
  824. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  825. check_ssl_x509_method(ssl);
  826. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  827. }
  828. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  829. check_ssl_ctx_x509_method(ctx);
  830. return SSL_CTX_set0_chain(ctx, NULL);
  831. }
  832. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  833. check_ssl_ctx_x509_method(ctx);
  834. return SSL_CTX_clear_chain_certs(ctx);
  835. }
  836. int SSL_clear_chain_certs(SSL *ssl) {
  837. check_ssl_x509_method(ssl);
  838. return SSL_set0_chain(ssl, NULL);
  839. }
  840. /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
  841. * |cert->chain|. */
  842. static int ssl_cert_cache_chain_certs(CERT *cert) {
  843. assert(cert->x509_method);
  844. if (cert->x509_chain != NULL ||
  845. cert->chain == NULL ||
  846. sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
  847. return 1;
  848. }
  849. STACK_OF(X509) *chain = sk_X509_new_null();
  850. if (chain == NULL) {
  851. return 0;
  852. }
  853. for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
  854. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
  855. X509 *x509 = X509_parse_from_buffer(buffer);
  856. if (x509 == NULL ||
  857. !sk_X509_push(chain, x509)) {
  858. X509_free(x509);
  859. goto err;
  860. }
  861. }
  862. cert->x509_chain = chain;
  863. return 1;
  864. err:
  865. sk_X509_pop_free(chain, X509_free);
  866. return 0;
  867. }
  868. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  869. check_ssl_ctx_x509_method(ctx);
  870. CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
  871. const int ret = ssl_cert_cache_chain_certs(ctx->cert);
  872. CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
  873. if (!ret) {
  874. *out_chain = NULL;
  875. return 0;
  876. }
  877. *out_chain = ctx->cert->x509_chain;
  878. return 1;
  879. }
  880. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  881. STACK_OF(X509) **out_chain) {
  882. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  883. }
  884. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  885. check_ssl_x509_method(ssl);
  886. if (!ssl_cert_cache_chain_certs(ssl->cert)) {
  887. *out_chain = NULL;
  888. return 0;
  889. }
  890. *out_chain = ssl->cert->x509_chain;
  891. return 1;
  892. }
  893. static SSL_SESSION *ssl_session_new_with_crypto_x509(void) {
  894. return ssl_session_new(&ssl_crypto_x509_method);
  895. }
  896. SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
  897. return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509,
  898. d2i_SSL_SESSION, bio, out);
  899. }
  900. int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
  901. return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session);
  902. }
  903. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
  904. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  905. if (length < 0) {
  906. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  907. return NULL;
  908. }
  909. CBS cbs;
  910. CBS_init(&cbs, *pp, length);
  911. SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
  912. NULL /* no buffer pool */);
  913. if (ret == NULL) {
  914. return NULL;
  915. }
  916. if (a) {
  917. SSL_SESSION_free(*a);
  918. *a = ret;
  919. }
  920. *pp = CBS_data(&cbs);
  921. return ret;
  922. }
  923. STACK_OF(X509_NAME) *SSL_dup_CA_list(STACK_OF(X509_NAME) *list) {
  924. return sk_X509_NAME_deep_copy(list, X509_NAME_dup, X509_NAME_free);
  925. }
  926. static void set_client_CA_list(STACK_OF(CRYPTO_BUFFER) **ca_list,
  927. const STACK_OF(X509_NAME) *name_list,
  928. CRYPTO_BUFFER_POOL *pool) {
  929. STACK_OF(CRYPTO_BUFFER) *buffers = sk_CRYPTO_BUFFER_new_null();
  930. if (buffers == NULL) {
  931. return;
  932. }
  933. for (size_t i = 0; i < sk_X509_NAME_num(name_list); i++) {
  934. X509_NAME *name = sk_X509_NAME_value(name_list, i);
  935. uint8_t *outp = NULL;
  936. int len = i2d_X509_NAME(name, &outp);
  937. if (len < 0) {
  938. goto err;
  939. }
  940. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
  941. OPENSSL_free(outp);
  942. if (buffer == NULL ||
  943. !sk_CRYPTO_BUFFER_push(buffers, buffer)) {
  944. CRYPTO_BUFFER_free(buffer);
  945. goto err;
  946. }
  947. }
  948. sk_CRYPTO_BUFFER_pop_free(*ca_list, CRYPTO_BUFFER_free);
  949. *ca_list = buffers;
  950. return;
  951. err:
  952. sk_CRYPTO_BUFFER_pop_free(buffers, CRYPTO_BUFFER_free);
  953. }
  954. void SSL_set_client_CA_list(SSL *ssl, STACK_OF(X509_NAME) *name_list) {
  955. check_ssl_x509_method(ssl);
  956. ssl->ctx->x509_method->ssl_flush_cached_client_CA(ssl);
  957. set_client_CA_list(&ssl->client_CA, name_list, ssl->ctx->pool);
  958. sk_X509_NAME_pop_free(name_list, X509_NAME_free);
  959. }
  960. void SSL_CTX_set_client_CA_list(SSL_CTX *ctx, STACK_OF(X509_NAME) *name_list) {
  961. check_ssl_ctx_x509_method(ctx);
  962. ctx->x509_method->ssl_ctx_flush_cached_client_CA(ctx);
  963. set_client_CA_list(&ctx->client_CA, name_list, ctx->pool);
  964. sk_X509_NAME_pop_free(name_list, X509_NAME_free);
  965. }
  966. static STACK_OF(X509_NAME) *
  967. buffer_names_to_x509(const STACK_OF(CRYPTO_BUFFER) *names,
  968. STACK_OF(X509_NAME) **cached) {
  969. if (names == NULL) {
  970. return NULL;
  971. }
  972. if (*cached != NULL) {
  973. return *cached;
  974. }
  975. STACK_OF(X509_NAME) *new_cache = sk_X509_NAME_new_null();
  976. if (new_cache == NULL) {
  977. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  978. return NULL;
  979. }
  980. for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(names); i++) {
  981. const CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(names, i);
  982. const uint8_t *inp = CRYPTO_BUFFER_data(buffer);
  983. X509_NAME *name = d2i_X509_NAME(NULL, &inp, CRYPTO_BUFFER_len(buffer));
  984. if (name == NULL ||
  985. inp != CRYPTO_BUFFER_data(buffer) + CRYPTO_BUFFER_len(buffer) ||
  986. !sk_X509_NAME_push(new_cache, name)) {
  987. X509_NAME_free(name);
  988. goto err;
  989. }
  990. }
  991. *cached = new_cache;
  992. return new_cache;
  993. err:
  994. sk_X509_NAME_pop_free(new_cache, X509_NAME_free);
  995. return NULL;
  996. }
  997. STACK_OF(X509_NAME) *SSL_get_client_CA_list(const SSL *ssl) {
  998. check_ssl_x509_method(ssl);
  999. /* For historical reasons, this function is used both to query configuration
  1000. * state on a server as well as handshake state on a client. However, whether
  1001. * |ssl| is a client or server is not known until explicitly configured with
  1002. * |SSL_set_connect_state|. If |handshake_func| is NULL, |ssl| is in an
  1003. * indeterminate mode and |ssl->server| is unset. */
  1004. if (ssl->handshake_func != NULL && !ssl->server) {
  1005. if (ssl->s3->hs != NULL) {
  1006. return buffer_names_to_x509(ssl->s3->hs->ca_names,
  1007. &ssl->s3->hs->cached_x509_ca_names);
  1008. }
  1009. return NULL;
  1010. }
  1011. if (ssl->client_CA != NULL) {
  1012. return buffer_names_to_x509(
  1013. ssl->client_CA, (STACK_OF(X509_NAME) **)&ssl->cached_x509_client_CA);
  1014. }
  1015. return buffer_names_to_x509(ssl->ctx->client_CA,
  1016. &ssl->ctx->cached_x509_client_CA);
  1017. }
  1018. STACK_OF(X509_NAME) *SSL_CTX_get_client_CA_list(const SSL_CTX *ctx) {
  1019. check_ssl_ctx_x509_method(ctx);
  1020. CRYPTO_MUTEX_lock_write((CRYPTO_MUTEX *) &ctx->lock);
  1021. STACK_OF(X509_NAME) *ret = buffer_names_to_x509(
  1022. ctx->client_CA, (STACK_OF(X509_NAME) **)&ctx->cached_x509_client_CA);
  1023. CRYPTO_MUTEX_unlock_write((CRYPTO_MUTEX *) &ctx->lock);
  1024. return ret;
  1025. }
  1026. static int add_client_CA(STACK_OF(CRYPTO_BUFFER) **names, X509 *x509,
  1027. CRYPTO_BUFFER_POOL *pool) {
  1028. if (x509 == NULL) {
  1029. return 0;
  1030. }
  1031. uint8_t *outp = NULL;
  1032. int len = i2d_X509_NAME(X509_get_subject_name(x509), &outp);
  1033. if (len < 0) {
  1034. return 0;
  1035. }
  1036. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(outp, len, pool);
  1037. OPENSSL_free(outp);
  1038. if (buffer == NULL) {
  1039. return 0;
  1040. }
  1041. int alloced = 0;
  1042. if (*names == NULL) {
  1043. *names = sk_CRYPTO_BUFFER_new_null();
  1044. alloced = 1;
  1045. if (*names == NULL) {
  1046. CRYPTO_BUFFER_free(buffer);
  1047. return 0;
  1048. }
  1049. }
  1050. if (!sk_CRYPTO_BUFFER_push(*names, buffer)) {
  1051. CRYPTO_BUFFER_free(buffer);
  1052. if (alloced) {
  1053. sk_CRYPTO_BUFFER_pop_free(*names, CRYPTO_BUFFER_free);
  1054. *names = NULL;
  1055. }
  1056. return 0;
  1057. }
  1058. return 1;
  1059. }
  1060. int SSL_add_client_CA(SSL *ssl, X509 *x509) {
  1061. check_ssl_x509_method(ssl);
  1062. if (!add_client_CA(&ssl->client_CA, x509, ssl->ctx->pool)) {
  1063. return 0;
  1064. }
  1065. ssl_crypto_x509_ssl_flush_cached_client_CA(ssl);
  1066. return 1;
  1067. }
  1068. int SSL_CTX_add_client_CA(SSL_CTX *ctx, X509 *x509) {
  1069. check_ssl_ctx_x509_method(ctx);
  1070. if (!add_client_CA(&ctx->client_CA, x509, ctx->pool)) {
  1071. return 0;
  1072. }
  1073. ssl_crypto_x509_ssl_ctx_flush_cached_client_CA(ctx);
  1074. return 1;
  1075. }
  1076. static int do_client_cert_cb(SSL *ssl, void *arg) {
  1077. if (ssl_has_certificate(ssl) || ssl->ctx->client_cert_cb == NULL) {
  1078. return 1;
  1079. }
  1080. X509 *x509 = NULL;
  1081. EVP_PKEY *pkey = NULL;
  1082. int ret = ssl->ctx->client_cert_cb(ssl, &x509, &pkey);
  1083. if (ret < 0) {
  1084. return -1;
  1085. }
  1086. if (ret != 0) {
  1087. if (!SSL_use_certificate(ssl, x509) ||
  1088. !SSL_use_PrivateKey(ssl, pkey)) {
  1089. return 0;
  1090. }
  1091. }
  1092. X509_free(x509);
  1093. EVP_PKEY_free(pkey);
  1094. return 1;
  1095. }
  1096. void SSL_CTX_set_client_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl,
  1097. X509 **out_x509,
  1098. EVP_PKEY **out_pkey)) {
  1099. check_ssl_ctx_x509_method(ctx);
  1100. /* Emulate the old client certificate callback with the new one. */
  1101. SSL_CTX_set_cert_cb(ctx, do_client_cert_cb, NULL);
  1102. ctx->client_cert_cb = cb;
  1103. }
  1104. static int set_cert_store(X509_STORE **store_ptr, X509_STORE *new_store,
  1105. int take_ref) {
  1106. X509_STORE_free(*store_ptr);
  1107. *store_ptr = new_store;
  1108. if (new_store != NULL && take_ref) {
  1109. X509_STORE_up_ref(new_store);
  1110. }
  1111. return 1;
  1112. }
  1113. int SSL_get_ex_data_X509_STORE_CTX_idx(void) {
  1114. /* The ex_data index to go from |X509_STORE_CTX| to |SSL| always uses the
  1115. * reserved app_data slot. Before ex_data was introduced, app_data was used.
  1116. * Avoid breaking any software which assumes |X509_STORE_CTX_get_app_data|
  1117. * works. */
  1118. return 0;
  1119. }
  1120. int SSL_CTX_set0_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1121. check_ssl_ctx_x509_method(ctx);
  1122. return set_cert_store(&ctx->cert->verify_store, store, 0);
  1123. }
  1124. int SSL_CTX_set1_verify_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1125. check_ssl_ctx_x509_method(ctx);
  1126. return set_cert_store(&ctx->cert->verify_store, store, 1);
  1127. }
  1128. int SSL_set0_verify_cert_store(SSL *ssl, X509_STORE *store) {
  1129. check_ssl_x509_method(ssl);
  1130. return set_cert_store(&ssl->cert->verify_store, store, 0);
  1131. }
  1132. int SSL_set1_verify_cert_store(SSL *ssl, X509_STORE *store) {
  1133. check_ssl_x509_method(ssl);
  1134. return set_cert_store(&ssl->cert->verify_store, store, 1);
  1135. }