]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (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 | #include <openssl/ocsp.h> | |
11 | #include "../ssl_local.h" | |
12 | #include "internal/cryptlib.h" | |
13 | #include "statem_local.h" | |
14 | ||
15 | EXT_RETURN tls_construct_ctos_renegotiate(SSL *s, WPACKET *pkt, | |
16 | unsigned int context, X509 *x, | |
17 | size_t chainidx) | |
18 | { | |
19 | /* Add RI if renegotiating */ | |
20 | if (!s->renegotiate) | |
21 | return EXT_RETURN_NOT_SENT; | |
22 | ||
23 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) | |
24 | || !WPACKET_start_sub_packet_u16(pkt) | |
25 | || !WPACKET_sub_memcpy_u8(pkt, s->s3.previous_client_finished, | |
26 | s->s3.previous_client_finished_len) | |
27 | || !WPACKET_close(pkt)) { | |
28 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
29 | return EXT_RETURN_FAIL; | |
30 | } | |
31 | ||
32 | return EXT_RETURN_SENT; | |
33 | } | |
34 | ||
35 | EXT_RETURN tls_construct_ctos_server_name(SSL *s, WPACKET *pkt, | |
36 | unsigned int context, X509 *x, | |
37 | size_t chainidx) | |
38 | { | |
39 | if (s->ext.hostname == NULL) | |
40 | return EXT_RETURN_NOT_SENT; | |
41 | ||
42 | /* Add TLS extension servername to the Client Hello message */ | |
43 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) | |
44 | /* Sub-packet for server_name extension */ | |
45 | || !WPACKET_start_sub_packet_u16(pkt) | |
46 | /* Sub-packet for servername list (always 1 hostname)*/ | |
47 | || !WPACKET_start_sub_packet_u16(pkt) | |
48 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_NAMETYPE_host_name) | |
49 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.hostname, | |
50 | strlen(s->ext.hostname)) | |
51 | || !WPACKET_close(pkt) | |
52 | || !WPACKET_close(pkt)) { | |
53 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
54 | return EXT_RETURN_FAIL; | |
55 | } | |
56 | ||
57 | return EXT_RETURN_SENT; | |
58 | } | |
59 | ||
60 | /* Push a Max Fragment Len extension into ClientHello */ | |
61 | EXT_RETURN tls_construct_ctos_maxfragmentlen(SSL *s, WPACKET *pkt, | |
62 | unsigned int context, X509 *x, | |
63 | size_t chainidx) | |
64 | { | |
65 | if (s->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_DISABLED) | |
66 | return EXT_RETURN_NOT_SENT; | |
67 | ||
68 | /* Add Max Fragment Length extension if client enabled it. */ | |
69 | /*- | |
70 | * 4 bytes for this extension type and extension length | |
71 | * 1 byte for the Max Fragment Length code value. | |
72 | */ | |
73 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length) | |
74 | /* Sub-packet for Max Fragment Length extension (1 byte) */ | |
75 | || !WPACKET_start_sub_packet_u16(pkt) | |
76 | || !WPACKET_put_bytes_u8(pkt, s->ext.max_fragment_len_mode) | |
77 | || !WPACKET_close(pkt)) { | |
78 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
79 | return EXT_RETURN_FAIL; | |
80 | } | |
81 | ||
82 | return EXT_RETURN_SENT; | |
83 | } | |
84 | ||
85 | #ifndef OPENSSL_NO_SRP | |
86 | EXT_RETURN tls_construct_ctos_srp(SSL *s, WPACKET *pkt, unsigned int context, | |
87 | X509 *x, size_t chainidx) | |
88 | { | |
89 | /* Add SRP username if there is one */ | |
90 | if (s->srp_ctx.login == NULL) | |
91 | return EXT_RETURN_NOT_SENT; | |
92 | ||
93 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_srp) | |
94 | /* Sub-packet for SRP extension */ | |
95 | || !WPACKET_start_sub_packet_u16(pkt) | |
96 | || !WPACKET_start_sub_packet_u8(pkt) | |
97 | /* login must not be zero...internal error if so */ | |
98 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) | |
99 | || !WPACKET_memcpy(pkt, s->srp_ctx.login, | |
100 | strlen(s->srp_ctx.login)) | |
101 | || !WPACKET_close(pkt) | |
102 | || !WPACKET_close(pkt)) { | |
103 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
104 | return EXT_RETURN_FAIL; | |
105 | } | |
106 | ||
107 | return EXT_RETURN_SENT; | |
108 | } | |
109 | #endif | |
110 | ||
111 | #ifndef OPENSSL_NO_EC | |
112 | static int use_ecc(SSL *s, int min_version, int max_version) | |
113 | { | |
114 | int i, end, ret = 0; | |
115 | unsigned long alg_k, alg_a; | |
116 | STACK_OF(SSL_CIPHER) *cipher_stack = NULL; | |
117 | const uint16_t *pgroups = NULL; | |
118 | size_t num_groups, j; | |
119 | ||
120 | /* See if we support any ECC ciphersuites */ | |
121 | if (s->version == SSL3_VERSION) | |
122 | return 0; | |
123 | ||
124 | cipher_stack = SSL_get1_supported_ciphers(s); | |
125 | end = sk_SSL_CIPHER_num(cipher_stack); | |
126 | for (i = 0; i < end; i++) { | |
127 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i); | |
128 | ||
129 | alg_k = c->algorithm_mkey; | |
130 | alg_a = c->algorithm_auth; | |
131 | if ((alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) | |
132 | || (alg_a & SSL_aECDSA) | |
133 | || c->min_tls >= TLS1_3_VERSION) { | |
134 | ret = 1; | |
135 | break; | |
136 | } | |
137 | } | |
138 | sk_SSL_CIPHER_free(cipher_stack); | |
139 | if (!ret) | |
140 | return 0; | |
141 | ||
142 | /* Check we have at least one EC supported group */ | |
143 | tls1_get_supported_groups(s, &pgroups, &num_groups); | |
144 | for (j = 0; j < num_groups; j++) { | |
145 | uint16_t ctmp = pgroups[j]; | |
146 | ||
147 | if (tls_valid_group(s, ctmp, min_version, max_version) | |
148 | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) | |
149 | return 1; | |
150 | } | |
151 | ||
152 | return 0; | |
153 | } | |
154 | ||
155 | EXT_RETURN tls_construct_ctos_ec_pt_formats(SSL *s, WPACKET *pkt, | |
156 | unsigned int context, X509 *x, | |
157 | size_t chainidx) | |
158 | { | |
159 | const unsigned char *pformats; | |
160 | size_t num_formats; | |
161 | int reason, min_version, max_version; | |
162 | ||
163 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); | |
164 | if (reason != 0) { | |
165 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); | |
166 | return EXT_RETURN_FAIL; | |
167 | } | |
168 | if (!use_ecc(s, min_version, max_version)) | |
169 | return EXT_RETURN_NOT_SENT; | |
170 | ||
171 | /* Add TLS extension ECPointFormats to the ClientHello message */ | |
172 | tls1_get_formatlist(s, &pformats, &num_formats); | |
173 | ||
174 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) | |
175 | /* Sub-packet for formats extension */ | |
176 | || !WPACKET_start_sub_packet_u16(pkt) | |
177 | || !WPACKET_sub_memcpy_u8(pkt, pformats, num_formats) | |
178 | || !WPACKET_close(pkt)) { | |
179 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
180 | return EXT_RETURN_FAIL; | |
181 | } | |
182 | ||
183 | return EXT_RETURN_SENT; | |
184 | } | |
185 | #endif | |
186 | ||
187 | EXT_RETURN tls_construct_ctos_supported_groups(SSL *s, WPACKET *pkt, | |
188 | unsigned int context, X509 *x, | |
189 | size_t chainidx) | |
190 | { | |
191 | const uint16_t *pgroups = NULL; | |
192 | size_t num_groups = 0, i; | |
193 | int min_version, max_version, reason; | |
194 | ||
195 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); | |
196 | if (reason != 0) { | |
197 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); | |
198 | return EXT_RETURN_FAIL; | |
199 | } | |
200 | ||
201 | #if defined(OPENSSL_NO_EC) | |
202 | if (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION) | |
203 | return EXT_RETURN_NOT_SENT; | |
204 | #else | |
205 | if (!use_ecc(s, min_version, max_version) && max_version < TLS1_3_VERSION) | |
206 | return EXT_RETURN_NOT_SENT; | |
207 | #endif | |
208 | ||
209 | /* | |
210 | * Add TLS extension supported_groups to the ClientHello message | |
211 | */ | |
212 | tls1_get_supported_groups(s, &pgroups, &num_groups); | |
213 | ||
214 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) | |
215 | /* Sub-packet for supported_groups extension */ | |
216 | || !WPACKET_start_sub_packet_u16(pkt) | |
217 | || !WPACKET_start_sub_packet_u16(pkt) | |
218 | || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)) { | |
219 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
220 | return EXT_RETURN_FAIL; | |
221 | } | |
222 | /* Copy group ID if supported */ | |
223 | for (i = 0; i < num_groups; i++) { | |
224 | uint16_t ctmp = pgroups[i]; | |
225 | ||
226 | if (tls_valid_group(s, ctmp, min_version, max_version) | |
227 | && tls_group_allowed(s, ctmp, SSL_SECOP_CURVE_SUPPORTED)) { | |
228 | if (!WPACKET_put_bytes_u16(pkt, ctmp)) { | |
229 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
230 | return EXT_RETURN_FAIL; | |
231 | } | |
232 | } | |
233 | } | |
234 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
235 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
236 | return EXT_RETURN_FAIL; | |
237 | } | |
238 | ||
239 | return EXT_RETURN_SENT; | |
240 | } | |
241 | ||
242 | EXT_RETURN tls_construct_ctos_session_ticket(SSL *s, WPACKET *pkt, | |
243 | unsigned int context, X509 *x, | |
244 | size_t chainidx) | |
245 | { | |
246 | size_t ticklen; | |
247 | ||
248 | if (!tls_use_ticket(s)) | |
249 | return EXT_RETURN_NOT_SENT; | |
250 | ||
251 | if (!s->new_session && s->session != NULL | |
252 | && s->session->ext.tick != NULL | |
253 | && s->session->ssl_version != TLS1_3_VERSION) { | |
254 | ticklen = s->session->ext.ticklen; | |
255 | } else if (s->session && s->ext.session_ticket != NULL | |
256 | && s->ext.session_ticket->data != NULL) { | |
257 | ticklen = s->ext.session_ticket->length; | |
258 | s->session->ext.tick = OPENSSL_malloc(ticklen); | |
259 | if (s->session->ext.tick == NULL) { | |
260 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
261 | return EXT_RETURN_FAIL; | |
262 | } | |
263 | memcpy(s->session->ext.tick, | |
264 | s->ext.session_ticket->data, ticklen); | |
265 | s->session->ext.ticklen = ticklen; | |
266 | } else { | |
267 | ticklen = 0; | |
268 | } | |
269 | ||
270 | if (ticklen == 0 && s->ext.session_ticket != NULL && | |
271 | s->ext.session_ticket->data == NULL) | |
272 | return EXT_RETURN_NOT_SENT; | |
273 | ||
274 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) | |
275 | || !WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, ticklen)) { | |
276 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
277 | return EXT_RETURN_FAIL; | |
278 | } | |
279 | ||
280 | return EXT_RETURN_SENT; | |
281 | } | |
282 | ||
283 | EXT_RETURN tls_construct_ctos_sig_algs(SSL *s, WPACKET *pkt, | |
284 | unsigned int context, X509 *x, | |
285 | size_t chainidx) | |
286 | { | |
287 | size_t salglen; | |
288 | const uint16_t *salg; | |
289 | ||
290 | if (!SSL_CLIENT_USE_SIGALGS(s)) | |
291 | return EXT_RETURN_NOT_SENT; | |
292 | ||
293 | salglen = tls12_get_psigalgs(s, 1, &salg); | |
294 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signature_algorithms) | |
295 | /* Sub-packet for sig-algs extension */ | |
296 | || !WPACKET_start_sub_packet_u16(pkt) | |
297 | /* Sub-packet for the actual list */ | |
298 | || !WPACKET_start_sub_packet_u16(pkt) | |
299 | || !tls12_copy_sigalgs(s, pkt, salg, salglen) | |
300 | || !WPACKET_close(pkt) | |
301 | || !WPACKET_close(pkt)) { | |
302 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
303 | return EXT_RETURN_FAIL; | |
304 | } | |
305 | ||
306 | return EXT_RETURN_SENT; | |
307 | } | |
308 | ||
309 | #ifndef OPENSSL_NO_OCSP | |
310 | EXT_RETURN tls_construct_ctos_status_request(SSL *s, WPACKET *pkt, | |
311 | unsigned int context, X509 *x, | |
312 | size_t chainidx) | |
313 | { | |
314 | int i; | |
315 | ||
316 | /* This extension isn't defined for client Certificates */ | |
317 | if (x != NULL) | |
318 | return EXT_RETURN_NOT_SENT; | |
319 | ||
320 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) | |
321 | return EXT_RETURN_NOT_SENT; | |
322 | ||
323 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) | |
324 | /* Sub-packet for status request extension */ | |
325 | || !WPACKET_start_sub_packet_u16(pkt) | |
326 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_STATUSTYPE_ocsp) | |
327 | /* Sub-packet for the ids */ | |
328 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
329 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
330 | return EXT_RETURN_FAIL; | |
331 | } | |
332 | for (i = 0; i < sk_OCSP_RESPID_num(s->ext.ocsp.ids); i++) { | |
333 | unsigned char *idbytes; | |
334 | OCSP_RESPID *id = sk_OCSP_RESPID_value(s->ext.ocsp.ids, i); | |
335 | int idlen = i2d_OCSP_RESPID(id, NULL); | |
336 | ||
337 | if (idlen <= 0 | |
338 | /* Sub-packet for an individual id */ | |
339 | || !WPACKET_sub_allocate_bytes_u16(pkt, idlen, &idbytes) | |
340 | || i2d_OCSP_RESPID(id, &idbytes) != idlen) { | |
341 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
342 | return EXT_RETURN_FAIL; | |
343 | } | |
344 | } | |
345 | if (!WPACKET_close(pkt) | |
346 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
347 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
348 | return EXT_RETURN_FAIL; | |
349 | } | |
350 | if (s->ext.ocsp.exts) { | |
351 | unsigned char *extbytes; | |
352 | int extlen = i2d_X509_EXTENSIONS(s->ext.ocsp.exts, NULL); | |
353 | ||
354 | if (extlen < 0) { | |
355 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
356 | return EXT_RETURN_FAIL; | |
357 | } | |
358 | if (!WPACKET_allocate_bytes(pkt, extlen, &extbytes) | |
359 | || i2d_X509_EXTENSIONS(s->ext.ocsp.exts, &extbytes) | |
360 | != extlen) { | |
361 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
362 | return EXT_RETURN_FAIL; | |
363 | } | |
364 | } | |
365 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
366 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
367 | return EXT_RETURN_FAIL; | |
368 | } | |
369 | ||
370 | return EXT_RETURN_SENT; | |
371 | } | |
372 | #endif | |
373 | ||
374 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
375 | EXT_RETURN tls_construct_ctos_npn(SSL *s, WPACKET *pkt, unsigned int context, | |
376 | X509 *x, size_t chainidx) | |
377 | { | |
378 | if (s->ctx->ext.npn_select_cb == NULL || !SSL_IS_FIRST_HANDSHAKE(s)) | |
379 | return EXT_RETURN_NOT_SENT; | |
380 | ||
381 | /* | |
382 | * The client advertises an empty extension to indicate its support | |
383 | * for Next Protocol Negotiation | |
384 | */ | |
385 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) | |
386 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
387 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
388 | return EXT_RETURN_FAIL; | |
389 | } | |
390 | ||
391 | return EXT_RETURN_SENT; | |
392 | } | |
393 | #endif | |
394 | ||
395 | EXT_RETURN tls_construct_ctos_alpn(SSL *s, WPACKET *pkt, unsigned int context, | |
396 | X509 *x, size_t chainidx) | |
397 | { | |
398 | s->s3.alpn_sent = 0; | |
399 | ||
400 | if (s->ext.alpn == NULL || !SSL_IS_FIRST_HANDSHAKE(s)) | |
401 | return EXT_RETURN_NOT_SENT; | |
402 | ||
403 | if (!WPACKET_put_bytes_u16(pkt, | |
404 | TLSEXT_TYPE_application_layer_protocol_negotiation) | |
405 | /* Sub-packet ALPN extension */ | |
406 | || !WPACKET_start_sub_packet_u16(pkt) | |
407 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.alpn, s->ext.alpn_len) | |
408 | || !WPACKET_close(pkt)) { | |
409 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
410 | return EXT_RETURN_FAIL; | |
411 | } | |
412 | s->s3.alpn_sent = 1; | |
413 | ||
414 | return EXT_RETURN_SENT; | |
415 | } | |
416 | ||
417 | ||
418 | #ifndef OPENSSL_NO_SRTP | |
419 | EXT_RETURN tls_construct_ctos_use_srtp(SSL *s, WPACKET *pkt, | |
420 | unsigned int context, X509 *x, | |
421 | size_t chainidx) | |
422 | { | |
423 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt = SSL_get_srtp_profiles(s); | |
424 | int i, end; | |
425 | ||
426 | if (clnt == NULL) | |
427 | return EXT_RETURN_NOT_SENT; | |
428 | ||
429 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) | |
430 | /* Sub-packet for SRTP extension */ | |
431 | || !WPACKET_start_sub_packet_u16(pkt) | |
432 | /* Sub-packet for the protection profile list */ | |
433 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
434 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
435 | return EXT_RETURN_FAIL; | |
436 | } | |
437 | ||
438 | end = sk_SRTP_PROTECTION_PROFILE_num(clnt); | |
439 | for (i = 0; i < end; i++) { | |
440 | const SRTP_PROTECTION_PROFILE *prof = | |
441 | sk_SRTP_PROTECTION_PROFILE_value(clnt, i); | |
442 | ||
443 | if (prof == NULL || !WPACKET_put_bytes_u16(pkt, prof->id)) { | |
444 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
445 | return EXT_RETURN_FAIL; | |
446 | } | |
447 | } | |
448 | if (!WPACKET_close(pkt) | |
449 | /* Add an empty use_mki value */ | |
450 | || !WPACKET_put_bytes_u8(pkt, 0) | |
451 | || !WPACKET_close(pkt)) { | |
452 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
453 | return EXT_RETURN_FAIL; | |
454 | } | |
455 | ||
456 | return EXT_RETURN_SENT; | |
457 | } | |
458 | #endif | |
459 | ||
460 | EXT_RETURN tls_construct_ctos_etm(SSL *s, WPACKET *pkt, unsigned int context, | |
461 | X509 *x, size_t chainidx) | |
462 | { | |
463 | if (s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) | |
464 | return EXT_RETURN_NOT_SENT; | |
465 | ||
466 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) | |
467 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
468 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
469 | return EXT_RETURN_FAIL; | |
470 | } | |
471 | ||
472 | return EXT_RETURN_SENT; | |
473 | } | |
474 | ||
475 | #ifndef OPENSSL_NO_CT | |
476 | EXT_RETURN tls_construct_ctos_sct(SSL *s, WPACKET *pkt, unsigned int context, | |
477 | X509 *x, size_t chainidx) | |
478 | { | |
479 | if (s->ct_validation_callback == NULL) | |
480 | return EXT_RETURN_NOT_SENT; | |
481 | ||
482 | /* Not defined for client Certificates */ | |
483 | if (x != NULL) | |
484 | return EXT_RETURN_NOT_SENT; | |
485 | ||
486 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_signed_certificate_timestamp) | |
487 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
488 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
489 | return EXT_RETURN_FAIL; | |
490 | } | |
491 | ||
492 | return EXT_RETURN_SENT; | |
493 | } | |
494 | #endif | |
495 | ||
496 | EXT_RETURN tls_construct_ctos_ems(SSL *s, WPACKET *pkt, unsigned int context, | |
497 | X509 *x, size_t chainidx) | |
498 | { | |
499 | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) | |
500 | return EXT_RETURN_NOT_SENT; | |
501 | ||
502 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) | |
503 | || !WPACKET_put_bytes_u16(pkt, 0)) { | |
504 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
505 | return EXT_RETURN_FAIL; | |
506 | } | |
507 | ||
508 | return EXT_RETURN_SENT; | |
509 | } | |
510 | ||
511 | EXT_RETURN tls_construct_ctos_supported_versions(SSL *s, WPACKET *pkt, | |
512 | unsigned int context, X509 *x, | |
513 | size_t chainidx) | |
514 | { | |
515 | int currv, min_version, max_version, reason; | |
516 | ||
517 | reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL); | |
518 | if (reason != 0) { | |
519 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason); | |
520 | return EXT_RETURN_FAIL; | |
521 | } | |
522 | ||
523 | /* | |
524 | * Don't include this if we can't negotiate TLSv1.3. We can do a straight | |
525 | * comparison here because we will never be called in DTLS. | |
526 | */ | |
527 | if (max_version < TLS1_3_VERSION) | |
528 | return EXT_RETURN_NOT_SENT; | |
529 | ||
530 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) | |
531 | || !WPACKET_start_sub_packet_u16(pkt) | |
532 | || !WPACKET_start_sub_packet_u8(pkt)) { | |
533 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
534 | return EXT_RETURN_FAIL; | |
535 | } | |
536 | ||
537 | for (currv = max_version; currv >= min_version; currv--) { | |
538 | if (!WPACKET_put_bytes_u16(pkt, currv)) { | |
539 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
540 | return EXT_RETURN_FAIL; | |
541 | } | |
542 | } | |
543 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
544 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
545 | return EXT_RETURN_FAIL; | |
546 | } | |
547 | ||
548 | return EXT_RETURN_SENT; | |
549 | } | |
550 | ||
551 | /* | |
552 | * Construct a psk_kex_modes extension. | |
553 | */ | |
554 | EXT_RETURN tls_construct_ctos_psk_kex_modes(SSL *s, WPACKET *pkt, | |
555 | unsigned int context, X509 *x, | |
556 | size_t chainidx) | |
557 | { | |
558 | #ifndef OPENSSL_NO_TLS1_3 | |
559 | int nodhe = s->options & SSL_OP_ALLOW_NO_DHE_KEX; | |
560 | ||
561 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk_kex_modes) | |
562 | || !WPACKET_start_sub_packet_u16(pkt) | |
563 | || !WPACKET_start_sub_packet_u8(pkt) | |
564 | || !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE_DHE) | |
565 | || (nodhe && !WPACKET_put_bytes_u8(pkt, TLSEXT_KEX_MODE_KE)) | |
566 | || !WPACKET_close(pkt) | |
567 | || !WPACKET_close(pkt)) { | |
568 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
569 | return EXT_RETURN_FAIL; | |
570 | } | |
571 | ||
572 | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE_DHE; | |
573 | if (nodhe) | |
574 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; | |
575 | #endif | |
576 | ||
577 | return EXT_RETURN_SENT; | |
578 | } | |
579 | ||
580 | #ifndef OPENSSL_NO_TLS1_3 | |
581 | static int add_key_share(SSL *s, WPACKET *pkt, unsigned int curve_id) | |
582 | { | |
583 | unsigned char *encoded_point = NULL; | |
584 | EVP_PKEY *key_share_key = NULL; | |
585 | size_t encodedlen; | |
586 | ||
587 | if (s->s3.tmp.pkey != NULL) { | |
588 | if (!ossl_assert(s->hello_retry_request == SSL_HRR_PENDING)) { | |
589 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
590 | return 0; | |
591 | } | |
592 | /* | |
593 | * Could happen if we got an HRR that wasn't requesting a new key_share | |
594 | */ | |
595 | key_share_key = s->s3.tmp.pkey; | |
596 | } else { | |
597 | key_share_key = ssl_generate_pkey_group(s, curve_id); | |
598 | if (key_share_key == NULL) { | |
599 | /* SSLfatal() already called */ | |
600 | return 0; | |
601 | } | |
602 | } | |
603 | ||
604 | /* Encode the public key. */ | |
605 | encodedlen = EVP_PKEY_get1_encoded_public_key(key_share_key, | |
606 | &encoded_point); | |
607 | if (encodedlen == 0) { | |
608 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); | |
609 | goto err; | |
610 | } | |
611 | ||
612 | /* Create KeyShareEntry */ | |
613 | if (!WPACKET_put_bytes_u16(pkt, curve_id) | |
614 | || !WPACKET_sub_memcpy_u16(pkt, encoded_point, encodedlen)) { | |
615 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
616 | goto err; | |
617 | } | |
618 | ||
619 | /* | |
620 | * TODO(TLS1.3): When changing to send more than one key_share we're | |
621 | * going to need to be able to save more than one EVP_PKEY. For now | |
622 | * we reuse the existing tmp.pkey | |
623 | */ | |
624 | s->s3.tmp.pkey = key_share_key; | |
625 | s->s3.group_id = curve_id; | |
626 | OPENSSL_free(encoded_point); | |
627 | ||
628 | return 1; | |
629 | err: | |
630 | if (s->s3.tmp.pkey == NULL) | |
631 | EVP_PKEY_free(key_share_key); | |
632 | OPENSSL_free(encoded_point); | |
633 | return 0; | |
634 | } | |
635 | #endif | |
636 | ||
637 | EXT_RETURN tls_construct_ctos_key_share(SSL *s, WPACKET *pkt, | |
638 | unsigned int context, X509 *x, | |
639 | size_t chainidx) | |
640 | { | |
641 | #ifndef OPENSSL_NO_TLS1_3 | |
642 | size_t i, num_groups = 0; | |
643 | const uint16_t *pgroups = NULL; | |
644 | uint16_t curve_id = 0; | |
645 | ||
646 | /* key_share extension */ | |
647 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) | |
648 | /* Extension data sub-packet */ | |
649 | || !WPACKET_start_sub_packet_u16(pkt) | |
650 | /* KeyShare list sub-packet */ | |
651 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
652 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
653 | return EXT_RETURN_FAIL; | |
654 | } | |
655 | ||
656 | tls1_get_supported_groups(s, &pgroups, &num_groups); | |
657 | ||
658 | /* | |
659 | * TODO(TLS1.3): Make the number of key_shares sent configurable. For | |
660 | * now, just send one | |
661 | */ | |
662 | if (s->s3.group_id != 0) { | |
663 | curve_id = s->s3.group_id; | |
664 | } else { | |
665 | for (i = 0; i < num_groups; i++) { | |
666 | ||
667 | if (!tls_group_allowed(s, pgroups[i], SSL_SECOP_CURVE_SUPPORTED)) | |
668 | continue; | |
669 | ||
670 | curve_id = pgroups[i]; | |
671 | break; | |
672 | } | |
673 | } | |
674 | ||
675 | if (curve_id == 0) { | |
676 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_SUITABLE_KEY_SHARE); | |
677 | return EXT_RETURN_FAIL; | |
678 | } | |
679 | ||
680 | if (!add_key_share(s, pkt, curve_id)) { | |
681 | /* SSLfatal() already called */ | |
682 | return EXT_RETURN_FAIL; | |
683 | } | |
684 | ||
685 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { | |
686 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
687 | return EXT_RETURN_FAIL; | |
688 | } | |
689 | return EXT_RETURN_SENT; | |
690 | #else | |
691 | return EXT_RETURN_NOT_SENT; | |
692 | #endif | |
693 | } | |
694 | ||
695 | EXT_RETURN tls_construct_ctos_cookie(SSL *s, WPACKET *pkt, unsigned int context, | |
696 | X509 *x, size_t chainidx) | |
697 | { | |
698 | EXT_RETURN ret = EXT_RETURN_FAIL; | |
699 | ||
700 | /* Should only be set if we've had an HRR */ | |
701 | if (s->ext.tls13_cookie_len == 0) | |
702 | return EXT_RETURN_NOT_SENT; | |
703 | ||
704 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie) | |
705 | /* Extension data sub-packet */ | |
706 | || !WPACKET_start_sub_packet_u16(pkt) | |
707 | || !WPACKET_sub_memcpy_u16(pkt, s->ext.tls13_cookie, | |
708 | s->ext.tls13_cookie_len) | |
709 | || !WPACKET_close(pkt)) { | |
710 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
711 | goto end; | |
712 | } | |
713 | ||
714 | ret = EXT_RETURN_SENT; | |
715 | end: | |
716 | OPENSSL_free(s->ext.tls13_cookie); | |
717 | s->ext.tls13_cookie = NULL; | |
718 | s->ext.tls13_cookie_len = 0; | |
719 | ||
720 | return ret; | |
721 | } | |
722 | ||
723 | EXT_RETURN tls_construct_ctos_early_data(SSL *s, WPACKET *pkt, | |
724 | unsigned int context, X509 *x, | |
725 | size_t chainidx) | |
726 | { | |
727 | #ifndef OPENSSL_NO_PSK | |
728 | char identity[PSK_MAX_IDENTITY_LEN + 1]; | |
729 | #endif /* OPENSSL_NO_PSK */ | |
730 | const unsigned char *id = NULL; | |
731 | size_t idlen = 0; | |
732 | SSL_SESSION *psksess = NULL; | |
733 | SSL_SESSION *edsess = NULL; | |
734 | const EVP_MD *handmd = NULL; | |
735 | ||
736 | if (s->hello_retry_request == SSL_HRR_PENDING) | |
737 | handmd = ssl_handshake_md(s); | |
738 | ||
739 | if (s->psk_use_session_cb != NULL | |
740 | && (!s->psk_use_session_cb(s, handmd, &id, &idlen, &psksess) | |
741 | || (psksess != NULL | |
742 | && psksess->ssl_version != TLS1_3_VERSION))) { | |
743 | SSL_SESSION_free(psksess); | |
744 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); | |
745 | return EXT_RETURN_FAIL; | |
746 | } | |
747 | ||
748 | #ifndef OPENSSL_NO_PSK | |
749 | if (psksess == NULL && s->psk_client_callback != NULL) { | |
750 | unsigned char psk[PSK_MAX_PSK_LEN]; | |
751 | size_t psklen = 0; | |
752 | ||
753 | memset(identity, 0, sizeof(identity)); | |
754 | psklen = s->psk_client_callback(s, NULL, identity, sizeof(identity) - 1, | |
755 | psk, sizeof(psk)); | |
756 | ||
757 | if (psklen > PSK_MAX_PSK_LEN) { | |
758 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); | |
759 | return EXT_RETURN_FAIL; | |
760 | } else if (psklen > 0) { | |
761 | const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; | |
762 | const SSL_CIPHER *cipher; | |
763 | ||
764 | idlen = strlen(identity); | |
765 | if (idlen > PSK_MAX_IDENTITY_LEN) { | |
766 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
767 | return EXT_RETURN_FAIL; | |
768 | } | |
769 | id = (unsigned char *)identity; | |
770 | ||
771 | /* | |
772 | * We found a PSK using an old style callback. We don't know | |
773 | * the digest so we default to SHA256 as per the TLSv1.3 spec | |
774 | */ | |
775 | cipher = SSL_CIPHER_find(s, tls13_aes128gcmsha256_id); | |
776 | if (cipher == NULL) { | |
777 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
778 | return EXT_RETURN_FAIL; | |
779 | } | |
780 | ||
781 | psksess = SSL_SESSION_new(); | |
782 | if (psksess == NULL | |
783 | || !SSL_SESSION_set1_master_key(psksess, psk, psklen) | |
784 | || !SSL_SESSION_set_cipher(psksess, cipher) | |
785 | || !SSL_SESSION_set_protocol_version(psksess, TLS1_3_VERSION)) { | |
786 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
787 | OPENSSL_cleanse(psk, psklen); | |
788 | return EXT_RETURN_FAIL; | |
789 | } | |
790 | OPENSSL_cleanse(psk, psklen); | |
791 | } | |
792 | } | |
793 | #endif /* OPENSSL_NO_PSK */ | |
794 | ||
795 | SSL_SESSION_free(s->psksession); | |
796 | s->psksession = psksess; | |
797 | if (psksess != NULL) { | |
798 | OPENSSL_free(s->psksession_id); | |
799 | s->psksession_id = OPENSSL_memdup(id, idlen); | |
800 | if (s->psksession_id == NULL) { | |
801 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
802 | return EXT_RETURN_FAIL; | |
803 | } | |
804 | s->psksession_id_len = idlen; | |
805 | } | |
806 | ||
807 | if (s->early_data_state != SSL_EARLY_DATA_CONNECTING | |
808 | || (s->session->ext.max_early_data == 0 | |
809 | && (psksess == NULL || psksess->ext.max_early_data == 0))) { | |
810 | s->max_early_data = 0; | |
811 | return EXT_RETURN_NOT_SENT; | |
812 | } | |
813 | edsess = s->session->ext.max_early_data != 0 ? s->session : psksess; | |
814 | s->max_early_data = edsess->ext.max_early_data; | |
815 | ||
816 | if (edsess->ext.hostname != NULL) { | |
817 | if (s->ext.hostname == NULL | |
818 | || (s->ext.hostname != NULL | |
819 | && strcmp(s->ext.hostname, edsess->ext.hostname) != 0)) { | |
820 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, | |
821 | SSL_R_INCONSISTENT_EARLY_DATA_SNI); | |
822 | return EXT_RETURN_FAIL; | |
823 | } | |
824 | } | |
825 | ||
826 | if ((s->ext.alpn == NULL && edsess->ext.alpn_selected != NULL)) { | |
827 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_INCONSISTENT_EARLY_DATA_ALPN); | |
828 | return EXT_RETURN_FAIL; | |
829 | } | |
830 | ||
831 | /* | |
832 | * Verify that we are offering an ALPN protocol consistent with the early | |
833 | * data. | |
834 | */ | |
835 | if (edsess->ext.alpn_selected != NULL) { | |
836 | PACKET prots, alpnpkt; | |
837 | int found = 0; | |
838 | ||
839 | if (!PACKET_buf_init(&prots, s->ext.alpn, s->ext.alpn_len)) { | |
840 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
841 | return EXT_RETURN_FAIL; | |
842 | } | |
843 | while (PACKET_get_length_prefixed_1(&prots, &alpnpkt)) { | |
844 | if (PACKET_equal(&alpnpkt, edsess->ext.alpn_selected, | |
845 | edsess->ext.alpn_selected_len)) { | |
846 | found = 1; | |
847 | break; | |
848 | } | |
849 | } | |
850 | if (!found) { | |
851 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, | |
852 | SSL_R_INCONSISTENT_EARLY_DATA_ALPN); | |
853 | return EXT_RETURN_FAIL; | |
854 | } | |
855 | } | |
856 | ||
857 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) | |
858 | || !WPACKET_start_sub_packet_u16(pkt) | |
859 | || !WPACKET_close(pkt)) { | |
860 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
861 | return EXT_RETURN_FAIL; | |
862 | } | |
863 | ||
864 | /* | |
865 | * We set this to rejected here. Later, if the server acknowledges the | |
866 | * extension, we set it to accepted. | |
867 | */ | |
868 | s->ext.early_data = SSL_EARLY_DATA_REJECTED; | |
869 | s->ext.early_data_ok = 1; | |
870 | ||
871 | return EXT_RETURN_SENT; | |
872 | } | |
873 | ||
874 | #define F5_WORKAROUND_MIN_MSG_LEN 0xff | |
875 | #define F5_WORKAROUND_MAX_MSG_LEN 0x200 | |
876 | ||
877 | /* | |
878 | * PSK pre binder overhead = | |
879 | * 2 bytes for TLSEXT_TYPE_psk | |
880 | * 2 bytes for extension length | |
881 | * 2 bytes for identities list length | |
882 | * 2 bytes for identity length | |
883 | * 4 bytes for obfuscated_ticket_age | |
884 | * 2 bytes for binder list length | |
885 | * 1 byte for binder length | |
886 | * The above excludes the number of bytes for the identity itself and the | |
887 | * subsequent binder bytes | |
888 | */ | |
889 | #define PSK_PRE_BINDER_OVERHEAD (2 + 2 + 2 + 2 + 4 + 2 + 1) | |
890 | ||
891 | EXT_RETURN tls_construct_ctos_padding(SSL *s, WPACKET *pkt, | |
892 | unsigned int context, X509 *x, | |
893 | size_t chainidx) | |
894 | { | |
895 | unsigned char *padbytes; | |
896 | size_t hlen; | |
897 | ||
898 | if ((s->options & SSL_OP_TLSEXT_PADDING) == 0) | |
899 | return EXT_RETURN_NOT_SENT; | |
900 | ||
901 | /* | |
902 | * Add padding to workaround bugs in F5 terminators. See RFC7685. | |
903 | * This code calculates the length of all extensions added so far but | |
904 | * excludes the PSK extension (because that MUST be written last). Therefore | |
905 | * this extension MUST always appear second to last. | |
906 | */ | |
907 | if (!WPACKET_get_total_written(pkt, &hlen)) { | |
908 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
909 | return EXT_RETURN_FAIL; | |
910 | } | |
911 | ||
912 | /* | |
913 | * If we're going to send a PSK then that will be written out after this | |
914 | * extension, so we need to calculate how long it is going to be. | |
915 | */ | |
916 | if (s->session->ssl_version == TLS1_3_VERSION | |
917 | && s->session->ext.ticklen != 0 | |
918 | && s->session->cipher != NULL) { | |
919 | const EVP_MD *md = ssl_md(s->ctx, s->session->cipher->algorithm2); | |
920 | ||
921 | if (md != NULL) { | |
922 | /* | |
923 | * Add the fixed PSK overhead, the identity length and the binder | |
924 | * length. | |
925 | */ | |
926 | hlen += PSK_PRE_BINDER_OVERHEAD + s->session->ext.ticklen | |
927 | + EVP_MD_size(md); | |
928 | } | |
929 | } | |
930 | ||
931 | if (hlen > F5_WORKAROUND_MIN_MSG_LEN && hlen < F5_WORKAROUND_MAX_MSG_LEN) { | |
932 | /* Calculate the amount of padding we need to add */ | |
933 | hlen = F5_WORKAROUND_MAX_MSG_LEN - hlen; | |
934 | ||
935 | /* | |
936 | * Take off the size of extension header itself (2 bytes for type and | |
937 | * 2 bytes for length bytes), but ensure that the extension is at least | |
938 | * 1 byte long so as not to have an empty extension last (WebSphere 7.x, | |
939 | * 8.x are intolerant of that condition) | |
940 | */ | |
941 | if (hlen > 4) | |
942 | hlen -= 4; | |
943 | else | |
944 | hlen = 1; | |
945 | ||
946 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_padding) | |
947 | || !WPACKET_sub_allocate_bytes_u16(pkt, hlen, &padbytes)) { | |
948 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
949 | return EXT_RETURN_FAIL; | |
950 | } | |
951 | memset(padbytes, 0, hlen); | |
952 | } | |
953 | ||
954 | return EXT_RETURN_SENT; | |
955 | } | |
956 | ||
957 | /* | |
958 | * Construct the pre_shared_key extension | |
959 | */ | |
960 | EXT_RETURN tls_construct_ctos_psk(SSL *s, WPACKET *pkt, unsigned int context, | |
961 | X509 *x, size_t chainidx) | |
962 | { | |
963 | #ifndef OPENSSL_NO_TLS1_3 | |
964 | uint32_t now, agesec, agems = 0; | |
965 | size_t reshashsize = 0, pskhashsize = 0, binderoffset, msglen; | |
966 | unsigned char *resbinder = NULL, *pskbinder = NULL, *msgstart = NULL; | |
967 | const EVP_MD *handmd = NULL, *mdres = NULL, *mdpsk = NULL; | |
968 | int dores = 0; | |
969 | ||
970 | s->ext.tick_identity = 0; | |
971 | ||
972 | /* | |
973 | * Note: At this stage of the code we only support adding a single | |
974 | * resumption PSK. If we add support for multiple PSKs then the length | |
975 | * calculations in the padding extension will need to be adjusted. | |
976 | */ | |
977 | ||
978 | /* | |
979 | * If this is an incompatible or new session then we have nothing to resume | |
980 | * so don't add this extension. | |
981 | */ | |
982 | if (s->session->ssl_version != TLS1_3_VERSION | |
983 | || (s->session->ext.ticklen == 0 && s->psksession == NULL)) | |
984 | return EXT_RETURN_NOT_SENT; | |
985 | ||
986 | if (s->hello_retry_request == SSL_HRR_PENDING) | |
987 | handmd = ssl_handshake_md(s); | |
988 | ||
989 | if (s->session->ext.ticklen != 0) { | |
990 | /* Get the digest associated with the ciphersuite in the session */ | |
991 | if (s->session->cipher == NULL) { | |
992 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
993 | return EXT_RETURN_FAIL; | |
994 | } | |
995 | mdres = ssl_md(s->ctx, s->session->cipher->algorithm2); | |
996 | if (mdres == NULL) { | |
997 | /* | |
998 | * Don't recognize this cipher so we can't use the session. | |
999 | * Ignore it | |
1000 | */ | |
1001 | goto dopsksess; | |
1002 | } | |
1003 | ||
1004 | if (s->hello_retry_request == SSL_HRR_PENDING && mdres != handmd) { | |
1005 | /* | |
1006 | * Selected ciphersuite hash does not match the hash for the session | |
1007 | * so we can't use it. | |
1008 | */ | |
1009 | goto dopsksess; | |
1010 | } | |
1011 | ||
1012 | /* | |
1013 | * Technically the C standard just says time() returns a time_t and says | |
1014 | * nothing about the encoding of that type. In practice most | |
1015 | * implementations follow POSIX which holds it as an integral type in | |
1016 | * seconds since epoch. We've already made the assumption that we can do | |
1017 | * this in multiple places in the code, so portability shouldn't be an | |
1018 | * issue. | |
1019 | */ | |
1020 | now = (uint32_t)time(NULL); | |
1021 | agesec = now - (uint32_t)s->session->time; | |
1022 | /* | |
1023 | * We calculate the age in seconds but the server may work in ms. Due to | |
1024 | * rounding errors we could overestimate the age by up to 1s. It is | |
1025 | * better to underestimate it. Otherwise, if the RTT is very short, when | |
1026 | * the server calculates the age reported by the client it could be | |
1027 | * bigger than the age calculated on the server - which should never | |
1028 | * happen. | |
1029 | */ | |
1030 | if (agesec > 0) | |
1031 | agesec--; | |
1032 | ||
1033 | if (s->session->ext.tick_lifetime_hint < agesec) { | |
1034 | /* Ticket is too old. Ignore it. */ | |
1035 | goto dopsksess; | |
1036 | } | |
1037 | ||
1038 | /* | |
1039 | * Calculate age in ms. We're just doing it to nearest second. Should be | |
1040 | * good enough. | |
1041 | */ | |
1042 | agems = agesec * (uint32_t)1000; | |
1043 | ||
1044 | if (agesec != 0 && agems / (uint32_t)1000 != agesec) { | |
1045 | /* | |
1046 | * Overflow. Shouldn't happen unless this is a *really* old session. | |
1047 | * If so we just ignore it. | |
1048 | */ | |
1049 | goto dopsksess; | |
1050 | } | |
1051 | ||
1052 | /* | |
1053 | * Obfuscate the age. Overflow here is fine, this addition is supposed | |
1054 | * to be mod 2^32. | |
1055 | */ | |
1056 | agems += s->session->ext.tick_age_add; | |
1057 | ||
1058 | reshashsize = EVP_MD_size(mdres); | |
1059 | s->ext.tick_identity++; | |
1060 | dores = 1; | |
1061 | } | |
1062 | ||
1063 | dopsksess: | |
1064 | if (!dores && s->psksession == NULL) | |
1065 | return EXT_RETURN_NOT_SENT; | |
1066 | ||
1067 | if (s->psksession != NULL) { | |
1068 | mdpsk = ssl_md(s->ctx, s->psksession->cipher->algorithm2); | |
1069 | if (mdpsk == NULL) { | |
1070 | /* | |
1071 | * Don't recognize this cipher so we can't use the session. | |
1072 | * If this happens it's an application bug. | |
1073 | */ | |
1074 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); | |
1075 | return EXT_RETURN_FAIL; | |
1076 | } | |
1077 | ||
1078 | if (s->hello_retry_request == SSL_HRR_PENDING && mdpsk != handmd) { | |
1079 | /* | |
1080 | * Selected ciphersuite hash does not match the hash for the PSK | |
1081 | * session. This is an application bug. | |
1082 | */ | |
1083 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK); | |
1084 | return EXT_RETURN_FAIL; | |
1085 | } | |
1086 | ||
1087 | pskhashsize = EVP_MD_size(mdpsk); | |
1088 | } | |
1089 | ||
1090 | /* Create the extension, but skip over the binder for now */ | |
1091 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) | |
1092 | || !WPACKET_start_sub_packet_u16(pkt) | |
1093 | || !WPACKET_start_sub_packet_u16(pkt)) { | |
1094 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1095 | return EXT_RETURN_FAIL; | |
1096 | } | |
1097 | ||
1098 | if (dores) { | |
1099 | if (!WPACKET_sub_memcpy_u16(pkt, s->session->ext.tick, | |
1100 | s->session->ext.ticklen) | |
1101 | || !WPACKET_put_bytes_u32(pkt, agems)) { | |
1102 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1103 | return EXT_RETURN_FAIL; | |
1104 | } | |
1105 | } | |
1106 | ||
1107 | if (s->psksession != NULL) { | |
1108 | if (!WPACKET_sub_memcpy_u16(pkt, s->psksession_id, | |
1109 | s->psksession_id_len) | |
1110 | || !WPACKET_put_bytes_u32(pkt, 0)) { | |
1111 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1112 | return EXT_RETURN_FAIL; | |
1113 | } | |
1114 | s->ext.tick_identity++; | |
1115 | } | |
1116 | ||
1117 | if (!WPACKET_close(pkt) | |
1118 | || !WPACKET_get_total_written(pkt, &binderoffset) | |
1119 | || !WPACKET_start_sub_packet_u16(pkt) | |
1120 | || (dores | |
1121 | && !WPACKET_sub_allocate_bytes_u8(pkt, reshashsize, &resbinder)) | |
1122 | || (s->psksession != NULL | |
1123 | && !WPACKET_sub_allocate_bytes_u8(pkt, pskhashsize, &pskbinder)) | |
1124 | || !WPACKET_close(pkt) | |
1125 | || !WPACKET_close(pkt) | |
1126 | || !WPACKET_get_total_written(pkt, &msglen) | |
1127 | /* | |
1128 | * We need to fill in all the sub-packet lengths now so we can | |
1129 | * calculate the HMAC of the message up to the binders | |
1130 | */ | |
1131 | || !WPACKET_fill_lengths(pkt)) { | |
1132 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1133 | return EXT_RETURN_FAIL; | |
1134 | } | |
1135 | ||
1136 | msgstart = WPACKET_get_curr(pkt) - msglen; | |
1137 | ||
1138 | if (dores | |
1139 | && tls_psk_do_binder(s, mdres, msgstart, binderoffset, NULL, | |
1140 | resbinder, s->session, 1, 0) != 1) { | |
1141 | /* SSLfatal() already called */ | |
1142 | return EXT_RETURN_FAIL; | |
1143 | } | |
1144 | ||
1145 | if (s->psksession != NULL | |
1146 | && tls_psk_do_binder(s, mdpsk, msgstart, binderoffset, NULL, | |
1147 | pskbinder, s->psksession, 1, 1) != 1) { | |
1148 | /* SSLfatal() already called */ | |
1149 | return EXT_RETURN_FAIL; | |
1150 | } | |
1151 | ||
1152 | return EXT_RETURN_SENT; | |
1153 | #else | |
1154 | return EXT_RETURN_NOT_SENT; | |
1155 | #endif | |
1156 | } | |
1157 | ||
1158 | EXT_RETURN tls_construct_ctos_post_handshake_auth(SSL *s, WPACKET *pkt, | |
1159 | ossl_unused unsigned int context, | |
1160 | ossl_unused X509 *x, | |
1161 | ossl_unused size_t chainidx) | |
1162 | { | |
1163 | #ifndef OPENSSL_NO_TLS1_3 | |
1164 | if (!s->pha_enabled) | |
1165 | return EXT_RETURN_NOT_SENT; | |
1166 | ||
1167 | /* construct extension - 0 length, no contents */ | |
1168 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_post_handshake_auth) | |
1169 | || !WPACKET_start_sub_packet_u16(pkt) | |
1170 | || !WPACKET_close(pkt)) { | |
1171 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1172 | return EXT_RETURN_FAIL; | |
1173 | } | |
1174 | ||
1175 | s->post_handshake_auth = SSL_PHA_EXT_SENT; | |
1176 | ||
1177 | return EXT_RETURN_SENT; | |
1178 | #else | |
1179 | return EXT_RETURN_NOT_SENT; | |
1180 | #endif | |
1181 | } | |
1182 | ||
1183 | ||
1184 | /* | |
1185 | * Parse the server's renegotiation binding and abort if it's not right | |
1186 | */ | |
1187 | int tls_parse_stoc_renegotiate(SSL *s, PACKET *pkt, unsigned int context, | |
1188 | X509 *x, size_t chainidx) | |
1189 | { | |
1190 | size_t expected_len = s->s3.previous_client_finished_len | |
1191 | + s->s3.previous_server_finished_len; | |
1192 | size_t ilen; | |
1193 | const unsigned char *data; | |
1194 | ||
1195 | /* Check for logic errors */ | |
1196 | if (!ossl_assert(expected_len == 0 | |
1197 | || s->s3.previous_client_finished_len != 0) | |
1198 | || !ossl_assert(expected_len == 0 | |
1199 | || s->s3.previous_server_finished_len != 0)) { | |
1200 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1201 | return 0; | |
1202 | } | |
1203 | ||
1204 | /* Parse the length byte */ | |
1205 | if (!PACKET_get_1_len(pkt, &ilen)) { | |
1206 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); | |
1207 | return 0; | |
1208 | } | |
1209 | ||
1210 | /* Consistency check */ | |
1211 | if (PACKET_remaining(pkt) != ilen) { | |
1212 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); | |
1213 | return 0; | |
1214 | } | |
1215 | ||
1216 | /* Check that the extension matches */ | |
1217 | if (ilen != expected_len) { | |
1218 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); | |
1219 | return 0; | |
1220 | } | |
1221 | ||
1222 | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_client_finished_len) | |
1223 | || memcmp(data, s->s3.previous_client_finished, | |
1224 | s->s3.previous_client_finished_len) != 0) { | |
1225 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); | |
1226 | return 0; | |
1227 | } | |
1228 | ||
1229 | if (!PACKET_get_bytes(pkt, &data, s->s3.previous_server_finished_len) | |
1230 | || memcmp(data, s->s3.previous_server_finished, | |
1231 | s->s3.previous_server_finished_len) != 0) { | |
1232 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_RENEGOTIATION_MISMATCH); | |
1233 | return 0; | |
1234 | } | |
1235 | s->s3.send_connection_binding = 1; | |
1236 | ||
1237 | return 1; | |
1238 | } | |
1239 | ||
1240 | /* Parse the server's max fragment len extension packet */ | |
1241 | int tls_parse_stoc_maxfragmentlen(SSL *s, PACKET *pkt, unsigned int context, | |
1242 | X509 *x, size_t chainidx) | |
1243 | { | |
1244 | unsigned int value; | |
1245 | ||
1246 | if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { | |
1247 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1248 | return 0; | |
1249 | } | |
1250 | ||
1251 | /* |value| should contains a valid max-fragment-length code. */ | |
1252 | if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { | |
1253 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, | |
1254 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); | |
1255 | return 0; | |
1256 | } | |
1257 | ||
1258 | /* Must be the same value as client-configured one who was sent to server */ | |
1259 | /*- | |
1260 | * RFC 6066: if a client receives a maximum fragment length negotiation | |
1261 | * response that differs from the length it requested, ... | |
1262 | * It must abort with SSL_AD_ILLEGAL_PARAMETER alert | |
1263 | */ | |
1264 | if (value != s->ext.max_fragment_len_mode) { | |
1265 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, | |
1266 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); | |
1267 | return 0; | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * Maximum Fragment Length Negotiation succeeded. | |
1272 | * The negotiated Maximum Fragment Length is binding now. | |
1273 | */ | |
1274 | s->session->ext.max_fragment_len_mode = value; | |
1275 | ||
1276 | return 1; | |
1277 | } | |
1278 | ||
1279 | int tls_parse_stoc_server_name(SSL *s, PACKET *pkt, unsigned int context, | |
1280 | X509 *x, size_t chainidx) | |
1281 | { | |
1282 | if (s->ext.hostname == NULL) { | |
1283 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1284 | return 0; | |
1285 | } | |
1286 | ||
1287 | if (PACKET_remaining(pkt) > 0) { | |
1288 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1289 | return 0; | |
1290 | } | |
1291 | ||
1292 | if (!s->hit) { | |
1293 | if (s->session->ext.hostname != NULL) { | |
1294 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1295 | return 0; | |
1296 | } | |
1297 | s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname); | |
1298 | if (s->session->ext.hostname == NULL) { | |
1299 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1300 | return 0; | |
1301 | } | |
1302 | } | |
1303 | ||
1304 | return 1; | |
1305 | } | |
1306 | ||
1307 | #ifndef OPENSSL_NO_EC | |
1308 | int tls_parse_stoc_ec_pt_formats(SSL *s, PACKET *pkt, unsigned int context, | |
1309 | X509 *x, size_t chainidx) | |
1310 | { | |
1311 | size_t ecpointformats_len; | |
1312 | PACKET ecptformatlist; | |
1313 | ||
1314 | if (!PACKET_as_length_prefixed_1(pkt, &ecptformatlist)) { | |
1315 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1316 | return 0; | |
1317 | } | |
1318 | if (!s->hit) { | |
1319 | ecpointformats_len = PACKET_remaining(&ecptformatlist); | |
1320 | if (ecpointformats_len == 0) { | |
1321 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); | |
1322 | return 0; | |
1323 | } | |
1324 | ||
1325 | s->ext.peer_ecpointformats_len = 0; | |
1326 | OPENSSL_free(s->ext.peer_ecpointformats); | |
1327 | s->ext.peer_ecpointformats = OPENSSL_malloc(ecpointformats_len); | |
1328 | if (s->ext.peer_ecpointformats == NULL) { | |
1329 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1330 | return 0; | |
1331 | } | |
1332 | ||
1333 | s->ext.peer_ecpointformats_len = ecpointformats_len; | |
1334 | ||
1335 | if (!PACKET_copy_bytes(&ecptformatlist, | |
1336 | s->ext.peer_ecpointformats, | |
1337 | ecpointformats_len)) { | |
1338 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1339 | return 0; | |
1340 | } | |
1341 | } | |
1342 | ||
1343 | return 1; | |
1344 | } | |
1345 | #endif | |
1346 | ||
1347 | int tls_parse_stoc_session_ticket(SSL *s, PACKET *pkt, unsigned int context, | |
1348 | X509 *x, size_t chainidx) | |
1349 | { | |
1350 | if (s->ext.session_ticket_cb != NULL && | |
1351 | !s->ext.session_ticket_cb(s, PACKET_data(pkt), | |
1352 | PACKET_remaining(pkt), | |
1353 | s->ext.session_ticket_cb_arg)) { | |
1354 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); | |
1355 | return 0; | |
1356 | } | |
1357 | ||
1358 | if (!tls_use_ticket(s)) { | |
1359 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); | |
1360 | return 0; | |
1361 | } | |
1362 | if (PACKET_remaining(pkt) > 0) { | |
1363 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1364 | return 0; | |
1365 | } | |
1366 | ||
1367 | s->ext.ticket_expected = 1; | |
1368 | ||
1369 | return 1; | |
1370 | } | |
1371 | ||
1372 | #ifndef OPENSSL_NO_OCSP | |
1373 | int tls_parse_stoc_status_request(SSL *s, PACKET *pkt, unsigned int context, | |
1374 | X509 *x, size_t chainidx) | |
1375 | { | |
1376 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { | |
1377 | /* We ignore this if the server sends a CertificateRequest */ | |
1378 | /* TODO(TLS1.3): Add support for this */ | |
1379 | return 1; | |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * MUST only be sent if we've requested a status | |
1384 | * request message. In TLS <= 1.2 it must also be empty. | |
1385 | */ | |
1386 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { | |
1387 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); | |
1388 | return 0; | |
1389 | } | |
1390 | if (!SSL_IS_TLS13(s) && PACKET_remaining(pkt) > 0) { | |
1391 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1392 | return 0; | |
1393 | } | |
1394 | ||
1395 | if (SSL_IS_TLS13(s)) { | |
1396 | /* We only know how to handle this if it's for the first Certificate in | |
1397 | * the chain. We ignore any other responses. | |
1398 | */ | |
1399 | if (chainidx != 0) | |
1400 | return 1; | |
1401 | ||
1402 | /* SSLfatal() already called */ | |
1403 | return tls_process_cert_status_body(s, pkt); | |
1404 | } | |
1405 | ||
1406 | /* Set flag to expect CertificateStatus message */ | |
1407 | s->ext.status_expected = 1; | |
1408 | ||
1409 | return 1; | |
1410 | } | |
1411 | #endif | |
1412 | ||
1413 | ||
1414 | #ifndef OPENSSL_NO_CT | |
1415 | int tls_parse_stoc_sct(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1416 | size_t chainidx) | |
1417 | { | |
1418 | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) { | |
1419 | /* We ignore this if the server sends it in a CertificateRequest */ | |
1420 | /* TODO(TLS1.3): Add support for this */ | |
1421 | return 1; | |
1422 | } | |
1423 | ||
1424 | /* | |
1425 | * Only take it if we asked for it - i.e if there is no CT validation | |
1426 | * callback set, then a custom extension MAY be processing it, so we | |
1427 | * need to let control continue to flow to that. | |
1428 | */ | |
1429 | if (s->ct_validation_callback != NULL) { | |
1430 | size_t size = PACKET_remaining(pkt); | |
1431 | ||
1432 | /* Simply copy it off for later processing */ | |
1433 | OPENSSL_free(s->ext.scts); | |
1434 | s->ext.scts = NULL; | |
1435 | ||
1436 | s->ext.scts_len = (uint16_t)size; | |
1437 | if (size > 0) { | |
1438 | s->ext.scts = OPENSSL_malloc(size); | |
1439 | if (s->ext.scts == NULL | |
1440 | || !PACKET_copy_bytes(pkt, s->ext.scts, size)) { | |
1441 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1442 | return 0; | |
1443 | } | |
1444 | } | |
1445 | } else { | |
1446 | ENDPOINT role = (context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0 | |
1447 | ? ENDPOINT_CLIENT : ENDPOINT_BOTH; | |
1448 | ||
1449 | /* | |
1450 | * If we didn't ask for it then there must be a custom extension, | |
1451 | * otherwise this is unsolicited. | |
1452 | */ | |
1453 | if (custom_ext_find(&s->cert->custext, role, | |
1454 | TLSEXT_TYPE_signed_certificate_timestamp, | |
1455 | NULL) == NULL) { | |
1456 | SSLfatal(s, TLS1_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); | |
1457 | return 0; | |
1458 | } | |
1459 | ||
1460 | if (!custom_ext_parse(s, context, | |
1461 | TLSEXT_TYPE_signed_certificate_timestamp, | |
1462 | PACKET_data(pkt), PACKET_remaining(pkt), | |
1463 | x, chainidx)) { | |
1464 | /* SSLfatal already called */ | |
1465 | return 0; | |
1466 | } | |
1467 | } | |
1468 | ||
1469 | return 1; | |
1470 | } | |
1471 | #endif | |
1472 | ||
1473 | ||
1474 | #ifndef OPENSSL_NO_NEXTPROTONEG | |
1475 | /* | |
1476 | * ssl_next_proto_validate validates a Next Protocol Negotiation block. No | |
1477 | * elements of zero length are allowed and the set of elements must exactly | |
1478 | * fill the length of the block. Returns 1 on success or 0 on failure. | |
1479 | */ | |
1480 | static int ssl_next_proto_validate(SSL *s, PACKET *pkt) | |
1481 | { | |
1482 | PACKET tmp_protocol; | |
1483 | ||
1484 | while (PACKET_remaining(pkt)) { | |
1485 | if (!PACKET_get_length_prefixed_1(pkt, &tmp_protocol) | |
1486 | || PACKET_remaining(&tmp_protocol) == 0) { | |
1487 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1488 | return 0; | |
1489 | } | |
1490 | } | |
1491 | ||
1492 | return 1; | |
1493 | } | |
1494 | ||
1495 | int tls_parse_stoc_npn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1496 | size_t chainidx) | |
1497 | { | |
1498 | unsigned char *selected; | |
1499 | unsigned char selected_len; | |
1500 | PACKET tmppkt; | |
1501 | ||
1502 | /* Check if we are in a renegotiation. If so ignore this extension */ | |
1503 | if (!SSL_IS_FIRST_HANDSHAKE(s)) | |
1504 | return 1; | |
1505 | ||
1506 | /* We must have requested it. */ | |
1507 | if (s->ctx->ext.npn_select_cb == NULL) { | |
1508 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); | |
1509 | return 0; | |
1510 | } | |
1511 | ||
1512 | /* The data must be valid */ | |
1513 | tmppkt = *pkt; | |
1514 | if (!ssl_next_proto_validate(s, &tmppkt)) { | |
1515 | /* SSLfatal() already called */ | |
1516 | return 0; | |
1517 | } | |
1518 | if (s->ctx->ext.npn_select_cb(s, &selected, &selected_len, | |
1519 | PACKET_data(pkt), | |
1520 | PACKET_remaining(pkt), | |
1521 | s->ctx->ext.npn_select_cb_arg) != | |
1522 | SSL_TLSEXT_ERR_OK) { | |
1523 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_EXTENSION); | |
1524 | return 0; | |
1525 | } | |
1526 | ||
1527 | /* | |
1528 | * Could be non-NULL if server has sent multiple NPN extensions in | |
1529 | * a single Serverhello | |
1530 | */ | |
1531 | OPENSSL_free(s->ext.npn); | |
1532 | s->ext.npn = OPENSSL_malloc(selected_len); | |
1533 | if (s->ext.npn == NULL) { | |
1534 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1535 | return 0; | |
1536 | } | |
1537 | ||
1538 | memcpy(s->ext.npn, selected, selected_len); | |
1539 | s->ext.npn_len = selected_len; | |
1540 | s->s3.npn_seen = 1; | |
1541 | ||
1542 | return 1; | |
1543 | } | |
1544 | #endif | |
1545 | ||
1546 | int tls_parse_stoc_alpn(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1547 | size_t chainidx) | |
1548 | { | |
1549 | size_t len; | |
1550 | ||
1551 | /* We must have requested it. */ | |
1552 | if (!s->s3.alpn_sent) { | |
1553 | SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION, SSL_R_BAD_EXTENSION); | |
1554 | return 0; | |
1555 | } | |
1556 | /*- | |
1557 | * The extension data consists of: | |
1558 | * uint16 list_length | |
1559 | * uint8 proto_length; | |
1560 | * uint8 proto[proto_length]; | |
1561 | */ | |
1562 | if (!PACKET_get_net_2_len(pkt, &len) | |
1563 | || PACKET_remaining(pkt) != len || !PACKET_get_1_len(pkt, &len) | |
1564 | || PACKET_remaining(pkt) != len) { | |
1565 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1566 | return 0; | |
1567 | } | |
1568 | OPENSSL_free(s->s3.alpn_selected); | |
1569 | s->s3.alpn_selected = OPENSSL_malloc(len); | |
1570 | if (s->s3.alpn_selected == NULL) { | |
1571 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1572 | return 0; | |
1573 | } | |
1574 | if (!PACKET_copy_bytes(pkt, s->s3.alpn_selected, len)) { | |
1575 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1576 | return 0; | |
1577 | } | |
1578 | s->s3.alpn_selected_len = len; | |
1579 | ||
1580 | if (s->session->ext.alpn_selected == NULL | |
1581 | || s->session->ext.alpn_selected_len != len | |
1582 | || memcmp(s->session->ext.alpn_selected, s->s3.alpn_selected, len) | |
1583 | != 0) { | |
1584 | /* ALPN not consistent with the old session so cannot use early_data */ | |
1585 | s->ext.early_data_ok = 0; | |
1586 | } | |
1587 | if (!s->hit) { | |
1588 | /* | |
1589 | * This is a new session and so alpn_selected should have been | |
1590 | * initialised to NULL. We should update it with the selected ALPN. | |
1591 | */ | |
1592 | if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { | |
1593 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1594 | return 0; | |
1595 | } | |
1596 | s->session->ext.alpn_selected = | |
1597 | OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); | |
1598 | if (s->session->ext.alpn_selected == NULL) { | |
1599 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1600 | return 0; | |
1601 | } | |
1602 | s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; | |
1603 | } | |
1604 | ||
1605 | return 1; | |
1606 | } | |
1607 | ||
1608 | #ifndef OPENSSL_NO_SRTP | |
1609 | int tls_parse_stoc_use_srtp(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1610 | size_t chainidx) | |
1611 | { | |
1612 | unsigned int id, ct, mki; | |
1613 | int i; | |
1614 | STACK_OF(SRTP_PROTECTION_PROFILE) *clnt; | |
1615 | SRTP_PROTECTION_PROFILE *prof; | |
1616 | ||
1617 | if (!PACKET_get_net_2(pkt, &ct) || ct != 2 | |
1618 | || !PACKET_get_net_2(pkt, &id) | |
1619 | || !PACKET_get_1(pkt, &mki) | |
1620 | || PACKET_remaining(pkt) != 0) { | |
1621 | SSLfatal(s, SSL_AD_DECODE_ERROR, | |
1622 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); | |
1623 | return 0; | |
1624 | } | |
1625 | ||
1626 | if (mki != 0) { | |
1627 | /* Must be no MKI, since we never offer one */ | |
1628 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRTP_MKI_VALUE); | |
1629 | return 0; | |
1630 | } | |
1631 | ||
1632 | /* Throw an error if the server gave us an unsolicited extension */ | |
1633 | clnt = SSL_get_srtp_profiles(s); | |
1634 | if (clnt == NULL) { | |
1635 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_SRTP_PROFILES); | |
1636 | return 0; | |
1637 | } | |
1638 | ||
1639 | /* | |
1640 | * Check to see if the server gave us something we support (and | |
1641 | * presumably offered) | |
1642 | */ | |
1643 | for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(clnt); i++) { | |
1644 | prof = sk_SRTP_PROTECTION_PROFILE_value(clnt, i); | |
1645 | ||
1646 | if (prof->id == id) { | |
1647 | s->srtp_profile = prof; | |
1648 | return 1; | |
1649 | } | |
1650 | } | |
1651 | ||
1652 | SSLfatal(s, SSL_AD_DECODE_ERROR, | |
1653 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); | |
1654 | return 0; | |
1655 | } | |
1656 | #endif | |
1657 | ||
1658 | int tls_parse_stoc_etm(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1659 | size_t chainidx) | |
1660 | { | |
1661 | /* Ignore if inappropriate ciphersuite */ | |
1662 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC) | |
1663 | && s->s3.tmp.new_cipher->algorithm_mac != SSL_AEAD | |
1664 | && s->s3.tmp.new_cipher->algorithm_enc != SSL_RC4) | |
1665 | s->ext.use_etm = 1; | |
1666 | ||
1667 | return 1; | |
1668 | } | |
1669 | ||
1670 | int tls_parse_stoc_ems(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1671 | size_t chainidx) | |
1672 | { | |
1673 | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) | |
1674 | return 1; | |
1675 | s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS; | |
1676 | if (!s->hit) | |
1677 | s->session->flags |= SSL_SESS_FLAG_EXTMS; | |
1678 | ||
1679 | return 1; | |
1680 | } | |
1681 | ||
1682 | int tls_parse_stoc_supported_versions(SSL *s, PACKET *pkt, unsigned int context, | |
1683 | X509 *x, size_t chainidx) | |
1684 | { | |
1685 | unsigned int version; | |
1686 | ||
1687 | if (!PACKET_get_net_2(pkt, &version) | |
1688 | || PACKET_remaining(pkt) != 0) { | |
1689 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1690 | return 0; | |
1691 | } | |
1692 | ||
1693 | /* | |
1694 | * The only protocol version we support which is valid in this extension in | |
1695 | * a ServerHello is TLSv1.3 therefore we shouldn't be getting anything else. | |
1696 | */ | |
1697 | if (version != TLS1_3_VERSION) { | |
1698 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, | |
1699 | SSL_R_BAD_PROTOCOL_VERSION_NUMBER); | |
1700 | return 0; | |
1701 | } | |
1702 | ||
1703 | /* We ignore this extension for HRRs except to sanity check it */ | |
1704 | if (context == SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) | |
1705 | return 1; | |
1706 | ||
1707 | /* We just set it here. We validate it in ssl_choose_client_version */ | |
1708 | s->version = version; | |
1709 | ||
1710 | return 1; | |
1711 | } | |
1712 | ||
1713 | int tls_parse_stoc_key_share(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1714 | size_t chainidx) | |
1715 | { | |
1716 | #ifndef OPENSSL_NO_TLS1_3 | |
1717 | unsigned int group_id; | |
1718 | PACKET encoded_pt; | |
1719 | EVP_PKEY *ckey = s->s3.tmp.pkey, *skey = NULL; | |
1720 | const TLS_GROUP_INFO *ginf = NULL; | |
1721 | ||
1722 | /* Sanity check */ | |
1723 | if (ckey == NULL || s->s3.peer_tmp != NULL) { | |
1724 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1725 | return 0; | |
1726 | } | |
1727 | ||
1728 | if (!PACKET_get_net_2(pkt, &group_id)) { | |
1729 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1730 | return 0; | |
1731 | } | |
1732 | ||
1733 | if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0) { | |
1734 | const uint16_t *pgroups = NULL; | |
1735 | size_t i, num_groups; | |
1736 | ||
1737 | if (PACKET_remaining(pkt) != 0) { | |
1738 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1739 | return 0; | |
1740 | } | |
1741 | ||
1742 | /* | |
1743 | * It is an error if the HelloRetryRequest wants a key_share that we | |
1744 | * already sent in the first ClientHello | |
1745 | */ | |
1746 | if (group_id == s->s3.group_id) { | |
1747 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); | |
1748 | return 0; | |
1749 | } | |
1750 | ||
1751 | /* Validate the selected group is one we support */ | |
1752 | tls1_get_supported_groups(s, &pgroups, &num_groups); | |
1753 | for (i = 0; i < num_groups; i++) { | |
1754 | if (group_id == pgroups[i]) | |
1755 | break; | |
1756 | } | |
1757 | if (i >= num_groups | |
1758 | || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED)) { | |
1759 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); | |
1760 | return 0; | |
1761 | } | |
1762 | ||
1763 | s->s3.group_id = group_id; | |
1764 | EVP_PKEY_free(s->s3.tmp.pkey); | |
1765 | s->s3.tmp.pkey = NULL; | |
1766 | return 1; | |
1767 | } | |
1768 | ||
1769 | if (group_id != s->s3.group_id) { | |
1770 | /* | |
1771 | * This isn't for the group that we sent in the original | |
1772 | * key_share! | |
1773 | */ | |
1774 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); | |
1775 | return 0; | |
1776 | } | |
1777 | ||
1778 | if ((ginf = tls1_group_id_lookup(s->ctx, group_id)) == NULL) { | |
1779 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); | |
1780 | return 0; | |
1781 | } | |
1782 | ||
1783 | if (!PACKET_as_length_prefixed_2(pkt, &encoded_pt) | |
1784 | || PACKET_remaining(&encoded_pt) == 0) { | |
1785 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1786 | return 0; | |
1787 | } | |
1788 | ||
1789 | if (!ginf->is_kem) { | |
1790 | /* Regular KEX */ | |
1791 | skey = EVP_PKEY_new(); | |
1792 | if (skey == NULL || EVP_PKEY_copy_parameters(skey, ckey) <= 0) { | |
1793 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); | |
1794 | return 0; | |
1795 | } | |
1796 | ||
1797 | if (EVP_PKEY_set1_encoded_public_key(skey, PACKET_data(&encoded_pt), | |
1798 | PACKET_remaining(&encoded_pt)) <= 0) { | |
1799 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT); | |
1800 | EVP_PKEY_free(skey); | |
1801 | return 0; | |
1802 | } | |
1803 | ||
1804 | if (ssl_derive(s, ckey, skey, 1) == 0) { | |
1805 | /* SSLfatal() already called */ | |
1806 | EVP_PKEY_free(skey); | |
1807 | return 0; | |
1808 | } | |
1809 | s->s3.peer_tmp = skey; | |
1810 | } else { | |
1811 | /* KEM Mode */ | |
1812 | const unsigned char *ct = PACKET_data(&encoded_pt); | |
1813 | size_t ctlen = PACKET_remaining(&encoded_pt); | |
1814 | ||
1815 | if (ssl_decapsulate(s, ckey, ct, ctlen, 1) == 0) { | |
1816 | /* SSLfatal() already called */ | |
1817 | return 0; | |
1818 | } | |
1819 | } | |
1820 | #endif | |
1821 | ||
1822 | return 1; | |
1823 | } | |
1824 | ||
1825 | int tls_parse_stoc_cookie(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1826 | size_t chainidx) | |
1827 | { | |
1828 | PACKET cookie; | |
1829 | ||
1830 | if (!PACKET_as_length_prefixed_2(pkt, &cookie) | |
1831 | || !PACKET_memdup(&cookie, &s->ext.tls13_cookie, | |
1832 | &s->ext.tls13_cookie_len)) { | |
1833 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1834 | return 0; | |
1835 | } | |
1836 | ||
1837 | return 1; | |
1838 | } | |
1839 | ||
1840 | int tls_parse_stoc_early_data(SSL *s, PACKET *pkt, unsigned int context, | |
1841 | X509 *x, size_t chainidx) | |
1842 | { | |
1843 | if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { | |
1844 | unsigned long max_early_data; | |
1845 | ||
1846 | if (!PACKET_get_net_4(pkt, &max_early_data) | |
1847 | || PACKET_remaining(pkt) != 0) { | |
1848 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_MAX_EARLY_DATA); | |
1849 | return 0; | |
1850 | } | |
1851 | ||
1852 | s->session->ext.max_early_data = max_early_data; | |
1853 | ||
1854 | return 1; | |
1855 | } | |
1856 | ||
1857 | if (PACKET_remaining(pkt) != 0) { | |
1858 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); | |
1859 | return 0; | |
1860 | } | |
1861 | ||
1862 | if (!s->ext.early_data_ok | |
1863 | || !s->hit) { | |
1864 | /* | |
1865 | * If we get here then we didn't send early data, or we didn't resume | |
1866 | * using the first identity, or the SNI/ALPN is not consistent so the | |
1867 | * server should not be accepting it. | |
1868 | */ | |
1869 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); | |
1870 | return 0; | |
1871 | } | |
1872 | ||
1873 | s->ext.early_data = SSL_EARLY_DATA_ACCEPTED; | |
1874 | ||
1875 | return 1; | |
1876 | } | |
1877 | ||
1878 | int tls_parse_stoc_psk(SSL *s, PACKET *pkt, unsigned int context, X509 *x, | |
1879 | size_t chainidx) | |
1880 | { | |
1881 | #ifndef OPENSSL_NO_TLS1_3 | |
1882 | unsigned int identity; | |
1883 | ||
1884 | if (!PACKET_get_net_2(pkt, &identity) || PACKET_remaining(pkt) != 0) { | |
1885 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); | |
1886 | return 0; | |
1887 | } | |
1888 | ||
1889 | if (identity >= (unsigned int)s->ext.tick_identity) { | |
1890 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_PSK_IDENTITY); | |
1891 | return 0; | |
1892 | } | |
1893 | ||
1894 | /* | |
1895 | * Session resumption tickets are always sent before PSK tickets. If the | |
1896 | * ticket index is 0 then it must be for a session resumption ticket if we | |
1897 | * sent two tickets, or if we didn't send a PSK ticket. | |
1898 | */ | |
1899 | if (identity == 0 && (s->psksession == NULL || s->ext.tick_identity == 2)) { | |
1900 | s->hit = 1; | |
1901 | SSL_SESSION_free(s->psksession); | |
1902 | s->psksession = NULL; | |
1903 | return 1; | |
1904 | } | |
1905 | ||
1906 | if (s->psksession == NULL) { | |
1907 | /* Should never happen */ | |
1908 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); | |
1909 | return 0; | |
1910 | } | |
1911 | ||
1912 | /* | |
1913 | * If we used the external PSK for sending early_data then s->early_secret | |
1914 | * is already set up, so don't overwrite it. Otherwise we copy the | |
1915 | * early_secret across that we generated earlier. | |
1916 | */ | |
1917 | if ((s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY | |
1918 | && s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) | |
1919 | || s->session->ext.max_early_data > 0 | |
1920 | || s->psksession->ext.max_early_data == 0) | |
1921 | memcpy(s->early_secret, s->psksession->early_secret, EVP_MAX_MD_SIZE); | |
1922 | ||
1923 | SSL_SESSION_free(s->session); | |
1924 | s->session = s->psksession; | |
1925 | s->psksession = NULL; | |
1926 | s->hit = 1; | |
1927 | /* Early data is only allowed if we used the first ticket */ | |
1928 | if (identity != 0) | |
1929 | s->ext.early_data_ok = 0; | |
1930 | #endif | |
1931 | ||
1932 | return 1; | |
1933 | } |