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