]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/statem/extensions.c
Fix incorrect function name in BN_bn2bin manpage
[thirdparty/openssl.git] / ssl / statem / extensions.c
CommitLineData
6b473aca 1/*
b186a592 2 * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
6b473aca
MC
3 *
4 * Licensed under the OpenSSL license (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
f6370040 10#include <string.h>
677963e5 11#include "internal/nelem.h"
6b473aca
MC
12#include "../ssl_locl.h"
13#include "statem_locl.h"
14
1266eefd 15static int final_renegotiate(SSL *s, unsigned int context, int sent,
805a2e9e 16 int *al);
1266eefd
MC
17static int init_server_name(SSL *s, unsigned int context);
18static int final_server_name(SSL *s, unsigned int context, int sent,
805a2e9e 19 int *al);
332eb390 20#ifndef OPENSSL_NO_EC
1266eefd 21static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
332eb390
MC
22 int *al);
23#endif
1266eefd 24static int init_session_ticket(SSL *s, unsigned int context);
8f8c11d8 25#ifndef OPENSSL_NO_OCSP
1266eefd 26static int init_status_request(SSL *s, unsigned int context);
8f8c11d8 27#endif
805a2e9e 28#ifndef OPENSSL_NO_NEXTPROTONEG
1266eefd 29static int init_npn(SSL *s, unsigned int context);
805a2e9e 30#endif
1266eefd 31static int init_alpn(SSL *s, unsigned int context);
630369d9 32static int final_alpn(SSL *s, unsigned int context, int sent, int *al);
1266eefd 33static int init_sig_algs(SSL *s, unsigned int context);
45615c5f 34static int init_certificate_authorities(SSL *s, unsigned int context);
b186a592
MC
35static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
36 unsigned int context,
37 X509 *x,
38 size_t chainidx,
39 int *al);
45615c5f
DSH
40static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
41 unsigned int context, X509 *x,
42 size_t chainidx, int *al);
805a2e9e 43#ifndef OPENSSL_NO_SRP
1266eefd 44static int init_srp(SSL *s, unsigned int context);
805a2e9e 45#endif
1266eefd
MC
46static int init_etm(SSL *s, unsigned int context);
47static int init_ems(SSL *s, unsigned int context);
48static int final_ems(SSL *s, unsigned int context, int sent, int *al);
b2f7e8c0 49static int init_psk_kex_modes(SSL *s, unsigned int context);
deb2d5e7 50#ifndef OPENSSL_NO_EC
f4bbb37c 51static int final_key_share(SSL *s, unsigned int context, int sent, int *al);
deb2d5e7 52#endif
805a2e9e 53#ifndef OPENSSL_NO_SRTP
1266eefd 54static int init_srtp(SSL *s, unsigned int context);
805a2e9e 55#endif
04904312 56static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al);
38df5a45 57static int final_early_data(SSL *s, unsigned int context, int sent, int *al);
805a2e9e 58
70af3d8e 59/* Structure to define a built-in extension */
1266eefd
MC
60typedef struct extensions_definition_st {
61 /* The defined type for the extension */
6b473aca 62 unsigned int type;
1266eefd
MC
63 /*
64 * The context that this extension applies to, e.g. what messages and
65 * protocol versions
66 */
67 unsigned int context;
68db4dda 68 /*
805a2e9e
MC
69 * Initialise extension before parsing. Always called for relevant contexts
70 * even if extension not present
68db4dda 71 */
1266eefd
MC
72 int (*init)(SSL *s, unsigned int context);
73 /* Parse extension sent from client to server */
61138358
MC
74 int (*parse_ctos)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
75 size_t chainidx, int *al);
1266eefd 76 /* Parse extension send from server to client */
61138358
MC
77 int (*parse_stoc)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
78 size_t chainidx, int *al);
1266eefd 79 /* Construct extension sent from server to client */
b186a592
MC
80 EXT_RETURN (*construct_stoc)(SSL *s, WPACKET *pkt, unsigned int context,
81 X509 *x, size_t chainidx, int *al);
1266eefd 82 /* Construct extension sent from client to server */
b186a592
MC
83 EXT_RETURN (*construct_ctos)(SSL *s, WPACKET *pkt, unsigned int context,
84 X509 *x, size_t chainidx, int *al);
68db4dda 85 /*
805a2e9e
MC
86 * Finalise extension after parsing. Always called where an extensions was
87 * initialised even if the extension was not present. |sent| is set to 1 if
88 * the extension was seen, or 0 otherwise.
68db4dda 89 */
1266eefd 90 int (*final)(SSL *s, unsigned int context, int sent, int *al);
6b473aca
MC
91} EXTENSION_DEFINITION;
92
4b299b8e 93/*
70af3d8e 94 * Definitions of all built-in extensions. NOTE: Changes in the number or order
bd91e3c8 95 * of these extensions should be mirrored with equivalent changes to the
3e6c1da8 96 * indexes ( TLSEXT_IDX_* ) defined in ssl_locl.h.
70af3d8e
MC
97 * Each extension has an initialiser, a client and
98 * server side parser and a finaliser. The initialiser is called (if the
99 * extension is relevant to the given context) even if we did not see the
100 * extension in the message that we received. The parser functions are only
101 * called if we see the extension in the message. The finalisers are always
102 * called if the initialiser was called.
103 * There are also server and client side constructor functions which are always
104 * called during message construction if the extension is relevant for the
105 * given context.
106 * The initialisation, parsing, finalisation and construction functions are
107 * always called in the order defined in this list. Some extensions may depend
108 * on others having been processed first, so the order of this list is
109 * significant.
110 * The extension context is defined by a series of flags which specify which
111 * messages the extension is relevant to. These flags also specify whether the
3e6c1da8 112 * extension is relevant to a particular protocol or protocol version.
a1448c26 113 *
70af3d8e 114 * TODO(TLS1.3): Make sure we have a test to check the consistency of these
10ed1b72
TS
115 *
116 * NOTE: WebSphere Application Server 7+ cannot handle empty extensions at
117 * the end, keep these extensions before signature_algorithm.
4b299b8e 118 */
0785274c 119#define INVALID_EXTENSION { 0x10000, 0, NULL, NULL, NULL, NULL, NULL, NULL }
6b473aca
MC
120static const EXTENSION_DEFINITION ext_defs[] = {
121 {
122 TLSEXT_TYPE_renegotiate,
fe874d27
MC
123 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
124 | SSL_EXT_SSL3_ALLOWED | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
125 NULL, tls_parse_ctos_renegotiate, tls_parse_stoc_renegotiate,
126 tls_construct_stoc_renegotiate, tls_construct_ctos_renegotiate,
127 final_renegotiate
6b473aca
MC
128 },
129 {
130 TLSEXT_TYPE_server_name,
fe874d27
MC
131 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
132 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
1266eefd
MC
133 init_server_name,
134 tls_parse_ctos_server_name, tls_parse_stoc_server_name,
135 tls_construct_stoc_server_name, tls_construct_ctos_server_name,
136 final_server_name
6b473aca
MC
137 },
138#ifndef OPENSSL_NO_SRP
139 {
140 TLSEXT_TYPE_srp,
fe874d27 141 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd 142 init_srp, tls_parse_ctos_srp, NULL, NULL, tls_construct_ctos_srp, NULL
6b473aca 143 },
0785274c
MC
144#else
145 INVALID_EXTENSION,
6b473aca
MC
146#endif
147#ifndef OPENSSL_NO_EC
148 {
149 TLSEXT_TYPE_ec_point_formats,
fe874d27
MC
150 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
151 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
152 NULL, tls_parse_ctos_ec_pt_formats, tls_parse_stoc_ec_pt_formats,
153 tls_construct_stoc_ec_pt_formats, tls_construct_ctos_ec_pt_formats,
154 final_ec_pt_formats
6b473aca
MC
155 },
156 {
157 TLSEXT_TYPE_supported_groups,
fe874d27 158 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
1266eefd 159 NULL, tls_parse_ctos_supported_groups, NULL,
6af87546 160 tls_construct_stoc_supported_groups,
1266eefd 161 tls_construct_ctos_supported_groups, NULL
6b473aca 162 },
0785274c
MC
163#else
164 INVALID_EXTENSION,
165 INVALID_EXTENSION,
6b473aca
MC
166#endif
167 {
168 TLSEXT_TYPE_session_ticket,
fe874d27
MC
169 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
170 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
171 init_session_ticket, tls_parse_ctos_session_ticket,
172 tls_parse_stoc_session_ticket, tls_construct_stoc_session_ticket,
173 tls_construct_ctos_session_ticket, NULL
6b473aca 174 },
ab83e314 175#ifndef OPENSSL_NO_OCSP
6b473aca
MC
176 {
177 TLSEXT_TYPE_status_request,
fe874d27
MC
178 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
179 | SSL_EXT_TLS1_3_CERTIFICATE,
1266eefd
MC
180 init_status_request, tls_parse_ctos_status_request,
181 tls_parse_stoc_status_request, tls_construct_stoc_status_request,
f63e4288 182 tls_construct_ctos_status_request, NULL
6b473aca 183 },
0785274c
MC
184#else
185 INVALID_EXTENSION,
ab83e314 186#endif
6b473aca
MC
187#ifndef OPENSSL_NO_NEXTPROTONEG
188 {
189 TLSEXT_TYPE_next_proto_neg,
fe874d27
MC
190 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
191 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
192 init_npn, tls_parse_ctos_npn, tls_parse_stoc_npn,
193 tls_construct_stoc_next_proto_neg, tls_construct_ctos_npn, NULL
6b473aca 194 },
0785274c
MC
195#else
196 INVALID_EXTENSION,
6b473aca
MC
197#endif
198 {
02f0274e
MC
199 /*
200 * Must appear in this list after server_name so that finalisation
201 * happens after server_name callbacks
202 */
6b473aca 203 TLSEXT_TYPE_application_layer_protocol_negotiation,
fe874d27
MC
204 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
205 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS,
1266eefd 206 init_alpn, tls_parse_ctos_alpn, tls_parse_stoc_alpn,
630369d9 207 tls_construct_stoc_alpn, tls_construct_ctos_alpn, final_alpn
6b473aca 208 },
7da160b0 209#ifndef OPENSSL_NO_SRTP
6b473aca
MC
210 {
211 TLSEXT_TYPE_use_srtp,
fe874d27
MC
212 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
213 | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS | SSL_EXT_DTLS_ONLY,
1266eefd
MC
214 init_srtp, tls_parse_ctos_use_srtp, tls_parse_stoc_use_srtp,
215 tls_construct_stoc_use_srtp, tls_construct_ctos_use_srtp, NULL
6b473aca 216 },
0785274c
MC
217#else
218 INVALID_EXTENSION,
7da160b0 219#endif
6b473aca
MC
220 {
221 TLSEXT_TYPE_encrypt_then_mac,
fe874d27
MC
222 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
223 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
224 init_etm, tls_parse_ctos_etm, tls_parse_stoc_etm,
225 tls_construct_stoc_etm, tls_construct_ctos_etm, NULL
6b473aca 226 },
6dd083fd 227#ifndef OPENSSL_NO_CT
6b473aca
MC
228 {
229 TLSEXT_TYPE_signed_certificate_timestamp,
fe874d27
MC
230 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
231 | SSL_EXT_TLS1_3_CERTIFICATE,
68db4dda 232 NULL,
6b473aca
MC
233 /*
234 * No server side support for this, but can be provided by a custom
235 * extension. This is an exception to the rule that custom extensions
236 * cannot override built in ones.
237 */
1266eefd 238 NULL, tls_parse_stoc_sct, NULL, tls_construct_ctos_sct, NULL
6b473aca 239 },
0785274c
MC
240#else
241 INVALID_EXTENSION,
6dd083fd 242#endif
6b473aca
MC
243 {
244 TLSEXT_TYPE_extended_master_secret,
fe874d27
MC
245 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO
246 | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd
MC
247 init_ems, tls_parse_ctos_ems, tls_parse_stoc_ems,
248 tls_construct_stoc_ems, tls_construct_ctos_ems, final_ems
6b473aca 249 },
10ed1b72
TS
250 {
251 TLSEXT_TYPE_signature_algorithms,
252 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST,
253 init_sig_algs, tls_parse_ctos_sig_algs,
254 tls_parse_ctos_sig_algs, tls_construct_ctos_sig_algs,
255 tls_construct_ctos_sig_algs, final_sig_algs
256 },
6b473aca
MC
257 {
258 TLSEXT_TYPE_supported_versions,
fe874d27
MC
259 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
260 | SSL_EXT_TLS1_3_ONLY,
68db4dda 261 NULL,
6b473aca 262 /* Processed inline as part of version selection */
1266eefd 263 NULL, NULL, NULL, tls_construct_ctos_supported_versions, NULL
6b473aca 264 },
b2f7e8c0 265 {
b2f7e8c0 266 TLSEXT_TYPE_psk_kex_modes,
fe874d27
MC
267 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS_IMPLEMENTATION_ONLY
268 | SSL_EXT_TLS1_3_ONLY,
b2f7e8c0
MC
269 init_psk_kex_modes, tls_parse_ctos_psk_kex_modes, NULL, NULL,
270 tls_construct_ctos_psk_kex_modes, NULL
271 },
deb2d5e7 272#ifndef OPENSSL_NO_EC
6b473aca 273 {
70af3d8e
MC
274 /*
275 * Must be in this list after supported_groups. We need that to have
276 * been parsed before we do this one.
277 */
6b473aca 278 TLSEXT_TYPE_key_share,
fe874d27
MC
279 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
280 | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST | SSL_EXT_TLS_IMPLEMENTATION_ONLY
281 | SSL_EXT_TLS1_3_ONLY,
1266eefd 282 NULL, tls_parse_ctos_key_share, tls_parse_stoc_key_share,
f4bbb37c
MC
283 tls_construct_stoc_key_share, tls_construct_ctos_key_share,
284 final_key_share
7da160b0 285 },
deb2d5e7 286#endif
cfef5027
MC
287 {
288 TLSEXT_TYPE_cookie,
fe874d27
MC
289 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST
290 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
cfef5027
MC
291 NULL, NULL, tls_parse_stoc_cookie, NULL, tls_construct_ctos_cookie,
292 NULL
293 },
7da160b0
MC
294 {
295 /*
296 * Special unsolicited ServerHello extension only used when
297 * SSL_OP_CRYPTOPRO_TLSEXT_BUG is set
298 */
299 TLSEXT_TYPE_cryptopro_bug,
fe874d27 300 SSL_EXT_TLS1_2_SERVER_HELLO | SSL_EXT_TLS1_2_AND_BELOW_ONLY,
1266eefd 301 NULL, NULL, NULL, tls_construct_stoc_cryptopro_bug, NULL, NULL
ab83e314 302 },
38df5a45
MC
303 {
304 TLSEXT_TYPE_early_data,
fe874d27
MC
305 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
306 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET,
38df5a45
MC
307 NULL, tls_parse_ctos_early_data, tls_parse_stoc_early_data,
308 tls_construct_stoc_early_data, tls_construct_ctos_early_data,
309 final_early_data
310 },
45615c5f
DSH
311 {
312 TLSEXT_TYPE_certificate_authorities,
fe874d27
MC
313 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
314 | SSL_EXT_TLS1_3_ONLY,
45615c5f
DSH
315 init_certificate_authorities,
316 tls_parse_certificate_authorities, tls_parse_certificate_authorities,
317 tls_construct_certificate_authorities,
318 tls_construct_certificate_authorities, NULL,
319 },
ab83e314 320 {
ec15acb6 321 /* Must be immediately before pre_shared_key */
ab83e314 322 TLSEXT_TYPE_padding,
fe874d27 323 SSL_EXT_CLIENT_HELLO,
68db4dda 324 NULL,
ab83e314 325 /* We send this, but don't read it */
1266eefd 326 NULL, NULL, NULL, tls_construct_ctos_padding, NULL
ec15acb6
MC
327 },
328 {
329 /* Required by the TLSv1.3 spec to always be the last extension */
330 TLSEXT_TYPE_psk,
fe874d27
MC
331 SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_3_SERVER_HELLO
332 | SSL_EXT_TLS_IMPLEMENTATION_ONLY | SSL_EXT_TLS1_3_ONLY,
0247086d 333 NULL, tls_parse_ctos_psk, tls_parse_stoc_psk, tls_construct_stoc_psk,
1053a6e2 334 tls_construct_ctos_psk, NULL
6b473aca
MC
335 }
336};
337
43ae5eed
MC
338/* Check whether an extension's context matches the current context */
339static int validate_context(SSL *s, unsigned int extctx, unsigned int thisctx)
340{
341 /* Check we're allowed to use this extension in this context */
342 if ((thisctx & extctx) == 0)
343 return 0;
344
345 if (SSL_IS_DTLS(s)) {
346 if ((extctx & SSL_EXT_TLS_ONLY) != 0)
347 return 0;
348 } else if ((extctx & SSL_EXT_DTLS_ONLY) != 0) {
349 return 0;
350 }
351
352 return 1;
353}
354
6b473aca
MC
355/*
356 * Verify whether we are allowed to use the extension |type| in the current
357 * |context|. Returns 1 to indicate the extension is allowed or unknown or 0 to
70af3d8e 358 * indicate the extension is not allowed. If returning 1 then |*found| is set to
69687aa8 359 * the definition for the extension we found.
6b473aca 360 */
70af3d8e 361static int verify_extension(SSL *s, unsigned int context, unsigned int type,
1266eefd
MC
362 custom_ext_methods *meths, RAW_EXTENSION *rawexlist,
363 RAW_EXTENSION **found)
6b473aca
MC
364{
365 size_t i;
70af3d8e 366 size_t builtin_num = OSSL_NELEM(ext_defs);
d270de32 367 const EXTENSION_DEFINITION *thisext;
6b473aca 368
1266eefd
MC
369 for (i = 0, thisext = ext_defs; i < builtin_num; i++, thisext++) {
370 if (type == thisext->type) {
43ae5eed 371 if (!validate_context(s, thisext->context, context))
6b473aca
MC
372 return 0;
373
1266eefd 374 *found = &rawexlist[i];
6b473aca
MC
375 return 1;
376 }
377 }
378
70af3d8e
MC
379 /* Check the custom extensions */
380 if (meths != NULL) {
43ae5eed 381 size_t offset = 0;
787d9ec7 382 ENDPOINT role = ENDPOINT_BOTH;
43ae5eed
MC
383 custom_ext_method *meth = NULL;
384
385 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
787d9ec7 386 role = ENDPOINT_SERVER;
43ae5eed 387 else if ((context & SSL_EXT_TLS1_2_SERVER_HELLO) != 0)
787d9ec7 388 role = ENDPOINT_CLIENT;
43ae5eed 389
787d9ec7 390 meth = custom_ext_find(meths, role, type, &offset);
43ae5eed
MC
391 if (meth != NULL) {
392 if (!validate_context(s, meth->context, context))
393 return 0;
394 *found = &rawexlist[offset + builtin_num];
395 return 1;
6b473aca
MC
396 }
397 }
398
70af3d8e 399 /* Unknown extension. We allow it */
1266eefd 400 *found = NULL;
70af3d8e 401 return 1;
6b473aca
MC
402}
403
70af3d8e
MC
404/*
405 * Check whether the context defined for an extension |extctx| means whether
406 * the extension is relevant for the current context |thisctx| or not. Returns
407 * 1 if the extension is relevant for this context, and 0 otherwise
408 */
43ae5eed 409int extension_is_relevant(SSL *s, unsigned int extctx, unsigned int thisctx)
805a2e9e
MC
410{
411 if ((SSL_IS_DTLS(s)
fe874d27 412 && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
805a2e9e 413 || (s->version == SSL3_VERSION
fe874d27 414 && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
805a2e9e 415 || (SSL_IS_TLS13(s)
fe874d27 416 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
43ae5eed
MC
417 || (!SSL_IS_TLS13(s) && (extctx & SSL_EXT_TLS1_3_ONLY) != 0)
418 || (s->hit && (extctx & SSL_EXT_IGNORE_ON_RESUMPTION) != 0))
805a2e9e
MC
419 return 0;
420
421 return 1;
422}
423
6b473aca
MC
424/*
425 * Gather a list of all the extensions from the data in |packet]. |context|
70af3d8e 426 * tells us which message this extension is for. The raw extension data is
1266eefd
MC
427 * stored in |*res| on success. In the event of an error the alert type to use
428 * is stored in |*al|. We don't actually process the content of the extensions
429 * yet, except to check their types. This function also runs the initialiser
735d5b59
TT
430 * functions for all known extensions if |init| is nonzero (whether we have
431 * collected them or not). If successful the caller is responsible for freeing
432 * the contents of |*res|.
6b473aca
MC
433 *
434 * Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
435 * more than one extension of the same type in a ClientHello or ServerHello.
436 * This function returns 1 if all extensions are unique and we have parsed their
437 * types, and 0 if the extensions contain duplicates, could not be successfully
1266eefd 438 * found, or an internal error occurred. We only check duplicates for
70af3d8e 439 * extensions that we know about. We ignore others.
6b473aca 440 */
6b473aca 441int tls_collect_extensions(SSL *s, PACKET *packet, unsigned int context,
735d5b59
TT
442 RAW_EXTENSION **res, int *al, size_t *len,
443 int init)
6b473aca
MC
444{
445 PACKET extensions = *packet;
d270de32 446 size_t i = 0;
fc5ece2e 447 size_t num_exts;
43ae5eed 448 custom_ext_methods *exts = &s->cert->custext;
6b473aca 449 RAW_EXTENSION *raw_extensions = NULL;
d270de32 450 const EXTENSION_DEFINITION *thisexd;
6b473aca 451
ecc2f938
MC
452 *res = NULL;
453
70af3d8e
MC
454 /*
455 * Initialise server side custom extensions. Client side is done during
456 * construction of extensions for the ClientHello.
457 */
43ae5eed
MC
458 if ((context & SSL_EXT_CLIENT_HELLO) != 0)
459 custom_ext_init(&s->cert->custext);
70af3d8e 460
fc5ece2e
BK
461 num_exts = OSSL_NELEM(ext_defs) + (exts != NULL ? exts->meths_count : 0);
462 raw_extensions = OPENSSL_zalloc(num_exts * sizeof(*raw_extensions));
70af3d8e
MC
463 if (raw_extensions == NULL) {
464 *al = SSL_AD_INTERNAL_ERROR;
465 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, ERR_R_MALLOC_FAILURE);
466 return 0;
467 }
468
193b5d76 469 i = 0;
6b473aca 470 while (PACKET_remaining(&extensions) > 0) {
b186a592 471 unsigned int type, idx;
6b473aca 472 PACKET extension;
1266eefd 473 RAW_EXTENSION *thisex;
6b473aca
MC
474
475 if (!PACKET_get_net_2(&extensions, &type) ||
476 !PACKET_get_length_prefixed_2(&extensions, &extension)) {
477 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
70af3d8e 478 *al = SSL_AD_DECODE_ERROR;
6b473aca
MC
479 goto err;
480 }
70af3d8e
MC
481 /*
482 * Verify this extension is allowed. We only check duplicates for
652a6b7e
MC
483 * extensions that we recognise. We also have a special case for the
484 * PSK extension, which must be the last one in the ClientHello.
70af3d8e 485 */
1266eefd 486 if (!verify_extension(s, context, type, exts, raw_extensions, &thisex)
652a6b7e
MC
487 || (thisex != NULL && thisex->present == 1)
488 || (type == TLSEXT_TYPE_psk
fe874d27 489 && (context & SSL_EXT_CLIENT_HELLO) != 0
652a6b7e 490 && PACKET_remaining(&extensions) != 0)) {
6b473aca 491 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_BAD_EXTENSION);
70af3d8e 492 *al = SSL_AD_ILLEGAL_PARAMETER;
6b473aca
MC
493 goto err;
494 }
b186a592
MC
495 idx = thisex - raw_extensions;
496 /*-
497 * Check that we requested this extension (if appropriate). Requests can
498 * be sent in the ClientHello and CertificateRequest. Unsolicited
499 * extensions can be sent in the NewSessionTicket. We only do this for
500 * the built-in extensions. Custom extensions have a different but
501 * similar check elsewhere.
502 * Special cases:
503 * - The HRR cookie extension is unsolicited
504 * - The renegotiate extension is unsolicited (the client signals
505 * support via an SCSV)
506 * - The signed_certificate_timestamp extension can be provided by a
507 * custom extension or by the built-in version. We let the extension
508 * itself handle unsolicited response checks.
509 */
510 if (idx < OSSL_NELEM(ext_defs)
511 && (context & (SSL_EXT_CLIENT_HELLO
512 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
513 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) == 0
514 && type != TLSEXT_TYPE_cookie
515 && type != TLSEXT_TYPE_renegotiate
516 && type != TLSEXT_TYPE_signed_certificate_timestamp
517 && (s->ext.extflags[idx] & SSL_EXT_FLAG_SENT) == 0) {
355a0d10 518 SSLerr(SSL_F_TLS_COLLECT_EXTENSIONS, SSL_R_UNSOLICITED_EXTENSION);
b186a592
MC
519 *al = SSL_AD_UNSUPPORTED_EXTENSION;
520 goto err;
521 }
1266eefd
MC
522 if (thisex != NULL) {
523 thisex->data = extension;
524 thisex->present = 1;
525 thisex->type = type;
193b5d76 526 thisex->received_order = i++;
b93a295a
TS
527 if (s->ext.debug_cb)
528 s->ext.debug_cb(s, !s->server, thisex->type,
529 PACKET_data(&thisex->data),
530 PACKET_remaining(&thisex->data),
531 s->ext.debug_arg);
6b473aca
MC
532 }
533 }
534
735d5b59
TT
535 if (init) {
536 /*
537 * Initialise all known extensions relevant to this context,
538 * whether we have found them or not
539 */
540 for (thisexd = ext_defs, i = 0; i < OSSL_NELEM(ext_defs);
541 i++, thisexd++) {
bf5c84f5
TT
542 if (thisexd->init != NULL && (thisexd->context & context) != 0
543 && extension_is_relevant(s, thisexd->context, context)
544 && !thisexd->init(s, context)) {
735d5b59
TT
545 *al = SSL_AD_INTERNAL_ERROR;
546 goto err;
547 }
68db4dda
MC
548 }
549 }
550
6b473aca 551 *res = raw_extensions;
fc5ece2e
BK
552 if (len != NULL)
553 *len = num_exts;
6b473aca
MC
554 return 1;
555
556 err:
557 OPENSSL_free(raw_extensions);
558 return 0;
559}
560
68db4dda 561/*
70af3d8e
MC
562 * Runs the parser for a given extension with index |idx|. |exts| contains the
563 * list of all parsed extensions previously collected by
564 * tls_collect_extensions(). The parser is only run if it is applicable for the
f97d4c37
MC
565 * given |context| and the parser has not already been run. If this is for a
566 * Certificate message, then we also provide the parser with the relevant
8521ced6 567 * Certificate |x| and its position in the |chainidx| with 0 being the first
f97d4c37
MC
568 * Certificate. Returns 1 on success or 0 on failure. In the event of a failure
569 * |*al| is populated with a suitable alert code. If an extension is not present
570 * this counted as success.
68db4dda 571 */
d270de32 572int tls_parse_extension(SSL *s, TLSEXT_INDEX idx, int context,
8521ced6 573 RAW_EXTENSION *exts, X509 *x, size_t chainidx, int *al)
6b473aca 574{
70af3d8e 575 RAW_EXTENSION *currext = &exts[idx];
61138358
MC
576 int (*parser)(SSL *s, PACKET *pkt, unsigned int context, X509 *x,
577 size_t chainidx, int *al) = NULL;
6b473aca 578
70af3d8e
MC
579 /* Skip if the extension is not present */
580 if (!currext->present)
581 return 1;
6b473aca 582
70af3d8e
MC
583 /* Skip if we've already parsed this extension */
584 if (currext->parsed)
585 return 1;
6b473aca 586
70af3d8e
MC
587 currext->parsed = 1;
588
589 if (idx < OSSL_NELEM(ext_defs)) {
590 /* We are handling a built-in extension */
591 const EXTENSION_DEFINITION *extdef = &ext_defs[idx];
592
593 /* Check if extension is defined for our protocol. If not, skip */
594 if (!extension_is_relevant(s, extdef->context, context))
595 return 1;
596
1266eefd 597 parser = s->server ? extdef->parse_ctos : extdef->parse_stoc;
224135e9 598
1266eefd 599 if (parser != NULL)
61138358 600 return parser(s, &currext->data, context, x, chainidx, al);
6b473aca 601
70af3d8e
MC
602 /*
603 * If the parser is NULL we fall through to the custom extension
604 * processing
605 */
6b473aca
MC
606 }
607
43ae5eed
MC
608 /* Parse custom extensions */
609 if (custom_ext_parse(s, context, currext->type,
610 PACKET_data(&currext->data),
611 PACKET_remaining(&currext->data),
612 x, chainidx, al) <= 0)
70af3d8e
MC
613 return 0;
614
805a2e9e
MC
615 return 1;
616}
617
618/*
619 * Parse all remaining extensions that have not yet been parsed. Also calls the
735d5b59
TT
620 * finalisation for all extensions at the end if |fin| is nonzero, whether we
621 * collected them or not. Returns 1 for success or 0 for failure. If we are
622 * working on a Certificate message then we also pass the Certificate |x| and
623 * its position in the |chainidx|, with 0 being the first certificate. On
624 * failure, |*al| is populated with a suitable alert code.
805a2e9e 625 */
f97d4c37 626int tls_parse_all_extensions(SSL *s, int context, RAW_EXTENSION *exts, X509 *x,
735d5b59 627 size_t chainidx, int *al, int fin)
805a2e9e 628{
1266eefd 629 size_t i, numexts = OSSL_NELEM(ext_defs);
d270de32 630 const EXTENSION_DEFINITION *thisexd;
805a2e9e 631
70af3d8e 632 /* Calculate the number of extensions in the extensions list */
43ae5eed 633 numexts += s->cert->custext.meths_count;
70af3d8e
MC
634
635 /* Parse each extension in turn */
1266eefd 636 for (i = 0; i < numexts; i++) {
8521ced6 637 if (!tls_parse_extension(s, i, context, exts, x, chainidx, al))
70af3d8e
MC
638 return 0;
639 }
805a2e9e 640
735d5b59
TT
641 if (fin) {
642 /*
643 * Finalise all known extensions relevant to this context,
644 * whether we have found them or not
645 */
646 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs);
647 i++, thisexd++) {
bf5c84f5
TT
648 if (thisexd->final != NULL && (thisexd->context & context) != 0
649 && !thisexd->final(s, context, exts[i].present, al))
735d5b59
TT
650 return 0;
651 }
68db4dda
MC
652 }
653
6b473aca
MC
654 return 1;
655}
656
43ae5eed
MC
657int should_add_extension(SSL *s, unsigned int extctx, unsigned int thisctx,
658 int max_version)
659{
660 /* Skip if not relevant for our context */
661 if ((extctx & thisctx) == 0)
662 return 0;
663
664 /* Check if this extension is defined for our protocol. If not, skip */
665 if ((SSL_IS_DTLS(s) && (extctx & SSL_EXT_TLS_IMPLEMENTATION_ONLY) != 0)
666 || (s->version == SSL3_VERSION
667 && (extctx & SSL_EXT_SSL3_ALLOWED) == 0)
668 || (SSL_IS_TLS13(s)
669 && (extctx & SSL_EXT_TLS1_2_AND_BELOW_ONLY) != 0)
670 || (!SSL_IS_TLS13(s)
671 && (extctx & SSL_EXT_TLS1_3_ONLY) != 0
672 && (thisctx & SSL_EXT_CLIENT_HELLO) == 0)
673 || ((extctx & SSL_EXT_TLS1_3_ONLY) != 0
674 && (thisctx & SSL_EXT_CLIENT_HELLO) != 0
675 && (SSL_IS_DTLS(s) || max_version < TLS1_3_VERSION)))
676 return 0;
677
678 return 1;
679}
680
6b473aca 681/*
70af3d8e 682 * Construct all the extensions relevant to the current |context| and write
30aeba43 683 * them to |pkt|. If this is an extension for a Certificate in a Certificate
8521ced6
MC
684 * message, then |x| will be set to the Certificate we are handling, and
685 * |chainidx| will indicate the position in the chainidx we are processing (with
686 * 0 being the first in the chain). Returns 1 on success or 0 on failure. If a
687 * failure occurs then |al| is populated with a suitable alert code. On a
688 * failure construction stops at the first extension to fail to construct.
6b473aca 689 */
224135e9 690int tls_construct_extensions(SSL *s, WPACKET *pkt, unsigned int context,
8521ced6 691 X509 *x, size_t chainidx, int *al)
224135e9 692{
1266eefd 693 size_t i;
43ae5eed 694 int min_version, max_version = 0, reason, tmpal;
d270de32 695 const EXTENSION_DEFINITION *thisexd;
224135e9 696
7da160b0 697 /*
70af3d8e 698 * Normally if something goes wrong during construction it's an internal
7da160b0
MC
699 * error. We can always override this later.
700 */
70af3d8e 701 tmpal = SSL_AD_INTERNAL_ERROR;
7da160b0 702
224135e9
MC
703 if (!WPACKET_start_sub_packet_u16(pkt)
704 /*
705 * If extensions are of zero length then we don't even add the
1c259bb5
BK
706 * extensions length bytes to a ClientHello/ServerHello
707 * (for non-TLSv1.3).
224135e9 708 */
fe874d27
MC
709 || ((context &
710 (SSL_EXT_CLIENT_HELLO | SSL_EXT_TLS1_2_SERVER_HELLO)) != 0
fe874d27 711 && !WPACKET_set_flags(pkt,
224135e9 712 WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))) {
224135e9 713 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
70af3d8e 714 goto err;
224135e9
MC
715 }
716
fe874d27 717 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
38a73150 718 reason = ssl_get_min_max_version(s, &min_version, &max_version);
ab83e314
MC
719 if (reason != 0) {
720 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, reason);
70af3d8e 721 goto err;
ab83e314
MC
722 }
723 }
724
725 /* Add custom extensions first */
fe874d27 726 if ((context & SSL_EXT_CLIENT_HELLO) != 0) {
44e69951 727 /* On the server side with initialise during ClientHello parsing */
43ae5eed 728 custom_ext_init(&s->cert->custext);
ab83e314 729 }
43ae5eed 730 if (!custom_ext_add(s, context, pkt, x, chainidx, max_version, &tmpal)) {
ab83e314 731 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
70af3d8e 732 goto err;
ab83e314
MC
733 }
734
1266eefd 735 for (i = 0, thisexd = ext_defs; i < OSSL_NELEM(ext_defs); i++, thisexd++) {
b186a592
MC
736 EXT_RETURN (*construct)(SSL *s, WPACKET *pkt, unsigned int context,
737 X509 *x, size_t chainidx, int *al);
738 EXT_RETURN ret;
4b299b8e 739
224135e9 740 /* Skip if not relevant for our context */
43ae5eed 741 if (!should_add_extension(s, thisexd->context, context, max_version))
224135e9
MC
742 continue;
743
1266eefd
MC
744 construct = s->server ? thisexd->construct_stoc
745 : thisexd->construct_ctos;
224135e9 746
43ae5eed 747 if (construct == NULL)
224135e9
MC
748 continue;
749
b186a592
MC
750 ret = construct(s, pkt, context, x, chainidx, &tmpal);
751 if (ret == EXT_RETURN_FAIL)
70af3d8e 752 goto err;
b186a592
MC
753 if (ret == EXT_RETURN_SENT
754 && (context & (SSL_EXT_CLIENT_HELLO
755 | SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
756 | SSL_EXT_TLS1_3_NEW_SESSION_TICKET)) != 0)
757 s->ext.extflags[i] |= SSL_EXT_FLAG_SENT;
224135e9
MC
758 }
759
224135e9 760 if (!WPACKET_close(pkt)) {
224135e9 761 SSLerr(SSL_F_TLS_CONSTRUCT_EXTENSIONS, ERR_R_INTERNAL_ERROR);
70af3d8e 762 goto err;
224135e9
MC
763 }
764
765 return 1;
70af3d8e
MC
766
767 err:
768 *al = tmpal;
769 return 0;
224135e9 770}
805a2e9e 771
70af3d8e
MC
772/*
773 * Built in extension finalisation and initialisation functions. All initialise
774 * or finalise the associated extension type for the given |context|. For
775 * finalisers |sent| is set to 1 if we saw the extension during parsing, and 0
776 * otherwise. These functions return 1 on success or 0 on failure. In the event
777 * of a failure then |*al| is populated with a suitable error code.
778 */
779
1266eefd 780static int final_renegotiate(SSL *s, unsigned int context, int sent,
805a2e9e
MC
781 int *al)
782{
332eb390
MC
783 if (!s->server) {
784 /*
785 * Check if we can connect to a server that doesn't support safe
786 * renegotiation
787 */
788 if (!(s->options & SSL_OP_LEGACY_SERVER_CONNECT)
789 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
790 && !sent) {
791 *al = SSL_AD_HANDSHAKE_FAILURE;
7fe97c07 792 SSLerr(SSL_F_FINAL_RENEGOTIATE,
332eb390
MC
793 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
794 return 0;
795 }
796
805a2e9e 797 return 1;
332eb390 798 }
805a2e9e
MC
799
800 /* Need RI if renegotiating */
801 if (s->renegotiate
802 && !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)
803 && !sent) {
804 *al = SSL_AD_HANDSHAKE_FAILURE;
7fe97c07 805 SSLerr(SSL_F_FINAL_RENEGOTIATE,
805a2e9e
MC
806 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
807 return 0;
808 }
809
332eb390 810
805a2e9e
MC
811 return 1;
812}
813
1266eefd 814static int init_server_name(SSL *s, unsigned int context)
805a2e9e
MC
815{
816 if (s->server)
817 s->servername_done = 0;
818
819 return 1;
820}
821
1266eefd 822static int final_server_name(SSL *s, unsigned int context, int sent,
805a2e9e
MC
823 int *al)
824{
825 int ret = SSL_TLSEXT_ERR_NOACK;
826 int altmp = SSL_AD_UNRECOGNIZED_NAME;
a84e5c9a 827 int was_ticket = (SSL_get_options(s) & SSL_OP_NO_TICKET) == 0;
805a2e9e 828
aff8c126
RS
829 if (s->ctx != NULL && s->ctx->ext.servername_cb != 0)
830 ret = s->ctx->ext.servername_cb(s, &altmp,
831 s->ctx->ext.servername_arg);
222da979
TS
832 else if (s->session_ctx != NULL
833 && s->session_ctx->ext.servername_cb != 0)
834 ret = s->session_ctx->ext.servername_cb(s, &altmp,
835 s->session_ctx->ext.servername_arg);
805a2e9e 836
9fb6cb81
MC
837 if (!sent) {
838 OPENSSL_free(s->session->ext.hostname);
839 s->session->ext.hostname = NULL;
840 }
841
a84e5c9a
TS
842 /*
843 * If we're expecting to send a ticket, and tickets were previously enabled,
844 * and now tickets are disabled, then turn off expected ticket.
845 * Also, if this is not a resumption, create a new session ID
846 */
847 if (ret == SSL_TLSEXT_ERR_OK && s->ext.ticket_expected
848 && was_ticket && (SSL_get_options(s) & SSL_OP_NO_TICKET) != 0) {
849 s->ext.ticket_expected = 0;
850 if (!s->hit) {
851 SSL_SESSION* ss = SSL_get_session(s);
852
853 if (ss != NULL) {
854 OPENSSL_free(ss->ext.tick);
855 ss->ext.tick = NULL;
856 ss->ext.ticklen = 0;
857 ss->ext.tick_lifetime_hint = 0;
858 ss->ext.tick_age_add = 0;
859 ss->ext.tick_identity = 0;
860 if (!ssl_generate_session_id(s, ss)) {
861 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
862 altmp = SSL_AD_INTERNAL_ERROR;
863 }
864 } else {
865 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
866 altmp = SSL_AD_INTERNAL_ERROR;
867 }
868 }
869 }
870
805a2e9e
MC
871 switch (ret) {
872 case SSL_TLSEXT_ERR_ALERT_FATAL:
873 *al = altmp;
874 return 0;
875
876 case SSL_TLSEXT_ERR_ALERT_WARNING:
877 *al = altmp;
878 return 1;
879
880 case SSL_TLSEXT_ERR_NOACK:
881 s->servername_done = 0;
4be3a7c7 882 if (s->server && s->session->ext.hostname != NULL)
630369d9 883 s->ext.early_data_ok = 0;
805a2e9e
MC
884 return 1;
885
886 default:
887 return 1;
888 }
889}
890
332eb390 891#ifndef OPENSSL_NO_EC
1266eefd 892static int final_ec_pt_formats(SSL *s, unsigned int context, int sent,
332eb390
MC
893 int *al)
894{
895 unsigned long alg_k, alg_a;
896
897 if (s->server)
898 return 1;
899
900 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
901 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
902
903 /*
904 * If we are client and using an elliptic curve cryptography cipher
905 * suite, then if server returns an EC point formats lists extension it
906 * must contain uncompressed.
907 */
aff8c126
RS
908 if (s->ext.ecpointformats != NULL
909 && s->ext.ecpointformats_len > 0
910 && s->session->ext.ecpointformats != NULL
911 && s->session->ext.ecpointformats_len > 0
1266eefd 912 && ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA))) {
332eb390
MC
913 /* we are using an ECC cipher */
914 size_t i;
aff8c126 915 unsigned char *list = s->session->ext.ecpointformats;
1266eefd 916
aff8c126 917 for (i = 0; i < s->session->ext.ecpointformats_len; i++) {
1266eefd 918 if (*list++ == TLSEXT_ECPOINTFORMAT_uncompressed)
332eb390 919 break;
332eb390 920 }
aff8c126 921 if (i == s->session->ext.ecpointformats_len) {
7fe97c07 922 SSLerr(SSL_F_FINAL_EC_PT_FORMATS,
332eb390
MC
923 SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
924 return 0;
925 }
926 }
927
928 return 1;
929}
930#endif
931
1266eefd 932static int init_session_ticket(SSL *s, unsigned int context)
332eb390
MC
933{
934 if (!s->server)
aff8c126 935 s->ext.ticket_expected = 0;
332eb390
MC
936
937 return 1;
938}
939
8f8c11d8 940#ifndef OPENSSL_NO_OCSP
1266eefd 941static int init_status_request(SSL *s, unsigned int context)
805a2e9e 942{
f63e4288 943 if (s->server) {
aff8c126 944 s->ext.status_type = TLSEXT_STATUSTYPE_nothing;
f63e4288
MC
945 } else {
946 /*
947 * Ensure we get sensible values passed to tlsext_status_cb in the event
948 * that we don't receive a status message
949 */
8cbfcc70
RS
950 OPENSSL_free(s->ext.ocsp.resp);
951 s->ext.ocsp.resp = NULL;
952 s->ext.ocsp.resp_len = 0;
f63e4288 953 }
332eb390
MC
954
955 return 1;
956}
8f8c11d8 957#endif
332eb390 958
805a2e9e 959#ifndef OPENSSL_NO_NEXTPROTONEG
1266eefd 960static int init_npn(SSL *s, unsigned int context)
805a2e9e 961{
aff8c126 962 s->s3->npn_seen = 0;
805a2e9e
MC
963
964 return 1;
965}
966#endif
967
1266eefd 968static int init_alpn(SSL *s, unsigned int context)
805a2e9e 969{
332eb390
MC
970 OPENSSL_free(s->s3->alpn_selected);
971 s->s3->alpn_selected = NULL;
a5bb1aa1 972 s->s3->alpn_selected_len = 0;
805a2e9e 973 if (s->server) {
805a2e9e
MC
974 OPENSSL_free(s->s3->alpn_proposed);
975 s->s3->alpn_proposed = NULL;
976 s->s3->alpn_proposed_len = 0;
977 }
805a2e9e
MC
978 return 1;
979}
980
630369d9
MC
981static int final_alpn(SSL *s, unsigned int context, int sent, int *al)
982{
4be3a7c7
MC
983 if (!s->server && !sent && s->session->ext.alpn_selected != NULL)
984 s->ext.early_data_ok = 0;
985
630369d9
MC
986 if (!s->server || !SSL_IS_TLS13(s))
987 return 1;
988
989 /*
990 * Call alpn_select callback if needed. Has to be done after SNI and
991 * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3
992 * we also have to do this before we decide whether to accept early_data.
993 * In TLSv1.3 we've already negotiated our cipher so we do this call now.
994 * For < TLSv1.3 we defer it until after cipher negotiation.
995 */
996 return tls_handle_alpn(s, al);
997}
998
1266eefd 999static int init_sig_algs(SSL *s, unsigned int context)
805a2e9e
MC
1000{
1001 /* Clear any signature algorithms extension received */
1002 OPENSSL_free(s->s3->tmp.peer_sigalgs);
1003 s->s3->tmp.peer_sigalgs = NULL;
1004
1005 return 1;
1006}
1007
1008#ifndef OPENSSL_NO_SRP
1266eefd 1009static int init_srp(SSL *s, unsigned int context)
805a2e9e
MC
1010{
1011 OPENSSL_free(s->srp_ctx.login);
1012 s->srp_ctx.login = NULL;
1013
1014 return 1;
1015}
1016#endif
1017
1266eefd 1018static int init_etm(SSL *s, unsigned int context)
805a2e9e 1019{
28a31a0a 1020 s->ext.use_etm = 0;
332eb390
MC
1021
1022 return 1;
1023}
1024
1266eefd 1025static int init_ems(SSL *s, unsigned int context)
332eb390
MC
1026{
1027 if (!s->server)
1028 s->s3->flags &= ~TLS1_FLAGS_RECEIVED_EXTMS;
1029
1030 return 1;
1031}
1032
1266eefd 1033static int final_ems(SSL *s, unsigned int context, int sent, int *al)
332eb390
MC
1034{
1035 if (!s->server && s->hit) {
1036 /*
1037 * Check extended master secret extension is consistent with
1038 * original session.
1039 */
1040 if (!(s->s3->flags & TLS1_FLAGS_RECEIVED_EXTMS) !=
1041 !(s->session->flags & SSL_SESS_FLAG_EXTMS)) {
1042 *al = SSL_AD_HANDSHAKE_FAILURE;
7fe97c07 1043 SSLerr(SSL_F_FINAL_EMS, SSL_R_INCONSISTENT_EXTMS);
332eb390
MC
1044 return 0;
1045 }
1046 }
805a2e9e
MC
1047
1048 return 1;
1049}
1050
45615c5f
DSH
1051static int init_certificate_authorities(SSL *s, unsigned int context)
1052{
fa7c2637
DSH
1053 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
1054 s->s3->tmp.peer_ca_names = NULL;
45615c5f
DSH
1055 return 1;
1056}
1057
b186a592
MC
1058static EXT_RETURN tls_construct_certificate_authorities(SSL *s, WPACKET *pkt,
1059 unsigned int context,
1060 X509 *x,
1061 size_t chainidx,
1062 int *al)
45615c5f 1063{
9784ec04 1064 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
45615c5f
DSH
1065
1066 if (ca_sk == NULL || sk_X509_NAME_num(ca_sk) == 0)
b186a592 1067 return EXT_RETURN_NOT_SENT;
45615c5f
DSH
1068
1069 if (!WPACKET_put_bytes_u16(pkt, TLSEXT_TYPE_certificate_authorities)
1070 || !WPACKET_start_sub_packet_u16(pkt)
1071 || !construct_ca_names(s, pkt)
1072 || !WPACKET_close(pkt)) {
1073 SSLerr(SSL_F_TLS_CONSTRUCT_CERTIFICATE_AUTHORITIES,
1074 ERR_R_INTERNAL_ERROR);
b186a592 1075 return EXT_RETURN_FAIL;
45615c5f
DSH
1076 }
1077
b186a592 1078 return EXT_RETURN_SENT;
45615c5f
DSH
1079}
1080
1081static int tls_parse_certificate_authorities(SSL *s, PACKET *pkt,
1082 unsigned int context, X509 *x,
1083 size_t chainidx, int *al)
1084{
1085 if (!parse_ca_names(s, pkt, al))
1086 return 0;
1087 if (PACKET_remaining(pkt) != 0) {
1088 *al = SSL_AD_DECODE_ERROR;
1089 return 0;
1090 }
1091 return 1;
1092}
1093
805a2e9e 1094#ifndef OPENSSL_NO_SRTP
1266eefd 1095static int init_srtp(SSL *s, unsigned int context)
805a2e9e
MC
1096{
1097 if (s->server)
1098 s->srtp_profile = NULL;
1099
1100 return 1;
1101}
1102#endif
04904312
MC
1103
1104static int final_sig_algs(SSL *s, unsigned int context, int sent, int *al)
1105{
108d45df 1106 if (!sent && SSL_IS_TLS13(s) && !s->hit) {
04904312
MC
1107 *al = TLS13_AD_MISSING_EXTENSION;
1108 SSLerr(SSL_F_FINAL_SIG_ALGS, SSL_R_MISSING_SIGALGS_EXTENSION);
1109 return 0;
1110 }
1111
1112 return 1;
1113}
b2f7e8c0 1114
deb2d5e7 1115#ifndef OPENSSL_NO_EC
f4bbb37c
MC
1116static int final_key_share(SSL *s, unsigned int context, int sent, int *al)
1117{
1118 if (!SSL_IS_TLS13(s))
1119 return 1;
1120
07d447a6
MC
1121 /* Nothing to do for key_share in an HRR */
1122 if ((context & SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST) != 0)
1123 return 1;
1124
f4bbb37c
MC
1125 /*
1126 * If
aff9929b
MC
1127 * we are a client
1128 * AND
f4bbb37c
MC
1129 * we have no key_share
1130 * AND
1131 * (we are not resuming
1132 * OR the kex_mode doesn't allow non key_share resumes)
1133 * THEN
aff9929b 1134 * fail;
f4bbb37c 1135 */
aff9929b
MC
1136 if (!s->server
1137 && !sent
f4bbb37c
MC
1138 && (!s->hit
1139 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0)) {
7d061fce 1140 /* Nothing left we can do - just fail */
fb34a0f4 1141 *al = SSL_AD_MISSING_EXTENSION;
f4bbb37c
MC
1142 SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
1143 return 0;
1144 }
aff9929b
MC
1145 /*
1146 * If
1147 * we are a server
1148 * AND
1149 * we have no key_share
1150 * THEN
1151 * If
1152 * we didn't already send a HelloRetryRequest
1153 * AND
1154 * the client sent a key_share extension
1155 * AND
1156 * (we are not resuming
1157 * OR the kex_mode allows key_share resumes)
1158 * AND
1159 * a shared group exists
1160 * THEN
1161 * send a HelloRetryRequest
1162 * ELSE If
1163 * we are not resuming
1164 * OR
1165 * the kex_mode doesn't allow non key_share resumes
1166 * THEN
1167 * fail;
1168 */
1169 if (s->server && s->s3->peer_tmp == NULL) {
1170 /* No suitable share */
1171 if (s->hello_retry_request == 0 && sent
1172 && (!s->hit
1173 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE_DHE)
1174 != 0)) {
f48d826e
DSH
1175 const uint16_t *pgroups, *clntgroups;
1176 size_t num_groups, clnt_num_groups, i;
319a33d0 1177 unsigned int group_id = 0;
aff9929b 1178
2248dbeb 1179 /* Check if a shared group exists */
aff9929b
MC
1180
1181 /* Get the clients list of supported groups. */
f48d826e
DSH
1182 tls1_get_peer_groups(s, &clntgroups, &clnt_num_groups);
1183 tls1_get_supported_groups(s, &pgroups, &num_groups);
aff9929b
MC
1184
1185 /* Find the first group we allow that is also in client's list */
f48d826e
DSH
1186 for (i = 0; i < num_groups; i++) {
1187 group_id = pgroups[i];
aff9929b 1188
f48d826e 1189 if (check_in_list(s, group_id, clntgroups, clnt_num_groups, 1))
aff9929b
MC
1190 break;
1191 }
1192
f48d826e 1193 if (i < num_groups) {
aff9929b
MC
1194 /* A shared group exists so send a HelloRetryRequest */
1195 s->s3->group_id = group_id;
1196 s->hello_retry_request = 1;
1197 return 1;
1198 }
1199 }
1200 if (!s->hit
1201 || (s->ext.psk_kex_mode & TLSEXT_KEX_MODE_FLAG_KE) == 0) {
1202 /* Nothing left we can do - just fail */
b6fdc12d
MC
1203 if (!sent)
1204 *al = SSL_AD_MISSING_EXTENSION;
1205 else
1206 *al = SSL_AD_HANDSHAKE_FAILURE;
aff9929b
MC
1207 SSLerr(SSL_F_FINAL_KEY_SHARE, SSL_R_NO_SUITABLE_KEY_SHARE);
1208 return 0;
1209 }
1210 }
1211
1212 /* We have a key_share so don't send any more HelloRetryRequest messages */
1213 if (s->server)
1214 s->hello_retry_request = 0;
f4bbb37c
MC
1215
1216 /*
1217 * For a client side resumption with no key_share we need to generate
1218 * the handshake secret (otherwise this is done during key_share
1219 * processing).
1220 */
1221 if (!sent && !s->server && !tls13_generate_handshake_secret(s, NULL, 0)) {
1222 *al = SSL_AD_INTERNAL_ERROR;
1223 SSLerr(SSL_F_FINAL_KEY_SHARE, ERR_R_INTERNAL_ERROR);
1224 return 0;
1225 }
1226
1227 return 1;
1228}
deb2d5e7 1229#endif
f4bbb37c 1230
b2f7e8c0
MC
1231static int init_psk_kex_modes(SSL *s, unsigned int context)
1232{
1233 s->ext.psk_kex_mode = TLSEXT_KEX_MODE_FLAG_NONE;
b2f7e8c0
MC
1234 return 1;
1235}
1053a6e2
MC
1236
1237int tls_psk_do_binder(SSL *s, const EVP_MD *md, const unsigned char *msgstart,
1238 size_t binderoffset, const unsigned char *binderin,
3a7c56b2
MC
1239 unsigned char *binderout, SSL_SESSION *sess, int sign,
1240 int external)
1053a6e2
MC
1241{
1242 EVP_PKEY *mackey = NULL;
1243 EVP_MD_CTX *mctx = NULL;
1244 unsigned char hash[EVP_MAX_MD_SIZE], binderkey[EVP_MAX_MD_SIZE];
1245 unsigned char finishedkey[EVP_MAX_MD_SIZE], tmpbinder[EVP_MAX_MD_SIZE];
b81bd336
MC
1246 unsigned char tmppsk[EVP_MAX_MD_SIZE];
1247 unsigned char *early_secret, *psk;
17aa119e 1248 const char resumption_label[] = "res binder";
3a7c56b2 1249 const char external_label[] = "ext binder";
b81bd336 1250 const char nonce_label[] = "resumption";
3a7c56b2
MC
1251 const char *label;
1252 size_t bindersize, labelsize, hashsize = EVP_MD_size(md);
1053a6e2 1253 int ret = -1;
add8d0e9
MC
1254 int usepskfored = 0;
1255
1256 if (external
1257 && s->early_data_state == SSL_EARLY_DATA_CONNECTING
1258 && s->session->ext.max_early_data == 0
1259 && sess->ext.max_early_data > 0)
1260 usepskfored = 1;
1053a6e2 1261
3a7c56b2
MC
1262 if (external) {
1263 label = external_label;
1264 labelsize = sizeof(external_label) - 1;
1265 } else {
1266 label = resumption_label;
1267 labelsize = sizeof(resumption_label) - 1;
1268 }
1269
b81bd336
MC
1270 if (sess->master_key_length != hashsize) {
1271 SSLerr(SSL_F_TLS_PSK_DO_BINDER, SSL_R_BAD_PSK);
1272 goto err;
1273 }
1274
1275 if (external) {
1276 psk = sess->master_key;
1277 } else {
b81bd336
MC
1278 psk = tmppsk;
1279 if (!tls13_hkdf_expand(s, md, sess->master_key,
1280 (const unsigned char *)nonce_label,
1281 sizeof(nonce_label) - 1, sess->ext.tick_nonce,
1282 sess->ext.tick_nonce_len, psk, hashsize)) {
1283 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1284 goto err;
1285 }
1286 }
1287
9368f865
MC
1288 /*
1289 * Generate the early_secret. On the server side we've selected a PSK to
1290 * resume with (internal or external) so we always do this. On the client
add8d0e9
MC
1291 * side we do this for a non-external (i.e. resumption) PSK or external PSK
1292 * that will be used for early_data so that it is in place for sending early
1293 * data. For client side external PSK not being used for early_data we
9368f865
MC
1294 * generate it but store it away for later use.
1295 */
add8d0e9 1296 if (s->server || !external || usepskfored)
9368f865
MC
1297 early_secret = (unsigned char *)s->early_secret;
1298 else
1299 early_secret = (unsigned char *)sess->early_secret;
b81bd336 1300 if (!tls13_generate_secret(s, md, NULL, psk, hashsize, early_secret)) {
1053a6e2
MC
1301 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1302 goto err;
1303 }
1304
1305 /*
1306 * Create the handshake hash for the binder key...the messages so far are
1307 * empty!
1308 */
1309 mctx = EVP_MD_CTX_new();
1310 if (mctx == NULL
1311 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
1312 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1313 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1314 goto err;
1315 }
1316
1317 /* Generate the binder key */
9368f865 1318 if (!tls13_hkdf_expand(s, md, early_secret, (unsigned char *)label,
a19ae67d 1319 labelsize, hash, hashsize, binderkey, hashsize)) {
1053a6e2
MC
1320 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1321 goto err;
1322 }
1323
1324 /* Generate the finished key */
1325 if (!tls13_derive_finishedkey(s, md, binderkey, finishedkey, hashsize)) {
1326 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1327 goto err;
1328 }
1329
aff9929b
MC
1330 if (EVP_DigestInit_ex(mctx, md, NULL) <= 0) {
1331 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1332 goto err;
1333 }
1334
1053a6e2 1335 /*
aff9929b
MC
1336 * Get a hash of the ClientHello up to the start of the binders. If we are
1337 * following a HelloRetryRequest then this includes the hash of the first
1338 * ClientHello and the HelloRetryRequest itself.
1053a6e2 1339 */
aff9929b
MC
1340 if (s->hello_retry_request) {
1341 size_t hdatalen;
1342 void *hdata;
1343
1344 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
1345 if (hdatalen <= 0) {
1346 SSLerr(SSL_F_TLS_PSK_DO_BINDER, SSL_R_BAD_HANDSHAKE_LENGTH);
1347 goto err;
1348 }
1349
1350 /*
1351 * For servers the handshake buffer data will include the second
1352 * ClientHello - which we don't want - so we need to take that bit off.
1353 */
1354 if (s->server) {
77815a02
MC
1355 PACKET hashprefix, msg;
1356
1357 /* Find how many bytes are left after the first two messages */
1358 if (!PACKET_buf_init(&hashprefix, hdata, hdatalen)
1359 || !PACKET_forward(&hashprefix, 1)
1360 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)
1361 || !PACKET_forward(&hashprefix, 1)
1362 || !PACKET_get_length_prefixed_3(&hashprefix, &msg)) {
aff9929b
MC
1363 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1364 goto err;
1365 }
77815a02 1366 hdatalen -= PACKET_remaining(&hashprefix);
aff9929b
MC
1367 }
1368
1369 if (EVP_DigestUpdate(mctx, hdata, hdatalen) <= 0) {
1370 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1371 goto err;
1372 }
1373 }
1374
1375 if (EVP_DigestUpdate(mctx, msgstart, binderoffset) <= 0
1053a6e2
MC
1376 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
1377 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1378 goto err;
1379 }
1380
1381 mackey = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, finishedkey, hashsize);
1382 if (mackey == NULL) {
1383 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1384 goto err;
1385 }
1386
1387 if (!sign)
1388 binderout = tmpbinder;
1389
1390 bindersize = hashsize;
1391 if (EVP_DigestSignInit(mctx, NULL, md, NULL, mackey) <= 0
1392 || EVP_DigestSignUpdate(mctx, hash, hashsize) <= 0
1393 || EVP_DigestSignFinal(mctx, binderout, &bindersize) <= 0
1394 || bindersize != hashsize) {
1395 SSLerr(SSL_F_TLS_PSK_DO_BINDER, ERR_R_INTERNAL_ERROR);
1396 goto err;
1397 }
1398
1399 if (sign) {
1400 ret = 1;
1401 } else {
1402 /* HMAC keys can't do EVP_DigestVerify* - use CRYPTO_memcmp instead */
1403 ret = (CRYPTO_memcmp(binderin, binderout, hashsize) == 0);
1404 }
1405
1406 err:
1407 OPENSSL_cleanse(binderkey, sizeof(binderkey));
1408 OPENSSL_cleanse(finishedkey, sizeof(finishedkey));
1409 EVP_PKEY_free(mackey);
1410 EVP_MD_CTX_free(mctx);
1411
1412 return ret;
1413}
38df5a45
MC
1414
1415static int final_early_data(SSL *s, unsigned int context, int sent, int *al)
1416{
4be3a7c7
MC
1417 if (!sent)
1418 return 1;
1419
1420 if (!s->server) {
1421 if (context == SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
1422 && sent
1423 && !s->ext.early_data_ok) {
1424 /*
1425 * If we get here then the server accepted our early_data but we
1426 * later realised that it shouldn't have done (e.g. inconsistent
1427 * ALPN)
1428 */
1429 *al = SSL_AD_ILLEGAL_PARAMETER;
1430 return 0;
1431 }
1432
38df5a45 1433 return 1;
4be3a7c7 1434 }
38df5a45
MC
1435
1436 if (s->max_early_data == 0
1437 || !s->hit
1438 || s->session->ext.tick_identity != 0
1439 || s->early_data_state != SSL_EARLY_DATA_ACCEPTING
1440 || !s->ext.early_data_ok
630369d9 1441 || s->hello_retry_request) {
38df5a45
MC
1442 s->ext.early_data = SSL_EARLY_DATA_REJECTED;
1443 } else {
1444 s->ext.early_data = SSL_EARLY_DATA_ACCEPTED;
1445
1446 if (!tls13_change_cipher_state(s,
1447 SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_SERVER_READ)) {
1448 *al = SSL_AD_INTERNAL_ERROR;
1449 return 0;
1450 }
1451 }
1452
1453 return 1;
1454}