2 * Copyright 2015-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 /*****************************************************************************
12 * The following definitions are PRIVATE to the state machine. They should *
13 * NOT be used outside of the state machine. *
15 *****************************************************************************/
17 /* Max message length definitions */
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 ENCRYPTED_EXTENSIONS_MAX_LENGTH 20000
23 #define SERVER_KEY_EXCH_MAX_LENGTH 102400
24 #define SERVER_HELLO_DONE_MAX_LENGTH 0
25 #define CCS_MAX_LENGTH 1
26 /* Max should actually be 36 but we are generous */
27 #define FINISHED_MAX_LENGTH 64
29 /* Extension context codes */
30 #define EXT_TLS_ONLY 0x0001
31 #define EXT_DTLS_ONLY 0x0002
32 /* Some extensions may be allowed in DTLS but we don't implement them for it */
33 #define EXT_TLS_IMPLEMENTATION_ONLY 0x0004
34 /* Most extensions are not defined for SSLv3 but EXT_TYPE_renegotiate is */
35 #define EXT_SSL3_ALLOWED 0x0008
36 #define EXT_TLS1_2_AND_BELOW_ONLY 0x0010
37 #define EXT_TLS1_3_ONLY 0x0020
38 #define EXT_CLIENT_HELLO 0x0040
39 /* Really means TLS1.2 or below */
40 #define EXT_TLS1_2_SERVER_HELLO 0x0080
41 #define EXT_TLS1_3_SERVER_HELLO 0x0100
42 #define EXT_TLS1_3_ENCRYPTED_EXTENSIONS 0x0200
43 #define EXT_TLS1_3_HELLO_RETRY_REQUEST 0x0400
44 #define EXT_TLS1_3_CERTIFICATE 0x0800
45 #define EXT_TLS1_3_NEW_SESSION_TICKET 0x1000
47 /* Message processing return codes */
49 /* Something bad happened */
51 /* We've finished reading - swap to writing */
52 MSG_PROCESS_FINISHED_READING,
54 * We've completed the main processing of this message but there is some
55 * post processing to be done.
57 MSG_PROCESS_CONTINUE_PROCESSING,
58 /* We've finished this message - read the next message */
59 MSG_PROCESS_CONTINUE_READING
62 /* Flush the write BIO */
63 int statem_flush(SSL *s);
65 typedef int (*confunc_f) (SSL *s, WPACKET *pkt);
68 * TLS/DTLS client state machine functions
70 int ossl_statem_client_read_transition(SSL *s, int mt);
71 WRITE_TRAN ossl_statem_client_write_transition(SSL *s);
72 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst);
73 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 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt);
78 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst);
81 * TLS/DTLS server state machine functions
83 int ossl_statem_server_read_transition(SSL *s, int mt);
84 WRITE_TRAN ossl_statem_server_write_transition(SSL *s);
85 WORK_STATE ossl_statem_server_pre_work(SSL *s, WORK_STATE wst);
86 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 MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL *s, PACKET *pkt);
91 WORK_STATE ossl_statem_server_post_process_message(SSL *s, WORK_STATE wst);
93 /* Functions for getting new message data */
94 __owur int tls_get_message_header(SSL *s, int *mt);
95 __owur int tls_get_message_body(SSL *s, size_t *len);
96 __owur int dtls_get_message(SSL *s, int *mt, size_t *len);
98 /* Message construction and processing functions */
99 __owur int tls_process_initial_server_flight(SSL *s, int *al);
100 __owur MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt);
101 __owur MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt);
102 __owur int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
103 __owur int dtls_construct_change_cipher_spec(SSL *s, WPACKET *pkt);
105 __owur int tls_construct_finished(SSL *s, WPACKET *pkt);
106 __owur WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst);
107 __owur WORK_STATE dtls_wait_for_dry(SSL *s);
109 int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
110 RAW_EXTENSION **res, size_t *numfound, int *ad);
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 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt);
118 __owur MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt);
119 __owur int tls_construct_client_verify(SSL *s, WPACKET *pkt);
120 __owur WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst);
121 __owur int tls_construct_client_certificate(SSL *s, WPACKET *pkt);
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);
124 __owur int tls_client_key_exchange_post_work(SSL *s);
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);
132 __owur MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt);
134 /* some server-only functions */
135 __owur MSG_PROCESS_RETURN tls_process_client_hello(SSL *s, PACKET *pkt);
136 __owur WORK_STATE tls_post_process_client_hello(SSL *s, WORK_STATE wst);
137 __owur int tls_construct_server_hello(SSL *s, WPACKET *pkt);
138 __owur int dtls_construct_hello_verify_request(SSL *s, WPACKET *pkt);
139 __owur int tls_construct_server_certificate(SSL *s, WPACKET *pkt);
140 __owur int tls_construct_server_key_exchange(SSL *s, WPACKET *pkt);
141 __owur int tls_construct_certificate_request(SSL *s, WPACKET *pkt);
142 __owur int tls_construct_server_done(SSL *s, WPACKET *pkt);
143 __owur MSG_PROCESS_RETURN tls_process_client_certificate(SSL *s, PACKET *pkt);
144 __owur MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL *s, PACKET *pkt);
145 __owur WORK_STATE tls_post_process_client_key_exchange(SSL *s, WORK_STATE wst);
146 __owur MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt);
147 #ifndef OPENSSL_NO_NEXTPROTONEG
148 __owur MSG_PROCESS_RETURN tls_process_next_proto(SSL *s, PACKET *pkt);
150 __owur int tls_construct_new_session_ticket(SSL *s, WPACKET *pkt);
152 __owur int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts,
153 size_t numexts, int *al);
154 __owur int tls_parse_extension(SSL *s, int type, int context, RAW_EXTENSION *exts,
155 size_t numexts, int *al);
156 __owur int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
159 /* Server Extension processing */
160 int tls_parse_clienthello_renegotiate(SSL *s, PACKET *pkt, int *al);
161 int tls_parse_clienthello_server_name(SSL *s, PACKET *pkt, int *al);
162 #ifndef OPENSSL_NO_SRP
163 int tls_parse_clienthello_srp(SSL *s, PACKET *pkt, int *al);
165 #ifndef OPENSSL_NO_EC
166 int tls_parse_clienthello_ec_pt_formats(SSL *s, PACKET *pkt, int *al);
167 int tls_parse_clienthello_supported_groups(SSL *s, PACKET *pkt, int *al);
169 int tls_parse_clienthello_session_ticket(SSL *s, PACKET *pkt, int *al);
170 int tls_parse_clienthello_sig_algs(SSL *s, PACKET *pkt, int *al);
171 int tls_parse_clienthello_status_request(SSL *s, PACKET *pkt, int *al);
172 #ifndef OPENSSL_NO_NEXTPROTONEG
173 int tls_parse_clienthello_npn(SSL *s, PACKET *pkt, int *al);
175 int tls_parse_clienthello_alpn(SSL *s, PACKET *pkt, int *al);
176 #ifndef OPENSSL_NO_SRTP
177 int tls_parse_clienthello_use_srtp(SSL *s, PACKET *pkt, int *al);
179 int tls_parse_clienthello_etm(SSL *s, PACKET *pkt, int *al);
180 int tls_parse_clienthello_key_share(SSL *s, PACKET *pkt, int *al);
181 int tls_parse_clienthello_ems(SSL *s, PACKET *pkt, int *al);