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