Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/ssl/tls_method.cc

raw
1 // Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/ssl.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/err.h>
10
11 #include "../crypto/internal.h"
12 #include "internal.h"
13
14
15 BSSL_NAMESPACE_BEGIN
16
17 static void tls_on_handshake_complete(SSL *ssl) {
18 // The handshake should have released its final message.
19 assert(!ssl->s3->has_message);
20
21 // During the handshake, |hs_buf| is retained. Release if it there is no
22 // excess in it. There should not be any excess because the handshake logic
23 // rejects unprocessed data after each Finished message. Note this means we do
24 // not allow a TLS 1.2 HelloRequest to be packed into the same record as
25 // Finished. (Schannel also rejects this.)
26 assert(!ssl->s3->hs_buf || ssl->s3->hs_buf->length == 0);
27 if (ssl->s3->hs_buf && ssl->s3->hs_buf->length == 0) {
28 ssl->s3->hs_buf.reset();
29 }
30 }
31
32 static bool tls_set_read_state(SSL *ssl, ssl_encryption_level_t level,
33 UniquePtr<SSLAEADContext> aead_ctx,
34 Span<const uint8_t> secret_for_quic) {
35 // Cipher changes are forbidden if the current epoch has leftover data.
36 if (tls_has_unprocessed_handshake_data(ssl)) {
37 OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA);
38 ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
39 return false;
40 }
41
42 if (ssl->quic_method != nullptr) {
43 if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
44 !ssl->quic_method->set_read_secret(ssl, level, aead_ctx->cipher(),
45 secret_for_quic.data(),
46 secret_for_quic.size())) {
47 return false;
48 }
49
50 // QUIC only uses |ssl| for handshake messages, which never use early data
51 // keys, so we return without installing anything. This avoids needing to
52 // have two secrets active at once in 0-RTT.
53 if (level == ssl_encryption_early_data) {
54 return true;
55 }
56 }
57
58 OPENSSL_memset(ssl->s3->read_sequence, 0, sizeof(ssl->s3->read_sequence));
59 ssl->s3->aead_read_ctx = std::move(aead_ctx);
60 ssl->s3->read_level = level;
61 return true;
62 }
63
64 static bool tls_set_write_state(SSL *ssl, ssl_encryption_level_t level,
65 UniquePtr<SSLAEADContext> aead_ctx,
66 Span<const uint8_t> secret_for_quic) {
67 if (!tls_flush_pending_hs_data(ssl)) {
68 return false;
69 }
70
71 if (ssl->quic_method != nullptr) {
72 if ((ssl->s3->hs == nullptr || !ssl->s3->hs->hints_requested) &&
73 !ssl->quic_method->set_write_secret(ssl, level, aead_ctx->cipher(),
74 secret_for_quic.data(),
75 secret_for_quic.size())) {
76 return false;
77 }
78
79 // QUIC only uses |ssl| for handshake messages, which never use early data
80 // keys, so we return without installing anything. This avoids needing to
81 // have two secrets active at once in 0-RTT.
82 if (level == ssl_encryption_early_data) {
83 return true;
84 }
85 }
86
87 OPENSSL_memset(ssl->s3->write_sequence, 0, sizeof(ssl->s3->write_sequence));
88 ssl->s3->aead_write_ctx = std::move(aead_ctx);
89 ssl->s3->write_level = level;
90 return true;
91 }
92
93 static const SSL_PROTOCOL_METHOD kTLSProtocolMethod = {
94 false /* is_dtls */,
95 tls_new,
96 tls_free,
97 tls_get_message,
98 tls_next_message,
99 tls_has_unprocessed_handshake_data,
100 tls_open_handshake,
101 tls_open_change_cipher_spec,
102 tls_open_app_data,
103 tls_write_app_data,
104 tls_dispatch_alert,
105 tls_init_message,
106 tls_finish_message,
107 tls_add_message,
108 tls_add_change_cipher_spec,
109 tls_flush_flight,
110 tls_on_handshake_complete,
111 tls_set_read_state,
112 tls_set_write_state,
113 };
114
115 static bool ssl_noop_x509_check_client_CA_names(
116 STACK_OF(CRYPTO_BUFFER) *names) {
117 return true;
118 }
119
120 static void ssl_noop_x509_clear(CERT *cert) {}
121 static void ssl_noop_x509_free(CERT *cert) {}
122 static void ssl_noop_x509_dup(CERT *new_cert, const CERT *cert) {}
123 static void ssl_noop_x509_flush_cached_leaf(CERT *cert) {}
124 static void ssl_noop_x509_flush_cached_chain(CERT *cert) {}
125 static bool ssl_noop_x509_session_cache_objects(SSL_SESSION *sess) {
126 return true;
127 }
128 static bool ssl_noop_x509_session_dup(SSL_SESSION *new_session,
129 const SSL_SESSION *session) {
130 return true;
131 }
132 static void ssl_noop_x509_session_clear(SSL_SESSION *session) {}
133 static bool ssl_noop_x509_session_verify_cert_chain(SSL_SESSION *session,
134 SSL_HANDSHAKE *hs,
135 uint8_t *out_alert) {
136 return false;
137 }
138
139 static void ssl_noop_x509_hs_flush_cached_ca_names(SSL_HANDSHAKE *hs) {}
140 static bool ssl_noop_x509_ssl_new(SSL_HANDSHAKE *hs) { return true; }
141 static void ssl_noop_x509_ssl_config_free(SSL_CONFIG *cfg) {}
142 static void ssl_noop_x509_ssl_flush_cached_client_CA(SSL_CONFIG *cfg) {}
143 static bool ssl_noop_x509_ssl_auto_chain_if_needed(SSL_HANDSHAKE *hs) {
144 return true;
145 }
146 static bool ssl_noop_x509_ssl_ctx_new(SSL_CTX *ctx) { return true; }
147 static void ssl_noop_x509_ssl_ctx_free(SSL_CTX *ctx) {}
148 static void ssl_noop_x509_ssl_ctx_flush_cached_client_CA(SSL_CTX *ctx) {}
149
150 const SSL_X509_METHOD ssl_noop_x509_method = {
151 ssl_noop_x509_check_client_CA_names,
152 ssl_noop_x509_clear,
153 ssl_noop_x509_free,
154 ssl_noop_x509_dup,
155 ssl_noop_x509_flush_cached_chain,
156 ssl_noop_x509_flush_cached_leaf,
157 ssl_noop_x509_session_cache_objects,
158 ssl_noop_x509_session_dup,
159 ssl_noop_x509_session_clear,
160 ssl_noop_x509_session_verify_cert_chain,
161 ssl_noop_x509_hs_flush_cached_ca_names,
162 ssl_noop_x509_ssl_new,
163 ssl_noop_x509_ssl_config_free,
164 ssl_noop_x509_ssl_flush_cached_client_CA,
165 ssl_noop_x509_ssl_auto_chain_if_needed,
166 ssl_noop_x509_ssl_ctx_new,
167 ssl_noop_x509_ssl_ctx_free,
168 ssl_noop_x509_ssl_ctx_flush_cached_client_CA,
169 };
170
171 BSSL_NAMESPACE_END
172
173 using namespace bssl;
174
175 const SSL_METHOD *TLS_method(void) {
176 static const SSL_METHOD kMethod = {
177 0,
178 &kTLSProtocolMethod,
179 &ssl_crypto_x509_method,
180 };
181 return &kMethod;
182 }
183
184 const SSL_METHOD *SSLv23_method(void) {
185 return TLS_method();
186 }
187
188 const SSL_METHOD *TLS_with_buffers_method(void) {
189 static const SSL_METHOD kMethod = {
190 0,
191 &kTLSProtocolMethod,
192 &ssl_noop_x509_method,
193 };
194 return &kMethod;
195 }
196
197 // Legacy version-locked methods.
198
199 const SSL_METHOD *TLSv1_2_method(void) {
200 static const SSL_METHOD kMethod = {
201 TLS1_2_VERSION,
202 &kTLSProtocolMethod,
203 &ssl_crypto_x509_method,
204 };
205 return &kMethod;
206 }
207
208 const SSL_METHOD *TLSv1_1_method(void) {
209 static const SSL_METHOD kMethod = {
210 TLS1_1_VERSION,
211 &kTLSProtocolMethod,
212 &ssl_crypto_x509_method,
213 };
214 return &kMethod;
215 }
216
217 const SSL_METHOD *TLSv1_method(void) {
218 static const SSL_METHOD kMethod = {
219 TLS1_VERSION,
220 &kTLSProtocolMethod,
221 &ssl_crypto_x509_method,
222 };
223 return &kMethod;
224 }
225
226 // Legacy side-specific methods.
227
228 const SSL_METHOD *TLSv1_2_server_method(void) {
229 return TLSv1_2_method();
230 }
231
232 const SSL_METHOD *TLSv1_1_server_method(void) {
233 return TLSv1_1_method();
234 }
235
236 const SSL_METHOD *TLSv1_server_method(void) {
237 return TLSv1_method();
238 }
239
240 const SSL_METHOD *TLSv1_2_client_method(void) {
241 return TLSv1_2_method();
242 }
243
244 const SSL_METHOD *TLSv1_1_client_method(void) {
245 return TLSv1_1_method();
246 }
247
248 const SSL_METHOD *TLSv1_client_method(void) {
249 return TLSv1_method();
250 }
251
252 const SSL_METHOD *SSLv23_server_method(void) {
253 return SSLv23_method();
254 }
255
256 const SSL_METHOD *SSLv23_client_method(void) {
257 return SSLv23_method();
258 }
259
260 const SSL_METHOD *TLS_server_method(void) {
261 return TLS_method();
262 }
263
264 const SSL_METHOD *TLS_client_method(void) {
265 return TLS_method();
266 }