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