xref: /openbsd-src/lib/libssl/tls13_internal.h (revision c1a45aed656e7d5627c30c92421893a76f370ccb)
1 /* $OpenBSD: tls13_internal.h,v 1.96 2022/01/05 17:10:02 jsing Exp $ */
2 /*
3  * Copyright (c) 2018 Bob Beck <beck@openbsd.org>
4  * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
5  * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org>
6  *
7  * Permission to use, copy, modify, and/or distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
14  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
16  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
17  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #ifndef HEADER_TLS13_INTERNAL_H
21 #define HEADER_TLS13_INTERNAL_H
22 
23 #include <openssl/evp.h>
24 #include <openssl/ssl.h>
25 
26 #include "bytestring.h"
27 #include "tls_internal.h"
28 
29 __BEGIN_HIDDEN_DECLS
30 
31 #define TLS13_HS_CLIENT			1
32 #define TLS13_HS_SERVER			2
33 
34 #define TLS13_IO_SUCCESS		 1
35 #define TLS13_IO_EOF			 0
36 #define TLS13_IO_FAILURE		-1
37 #define TLS13_IO_ALERT			-2
38 #define TLS13_IO_WANT_POLLIN		-3
39 #define TLS13_IO_WANT_POLLOUT		-4
40 #define TLS13_IO_WANT_RETRY		-5 /* Retry the previous call immediately. */
41 #define TLS13_IO_USE_LEGACY		-6
42 #define TLS13_IO_RECORD_VERSION		-7
43 #define TLS13_IO_RECORD_OVERFLOW	-8
44 
45 #define TLS13_ERR_VERIFY_FAILED		16
46 #define TLS13_ERR_HRR_FAILED		17
47 #define TLS13_ERR_TRAILING_DATA		18
48 #define TLS13_ERR_NO_SHARED_CIPHER	19
49 #define TLS13_ERR_NO_CERTIFICATE	20
50 #define TLS13_ERR_NO_PEER_CERTIFICATE	21
51 
52 #define TLS13_ALERT_LEVEL_WARNING			1
53 #define TLS13_ALERT_LEVEL_FATAL				2
54 
55 #define TLS13_ALERT_CLOSE_NOTIFY			0
56 #define TLS13_ALERT_UNEXPECTED_MESSAGE			10
57 #define TLS13_ALERT_BAD_RECORD_MAC			20
58 #define TLS13_ALERT_RECORD_OVERFLOW			22
59 #define TLS13_ALERT_HANDSHAKE_FAILURE			40
60 #define TLS13_ALERT_BAD_CERTIFICATE			42
61 #define TLS13_ALERT_UNSUPPORTED_CERTIFICATE		43
62 #define TLS13_ALERT_CERTIFICATE_REVOKED			44
63 #define TLS13_ALERT_CERTIFICATE_EXPIRED			45
64 #define TLS13_ALERT_CERTIFICATE_UNKNOWN			46
65 #define TLS13_ALERT_ILLEGAL_PARAMETER			47
66 #define TLS13_ALERT_UNKNOWN_CA				48
67 #define TLS13_ALERT_ACCESS_DENIED			49
68 #define TLS13_ALERT_DECODE_ERROR			50
69 #define TLS13_ALERT_DECRYPT_ERROR			51
70 #define TLS13_ALERT_PROTOCOL_VERSION			70
71 #define TLS13_ALERT_INSUFFICIENT_SECURITY		71
72 #define TLS13_ALERT_INTERNAL_ERROR			80
73 #define TLS13_ALERT_INAPPROPRIATE_FALLBACK		86
74 #define TLS13_ALERT_USER_CANCELED			90
75 #define TLS13_ALERT_MISSING_EXTENSION			109
76 #define TLS13_ALERT_UNSUPPORTED_EXTENSION		110
77 #define TLS13_ALERT_UNRECOGNIZED_NAME			112
78 #define TLS13_ALERT_BAD_CERTIFICATE_STATUS_RESPONSE	113
79 #define TLS13_ALERT_UNKNOWN_PSK_IDENTITY		115
80 #define TLS13_ALERT_CERTIFICATE_REQUIRED		116
81 #define TLS13_ALERT_NO_APPLICATION_PROTOCOL		120
82 
83 #define TLS13_INFO_HANDSHAKE_STARTED			SSL_CB_HANDSHAKE_START
84 #define TLS13_INFO_HANDSHAKE_COMPLETED			SSL_CB_HANDSHAKE_DONE
85 #define TLS13_INFO_ACCEPT_LOOP				SSL_CB_ACCEPT_LOOP
86 #define TLS13_INFO_CONNECT_LOOP				SSL_CB_CONNECT_LOOP
87 #define TLS13_INFO_ACCEPT_EXIT				SSL_CB_ACCEPT_EXIT
88 #define TLS13_INFO_CONNECT_EXIT				SSL_CB_CONNECT_EXIT
89 
90 typedef void (*tls13_alert_cb)(uint8_t _alert_desc, void *_cb_arg);
91 typedef ssize_t (*tls13_phh_recv_cb)(void *_cb_arg, CBS *_cbs);
92 typedef void (*tls13_phh_sent_cb)(void *_cb_arg);
93 typedef void (*tls13_handshake_message_cb)(void *_cb_arg);
94 typedef void (*tls13_info_cb)(void *_cb_arg, int _state, int _ret);
95 typedef int (*tls13_ocsp_status_cb)(void *_cb_arg);
96 
97 /*
98  * Secrets.
99  */
100 struct tls13_secret {
101 	uint8_t *data;
102 	size_t len;
103 };
104 
105 /* RFC 8446 Section 7.1  Page 92 */
106 struct tls13_secrets {
107 	const EVP_MD *digest;
108 	int resumption;
109 	int init_done;
110 	int early_done;
111 	int handshake_done;
112 	int schedule_done;
113 	int insecure; /* Set by tests */
114 	struct tls13_secret zeros;
115 	struct tls13_secret empty_hash;
116 	struct tls13_secret extracted_early;
117 	struct tls13_secret binder_key;
118 	struct tls13_secret client_early_traffic;
119 	struct tls13_secret early_exporter_master;
120 	struct tls13_secret derived_early;
121 	struct tls13_secret extracted_handshake;
122 	struct tls13_secret client_handshake_traffic;
123 	struct tls13_secret server_handshake_traffic;
124 	struct tls13_secret derived_handshake;
125 	struct tls13_secret extracted_master;
126 	struct tls13_secret client_application_traffic;
127 	struct tls13_secret server_application_traffic;
128 	struct tls13_secret exporter_master;
129 	struct tls13_secret resumption_master;
130 };
131 
132 int tls13_secret_init(struct tls13_secret *secret, size_t len);
133 void tls13_secret_cleanup(struct tls13_secret *secret);
134 struct tls13_secrets *tls13_secrets_create(const EVP_MD *digest,
135     int resumption);
136 void tls13_secrets_destroy(struct tls13_secrets *secrets);
137 
138 int tls13_hkdf_expand_label(struct tls13_secret *out, const EVP_MD *digest,
139     const struct tls13_secret *secret, const char *label,
140     const struct tls13_secret *context);
141 int tls13_hkdf_expand_label_with_length(struct tls13_secret *out,
142     const EVP_MD *digest, const struct tls13_secret *secret,
143     const uint8_t *label, size_t label_len, const struct tls13_secret *context);
144 
145 int tls13_derive_secret(struct tls13_secret *out, const EVP_MD *digest,
146     const struct tls13_secret *secret, const char *label,
147     const struct tls13_secret *context);
148 int tls13_derive_secret_with_label_length(struct tls13_secret *out,
149     const EVP_MD *digest, const struct tls13_secret *secret,
150     const uint8_t *label, size_t label_len, const struct tls13_secret *context);
151 
152 int tls13_derive_early_secrets(struct tls13_secrets *secrets, uint8_t *psk,
153     size_t psk_len, const struct tls13_secret *context);
154 int tls13_derive_handshake_secrets(struct tls13_secrets *secrets,
155     const uint8_t *ecdhe, size_t ecdhe_len, const struct tls13_secret *context);
156 int tls13_derive_application_secrets(struct tls13_secrets *secrets,
157     const struct tls13_secret *context);
158 int tls13_update_client_traffic_secret(struct tls13_secrets *secrets);
159 int tls13_update_server_traffic_secret(struct tls13_secrets *secrets);
160 
161 /*
162  * Record Layer.
163  */
164 struct tls13_record_layer;
165 
166 struct tls13_record_layer_callbacks {
167 	tls_read_cb wire_read;
168 	tls_write_cb wire_write;
169 	tls_flush_cb wire_flush;
170 	tls13_alert_cb alert_recv;
171 	tls13_alert_cb alert_sent;
172 	tls13_phh_recv_cb phh_recv;
173 	tls13_phh_sent_cb phh_sent;
174 };
175 
176 struct tls13_record_layer *tls13_record_layer_new(
177     const struct tls13_record_layer_callbacks *callbacks, void *cb_arg);
178 void tls13_record_layer_free(struct tls13_record_layer *rl);
179 void tls13_record_layer_allow_ccs(struct tls13_record_layer *rl, int allow);
180 void tls13_record_layer_allow_legacy_alerts(struct tls13_record_layer *rl, int allow);
181 void tls13_record_layer_rcontent(struct tls13_record_layer *rl, CBS *cbs);
182 void tls13_record_layer_set_aead(struct tls13_record_layer *rl,
183     const EVP_AEAD *aead);
184 void tls13_record_layer_set_hash(struct tls13_record_layer *rl,
185     const EVP_MD *hash);
186 void tls13_record_layer_set_legacy_version(struct tls13_record_layer *rl,
187     uint16_t version);
188 void tls13_record_layer_set_retry_after_phh(struct tls13_record_layer *rl, int retry);
189 void tls13_record_layer_handshake_completed(struct tls13_record_layer *rl);
190 int tls13_record_layer_set_read_traffic_key(struct tls13_record_layer *rl,
191     struct tls13_secret *read_key);
192 int tls13_record_layer_set_write_traffic_key(struct tls13_record_layer *rl,
193     struct tls13_secret *write_key);
194 ssize_t tls13_record_layer_send_pending(struct tls13_record_layer *rl);
195 ssize_t tls13_record_layer_phh(struct tls13_record_layer *rl, CBS *cbs);
196 ssize_t tls13_record_layer_flush(struct tls13_record_layer *rl);
197 
198 ssize_t tls13_read_handshake_data(struct tls13_record_layer *rl, uint8_t *buf, size_t n);
199 ssize_t tls13_write_handshake_data(struct tls13_record_layer *rl, const uint8_t *buf,
200     size_t n);
201 ssize_t tls13_pending_application_data(struct tls13_record_layer *rl);
202 ssize_t tls13_peek_application_data(struct tls13_record_layer *rl, uint8_t *buf, size_t n);
203 ssize_t tls13_read_application_data(struct tls13_record_layer *rl, uint8_t *buf, size_t n);
204 ssize_t tls13_write_application_data(struct tls13_record_layer *rl, const uint8_t *buf,
205     size_t n);
206 
207 ssize_t tls13_send_alert(struct tls13_record_layer *rl, uint8_t alert_desc);
208 ssize_t tls13_send_dummy_ccs(struct tls13_record_layer *rl);
209 
210 /*
211  * Handshake Messages.
212  */
213 struct tls13_handshake_msg;
214 
215 struct tls13_handshake_msg *tls13_handshake_msg_new(void);
216 void tls13_handshake_msg_free(struct tls13_handshake_msg *msg);
217 void tls13_handshake_msg_data(struct tls13_handshake_msg *msg, CBS *cbs);
218 int tls13_handshake_msg_set_buffer(struct tls13_handshake_msg *msg, CBS *cbs);
219 uint8_t tls13_handshake_msg_type(struct tls13_handshake_msg *msg);
220 int tls13_handshake_msg_content(struct tls13_handshake_msg *msg, CBS *cbs);
221 int tls13_handshake_msg_start(struct tls13_handshake_msg *msg, CBB *body,
222     uint8_t msg_type);
223 int tls13_handshake_msg_finish(struct tls13_handshake_msg *msg);
224 int tls13_handshake_msg_recv(struct tls13_handshake_msg *msg,
225     struct tls13_record_layer *rl);
226 int tls13_handshake_msg_send(struct tls13_handshake_msg *msg,
227     struct tls13_record_layer *rl);
228 
229 struct tls13_handshake_stage {
230 	uint8_t	hs_type;
231 	uint8_t	message_number;
232 };
233 
234 struct ssl_handshake_tls13_st;
235 
236 struct tls13_error {
237 	int code;
238 	int subcode;
239 	int errnum;
240 	const char *file;
241 	int line;
242 	char *msg;
243 };
244 
245 struct tls13_ctx {
246 	struct tls13_error error;
247 
248 	SSL *ssl;
249 	struct ssl_handshake_st *hs;
250 	uint8_t	mode;
251 	struct tls13_handshake_stage handshake_stage;
252 	int handshake_started;
253 	int handshake_completed;
254 	int need_flush;
255 	int middlebox_compat;
256 	int send_dummy_ccs;
257 	int send_dummy_ccs_after;
258 
259 	int close_notify_sent;
260 	int close_notify_recv;
261 
262 	const EVP_AEAD *aead;
263 	const EVP_MD *hash;
264 
265 	struct tls13_record_layer *rl;
266 	struct tls13_handshake_msg *hs_msg;
267 	uint8_t key_update_request;
268 	uint8_t alert;
269 	int phh_count;
270 	time_t phh_last_seen;
271 
272 	tls13_handshake_message_cb handshake_message_sent_cb;
273 	tls13_handshake_message_cb handshake_message_recv_cb;
274 	tls13_info_cb info_cb;
275 	tls13_ocsp_status_cb ocsp_status_recv_cb;
276 };
277 #ifndef TLS13_PHH_LIMIT_TIME
278 #define TLS13_PHH_LIMIT_TIME 3600
279 #endif
280 #ifndef TLS13_PHH_LIMIT
281 #define TLS13_PHH_LIMIT 100
282 #endif
283 
284 struct tls13_ctx *tls13_ctx_new(int mode);
285 void tls13_ctx_free(struct tls13_ctx *ctx);
286 
287 const EVP_AEAD *tls13_cipher_aead(const SSL_CIPHER *cipher);
288 const EVP_MD *tls13_cipher_hash(const SSL_CIPHER *cipher);
289 
290 /*
291  * Legacy interfaces.
292  */
293 int tls13_use_legacy_client(struct tls13_ctx *ctx);
294 int tls13_use_legacy_server(struct tls13_ctx *ctx);
295 int tls13_legacy_accept(SSL *ssl);
296 int tls13_legacy_connect(SSL *ssl);
297 int tls13_legacy_return_code(SSL *ssl, ssize_t ret);
298 ssize_t tls13_legacy_wire_read_cb(void *buf, size_t n, void *arg);
299 ssize_t tls13_legacy_wire_write_cb(const void *buf, size_t n, void *arg);
300 ssize_t tls13_legacy_wire_flush_cb(void *arg);
301 int tls13_legacy_pending(const SSL *ssl);
302 int tls13_legacy_read_bytes(SSL *ssl, int type, unsigned char *buf, int len,
303     int peek);
304 int tls13_legacy_write_bytes(SSL *ssl, int type, const void *buf, int len);
305 int tls13_legacy_shutdown(SSL *ssl);
306 int tls13_legacy_servername_process(struct tls13_ctx *ctx, uint8_t *alert);
307 
308 /*
309  * Message Types - RFC 8446, Section B.3.
310  *
311  * Values listed as "_RESERVED" were used in previous versions of TLS and are
312  * listed here for completeness.  TLS 1.3 implementations MUST NOT send them but
313  * might receive them from older TLS implementations.
314  */
315 #define	TLS13_MT_HELLO_REQUEST_RESERVED		0
316 #define	TLS13_MT_CLIENT_HELLO			1
317 #define	TLS13_MT_SERVER_HELLO			2
318 #define	TLS13_MT_HELLO_VERIFY_REQUEST_RESERVED	3
319 #define	TLS13_MT_NEW_SESSION_TICKET		4
320 #define	TLS13_MT_END_OF_EARLY_DATA		5
321 #define	TLS13_MT_HELLO_RETRY_REQUEST_RESERVED	6
322 #define	TLS13_MT_ENCRYPTED_EXTENSIONS		8
323 #define	TLS13_MT_CERTIFICATE			11
324 #define	TLS13_MT_SERVER_KEY_EXCHANGE_RESERVED	12
325 #define	TLS13_MT_CERTIFICATE_REQUEST		13
326 #define	TLS13_MT_SERVER_HELLO_DONE_RESERVED	14
327 #define	TLS13_MT_CERTIFICATE_VERIFY		15
328 #define	TLS13_MT_CLIENT_KEY_EXCHANGE_RESERVED	16
329 #define	TLS13_MT_FINISHED			20
330 #define	TLS13_MT_CERTIFICATE_URL_RESERVED	21
331 #define	TLS13_MT_CERTIFICATE_STATUS_RESERVED	22
332 #define	TLS13_MT_SUPPLEMENTAL_DATA_RESERVED	23
333 #define	TLS13_MT_KEY_UPDATE			24
334 #define	TLS13_MT_MESSAGE_HASH			254
335 
336 int tls13_handshake_msg_record(struct tls13_ctx *ctx);
337 int tls13_handshake_perform(struct tls13_ctx *ctx);
338 
339 int tls13_client_init(struct tls13_ctx *ctx);
340 int tls13_server_init(struct tls13_ctx *ctx);
341 int tls13_client_connect(struct tls13_ctx *ctx);
342 int tls13_server_accept(struct tls13_ctx *ctx);
343 
344 int tls13_client_hello_send(struct tls13_ctx *ctx, CBB *cbb);
345 int tls13_client_hello_sent(struct tls13_ctx *ctx);
346 int tls13_client_hello_recv(struct tls13_ctx *ctx, CBS *cbs);
347 int tls13_client_hello_retry_send(struct tls13_ctx *ctx, CBB *cbb);
348 int tls13_client_hello_retry_recv(struct tls13_ctx *ctx, CBS *cbs);
349 int tls13_client_end_of_early_data_send(struct tls13_ctx *ctx, CBB *cbb);
350 int tls13_client_end_of_early_data_recv(struct tls13_ctx *ctx, CBS *cbs);
351 int tls13_client_certificate_send(struct tls13_ctx *ctx, CBB *cbb);
352 int tls13_client_certificate_recv(struct tls13_ctx *ctx, CBS *cbs);
353 int tls13_client_certificate_verify_send(struct tls13_ctx *ctx, CBB *cbb);
354 int tls13_client_certificate_verify_recv(struct tls13_ctx *ctx, CBS *cbs);
355 int tls13_client_finished_recv(struct tls13_ctx *ctx, CBS *cbs);
356 int tls13_client_finished_send(struct tls13_ctx *ctx, CBB *cbb);
357 int tls13_client_finished_sent(struct tls13_ctx *ctx);
358 int tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs);
359 int tls13_server_hello_send(struct tls13_ctx *ctx, CBB *cbb);
360 int tls13_server_hello_sent(struct tls13_ctx *ctx);
361 int tls13_server_hello_retry_request_recv(struct tls13_ctx *ctx, CBS *cbs);
362 int tls13_server_hello_retry_request_send(struct tls13_ctx *ctx, CBB *cbb);
363 int tls13_server_hello_retry_request_sent(struct tls13_ctx *ctx);
364 int tls13_server_encrypted_extensions_recv(struct tls13_ctx *ctx, CBS *cbs);
365 int tls13_server_encrypted_extensions_send(struct tls13_ctx *ctx, CBB *cbb);
366 int tls13_server_certificate_recv(struct tls13_ctx *ctx, CBS *cbs);
367 int tls13_server_certificate_send(struct tls13_ctx *ctx, CBB *cbb);
368 int tls13_server_certificate_request_recv(struct tls13_ctx *ctx, CBS *cbs);
369 int tls13_server_certificate_request_send(struct tls13_ctx *ctx, CBB *cbb);
370 int tls13_server_certificate_verify_send(struct tls13_ctx *ctx, CBB *cbb);
371 int tls13_server_certificate_verify_recv(struct tls13_ctx *ctx, CBS *cbs);
372 int tls13_server_finished_recv(struct tls13_ctx *ctx, CBS *cbs);
373 int tls13_server_finished_send(struct tls13_ctx *ctx, CBB *cbb);
374 int tls13_server_finished_sent(struct tls13_ctx *ctx);
375 
376 void tls13_error_clear(struct tls13_error *error);
377 int tls13_cert_add(struct tls13_ctx *ctx, CBB *cbb, X509 *cert,
378     int(*build_extensions)(SSL *s, uint16_t msg_type, CBB *cbb));
379 
380 int tls13_synthetic_handshake_message(struct tls13_ctx *ctx);
381 int tls13_clienthello_hash_init(struct tls13_ctx *ctx);
382 void tls13_clienthello_hash_clear(struct ssl_handshake_tls13_st *hs);
383 int tls13_clienthello_hash_update_bytes(struct tls13_ctx *ctx, void *data,
384     size_t len);
385 int tls13_clienthello_hash_update(struct tls13_ctx *ctx, CBS *cbs);
386 int tls13_clienthello_hash_finalize(struct tls13_ctx *ctx);
387 int tls13_clienthello_hash_validate(struct tls13_ctx *ctx);
388 
389 int tls13_error_set(struct tls13_error *error, int code, int subcode,
390     const char *file, int line, const char *fmt, ...);
391 int tls13_error_setx(struct tls13_error *error, int code, int subcode,
392     const char *file, int line, const char *fmt, ...);
393 
394 #define tls13_set_error(ctx, code, subcode, fmt, ...) \
395 	tls13_error_set(&(ctx)->error, (code), (subcode), __FILE__, __LINE__, \
396 	    (fmt), __VA_ARGS__)
397 #define tls13_set_errorx(ctx, code, subcode, fmt, ...) \
398 	tls13_error_setx(&(ctx)->error, (code), (subcode), __FILE__, __LINE__, \
399 	    (fmt), __VA_ARGS__)
400 
401 int tls13_exporter(struct tls13_ctx *ctx, const uint8_t *label, size_t label_len,
402     const uint8_t *context_value, size_t context_value_len, uint8_t *out,
403     size_t out_len);
404 
405 extern const uint8_t tls13_downgrade_12[8];
406 extern const uint8_t tls13_downgrade_11[8];
407 extern const uint8_t tls13_hello_retry_request_hash[32];
408 extern const uint8_t tls13_cert_verify_pad[64];
409 extern const uint8_t tls13_cert_client_verify_context[];
410 extern const uint8_t tls13_cert_server_verify_context[];
411 
412 __END_HIDDEN_DECLS
413 
414 #endif
415