2
0

statem.c 27 KB

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