ssl_x509.cc 39 KB

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