2
0

statem.c 31 KB

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