statem.c 30 KB

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