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