5e0ce7e9978d3c38f01928a3621eaf9f51dcefc0
[openssl.git] / ssl / statem / statem_locl.h
1 /*
2  * Copyright 2015-2017 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 /* Dummy message type */
36 #define SSL3_MT_DUMMY   -1
37
38 extern const unsigned char hrrrandom[];
39
40 /* Message processing return codes */
41 typedef enum {
42     /* Something bad happened */
43     MSG_PROCESS_ERROR,
44     /* We've finished reading - swap to writing */
45     MSG_PROCESS_FINISHED_READING,
46     /*
47      * We've completed the main processing of this message but there is some
48      * post processing to be done.
49      */
50     MSG_PROCESS_CONTINUE_PROCESSING,
51     /* We've finished this message - read the next message */
52     MSG_PROCESS_CONTINUE_READING
53 } MSG_PROCESS_RETURN;
54
55 /* Flush the write BIO */
56 int statem_flush(SSL *s);
57
58 typedef int (*confunc_f) (SSL *s, WPACKET *pkt);
59
60 int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
61                   size_t num_groups, int checkallow);
62 int create_synthetic_message_hash(SSL *s);
63 int parse_ca_names(SSL *s, PACKET *pkt);
64 int construct_ca_names(SSL *s, WPACKET *pkt);
65 size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
66                                   const void *param, size_t paramlen);
67
68 /*
69  * TLS/DTLS client state machine functions
70  */
71 int ossl_statem_client_read_transition(SSL *s, int mt);
72 WRITE_TRAN ossl_statem_client_write_transition(SSL *s);
73 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst);
74 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst);
75 int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
76                                          confunc_f *confunc, int *mt);
77 size_t ossl_statem_client_max_message_size(SSL *s);
78 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt);
79 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst);
80
81 /*
82  * TLS/DTLS server state machine functions
83  */
84 int ossl_statem_server_read_transition(SSL *s, int mt);
85 WRITE_TRAN ossl_statem_server_write_transition(SSL *s);
86 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst);
87 WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst);
88 int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
89                                          confunc_f *confunc,int *mt);
90 size_t ossl_statem_server_max_message_size(SSL *s);
91 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt);
92 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
93
94 /* Functions for getting new message data */
95 __owur int tls_get_message_header(SSL *s, int *mt);
96 __owur int tls_get_message_body(SSL *s, size_t *len);
97 __owur int dtls_get_message(SSL *s, int *mt, size_t *len);
98
99 /* Message construction and processing functions */
100 __owur int tls_process_initial_server_flight(SSL *s);
101 __owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
102 __owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
103 __owur int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
104 __owur int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
105
106 __owur int tls_construct_finished(SSL *s, WPACKET *pkt);
107 __owur int tls_construct_key_update(SSL *s, WPACKET *pkt);
108 __owur MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt);
109 __owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs);
110 __owur WORK_STATE dtls_wait_for_dry(SSL *s);
111
112 /* some client-only functions */
113 __owur int tls_construct_client_hello(SSL *s, WPACKET *pkt);
114 __owur MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt);
115 __owur MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt);
116 __owur MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt);
117 __owur int tls_process_cert_status_body(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_cert_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_body(SSL *s, WPACKET *pkt);
127 __owur int tls_construct_cert_status(SSL *s, WPACKET *pkt);
128 __owur MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt);
129 __owur MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt);
130 __owur int ssl3_check_cert_and_algorithm(SSL *s);
131 #ifndef OPENSSL_NO_NEXTPROTONEG
132 __owur int tls_construct_next_proto(SSL *s, WPACKET *pkt);
133 #endif
134 __owur MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt);
135 __owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt);
136 __owur int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt);
137
138 /* some server-only functions */
139 __owur MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt);
140 __owur WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst);
141 __owur int tls_construct_server_hello(SSL *s, WPACKET *pkt);
142 __owur int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt);
143 __owur int tls_construct_server_certificate(SSL *s, WPACKET *pkt);
144 __owur int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt);
145 __owur int tls_construct_certificate_request(SSL *s, WPACKET *pkt);
146 __owur int tls_construct_server_done(SSL *s, WPACKET *pkt);
147 __owur MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt);
148 __owur MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt);
149 __owur WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst);
150 __owur MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt);
151 #ifndef OPENSSL_NO_NEXTPROTONEG
152 __owur MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt);
153 #endif
154 __owur int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt);
155 MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt);
156
157
158 /* Extension processing */
159
160 typedef enum ext_return_en {
161     EXT_RETURN_FAIL,
162     EXT_RETURN_SENT,
163     EXT_RETURN_NOT_SENT
164 } EXT_RETURN;
165
166 __owur int tls_validate_all_contexts(SSL *s, unsigned int thisctx,
167                                      RAW_EXTENSION *exts);
168 __owur int extension_is_relevant(SSL *s, unsigned int extctx,
169                                  unsigned int thisctx);
170 __owur int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
171                                   RAW_EXTENSION **res, size_t *len, int init);
172 __owur int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
173                                RAW_EXTENSION *exts,  X509 *x, size_t chainidx);
174 __owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
175                                     X509 *x, size_t chainidx, int fin);
176 __owur int should_add_extension(SSL *s, unsigned int extctx,
177                                 unsigned int thisctx, int max_version);
178 __owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
179                                     X509 *x, size_t chainidx);
180
181 __owur int tls_psk_do_binder(SSL *s, const EVP_MD *md,
182                              const unsigned char *msgstart,
183                              size_t binderoffset, const unsigned char *binderin,
184                              unsigned char *binderout,
185                              SSL_SESSION *sess, int sign, int external);
186
187 /* Server Extension processing */
188 int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
189                                X509 *x, size_t chainidx);
190 int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
191                                X509 *x, size_t chainidx);
192 int tls_parse_ctos_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
193                                   X509 *x, size_t chainidx);
194 #ifndef OPENSSL_NO_SRP
195 int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
196                        size_t chainidx);
197 #endif
198 int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
199                               X509 *x, size_t chainidx);
200 #ifndef OPENSSL_NO_EC
201 int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
202                                  X509 *x, size_t chainidx);
203 int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
204                                     X509 *x, size_t chainidxl);
205 #endif
206 int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
207                                   X509 *x, size_t chainidx);
208 int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
209                             size_t chainidx);
210 #ifndef OPENSSL_NO_OCSP
211 int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
212                                   X509 *x, size_t chainidx);
213 #endif
214 #ifndef OPENSSL_NO_NEXTPROTONEG
215 int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
216                        size_t chainidx);
217 #endif
218 int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
219                         size_t chainidx);
220 #ifndef OPENSSL_NO_SRTP
221 int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
222                             size_t chainidx);
223 #endif
224 int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
225                        size_t chainidx);
226 int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
227                              size_t chainidx);
228 int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
229                        size_t chainidx);
230 int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
231                                  X509 *x, size_t chainidx);
232 int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
233                        size_t chainidx);
234
235 EXT_RETURN tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt,
236                                           unsigned int context, X509 *x,
237                                           size_t chainidx);
238 EXT_RETURN tls_construct_stoc_server_name(SSL *s, WPACKET *pkt,
239                                           unsigned int context, X509 *x,
240                                           size_t chainidx);
241 EXT_RETURN tls_construct_stoc_early_data(SSL *s, WPACKET *pkt,
242                                          unsigned int context, X509 *x,
243                                          size_t chainidx);
244 EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL *s, WPACKET *pkt,
245                                              unsigned int context, X509 *x,
246                                              size_t chainidx);
247 #ifndef OPENSSL_NO_EC
248 EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt,
249                                             unsigned int context, X509 *x,
250                                             size_t chainidx);
251 #endif
252 EXT_RETURN tls_construct_stoc_supported_groups(SSL *s, WPACKET *pkt,
253                                                unsigned int context, X509 *x,
254                                                size_t chainidx);
255 EXT_RETURN tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
256                                              unsigned int context, X509 *x,
257                                              size_t chainidx);
258 #ifndef OPENSSL_NO_OCSP
259 EXT_RETURN tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
260                                              unsigned int context, X509 *x,
261                                              size_t chainidx);
262 #endif
263 #ifndef OPENSSL_NO_NEXTPROTONEG
264 EXT_RETURN tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
265                                              unsigned int context, X509 *x,
266                                              size_t chainidx);
267 #endif
268 EXT_RETURN tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context,
269                                    X509 *x, size_t chainidx);
270 #ifndef OPENSSL_NO_SRTP
271 EXT_RETURN tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
272                                 X509 *x, size_t chainidx);
273 #endif
274 EXT_RETURN tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context,
275                                   X509 *x, size_t chainidx);
276 EXT_RETURN tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context,
277                                   X509 *x, size_t chainidx);
278 EXT_RETURN tls_construct_stoc_supported_versions(SSL *s, WPACKET *pkt,
279                                                  unsigned int context, X509 *x,
280                                                  size_t chainidx);
281 EXT_RETURN tls_construct_stoc_key_share(SSL *s, WPACKET *pkt,
282                                         unsigned int context, X509 *x,
283                                         size_t chainidx);
284 /*
285  * Not in public headers as this is not an official extension. Only used when
286  * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set.
287  */
288 #define TLSEXT_TYPE_cryptopro_bug      0xfde8
289 EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt,
290                                             unsigned int context, X509 *x,
291                                             size_t chainidx);
292 EXT_RETURN tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context,
293                                   X509 *x, size_t chainidx);
294
295 /* Client Extension processing */
296 EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
297                                    X509 *x, size_t chainidx);
298 EXT_RETURN tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
299                                    X509 *x, size_t chainidx);
300 EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL *s, WPACKET *pkt, unsigned int context,
301                                              X509 *x, size_t chainidx);
302 #ifndef OPENSSL_NO_SRP
303 EXT_RETURN tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
304                            size_t chainidx);
305 #endif
306 #ifndef OPENSSL_NO_EC
307 EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt,
308                                             unsigned int context, X509 *x,
309                                             size_t chainidx);
310 EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
311                                                unsigned int context, X509 *x,
312                                                size_t chainidx);
313 #endif
314 EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt,
315                                          unsigned int context, X509 *x,
316                                          size_t chainidx);
317 EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
318                                              unsigned int context, X509 *x,
319                                              size_t chainidx);
320 EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt,
321                                        unsigned int context, X509 *x,
322                                        size_t chainidx);
323 #ifndef OPENSSL_NO_OCSP
324 EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
325                                              unsigned int context, X509 *x,
326                                              size_t chainidx);
327 #endif
328 #ifndef OPENSSL_NO_NEXTPROTONEG
329 EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context,
330                                   X509 *x, size_t chainidx);
331 #endif
332 EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context,
333                                    X509 *x, size_t chainidx);
334 #ifndef OPENSSL_NO_SRTP
335 EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
336                                        X509 *x, size_t chainidx);
337 #endif
338 EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context,
339                                   X509 *x, size_t chainidx);
340 #ifndef OPENSSL_NO_CT
341 EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context,
342                                   X509 *x, size_t chainidx);
343 #endif
344 EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context,
345                                   X509 *x, size_t chainidx);
346 EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
347                                                  unsigned int context, X509 *x,
348                                                  size_t chainidx);
349 EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt,
350                                         unsigned int context, X509 *x,
351                                         size_t chainidx);
352 EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt,
353                                             unsigned int context, X509 *x,
354                                             size_t chainidx);
355 EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
356                                      X509 *x, size_t chainidx);
357 EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt,
358                                       unsigned int context, X509 *x,
359                                       size_t chainidx);
360 EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context,
361                                   X509 *x, size_t chainidx);
362 int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
363                                X509 *x, size_t chainidx);
364 int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
365                                X509 *x, size_t chainidx);
366 int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
367                               X509 *x, size_t chainidx);
368 int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context,
369                                   X509 *x, size_t chainidx);
370 #ifndef OPENSSL_NO_EC
371 int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
372                                  X509 *x, size_t chainidx);
373 #endif
374 int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
375                                   X509 *x, size_t chainidx);
376 #ifndef OPENSSL_NO_OCSP
377 int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
378                                   X509 *x, size_t chainidx);
379 #endif
380 #ifndef OPENSSL_NO_CT
381 int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
382                        size_t chainidx);
383 #endif
384 #ifndef OPENSSL_NO_NEXTPROTONEG
385 int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
386                        size_t chainidx);
387 #endif
388 int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
389                         size_t chainidx);
390 #ifndef OPENSSL_NO_SRTP
391 int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
392                             size_t chainidx);
393 #endif
394 int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
395                        size_t chainidx);
396 int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
397                        size_t chainidx);
398 int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context,
399                                       X509 *x, size_t chainidx);
400 int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
401                              size_t chainidx);
402 int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
403                        size_t chainidx);
404 int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
405                        size_t chainidx);
406
407 int tls_handle_alpn(SSL *s);