statem_locl.h 18.7 KB
Newer Older
R
Rich Salz 已提交
1 2
/*
 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
M
Matt Caswell 已提交
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
M
Matt Caswell 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
 */

/*****************************************************************************
 *                                                                           *
 * The following definitions are PRIVATE to the state machine. They should   *
 * NOT be used outside of the state machine.                                 *
 *                                                                           *
 *****************************************************************************/

/* Max message length definitions */

/* The spec allows for a longer length than this, but we limit it */
#define HELLO_VERIFY_REQUEST_MAX_LENGTH 258
21
#define END_OF_EARLY_DATA_MAX_LENGTH    0
M
Matt Caswell 已提交
22
#define SERVER_HELLO_MAX_LENGTH         20000
23
#define HELLO_RETRY_REQUEST_MAX_LENGTH  20000
M
Matt Caswell 已提交
24
#define ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
M
Matt Caswell 已提交
25 26
#define SERVER_KEY_EXCH_MAX_LENGTH      102400
#define SERVER_HELLO_DONE_MAX_LENGTH    0
27
#define KEY_UPDATE_MAX_LENGTH           1
M
Matt Caswell 已提交
28 29 30 31
#define CCS_MAX_LENGTH                  1
/* Max should actually be 36 but we are generous */
#define FINISHED_MAX_LENGTH             64

M
Matt Caswell 已提交
32 33 34
/* The maximum number of incoming KeyUpdate messages we will accept */
#define MAX_KEY_UPDATE_MESSAGES     32

35 36 37
/* Dummy message type */
#define SSL3_MT_DUMMY   -1

M
Matt Caswell 已提交
38
/* Message processing return codes */
M
Matt Caswell 已提交
39
typedef enum {
M
Matt Caswell 已提交
40 41 42 43 44 45 46 47 48 49 50
    /* Something bad happened */
    MSG_PROCESS_ERROR,
    /* We've finished reading - swap to writing */
    MSG_PROCESS_FINISHED_READING,
    /*
     * We've completed the main processing of this message but there is some
     * post processing to be done.
     */
    MSG_PROCESS_CONTINUE_PROCESSING,
    /* We've finished this message - read the next message */
    MSG_PROCESS_CONTINUE_READING
M
Matt Caswell 已提交
51
} MSG_PROCESS_RETURN;
M
Matt Caswell 已提交
52 53 54 55

/* Flush the write BIO */
int statem_flush(SSL *s);

56 57
typedef int (*confunc_f) (SSL *s, WPACKET *pkt);

58 59
int check_in_list(SSL *s, unsigned int group_id, const unsigned char *groups,
                  size_t num_groups, int checkallow);
60
int create_synthetic_message_hash(SSL *s);
61 62 63
int parse_ca_names(SSL *s, PACKET *pkt, int *al);
int construct_ca_names(SSL *s, WPACKET *pkt);

M
Matt Caswell 已提交
64 65 66
/*
 * TLS/DTLS client state machine functions
 */
67 68 69 70
int ossl_statem_client_read_transition(SSL *s, int mt);
WRITE_TRAN ossl_statem_client_write_transition(SSL *s);
WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst);
WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst);
71
int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
72
                                         confunc_f *confunc, int *mt);
73
size_t ossl_statem_client_max_message_size(SSL *s);
74 75
MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt);
WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst);
M
Matt Caswell 已提交
76 77 78 79

/*
 * TLS/DTLS server state machine functions
 */
80 81 82 83
int ossl_statem_server_read_transition(SSL *s, int mt);
WRITE_TRAN ossl_statem_server_write_transition(SSL *s);
WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst);
WORK_STATE ossl_statem_server_post_work(SSL *s, WORK_STATE wst);
84
int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
85
                                         confunc_f *confunc,int *mt);
86
size_t ossl_statem_server_max_message_size(SSL *s);
87 88
MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt);
WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
M
Matt Caswell 已提交
89 90 91

/* Functions for getting new message data */
__owur int tls_get_message_header(SSL *s, int *mt);
92 93
__owur int tls_get_message_body(SSL *s, size_t *len);
__owur int dtls_get_message(SSL *s, int *mt, size_t *len);
M
Matt Caswell 已提交
94 95

/* Message construction and processing functions */
96
__owur int tls_process_initial_server_flight(SSL *s, int *al);
M
Matt Caswell 已提交
97 98
__owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
99 100
__owur int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
__owur int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
101

102
__owur int tls_construct_finished(SSL *s, WPACKET *pkt);
103
__owur int tls_construct_key_update(SSL *s, WPACKET *pkt);
104
__owur MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt);
105
__owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs);
M
Matt Caswell 已提交
106
__owur WORK_STATE dtls_wait_for_dry(SSL *s);
M
Matt Caswell 已提交
107 108

/* some client-only functions */
109
__owur int tls_construct_client_hello(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
110 111 112
__owur MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt);
113
__owur int tls_process_cert_status_body(SSL *s, PACKET *pkt, int *al);
M
Matt Caswell 已提交
114 115
__owur MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt);
116
__owur int tls_construct_cert_verify(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
117
__owur WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst);
118
__owur int tls_construct_client_certificate(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
119
__owur int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
120
__owur int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
121
__owur int tls_client_key_exchange_post_work(SSL *s);
122
__owur int tls_construct_cert_status_body(SSL *s, WPACKET *pkt);
123
__owur int tls_construct_cert_status(SSL *s, WPACKET *pkt);
E
Emilia Kasper 已提交
124
__owur MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt);
M
Matt Caswell 已提交
125
__owur MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt);
M
Matt Caswell 已提交
126
__owur int ssl3_check_cert_and_algorithm(SSL *s);
E
Emilia Kasper 已提交
127
#ifndef OPENSSL_NO_NEXTPROTONEG
128
__owur int tls_construct_next_proto(SSL *s, WPACKET *pkt);
E
Emilia Kasper 已提交
129
#endif
130
__owur MSG_PROCESS_RETURN tls_process_hello_req(SSL *s, PACKET *pkt);
M
Matt Caswell 已提交
131
__owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt);
132
__owur int tls_construct_end_of_early_data(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
133 134

/* some server-only functions */
M
Matt Caswell 已提交
135 136
__owur MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt);
__owur WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst);
137 138 139 140 141 142
__owur int tls_construct_server_hello(SSL *s, WPACKET *pkt);
__owur int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt);
__owur int tls_construct_server_certificate(SSL *s, WPACKET *pkt);
__owur int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt);
__owur int tls_construct_certificate_request(SSL *s, WPACKET *pkt);
__owur int tls_construct_server_done(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
143 144 145 146
__owur MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt);
__owur WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst);
__owur MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt);
E
Emilia Kasper 已提交
147
#ifndef OPENSSL_NO_NEXTPROTONEG
M
Matt Caswell 已提交
148
__owur MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt);
E
Emilia Kasper 已提交
149
#endif
150
__owur int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt);
151
MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL *s, PACKET *pkt);
152

153 154 155

/* Extension processing */

156 157
__owur int extension_is_relevant(SSL *s, unsigned int extctx,
                                 unsigned int thisctx);
158
__owur int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
159 160
                                  RAW_EXTENSION **res, int *al, size_t *len,
                                  int init);
161
__owur int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
162
                               RAW_EXTENSION *exts,  X509 *x, size_t chainidx,
163
                               int *al);
M
Matt Caswell 已提交
164
__owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
165
                                    X509 *x, size_t chainidx, int *al, int fin);
166 167
__owur int should_add_extension(SSL *s, unsigned int extctx,
                                unsigned int thisctx, int max_version);
M
Matt Caswell 已提交
168
__owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
169
                                    X509 *x, size_t chainidx, int *al);
170

171 172 173 174 175 176
__owur int tls_psk_do_binder(SSL *s, const EVP_MD *md,
                             const unsigned char *msgstart,
                             size_t binderoffset, const unsigned char *binderin,
                             unsigned char *binderout,
                             SSL_SESSION *sess, int sign);

177
/* Server Extension processing */
178 179 180 181
int tls_parse_ctos_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al);
int tls_parse_ctos_server_name(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al);
182
#ifndef OPENSSL_NO_SRP
183 184
int tls_parse_ctos_srp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
185
#endif
M
Matt Caswell 已提交
186 187
int tls_parse_ctos_early_data(SSL *s, PACKET *pkt, unsigned int context,
                              X509 *x, size_t chainidx, int *al);
188
#ifndef OPENSSL_NO_EC
189 190 191 192
int tls_parse_ctos_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al);
int tls_parse_ctos_supported_groups(SSL *s, PACKET *pkt, unsigned int context,
                                    X509 *x, size_t chainidx, int *al);
193
#endif
194 195 196 197
int tls_parse_ctos_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
int tls_parse_ctos_sig_algs(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al);
198
#ifndef OPENSSL_NO_OCSP
199 200
int tls_parse_ctos_status_request(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
201
#endif
202
#ifndef OPENSSL_NO_NEXTPROTONEG
203 204
int tls_parse_ctos_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
205
#endif
206 207
int tls_parse_ctos_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                        size_t chainidx, int *al);
208
#ifndef OPENSSL_NO_SRTP
209 210
int tls_parse_ctos_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al);
211
#endif
212 213 214 215 216 217 218 219 220 221
int tls_parse_ctos_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
int tls_parse_ctos_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                             size_t chainidx, int *al);
int tls_parse_ctos_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
int tls_parse_ctos_psk_kex_modes(SSL *s, PACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al);
int tls_parse_ctos_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
222

223 224 225 226
int tls_construct_stoc_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
                                   X509 *x, size_t chainidx, int *al);
int tls_construct_stoc_server_name(SSL *s, WPACKET *pkt, unsigned int context,
                                   X509 *x, size_t chainidx, int *al);
227 228
int tls_construct_stoc_early_data(SSL *s, WPACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
229
#ifndef OPENSSL_NO_EC
230 231
int tls_construct_stoc_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
                                     X509 *x, size_t chainidx, int *al);
232
#endif
233 234
int tls_construct_stoc_session_ticket(SSL *s, WPACKET *pkt,
                                      unsigned int context, X509 *x,
235
                                      size_t chainidx, int *al);
236
#ifndef OPENSSL_NO_OCSP
237 238
int tls_construct_stoc_status_request(SSL *s, WPACKET *pkt,
                                      unsigned int context, X509 *x,
239
                                      size_t chainidx, int *al);
240 241
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
242 243
int tls_construct_stoc_next_proto_neg(SSL *s, WPACKET *pkt,
                                      unsigned int context, X509 *x,
244
                                      size_t chainidx, int *al);
245
#endif
246 247
int tls_construct_stoc_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al);
248
#ifndef OPENSSL_NO_SRTP
249 250
int tls_construct_stoc_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
                                X509 *x, size_t chainidx, int *al);
251
#endif
252 253 254 255 256 257
int tls_construct_stoc_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
int tls_construct_stoc_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
int tls_construct_stoc_key_share(SSL *s, WPACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al);
258 259 260 261 262
/*
 * Not in public headers as this is not an official extension. Only used when
 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set.
 */
#define TLSEXT_TYPE_cryptopro_bug      0xfde8
263 264 265 266
int tls_construct_stoc_cryptopro_bug(SSL *s, WPACKET *pkt, unsigned int context,
                                     X509 *x, size_t chainidx, int *al);
int tls_construct_stoc_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
267 268

/* Client Extension processing */
269 270 271 272
int tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, unsigned int context,
                                   X509 *x, size_t chainidx, int *al);
int tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, unsigned int context,
                                   X509 *x, size_t chainidx, int *al);
273
#ifndef OPENSSL_NO_SRP
274 275
int tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
276 277
#endif
#ifndef OPENSSL_NO_EC
278 279 280 281
int tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, unsigned int context,
                                     X509 *x, size_t chainidx, int *al);
int tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt,
                                        unsigned int context, X509 *x,
282
                                        size_t chainidx, int *al);
283
#endif
284 285
int tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
286 287
int tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt,
                                      unsigned int context, X509 *x,
288
                                      size_t chainidx, int *al);
289 290
int tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, unsigned int context,
                                X509 *x, size_t chainidx, int *al);
291
#ifndef OPENSSL_NO_OCSP
292 293
int tls_construct_ctos_status_request(SSL *s, WPACKET *pkt,
                                      unsigned int context, X509 *x,
294
                                      size_t chainidx, int *al);
295 296
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
297 298
int tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
299
#endif
300 301
int tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al);
302
#ifndef OPENSSL_NO_SRTP
303 304
int tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, unsigned int context,
                                X509 *x, size_t chainidx, int *al);
305
#endif
306 307
int tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
308
#ifndef OPENSSL_NO_CT
309 310
int tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
311
#endif
312 313 314 315
int tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
int tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt,
                                          unsigned int context, X509 *x,
316
                                          size_t chainidx, int *al);
317 318 319 320
int tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al);
int tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, unsigned int context,
                                     X509 *x, size_t chainidx, int *al);
M
Matt Caswell 已提交
321 322
int tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context,
                              X509 *x, size_t chainidx, int *al);
323 324 325 326 327 328 329 330
int tls_construct_ctos_padding(SSL *s, WPACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al);
int tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, X509 *x,
                           size_t chainidx, int *al);
int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al);
int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context,
                               X509 *x, size_t chainidx, int *al);
331 332
int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context,
                              X509 *x, size_t chainidx, int *al);
333
#ifndef OPENSSL_NO_EC
334 335
int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context,
                                 X509 *x, size_t chainidx, int *al);
336
#endif
337 338
int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
339
#ifndef OPENSSL_NO_OCSP
340 341
int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context,
                                  X509 *x, size_t chainidx, int *al);
342
#endif
343
#ifndef OPENSSL_NO_CT
344 345
int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
346 347
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
348 349
int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
350
#endif
351 352
int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                        size_t chainidx, int *al);
353
#ifndef OPENSSL_NO_SRTP
354 355
int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                            size_t chainidx, int *al);
356
#endif
357 358 359 360 361 362
int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                             size_t chainidx, int *al);
M
Matt Caswell 已提交
363 364
int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);
365 366
int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
                       size_t chainidx, int *al);