blob: 5fcf684de2d889f62b7d06d122c72ae1fe04e805 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* 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
David Benjamin9e4e01e2015-09-15 01:48:04 -040057#include <openssl/ssl.h>
58
David Benjamin2dc02042016-09-19 19:57:37 -040059#include <assert.h>
David Benjamin61672812016-07-14 23:10:43 -040060#include <string.h>
61
David Benjamin3ba95862019-10-21 16:14:33 -040062#include <openssl/err.h>
David Benjamin97718f12016-07-08 09:16:50 -070063
David Benjamin17cf2cb2016-12-13 01:07:13 -050064#include "../crypto/internal.h"
David Benjamin2ee94aa2015-04-07 22:38:30 -040065#include "internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070066
67
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -070068BSSL_NAMESPACE_BEGIN
David Benjamin86e95b82017-07-18 16:34:25 -040069
David Benjamin82a4b222020-02-09 17:51:45 -050070static void tls_on_handshake_complete(SSL *ssl) {
David Benjaminc11ea9422017-08-29 16:33:21 -040071 // The handshake should have released its final message.
David Benjamin7934f082017-08-01 16:32:25 -040072 assert(!ssl->s3->has_message);
David Benjamin8f94c312017-08-01 17:35:55 -040073
David Benjamin32ce0ac2017-10-13 19:17:22 -040074 // During the handshake, |hs_buf| is retained. Release if it there is no
David Benjaminf9cc26f2020-02-09 16:49:31 -050075 // excess in it. There should not be any excess because the handshake logic
76 // rejects unprocessed data after each Finished message. Note this means we do
77 // not allow a TLS 1.2 HelloRequest to be packed into the same record as
78 // Finished. (Schannel also rejects this.)
79 assert(!ssl->s3->hs_buf || ssl->s3->hs_buf->length == 0);
David Benjamin32ce0ac2017-10-13 19:17:22 -040080 if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
81 ssl->s3->hs_buf.reset();
David Benjamin8f94c312017-08-01 17:35:55 -040082 }
David Benjamine3dee272017-08-02 20:06:53 -040083}
David Benjamin0be6fc42016-12-03 23:23:52 -050084
David Benjaminb0921922020-02-20 12:33:28 -050085static bool tls_set_read_state(SSL *ssl, ssl_encryption_level_t level,
David Benjamin5298ef92020-03-13 12:17:30 -040086 UniquePtr<SSLAEADContext> aead_ctx,
87 Span<const uint8_t> secret_for_quic) {
David Benjamin40e94702017-10-06 18:26:36 -040088 // Cipher changes are forbidden if the current epoch has leftover data.
David Benjamind9229f92017-10-06 17:36:20 -040089 if (tls_has_unprocessed_handshake_data(ssl)) {
David Benjaminf9cc26f2020-02-09 16:49:31 -050090 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
David Benjamind1e3ce12017-10-06 18:31:15 -040091 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
David Benjamin97250f42017-10-07 04:12:35 -040092 return false;
Steven Valdez143e8b32016-07-11 13:19:03 -040093 }
David Benjamin61672812016-07-14 23:10:43 -040094
David Benjamin5298ef92020-03-13 12:17:30 -040095 if (ssl->quic_method != nullptr) {
David Benjaminb571e772021-03-25 19:42:16 -040096 if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
97 !ssl->quic_method->set_read_secret(ssl, level, aead_ctx->cipher(),
David Benjamin5298ef92020-03-13 12:17:30 -040098 secret_for_quic.data(),
99 secret_for_quic.size())) {
100 return false;
101 }
102
103 // QUIC only uses |ssl| for handshake messages, which never use early data
104 // keys, so we return without installing anything. This avoids needing to
105 // have two secrets active at once in 0-RTT.
106 if (level == ssl_encryption_early_data) {
107 return true;
108 }
109 }
110
David Benjamin32013e82022-09-22 16:55:34 -0400111 ssl->s3->read_sequence = 0;
David Benjamin8e7bbba2017-10-13 17:18:35 -0400112 ssl->s3->aead_read_ctx = std::move(aead_ctx);
David Benjaminb0921922020-02-20 12:33:28 -0500113 ssl->s3->read_level = level;
David Benjamin97250f42017-10-07 04:12:35 -0400114 return true;
David Benjamin61672812016-07-14 23:10:43 -0400115}
116
David Benjaminb0921922020-02-20 12:33:28 -0500117static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level,
David Benjamin5298ef92020-03-13 12:17:30 -0400118 UniquePtr<SSLAEADContext> aead_ctx,
119 Span<const uint8_t> secret_for_quic) {
David Benjamin700631b2018-05-24 17:17:34 -0400120 if (!tls_flush_pending_hs_data(ssl)) {
121 return false;
122 }
123
David Benjamin5298ef92020-03-13 12:17:30 -0400124 if (ssl->quic_method != nullptr) {
David Benjaminb571e772021-03-25 19:42:16 -0400125 if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
126 !ssl->quic_method->set_write_secret(ssl, level, aead_ctx->cipher(),
David Benjamin5298ef92020-03-13 12:17:30 -0400127 secret_for_quic.data(),
128 secret_for_quic.size())) {
129 return false;
130 }
131
132 // QUIC only uses |ssl| for handshake messages, which never use early data
133 // keys, so we return without installing anything. This avoids needing to
134 // have two secrets active at once in 0-RTT.
135 if (level == ssl_encryption_early_data) {
136 return true;
137 }
138 }
139
David Benjamin32013e82022-09-22 16:55:34 -0400140 ssl->s3->write_sequence = 0;
David Benjamin8e7bbba2017-10-13 17:18:35 -0400141 ssl->s3->aead_write_ctx = std::move(aead_ctx);
David Benjaminb0921922020-02-20 12:33:28 -0500142 ssl->s3->write_level = level;
David Benjamin97250f42017-10-07 04:12:35 -0400143 return true;
David Benjamin61672812016-07-14 23:10:43 -0400144}
145
David Benjamina2c42d72016-07-08 09:05:45 -0700146static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
David Benjamin97250f42017-10-07 04:12:35 -0400147 false /* is_dtls */,
David Benjamin82a4b222020-02-09 17:51:45 -0500148 tls_new,
149 tls_free,
150 tls_get_message,
151 tls_next_message,
David Benjaminf9cc26f2020-02-09 16:49:31 -0500152 tls_has_unprocessed_handshake_data,
David Benjamin82a4b222020-02-09 17:51:45 -0500153 tls_open_handshake,
154 tls_open_change_cipher_spec,
155 tls_open_app_data,
156 tls_write_app_data,
157 tls_dispatch_alert,
158 tls_init_message,
159 tls_finish_message,
160 tls_add_message,
161 tls_add_change_cipher_spec,
162 tls_flush_flight,
163 tls_on_handshake_complete,
164 tls_set_read_state,
165 tls_set_write_state,
David Benjamin82c9e902014-12-12 15:55:27 -0500166};
David Benjamin5b33a5e2014-09-24 16:27:30 -0400167
David Benjamin1e77ef42019-03-30 01:03:50 -0500168static bool ssl_noop_x509_check_client_CA_names(
David Benjamin86e95b82017-07-18 16:34:25 -0400169 STACK_OF(CRYPTO_BUFFER) *names) {
David Benjamin1e77ef42019-03-30 01:03:50 -0500170 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400171}
172
173static void ssl_noop_x509_clear(CERT *cert) {}
174static void ssl_noop_x509_free(CERT *cert) {}
175static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
176static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
177static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
David Benjamin1e77ef42019-03-30 01:03:50 -0500178static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
179 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400180}
David Benjamin1e77ef42019-03-30 01:03:50 -0500181static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
182 const SSL_SESSION *session) {
183 return true;
David Benjamin86e95b82017-07-18 16:34:25 -0400184}
185static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
David Benjamin1e77ef42019-03-30 01:03:50 -0500186static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
187 SSL_HANDSHAKE *hs,
188 uint8_t *out_alert) {
189 return false;
David Benjamin86e95b82017-07-18 16:34:25 -0400190}
191
192static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
David Benjamin1e77ef42019-03-30 01:03:50 -0500193static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700194static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
195static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
David Benjamin1e77ef42019-03-30 01:03:50 -0500196static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
197 return true;
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700198}
David Benjamin1e77ef42019-03-30 01:03:50 -0500199static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
200static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) {}
David Benjamin86e95b82017-07-18 16:34:25 -0400201static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
202
David Benjaminba2d3df2017-08-04 13:59:24 -0400203const SSL_X509_METHOD ssl_noop_x509_method = {
David Benjamin86e95b82017-07-18 16:34:25 -0400204 ssl_noop_x509_check_client_CA_names,
205 ssl_noop_x509_clear,
206 ssl_noop_x509_free,
207 ssl_noop_x509_dup,
208 ssl_noop_x509_flush_cached_chain,
209 ssl_noop_x509_flush_cached_leaf,
210 ssl_noop_x509_session_cache_objects,
211 ssl_noop_x509_session_dup,
212 ssl_noop_x509_session_clear,
213 ssl_noop_x509_session_verify_cert_chain,
214 ssl_noop_x509_hs_flush_cached_ca_names,
215 ssl_noop_x509_ssl_new,
Matthew Braithwaiteb7bc80a2018-04-13 15:51:30 -0700216 ssl_noop_x509_ssl_config_free,
David Benjamin86e95b82017-07-18 16:34:25 -0400217 ssl_noop_x509_ssl_flush_cached_client_CA,
218 ssl_noop_x509_ssl_auto_chain_if_needed,
219 ssl_noop_x509_ssl_ctx_new,
220 ssl_noop_x509_ssl_ctx_free,
221 ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
222};
223
Joshua Liebow-Feeser8c7c6352018-08-26 18:53:36 -0700224BSSL_NAMESPACE_END
David Benjamin86e95b82017-07-18 16:34:25 -0400225
226using namespace bssl;
227
David Benjamin82c9e902014-12-12 15:55:27 -0500228const SSL_METHOD *TLS_method(void) {
David Benjamina2c42d72016-07-08 09:05:45 -0700229 static const SSL_METHOD kMethod = {
David Benjamin82c9e902014-12-12 15:55:27 -0500230 0,
David Benjamina2c42d72016-07-08 09:05:45 -0700231 &kTLSProtocolMethod,
Adam Langley3509dac2017-02-01 11:59:18 -0800232 &ssl_crypto_x509_method,
David Benjamin82c9e902014-12-12 15:55:27 -0500233 };
David Benjamina2c42d72016-07-08 09:05:45 -0700234 return &kMethod;
David Benjamin82c9e902014-12-12 15:55:27 -0500235}
David Benjamin5b33a5e2014-09-24 16:27:30 -0400236
David Benjamin82c9e902014-12-12 15:55:27 -0500237const SSL_METHOD *SSLv23_method(void) {
238 return TLS_method();
239}
David Benjamin5b33a5e2014-09-24 16:27:30 -0400240
David Benjamin86e95b82017-07-18 16:34:25 -0400241const SSL_METHOD *TLS_with_buffers_method(void) {
242 static const SSL_METHOD kMethod = {
243 0,
244 &kTLSProtocolMethod,
245 &ssl_noop_x509_method,
246 };
247 return &kMethod;
248}
249
David Benjaminc11ea9422017-08-29 16:33:21 -0400250// Legacy version-locked methods.
David Benjamincde8aba2014-11-23 15:47:20 -0500251
David Benjamin82c9e902014-12-12 15:55:27 -0500252const SSL_METHOD *TLSv1_2_method(void) {
David Benjamina2c42d72016-07-08 09:05:45 -0700253 static const SSL_METHOD kMethod = {
David Benjamin82c9e902014-12-12 15:55:27 -0500254 TLS1_2_VERSION,
David Benjamina2c42d72016-07-08 09:05:45 -0700255 &kTLSProtocolMethod,
Adam Langley3509dac2017-02-01 11:59:18 -0800256 &ssl_crypto_x509_method,
David Benjamin82c9e902014-12-12 15:55:27 -0500257 };
David Benjamina2c42d72016-07-08 09:05:45 -0700258 return &kMethod;
David Benjamin82c9e902014-12-12 15:55:27 -0500259}
David Benjamincde8aba2014-11-23 15:47:20 -0500260
David Benjamin82c9e902014-12-12 15:55:27 -0500261const SSL_METHOD *TLSv1_1_method(void) {
David Benjamina2c42d72016-07-08 09:05:45 -0700262 static const SSL_METHOD kMethod = {
David Benjamin82c9e902014-12-12 15:55:27 -0500263 TLS1_1_VERSION,
David Benjamina2c42d72016-07-08 09:05:45 -0700264 &kTLSProtocolMethod,
Adam Langley3509dac2017-02-01 11:59:18 -0800265 &ssl_crypto_x509_method,
David Benjamin82c9e902014-12-12 15:55:27 -0500266 };
David Benjamina2c42d72016-07-08 09:05:45 -0700267 return &kMethod;
David Benjamin82c9e902014-12-12 15:55:27 -0500268}
David Benjamincde8aba2014-11-23 15:47:20 -0500269
David Benjamin82c9e902014-12-12 15:55:27 -0500270const SSL_METHOD *TLSv1_method(void) {
David Benjamina2c42d72016-07-08 09:05:45 -0700271 static const SSL_METHOD kMethod = {
David Benjamin82c9e902014-12-12 15:55:27 -0500272 TLS1_VERSION,
David Benjamina2c42d72016-07-08 09:05:45 -0700273 &kTLSProtocolMethod,
Adam Langley3509dac2017-02-01 11:59:18 -0800274 &ssl_crypto_x509_method,
David Benjamin82c9e902014-12-12 15:55:27 -0500275 };
David Benjamina2c42d72016-07-08 09:05:45 -0700276 return &kMethod;
David Benjamin82c9e902014-12-12 15:55:27 -0500277}
David Benjamincde8aba2014-11-23 15:47:20 -0500278
David Benjaminc11ea9422017-08-29 16:33:21 -0400279// Legacy side-specific methods.
David Benjamincde8aba2014-11-23 15:47:20 -0500280
David Benjamin82c9e902014-12-12 15:55:27 -0500281const SSL_METHOD *TLSv1_2_server_method(void) {
282 return TLSv1_2_method();
283}
David Benjamincde8aba2014-11-23 15:47:20 -0500284
David Benjamin82c9e902014-12-12 15:55:27 -0500285const SSL_METHOD *TLSv1_1_server_method(void) {
286 return TLSv1_1_method();
287}
David Benjamincde8aba2014-11-23 15:47:20 -0500288
David Benjamin82c9e902014-12-12 15:55:27 -0500289const SSL_METHOD *TLSv1_server_method(void) {
290 return TLSv1_method();
291}
292
David Benjamin82c9e902014-12-12 15:55:27 -0500293const SSL_METHOD *TLSv1_2_client_method(void) {
294 return TLSv1_2_method();
295}
296
297const SSL_METHOD *TLSv1_1_client_method(void) {
298 return TLSv1_1_method();
299}
300
301const SSL_METHOD *TLSv1_client_method(void) {
302 return TLSv1_method();
303}
304
David Benjamin82c9e902014-12-12 15:55:27 -0500305const SSL_METHOD *SSLv23_server_method(void) {
306 return SSLv23_method();
307}
308
309const SSL_METHOD *SSLv23_client_method(void) {
310 return SSLv23_method();
311}
David Benjamina9c3bf12016-08-05 10:41:07 -0400312
313const SSL_METHOD *TLS_server_method(void) {
314 return TLS_method();
315}
316
317const SSL_METHOD *TLS_client_method(void) {
318 return TLS_method();
319}