VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.4/ssl/statem/extensions.c@ 103621

最後變更 在這個檔案從103621是 102863,由 vboxsync 提交於 13 月 前

openssl-3.1.4: Applied and adjusted our OpenSSL changes to 3.1.3. bugref:10577

檔案大小: 60.0 KB
 
1/*
2 * Copyright 2016-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10#if defined(__TANDEM) && defined(_SPT_MODEL_)
11# include <spthread.h>
12# include <spt_extensions.h> /* timeval */
13#endif
14
15#include <string.h>
16#include "internal/nelem.h"
17#include "internal/cryptlib.h"
18#include "../ssl_local.h"
19#include "statem_local.h"
20
21static int final_renegotiate(SSL *s, unsigned int context, int sent);
22static int init_server_name(SSL *s, unsigned int context);
23static int final_server_name(SSL *s, unsigned int context, int sent);
24static int final_ec_pt_formats(SSL *s, unsigned int context, int sent);
25static int init_session_ticket(SSL *s, unsigned int context);
26#ifndef OPENSSL_NO_OCSP
27static int init_status_request(SSL *s, unsigned int context);
28#endif
29#ifndef OPENSSL_NO_NEXTPROTONEG
30static int init_npn(SSL *s, unsigned int context);
31#endif
32static int init_alpn(SSL *s, unsigned int context);
33static int final_alpn(SSL *s, unsigned int context, int sent);
34static int init_sig_algs_cert(SSL *s, unsigned int context);
35static int init_sig_algs(SSL *s, unsigned int context);
36static int init_certificate_authorities(SSL *s, unsigned int context);
37static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
38 unsigned int context,
39 X509 *x,
40 size_t chainidx);
41static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
42 unsigned int context, X509 *x,
43 size_t chainidx);
44#ifndef OPENSSL_NO_SRP
45static int init_srp(SSL *s, unsigned int context);
46#endif
47static int init_ec_point_formats(SSL *s, unsigned int context);
48static int init_etm(SSL *s, unsigned int context);
49static int init_ems(SSL *s, unsigned int context);
50static int final_ems(SSL *s, unsigned int context, int sent);
51static int init_psk_kex_modes(SSL *s, unsigned int context);
52static int final_key_share(SSL *s, unsigned int context, int sent);
53#ifndef OPENSSL_NO_SRTP
54static int init_srtp(SSL *s, unsigned int context);
55#endif
56static int final_sig_algs(SSL *s, unsigned int context, int sent);
57static int final_early_data(SSL *s, unsigned int context, int sent);
58static int final_maxfragmentlen(SSL *s, unsigned int context, int sent);
59static int init_post_handshake_auth(SSL *s, unsigned int context);
60static int final_psk(SSL *s, unsigned int context, int sent);
61
62/* Structure to define a built-in extension */
63typedef struct extensions_definition_st {
64 /* The defined type for the extension */
65 unsigned int type;
66 /*
67 * The context that this extension applies to, e.g. what messages and
68 * protocol versions
69 */
70 unsigned int context;
71 /*
72 * Initialise extension before parsing. Always called for relevant contexts
73 * even if extension not present
74 */
75 int (*init)(SSL *s, unsigned int context);
76 /* Parse extension sent from client to server */
77 int (*parse_ctos)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
78 size_t chainidx);
79 /* Parse extension send from server to client */
80 int (*parse_stoc)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
81 size_t chainidx);
82 /* Construct extension sent from server to client */
83 EXT_RETURN (*construct_stoc)(SSL *s, WPACKET *pkt, unsigned int context,
84 X509 *x, size_t chainidx);
85 /* Construct extension sent from client to server */
86 EXT_RETURN (*construct_ctos)(SSL *s, WPACKET *pkt, unsigned int context,
87 X509 *x, size_t chainidx);
88 /*
89 * Finalise extension after parsing. Always called where an extensions was
90 * initialised even if the extension was not present. |sent| is set to 1 if
91 * the extension was seen, or 0 otherwise.
92 */
93 int (*final)(SSL *s, unsigned int context, int sent);
94} EXTENSION_DEFINITION;
95
96/*
97 * Definitions of all built-in extensions. NOTE: Changes in the number or order
98 * of these extensions should be mirrored with equivalent changes to the
99 * indexes ( TLSEXT_IDX_* ) defined in ssl_local.h.
100 * Extensions should be added to test/ext_internal_test.c as well, as that
101 * tests the ordering of the extensions.
102 *
103 * Each extension has an initialiser, a client and
104 * server side parser and a finaliser. The initialiser is called (if the
105 * extension is relevant to the given context) even if we did not see the
106 * extension in the message that we received. The parser functions are only
107 * called if we see the extension in the message. The finalisers are always
108 * called if the initialiser was called.
109 * There are also server and client side constructor functions which are always
110 * called during message construction if the extension is relevant for the
111 * given context.
112 * The initialisation, parsing, finalisation and construction functions are
113 * always called in the order defined in this list. Some extensions may depend
114 * on others having been processed first, so the order of this list is
115 * significant.
116 * The extension context is defined by a series of flags which specify which
117 * messages the extension is relevant to. These flags also specify whether the
118 * extension is relevant to a particular protocol or protocol version.
119 *
120 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
121 * the end, keep these extensions before signature_algorithm.
122 */
123#define INVALID_EXTENSION { TLSEXT_TYPE_invalid, 0, NULL, NULL, NULL, NULL, NULL, NULL }
124static const EXTENSION_DEFINITION ext_defs[] = {
125 {
126 TLSEXT_TYPE_renegotiate,
127 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
128 | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
129 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
130 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
131 final_renegotiate
132 },
133 {
134 TLSEXT_TYPE_server_name,
135 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
136 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
137 init_server_name,
138 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
139 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
140 final_server_name
141 },
142 {
143 TLSEXT_TYPE_max_fragment_length,
144 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
145 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
146 NULL, tls_parse_ctos_maxfragmentlen, tls_parse_stoc_maxfragmentlen,
147 tls_construct_stoc_maxfragmentlen, tls_construct_ctos_maxfragmentlen,
148 final_maxfragmentlen
149 },
150#ifndef OPENSSL_NO_SRP
151 {
152 TLSEXT_TYPE_srp,
153 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
154 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
155 },
156#else
157 INVALID_EXTENSION,
158#endif
159 {
160 TLSEXT_TYPE_ec_point_formats,
161 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
162 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
163 init_ec_point_formats, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
164 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
165 final_ec_pt_formats
166 },
167 {
168 /*
169 * "supported_groups" is spread across several specifications.
170 * It was originally specified as "elliptic_curves" in RFC 4492,
171 * and broadened to include named FFDH groups by RFC 7919.
172 * Both RFCs 4492 and 7919 do not include a provision for the server
173 * to indicate to the client the complete list of groups supported
174 * by the server, with the server instead just indicating the
175 * selected group for this connection in the ServerKeyExchange
176 * message. TLS 1.3 adds a scheme for the server to indicate
177 * to the client its list of supported groups in the
178 * EncryptedExtensions message, but none of the relevant
179 * specifications permit sending supported_groups in the ServerHello.
180 * Nonetheless (possibly due to the close proximity to the
181 * "ec_point_formats" extension, which is allowed in the ServerHello),
182 * there are several servers that send this extension in the
183 * ServerHello anyway. Up to and including the 1.1.0 release,
184 * we did not check for the presence of nonpermitted extensions,
185 * so to avoid a regression, we must permit this extension in the
186 * TLS 1.2 ServerHello as well.
187 *
188 * Note that there is no tls_parse_stoc_supported_groups function,
189 * so we do not perform any additional parsing, validation, or
190 * processing on the server's group list -- this is just a minimal
191 * change to preserve compatibility with these misbehaving servers.
192 */
193 TLSEXT_TYPE_supported_groups,
194 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
195 | SSL_EXT_TLS1_2_SERVER_HELLO,
196 NULL, tls_parse_ctos_supported_groups, NULL,
197 tls_construct_stoc_supported_groups,
198 tls_construct_ctos_supported_groups, NULL
199 },
200 {
201 TLSEXT_TYPE_session_ticket,
202 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
203 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
204 init_session_ticket, tls_parse_ctos_session_ticket,
205 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
206 tls_construct_ctos_session_ticket, NULL
207 },
208#ifndef OPENSSL_NO_OCSP
209 {
210 TLSEXT_TYPE_status_request,
211 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
212 | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
213 init_status_request, tls_parse_ctos_status_request,
214 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
215 tls_construct_ctos_status_request, NULL
216 },
217#else
218 INVALID_EXTENSION,
219#endif
220#ifndef OPENSSL_NO_NEXTPROTONEG
221 {
222 TLSEXT_TYPE_next_proto_neg,
223 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
224 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
225 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
226 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
227 },
228#else
229 INVALID_EXTENSION,
230#endif
231 {
232 /*
233 * Must appear in this list after server_name so that finalisation
234 * happens after server_name callbacks
235 */
236 TLSEXT_TYPE_application_layer_protocol_negotiation,
237 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
238 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
239 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
240 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
241 },
242#ifndef OPENSSL_NO_SRTP
243 {
244 TLSEXT_TYPE_use_srtp,
245 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
246 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
247 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
248 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
249 },
250#else
251 INVALID_EXTENSION,
252#endif
253 {
254 TLSEXT_TYPE_encrypt_then_mac,
255 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
256 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
257 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
258 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
259 },
260#ifndef OPENSSL_NO_CT
261 {
262 TLSEXT_TYPE_signed_certificate_timestamp,
263 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
264 | SSL_EXT_TLS1_3_CERTIFICATE | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
265 NULL,
266 /*
267 * No server side support for this, but can be provided by a custom
268 * extension. This is an exception to the rule that custom extensions
269 * cannot override built in ones.
270 */
271 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
272 },
273#else
274 INVALID_EXTENSION,
275#endif
276 {
277 TLSEXT_TYPE_extended_master_secret,
278 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
279 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
280 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
281 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
282 },
283 {
284 TLSEXT_TYPE_signature_algorithms_cert,
285 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
286 init_sig_algs_cert, tls_parse_ctos_sig_algs_cert,
287 tls_parse_ctos_sig_algs_cert,
288 /* We do not generate signature_algorithms_cert at present. */
289 NULL, NULL, NULL
290 },
291 {
292 TLSEXT_TYPE_post_handshake_auth,
293 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ONLY,
294 init_post_handshake_auth,
295 tls_parse_ctos_post_handshake_auth, NULL,
296 NULL, tls_construct_ctos_post_handshake_auth,
297 NULL,
298 },
299 {
300 TLSEXT_TYPE_signature_algorithms,
301 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
302 init_sig_algs, tls_parse_ctos_sig_algs,
303 tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
304 tls_construct_ctos_sig_algs, final_sig_algs
305 },
306 {
307 TLSEXT_TYPE_supported_versions,
308 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
309 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY,
310 NULL,
311 /* Processed inline as part of version selection */
312 NULL, tls_parse_stoc_supported_versions,
313 tls_construct_stoc_supported_versions,
314 tls_construct_ctos_supported_versions, NULL
315 },
316 {
317 TLSEXT_TYPE_psk_kex_modes,
318 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
319 | SSL_EXT_TLS1_3_ONLY,
320 init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
321 tls_construct_ctos_psk_kex_modes, NULL
322 },
323 {
324 /*
325 * Must be in this list after supported_groups. We need that to have
326 * been parsed before we do this one.
327 */
328 TLSEXT_TYPE_key_share,
329 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
330 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
331 | SSL_EXT_TLS1_3_ONLY,
332 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
333 tls_construct_stoc_key_share, tls_construct_ctos_key_share,
334 final_key_share
335 },
336 {
337 /* Must be after key_share */
338 TLSEXT_TYPE_cookie,
339 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
340 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
341 NULL, tls_parse_ctos_cookie, tls_parse_stoc_cookie,
342 tls_construct_stoc_cookie, tls_construct_ctos_cookie, NULL
343 },
344 {
345 /*
346 * Special unsolicited ServerHello extension only used when
347 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set. We allow it in a ClientHello but
348 * ignore it.
349 */
350 TLSEXT_TYPE_cryptopro_bug,
351 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
352 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
353 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
354 },
355 {
356 TLSEXT_TYPE_early_data,
357 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
358 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET | SSL_EXT_TLS1_3_ONLY,
359 NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
360 tls_construct_stoc_early_data, tls_construct_ctos_early_data,
361 final_early_data
362 },
363 {
364 TLSEXT_TYPE_certificate_authorities,
365 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
366 | SSL_EXT_TLS1_3_ONLY,
367 init_certificate_authorities,
368 tls_parse_certificate_authorities, tls_parse_certificate_authorities,
369 tls_construct_certificate_authorities,
370 tls_construct_certificate_authorities, NULL,
371 },
372 {
373 /* Must be immediately before pre_shared_key */
374 TLSEXT_TYPE_padding,
375 SSL_EXT_CLIENT_HELLO,
376 NULL,
377 /* We send this, but don't read it */
378 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
379 },
380 {
381 /* Required by the TLSv1.3 spec to always be the last extension */
382 TLSEXT_TYPE_psk,
383 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
384 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
385 NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
386 tls_construct_ctos_psk, final_psk
387 }
388};
389
390/* Returns a TLSEXT_TYPE for the given index */
391unsigned int ossl_get_extension_type(size_t idx)
392{
393 size_t num_exts = OSSL_NELEM(ext_defs);
394
395 if (idx >= num_exts)
396 return TLSEXT_TYPE_out_of_range;
397
398 return ext_defs[idx].type;
399}
400
401/* Check whether an extension's context matches the current context */
402static int validate_context(SSL *s, unsigned int extctx, unsigned int thisctx)
403{
404 /* Check we're allowed to use this extension in this context */
405 if ((thisctx & extctx) == 0)
406 return 0;
407
408 if (SSL_IS_DTLS(s)) {
409 if ((extctx & SSL_EXT_TLS_ONLY) != 0)
410 return 0;
411 } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
412 return 0;
413 }
414
415 return 1;
416}
417
418int tls_validate_all_contexts(SSL *s, unsigned int thisctx, RAW_EXTENSION *exts)
419{
420 size_t i, num_exts, builtin_num = OSSL_NELEM(ext_defs), offset;
421 RAW_EXTENSION *thisext;
422 unsigned int context;
423 ENDPOINT role = ENDPOINT_BOTH;
424
425 if ((thisctx & SSL_EXT_CLIENT_HELLO) != 0)
426 role = ENDPOINT_SERVER;
427 else if ((thisctx & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
428 role = ENDPOINT_CLIENT;
429
430 /* Calculate the number of extensions in the extensions list */
431 num_exts = builtin_num + s->cert->custext.meths_count;
432
433 for (thisext = exts, i = 0; i < num_exts; i++, thisext++) {
434 if (!thisext->present)
435 continue;
436
437 if (i < builtin_num) {
438 context = ext_defs[i].context;
439 } else {
440 custom_ext_method *meth = NULL;
441
442 meth = custom_ext_find(&s->cert->custext, role, thisext->type,
443 &offset);
444 if (!ossl_assert(meth != NULL))
445 return 0;
446 context = meth->context;
447 }
448
449 if (!validate_context(s, context, thisctx))
450 return 0;
451 }
452
453 return 1;
454}
455
456/*
457 * Verify whether we are allowed to use the extension |type| in the current
458 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
459 * indicate the extension is not allowed. If returning 1 then |*found| is set to
460 * the definition for the extension we found.
461 */
462static int verify_extension(SSL *s, unsigned int context, unsigned int type,
463 custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
464 RAW_EXTENSION **found)
465{
466 size_t i;
467 size_t builtin_num = OSSL_NELEM(ext_defs);
468 const EXTENSION_DEFINITION *thisext;
469
470 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
471 if (type == thisext->type) {
472 if (!validate_context(s, thisext->context, context))
473 return 0;
474
475 *found = &rawexlist[i];
476 return 1;
477 }
478 }
479
480 /* Check the custom extensions */
481 if (meths != NULL) {
482 size_t offset = 0;
483 ENDPOINT role = ENDPOINT_BOTH;
484 custom_ext_method *meth = NULL;
485
486 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
487 role = ENDPOINT_SERVER;
488 else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
489 role = ENDPOINT_CLIENT;
490
491 meth = custom_ext_find(meths, role, type, &offset);
492 if (meth != NULL) {
493 if (!validate_context(s, meth->context, context))
494 return 0;
495 *found = &rawexlist[offset + builtin_num];
496 return 1;
497 }
498 }
499
500 /* Unknown extension. We allow it */
501 *found = NULL;
502 return 1;
503}
504
505/*
506 * Check whether the context defined for an extension |extctx| means whether
507 * the extension is relevant for the current context |thisctx| or not. Returns
508 * 1 if the extension is relevant for this context, and 0 otherwise
509 */
510int extension_is_relevant(SSL *s, unsigned int extctx, unsigned int thisctx)
511{
512 int is_tls13;
513
514 /*
515 * For HRR we haven't selected the version yet but we know it will be
516 * TLSv1.3
517 */
518 if ((thisctx & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
519 is_tls13 = 1;
520 else
521 is_tls13 = SSL_IS_TLS13(s);
522
523 if ((SSL_IS_DTLS(s)
524 && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
525 || (s->version == SSL3_VERSION
526 && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
527 /*
528 * Note that SSL_IS_TLS13() means "TLS 1.3 has been negotiated",
529 * which is never true when generating the ClientHello.
530 * However, version negotiation *has* occurred by the time the
531 * ClientHello extensions are being parsed.
532 * Be careful to allow TLS 1.3-only extensions when generating
533 * the ClientHello.
534 */
535 || (is_tls13 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
536 || (!is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
537 && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
538 || (s->server && !is_tls13 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
539 || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
540 return 0;
541 return 1;
542}
543
544/*
545 * Gather a list of all the extensions from the data in |packet]. |context|
546 * tells us which message this extension is for. The raw extension data is
547 * stored in |*res| on success. We don't actually process the content of the
548 * extensions yet, except to check their types. This function also runs the
549 * initialiser functions for all known extensions if |init| is nonzero (whether
550 * we have collected them or not). If successful the caller is responsible for
551 * freeing the contents of |*res|.
552 *
553 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
554 * more than one extension of the same type in a ClientHello or ServerHello.
555 * This function returns 1 if all extensions are unique and we have parsed their
556 * types, and 0 if the extensions contain duplicates, could not be successfully
557 * found, or an internal error occurred. We only check duplicates for
558 * extensions that we know about. We ignore others.
559 */
560int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
561 RAW_EXTENSION **res, size_t *len, int init)
562{
563 PACKET extensions = *packet;
564 size_t i = 0;
565 size_t num_exts;
566 custom_ext_methods *exts = &s->cert->custext;
567 RAW_EXTENSION *raw_extensions = NULL;
568 const EXTENSION_DEFINITION *thisexd;
569
570 *res = NULL;
571
572 /*
573 * Initialise server side custom extensions. Client side is done during
574 * construction of extensions for the ClientHello.
575 */
576 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
577 custom_ext_init(&s->cert->custext);
578
579 num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
580 raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
581 if (raw_extensions == NULL) {
582 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
583 return 0;
584 }
585
586 i = 0;
587 while (PACKET_remaining(&extensions) > 0) {
588 unsigned int type, idx;
589 PACKET extension;
590 RAW_EXTENSION *thisex;
591
592 if (!PACKET_get_net_2(&extensions, &type) ||
593 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
594 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
595 goto err;
596 }
597 /*
598 * Verify this extension is allowed. We only check duplicates for
599 * extensions that we recognise. We also have a special case for the
600 * PSK extension, which must be the last one in the ClientHello.
601 */
602 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
603 || (thisex != NULL && thisex->present == 1)
604 || (type == TLSEXT_TYPE_psk
605 && (context & SSL_EXT_CLIENT_HELLO) != 0
606 && PACKET_remaining(&extensions) != 0)) {
607 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION);
608 goto err;
609 }
610 idx = thisex - raw_extensions;
611 /*-
612 * Check that we requested this extension (if appropriate). Requests can
613 * be sent in the ClientHello and CertificateRequest. Unsolicited
614 * extensions can be sent in the NewSessionTicket. We only do this for
615 * the built-in extensions. Custom extensions have a different but
616 * similar check elsewhere.
617 * Special cases:
618 * - The HRR cookie extension is unsolicited
619 * - The renegotiate extension is unsolicited (the client signals
620 * support via an SCSV)
621 * - The signed_certificate_timestamp extension can be provided by a
622 * custom extension or by the built-in version. We let the extension
623 * itself handle unsolicited response checks.
624 */
625 if (idx < OSSL_NELEM(ext_defs)
626 && (context & (SSL_EXT_CLIENT_HELLO
627 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
628 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
629 && type != TLSEXT_TYPE_cookie
630 && type != TLSEXT_TYPE_renegotiate
631 && type != TLSEXT_TYPE_signed_certificate_timestamp
632 && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0
633#ifndef OPENSSL_NO_GOST
634 && !((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0
635 && type == TLSEXT_TYPE_cryptopro_bug)
636#endif
637 ) {
638 SSLfatal(s, SSL_AD_UNSUPPORTED_EXTENSION,
639 SSL_R_UNSOLICITED_EXTENSION);
640 goto err;
641 }
642 if (thisex != NULL) {
643 thisex->data = extension;
644 thisex->present = 1;
645 thisex->type = type;
646 thisex->received_order = i++;
647 if (s->ext.debug_cb)
648 s->ext.debug_cb(s, !s->server, thisex->type,
649 PACKET_data(&thisex->data),
650 PACKET_remaining(&thisex->data),
651 s->ext.debug_arg);
652 }
653 }
654
655 if (init) {
656 /*
657 * Initialise all known extensions relevant to this context,
658 * whether we have found them or not
659 */
660 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
661 i++, thisexd++) {
662 if (thisexd->init != NULL && (thisexd->context & context) != 0
663 && extension_is_relevant(s, thisexd->context, context)
664 && !thisexd->init(s, context)) {
665 /* SSLfatal() already called */
666 goto err;
667 }
668 }
669 }
670
671 *res = raw_extensions;
672 if (len != NULL)
673 *len = num_exts;
674 return 1;
675
676 err:
677 OPENSSL_free(raw_extensions);
678 return 0;
679}
680
681/*
682 * Runs the parser for a given extension with index |idx|. |exts| contains the
683 * list of all parsed extensions previously collected by
684 * tls_collect_extensions(). The parser is only run if it is applicable for the
685 * given |context| and the parser has not already been run. If this is for a
686 * Certificate message, then we also provide the parser with the relevant
687 * Certificate |x| and its position in the |chainidx| with 0 being the first
688 * Certificate. Returns 1 on success or 0 on failure. If an extension is not
689 * present this counted as success.
690 */
691int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
692 RAW_EXTENSION *exts, X509 *x, size_t chainidx)
693{
694 RAW_EXTENSION *currext = &exts[idx];
695 int (*parser)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
696 size_t chainidx) = NULL;
697
698 /* Skip if the extension is not present */
699 if (!currext->present)
700 return 1;
701
702 /* Skip if we've already parsed this extension */
703 if (currext->parsed)
704 return 1;
705
706 currext->parsed = 1;
707
708 if (idx < OSSL_NELEM(ext_defs)) {
709 /* We are handling a built-in extension */
710 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
711
712 /* Check if extension is defined for our protocol. If not, skip */
713 if (!extension_is_relevant(s, extdef->context, context))
714 return 1;
715
716 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
717
718 if (parser != NULL)
719 return parser(s, &currext->data, context, x, chainidx);
720
721 /*
722 * If the parser is NULL we fall through to the custom extension
723 * processing
724 */
725 }
726
727 /* Parse custom extensions */
728 return custom_ext_parse(s, context, currext->type,
729 PACKET_data(&currext->data),
730 PACKET_remaining(&currext->data),
731 x, chainidx);
732}
733
734/*
735 * Parse all remaining extensions that have not yet been parsed. Also calls the
736 * finalisation for all extensions at the end if |fin| is nonzero, whether we
737 * collected them or not. Returns 1 for success or 0 for failure. If we are
738 * working on a Certificate message then we also pass the Certificate |x| and
739 * its position in the |chainidx|, with 0 being the first certificate.
740 */
741int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, X509 *x,
742 size_t chainidx, int fin)
743{
744 size_t i, numexts = OSSL_NELEM(ext_defs);
745 const EXTENSION_DEFINITION *thisexd;
746
747 /* Calculate the number of extensions in the extensions list */
748 numexts += s->cert->custext.meths_count;
749
750 /* Parse each extension in turn */
751 for (i = 0; i < numexts; i++) {
752 if (!tls_parse_extension(s, i, context, exts, x, chainidx)) {
753 /* SSLfatal() already called */
754 return 0;
755 }
756 }
757
758 if (fin) {
759 /*
760 * Finalise all known extensions relevant to this context,
761 * whether we have found them or not
762 */
763 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
764 i++, thisexd++) {
765 if (thisexd->final != NULL && (thisexd->context & context) != 0
766 && !thisexd->final(s, context, exts[i].present)) {
767 /* SSLfatal() already called */
768 return 0;
769 }
770 }
771 }
772
773 return 1;
774}
775
776int should_add_extension(SSL *s, unsigned int extctx, unsigned int thisctx,
777 int max_version)
778{
779 /* Skip if not relevant for our context */
780 if ((extctx & thisctx) == 0)
781 return 0;
782
783 /* Check if this extension is defined for our protocol. If not, skip */
784 if (!extension_is_relevant(s, extctx, thisctx)
785 || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
786 && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
787 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
788 return 0;
789
790 return 1;
791}
792
793/*
794 * Construct all the extensions relevant to the current |context| and write
795 * them to |pkt|. If this is an extension for a Certificate in a Certificate
796 * message, then |x| will be set to the Certificate we are handling, and
797 * |chainidx| will indicate the position in the chainidx we are processing (with
798 * 0 being the first in the chain). Returns 1 on success or 0 on failure. On a
799 * failure construction stops at the first extension to fail to construct.
800 */
801int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
802 X509 *x, size_t chainidx)
803{
804 size_t i;
805 int min_version, max_version = 0, reason;
806 const EXTENSION_DEFINITION *thisexd;
807
808 if (!WPACKET_start_sub_packet_u16(pkt)
809 /*
810 * If extensions are of zero length then we don't even add the
811 * extensions length bytes to a ClientHello/ServerHello
812 * (for non-TLSv1.3).
813 */
814 || ((context &
815 (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
816 && !WPACKET_set_flags(pkt,
817 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
818 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
819 return 0;
820 }
821
822 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
823 reason = ssl_get_min_max_version(s, &min_version, &max_version, NULL);
824 if (reason != 0) {
825 SSLfatal(s, SSL_AD_INTERNAL_ERROR, reason);
826 return 0;
827 }
828 }
829
830 /* Add custom extensions first */
831 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
832 /* On the server side with initialise during ClientHello parsing */
833 custom_ext_init(&s->cert->custext);
834 }
835 if (!custom_ext_add(s, context, pkt, x, chainidx, max_version)) {
836 /* SSLfatal() already called */
837 return 0;
838 }
839
840 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
841 EXT_RETURN (*construct)(SSL *s, WPACKET *pkt, unsigned int context,
842 X509 *x, size_t chainidx);
843 EXT_RETURN ret;
844
845 /* Skip if not relevant for our context */
846 if (!should_add_extension(s, thisexd->context, context, max_version))
847 continue;
848
849 construct = s->server ? thisexd->construct_stoc
850 : thisexd->construct_ctos;
851
852 if (construct == NULL)
853 continue;
854
855 ret = construct(s, pkt, context, x, chainidx);
856 if (ret == EXT_RETURN_FAIL) {
857 /* SSLfatal() already called */
858 return 0;
859 }
860 if (ret == EXT_RETURN_SENT
861 && (context & (SSL_EXT_CLIENT_HELLO
862 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
863 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
864 s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
865 }
866
867 if (!WPACKET_close(pkt)) {
868 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
869 return 0;
870 }
871
872 return 1;
873}
874
875/*
876 * Built in extension finalisation and initialisation functions. All initialise
877 * or finalise the associated extension type for the given |context|. For
878 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
879 * otherwise. These functions return 1 on success or 0 on failure.
880 */
881
882static int final_renegotiate(SSL *s, unsigned int context, int sent)
883{
884 if (!s->server) {
885 /*
886 * Check if we can connect to a server that doesn't support safe
887 * renegotiation
888 */
889 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
890 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
891 && !sent) {
892 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
893 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
894 return 0;
895 }
896
897 return 1;
898 }
899
900 /* Need RI if renegotiating */
901 if (s->renegotiate
902 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
903 && !sent) {
904 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE,
905 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
906 return 0;
907 }
908
909
910 return 1;
911}
912
913static ossl_inline void ssl_tsan_decr(const SSL_CTX *ctx,
914 TSAN_QUALIFIER int *stat)
915{
916 if (ssl_tsan_lock(ctx)) {
917 tsan_decr(stat);
918 ssl_tsan_unlock(ctx);
919 }
920}
921
922static int init_server_name(SSL *s, unsigned int context)
923{
924 if (s->server) {
925 s->servername_done = 0;
926
927 OPENSSL_free(s->ext.hostname);
928 s->ext.hostname = NULL;
929 }
930
931 return 1;
932}
933
934static int final_server_name(SSL *s, unsigned int context, int sent)
935{
936 int ret = SSL_TLSEXT_ERR_NOACK;
937 int altmp = SSL_AD_UNRECOGNIZED_NAME;
938 int was_ticket = (SSL_get_options(s) & SSL_OP_NO_TICKET) == 0;
939
940 if (!ossl_assert(s->ctx != NULL) || !ossl_assert(s->session_ctx != NULL)) {
941 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
942 return 0;
943 }
944
945 if (s->ctx->ext.servername_cb != NULL)
946 ret = s->ctx->ext.servername_cb(s, &altmp,
947 s->ctx->ext.servername_arg);
948 else if (s->session_ctx->ext.servername_cb != NULL)
949 ret = s->session_ctx->ext.servername_cb(s, &altmp,
950 s->session_ctx->ext.servername_arg);
951
952 /*
953 * For servers, propagate the SNI hostname from the temporary
954 * storage in the SSL to the persistent SSL_SESSION, now that we
955 * know we accepted it.
956 * Clients make this copy when parsing the server's response to
957 * the extension, which is when they find out that the negotiation
958 * was successful.
959 */
960 if (s->server) {
961 if (sent && ret == SSL_TLSEXT_ERR_OK && !s->hit) {
962 /* Only store the hostname in the session if we accepted it. */
963 OPENSSL_free(s->session->ext.hostname);
964 s->session->ext.hostname = OPENSSL_strdup(s->ext.hostname);
965 if (s->session->ext.hostname == NULL && s->ext.hostname != NULL) {
966 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
967 }
968 }
969 }
970
971 /*
972 * If we switched contexts (whether here or in the client_hello callback),
973 * move the sess_accept increment from the session_ctx to the new
974 * context, to avoid the confusing situation of having sess_accept_good
975 * exceed sess_accept (zero) for the new context.
976 */
977 if (SSL_IS_FIRST_HANDSHAKE(s) && s->ctx != s->session_ctx
978 && s->hello_retry_request == SSL_HRR_NONE) {
979 ssl_tsan_counter(s->ctx, &s->ctx->stats.sess_accept);
980 ssl_tsan_decr(s->session_ctx, &s->session_ctx->stats.sess_accept);
981 }
982
983 /*
984 * If we're expecting to send a ticket, and tickets were previously enabled,
985 * and now tickets are disabled, then turn off expected ticket.
986 * Also, if this is not a resumption, create a new session ID
987 */
988 if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
989 && was_ticket && (SSL_get_options(s) & SSL_OP_NO_TICKET) != 0) {
990 s->ext.ticket_expected = 0;
991 if (!s->hit) {
992 SSL_SESSION* ss = SSL_get_session(s);
993
994 if (ss != NULL) {
995 OPENSSL_free(ss->ext.tick);
996 ss->ext.tick = NULL;
997 ss->ext.ticklen = 0;
998 ss->ext.tick_lifetime_hint = 0;
999 ss->ext.tick_age_add = 0;
1000 if (!ssl_generate_session_id(s, ss)) {
1001 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1002 return 0;
1003 }
1004 } else {
1005 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1006 return 0;
1007 }
1008 }
1009 }
1010
1011 switch (ret) {
1012 case SSL_TLSEXT_ERR_ALERT_FATAL:
1013 SSLfatal(s, altmp, SSL_R_CALLBACK_FAILED);
1014 return 0;
1015
1016 case SSL_TLSEXT_ERR_ALERT_WARNING:
1017 /* TLSv1.3 doesn't have warning alerts so we suppress this */
1018 if (!SSL_IS_TLS13(s))
1019 ssl3_send_alert(s, SSL3_AL_WARNING, altmp);
1020 s->servername_done = 0;
1021 return 1;
1022
1023 case SSL_TLSEXT_ERR_NOACK:
1024 s->servername_done = 0;
1025 return 1;
1026
1027 default:
1028 return 1;
1029 }
1030}
1031
1032static int final_ec_pt_formats(SSL *s, unsigned int context, int sent)
1033{
1034 unsigned long alg_k, alg_a;
1035
1036 if (s->server)
1037 return 1;
1038
1039 alg_k = s->s3.tmp.new_cipher->algorithm_mkey;
1040 alg_a = s->s3.tmp.new_cipher->algorithm_auth;
1041
1042 /*
1043 * If we are client and using an elliptic curve cryptography cipher
1044 * suite, then if server returns an EC point formats lists extension it
1045 * must contain uncompressed.
1046 */
1047 if (s->ext.ecpointformats != NULL
1048 && s->ext.ecpointformats_len > 0
1049 && s->ext.peer_ecpointformats != NULL
1050 && s->ext.peer_ecpointformats_len > 0
1051 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
1052 /* we are using an ECC cipher */
1053 size_t i;
1054 unsigned char *list = s->ext.peer_ecpointformats;
1055
1056 for (i = 0; i < s->ext.peer_ecpointformats_len; i++) {
1057 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
1058 break;
1059 }
1060 if (i == s->ext.peer_ecpointformats_len) {
1061 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1062 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
1063 return 0;
1064 }
1065 }
1066
1067 return 1;
1068}
1069
1070static int init_session_ticket(SSL *s, unsigned int context)
1071{
1072 if (!s->server)
1073 s->ext.ticket_expected = 0;
1074
1075 return 1;
1076}
1077
1078#ifndef OPENSSL_NO_OCSP
1079static int init_status_request(SSL *s, unsigned int context)
1080{
1081 if (s->server) {
1082 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
1083 } else {
1084 /*
1085 * Ensure we get sensible values passed to tlsext_status_cb in the event
1086 * that we don't receive a status message
1087 */
1088 OPENSSL_free(s->ext.ocsp.resp);
1089 s->ext.ocsp.resp = NULL;
1090 s->ext.ocsp.resp_len = 0;
1091 }
1092
1093 return 1;
1094}
1095#endif
1096
1097#ifndef OPENSSL_NO_NEXTPROTONEG
1098static int init_npn(SSL *s, unsigned int context)
1099{
1100 s->s3.npn_seen = 0;
1101
1102 return 1;
1103}
1104#endif
1105
1106static int init_alpn(SSL *s, unsigned int context)
1107{
1108 OPENSSL_free(s->s3.alpn_selected);
1109 s->s3.alpn_selected = NULL;
1110 s->s3.alpn_selected_len = 0;
1111 if (s->server) {
1112 OPENSSL_free(s->s3.alpn_proposed);
1113 s->s3.alpn_proposed = NULL;
1114 s->s3.alpn_proposed_len = 0;
1115 }
1116 return 1;
1117}
1118
1119static int final_alpn(SSL *s, unsigned int context, int sent)
1120{
1121 if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
1122 s->ext.early_data_ok = 0;
1123
1124 if (!s->server || !SSL_IS_TLS13(s))
1125 return 1;
1126
1127 /*
1128 * Call alpn_select callback if needed. Has to be done after SNI and
1129 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
1130 * we also have to do this before we decide whether to accept early_data.
1131 * In TLSv1.3 we've already negotiated our cipher so we do this call now.
1132 * For < TLSv1.3 we defer it until after cipher negotiation.
1133 *
1134 * On failure SSLfatal() already called.
1135 */
1136 return tls_handle_alpn(s);
1137}
1138
1139static int init_sig_algs(SSL *s, unsigned int context)
1140{
1141 /* Clear any signature algorithms extension received */
1142 OPENSSL_free(s->s3.tmp.peer_sigalgs);
1143 s->s3.tmp.peer_sigalgs = NULL;
1144 s->s3.tmp.peer_sigalgslen = 0;
1145
1146 return 1;
1147}
1148
1149static int init_sig_algs_cert(SSL *s, ossl_unused unsigned int context)
1150{
1151 /* Clear any signature algorithms extension received */
1152 OPENSSL_free(s->s3.tmp.peer_cert_sigalgs);
1153 s->s3.tmp.peer_cert_sigalgs = NULL;
1154 s->s3.tmp.peer_cert_sigalgslen = 0;
1155
1156 return 1;
1157}
1158
1159#ifndef OPENSSL_NO_SRP
1160static int init_srp(SSL *s, unsigned int context)
1161{
1162 OPENSSL_free(s->srp_ctx.login);
1163 s->srp_ctx.login = NULL;
1164
1165 return 1;
1166}
1167#endif
1168
1169static int init_ec_point_formats(SSL *s, unsigned int context)
1170{
1171 OPENSSL_free(s->ext.peer_ecpointformats);
1172 s->ext.peer_ecpointformats = NULL;
1173 s->ext.peer_ecpointformats_len = 0;
1174
1175 return 1;
1176}
1177
1178static int init_etm(SSL *s, unsigned int context)
1179{
1180 s->ext.use_etm = 0;
1181
1182 return 1;
1183}
1184
1185static int init_ems(SSL *s, unsigned int context)
1186{
1187 if (s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) {
1188 s->s3.flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1189 s->s3.flags |= TLS1_FLAGS_REQUIRED_EXTMS;
1190 }
1191
1192 return 1;
1193}
1194
1195static int final_ems(SSL *s, unsigned int context, int sent)
1196{
1197 /*
1198 * Check extended master secret extension is not dropped on
1199 * renegotiation.
1200 */
1201 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS)
1202 && (s->s3.flags & TLS1_FLAGS_REQUIRED_EXTMS)) {
1203 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1204 return 0;
1205 }
1206 if (!s->server && s->hit) {
1207 /*
1208 * Check extended master secret extension is consistent with
1209 * original session.
1210 */
1211 if (!(s->s3.flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
1212 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1213 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_EXTMS);
1214 return 0;
1215 }
1216 }
1217
1218 return 1;
1219}
1220
1221static int init_certificate_authorities(SSL *s, unsigned int context)
1222{
1223 sk_X509_NAME_pop_free(s->s3.tmp.peer_ca_names, X509_NAME_free);
1224 s->s3.tmp.peer_ca_names = NULL;
1225 return 1;
1226}
1227
1228static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
1229 unsigned int context,
1230 X509 *x,
1231 size_t chainidx)
1232{
1233 const STACK_OF(X509_NAME) *ca_sk = get_ca_names(s);
1234
1235 if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
1236 return EXT_RETURN_NOT_SENT;
1237
1238 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
1239 || !WPACKET_start_sub_packet_u16(pkt)) {
1240 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1241 return EXT_RETURN_FAIL;
1242 }
1243
1244 if (!construct_ca_names(s, ca_sk, pkt)) {
1245 /* SSLfatal() already called */
1246 return EXT_RETURN_FAIL;
1247 }
1248
1249 if (!WPACKET_close(pkt)) {
1250 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1251 return EXT_RETURN_FAIL;
1252 }
1253
1254 return EXT_RETURN_SENT;
1255}
1256
1257static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
1258 unsigned int context, X509 *x,
1259 size_t chainidx)
1260{
1261 if (!parse_ca_names(s, pkt))
1262 return 0;
1263 if (PACKET_remaining(pkt) != 0) {
1264 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_EXTENSION);
1265 return 0;
1266 }
1267 return 1;
1268}
1269
1270#ifndef OPENSSL_NO_SRTP
1271static int init_srtp(SSL *s, unsigned int context)
1272{
1273 if (s->server)
1274 s->srtp_profile = NULL;
1275
1276 return 1;
1277}
1278#endif
1279
1280static int final_sig_algs(SSL *s, unsigned int context, int sent)
1281{
1282 if (!sent && SSL_IS_TLS13(s) && !s->hit) {
1283 SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1284 SSL_R_MISSING_SIGALGS_EXTENSION);
1285 return 0;
1286 }
1287
1288 return 1;
1289}
1290
1291static int final_key_share(SSL *s, unsigned int context, int sent)
1292{
1293#if !defined(OPENSSL_NO_TLS1_3)
1294 if (!SSL_IS_TLS13(s))
1295 return 1;
1296
1297 /* Nothing to do for key_share in an HRR */
1298 if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1299 return 1;
1300
1301 /*
1302 * If
1303 * we are a client
1304 * AND
1305 * we have no key_share
1306 * AND
1307 * (we are not resuming
1308 * OR the kex_mode doesn't allow non key_share resumes)
1309 * THEN
1310 * fail;
1311 */
1312 if (!s->server
1313 && !sent
1314 && (!s->hit
1315 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
1316 /* Nothing left we can do - just fail */
1317 SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_NO_SUITABLE_KEY_SHARE);
1318 return 0;
1319 }
1320 /*
1321 * IF
1322 * we are a server
1323 * THEN
1324 * IF
1325 * we have a suitable key_share
1326 * THEN
1327 * IF
1328 * we are stateless AND we have no cookie
1329 * THEN
1330 * send a HelloRetryRequest
1331 * ELSE
1332 * IF
1333 * we didn't already send a HelloRetryRequest
1334 * AND
1335 * the client sent a key_share extension
1336 * AND
1337 * (we are not resuming
1338 * OR the kex_mode allows key_share resumes)
1339 * AND
1340 * a shared group exists
1341 * THEN
1342 * send a HelloRetryRequest
1343 * ELSE IF
1344 * we are not resuming
1345 * OR
1346 * the kex_mode doesn't allow non key_share resumes
1347 * THEN
1348 * fail
1349 * ELSE IF
1350 * we are stateless AND we have no cookie
1351 * THEN
1352 * send a HelloRetryRequest
1353 */
1354 if (s->server) {
1355 if (s->s3.peer_tmp != NULL) {
1356 /* We have a suitable key_share */
1357 if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1358 && !s->ext.cookieok) {
1359 if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1360 /*
1361 * If we are stateless then we wouldn't know about any
1362 * previously sent HRR - so how can this be anything other
1363 * than 0?
1364 */
1365 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1366 return 0;
1367 }
1368 s->hello_retry_request = SSL_HRR_PENDING;
1369 return 1;
1370 }
1371 } else {
1372 /* No suitable key_share */
1373 if (s->hello_retry_request == SSL_HRR_NONE && sent
1374 && (!s->hit
1375 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
1376 != 0)) {
1377 const uint16_t *pgroups, *clntgroups;
1378 size_t num_groups, clnt_num_groups, i;
1379 unsigned int group_id = 0;
1380
1381 /* Check if a shared group exists */
1382
1383 /* Get the clients list of supported groups. */
1384 tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
1385 tls1_get_supported_groups(s, &pgroups, &num_groups);
1386
1387 /*
1388 * Find the first group we allow that is also in client's list
1389 */
1390 for (i = 0; i < num_groups; i++) {
1391 group_id = pgroups[i];
1392
1393 if (check_in_list(s, group_id, clntgroups, clnt_num_groups,
1394 1)
1395 && tls_group_allowed(s, group_id,
1396 SSL_SECOP_CURVE_SUPPORTED)
1397 && tls_valid_group(s, group_id, TLS1_3_VERSION,
1398 TLS1_3_VERSION, 0, NULL))
1399 break;
1400 }
1401
1402 if (i < num_groups) {
1403 /* A shared group exists so send a HelloRetryRequest */
1404 s->s3.group_id = group_id;
1405 s->hello_retry_request = SSL_HRR_PENDING;
1406 return 1;
1407 }
1408 }
1409 if (!s->hit
1410 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1411 /* Nothing left we can do - just fail */
1412 SSLfatal(s, sent ? SSL_AD_HANDSHAKE_FAILURE
1413 : SSL_AD_MISSING_EXTENSION,
1414 SSL_R_NO_SUITABLE_KEY_SHARE);
1415 return 0;
1416 }
1417
1418 if ((s->s3.flags & TLS1_FLAGS_STATELESS) != 0
1419 && !s->ext.cookieok) {
1420 if (!ossl_assert(s->hello_retry_request == SSL_HRR_NONE)) {
1421 /*
1422 * If we are stateless then we wouldn't know about any
1423 * previously sent HRR - so how can this be anything other
1424 * than 0?
1425 */
1426 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1427 return 0;
1428 }
1429 s->hello_retry_request = SSL_HRR_PENDING;
1430 return 1;
1431 }
1432 }
1433
1434 /*
1435 * We have a key_share so don't send any more HelloRetryRequest
1436 * messages
1437 */
1438 if (s->hello_retry_request == SSL_HRR_PENDING)
1439 s->hello_retry_request = SSL_HRR_COMPLETE;
1440 } else {
1441 /*
1442 * For a client side resumption with no key_share we need to generate
1443 * the handshake secret (otherwise this is done during key_share
1444 * processing).
1445 */
1446 if (!sent && !tls13_generate_handshake_secret(s, NULL, 0)) {
1447 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1448 return 0;
1449 }
1450 }
1451#endif /* !defined(OPENSSL_NO_TLS1_3) */
1452 return 1;
1453}
1454
1455static int init_psk_kex_modes(SSL *s, unsigned int context)
1456{
1457 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
1458 return 1;
1459}
1460
1461int tls_psk_do_binder(SSL *s, const EVP_MD *md, const unsigned char *msgstart,
1462 size_t binderoffset, const unsigned char *binderin,
1463 unsigned char *binderout, SSL_SESSION *sess, int sign,
1464 int external)
1465{
1466 EVP_PKEY *mackey = NULL;
1467 EVP_MD_CTX *mctx = NULL;
1468 unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1469 unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
1470 unsigned char *early_secret;
1471 /* ASCII: "res binder", in hex for EBCDIC compatibility */
1472 static const unsigned char resumption_label[] = "\x72\x65\x73\x20\x62\x69\x6E\x64\x65\x72";
1473 /* ASCII: "ext binder", in hex for EBCDIC compatibility */
1474 static const unsigned char external_label[] = "\x65\x78\x74\x20\x62\x69\x6E\x64\x65\x72";
1475 const unsigned char *label;
1476 size_t bindersize, labelsize, hashsize;
1477 int hashsizei = EVP_MD_get_size(md);
1478 int ret = -1;
1479 int usepskfored = 0;
1480
1481 /* Ensure cast to size_t is safe */
1482 if (!ossl_assert(hashsizei >= 0)) {
1483 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1484 goto err;
1485 }
1486 hashsize = (size_t)hashsizei;
1487
1488 if (external
1489 && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1490 && s->session->ext.max_early_data == 0
1491 && sess->ext.max_early_data > 0)
1492 usepskfored = 1;
1493
1494 if (external) {
1495 label = external_label;
1496 labelsize = sizeof(external_label) - 1;
1497 } else {
1498 label = resumption_label;
1499 labelsize = sizeof(resumption_label) - 1;
1500 }
1501
1502 /*
1503 * Generate the early_secret. On the server side we've selected a PSK to
1504 * resume with (internal or external) so we always do this. On the client
1505 * side we do this for a non-external (i.e. resumption) PSK or external PSK
1506 * that will be used for early_data so that it is in place for sending early
1507 * data. For client side external PSK not being used for early_data we
1508 * generate it but store it away for later use.
1509 */
1510 if (s->server || !external || usepskfored)
1511 early_secret = (unsigned char *)s->early_secret;
1512 else
1513 early_secret = (unsigned char *)sess->early_secret;
1514
1515 if (!tls13_generate_secret(s, md, NULL, sess->master_key,
1516 sess->master_key_length, early_secret)) {
1517 /* SSLfatal() already called */
1518 goto err;
1519 }
1520
1521 /*
1522 * Create the handshake hash for the binder key...the messages so far are
1523 * empty!
1524 */
1525 mctx = EVP_MD_CTX_new();
1526 if (mctx == NULL
1527 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1528 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1529 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1530 goto err;
1531 }
1532
1533 /* Generate the binder key */
1534 if (!tls13_hkdf_expand(s, md, early_secret, label, labelsize, hash,
1535 hashsize, binderkey, hashsize, 1)) {
1536 /* SSLfatal() already called */
1537 goto err;
1538 }
1539
1540 /* Generate the finished key */
1541 if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1542 /* SSLfatal() already called */
1543 goto err;
1544 }
1545
1546 if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1547 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1548 goto err;
1549 }
1550
1551 /*
1552 * Get a hash of the ClientHello up to the start of the binders. If we are
1553 * following a HelloRetryRequest then this includes the hash of the first
1554 * ClientHello and the HelloRetryRequest itself.
1555 */
1556 if (s->hello_retry_request == SSL_HRR_PENDING) {
1557 size_t hdatalen;
1558 long hdatalen_l;
1559 void *hdata;
1560
1561 hdatalen = hdatalen_l =
1562 BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
1563 if (hdatalen_l <= 0) {
1564 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
1565 goto err;
1566 }
1567
1568 /*
1569 * For servers the handshake buffer data will include the second
1570 * ClientHello - which we don't want - so we need to take that bit off.
1571 */
1572 if (s->server) {
1573 PACKET hashprefix, msg;
1574
1575 /* Find how many bytes are left after the first two messages */
1576 if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1577 || !PACKET_forward(&hashprefix, 1)
1578 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1579 || !PACKET_forward(&hashprefix, 1)
1580 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
1581 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1582 goto err;
1583 }
1584 hdatalen -= PACKET_remaining(&hashprefix);
1585 }
1586
1587 if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1588 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1589 goto err;
1590 }
1591 }
1592
1593 if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1594 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1595 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1596 goto err;
1597 }
1598
1599 mackey = EVP_PKEY_new_raw_private_key_ex(s->ctx->libctx, "HMAC",
1600 s->ctx->propq, finishedkey,
1601 hashsize);
1602 if (mackey == NULL) {
1603 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1604 goto err;
1605 }
1606
1607 if (!sign)
1608 binderout = tmpbinder;
1609
1610 bindersize = hashsize;
1611 if (EVP_DigestSignInit_ex(mctx, NULL, EVP_MD_get0_name(md), s->ctx->libctx,
1612 s->ctx->propq, mackey, NULL) <= 0
1613 || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1614 || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1615 || bindersize != hashsize) {
1616 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
1617 goto err;
1618 }
1619
1620 if (sign) {
1621 ret = 1;
1622 } else {
1623 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1624 ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1625 if (!ret)
1626 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BINDER_DOES_NOT_VERIFY);
1627 }
1628
1629 err:
1630 OPENSSL_cleanse(binderkey, sizeof(binderkey));
1631 OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1632 EVP_PKEY_free(mackey);
1633 EVP_MD_CTX_free(mctx);
1634
1635 return ret;
1636}
1637
1638static int final_early_data(SSL *s, unsigned int context, int sent)
1639{
1640 if (!sent)
1641 return 1;
1642
1643 if (!s->server) {
1644 if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1645 && sent
1646 && !s->ext.early_data_ok) {
1647 /*
1648 * If we get here then the server accepted our early_data but we
1649 * later realised that it shouldn't have done (e.g. inconsistent
1650 * ALPN)
1651 */
1652 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EARLY_DATA);
1653 return 0;
1654 }
1655
1656 return 1;
1657 }
1658
1659 if (s->max_early_data == 0
1660 || !s->hit
1661 || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1662 || !s->ext.early_data_ok
1663 || s->hello_retry_request != SSL_HRR_NONE
1664 || (s->allow_early_data_cb != NULL
1665 && !s->allow_early_data_cb(s,
1666 s->allow_early_data_cb_data))) {
1667 s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1668 } else {
1669 s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1670
1671 if (!tls13_change_cipher_state(s,
1672 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1673 /* SSLfatal() already called */
1674 return 0;
1675 }
1676 }
1677
1678 return 1;
1679}
1680
1681static int final_maxfragmentlen(SSL *s, unsigned int context, int sent)
1682{
1683 /*
1684 * Session resumption on server-side with MFL extension active
1685 * BUT MFL extension packet was not resent (i.e. sent == 0)
1686 */
1687 if (s->server && s->hit && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
1688 && !sent ) {
1689 SSLfatal(s, SSL_AD_MISSING_EXTENSION, SSL_R_BAD_EXTENSION);
1690 return 0;
1691 }
1692
1693 /* Current SSL buffer is lower than requested MFL */
1694 if (s->session && USE_MAX_FRAGMENT_LENGTH_EXT(s->session)
1695 && s->max_send_fragment < GET_MAX_FRAGMENT_LENGTH(s->session))
1696 /* trigger a larger buffer reallocation */
1697 if (!ssl3_setup_buffers(s)) {
1698 /* SSLfatal() already called */
1699 return 0;
1700 }
1701
1702 return 1;
1703}
1704
1705static int init_post_handshake_auth(SSL *s, ossl_unused unsigned int context)
1706{
1707 s->post_handshake_auth = SSL_PHA_NONE;
1708
1709 return 1;
1710}
1711
1712/*
1713 * If clients offer "pre_shared_key" without a "psk_key_exchange_modes"
1714 * extension, servers MUST abort the handshake.
1715 */
1716static int final_psk(SSL *s, unsigned int context, int sent)
1717{
1718 if (s->server && sent && s->clienthello != NULL
1719 && !s->clienthello->pre_proc_exts[TLSEXT_IDX_psk_kex_modes].present) {
1720 SSLfatal(s, TLS13_AD_MISSING_EXTENSION,
1721 SSL_R_MISSING_PSK_KEX_MODES_EXTENSION);
1722 return 0;
1723 }
1724
1725 return 1;
1726}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette