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