]>
Commit | Line | Data |
---|---|---|
f8e0a557 | 1 | /* |
0c679f55 | 2 | * Copyright 2015-2025 The OpenSSL Project Authors. All Rights Reserved. |
f8e0a557 | 3 | * |
2c18d164 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
846e33c7 RS |
5 | * this file except in compliance with the License. You can obtain a copy |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
f8e0a557 MC |
8 | */ |
9 | ||
2bb83824 F |
10 | #include "internal/e_os.h" |
11 | ||
650c6687 RB |
12 | #if defined(__TANDEM) && defined(_SPT_MODEL_) |
13 | # include <spthread.h> | |
14 | # include <spt_extensions.h> /* timeval */ | |
15 | #endif | |
16 | ||
198c42f5 | 17 | #include "internal/cryptlib.h" |
bf553267 | 18 | #include "internal/ssl_unwrap.h" |
07016a8a | 19 | #include <openssl/rand.h> |
706457b7 DMSP |
20 | #include "../ssl_local.h" |
21 | #include "statem_local.h" | |
f9f674eb | 22 | #include <assert.h> |
f8e0a557 MC |
23 | |
24 | /* | |
25 | * This file implements the SSL/TLS/DTLS state machines. | |
26 | * | |
27 | * There are two primary state machines: | |
28 | * | |
29 | * 1) Message flow state machine | |
30 | * 2) Handshake state machine | |
31 | * | |
32 | * The Message flow state machine controls the reading and sending of messages | |
33 | * including handling of non-blocking IO events, flushing of the underlying | |
34 | * write BIO, handling unexpected messages, etc. It is itself broken into two | |
35 | * separate sub-state machines which control reading and writing respectively. | |
36 | * | |
37 | * The Handshake state machine keeps track of the current SSL/TLS handshake | |
38 | * state. Transitions of the handshake state are the result of events that | |
39 | * occur within the Message flow state machine. | |
40 | * | |
41 | * Overall it looks like this: | |
42 | * | |
43 | * --------------------------------------------- ------------------- | |
44 | * | | | | | |
45 | * | Message flow state machine | | | | |
46 | * | | | | | |
47 | * | -------------------- -------------------- | Transition | Handshake state | | |
61ae935a | 48 | * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine | |
f8e0a557 MC |
49 | * | | sub-state | | sub-state | |----------->| | |
50 | * | | machine for | | machine for | | | | | |
51 | * | | reading messages | | writing messages | | | | | |
52 | * | -------------------- -------------------- | | | | |
53 | * | | | | | |
54 | * --------------------------------------------- ------------------- | |
55 | * | |
56 | */ | |
57 | ||
58 | /* Sub state machine return values */ | |
a230b26e | 59 | typedef enum { |
f8e0a557 MC |
60 | /* Something bad happened or NBIO */ |
61 | SUB_STATE_ERROR, | |
62 | /* Sub state finished go to the next sub state */ | |
63 | SUB_STATE_FINISHED, | |
64 | /* Sub state finished and handshake was completed */ | |
65 | SUB_STATE_END_HANDSHAKE | |
d78052cf | 66 | } SUB_STATE_RETURN; |
f8e0a557 | 67 | |
38b051a1 TM |
68 | static int state_machine(SSL_CONNECTION *s, int server); |
69 | static void init_read_state_machine(SSL_CONNECTION *s); | |
70 | static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s); | |
71 | static void init_write_state_machine(SSL_CONNECTION *s); | |
72 | static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s); | |
49ae7423 | 73 | |
5998e290 | 74 | OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl) |
49ae7423 | 75 | { |
38b051a1 TM |
76 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl); |
77 | ||
78 | if (sc == NULL) | |
79 | return TLS_ST_BEFORE; | |
80 | ||
81 | return sc->statem.hand_state; | |
49ae7423 MC |
82 | } |
83 | ||
4cc968df | 84 | int SSL_in_init(const SSL *s) |
49ae7423 | 85 | { |
38b051a1 TM |
86 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
87 | ||
88 | if (sc == NULL) | |
89 | return 0; | |
90 | ||
91 | return sc->statem.in_init; | |
49ae7423 MC |
92 | } |
93 | ||
4cc968df | 94 | int SSL_is_init_finished(const SSL *s) |
49ae7423 | 95 | { |
38b051a1 TM |
96 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
97 | ||
98 | if (sc == NULL) | |
99 | return 0; | |
100 | ||
101 | return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK); | |
49ae7423 MC |
102 | } |
103 | ||
4cc968df | 104 | int SSL_in_before(const SSL *s) |
49ae7423 | 105 | { |
38b051a1 TM |
106 | const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s); |
107 | ||
108 | if (sc == NULL) | |
109 | return 0; | |
110 | ||
49ae7423 MC |
111 | /* |
112 | * Historically being "in before" meant before anything had happened. In the | |
113 | * current code though we remain in the "before" state for a while after we | |
114 | * have started the handshake process (e.g. as a server waiting for the | |
115 | * first message to arrive). There "in before" is taken to mean "in before" | |
116 | * and not started any handshake process yet. | |
117 | */ | |
38b051a1 TM |
118 | return (sc->statem.hand_state == TLS_ST_BEFORE) |
119 | && (sc->statem.state == MSG_FLOW_UNINITED); | |
49ae7423 MC |
120 | } |
121 | ||
846975f3 | 122 | OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s) |
123 | { | |
124 | return s != NULL ? s->statem.hand_state : TLS_ST_BEFORE; | |
125 | } | |
126 | ||
f8e0a557 MC |
127 | /* |
128 | * Clear the state machine state and reset back to MSG_FLOW_UNINITED | |
129 | */ | |
38b051a1 | 130 | void ossl_statem_clear(SSL_CONNECTION *s) |
f8e0a557 MC |
131 | { |
132 | s->statem.state = MSG_FLOW_UNINITED; | |
49ae7423 | 133 | s->statem.hand_state = TLS_ST_BEFORE; |
bfc0f10d | 134 | ossl_statem_set_in_init(s, 1); |
a71a4966 | 135 | s->statem.no_cert_verify = 0; |
f8e0a557 MC |
136 | } |
137 | ||
138 | /* | |
139 | * Set the state machine up ready for a renegotiation handshake | |
140 | */ | |
38b051a1 | 141 | void ossl_statem_set_renegotiate(SSL_CONNECTION *s) |
f8e0a557 | 142 | { |
bfc0f10d | 143 | ossl_statem_set_in_init(s, 1); |
0386aad1 | 144 | s->statem.request_state = TLS_ST_SW_HELLO_REQ; |
f8e0a557 MC |
145 | } |
146 | ||
38b051a1 | 147 | void ossl_statem_send_fatal(SSL_CONNECTION *s, int al) |
5a2d0ef3 RL |
148 | { |
149 | /* We shouldn't call SSLfatal() twice. Once is enough */ | |
150 | if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR) | |
151 | return; | |
bfc0f10d | 152 | ossl_statem_set_in_init(s, 1); |
5a2d0ef3 | 153 | s->statem.state = MSG_FLOW_ERROR; |
4bf610bd | 154 | if (al != SSL_AD_NO_ALERT) |
5a2d0ef3 RL |
155 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
156 | } | |
157 | ||
f8e0a557 | 158 | /* |
e92519b5 RL |
159 | * Error reporting building block that's used instead of ERR_set_error(). |
160 | * In addition to what ERR_set_error() does, this puts the state machine | |
161 | * into an error state and sends an alert if appropriate. | |
1f359471 | 162 | * This is a permanent error for the current connection. |
f8e0a557 | 163 | */ |
38b051a1 TM |
164 | void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason, |
165 | const char *fmt, ...) | |
f8e0a557 | 166 | { |
e92519b5 RL |
167 | va_list args; |
168 | ||
169 | va_start(args, fmt); | |
170 | ERR_vset_error(ERR_LIB_SSL, reason, fmt, args); | |
171 | va_end(args); | |
172 | ||
5a2d0ef3 | 173 | ossl_statem_send_fatal(s, al); |
49ae7423 MC |
174 | } |
175 | ||
47e2ee07 MC |
176 | /* |
177 | * This macro should only be called if we are already expecting to be in | |
178 | * a fatal error state. We verify that we are, and set it if not (this would | |
179 | * indicate a bug). | |
180 | */ | |
c48ffbcc | 181 | #define check_fatal(s) \ |
47e2ee07 MC |
182 | do { \ |
183 | if (!ossl_assert((s)->statem.in_init \ | |
e1dd8fa0 | 184 | && (s)->statem.state == MSG_FLOW_ERROR)) \ |
c48ffbcc | 185 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \ |
47e2ee07 MC |
186 | } while (0) |
187 | ||
49ae7423 MC |
188 | /* |
189 | * Discover whether the current connection is in the error state. | |
190 | * | |
191 | * Valid return values are: | |
192 | * 1: Yes | |
193 | * 0: No | |
194 | */ | |
38b051a1 | 195 | int ossl_statem_in_error(const SSL_CONNECTION *s) |
49ae7423 MC |
196 | { |
197 | if (s->statem.state == MSG_FLOW_ERROR) | |
198 | return 1; | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
38b051a1 | 203 | void ossl_statem_set_in_init(SSL_CONNECTION *s, int init) |
49ae7423 MC |
204 | { |
205 | s->statem.in_init = init; | |
cffafb5f MC |
206 | if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL) |
207 | s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init); | |
f8e0a557 MC |
208 | } |
209 | ||
38b051a1 | 210 | int ossl_statem_get_in_handshake(SSL_CONNECTION *s) |
024f543c MC |
211 | { |
212 | return s->statem.in_handshake; | |
213 | } | |
214 | ||
38b051a1 | 215 | void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand) |
024f543c MC |
216 | { |
217 | if (inhand) | |
218 | s->statem.in_handshake++; | |
219 | else | |
220 | s->statem.in_handshake--; | |
221 | } | |
222 | ||
0a87d0ac | 223 | /* Are we in a sensible state to skip over unreadable early data? */ |
38b051a1 | 224 | int ossl_statem_skip_early_data(SSL_CONNECTION *s) |
0a87d0ac | 225 | { |
1ea4d09a | 226 | if (s->ext.early_data != SSL_EARLY_DATA_REJECTED) |
0a87d0ac MC |
227 | return 0; |
228 | ||
1c1e4160 MC |
229 | if (!s->server |
230 | || s->statem.hand_state != TLS_ST_EARLY_DATA | |
231 | || s->hello_retry_request == SSL_HRR_COMPLETE) | |
d4504fe5 | 232 | return 0; |
0a87d0ac MC |
233 | |
234 | return 1; | |
235 | } | |
236 | ||
3eaa4170 MC |
237 | /* |
238 | * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept() | |
239 | * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early | |
240 | * data state and whether we should attempt to move the handshake on if so. | |
d1186c30 | 241 | * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are |
3eaa4170 MC |
242 | * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake() |
243 | * or similar. | |
244 | */ | |
1b3f27f9 | 245 | int ossl_statem_check_finish_init(SSL_CONNECTION *s, int sending) |
564547e4 | 246 | { |
d1186c30 | 247 | if (sending == -1) { |
3eaa4170 | 248 | if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END |
ef6c191b | 249 | || s->statem.hand_state == TLS_ST_EARLY_DATA) { |
3eaa4170 | 250 | ossl_statem_set_in_init(s, 1); |
ef6c191b MC |
251 | if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) { |
252 | /* | |
253 | * SSL_connect() or SSL_do_handshake() has been called directly. | |
254 | * We don't allow any more writing of early data. | |
255 | */ | |
256 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; | |
257 | } | |
258 | } | |
3eaa4170 | 259 | } else if (!s->server) { |
d1186c30 | 260 | if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END |
ef6c191b | 261 | || s->statem.hand_state == TLS_ST_EARLY_DATA) |
f7e393be | 262 | && s->early_data_state != SSL_EARLY_DATA_WRITING) |
d1186c30 | 263 | || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) { |
d7f8783f | 264 | ossl_statem_set_in_init(s, 1); |
ef6c191b MC |
265 | /* |
266 | * SSL_write() has been called directly. We don't allow any more | |
267 | * writing of early data. | |
268 | */ | |
d1186c30 | 269 | if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) |
ef6c191b MC |
270 | s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; |
271 | } | |
f7e393be MC |
272 | } else { |
273 | if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING | |
274 | && s->statem.hand_state == TLS_ST_EARLY_DATA) | |
275 | ossl_statem_set_in_init(s, 1); | |
d7f8783f | 276 | } |
1b3f27f9 | 277 | return 1; |
564547e4 MC |
278 | } |
279 | ||
38b051a1 | 280 | void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s) |
31fd10e6 MC |
281 | { |
282 | s->statem.state = MSG_FLOW_UNINITED; | |
bfc0f10d | 283 | ossl_statem_set_in_init(s, 1); |
31fd10e6 MC |
284 | /* |
285 | * This will get reset (briefly) back to TLS_ST_BEFORE when we enter | |
286 | * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any | |
287 | * calls to SSL_in_before() will return false. Also calls to | |
288 | * SSL_state_string() and SSL_state_string_long() will return something | |
289 | * sensible. | |
290 | */ | |
291 | s->statem.hand_state = TLS_ST_SR_CLNT_HELLO; | |
292 | } | |
293 | ||
a230b26e EK |
294 | int ossl_statem_connect(SSL *s) |
295 | { | |
38b051a1 TM |
296 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
297 | ||
298 | if (sc == NULL) | |
299 | return -1; | |
300 | ||
301 | return state_machine(sc, 0); | |
8723588e MC |
302 | } |
303 | ||
fe3a3291 | 304 | int ossl_statem_accept(SSL *s) |
c130dd8e | 305 | { |
38b051a1 TM |
306 | SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s); |
307 | ||
308 | if (sc == NULL) | |
309 | return -1; | |
310 | ||
311 | return state_machine(sc, 1); | |
c130dd8e MC |
312 | } |
313 | ||
a230b26e EK |
314 | typedef void (*info_cb) (const SSL *, int, int); |
315 | ||
38b051a1 | 316 | static info_cb get_callback(SSL_CONNECTION *s) |
91eac8d5 | 317 | { |
38b051a1 TM |
318 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
319 | ||
91eac8d5 MC |
320 | if (s->info_callback != NULL) |
321 | return s->info_callback; | |
38b051a1 TM |
322 | else if (sctx->info_callback != NULL) |
323 | return sctx->info_callback; | |
91eac8d5 MC |
324 | |
325 | return NULL; | |
326 | } | |
327 | ||
f8e0a557 MC |
328 | /* |
329 | * The main message flow state machine. We start in the MSG_FLOW_UNINITED or | |
c7f47786 | 330 | * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and |
f8e0a557 MC |
331 | * transitions are as follows: |
332 | * | |
c7f47786 | 333 | * MSG_FLOW_UNINITED MSG_FLOW_FINISHED |
f8e0a557 MC |
334 | * | | |
335 | * +-----------------------+ | |
336 | * v | |
337 | * MSG_FLOW_WRITING <---> MSG_FLOW_READING | |
338 | * | | |
339 | * V | |
340 | * MSG_FLOW_FINISHED | |
341 | * | | |
342 | * V | |
343 | * [SUCCESS] | |
344 | * | |
345 | * We may exit at any point due to an error or NBIO event. If an NBIO event | |
346 | * occurs then we restart at the point we left off when we are recalled. | |
347 | * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them. | |
348 | * | |
349 | * In addition to the above there is also the MSG_FLOW_ERROR state. We can move | |
350 | * into that state at any point in the event that an irrecoverable error occurs. | |
351 | * | |
352 | * Valid return values are: | |
353 | * 1: Success | |
354 | * <=0: NBIO or error | |
355 | */ | |
38b051a1 | 356 | static int state_machine(SSL_CONNECTION *s, int server) |
4fa52141 | 357 | { |
f8e0a557 | 358 | BUF_MEM *buf = NULL; |
f8e0a557 | 359 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
d6f1a6e9 | 360 | OSSL_STATEM *st = &s->statem; |
f8e0a557 MC |
361 | int ret = -1; |
362 | int ssret; | |
38b051a1 | 363 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
dc84829c | 364 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
f8e0a557 MC |
365 | |
366 | if (st->state == MSG_FLOW_ERROR) { | |
367 | /* Shouldn't have been called if we're already in the error state */ | |
368 | return -1; | |
369 | } | |
370 | ||
f8e0a557 MC |
371 | ERR_clear_error(); |
372 | clear_sys_error(); | |
373 | ||
91eac8d5 | 374 | cb = get_callback(s); |
f8e0a557 | 375 | |
024f543c | 376 | st->in_handshake++; |
38b051a1 | 377 | if (!SSL_in_init(ssl) || SSL_in_before(ssl)) { |
808d1601 MC |
378 | /* |
379 | * If we are stateless then we already called SSL_clear() - don't do | |
380 | * it again and clear the STATELESS flag itself. | |
381 | */ | |
38b051a1 | 382 | if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl)) |
f8e0a557 MC |
383 | return -1; |
384 | } | |
473483d4 | 385 | #ifndef OPENSSL_NO_SCTP |
38b051a1 | 386 | if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
473483d4 MC |
387 | /* |
388 | * Notify SCTP BIO socket to enter handshake mode and prevent stream | |
99240875 | 389 | * identifier other than 0. |
473483d4 | 390 | */ |
38b051a1 | 391 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, |
024f543c | 392 | st->in_handshake, NULL); |
473483d4 MC |
393 | } |
394 | #endif | |
395 | ||
f8e0a557 | 396 | /* Initialise state machine */ |
0386aad1 | 397 | if (st->state == MSG_FLOW_UNINITED |
0386aad1 | 398 | || st->state == MSG_FLOW_FINISHED) { |
f8e0a557 MC |
399 | if (st->state == MSG_FLOW_UNINITED) { |
400 | st->hand_state = TLS_ST_BEFORE; | |
0386aad1 | 401 | st->request_state = TLS_ST_BEFORE; |
f8e0a557 MC |
402 | } |
403 | ||
404 | s->server = server; | |
4af5836b | 405 | if (cb != NULL) { |
38b051a1 | 406 | if (SSL_IS_FIRST_HANDSHAKE(s) || !SSL_CONNECTION_IS_TLS13(s)) |
dc84829c | 407 | cb(ussl, SSL_CB_HANDSHAKE_START, 1); |
4af5836b | 408 | } |
f8e0a557 | 409 | |
47e2ee07 MC |
410 | /* |
411 | * Fatal errors in this block don't send an alert because we have | |
412 | * failed to even initialise properly. Sending an alert is probably | |
413 | * doomed to failure. | |
414 | */ | |
415 | ||
38b051a1 | 416 | if (SSL_CONNECTION_IS_DTLS(s)) { |
f8e0a557 | 417 | if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) && |
a230b26e | 418 | (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) { |
c48ffbcc | 419 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
420 | goto end; |
421 | } | |
422 | } else { | |
4fa52141 | 423 | if ((s->version >> 8) != SSL3_VERSION_MAJOR) { |
c48ffbcc | 424 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
425 | goto end; |
426 | } | |
427 | } | |
428 | ||
4fa52141 | 429 | if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) { |
c48ffbcc | 430 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
4fa52141 | 431 | goto end; |
f8e0a557 MC |
432 | } |
433 | ||
f8e0a557 MC |
434 | if (s->init_buf == NULL) { |
435 | if ((buf = BUF_MEM_new()) == NULL) { | |
c48ffbcc | 436 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
437 | goto end; |
438 | } | |
439 | if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { | |
c48ffbcc | 440 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
441 | goto end; |
442 | } | |
443 | s->init_buf = buf; | |
444 | buf = NULL; | |
445 | } | |
446 | ||
f8e0a557 MC |
447 | s->init_num = 0; |
448 | ||
449 | /* | |
450 | * Should have been reset by tls_process_finished, too. | |
451 | */ | |
555cbb32 | 452 | s->s3.change_cipher_spec = 0; |
f8e0a557 | 453 | |
46417569 MC |
454 | /* |
455 | * Ok, we now need to push on a buffering BIO ...but not with | |
456 | * SCTP | |
457 | */ | |
f8e0a557 | 458 | #ifndef OPENSSL_NO_SCTP |
38b051a1 | 459 | if (!SSL_CONNECTION_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(ssl))) |
f8e0a557 | 460 | #endif |
46417569 | 461 | if (!ssl_init_wbio_buffer(s)) { |
c48ffbcc | 462 | SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); |
46417569 MC |
463 | goto end; |
464 | } | |
f8e0a557 | 465 | |
38b051a1 | 466 | if ((SSL_in_before(ssl)) |
49e7fe12 | 467 | || s->renegotiate) { |
47e2ee07 MC |
468 | if (!tls_setup_handshake(s)) { |
469 | /* SSLfatal() already called */ | |
c7f47786 | 470 | goto end; |
47e2ee07 | 471 | } |
0386aad1 | 472 | |
c7f47786 MC |
473 | if (SSL_IS_FIRST_HANDSHAKE(s)) |
474 | st->read_state_first_init = 1; | |
f8e0a557 MC |
475 | } |
476 | ||
477 | st->state = MSG_FLOW_WRITING; | |
478 | init_write_state_machine(s); | |
f8e0a557 MC |
479 | } |
480 | ||
e8aa8b6c F |
481 | while (st->state != MSG_FLOW_FINISHED) { |
482 | if (st->state == MSG_FLOW_READING) { | |
f8e0a557 MC |
483 | ssret = read_state_machine(s); |
484 | if (ssret == SUB_STATE_FINISHED) { | |
485 | st->state = MSG_FLOW_WRITING; | |
486 | init_write_state_machine(s); | |
487 | } else { | |
488 | /* NBIO or error */ | |
489 | goto end; | |
490 | } | |
491 | } else if (st->state == MSG_FLOW_WRITING) { | |
492 | ssret = write_state_machine(s); | |
493 | if (ssret == SUB_STATE_FINISHED) { | |
494 | st->state = MSG_FLOW_READING; | |
495 | init_read_state_machine(s); | |
496 | } else if (ssret == SUB_STATE_END_HANDSHAKE) { | |
497 | st->state = MSG_FLOW_FINISHED; | |
498 | } else { | |
499 | /* NBIO or error */ | |
500 | goto end; | |
501 | } | |
502 | } else { | |
503 | /* Error */ | |
c48ffbcc | 504 | check_fatal(s); |
6849b73c | 505 | ERR_raise(ERR_LIB_SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
f8e0a557 MC |
506 | goto end; |
507 | } | |
508 | } | |
509 | ||
f8e0a557 MC |
510 | ret = 1; |
511 | ||
512 | end: | |
024f543c | 513 | st->in_handshake--; |
473483d4 MC |
514 | |
515 | #ifndef OPENSSL_NO_SCTP | |
38b051a1 | 516 | if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { |
473483d4 MC |
517 | /* |
518 | * Notify SCTP BIO socket to leave handshake mode and allow stream | |
99240875 | 519 | * identifier other than 0. |
473483d4 | 520 | */ |
38b051a1 | 521 | BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE, |
024f543c | 522 | st->in_handshake, NULL); |
473483d4 MC |
523 | } |
524 | #endif | |
525 | ||
f8e0a557 MC |
526 | BUF_MEM_free(buf); |
527 | if (cb != NULL) { | |
528 | if (server) | |
dc84829c | 529 | cb(ussl, SSL_CB_ACCEPT_EXIT, ret); |
f8e0a557 | 530 | else |
dc84829c | 531 | cb(ussl, SSL_CB_CONNECT_EXIT, ret); |
f8e0a557 MC |
532 | } |
533 | return ret; | |
534 | } | |
535 | ||
536 | /* | |
537 | * Initialise the MSG_FLOW_READING sub-state machine | |
538 | */ | |
38b051a1 | 539 | static void init_read_state_machine(SSL_CONNECTION *s) |
f8e0a557 | 540 | { |
d6f1a6e9 | 541 | OSSL_STATEM *st = &s->statem; |
f8e0a557 MC |
542 | |
543 | st->read_state = READ_STATE_HEADER; | |
544 | } | |
545 | ||
38b051a1 | 546 | static int grow_init_buf(SSL_CONNECTION *s, size_t size) { |
0d698f66 MC |
547 | |
548 | size_t msg_offset = (char *)s->init_msg - s->init_buf->data; | |
549 | ||
550 | if (!BUF_MEM_grow_clean(s->init_buf, (int)size)) | |
551 | return 0; | |
552 | ||
553 | if (size < msg_offset) | |
554 | return 0; | |
555 | ||
556 | s->init_msg = s->init_buf->data + msg_offset; | |
557 | ||
558 | return 1; | |
559 | } | |
560 | ||
f8e0a557 MC |
561 | /* |
562 | * This function implements the sub-state machine when the message flow is in | |
563 | * MSG_FLOW_READING. The valid sub-states and transitions are: | |
564 | * | |
565 | * READ_STATE_HEADER <--+<-------------+ | |
566 | * | | | | |
567 | * v | | | |
568 | * READ_STATE_BODY -----+-->READ_STATE_POST_PROCESS | |
569 | * | | | |
570 | * +----------------------------+ | |
571 | * v | |
572 | * [SUB_STATE_FINISHED] | |
573 | * | |
574 | * READ_STATE_HEADER has the responsibility for reading in the message header | |
575 | * and transitioning the state of the handshake state machine. | |
576 | * | |
577 | * READ_STATE_BODY reads in the rest of the message and then subsequently | |
578 | * processes it. | |
579 | * | |
580 | * READ_STATE_POST_PROCESS is an optional step that may occur if some post | |
581 | * processing activity performed on the message may block. | |
582 | * | |
0d4fb843 | 583 | * Any of the above states could result in an NBIO event occurring in which case |
f8e0a557 MC |
584 | * control returns to the calling application. When this function is recalled we |
585 | * will resume in the same state where we left off. | |
586 | */ | |
38b051a1 | 587 | static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s) |
a230b26e | 588 | { |
d6f1a6e9 | 589 | OSSL_STATEM *st = &s->statem; |
f8e0a557 | 590 | int ret, mt; |
eda75751 | 591 | size_t len = 0; |
38b051a1 | 592 | int (*transition) (SSL_CONNECTION *s, int mt); |
73999b62 | 593 | PACKET pkt; |
38b051a1 TM |
594 | MSG_PROCESS_RETURN(*process_message) (SSL_CONNECTION *s, PACKET *pkt); |
595 | WORK_STATE(*post_process_message) (SSL_CONNECTION *s, WORK_STATE wst); | |
596 | size_t (*max_message_size) (SSL_CONNECTION *s); | |
f8e0a557 | 597 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
dc84829c | 598 | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
f8e0a557 | 599 | |
91eac8d5 | 600 | cb = get_callback(s); |
f8e0a557 | 601 | |
e8aa8b6c | 602 | if (s->server) { |
8481f583 MC |
603 | transition = ossl_statem_server_read_transition; |
604 | process_message = ossl_statem_server_process_message; | |
605 | max_message_size = ossl_statem_server_max_message_size; | |
606 | post_process_message = ossl_statem_server_post_process_message; | |
f8e0a557 | 607 | } else { |
8481f583 MC |
608 | transition = ossl_statem_client_read_transition; |
609 | process_message = ossl_statem_client_process_message; | |
610 | max_message_size = ossl_statem_client_max_message_size; | |
611 | post_process_message = ossl_statem_client_post_process_message; | |
f8e0a557 MC |
612 | } |
613 | ||
614 | if (st->read_state_first_init) { | |
615 | s->first_packet = 1; | |
616 | st->read_state_first_init = 0; | |
617 | } | |
618 | ||
e8aa8b6c F |
619 | while (1) { |
620 | switch (st->read_state) { | |
f8e0a557 | 621 | case READ_STATE_HEADER: |
f8e0a557 | 622 | /* Get the state the peer wants to move to */ |
38b051a1 | 623 | if (SSL_CONNECTION_IS_DTLS(s)) { |
76af3037 MC |
624 | /* |
625 | * In DTLS we get the whole message in one go - header and body | |
626 | */ | |
f42e68dc | 627 | ret = dtls_get_message(s, &mt); |
76af3037 MC |
628 | } else { |
629 | ret = tls_get_message_header(s, &mt); | |
630 | } | |
f8e0a557 MC |
631 | |
632 | if (ret == 0) { | |
633 | /* Could be non-blocking IO */ | |
634 | return SUB_STATE_ERROR; | |
635 | } | |
636 | ||
637 | if (cb != NULL) { | |
638 | /* Notify callback of an impending state change */ | |
639 | if (s->server) | |
38b051a1 | 640 | cb(ssl, SSL_CB_ACCEPT_LOOP, 1); |
f8e0a557 | 641 | else |
38b051a1 | 642 | cb(ssl, SSL_CB_CONNECT_LOOP, 1); |
f8e0a557 MC |
643 | } |
644 | /* | |
645 | * Validate that we are allowed to move to the new state and move | |
646 | * to that state if so | |
647 | */ | |
f20404fc | 648 | if (!transition(s, mt)) |
f8e0a557 | 649 | return SUB_STATE_ERROR; |
f8e0a557 | 650 | |
555cbb32 | 651 | if (s->s3.tmp.message_size > max_message_size(s)) { |
c48ffbcc | 652 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
f63a17d6 | 653 | SSL_R_EXCESSIVE_MESSAGE_SIZE); |
f8e0a557 MC |
654 | return SUB_STATE_ERROR; |
655 | } | |
656 | ||
c1ef7c97 | 657 | /* dtls_get_message already did this */ |
38b051a1 | 658 | if (!SSL_CONNECTION_IS_DTLS(s) |
555cbb32 TS |
659 | && s->s3.tmp.message_size > 0 |
660 | && !grow_init_buf(s, s->s3.tmp.message_size | |
0d698f66 | 661 | + SSL3_HM_HEADER_LENGTH)) { |
c48ffbcc | 662 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BUF_LIB); |
c1ef7c97 MC |
663 | return SUB_STATE_ERROR; |
664 | } | |
665 | ||
f8e0a557 MC |
666 | st->read_state = READ_STATE_BODY; |
667 | /* Fall through */ | |
668 | ||
669 | case READ_STATE_BODY: | |
38b051a1 | 670 | if (SSL_CONNECTION_IS_DTLS(s)) { |
f42e68dc MC |
671 | /* |
672 | * Actually we already have the body, but we give DTLS the | |
673 | * opportunity to do any further processing. | |
674 | */ | |
675 | ret = dtls_get_message_body(s, &len); | |
676 | } else { | |
f8e0a557 | 677 | ret = tls_get_message_body(s, &len); |
f42e68dc MC |
678 | } |
679 | if (ret == 0) { | |
680 | /* Could be non-blocking IO */ | |
681 | return SUB_STATE_ERROR; | |
f8e0a557 MC |
682 | } |
683 | ||
684 | s->first_packet = 0; | |
73999b62 | 685 | if (!PACKET_buf_init(&pkt, s->init_msg, len)) { |
c48ffbcc | 686 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
73999b62 MC |
687 | return SUB_STATE_ERROR; |
688 | } | |
689 | ret = process_message(s, &pkt); | |
1689e7e6 MC |
690 | |
691 | /* Discard the packet data */ | |
692 | s->init_num = 0; | |
693 | ||
4f8a5f4d AG |
694 | switch (ret) { |
695 | case MSG_PROCESS_ERROR: | |
c48ffbcc | 696 | check_fatal(s); |
f8e0a557 | 697 | return SUB_STATE_ERROR; |
f8e0a557 | 698 | |
4f8a5f4d | 699 | case MSG_PROCESS_FINISHED_READING: |
38b051a1 | 700 | if (SSL_CONNECTION_IS_DTLS(s)) { |
f8e0a557 MC |
701 | dtls1_stop_timer(s); |
702 | } | |
703 | return SUB_STATE_FINISHED; | |
f8e0a557 | 704 | |
4f8a5f4d | 705 | case MSG_PROCESS_CONTINUE_PROCESSING: |
f8e0a557 MC |
706 | st->read_state = READ_STATE_POST_PROCESS; |
707 | st->read_state_work = WORK_MORE_A; | |
4f8a5f4d AG |
708 | break; |
709 | ||
710 | default: | |
f8e0a557 | 711 | st->read_state = READ_STATE_HEADER; |
4f8a5f4d | 712 | break; |
f8e0a557 MC |
713 | } |
714 | break; | |
715 | ||
716 | case READ_STATE_POST_PROCESS: | |
717 | st->read_state_work = post_process_message(s, st->read_state_work); | |
e8aa8b6c | 718 | switch (st->read_state_work) { |
f3b3d7f0 | 719 | case WORK_ERROR: |
c48ffbcc | 720 | check_fatal(s); |
47e2ee07 | 721 | /* Fall through */ |
f3b3d7f0 RS |
722 | case WORK_MORE_A: |
723 | case WORK_MORE_B: | |
ddf97258 | 724 | case WORK_MORE_C: |
f8e0a557 MC |
725 | return SUB_STATE_ERROR; |
726 | ||
727 | case WORK_FINISHED_CONTINUE: | |
728 | st->read_state = READ_STATE_HEADER; | |
729 | break; | |
730 | ||
95051052 | 731 | case WORK_FINISHED_SWAP: |
f8e0a557 | 732 | case WORK_FINISHED_STOP: |
38b051a1 | 733 | if (SSL_CONNECTION_IS_DTLS(s)) { |
f8e0a557 MC |
734 | dtls1_stop_timer(s); |
735 | } | |
736 | return SUB_STATE_FINISHED; | |
737 | } | |
738 | break; | |
739 | ||
740 | default: | |
741 | /* Shouldn't happen */ | |
c48ffbcc | 742 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
743 | return SUB_STATE_ERROR; |
744 | } | |
745 | } | |
746 | } | |
747 | ||
748 | /* | |
749 | * Send a previously constructed message to the peer. | |
750 | */ | |
38b051a1 | 751 | static int statem_do_write(SSL_CONNECTION *s) |
f8e0a557 | 752 | { |
d6f1a6e9 | 753 | OSSL_STATEM *st = &s->statem; |
f8e0a557 MC |
754 | |
755 | if (st->hand_state == TLS_ST_CW_CHANGE | |
a230b26e | 756 | || st->hand_state == TLS_ST_SW_CHANGE) { |
38b051a1 | 757 | if (SSL_CONNECTION_IS_DTLS(s)) |
f8e0a557 MC |
758 | return dtls1_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); |
759 | else | |
760 | return ssl3_do_write(s, SSL3_RT_CHANGE_CIPHER_SPEC); | |
761 | } else { | |
762 | return ssl_do_write(s); | |
763 | } | |
764 | } | |
765 | ||
766 | /* | |
767 | * Initialise the MSG_FLOW_WRITING sub-state machine | |
768 | */ | |
38b051a1 | 769 | static void init_write_state_machine(SSL_CONNECTION *s) |
f8e0a557 | 770 | { |
d6f1a6e9 | 771 | OSSL_STATEM *st = &s->statem; |
f8e0a557 MC |
772 | |
773 | st->write_state = WRITE_STATE_TRANSITION; | |
774 | } | |
775 | ||
776 | /* | |
777 | * This function implements the sub-state machine when the message flow is in | |
778 | * MSG_FLOW_WRITING. The valid sub-states and transitions are: | |
779 | * | |
780 | * +-> WRITE_STATE_TRANSITION ------> [SUB_STATE_FINISHED] | |
781 | * | | | |
782 | * | v | |
783 | * | WRITE_STATE_PRE_WORK -----> [SUB_STATE_END_HANDSHAKE] | |
784 | * | | | |
785 | * | v | |
786 | * | WRITE_STATE_SEND | |
787 | * | | | |
788 | * | v | |
789 | * | WRITE_STATE_POST_WORK | |
790 | * | | | |
791 | * +-------------+ | |
792 | * | |
793 | * WRITE_STATE_TRANSITION transitions the state of the handshake state machine | |
794 | ||
795 | * WRITE_STATE_PRE_WORK performs any work necessary to prepare the later | |
0d4fb843 | 796 | * sending of the message. This could result in an NBIO event occurring in |
f8e0a557 MC |
797 | * which case control returns to the calling application. When this function |
798 | * is recalled we will resume in the same state where we left off. | |
799 | * | |
800 | * WRITE_STATE_SEND sends the message and performs any work to be done after | |
801 | * sending. | |
802 | * | |
803 | * WRITE_STATE_POST_WORK performs any work necessary after the sending of the | |
804 | * message has been completed. As for WRITE_STATE_PRE_WORK this could also | |
805 | * result in an NBIO event. | |
806 | */ | |
38b051a1 | 807 | static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s) |
f8e0a557 | 808 | { |
d6f1a6e9 | 809 | OSSL_STATEM *st = &s->statem; |
f8e0a557 | 810 | int ret; |
38b051a1 TM |
811 | WRITE_TRAN(*transition) (SSL_CONNECTION *s); |
812 | WORK_STATE(*pre_work) (SSL_CONNECTION *s, WORK_STATE wst); | |
813 | WORK_STATE(*post_work) (SSL_CONNECTION *s, WORK_STATE wst); | |
814 | int (*get_construct_message_f) (SSL_CONNECTION *s, | |
67ec6d2b MC |
815 | CON_FUNC_RETURN (**confunc) (SSL_CONNECTION *s, |
816 | WPACKET *pkt), | |
6392fb8e | 817 | int *mt); |
f8e0a557 | 818 | void (*cb) (const SSL *ssl, int type, int val) = NULL; |
67ec6d2b | 819 | CON_FUNC_RETURN (*confunc) (SSL_CONNECTION *s, WPACKET *pkt); |
6392fb8e | 820 | int mt; |
7cea05dc | 821 | WPACKET pkt; |
dc84829c | 822 | SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); |
f8e0a557 | 823 | |
91eac8d5 | 824 | cb = get_callback(s); |
f8e0a557 | 825 | |
e8aa8b6c | 826 | if (s->server) { |
8481f583 MC |
827 | transition = ossl_statem_server_write_transition; |
828 | pre_work = ossl_statem_server_pre_work; | |
829 | post_work = ossl_statem_server_post_work; | |
6392fb8e | 830 | get_construct_message_f = ossl_statem_server_construct_message; |
f8e0a557 | 831 | } else { |
8481f583 MC |
832 | transition = ossl_statem_client_write_transition; |
833 | pre_work = ossl_statem_client_pre_work; | |
834 | post_work = ossl_statem_client_post_work; | |
6392fb8e | 835 | get_construct_message_f = ossl_statem_client_construct_message; |
f8e0a557 MC |
836 | } |
837 | ||
e8aa8b6c F |
838 | while (1) { |
839 | switch (st->write_state) { | |
f8e0a557 MC |
840 | case WRITE_STATE_TRANSITION: |
841 | if (cb != NULL) { | |
842 | /* Notify callback of an impending state change */ | |
843 | if (s->server) | |
38b051a1 | 844 | cb(ssl, SSL_CB_ACCEPT_LOOP, 1); |
f8e0a557 | 845 | else |
38b051a1 | 846 | cb(ssl, SSL_CB_CONNECT_LOOP, 1); |
f8e0a557 | 847 | } |
e8aa8b6c | 848 | switch (transition(s)) { |
f8e0a557 MC |
849 | case WRITE_TRAN_CONTINUE: |
850 | st->write_state = WRITE_STATE_PRE_WORK; | |
851 | st->write_state_work = WORK_MORE_A; | |
852 | break; | |
853 | ||
854 | case WRITE_TRAN_FINISHED: | |
855 | return SUB_STATE_FINISHED; | |
f8e0a557 | 856 | |
f3b3d7f0 | 857 | case WRITE_TRAN_ERROR: |
c48ffbcc | 858 | check_fatal(s); |
f8e0a557 MC |
859 | return SUB_STATE_ERROR; |
860 | } | |
861 | break; | |
862 | ||
863 | case WRITE_STATE_PRE_WORK: | |
e8aa8b6c | 864 | switch (st->write_state_work = pre_work(s, st->write_state_work)) { |
f3b3d7f0 | 865 | case WORK_ERROR: |
c48ffbcc | 866 | check_fatal(s); |
47e2ee07 | 867 | /* Fall through */ |
f3b3d7f0 RS |
868 | case WORK_MORE_A: |
869 | case WORK_MORE_B: | |
ddf97258 | 870 | case WORK_MORE_C: |
f8e0a557 MC |
871 | return SUB_STATE_ERROR; |
872 | ||
873 | case WORK_FINISHED_CONTINUE: | |
874 | st->write_state = WRITE_STATE_SEND; | |
875 | break; | |
876 | ||
95051052 MC |
877 | case WORK_FINISHED_SWAP: |
878 | return SUB_STATE_FINISHED; | |
879 | ||
f8e0a557 MC |
880 | case WORK_FINISHED_STOP: |
881 | return SUB_STATE_END_HANDSHAKE; | |
882 | } | |
e1c12271 | 883 | if (!get_construct_message_f(s, &confunc, &mt)) { |
f63a17d6 | 884 | /* SSLfatal() already called */ |
f7e393be MC |
885 | return SUB_STATE_ERROR; |
886 | } | |
887 | if (mt == SSL3_MT_DUMMY) { | |
888 | /* Skip construction and sending. This isn't a "real" state */ | |
889 | st->write_state = WRITE_STATE_POST_WORK; | |
890 | st->write_state_work = WORK_MORE_A; | |
891 | break; | |
892 | } | |
7cea05dc | 893 | if (!WPACKET_init(&pkt, s->init_buf) |
f63a17d6 MC |
894 | || !ssl_set_handshake_header(s, &pkt, mt)) { |
895 | WPACKET_cleanup(&pkt); | |
c48ffbcc | 896 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f63a17d6 MC |
897 | return SUB_STATE_ERROR; |
898 | } | |
3e93c5fe | 899 | if (confunc != NULL) { |
67ec6d2b | 900 | CON_FUNC_RETURN tmpret; |
3e93c5fe MC |
901 | |
902 | tmpret = confunc(s, &pkt); | |
67ec6d2b | 903 | if (tmpret == CON_FUNC_ERROR) { |
3e93c5fe MC |
904 | WPACKET_cleanup(&pkt); |
905 | check_fatal(s); | |
906 | return SUB_STATE_ERROR; | |
67ec6d2b | 907 | } else if (tmpret == CON_FUNC_DONT_SEND) { |
3e93c5fe MC |
908 | /* |
909 | * The construction function decided not to construct the | |
910 | * message after all and continue. Skip sending. | |
911 | */ | |
912 | WPACKET_cleanup(&pkt); | |
913 | st->write_state = WRITE_STATE_POST_WORK; | |
914 | st->write_state_work = WORK_MORE_A; | |
915 | break; | |
916 | } /* else success */ | |
f63a17d6 MC |
917 | } |
918 | if (!ssl_close_construct_packet(s, &pkt, mt) | |
7cea05dc MC |
919 | || !WPACKET_finish(&pkt)) { |
920 | WPACKET_cleanup(&pkt); | |
c48ffbcc | 921 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f8e0a557 | 922 | return SUB_STATE_ERROR; |
7cea05dc | 923 | } |
f8e0a557 MC |
924 | |
925 | /* Fall through */ | |
926 | ||
927 | case WRITE_STATE_SEND: | |
38b051a1 | 928 | if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) { |
f8e0a557 MC |
929 | dtls1_start_timer(s); |
930 | } | |
931 | ret = statem_do_write(s); | |
932 | if (ret <= 0) { | |
933 | return SUB_STATE_ERROR; | |
934 | } | |
935 | st->write_state = WRITE_STATE_POST_WORK; | |
936 | st->write_state_work = WORK_MORE_A; | |
937 | /* Fall through */ | |
938 | ||
939 | case WRITE_STATE_POST_WORK: | |
e8aa8b6c | 940 | switch (st->write_state_work = post_work(s, st->write_state_work)) { |
f3b3d7f0 | 941 | case WORK_ERROR: |
c48ffbcc | 942 | check_fatal(s); |
47e2ee07 | 943 | /* Fall through */ |
f3b3d7f0 RS |
944 | case WORK_MORE_A: |
945 | case WORK_MORE_B: | |
ddf97258 | 946 | case WORK_MORE_C: |
f8e0a557 MC |
947 | return SUB_STATE_ERROR; |
948 | ||
949 | case WORK_FINISHED_CONTINUE: | |
950 | st->write_state = WRITE_STATE_TRANSITION; | |
951 | break; | |
952 | ||
95051052 MC |
953 | case WORK_FINISHED_SWAP: |
954 | return SUB_STATE_FINISHED; | |
955 | ||
f8e0a557 MC |
956 | case WORK_FINISHED_STOP: |
957 | return SUB_STATE_END_HANDSHAKE; | |
958 | } | |
959 | break; | |
960 | ||
961 | default: | |
c48ffbcc | 962 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
f8e0a557 MC |
963 | return SUB_STATE_ERROR; |
964 | } | |
965 | } | |
966 | } | |
967 | ||
8723588e MC |
968 | /* |
969 | * Flush the write BIO | |
970 | */ | |
38b051a1 | 971 | int statem_flush(SSL_CONNECTION *s) |
8723588e MC |
972 | { |
973 | s->rwstate = SSL_WRITING; | |
974 | if (BIO_flush(s->wbio) <= 0) { | |
975 | return 0; | |
976 | } | |
977 | s->rwstate = SSL_NOTHING; | |
978 | ||
979 | return 1; | |
980 | } | |
981 | ||
f8e0a557 MC |
982 | /* |
983 | * Called by the record layer to determine whether application data is | |
c7f47786 | 984 | * allowed to be received in the current handshake state or not. |
f8e0a557 MC |
985 | * |
986 | * Return values are: | |
987 | * 1: Yes (application data allowed) | |
988 | * 0: No (application data not allowed) | |
989 | */ | |
38b051a1 | 990 | int ossl_statem_app_data_allowed(SSL_CONNECTION *s) |
f8e0a557 | 991 | { |
d6f1a6e9 | 992 | OSSL_STATEM *st = &s->statem; |
f8e0a557 | 993 | |
c7f47786 | 994 | if (st->state == MSG_FLOW_UNINITED) |
8723588e MC |
995 | return 0; |
996 | ||
555cbb32 | 997 | if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0)) |
94836de2 | 998 | return 0; |
8723588e | 999 | |
94836de2 MC |
1000 | if (s->server) { |
1001 | /* | |
1002 | * If we're a server and we haven't got as far as writing our | |
1003 | * ServerHello yet then we allow app data | |
1004 | */ | |
1005 | if (st->hand_state == TLS_ST_BEFORE | |
a230b26e | 1006 | || st->hand_state == TLS_ST_SR_CLNT_HELLO) |
94836de2 MC |
1007 | return 1; |
1008 | } else { | |
1009 | /* | |
1010 | * If we're a client and we haven't read the ServerHello yet then we | |
1011 | * allow app data | |
1012 | */ | |
1013 | if (st->hand_state == TLS_ST_CW_CLNT_HELLO) | |
8723588e | 1014 | return 1; |
8723588e MC |
1015 | } |
1016 | ||
8723588e MC |
1017 | return 0; |
1018 | } | |
1f5878b8 TT |
1019 | |
1020 | /* | |
1021 | * This function returns 1 if TLS exporter is ready to export keying | |
1022 | * material, or 0 if otherwise. | |
1023 | */ | |
38b051a1 | 1024 | int ossl_statem_export_allowed(SSL_CONNECTION *s) |
1f5878b8 | 1025 | { |
555cbb32 | 1026 | return s->s3.previous_server_finished_len != 0 |
1f5878b8 TT |
1027 | && s->statem.hand_state != TLS_ST_SW_FINISHED; |
1028 | } | |
b38ede80 TT |
1029 | |
1030 | /* | |
1031 | * Return 1 if early TLS exporter is ready to export keying material, | |
1032 | * or 0 if otherwise. | |
1033 | */ | |
38b051a1 | 1034 | int ossl_statem_export_early_allowed(SSL_CONNECTION *s) |
b38ede80 TT |
1035 | { |
1036 | /* | |
1037 | * The early exporter secret is only present on the server if we | |
1038 | * have accepted early_data. It is present on the client as long | |
1039 | * as we have sent early_data. | |
1040 | */ | |
1041 | return s->ext.early_data == SSL_EARLY_DATA_ACCEPTED | |
1042 | || (!s->server && s->ext.early_data != SSL_EARLY_DATA_NOT_SENT); | |
1043 | } |