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