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