daf8a5b1927b9cdf564640af6e226f74cd762f84
[openssl.git] / ssl / statem / statem_locl.h
1 /*
2  * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 /*****************************************************************************
11  *                                                                           *
12  * The following definitions are PRIVATE to the state machine. They should   *
13  * NOT be used outside of the state machine.                                 *
14  *                                                                           *
15  *****************************************************************************/
16
17 /* Max message length definitions */
18
19 /* The spec allows for a longer length than this, but we limit it */
20 #define HELLO_VERIFY_REQUEST_MAX_LENGTH 258
21 #define END_OF_EARLY_DATA_MAX_LENGTH    0
22 #define SERVER_HELLO_MAX_LENGTH         20000
23 #define HELLO_RETRY_REQUEST_MAX_LENGTH  20000
24 #define ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
25 #define SERVER_KEY_EXCH_MAX_LENGTH      102400
26 #define SERVER_HELLO_DONE_MAX_LENGTH    0
27 #define KEY_UPDATE_MAX_LENGTH           1
28 #define CCS_MAX_LENGTH                  1
29 /* Max should actually be 36 but we are generous */
30 #define FINISHED_MAX_LENGTH             64
31
32 /* The maximum number of incoming KeyUpdate messages we will accept */
33 #define MAX_KEY_UPDATE_MESSAGES     32
34
35 /* Extension context codes */
36 /* This extension is only allowed in TLS */
37 #define EXT_TLS_ONLY                        0x0001
38 /* This extension is only allowed in DTLS */
39 #define EXT_DTLS_ONLY                       0x0002
40 /* Some extensions may be allowed in DTLS but we don't implement them for it */
41 #define EXT_TLS_IMPLEMENTATION_ONLY         0x0004
42 /* Most extensions are not defined for SSLv3 but EXT_TYPE_renegotiate is */
43 #define EXT_SSL3_ALLOWED                    0x0008
44 /* Extension is only defined for TLS1.2 and above */
45 #define EXT_TLS1_2_AND_BELOW_ONLY           0x0010
46 /* Extension is only defined for TLS1.3 and above */
47 #define EXT_TLS1_3_ONLY                     0x0020
48 #define EXT_CLIENT_HELLO                    0x0040
49 /* Really means TLS1.2 or below */
50 #define EXT_TLS1_2_SERVER_HELLO             0x0080
51 #define EXT_TLS1_3_SERVER_HELLO             0x0100
52 #define EXT_TLS1_3_ENCRYPTED_EXTENSIONS     0x0200
53 #define EXT_TLS1_3_HELLO_RETRY_REQUEST      0x0400
54 #define EXT_TLS1_3_CERTIFICATE              0x0800
55 #define EXT_TLS1_3_NEW_SESSION_TICKET       0x1000
56
57 /* Dummy message type */
58 #define SSL3_MT_DUMMY   -1
59
60 /* Message processing return codes */
61 typedef enum {
62     /* Something bad happened */
63     MSG_PROCESS_ERROR,
64     /* We've finished reading - swap to writing */
65     MSG_PROCESS_FINISHED_READING,
66     /*
67      * We've completed the main processing of this message but there is some
68      * post processing to be done.
69      */
70     MSG_PROCESS_CONTINUE_PROCESSING,
71     /* We've finished this message - read the next message */
72     MSG_PROCESS_CONTINUE_READING
73 } MSG_PROCESS_RETURN;
74
75 /* Flush the write BIO */
76 int statem_flush(SSL *s);
77
78 typedef int (*confunc_f) (SSL *s, WPACKET *pkt);
79
80 int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups,
81                   size_t num_groups, int checkallow);
82
83 /*
84  * TLS/DTLS client state machine functions
85  */
86 int ossl_statem_client_read_transition(SSL *s, int mt);
87 WRITE_TRAN ossl_statem_client_write_transition(SSL *s);
88 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst);
89 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst);
90 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
91                                          confunc_f *confunc, int *mt);
92 size_t ossl_statem_client_max_message_size(SSL *s);
93 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt);
94 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst);
95
96 /*
97  * TLS/DTLS server state machine functions
98  */
99 int ossl_statem_server_read_transition(SSL *s, int mt);
100 WRITE_TRAN ossl_statem_server_write_transition(SSL *s);
101 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst);
102 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst);
103 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
104                                          confunc_f *confunc,int *mt);
105 size_t ossl_statem_server_max_message_size(SSL *s);
106 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt);
107 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
108
109 /* Functions for getting new message data */
110 __owur int tls_get_message_header(SSL *s, int *mt);
111 __owur int tls_get_message_body(SSL *s, size_t *len);
112 __owur int dtls_get_message(SSL *s, int *mt, size_t *len);
113
114 /* Message construction and processing functions */
115 __owur int tls_process_initial_server_flight(SSL *s, int *al);
116 __owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
117 __owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
118 __owur int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
119 __owur int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
120
121 __owur int tls_construct_finished(SSL *s, WPACKET *pkt);
122 __owur int tls_construct_key_update(SSL *s, WPACKET *pkt);
123 __owur MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt);
124 __owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs);
125 __owur WORK_STATE dtls_wait_for_dry(SSL *s);
126
127 /* some client-only functions */
128 __owur int tls_construct_client_hello(SSL *s, WPACKET *pkt);
129 __owur MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt);
130 __owur MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt);
131 __owur MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt);
132 __owur int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al);
133 __owur MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt);
134 __owur MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt);
135 __owur int tls_construct_cert_verify(SSL *s, WPACKET *pkt);
136 __owur WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst);
137 __owur int tls_construct_client_certificate(SSL *s, WPACKET *pkt);
138 __owur int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
139 __owur int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt);
140 __owur int tls_client_key_exchange_post_work(SSL *s);
141 __owur int tls_construct_cert_status_body(SSL *s, WPACKET *pkt);
142 __owur int tls_construct_cert_status(SSL *s, WPACKET *pkt);
143 __owur MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt);
144 __owur MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt);
145 __owur int ssl3_check_cert_and_algorithm(SSL *s);
146 #ifndef OPENSSL_NO_NEXTPROTONEG
147 __owur int tls_construct_next_proto(SSL *s, WPACKET *pkt);
148 #endif
149 __owur MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt);
150 __owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt);
151 __owur int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt);
152
153 /* some server-only functions */
154 __owur MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt);
155 __owur WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst);
156 __owur int tls_construct_server_hello(SSL *s, WPACKET *pkt);
157 __owur int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt);
158 __owur int tls_construct_server_certificate(SSL *s, WPACKET *pkt);
159 __owur int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt);
160 __owur int tls_construct_certificate_request(SSL *s, WPACKET *pkt);
161 __owur int tls_construct_server_done(SSL *s, WPACKET *pkt);
162 __owur MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt);
163 __owur MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt);
164 __owur WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst);
165 __owur MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt);
166 #ifndef OPENSSL_NO_NEXTPROTONEG
167 __owur MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt);
168 #endif
169 __owur int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt);
170 MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt);
171
172
173 /* Extension processing */
174
175 __owur int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
176                                   RAW_EXTENSION **res, int *al, size_t *len);
177 __owur int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
178                                RAW_EXTENSION *exts,  X509 *x, size_t chainidx,
179                                int *al);
180 __owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
181                                     X509 *x, size_t chainidx, int *al);
182 __owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
183                                     X509 *x, size_t chainidx, int *al);
184
185 __owur int tls_psk_do_binder(SSL *s, const EVP_MD *md,
186                              const unsigned char *msgstart,
187                              size_t binderoffset, const unsigned char *binderin,
188                              unsigned char *binderout,
189                              SSL_SESSION *sess, int sign);
190
191 /* Server Extension processing */
192 int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
193                                X509 *x, size_t chainidx, int *al);
194 int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
195                                X509 *x, size_t chainidx, int *al);
196 #ifndef OPENSSL_NO_SRP
197 int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
198                        size_t chainidx, int *al);
199 #endif
200 int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
201                               X509 *x, size_t chainidx, int *al);
202 #ifndef OPENSSL_NO_EC
203 int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
204                                  X509 *x, size_t chainidx, int *al);
205 int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
206                                     X509 *x, size_t chainidx, int *al);
207 #endif
208 int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
209                                   X509 *x, size_t chainidx, int *al);
210 int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
211                             size_t chainidx, int *al);
212 #ifndef OPENSSL_NO_OCSP
213 int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
214                                   X509 *x, size_t chainidx, int *al);
215 #endif
216 #ifndef OPENSSL_NO_NEXTPROTONEG
217 int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
218                        size_t chainidx, int *al);
219 #endif
220 int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
221                         size_t chainidx, int *al);
222 #ifndef OPENSSL_NO_SRTP
223 int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
224                             size_t chainidx, int *al);
225 #endif
226 int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
227                        size_t chainidx, int *al);
228 int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
229                              size_t chainidx, int *al);
230 int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
231                        size_t chainidx, int *al);
232 int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
233                                  X509 *x, size_t chainidx, int *al);
234 int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
235                        size_t chainidx, int *al);
236
237 int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
238                                    X509 *x, size_t chainidx, int *al);
239 int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, unsigned int context,
240                                    X509 *x, size_t chainidx, int *al);
241 int tls_construct_stoc_early_data_info(SSL *s, WPACKET *pkt,
242                                        unsigned int context, X509 *x,
243                                        size_t chainidx, int *al);
244 int tls_construct_stoc_early_data(SSL *s, WPACKET *pkt, unsigned int context,
245                                   X509 *x, size_t chainidx, int *al);
246 #ifndef OPENSSL_NO_EC
247 int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
248                                      X509 *x, size_t chainidx, int *al);
249 #endif
250 int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
251                                       unsigned int context, X509 *x,
252                                       size_t chainidx, int *al);
253 #ifndef OPENSSL_NO_OCSP
254 int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
255                                       unsigned int context, X509 *x,
256                                       size_t chainidx, int *al);
257 #endif
258 #ifndef OPENSSL_NO_NEXTPROTONEG
259 int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
260                                       unsigned int context, X509 *x,
261                                       size_t chainidx, int *al);
262 #endif
263 int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
264                             size_t chainidx, int *al);
265 #ifndef OPENSSL_NO_SRTP
266 int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
267                                 X509 *x, size_t chainidx, int *al);
268 #endif
269 int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
270                            size_t chainidx, int *al);
271 int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
272                            size_t chainidx, int *al);
273 int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, unsigned int context,
274                                  X509 *x, size_t chainidx, int *al);
275 /*
276  * Not in public headers as this is not an official extension. Only used when
277  * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set.
278  */
279 #define TLSEXT_TYPE_cryptopro_bug      0xfde8
280 int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, unsigned int context,
281                                      X509 *x, size_t chainidx, int *al);
282 int tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
283                            size_t chainidx, int *al);
284
285 /* Client Extension processing */
286 int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
287                                    X509 *x, size_t chainidx, int *al);
288 int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
289                                    X509 *x, size_t chainidx, int *al);
290 #ifndef OPENSSL_NO_SRP
291 int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
292                            size_t chainidx, int *al);
293 #endif
294 #ifndef OPENSSL_NO_EC
295 int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
296                                      X509 *x, size_t chainidx, int *al);
297 int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
298                                         unsigned int context, X509 *x,
299                                         size_t chainidx, int *al);
300 #endif
301 int tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, unsigned int context,
302                                   X509 *x, size_t chainidx, int *al);
303 int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
304                                       unsigned int context, X509 *x,
305                                       size_t chainidx, int *al);
306 int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, unsigned int context,
307                                 X509 *x, size_t chainidx, int *al);
308 #ifndef OPENSSL_NO_OCSP
309 int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
310                                       unsigned int context, X509 *x,
311                                       size_t chainidx, int *al);
312 #endif
313 #ifndef OPENSSL_NO_NEXTPROTONEG
314 int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
315                            size_t chainidx, int *al);
316 #endif
317 int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
318                             size_t chainidx, int *al);
319 #ifndef OPENSSL_NO_SRTP
320 int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
321                                 X509 *x, size_t chainidx, int *al);
322 #endif
323 int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
324                            size_t chainidx, int *al);
325 #ifndef OPENSSL_NO_CT
326 int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
327                            size_t chainidx, int *al);
328 #endif
329 int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
330                            size_t chainidx, int *al);
331 int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
332                                           unsigned int context, X509 *x,
333                                           size_t chainidx, int *al);
334 int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, unsigned int context,
335                                  X509 *x, size_t chainidx, int *al);
336 int tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, unsigned int context,
337                                      X509 *x, size_t chainidx, int *al);
338 int tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
339                               X509 *x, size_t chainidx, int *al);
340 int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, unsigned int context,
341                                X509 *x, size_t chainidx, int *al);
342 int tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
343                            size_t chainidx, int *al);
344 int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
345                                X509 *x, size_t chainidx, int *al);
346 int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
347                                X509 *x, size_t chainidx, int *al);
348 int tls_parse_stoc_early_data_info(SSL *s, PACKET *pkt, unsigned int context,
349                               X509 *x, size_t chainidx, int *al);
350 int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
351                               X509 *x, size_t chainidx, int *al);
352 #ifndef OPENSSL_NO_EC
353 int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
354                                  X509 *x, size_t chainidx, int *al);
355 #endif
356 int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
357                                   X509 *x, size_t chainidx, int *al);
358 #ifndef OPENSSL_NO_OCSP
359 int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
360                                   X509 *x, size_t chainidx, int *al);
361 #endif
362 #ifndef OPENSSL_NO_CT
363 int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
364                        size_t chainidx, int *al);
365 #endif
366 #ifndef OPENSSL_NO_NEXTPROTONEG
367 int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
368                        size_t chainidx, int *al);
369 #endif
370 int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
371                         size_t chainidx, int *al);
372 #ifndef OPENSSL_NO_SRTP
373 int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
374                             size_t chainidx, int *al);
375 #endif
376 int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
377                        size_t chainidx, int *al);
378 int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
379                        size_t chainidx, int *al);
380 int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
381                              size_t chainidx, int *al);
382 int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
383                        size_t chainidx, int *al);
384 int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
385                        size_t chainidx, int *al);