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