/src/openssl/ssl/statem/extensions_srvr.c
Line | Count | Source (jump to first uncovered line) |
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 "statem_local.h" |
13 | | #include "internal/cryptlib.h" |
14 | | #include "internal/ssl_unwrap.h" |
15 | | |
16 | 0 | #define COOKIE_STATE_FORMAT_VERSION 1 |
17 | | |
18 | | /* |
19 | | * 2 bytes for packet length, 2 bytes for format version, 2 bytes for |
20 | | * protocol version, 2 bytes for group id, 2 bytes for cipher id, 1 byte for |
21 | | * key_share present flag, 8 bytes for timestamp, 2 bytes for the hashlen, |
22 | | * EVP_MAX_MD_SIZE for transcript hash, 1 byte for app cookie length, app cookie |
23 | | * length bytes, SHA256_DIGEST_LENGTH bytes for the HMAC of the whole thing. |
24 | | */ |
25 | 0 | #define MAX_COOKIE_SIZE (2 + 2 + 2 + 2 + 2 + 1 + 8 + 2 + EVP_MAX_MD_SIZE + 1 \ |
26 | 0 | + SSL_COOKIE_LENGTH + SHA256_DIGEST_LENGTH) |
27 | | |
28 | | /* |
29 | | * Message header + 2 bytes for protocol version + number of random bytes + |
30 | | * + 1 byte for legacy session id length + number of bytes in legacy session id |
31 | | * + 2 bytes for ciphersuite + 1 byte for legacy compression |
32 | | * + 2 bytes for extension block length + 6 bytes for key_share extension |
33 | | * + 4 bytes for cookie extension header + the number of bytes in the cookie |
34 | | */ |
35 | | #define MAX_HRR_SIZE (SSL3_HM_HEADER_LENGTH + 2 + SSL3_RANDOM_SIZE + 1 \ |
36 | | + SSL_MAX_SSL_SESSION_ID_LENGTH + 2 + 1 + 2 + 6 + 4 \ |
37 | | + MAX_COOKIE_SIZE) |
38 | | |
39 | | /* |
40 | | * Parse the client's renegotiation binding and abort if it's not right |
41 | | */ |
42 | | int tls_parse_ctos_renegotiate(SSL_CONNECTION *s, PACKET *pkt, |
43 | | unsigned int context, |
44 | | X509 *x, size_t chainidx) |
45 | 787 | { |
46 | 787 | unsigned int ilen; |
47 | 787 | const unsigned char *data; |
48 | 787 | int ok; |
49 | | |
50 | | /* Parse the length byte */ |
51 | 787 | if (!PACKET_get_1(pkt, &ilen) |
52 | 787 | || !PACKET_get_bytes(pkt, &data, ilen)) { |
53 | 7 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RENEGOTIATION_ENCODING_ERR); |
54 | 7 | return 0; |
55 | 7 | } |
56 | | |
57 | | /* Check that the extension matches */ |
58 | 780 | if (ilen != s->s3.previous_client_finished_len) { |
59 | 5 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH); |
60 | 5 | return 0; |
61 | 5 | } |
62 | | |
63 | 775 | ok = memcmp(data, s->s3.previous_client_finished, |
64 | 775 | s->s3.previous_client_finished_len); |
65 | 775 | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
66 | 775 | if (ok) { |
67 | 0 | if ((data[0] ^ s->s3.previous_client_finished[0]) != 0xFF) { |
68 | 0 | ok = 0; |
69 | 0 | } |
70 | 0 | } |
71 | 775 | #endif |
72 | 775 | if (ok) { |
73 | 0 | SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_RENEGOTIATION_MISMATCH); |
74 | 0 | return 0; |
75 | 0 | } |
76 | | |
77 | 775 | s->s3.send_connection_binding = 1; |
78 | | |
79 | 775 | return 1; |
80 | 775 | } |
81 | | |
82 | | /*- |
83 | | * The servername extension is treated as follows: |
84 | | * |
85 | | * - Only the hostname type is supported with a maximum length of 255. |
86 | | * - The servername is rejected if too long or if it contains zeros, |
87 | | * in which case an fatal alert is generated. |
88 | | * - The servername field is maintained together with the session cache. |
89 | | * - When a session is resumed, the servername call back invoked in order |
90 | | * to allow the application to position itself to the right context. |
91 | | * - The servername is acknowledged if it is new for a session or when |
92 | | * it is identical to a previously used for the same session. |
93 | | * Applications can control the behaviour. They can at any time |
94 | | * set a 'desirable' servername for a new SSL object. This can be the |
95 | | * case for example with HTTPS when a Host: header field is received and |
96 | | * a renegotiation is requested. In this case, a possible servername |
97 | | * presented in the new client hello is only acknowledged if it matches |
98 | | * the value of the Host: field. |
99 | | * - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION |
100 | | * if they provide for changing an explicit servername context for the |
101 | | * session, i.e. when the session has been established with a servername |
102 | | * extension. |
103 | | * - On session reconnect, the servername extension may be absent. |
104 | | */ |
105 | | int tls_parse_ctos_server_name(SSL_CONNECTION *s, PACKET *pkt, |
106 | | unsigned int context, X509 *x, size_t chainidx) |
107 | 1.08k | { |
108 | 1.08k | unsigned int servname_type; |
109 | 1.08k | PACKET sni, hostname; |
110 | | |
111 | 1.08k | if (!PACKET_as_length_prefixed_2(pkt, &sni) |
112 | | /* ServerNameList must be at least 1 byte long. */ |
113 | 1.08k | || PACKET_remaining(&sni) == 0) { |
114 | 23 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
115 | 23 | return 0; |
116 | 23 | } |
117 | | |
118 | | /* |
119 | | * Although the intent was for server_name to be extensible, RFC 4366 |
120 | | * was not clear about it; and so OpenSSL among other implementations, |
121 | | * always and only allows a 'host_name' name types. |
122 | | * RFC 6066 corrected the mistake but adding new name types |
123 | | * is nevertheless no longer feasible, so act as if no other |
124 | | * SNI types can exist, to simplify parsing. |
125 | | * |
126 | | * Also note that the RFC permits only one SNI value per type, |
127 | | * i.e., we can only have a single hostname. |
128 | | */ |
129 | 1.06k | if (!PACKET_get_1(&sni, &servname_type) |
130 | 1.06k | || servname_type != TLSEXT_NAMETYPE_host_name |
131 | 1.06k | || !PACKET_as_length_prefixed_2(&sni, &hostname)) { |
132 | 19 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
133 | 19 | return 0; |
134 | 19 | } |
135 | | |
136 | | /* |
137 | | * In TLSv1.2 and below the SNI is associated with the session. In TLSv1.3 |
138 | | * we always use the SNI value from the handshake. |
139 | | */ |
140 | 1.04k | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
141 | 1.04k | if (PACKET_remaining(&hostname) > TLSEXT_MAXLEN_host_name) { |
142 | 0 | SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION); |
143 | 0 | return 0; |
144 | 0 | } |
145 | | |
146 | 1.04k | if (PACKET_contains_zero_byte(&hostname)) { |
147 | 7 | SSLfatal(s, SSL_AD_UNRECOGNIZED_NAME, SSL_R_BAD_EXTENSION); |
148 | 7 | return 0; |
149 | 7 | } |
150 | | |
151 | | /* |
152 | | * Store the requested SNI in the SSL as temporary storage. |
153 | | * If we accept it, it will get stored in the SSL_SESSION as well. |
154 | | */ |
155 | 1.04k | OPENSSL_free(s->ext.hostname); |
156 | 1.04k | s->ext.hostname = NULL; |
157 | 1.04k | if (!PACKET_strndup(&hostname, &s->ext.hostname)) { |
158 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
159 | 0 | return 0; |
160 | 0 | } |
161 | | |
162 | 1.04k | s->servername_done = 1; |
163 | 1.04k | } else { |
164 | | /* |
165 | | * In TLSv1.2 and below we should check if the SNI is consistent between |
166 | | * the initial handshake and the resumption. In TLSv1.3 SNI is not |
167 | | * associated with the session. |
168 | | */ |
169 | 0 | s->servername_done = (s->session->ext.hostname != NULL) |
170 | 0 | && PACKET_equal(&hostname, s->session->ext.hostname, |
171 | 0 | strlen(s->session->ext.hostname)); |
172 | 0 | } |
173 | | |
174 | 1.04k | return 1; |
175 | 1.04k | } |
176 | | |
177 | | int tls_parse_ctos_maxfragmentlen(SSL_CONNECTION *s, PACKET *pkt, |
178 | | unsigned int context, |
179 | | X509 *x, size_t chainidx) |
180 | 280 | { |
181 | 280 | unsigned int value; |
182 | | |
183 | 280 | if (PACKET_remaining(pkt) != 1 || !PACKET_get_1(pkt, &value)) { |
184 | 2 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
185 | 2 | return 0; |
186 | 2 | } |
187 | | |
188 | | /* Received |value| should be a valid max-fragment-length code. */ |
189 | 278 | if (!IS_MAX_FRAGMENT_LENGTH_EXT_VALID(value)) { |
190 | 8 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
191 | 8 | SSL_R_SSL3_EXT_INVALID_MAX_FRAGMENT_LENGTH); |
192 | 8 | return 0; |
193 | 8 | } |
194 | | |
195 | | /* |
196 | | * When doing a full handshake or a renegotiation max_fragment_len_mode will |
197 | | * be TLSEXT_max_fragment_length_UNSPECIFIED |
198 | | * |
199 | | * In case of a resumption max_fragment_len_mode will be one of |
200 | | * TLSEXT_max_fragment_length_DISABLED, TLSEXT_max_fragment_length_512, |
201 | | * TLSEXT_max_fragment_length_1024, TLSEXT_max_fragment_length_2048. |
202 | | * TLSEXT_max_fragment_length_4096 |
203 | | * |
204 | | * RFC 6066: The negotiated length applies for the duration of the session |
205 | | * including session resumptions. |
206 | | * |
207 | | * So we only set the value in case it is unspecified. |
208 | | */ |
209 | 270 | if (s->session->ext.max_fragment_len_mode == TLSEXT_max_fragment_length_UNSPECIFIED) |
210 | | /* |
211 | | * Store it in session, so it'll become binding for us |
212 | | * and we'll include it in a next Server Hello. |
213 | | */ |
214 | 270 | s->session->ext.max_fragment_len_mode = value; |
215 | | |
216 | 270 | return 1; |
217 | 278 | } |
218 | | |
219 | | #ifndef OPENSSL_NO_SRP |
220 | | int tls_parse_ctos_srp(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
221 | | X509 *x, size_t chainidx) |
222 | 9 | { |
223 | 9 | PACKET srp_I; |
224 | | |
225 | 9 | if (!PACKET_as_length_prefixed_1(pkt, &srp_I) |
226 | 9 | || PACKET_contains_zero_byte(&srp_I)) { |
227 | 4 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
228 | 4 | return 0; |
229 | 4 | } |
230 | | |
231 | 5 | if (!PACKET_strndup(&srp_I, &s->srp_ctx.login)) { |
232 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
233 | 0 | return 0; |
234 | 0 | } |
235 | | |
236 | 5 | return 1; |
237 | 5 | } |
238 | | #endif |
239 | | |
240 | | int tls_parse_ctos_ec_pt_formats(SSL_CONNECTION *s, PACKET *pkt, |
241 | | unsigned int context, |
242 | | X509 *x, size_t chainidx) |
243 | 834 | { |
244 | 834 | PACKET ec_point_format_list; |
245 | | |
246 | 834 | if (!PACKET_as_length_prefixed_1(pkt, &ec_point_format_list) |
247 | 834 | || PACKET_remaining(&ec_point_format_list) == 0) { |
248 | 13 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
249 | 13 | return 0; |
250 | 13 | } |
251 | | |
252 | 821 | if (!s->hit) { |
253 | 821 | if (!PACKET_memdup(&ec_point_format_list, |
254 | 821 | &s->ext.peer_ecpointformats, |
255 | 821 | &s->ext.peer_ecpointformats_len)) { |
256 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
257 | 0 | return 0; |
258 | 0 | } |
259 | 821 | } |
260 | | |
261 | 821 | return 1; |
262 | 821 | } |
263 | | |
264 | | int tls_parse_ctos_session_ticket(SSL_CONNECTION *s, PACKET *pkt, |
265 | | unsigned int context, |
266 | | X509 *x, size_t chainidx) |
267 | 279 | { |
268 | 279 | if (s->ext.session_ticket_cb && |
269 | 279 | !s->ext.session_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), |
270 | 0 | PACKET_data(pkt), PACKET_remaining(pkt), |
271 | 0 | s->ext.session_ticket_cb_arg)) { |
272 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
273 | 0 | return 0; |
274 | 0 | } |
275 | | |
276 | 279 | return 1; |
277 | 279 | } |
278 | | |
279 | | int tls_parse_ctos_sig_algs_cert(SSL_CONNECTION *s, PACKET *pkt, |
280 | | ossl_unused unsigned int context, |
281 | | ossl_unused X509 *x, |
282 | | ossl_unused size_t chainidx) |
283 | 72 | { |
284 | 72 | PACKET supported_sig_algs; |
285 | | |
286 | 72 | if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs) |
287 | 72 | || PACKET_remaining(&supported_sig_algs) == 0) { |
288 | 13 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
289 | 13 | return 0; |
290 | 13 | } |
291 | | |
292 | | /* |
293 | | * We use this routine on both clients and servers, and when clients |
294 | | * get asked for PHA we need to always save the sigalgs regardless |
295 | | * of whether it was a resumption or not. |
296 | | */ |
297 | 59 | if ((!s->server || (s->server && !s->hit)) |
298 | 59 | && !tls1_save_sigalgs(s, &supported_sig_algs, 1)) { |
299 | 1 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
300 | 1 | return 0; |
301 | 1 | } |
302 | | |
303 | 58 | return 1; |
304 | 59 | } |
305 | | |
306 | | int tls_parse_ctos_sig_algs(SSL_CONNECTION *s, PACKET *pkt, |
307 | | unsigned int context, X509 *x, size_t chainidx) |
308 | 804 | { |
309 | 804 | PACKET supported_sig_algs; |
310 | | |
311 | 804 | if (!PACKET_as_length_prefixed_2(pkt, &supported_sig_algs) |
312 | 804 | || PACKET_remaining(&supported_sig_algs) == 0) { |
313 | 20 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
314 | 20 | return 0; |
315 | 20 | } |
316 | | |
317 | | /* |
318 | | * We use this routine on both clients and servers, and when clients |
319 | | * get asked for PHA we need to always save the sigalgs regardless |
320 | | * of whether it was a resumption or not. |
321 | | */ |
322 | 784 | if ((!s->server || (s->server && !s->hit)) |
323 | 784 | && !tls1_save_sigalgs(s, &supported_sig_algs, 0)) { |
324 | 1 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
325 | 1 | return 0; |
326 | 1 | } |
327 | | |
328 | 783 | return 1; |
329 | 784 | } |
330 | | |
331 | | #ifndef OPENSSL_NO_OCSP |
332 | | int tls_parse_ctos_status_request(SSL_CONNECTION *s, PACKET *pkt, |
333 | | unsigned int context, |
334 | | X509 *x, size_t chainidx) |
335 | 335 | { |
336 | 335 | PACKET responder_id_list, exts; |
337 | | |
338 | | /* We ignore this in a resumption handshake */ |
339 | 335 | if (s->hit) |
340 | 0 | return 1; |
341 | | |
342 | | /* Not defined if we get one of these in a client Certificate */ |
343 | 335 | if (x != NULL) |
344 | 0 | return 1; |
345 | | |
346 | 335 | if (!PACKET_get_1(pkt, (unsigned int *)&s->ext.status_type)) { |
347 | 1 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
348 | 1 | return 0; |
349 | 1 | } |
350 | | |
351 | 334 | if (s->ext.status_type != TLSEXT_STATUSTYPE_ocsp) { |
352 | | /* |
353 | | * We don't know what to do with any other type so ignore it. |
354 | | */ |
355 | 14 | s->ext.status_type = TLSEXT_STATUSTYPE_nothing; |
356 | 14 | return 1; |
357 | 14 | } |
358 | | |
359 | 320 | if (!PACKET_get_length_prefixed_2 (pkt, &responder_id_list)) { |
360 | 10 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
361 | 10 | return 0; |
362 | 10 | } |
363 | | |
364 | | /* |
365 | | * We remove any OCSP_RESPIDs from a previous handshake |
366 | | * to prevent unbounded memory growth - CVE-2016-6304 |
367 | | */ |
368 | 310 | sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free); |
369 | 310 | if (PACKET_remaining(&responder_id_list) > 0) { |
370 | 252 | s->ext.ocsp.ids = sk_OCSP_RESPID_new_null(); |
371 | 252 | if (s->ext.ocsp.ids == NULL) { |
372 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 252 | } else { |
376 | 58 | s->ext.ocsp.ids = NULL; |
377 | 58 | } |
378 | | |
379 | 313 | while (PACKET_remaining(&responder_id_list) > 0) { |
380 | 254 | OCSP_RESPID *id; |
381 | 254 | PACKET responder_id; |
382 | 254 | const unsigned char *id_data; |
383 | | |
384 | 254 | if (!PACKET_get_length_prefixed_2(&responder_id_list, &responder_id) |
385 | 254 | || PACKET_remaining(&responder_id) == 0) { |
386 | 12 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
387 | 12 | return 0; |
388 | 12 | } |
389 | | |
390 | 242 | id_data = PACKET_data(&responder_id); |
391 | 242 | id = d2i_OCSP_RESPID(NULL, &id_data, |
392 | 242 | (int)PACKET_remaining(&responder_id)); |
393 | 242 | if (id == NULL) { |
394 | 236 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
395 | 236 | return 0; |
396 | 236 | } |
397 | | |
398 | 6 | if (id_data != PACKET_end(&responder_id)) { |
399 | 3 | OCSP_RESPID_free(id); |
400 | 3 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
401 | | |
402 | 3 | return 0; |
403 | 3 | } |
404 | | |
405 | 3 | if (!sk_OCSP_RESPID_push(s->ext.ocsp.ids, id)) { |
406 | 0 | OCSP_RESPID_free(id); |
407 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
408 | |
|
409 | 0 | return 0; |
410 | 0 | } |
411 | 3 | } |
412 | | |
413 | | /* Read in request_extensions */ |
414 | 59 | if (!PACKET_as_length_prefixed_2(pkt, &exts)) { |
415 | 13 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
416 | 13 | return 0; |
417 | 13 | } |
418 | | |
419 | 46 | if (PACKET_remaining(&exts) > 0) { |
420 | 46 | const unsigned char *ext_data = PACKET_data(&exts); |
421 | | |
422 | 46 | sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, |
423 | 46 | X509_EXTENSION_free); |
424 | 46 | s->ext.ocsp.exts = |
425 | 46 | d2i_X509_EXTENSIONS(NULL, &ext_data, (int)PACKET_remaining(&exts)); |
426 | 46 | if (s->ext.ocsp.exts == NULL || ext_data != PACKET_end(&exts)) { |
427 | 46 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
428 | 46 | return 0; |
429 | 46 | } |
430 | 46 | } |
431 | | |
432 | 0 | return 1; |
433 | 46 | } |
434 | | #endif |
435 | | |
436 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
437 | | int tls_parse_ctos_npn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
438 | | X509 *x, size_t chainidx) |
439 | 1 | { |
440 | | /* |
441 | | * We shouldn't accept this extension on a |
442 | | * renegotiation. |
443 | | */ |
444 | 1 | if (SSL_IS_FIRST_HANDSHAKE(s)) |
445 | 1 | s->s3.npn_seen = 1; |
446 | | |
447 | 1 | return 1; |
448 | 1 | } |
449 | | #endif |
450 | | |
451 | | /* |
452 | | * Save the ALPN extension in a ClientHello.|pkt| holds the contents of the ALPN |
453 | | * extension, not including type and length. Returns: 1 on success, 0 on error. |
454 | | */ |
455 | | int tls_parse_ctos_alpn(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
456 | | X509 *x, size_t chainidx) |
457 | 24 | { |
458 | 24 | PACKET protocol_list, save_protocol_list, protocol; |
459 | | |
460 | 24 | if (!SSL_IS_FIRST_HANDSHAKE(s)) |
461 | 0 | return 1; |
462 | | |
463 | 24 | if (!PACKET_as_length_prefixed_2(pkt, &protocol_list) |
464 | 24 | || PACKET_remaining(&protocol_list) < 2) { |
465 | 12 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
466 | 12 | return 0; |
467 | 12 | } |
468 | | |
469 | 12 | save_protocol_list = protocol_list; |
470 | 58 | do { |
471 | | /* Protocol names can't be empty. */ |
472 | 58 | if (!PACKET_get_length_prefixed_1(&protocol_list, &protocol) |
473 | 58 | || PACKET_remaining(&protocol) == 0) { |
474 | 10 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
475 | 10 | return 0; |
476 | 10 | } |
477 | 58 | } while (PACKET_remaining(&protocol_list) != 0); |
478 | | |
479 | 2 | OPENSSL_free(s->s3.alpn_proposed); |
480 | 2 | s->s3.alpn_proposed = NULL; |
481 | 2 | s->s3.alpn_proposed_len = 0; |
482 | 2 | if (!PACKET_memdup(&save_protocol_list, |
483 | 2 | &s->s3.alpn_proposed, &s->s3.alpn_proposed_len)) { |
484 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 2 | return 1; |
489 | 2 | } |
490 | | |
491 | | #ifndef OPENSSL_NO_SRTP |
492 | | int tls_parse_ctos_use_srtp(SSL_CONNECTION *s, PACKET *pkt, |
493 | | unsigned int context, X509 *x, size_t chainidx) |
494 | 14 | { |
495 | 14 | STACK_OF(SRTP_PROTECTION_PROFILE) *srvr; |
496 | 14 | unsigned int ct, mki_len, id; |
497 | 14 | int i, srtp_pref; |
498 | 14 | PACKET subpkt; |
499 | 14 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
500 | | |
501 | | /* Ignore this if we have no SRTP profiles */ |
502 | 14 | if (SSL_get_srtp_profiles(ssl) == NULL) |
503 | 14 | return 1; |
504 | | |
505 | | /* Pull off the length of the cipher suite list and check it is even */ |
506 | 0 | if (!PACKET_get_net_2(pkt, &ct) || (ct & 1) != 0 |
507 | 0 | || !PACKET_get_sub_packet(pkt, &subpkt, ct)) { |
508 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
509 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
510 | 0 | return 0; |
511 | 0 | } |
512 | | |
513 | 0 | srvr = SSL_get_srtp_profiles(ssl); |
514 | 0 | s->srtp_profile = NULL; |
515 | | /* Search all profiles for a match initially */ |
516 | 0 | srtp_pref = sk_SRTP_PROTECTION_PROFILE_num(srvr); |
517 | |
|
518 | 0 | while (PACKET_remaining(&subpkt)) { |
519 | 0 | if (!PACKET_get_net_2(&subpkt, &id)) { |
520 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
521 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
522 | 0 | return 0; |
523 | 0 | } |
524 | | |
525 | | /* |
526 | | * Only look for match in profiles of higher preference than |
527 | | * current match. |
528 | | * If no profiles have been have been configured then this |
529 | | * does nothing. |
530 | | */ |
531 | 0 | for (i = 0; i < srtp_pref; i++) { |
532 | 0 | SRTP_PROTECTION_PROFILE *sprof = |
533 | 0 | sk_SRTP_PROTECTION_PROFILE_value(srvr, i); |
534 | |
|
535 | 0 | if (sprof->id == id) { |
536 | 0 | s->srtp_profile = sprof; |
537 | 0 | srtp_pref = i; |
538 | 0 | break; |
539 | 0 | } |
540 | 0 | } |
541 | 0 | } |
542 | | |
543 | | /* Now extract the MKI value as a sanity check, but discard it for now */ |
544 | 0 | if (!PACKET_get_1(pkt, &mki_len)) { |
545 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
546 | 0 | SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST); |
547 | 0 | return 0; |
548 | 0 | } |
549 | | |
550 | 0 | if (!PACKET_forward(pkt, mki_len) |
551 | 0 | || PACKET_remaining(pkt)) { |
552 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRTP_MKI_VALUE); |
553 | 0 | return 0; |
554 | 0 | } |
555 | | |
556 | 0 | return 1; |
557 | 0 | } |
558 | | #endif |
559 | | |
560 | | int tls_parse_ctos_etm(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
561 | | X509 *x, size_t chainidx) |
562 | 126 | { |
563 | 126 | if (!(s->options & SSL_OP_NO_ENCRYPT_THEN_MAC)) |
564 | 126 | s->ext.use_etm = 1; |
565 | | |
566 | 126 | return 1; |
567 | 126 | } |
568 | | |
569 | | /* |
570 | | * Process a psk_kex_modes extension received in the ClientHello. |pkt| contains |
571 | | * the raw PACKET data for the extension. Returns 1 on success or 0 on failure. |
572 | | */ |
573 | | int tls_parse_ctos_psk_kex_modes(SSL_CONNECTION *s, PACKET *pkt, |
574 | | unsigned int context, |
575 | | X509 *x, size_t chainidx) |
576 | 0 | { |
577 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
578 | 0 | PACKET psk_kex_modes; |
579 | 0 | unsigned int mode; |
580 | |
|
581 | 0 | if (!PACKET_as_length_prefixed_1(pkt, &psk_kex_modes) |
582 | 0 | || PACKET_remaining(&psk_kex_modes) == 0) { |
583 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | 0 | while (PACKET_get_1(&psk_kex_modes, &mode)) { |
588 | 0 | if (mode == TLSEXT_KEX_MODE_KE_DHE) |
589 | 0 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE_DHE; |
590 | 0 | else if (mode == TLSEXT_KEX_MODE_KE |
591 | 0 | && (s->options & SSL_OP_ALLOW_NO_DHE_KEX) != 0) |
592 | 0 | s->ext.psk_kex_mode |= TLSEXT_KEX_MODE_FLAG_KE; |
593 | 0 | } |
594 | |
|
595 | 0 | if (((s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) != 0) |
596 | 0 | && (s->options & SSL_OP_PREFER_NO_DHE_KEX) != 0) { |
597 | | |
598 | | /* |
599 | | * If NO_DHE is supported and preferred, then we only remember this |
600 | | * mode. DHE PSK will not be used for sure, because in any case where |
601 | | * it would be supported (i.e. if a key share is present), NO_DHE would |
602 | | * be supported as well. As the latter is preferred it would be |
603 | | * chosen. By removing DHE PSK here, we don't have to deal with the |
604 | | * SSL_OP_PREFER_NO_DHE_KEX option in any other place. |
605 | | */ |
606 | 0 | s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_KE; |
607 | 0 | } |
608 | |
|
609 | 0 | #endif |
610 | |
|
611 | 0 | return 1; |
612 | 0 | } |
613 | | |
614 | | /* |
615 | | * Use function tls_parse_ctos_key_share with helper functions extract_keyshares, |
616 | | * check_overlap and tls_accept_ksgroup to parse the key_share extension(s) |
617 | | * received in the ClientHello and to select the group used of the key exchange |
618 | | */ |
619 | | |
620 | | #ifndef OPENSSL_NO_TLS1_3 |
621 | | /* |
622 | | * Accept a key share group by setting the related variables in s->s3 and |
623 | | * by generating a pubkey for this group |
624 | | */ |
625 | | static int tls_accept_ksgroup(SSL_CONNECTION *s, uint16_t ksgroup, PACKET *encoded_pubkey) |
626 | 0 | { |
627 | | /* Accept the key share group */ |
628 | 0 | s->s3.group_id = ksgroup; |
629 | 0 | s->s3.group_id_candidate = ksgroup; |
630 | | /* Cache the selected group ID in the SSL_SESSION */ |
631 | 0 | s->session->kex_group = ksgroup; |
632 | 0 | if ((s->s3.peer_tmp = ssl_generate_param_group(s, ksgroup)) == NULL) { |
633 | 0 | SSLfatal(s, |
634 | 0 | SSL_AD_INTERNAL_ERROR, |
635 | 0 | SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
636 | 0 | return 0; |
637 | 0 | } |
638 | 0 | if (tls13_set_encoded_pub_key(s->s3.peer_tmp, |
639 | 0 | PACKET_data(encoded_pubkey), |
640 | 0 | PACKET_remaining(encoded_pubkey)) <= 0) { |
641 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT); |
642 | 0 | return 0; |
643 | 0 | } |
644 | 0 | return 1; |
645 | 0 | } |
646 | | |
647 | 0 | # define GROUPLIST_INCREMENT 32 /* Memory allocation chunk size (nominally 64 Bytes chunks) */ |
648 | | |
649 | | typedef enum KS_EXTRACTION_RESULT { |
650 | | EXTRACTION_FAILURE, |
651 | | EXTRACTION_SUCCESS, |
652 | | EXTRACTION_SUCCESS_HRR |
653 | | } KS_EXTRACTION_RESULT; |
654 | | |
655 | | static KS_EXTRACTION_RESULT extract_keyshares(SSL_CONNECTION *s, PACKET *key_share_list, |
656 | | const uint16_t *clntgroups, size_t clnt_num_groups, |
657 | | const uint16_t *srvrgroups, size_t srvr_num_groups, |
658 | | uint16_t **keyshares_arr, PACKET **encoded_pubkey_arr, |
659 | | size_t *keyshares_cnt, size_t *keyshares_max) |
660 | 0 | { |
661 | 0 | PACKET encoded_pubkey; |
662 | 0 | size_t key_share_pos = 0; |
663 | 0 | size_t previous_key_share_pos = 0; |
664 | 0 | unsigned int group_id = 0; |
665 | | |
666 | | /* Prepare memory to hold the extracted key share groups and related pubkeys */ |
667 | 0 | *keyshares_arr = OPENSSL_malloc(*keyshares_max * sizeof(**keyshares_arr)); |
668 | 0 | if (*keyshares_arr == NULL) { |
669 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
670 | 0 | goto failure; |
671 | 0 | } |
672 | 0 | *encoded_pubkey_arr = OPENSSL_malloc(*keyshares_max * sizeof(**encoded_pubkey_arr)); |
673 | 0 | if (*encoded_pubkey_arr == NULL) { |
674 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
675 | 0 | goto failure; |
676 | 0 | } |
677 | | |
678 | 0 | while (PACKET_remaining(key_share_list) > 0) { |
679 | | /* Get the group_id for the current share and its encoded_pubkey */ |
680 | 0 | if (!PACKET_get_net_2(key_share_list, &group_id) |
681 | 0 | || !PACKET_get_length_prefixed_2(key_share_list, &encoded_pubkey) |
682 | 0 | || PACKET_remaining(&encoded_pubkey) == 0) { |
683 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
684 | 0 | goto failure; |
685 | 0 | } |
686 | | |
687 | | /* |
688 | | * If we sent an HRR then the key_share sent back MUST be for the group |
689 | | * we requested, and must be the only key_share sent. |
690 | | */ |
691 | 0 | if (s->s3.group_id != 0 |
692 | 0 | && (group_id != s->s3.group_id |
693 | 0 | || PACKET_remaining(key_share_list) != 0)) { |
694 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
695 | 0 | goto failure; |
696 | 0 | } |
697 | | |
698 | | /* |
699 | | * Check if this share is in supported_groups sent from client |
700 | | * RFC 8446 also mandates that clients send keyshares in the same |
701 | | * order as listed in the supported groups extension, but its not |
702 | | * required that the server check that, and some clients violate this |
703 | | * so instead of failing the connection when that occurs, log a trace |
704 | | * message indicating the client discrepancy. |
705 | | */ |
706 | 0 | if (!check_in_list(s, group_id, clntgroups, clnt_num_groups, 0, &key_share_pos)) { |
707 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
708 | 0 | goto failure; |
709 | 0 | } |
710 | | |
711 | 0 | if (key_share_pos < previous_key_share_pos) |
712 | 0 | OSSL_TRACE1(TLS, "key share group id %d is out of RFC 8446 order\n", group_id); |
713 | |
|
714 | 0 | previous_key_share_pos = key_share_pos; |
715 | |
|
716 | 0 | if (s->s3.group_id != 0) { |
717 | | /* |
718 | | * We have sent a HRR, and the key share we got back is |
719 | | * the one we expected and is the only key share and is |
720 | | * in the list of supported_groups (checked |
721 | | * above already), hence we accept this key share group |
722 | | */ |
723 | 0 | if (!tls_accept_ksgroup(s, s->s3.group_id, &encoded_pubkey)) |
724 | 0 | goto failure; /* SSLfatal already called */ |
725 | | /* We have selected a key share group via HRR, hence we're done here */ |
726 | 0 | return EXTRACTION_SUCCESS_HRR; |
727 | 0 | } |
728 | | |
729 | | /* |
730 | | * We tolerate but ignore a group id that we don't think is |
731 | | * suitable for TLSv1.3 or which is not supported by the server |
732 | | */ |
733 | 0 | if (!check_in_list(s, group_id, srvrgroups, srvr_num_groups, 1, NULL) |
734 | 0 | || !tls_group_allowed(s, group_id, SSL_SECOP_CURVE_SUPPORTED) |
735 | 0 | || !tls_valid_group(s, group_id, TLS1_3_VERSION, TLS1_3_VERSION, |
736 | 0 | 0, NULL)) { |
737 | | /* Share not suitable or not supported, check next share */ |
738 | 0 | continue; |
739 | 0 | } |
740 | | |
741 | | /* Memorize this key share group ID and its encoded point */ |
742 | 0 | (*keyshares_arr)[*keyshares_cnt] = group_id; |
743 | 0 | (*encoded_pubkey_arr)[(*keyshares_cnt)++] = encoded_pubkey; |
744 | | |
745 | | /* |
746 | | * Memory management (remark: While limiting the client to only allow |
747 | | * a maximum of OPENSSL_CLIENT_MAX_KEY_SHARES to be sent, the server can |
748 | | * handle any number of key shares) |
749 | | */ |
750 | 0 | if (*keyshares_cnt == *keyshares_max) { |
751 | 0 | PACKET *tmp_pkt; |
752 | 0 | uint16_t *tmp = |
753 | 0 | OPENSSL_realloc(*keyshares_arr, |
754 | 0 | (*keyshares_max + GROUPLIST_INCREMENT) * sizeof(**keyshares_arr)); |
755 | |
|
756 | 0 | if (tmp == NULL) |
757 | 0 | goto failure; |
758 | 0 | *keyshares_arr = tmp; |
759 | 0 | tmp_pkt = |
760 | 0 | OPENSSL_realloc(*encoded_pubkey_arr, |
761 | 0 | (*keyshares_max + GROUPLIST_INCREMENT) * |
762 | 0 | sizeof(**encoded_pubkey_arr)); |
763 | 0 | if (tmp_pkt == NULL) |
764 | 0 | goto failure; |
765 | 0 | *encoded_pubkey_arr = tmp_pkt; |
766 | 0 | *keyshares_max += GROUPLIST_INCREMENT; |
767 | 0 | } |
768 | |
|
769 | 0 | } |
770 | | |
771 | 0 | return EXTRACTION_SUCCESS; |
772 | | |
773 | 0 | failure: |
774 | | /* Fatal error -> free any allocated memory and return 0 */ |
775 | 0 | OPENSSL_free(*keyshares_arr); |
776 | 0 | OPENSSL_free(*encoded_pubkey_arr); |
777 | 0 | return EXTRACTION_FAILURE; |
778 | 0 | } |
779 | | #endif |
780 | | |
781 | | /* |
782 | | * For each group in the priority list of groups, check if that group is |
783 | | * also present in the secondary list; if so, select the first overlap and |
784 | | * assign to selected_group and also set the related index in the candidate group list, |
785 | | * or set selected_group to 0 if no overlap |
786 | | */ |
787 | | #ifndef OPENSSL_NO_TLS1_3 |
788 | | static void check_overlap(SSL_CONNECTION *s, |
789 | | const uint16_t *prio_groups, size_t prio_num_groups, |
790 | | const uint16_t *candidate_groups, size_t candidate_num_groups, |
791 | | int *prio_group_idx, int *candidate_group_idx, |
792 | | uint16_t *selected_group) |
793 | 0 | { |
794 | 0 | uint16_t current_group; |
795 | 0 | size_t group_idx = prio_num_groups; |
796 | 0 | size_t new_group_idx = 0; |
797 | |
|
798 | 0 | *candidate_group_idx = 0; |
799 | 0 | *prio_group_idx = 0; |
800 | 0 | *selected_group = 0; |
801 | |
|
802 | 0 | for (current_group = 0; current_group < candidate_num_groups; current_group++) { |
803 | 0 | if (!check_in_list(s, candidate_groups[current_group], prio_groups, |
804 | 0 | prio_num_groups, 1, &new_group_idx) |
805 | 0 | || !tls_group_allowed(s, candidate_groups[current_group], |
806 | 0 | SSL_SECOP_CURVE_SUPPORTED) |
807 | 0 | || !tls_valid_group(s, candidate_groups[current_group], TLS1_3_VERSION, |
808 | 0 | TLS1_3_VERSION, 0, NULL)) |
809 | | /* No overlap or group not suitable, check next group */ |
810 | 0 | continue; |
811 | | |
812 | | /* |
813 | | * is the found new_group_idx earlier in the priority list than |
814 | | * initial or last group_idx? |
815 | | */ |
816 | 0 | if (new_group_idx < group_idx) { |
817 | 0 | group_idx = new_group_idx; |
818 | 0 | *candidate_group_idx = current_group; |
819 | 0 | *prio_group_idx = group_idx; |
820 | 0 | *selected_group = prio_groups[group_idx]; |
821 | 0 | } |
822 | 0 | } |
823 | 0 | } |
824 | | #endif |
825 | | |
826 | | int tls_parse_ctos_key_share(SSL_CONNECTION *s, PACKET *pkt, |
827 | | unsigned int context, X509 *x, size_t chainidx) |
828 | 0 | { |
829 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
830 | 0 | PACKET key_share_list; |
831 | 0 | const uint16_t *clntgroups, *srvrgroups; |
832 | 0 | const size_t *srvrtuples; |
833 | 0 | uint16_t *first_group_in_tuple; |
834 | 0 | size_t clnt_num_groups, srvr_num_groups, srvr_num_tuples; |
835 | 0 | PACKET *encoded_pubkey_arr = NULL; |
836 | 0 | uint16_t *keyshares_arr = NULL; |
837 | 0 | size_t keyshares_cnt = 0; |
838 | 0 | size_t keyshares_max = GROUPLIST_INCREMENT; |
839 | | /* We conservatively assume that we did not find a suitable group */ |
840 | 0 | uint16_t group_id_candidate = 0; |
841 | 0 | KS_EXTRACTION_RESULT ks_extraction_result; |
842 | 0 | size_t current_tuple; |
843 | 0 | int ret = 0; |
844 | |
|
845 | 0 | s->s3.group_id_candidate = 0; |
846 | 0 | if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) |
847 | 0 | return 1; |
848 | | |
849 | | /* Sanity check */ |
850 | 0 | if (s->s3.peer_tmp != NULL) { |
851 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
852 | 0 | return 0; |
853 | 0 | } |
854 | | |
855 | 0 | if (!PACKET_as_length_prefixed_2(pkt, &key_share_list)) { |
856 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
857 | 0 | return 0; |
858 | 0 | } |
859 | | |
860 | | /* Get list of server supported groups and the group tuples */ |
861 | 0 | tls1_get_supported_groups(s, &srvrgroups, &srvr_num_groups); |
862 | 0 | tls1_get_group_tuples(s, &srvrtuples, &srvr_num_tuples); |
863 | | /* Get the clients list of supported groups. */ |
864 | 0 | tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups); |
865 | |
|
866 | 0 | if (clnt_num_groups == 0) { |
867 | | /* |
868 | | * This can only happen if the supported_groups extension was not sent, |
869 | | * because we verify that the length is non-zero when we process that |
870 | | * extension. |
871 | | */ |
872 | 0 | SSLfatal(s, SSL_AD_MISSING_EXTENSION, |
873 | 0 | SSL_R_MISSING_SUPPORTED_GROUPS_EXTENSION); |
874 | 0 | return 0; |
875 | 0 | } |
876 | | |
877 | 0 | if (s->s3.group_id != 0 && PACKET_remaining(&key_share_list) == 0) { |
878 | | /* |
879 | | * If we set a group_id already, then we must have sent an HRR |
880 | | * requesting a new key_share. If we haven't got one then that is an |
881 | | * error |
882 | | */ |
883 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); |
884 | 0 | return 0; |
885 | 0 | } |
886 | | |
887 | | /* We parse the key share extension and memorize the entries (after some checks) */ |
888 | 0 | ks_extraction_result = extract_keyshares(s, |
889 | 0 | &key_share_list, |
890 | 0 | clntgroups, clnt_num_groups, |
891 | 0 | srvrgroups, srvr_num_groups, |
892 | 0 | &keyshares_arr, &encoded_pubkey_arr, |
893 | 0 | &keyshares_cnt, &keyshares_max); |
894 | |
|
895 | 0 | if (ks_extraction_result == EXTRACTION_FAILURE) /* Fatal error during tests */ |
896 | 0 | return 0; /* Memory already freed and SSLfatal already called */ |
897 | 0 | if (ks_extraction_result == EXTRACTION_SUCCESS_HRR) /* Successful HRR */ |
898 | 0 | goto end; |
899 | | |
900 | | /* |
901 | | * We now have the folowing lists available to make a decision for |
902 | | * which group the server should use for key exchange : |
903 | | * From client: clntgroups[clnt_num_groups], |
904 | | * keyshares_arr[keyshares_cnt], encoded_pubkey_arr[keyshares_cnt] |
905 | | * From server: srvrgroups[srvr_num_groups], srvrtuples[srvr_num_tuples] |
906 | | * |
907 | | * Group selection algorithm: |
908 | | * For all tuples do: |
909 | | * key share group(s) overlapping with current tuple? |
910 | | * --> Yes: accept group_id for SH |
911 | | * --> No: is any of the client supported_groups overlapping with current tuple? |
912 | | * --> Yes: memorize group_id for HRR, break |
913 | | * --> No: continue to check next tuple |
914 | | * |
915 | | * Remark: Selection priority different for client- or server-preference |
916 | | */ |
917 | 0 | first_group_in_tuple = (uint16_t *)srvrgroups; |
918 | 0 | for (current_tuple = 0; current_tuple < srvr_num_tuples; current_tuple++) { |
919 | 0 | size_t number_of_groups_in_tuple = srvrtuples[current_tuple]; |
920 | 0 | int prio_group_idx = 0, candidate_group_idx = 0; |
921 | | |
922 | | /* Server or client preference ? */ |
923 | 0 | if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
924 | | /* Server preference */ |
925 | | /* Is there overlap with a key share group? */ |
926 | 0 | check_overlap(s, |
927 | 0 | first_group_in_tuple, number_of_groups_in_tuple, |
928 | 0 | keyshares_arr, keyshares_cnt, |
929 | 0 | &prio_group_idx, &candidate_group_idx, |
930 | 0 | &group_id_candidate); |
931 | 0 | if (group_id_candidate > 0) { /* Overlap found -> accept the key share group */ |
932 | 0 | if (!tls_accept_ksgroup(s, group_id_candidate, |
933 | 0 | &encoded_pubkey_arr[candidate_group_idx])) |
934 | 0 | goto err; /* SSLfatal already called */ |
935 | | /* We have all info for a SH, hence we're done here */ |
936 | 0 | goto end; |
937 | 0 | } else { |
938 | | /* |
939 | | * There's no overlap with a key share, but is there at least a client |
940 | | * supported_group overlapping with the current tuple? |
941 | | */ |
942 | 0 | check_overlap(s, |
943 | 0 | first_group_in_tuple, number_of_groups_in_tuple, |
944 | 0 | clntgroups, clnt_num_groups, |
945 | 0 | &prio_group_idx, &candidate_group_idx, |
946 | 0 | &group_id_candidate); |
947 | 0 | if (group_id_candidate > 0) { |
948 | | /* |
949 | | * We did not have a key share overlap, but at least the supported |
950 | | * groups overlap hence we can stop searching |
951 | | * (and report group_id_candidate 'upward' for HRR) |
952 | | */ |
953 | 0 | s->s3.group_id_candidate = group_id_candidate; |
954 | 0 | goto end; |
955 | 0 | } else { |
956 | | /* |
957 | | * Neither key share nor supported_groups overlap current |
958 | | * tuple, hence we try the next tuple |
959 | | */ |
960 | 0 | first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple]; |
961 | 0 | continue; |
962 | 0 | } |
963 | 0 | } |
964 | |
|
965 | 0 | } else { /* We have client preference */ |
966 | 0 | check_overlap(s, |
967 | 0 | keyshares_arr, keyshares_cnt, |
968 | 0 | first_group_in_tuple, number_of_groups_in_tuple, |
969 | 0 | &prio_group_idx, &candidate_group_idx, |
970 | 0 | &group_id_candidate); |
971 | 0 | if (group_id_candidate > 0) { |
972 | 0 | if (!tls_accept_ksgroup(s, group_id_candidate, &encoded_pubkey_arr[prio_group_idx])) |
973 | 0 | goto err; |
974 | 0 | goto end; |
975 | 0 | } else { |
976 | 0 | check_overlap(s, |
977 | 0 | clntgroups, clnt_num_groups, |
978 | 0 | first_group_in_tuple, number_of_groups_in_tuple, |
979 | 0 | &prio_group_idx, &candidate_group_idx, |
980 | 0 | &group_id_candidate); |
981 | 0 | if (group_id_candidate > 0) { |
982 | 0 | s->s3.group_id_candidate = group_id_candidate; |
983 | 0 | goto end; |
984 | 0 | } else { |
985 | 0 | first_group_in_tuple = &first_group_in_tuple[number_of_groups_in_tuple]; |
986 | 0 | continue; |
987 | 0 | } |
988 | 0 | } |
989 | 0 | } |
990 | 0 | } |
991 | | |
992 | 0 | end: |
993 | 0 | ret = 1; |
994 | |
|
995 | 0 | err: |
996 | 0 | OPENSSL_free(keyshares_arr); |
997 | 0 | OPENSSL_free(encoded_pubkey_arr); |
998 | 0 | return ret; |
999 | | |
1000 | 0 | #endif |
1001 | | |
1002 | 0 | return 1; |
1003 | 0 | } |
1004 | | |
1005 | | int tls_parse_ctos_cookie(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1006 | | X509 *x, size_t chainidx) |
1007 | 0 | { |
1008 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
1009 | 0 | unsigned int format, version, key_share, group_id; |
1010 | 0 | EVP_MD_CTX *hctx; |
1011 | 0 | EVP_PKEY *pkey; |
1012 | 0 | PACKET cookie, raw, chhash, appcookie; |
1013 | 0 | WPACKET hrrpkt; |
1014 | 0 | const unsigned char *data, *mdin, *ciphdata; |
1015 | 0 | unsigned char hmac[SHA256_DIGEST_LENGTH]; |
1016 | 0 | unsigned char hrr[MAX_HRR_SIZE]; |
1017 | 0 | size_t rawlen, hmaclen, hrrlen, ciphlen; |
1018 | 0 | uint64_t tm, now; |
1019 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
1020 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1021 | | |
1022 | | /* Ignore any cookie if we're not set up to verify it */ |
1023 | 0 | if (sctx->verify_stateless_cookie_cb == NULL |
1024 | 0 | || (s->s3.flags & TLS1_FLAGS_STATELESS) == 0) |
1025 | 0 | return 1; |
1026 | | |
1027 | 0 | if (!PACKET_as_length_prefixed_2(pkt, &cookie)) { |
1028 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1029 | 0 | return 0; |
1030 | 0 | } |
1031 | | |
1032 | 0 | raw = cookie; |
1033 | 0 | data = PACKET_data(&raw); |
1034 | 0 | rawlen = PACKET_remaining(&raw); |
1035 | 0 | if (rawlen < SHA256_DIGEST_LENGTH |
1036 | 0 | || !PACKET_forward(&raw, rawlen - SHA256_DIGEST_LENGTH)) { |
1037 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1038 | 0 | return 0; |
1039 | 0 | } |
1040 | 0 | mdin = PACKET_data(&raw); |
1041 | | |
1042 | | /* Verify the HMAC of the cookie */ |
1043 | 0 | hctx = EVP_MD_CTX_create(); |
1044 | 0 | pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC", |
1045 | 0 | sctx->propq, |
1046 | 0 | s->session_ctx->ext.cookie_hmac_key, |
1047 | 0 | sizeof(s->session_ctx->ext.cookie_hmac_key)); |
1048 | 0 | if (hctx == NULL || pkey == NULL) { |
1049 | 0 | EVP_MD_CTX_free(hctx); |
1050 | 0 | EVP_PKEY_free(pkey); |
1051 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
1052 | 0 | return 0; |
1053 | 0 | } |
1054 | | |
1055 | 0 | hmaclen = SHA256_DIGEST_LENGTH; |
1056 | 0 | if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx, |
1057 | 0 | sctx->propq, pkey, NULL) <= 0 |
1058 | 0 | || EVP_DigestSign(hctx, hmac, &hmaclen, data, |
1059 | 0 | rawlen - SHA256_DIGEST_LENGTH) <= 0 |
1060 | 0 | || hmaclen != SHA256_DIGEST_LENGTH) { |
1061 | 0 | EVP_MD_CTX_free(hctx); |
1062 | 0 | EVP_PKEY_free(pkey); |
1063 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1064 | 0 | return 0; |
1065 | 0 | } |
1066 | | |
1067 | 0 | EVP_MD_CTX_free(hctx); |
1068 | 0 | EVP_PKEY_free(pkey); |
1069 | |
|
1070 | 0 | if (CRYPTO_memcmp(hmac, mdin, SHA256_DIGEST_LENGTH) != 0) { |
1071 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH); |
1072 | 0 | return 0; |
1073 | 0 | } |
1074 | | |
1075 | 0 | if (!PACKET_get_net_2(&cookie, &format)) { |
1076 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1077 | 0 | return 0; |
1078 | 0 | } |
1079 | | /* Check the cookie format is something we recognise. Ignore it if not */ |
1080 | 0 | if (format != COOKIE_STATE_FORMAT_VERSION) |
1081 | 0 | return 1; |
1082 | | |
1083 | | /* |
1084 | | * The rest of these checks really shouldn't fail since we have verified the |
1085 | | * HMAC above. |
1086 | | */ |
1087 | | |
1088 | | /* Check the version number is sane */ |
1089 | 0 | if (!PACKET_get_net_2(&cookie, &version)) { |
1090 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1091 | 0 | return 0; |
1092 | 0 | } |
1093 | 0 | if (version != TLS1_3_VERSION) { |
1094 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
1095 | 0 | SSL_R_BAD_PROTOCOL_VERSION_NUMBER); |
1096 | 0 | return 0; |
1097 | 0 | } |
1098 | | |
1099 | 0 | if (!PACKET_get_net_2(&cookie, &group_id)) { |
1100 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1101 | 0 | return 0; |
1102 | 0 | } |
1103 | | |
1104 | 0 | ciphdata = PACKET_data(&cookie); |
1105 | 0 | if (!PACKET_forward(&cookie, 2)) { |
1106 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1107 | 0 | return 0; |
1108 | 0 | } |
1109 | 0 | if (group_id != s->s3.group_id |
1110 | 0 | || s->s3.tmp.new_cipher |
1111 | 0 | != ssl_get_cipher_by_char(s, ciphdata, 0)) { |
1112 | | /* |
1113 | | * We chose a different cipher or group id this time around to what is |
1114 | | * in the cookie. Something must have changed. |
1115 | | */ |
1116 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER); |
1117 | 0 | return 0; |
1118 | 0 | } |
1119 | | |
1120 | 0 | if (!PACKET_get_1(&cookie, &key_share) |
1121 | 0 | || !PACKET_get_net_8(&cookie, &tm) |
1122 | 0 | || !PACKET_get_length_prefixed_2(&cookie, &chhash) |
1123 | 0 | || !PACKET_get_length_prefixed_1(&cookie, &appcookie) |
1124 | 0 | || PACKET_remaining(&cookie) != SHA256_DIGEST_LENGTH) { |
1125 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
1126 | 0 | return 0; |
1127 | 0 | } |
1128 | | |
1129 | | /* We tolerate a cookie age of up to 10 minutes (= 60 * 10 seconds) */ |
1130 | 0 | now = time(NULL); |
1131 | 0 | if (tm > now || (now - tm) > 600) { |
1132 | | /* Cookie is stale. Ignore it */ |
1133 | 0 | return 1; |
1134 | 0 | } |
1135 | | |
1136 | | /* Verify the app cookie */ |
1137 | 0 | if (sctx->verify_stateless_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s), |
1138 | 0 | PACKET_data(&appcookie), |
1139 | 0 | PACKET_remaining(&appcookie)) == 0) { |
1140 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COOKIE_MISMATCH); |
1141 | 0 | return 0; |
1142 | 0 | } |
1143 | | |
1144 | | /* |
1145 | | * Reconstruct the HRR that we would have sent in response to the original |
1146 | | * ClientHello so we can add it to the transcript hash. |
1147 | | * Note: This won't work with custom HRR extensions |
1148 | | */ |
1149 | 0 | if (!WPACKET_init_static_len(&hrrpkt, hrr, sizeof(hrr), 0)) { |
1150 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1151 | 0 | return 0; |
1152 | 0 | } |
1153 | 0 | if (!WPACKET_put_bytes_u8(&hrrpkt, SSL3_MT_SERVER_HELLO) |
1154 | 0 | || !WPACKET_start_sub_packet_u24(&hrrpkt) |
1155 | 0 | || !WPACKET_put_bytes_u16(&hrrpkt, TLS1_2_VERSION) |
1156 | 0 | || !WPACKET_memcpy(&hrrpkt, hrrrandom, SSL3_RANDOM_SIZE) |
1157 | 0 | || !WPACKET_sub_memcpy_u8(&hrrpkt, s->tmp_session_id, |
1158 | 0 | s->tmp_session_id_len) |
1159 | 0 | || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, &hrrpkt, |
1160 | 0 | &ciphlen) |
1161 | 0 | || !WPACKET_put_bytes_u8(&hrrpkt, 0) |
1162 | 0 | || !WPACKET_start_sub_packet_u16(&hrrpkt)) { |
1163 | 0 | WPACKET_cleanup(&hrrpkt); |
1164 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1165 | 0 | return 0; |
1166 | 0 | } |
1167 | 0 | if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_supported_versions) |
1168 | 0 | || !WPACKET_start_sub_packet_u16(&hrrpkt) |
1169 | 0 | || !WPACKET_put_bytes_u16(&hrrpkt, s->version) |
1170 | 0 | || !WPACKET_close(&hrrpkt)) { |
1171 | 0 | WPACKET_cleanup(&hrrpkt); |
1172 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1173 | 0 | return 0; |
1174 | 0 | } |
1175 | 0 | if (key_share) { |
1176 | 0 | if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_key_share) |
1177 | 0 | || !WPACKET_start_sub_packet_u16(&hrrpkt) |
1178 | 0 | || !WPACKET_put_bytes_u16(&hrrpkt, s->s3.group_id) |
1179 | 0 | || !WPACKET_close(&hrrpkt)) { |
1180 | 0 | WPACKET_cleanup(&hrrpkt); |
1181 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1182 | 0 | return 0; |
1183 | 0 | } |
1184 | 0 | } |
1185 | 0 | if (!WPACKET_put_bytes_u16(&hrrpkt, TLSEXT_TYPE_cookie) |
1186 | 0 | || !WPACKET_start_sub_packet_u16(&hrrpkt) |
1187 | 0 | || !WPACKET_sub_memcpy_u16(&hrrpkt, data, rawlen) |
1188 | 0 | || !WPACKET_close(&hrrpkt) /* cookie extension */ |
1189 | 0 | || !WPACKET_close(&hrrpkt) /* extension block */ |
1190 | 0 | || !WPACKET_close(&hrrpkt) /* message */ |
1191 | 0 | || !WPACKET_get_total_written(&hrrpkt, &hrrlen) |
1192 | 0 | || !WPACKET_finish(&hrrpkt)) { |
1193 | 0 | WPACKET_cleanup(&hrrpkt); |
1194 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1195 | 0 | return 0; |
1196 | 0 | } |
1197 | | |
1198 | | /* Reconstruct the transcript hash */ |
1199 | 0 | if (!create_synthetic_message_hash(s, PACKET_data(&chhash), |
1200 | 0 | PACKET_remaining(&chhash), hrr, |
1201 | 0 | hrrlen)) { |
1202 | | /* SSLfatal() already called */ |
1203 | 0 | return 0; |
1204 | 0 | } |
1205 | | |
1206 | | /* Act as if this ClientHello came after a HelloRetryRequest */ |
1207 | 0 | s->hello_retry_request = SSL_HRR_PENDING; |
1208 | |
|
1209 | 0 | s->ext.cookieok = 1; |
1210 | 0 | #endif |
1211 | |
|
1212 | 0 | return 1; |
1213 | 0 | } |
1214 | | |
1215 | | int tls_parse_ctos_supported_groups(SSL_CONNECTION *s, PACKET *pkt, |
1216 | | unsigned int context, |
1217 | | X509 *x, size_t chainidx) |
1218 | 1.80k | { |
1219 | 1.80k | PACKET supported_groups_list; |
1220 | | |
1221 | | /* Each group is 2 bytes and we must have at least 1. */ |
1222 | 1.80k | if (!PACKET_as_length_prefixed_2(pkt, &supported_groups_list) |
1223 | 1.80k | || PACKET_remaining(&supported_groups_list) == 0 |
1224 | 1.80k | || (PACKET_remaining(&supported_groups_list) % 2) != 0) { |
1225 | 26 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1226 | 26 | return 0; |
1227 | 26 | } |
1228 | | |
1229 | 1.78k | if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { |
1230 | 1.78k | OPENSSL_free(s->ext.peer_supportedgroups); |
1231 | 1.78k | s->ext.peer_supportedgroups = NULL; |
1232 | 1.78k | s->ext.peer_supportedgroups_len = 0; |
1233 | 1.78k | if (!tls1_save_u16(&supported_groups_list, |
1234 | 1.78k | &s->ext.peer_supportedgroups, |
1235 | 1.78k | &s->ext.peer_supportedgroups_len)) { |
1236 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1237 | 0 | return 0; |
1238 | 0 | } |
1239 | 1.78k | } |
1240 | | |
1241 | 1.78k | return 1; |
1242 | 1.78k | } |
1243 | | |
1244 | | int tls_parse_ctos_ems(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1245 | | X509 *x, size_t chainidx) |
1246 | 571 | { |
1247 | | /* The extension must always be empty */ |
1248 | 571 | if (PACKET_remaining(pkt) != 0) { |
1249 | 1 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1250 | 1 | return 0; |
1251 | 1 | } |
1252 | | |
1253 | 570 | if (s->options & SSL_OP_NO_EXTENDED_MASTER_SECRET) |
1254 | 0 | return 1; |
1255 | | |
1256 | 570 | s->s3.flags |= TLS1_FLAGS_RECEIVED_EXTMS; |
1257 | | |
1258 | 570 | return 1; |
1259 | 570 | } |
1260 | | |
1261 | | |
1262 | | int tls_parse_ctos_early_data(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1263 | | X509 *x, size_t chainidx) |
1264 | 0 | { |
1265 | 0 | if (PACKET_remaining(pkt) != 0) { |
1266 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1267 | 0 | return 0; |
1268 | 0 | } |
1269 | | |
1270 | 0 | if (s->hello_retry_request != SSL_HRR_NONE) { |
1271 | 0 | SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
1272 | 0 | return 0; |
1273 | 0 | } |
1274 | | |
1275 | 0 | return 1; |
1276 | 0 | } |
1277 | | |
1278 | | static SSL_TICKET_STATUS tls_get_stateful_ticket(SSL_CONNECTION *s, PACKET *tick, |
1279 | | SSL_SESSION **sess) |
1280 | 0 | { |
1281 | 0 | SSL_SESSION *tmpsess = NULL; |
1282 | |
|
1283 | 0 | s->ext.ticket_expected = 1; |
1284 | |
|
1285 | 0 | switch (PACKET_remaining(tick)) { |
1286 | 0 | case 0: |
1287 | 0 | return SSL_TICKET_EMPTY; |
1288 | | |
1289 | 0 | case SSL_MAX_SSL_SESSION_ID_LENGTH: |
1290 | 0 | break; |
1291 | | |
1292 | 0 | default: |
1293 | 0 | return SSL_TICKET_NO_DECRYPT; |
1294 | 0 | } |
1295 | | |
1296 | 0 | tmpsess = lookup_sess_in_cache(s, PACKET_data(tick), |
1297 | 0 | SSL_MAX_SSL_SESSION_ID_LENGTH); |
1298 | |
|
1299 | 0 | if (tmpsess == NULL) |
1300 | 0 | return SSL_TICKET_NO_DECRYPT; |
1301 | | |
1302 | 0 | *sess = tmpsess; |
1303 | 0 | return SSL_TICKET_SUCCESS; |
1304 | 0 | } |
1305 | | |
1306 | | int tls_parse_ctos_psk(SSL_CONNECTION *s, PACKET *pkt, unsigned int context, |
1307 | | X509 *x, size_t chainidx) |
1308 | 0 | { |
1309 | 0 | PACKET identities, binders, binder; |
1310 | 0 | size_t binderoffset; |
1311 | 0 | int hashsize; |
1312 | 0 | SSL_SESSION *sess = NULL; |
1313 | 0 | unsigned int id, i, ext = 0; |
1314 | 0 | const EVP_MD *md = NULL; |
1315 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1316 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
1317 | | |
1318 | | /* |
1319 | | * If we have no PSK kex mode that we recognise then we can't resume so |
1320 | | * ignore this extension |
1321 | | */ |
1322 | 0 | if ((s->ext.psk_kex_mode |
1323 | 0 | & (TLSEXT_KEX_MODE_FLAG_KE | TLSEXT_KEX_MODE_FLAG_KE_DHE)) == 0) |
1324 | 0 | return 1; |
1325 | | |
1326 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &identities)) { |
1327 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1328 | 0 | return 0; |
1329 | 0 | } |
1330 | | |
1331 | 0 | s->ext.ticket_expected = 0; |
1332 | 0 | for (id = 0; PACKET_remaining(&identities) != 0; id++) { |
1333 | 0 | PACKET identity; |
1334 | 0 | unsigned long ticket_agel; |
1335 | 0 | size_t idlen; |
1336 | |
|
1337 | 0 | if (!PACKET_get_length_prefixed_2(&identities, &identity) |
1338 | 0 | || !PACKET_get_net_4(&identities, &ticket_agel)) { |
1339 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1340 | 0 | return 0; |
1341 | 0 | } |
1342 | | |
1343 | 0 | idlen = PACKET_remaining(&identity); |
1344 | 0 | if (s->psk_find_session_cb != NULL |
1345 | 0 | && !s->psk_find_session_cb(ussl, PACKET_data(&identity), idlen, |
1346 | 0 | &sess)) { |
1347 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_EXTENSION); |
1348 | 0 | return 0; |
1349 | 0 | } |
1350 | | |
1351 | 0 | #ifndef OPENSSL_NO_PSK |
1352 | 0 | if (sess == NULL |
1353 | 0 | && s->psk_server_callback != NULL |
1354 | 0 | && idlen <= PSK_MAX_IDENTITY_LEN) { |
1355 | 0 | char *pskid = NULL; |
1356 | 0 | unsigned char pskdata[PSK_MAX_PSK_LEN]; |
1357 | 0 | unsigned int pskdatalen; |
1358 | |
|
1359 | 0 | if (!PACKET_strndup(&identity, &pskid)) { |
1360 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1361 | 0 | return 0; |
1362 | 0 | } |
1363 | 0 | pskdatalen = s->psk_server_callback(ussl, pskid, pskdata, |
1364 | 0 | sizeof(pskdata)); |
1365 | 0 | OPENSSL_free(pskid); |
1366 | 0 | if (pskdatalen > PSK_MAX_PSK_LEN) { |
1367 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1368 | 0 | return 0; |
1369 | 0 | } else if (pskdatalen > 0) { |
1370 | 0 | const SSL_CIPHER *cipher; |
1371 | 0 | const unsigned char tls13_aes128gcmsha256_id[] = { 0x13, 0x01 }; |
1372 | | |
1373 | | /* |
1374 | | * We found a PSK using an old style callback. We don't know |
1375 | | * the digest so we default to SHA256 as per the TLSv1.3 spec |
1376 | | */ |
1377 | 0 | cipher = SSL_CIPHER_find(SSL_CONNECTION_GET_SSL(s), |
1378 | 0 | tls13_aes128gcmsha256_id); |
1379 | 0 | if (cipher == NULL) { |
1380 | 0 | OPENSSL_cleanse(pskdata, pskdatalen); |
1381 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1382 | 0 | return 0; |
1383 | 0 | } |
1384 | | |
1385 | 0 | sess = SSL_SESSION_new(); |
1386 | 0 | if (sess == NULL |
1387 | 0 | || !SSL_SESSION_set1_master_key(sess, pskdata, |
1388 | 0 | pskdatalen) |
1389 | 0 | || !SSL_SESSION_set_cipher(sess, cipher) |
1390 | 0 | || !SSL_SESSION_set_protocol_version(sess, |
1391 | 0 | TLS1_3_VERSION)) { |
1392 | 0 | OPENSSL_cleanse(pskdata, pskdatalen); |
1393 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1394 | 0 | goto err; |
1395 | 0 | } |
1396 | 0 | OPENSSL_cleanse(pskdata, pskdatalen); |
1397 | 0 | } |
1398 | 0 | } |
1399 | 0 | #endif /* OPENSSL_NO_PSK */ |
1400 | | |
1401 | 0 | if (sess != NULL) { |
1402 | | /* We found a PSK */ |
1403 | 0 | SSL_SESSION *sesstmp = ssl_session_dup(sess, 0); |
1404 | |
|
1405 | 0 | if (sesstmp == NULL) { |
1406 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1407 | 0 | goto err; |
1408 | 0 | } |
1409 | 0 | SSL_SESSION_free(sess); |
1410 | 0 | sess = sesstmp; |
1411 | | |
1412 | | /* |
1413 | | * We've just been told to use this session for this context so |
1414 | | * make sure the sid_ctx matches up. |
1415 | | */ |
1416 | 0 | memcpy(sess->sid_ctx, s->sid_ctx, s->sid_ctx_length); |
1417 | 0 | sess->sid_ctx_length = s->sid_ctx_length; |
1418 | 0 | ext = 1; |
1419 | 0 | if (id == 0) |
1420 | 0 | s->ext.early_data_ok = 1; |
1421 | 0 | s->ext.ticket_expected = 1; |
1422 | 0 | } else { |
1423 | 0 | OSSL_TIME t, age, expire; |
1424 | 0 | int ret; |
1425 | | |
1426 | | /* |
1427 | | * If we are using anti-replay protection then we behave as if |
1428 | | * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there |
1429 | | * is no point in using full stateless tickets. |
1430 | | */ |
1431 | 0 | if ((s->options & SSL_OP_NO_TICKET) != 0 |
1432 | 0 | || (s->max_early_data > 0 |
1433 | 0 | && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)) |
1434 | 0 | ret = tls_get_stateful_ticket(s, &identity, &sess); |
1435 | 0 | else |
1436 | 0 | ret = tls_decrypt_ticket(s, PACKET_data(&identity), |
1437 | 0 | PACKET_remaining(&identity), NULL, 0, |
1438 | 0 | &sess); |
1439 | |
|
1440 | 0 | if (ret == SSL_TICKET_EMPTY) { |
1441 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1442 | 0 | return 0; |
1443 | 0 | } |
1444 | | |
1445 | 0 | if (ret == SSL_TICKET_FATAL_ERR_MALLOC |
1446 | 0 | || ret == SSL_TICKET_FATAL_ERR_OTHER) { |
1447 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1448 | 0 | return 0; |
1449 | 0 | } |
1450 | 0 | if (ret == SSL_TICKET_NONE || ret == SSL_TICKET_NO_DECRYPT) |
1451 | 0 | continue; |
1452 | | |
1453 | | /* Check for replay */ |
1454 | 0 | if (s->max_early_data > 0 |
1455 | 0 | && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0 |
1456 | 0 | && !SSL_CTX_remove_session(s->session_ctx, sess)) { |
1457 | 0 | SSL_SESSION_free(sess); |
1458 | 0 | sess = NULL; |
1459 | 0 | continue; |
1460 | 0 | } |
1461 | | |
1462 | 0 | age = ossl_time_subtract(ossl_ms2time(ticket_agel), |
1463 | 0 | ossl_ms2time(sess->ext.tick_age_add)); |
1464 | 0 | t = ossl_time_subtract(ossl_time_now(), sess->time); |
1465 | | |
1466 | | /* |
1467 | | * Although internally we use OSS_TIME which has ns granularity, |
1468 | | * when SSL_SESSION structures are serialised/deserialised we use |
1469 | | * second granularity for the sess->time field. Therefore it could |
1470 | | * appear that the client's ticket age is longer than ours (our |
1471 | | * ticket age calculation should always be slightly longer than the |
1472 | | * client's due to the network latency). Therefore we add 1000ms to |
1473 | | * our age calculation to adjust for rounding errors. |
1474 | | */ |
1475 | 0 | expire = ossl_time_add(t, ossl_ms2time(1000)); |
1476 | |
|
1477 | 0 | if (id == 0 |
1478 | 0 | && ossl_time_compare(sess->timeout, t) >= 0 |
1479 | 0 | && ossl_time_compare(age, expire) <= 0 |
1480 | 0 | && ossl_time_compare(ossl_time_add(age, TICKET_AGE_ALLOWANCE), |
1481 | 0 | expire) >= 0) { |
1482 | | /* |
1483 | | * Ticket age is within tolerance and not expired. We allow it |
1484 | | * for early data |
1485 | | */ |
1486 | 0 | s->ext.early_data_ok = 1; |
1487 | 0 | } |
1488 | 0 | } |
1489 | | |
1490 | 0 | md = ssl_md(sctx, sess->cipher->algorithm2); |
1491 | 0 | if (md == NULL) { |
1492 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1493 | 0 | goto err; |
1494 | 0 | } |
1495 | 0 | if (!EVP_MD_is_a(md, |
1496 | 0 | EVP_MD_get0_name(ssl_md(sctx, |
1497 | 0 | s->s3.tmp.new_cipher->algorithm2)))) { |
1498 | | /* The ciphersuite is not compatible with this session. */ |
1499 | 0 | SSL_SESSION_free(sess); |
1500 | 0 | sess = NULL; |
1501 | 0 | s->ext.early_data_ok = 0; |
1502 | 0 | s->ext.ticket_expected = 0; |
1503 | 0 | continue; |
1504 | 0 | } |
1505 | 0 | break; |
1506 | 0 | } |
1507 | | |
1508 | 0 | if (sess == NULL) |
1509 | 0 | return 1; |
1510 | | |
1511 | 0 | binderoffset = PACKET_data(pkt) - (const unsigned char *)s->init_buf->data; |
1512 | 0 | hashsize = EVP_MD_get_size(md); |
1513 | 0 | if (hashsize <= 0) |
1514 | 0 | goto err; |
1515 | | |
1516 | 0 | if (!PACKET_get_length_prefixed_2(pkt, &binders)) { |
1517 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1518 | 0 | goto err; |
1519 | 0 | } |
1520 | | |
1521 | 0 | for (i = 0; i <= id; i++) { |
1522 | 0 | if (!PACKET_get_length_prefixed_1(&binders, &binder)) { |
1523 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1524 | 0 | goto err; |
1525 | 0 | } |
1526 | 0 | } |
1527 | | |
1528 | 0 | if (PACKET_remaining(&binder) != (size_t)hashsize) { |
1529 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
1530 | 0 | goto err; |
1531 | 0 | } |
1532 | 0 | if (tls_psk_do_binder(s, md, (const unsigned char *)s->init_buf->data, |
1533 | 0 | binderoffset, PACKET_data(&binder), NULL, sess, 0, |
1534 | 0 | ext) != 1) { |
1535 | | /* SSLfatal() already called */ |
1536 | 0 | goto err; |
1537 | 0 | } |
1538 | | |
1539 | 0 | s->ext.tick_identity = id; |
1540 | |
|
1541 | 0 | SSL_SESSION_free(s->session); |
1542 | 0 | s->session = sess; |
1543 | 0 | return 1; |
1544 | 0 | err: |
1545 | 0 | SSL_SESSION_free(sess); |
1546 | 0 | return 0; |
1547 | 0 | } |
1548 | | |
1549 | | int tls_parse_ctos_post_handshake_auth(SSL_CONNECTION *s, PACKET *pkt, |
1550 | | ossl_unused unsigned int context, |
1551 | | ossl_unused X509 *x, |
1552 | | ossl_unused size_t chainidx) |
1553 | 0 | { |
1554 | 0 | if (PACKET_remaining(pkt) != 0) { |
1555 | 0 | SSLfatal(s, SSL_AD_DECODE_ERROR, |
1556 | 0 | SSL_R_POST_HANDSHAKE_AUTH_ENCODING_ERR); |
1557 | 0 | return 0; |
1558 | 0 | } |
1559 | | |
1560 | 0 | s->post_handshake_auth = SSL_PHA_EXT_RECEIVED; |
1561 | |
|
1562 | 0 | return 1; |
1563 | 0 | } |
1564 | | |
1565 | | /* |
1566 | | * Add the server's renegotiation binding |
1567 | | */ |
1568 | | EXT_RETURN tls_construct_stoc_renegotiate(SSL_CONNECTION *s, WPACKET *pkt, |
1569 | | unsigned int context, X509 *x, |
1570 | | size_t chainidx) |
1571 | 2.71k | { |
1572 | 2.71k | if (!s->s3.send_connection_binding) |
1573 | 1.77k | return EXT_RETURN_NOT_SENT; |
1574 | | |
1575 | | /* Still add this even if SSL_OP_NO_RENEGOTIATION is set */ |
1576 | 938 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_renegotiate) |
1577 | 938 | || !WPACKET_start_sub_packet_u16(pkt) |
1578 | 938 | || !WPACKET_start_sub_packet_u8(pkt) |
1579 | 938 | || !WPACKET_memcpy(pkt, s->s3.previous_client_finished, |
1580 | 938 | s->s3.previous_client_finished_len) |
1581 | 938 | || !WPACKET_memcpy(pkt, s->s3.previous_server_finished, |
1582 | 938 | s->s3.previous_server_finished_len) |
1583 | 938 | || !WPACKET_close(pkt) |
1584 | 938 | || !WPACKET_close(pkt)) { |
1585 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1586 | 0 | return EXT_RETURN_FAIL; |
1587 | 0 | } |
1588 | | |
1589 | 938 | return EXT_RETURN_SENT; |
1590 | 938 | } |
1591 | | |
1592 | | EXT_RETURN tls_construct_stoc_server_name(SSL_CONNECTION *s, WPACKET *pkt, |
1593 | | unsigned int context, X509 *x, |
1594 | | size_t chainidx) |
1595 | 2.71k | { |
1596 | 2.71k | if (s->servername_done != 1) |
1597 | 2.71k | return EXT_RETURN_NOT_SENT; |
1598 | | |
1599 | | /* |
1600 | | * Prior to TLSv1.3 we ignore any SNI in the current handshake if resuming. |
1601 | | * We just use the servername from the initial handshake. |
1602 | | */ |
1603 | 0 | if (s->hit && !SSL_CONNECTION_IS_TLS13(s)) |
1604 | 0 | return EXT_RETURN_NOT_SENT; |
1605 | | |
1606 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_name) |
1607 | 0 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
1608 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1609 | 0 | return EXT_RETURN_FAIL; |
1610 | 0 | } |
1611 | | |
1612 | 0 | return EXT_RETURN_SENT; |
1613 | 0 | } |
1614 | | |
1615 | | /* Add/include the server's max fragment len extension into ServerHello */ |
1616 | | EXT_RETURN tls_construct_stoc_maxfragmentlen(SSL_CONNECTION *s, WPACKET *pkt, |
1617 | | unsigned int context, X509 *x, |
1618 | | size_t chainidx) |
1619 | 2.71k | { |
1620 | 2.71k | if (!USE_MAX_FRAGMENT_LENGTH_EXT(s->session)) |
1621 | 2.45k | return EXT_RETURN_NOT_SENT; |
1622 | | |
1623 | | /*- |
1624 | | * 4 bytes for this extension type and extension length |
1625 | | * 1 byte for the Max Fragment Length code value. |
1626 | | */ |
1627 | 263 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_max_fragment_length) |
1628 | 263 | || !WPACKET_start_sub_packet_u16(pkt) |
1629 | 263 | || !WPACKET_put_bytes_u8(pkt, s->session->ext.max_fragment_len_mode) |
1630 | 263 | || !WPACKET_close(pkt)) { |
1631 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1632 | 0 | return EXT_RETURN_FAIL; |
1633 | 0 | } |
1634 | | |
1635 | 263 | return EXT_RETURN_SENT; |
1636 | 263 | } |
1637 | | |
1638 | | EXT_RETURN tls_construct_stoc_ec_pt_formats(SSL_CONNECTION *s, WPACKET *pkt, |
1639 | | unsigned int context, X509 *x, |
1640 | | size_t chainidx) |
1641 | 2.71k | { |
1642 | 2.71k | unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
1643 | 2.71k | unsigned long alg_a = s->s3.tmp.new_cipher->algorithm_auth; |
1644 | 2.71k | int using_ecc = ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) |
1645 | 2.71k | && (s->ext.peer_ecpointformats != NULL); |
1646 | 2.71k | const unsigned char *plist; |
1647 | 2.71k | size_t plistlen; |
1648 | | |
1649 | 2.71k | if (!using_ecc) |
1650 | 2.01k | return EXT_RETURN_NOT_SENT; |
1651 | | |
1652 | 700 | tls1_get_formatlist(s, &plist, &plistlen); |
1653 | 700 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_ec_point_formats) |
1654 | 700 | || !WPACKET_start_sub_packet_u16(pkt) |
1655 | 700 | || !WPACKET_sub_memcpy_u8(pkt, plist, plistlen) |
1656 | 700 | || !WPACKET_close(pkt)) { |
1657 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1658 | 0 | return EXT_RETURN_FAIL; |
1659 | 0 | } |
1660 | | |
1661 | 700 | return EXT_RETURN_SENT; |
1662 | 700 | } |
1663 | | |
1664 | | EXT_RETURN tls_construct_stoc_supported_groups(SSL_CONNECTION *s, WPACKET *pkt, |
1665 | | unsigned int context, X509 *x, |
1666 | | size_t chainidx) |
1667 | 2.71k | { |
1668 | 2.71k | const uint16_t *groups; |
1669 | 2.71k | size_t numgroups, i, first = 1; |
1670 | 2.71k | int version; |
1671 | | |
1672 | | /* s->s3.group_id is non zero if we accepted a key_share */ |
1673 | 2.71k | if (s->s3.group_id == 0) |
1674 | 2.71k | return EXT_RETURN_NOT_SENT; |
1675 | | |
1676 | | /* Get our list of supported groups */ |
1677 | 0 | tls1_get_supported_groups(s, &groups, &numgroups); |
1678 | 0 | if (numgroups == 0) { |
1679 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1680 | 0 | return EXT_RETURN_FAIL; |
1681 | 0 | } |
1682 | | |
1683 | | /* Copy group ID if supported */ |
1684 | 0 | version = SSL_version(SSL_CONNECTION_GET_SSL(s)); |
1685 | 0 | for (i = 0; i < numgroups; i++) { |
1686 | 0 | uint16_t group = groups[i]; |
1687 | |
|
1688 | 0 | if (tls_valid_group(s, group, version, version, 0, NULL) |
1689 | 0 | && tls_group_allowed(s, group, SSL_SECOP_CURVE_SUPPORTED)) { |
1690 | 0 | if (first) { |
1691 | | /* |
1692 | | * Check if the client is already using our preferred group. If |
1693 | | * so we don't need to add this extension |
1694 | | */ |
1695 | 0 | if (s->s3.group_id == group) |
1696 | 0 | return EXT_RETURN_NOT_SENT; |
1697 | | |
1698 | | /* Add extension header */ |
1699 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_groups) |
1700 | | /* Sub-packet for supported_groups extension */ |
1701 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1702 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
1703 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1704 | 0 | return EXT_RETURN_FAIL; |
1705 | 0 | } |
1706 | | |
1707 | 0 | first = 0; |
1708 | 0 | } |
1709 | 0 | if (!WPACKET_put_bytes_u16(pkt, group)) { |
1710 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1711 | 0 | return EXT_RETURN_FAIL; |
1712 | 0 | } |
1713 | 0 | } |
1714 | 0 | } |
1715 | | |
1716 | 0 | if (!WPACKET_close(pkt) || !WPACKET_close(pkt)) { |
1717 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1718 | 0 | return EXT_RETURN_FAIL; |
1719 | 0 | } |
1720 | | |
1721 | 0 | return EXT_RETURN_SENT; |
1722 | 0 | } |
1723 | | |
1724 | | EXT_RETURN tls_construct_stoc_session_ticket(SSL_CONNECTION *s, WPACKET *pkt, |
1725 | | unsigned int context, X509 *x, |
1726 | | size_t chainidx) |
1727 | 2.71k | { |
1728 | 2.71k | if (!s->ext.ticket_expected || !tls_use_ticket(s)) { |
1729 | 2.45k | s->ext.ticket_expected = 0; |
1730 | 2.45k | return EXT_RETURN_NOT_SENT; |
1731 | 2.45k | } |
1732 | | |
1733 | 258 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_session_ticket) |
1734 | 258 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
1735 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1736 | 0 | return EXT_RETURN_FAIL; |
1737 | 0 | } |
1738 | | |
1739 | 258 | return EXT_RETURN_SENT; |
1740 | 258 | } |
1741 | | |
1742 | | #ifndef OPENSSL_NO_OCSP |
1743 | | EXT_RETURN tls_construct_stoc_status_request(SSL_CONNECTION *s, WPACKET *pkt, |
1744 | | unsigned int context, X509 *x, |
1745 | | size_t chainidx) |
1746 | 2.71k | { |
1747 | | /* We don't currently support this extension inside a CertificateRequest */ |
1748 | 2.71k | if (context == SSL_EXT_TLS1_3_CERTIFICATE_REQUEST) |
1749 | 0 | return EXT_RETURN_NOT_SENT; |
1750 | | |
1751 | 2.71k | if (!s->ext.status_expected) |
1752 | 2.71k | return EXT_RETURN_NOT_SENT; |
1753 | | |
1754 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && chainidx != 0) |
1755 | 0 | return EXT_RETURN_NOT_SENT; |
1756 | | |
1757 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_status_request) |
1758 | 0 | || !WPACKET_start_sub_packet_u16(pkt)) { |
1759 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1760 | 0 | return EXT_RETURN_FAIL; |
1761 | 0 | } |
1762 | | |
1763 | | /* |
1764 | | * In TLSv1.3 we include the certificate status itself. In <= TLSv1.2 we |
1765 | | * send back an empty extension, with the certificate status appearing as a |
1766 | | * separate message |
1767 | | */ |
1768 | 0 | if (SSL_CONNECTION_IS_TLS13(s) && !tls_construct_cert_status_body(s, pkt)) { |
1769 | | /* SSLfatal() already called */ |
1770 | 0 | return EXT_RETURN_FAIL; |
1771 | 0 | } |
1772 | 0 | if (!WPACKET_close(pkt)) { |
1773 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1774 | 0 | return EXT_RETURN_FAIL; |
1775 | 0 | } |
1776 | | |
1777 | 0 | return EXT_RETURN_SENT; |
1778 | 0 | } |
1779 | | #endif |
1780 | | |
1781 | | #ifndef OPENSSL_NO_NEXTPROTONEG |
1782 | | EXT_RETURN tls_construct_stoc_next_proto_neg(SSL_CONNECTION *s, WPACKET *pkt, |
1783 | | unsigned int context, X509 *x, |
1784 | | size_t chainidx) |
1785 | 2.71k | { |
1786 | 2.71k | const unsigned char *npa; |
1787 | 2.71k | unsigned int npalen; |
1788 | 2.71k | int ret; |
1789 | 2.71k | int npn_seen = s->s3.npn_seen; |
1790 | 2.71k | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
1791 | | |
1792 | 2.71k | s->s3.npn_seen = 0; |
1793 | 2.71k | if (!npn_seen || sctx->ext.npn_advertised_cb == NULL) |
1794 | 2.71k | return EXT_RETURN_NOT_SENT; |
1795 | | |
1796 | 0 | ret = sctx->ext.npn_advertised_cb(SSL_CONNECTION_GET_USER_SSL(s), &npa, |
1797 | 0 | &npalen, sctx->ext.npn_advertised_cb_arg); |
1798 | 0 | if (ret == SSL_TLSEXT_ERR_OK) { |
1799 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_next_proto_neg) |
1800 | 0 | || !WPACKET_sub_memcpy_u16(pkt, npa, npalen)) { |
1801 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1802 | 0 | return EXT_RETURN_FAIL; |
1803 | 0 | } |
1804 | 0 | s->s3.npn_seen = 1; |
1805 | 0 | return EXT_RETURN_SENT; |
1806 | 0 | } |
1807 | | |
1808 | 0 | return EXT_RETURN_NOT_SENT; |
1809 | 0 | } |
1810 | | #endif |
1811 | | |
1812 | | EXT_RETURN tls_construct_stoc_alpn(SSL_CONNECTION *s, WPACKET *pkt, unsigned int context, |
1813 | | X509 *x, size_t chainidx) |
1814 | 2.71k | { |
1815 | 2.71k | if (s->s3.alpn_selected == NULL) |
1816 | 2.71k | return EXT_RETURN_NOT_SENT; |
1817 | | |
1818 | 0 | if (!WPACKET_put_bytes_u16(pkt, |
1819 | 0 | TLSEXT_TYPE_application_layer_protocol_negotiation) |
1820 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1821 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1822 | 0 | || !WPACKET_sub_memcpy_u8(pkt, s->s3.alpn_selected, |
1823 | 0 | s->s3.alpn_selected_len) |
1824 | 0 | || !WPACKET_close(pkt) |
1825 | 0 | || !WPACKET_close(pkt)) { |
1826 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1827 | 0 | return EXT_RETURN_FAIL; |
1828 | 0 | } |
1829 | | |
1830 | 0 | return EXT_RETURN_SENT; |
1831 | 0 | } |
1832 | | |
1833 | | #ifndef OPENSSL_NO_SRTP |
1834 | | EXT_RETURN tls_construct_stoc_use_srtp(SSL_CONNECTION *s, WPACKET *pkt, |
1835 | | unsigned int context, X509 *x, |
1836 | | size_t chainidx) |
1837 | 2.71k | { |
1838 | 2.71k | if (s->srtp_profile == NULL) |
1839 | 2.71k | return EXT_RETURN_NOT_SENT; |
1840 | | |
1841 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_use_srtp) |
1842 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1843 | 0 | || !WPACKET_put_bytes_u16(pkt, 2) |
1844 | 0 | || !WPACKET_put_bytes_u16(pkt, s->srtp_profile->id) |
1845 | 0 | || !WPACKET_put_bytes_u8(pkt, 0) |
1846 | 0 | || !WPACKET_close(pkt)) { |
1847 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1848 | 0 | return EXT_RETURN_FAIL; |
1849 | 0 | } |
1850 | | |
1851 | 0 | return EXT_RETURN_SENT; |
1852 | 0 | } |
1853 | | #endif |
1854 | | |
1855 | | EXT_RETURN tls_construct_stoc_etm(SSL_CONNECTION *s, WPACKET *pkt, |
1856 | | unsigned int context, |
1857 | | X509 *x, size_t chainidx) |
1858 | 2.71k | { |
1859 | 2.71k | if (!s->ext.use_etm) |
1860 | 2.59k | return EXT_RETURN_NOT_SENT; |
1861 | | |
1862 | | /* |
1863 | | * Don't use encrypt_then_mac if AEAD or RC4 might want to disable |
1864 | | * for other cases too. |
1865 | | */ |
1866 | 123 | if (s->s3.tmp.new_cipher->algorithm_mac == SSL_AEAD |
1867 | 123 | || s->s3.tmp.new_cipher->algorithm_enc == SSL_RC4 |
1868 | 123 | || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT |
1869 | 123 | || s->s3.tmp.new_cipher->algorithm_enc == SSL_eGOST2814789CNT12 |
1870 | 123 | || s->s3.tmp.new_cipher->algorithm_enc == SSL_MAGMA |
1871 | 123 | || s->s3.tmp.new_cipher->algorithm_enc == SSL_KUZNYECHIK) { |
1872 | 5 | s->ext.use_etm = 0; |
1873 | 5 | return EXT_RETURN_NOT_SENT; |
1874 | 5 | } |
1875 | | |
1876 | 118 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_encrypt_then_mac) |
1877 | 118 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
1878 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1879 | 0 | return EXT_RETURN_FAIL; |
1880 | 0 | } |
1881 | | |
1882 | 118 | return EXT_RETURN_SENT; |
1883 | 118 | } |
1884 | | |
1885 | | EXT_RETURN tls_construct_stoc_ems(SSL_CONNECTION *s, WPACKET *pkt, |
1886 | | unsigned int context, |
1887 | | X509 *x, size_t chainidx) |
1888 | 2.71k | { |
1889 | 2.71k | if ((s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) == 0) |
1890 | 2.15k | return EXT_RETURN_NOT_SENT; |
1891 | | |
1892 | 565 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_extended_master_secret) |
1893 | 565 | || !WPACKET_put_bytes_u16(pkt, 0)) { |
1894 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1895 | 0 | return EXT_RETURN_FAIL; |
1896 | 0 | } |
1897 | | |
1898 | 565 | return EXT_RETURN_SENT; |
1899 | 565 | } |
1900 | | |
1901 | | EXT_RETURN tls_construct_stoc_supported_versions(SSL_CONNECTION *s, WPACKET *pkt, |
1902 | | unsigned int context, X509 *x, |
1903 | | size_t chainidx) |
1904 | 0 | { |
1905 | 0 | if (!ossl_assert(SSL_CONNECTION_IS_TLS13(s))) { |
1906 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1907 | 0 | return EXT_RETURN_FAIL; |
1908 | 0 | } |
1909 | | |
1910 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_supported_versions) |
1911 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1912 | 0 | || !WPACKET_put_bytes_u16(pkt, s->version) |
1913 | 0 | || !WPACKET_close(pkt)) { |
1914 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1915 | 0 | return EXT_RETURN_FAIL; |
1916 | 0 | } |
1917 | | |
1918 | 0 | return EXT_RETURN_SENT; |
1919 | 0 | } |
1920 | | |
1921 | | EXT_RETURN tls_construct_stoc_key_share(SSL_CONNECTION *s, WPACKET *pkt, |
1922 | | unsigned int context, X509 *x, |
1923 | | size_t chainidx) |
1924 | 0 | { |
1925 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
1926 | 0 | unsigned char *encoded_pubkey; |
1927 | 0 | size_t encoded_pubkey_len = 0; |
1928 | 0 | EVP_PKEY *ckey = s->s3.peer_tmp, *skey = NULL; |
1929 | 0 | const TLS_GROUP_INFO *ginf = NULL; |
1930 | |
|
1931 | 0 | if (s->hello_retry_request == SSL_HRR_PENDING) { |
1932 | 0 | if (ckey != NULL) { |
1933 | | /* Original key_share was acceptable so don't ask for another one */ |
1934 | 0 | return EXT_RETURN_NOT_SENT; |
1935 | 0 | } |
1936 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
1937 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1938 | 0 | || !WPACKET_put_bytes_u16(pkt, s->s3.group_id) |
1939 | 0 | || !WPACKET_close(pkt)) { |
1940 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1941 | 0 | return EXT_RETURN_FAIL; |
1942 | 0 | } |
1943 | | |
1944 | 0 | return EXT_RETURN_SENT; |
1945 | 0 | } |
1946 | | |
1947 | 0 | if (ckey == NULL) { |
1948 | | /* No key_share received from client - must be resuming */ |
1949 | 0 | if (!s->hit || !tls13_generate_handshake_secret(s, NULL, 0)) { |
1950 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1951 | 0 | return EXT_RETURN_FAIL; |
1952 | 0 | } |
1953 | 0 | return EXT_RETURN_NOT_SENT; |
1954 | 0 | } |
1955 | | |
1956 | 0 | if (s->hit && (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE) == 0) { |
1957 | | /* |
1958 | | * PSK ('hit') and explicitly not doing DHE. If the client sent the |
1959 | | * DHE option, we take it by default, except if non-DHE would be |
1960 | | * preferred by config, but this case would have been handled in |
1961 | | * tls_parse_ctos_psk_kex_modes(). |
1962 | | */ |
1963 | 0 | return EXT_RETURN_NOT_SENT; |
1964 | 0 | } |
1965 | | |
1966 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_key_share) |
1967 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
1968 | 0 | || !WPACKET_put_bytes_u16(pkt, s->s3.group_id)) { |
1969 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1970 | 0 | return EXT_RETURN_FAIL; |
1971 | 0 | } |
1972 | | |
1973 | 0 | if ((ginf = tls1_group_id_lookup(SSL_CONNECTION_GET_CTX(s), |
1974 | 0 | s->s3.group_id)) == NULL) { |
1975 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1976 | 0 | return EXT_RETURN_FAIL; |
1977 | 0 | } |
1978 | | |
1979 | 0 | if (!ginf->is_kem) { |
1980 | | /* Regular KEX */ |
1981 | 0 | skey = ssl_generate_pkey(s, ckey); |
1982 | 0 | if (skey == NULL) { |
1983 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
1984 | 0 | return EXT_RETURN_FAIL; |
1985 | 0 | } |
1986 | | |
1987 | | /* Generate encoding of server key */ |
1988 | 0 | encoded_pubkey_len = EVP_PKEY_get1_encoded_public_key(skey, &encoded_pubkey); |
1989 | 0 | if (encoded_pubkey_len == 0) { |
1990 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
1991 | 0 | EVP_PKEY_free(skey); |
1992 | 0 | return EXT_RETURN_FAIL; |
1993 | 0 | } |
1994 | | |
1995 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, encoded_pubkey, encoded_pubkey_len) |
1996 | 0 | || !WPACKET_close(pkt)) { |
1997 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
1998 | 0 | EVP_PKEY_free(skey); |
1999 | 0 | OPENSSL_free(encoded_pubkey); |
2000 | 0 | return EXT_RETURN_FAIL; |
2001 | 0 | } |
2002 | 0 | OPENSSL_free(encoded_pubkey); |
2003 | | |
2004 | | /* |
2005 | | * This causes the crypto state to be updated based on the derived keys |
2006 | | */ |
2007 | 0 | s->s3.tmp.pkey = skey; |
2008 | 0 | if (ssl_derive(s, skey, ckey, 1) == 0) { |
2009 | | /* SSLfatal() already called */ |
2010 | 0 | return EXT_RETURN_FAIL; |
2011 | 0 | } |
2012 | 0 | } else { |
2013 | | /* KEM mode */ |
2014 | 0 | unsigned char *ct = NULL; |
2015 | 0 | size_t ctlen = 0; |
2016 | | |
2017 | | /* |
2018 | | * This does not update the crypto state. |
2019 | | * |
2020 | | * The generated pms is stored in `s->s3.tmp.pms` to be later used via |
2021 | | * ssl_gensecret(). |
2022 | | */ |
2023 | 0 | if (ssl_encapsulate(s, ckey, &ct, &ctlen, 0) == 0) { |
2024 | | /* SSLfatal() already called */ |
2025 | 0 | return EXT_RETURN_FAIL; |
2026 | 0 | } |
2027 | | |
2028 | 0 | if (ctlen == 0) { |
2029 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2030 | 0 | OPENSSL_free(ct); |
2031 | 0 | return EXT_RETURN_FAIL; |
2032 | 0 | } |
2033 | | |
2034 | 0 | if (!WPACKET_sub_memcpy_u16(pkt, ct, ctlen) |
2035 | 0 | || !WPACKET_close(pkt)) { |
2036 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2037 | 0 | OPENSSL_free(ct); |
2038 | 0 | return EXT_RETURN_FAIL; |
2039 | 0 | } |
2040 | 0 | OPENSSL_free(ct); |
2041 | | |
2042 | | /* |
2043 | | * This causes the crypto state to be updated based on the generated pms |
2044 | | */ |
2045 | 0 | if (ssl_gensecret(s, s->s3.tmp.pms, s->s3.tmp.pmslen) == 0) { |
2046 | | /* SSLfatal() already called */ |
2047 | 0 | return EXT_RETURN_FAIL; |
2048 | 0 | } |
2049 | 0 | } |
2050 | 0 | s->s3.did_kex = 1; |
2051 | 0 | return EXT_RETURN_SENT; |
2052 | | #else |
2053 | | return EXT_RETURN_FAIL; |
2054 | | #endif |
2055 | 0 | } |
2056 | | |
2057 | | EXT_RETURN tls_construct_stoc_cookie(SSL_CONNECTION *s, WPACKET *pkt, |
2058 | | unsigned int context, |
2059 | | X509 *x, size_t chainidx) |
2060 | 0 | { |
2061 | 0 | #ifndef OPENSSL_NO_TLS1_3 |
2062 | 0 | unsigned char *hashval1, *hashval2, *appcookie1, *appcookie2, *cookie; |
2063 | 0 | unsigned char *hmac, *hmac2; |
2064 | 0 | size_t startlen, ciphlen, totcookielen, hashlen, hmaclen, appcookielen; |
2065 | 0 | EVP_MD_CTX *hctx; |
2066 | 0 | EVP_PKEY *pkey; |
2067 | 0 | int ret = EXT_RETURN_FAIL; |
2068 | 0 | SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
2069 | 0 | SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
2070 | 0 | SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
2071 | |
|
2072 | 0 | if ((s->s3.flags & TLS1_FLAGS_STATELESS) == 0) |
2073 | 0 | return EXT_RETURN_NOT_SENT; |
2074 | | |
2075 | 0 | if (sctx->gen_stateless_cookie_cb == NULL) { |
2076 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_COOKIE_CALLBACK_SET); |
2077 | 0 | return EXT_RETURN_FAIL; |
2078 | 0 | } |
2079 | | |
2080 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_cookie) |
2081 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2082 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2083 | 0 | || !WPACKET_get_total_written(pkt, &startlen) |
2084 | 0 | || !WPACKET_reserve_bytes(pkt, MAX_COOKIE_SIZE, &cookie) |
2085 | 0 | || !WPACKET_put_bytes_u16(pkt, COOKIE_STATE_FORMAT_VERSION) |
2086 | 0 | || !WPACKET_put_bytes_u16(pkt, TLS1_3_VERSION) |
2087 | 0 | || !WPACKET_put_bytes_u16(pkt, s->s3.group_id) |
2088 | 0 | || !ssl->method->put_cipher_by_char(s->s3.tmp.new_cipher, pkt, |
2089 | 0 | &ciphlen) |
2090 | | /* Is there a key_share extension present in this HRR? */ |
2091 | 0 | || !WPACKET_put_bytes_u8(pkt, s->s3.peer_tmp == NULL) |
2092 | 0 | || !WPACKET_put_bytes_u64(pkt, time(NULL)) |
2093 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2094 | 0 | || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &hashval1)) { |
2095 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2096 | 0 | return EXT_RETURN_FAIL; |
2097 | 0 | } |
2098 | | |
2099 | | /* |
2100 | | * Get the hash of the initial ClientHello. ssl_handshake_hash() operates |
2101 | | * on raw buffers, so we first reserve sufficient bytes (above) and then |
2102 | | * subsequently allocate them (below) |
2103 | | */ |
2104 | 0 | if (!ssl3_digest_cached_records(s, 0) |
2105 | 0 | || !ssl_handshake_hash(s, hashval1, EVP_MAX_MD_SIZE, &hashlen)) { |
2106 | | /* SSLfatal() already called */ |
2107 | 0 | return EXT_RETURN_FAIL; |
2108 | 0 | } |
2109 | | |
2110 | 0 | if (!WPACKET_allocate_bytes(pkt, hashlen, &hashval2) |
2111 | 0 | || !ossl_assert(hashval1 == hashval2) |
2112 | 0 | || !WPACKET_close(pkt) |
2113 | 0 | || !WPACKET_start_sub_packet_u8(pkt) |
2114 | 0 | || !WPACKET_reserve_bytes(pkt, SSL_COOKIE_LENGTH, &appcookie1)) { |
2115 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2116 | 0 | return EXT_RETURN_FAIL; |
2117 | 0 | } |
2118 | | |
2119 | | /* Generate the application cookie */ |
2120 | 0 | if (sctx->gen_stateless_cookie_cb(ussl, appcookie1, |
2121 | 0 | &appcookielen) == 0) { |
2122 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); |
2123 | 0 | return EXT_RETURN_FAIL; |
2124 | 0 | } |
2125 | | |
2126 | 0 | if (!WPACKET_allocate_bytes(pkt, appcookielen, &appcookie2) |
2127 | 0 | || !ossl_assert(appcookie1 == appcookie2) |
2128 | 0 | || !WPACKET_close(pkt) |
2129 | 0 | || !WPACKET_get_total_written(pkt, &totcookielen) |
2130 | 0 | || !WPACKET_reserve_bytes(pkt, SHA256_DIGEST_LENGTH, &hmac)) { |
2131 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2132 | 0 | return EXT_RETURN_FAIL; |
2133 | 0 | } |
2134 | 0 | hmaclen = SHA256_DIGEST_LENGTH; |
2135 | |
|
2136 | 0 | totcookielen -= startlen; |
2137 | 0 | if (!ossl_assert(totcookielen <= MAX_COOKIE_SIZE - SHA256_DIGEST_LENGTH)) { |
2138 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2139 | 0 | return EXT_RETURN_FAIL; |
2140 | 0 | } |
2141 | | |
2142 | | /* HMAC the cookie */ |
2143 | 0 | hctx = EVP_MD_CTX_create(); |
2144 | 0 | pkey = EVP_PKEY_new_raw_private_key_ex(sctx->libctx, "HMAC", |
2145 | 0 | sctx->propq, |
2146 | 0 | s->session_ctx->ext.cookie_hmac_key, |
2147 | 0 | sizeof(s->session_ctx->ext.cookie_hmac_key)); |
2148 | 0 | if (hctx == NULL || pkey == NULL) { |
2149 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
2150 | 0 | goto err; |
2151 | 0 | } |
2152 | | |
2153 | 0 | if (EVP_DigestSignInit_ex(hctx, NULL, "SHA2-256", sctx->libctx, |
2154 | 0 | sctx->propq, pkey, NULL) <= 0 |
2155 | 0 | || EVP_DigestSign(hctx, hmac, &hmaclen, cookie, |
2156 | 0 | totcookielen) <= 0) { |
2157 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2158 | 0 | goto err; |
2159 | 0 | } |
2160 | | |
2161 | 0 | if (!ossl_assert(totcookielen + hmaclen <= MAX_COOKIE_SIZE)) { |
2162 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2163 | 0 | goto err; |
2164 | 0 | } |
2165 | | |
2166 | 0 | if (!WPACKET_allocate_bytes(pkt, hmaclen, &hmac2) |
2167 | 0 | || !ossl_assert(hmac == hmac2) |
2168 | 0 | || !ossl_assert(cookie == hmac - totcookielen) |
2169 | 0 | || !WPACKET_close(pkt) |
2170 | 0 | || !WPACKET_close(pkt)) { |
2171 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2172 | 0 | goto err; |
2173 | 0 | } |
2174 | | |
2175 | 0 | ret = EXT_RETURN_SENT; |
2176 | |
|
2177 | 0 | err: |
2178 | 0 | EVP_MD_CTX_free(hctx); |
2179 | 0 | EVP_PKEY_free(pkey); |
2180 | 0 | return ret; |
2181 | | #else |
2182 | | return EXT_RETURN_FAIL; |
2183 | | #endif |
2184 | 0 | } |
2185 | | |
2186 | | EXT_RETURN tls_construct_stoc_cryptopro_bug(SSL_CONNECTION *s, WPACKET *pkt, |
2187 | | unsigned int context, X509 *x, |
2188 | | size_t chainidx) |
2189 | 2.71k | { |
2190 | 2.71k | const unsigned char cryptopro_ext[36] = { |
2191 | 2.71k | 0xfd, 0xe8, /* 65000 */ |
2192 | 2.71k | 0x00, 0x20, /* 32 bytes length */ |
2193 | 2.71k | 0x30, 0x1e, 0x30, 0x08, 0x06, 0x06, 0x2a, 0x85, |
2194 | 2.71k | 0x03, 0x02, 0x02, 0x09, 0x30, 0x08, 0x06, 0x06, |
2195 | 2.71k | 0x2a, 0x85, 0x03, 0x02, 0x02, 0x16, 0x30, 0x08, |
2196 | 2.71k | 0x06, 0x06, 0x2a, 0x85, 0x03, 0x02, 0x02, 0x17 |
2197 | 2.71k | }; |
2198 | | |
2199 | 2.71k | if (((s->s3.tmp.new_cipher->id & 0xFFFF) != 0x80 |
2200 | 2.71k | && (s->s3.tmp.new_cipher->id & 0xFFFF) != 0x81) |
2201 | 2.71k | || (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) |
2202 | 0 | & SSL_OP_CRYPTOPRO_TLSEXT_BUG) == 0) |
2203 | 2.71k | return EXT_RETURN_NOT_SENT; |
2204 | | |
2205 | 0 | if (!WPACKET_memcpy(pkt, cryptopro_ext, sizeof(cryptopro_ext))) { |
2206 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2207 | 0 | return EXT_RETURN_FAIL; |
2208 | 0 | } |
2209 | | |
2210 | 0 | return EXT_RETURN_SENT; |
2211 | 0 | } |
2212 | | |
2213 | | EXT_RETURN tls_construct_stoc_early_data(SSL_CONNECTION *s, WPACKET *pkt, |
2214 | | unsigned int context, X509 *x, |
2215 | | size_t chainidx) |
2216 | 0 | { |
2217 | 0 | if (context == SSL_EXT_TLS1_3_NEW_SESSION_TICKET) { |
2218 | 0 | if (s->max_early_data == 0) |
2219 | 0 | return EXT_RETURN_NOT_SENT; |
2220 | | |
2221 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) |
2222 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2223 | 0 | || !WPACKET_put_bytes_u32(pkt, s->max_early_data) |
2224 | 0 | || !WPACKET_close(pkt)) { |
2225 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2226 | 0 | return EXT_RETURN_FAIL; |
2227 | 0 | } |
2228 | | |
2229 | 0 | return EXT_RETURN_SENT; |
2230 | 0 | } |
2231 | | |
2232 | 0 | if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED) |
2233 | 0 | return EXT_RETURN_NOT_SENT; |
2234 | | |
2235 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_early_data) |
2236 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2237 | 0 | || !WPACKET_close(pkt)) { |
2238 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2239 | 0 | return EXT_RETURN_FAIL; |
2240 | 0 | } |
2241 | | |
2242 | 0 | return EXT_RETURN_SENT; |
2243 | 0 | } |
2244 | | |
2245 | | EXT_RETURN tls_construct_stoc_psk(SSL_CONNECTION *s, WPACKET *pkt, |
2246 | | unsigned int context, |
2247 | | X509 *x, size_t chainidx) |
2248 | 0 | { |
2249 | 0 | if (!s->hit) |
2250 | 0 | return EXT_RETURN_NOT_SENT; |
2251 | | |
2252 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_psk) |
2253 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2254 | 0 | || !WPACKET_put_bytes_u16(pkt, s->ext.tick_identity) |
2255 | 0 | || !WPACKET_close(pkt)) { |
2256 | 0 | SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2257 | 0 | return EXT_RETURN_FAIL; |
2258 | 0 | } |
2259 | | |
2260 | 0 | return EXT_RETURN_SENT; |
2261 | 0 | } |
2262 | | |
2263 | | EXT_RETURN tls_construct_stoc_client_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2264 | | unsigned int context, |
2265 | | X509 *x, size_t chainidx) |
2266 | 2.71k | { |
2267 | 2.71k | if (sc->ext.client_cert_type_ctos == OSSL_CERT_TYPE_CTOS_ERROR |
2268 | 2.71k | && (send_certificate_request(sc) |
2269 | 0 | || sc->post_handshake_auth == SSL_PHA_EXT_RECEIVED)) { |
2270 | | /* Did not receive an acceptable cert type - and doing client auth */ |
2271 | 0 | SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION); |
2272 | 0 | return EXT_RETURN_FAIL; |
2273 | 0 | } |
2274 | | |
2275 | 2.71k | if (sc->ext.client_cert_type == TLSEXT_cert_type_x509) { |
2276 | 2.71k | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2277 | 2.71k | return EXT_RETURN_NOT_SENT; |
2278 | 2.71k | } |
2279 | | |
2280 | | /* |
2281 | | * Note: only supposed to send this if we are going to do a cert request, |
2282 | | * but TLSv1.3 could do a PHA request if the client supports it |
2283 | | */ |
2284 | 0 | if ((!send_certificate_request(sc) && sc->post_handshake_auth != SSL_PHA_EXT_RECEIVED) |
2285 | 0 | || sc->ext.client_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD |
2286 | 0 | || sc->client_cert_type == NULL) { |
2287 | | /* if we don't send it, reset to TLSEXT_cert_type_x509 */ |
2288 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2289 | 0 | sc->ext.client_cert_type = TLSEXT_cert_type_x509; |
2290 | 0 | return EXT_RETURN_NOT_SENT; |
2291 | 0 | } |
2292 | | |
2293 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_client_cert_type) |
2294 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2295 | 0 | || !WPACKET_put_bytes_u8(pkt, sc->ext.client_cert_type) |
2296 | 0 | || !WPACKET_close(pkt)) { |
2297 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2298 | 0 | return EXT_RETURN_FAIL; |
2299 | 0 | } |
2300 | 0 | return EXT_RETURN_SENT; |
2301 | 0 | } |
2302 | | |
2303 | | /* One of |pref|, |other| is configured and the values are sanitized */ |
2304 | | static int reconcile_cert_type(const unsigned char *pref, size_t pref_len, |
2305 | | const unsigned char *other, size_t other_len, |
2306 | | uint8_t *chosen_cert_type) |
2307 | 0 | { |
2308 | 0 | size_t i; |
2309 | |
|
2310 | 0 | for (i = 0; i < pref_len; i++) { |
2311 | 0 | if (memchr(other, pref[i], other_len) != NULL) { |
2312 | 0 | *chosen_cert_type = pref[i]; |
2313 | 0 | return OSSL_CERT_TYPE_CTOS_GOOD; |
2314 | 0 | } |
2315 | 0 | } |
2316 | 0 | return OSSL_CERT_TYPE_CTOS_ERROR; |
2317 | 0 | } |
2318 | | |
2319 | | int tls_parse_ctos_client_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2320 | | unsigned int context, |
2321 | | X509 *x, size_t chainidx) |
2322 | 10 | { |
2323 | 10 | PACKET supported_cert_types; |
2324 | 10 | const unsigned char *data; |
2325 | 10 | size_t len; |
2326 | | |
2327 | | /* Ignore the extension */ |
2328 | 10 | if (sc->client_cert_type == NULL) { |
2329 | 10 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2330 | 10 | sc->ext.client_cert_type = TLSEXT_cert_type_x509; |
2331 | 10 | return 1; |
2332 | 10 | } |
2333 | | |
2334 | 0 | if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) { |
2335 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR; |
2336 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2337 | 0 | return 0; |
2338 | 0 | } |
2339 | 0 | if ((len = PACKET_remaining(&supported_cert_types)) == 0) { |
2340 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR; |
2341 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2342 | 0 | return 0; |
2343 | 0 | } |
2344 | 0 | if (!PACKET_get_bytes(&supported_cert_types, &data, len)) { |
2345 | 0 | sc->ext.client_cert_type_ctos = OSSL_CERT_TYPE_CTOS_ERROR; |
2346 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2347 | 0 | return 0; |
2348 | 0 | } |
2349 | | /* client_cert_type: client (peer) has priority */ |
2350 | 0 | sc->ext.client_cert_type_ctos = reconcile_cert_type(data, len, |
2351 | 0 | sc->client_cert_type, sc->client_cert_type_len, |
2352 | 0 | &sc->ext.client_cert_type); |
2353 | | |
2354 | | /* Ignore the error until sending - so we can check cert auth*/ |
2355 | 0 | return 1; |
2356 | 0 | } |
2357 | | |
2358 | | EXT_RETURN tls_construct_stoc_server_cert_type(SSL_CONNECTION *sc, WPACKET *pkt, |
2359 | | unsigned int context, |
2360 | | X509 *x, size_t chainidx) |
2361 | 2.71k | { |
2362 | 2.71k | if (sc->ext.server_cert_type == TLSEXT_cert_type_x509) { |
2363 | 2.71k | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2364 | 2.71k | return EXT_RETURN_NOT_SENT; |
2365 | 2.71k | } |
2366 | 0 | if (sc->ext.server_cert_type_ctos != OSSL_CERT_TYPE_CTOS_GOOD |
2367 | 0 | || sc->server_cert_type == NULL) { |
2368 | | /* if we don't send it, reset to TLSEXT_cert_type_x509 */ |
2369 | 0 | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2370 | 0 | sc->ext.server_cert_type = TLSEXT_cert_type_x509; |
2371 | 0 | return EXT_RETURN_NOT_SENT; |
2372 | 0 | } |
2373 | | |
2374 | 0 | if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_server_cert_type) |
2375 | 0 | || !WPACKET_start_sub_packet_u16(pkt) |
2376 | 0 | || !WPACKET_put_bytes_u8(pkt, sc->ext.server_cert_type) |
2377 | 0 | || !WPACKET_close(pkt)) { |
2378 | 0 | SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
2379 | 0 | return EXT_RETURN_FAIL; |
2380 | 0 | } |
2381 | 0 | return EXT_RETURN_SENT; |
2382 | 0 | } |
2383 | | |
2384 | | int tls_parse_ctos_server_cert_type(SSL_CONNECTION *sc, PACKET *pkt, |
2385 | | unsigned int context, |
2386 | | X509 *x, size_t chainidx) |
2387 | 10 | { |
2388 | 10 | PACKET supported_cert_types; |
2389 | 10 | const unsigned char *data; |
2390 | 10 | size_t len; |
2391 | | |
2392 | | /* Ignore the extension */ |
2393 | 10 | if (sc->server_cert_type == NULL) { |
2394 | 10 | sc->ext.server_cert_type_ctos = OSSL_CERT_TYPE_CTOS_NONE; |
2395 | 10 | sc->ext.server_cert_type = TLSEXT_cert_type_x509; |
2396 | 10 | return 1; |
2397 | 10 | } |
2398 | | |
2399 | 0 | if (!PACKET_as_length_prefixed_1(pkt, &supported_cert_types)) { |
2400 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2401 | 0 | return 0; |
2402 | 0 | } |
2403 | | |
2404 | 0 | if ((len = PACKET_remaining(&supported_cert_types)) == 0) { |
2405 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2406 | 0 | return 0; |
2407 | 0 | } |
2408 | 0 | if (!PACKET_get_bytes(&supported_cert_types, &data, len)) { |
2409 | 0 | SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION); |
2410 | 0 | return 0; |
2411 | 0 | } |
2412 | | /* server_cert_type: server (this) has priority */ |
2413 | 0 | sc->ext.server_cert_type_ctos = reconcile_cert_type(sc->server_cert_type, sc->server_cert_type_len, |
2414 | 0 | data, len, |
2415 | 0 | &sc->ext.server_cert_type); |
2416 | 0 | if (sc->ext.server_cert_type_ctos == OSSL_CERT_TYPE_CTOS_GOOD) |
2417 | 0 | return 1; |
2418 | | |
2419 | | /* Did not receive an acceptable cert type */ |
2420 | 0 | SSLfatal(sc, SSL_AD_UNSUPPORTED_CERTIFICATE, SSL_R_BAD_EXTENSION); |
2421 | 0 | return 0; |
2422 | 0 | } |