1e1051a39Sopenharmony_ciState Machine Design
2e1051a39Sopenharmony_ci====================
3e1051a39Sopenharmony_ci
4e1051a39Sopenharmony_ciThis file provides some guidance on the thinking behind the design of the
5e1051a39Sopenharmony_cistate machine code to aid future maintenance.
6e1051a39Sopenharmony_ci
7e1051a39Sopenharmony_ciThe state machine code replaces an older state machine present in OpenSSL
8e1051a39Sopenharmony_civersions 1.0.2 and below. The new state machine has the following objectives:
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci  - Remove duplication of state code between client and server
11e1051a39Sopenharmony_ci  - Remove duplication of state code between TLS and DTLS
12e1051a39Sopenharmony_ci  - Simplify transitions and bring the logic together in a single location
13e1051a39Sopenharmony_ci    so that it is easier to validate
14e1051a39Sopenharmony_ci  - Remove duplication of code between each of the message handling functions
15e1051a39Sopenharmony_ci  - Receive a message first and then work out whether that is a valid
16e1051a39Sopenharmony_ci    transition - not the other way around (the other way causes lots of issues
17e1051a39Sopenharmony_ci    where we are expecting one type of message next but actually get something
18e1051a39Sopenharmony_ci    else)
19e1051a39Sopenharmony_ci  - Separate message flow state from handshake state (in order to better
20e1051a39Sopenharmony_ci    understand each)
21e1051a39Sopenharmony_ci    * message flow state = when to flush buffers; handling restarts in the
22e1051a39Sopenharmony_ci      event of NBIO events; handling the common flow of steps for reading a
23e1051a39Sopenharmony_ci      message and the common flow of steps for writing a message etc
24e1051a39Sopenharmony_ci    * handshake state = what handshake message are we working on now
25e1051a39Sopenharmony_ci  - Control complexity: only the state machine can change state: keep all
26e1051a39Sopenharmony_ci    the state changes local to the state machine component
27e1051a39Sopenharmony_ci
28e1051a39Sopenharmony_ciThe message flow state machine is divided into a reading sub-state machine and a
29e1051a39Sopenharmony_ciwriting sub-state machine. See the source comments in statem.c for a more
30e1051a39Sopenharmony_cidetailed description of the various states and transitions possible.
31e1051a39Sopenharmony_ci
32e1051a39Sopenharmony_ciConceptually the state machine component is designed as follows:
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci                          libssl
35e1051a39Sopenharmony_ci                             |
36e1051a39Sopenharmony_ci    -------------------------|-----statem.h------------------------------------
37e1051a39Sopenharmony_ci                             |
38e1051a39Sopenharmony_ci                      _______V____________________
39e1051a39Sopenharmony_ci                     |                            |
40e1051a39Sopenharmony_ci                     |    statem.c                |
41e1051a39Sopenharmony_ci                     |                            |
42e1051a39Sopenharmony_ci                     |    Core state machine code |
43e1051a39Sopenharmony_ci                     |____________________________|
44e1051a39Sopenharmony_ci          statem_local.h     ^          ^
45e1051a39Sopenharmony_ci                   _________|          |_______
46e1051a39Sopenharmony_ci                  |                            |
47e1051a39Sopenharmony_ci     _____________|____________   _____________|____________
48e1051a39Sopenharmony_ci    |                          | |                          |
49e1051a39Sopenharmony_ci    | statem_clnt.c            | | statem_srvr.c            |
50e1051a39Sopenharmony_ci    |                          | |                          |
51e1051a39Sopenharmony_ci    | TLS/DTLS client specific | | TLS/DTLS server specific |
52e1051a39Sopenharmony_ci    | state machine code       | | state machine code       |
53e1051a39Sopenharmony_ci    |__________________________| |__________________________|
54e1051a39Sopenharmony_ci                 |        |_______________|__       |
55e1051a39Sopenharmony_ci                 |        ________________|  |      |
56e1051a39Sopenharmony_ci                 |       |                   |      |
57e1051a39Sopenharmony_ci     ____________V_______V________   ________V______V_______________
58e1051a39Sopenharmony_ci    |                             | |                               |
59e1051a39Sopenharmony_ci    | statem_lib.c                | | statem_dtls.c                 |
60e1051a39Sopenharmony_ci    |                             | |                               |
61e1051a39Sopenharmony_ci    | Non core functions common   | | Non core functions common to  |
62e1051a39Sopenharmony_ci    | to both servers and clients | | both DTLS servers and clients |
63e1051a39Sopenharmony_ci    |_____________________________| |_______________________________|
64