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