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