statem.c 27 KB

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