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 #include <openssl/ssl.h>
58
59 #include <assert.h>
60 #include <string.h>
61
62 #include <openssl/err.h>
63
64 #include "../crypto/internal.h"
65 #include "internal.h"
66
67
68 BSSL_NAMESPACE_BEGIN
69
tls_on_handshake_complete(SSL * ssl)70 static void tls_on_handshake_complete(SSL *ssl) {
71 // The handshake should have released its final message.
72 assert(!ssl->s3->has_message);
73
74 // During the handshake, |hs_buf| is retained. Release if it there is no
75 // 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);
80 if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
81 ssl->s3->hs_buf.reset();
82 }
83 }
84
tls_set_read_state(SSL * ssl,ssl_encryption_level_t level,UniquePtr<SSLAEADContext> aead_ctx,Span<const uint8_t> secret_for_quic)85 static bool tls_set_read_state(SSL *ssl, ssl_encryption_level_t level,
86 UniquePtr<SSLAEADContext> aead_ctx,
87 Span<const uint8_t> secret_for_quic) {
88 // Cipher changes are forbidden if the current epoch has leftover data.
89 if (tls_has_unprocessed_handshake_data(ssl)) {
90 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
91 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
92 return false;
93 }
94
95 if (ssl->quic_method != nullptr) {
96 if (!ssl->quic_method->set_read_secret(ssl, level, aead_ctx->cipher(),
97 secret_for_quic.data(),
98 secret_for_quic.size())) {
99 return false;
100 }
101
102 // QUIC only uses |ssl| for handshake messages, which never use early data
103 // keys, so we return without installing anything. This avoids needing to
104 // have two secrets active at once in 0-RTT.
105 if (level == ssl_encryption_early_data) {
106 return true;
107 }
108 }
109
110 OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
111 ssl->s3->aead_read_ctx = std::move(aead_ctx);
112 ssl->s3->read_level = level;
113 return true;
114 }
115
tls_set_write_state(SSL * ssl,ssl_encryption_level_t level,UniquePtr<SSLAEADContext> aead_ctx,Span<const uint8_t> secret_for_quic)116 static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level,
117 UniquePtr<SSLAEADContext> aead_ctx,
118 Span<const uint8_t> secret_for_quic) {
119 if (!tls_flush_pending_hs_data(ssl)) {
120 return false;
121 }
122
123 if (ssl->quic_method != nullptr) {
124 if (!ssl->quic_method->set_write_secret(ssl, level, aead_ctx->cipher(),
125 secret_for_quic.data(),
126 secret_for_quic.size())) {
127 return false;
128 }
129
130 // QUIC only uses |ssl| for handshake messages, which never use early data
131 // keys, so we return without installing anything. This avoids needing to
132 // have two secrets active at once in 0-RTT.
133 if (level == ssl_encryption_early_data) {
134 return true;
135 }
136 }
137
138 OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
139 ssl->s3->aead_write_ctx = std::move(aead_ctx);
140 ssl->s3->write_level = level;
141 return true;
142 }
143
144 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
145 false /* is_dtls */,
146 tls_new,
147 tls_free,
148 tls_get_message,
149 tls_next_message,
150 tls_has_unprocessed_handshake_data,
151 tls_open_handshake,
152 tls_open_change_cipher_spec,
153 tls_open_app_data,
154 tls_write_app_data,
155 tls_dispatch_alert,
156 tls_init_message,
157 tls_finish_message,
158 tls_add_message,
159 tls_add_change_cipher_spec,
160 tls_flush_flight,
161 tls_on_handshake_complete,
162 tls_set_read_state,
163 tls_set_write_state,
164 };
165
ssl_noop_x509_check_client_CA_names(STACK_OF (CRYPTO_BUFFER)* names)166 static bool ssl_noop_x509_check_client_CA_names(
167 STACK_OF(CRYPTO_BUFFER) *names) {
168 return true;
169 }
170
ssl_noop_x509_clear(CERT * cert)171 static void ssl_noop_x509_clear(CERT *cert) {}
ssl_noop_x509_free(CERT * cert)172 static void ssl_noop_x509_free(CERT *cert) {}
ssl_noop_x509_dup(CERT * new_cert,const CERT * cert)173 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
ssl_noop_x509_flush_cached_leaf(CERT * cert)174 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
ssl_noop_x509_flush_cached_chain(CERT * cert)175 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
ssl_noop_x509_session_cache_objects(SSL_SESSION * sess)176 static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
177 return true;
178 }
ssl_noop_x509_session_dup(SSL_SESSION * new_session,const SSL_SESSION * session)179 static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
180 const SSL_SESSION *session) {
181 return true;
182 }
ssl_noop_x509_session_clear(SSL_SESSION * session)183 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
ssl_noop_x509_session_verify_cert_chain(SSL_SESSION * session,SSL_HANDSHAKE * hs,uint8_t * out_alert)184 static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
185 SSL_HANDSHAKE *hs,
186 uint8_t *out_alert) {
187 return false;
188 }
189
ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)190 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
ssl_noop_x509_ssl_new(SSL_HANDSHAKE * hs)191 static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
ssl_noop_x509_ssl_config_free(SSL_CONFIG * cfg)192 static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG * cfg)193 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE * hs)194 static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
195 return true;
196 }
ssl_noop_x509_ssl_ctx_new(SSL_CTX * ctx)197 static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
ssl_noop_x509_ssl_ctx_free(SSL_CTX * ctx)198 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) {}
ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)199 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
200
201 const SSL_X509_METHOD ssl_noop_x509_method = {
202 ssl_noop_x509_check_client_CA_names,
203 ssl_noop_x509_clear,
204 ssl_noop_x509_free,
205 ssl_noop_x509_dup,
206 ssl_noop_x509_flush_cached_chain,
207 ssl_noop_x509_flush_cached_leaf,
208 ssl_noop_x509_session_cache_objects,
209 ssl_noop_x509_session_dup,
210 ssl_noop_x509_session_clear,
211 ssl_noop_x509_session_verify_cert_chain,
212 ssl_noop_x509_hs_flush_cached_ca_names,
213 ssl_noop_x509_ssl_new,
214 ssl_noop_x509_ssl_config_free,
215 ssl_noop_x509_ssl_flush_cached_client_CA,
216 ssl_noop_x509_ssl_auto_chain_if_needed,
217 ssl_noop_x509_ssl_ctx_new,
218 ssl_noop_x509_ssl_ctx_free,
219 ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
220 };
221
222 BSSL_NAMESPACE_END
223
224 using namespace bssl;
225
TLS_method(void)226 const SSL_METHOD *TLS_method(void) {
227 static const SSL_METHOD kMethod = {
228 0,
229 &kTLSProtocolMethod,
230 &ssl_crypto_x509_method,
231 };
232 return &kMethod;
233 }
234
SSLv23_method(void)235 const SSL_METHOD *SSLv23_method(void) {
236 return TLS_method();
237 }
238
TLS_with_buffers_method(void)239 const SSL_METHOD *TLS_with_buffers_method(void) {
240 static const SSL_METHOD kMethod = {
241 0,
242 &kTLSProtocolMethod,
243 &ssl_noop_x509_method,
244 };
245 return &kMethod;
246 }
247
248 // Legacy version-locked methods.
249
TLSv1_2_method(void)250 const SSL_METHOD *TLSv1_2_method(void) {
251 static const SSL_METHOD kMethod = {
252 TLS1_2_VERSION,
253 &kTLSProtocolMethod,
254 &ssl_crypto_x509_method,
255 };
256 return &kMethod;
257 }
258
TLSv1_1_method(void)259 const SSL_METHOD *TLSv1_1_method(void) {
260 static const SSL_METHOD kMethod = {
261 TLS1_1_VERSION,
262 &kTLSProtocolMethod,
263 &ssl_crypto_x509_method,
264 };
265 return &kMethod;
266 }
267
TLSv1_method(void)268 const SSL_METHOD *TLSv1_method(void) {
269 static const SSL_METHOD kMethod = {
270 TLS1_VERSION,
271 &kTLSProtocolMethod,
272 &ssl_crypto_x509_method,
273 };
274 return &kMethod;
275 }
276
277 // Legacy side-specific methods.
278
TLSv1_2_server_method(void)279 const SSL_METHOD *TLSv1_2_server_method(void) {
280 return TLSv1_2_method();
281 }
282
TLSv1_1_server_method(void)283 const SSL_METHOD *TLSv1_1_server_method(void) {
284 return TLSv1_1_method();
285 }
286
TLSv1_server_method(void)287 const SSL_METHOD *TLSv1_server_method(void) {
288 return TLSv1_method();
289 }
290
TLSv1_2_client_method(void)291 const SSL_METHOD *TLSv1_2_client_method(void) {
292 return TLSv1_2_method();
293 }
294
TLSv1_1_client_method(void)295 const SSL_METHOD *TLSv1_1_client_method(void) {
296 return TLSv1_1_method();
297 }
298
TLSv1_client_method(void)299 const SSL_METHOD *TLSv1_client_method(void) {
300 return TLSv1_method();
301 }
302
SSLv23_server_method(void)303 const SSL_METHOD *SSLv23_server_method(void) {
304 return SSLv23_method();
305 }
306
SSLv23_client_method(void)307 const SSL_METHOD *SSLv23_client_method(void) {
308 return SSLv23_method();
309 }
310
TLS_server_method(void)311 const SSL_METHOD *TLS_server_method(void) {
312 return TLS_method();
313 }
314
TLS_client_method(void)315 const SSL_METHOD *TLS_client_method(void) {
316 return TLS_method();
317 }
318