statem.c 27 KB

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