2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 * The following definitions are PRIVATE to the state machine. They should *
13 * NOT be used outside of the state machine. *
15 *****************************************************************************/
17 /* Max message length definitions */
19 /* The spec allows for a longer length than this, but we limit it */
20 #define HELLO_VERIFY_REQUEST_MAX_LENGTH 258
21 #define SERVER_HELLO_MAX_LENGTH 20000
22 #define ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
23 #define SERVER_KEY_EXCH_MAX_LENGTH 102400
24 #define SERVER_HELLO_DONE_MAX_LENGTH 0
25 #define CCS_MAX_LENGTH 1
26 /* Max should actually be 36 but we are generous */
27 #define FINISHED_MAX_LENGTH 64
29 /* Extension context codes */
30 /* This extension is only allowed in TLS */
31 #define EXT_TLS_ONLY 0x0001
32 /* This extension is only allowed in DTLS */
33 #define EXT_DTLS_ONLY 0x0002
34 /* Some extensions may be allowed in DTLS but we don't implement them for it */
35 #define EXT_TLS_IMPLEMENTATION_ONLY 0x0004
36 /* Most extensions are not defined for SSLv3 but EXT_TYPE_renegotiate is */
37 #define EXT_SSL3_ALLOWED 0x0008
38 /* Extension is only defined for TLS1.2 and above */
39 #define EXT_TLS1_2_AND_BELOW_ONLY 0x0010
40 /* Extension is only defined for TLS1.3 and above */
41 #define EXT_TLS1_3_ONLY 0x0020
42 #define EXT_CLIENT_HELLO 0x0040
43 /* Really means TLS1.2 or below */
44 #define EXT_TLS1_2_SERVER_HELLO 0x0080
45 #define EXT_TLS1_3_SERVER_HELLO 0x0100
46 #define EXT_TLS1_3_ENCRYPTED_EXTENSIONS 0x0200
47 #define EXT_TLS1_3_HELLO_RETRY_REQUEST 0x0400
48 #define EXT_TLS1_3_CERTIFICATE 0x0800
49 #define EXT_TLS1_3_NEW_SESSION_TICKET 0x1000
51 /* Message processing return codes */
53 /* Something bad happened */
55 /* We've finished reading - swap to writing */
56 MSG_PROCESS_FINISHED_READING
,
58 * We've completed the main processing of this message but there is some
59 * post processing to be done.
61 MSG_PROCESS_CONTINUE_PROCESSING
,
62 /* We've finished this message - read the next message */
63 MSG_PROCESS_CONTINUE_READING
66 /* Flush the write BIO */
67 int statem_flush(SSL
*s
);
69 typedef int (*confunc_f
) (SSL
*s
, WPACKET
*pkt
);
72 * TLS/DTLS client state machine functions
74 int ossl_statem_client_read_transition(SSL
*s
, int mt
);
75 WRITE_TRAN
ossl_statem_client_write_transition(SSL
*s
);
76 WORK_STATE
ossl_statem_client_pre_work(SSL
*s
, WORK_STATE wst
);
77 WORK_STATE
ossl_statem_client_post_work(SSL
*s
, WORK_STATE wst
);
78 int ossl_statem_client_construct_message(SSL
*s
, WPACKET
*pkt
,
79 confunc_f
*confunc
, int *mt
);
80 size_t ossl_statem_client_max_message_size(SSL
*s
);
81 MSG_PROCESS_RETURN
ossl_statem_client_process_message(SSL
*s
, PACKET
*pkt
);
82 WORK_STATE
ossl_statem_client_post_process_message(SSL
*s
, WORK_STATE wst
);
85 * TLS/DTLS server state machine functions
87 int ossl_statem_server_read_transition(SSL
*s
, int mt
);
88 WRITE_TRAN
ossl_statem_server_write_transition(SSL
*s
);
89 WORK_STATE
ossl_statem_server_pre_work(SSL
*s
, WORK_STATE wst
);
90 WORK_STATE
ossl_statem_server_post_work(SSL
*s
, WORK_STATE wst
);
91 int ossl_statem_server_construct_message(SSL
*s
, WPACKET
*pkt
,
92 confunc_f
*confunc
,int *mt
);
93 size_t ossl_statem_server_max_message_size(SSL
*s
);
94 MSG_PROCESS_RETURN
ossl_statem_server_process_message(SSL
*s
, PACKET
*pkt
);
95 WORK_STATE
ossl_statem_server_post_process_message(SSL
*s
, WORK_STATE wst
);
97 /* Functions for getting new message data */
98 __owur
int tls_get_message_header(SSL
*s
, int *mt
);
99 __owur
int tls_get_message_body(SSL
*s
, size_t *len
);
100 __owur
int dtls_get_message(SSL
*s
, int *mt
, size_t *len
);
102 /* Message construction and processing functions */
103 __owur
int tls_process_initial_server_flight(SSL
*s
, int *al
);
104 __owur MSG_PROCESS_RETURN
tls_process_change_cipher_spec(SSL
*s
, PACKET
*pkt
);
105 __owur MSG_PROCESS_RETURN
tls_process_finished(SSL
*s
, PACKET
*pkt
);
106 __owur
int tls_construct_change_cipher_spec(SSL
*s
, WPACKET
*pkt
);
107 __owur
int dtls_construct_change_cipher_spec(SSL
*s
, WPACKET
*pkt
);
109 __owur
int tls_construct_finished(SSL
*s
, WPACKET
*pkt
);
110 __owur WORK_STATE
tls_finish_handshake(SSL
*s
, WORK_STATE wst
);
111 __owur WORK_STATE
dtls_wait_for_dry(SSL
*s
);
113 /* some client-only functions */
114 __owur
int tls_construct_client_hello(SSL
*s
, WPACKET
*pkt
);
115 __owur MSG_PROCESS_RETURN
tls_process_server_hello(SSL
*s
, PACKET
*pkt
);
116 __owur MSG_PROCESS_RETURN
tls_process_certificate_request(SSL
*s
, PACKET
*pkt
);
117 __owur MSG_PROCESS_RETURN
tls_process_new_session_ticket(SSL
*s
, PACKET
*pkt
);
118 __owur MSG_PROCESS_RETURN
tls_process_cert_status(SSL
*s
, PACKET
*pkt
);
119 __owur MSG_PROCESS_RETURN
tls_process_server_done(SSL
*s
, PACKET
*pkt
);
120 __owur
int tls_construct_client_verify(SSL
*s
, WPACKET
*pkt
);
121 __owur WORK_STATE
tls_prepare_client_certificate(SSL
*s
, WORK_STATE wst
);
122 __owur
int tls_construct_client_certificate(SSL
*s
, WPACKET
*pkt
);
123 __owur
int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
);
124 __owur
int tls_construct_client_key_exchange(SSL
*s
, WPACKET
*pkt
);
125 __owur
int tls_client_key_exchange_post_work(SSL
*s
);
126 __owur
int tls_construct_cert_status(SSL
*s
, WPACKET
*pkt
);
127 __owur MSG_PROCESS_RETURN
tls_process_key_exchange(SSL
*s
, PACKET
*pkt
);
128 __owur MSG_PROCESS_RETURN
tls_process_server_certificate(SSL
*s
, PACKET
*pkt
);
129 __owur
int ssl3_check_cert_and_algorithm(SSL
*s
);
130 #ifndef OPENSSL_NO_NEXTPROTONEG
131 __owur
int tls_construct_next_proto(SSL
*s
, WPACKET
*pkt
);
133 __owur MSG_PROCESS_RETURN
dtls_process_hello_verify(SSL
*s
, PACKET
*pkt
);
135 /* some server-only functions */
136 __owur MSG_PROCESS_RETURN
tls_process_client_hello(SSL
*s
, PACKET
*pkt
);
137 __owur WORK_STATE
tls_post_process_client_hello(SSL
*s
, WORK_STATE wst
);
138 __owur
int tls_construct_server_hello(SSL
*s
, WPACKET
*pkt
);
139 __owur
int dtls_construct_hello_verify_request(SSL
*s
, WPACKET
*pkt
);
140 __owur
int tls_construct_server_certificate(SSL
*s
, WPACKET
*pkt
);
141 __owur
int tls_construct_server_key_exchange(SSL
*s
, WPACKET
*pkt
);
142 __owur
int tls_construct_certificate_request(SSL
*s
, WPACKET
*pkt
);
143 __owur
int tls_construct_server_done(SSL
*s
, WPACKET
*pkt
);
144 __owur MSG_PROCESS_RETURN
tls_process_client_certificate(SSL
*s
, PACKET
*pkt
);
145 __owur MSG_PROCESS_RETURN
tls_process_client_key_exchange(SSL
*s
, PACKET
*pkt
);
146 __owur WORK_STATE
tls_post_process_client_key_exchange(SSL
*s
, WORK_STATE wst
);
147 __owur MSG_PROCESS_RETURN
tls_process_cert_verify(SSL
*s
, PACKET
*pkt
);
148 #ifndef OPENSSL_NO_NEXTPROTONEG
149 __owur MSG_PROCESS_RETURN
tls_process_next_proto(SSL
*s
, PACKET
*pkt
);
151 __owur
int tls_construct_new_session_ticket(SSL
*s
, WPACKET
*pkt
);
154 /* Extension processing */
156 __owur
int tls_collect_extensions(SSL
*s
, PACKET
*packet
, unsigned int context
,
157 RAW_EXTENSION
**res
, int *al
);
158 __owur
int tls_parse_extension(SSL
*s
, unsigned int idx
, int context
,
159 RAW_EXTENSION
*exts
, int *al
);
160 __owur
int tls_parse_all_extensions(SSL
*s
, int context
, RAW_EXTENSION
*exts
,
162 __owur
int tls_construct_extensions(SSL
*s
, WPACKET
*pkt
, unsigned int context
,
165 /* Server Extension processing */
166 int tls_parse_ctos_renegotiate(SSL
*s
, PACKET
*pkt
, int *al
);
167 int tls_parse_ctos_server_name(SSL
*s
, PACKET
*pkt
, int *al
);
168 #ifndef OPENSSL_NO_SRP
169 int tls_parse_ctos_srp(SSL
*s
, PACKET
*pkt
, int *al
);
171 #ifndef OPENSSL_NO_EC
172 int tls_parse_ctos_ec_pt_formats(SSL
*s
, PACKET
*pkt
, int *al
);
173 int tls_parse_ctos_supported_groups(SSL
*s
, PACKET
*pkt
, int *al
);
175 int tls_parse_ctos_session_ticket(SSL
*s
, PACKET
*pkt
, int *al
);
176 int tls_parse_ctos_sig_algs(SSL
*s
, PACKET
*pkt
, int *al
);
177 #ifndef OPENSSL_NO_OCSP
178 int tls_parse_ctos_status_request(SSL
*s
, PACKET
*pkt
, int *al
);
180 #ifndef OPENSSL_NO_NEXTPROTONEG
181 int tls_parse_ctos_npn(SSL
*s
, PACKET
*pkt
, int *al
);
183 int tls_parse_ctos_alpn(SSL
*s
, PACKET
*pkt
, int *al
);
184 #ifndef OPENSSL_NO_SRTP
185 int tls_parse_ctos_use_srtp(SSL
*s
, PACKET
*pkt
, int *al
);
187 int tls_parse_ctos_etm(SSL
*s
, PACKET
*pkt
, int *al
);
188 int tls_parse_ctos_key_share(SSL
*s
, PACKET
*pkt
, int *al
);
189 int tls_parse_ctos_ems(SSL
*s
, PACKET
*pkt
, int *al
);
191 int tls_construct_stoc_renegotiate(SSL
*s
, WPACKET
*pkt
, int *al
);
192 int tls_construct_stoc_server_name(SSL
*s
, WPACKET
*pkt
, int *al
);
193 #ifndef OPENSSL_NO_EC
194 int tls_construct_stoc_ec_pt_formats(SSL
*s
, WPACKET
*pkt
, int *al
);
196 int tls_construct_stoc_session_ticket(SSL
*s
, WPACKET
*pkt
, int *al
);
197 #ifndef OPENSSL_NO_OCSP
198 int tls_construct_stoc_status_request(SSL
*s
, WPACKET
*pkt
, int *al
);
200 #ifndef OPENSSL_NO_NEXTPROTONEG
201 int tls_construct_stoc_next_proto_neg(SSL
*s
, WPACKET
*pkt
, int *al
);
203 int tls_construct_stoc_alpn(SSL
*s
, WPACKET
*pkt
, int *al
);
204 #ifndef OPENSSL_NO_SRTP
205 int tls_construct_stoc_use_srtp(SSL
*s
, WPACKET
*pkt
, int *al
);
207 int tls_construct_stoc_etm(SSL
*s
, WPACKET
*pkt
, int *al
);
208 int tls_construct_stoc_ems(SSL
*s
, WPACKET
*pkt
, int *al
);
209 int tls_construct_stoc_key_share(SSL
*s
, WPACKET
*pkt
, int *al
);
211 * Not in public headers as this is not an official extension. Only used when
212 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set.
214 #define TLSEXT_TYPE_cryptopro_bug 0xfde8
215 int tls_construct_stoc_cryptopro_bug(SSL
*s
, WPACKET
*pkt
, int *al
);
217 /* Client Extension processing */
218 int tls_construct_ctos_renegotiate(SSL
*s
, WPACKET
*pkt
, int *al
);
219 int tls_construct_ctos_server_name(SSL
*s
, WPACKET
*pkt
, int *al
);
220 #ifndef OPENSSL_NO_SRP
221 int tls_construct_ctos_srp(SSL
*s
, WPACKET
*pkt
, int *al
);
223 #ifndef OPENSSL_NO_EC
224 int tls_construct_ctos_ec_pt_formats(SSL
*s
, WPACKET
*pkt
, int *al
);
225 int tls_construct_ctos_supported_groups(SSL
*s
, WPACKET
*pkt
, int *al
);
227 int tls_construct_ctos_session_ticket(SSL
*s
, WPACKET
*pkt
, int *al
);
228 int tls_construct_ctos_sig_algs(SSL
*s
, WPACKET
*pkt
, int *al
);
229 #ifndef OPENSSL_NO_OCSP
230 int tls_construct_ctos_status_request(SSL
*s
, WPACKET
*pkt
, int *al
);
232 #ifndef OPENSSL_NO_NEXTPROTONEG
233 int tls_construct_ctos_npn(SSL
*s
, WPACKET
*pkt
, int *al
);
235 int tls_construct_ctos_alpn(SSL
*s
, WPACKET
*pkt
, int *al
);
236 #ifndef OPENSSL_NO_SRTP
237 int tls_construct_ctos_use_srtp(SSL
*s
, WPACKET
*pkt
, int *al
);
239 int tls_construct_ctos_etm(SSL
*s
, WPACKET
*pkt
, int *al
);
240 #ifndef OPENSSL_NO_CT
241 int tls_construct_ctos_sct(SSL
*s
, WPACKET
*pkt
, int *al
);
243 int tls_construct_ctos_ems(SSL
*s
, WPACKET
*pkt
, int *al
);
244 int tls_construct_ctos_supported_versions(SSL
*s
, WPACKET
*pkt
, int *al
);
245 int tls_construct_ctos_key_share(SSL
*s
, WPACKET
*pkt
, int *al
);
246 int tls_construct_ctos_padding(SSL
*s
, WPACKET
*pkt
, int *al
);
247 int tls_parse_stoc_renegotiate(SSL
*s
, PACKET
*pkt
, int *al
);
248 int tls_parse_stoc_server_name(SSL
*s
, PACKET
*pkt
, int *al
);
249 #ifndef OPENSSL_NO_EC
250 int tls_parse_stoc_ec_pt_formats(SSL
*s
, PACKET
*pkt
, int *al
);
252 int tls_parse_stoc_session_ticket(SSL
*s
, PACKET
*pkt
, int *al
);
253 #ifndef OPENSSL_NO_OCSP
254 int tls_parse_stoc_status_request(SSL
*s
, PACKET
*pkt
, int *al
);
256 #ifndef OPENSSL_NO_CT
257 int tls_parse_stoc_sct(SSL
*s
, PACKET
*pkt
, int *al
);
259 #ifndef OPENSSL_NO_NEXTPROTONEG
260 int tls_parse_stoc_npn(SSL
*s
, PACKET
*pkt
, int *al
);
262 int tls_parse_stoc_alpn(SSL
*s
, PACKET
*pkt
, int *al
);
263 #ifndef OPENSSL_NO_SRTP
264 int tls_parse_stoc_use_srtp(SSL
*s
, PACKET
*pkt
, int *al
);
266 int tls_parse_stoc_etm(SSL
*s
, PACKET
*pkt
, int *al
);
267 int tls_parse_stoc_ems(SSL
*s
, PACKET
*pkt
, int *al
);
268 int tls_parse_stoc_key_share(SSL
*s
, PACKET
*pkt
, int *al
);