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