2
0

ssl_x509.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  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. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  152. if (ssl == NULL) {
  153. return NULL;
  154. }
  155. SSL_SESSION *session = SSL_get_session(ssl);
  156. if (session == NULL || session->x509_peer == NULL) {
  157. return NULL;
  158. }
  159. X509_up_ref(session->x509_peer);
  160. return session->x509_peer;
  161. }
  162. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  163. if (ssl == NULL) {
  164. return NULL;
  165. }
  166. SSL_SESSION *session = SSL_get_session(ssl);
  167. if (session == NULL ||
  168. session->x509_chain == NULL) {
  169. return NULL;
  170. }
  171. if (!ssl->server) {
  172. return session->x509_chain;
  173. }
  174. /* OpenSSL historically didn't include the leaf certificate in the returned
  175. * certificate chain, but only for servers. */
  176. if (session->x509_chain_without_leaf == NULL) {
  177. session->x509_chain_without_leaf = sk_X509_new_null();
  178. if (session->x509_chain_without_leaf == NULL) {
  179. return NULL;
  180. }
  181. for (size_t i = 1; i < sk_X509_num(session->x509_chain); i++) {
  182. X509 *cert = sk_X509_value(session->x509_chain, i);
  183. if (!sk_X509_push(session->x509_chain_without_leaf, cert)) {
  184. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  185. session->x509_chain_without_leaf = NULL;
  186. return NULL;
  187. }
  188. X509_up_ref(cert);
  189. }
  190. }
  191. return session->x509_chain_without_leaf;
  192. }
  193. STACK_OF(X509) *SSL_get_peer_full_cert_chain(const SSL *ssl) {
  194. SSL_SESSION *session = SSL_get_session(ssl);
  195. if (session == NULL) {
  196. return NULL;
  197. }
  198. return session->x509_chain;
  199. }
  200. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  201. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  202. }
  203. int SSL_set_purpose(SSL *ssl, int purpose) {
  204. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  205. }
  206. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  207. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  208. }
  209. int SSL_set_trust(SSL *ssl, int trust) {
  210. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  211. }
  212. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  213. return X509_VERIFY_PARAM_set1(ctx->param, param);
  214. }
  215. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  216. return X509_VERIFY_PARAM_set1(ssl->param, param);
  217. }
  218. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  219. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  220. int SSL_get_verify_depth(const SSL *ssl) {
  221. return X509_VERIFY_PARAM_get_depth(ssl->param);
  222. }
  223. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  224. return ssl->verify_callback;
  225. }
  226. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  227. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  228. return X509_VERIFY_PARAM_get_depth(ctx->param);
  229. }
  230. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  231. int ok, X509_STORE_CTX *store_ctx) {
  232. return ctx->default_verify_callback;
  233. }
  234. void SSL_set_verify(SSL *ssl, int mode,
  235. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  236. ssl->verify_mode = mode;
  237. if (callback != NULL) {
  238. ssl->verify_callback = callback;
  239. }
  240. }
  241. void SSL_set_verify_depth(SSL *ssl, int depth) {
  242. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  243. }
  244. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  245. int (*cb)(X509_STORE_CTX *store_ctx,
  246. void *arg),
  247. void *arg) {
  248. ctx->app_verify_callback = cb;
  249. ctx->app_verify_arg = arg;
  250. }
  251. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  252. int (*cb)(int, X509_STORE_CTX *)) {
  253. ctx->verify_mode = mode;
  254. ctx->default_verify_callback = cb;
  255. }
  256. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  257. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  258. }
  259. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  260. return X509_STORE_set_default_paths(ctx->cert_store);
  261. }
  262. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  263. const char *ca_dir) {
  264. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  265. }
  266. void SSL_set_verify_result(SSL *ssl, long result) {
  267. if (result != X509_V_OK) {
  268. abort();
  269. }
  270. }
  271. long SSL_get_verify_result(const SSL *ssl) {
  272. SSL_SESSION *session = SSL_get_session(ssl);
  273. if (session == NULL) {
  274. return X509_V_ERR_INVALID_CALL;
  275. }
  276. return session->verify_result;
  277. }
  278. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  279. return ctx->cert_store;
  280. }
  281. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  282. X509_STORE_free(ctx->cert_store);
  283. ctx->cert_store = store;
  284. }
  285. static void ssl_crypto_x509_flush_cached_leaf(CERT *cert) {
  286. X509_free(cert->x509_leaf);
  287. cert->x509_leaf = NULL;
  288. }
  289. static void ssl_crypto_x509_flush_cached_chain(CERT *cert) {
  290. sk_X509_pop_free(cert->x509_chain, X509_free);
  291. cert->x509_chain = NULL;
  292. }
  293. static void ssl_crypto_x509_clear(CERT *cert) {
  294. ssl_crypto_x509_flush_cached_leaf(cert);
  295. ssl_crypto_x509_flush_cached_chain(cert);
  296. X509_free(cert->x509_stash);
  297. cert->x509_stash = NULL;
  298. }
  299. static int ssl_crypto_x509_session_cache_objects(SSL_SESSION *sess) {
  300. STACK_OF(X509) *chain = NULL;
  301. const size_t num_certs = sk_CRYPTO_BUFFER_num(sess->certs);
  302. if (num_certs > 0) {
  303. chain = sk_X509_new_null();
  304. if (chain == NULL) {
  305. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  306. goto err;
  307. }
  308. }
  309. X509 *leaf = NULL;
  310. for (size_t i = 0; i < num_certs; i++) {
  311. X509 *x509 = X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(sess->certs, i));
  312. if (x509 == NULL) {
  313. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  314. goto err;
  315. }
  316. if (!sk_X509_push(chain, x509)) {
  317. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  318. X509_free(x509);
  319. goto err;
  320. }
  321. if (i == 0) {
  322. leaf = x509;
  323. }
  324. }
  325. sk_X509_pop_free(sess->x509_chain, X509_free);
  326. sess->x509_chain = chain;
  327. sk_X509_pop_free(sess->x509_chain_without_leaf, X509_free);
  328. sess->x509_chain_without_leaf = NULL;
  329. X509_free(sess->x509_peer);
  330. if (leaf != NULL) {
  331. X509_up_ref(leaf);
  332. }
  333. sess->x509_peer = leaf;
  334. return 1;
  335. err:
  336. sk_X509_pop_free(chain, X509_free);
  337. return 0;
  338. }
  339. static int ssl_crypto_x509_session_dup(SSL_SESSION *new_session,
  340. const SSL_SESSION *session) {
  341. if (session->x509_peer != NULL) {
  342. X509_up_ref(session->x509_peer);
  343. new_session->x509_peer = session->x509_peer;
  344. }
  345. if (session->x509_chain != NULL) {
  346. new_session->x509_chain = X509_chain_up_ref(session->x509_chain);
  347. if (new_session->x509_chain == NULL) {
  348. return 0;
  349. }
  350. }
  351. return 1;
  352. }
  353. static void ssl_crypto_x509_session_clear(SSL_SESSION *session) {
  354. X509_free(session->x509_peer);
  355. session->x509_peer = NULL;
  356. sk_X509_pop_free(session->x509_chain, X509_free);
  357. session->x509_chain = NULL;
  358. sk_X509_pop_free(session->x509_chain_without_leaf, X509_free);
  359. session->x509_chain_without_leaf = NULL;
  360. }
  361. const SSL_X509_METHOD ssl_crypto_x509_method = {
  362. ssl_crypto_x509_clear,
  363. ssl_crypto_x509_flush_cached_chain,
  364. ssl_crypto_x509_flush_cached_leaf,
  365. ssl_crypto_x509_session_cache_objects,
  366. ssl_crypto_x509_session_dup,
  367. ssl_crypto_x509_session_clear,
  368. };
  369. /* x509_to_buffer returns a |CRYPTO_BUFFER| that contains the serialised
  370. * contents of |x509|. */
  371. static CRYPTO_BUFFER *x509_to_buffer(X509 *x509) {
  372. uint8_t *buf = NULL;
  373. int cert_len = i2d_X509(x509, &buf);
  374. if (cert_len <= 0) {
  375. return 0;
  376. }
  377. CRYPTO_BUFFER *buffer = CRYPTO_BUFFER_new(buf, cert_len, NULL);
  378. OPENSSL_free(buf);
  379. return buffer;
  380. }
  381. static int ssl_use_certificate(CERT *cert, X509 *x) {
  382. if (x == NULL) {
  383. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  384. return 0;
  385. }
  386. CRYPTO_BUFFER *buffer = x509_to_buffer(x);
  387. if (buffer == NULL) {
  388. return 0;
  389. }
  390. const int ok = ssl_set_cert(cert, buffer);
  391. CRYPTO_BUFFER_free(buffer);
  392. return ok;
  393. }
  394. int SSL_use_certificate(SSL *ssl, X509 *x) {
  395. return ssl_use_certificate(ssl->cert, x);
  396. }
  397. int SSL_CTX_use_certificate(SSL_CTX *ctx, X509 *x) {
  398. return ssl_use_certificate(ctx->cert, x);
  399. }
  400. /* ssl_cert_cache_leaf_cert sets |cert->x509_leaf|, if currently NULL, from the
  401. * first element of |cert->chain|. */
  402. static int ssl_cert_cache_leaf_cert(CERT *cert) {
  403. assert(cert->x509_method);
  404. if (cert->x509_leaf != NULL ||
  405. cert->chain == NULL) {
  406. return 1;
  407. }
  408. CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
  409. if (!leaf) {
  410. return 1;
  411. }
  412. cert->x509_leaf = X509_parse_from_buffer(leaf);
  413. return cert->x509_leaf != NULL;
  414. }
  415. static X509 *ssl_cert_get0_leaf(CERT *cert) {
  416. if (cert->x509_leaf == NULL &&
  417. !ssl_cert_cache_leaf_cert(cert)) {
  418. return NULL;
  419. }
  420. return cert->x509_leaf;
  421. }
  422. X509 *SSL_get_certificate(const SSL *ssl) {
  423. return ssl_cert_get0_leaf(ssl->cert);
  424. }
  425. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  426. return ssl_cert_get0_leaf(ctx->cert);
  427. }
  428. /* new_leafless_chain returns a fresh stack of buffers set to {NULL}. */
  429. static STACK_OF(CRYPTO_BUFFER) *new_leafless_chain(void) {
  430. STACK_OF(CRYPTO_BUFFER) *chain = sk_CRYPTO_BUFFER_new_null();
  431. if (chain == NULL) {
  432. return NULL;
  433. }
  434. if (!sk_CRYPTO_BUFFER_push(chain, NULL)) {
  435. sk_CRYPTO_BUFFER_free(chain);
  436. return NULL;
  437. }
  438. return chain;
  439. }
  440. /* ssl_cert_set_chain sets elements 1.. of |cert->chain| to the serialised
  441. * forms of elements of |chain|. It returns one on success or zero on error, in
  442. * which case no change to |cert->chain| is made. It preverses the existing
  443. * leaf from |cert->chain|, if any. */
  444. static int ssl_cert_set_chain(CERT *cert, STACK_OF(X509) *chain) {
  445. STACK_OF(CRYPTO_BUFFER) *new_chain = NULL;
  446. if (cert->chain != NULL) {
  447. new_chain = sk_CRYPTO_BUFFER_new_null();
  448. if (new_chain == NULL) {
  449. return 0;
  450. }
  451. CRYPTO_BUFFER *leaf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
  452. if (!sk_CRYPTO_BUFFER_push(new_chain, leaf)) {
  453. goto err;
  454. }
  455. /* |leaf| might be NULL if it's a “leafless” chain. */
  456. if (leaf != NULL) {
  457. CRYPTO_BUFFER_up_ref(leaf);
  458. }
  459. }
  460. for (size_t i = 0; i < sk_X509_num(chain); i++) {
  461. if (new_chain == NULL) {
  462. new_chain = new_leafless_chain();
  463. if (new_chain == NULL) {
  464. goto err;
  465. }
  466. }
  467. CRYPTO_BUFFER *buffer = x509_to_buffer(sk_X509_value(chain, i));
  468. if (buffer == NULL ||
  469. !sk_CRYPTO_BUFFER_push(new_chain, buffer)) {
  470. CRYPTO_BUFFER_free(buffer);
  471. goto err;
  472. }
  473. }
  474. sk_CRYPTO_BUFFER_pop_free(cert->chain, CRYPTO_BUFFER_free);
  475. cert->chain = new_chain;
  476. return 1;
  477. err:
  478. sk_CRYPTO_BUFFER_pop_free(new_chain, CRYPTO_BUFFER_free);
  479. return 0;
  480. }
  481. static int ssl_cert_set0_chain(CERT *cert, STACK_OF(X509) *chain) {
  482. if (!ssl_cert_set_chain(cert, chain)) {
  483. return 0;
  484. }
  485. sk_X509_pop_free(chain, X509_free);
  486. ssl_crypto_x509_flush_cached_chain(cert);
  487. return 1;
  488. }
  489. static int ssl_cert_set1_chain(CERT *cert, STACK_OF(X509) *chain) {
  490. if (!ssl_cert_set_chain(cert, chain)) {
  491. return 0;
  492. }
  493. ssl_crypto_x509_flush_cached_chain(cert);
  494. return 1;
  495. }
  496. static int ssl_cert_append_cert(CERT *cert, X509 *x509) {
  497. assert(cert->x509_method);
  498. CRYPTO_BUFFER *buffer = x509_to_buffer(x509);
  499. if (buffer == NULL) {
  500. return 0;
  501. }
  502. if (cert->chain != NULL) {
  503. if (!sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  504. CRYPTO_BUFFER_free(buffer);
  505. return 0;
  506. }
  507. return 1;
  508. }
  509. cert->chain = new_leafless_chain();
  510. if (cert->chain == NULL ||
  511. !sk_CRYPTO_BUFFER_push(cert->chain, buffer)) {
  512. CRYPTO_BUFFER_free(buffer);
  513. sk_CRYPTO_BUFFER_free(cert->chain);
  514. cert->chain = NULL;
  515. return 0;
  516. }
  517. return 1;
  518. }
  519. static int ssl_cert_add0_chain_cert(CERT *cert, X509 *x509) {
  520. if (!ssl_cert_append_cert(cert, x509)) {
  521. return 0;
  522. }
  523. X509_free(cert->x509_stash);
  524. cert->x509_stash = x509;
  525. ssl_crypto_x509_flush_cached_chain(cert);
  526. return 1;
  527. }
  528. static int ssl_cert_add1_chain_cert(CERT *cert, X509 *x509) {
  529. if (!ssl_cert_append_cert(cert, x509)) {
  530. return 0;
  531. }
  532. ssl_crypto_x509_flush_cached_chain(cert);
  533. return 1;
  534. }
  535. int SSL_CTX_set0_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  536. return ssl_cert_set0_chain(ctx->cert, chain);
  537. }
  538. int SSL_CTX_set1_chain(SSL_CTX *ctx, STACK_OF(X509) *chain) {
  539. return ssl_cert_set1_chain(ctx->cert, chain);
  540. }
  541. int SSL_set0_chain(SSL *ssl, STACK_OF(X509) *chain) {
  542. return ssl_cert_set0_chain(ssl->cert, chain);
  543. }
  544. int SSL_set1_chain(SSL *ssl, STACK_OF(X509) *chain) {
  545. return ssl_cert_set1_chain(ssl->cert, chain);
  546. }
  547. int SSL_CTX_add0_chain_cert(SSL_CTX *ctx, X509 *x509) {
  548. return ssl_cert_add0_chain_cert(ctx->cert, x509);
  549. }
  550. int SSL_CTX_add1_chain_cert(SSL_CTX *ctx, X509 *x509) {
  551. return ssl_cert_add1_chain_cert(ctx->cert, x509);
  552. }
  553. int SSL_CTX_add_extra_chain_cert(SSL_CTX *ctx, X509 *x509) {
  554. return SSL_CTX_add0_chain_cert(ctx, x509);
  555. }
  556. int SSL_add0_chain_cert(SSL *ssl, X509 *x509) {
  557. return ssl_cert_add0_chain_cert(ssl->cert, x509);
  558. }
  559. int SSL_add1_chain_cert(SSL *ssl, X509 *x509) {
  560. return ssl_cert_add1_chain_cert(ssl->cert, x509);
  561. }
  562. int SSL_CTX_clear_chain_certs(SSL_CTX *ctx) {
  563. return SSL_CTX_set0_chain(ctx, NULL);
  564. }
  565. int SSL_CTX_clear_extra_chain_certs(SSL_CTX *ctx) {
  566. return SSL_CTX_clear_chain_certs(ctx);
  567. }
  568. int SSL_clear_chain_certs(SSL *ssl) {
  569. return SSL_set0_chain(ssl, NULL);
  570. }
  571. int ssl_auto_chain_if_needed(SSL *ssl) {
  572. /* Only build a chain if there are no intermediates configured and the feature
  573. * isn't disabled. */
  574. if ((ssl->mode & SSL_MODE_NO_AUTO_CHAIN) ||
  575. !ssl_has_certificate(ssl) ||
  576. ssl->cert->chain == NULL ||
  577. sk_CRYPTO_BUFFER_num(ssl->cert->chain) > 1) {
  578. return 1;
  579. }
  580. X509 *leaf =
  581. X509_parse_from_buffer(sk_CRYPTO_BUFFER_value(ssl->cert->chain, 0));
  582. if (!leaf) {
  583. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  584. return 0;
  585. }
  586. X509_STORE_CTX ctx;
  587. if (!X509_STORE_CTX_init(&ctx, ssl->ctx->cert_store, leaf, NULL)) {
  588. X509_free(leaf);
  589. OPENSSL_PUT_ERROR(SSL, ERR_R_X509_LIB);
  590. return 0;
  591. }
  592. /* Attempt to build a chain, ignoring the result. */
  593. X509_verify_cert(&ctx);
  594. X509_free(leaf);
  595. ERR_clear_error();
  596. /* Remove the leaf from the generated chain. */
  597. X509_free(sk_X509_shift(ctx.chain));
  598. const int ok = ssl_cert_set_chain(ssl->cert, ctx.chain);
  599. X509_STORE_CTX_cleanup(&ctx);
  600. if (!ok) {
  601. return 0;
  602. }
  603. ssl_crypto_x509_flush_cached_chain(ssl->cert);
  604. return 1;
  605. }
  606. /* ssl_cert_cache_chain_certs fills in |cert->x509_chain| from elements 1.. of
  607. * |cert->chain|. */
  608. static int ssl_cert_cache_chain_certs(CERT *cert) {
  609. assert(cert->x509_method);
  610. if (cert->x509_chain != NULL ||
  611. cert->chain == NULL ||
  612. sk_CRYPTO_BUFFER_num(cert->chain) < 2) {
  613. return 1;
  614. }
  615. STACK_OF(X509) *chain = sk_X509_new_null();
  616. if (chain == NULL) {
  617. return 0;
  618. }
  619. for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
  620. CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(cert->chain, i);
  621. X509 *x509 = X509_parse_from_buffer(buffer);
  622. if (x509 == NULL ||
  623. !sk_X509_push(chain, x509)) {
  624. X509_free(x509);
  625. goto err;
  626. }
  627. }
  628. cert->x509_chain = chain;
  629. return 1;
  630. err:
  631. sk_X509_pop_free(chain, X509_free);
  632. return 0;
  633. }
  634. int SSL_CTX_get0_chain_certs(const SSL_CTX *ctx, STACK_OF(X509) **out_chain) {
  635. if (!ssl_cert_cache_chain_certs(ctx->cert)) {
  636. *out_chain = NULL;
  637. return 0;
  638. }
  639. *out_chain = ctx->cert->x509_chain;
  640. return 1;
  641. }
  642. int SSL_CTX_get_extra_chain_certs(const SSL_CTX *ctx,
  643. STACK_OF(X509) **out_chain) {
  644. return SSL_CTX_get0_chain_certs(ctx, out_chain);
  645. }
  646. int SSL_get0_chain_certs(const SSL *ssl, STACK_OF(X509) **out_chain) {
  647. if (!ssl_cert_cache_chain_certs(ssl->cert)) {
  648. *out_chain = NULL;
  649. return 0;
  650. }
  651. *out_chain = ssl->cert->x509_chain;
  652. return 1;
  653. }
  654. static SSL_SESSION *ssl_session_new_with_crypto_x509(void) {
  655. return ssl_session_new(&ssl_crypto_x509_method);
  656. }
  657. SSL_SESSION *d2i_SSL_SESSION_bio(BIO *bio, SSL_SESSION **out) {
  658. return ASN1_d2i_bio_of(SSL_SESSION, ssl_session_new_with_crypto_x509,
  659. d2i_SSL_SESSION, bio, out);
  660. }
  661. int i2d_SSL_SESSION_bio(BIO *bio, const SSL_SESSION *session) {
  662. return ASN1_i2d_bio_of(SSL_SESSION, i2d_SSL_SESSION, bio, session);
  663. }
  664. IMPLEMENT_PEM_rw(SSL_SESSION, SSL_SESSION, PEM_STRING_SSL_SESSION, SSL_SESSION)
  665. SSL_SESSION *d2i_SSL_SESSION(SSL_SESSION **a, const uint8_t **pp, long length) {
  666. if (length < 0) {
  667. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  668. return NULL;
  669. }
  670. CBS cbs;
  671. CBS_init(&cbs, *pp, length);
  672. SSL_SESSION *ret = SSL_SESSION_parse(&cbs, &ssl_crypto_x509_method,
  673. NULL /* no buffer pool */);
  674. if (ret == NULL) {
  675. return NULL;
  676. }
  677. if (a) {
  678. SSL_SESSION_free(*a);
  679. *a = ret;
  680. }
  681. *pp = CBS_data(&cbs);
  682. return ret;
  683. }