2 * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
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
10 /*****************************************************************************
12 * These enums should be considered PRIVATE to the state machine. No *
13 * non-state machine code should need to use these *
15 *****************************************************************************/
17 * Valid return codes used for functions performing work prior to or after
18 * sending or receiving a message
21 /* Something went wrong */
23 /* We're done working and there shouldn't be anything else to do after */
25 /* We're done working move onto the next thing */
26 WORK_FINISHED_CONTINUE,
27 /* We're working on phase A */
29 /* We're working on phase B */
31 /* We're working on phase C */
35 /* Write transition return codes */
37 /* Something went wrong */
39 /* A transition was successfully completed and we should continue */
41 /* There is no more write work to be done */
45 /* Message flow states */
47 /* No handshake in progress */
49 /* A permanent error with this connection */
51 /* We are reading messages */
53 /* We are writing messages */
55 /* Handshake has finished */
63 READ_STATE_POST_PROCESS
68 WRITE_STATE_TRANSITION,
75 /* The enc_write_ctx can be used normally */
76 ENC_WRITE_STATE_VALID,
77 /* The enc_write_ctx cannot be used */
78 ENC_WRITE_STATE_INVALID,
79 /* Write alerts in plaintext, but otherwise use the enc_write_ctx */
80 ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
84 /* The enc_read_ctx can be used normally */
86 /* We may receive encrypted or plaintext alerts */
87 ENC_READ_STATE_ALLOW_PLAIN_ALERTS
90 /*****************************************************************************
92 * This structure should be considered "opaque" to anything outside of the *
93 * state machine. No non-state machine code should be accessing the members *
94 * of this structure. *
96 *****************************************************************************/
98 struct ossl_statem_st {
100 WRITE_STATE write_state;
101 WORK_STATE write_state_work;
102 READ_STATE read_state;
103 WORK_STATE read_state_work;
104 OSSL_HANDSHAKE_STATE hand_state;
105 /* The handshake state requested by an API call (e.g. HelloRequest) */
106 OSSL_HANDSHAKE_STATE request_state;
108 int read_state_first_init;
109 /* true when we are actually in SSL_accept() or SSL_connect() */
112 * True when are processing a "real" handshake that needs cleaning up (not
113 * just a HelloRequest or similar).
116 /* Should we skip the CertificateVerify message? */
117 unsigned int no_cert_verify;
119 ENC_WRITE_STATES enc_write_state;
120 ENC_READ_STATES enc_read_state;
122 typedef struct ossl_statem_st OSSL_STATEM;
124 /*****************************************************************************
126 * The following macros/functions represent the libssl internal API to the *
127 * state machine. Any libssl code may call these functions/macros *
129 *****************************************************************************/
131 __owur int ossl_statem_accept(SSL *s);
132 __owur int ossl_statem_connect(SSL *s);
133 void ossl_statem_clear(SSL *s);
134 void ossl_statem_set_renegotiate(SSL *s);
135 void ossl_statem_fatal(SSL *s, int al, int func, int reason, const char *file,
137 # define SSL_AD_NO_ALERT -1
138 # ifndef OPENSSL_NO_ERR
139 # define SSLfatal(s, al, f, r) ossl_statem_fatal((s), (al), (0), (r), \
140 OPENSSL_FILE, OPENSSL_LINE)
142 # define SSLfatal(s, al, f, r) ossl_statem_fatal((s), (al), (0), (r), NULL, 0)
145 int ossl_statem_in_error(const SSL *s);
146 void ossl_statem_set_in_init(SSL *s, int init);
147 int ossl_statem_get_in_handshake(SSL *s);
148 void ossl_statem_set_in_handshake(SSL *s, int inhand);
149 __owur int ossl_statem_skip_early_data(SSL *s);
150 void ossl_statem_check_finish_init(SSL *s, int send);
151 void ossl_statem_set_hello_verify_done(SSL *s);
152 __owur int ossl_statem_app_data_allowed(SSL *s);
153 __owur int ossl_statem_export_allowed(SSL *s);
154 __owur int ossl_statem_export_early_allowed(SSL *s);
156 /* Flush the write BIO */
157 int statem_flush(SSL *s);