statem_locl.h 11.9 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 21
 */

/*****************************************************************************
 *                                                                           *
 * 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
#define SERVER_HELLO_MAX_LENGTH         20000
M
Matt Caswell 已提交
22
#define ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
M
Matt Caswell 已提交
23 24 25 26 27 28
#define SERVER_KEY_EXCH_MAX_LENGTH      102400
#define SERVER_HELLO_DONE_MAX_LENGTH    0
#define CCS_MAX_LENGTH                  1
/* Max should actually be 36 but we are generous */
#define FINISHED_MAX_LENGTH             64

29
/* Extension context codes */
30 31 32 33 34 35 36 37 38
#define EXT_TLS_ONLY                        0x0001
#define EXT_DTLS_ONLY                       0x0002
/* Some extensions may be allowed in DTLS but we don't implement them for it */
#define EXT_TLS_IMPLEMENTATION_ONLY         0x0004
/* Most extensions are not defined for SSLv3 but EXT_TYPE_renegotiate is */
#define EXT_SSL3_ALLOWED                    0x0008
#define EXT_TLS1_2_AND_BELOW_ONLY           0x0010
#define EXT_TLS1_3_ONLY                     0x0020
#define EXT_CLIENT_HELLO                    0x0040
39
/* Really means TLS1.2 or below */
40 41 42 43 44 45
#define EXT_TLS1_2_SERVER_HELLO             0x0080
#define EXT_TLS1_3_SERVER_HELLO             0x0100
#define EXT_TLS1_3_ENCRYPTED_EXTENSIONS     0x0200
#define EXT_TLS1_3_HELLO_RETRY_REQUEST      0x0400
#define EXT_TLS1_3_CERTIFICATE              0x0800
#define EXT_TLS1_3_NEW_SESSION_TICKET       0x1000
46

M
Matt Caswell 已提交
47
/* Message processing return codes */
M
Matt Caswell 已提交
48
typedef enum {
M
Matt Caswell 已提交
49 50 51 52 53 54 55 56 57 58 59
    /* 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 已提交
60
} MSG_PROCESS_RETURN;
M
Matt Caswell 已提交
61 62 63 64

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

65 66
typedef int (*confunc_f) (SSL *s, WPACKET *pkt);

M
Matt Caswell 已提交
67 68 69
/*
 * TLS/DTLS client state machine functions
 */
70 71 72 73
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);
74
int ossl_statem_client_construct_message(SSL *s, WPACKET *pkt,
75
                                         confunc_f *confunc, int *mt);
76
size_t ossl_statem_client_max_message_size(SSL *s);
77 78
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 已提交
79 80 81 82

/*
 * TLS/DTLS server state machine functions
 */
83 84 85 86
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);
87
int ossl_statem_server_construct_message(SSL *s, WPACKET *pkt,
88
                                         confunc_f *confunc,int *mt);
89
size_t ossl_statem_server_max_message_size(SSL *s);
90 91
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 已提交
92 93 94

/* Functions for getting new message data */
__owur int tls_get_message_header(SSL *s, int *mt);
95 96
__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 已提交
97 98

/* Message construction and processing functions */
99
__owur int tls_process_initial_server_flight(SSL *s, int *al);
M
Matt Caswell 已提交
100 101
__owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
102 103
__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 已提交
104

105
__owur int tls_construct_finished(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
106 107
__owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst);
__owur WORK_STATE dtls_wait_for_dry(SSL *s);
M
Matt Caswell 已提交
108

109 110
int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
                           RAW_EXTENSION **res, size_t *numfound, int *ad);
111

M
Matt Caswell 已提交
112
/* some client-only functions */
113
__owur int tls_construct_client_hello(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
114 115 116 117 118
__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);
__owur MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt);
__owur MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt);
119
__owur int tls_construct_client_verify(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
120
__owur WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst);
121
__owur int tls_construct_client_certificate(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
122
__owur int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey);
123
__owur int tls_construct_client_key_exchange(SSL *s, WPACKET *pkt);
M
Matt Caswell 已提交
124
__owur int tls_client_key_exchange_post_work(SSL *s);
125
__owur int tls_construct_cert_status(SSL *s, WPACKET *pkt);
E
Emilia Kasper 已提交
126
__owur MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt);
M
Matt Caswell 已提交
127
__owur MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt);
M
Matt Caswell 已提交
128
__owur int ssl3_check_cert_and_algorithm(SSL *s);
E
Emilia Kasper 已提交
129
#ifndef OPENSSL_NO_NEXTPROTONEG
130
__owur int tls_construct_next_proto(SSL *s, WPACKET *pkt);
E
Emilia Kasper 已提交
131
#endif
M
Matt Caswell 已提交
132
__owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *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

M
Matt Caswell 已提交
152 153 154
__owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
                                    size_t numexts, int *al);
__owur int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
155
                               size_t numexts, int *al);
M
Matt Caswell 已提交
156 157
__owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
                                    int *al);
158 159

/* Server Extension processing */
M
Matt Caswell 已提交
160 161
int tls_parse_client_renegotiate(SSL *s, PACKET *pkt, int *al);
int tls_parse_client_server_name(SSL *s, PACKET *pkt, int *al);
162
#ifndef OPENSSL_NO_SRP
M
Matt Caswell 已提交
163
int tls_parse_client_srp(SSL *s, PACKET *pkt, int *al);
164 165
#endif
#ifndef OPENSSL_NO_EC
M
Matt Caswell 已提交
166 167
int tls_parse_client_ec_pt_formats(SSL *s, PACKET *pkt, int *al);
int tls_parse_client_supported_groups(SSL *s, PACKET *pkt, int *al);
168
#endif
M
Matt Caswell 已提交
169 170
int tls_parse_client_session_ticket(SSL *s, PACKET *pkt, int *al);
int tls_parse_client_sig_algs(SSL *s, PACKET *pkt, int *al);
171
#ifndef OPENSSL_NO_OCSP
M
Matt Caswell 已提交
172
int tls_parse_client_status_request(SSL *s, PACKET *pkt, int *al);
173
#endif
174
#ifndef OPENSSL_NO_NEXTPROTONEG
M
Matt Caswell 已提交
175
int tls_parse_client_npn(SSL *s, PACKET *pkt, int *al);
176
#endif
M
Matt Caswell 已提交
177
int tls_parse_client_alpn(SSL *s, PACKET *pkt, int *al);
178
#ifndef OPENSSL_NO_SRTP
M
Matt Caswell 已提交
179
int tls_parse_client_use_srtp(SSL *s, PACKET *pkt, int *al);
180
#endif
M
Matt Caswell 已提交
181 182 183
int tls_parse_client_etm(SSL *s, PACKET *pkt, int *al);
int tls_parse_client_key_share(SSL *s, PACKET *pkt, int *al);
int tls_parse_client_ems(SSL *s, PACKET *pkt, int *al);
184 185 186

int tls_construct_server_renegotiate(SSL *s, WPACKET *pkt, int *al);
int tls_construct_server_server_name(SSL *s, WPACKET *pkt, int *al);
187
#ifndef OPENSSL_NO_EC
188
int tls_construct_server_ec_pt_formats(SSL *s, WPACKET *pkt, int *al);
189
#endif
190
int tls_construct_server_session_ticket(SSL *s, WPACKET *pkt, int *al);
191
#ifndef OPENSSL_NO_OCSP
192
int tls_construct_server_status_request(SSL *s, WPACKET *pkt, int *al);
193 194
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
195
int tls_construct_server_next_proto_neg(SSL *s, WPACKET *pkt, int *al);
196
#endif
197
int tls_construct_server_alpn(SSL *s, WPACKET *pkt, int *al);
198
#ifndef OPENSSL_NO_SRTP
199
int tls_construct_server_use_srtp(SSL *s, WPACKET *pkt, int *al);
200
#endif
201 202 203 204 205 206 207 208 209
int tls_construct_server_etm(SSL *s, WPACKET *pkt, int *al);
int tls_construct_server_ems(SSL *s, WPACKET *pkt, int *al);
int tls_construct_server_key_share(SSL *s, WPACKET *pkt, int *al);
/*
 * 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
int tls_construct_server_cryptopro_bug(SSL *s, WPACKET *pkt, int *al);
210 211

/* Client Extension processing */
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
int tls_construct_client_renegotiate(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_server_name(SSL *s, WPACKET *pkt, int *al);
#ifndef OPENSSL_NO_SRP
int tls_construct_client_srp(SSL *s, WPACKET *pkt, int *al);
#endif
#ifndef OPENSSL_NO_EC
int tls_construct_client_ec_pt_formats(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_supported_groups(SSL *s, WPACKET *pkt, int *al);
#endif
int tls_construct_client_session_ticket(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_sig_algs(SSL *s, WPACKET *pkt, int *al);
#ifndef OPENSSL_NO_OCSP
int tls_construct_client_status_request(SSL *s, WPACKET *pkt, int *al);
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
int tls_construct_client_npn(SSL *s, WPACKET *pkt, int *al);
#endif
int tls_construct_client_alpn(SSL *s, WPACKET *pkt, int *al);
#ifndef OPENSSL_NO_SRTP
int tls_construct_client_use_srtp(SSL *s, WPACKET *pkt, int *al);
#endif
int tls_construct_client_etm(SSL *s, WPACKET *pkt, int *al);
#ifndef OPENSSL_NO_CT
int tls_construct_client_sct(SSL *s, WPACKET *pkt, int *al);
#endif
int tls_construct_client_ems(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_supported_versions(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_key_share(SSL *s, WPACKET *pkt, int *al);
int tls_construct_client_padding(SSL *s, WPACKET *pkt, int *al);
241 242 243 244 245 246
int tls_parse_server_renegotiate(SSL *s, PACKET *pkt, int *al);
int tls_parse_server_server_name(SSL *s, PACKET *pkt, int *al);
#ifndef OPENSSL_NO_EC
int tls_parse_server_ec_pt_formats(SSL *s, PACKET *pkt, int *al);
#endif
int tls_parse_server_session_ticket(SSL *s, PACKET *pkt, int *al);
247
#ifndef OPENSSL_NO_OCSP
248
int tls_parse_server_status_request(SSL *s, PACKET *pkt, int *al);
249
#endif
250 251 252 253 254 255 256 257 258 259 260 261 262 263
#ifndef OPENSSL_NO_CT
int tls_parse_server_sct(SSL *s, PACKET *pkt, int *al);
#endif
#ifndef OPENSSL_NO_NEXTPROTONEG
int tls_parse_server_npn(SSL *s, PACKET *pkt, int *al);
#endif
int tls_parse_server_alpn(SSL *s, PACKET *pkt, int *al);
#ifndef OPENSSL_NO_SRTP
int tls_parse_server_use_srtp(SSL *s, PACKET *pkt, int *al);
#endif
int tls_parse_server_etm(SSL *s, PACKET *pkt, int *al);
int tls_parse_server_ems(SSL *s, PACKET *pkt, int *al);
int tls_parse_server_key_share(SSL *s, PACKET *pkt, int *al);
int ssl_parse_serverhello_tlsext(SSL *s, PACKET *pkt);