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