statem.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 "internal/ssl_unwrap.h"
  15. #include <openssl/rand.h>
  16. #include "../ssl_local.h"
  17. #include "statem_local.h"
  18. #include <assert.h>
  19. /*
  20. * This file implements the SSL/TLS/DTLS state machines.
  21. *
  22. * There are two primary state machines:
  23. *
  24. * 1) Message flow state machine
  25. * 2) Handshake state machine
  26. *
  27. * The Message flow state machine controls the reading and sending of messages
  28. * including handling of non-blocking IO events, flushing of the underlying
  29. * write BIO, handling unexpected messages, etc. It is itself broken into two
  30. * separate sub-state machines which control reading and writing respectively.
  31. *
  32. * The Handshake state machine keeps track of the current SSL/TLS handshake
  33. * state. Transitions of the handshake state are the result of events that
  34. * occur within the Message flow state machine.
  35. *
  36. * Overall it looks like this:
  37. *
  38. * --------------------------------------------- -------------------
  39. * | | | |
  40. * | Message flow state machine | | |
  41. * | | | |
  42. * | -------------------- -------------------- | Transition | Handshake state |
  43. * | | MSG_FLOW_READING | | MSG_FLOW_WRITING | | Event | machine |
  44. * | | sub-state | | sub-state | |----------->| |
  45. * | | machine for | | machine for | | | |
  46. * | | reading messages | | writing messages | | | |
  47. * | -------------------- -------------------- | | |
  48. * | | | |
  49. * --------------------------------------------- -------------------
  50. *
  51. */
  52. /* Sub state machine return values */
  53. typedef enum {
  54. /* Something bad happened or NBIO */
  55. SUB_STATE_ERROR,
  56. /* Sub state finished go to the next sub state */
  57. SUB_STATE_FINISHED,
  58. /* Sub state finished and handshake was completed */
  59. SUB_STATE_END_HANDSHAKE
  60. } SUB_STATE_RETURN;
  61. static int state_machine(SSL_CONNECTION *s, int server);
  62. static void init_read_state_machine(SSL_CONNECTION *s);
  63. static SUB_STATE_RETURN read_state_machine(SSL_CONNECTION *s);
  64. static void init_write_state_machine(SSL_CONNECTION *s);
  65. static SUB_STATE_RETURN write_state_machine(SSL_CONNECTION *s);
  66. OSSL_HANDSHAKE_STATE SSL_get_state(const SSL *ssl)
  67. {
  68. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(ssl);
  69. if (sc == NULL)
  70. return TLS_ST_BEFORE;
  71. return sc->statem.hand_state;
  72. }
  73. int SSL_in_init(const SSL *s)
  74. {
  75. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  76. if (sc == NULL)
  77. return 0;
  78. return sc->statem.in_init;
  79. }
  80. int SSL_is_init_finished(const SSL *s)
  81. {
  82. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  83. if (sc == NULL)
  84. return 0;
  85. return !(sc->statem.in_init) && (sc->statem.hand_state == TLS_ST_OK);
  86. }
  87. int SSL_in_before(const SSL *s)
  88. {
  89. const SSL_CONNECTION *sc = SSL_CONNECTION_FROM_CONST_SSL(s);
  90. if (sc == NULL)
  91. return 0;
  92. /*
  93. * Historically being "in before" meant before anything had happened. In the
  94. * current code though we remain in the "before" state for a while after we
  95. * have started the handshake process (e.g. as a server waiting for the
  96. * first message to arrive). There "in before" is taken to mean "in before"
  97. * and not started any handshake process yet.
  98. */
  99. return (sc->statem.hand_state == TLS_ST_BEFORE)
  100. && (sc->statem.state == MSG_FLOW_UNINITED);
  101. }
  102. OSSL_HANDSHAKE_STATE ossl_statem_get_state(SSL_CONNECTION *s)
  103. {
  104. return s != NULL ? s->statem.hand_state : TLS_ST_BEFORE;
  105. }
  106. /*
  107. * Clear the state machine state and reset back to MSG_FLOW_UNINITED
  108. */
  109. void ossl_statem_clear(SSL_CONNECTION *s)
  110. {
  111. s->statem.state = MSG_FLOW_UNINITED;
  112. s->statem.hand_state = TLS_ST_BEFORE;
  113. ossl_statem_set_in_init(s, 1);
  114. s->statem.no_cert_verify = 0;
  115. }
  116. /*
  117. * Set the state machine up ready for a renegotiation handshake
  118. */
  119. void ossl_statem_set_renegotiate(SSL_CONNECTION *s)
  120. {
  121. ossl_statem_set_in_init(s, 1);
  122. s->statem.request_state = TLS_ST_SW_HELLO_REQ;
  123. }
  124. void ossl_statem_send_fatal(SSL_CONNECTION *s, int al)
  125. {
  126. /* We shouldn't call SSLfatal() twice. Once is enough */
  127. if (s->statem.in_init && s->statem.state == MSG_FLOW_ERROR)
  128. return;
  129. ossl_statem_set_in_init(s, 1);
  130. s->statem.state = MSG_FLOW_ERROR;
  131. if (al != SSL_AD_NO_ALERT)
  132. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  133. }
  134. /*
  135. * Error reporting building block that's used instead of ERR_set_error().
  136. * In addition to what ERR_set_error() does, this puts the state machine
  137. * into an error state and sends an alert if appropriate.
  138. * This is a permanent error for the current connection.
  139. */
  140. void ossl_statem_fatal(SSL_CONNECTION *s, int al, int reason,
  141. const char *fmt, ...)
  142. {
  143. va_list args;
  144. va_start(args, fmt);
  145. ERR_vset_error(ERR_LIB_SSL, reason, fmt, args);
  146. va_end(args);
  147. ossl_statem_send_fatal(s, al);
  148. }
  149. /*
  150. * This macro should only be called if we are already expecting to be in
  151. * a fatal error state. We verify that we are, and set it if not (this would
  152. * indicate a bug).
  153. */
  154. #define check_fatal(s) \
  155. do { \
  156. if (!ossl_assert((s)->statem.in_init \
  157. && (s)->statem.state == MSG_FLOW_ERROR)) \
  158. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_FATAL); \
  159. } while (0)
  160. /*
  161. * Discover whether the current connection is in the error state.
  162. *
  163. * Valid return values are:
  164. * 1: Yes
  165. * 0: No
  166. */
  167. int ossl_statem_in_error(const SSL_CONNECTION *s)
  168. {
  169. if (s->statem.state == MSG_FLOW_ERROR)
  170. return 1;
  171. return 0;
  172. }
  173. void ossl_statem_set_in_init(SSL_CONNECTION *s, int init)
  174. {
  175. s->statem.in_init = init;
  176. if (s->rlayer.rrlmethod != NULL && s->rlayer.rrlmethod->set_in_init != NULL)
  177. s->rlayer.rrlmethod->set_in_init(s->rlayer.rrl, init);
  178. }
  179. int ossl_statem_get_in_handshake(SSL_CONNECTION *s)
  180. {
  181. return s->statem.in_handshake;
  182. }
  183. void ossl_statem_set_in_handshake(SSL_CONNECTION *s, int inhand)
  184. {
  185. if (inhand)
  186. s->statem.in_handshake++;
  187. else
  188. s->statem.in_handshake--;
  189. }
  190. /* Are we in a sensible state to skip over unreadable early data? */
  191. int ossl_statem_skip_early_data(SSL_CONNECTION *s)
  192. {
  193. if (s->ext.early_data != SSL_EARLY_DATA_REJECTED)
  194. return 0;
  195. if (!s->server
  196. || s->statem.hand_state != TLS_ST_EARLY_DATA
  197. || s->hello_retry_request == SSL_HRR_COMPLETE)
  198. return 0;
  199. return 1;
  200. }
  201. /*
  202. * Called when we are in SSL_read*(), SSL_write*(), or SSL_accept()
  203. * /SSL_connect()/SSL_do_handshake(). Used to test whether we are in an early
  204. * data state and whether we should attempt to move the handshake on if so.
  205. * |sending| is 1 if we are attempting to send data (SSL_write*()), 0 if we are
  206. * attempting to read data (SSL_read*()), or -1 if we are in SSL_do_handshake()
  207. * or similar.
  208. */
  209. void ossl_statem_check_finish_init(SSL_CONNECTION *s, int sending)
  210. {
  211. if (sending == -1) {
  212. if (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
  213. || s->statem.hand_state == TLS_ST_EARLY_DATA) {
  214. ossl_statem_set_in_init(s, 1);
  215. if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY) {
  216. /*
  217. * SSL_connect() or SSL_do_handshake() has been called directly.
  218. * We don't allow any more writing of early data.
  219. */
  220. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  221. }
  222. }
  223. } else if (!s->server) {
  224. if ((sending && (s->statem.hand_state == TLS_ST_PENDING_EARLY_DATA_END
  225. || s->statem.hand_state == TLS_ST_EARLY_DATA)
  226. && s->early_data_state != SSL_EARLY_DATA_WRITING)
  227. || (!sending && s->statem.hand_state == TLS_ST_EARLY_DATA)) {
  228. ossl_statem_set_in_init(s, 1);
  229. /*
  230. * SSL_write() has been called directly. We don't allow any more
  231. * writing of early data.
  232. */
  233. if (sending && s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY)
  234. s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING;
  235. }
  236. } else {
  237. if (s->early_data_state == SSL_EARLY_DATA_FINISHED_READING
  238. && s->statem.hand_state == TLS_ST_EARLY_DATA)
  239. ossl_statem_set_in_init(s, 1);
  240. }
  241. }
  242. void ossl_statem_set_hello_verify_done(SSL_CONNECTION *s)
  243. {
  244. s->statem.state = MSG_FLOW_UNINITED;
  245. ossl_statem_set_in_init(s, 1);
  246. /*
  247. * This will get reset (briefly) back to TLS_ST_BEFORE when we enter
  248. * state_machine() because |state| is MSG_FLOW_UNINITED, but until then any
  249. * calls to SSL_in_before() will return false. Also calls to
  250. * SSL_state_string() and SSL_state_string_long() will return something
  251. * sensible.
  252. */
  253. s->statem.hand_state = TLS_ST_SR_CLNT_HELLO;
  254. }
  255. int ossl_statem_connect(SSL *s)
  256. {
  257. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  258. if (sc == NULL)
  259. return -1;
  260. return state_machine(sc, 0);
  261. }
  262. int ossl_statem_accept(SSL *s)
  263. {
  264. SSL_CONNECTION *sc = SSL_CONNECTION_FROM_SSL(s);
  265. if (sc == NULL)
  266. return -1;
  267. return state_machine(sc, 1);
  268. }
  269. typedef void (*info_cb) (const SSL *, int, int);
  270. static info_cb get_callback(SSL_CONNECTION *s)
  271. {
  272. SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
  273. if (s->info_callback != NULL)
  274. return s->info_callback;
  275. else if (sctx->info_callback != NULL)
  276. return sctx->info_callback;
  277. return NULL;
  278. }
  279. /*
  280. * The main message flow state machine. We start in the MSG_FLOW_UNINITED or
  281. * MSG_FLOW_FINISHED state and finish in MSG_FLOW_FINISHED. Valid states and
  282. * transitions are as follows:
  283. *
  284. * MSG_FLOW_UNINITED MSG_FLOW_FINISHED
  285. * | |
  286. * +-----------------------+
  287. * v
  288. * MSG_FLOW_WRITING <---> MSG_FLOW_READING
  289. * |
  290. * V
  291. * MSG_FLOW_FINISHED
  292. * |
  293. * V
  294. * [SUCCESS]
  295. *
  296. * We may exit at any point due to an error or NBIO event. If an NBIO event
  297. * occurs then we restart at the point we left off when we are recalled.
  298. * MSG_FLOW_WRITING and MSG_FLOW_READING have sub-state machines associated with them.
  299. *
  300. * In addition to the above there is also the MSG_FLOW_ERROR state. We can move
  301. * into that state at any point in the event that an irrecoverable error occurs.
  302. *
  303. * Valid return values are:
  304. * 1: Success
  305. * <=0: NBIO or error
  306. */
  307. static int state_machine(SSL_CONNECTION *s, int server)
  308. {
  309. BUF_MEM *buf = NULL;
  310. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  311. OSSL_STATEM *st = &s->statem;
  312. int ret = -1;
  313. int ssret;
  314. SSL *ssl = SSL_CONNECTION_GET_SSL(s);
  315. if (st->state == MSG_FLOW_ERROR) {
  316. /* Shouldn't have been called if we're already in the error state */
  317. return -1;
  318. }
  319. ERR_clear_error();
  320. clear_sys_error();
  321. cb = get_callback(s);
  322. st->in_handshake++;
  323. if (!SSL_in_init(ssl) || SSL_in_before(ssl)) {
  324. /*
  325. * If we are stateless then we already called SSL_clear() - don't do
  326. * it again and clear the STATELESS flag itself.
  327. */
  328. if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0 && !SSL_clear(ssl))
  329. return -1;
  330. }
  331. #ifndef OPENSSL_NO_SCTP
  332. if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) {
  333. /*
  334. * Notify SCTP BIO socket to enter handshake mode and prevent stream
  335. * identifier other than 0.
  336. */
  337. BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_SET_IN_HANDSHAKE,
  338. st->in_handshake, NULL);
  339. }
  340. #endif
  341. /* Initialise state machine */
  342. if (st->state == MSG_FLOW_UNINITED
  343. || st->state == MSG_FLOW_FINISHED) {
  344. if (st->state == MSG_FLOW_UNINITED) {
  345. st->hand_state = TLS_ST_BEFORE;
  346. st->request_state = TLS_ST_BEFORE;
  347. }
  348. s->server = server;
  349. if (cb != NULL) {
  350. if (SSL_IS_FIRST_HANDSHAKE(s) || !SSL_CONNECTION_IS_TLS13(s))
  351. cb(ssl, SSL_CB_HANDSHAKE_START, 1);
  352. }
  353. /*
  354. * Fatal errors in this block don't send an alert because we have
  355. * failed to even initialise properly. Sending an alert is probably
  356. * doomed to failure.
  357. */
  358. if (SSL_CONNECTION_IS_DTLS(s)) {
  359. if ((s->version & 0xff00) != (DTLS1_VERSION & 0xff00) &&
  360. (server || (s->version & 0xff00) != (DTLS1_BAD_VER & 0xff00))) {
  361. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  362. goto end;
  363. }
  364. } else {
  365. if ((s->version >> 8) != SSL3_VERSION_MAJOR) {
  366. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  367. goto end;
  368. }
  369. }
  370. if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
  371. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  372. goto end;
  373. }
  374. if (s->init_buf == NULL) {
  375. if ((buf = BUF_MEM_new()) == NULL) {
  376. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  377. goto end;
  378. }
  379. if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
  380. SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR);
  381. goto end;
  382. }
  383. s->init_buf = buf;
  384. buf = NULL;
  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. CON_FUNC_RETURN (**confunc) (SSL_CONNECTION *s,
  709. WPACKET *pkt),
  710. int *mt);
  711. void (*cb) (const SSL *ssl, int type, int val) = NULL;
  712. CON_FUNC_RETURN (*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) {
  783. CON_FUNC_RETURN tmpret;
  784. tmpret = confunc(s, &pkt);
  785. if (tmpret == CON_FUNC_ERROR) {
  786. WPACKET_cleanup(&pkt);
  787. check_fatal(s);
  788. return SUB_STATE_ERROR;
  789. } else if (tmpret == CON_FUNC_DONT_SEND) {
  790. /*
  791. * The construction function decided not to construct the
  792. * message after all and continue. Skip sending.
  793. */
  794. WPACKET_cleanup(&pkt);
  795. st->write_state = WRITE_STATE_POST_WORK;
  796. st->write_state_work = WORK_MORE_A;
  797. break;
  798. } /* else success */
  799. }
  800. if (!ssl_close_construct_packet(s, &pkt, mt)
  801. || !WPACKET_finish(&pkt)) {
  802. WPACKET_cleanup(&pkt);
  803. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  804. return SUB_STATE_ERROR;
  805. }
  806. /* Fall through */
  807. case WRITE_STATE_SEND:
  808. if (SSL_CONNECTION_IS_DTLS(s) && st->use_timer) {
  809. dtls1_start_timer(s);
  810. }
  811. ret = statem_do_write(s);
  812. if (ret <= 0) {
  813. return SUB_STATE_ERROR;
  814. }
  815. st->write_state = WRITE_STATE_POST_WORK;
  816. st->write_state_work = WORK_MORE_A;
  817. /* Fall through */
  818. case WRITE_STATE_POST_WORK:
  819. switch (st->write_state_work = post_work(s, st->write_state_work)) {
  820. case WORK_ERROR:
  821. check_fatal(s);
  822. /* Fall through */
  823. case WORK_MORE_A:
  824. case WORK_MORE_B:
  825. case WORK_MORE_C:
  826. return SUB_STATE_ERROR;
  827. case WORK_FINISHED_CONTINUE:
  828. st->write_state = WRITE_STATE_TRANSITION;
  829. break;
  830. case WORK_FINISHED_STOP:
  831. return SUB_STATE_END_HANDSHAKE;
  832. }
  833. break;
  834. default:
  835. SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  836. return SUB_STATE_ERROR;
  837. }
  838. }
  839. }
  840. /*
  841. * Flush the write BIO
  842. */
  843. int statem_flush(SSL_CONNECTION *s)
  844. {
  845. s->rwstate = SSL_WRITING;
  846. if (BIO_flush(s->wbio) <= 0) {
  847. return 0;
  848. }
  849. s->rwstate = SSL_NOTHING;
  850. return 1;
  851. }
  852. /*
  853. * Called by the record layer to determine whether application data is
  854. * allowed to be received in the current handshake state or not.
  855. *
  856. * Return values are:
  857. * 1: Yes (application data allowed)
  858. * 0: No (application data not allowed)
  859. */
  860. int ossl_statem_app_data_allowed(SSL_CONNECTION *s)
  861. {
  862. OSSL_STATEM *st = &s->statem;
  863. if (st->state == MSG_FLOW_UNINITED)
  864. return 0;
  865. if (!s->s3.in_read_app_data || (s->s3.total_renegotiations == 0))
  866. return 0;
  867. if (s->server) {
  868. /*
  869. * If we're a server and we haven't got as far as writing our
  870. * ServerHello yet then we allow app data
  871. */
  872. if (st->hand_state == TLS_ST_BEFORE
  873. || st->hand_state == TLS_ST_SR_CLNT_HELLO)
  874. return 1;
  875. } else {
  876. /*
  877. * If we're a client and we haven't read the ServerHello yet then we
  878. * allow app data
  879. */
  880. if (st->hand_state == TLS_ST_CW_CLNT_HELLO)
  881. return 1;
  882. }
  883. return 0;
  884. }
  885. /*
  886. * This function returns 1 if TLS exporter is ready to export keying
  887. * material, or 0 if otherwise.
  888. */
  889. int ossl_statem_export_allowed(SSL_CONNECTION *s)
  890. {
  891. return s->s3.previous_server_finished_len != 0
  892. && s->statem.hand_state != TLS_ST_SW_FINISHED;
  893. }
  894. /*
  895. * Return 1 if early TLS exporter is ready to export keying material,
  896. * or 0 if otherwise.
  897. */
  898. int ossl_statem_export_early_allowed(SSL_CONNECTION *s)
  899. {
  900. /*
  901. * The early exporter secret is only present on the server if we
  902. * have accepted early_data. It is present on the client as long
  903. * as we have sent early_data.
  904. */
  905. return s->ext.early_data == SSL_EARLY_DATA_ACCEPTED
  906. || (!s->server && s->ext.early_data != SSL_EARLY_DATA_NOT_SENT);
  907. }