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