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