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