xref: /aosp_15_r20/external/boringssl/src/ssl/tls_method.cc (revision 8fb009dc861624b67b6cdb62ea21f0f22d0c584b)
1 /* Copyright (C) 1995-1998 Eric Young ([email protected])
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young ([email protected]).
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 ([email protected]).
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 ([email protected])"
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 ([email protected])"
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->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
97         !ssl->quic_method->set_read_secret(ssl, level, aead_ctx->cipher(),
98                                            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 
111   ssl->s3->read_sequence = 0;
112   ssl->s3->aead_read_ctx = std::move(aead_ctx);
113   ssl->s3->read_level = level;
114   return true;
115 }
116 
tls_set_write_state(SSL * ssl,ssl_encryption_level_t level,UniquePtr<SSLAEADContext> aead_ctx,Span<const uint8_t> secret_for_quic)117 static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level,
118                                 UniquePtr<SSLAEADContext> aead_ctx,
119                                 Span<const uint8_t> secret_for_quic) {
120   if (!tls_flush_pending_hs_data(ssl)) {
121     return false;
122   }
123 
124   if (ssl->quic_method != nullptr) {
125     if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
126         !ssl->quic_method->set_write_secret(ssl, level, aead_ctx->cipher(),
127                                             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 
140   ssl->s3->write_sequence = 0;
141   ssl->s3->aead_write_ctx = std::move(aead_ctx);
142   ssl->s3->write_level = level;
143   return true;
144 }
145 
146 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
147     false /* is_dtls */,
148     tls_new,
149     tls_free,
150     tls_get_message,
151     tls_next_message,
152     tls_has_unprocessed_handshake_data,
153     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,
166 };
167 
ssl_noop_x509_check_client_CA_names(STACK_OF (CRYPTO_BUFFER)* names)168 static bool ssl_noop_x509_check_client_CA_names(
169     STACK_OF(CRYPTO_BUFFER) *names) {
170   return true;
171 }
172 
ssl_noop_x509_clear(CERT * cert)173 static void ssl_noop_x509_clear(CERT *cert) {}
ssl_noop_x509_free(CERT * cert)174 static void ssl_noop_x509_free(CERT *cert) {}
ssl_noop_x509_dup(CERT * new_cert,const CERT * cert)175 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
ssl_noop_x509_flush_cached_leaf(CERT * cert)176 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
ssl_noop_x509_flush_cached_chain(CERT * cert)177 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
ssl_noop_x509_session_cache_objects(SSL_SESSION * sess)178 static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
179   return true;
180 }
ssl_noop_x509_session_dup(SSL_SESSION * new_session,const SSL_SESSION * session)181 static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
182                                       const SSL_SESSION *session) {
183   return true;
184 }
ssl_noop_x509_session_clear(SSL_SESSION * session)185 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)186 static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
187                                                     SSL_HANDSHAKE *hs,
188                                                     uint8_t *out_alert) {
189   return false;
190 }
191 
ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE * hs)192 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
ssl_noop_x509_ssl_new(SSL_HANDSHAKE * hs)193 static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
ssl_noop_x509_ssl_config_free(SSL_CONFIG * cfg)194 static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG * cfg)195 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE * hs)196 static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
197   return true;
198 }
ssl_noop_x509_ssl_ctx_new(SSL_CTX * ctx)199 static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
ssl_noop_x509_ssl_ctx_free(SSL_CTX * ctx)200 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) {}
ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX * ctx)201 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
202 
203 const SSL_X509_METHOD ssl_noop_x509_method = {
204   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,
216   ssl_noop_x509_ssl_config_free,
217   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 
224 BSSL_NAMESPACE_END
225 
226 using namespace bssl;
227 
TLS_method(void)228 const SSL_METHOD *TLS_method(void) {
229   static const SSL_METHOD kMethod = {
230       0,
231       &kTLSProtocolMethod,
232       &ssl_crypto_x509_method,
233   };
234   return &kMethod;
235 }
236 
SSLv23_method(void)237 const SSL_METHOD *SSLv23_method(void) {
238   return TLS_method();
239 }
240 
TLS_with_buffers_method(void)241 const 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 
250 // Legacy version-locked methods.
251 
TLSv1_2_method(void)252 const SSL_METHOD *TLSv1_2_method(void) {
253   static const SSL_METHOD kMethod = {
254       TLS1_2_VERSION,
255       &kTLSProtocolMethod,
256       &ssl_crypto_x509_method,
257   };
258   return &kMethod;
259 }
260 
TLSv1_1_method(void)261 const SSL_METHOD *TLSv1_1_method(void) {
262   static const SSL_METHOD kMethod = {
263       TLS1_1_VERSION,
264       &kTLSProtocolMethod,
265       &ssl_crypto_x509_method,
266   };
267   return &kMethod;
268 }
269 
TLSv1_method(void)270 const SSL_METHOD *TLSv1_method(void) {
271   static const SSL_METHOD kMethod = {
272       TLS1_VERSION,
273       &kTLSProtocolMethod,
274       &ssl_crypto_x509_method,
275   };
276   return &kMethod;
277 }
278 
279 // Legacy side-specific methods.
280 
TLSv1_2_server_method(void)281 const SSL_METHOD *TLSv1_2_server_method(void) {
282   return TLSv1_2_method();
283 }
284 
TLSv1_1_server_method(void)285 const SSL_METHOD *TLSv1_1_server_method(void) {
286   return TLSv1_1_method();
287 }
288 
TLSv1_server_method(void)289 const SSL_METHOD *TLSv1_server_method(void) {
290   return TLSv1_method();
291 }
292 
TLSv1_2_client_method(void)293 const SSL_METHOD *TLSv1_2_client_method(void) {
294   return TLSv1_2_method();
295 }
296 
TLSv1_1_client_method(void)297 const SSL_METHOD *TLSv1_1_client_method(void) {
298   return TLSv1_1_method();
299 }
300 
TLSv1_client_method(void)301 const SSL_METHOD *TLSv1_client_method(void) {
302   return TLSv1_method();
303 }
304 
SSLv23_server_method(void)305 const SSL_METHOD *SSLv23_server_method(void) {
306   return SSLv23_method();
307 }
308 
SSLv23_client_method(void)309 const SSL_METHOD *SSLv23_client_method(void) {
310   return SSLv23_method();
311 }
312 
TLS_server_method(void)313 const SSL_METHOD *TLS_server_method(void) {
314   return TLS_method();
315 }
316 
TLS_client_method(void)317 const SSL_METHOD *TLS_client_method(void) {
318   return TLS_method();
319 }
320