statem.c 31 KB

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