1 1.1 christos /* 2 1.1 christos * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. 3 1.1 christos * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved 4 1.1 christos * Copyright 2005 Nokia. All rights reserved. 5 1.1 christos * 6 1.1 christos * Licensed under the Apache License 2.0 (the "License"). You may not use 7 1.1 christos * this file except in compliance with the License. You can obtain a copy 8 1.1 christos * in the file LICENSE in the source distribution or at 9 1.1 christos * https://www.openssl.org/source/license.html 10 1.1 christos */ 11 1.1 christos 12 1.1 christos #include "internal/e_os.h" 13 1.1 christos 14 1.1 christos #include <stdio.h> 15 1.1 christos #include "../ssl_local.h" 16 1.1 christos #include "statem_local.h" 17 1.1 christos #include "internal/constant_time.h" 18 1.1 christos #include "internal/cryptlib.h" 19 1.1 christos #include "internal/ssl_unwrap.h" 20 1.1 christos #include <openssl/buffer.h> 21 1.1 christos #include <openssl/rand.h> 22 1.1 christos #include <openssl/objects.h> 23 1.1 christos #include <openssl/evp.h> 24 1.1 christos #include <openssl/x509.h> 25 1.1 christos #include <openssl/dh.h> 26 1.1 christos #include <openssl/rsa.h> 27 1.1 christos #include <openssl/bn.h> 28 1.1 christos #include <openssl/md5.h> 29 1.1 christos #include <openssl/trace.h> 30 1.1 christos #include <openssl/core_names.h> 31 1.1 christos #include <openssl/asn1t.h> 32 1.1 christos #include <openssl/comp.h> 33 1.1 christos #include "internal/comp.h" 34 1.1 christos 35 1.1.1.2 christos #define TICKET_NONCE_SIZE 8 36 1.1 christos 37 1.1 christos typedef struct { 38 1.1.1.2 christos ASN1_TYPE *kxBlob; 39 1.1.1.2 christos ASN1_TYPE *opaqueBlob; 40 1.1 christos } GOST_KX_MESSAGE; 41 1.1 christos 42 1.1 christos DECLARE_ASN1_FUNCTIONS(GOST_KX_MESSAGE) 43 1.1 christos 44 1.1 christos ASN1_SEQUENCE(GOST_KX_MESSAGE) = { 45 1.1.1.2 christos ASN1_SIMPLE(GOST_KX_MESSAGE, kxBlob, ASN1_ANY), 46 1.1.1.2 christos ASN1_OPT(GOST_KX_MESSAGE, opaqueBlob, ASN1_ANY), 47 1.1 christos } ASN1_SEQUENCE_END(GOST_KX_MESSAGE) 48 1.1 christos 49 1.1 christos IMPLEMENT_ASN1_FUNCTIONS(GOST_KX_MESSAGE) 50 1.1 christos 51 1.1 christos static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, 52 1.1.1.2 christos WPACKET *pkt); 53 1.1 christos 54 1.1 christos static ossl_inline int received_client_cert(const SSL_CONNECTION *sc) 55 1.1 christos { 56 1.1 christos return sc->session->peer_rpk != NULL || sc->session->peer != NULL; 57 1.1 christos } 58 1.1 christos 59 1.1 christos /* 60 1.1 christos * ossl_statem_server13_read_transition() encapsulates the logic for the allowed 61 1.1 christos * handshake state transitions when a TLSv1.3 server is reading messages from 62 1.1 christos * the client. The message type that the client has sent is provided in |mt|. 63 1.1 christos * The current state is in |s->statem.hand_state|. 64 1.1 christos * 65 1.1 christos * Return values are 1 for success (transition allowed) and 0 on error 66 1.1 christos * (transition not allowed) 67 1.1 christos */ 68 1.1 christos static int ossl_statem_server13_read_transition(SSL_CONNECTION *s, int mt) 69 1.1 christos { 70 1.1 christos OSSL_STATEM *st = &s->statem; 71 1.1 christos 72 1.1 christos /* 73 1.1 christos * Note: There is no case for TLS_ST_BEFORE because at that stage we have 74 1.1 christos * not negotiated TLSv1.3 yet, so that case is handled by 75 1.1 christos * ossl_statem_server_read_transition() 76 1.1 christos */ 77 1.1 christos switch (st->hand_state) { 78 1.1 christos default: 79 1.1 christos break; 80 1.1 christos 81 1.1 christos case TLS_ST_EARLY_DATA: 82 1.1 christos if (s->hello_retry_request == SSL_HRR_PENDING) { 83 1.1 christos if (mt == SSL3_MT_CLIENT_HELLO) { 84 1.1 christos st->hand_state = TLS_ST_SR_CLNT_HELLO; 85 1.1 christos return 1; 86 1.1 christos } 87 1.1 christos break; 88 1.1 christos } else if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED 89 1.1.1.2 christos && !SSL_NO_EOED(s)) { 90 1.1 christos if (mt == SSL3_MT_END_OF_EARLY_DATA) { 91 1.1 christos st->hand_state = TLS_ST_SR_END_OF_EARLY_DATA; 92 1.1 christos return 1; 93 1.1 christos } 94 1.1 christos break; 95 1.1 christos } 96 1.1 christos /* Fall through */ 97 1.1 christos 98 1.1 christos case TLS_ST_SR_END_OF_EARLY_DATA: 99 1.1 christos case TLS_ST_SW_FINISHED: 100 1.1 christos if (s->s3.tmp.cert_request) { 101 1.1 christos if (mt == SSL3_MT_CERTIFICATE) { 102 1.1 christos st->hand_state = TLS_ST_SR_CERT; 103 1.1 christos return 1; 104 1.1 christos } 105 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 106 1.1 christos if (mt == SSL3_MT_COMPRESSED_CERTIFICATE 107 1.1.1.2 christos && s->ext.compress_certificate_sent) { 108 1.1 christos st->hand_state = TLS_ST_SR_COMP_CERT; 109 1.1 christos return 1; 110 1.1 christos } 111 1.1 christos #endif 112 1.1 christos } else { 113 1.1 christos if (mt == SSL3_MT_FINISHED) { 114 1.1 christos st->hand_state = TLS_ST_SR_FINISHED; 115 1.1 christos return 1; 116 1.1 christos } 117 1.1 christos } 118 1.1 christos break; 119 1.1 christos 120 1.1 christos case TLS_ST_SR_COMP_CERT: 121 1.1 christos case TLS_ST_SR_CERT: 122 1.1 christos if (!received_client_cert(s)) { 123 1.1 christos if (mt == SSL3_MT_FINISHED) { 124 1.1 christos st->hand_state = TLS_ST_SR_FINISHED; 125 1.1 christos return 1; 126 1.1 christos } 127 1.1 christos } else { 128 1.1 christos if (mt == SSL3_MT_CERTIFICATE_VERIFY) { 129 1.1 christos st->hand_state = TLS_ST_SR_CERT_VRFY; 130 1.1 christos return 1; 131 1.1 christos } 132 1.1 christos } 133 1.1 christos break; 134 1.1 christos 135 1.1 christos case TLS_ST_SR_CERT_VRFY: 136 1.1 christos if (mt == SSL3_MT_FINISHED) { 137 1.1 christos st->hand_state = TLS_ST_SR_FINISHED; 138 1.1 christos return 1; 139 1.1 christos } 140 1.1 christos break; 141 1.1 christos 142 1.1 christos case TLS_ST_OK: 143 1.1 christos /* 144 1.1 christos * Its never ok to start processing handshake messages in the middle of 145 1.1 christos * early data (i.e. before we've received the end of early data alert) 146 1.1 christos */ 147 1.1 christos if (s->early_data_state == SSL_EARLY_DATA_READING) 148 1.1 christos break; 149 1.1 christos 150 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUESTED) { 151 1.1 christos if (mt == SSL3_MT_CERTIFICATE) { 152 1.1 christos st->hand_state = TLS_ST_SR_CERT; 153 1.1 christos return 1; 154 1.1 christos } 155 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 156 1.1 christos if (mt == SSL3_MT_COMPRESSED_CERTIFICATE 157 1.1.1.2 christos && s->ext.compress_certificate_sent) { 158 1.1 christos st->hand_state = TLS_ST_SR_COMP_CERT; 159 1.1 christos return 1; 160 1.1 christos } 161 1.1 christos #endif 162 1.1 christos } 163 1.1 christos 164 1.1 christos if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) { 165 1.1 christos st->hand_state = TLS_ST_SR_KEY_UPDATE; 166 1.1 christos return 1; 167 1.1 christos } 168 1.1 christos break; 169 1.1 christos } 170 1.1 christos 171 1.1 christos /* No valid transition found */ 172 1.1 christos return 0; 173 1.1 christos } 174 1.1 christos 175 1.1 christos /* 176 1.1 christos * ossl_statem_server_read_transition() encapsulates the logic for the allowed 177 1.1 christos * handshake state transitions when the server is reading messages from the 178 1.1 christos * client. The message type that the client has sent is provided in |mt|. The 179 1.1 christos * current state is in |s->statem.hand_state|. 180 1.1 christos * 181 1.1 christos * Return values are 1 for success (transition allowed) and 0 on error 182 1.1 christos * (transition not allowed) 183 1.1 christos */ 184 1.1 christos int ossl_statem_server_read_transition(SSL_CONNECTION *s, int mt) 185 1.1 christos { 186 1.1 christos OSSL_STATEM *st = &s->statem; 187 1.1 christos 188 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 189 1.1 christos if (!ossl_statem_server13_read_transition(s, mt)) 190 1.1 christos goto err; 191 1.1 christos return 1; 192 1.1 christos } 193 1.1 christos 194 1.1 christos switch (st->hand_state) { 195 1.1 christos default: 196 1.1 christos break; 197 1.1 christos 198 1.1 christos case TLS_ST_BEFORE: 199 1.1 christos case TLS_ST_OK: 200 1.1 christos case DTLS_ST_SW_HELLO_VERIFY_REQUEST: 201 1.1 christos if (mt == SSL3_MT_CLIENT_HELLO) { 202 1.1 christos st->hand_state = TLS_ST_SR_CLNT_HELLO; 203 1.1 christos return 1; 204 1.1 christos } 205 1.1 christos break; 206 1.1 christos 207 1.1 christos case TLS_ST_SW_SRVR_DONE: 208 1.1 christos /* 209 1.1 christos * If we get a CKE message after a ServerDone then either 210 1.1 christos * 1) We didn't request a Certificate 211 1.1 christos * OR 212 1.1 christos * 2) If we did request one then 213 1.1 christos * a) We allow no Certificate to be returned 214 1.1 christos * AND 215 1.1 christos * b) We are running SSL3 (in TLS1.0+ the client must return a 0 216 1.1 christos * list if we requested a certificate) 217 1.1 christos */ 218 1.1 christos if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { 219 1.1 christos if (s->s3.tmp.cert_request) { 220 1.1 christos if (s->version == SSL3_VERSION) { 221 1.1 christos if ((s->verify_mode & SSL_VERIFY_PEER) 222 1.1 christos && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 223 1.1 christos /* 224 1.1 christos * This isn't an unexpected message as such - we're just 225 1.1 christos * not going to accept it because we require a client 226 1.1 christos * cert. 227 1.1 christos */ 228 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 229 1.1.1.2 christos SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 230 1.1 christos return 0; 231 1.1 christos } 232 1.1 christos st->hand_state = TLS_ST_SR_KEY_EXCH; 233 1.1 christos return 1; 234 1.1 christos } 235 1.1 christos } else { 236 1.1 christos st->hand_state = TLS_ST_SR_KEY_EXCH; 237 1.1 christos return 1; 238 1.1 christos } 239 1.1 christos } else if (s->s3.tmp.cert_request) { 240 1.1 christos if (mt == SSL3_MT_CERTIFICATE) { 241 1.1 christos st->hand_state = TLS_ST_SR_CERT; 242 1.1 christos return 1; 243 1.1 christos } 244 1.1 christos } 245 1.1 christos break; 246 1.1 christos 247 1.1 christos case TLS_ST_SR_CERT: 248 1.1 christos if (mt == SSL3_MT_CLIENT_KEY_EXCHANGE) { 249 1.1 christos st->hand_state = TLS_ST_SR_KEY_EXCH; 250 1.1 christos return 1; 251 1.1 christos } 252 1.1 christos break; 253 1.1 christos 254 1.1 christos case TLS_ST_SR_KEY_EXCH: 255 1.1 christos /* 256 1.1 christos * We should only process a CertificateVerify message if we have 257 1.1 christos * received a Certificate from the client. If so then |s->session->peer| 258 1.1 christos * will be non NULL. In some instances a CertificateVerify message is 259 1.1 christos * not required even if the peer has sent a Certificate (e.g. such as in 260 1.1 christos * the case of static DH). In that case |st->no_cert_verify| should be 261 1.1 christos * set. 262 1.1 christos */ 263 1.1 christos if (!received_client_cert(s) || st->no_cert_verify) { 264 1.1 christos if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { 265 1.1 christos /* 266 1.1 christos * For the ECDH ciphersuites when the client sends its ECDH 267 1.1 christos * pub key in a certificate, the CertificateVerify message is 268 1.1 christos * not sent. Also for GOST ciphersuites when the client uses 269 1.1 christos * its key from the certificate for key exchange. 270 1.1 christos */ 271 1.1 christos st->hand_state = TLS_ST_SR_CHANGE; 272 1.1 christos return 1; 273 1.1 christos } 274 1.1 christos } else { 275 1.1 christos if (mt == SSL3_MT_CERTIFICATE_VERIFY) { 276 1.1 christos st->hand_state = TLS_ST_SR_CERT_VRFY; 277 1.1 christos return 1; 278 1.1 christos } 279 1.1 christos } 280 1.1 christos break; 281 1.1 christos 282 1.1 christos case TLS_ST_SR_CERT_VRFY: 283 1.1 christos if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { 284 1.1 christos st->hand_state = TLS_ST_SR_CHANGE; 285 1.1 christos return 1; 286 1.1 christos } 287 1.1 christos break; 288 1.1 christos 289 1.1 christos case TLS_ST_SR_CHANGE: 290 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 291 1.1 christos if (s->s3.npn_seen) { 292 1.1 christos if (mt == SSL3_MT_NEXT_PROTO) { 293 1.1 christos st->hand_state = TLS_ST_SR_NEXT_PROTO; 294 1.1 christos return 1; 295 1.1 christos } 296 1.1 christos } else { 297 1.1 christos #endif 298 1.1 christos if (mt == SSL3_MT_FINISHED) { 299 1.1 christos st->hand_state = TLS_ST_SR_FINISHED; 300 1.1 christos return 1; 301 1.1 christos } 302 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 303 1.1 christos } 304 1.1 christos #endif 305 1.1 christos break; 306 1.1 christos 307 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 308 1.1 christos case TLS_ST_SR_NEXT_PROTO: 309 1.1 christos if (mt == SSL3_MT_FINISHED) { 310 1.1 christos st->hand_state = TLS_ST_SR_FINISHED; 311 1.1 christos return 1; 312 1.1 christos } 313 1.1 christos break; 314 1.1 christos #endif 315 1.1 christos 316 1.1 christos case TLS_ST_SW_FINISHED: 317 1.1 christos if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { 318 1.1 christos st->hand_state = TLS_ST_SR_CHANGE; 319 1.1 christos return 1; 320 1.1 christos } 321 1.1 christos break; 322 1.1 christos } 323 1.1 christos 324 1.1.1.2 christos err: 325 1.1 christos /* No valid transition found */ 326 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { 327 1.1 christos BIO *rbio; 328 1.1 christos 329 1.1 christos /* 330 1.1 christos * CCS messages don't have a message sequence number so this is probably 331 1.1 christos * because of an out-of-order CCS. We'll just drop it. 332 1.1 christos */ 333 1.1 christos s->init_num = 0; 334 1.1 christos s->rwstate = SSL_READING; 335 1.1 christos rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s)); 336 1.1 christos BIO_clear_retry_flags(rbio); 337 1.1 christos BIO_set_retry_read(rbio); 338 1.1 christos return 0; 339 1.1 christos } 340 1.1 christos SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); 341 1.1 christos return 0; 342 1.1 christos } 343 1.1 christos 344 1.1 christos /* 345 1.1 christos * Should we send a ServerKeyExchange message? 346 1.1 christos * 347 1.1 christos * Valid return values are: 348 1.1 christos * 1: Yes 349 1.1 christos * 0: No 350 1.1 christos */ 351 1.1 christos static int send_server_key_exchange(SSL_CONNECTION *s) 352 1.1 christos { 353 1.1 christos unsigned long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; 354 1.1 christos 355 1.1 christos /* 356 1.1 christos * only send a ServerKeyExchange if DH or fortezza but we have a 357 1.1 christos * sign only certificate PSK: may send PSK identity hints For 358 1.1 christos * ECC ciphersuites, we send a serverKeyExchange message only if 359 1.1 christos * the cipher suite is either ECDH-anon or ECDHE. In other cases, 360 1.1 christos * the server certificate contains the server's public key for 361 1.1 christos * key exchange. 362 1.1 christos */ 363 1.1 christos if (alg_k & (SSL_kDHE | SSL_kECDHE) 364 1.1.1.2 christos /* 365 1.1.1.2 christos * PSK: send ServerKeyExchange if PSK identity hint if 366 1.1.1.2 christos * provided 367 1.1.1.2 christos */ 368 1.1 christos #ifndef OPENSSL_NO_PSK 369 1.1 christos /* Only send SKE if we have identity hint for plain PSK */ 370 1.1 christos || ((alg_k & (SSL_kPSK | SSL_kRSAPSK)) 371 1.1 christos && s->cert->psk_identity_hint) 372 1.1 christos /* For other PSK always send SKE */ 373 1.1 christos || (alg_k & (SSL_PSK & (SSL_kDHEPSK | SSL_kECDHEPSK))) 374 1.1 christos #endif 375 1.1 christos #ifndef OPENSSL_NO_SRP 376 1.1 christos /* SRP: send ServerKeyExchange */ 377 1.1 christos || (alg_k & SSL_kSRP) 378 1.1 christos #endif 379 1.1.1.2 christos ) { 380 1.1 christos return 1; 381 1.1 christos } 382 1.1 christos 383 1.1 christos return 0; 384 1.1 christos } 385 1.1 christos 386 1.1 christos /* 387 1.1 christos * Used to determine if we should send a CompressedCertificate message 388 1.1 christos * 389 1.1 christos * Returns the algorithm to use, TLSEXT_comp_cert_none means no compression 390 1.1 christos */ 391 1.1 christos static int get_compressed_certificate_alg(SSL_CONNECTION *sc) 392 1.1 christos { 393 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 394 1.1 christos int *alg = sc->ext.compress_certificate_from_peer; 395 1.1 christos 396 1.1 christos if (sc->s3.tmp.cert == NULL) 397 1.1 christos return TLSEXT_comp_cert_none; 398 1.1 christos 399 1.1 christos for (; *alg != TLSEXT_comp_cert_none; alg++) { 400 1.1 christos if (sc->s3.tmp.cert->comp_cert[*alg] != NULL) 401 1.1 christos return *alg; 402 1.1 christos } 403 1.1 christos #endif 404 1.1 christos return TLSEXT_comp_cert_none; 405 1.1 christos } 406 1.1 christos 407 1.1 christos /* 408 1.1 christos * Should we send a CertificateRequest message? 409 1.1 christos * 410 1.1 christos * Valid return values are: 411 1.1 christos * 1: Yes 412 1.1 christos * 0: No 413 1.1 christos */ 414 1.1 christos int send_certificate_request(SSL_CONNECTION *s) 415 1.1 christos { 416 1.1 christos if ( 417 1.1.1.2 christos /* don't request cert unless asked for it: */ 418 1.1.1.2 christos s->verify_mode & SSL_VERIFY_PEER 419 1.1.1.2 christos /* 420 1.1.1.2 christos * don't request if post-handshake-only unless doing 421 1.1.1.2 christos * post-handshake in TLSv1.3: 422 1.1.1.2 christos */ 423 1.1.1.2 christos && (!SSL_CONNECTION_IS_TLS13(s) 424 1.1.1.2 christos || !(s->verify_mode & SSL_VERIFY_POST_HANDSHAKE) 425 1.1.1.2 christos || s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) 426 1.1.1.2 christos /* 427 1.1.1.2 christos * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert 428 1.1.1.2 christos * a second time: 429 1.1.1.2 christos */ 430 1.1.1.2 christos && (s->certreqs_sent < 1 || !(s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) 431 1.1.1.2 christos /* 432 1.1.1.2 christos * never request cert in anonymous ciphersuites (see 433 1.1.1.2 christos * section "Certificate request" in SSL 3 drafts and in 434 1.1.1.2 christos * RFC 2246): 435 1.1.1.2 christos */ 436 1.1.1.2 christos && (!(s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL) 437 1.1.1.2 christos /* 438 1.1.1.2 christos * ... except when the application insists on 439 1.1.1.2 christos * verification (against the specs, but statem_clnt.c accepts 440 1.1.1.2 christos * this for SSL 3) 441 1.1.1.2 christos */ 442 1.1.1.2 christos || (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) 443 1.1.1.2 christos /* don't request certificate for SRP auth */ 444 1.1.1.2 christos && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aSRP) 445 1.1.1.2 christos /* 446 1.1.1.2 christos * With normal PSK Certificates and Certificate Requests 447 1.1.1.2 christos * are omitted 448 1.1.1.2 christos */ 449 1.1.1.2 christos && !(s->s3.tmp.new_cipher->algorithm_auth & SSL_aPSK)) { 450 1.1 christos return 1; 451 1.1 christos } 452 1.1 christos 453 1.1 christos return 0; 454 1.1 christos } 455 1.1 christos 456 1.1 christos static int do_compressed_cert(SSL_CONNECTION *sc) 457 1.1 christos { 458 1.1 christos /* If we negotiated RPK, we won't attempt to compress it */ 459 1.1 christos return sc->ext.server_cert_type == TLSEXT_cert_type_x509 460 1.1 christos && get_compressed_certificate_alg(sc) != TLSEXT_comp_cert_none; 461 1.1 christos } 462 1.1 christos 463 1.1 christos /* 464 1.1 christos * ossl_statem_server13_write_transition() works out what handshake state to 465 1.1 christos * move to next when a TLSv1.3 server is writing messages to be sent to the 466 1.1 christos * client. 467 1.1 christos */ 468 1.1 christos static WRITE_TRAN ossl_statem_server13_write_transition(SSL_CONNECTION *s) 469 1.1 christos { 470 1.1 christos OSSL_STATEM *st = &s->statem; 471 1.1 christos 472 1.1 christos /* 473 1.1 christos * No case for TLS_ST_BEFORE, because at that stage we have not negotiated 474 1.1 christos * TLSv1.3 yet, so that is handled by ossl_statem_server_write_transition() 475 1.1 christos */ 476 1.1 christos 477 1.1 christos switch (st->hand_state) { 478 1.1 christos default: 479 1.1 christos /* Shouldn't happen */ 480 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 481 1.1 christos return WRITE_TRAN_ERROR; 482 1.1 christos 483 1.1 christos case TLS_ST_OK: 484 1.1 christos if (s->key_update != SSL_KEY_UPDATE_NONE) { 485 1.1 christos st->hand_state = TLS_ST_SW_KEY_UPDATE; 486 1.1 christos return WRITE_TRAN_CONTINUE; 487 1.1 christos } 488 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { 489 1.1 christos st->hand_state = TLS_ST_SW_CERT_REQ; 490 1.1 christos return WRITE_TRAN_CONTINUE; 491 1.1 christos } 492 1.1 christos if (s->ext.extra_tickets_expected > 0) { 493 1.1 christos st->hand_state = TLS_ST_SW_SESSION_TICKET; 494 1.1 christos return WRITE_TRAN_CONTINUE; 495 1.1 christos } 496 1.1 christos /* Try to read from the client instead */ 497 1.1 christos return WRITE_TRAN_FINISHED; 498 1.1 christos 499 1.1 christos case TLS_ST_SR_CLNT_HELLO: 500 1.1 christos st->hand_state = TLS_ST_SW_SRVR_HELLO; 501 1.1 christos return WRITE_TRAN_CONTINUE; 502 1.1 christos 503 1.1 christos case TLS_ST_SW_SRVR_HELLO: 504 1.1 christos if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 505 1.1.1.2 christos && s->hello_retry_request != SSL_HRR_COMPLETE) 506 1.1 christos st->hand_state = TLS_ST_SW_CHANGE; 507 1.1 christos else if (s->hello_retry_request == SSL_HRR_PENDING) 508 1.1 christos st->hand_state = TLS_ST_EARLY_DATA; 509 1.1 christos else 510 1.1 christos st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; 511 1.1 christos return WRITE_TRAN_CONTINUE; 512 1.1 christos 513 1.1 christos case TLS_ST_SW_CHANGE: 514 1.1 christos if (s->hello_retry_request == SSL_HRR_PENDING) 515 1.1 christos st->hand_state = TLS_ST_EARLY_DATA; 516 1.1 christos else 517 1.1 christos st->hand_state = TLS_ST_SW_ENCRYPTED_EXTENSIONS; 518 1.1 christos return WRITE_TRAN_CONTINUE; 519 1.1 christos 520 1.1 christos case TLS_ST_SW_ENCRYPTED_EXTENSIONS: 521 1.1 christos if (s->hit) 522 1.1 christos st->hand_state = TLS_ST_SW_FINISHED; 523 1.1 christos else if (send_certificate_request(s)) 524 1.1 christos st->hand_state = TLS_ST_SW_CERT_REQ; 525 1.1 christos else if (do_compressed_cert(s)) 526 1.1 christos st->hand_state = TLS_ST_SW_COMP_CERT; 527 1.1 christos else 528 1.1 christos st->hand_state = TLS_ST_SW_CERT; 529 1.1 christos 530 1.1 christos return WRITE_TRAN_CONTINUE; 531 1.1 christos 532 1.1 christos case TLS_ST_SW_CERT_REQ: 533 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { 534 1.1 christos s->post_handshake_auth = SSL_PHA_REQUESTED; 535 1.1 christos st->hand_state = TLS_ST_OK; 536 1.1 christos } else if (do_compressed_cert(s)) { 537 1.1 christos st->hand_state = TLS_ST_SW_COMP_CERT; 538 1.1 christos } else { 539 1.1 christos st->hand_state = TLS_ST_SW_CERT; 540 1.1 christos } 541 1.1 christos return WRITE_TRAN_CONTINUE; 542 1.1 christos 543 1.1 christos case TLS_ST_SW_COMP_CERT: 544 1.1 christos case TLS_ST_SW_CERT: 545 1.1 christos st->hand_state = TLS_ST_SW_CERT_VRFY; 546 1.1 christos return WRITE_TRAN_CONTINUE; 547 1.1 christos 548 1.1 christos case TLS_ST_SW_CERT_VRFY: 549 1.1 christos st->hand_state = TLS_ST_SW_FINISHED; 550 1.1 christos return WRITE_TRAN_CONTINUE; 551 1.1 christos 552 1.1 christos case TLS_ST_SW_FINISHED: 553 1.1 christos st->hand_state = TLS_ST_EARLY_DATA; 554 1.1 christos s->ts_msg_write = ossl_time_now(); 555 1.1 christos return WRITE_TRAN_CONTINUE; 556 1.1 christos 557 1.1 christos case TLS_ST_EARLY_DATA: 558 1.1 christos return WRITE_TRAN_FINISHED; 559 1.1 christos 560 1.1 christos case TLS_ST_SR_FINISHED: 561 1.1 christos s->ts_msg_read = ossl_time_now(); 562 1.1 christos /* 563 1.1 christos * Technically we have finished the handshake at this point, but we're 564 1.1 christos * going to remain "in_init" for now and write out any session tickets 565 1.1 christos * immediately. 566 1.1 christos */ 567 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUESTED) { 568 1.1 christos s->post_handshake_auth = SSL_PHA_EXT_RECEIVED; 569 1.1 christos } else if (!s->ext.ticket_expected) { 570 1.1 christos /* 571 1.1 christos * If we're not going to renew the ticket then we just finish the 572 1.1 christos * handshake at this point. 573 1.1 christos */ 574 1.1 christos st->hand_state = TLS_ST_OK; 575 1.1 christos return WRITE_TRAN_CONTINUE; 576 1.1 christos } 577 1.1 christos if (s->num_tickets > s->sent_tickets) 578 1.1 christos st->hand_state = TLS_ST_SW_SESSION_TICKET; 579 1.1 christos else 580 1.1 christos st->hand_state = TLS_ST_OK; 581 1.1 christos return WRITE_TRAN_CONTINUE; 582 1.1 christos 583 1.1 christos case TLS_ST_SR_KEY_UPDATE: 584 1.1 christos case TLS_ST_SW_KEY_UPDATE: 585 1.1 christos st->hand_state = TLS_ST_OK; 586 1.1 christos return WRITE_TRAN_CONTINUE; 587 1.1 christos 588 1.1 christos case TLS_ST_SW_SESSION_TICKET: 589 1.1 christos /* In a resumption we only ever send a maximum of one new ticket. 590 1.1 christos * Following an initial handshake we send the number of tickets we have 591 1.1 christos * been configured for. 592 1.1 christos */ 593 1.1 christos if (!SSL_IS_FIRST_HANDSHAKE(s) && s->ext.extra_tickets_expected > 0) { 594 1.1 christos return WRITE_TRAN_CONTINUE; 595 1.1 christos } else if (s->hit || s->num_tickets <= s->sent_tickets) { 596 1.1 christos /* We've written enough tickets out. */ 597 1.1 christos st->hand_state = TLS_ST_OK; 598 1.1 christos } 599 1.1 christos return WRITE_TRAN_CONTINUE; 600 1.1 christos } 601 1.1 christos } 602 1.1 christos 603 1.1 christos /* 604 1.1 christos * ossl_statem_server_write_transition() works out what handshake state to move 605 1.1 christos * to next when the server is writing messages to be sent to the client. 606 1.1 christos */ 607 1.1 christos WRITE_TRAN ossl_statem_server_write_transition(SSL_CONNECTION *s) 608 1.1 christos { 609 1.1 christos OSSL_STATEM *st = &s->statem; 610 1.1 christos 611 1.1 christos /* 612 1.1 christos * Note that before the ClientHello we don't know what version we are going 613 1.1 christos * to negotiate yet, so we don't take this branch until later 614 1.1 christos */ 615 1.1 christos 616 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) 617 1.1 christos return ossl_statem_server13_write_transition(s); 618 1.1 christos 619 1.1 christos switch (st->hand_state) { 620 1.1 christos default: 621 1.1 christos /* Shouldn't happen */ 622 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 623 1.1 christos return WRITE_TRAN_ERROR; 624 1.1 christos 625 1.1 christos case TLS_ST_OK: 626 1.1 christos if (st->request_state == TLS_ST_SW_HELLO_REQ) { 627 1.1 christos /* We must be trying to renegotiate */ 628 1.1 christos st->hand_state = TLS_ST_SW_HELLO_REQ; 629 1.1 christos st->request_state = TLS_ST_BEFORE; 630 1.1 christos return WRITE_TRAN_CONTINUE; 631 1.1 christos } 632 1.1 christos /* Must be an incoming ClientHello */ 633 1.1 christos if (!tls_setup_handshake(s)) { 634 1.1 christos /* SSLfatal() already called */ 635 1.1 christos return WRITE_TRAN_ERROR; 636 1.1 christos } 637 1.1 christos /* Fall through */ 638 1.1 christos 639 1.1 christos case TLS_ST_BEFORE: 640 1.1 christos /* Just go straight to trying to read from the client */ 641 1.1 christos return WRITE_TRAN_FINISHED; 642 1.1 christos 643 1.1 christos case TLS_ST_SW_HELLO_REQ: 644 1.1 christos st->hand_state = TLS_ST_OK; 645 1.1 christos return WRITE_TRAN_CONTINUE; 646 1.1 christos 647 1.1 christos case TLS_ST_SR_CLNT_HELLO: 648 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && !s->d1->cookie_verified 649 1.1 christos && (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE)) { 650 1.1 christos st->hand_state = DTLS_ST_SW_HELLO_VERIFY_REQUEST; 651 1.1 christos } else if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { 652 1.1 christos /* We must have rejected the renegotiation */ 653 1.1 christos st->hand_state = TLS_ST_OK; 654 1.1 christos return WRITE_TRAN_CONTINUE; 655 1.1 christos } else { 656 1.1 christos st->hand_state = TLS_ST_SW_SRVR_HELLO; 657 1.1 christos } 658 1.1 christos return WRITE_TRAN_CONTINUE; 659 1.1 christos 660 1.1 christos case DTLS_ST_SW_HELLO_VERIFY_REQUEST: 661 1.1 christos return WRITE_TRAN_FINISHED; 662 1.1 christos 663 1.1 christos case TLS_ST_SW_SRVR_HELLO: 664 1.1 christos if (s->hit) { 665 1.1 christos if (s->ext.ticket_expected) 666 1.1 christos st->hand_state = TLS_ST_SW_SESSION_TICKET; 667 1.1 christos else 668 1.1 christos st->hand_state = TLS_ST_SW_CHANGE; 669 1.1 christos } else { 670 1.1 christos /* Check if it is anon DH or anon ECDH, */ 671 1.1 christos /* normal PSK or SRP */ 672 1.1.1.2 christos if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { 673 1.1 christos st->hand_state = TLS_ST_SW_CERT; 674 1.1 christos } else if (send_server_key_exchange(s)) { 675 1.1 christos st->hand_state = TLS_ST_SW_KEY_EXCH; 676 1.1 christos } else if (send_certificate_request(s)) { 677 1.1 christos st->hand_state = TLS_ST_SW_CERT_REQ; 678 1.1 christos } else { 679 1.1 christos st->hand_state = TLS_ST_SW_SRVR_DONE; 680 1.1 christos } 681 1.1 christos } 682 1.1 christos return WRITE_TRAN_CONTINUE; 683 1.1 christos 684 1.1 christos case TLS_ST_SW_CERT: 685 1.1 christos if (s->ext.status_expected) { 686 1.1 christos st->hand_state = TLS_ST_SW_CERT_STATUS; 687 1.1 christos return WRITE_TRAN_CONTINUE; 688 1.1 christos } 689 1.1 christos /* Fall through */ 690 1.1 christos 691 1.1 christos case TLS_ST_SW_CERT_STATUS: 692 1.1 christos if (send_server_key_exchange(s)) { 693 1.1 christos st->hand_state = TLS_ST_SW_KEY_EXCH; 694 1.1 christos return WRITE_TRAN_CONTINUE; 695 1.1 christos } 696 1.1 christos /* Fall through */ 697 1.1 christos 698 1.1 christos case TLS_ST_SW_KEY_EXCH: 699 1.1 christos if (send_certificate_request(s)) { 700 1.1 christos st->hand_state = TLS_ST_SW_CERT_REQ; 701 1.1 christos return WRITE_TRAN_CONTINUE; 702 1.1 christos } 703 1.1 christos /* Fall through */ 704 1.1 christos 705 1.1 christos case TLS_ST_SW_CERT_REQ: 706 1.1 christos st->hand_state = TLS_ST_SW_SRVR_DONE; 707 1.1 christos return WRITE_TRAN_CONTINUE; 708 1.1 christos 709 1.1 christos case TLS_ST_SW_SRVR_DONE: 710 1.1 christos s->ts_msg_write = ossl_time_now(); 711 1.1 christos return WRITE_TRAN_FINISHED; 712 1.1 christos 713 1.1 christos case TLS_ST_SR_FINISHED: 714 1.1 christos s->ts_msg_read = ossl_time_now(); 715 1.1 christos if (s->hit) { 716 1.1 christos st->hand_state = TLS_ST_OK; 717 1.1 christos return WRITE_TRAN_CONTINUE; 718 1.1 christos } else if (s->ext.ticket_expected) { 719 1.1 christos st->hand_state = TLS_ST_SW_SESSION_TICKET; 720 1.1 christos } else { 721 1.1 christos st->hand_state = TLS_ST_SW_CHANGE; 722 1.1 christos } 723 1.1 christos return WRITE_TRAN_CONTINUE; 724 1.1 christos 725 1.1 christos case TLS_ST_SW_SESSION_TICKET: 726 1.1 christos st->hand_state = TLS_ST_SW_CHANGE; 727 1.1 christos return WRITE_TRAN_CONTINUE; 728 1.1 christos 729 1.1 christos case TLS_ST_SW_CHANGE: 730 1.1 christos st->hand_state = TLS_ST_SW_FINISHED; 731 1.1 christos return WRITE_TRAN_CONTINUE; 732 1.1 christos 733 1.1 christos case TLS_ST_SW_FINISHED: 734 1.1 christos if (s->hit) { 735 1.1 christos return WRITE_TRAN_FINISHED; 736 1.1 christos } 737 1.1 christos st->hand_state = TLS_ST_OK; 738 1.1 christos return WRITE_TRAN_CONTINUE; 739 1.1 christos } 740 1.1 christos } 741 1.1 christos 742 1.1 christos /* 743 1.1 christos * Perform any pre work that needs to be done prior to sending a message from 744 1.1 christos * the server to the client. 745 1.1 christos */ 746 1.1 christos WORK_STATE ossl_statem_server_pre_work(SSL_CONNECTION *s, WORK_STATE wst) 747 1.1 christos { 748 1.1 christos OSSL_STATEM *st = &s->statem; 749 1.1 christos SSL *ssl = SSL_CONNECTION_GET_SSL(s); 750 1.1 christos 751 1.1 christos switch (st->hand_state) { 752 1.1 christos default: 753 1.1 christos /* No pre work to be done */ 754 1.1 christos break; 755 1.1 christos 756 1.1 christos case TLS_ST_SW_HELLO_REQ: 757 1.1 christos s->shutdown = 0; 758 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) 759 1.1 christos dtls1_clear_sent_buffer(s); 760 1.1 christos break; 761 1.1 christos 762 1.1 christos case DTLS_ST_SW_HELLO_VERIFY_REQUEST: 763 1.1 christos s->shutdown = 0; 764 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 765 1.1 christos dtls1_clear_sent_buffer(s); 766 1.1 christos /* We don't buffer this message so don't use the timer */ 767 1.1 christos st->use_timer = 0; 768 1.1 christos } 769 1.1 christos break; 770 1.1 christos 771 1.1 christos case TLS_ST_SW_SRVR_HELLO: 772 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 773 1.1 christos /* 774 1.1 christos * Messages we write from now on should be buffered and 775 1.1 christos * retransmitted if necessary, so we need to use the timer now 776 1.1 christos */ 777 1.1 christos st->use_timer = 1; 778 1.1 christos } 779 1.1 christos break; 780 1.1 christos 781 1.1 christos case TLS_ST_SW_SRVR_DONE: 782 1.1 christos #ifndef OPENSSL_NO_SCTP 783 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(ssl))) { 784 1.1 christos /* Calls SSLfatal() as required */ 785 1.1 christos return dtls_wait_for_dry(s); 786 1.1 christos } 787 1.1 christos #endif 788 1.1 christos return WORK_FINISHED_CONTINUE; 789 1.1 christos 790 1.1 christos case TLS_ST_SW_SESSION_TICKET: 791 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) && s->sent_tickets == 0 792 1.1.1.2 christos && s->ext.extra_tickets_expected == 0) { 793 1.1 christos /* 794 1.1 christos * Actually this is the end of the handshake, but we're going 795 1.1 christos * straight into writing the session ticket out. So we finish off 796 1.1 christos * the handshake, but keep the various buffers active. 797 1.1 christos * 798 1.1 christos * Calls SSLfatal as required. 799 1.1 christos */ 800 1.1 christos return tls_finish_handshake(s, wst, 0, 0); 801 1.1 christos } 802 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 803 1.1 christos /* 804 1.1 christos * We're into the last flight. We don't retransmit the last flight 805 1.1 christos * unless we need to, so we don't use the timer 806 1.1 christos */ 807 1.1 christos st->use_timer = 0; 808 1.1 christos } 809 1.1 christos break; 810 1.1 christos 811 1.1 christos case TLS_ST_SW_CHANGE: 812 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) 813 1.1 christos break; 814 1.1 christos /* Writes to s->session are only safe for initial handshakes */ 815 1.1 christos if (s->session->cipher == NULL) { 816 1.1 christos s->session->cipher = s->s3.tmp.new_cipher; 817 1.1 christos } else if (s->session->cipher != s->s3.tmp.new_cipher) { 818 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 819 1.1 christos return WORK_ERROR; 820 1.1 christos } 821 1.1 christos if (!ssl->method->ssl3_enc->setup_key_block(s)) { 822 1.1 christos /* SSLfatal() already called */ 823 1.1 christos return WORK_ERROR; 824 1.1 christos } 825 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 826 1.1 christos /* 827 1.1 christos * We're into the last flight. We don't retransmit the last flight 828 1.1 christos * unless we need to, so we don't use the timer. This might have 829 1.1 christos * already been set to 0 if we sent a NewSessionTicket message, 830 1.1 christos * but we'll set it again here in case we didn't. 831 1.1 christos */ 832 1.1 christos st->use_timer = 0; 833 1.1 christos } 834 1.1 christos return WORK_FINISHED_CONTINUE; 835 1.1 christos 836 1.1 christos case TLS_ST_EARLY_DATA: 837 1.1 christos if (s->early_data_state != SSL_EARLY_DATA_ACCEPTING 838 1.1.1.2 christos && (s->s3.flags & TLS1_FLAGS_STATELESS) == 0) 839 1.1 christos return WORK_FINISHED_CONTINUE; 840 1.1 christos 841 1.1 christos /* 842 1.1 christos * In QUIC with 0-RTT we just carry on when otherwise we would stop 843 1.1 christos * to allow the server to read early data 844 1.1 christos */ 845 1.1 christos if (SSL_NO_EOED(s) && s->ext.early_data == SSL_EARLY_DATA_ACCEPTED 846 1.1 christos && s->early_data_state != SSL_EARLY_DATA_FINISHED_READING) { 847 1.1 christos s->early_data_state = SSL_EARLY_DATA_FINISHED_READING; 848 1.1.1.2 christos if (!ssl->method->ssl3_enc->change_cipher_state(s, SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { 849 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 850 1.1 christos return WORK_ERROR; 851 1.1 christos } 852 1.1 christos return WORK_FINISHED_SWAP; 853 1.1 christos } 854 1.1 christos /* Fall through */ 855 1.1 christos 856 1.1 christos case TLS_ST_OK: 857 1.1 christos /* Calls SSLfatal() as required */ 858 1.1 christos return tls_finish_handshake(s, wst, 1, 1); 859 1.1 christos } 860 1.1 christos 861 1.1 christos return WORK_FINISHED_CONTINUE; 862 1.1 christos } 863 1.1 christos 864 1.1 christos static ossl_inline int conn_is_closed(void) 865 1.1 christos { 866 1.1 christos switch (get_last_sys_error()) { 867 1.1 christos #if defined(EPIPE) 868 1.1 christos case EPIPE: 869 1.1 christos return 1; 870 1.1 christos #endif 871 1.1 christos #if defined(ECONNRESET) 872 1.1 christos case ECONNRESET: 873 1.1 christos return 1; 874 1.1 christos #endif 875 1.1 christos #if defined(WSAECONNRESET) 876 1.1 christos case WSAECONNRESET: 877 1.1 christos return 1; 878 1.1 christos #endif 879 1.1 christos default: 880 1.1 christos return 0; 881 1.1 christos } 882 1.1 christos } 883 1.1 christos 884 1.1 christos /* 885 1.1 christos * Perform any work that needs to be done after sending a message from the 886 1.1 christos * server to the client. 887 1.1 christos */ 888 1.1 christos WORK_STATE ossl_statem_server_post_work(SSL_CONNECTION *s, WORK_STATE wst) 889 1.1 christos { 890 1.1 christos OSSL_STATEM *st = &s->statem; 891 1.1 christos SSL *ssl = SSL_CONNECTION_GET_SSL(s); 892 1.1 christos 893 1.1 christos s->init_num = 0; 894 1.1 christos 895 1.1 christos switch (st->hand_state) { 896 1.1 christos default: 897 1.1 christos /* No post work to be done */ 898 1.1 christos break; 899 1.1 christos 900 1.1 christos case TLS_ST_SW_HELLO_REQ: 901 1.1 christos if (statem_flush(s) != 1) 902 1.1 christos return WORK_MORE_A; 903 1.1 christos if (!ssl3_init_finished_mac(s)) { 904 1.1 christos /* SSLfatal() already called */ 905 1.1 christos return WORK_ERROR; 906 1.1 christos } 907 1.1 christos break; 908 1.1 christos 909 1.1 christos case DTLS_ST_SW_HELLO_VERIFY_REQUEST: 910 1.1 christos if (statem_flush(s) != 1) 911 1.1 christos return WORK_MORE_A; 912 1.1 christos /* HelloVerifyRequest resets Finished MAC */ 913 1.1 christos if (s->version != DTLS1_BAD_VER && !ssl3_init_finished_mac(s)) { 914 1.1 christos /* SSLfatal() already called */ 915 1.1 christos return WORK_ERROR; 916 1.1 christos } 917 1.1 christos /* 918 1.1 christos * The next message should be another ClientHello which we need to 919 1.1 christos * treat like it was the first packet 920 1.1 christos */ 921 1.1 christos s->first_packet = 1; 922 1.1 christos break; 923 1.1 christos 924 1.1 christos case TLS_ST_SW_SRVR_HELLO: 925 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) 926 1.1 christos && s->hello_retry_request == SSL_HRR_PENDING) { 927 1.1 christos if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0 928 1.1.1.2 christos && statem_flush(s) != 1) 929 1.1 christos return WORK_MORE_A; 930 1.1 christos break; 931 1.1 christos } 932 1.1 christos #ifndef OPENSSL_NO_SCTP 933 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { 934 1.1 christos unsigned char sctpauthkey[64]; 935 1.1 christos char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; 936 1.1 christos size_t labellen; 937 1.1 christos 938 1.1 christos /* 939 1.1 christos * Add new shared key for SCTP-Auth, will be ignored if no 940 1.1 christos * SCTP used. 941 1.1 christos */ 942 1.1 christos memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, 943 1.1.1.2 christos sizeof(DTLS1_SCTP_AUTH_LABEL)); 944 1.1 christos 945 1.1 christos /* Don't include the terminating zero. */ 946 1.1 christos labellen = sizeof(labelbuffer) - 1; 947 1.1 christos if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) 948 1.1 christos labellen += 1; 949 1.1 christos 950 1.1 christos if (SSL_export_keying_material(ssl, sctpauthkey, 951 1.1.1.2 christos sizeof(sctpauthkey), labelbuffer, 952 1.1.1.2 christos labellen, NULL, 0, 953 1.1.1.2 christos 0) 954 1.1.1.2 christos <= 0) { 955 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 956 1.1 christos return WORK_ERROR; 957 1.1 christos } 958 1.1 christos 959 1.1 christos BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, 960 1.1.1.2 christos sizeof(sctpauthkey), sctpauthkey); 961 1.1 christos } 962 1.1 christos #endif 963 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s) 964 1.1.1.2 christos || ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 965 1.1.1.2 christos && s->hello_retry_request != SSL_HRR_COMPLETE)) 966 1.1 christos break; 967 1.1 christos /* Fall through */ 968 1.1 christos 969 1.1 christos case TLS_ST_SW_CHANGE: 970 1.1 christos if (s->hello_retry_request == SSL_HRR_PENDING) { 971 1.1 christos if (!statem_flush(s)) 972 1.1 christos return WORK_MORE_A; 973 1.1 christos break; 974 1.1 christos } 975 1.1 christos 976 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 977 1.1 christos if (!ssl->method->ssl3_enc->setup_key_block(s) 978 1.1 christos || !tls13_store_handshake_traffic_hash(s) 979 1.1 christos || !ssl->method->ssl3_enc->change_cipher_state(s, 980 1.1.1.2 christos SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_WRITE)) { 981 1.1 christos /* SSLfatal() already called */ 982 1.1 christos return WORK_ERROR; 983 1.1 christos } 984 1.1 christos 985 1.1 christos if (s->ext.early_data != SSL_EARLY_DATA_ACCEPTED 986 1.1 christos && !ssl->method->ssl3_enc->change_cipher_state(s, 987 1.1.1.2 christos SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { 988 1.1 christos /* SSLfatal() already called */ 989 1.1 christos return WORK_ERROR; 990 1.1 christos } 991 1.1 christos /* 992 1.1 christos * We don't yet know whether the next record we are going to receive 993 1.1 christos * is an unencrypted alert, an encrypted alert, or an encrypted 994 1.1 christos * handshake message. We temporarily tolerate unencrypted alerts. 995 1.1 christos */ 996 1.1 christos if (s->rlayer.rrlmethod->set_plain_alerts != NULL) 997 1.1 christos s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 1); 998 1.1 christos break; 999 1.1 christos } 1000 1.1 christos 1001 1.1 christos #ifndef OPENSSL_NO_SCTP 1002 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && !s->hit) { 1003 1.1 christos /* 1004 1.1 christos * Change to new shared key of SCTP-Auth, will be ignored if 1005 1.1 christos * no SCTP used. 1006 1.1 christos */ 1007 1.1 christos BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 1008 1.1.1.2 christos 0, NULL); 1009 1.1 christos } 1010 1.1 christos #endif 1011 1.1 christos if (!ssl->method->ssl3_enc->change_cipher_state(s, 1012 1.1.1.2 christos SSL3_CHANGE_CIPHER_SERVER_WRITE)) { 1013 1.1 christos /* SSLfatal() already called */ 1014 1.1 christos return WORK_ERROR; 1015 1.1 christos } 1016 1.1 christos break; 1017 1.1 christos 1018 1.1 christos case TLS_ST_SW_SRVR_DONE: 1019 1.1 christos if (statem_flush(s) != 1) 1020 1.1 christos return WORK_MORE_A; 1021 1.1 christos break; 1022 1.1 christos 1023 1.1 christos case TLS_ST_SW_FINISHED: 1024 1.1 christos if (statem_flush(s) != 1) 1025 1.1 christos return WORK_MORE_A; 1026 1.1 christos #ifndef OPENSSL_NO_SCTP 1027 1.1 christos if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { 1028 1.1 christos /* 1029 1.1 christos * Change to new shared key of SCTP-Auth, will be ignored if 1030 1.1 christos * no SCTP used. 1031 1.1 christos */ 1032 1.1 christos BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, 1033 1.1.1.2 christos 0, NULL); 1034 1.1 christos } 1035 1.1 christos #endif 1036 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 1037 1.1 christos /* TLS 1.3 gets the secret size from the handshake md */ 1038 1.1 christos size_t dummy; 1039 1.1 christos if (!ssl->method->ssl3_enc->generate_master_secret(s, 1040 1.1.1.2 christos s->master_secret, s->handshake_secret, 0, 1041 1.1.1.2 christos &dummy) 1042 1.1 christos || !tls13_store_server_finished_hash(s) 1043 1.1 christos || !ssl->method->ssl3_enc->change_cipher_state(s, 1044 1.1.1.2 christos SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_WRITE)) 1045 1.1.1.2 christos /* SSLfatal() already called */ 1046 1.1.1.2 christos return WORK_ERROR; 1047 1.1 christos } 1048 1.1 christos break; 1049 1.1 christos 1050 1.1 christos case TLS_ST_SW_CERT_REQ: 1051 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { 1052 1.1 christos if (statem_flush(s) != 1) 1053 1.1 christos return WORK_MORE_A; 1054 1.1 christos } else { 1055 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s) 1056 1.1.1.2 christos || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) 1057 1.1 christos s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; 1058 1.1 christos } 1059 1.1 christos break; 1060 1.1 christos 1061 1.1 christos case TLS_ST_SW_ENCRYPTED_EXTENSIONS: 1062 1.1 christos if (!s->hit && !send_certificate_request(s)) { 1063 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s) 1064 1.1.1.2 christos || (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) 1065 1.1 christos s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; 1066 1.1 christos } 1067 1.1 christos break; 1068 1.1 christos 1069 1.1 christos case TLS_ST_SW_KEY_UPDATE: 1070 1.1 christos if (statem_flush(s) != 1) 1071 1.1 christos return WORK_MORE_A; 1072 1.1 christos if (!tls13_update_key(s, 1)) { 1073 1.1 christos /* SSLfatal() already called */ 1074 1.1 christos return WORK_ERROR; 1075 1.1 christos } 1076 1.1 christos break; 1077 1.1 christos 1078 1.1 christos case TLS_ST_SW_SESSION_TICKET: 1079 1.1 christos clear_sys_error(); 1080 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) && statem_flush(s) != 1) { 1081 1.1 christos if (SSL_get_error(ssl, 0) == SSL_ERROR_SYSCALL 1082 1.1.1.2 christos && conn_is_closed()) { 1083 1.1 christos /* 1084 1.1 christos * We ignore connection closed errors in TLSv1.3 when sending a 1085 1.1 christos * NewSessionTicket and behave as if we were successful. This is 1086 1.1 christos * so that we are still able to read data sent to us by a client 1087 1.1 christos * that closes soon after the end of the handshake without 1088 1.1 christos * waiting to read our post-handshake NewSessionTickets. 1089 1.1 christos */ 1090 1.1 christos s->rwstate = SSL_NOTHING; 1091 1.1 christos break; 1092 1.1 christos } 1093 1.1 christos 1094 1.1 christos return WORK_MORE_A; 1095 1.1 christos } 1096 1.1 christos break; 1097 1.1 christos } 1098 1.1 christos 1099 1.1 christos return WORK_FINISHED_CONTINUE; 1100 1.1 christos } 1101 1.1 christos 1102 1.1 christos /* 1103 1.1 christos * Get the message construction function and message type for sending from the 1104 1.1 christos * server 1105 1.1 christos * 1106 1.1 christos * Valid return values are: 1107 1.1 christos * 1: Success 1108 1.1 christos * 0: Error 1109 1.1 christos */ 1110 1.1 christos int ossl_statem_server_construct_message(SSL_CONNECTION *s, 1111 1.1.1.2 christos confunc_f *confunc, int *mt) 1112 1.1 christos { 1113 1.1 christos OSSL_STATEM *st = &s->statem; 1114 1.1 christos 1115 1.1 christos switch (st->hand_state) { 1116 1.1 christos default: 1117 1.1 christos /* Shouldn't happen */ 1118 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); 1119 1.1 christos return 0; 1120 1.1 christos 1121 1.1 christos case TLS_ST_SW_CHANGE: 1122 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) 1123 1.1 christos *confunc = dtls_construct_change_cipher_spec; 1124 1.1 christos else 1125 1.1 christos *confunc = tls_construct_change_cipher_spec; 1126 1.1 christos *mt = SSL3_MT_CHANGE_CIPHER_SPEC; 1127 1.1 christos break; 1128 1.1 christos 1129 1.1 christos case DTLS_ST_SW_HELLO_VERIFY_REQUEST: 1130 1.1 christos *confunc = dtls_construct_hello_verify_request; 1131 1.1 christos *mt = DTLS1_MT_HELLO_VERIFY_REQUEST; 1132 1.1 christos break; 1133 1.1 christos 1134 1.1 christos case TLS_ST_SW_HELLO_REQ: 1135 1.1 christos /* No construction function needed */ 1136 1.1 christos *confunc = NULL; 1137 1.1 christos *mt = SSL3_MT_HELLO_REQUEST; 1138 1.1 christos break; 1139 1.1 christos 1140 1.1 christos case TLS_ST_SW_SRVR_HELLO: 1141 1.1 christos *confunc = tls_construct_server_hello; 1142 1.1 christos *mt = SSL3_MT_SERVER_HELLO; 1143 1.1 christos break; 1144 1.1 christos 1145 1.1 christos case TLS_ST_SW_CERT: 1146 1.1 christos *confunc = tls_construct_server_certificate; 1147 1.1 christos *mt = SSL3_MT_CERTIFICATE; 1148 1.1 christos break; 1149 1.1 christos 1150 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 1151 1.1 christos case TLS_ST_SW_COMP_CERT: 1152 1.1 christos *confunc = tls_construct_server_compressed_certificate; 1153 1.1 christos *mt = SSL3_MT_COMPRESSED_CERTIFICATE; 1154 1.1 christos break; 1155 1.1 christos #endif 1156 1.1 christos 1157 1.1 christos case TLS_ST_SW_CERT_VRFY: 1158 1.1 christos *confunc = tls_construct_cert_verify; 1159 1.1 christos *mt = SSL3_MT_CERTIFICATE_VERIFY; 1160 1.1 christos break; 1161 1.1 christos 1162 1.1 christos case TLS_ST_SW_KEY_EXCH: 1163 1.1 christos *confunc = tls_construct_server_key_exchange; 1164 1.1 christos *mt = SSL3_MT_SERVER_KEY_EXCHANGE; 1165 1.1 christos break; 1166 1.1 christos 1167 1.1 christos case TLS_ST_SW_CERT_REQ: 1168 1.1 christos *confunc = tls_construct_certificate_request; 1169 1.1 christos *mt = SSL3_MT_CERTIFICATE_REQUEST; 1170 1.1 christos break; 1171 1.1 christos 1172 1.1 christos case TLS_ST_SW_SRVR_DONE: 1173 1.1 christos *confunc = tls_construct_server_done; 1174 1.1 christos *mt = SSL3_MT_SERVER_DONE; 1175 1.1 christos break; 1176 1.1 christos 1177 1.1 christos case TLS_ST_SW_SESSION_TICKET: 1178 1.1 christos *confunc = tls_construct_new_session_ticket; 1179 1.1 christos *mt = SSL3_MT_NEWSESSION_TICKET; 1180 1.1 christos break; 1181 1.1 christos 1182 1.1 christos case TLS_ST_SW_CERT_STATUS: 1183 1.1 christos *confunc = tls_construct_cert_status; 1184 1.1 christos *mt = SSL3_MT_CERTIFICATE_STATUS; 1185 1.1 christos break; 1186 1.1 christos 1187 1.1 christos case TLS_ST_SW_FINISHED: 1188 1.1 christos *confunc = tls_construct_finished; 1189 1.1 christos *mt = SSL3_MT_FINISHED; 1190 1.1 christos break; 1191 1.1 christos 1192 1.1 christos case TLS_ST_EARLY_DATA: 1193 1.1 christos *confunc = NULL; 1194 1.1 christos *mt = SSL3_MT_DUMMY; 1195 1.1 christos break; 1196 1.1 christos 1197 1.1 christos case TLS_ST_SW_ENCRYPTED_EXTENSIONS: 1198 1.1 christos *confunc = tls_construct_encrypted_extensions; 1199 1.1 christos *mt = SSL3_MT_ENCRYPTED_EXTENSIONS; 1200 1.1 christos break; 1201 1.1 christos 1202 1.1 christos case TLS_ST_SW_KEY_UPDATE: 1203 1.1 christos *confunc = tls_construct_key_update; 1204 1.1 christos *mt = SSL3_MT_KEY_UPDATE; 1205 1.1 christos break; 1206 1.1 christos } 1207 1.1 christos 1208 1.1 christos return 1; 1209 1.1 christos } 1210 1.1 christos 1211 1.1 christos /* 1212 1.1 christos * Maximum size (excluding the Handshake header) of a ClientHello message, 1213 1.1 christos * calculated as follows: 1214 1.1 christos * 1215 1.1 christos * 2 + # client_version 1216 1.1 christos * 32 + # only valid length for random 1217 1.1 christos * 1 + # length of session_id 1218 1.1 christos * 32 + # maximum size for session_id 1219 1.1 christos * 2 + # length of cipher suites 1220 1.1 christos * 2^16-2 + # maximum length of cipher suites array 1221 1.1 christos * 1 + # length of compression_methods 1222 1.1 christos * 2^8-1 + # maximum length of compression methods 1223 1.1 christos * 2 + # length of extensions 1224 1.1 christos * 2^16-1 # maximum length of extensions 1225 1.1 christos */ 1226 1.1.1.2 christos #define CLIENT_HELLO_MAX_LENGTH 131396 1227 1.1 christos 1228 1.1.1.2 christos #define CLIENT_KEY_EXCH_MAX_LENGTH 2048 1229 1.1.1.2 christos #define NEXT_PROTO_MAX_LENGTH 514 1230 1.1 christos 1231 1.1 christos /* 1232 1.1 christos * Returns the maximum allowed length for the current message that we are 1233 1.1 christos * reading. Excludes the message header. 1234 1.1 christos */ 1235 1.1 christos size_t ossl_statem_server_max_message_size(SSL_CONNECTION *s) 1236 1.1 christos { 1237 1.1 christos OSSL_STATEM *st = &s->statem; 1238 1.1 christos 1239 1.1 christos switch (st->hand_state) { 1240 1.1 christos default: 1241 1.1 christos /* Shouldn't happen */ 1242 1.1 christos return 0; 1243 1.1 christos 1244 1.1 christos case TLS_ST_SR_CLNT_HELLO: 1245 1.1 christos return CLIENT_HELLO_MAX_LENGTH; 1246 1.1 christos 1247 1.1 christos case TLS_ST_SR_END_OF_EARLY_DATA: 1248 1.1 christos return END_OF_EARLY_DATA_MAX_LENGTH; 1249 1.1 christos 1250 1.1 christos case TLS_ST_SR_COMP_CERT: 1251 1.1 christos case TLS_ST_SR_CERT: 1252 1.1 christos return s->max_cert_list; 1253 1.1 christos 1254 1.1 christos case TLS_ST_SR_KEY_EXCH: 1255 1.1 christos return CLIENT_KEY_EXCH_MAX_LENGTH; 1256 1.1 christos 1257 1.1 christos case TLS_ST_SR_CERT_VRFY: 1258 1.1 christos return CERTIFICATE_VERIFY_MAX_LENGTH; 1259 1.1 christos 1260 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 1261 1.1 christos case TLS_ST_SR_NEXT_PROTO: 1262 1.1 christos return NEXT_PROTO_MAX_LENGTH; 1263 1.1 christos #endif 1264 1.1 christos 1265 1.1 christos case TLS_ST_SR_CHANGE: 1266 1.1 christos return CCS_MAX_LENGTH; 1267 1.1 christos 1268 1.1 christos case TLS_ST_SR_FINISHED: 1269 1.1 christos return FINISHED_MAX_LENGTH; 1270 1.1 christos 1271 1.1 christos case TLS_ST_SR_KEY_UPDATE: 1272 1.1 christos return KEY_UPDATE_MAX_LENGTH; 1273 1.1 christos } 1274 1.1 christos } 1275 1.1 christos 1276 1.1 christos /* 1277 1.1 christos * Process a message that the server has received from the client. 1278 1.1 christos */ 1279 1.1 christos MSG_PROCESS_RETURN ossl_statem_server_process_message(SSL_CONNECTION *s, 1280 1.1.1.2 christos PACKET *pkt) 1281 1.1 christos { 1282 1.1 christos OSSL_STATEM *st = &s->statem; 1283 1.1 christos 1284 1.1 christos switch (st->hand_state) { 1285 1.1 christos default: 1286 1.1 christos /* Shouldn't happen */ 1287 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1288 1.1 christos return MSG_PROCESS_ERROR; 1289 1.1 christos 1290 1.1 christos case TLS_ST_SR_CLNT_HELLO: 1291 1.1 christos return tls_process_client_hello(s, pkt); 1292 1.1 christos 1293 1.1 christos case TLS_ST_SR_END_OF_EARLY_DATA: 1294 1.1 christos return tls_process_end_of_early_data(s, pkt); 1295 1.1 christos 1296 1.1 christos case TLS_ST_SR_CERT: 1297 1.1 christos return tls_process_client_certificate(s, pkt); 1298 1.1 christos 1299 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 1300 1.1 christos case TLS_ST_SR_COMP_CERT: 1301 1.1 christos return tls_process_client_compressed_certificate(s, pkt); 1302 1.1 christos #endif 1303 1.1 christos 1304 1.1 christos case TLS_ST_SR_KEY_EXCH: 1305 1.1 christos return tls_process_client_key_exchange(s, pkt); 1306 1.1 christos 1307 1.1 christos case TLS_ST_SR_CERT_VRFY: 1308 1.1 christos return tls_process_cert_verify(s, pkt); 1309 1.1 christos 1310 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 1311 1.1 christos case TLS_ST_SR_NEXT_PROTO: 1312 1.1 christos return tls_process_next_proto(s, pkt); 1313 1.1 christos #endif 1314 1.1 christos 1315 1.1 christos case TLS_ST_SR_CHANGE: 1316 1.1 christos return tls_process_change_cipher_spec(s, pkt); 1317 1.1 christos 1318 1.1 christos case TLS_ST_SR_FINISHED: 1319 1.1 christos return tls_process_finished(s, pkt); 1320 1.1 christos 1321 1.1 christos case TLS_ST_SR_KEY_UPDATE: 1322 1.1 christos return tls_process_key_update(s, pkt); 1323 1.1 christos } 1324 1.1 christos } 1325 1.1 christos 1326 1.1 christos /* 1327 1.1 christos * Perform any further processing required following the receipt of a message 1328 1.1 christos * from the client 1329 1.1 christos */ 1330 1.1 christos WORK_STATE ossl_statem_server_post_process_message(SSL_CONNECTION *s, 1331 1.1.1.2 christos WORK_STATE wst) 1332 1.1 christos { 1333 1.1 christos OSSL_STATEM *st = &s->statem; 1334 1.1 christos 1335 1.1 christos switch (st->hand_state) { 1336 1.1 christos default: 1337 1.1 christos /* Shouldn't happen */ 1338 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1339 1.1 christos return WORK_ERROR; 1340 1.1 christos 1341 1.1 christos case TLS_ST_SR_CLNT_HELLO: 1342 1.1 christos return tls_post_process_client_hello(s, wst); 1343 1.1 christos 1344 1.1 christos case TLS_ST_SR_KEY_EXCH: 1345 1.1 christos return tls_post_process_client_key_exchange(s, wst); 1346 1.1 christos } 1347 1.1 christos } 1348 1.1 christos 1349 1.1 christos #ifndef OPENSSL_NO_SRP 1350 1.1 christos /* Returns 1 on success, 0 for retryable error, -1 for fatal error */ 1351 1.1 christos static int ssl_check_srp_ext_ClientHello(SSL_CONNECTION *s) 1352 1.1 christos { 1353 1.1 christos int ret; 1354 1.1 christos int al = SSL_AD_UNRECOGNIZED_NAME; 1355 1.1 christos 1356 1.1.1.2 christos if ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) && (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) { 1357 1.1 christos if (s->srp_ctx.login == NULL) { 1358 1.1 christos /* 1359 1.1 christos * RFC 5054 says SHOULD reject, we do so if There is no srp 1360 1.1 christos * login name 1361 1.1 christos */ 1362 1.1 christos SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, 1363 1.1.1.2 christos SSL_R_PSK_IDENTITY_NOT_FOUND); 1364 1.1 christos return -1; 1365 1.1 christos } else { 1366 1.1 christos ret = ssl_srp_server_param_with_username_intern(s, &al); 1367 1.1 christos if (ret < 0) 1368 1.1 christos return 0; 1369 1.1 christos if (ret == SSL3_AL_FATAL) { 1370 1.1 christos SSLfatal(s, al, 1371 1.1.1.2 christos al == SSL_AD_UNKNOWN_PSK_IDENTITY 1372 1.1.1.2 christos ? SSL_R_PSK_IDENTITY_NOT_FOUND 1373 1.1.1.2 christos : SSL_R_CLIENTHELLO_TLSEXT); 1374 1.1 christos return -1; 1375 1.1 christos } 1376 1.1 christos } 1377 1.1 christos } 1378 1.1 christos return 1; 1379 1.1 christos } 1380 1.1 christos #endif 1381 1.1 christos 1382 1.1 christos int dtls_raw_hello_verify_request(WPACKET *pkt, unsigned char *cookie, 1383 1.1.1.2 christos size_t cookie_len) 1384 1.1 christos { 1385 1.1 christos /* Always use DTLS 1.0 version: see RFC 6347 */ 1386 1.1 christos if (!WPACKET_put_bytes_u16(pkt, DTLS1_VERSION) 1387 1.1.1.2 christos || !WPACKET_sub_memcpy_u8(pkt, cookie, cookie_len)) 1388 1.1 christos return 0; 1389 1.1 christos 1390 1.1 christos return 1; 1391 1.1 christos } 1392 1.1 christos 1393 1.1 christos CON_FUNC_RETURN dtls_construct_hello_verify_request(SSL_CONNECTION *s, 1394 1.1.1.2 christos WPACKET *pkt) 1395 1.1 christos { 1396 1.1 christos unsigned int cookie_leni; 1397 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 1398 1.1 christos 1399 1.1 christos if (sctx->app_gen_cookie_cb == NULL 1400 1.1 christos || sctx->app_gen_cookie_cb(SSL_CONNECTION_GET_USER_SSL(s), s->d1->cookie, 1401 1.1.1.2 christos &cookie_leni) 1402 1.1.1.2 christos == 0 1403 1.1 christos || cookie_leni > DTLS1_COOKIE_LENGTH) { 1404 1.1 christos SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_COOKIE_GEN_CALLBACK_FAILURE); 1405 1.1 christos return CON_FUNC_ERROR; 1406 1.1 christos } 1407 1.1 christos s->d1->cookie_len = cookie_leni; 1408 1.1 christos 1409 1.1 christos if (!dtls_raw_hello_verify_request(pkt, s->d1->cookie, 1410 1.1.1.2 christos s->d1->cookie_len)) { 1411 1.1 christos SSLfatal(s, SSL_AD_NO_ALERT, ERR_R_INTERNAL_ERROR); 1412 1.1 christos return CON_FUNC_ERROR; 1413 1.1 christos } 1414 1.1 christos 1415 1.1 christos return CON_FUNC_SUCCESS; 1416 1.1 christos } 1417 1.1 christos 1418 1.1 christos /*- 1419 1.1 christos * ssl_check_for_safari attempts to fingerprint Safari using OS X 1420 1.1 christos * SecureTransport using the TLS extension block in |hello|. 1421 1.1 christos * Safari, since 10.6, sends exactly these extensions, in this order: 1422 1.1 christos * SNI, 1423 1.1 christos * elliptic_curves 1424 1.1 christos * ec_point_formats 1425 1.1 christos * signature_algorithms (for TLSv1.2 only) 1426 1.1 christos * 1427 1.1 christos * We wish to fingerprint Safari because they broke ECDHE-ECDSA support in 10.8, 1428 1.1 christos * but they advertise support. So enabling ECDHE-ECDSA ciphers breaks them. 1429 1.1 christos * Sadly we cannot differentiate 10.6, 10.7 and 10.8.4 (which work), from 1430 1.1 christos * 10.8..10.8.3 (which don't work). 1431 1.1 christos */ 1432 1.1 christos static void ssl_check_for_safari(SSL_CONNECTION *s, 1433 1.1.1.2 christos const CLIENTHELLO_MSG *hello) 1434 1.1 christos { 1435 1.1 christos static const unsigned char kSafariExtensionsBlock[] = { 1436 1.1.1.2 christos 0x00, 1437 1.1.1.2 christos 0x0a, /* elliptic_curves extension */ 1438 1.1.1.2 christos 0x00, 1439 1.1.1.2 christos 0x08, /* 8 bytes */ 1440 1.1.1.2 christos 0x00, 1441 1.1.1.2 christos 0x06, /* 6 bytes of curve ids */ 1442 1.1.1.2 christos 0x00, 1443 1.1.1.2 christos 0x17, /* P-256 */ 1444 1.1.1.2 christos 0x00, 1445 1.1.1.2 christos 0x18, /* P-384 */ 1446 1.1.1.2 christos 0x00, 1447 1.1.1.2 christos 0x19, /* P-521 */ 1448 1.1.1.2 christos 1449 1.1.1.2 christos 0x00, 1450 1.1.1.2 christos 0x0b, /* ec_point_formats */ 1451 1.1.1.2 christos 0x00, 1452 1.1.1.2 christos 0x02, /* 2 bytes */ 1453 1.1.1.2 christos 0x01, /* 1 point format */ 1454 1.1.1.2 christos 0x00, /* uncompressed */ 1455 1.1 christos /* The following is only present in TLS 1.2 */ 1456 1.1.1.2 christos 0x00, 1457 1.1.1.2 christos 0x0d, /* signature_algorithms */ 1458 1.1.1.2 christos 0x00, 1459 1.1.1.2 christos 0x0c, /* 12 bytes */ 1460 1.1.1.2 christos 0x00, 1461 1.1.1.2 christos 0x0a, /* 10 bytes */ 1462 1.1.1.2 christos 0x05, 1463 1.1.1.2 christos 0x01, /* SHA-384/RSA */ 1464 1.1.1.2 christos 0x04, 1465 1.1.1.2 christos 0x01, /* SHA-256/RSA */ 1466 1.1.1.2 christos 0x02, 1467 1.1.1.2 christos 0x01, /* SHA-1/RSA */ 1468 1.1.1.2 christos 0x04, 1469 1.1.1.2 christos 0x03, /* SHA-256/ECDSA */ 1470 1.1.1.2 christos 0x02, 1471 1.1.1.2 christos 0x03, /* SHA-1/ECDSA */ 1472 1.1 christos }; 1473 1.1 christos /* Length of the common prefix (first two extensions). */ 1474 1.1 christos static const size_t kSafariCommonExtensionsLength = 18; 1475 1.1 christos unsigned int type; 1476 1.1 christos PACKET sni, tmppkt; 1477 1.1 christos size_t ext_len; 1478 1.1 christos 1479 1.1 christos tmppkt = hello->extensions; 1480 1.1 christos 1481 1.1 christos if (!PACKET_forward(&tmppkt, 2) 1482 1.1 christos || !PACKET_get_net_2(&tmppkt, &type) 1483 1.1 christos || !PACKET_get_length_prefixed_2(&tmppkt, &sni)) { 1484 1.1 christos return; 1485 1.1 christos } 1486 1.1 christos 1487 1.1 christos if (type != TLSEXT_TYPE_server_name) 1488 1.1 christos return; 1489 1.1 christos 1490 1.1 christos ext_len = TLS1_get_client_version( 1491 1.1.1.2 christos SSL_CONNECTION_GET_SSL(s)) 1492 1.1.1.2 christos >= TLS1_2_VERSION 1493 1.1.1.2 christos ? sizeof(kSafariExtensionsBlock) 1494 1.1.1.2 christos : kSafariCommonExtensionsLength; 1495 1.1 christos 1496 1.1 christos s->s3.is_probably_safari = PACKET_equal(&tmppkt, kSafariExtensionsBlock, 1497 1.1.1.2 christos ext_len); 1498 1.1 christos } 1499 1.1 christos 1500 1.1.1.2 christos #define RENEG_OPTIONS_OK(options) \ 1501 1.1 christos ((options & SSL_OP_NO_RENEGOTIATION) == 0 \ 1502 1.1.1.2 christos && (options & SSL_OP_ALLOW_CLIENT_RENEGOTIATION) != 0) 1503 1.1 christos 1504 1.1 christos MSG_PROCESS_RETURN tls_process_client_hello(SSL_CONNECTION *s, PACKET *pkt) 1505 1.1 christos { 1506 1.1 christos /* |cookie| will only be initialized for DTLS. */ 1507 1.1 christos PACKET session_id, compression, extensions, cookie; 1508 1.1 christos static const unsigned char null_compression = 0; 1509 1.1 christos CLIENTHELLO_MSG *clienthello = NULL; 1510 1.1 christos 1511 1.1 christos /* Check if this is actually an unexpected renegotiation ClientHello */ 1512 1.1 christos if (s->renegotiate == 0 && !SSL_IS_FIRST_HANDSHAKE(s)) { 1513 1.1 christos if (!ossl_assert(!SSL_CONNECTION_IS_TLS13(s))) { 1514 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1515 1.1 christos goto err; 1516 1.1 christos } 1517 1.1 christos if (!RENEG_OPTIONS_OK(s->options) 1518 1.1.1.2 christos || (!s->s3.send_connection_binding 1519 1.1.1.2 christos && (s->options 1520 1.1.1.2 christos & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION) 1521 1.1.1.2 christos == 0)) { 1522 1.1 christos ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); 1523 1.1 christos return MSG_PROCESS_FINISHED_READING; 1524 1.1 christos } 1525 1.1 christos s->renegotiate = 1; 1526 1.1 christos s->new_session = 1; 1527 1.1 christos } 1528 1.1 christos 1529 1.1 christos clienthello = OPENSSL_zalloc(sizeof(*clienthello)); 1530 1.1 christos if (clienthello == NULL) { 1531 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1532 1.1 christos goto err; 1533 1.1 christos } 1534 1.1 christos 1535 1.1 christos /* 1536 1.1 christos * First, parse the raw ClientHello data into the CLIENTHELLO_MSG structure. 1537 1.1 christos */ 1538 1.1 christos clienthello->isv2 = RECORD_LAYER_is_sslv2_record(&s->rlayer); 1539 1.1 christos PACKET_null_init(&cookie); 1540 1.1 christos 1541 1.1 christos if (clienthello->isv2) { 1542 1.1 christos unsigned int mt; 1543 1.1 christos 1544 1.1 christos if (!SSL_IS_FIRST_HANDSHAKE(s) 1545 1.1.1.2 christos || s->hello_retry_request != SSL_HRR_NONE) { 1546 1.1 christos SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); 1547 1.1 christos goto err; 1548 1.1 christos } 1549 1.1 christos 1550 1.1 christos /*- 1551 1.1 christos * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2 1552 1.1 christos * header is sent directly on the wire, not wrapped as a TLS 1553 1.1 christos * record. Our record layer just processes the message length and passes 1554 1.1 christos * the rest right through. Its format is: 1555 1.1 christos * Byte Content 1556 1.1 christos * 0-1 msg_length - decoded by the record layer 1557 1.1 christos * 2 msg_type - s->init_msg points here 1558 1.1 christos * 3-4 version 1559 1.1 christos * 5-6 cipher_spec_length 1560 1.1 christos * 7-8 session_id_length 1561 1.1 christos * 9-10 challenge_length 1562 1.1 christos * ... ... 1563 1.1 christos */ 1564 1.1 christos 1565 1.1 christos if (!PACKET_get_1(pkt, &mt) 1566 1.1 christos || mt != SSL2_MT_CLIENT_HELLO) { 1567 1.1 christos /* 1568 1.1 christos * Should never happen. We should have tested this in the record 1569 1.1 christos * layer in order to have determined that this is an SSLv2 record 1570 1.1 christos * in the first place 1571 1.1 christos */ 1572 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1573 1.1 christos goto err; 1574 1.1 christos } 1575 1.1 christos } 1576 1.1 christos 1577 1.1 christos if (!PACKET_get_net_2(pkt, &clienthello->legacy_version)) { 1578 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); 1579 1.1 christos goto err; 1580 1.1 christos } 1581 1.1 christos 1582 1.1 christos /* Parse the message and load client random. */ 1583 1.1 christos if (clienthello->isv2) { 1584 1.1 christos /* 1585 1.1 christos * Handle an SSLv2 backwards compatible ClientHello 1586 1.1 christos * Note, this is only for SSLv3+ using the backward compatible format. 1587 1.1 christos * Real SSLv2 is not supported, and is rejected below. 1588 1.1 christos */ 1589 1.1 christos unsigned int ciphersuite_len, session_id_len, challenge_len; 1590 1.1 christos PACKET challenge; 1591 1.1 christos 1592 1.1 christos if (!PACKET_get_net_2(pkt, &ciphersuite_len) 1593 1.1 christos || !PACKET_get_net_2(pkt, &session_id_len) 1594 1.1 christos || !PACKET_get_net_2(pkt, &challenge_len)) { 1595 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); 1596 1.1 christos goto err; 1597 1.1 christos } 1598 1.1 christos 1599 1.1 christos if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) { 1600 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_MISMATCH); 1601 1.1 christos goto err; 1602 1.1 christos } 1603 1.1 christos 1604 1.1 christos if (!PACKET_get_sub_packet(pkt, &clienthello->ciphersuites, 1605 1.1.1.2 christos ciphersuite_len) 1606 1.1 christos || !PACKET_copy_bytes(pkt, clienthello->session_id, session_id_len) 1607 1.1 christos || !PACKET_get_sub_packet(pkt, &challenge, challenge_len) 1608 1.1 christos /* No extensions. */ 1609 1.1 christos || PACKET_remaining(pkt) != 0) { 1610 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_RECORD_LENGTH_MISMATCH); 1611 1.1 christos goto err; 1612 1.1 christos } 1613 1.1 christos clienthello->session_id_len = session_id_len; 1614 1.1 christos 1615 1.1 christos /* Load the client random and compression list. We use SSL3_RANDOM_SIZE 1616 1.1 christos * here rather than sizeof(clienthello->random) because that is the limit 1617 1.1 christos * for SSLv3 and it is fixed. It won't change even if 1618 1.1 christos * sizeof(clienthello->random) does. 1619 1.1 christos */ 1620 1.1 christos challenge_len = challenge_len > SSL3_RANDOM_SIZE 1621 1.1.1.2 christos ? SSL3_RANDOM_SIZE 1622 1.1.1.2 christos : challenge_len; 1623 1.1 christos memset(clienthello->random, 0, SSL3_RANDOM_SIZE); 1624 1.1 christos if (!PACKET_copy_bytes(&challenge, 1625 1.1.1.2 christos clienthello->random + SSL3_RANDOM_SIZE - challenge_len, challenge_len) 1626 1.1 christos /* Advertise only null compression. */ 1627 1.1 christos || !PACKET_buf_init(&compression, &null_compression, 1)) { 1628 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1629 1.1 christos goto err; 1630 1.1 christos } 1631 1.1 christos 1632 1.1 christos PACKET_null_init(&clienthello->extensions); 1633 1.1 christos } else { 1634 1.1 christos /* Regular ClientHello. */ 1635 1.1 christos if (!PACKET_copy_bytes(pkt, clienthello->random, SSL3_RANDOM_SIZE) 1636 1.1 christos || !PACKET_get_length_prefixed_1(pkt, &session_id) 1637 1.1 christos || !PACKET_copy_all(&session_id, clienthello->session_id, 1638 1.1.1.2 christos SSL_MAX_SSL_SESSION_ID_LENGTH, 1639 1.1.1.2 christos &clienthello->session_id_len)) { 1640 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 1641 1.1 christos goto err; 1642 1.1 christos } 1643 1.1 christos 1644 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 1645 1.1 christos if (!PACKET_get_length_prefixed_1(pkt, &cookie)) { 1646 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 1647 1.1 christos goto err; 1648 1.1 christos } 1649 1.1 christos if (!PACKET_copy_all(&cookie, clienthello->dtls_cookie, 1650 1.1.1.2 christos DTLS1_COOKIE_LENGTH, 1651 1.1.1.2 christos &clienthello->dtls_cookie_len)) { 1652 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1653 1.1 christos goto err; 1654 1.1 christos } 1655 1.1 christos /* 1656 1.1 christos * If we require cookies and this ClientHello doesn't contain one, 1657 1.1 christos * just return since we do not want to allocate any memory yet. 1658 1.1 christos * So check cookie length... 1659 1.1 christos */ 1660 1.1 christos if (SSL_get_options(SSL_CONNECTION_GET_SSL(s)) & SSL_OP_COOKIE_EXCHANGE) { 1661 1.1 christos if (clienthello->dtls_cookie_len == 0) { 1662 1.1 christos OPENSSL_free(clienthello); 1663 1.1 christos return MSG_PROCESS_FINISHED_READING; 1664 1.1 christos } 1665 1.1 christos } 1666 1.1 christos } 1667 1.1 christos 1668 1.1 christos if (!PACKET_get_length_prefixed_2(pkt, &clienthello->ciphersuites)) { 1669 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 1670 1.1 christos goto err; 1671 1.1 christos } 1672 1.1 christos 1673 1.1 christos if (!PACKET_get_length_prefixed_1(pkt, &compression)) { 1674 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 1675 1.1 christos goto err; 1676 1.1 christos } 1677 1.1 christos 1678 1.1 christos /* Could be empty. */ 1679 1.1 christos if (PACKET_remaining(pkt) == 0) { 1680 1.1 christos PACKET_null_init(&clienthello->extensions); 1681 1.1 christos } else { 1682 1.1 christos if (!PACKET_get_length_prefixed_2(pkt, &clienthello->extensions) 1683 1.1.1.2 christos || PACKET_remaining(pkt) != 0) { 1684 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 1685 1.1 christos goto err; 1686 1.1 christos } 1687 1.1 christos } 1688 1.1 christos } 1689 1.1 christos 1690 1.1 christos if (!PACKET_copy_all(&compression, clienthello->compressions, 1691 1.1.1.2 christos MAX_COMPRESSIONS_SIZE, 1692 1.1.1.2 christos &clienthello->compressions_len)) { 1693 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1694 1.1 christos goto err; 1695 1.1 christos } 1696 1.1 christos 1697 1.1 christos /* Preserve the raw extensions PACKET for later use */ 1698 1.1 christos extensions = clienthello->extensions; 1699 1.1 christos if (!tls_collect_extensions(s, &extensions, SSL_EXT_CLIENT_HELLO, 1700 1.1.1.2 christos &clienthello->pre_proc_exts, 1701 1.1.1.2 christos &clienthello->pre_proc_exts_len, 1)) { 1702 1.1 christos /* SSLfatal already been called */ 1703 1.1 christos goto err; 1704 1.1 christos } 1705 1.1 christos s->clienthello = clienthello; 1706 1.1 christos 1707 1.1 christos return MSG_PROCESS_CONTINUE_PROCESSING; 1708 1.1 christos 1709 1.1.1.2 christos err: 1710 1.1 christos if (clienthello != NULL) 1711 1.1 christos OPENSSL_free(clienthello->pre_proc_exts); 1712 1.1 christos OPENSSL_free(clienthello); 1713 1.1 christos 1714 1.1 christos return MSG_PROCESS_ERROR; 1715 1.1 christos } 1716 1.1 christos 1717 1.1 christos static int tls_early_post_process_client_hello(SSL_CONNECTION *s) 1718 1.1 christos { 1719 1.1 christos unsigned int j; 1720 1.1 christos int i, al = SSL_AD_INTERNAL_ERROR; 1721 1.1 christos int protverr; 1722 1.1 christos unsigned long id; 1723 1.1 christos #ifndef OPENSSL_NO_COMP 1724 1.1 christos SSL_COMP *comp = NULL; 1725 1.1 christos #endif 1726 1.1 christos const SSL_CIPHER *c; 1727 1.1 christos STACK_OF(SSL_CIPHER) *ciphers = NULL; 1728 1.1 christos STACK_OF(SSL_CIPHER) *scsvs = NULL; 1729 1.1 christos CLIENTHELLO_MSG *clienthello = s->clienthello; 1730 1.1 christos DOWNGRADE dgrd = DOWNGRADE_NONE; 1731 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 1732 1.1 christos SSL *ssl = SSL_CONNECTION_GET_SSL(s); 1733 1.1 christos SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); 1734 1.1 christos 1735 1.1 christos /* Finished parsing the ClientHello, now we can start processing it */ 1736 1.1 christos /* Give the ClientHello callback a crack at things */ 1737 1.1 christos if (sctx->client_hello_cb != NULL) { 1738 1.1 christos /* A failure in the ClientHello callback terminates the connection. */ 1739 1.1 christos switch (sctx->client_hello_cb(ussl, &al, sctx->client_hello_cb_arg)) { 1740 1.1 christos case SSL_CLIENT_HELLO_SUCCESS: 1741 1.1 christos break; 1742 1.1 christos case SSL_CLIENT_HELLO_RETRY: 1743 1.1 christos s->rwstate = SSL_CLIENT_HELLO_CB; 1744 1.1 christos return -1; 1745 1.1 christos case SSL_CLIENT_HELLO_ERROR: 1746 1.1 christos default: 1747 1.1 christos SSLfatal(s, al, SSL_R_CALLBACK_FAILED); 1748 1.1 christos goto err; 1749 1.1 christos } 1750 1.1 christos } 1751 1.1 christos 1752 1.1 christos /* Set up the client_random */ 1753 1.1 christos memcpy(s->s3.client_random, clienthello->random, SSL3_RANDOM_SIZE); 1754 1.1 christos 1755 1.1 christos /* Choose the version */ 1756 1.1 christos 1757 1.1 christos if (clienthello->isv2) { 1758 1.1 christos if (clienthello->legacy_version == SSL2_VERSION 1759 1.1.1.2 christos || (clienthello->legacy_version & 0xff00) 1760 1.1.1.2 christos != (SSL3_VERSION_MAJOR << 8)) { 1761 1.1 christos /* 1762 1.1 christos * This is real SSLv2 or something completely unknown. We don't 1763 1.1 christos * support it. 1764 1.1 christos */ 1765 1.1 christos SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_R_UNKNOWN_PROTOCOL); 1766 1.1 christos goto err; 1767 1.1 christos } 1768 1.1 christos /* SSLv3/TLS */ 1769 1.1 christos s->client_version = clienthello->legacy_version; 1770 1.1 christos } 1771 1.1 christos 1772 1.1 christos /* Choose the server SSL/TLS/DTLS version. */ 1773 1.1 christos protverr = ssl_choose_server_version(s, clienthello, &dgrd); 1774 1.1 christos 1775 1.1 christos if (protverr) { 1776 1.1 christos if (SSL_IS_FIRST_HANDSHAKE(s)) { 1777 1.1 christos /* like ssl3_get_record, send alert using remote version number */ 1778 1.1 christos s->version = s->client_version = clienthello->legacy_version; 1779 1.1 christos } 1780 1.1 christos SSLfatal(s, SSL_AD_PROTOCOL_VERSION, protverr); 1781 1.1 christos goto err; 1782 1.1 christos } 1783 1.1 christos 1784 1.1 christos /* TLSv1.3 specifies that a ClientHello must end on a record boundary */ 1785 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) 1786 1.1 christos && RECORD_LAYER_processed_read_pending(&s->rlayer)) { 1787 1.1 christos SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); 1788 1.1 christos goto err; 1789 1.1 christos } 1790 1.1 christos 1791 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 1792 1.1 christos /* Empty cookie was already handled above by returning early. */ 1793 1.1 christos if (SSL_get_options(ssl) & SSL_OP_COOKIE_EXCHANGE) { 1794 1.1 christos if (sctx->app_verify_cookie_cb != NULL) { 1795 1.1 christos if (sctx->app_verify_cookie_cb(ussl, clienthello->dtls_cookie, 1796 1.1.1.2 christos clienthello->dtls_cookie_len) 1797 1.1.1.2 christos == 0) { 1798 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 1799 1.1.1.2 christos SSL_R_COOKIE_MISMATCH); 1800 1.1 christos goto err; 1801 1.1 christos /* else cookie verification succeeded */ 1802 1.1 christos } 1803 1.1 christos /* default verification */ 1804 1.1 christos } else if (s->d1->cookie_len != clienthello->dtls_cookie_len 1805 1.1.1.2 christos || memcmp(clienthello->dtls_cookie, s->d1->cookie, 1806 1.1.1.2 christos s->d1->cookie_len) 1807 1.1.1.2 christos != 0) { 1808 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_COOKIE_MISMATCH); 1809 1.1 christos goto err; 1810 1.1 christos } 1811 1.1 christos s->d1->cookie_verified = 1; 1812 1.1 christos } 1813 1.1 christos } 1814 1.1 christos 1815 1.1 christos s->hit = 0; 1816 1.1 christos 1817 1.1 christos if (!ssl_cache_cipherlist(s, &clienthello->ciphersuites, 1818 1.1.1.2 christos clienthello->isv2) 1819 1.1.1.2 christos || !ossl_bytes_to_cipher_list(s, &clienthello->ciphersuites, &ciphers, 1820 1.1.1.2 christos &scsvs, clienthello->isv2, 1)) { 1821 1.1 christos /* SSLfatal() already called */ 1822 1.1 christos goto err; 1823 1.1 christos } 1824 1.1 christos 1825 1.1 christos s->s3.send_connection_binding = 0; 1826 1.1 christos /* Check what signalling cipher-suite values were received. */ 1827 1.1 christos if (scsvs != NULL) { 1828 1.1 christos for (i = 0; i < sk_SSL_CIPHER_num(scsvs); i++) { 1829 1.1 christos c = sk_SSL_CIPHER_value(scsvs, i); 1830 1.1 christos if (SSL_CIPHER_get_id(c) == SSL3_CK_SCSV) { 1831 1.1 christos if (s->renegotiate) { 1832 1.1 christos /* SCSV is fatal if renegotiating */ 1833 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 1834 1.1.1.2 christos SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING); 1835 1.1 christos goto err; 1836 1.1 christos } 1837 1.1 christos s->s3.send_connection_binding = 1; 1838 1.1.1.2 christos } else if (SSL_CIPHER_get_id(c) == SSL3_CK_FALLBACK_SCSV && !ssl_check_version_downgrade(s)) { 1839 1.1 christos /* 1840 1.1 christos * This SCSV indicates that the client previously tried 1841 1.1 christos * a higher version. We should fail if the current version 1842 1.1 christos * is an unexpected downgrade, as that indicates that the first 1843 1.1 christos * connection may have been tampered with in order to trigger 1844 1.1 christos * an insecure downgrade. 1845 1.1 christos */ 1846 1.1 christos SSLfatal(s, SSL_AD_INAPPROPRIATE_FALLBACK, 1847 1.1.1.2 christos SSL_R_INAPPROPRIATE_FALLBACK); 1848 1.1 christos goto err; 1849 1.1 christos } 1850 1.1 christos } 1851 1.1 christos } 1852 1.1 christos 1853 1.1 christos /* For TLSv1.3 we must select the ciphersuite *before* session resumption */ 1854 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 1855 1.1.1.2 christos const SSL_CIPHER *cipher = ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(ssl)); 1856 1.1 christos 1857 1.1 christos if (cipher == NULL) { 1858 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); 1859 1.1 christos goto err; 1860 1.1 christos } 1861 1.1 christos if (s->hello_retry_request == SSL_HRR_PENDING 1862 1.1.1.2 christos && (s->s3.tmp.new_cipher == NULL 1863 1.1.1.2 christos || s->s3.tmp.new_cipher->id != cipher->id)) { 1864 1.1 christos /* 1865 1.1 christos * A previous HRR picked a different ciphersuite to the one we 1866 1.1 christos * just selected. Something must have changed. 1867 1.1 christos */ 1868 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_CIPHER); 1869 1.1 christos goto err; 1870 1.1 christos } 1871 1.1 christos s->s3.tmp.new_cipher = cipher; 1872 1.1 christos } 1873 1.1 christos 1874 1.1 christos /* We need to do this before getting the session */ 1875 1.1 christos if (!tls_parse_extension(s, TLSEXT_IDX_extended_master_secret, 1876 1.1.1.2 christos SSL_EXT_CLIENT_HELLO, 1877 1.1.1.2 christos clienthello->pre_proc_exts, NULL, 0)) { 1878 1.1 christos /* SSLfatal() already called */ 1879 1.1 christos goto err; 1880 1.1 christos } 1881 1.1 christos 1882 1.1 christos /* 1883 1.1 christos * We don't allow resumption in a backwards compatible ClientHello. 1884 1.1 christos * In TLS1.1+, session_id MUST be empty. 1885 1.1 christos * 1886 1.1 christos * Versions before 0.9.7 always allow clients to resume sessions in 1887 1.1 christos * renegotiation. 0.9.7 and later allow this by default, but optionally 1888 1.1 christos * ignore resumption requests with flag 1889 1.1 christos * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather 1890 1.1 christos * than a change to default behavior so that applications relying on 1891 1.1 christos * this for security won't even compile against older library versions). 1892 1.1 christos * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to 1893 1.1 christos * request renegotiation but not a new session (s->new_session remains 1894 1.1 christos * unset): for servers, this essentially just means that the 1895 1.1 christos * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be 1896 1.1 christos * ignored. 1897 1.1 christos */ 1898 1.1.1.2 christos if (clienthello->isv2 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) { 1899 1.1 christos if (!ssl_get_new_session(s, 1)) { 1900 1.1 christos /* SSLfatal() already called */ 1901 1.1 christos goto err; 1902 1.1 christos } 1903 1.1 christos } else { 1904 1.1 christos i = ssl_get_prev_session(s, clienthello); 1905 1.1 christos if (i == 1) { 1906 1.1 christos /* previous session */ 1907 1.1 christos s->hit = 1; 1908 1.1 christos } else if (i == -1) { 1909 1.1 christos /* SSLfatal() already called */ 1910 1.1 christos goto err; 1911 1.1 christos } else { 1912 1.1 christos /* i == 0 */ 1913 1.1 christos if (!ssl_get_new_session(s, 1)) { 1914 1.1 christos /* SSLfatal() already called */ 1915 1.1 christos goto err; 1916 1.1 christos } 1917 1.1 christos } 1918 1.1 christos } 1919 1.1 christos 1920 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 1921 1.1 christos memcpy(s->tmp_session_id, s->clienthello->session_id, 1922 1.1.1.2 christos s->clienthello->session_id_len); 1923 1.1 christos s->tmp_session_id_len = s->clienthello->session_id_len; 1924 1.1 christos } 1925 1.1 christos 1926 1.1 christos /* 1927 1.1 christos * If it is a hit, check that the cipher is in the list. In TLSv1.3 we check 1928 1.1 christos * ciphersuite compatibility with the session as part of resumption. 1929 1.1 christos */ 1930 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s) && s->hit) { 1931 1.1 christos j = 0; 1932 1.1 christos id = s->session->cipher->id; 1933 1.1 christos 1934 1.1.1.2 christos OSSL_TRACE_BEGIN(TLS_CIPHER) 1935 1.1.1.2 christos { 1936 1.1 christos BIO_printf(trc_out, "client sent %d ciphers\n", 1937 1.1.1.2 christos sk_SSL_CIPHER_num(ciphers)); 1938 1.1 christos } 1939 1.1 christos for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) { 1940 1.1 christos c = sk_SSL_CIPHER_value(ciphers, i); 1941 1.1 christos if (trc_out != NULL) 1942 1.1 christos BIO_printf(trc_out, "client [%2d of %2d]:%s\n", i, 1943 1.1.1.2 christos sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c)); 1944 1.1 christos if (c->id == id) { 1945 1.1 christos j = 1; 1946 1.1 christos break; 1947 1.1 christos } 1948 1.1 christos } 1949 1.1 christos if (j == 0) { 1950 1.1 christos /* 1951 1.1 christos * we need to have the cipher in the cipher list if we are asked 1952 1.1 christos * to reuse it 1953 1.1 christos */ 1954 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 1955 1.1.1.2 christos SSL_R_REQUIRED_CIPHER_MISSING); 1956 1.1 christos OSSL_TRACE_CANCEL(TLS_CIPHER); 1957 1.1 christos goto err; 1958 1.1 christos } 1959 1.1 christos OSSL_TRACE_END(TLS_CIPHER); 1960 1.1 christos } 1961 1.1 christos 1962 1.1 christos /* At least one compression method must be preset. */ 1963 1.1 christos if (clienthello->compressions_len == 0) { 1964 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_NO_COMPRESSION_SPECIFIED); 1965 1.1 christos goto err; 1966 1.1 christos } 1967 1.1 christos /* Make sure at least the null compression is supported. */ 1968 1.1 christos if (memchr(clienthello->compressions, 0, 1969 1.1.1.2 christos clienthello->compressions_len) 1970 1.1.1.2 christos == NULL) { 1971 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 1972 1.1.1.2 christos SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); 1973 1.1 christos goto err; 1974 1.1 christos } 1975 1.1 christos 1976 1.1 christos if (s->options & SSL_OP_SAFARI_ECDHE_ECDSA_BUG) 1977 1.1 christos ssl_check_for_safari(s, clienthello); 1978 1.1 christos 1979 1.1 christos /* TLS extensions */ 1980 1.1 christos if (!tls_parse_all_extensions(s, SSL_EXT_CLIENT_HELLO, 1981 1.1.1.2 christos clienthello->pre_proc_exts, NULL, 0, 1)) { 1982 1.1 christos /* SSLfatal() already called */ 1983 1.1 christos goto err; 1984 1.1 christos } 1985 1.1 christos 1986 1.1 christos /* 1987 1.1 christos * Check if we want to use external pre-shared secret for this handshake 1988 1.1 christos * for not reused session only. We need to generate server_random before 1989 1.1 christos * calling tls_session_secret_cb in order to allow SessionTicket 1990 1.1 christos * processing to use it in key derivation. 1991 1.1 christos */ 1992 1.1 christos { 1993 1.1 christos unsigned char *pos; 1994 1.1 christos pos = s->s3.server_random; 1995 1.1 christos if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE, dgrd) <= 0) { 1996 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 1997 1.1 christos goto err; 1998 1.1 christos } 1999 1.1 christos } 2000 1.1 christos 2001 1.1 christos if (!s->hit && !tls1_set_server_sigalgs(s)) { 2002 1.1 christos /* SSLfatal() already called */ 2003 1.1 christos goto err; 2004 1.1 christos } 2005 1.1 christos 2006 1.1 christos if (!s->hit 2007 1.1.1.2 christos && s->version >= TLS1_VERSION 2008 1.1.1.2 christos && !SSL_CONNECTION_IS_TLS13(s) 2009 1.1.1.2 christos && !SSL_CONNECTION_IS_DTLS(s) 2010 1.1.1.2 christos && s->ext.session_secret_cb != NULL) { 2011 1.1 christos const SSL_CIPHER *pref_cipher = NULL; 2012 1.1 christos /* 2013 1.1 christos * s->session->master_key_length is a size_t, but this is an int for 2014 1.1 christos * backwards compat reasons 2015 1.1 christos */ 2016 1.1 christos int master_key_length; 2017 1.1 christos 2018 1.1 christos master_key_length = sizeof(s->session->master_key); 2019 1.1 christos if (s->ext.session_secret_cb(ussl, s->session->master_key, 2020 1.1.1.2 christos &master_key_length, ciphers, 2021 1.1.1.2 christos &pref_cipher, 2022 1.1.1.2 christos s->ext.session_secret_cb_arg) 2023 1.1.1.2 christos && master_key_length > 0) { 2024 1.1 christos s->session->master_key_length = master_key_length; 2025 1.1 christos s->hit = 1; 2026 1.1 christos s->peer_ciphers = ciphers; 2027 1.1 christos s->session->verify_result = X509_V_OK; 2028 1.1 christos 2029 1.1 christos ciphers = NULL; 2030 1.1 christos 2031 1.1 christos /* check if some cipher was preferred by call back */ 2032 1.1 christos if (pref_cipher == NULL) 2033 1.1 christos pref_cipher = ssl3_choose_cipher(s, s->peer_ciphers, 2034 1.1.1.2 christos SSL_get_ciphers(ssl)); 2035 1.1 christos if (pref_cipher == NULL) { 2036 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_NO_SHARED_CIPHER); 2037 1.1 christos goto err; 2038 1.1 christos } 2039 1.1 christos 2040 1.1 christos s->session->cipher = pref_cipher; 2041 1.1 christos sk_SSL_CIPHER_free(s->cipher_list); 2042 1.1 christos s->cipher_list = sk_SSL_CIPHER_dup(s->peer_ciphers); 2043 1.1 christos sk_SSL_CIPHER_free(s->cipher_list_by_id); 2044 1.1 christos s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->peer_ciphers); 2045 1.1 christos } 2046 1.1 christos } 2047 1.1 christos 2048 1.1 christos /* 2049 1.1 christos * Worst case, we will use the NULL compression, but if we have other 2050 1.1 christos * options, we will now look for them. We have complen-1 compression 2051 1.1 christos * algorithms from the client, starting at q. 2052 1.1 christos */ 2053 1.1 christos s->s3.tmp.new_compression = NULL; 2054 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 2055 1.1 christos /* 2056 1.1 christos * We already checked above that the NULL compression method appears in 2057 1.1 christos * the list. Now we check there aren't any others (which is illegal in 2058 1.1 christos * a TLSv1.3 ClientHello. 2059 1.1 christos */ 2060 1.1 christos if (clienthello->compressions_len != 1) { 2061 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 2062 1.1.1.2 christos SSL_R_INVALID_COMPRESSION_ALGORITHM); 2063 1.1 christos goto err; 2064 1.1 christos } 2065 1.1 christos } 2066 1.1 christos #ifndef OPENSSL_NO_COMP 2067 1.1 christos /* This only happens if we have a cache hit */ 2068 1.1 christos else if (s->session->compress_meth != 0) { 2069 1.1 christos int m, comp_id = s->session->compress_meth; 2070 1.1 christos unsigned int k; 2071 1.1 christos /* Perform sanity checks on resumed compression algorithm */ 2072 1.1 christos /* Can't disable compression */ 2073 1.1 christos if (!ssl_allow_compression(s)) { 2074 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 2075 1.1.1.2 christos SSL_R_INCONSISTENT_COMPRESSION); 2076 1.1 christos goto err; 2077 1.1 christos } 2078 1.1 christos /* Look for resumed compression method */ 2079 1.1 christos for (m = 0; m < sk_SSL_COMP_num(sctx->comp_methods); m++) { 2080 1.1 christos comp = sk_SSL_COMP_value(sctx->comp_methods, m); 2081 1.1 christos if (comp_id == comp->id) { 2082 1.1 christos s->s3.tmp.new_compression = comp; 2083 1.1 christos break; 2084 1.1 christos } 2085 1.1 christos } 2086 1.1 christos if (s->s3.tmp.new_compression == NULL) { 2087 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 2088 1.1.1.2 christos SSL_R_INVALID_COMPRESSION_ALGORITHM); 2089 1.1 christos goto err; 2090 1.1 christos } 2091 1.1 christos /* Look for resumed method in compression list */ 2092 1.1 christos for (k = 0; k < clienthello->compressions_len; k++) { 2093 1.1 christos if (clienthello->compressions[k] == comp_id) 2094 1.1 christos break; 2095 1.1 christos } 2096 1.1 christos if (k >= clienthello->compressions_len) { 2097 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, 2098 1.1.1.2 christos SSL_R_REQUIRED_COMPRESSION_ALGORITHM_MISSING); 2099 1.1 christos goto err; 2100 1.1 christos } 2101 1.1 christos } else if (s->hit) { 2102 1.1 christos comp = NULL; 2103 1.1 christos } else if (ssl_allow_compression(s) && sctx->comp_methods) { 2104 1.1 christos /* See if we have a match */ 2105 1.1 christos int m, nn, v, done = 0; 2106 1.1 christos unsigned int o; 2107 1.1 christos 2108 1.1 christos nn = sk_SSL_COMP_num(sctx->comp_methods); 2109 1.1 christos for (m = 0; m < nn; m++) { 2110 1.1 christos comp = sk_SSL_COMP_value(sctx->comp_methods, m); 2111 1.1 christos v = comp->id; 2112 1.1 christos for (o = 0; o < clienthello->compressions_len; o++) { 2113 1.1 christos if (v == clienthello->compressions[o]) { 2114 1.1 christos done = 1; 2115 1.1 christos break; 2116 1.1 christos } 2117 1.1 christos } 2118 1.1 christos if (done) 2119 1.1 christos break; 2120 1.1 christos } 2121 1.1 christos if (done) 2122 1.1 christos s->s3.tmp.new_compression = comp; 2123 1.1 christos else 2124 1.1 christos comp = NULL; 2125 1.1 christos } 2126 1.1 christos #else 2127 1.1 christos /* 2128 1.1 christos * If compression is disabled we'd better not try to resume a session 2129 1.1 christos * using compression. 2130 1.1 christos */ 2131 1.1 christos if (s->session->compress_meth != 0) { 2132 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION); 2133 1.1 christos goto err; 2134 1.1 christos } 2135 1.1 christos #endif 2136 1.1 christos 2137 1.1 christos /* 2138 1.1 christos * Given s->peer_ciphers and SSL_get_ciphers, we must pick a cipher 2139 1.1 christos */ 2140 1.1 christos 2141 1.1 christos if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { 2142 1.1 christos sk_SSL_CIPHER_free(s->peer_ciphers); 2143 1.1 christos s->peer_ciphers = ciphers; 2144 1.1 christos if (ciphers == NULL) { 2145 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2146 1.1 christos goto err; 2147 1.1 christos } 2148 1.1 christos ciphers = NULL; 2149 1.1 christos } 2150 1.1 christos 2151 1.1 christos if (!s->hit) { 2152 1.1 christos #ifdef OPENSSL_NO_COMP 2153 1.1 christos s->session->compress_meth = 0; 2154 1.1 christos #else 2155 1.1 christos s->session->compress_meth = (comp == NULL) ? 0 : comp->id; 2156 1.1 christos #endif 2157 1.1 christos } 2158 1.1 christos 2159 1.1 christos sk_SSL_CIPHER_free(ciphers); 2160 1.1 christos sk_SSL_CIPHER_free(scsvs); 2161 1.1 christos OPENSSL_free(clienthello->pre_proc_exts); 2162 1.1 christos OPENSSL_free(s->clienthello); 2163 1.1 christos s->clienthello = NULL; 2164 1.1 christos return 1; 2165 1.1.1.2 christos err: 2166 1.1 christos sk_SSL_CIPHER_free(ciphers); 2167 1.1 christos sk_SSL_CIPHER_free(scsvs); 2168 1.1 christos OPENSSL_free(clienthello->pre_proc_exts); 2169 1.1 christos OPENSSL_free(s->clienthello); 2170 1.1 christos s->clienthello = NULL; 2171 1.1 christos 2172 1.1 christos return 0; 2173 1.1 christos } 2174 1.1 christos 2175 1.1 christos /* 2176 1.1 christos * Call the status request callback if needed. Upon success, returns 1. 2177 1.1 christos * Upon failure, returns 0. 2178 1.1 christos */ 2179 1.1 christos static int tls_handle_status_request(SSL_CONNECTION *s) 2180 1.1 christos { 2181 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 2182 1.1 christos 2183 1.1 christos s->ext.status_expected = 0; 2184 1.1 christos 2185 1.1 christos /* 2186 1.1 christos * If status request then ask callback what to do. Note: this must be 2187 1.1 christos * called after servername callbacks in case the certificate has changed, 2188 1.1 christos * and must be called after the cipher has been chosen because this may 2189 1.1 christos * influence which certificate is sent 2190 1.1 christos */ 2191 1.1 christos if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing && sctx != NULL 2192 1.1.1.2 christos && sctx->ext.status_cb != NULL) { 2193 1.1 christos int ret; 2194 1.1 christos 2195 1.1 christos /* If no certificate can't return certificate status */ 2196 1.1 christos if (s->s3.tmp.cert != NULL) { 2197 1.1 christos /* 2198 1.1 christos * Set current certificate to one we will use so SSL_get_certificate 2199 1.1 christos * et al can pick it up. 2200 1.1 christos */ 2201 1.1 christos s->cert->key = s->s3.tmp.cert; 2202 1.1 christos ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s), 2203 1.1.1.2 christos sctx->ext.status_arg); 2204 1.1 christos switch (ret) { 2205 1.1 christos /* We don't want to send a status request response */ 2206 1.1 christos case SSL_TLSEXT_ERR_NOACK: 2207 1.1 christos s->ext.status_expected = 0; 2208 1.1 christos break; 2209 1.1 christos /* status request response should be sent */ 2210 1.1 christos case SSL_TLSEXT_ERR_OK: 2211 1.1 christos if (s->ext.ocsp.resp) 2212 1.1 christos s->ext.status_expected = 1; 2213 1.1 christos break; 2214 1.1 christos /* something bad happened */ 2215 1.1 christos case SSL_TLSEXT_ERR_ALERT_FATAL: 2216 1.1 christos default: 2217 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CLIENTHELLO_TLSEXT); 2218 1.1 christos return 0; 2219 1.1 christos } 2220 1.1 christos } 2221 1.1 christos } 2222 1.1 christos 2223 1.1 christos return 1; 2224 1.1 christos } 2225 1.1 christos 2226 1.1 christos /* 2227 1.1 christos * Call the alpn_select callback if needed. Upon success, returns 1. 2228 1.1 christos * Upon failure, returns 0. 2229 1.1 christos */ 2230 1.1 christos int tls_handle_alpn(SSL_CONNECTION *s) 2231 1.1 christos { 2232 1.1 christos const unsigned char *selected = NULL; 2233 1.1 christos unsigned char selected_len = 0; 2234 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 2235 1.1 christos 2236 1.1 christos if (sctx->ext.alpn_select_cb != NULL && s->s3.alpn_proposed != NULL) { 2237 1.1 christos int r = sctx->ext.alpn_select_cb(SSL_CONNECTION_GET_USER_SSL(s), 2238 1.1.1.2 christos &selected, &selected_len, 2239 1.1.1.2 christos s->s3.alpn_proposed, 2240 1.1.1.2 christos (unsigned int)s->s3.alpn_proposed_len, 2241 1.1.1.2 christos sctx->ext.alpn_select_cb_arg); 2242 1.1 christos 2243 1.1 christos if (r == SSL_TLSEXT_ERR_OK) { 2244 1.1 christos OPENSSL_free(s->s3.alpn_selected); 2245 1.1 christos s->s3.alpn_selected = OPENSSL_memdup(selected, selected_len); 2246 1.1 christos if (s->s3.alpn_selected == NULL) { 2247 1.1 christos s->s3.alpn_selected_len = 0; 2248 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2249 1.1 christos return 0; 2250 1.1 christos } 2251 1.1 christos s->s3.alpn_selected_len = selected_len; 2252 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 2253 1.1 christos /* ALPN takes precedence over NPN. */ 2254 1.1 christos s->s3.npn_seen = 0; 2255 1.1 christos #endif 2256 1.1 christos 2257 1.1 christos /* Check ALPN is consistent with session */ 2258 1.1 christos if (s->session->ext.alpn_selected == NULL 2259 1.1.1.2 christos || selected_len != s->session->ext.alpn_selected_len 2260 1.1.1.2 christos || memcmp(selected, s->session->ext.alpn_selected, 2261 1.1.1.2 christos selected_len) 2262 1.1.1.2 christos != 0) { 2263 1.1 christos /* Not consistent so can't be used for early_data */ 2264 1.1 christos s->ext.early_data_ok = 0; 2265 1.1 christos 2266 1.1 christos if (!s->hit) { 2267 1.1 christos /* 2268 1.1 christos * This is a new session and so alpn_selected should have 2269 1.1 christos * been initialised to NULL. We should update it with the 2270 1.1 christos * selected ALPN. 2271 1.1 christos */ 2272 1.1 christos if (!ossl_assert(s->session->ext.alpn_selected == NULL)) { 2273 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 2274 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 2275 1.1 christos return 0; 2276 1.1 christos } 2277 1.1 christos s->session->ext.alpn_selected = OPENSSL_memdup(selected, 2278 1.1.1.2 christos selected_len); 2279 1.1 christos if (s->session->ext.alpn_selected == NULL) { 2280 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, 2281 1.1.1.2 christos ERR_R_INTERNAL_ERROR); 2282 1.1 christos return 0; 2283 1.1 christos } 2284 1.1 christos s->session->ext.alpn_selected_len = selected_len; 2285 1.1 christos } 2286 1.1 christos } 2287 1.1 christos 2288 1.1 christos return 1; 2289 1.1 christos } else if (r != SSL_TLSEXT_ERR_NOACK) { 2290 1.1 christos SSLfatal(s, SSL_AD_NO_APPLICATION_PROTOCOL, 2291 1.1.1.2 christos SSL_R_NO_APPLICATION_PROTOCOL); 2292 1.1 christos return 0; 2293 1.1 christos } 2294 1.1 christos /* 2295 1.1 christos * If r == SSL_TLSEXT_ERR_NOACK then behave as if no callback was 2296 1.1 christos * present. 2297 1.1 christos */ 2298 1.1 christos } 2299 1.1 christos 2300 1.1 christos /* Check ALPN is consistent with session */ 2301 1.1 christos if (s->session->ext.alpn_selected != NULL) { 2302 1.1 christos /* Not consistent so can't be used for early_data */ 2303 1.1 christos s->ext.early_data_ok = 0; 2304 1.1 christos } 2305 1.1 christos 2306 1.1 christos return 1; 2307 1.1 christos } 2308 1.1 christos 2309 1.1 christos WORK_STATE tls_post_process_client_hello(SSL_CONNECTION *s, WORK_STATE wst) 2310 1.1 christos { 2311 1.1 christos const SSL_CIPHER *cipher; 2312 1.1 christos SSL *ssl = SSL_CONNECTION_GET_SSL(s); 2313 1.1 christos SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); 2314 1.1 christos 2315 1.1 christos if (wst == WORK_MORE_A) { 2316 1.1 christos int rv = tls_early_post_process_client_hello(s); 2317 1.1 christos if (rv == 0) { 2318 1.1 christos /* SSLfatal() was already called */ 2319 1.1 christos goto err; 2320 1.1 christos } 2321 1.1 christos if (rv < 0) 2322 1.1 christos return WORK_MORE_A; 2323 1.1 christos wst = WORK_MORE_B; 2324 1.1 christos } 2325 1.1 christos if (wst == WORK_MORE_B) { 2326 1.1 christos if (!s->hit || SSL_CONNECTION_IS_TLS13(s)) { 2327 1.1 christos /* Let cert callback update server certificates if required */ 2328 1.1 christos if (!s->hit && s->cert->cert_cb != NULL) { 2329 1.1 christos int rv = s->cert->cert_cb(ussl, s->cert->cert_cb_arg); 2330 1.1 christos 2331 1.1 christos if (rv == 0) { 2332 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CERT_CB_ERROR); 2333 1.1 christos goto err; 2334 1.1 christos } 2335 1.1 christos if (rv < 0) { 2336 1.1 christos s->rwstate = SSL_X509_LOOKUP; 2337 1.1 christos return WORK_MORE_B; 2338 1.1 christos } 2339 1.1 christos s->rwstate = SSL_NOTHING; 2340 1.1 christos } 2341 1.1 christos 2342 1.1 christos /* In TLSv1.3 we selected the ciphersuite before resumption */ 2343 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s)) { 2344 1.1.1.2 christos cipher = ssl3_choose_cipher(s, s->peer_ciphers, 2345 1.1.1.2 christos SSL_get_ciphers(ssl)); 2346 1.1 christos 2347 1.1 christos if (cipher == NULL) { 2348 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 2349 1.1.1.2 christos SSL_R_NO_SHARED_CIPHER); 2350 1.1 christos goto err; 2351 1.1 christos } 2352 1.1 christos s->s3.tmp.new_cipher = cipher; 2353 1.1 christos } 2354 1.1 christos if (!s->hit) { 2355 1.1 christos if (!tls_choose_sigalg(s, 1)) { 2356 1.1 christos /* SSLfatal already called */ 2357 1.1 christos goto err; 2358 1.1 christos } 2359 1.1 christos /* check whether we should disable session resumption */ 2360 1.1 christos if (s->not_resumable_session_cb != NULL) 2361 1.1.1.2 christos s->session->not_resumable = s->not_resumable_session_cb(ussl, 2362 1.1.1.2 christos ((s->s3.tmp.new_cipher->algorithm_mkey 2363 1.1.1.2 christos & (SSL_kDHE | SSL_kECDHE)) 2364 1.1.1.2 christos != 0)); 2365 1.1 christos if (s->session->not_resumable) 2366 1.1 christos /* do not send a session ticket */ 2367 1.1 christos s->ext.ticket_expected = 0; 2368 1.1 christos } 2369 1.1 christos } else { 2370 1.1 christos /* Session-id reuse */ 2371 1.1 christos s->s3.tmp.new_cipher = s->session->cipher; 2372 1.1 christos } 2373 1.1 christos 2374 1.1 christos /*- 2375 1.1 christos * we now have the following setup. 2376 1.1 christos * client_random 2377 1.1 christos * cipher_list - our preferred list of ciphers 2378 1.1 christos * ciphers - the client's preferred list of ciphers 2379 1.1 christos * compression - basically ignored right now 2380 1.1 christos * ssl version is set - sslv3 2381 1.1 christos * s->session - The ssl session has been setup. 2382 1.1 christos * s->hit - session reuse flag 2383 1.1 christos * s->s3.tmp.new_cipher - the new cipher to use. 2384 1.1 christos */ 2385 1.1 christos 2386 1.1 christos /* 2387 1.1 christos * Call status_request callback if needed. Has to be done after the 2388 1.1 christos * certificate callbacks etc above. 2389 1.1 christos */ 2390 1.1 christos if (!tls_handle_status_request(s)) { 2391 1.1 christos /* SSLfatal() already called */ 2392 1.1 christos goto err; 2393 1.1 christos } 2394 1.1 christos /* 2395 1.1 christos * Call alpn_select callback if needed. Has to be done after SNI and 2396 1.1 christos * cipher negotiation (HTTP/2 restricts permitted ciphers). In TLSv1.3 2397 1.1 christos * we already did this because cipher negotiation happens earlier, and 2398 1.1 christos * we must handle ALPN before we decide whether to accept early_data. 2399 1.1 christos */ 2400 1.1 christos if (!SSL_CONNECTION_IS_TLS13(s) && !tls_handle_alpn(s)) { 2401 1.1 christos /* SSLfatal() already called */ 2402 1.1 christos goto err; 2403 1.1 christos } 2404 1.1 christos 2405 1.1 christos wst = WORK_MORE_C; 2406 1.1 christos } 2407 1.1 christos #ifndef OPENSSL_NO_SRP 2408 1.1 christos if (wst == WORK_MORE_C) { 2409 1.1 christos int ret; 2410 1.1 christos if ((ret = ssl_check_srp_ext_ClientHello(s)) == 0) { 2411 1.1 christos /* 2412 1.1 christos * callback indicates further work to be done 2413 1.1 christos */ 2414 1.1 christos s->rwstate = SSL_X509_LOOKUP; 2415 1.1 christos return WORK_MORE_C; 2416 1.1 christos } 2417 1.1 christos if (ret < 0) { 2418 1.1 christos /* SSLfatal() already called */ 2419 1.1 christos goto err; 2420 1.1 christos } 2421 1.1 christos } 2422 1.1 christos #endif 2423 1.1 christos 2424 1.1 christos return WORK_FINISHED_STOP; 2425 1.1.1.2 christos err: 2426 1.1 christos return WORK_ERROR; 2427 1.1 christos } 2428 1.1 christos 2429 1.1 christos CON_FUNC_RETURN tls_construct_server_hello(SSL_CONNECTION *s, WPACKET *pkt) 2430 1.1 christos { 2431 1.1 christos int compm; 2432 1.1 christos size_t sl, len; 2433 1.1 christos int version; 2434 1.1 christos unsigned char *session_id; 2435 1.1 christos int usetls13 = SSL_CONNECTION_IS_TLS13(s) 2436 1.1.1.2 christos || s->hello_retry_request == SSL_HRR_PENDING; 2437 1.1 christos 2438 1.1 christos version = usetls13 ? TLS1_2_VERSION : s->version; 2439 1.1 christos if (!WPACKET_put_bytes_u16(pkt, version) 2440 1.1.1.2 christos /* 2441 1.1.1.2 christos * Random stuff. Filling of the server_random takes place in 2442 1.1.1.2 christos * tls_process_client_hello() 2443 1.1.1.2 christos */ 2444 1.1.1.2 christos || !WPACKET_memcpy(pkt, 2445 1.1.1.2 christos s->hello_retry_request == SSL_HRR_PENDING 2446 1.1.1.2 christos ? hrrrandom 2447 1.1.1.2 christos : s->s3.server_random, 2448 1.1.1.2 christos SSL3_RANDOM_SIZE)) { 2449 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2450 1.1 christos return CON_FUNC_ERROR; 2451 1.1 christos } 2452 1.1 christos 2453 1.1 christos /*- 2454 1.1 christos * There are several cases for the session ID to send 2455 1.1 christos * back in the server hello: 2456 1.1 christos * - For session reuse from the session cache, 2457 1.1 christos * we send back the old session ID. 2458 1.1 christos * - If stateless session reuse (using a session ticket) 2459 1.1 christos * is successful, we send back the client's "session ID" 2460 1.1 christos * (which doesn't actually identify the session). 2461 1.1 christos * - If it is a new session, we send back the new 2462 1.1 christos * session ID. 2463 1.1 christos * - However, if we want the new session to be single-use, 2464 1.1 christos * we send back a 0-length session ID. 2465 1.1 christos * - In TLSv1.3 we echo back the session id sent to us by the client 2466 1.1 christos * regardless 2467 1.1 christos * s->hit is non-zero in either case of session reuse, 2468 1.1 christos * so the following won't overwrite an ID that we're supposed 2469 1.1 christos * to send back. 2470 1.1 christos */ 2471 1.1 christos if (!(SSL_CONNECTION_GET_CTX(s)->session_cache_mode & SSL_SESS_CACHE_SERVER) 2472 1.1.1.2 christos && !s->hit) 2473 1.1 christos s->session->session_id_length = 0; 2474 1.1 christos 2475 1.1 christos if (usetls13) { 2476 1.1 christos sl = s->tmp_session_id_len; 2477 1.1 christos session_id = s->tmp_session_id; 2478 1.1 christos } else { 2479 1.1 christos sl = s->session->session_id_length; 2480 1.1 christos session_id = s->session->session_id; 2481 1.1 christos } 2482 1.1 christos 2483 1.1 christos if (sl > sizeof(s->session->session_id)) { 2484 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2485 1.1 christos return CON_FUNC_ERROR; 2486 1.1 christos } 2487 1.1 christos 2488 1.1 christos /* set up the compression method */ 2489 1.1 christos #ifdef OPENSSL_NO_COMP 2490 1.1 christos compm = 0; 2491 1.1 christos #else 2492 1.1 christos if (usetls13 || s->s3.tmp.new_compression == NULL) 2493 1.1 christos compm = 0; 2494 1.1 christos else 2495 1.1 christos compm = s->s3.tmp.new_compression->id; 2496 1.1 christos #endif 2497 1.1 christos 2498 1.1 christos if (!WPACKET_sub_memcpy_u8(pkt, session_id, sl) 2499 1.1.1.2 christos || !SSL_CONNECTION_GET_SSL(s)->method->put_cipher_by_char(s->s3.tmp.new_cipher, 2500 1.1.1.2 christos pkt, &len) 2501 1.1.1.2 christos || !WPACKET_put_bytes_u8(pkt, compm)) { 2502 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2503 1.1 christos return CON_FUNC_ERROR; 2504 1.1 christos } 2505 1.1 christos 2506 1.1 christos if (!tls_construct_extensions(s, pkt, 2507 1.1.1.2 christos s->hello_retry_request == SSL_HRR_PENDING 2508 1.1.1.2 christos ? SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST 2509 1.1.1.2 christos : (SSL_CONNECTION_IS_TLS13(s) 2510 1.1.1.2 christos ? SSL_EXT_TLS1_3_SERVER_HELLO 2511 1.1.1.2 christos : SSL_EXT_TLS1_2_SERVER_HELLO), 2512 1.1.1.2 christos NULL, 0)) { 2513 1.1 christos /* SSLfatal() already called */ 2514 1.1 christos return CON_FUNC_ERROR; 2515 1.1 christos } 2516 1.1 christos 2517 1.1 christos if (s->hello_retry_request == SSL_HRR_PENDING) { 2518 1.1 christos /* Ditch the session. We'll create a new one next time around */ 2519 1.1 christos SSL_SESSION_free(s->session); 2520 1.1 christos s->session = NULL; 2521 1.1 christos s->hit = 0; 2522 1.1 christos 2523 1.1 christos /* 2524 1.1 christos * Re-initialise the Transcript Hash. We're going to prepopulate it with 2525 1.1 christos * a synthetic message_hash in place of ClientHello1. 2526 1.1 christos */ 2527 1.1 christos if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { 2528 1.1 christos /* SSLfatal() already called */ 2529 1.1 christos return CON_FUNC_ERROR; 2530 1.1 christos } 2531 1.1 christos } else if (!(s->verify_mode & SSL_VERIFY_PEER) 2532 1.1.1.2 christos && !ssl3_digest_cached_records(s, 0)) { 2533 1.1 christos /* SSLfatal() already called */; 2534 1.1 christos return CON_FUNC_ERROR; 2535 1.1 christos } 2536 1.1 christos 2537 1.1 christos return CON_FUNC_SUCCESS; 2538 1.1 christos } 2539 1.1 christos 2540 1.1 christos CON_FUNC_RETURN tls_construct_server_done(SSL_CONNECTION *s, WPACKET *pkt) 2541 1.1 christos { 2542 1.1 christos if (!s->s3.tmp.cert_request) { 2543 1.1 christos if (!ssl3_digest_cached_records(s, 0)) { 2544 1.1 christos /* SSLfatal() already called */ 2545 1.1 christos return CON_FUNC_ERROR; 2546 1.1 christos } 2547 1.1 christos } 2548 1.1 christos return CON_FUNC_SUCCESS; 2549 1.1 christos } 2550 1.1 christos 2551 1.1 christos CON_FUNC_RETURN tls_construct_server_key_exchange(SSL_CONNECTION *s, 2552 1.1.1.2 christos WPACKET *pkt) 2553 1.1 christos { 2554 1.1 christos EVP_PKEY *pkdh = NULL; 2555 1.1 christos unsigned char *encodedPoint = NULL; 2556 1.1 christos size_t encodedlen = 0; 2557 1.1 christos int curve_id = 0; 2558 1.1 christos const SIGALG_LOOKUP *lu = s->s3.tmp.sigalg; 2559 1.1 christos int i; 2560 1.1 christos unsigned long type; 2561 1.1 christos BIGNUM *r[4]; 2562 1.1 christos EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); 2563 1.1 christos EVP_PKEY_CTX *pctx = NULL; 2564 1.1 christos size_t paramlen, paramoffset; 2565 1.1 christos int freer = 0; 2566 1.1 christos CON_FUNC_RETURN ret = CON_FUNC_ERROR; 2567 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 2568 1.1 christos 2569 1.1 christos if (!WPACKET_get_total_written(pkt, ¶moffset)) { 2570 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2571 1.1 christos goto err; 2572 1.1 christos } 2573 1.1 christos 2574 1.1 christos if (md_ctx == NULL) { 2575 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 2576 1.1 christos goto err; 2577 1.1 christos } 2578 1.1 christos 2579 1.1 christos type = s->s3.tmp.new_cipher->algorithm_mkey; 2580 1.1 christos 2581 1.1 christos r[0] = r[1] = r[2] = r[3] = NULL; 2582 1.1 christos #ifndef OPENSSL_NO_PSK 2583 1.1 christos /* Plain PSK or RSAPSK nothing to do */ 2584 1.1 christos if (type & (SSL_kPSK | SSL_kRSAPSK)) { 2585 1.1 christos } else 2586 1.1.1.2 christos #endif /* !OPENSSL_NO_PSK */ 2587 1.1.1.2 christos if (type & (SSL_kDHE | SSL_kDHEPSK)) { 2588 1.1.1.2 christos CERT *cert = s->cert; 2589 1.1.1.2 christos EVP_PKEY *pkdhp = NULL; 2590 1.1.1.2 christos 2591 1.1.1.2 christos if (s->cert->dh_tmp_auto) { 2592 1.1.1.2 christos pkdh = ssl_get_auto_dh(s); 2593 1.1.1.2 christos if (pkdh == NULL) { 2594 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2595 1.1.1.2 christos goto err; 2596 1.1.1.2 christos } 2597 1.1.1.2 christos pkdhp = pkdh; 2598 1.1.1.2 christos } else { 2599 1.1.1.2 christos pkdhp = cert->dh_tmp; 2600 1.1 christos } 2601 1.1 christos #if !defined(OPENSSL_NO_DEPRECATED_3_0) 2602 1.1.1.2 christos if ((pkdhp == NULL) && (s->cert->dh_tmp_cb != NULL)) { 2603 1.1.1.2 christos pkdh = ssl_dh_to_pkey(s->cert->dh_tmp_cb(SSL_CONNECTION_GET_USER_SSL(s), 2604 1.1.1.2 christos 0, 1024)); 2605 1.1.1.2 christos if (pkdh == NULL) { 2606 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2607 1.1.1.2 christos goto err; 2608 1.1.1.2 christos } 2609 1.1.1.2 christos pkdhp = pkdh; 2610 1.1.1.2 christos } 2611 1.1.1.2 christos #endif 2612 1.1.1.2 christos if (pkdhp == NULL) { 2613 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); 2614 1.1.1.2 christos goto err; 2615 1.1.1.2 christos } 2616 1.1.1.2 christos if (!ssl_security(s, SSL_SECOP_TMP_DH, 2617 1.1.1.2 christos EVP_PKEY_get_security_bits(pkdhp), 0, pkdhp)) { 2618 1.1.1.2 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL); 2619 1.1.1.2 christos goto err; 2620 1.1.1.2 christos } 2621 1.1.1.2 christos if (s->s3.tmp.pkey != NULL) { 2622 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2623 1.1 christos goto err; 2624 1.1 christos } 2625 1.1 christos 2626 1.1.1.2 christos s->s3.tmp.pkey = ssl_generate_pkey(s, pkdhp); 2627 1.1.1.2 christos if (s->s3.tmp.pkey == NULL) { 2628 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2629 1.1.1.2 christos goto err; 2630 1.1.1.2 christos } 2631 1.1 christos 2632 1.1.1.2 christos EVP_PKEY_free(pkdh); 2633 1.1.1.2 christos pkdh = NULL; 2634 1.1 christos 2635 1.1.1.2 christos /* These BIGNUMs need to be freed when we're finished */ 2636 1.1.1.2 christos freer = 1; 2637 1.1.1.2 christos if (!EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_P, 2638 1.1.1.2 christos &r[0]) 2639 1.1 christos || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, OSSL_PKEY_PARAM_FFC_G, 2640 1.1.1.2 christos &r[1]) 2641 1.1 christos || !EVP_PKEY_get_bn_param(s->s3.tmp.pkey, 2642 1.1.1.2 christos OSSL_PKEY_PARAM_PUB_KEY, &r[2])) { 2643 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2644 1.1.1.2 christos goto err; 2645 1.1.1.2 christos } 2646 1.1.1.2 christos } else if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { 2647 1.1 christos 2648 1.1.1.2 christos if (s->s3.tmp.pkey != NULL) { 2649 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2650 1.1.1.2 christos goto err; 2651 1.1.1.2 christos } 2652 1.1 christos 2653 1.1.1.2 christos /* Get NID of appropriate shared curve */ 2654 1.1.1.2 christos curve_id = tls1_shared_group(s, -2); 2655 1.1.1.2 christos if (curve_id == 0) { 2656 1.1.1.2 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 2657 1.1.1.2 christos SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); 2658 1.1.1.2 christos goto err; 2659 1.1.1.2 christos } 2660 1.1.1.2 christos /* Cache the group used in the SSL_SESSION */ 2661 1.1.1.2 christos s->session->kex_group = curve_id; 2662 1.1.1.2 christos /* Generate a new key for this curve */ 2663 1.1.1.2 christos s->s3.tmp.pkey = ssl_generate_pkey_group(s, curve_id); 2664 1.1.1.2 christos if (s->s3.tmp.pkey == NULL) { 2665 1.1.1.2 christos /* SSLfatal() already called */ 2666 1.1.1.2 christos goto err; 2667 1.1.1.2 christos } 2668 1.1 christos 2669 1.1.1.2 christos /* Encode the public key. */ 2670 1.1.1.2 christos encodedlen = EVP_PKEY_get1_encoded_public_key(s->s3.tmp.pkey, 2671 1.1.1.2 christos &encodedPoint); 2672 1.1.1.2 christos if (encodedlen == 0) { 2673 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); 2674 1.1.1.2 christos goto err; 2675 1.1.1.2 christos } 2676 1.1 christos 2677 1.1.1.2 christos /* 2678 1.1.1.2 christos * We'll generate the serverKeyExchange message explicitly so we 2679 1.1.1.2 christos * can set these to NULLs 2680 1.1.1.2 christos */ 2681 1.1.1.2 christos r[0] = NULL; 2682 1.1.1.2 christos r[1] = NULL; 2683 1.1.1.2 christos r[2] = NULL; 2684 1.1.1.2 christos r[3] = NULL; 2685 1.1.1.2 christos } else 2686 1.1 christos #ifndef OPENSSL_NO_SRP 2687 1.1.1.2 christos if (type & SSL_kSRP) { 2688 1.1.1.2 christos if ((s->srp_ctx.N == NULL) || (s->srp_ctx.g == NULL) || (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) { 2689 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_SRP_PARAM); 2690 1.1.1.2 christos goto err; 2691 1.1.1.2 christos } 2692 1.1.1.2 christos r[0] = s->srp_ctx.N; 2693 1.1.1.2 christos r[1] = s->srp_ctx.g; 2694 1.1.1.2 christos r[2] = s->srp_ctx.s; 2695 1.1.1.2 christos r[3] = s->srp_ctx.B; 2696 1.1.1.2 christos } else 2697 1.1.1.2 christos #endif 2698 1.1.1.2 christos { 2699 1.1.1.2 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); 2700 1.1 christos goto err; 2701 1.1 christos } 2702 1.1 christos 2703 1.1 christos if (((s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) != 0) 2704 1.1 christos || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK)) != 0) { 2705 1.1 christos lu = NULL; 2706 1.1 christos } else if (lu == NULL) { 2707 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR); 2708 1.1 christos goto err; 2709 1.1 christos } 2710 1.1 christos 2711 1.1 christos #ifndef OPENSSL_NO_PSK 2712 1.1 christos if (type & SSL_PSK) { 2713 1.1 christos size_t len = (s->cert->psk_identity_hint == NULL) 2714 1.1.1.2 christos ? 0 2715 1.1.1.2 christos : strlen(s->cert->psk_identity_hint); 2716 1.1 christos 2717 1.1 christos /* 2718 1.1 christos * It should not happen that len > PSK_MAX_IDENTITY_LEN - we already 2719 1.1 christos * checked this when we set the identity hint - but just in case 2720 1.1 christos */ 2721 1.1 christos if (len > PSK_MAX_IDENTITY_LEN 2722 1.1.1.2 christos || !WPACKET_sub_memcpy_u16(pkt, s->cert->psk_identity_hint, 2723 1.1.1.2 christos len)) { 2724 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2725 1.1 christos goto err; 2726 1.1 christos } 2727 1.1 christos } 2728 1.1 christos #endif 2729 1.1 christos 2730 1.1 christos for (i = 0; i < 4 && r[i] != NULL; i++) { 2731 1.1 christos unsigned char *binval; 2732 1.1 christos int res; 2733 1.1 christos 2734 1.1 christos #ifndef OPENSSL_NO_SRP 2735 1.1 christos if ((i == 2) && (type & SSL_kSRP)) { 2736 1.1 christos res = WPACKET_start_sub_packet_u8(pkt); 2737 1.1 christos } else 2738 1.1 christos #endif 2739 1.1 christos res = WPACKET_start_sub_packet_u16(pkt); 2740 1.1 christos 2741 1.1 christos if (!res) { 2742 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2743 1.1 christos goto err; 2744 1.1 christos } 2745 1.1 christos 2746 1.1 christos /*- 2747 1.1 christos * for interoperability with some versions of the Microsoft TLS 2748 1.1 christos * stack, we need to zero pad the DHE pub key to the same length 2749 1.1 christos * as the prime 2750 1.1 christos */ 2751 1.1 christos if ((i == 2) && (type & (SSL_kDHE | SSL_kDHEPSK))) { 2752 1.1 christos size_t len = BN_num_bytes(r[0]) - BN_num_bytes(r[2]); 2753 1.1 christos 2754 1.1 christos if (len > 0) { 2755 1.1 christos if (!WPACKET_allocate_bytes(pkt, len, &binval)) { 2756 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2757 1.1 christos goto err; 2758 1.1 christos } 2759 1.1 christos memset(binval, 0, len); 2760 1.1 christos } 2761 1.1 christos } 2762 1.1 christos 2763 1.1 christos if (!WPACKET_allocate_bytes(pkt, BN_num_bytes(r[i]), &binval) 2764 1.1.1.2 christos || !WPACKET_close(pkt)) { 2765 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2766 1.1 christos goto err; 2767 1.1 christos } 2768 1.1 christos 2769 1.1 christos BN_bn2bin(r[i], binval); 2770 1.1 christos } 2771 1.1 christos 2772 1.1 christos if (type & (SSL_kECDHE | SSL_kECDHEPSK)) { 2773 1.1 christos /* 2774 1.1 christos * We only support named (not generic) curves. In this situation, the 2775 1.1 christos * ServerKeyExchange message has: [1 byte CurveType], [2 byte CurveName] 2776 1.1 christos * [1 byte length of encoded point], followed by the actual encoded 2777 1.1 christos * point itself 2778 1.1 christos */ 2779 1.1 christos if (!WPACKET_put_bytes_u8(pkt, NAMED_CURVE_TYPE) 2780 1.1.1.2 christos || !WPACKET_put_bytes_u8(pkt, 0) 2781 1.1.1.2 christos || !WPACKET_put_bytes_u8(pkt, curve_id) 2782 1.1.1.2 christos || !WPACKET_sub_memcpy_u8(pkt, encodedPoint, encodedlen)) { 2783 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2784 1.1 christos goto err; 2785 1.1 christos } 2786 1.1 christos OPENSSL_free(encodedPoint); 2787 1.1 christos encodedPoint = NULL; 2788 1.1 christos } 2789 1.1 christos 2790 1.1 christos /* not anonymous */ 2791 1.1 christos if (lu != NULL) { 2792 1.1 christos EVP_PKEY *pkey = s->s3.tmp.cert->privatekey; 2793 1.1 christos const EVP_MD *md; 2794 1.1 christos unsigned char *sigbytes1, *sigbytes2, *tbs; 2795 1.1 christos size_t siglen = 0, tbslen; 2796 1.1 christos 2797 1.1 christos if (pkey == NULL || !tls1_lookup_md(sctx, lu, &md)) { 2798 1.1 christos /* Should never happen */ 2799 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2800 1.1 christos goto err; 2801 1.1 christos } 2802 1.1 christos /* Get length of the parameters we have written above */ 2803 1.1 christos if (!WPACKET_get_length(pkt, ¶mlen)) { 2804 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2805 1.1 christos goto err; 2806 1.1 christos } 2807 1.1 christos /* send signature algorithm */ 2808 1.1 christos if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) { 2809 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2810 1.1 christos goto err; 2811 1.1 christos } 2812 1.1 christos 2813 1.1 christos if (EVP_DigestSignInit_ex(md_ctx, &pctx, 2814 1.1.1.2 christos md == NULL ? NULL : EVP_MD_get0_name(md), 2815 1.1.1.2 christos sctx->libctx, sctx->propq, pkey, 2816 1.1.1.2 christos NULL) 2817 1.1.1.2 christos <= 0) { 2818 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2819 1.1 christos goto err; 2820 1.1 christos } 2821 1.1 christos if (lu->sig == EVP_PKEY_RSA_PSS) { 2822 1.1 christos if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 2823 1.1 christos || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, RSA_PSS_SALTLEN_DIGEST) <= 0) { 2824 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 2825 1.1 christos goto err; 2826 1.1 christos } 2827 1.1 christos } 2828 1.1 christos tbslen = construct_key_exchange_tbs(s, &tbs, 2829 1.1.1.2 christos s->init_buf->data + paramoffset, 2830 1.1.1.2 christos paramlen); 2831 1.1 christos if (tbslen == 0) { 2832 1.1 christos /* SSLfatal() already called */ 2833 1.1 christos goto err; 2834 1.1 christos } 2835 1.1 christos 2836 1.1.1.2 christos if (EVP_DigestSign(md_ctx, NULL, &siglen, tbs, tbslen) <= 0 2837 1.1.1.2 christos || !WPACKET_sub_reserve_bytes_u16(pkt, siglen, &sigbytes1) 2838 1.1.1.2 christos || EVP_DigestSign(md_ctx, sigbytes1, &siglen, tbs, tbslen) <= 0 2839 1.1.1.2 christos || !WPACKET_sub_allocate_bytes_u16(pkt, siglen, &sigbytes2) 2840 1.1.1.2 christos || sigbytes1 != sigbytes2) { 2841 1.1 christos OPENSSL_free(tbs); 2842 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2843 1.1 christos goto err; 2844 1.1 christos } 2845 1.1 christos OPENSSL_free(tbs); 2846 1.1 christos } 2847 1.1 christos 2848 1.1 christos ret = CON_FUNC_SUCCESS; 2849 1.1.1.2 christos err: 2850 1.1 christos EVP_PKEY_free(pkdh); 2851 1.1 christos OPENSSL_free(encodedPoint); 2852 1.1 christos EVP_MD_CTX_free(md_ctx); 2853 1.1 christos if (freer) { 2854 1.1 christos BN_free(r[0]); 2855 1.1 christos BN_free(r[1]); 2856 1.1 christos BN_free(r[2]); 2857 1.1 christos BN_free(r[3]); 2858 1.1 christos } 2859 1.1 christos return ret; 2860 1.1 christos } 2861 1.1 christos 2862 1.1 christos CON_FUNC_RETURN tls_construct_certificate_request(SSL_CONNECTION *s, 2863 1.1.1.2 christos WPACKET *pkt) 2864 1.1 christos { 2865 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 2866 1.1 christos /* Send random context when doing post-handshake auth */ 2867 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUEST_PENDING) { 2868 1.1 christos OPENSSL_free(s->pha_context); 2869 1.1 christos s->pha_context_len = 32; 2870 1.1 christos if ((s->pha_context = OPENSSL_malloc(s->pha_context_len)) == NULL) { 2871 1.1 christos s->pha_context_len = 0; 2872 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2873 1.1 christos return CON_FUNC_ERROR; 2874 1.1 christos } 2875 1.1 christos if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, 2876 1.1.1.2 christos s->pha_context, s->pha_context_len, 0) 2877 1.1.1.2 christos <= 0 2878 1.1.1.2 christos || !WPACKET_sub_memcpy_u8(pkt, s->pha_context, 2879 1.1.1.2 christos s->pha_context_len)) { 2880 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2881 1.1 christos return CON_FUNC_ERROR; 2882 1.1 christos } 2883 1.1 christos /* reset the handshake hash back to just after the ClientFinished */ 2884 1.1 christos if (!tls13_restore_handshake_digest_for_pha(s)) { 2885 1.1 christos /* SSLfatal() already called */ 2886 1.1 christos return CON_FUNC_ERROR; 2887 1.1 christos } 2888 1.1 christos } else { 2889 1.1 christos if (!WPACKET_put_bytes_u8(pkt, 0)) { 2890 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2891 1.1 christos return CON_FUNC_ERROR; 2892 1.1 christos } 2893 1.1 christos } 2894 1.1 christos 2895 1.1 christos if (!tls_construct_extensions(s, pkt, 2896 1.1.1.2 christos SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, NULL, 2897 1.1.1.2 christos 0)) { 2898 1.1 christos /* SSLfatal() already called */ 2899 1.1 christos return CON_FUNC_ERROR; 2900 1.1 christos } 2901 1.1 christos goto done; 2902 1.1 christos } 2903 1.1 christos 2904 1.1 christos /* get the list of acceptable cert types */ 2905 1.1 christos if (!WPACKET_start_sub_packet_u8(pkt) 2906 1.1 christos || !ssl3_get_req_cert_type(s, pkt) || !WPACKET_close(pkt)) { 2907 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2908 1.1 christos return CON_FUNC_ERROR; 2909 1.1 christos } 2910 1.1 christos 2911 1.1 christos if (SSL_USE_SIGALGS(s)) { 2912 1.1 christos const uint16_t *psigs; 2913 1.1 christos size_t nl = tls12_get_psigalgs(s, 1, &psigs); 2914 1.1 christos 2915 1.1 christos if (!WPACKET_start_sub_packet_u16(pkt) 2916 1.1.1.2 christos || !WPACKET_set_flags(pkt, WPACKET_FLAGS_NON_ZERO_LENGTH) 2917 1.1.1.2 christos || !tls12_copy_sigalgs(s, pkt, psigs, nl) 2918 1.1.1.2 christos || !WPACKET_close(pkt)) { 2919 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2920 1.1 christos return CON_FUNC_ERROR; 2921 1.1 christos } 2922 1.1 christos } 2923 1.1 christos 2924 1.1 christos if (!construct_ca_names(s, get_ca_names(s), pkt)) { 2925 1.1 christos /* SSLfatal() already called */ 2926 1.1 christos return CON_FUNC_ERROR; 2927 1.1 christos } 2928 1.1 christos 2929 1.1.1.2 christos done: 2930 1.1 christos s->certreqs_sent++; 2931 1.1 christos s->s3.tmp.cert_request = 1; 2932 1.1 christos return CON_FUNC_SUCCESS; 2933 1.1 christos } 2934 1.1 christos 2935 1.1 christos static int tls_process_cke_psk_preamble(SSL_CONNECTION *s, PACKET *pkt) 2936 1.1 christos { 2937 1.1 christos #ifndef OPENSSL_NO_PSK 2938 1.1 christos unsigned char psk[PSK_MAX_PSK_LEN]; 2939 1.1 christos size_t psklen; 2940 1.1 christos PACKET psk_identity; 2941 1.1 christos 2942 1.1 christos if (!PACKET_get_length_prefixed_2(pkt, &psk_identity)) { 2943 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 2944 1.1 christos return 0; 2945 1.1 christos } 2946 1.1 christos if (PACKET_remaining(&psk_identity) > PSK_MAX_IDENTITY_LEN) { 2947 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DATA_LENGTH_TOO_LONG); 2948 1.1 christos return 0; 2949 1.1 christos } 2950 1.1 christos if (s->psk_server_callback == NULL) { 2951 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_SERVER_CB); 2952 1.1 christos return 0; 2953 1.1 christos } 2954 1.1 christos 2955 1.1 christos if (!PACKET_strndup(&psk_identity, &s->session->psk_identity)) { 2956 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2957 1.1 christos return 0; 2958 1.1 christos } 2959 1.1 christos 2960 1.1 christos psklen = s->psk_server_callback(SSL_CONNECTION_GET_USER_SSL(s), 2961 1.1.1.2 christos s->session->psk_identity, 2962 1.1.1.2 christos psk, sizeof(psk)); 2963 1.1 christos 2964 1.1 christos if (psklen > PSK_MAX_PSK_LEN) { 2965 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2966 1.1 christos return 0; 2967 1.1 christos } else if (psklen == 0) { 2968 1.1 christos /* 2969 1.1 christos * PSK related to the given identity not found 2970 1.1 christos */ 2971 1.1 christos SSLfatal(s, SSL_AD_UNKNOWN_PSK_IDENTITY, SSL_R_PSK_IDENTITY_NOT_FOUND); 2972 1.1 christos return 0; 2973 1.1 christos } 2974 1.1 christos 2975 1.1 christos OPENSSL_free(s->s3.tmp.psk); 2976 1.1 christos s->s3.tmp.psk = OPENSSL_memdup(psk, psklen); 2977 1.1 christos OPENSSL_cleanse(psk, psklen); 2978 1.1 christos 2979 1.1 christos if (s->s3.tmp.psk == NULL) { 2980 1.1 christos s->s3.tmp.psklen = 0; 2981 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 2982 1.1 christos return 0; 2983 1.1 christos } 2984 1.1 christos 2985 1.1 christos s->s3.tmp.psklen = psklen; 2986 1.1 christos 2987 1.1 christos return 1; 2988 1.1 christos #else 2989 1.1 christos /* Should never happen */ 2990 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 2991 1.1 christos return 0; 2992 1.1 christos #endif 2993 1.1 christos } 2994 1.1 christos 2995 1.1 christos static int tls_process_cke_rsa(SSL_CONNECTION *s, PACKET *pkt) 2996 1.1 christos { 2997 1.1 christos size_t outlen; 2998 1.1 christos PACKET enc_premaster; 2999 1.1 christos EVP_PKEY *rsa = NULL; 3000 1.1 christos unsigned char *rsa_decrypt = NULL; 3001 1.1 christos int ret = 0; 3002 1.1 christos EVP_PKEY_CTX *ctx = NULL; 3003 1.1 christos OSSL_PARAM params[3], *p = params; 3004 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 3005 1.1 christos 3006 1.1 christos rsa = s->cert->pkeys[SSL_PKEY_RSA].privatekey; 3007 1.1 christos if (rsa == NULL) { 3008 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_RSA_CERTIFICATE); 3009 1.1 christos return 0; 3010 1.1 christos } 3011 1.1 christos 3012 1.1 christos /* SSLv3 and pre-standard DTLS omit the length bytes. */ 3013 1.1 christos if (s->version == SSL3_VERSION || s->version == DTLS1_BAD_VER) { 3014 1.1 christos enc_premaster = *pkt; 3015 1.1 christos } else { 3016 1.1 christos if (!PACKET_get_length_prefixed_2(pkt, &enc_premaster) 3017 1.1 christos || PACKET_remaining(pkt) != 0) { 3018 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 3019 1.1 christos return 0; 3020 1.1 christos } 3021 1.1 christos } 3022 1.1 christos 3023 1.1 christos outlen = SSL_MAX_MASTER_KEY_LENGTH; 3024 1.1 christos rsa_decrypt = OPENSSL_malloc(outlen); 3025 1.1 christos if (rsa_decrypt == NULL) { 3026 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 3027 1.1 christos return 0; 3028 1.1 christos } 3029 1.1 christos 3030 1.1 christos ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, rsa, sctx->propq); 3031 1.1 christos if (ctx == NULL) { 3032 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 3033 1.1 christos goto err; 3034 1.1 christos } 3035 1.1 christos 3036 1.1 christos /* 3037 1.1 christos * We must not leak whether a decryption failure occurs because of 3038 1.1 christos * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, 3039 1.1 christos * section 7.4.7.1). We use the special padding type 3040 1.1 christos * RSA_PKCS1_WITH_TLS_PADDING to do that. It will automatically decrypt the 3041 1.1 christos * RSA, check the padding and check that the client version is as expected 3042 1.1 christos * in the premaster secret. If any of that fails then the function appears 3043 1.1 christos * to return successfully but with a random result. The call below could 3044 1.1 christos * still fail if the input is publicly invalid. 3045 1.1 christos * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 3046 1.1 christos */ 3047 1.1 christos if (EVP_PKEY_decrypt_init(ctx) <= 0 3048 1.1.1.2 christos || EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_WITH_TLS_PADDING) <= 0) { 3049 1.1 christos SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); 3050 1.1 christos goto err; 3051 1.1 christos } 3052 1.1 christos 3053 1.1 christos *p++ = OSSL_PARAM_construct_uint(OSSL_ASYM_CIPHER_PARAM_TLS_CLIENT_VERSION, 3054 1.1.1.2 christos (unsigned int *)&s->client_version); 3055 1.1.1.2 christos if ((s->options & SSL_OP_TLS_ROLLBACK_BUG) != 0) 3056 1.1 christos *p++ = OSSL_PARAM_construct_uint( 3057 1.1 christos OSSL_ASYM_CIPHER_PARAM_TLS_NEGOTIATED_VERSION, 3058 1.1 christos (unsigned int *)&s->version); 3059 1.1 christos *p++ = OSSL_PARAM_construct_end(); 3060 1.1 christos 3061 1.1 christos if (!EVP_PKEY_CTX_set_params(ctx, params) 3062 1.1.1.2 christos || EVP_PKEY_decrypt(ctx, rsa_decrypt, &outlen, 3063 1.1.1.2 christos PACKET_data(&enc_premaster), 3064 1.1.1.2 christos PACKET_remaining(&enc_premaster)) 3065 1.1.1.2 christos <= 0) { 3066 1.1 christos SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); 3067 1.1 christos goto err; 3068 1.1 christos } 3069 1.1 christos 3070 1.1 christos /* 3071 1.1 christos * This test should never fail (otherwise we should have failed above) but 3072 1.1 christos * we double check anyway. 3073 1.1 christos */ 3074 1.1 christos if (outlen != SSL_MAX_MASTER_KEY_LENGTH) { 3075 1.1 christos OPENSSL_cleanse(rsa_decrypt, SSL_MAX_MASTER_KEY_LENGTH); 3076 1.1 christos SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_DECRYPTION_FAILED); 3077 1.1 christos goto err; 3078 1.1 christos } 3079 1.1 christos 3080 1.1 christos /* Also cleanses rsa_decrypt (on success or failure) */ 3081 1.1 christos if (!ssl_generate_master_secret(s, rsa_decrypt, outlen, 0)) { 3082 1.1 christos /* SSLfatal() already called */ 3083 1.1 christos goto err; 3084 1.1 christos } 3085 1.1 christos 3086 1.1 christos ret = 1; 3087 1.1.1.2 christos err: 3088 1.1 christos OPENSSL_free(rsa_decrypt); 3089 1.1 christos EVP_PKEY_CTX_free(ctx); 3090 1.1 christos return ret; 3091 1.1 christos } 3092 1.1 christos 3093 1.1 christos static int tls_process_cke_dhe(SSL_CONNECTION *s, PACKET *pkt) 3094 1.1 christos { 3095 1.1 christos EVP_PKEY *skey = NULL; 3096 1.1 christos unsigned int i; 3097 1.1 christos const unsigned char *data; 3098 1.1 christos EVP_PKEY *ckey = NULL; 3099 1.1 christos int ret = 0; 3100 1.1 christos 3101 1.1 christos if (!PACKET_get_net_2(pkt, &i) || PACKET_remaining(pkt) != i) { 3102 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); 3103 1.1 christos goto err; 3104 1.1 christos } 3105 1.1 christos skey = s->s3.tmp.pkey; 3106 1.1 christos if (skey == NULL) { 3107 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_DH_KEY); 3108 1.1 christos goto err; 3109 1.1 christos } 3110 1.1 christos 3111 1.1 christos if (PACKET_remaining(pkt) == 0L) { 3112 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_MISSING_TMP_DH_KEY); 3113 1.1 christos goto err; 3114 1.1 christos } 3115 1.1 christos if (!PACKET_get_bytes(pkt, &data, i)) { 3116 1.1 christos /* We already checked we have enough data */ 3117 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3118 1.1 christos goto err; 3119 1.1 christos } 3120 1.1 christos ckey = EVP_PKEY_new(); 3121 1.1 christos if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) == 0) { 3122 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); 3123 1.1 christos goto err; 3124 1.1 christos } 3125 1.1 christos 3126 1.1 christos if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { 3127 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); 3128 1.1 christos goto err; 3129 1.1 christos } 3130 1.1 christos 3131 1.1 christos if (ssl_derive(s, skey, ckey, 1) == 0) { 3132 1.1 christos /* SSLfatal() already called */ 3133 1.1 christos goto err; 3134 1.1 christos } 3135 1.1 christos 3136 1.1 christos ret = 1; 3137 1.1 christos EVP_PKEY_free(s->s3.tmp.pkey); 3138 1.1 christos s->s3.tmp.pkey = NULL; 3139 1.1.1.2 christos err: 3140 1.1 christos EVP_PKEY_free(ckey); 3141 1.1 christos return ret; 3142 1.1 christos } 3143 1.1 christos 3144 1.1 christos static int tls_process_cke_ecdhe(SSL_CONNECTION *s, PACKET *pkt) 3145 1.1 christos { 3146 1.1 christos EVP_PKEY *skey = s->s3.tmp.pkey; 3147 1.1 christos EVP_PKEY *ckey = NULL; 3148 1.1 christos int ret = 0; 3149 1.1 christos 3150 1.1 christos if (PACKET_remaining(pkt) == 0L) { 3151 1.1 christos /* We don't support ECDH client auth */ 3152 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_TMP_ECDH_KEY); 3153 1.1 christos goto err; 3154 1.1 christos } else { 3155 1.1 christos unsigned int i; 3156 1.1 christos const unsigned char *data; 3157 1.1 christos 3158 1.1 christos /* 3159 1.1 christos * Get client's public key from encoded point in the 3160 1.1 christos * ClientKeyExchange message. 3161 1.1 christos */ 3162 1.1 christos 3163 1.1 christos /* Get encoded point length */ 3164 1.1 christos if (!PACKET_get_1(pkt, &i) || !PACKET_get_bytes(pkt, &data, i) 3165 1.1 christos || PACKET_remaining(pkt) != 0) { 3166 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 3167 1.1 christos goto err; 3168 1.1 christos } 3169 1.1 christos if (skey == NULL) { 3170 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_MISSING_TMP_ECDH_KEY); 3171 1.1 christos goto err; 3172 1.1 christos } 3173 1.1 christos 3174 1.1 christos ckey = EVP_PKEY_new(); 3175 1.1 christos if (ckey == NULL || EVP_PKEY_copy_parameters(ckey, skey) <= 0) { 3176 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_COPY_PARAMETERS_FAILED); 3177 1.1 christos goto err; 3178 1.1 christos } 3179 1.1 christos 3180 1.1 christos if (EVP_PKEY_set1_encoded_public_key(ckey, data, i) <= 0) { 3181 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_KEY_SHARE); 3182 1.1 christos goto err; 3183 1.1 christos } 3184 1.1 christos } 3185 1.1 christos 3186 1.1 christos if (ssl_derive(s, skey, ckey, 1) == 0) { 3187 1.1 christos /* SSLfatal() already called */ 3188 1.1 christos goto err; 3189 1.1 christos } 3190 1.1 christos 3191 1.1 christos ret = 1; 3192 1.1 christos EVP_PKEY_free(s->s3.tmp.pkey); 3193 1.1 christos s->s3.tmp.pkey = NULL; 3194 1.1.1.2 christos err: 3195 1.1 christos EVP_PKEY_free(ckey); 3196 1.1 christos 3197 1.1 christos return ret; 3198 1.1 christos } 3199 1.1 christos 3200 1.1 christos static int tls_process_cke_srp(SSL_CONNECTION *s, PACKET *pkt) 3201 1.1 christos { 3202 1.1 christos #ifndef OPENSSL_NO_SRP 3203 1.1 christos unsigned int i; 3204 1.1 christos const unsigned char *data; 3205 1.1 christos 3206 1.1 christos if (!PACKET_get_net_2(pkt, &i) 3207 1.1 christos || !PACKET_get_bytes(pkt, &data, i)) { 3208 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_SRP_A_LENGTH); 3209 1.1 christos return 0; 3210 1.1 christos } 3211 1.1 christos if ((s->srp_ctx.A = BN_bin2bn(data, i, NULL)) == NULL) { 3212 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); 3213 1.1 christos return 0; 3214 1.1 christos } 3215 1.1 christos if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0 || BN_is_zero(s->srp_ctx.A)) { 3216 1.1 christos SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_SRP_PARAMETERS); 3217 1.1 christos return 0; 3218 1.1 christos } 3219 1.1 christos OPENSSL_free(s->session->srp_username); 3220 1.1 christos s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); 3221 1.1 christos if (s->session->srp_username == NULL) { 3222 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 3223 1.1 christos return 0; 3224 1.1 christos } 3225 1.1 christos 3226 1.1 christos if (!srp_generate_server_master_secret(s)) { 3227 1.1 christos /* SSLfatal() already called */ 3228 1.1 christos return 0; 3229 1.1 christos } 3230 1.1 christos 3231 1.1 christos return 1; 3232 1.1 christos #else 3233 1.1 christos /* Should never happen */ 3234 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3235 1.1 christos return 0; 3236 1.1 christos #endif 3237 1.1 christos } 3238 1.1 christos 3239 1.1 christos static int tls_process_cke_gost(SSL_CONNECTION *s, PACKET *pkt) 3240 1.1 christos { 3241 1.1 christos #ifndef OPENSSL_NO_GOST 3242 1.1 christos EVP_PKEY_CTX *pkey_ctx; 3243 1.1 christos EVP_PKEY *client_pub_pkey = NULL, *pk = NULL; 3244 1.1 christos unsigned char premaster_secret[32]; 3245 1.1 christos const unsigned char *start; 3246 1.1 christos size_t outlen = sizeof(premaster_secret), inlen; 3247 1.1 christos unsigned long alg_a; 3248 1.1 christos GOST_KX_MESSAGE *pKX = NULL; 3249 1.1 christos const unsigned char *ptr; 3250 1.1 christos int ret = 0; 3251 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 3252 1.1 christos 3253 1.1 christos /* Get our certificate private key */ 3254 1.1 christos alg_a = s->s3.tmp.new_cipher->algorithm_auth; 3255 1.1 christos if (alg_a & SSL_aGOST12) { 3256 1.1 christos /* 3257 1.1 christos * New GOST ciphersuites have SSL_aGOST01 bit too 3258 1.1 christos */ 3259 1.1 christos pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey; 3260 1.1 christos if (pk == NULL) { 3261 1.1 christos pk = s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; 3262 1.1 christos } 3263 1.1 christos if (pk == NULL) { 3264 1.1 christos pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; 3265 1.1 christos } 3266 1.1 christos } else if (alg_a & SSL_aGOST01) { 3267 1.1 christos pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey; 3268 1.1 christos } 3269 1.1 christos 3270 1.1 christos pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); 3271 1.1 christos if (pkey_ctx == NULL) { 3272 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 3273 1.1 christos return 0; 3274 1.1 christos } 3275 1.1 christos if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { 3276 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3277 1.1 christos goto err; 3278 1.1 christos } 3279 1.1 christos /* 3280 1.1 christos * If client certificate is present and is of the same type, maybe 3281 1.1 christos * use it for key exchange. Don't mind errors from 3282 1.1 christos * EVP_PKEY_derive_set_peer, because it is completely valid to use a 3283 1.1 christos * client certificate for authorization only. 3284 1.1 christos */ 3285 1.1 christos client_pub_pkey = tls_get_peer_pkey(s); 3286 1.1 christos if (client_pub_pkey) { 3287 1.1 christos if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0) 3288 1.1 christos ERR_clear_error(); 3289 1.1 christos } 3290 1.1 christos 3291 1.1 christos ptr = PACKET_data(pkt); 3292 1.1 christos /* Some implementations provide extra data in the opaqueBlob 3293 1.1 christos * We have nothing to do with this blob so we just skip it */ 3294 1.1 christos pKX = d2i_GOST_KX_MESSAGE(NULL, &ptr, PACKET_remaining(pkt)); 3295 1.1 christos if (pKX == NULL 3296 1.1.1.2 christos || pKX->kxBlob == NULL 3297 1.1.1.2 christos || ASN1_TYPE_get(pKX->kxBlob) != V_ASN1_SEQUENCE) { 3298 1.1.1.2 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); 3299 1.1.1.2 christos goto err; 3300 1.1 christos } 3301 1.1 christos 3302 1.1 christos if (!PACKET_forward(pkt, ptr - PACKET_data(pkt))) { 3303 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); 3304 1.1 christos goto err; 3305 1.1 christos } 3306 1.1 christos 3307 1.1 christos if (PACKET_remaining(pkt) != 0) { 3308 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_DECRYPTION_FAILED); 3309 1.1 christos goto err; 3310 1.1 christos } 3311 1.1 christos 3312 1.1 christos inlen = pKX->kxBlob->value.sequence->length; 3313 1.1 christos start = pKX->kxBlob->value.sequence->data; 3314 1.1 christos 3315 1.1 christos if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, 3316 1.1.1.2 christos inlen) 3317 1.1.1.2 christos <= 0) { 3318 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); 3319 1.1 christos goto err; 3320 1.1 christos } 3321 1.1 christos /* Generate master secret */ 3322 1.1 christos if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { 3323 1.1 christos /* SSLfatal() already called */ 3324 1.1 christos goto err; 3325 1.1 christos } 3326 1.1 christos /* Check if pubkey from client certificate was used */ 3327 1.1 christos if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, 3328 1.1.1.2 christos NULL) 3329 1.1.1.2 christos > 0) 3330 1.1 christos s->statem.no_cert_verify = 1; 3331 1.1 christos 3332 1.1 christos ret = 1; 3333 1.1.1.2 christos err: 3334 1.1 christos EVP_PKEY_CTX_free(pkey_ctx); 3335 1.1 christos GOST_KX_MESSAGE_free(pKX); 3336 1.1 christos return ret; 3337 1.1 christos #else 3338 1.1 christos /* Should never happen */ 3339 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3340 1.1 christos return 0; 3341 1.1 christos #endif 3342 1.1 christos } 3343 1.1 christos 3344 1.1 christos static int tls_process_cke_gost18(SSL_CONNECTION *s, PACKET *pkt) 3345 1.1 christos { 3346 1.1 christos #ifndef OPENSSL_NO_GOST 3347 1.1 christos unsigned char rnd_dgst[32]; 3348 1.1 christos EVP_PKEY_CTX *pkey_ctx = NULL; 3349 1.1 christos EVP_PKEY *pk = NULL; 3350 1.1 christos unsigned char premaster_secret[32]; 3351 1.1 christos const unsigned char *start = NULL; 3352 1.1 christos size_t outlen = sizeof(premaster_secret), inlen = 0; 3353 1.1 christos int ret = 0; 3354 1.1 christos int cipher_nid = ossl_gost18_cke_cipher_nid(s); 3355 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 3356 1.1 christos 3357 1.1 christos if (cipher_nid == NID_undef) { 3358 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3359 1.1 christos return 0; 3360 1.1 christos } 3361 1.1 christos 3362 1.1 christos if (ossl_gost_ukm(s, rnd_dgst) <= 0) { 3363 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3364 1.1 christos goto err; 3365 1.1 christos } 3366 1.1 christos 3367 1.1 christos /* Get our certificate private key */ 3368 1.1.1.2 christos pk = s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey != NULL ? s->cert->pkeys[SSL_PKEY_GOST12_512].privatekey : s->cert->pkeys[SSL_PKEY_GOST12_256].privatekey; 3369 1.1 christos if (pk == NULL) { 3370 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); 3371 1.1 christos goto err; 3372 1.1 christos } 3373 1.1 christos 3374 1.1 christos pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pk, sctx->propq); 3375 1.1 christos if (pkey_ctx == NULL) { 3376 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 3377 1.1 christos goto err; 3378 1.1 christos } 3379 1.1 christos if (EVP_PKEY_decrypt_init(pkey_ctx) <= 0) { 3380 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3381 1.1 christos goto err; 3382 1.1 christos } 3383 1.1 christos 3384 1.1 christos /* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code depending on size */ 3385 1.1 christos if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, 3386 1.1.1.2 christos EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) 3387 1.1.1.2 christos <= 0) { 3388 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); 3389 1.1 christos goto err; 3390 1.1 christos } 3391 1.1 christos 3392 1.1 christos if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_DECRYPT, 3393 1.1.1.2 christos EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) 3394 1.1.1.2 christos <= 0) { 3395 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); 3396 1.1 christos goto err; 3397 1.1 christos } 3398 1.1 christos inlen = PACKET_remaining(pkt); 3399 1.1 christos start = PACKET_data(pkt); 3400 1.1 christos 3401 1.1 christos if (EVP_PKEY_decrypt(pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) { 3402 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_DECRYPTION_FAILED); 3403 1.1 christos goto err; 3404 1.1 christos } 3405 1.1 christos /* Generate master secret */ 3406 1.1 christos if (!ssl_generate_master_secret(s, premaster_secret, outlen, 0)) { 3407 1.1.1.2 christos /* SSLfatal() already called */ 3408 1.1.1.2 christos goto err; 3409 1.1 christos } 3410 1.1 christos ret = 1; 3411 1.1 christos 3412 1.1.1.2 christos err: 3413 1.1 christos EVP_PKEY_CTX_free(pkey_ctx); 3414 1.1 christos return ret; 3415 1.1 christos #else 3416 1.1 christos /* Should never happen */ 3417 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3418 1.1 christos return 0; 3419 1.1 christos #endif 3420 1.1 christos } 3421 1.1 christos 3422 1.1 christos MSG_PROCESS_RETURN tls_process_client_key_exchange(SSL_CONNECTION *s, 3423 1.1.1.2 christos PACKET *pkt) 3424 1.1 christos { 3425 1.1 christos unsigned long alg_k; 3426 1.1 christos 3427 1.1 christos alg_k = s->s3.tmp.new_cipher->algorithm_mkey; 3428 1.1 christos 3429 1.1 christos /* For PSK parse and retrieve identity, obtain PSK key */ 3430 1.1 christos if ((alg_k & SSL_PSK) && !tls_process_cke_psk_preamble(s, pkt)) { 3431 1.1 christos /* SSLfatal() already called */ 3432 1.1 christos goto err; 3433 1.1 christos } 3434 1.1 christos 3435 1.1 christos if (alg_k & SSL_kPSK) { 3436 1.1 christos /* Identity extracted earlier: should be nothing left */ 3437 1.1 christos if (PACKET_remaining(pkt) != 0) { 3438 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 3439 1.1 christos goto err; 3440 1.1 christos } 3441 1.1 christos /* PSK handled by ssl_generate_master_secret */ 3442 1.1 christos if (!ssl_generate_master_secret(s, NULL, 0, 0)) { 3443 1.1 christos /* SSLfatal() already called */ 3444 1.1 christos goto err; 3445 1.1 christos } 3446 1.1 christos } else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { 3447 1.1 christos if (!tls_process_cke_rsa(s, pkt)) { 3448 1.1 christos /* SSLfatal() already called */ 3449 1.1 christos goto err; 3450 1.1 christos } 3451 1.1 christos } else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { 3452 1.1 christos if (!tls_process_cke_dhe(s, pkt)) { 3453 1.1 christos /* SSLfatal() already called */ 3454 1.1 christos goto err; 3455 1.1 christos } 3456 1.1 christos } else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { 3457 1.1 christos if (!tls_process_cke_ecdhe(s, pkt)) { 3458 1.1 christos /* SSLfatal() already called */ 3459 1.1 christos goto err; 3460 1.1 christos } 3461 1.1 christos } else if (alg_k & SSL_kSRP) { 3462 1.1 christos if (!tls_process_cke_srp(s, pkt)) { 3463 1.1 christos /* SSLfatal() already called */ 3464 1.1 christos goto err; 3465 1.1 christos } 3466 1.1 christos } else if (alg_k & SSL_kGOST) { 3467 1.1 christos if (!tls_process_cke_gost(s, pkt)) { 3468 1.1 christos /* SSLfatal() already called */ 3469 1.1 christos goto err; 3470 1.1 christos } 3471 1.1 christos } else if (alg_k & SSL_kGOST18) { 3472 1.1 christos if (!tls_process_cke_gost18(s, pkt)) { 3473 1.1 christos /* SSLfatal() already called */ 3474 1.1 christos goto err; 3475 1.1 christos } 3476 1.1 christos } else { 3477 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_UNKNOWN_CIPHER_TYPE); 3478 1.1 christos goto err; 3479 1.1 christos } 3480 1.1 christos 3481 1.1 christos return MSG_PROCESS_CONTINUE_PROCESSING; 3482 1.1.1.2 christos err: 3483 1.1 christos #ifndef OPENSSL_NO_PSK 3484 1.1 christos OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen); 3485 1.1 christos s->s3.tmp.psk = NULL; 3486 1.1 christos s->s3.tmp.psklen = 0; 3487 1.1 christos #endif 3488 1.1 christos return MSG_PROCESS_ERROR; 3489 1.1 christos } 3490 1.1 christos 3491 1.1 christos WORK_STATE tls_post_process_client_key_exchange(SSL_CONNECTION *s, 3492 1.1.1.2 christos WORK_STATE wst) 3493 1.1 christos { 3494 1.1 christos #ifndef OPENSSL_NO_SCTP 3495 1.1 christos if (wst == WORK_MORE_A) { 3496 1.1 christos if (SSL_CONNECTION_IS_DTLS(s)) { 3497 1.1 christos unsigned char sctpauthkey[64]; 3498 1.1 christos char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; 3499 1.1 christos size_t labellen; 3500 1.1 christos /* 3501 1.1 christos * Add new shared key for SCTP-Auth, will be ignored if no SCTP 3502 1.1 christos * used. 3503 1.1 christos */ 3504 1.1 christos memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, 3505 1.1.1.2 christos sizeof(DTLS1_SCTP_AUTH_LABEL)); 3506 1.1 christos 3507 1.1 christos /* Don't include the terminating zero. */ 3508 1.1 christos labellen = sizeof(labelbuffer) - 1; 3509 1.1 christos if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) 3510 1.1 christos labellen += 1; 3511 1.1 christos 3512 1.1 christos if (SSL_export_keying_material(SSL_CONNECTION_GET_SSL(s), 3513 1.1.1.2 christos sctpauthkey, 3514 1.1.1.2 christos sizeof(sctpauthkey), labelbuffer, 3515 1.1.1.2 christos labellen, NULL, 0, 3516 1.1.1.2 christos 0) 3517 1.1.1.2 christos <= 0) { 3518 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3519 1.1 christos return WORK_ERROR; 3520 1.1 christos } 3521 1.1 christos 3522 1.1 christos BIO_ctrl(s->wbio, BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, 3523 1.1.1.2 christos sizeof(sctpauthkey), sctpauthkey); 3524 1.1 christos } 3525 1.1 christos } 3526 1.1 christos #endif 3527 1.1 christos 3528 1.1 christos if (s->statem.no_cert_verify || !received_client_cert(s)) { 3529 1.1 christos /* 3530 1.1 christos * No certificate verify or no peer certificate so we no longer need 3531 1.1 christos * the handshake_buffer 3532 1.1 christos */ 3533 1.1 christos if (!ssl3_digest_cached_records(s, 0)) { 3534 1.1 christos /* SSLfatal() already called */ 3535 1.1 christos return WORK_ERROR; 3536 1.1 christos } 3537 1.1 christos return WORK_FINISHED_CONTINUE; 3538 1.1 christos } else { 3539 1.1 christos if (!s->s3.handshake_buffer) { 3540 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3541 1.1 christos return WORK_ERROR; 3542 1.1 christos } 3543 1.1 christos /* 3544 1.1 christos * For sigalgs freeze the handshake buffer. If we support 3545 1.1 christos * extms we've done this already so this is a no-op 3546 1.1 christos */ 3547 1.1 christos if (!ssl3_digest_cached_records(s, 1)) { 3548 1.1 christos /* SSLfatal() already called */ 3549 1.1 christos return WORK_ERROR; 3550 1.1 christos } 3551 1.1 christos } 3552 1.1 christos 3553 1.1 christos return WORK_FINISHED_CONTINUE; 3554 1.1 christos } 3555 1.1 christos 3556 1.1 christos MSG_PROCESS_RETURN tls_process_client_rpk(SSL_CONNECTION *sc, PACKET *pkt) 3557 1.1 christos { 3558 1.1 christos MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; 3559 1.1 christos SSL_SESSION *new_sess = NULL; 3560 1.1 christos EVP_PKEY *peer_rpk = NULL; 3561 1.1 christos 3562 1.1 christos if (!tls_process_rpk(sc, pkt, &peer_rpk)) { 3563 1.1 christos /* SSLfatal already called */ 3564 1.1 christos goto err; 3565 1.1 christos } 3566 1.1 christos 3567 1.1 christos if (peer_rpk == NULL) { 3568 1.1 christos if ((sc->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) 3569 1.1.1.2 christos && (sc->verify_mode & SSL_VERIFY_PEER)) { 3570 1.1 christos SSLfatal(sc, SSL_AD_CERTIFICATE_REQUIRED, 3571 1.1.1.2 christos SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 3572 1.1 christos goto err; 3573 1.1 christos } 3574 1.1 christos } else { 3575 1.1 christos if (ssl_verify_rpk(sc, peer_rpk) <= 0) { 3576 1.1 christos SSLfatal(sc, ssl_x509err2alert(sc->verify_result), 3577 1.1.1.2 christos SSL_R_CERTIFICATE_VERIFY_FAILED); 3578 1.1 christos goto err; 3579 1.1 christos } 3580 1.1 christos } 3581 1.1 christos 3582 1.1 christos /* 3583 1.1 christos * Sessions must be immutable once they go into the session cache. Otherwise 3584 1.1 christos * we can get multi-thread problems. Therefore we don't "update" sessions, 3585 1.1 christos * we replace them with a duplicate. Here, we need to do this every time 3586 1.1 christos * a new RPK (or certificate) is received via post-handshake authentication, 3587 1.1 christos * as the session may have already gone into the session cache. 3588 1.1 christos */ 3589 1.1 christos 3590 1.1 christos if (sc->post_handshake_auth == SSL_PHA_REQUESTED) { 3591 1.1 christos if ((new_sess = ssl_session_dup(sc->session, 0)) == NULL) { 3592 1.1 christos SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE); 3593 1.1 christos goto err; 3594 1.1 christos } 3595 1.1 christos 3596 1.1 christos SSL_SESSION_free(sc->session); 3597 1.1 christos sc->session = new_sess; 3598 1.1 christos } 3599 1.1 christos 3600 1.1 christos /* Ensure there is no peer/peer_chain */ 3601 1.1 christos X509_free(sc->session->peer); 3602 1.1 christos sc->session->peer = NULL; 3603 1.1 christos sk_X509_pop_free(sc->session->peer_chain, X509_free); 3604 1.1 christos sc->session->peer_chain = NULL; 3605 1.1 christos /* Save RPK */ 3606 1.1 christos EVP_PKEY_free(sc->session->peer_rpk); 3607 1.1 christos sc->session->peer_rpk = peer_rpk; 3608 1.1 christos peer_rpk = NULL; 3609 1.1 christos 3610 1.1 christos sc->session->verify_result = sc->verify_result; 3611 1.1 christos 3612 1.1 christos /* 3613 1.1 christos * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE 3614 1.1 christos * message 3615 1.1 christos */ 3616 1.1 christos if (SSL_CONNECTION_IS_TLS13(sc)) { 3617 1.1 christos if (!ssl3_digest_cached_records(sc, 1)) { 3618 1.1 christos /* SSLfatal() already called */ 3619 1.1 christos goto err; 3620 1.1 christos } 3621 1.1 christos 3622 1.1 christos /* Save the current hash state for when we receive the CertificateVerify */ 3623 1.1 christos if (!ssl_handshake_hash(sc, sc->cert_verify_hash, 3624 1.1.1.2 christos sizeof(sc->cert_verify_hash), 3625 1.1.1.2 christos &sc->cert_verify_hash_len)) { 3626 1.1 christos /* SSLfatal() already called */; 3627 1.1 christos goto err; 3628 1.1 christos } 3629 1.1 christos 3630 1.1 christos /* resend session tickets */ 3631 1.1 christos sc->sent_tickets = 0; 3632 1.1 christos } 3633 1.1 christos 3634 1.1 christos ret = MSG_PROCESS_CONTINUE_READING; 3635 1.1 christos 3636 1.1.1.2 christos err: 3637 1.1 christos EVP_PKEY_free(peer_rpk); 3638 1.1 christos return ret; 3639 1.1 christos } 3640 1.1 christos 3641 1.1 christos MSG_PROCESS_RETURN tls_process_client_certificate(SSL_CONNECTION *s, 3642 1.1.1.2 christos PACKET *pkt) 3643 1.1 christos { 3644 1.1 christos int i; 3645 1.1 christos MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; 3646 1.1 christos X509 *x = NULL; 3647 1.1 christos unsigned long l; 3648 1.1 christos const unsigned char *certstart, *certbytes; 3649 1.1 christos STACK_OF(X509) *sk = NULL; 3650 1.1 christos PACKET spkt, context; 3651 1.1 christos size_t chainidx; 3652 1.1 christos SSL_SESSION *new_sess = NULL; 3653 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 3654 1.1 christos 3655 1.1 christos /* 3656 1.1 christos * To get this far we must have read encrypted data from the client. We no 3657 1.1 christos * longer tolerate unencrypted alerts. This is ignored if less than TLSv1.3 3658 1.1 christos */ 3659 1.1 christos if (s->rlayer.rrlmethod->set_plain_alerts != NULL) 3660 1.1 christos s->rlayer.rrlmethod->set_plain_alerts(s->rlayer.rrl, 0); 3661 1.1 christos 3662 1.1 christos if (s->ext.client_cert_type == TLSEXT_cert_type_rpk) 3663 1.1 christos return tls_process_client_rpk(s, pkt); 3664 1.1 christos 3665 1.1 christos if (s->ext.client_cert_type != TLSEXT_cert_type_x509) { 3666 1.1 christos SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE, 3667 1.1.1.2 christos SSL_R_UNKNOWN_CERTIFICATE_TYPE); 3668 1.1 christos goto err; 3669 1.1 christos } 3670 1.1 christos 3671 1.1 christos if ((sk = sk_X509_new_null()) == NULL) { 3672 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 3673 1.1 christos goto err; 3674 1.1 christos } 3675 1.1 christos 3676 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) 3677 1.1 christos && (!PACKET_get_length_prefixed_1(pkt, &context) 3678 1.1.1.2 christos || (s->pha_context == NULL && PACKET_remaining(&context) != 0) 3679 1.1.1.2 christos || (s->pha_context != NULL 3680 1.1.1.2 christos && !PACKET_equal(&context, s->pha_context, 3681 1.1.1.2 christos s->pha_context_len)))) { 3682 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_INVALID_CONTEXT); 3683 1.1 christos goto err; 3684 1.1 christos } 3685 1.1 christos 3686 1.1 christos if (!PACKET_get_length_prefixed_3(pkt, &spkt) 3687 1.1.1.2 christos || PACKET_remaining(pkt) != 0) { 3688 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 3689 1.1 christos goto err; 3690 1.1 christos } 3691 1.1 christos 3692 1.1 christos for (chainidx = 0; PACKET_remaining(&spkt) > 0; chainidx++) { 3693 1.1 christos if (!PACKET_get_net_3(&spkt, &l) 3694 1.1 christos || !PACKET_get_bytes(&spkt, &certbytes, l)) { 3695 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); 3696 1.1 christos goto err; 3697 1.1 christos } 3698 1.1 christos 3699 1.1 christos certstart = certbytes; 3700 1.1 christos x = X509_new_ex(sctx->libctx, sctx->propq); 3701 1.1 christos if (x == NULL) { 3702 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_X509_LIB); 3703 1.1 christos goto err; 3704 1.1 christos } 3705 1.1 christos if (d2i_X509(&x, (const unsigned char **)&certbytes, l) == NULL) { 3706 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); 3707 1.1 christos goto err; 3708 1.1 christos } 3709 1.1 christos 3710 1.1 christos if (certbytes != (certstart + l)) { 3711 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); 3712 1.1 christos goto err; 3713 1.1 christos } 3714 1.1 christos 3715 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 3716 1.1 christos RAW_EXTENSION *rawexts = NULL; 3717 1.1 christos PACKET extensions; 3718 1.1 christos 3719 1.1 christos if (!PACKET_get_length_prefixed_2(&spkt, &extensions)) { 3720 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); 3721 1.1 christos goto err; 3722 1.1 christos } 3723 1.1 christos if (!tls_collect_extensions(s, &extensions, 3724 1.1.1.2 christos SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, 3725 1.1.1.2 christos NULL, chainidx == 0) 3726 1.1 christos || !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, 3727 1.1.1.2 christos rawexts, x, chainidx, 3728 1.1.1.2 christos PACKET_remaining(&spkt) == 0)) { 3729 1.1 christos OPENSSL_free(rawexts); 3730 1.1 christos goto err; 3731 1.1 christos } 3732 1.1 christos OPENSSL_free(rawexts); 3733 1.1 christos } 3734 1.1 christos 3735 1.1 christos if (!sk_X509_push(sk, x)) { 3736 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 3737 1.1 christos goto err; 3738 1.1 christos } 3739 1.1 christos x = NULL; 3740 1.1 christos } 3741 1.1 christos 3742 1.1 christos if (sk_X509_num(sk) <= 0) { 3743 1.1 christos /* TLS does not mind 0 certs returned */ 3744 1.1 christos if (s->version == SSL3_VERSION) { 3745 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 3746 1.1.1.2 christos SSL_R_NO_CERTIFICATES_RETURNED); 3747 1.1 christos goto err; 3748 1.1 christos } 3749 1.1 christos /* Fail for TLS only if we required a certificate */ 3750 1.1.1.2 christos else if ((s->verify_mode & SSL_VERIFY_PEER) && (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { 3751 1.1 christos SSLfatal(s, SSL_AD_CERTIFICATE_REQUIRED, 3752 1.1.1.2 christos SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); 3753 1.1 christos goto err; 3754 1.1 christos } 3755 1.1 christos /* No client certificate so digest cached records */ 3756 1.1 christos if (s->s3.handshake_buffer && !ssl3_digest_cached_records(s, 0)) { 3757 1.1 christos /* SSLfatal() already called */ 3758 1.1 christos goto err; 3759 1.1 christos } 3760 1.1 christos } else { 3761 1.1 christos EVP_PKEY *pkey; 3762 1.1 christos i = ssl_verify_cert_chain(s, sk); 3763 1.1 christos if (i <= 0) { 3764 1.1 christos SSLfatal(s, ssl_x509err2alert(s->verify_result), 3765 1.1.1.2 christos SSL_R_CERTIFICATE_VERIFY_FAILED); 3766 1.1 christos goto err; 3767 1.1 christos } 3768 1.1 christos pkey = X509_get0_pubkey(sk_X509_value(sk, 0)); 3769 1.1 christos if (pkey == NULL) { 3770 1.1 christos SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, 3771 1.1.1.2 christos SSL_R_UNKNOWN_CERTIFICATE_TYPE); 3772 1.1 christos goto err; 3773 1.1 christos } 3774 1.1 christos } 3775 1.1 christos 3776 1.1 christos /* 3777 1.1 christos * Sessions must be immutable once they go into the session cache. Otherwise 3778 1.1 christos * we can get multi-thread problems. Therefore we don't "update" sessions, 3779 1.1 christos * we replace them with a duplicate. Here, we need to do this every time 3780 1.1 christos * a new certificate is received via post-handshake authentication, as the 3781 1.1 christos * session may have already gone into the session cache. 3782 1.1 christos */ 3783 1.1 christos 3784 1.1 christos if (s->post_handshake_auth == SSL_PHA_REQUESTED) { 3785 1.1 christos if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { 3786 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); 3787 1.1 christos goto err; 3788 1.1 christos } 3789 1.1 christos 3790 1.1 christos SSL_SESSION_free(s->session); 3791 1.1 christos s->session = new_sess; 3792 1.1 christos } 3793 1.1 christos 3794 1.1 christos X509_free(s->session->peer); 3795 1.1 christos s->session->peer = sk_X509_shift(sk); 3796 1.1 christos s->session->verify_result = s->verify_result; 3797 1.1 christos 3798 1.1 christos OSSL_STACK_OF_X509_free(s->session->peer_chain); 3799 1.1 christos s->session->peer_chain = sk; 3800 1.1 christos sk = NULL; 3801 1.1 christos /* Ensure there is no RPK */ 3802 1.1 christos EVP_PKEY_free(s->session->peer_rpk); 3803 1.1 christos s->session->peer_rpk = NULL; 3804 1.1 christos 3805 1.1 christos /* 3806 1.1 christos * Freeze the handshake buffer. For <TLS1.3 we do this after the CKE 3807 1.1 christos * message 3808 1.1 christos */ 3809 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) && !ssl3_digest_cached_records(s, 1)) { 3810 1.1 christos /* SSLfatal() already called */ 3811 1.1 christos goto err; 3812 1.1 christos } 3813 1.1 christos 3814 1.1 christos /* 3815 1.1 christos * Inconsistency alert: cert_chain does *not* include the peer's own 3816 1.1 christos * certificate, while we do include it in statem_clnt.c 3817 1.1 christos */ 3818 1.1 christos 3819 1.1 christos /* Save the current hash state for when we receive the CertificateVerify */ 3820 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 3821 1.1 christos if (!ssl_handshake_hash(s, s->cert_verify_hash, 3822 1.1.1.2 christos sizeof(s->cert_verify_hash), 3823 1.1.1.2 christos &s->cert_verify_hash_len)) { 3824 1.1 christos /* SSLfatal() already called */ 3825 1.1 christos goto err; 3826 1.1 christos } 3827 1.1 christos 3828 1.1 christos /* Resend session tickets */ 3829 1.1 christos s->sent_tickets = 0; 3830 1.1 christos } 3831 1.1 christos 3832 1.1 christos ret = MSG_PROCESS_CONTINUE_READING; 3833 1.1 christos 3834 1.1.1.2 christos err: 3835 1.1 christos X509_free(x); 3836 1.1 christos OSSL_STACK_OF_X509_free(sk); 3837 1.1 christos return ret; 3838 1.1 christos } 3839 1.1 christos 3840 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 3841 1.1 christos MSG_PROCESS_RETURN tls_process_client_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt) 3842 1.1 christos { 3843 1.1 christos MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; 3844 1.1 christos PACKET tmppkt; 3845 1.1 christos BUF_MEM *buf = BUF_MEM_new(); 3846 1.1 christos 3847 1.1 christos if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR) 3848 1.1 christos ret = tls_process_client_certificate(sc, &tmppkt); 3849 1.1 christos 3850 1.1 christos BUF_MEM_free(buf); 3851 1.1 christos return ret; 3852 1.1 christos } 3853 1.1 christos #endif 3854 1.1 christos 3855 1.1 christos CON_FUNC_RETURN tls_construct_server_certificate(SSL_CONNECTION *s, WPACKET *pkt) 3856 1.1 christos { 3857 1.1 christos CERT_PKEY *cpk = s->s3.tmp.cert; 3858 1.1 christos 3859 1.1 christos if (cpk == NULL) { 3860 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3861 1.1 christos return CON_FUNC_ERROR; 3862 1.1 christos } 3863 1.1 christos 3864 1.1 christos /* 3865 1.1 christos * In TLSv1.3 the certificate chain is always preceded by a 0 length context 3866 1.1 christos * for the server Certificate message 3867 1.1 christos */ 3868 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) && !WPACKET_put_bytes_u8(pkt, 0)) { 3869 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3870 1.1 christos return CON_FUNC_ERROR; 3871 1.1 christos } 3872 1.1 christos switch (s->ext.server_cert_type) { 3873 1.1 christos case TLSEXT_cert_type_rpk: 3874 1.1 christos if (!tls_output_rpk(s, pkt, cpk)) { 3875 1.1 christos /* SSLfatal() already called */ 3876 1.1 christos return 0; 3877 1.1 christos } 3878 1.1 christos break; 3879 1.1 christos case TLSEXT_cert_type_x509: 3880 1.1 christos if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { 3881 1.1 christos /* SSLfatal() already called */ 3882 1.1 christos return 0; 3883 1.1 christos } 3884 1.1 christos break; 3885 1.1 christos default: 3886 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3887 1.1 christos return 0; 3888 1.1 christos } 3889 1.1 christos 3890 1.1 christos return CON_FUNC_SUCCESS; 3891 1.1 christos } 3892 1.1 christos 3893 1.1 christos #ifndef OPENSSL_NO_COMP_ALG 3894 1.1 christos CON_FUNC_RETURN tls_construct_server_compressed_certificate(SSL_CONNECTION *sc, WPACKET *pkt) 3895 1.1 christos { 3896 1.1 christos int alg = get_compressed_certificate_alg(sc); 3897 1.1 christos OSSL_COMP_CERT *cc = sc->s3.tmp.cert->comp_cert[alg]; 3898 1.1 christos 3899 1.1 christos if (!ossl_assert(cc != NULL)) { 3900 1.1 christos SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3901 1.1 christos return 0; 3902 1.1 christos } 3903 1.1 christos /* 3904 1.1 christos * Server can't compress on-demand 3905 1.1 christos * Use pre-compressed certificate 3906 1.1 christos */ 3907 1.1 christos if (!WPACKET_put_bytes_u16(pkt, alg) 3908 1.1.1.2 christos || !WPACKET_put_bytes_u24(pkt, cc->orig_len) 3909 1.1.1.2 christos || !WPACKET_start_sub_packet_u24(pkt) 3910 1.1.1.2 christos || !WPACKET_memcpy(pkt, cc->data, cc->len) 3911 1.1.1.2 christos || !WPACKET_close(pkt)) 3912 1.1 christos return 0; 3913 1.1 christos 3914 1.1 christos sc->s3.tmp.cert->cert_comp_used++; 3915 1.1 christos return 1; 3916 1.1 christos } 3917 1.1 christos #endif 3918 1.1 christos 3919 1.1 christos static int create_ticket_prequel(SSL_CONNECTION *s, WPACKET *pkt, 3920 1.1.1.2 christos uint32_t age_add, unsigned char *tick_nonce) 3921 1.1 christos { 3922 1.1 christos uint32_t timeout = (uint32_t)ossl_time2seconds(s->session->timeout); 3923 1.1 christos 3924 1.1 christos /* 3925 1.1 christos * Ticket lifetime hint: 3926 1.1 christos * In TLSv1.3 we reset the "time" field above, and always specify the 3927 1.1 christos * timeout, limited to a 1 week period per RFC8446. 3928 1.1 christos * For TLSv1.2 this is advisory only and we leave this unspecified for 3929 1.1 christos * resumed session (for simplicity). 3930 1.1 christos */ 3931 1.1 christos #define ONE_WEEK_SEC (7 * 24 * 60 * 60) 3932 1.1 christos 3933 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 3934 1.1 christos if (ossl_time_compare(s->session->timeout, 3935 1.1.1.2 christos ossl_seconds2time(ONE_WEEK_SEC)) 3936 1.1.1.2 christos > 0) 3937 1.1 christos timeout = ONE_WEEK_SEC; 3938 1.1 christos } else if (s->hit) 3939 1.1 christos timeout = 0; 3940 1.1 christos 3941 1.1 christos if (!WPACKET_put_bytes_u32(pkt, timeout)) { 3942 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3943 1.1 christos return 0; 3944 1.1 christos } 3945 1.1 christos 3946 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 3947 1.1 christos if (!WPACKET_put_bytes_u32(pkt, age_add) 3948 1.1.1.2 christos || !WPACKET_sub_memcpy_u8(pkt, tick_nonce, TICKET_NONCE_SIZE)) { 3949 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3950 1.1 christos return 0; 3951 1.1 christos } 3952 1.1 christos } 3953 1.1 christos 3954 1.1 christos /* Start the sub-packet for the actual ticket data */ 3955 1.1 christos if (!WPACKET_start_sub_packet_u16(pkt)) { 3956 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3957 1.1 christos return 0; 3958 1.1 christos } 3959 1.1 christos 3960 1.1 christos return 1; 3961 1.1 christos } 3962 1.1 christos 3963 1.1 christos static CON_FUNC_RETURN construct_stateless_ticket(SSL_CONNECTION *s, 3964 1.1.1.2 christos WPACKET *pkt, 3965 1.1.1.2 christos uint32_t age_add, 3966 1.1.1.2 christos unsigned char *tick_nonce) 3967 1.1 christos { 3968 1.1 christos unsigned char *senc = NULL; 3969 1.1 christos EVP_CIPHER_CTX *ctx = NULL; 3970 1.1 christos SSL_HMAC *hctx = NULL; 3971 1.1 christos unsigned char *p, *encdata1, *encdata2, *macdata1, *macdata2; 3972 1.1 christos const unsigned char *const_p; 3973 1.1 christos int len, slen_full, slen, lenfinal; 3974 1.1 christos SSL_SESSION *sess; 3975 1.1 christos size_t hlen; 3976 1.1 christos SSL_CTX *tctx = s->session_ctx; 3977 1.1 christos unsigned char iv[EVP_MAX_IV_LENGTH]; 3978 1.1 christos unsigned char key_name[TLSEXT_KEYNAME_LENGTH]; 3979 1.1 christos int iv_len; 3980 1.1 christos CON_FUNC_RETURN ok = CON_FUNC_ERROR; 3981 1.1 christos size_t macoffset, macendoffset; 3982 1.1 christos SSL *ssl = SSL_CONNECTION_GET_USER_SSL(s); 3983 1.1 christos SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); 3984 1.1 christos 3985 1.1 christos /* get session encoding length */ 3986 1.1 christos slen_full = i2d_SSL_SESSION(s->session, NULL); 3987 1.1 christos /* 3988 1.1 christos * Some length values are 16 bits, so forget it if session is too 3989 1.1 christos * long 3990 1.1 christos */ 3991 1.1 christos if (slen_full == 0 || slen_full > 0xFF00) { 3992 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 3993 1.1 christos goto err; 3994 1.1 christos } 3995 1.1 christos senc = OPENSSL_malloc(slen_full); 3996 1.1 christos if (senc == NULL) { 3997 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 3998 1.1 christos goto err; 3999 1.1 christos } 4000 1.1 christos 4001 1.1 christos ctx = EVP_CIPHER_CTX_new(); 4002 1.1 christos if (ctx == NULL) { 4003 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); 4004 1.1 christos goto err; 4005 1.1 christos } 4006 1.1 christos hctx = ssl_hmac_new(tctx); 4007 1.1 christos if (hctx == NULL) { 4008 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); 4009 1.1 christos goto err; 4010 1.1 christos } 4011 1.1 christos 4012 1.1 christos p = senc; 4013 1.1 christos if (!i2d_SSL_SESSION(s->session, &p)) { 4014 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4015 1.1 christos goto err; 4016 1.1 christos } 4017 1.1 christos 4018 1.1 christos /* 4019 1.1 christos * create a fresh copy (not shared with other threads) to clean up 4020 1.1 christos */ 4021 1.1 christos const_p = senc; 4022 1.1 christos sess = d2i_SSL_SESSION_ex(NULL, &const_p, slen_full, sctx->libctx, 4023 1.1.1.2 christos sctx->propq); 4024 1.1 christos if (sess == NULL) { 4025 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4026 1.1 christos goto err; 4027 1.1 christos } 4028 1.1 christos 4029 1.1 christos slen = i2d_SSL_SESSION(sess, NULL); 4030 1.1 christos if (slen == 0 || slen > slen_full) { 4031 1.1 christos /* shouldn't ever happen */ 4032 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4033 1.1 christos SSL_SESSION_free(sess); 4034 1.1 christos goto err; 4035 1.1 christos } 4036 1.1 christos p = senc; 4037 1.1 christos if (!i2d_SSL_SESSION(sess, &p)) { 4038 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4039 1.1 christos SSL_SESSION_free(sess); 4040 1.1 christos goto err; 4041 1.1 christos } 4042 1.1 christos SSL_SESSION_free(sess); 4043 1.1 christos 4044 1.1 christos /* 4045 1.1 christos * Initialize HMAC and cipher contexts. If callback present it does 4046 1.1 christos * all the work otherwise use generated values from parent ctx. 4047 1.1 christos */ 4048 1.1 christos #ifndef OPENSSL_NO_DEPRECATED_3_0 4049 1.1 christos if (tctx->ext.ticket_key_evp_cb != NULL || tctx->ext.ticket_key_cb != NULL) 4050 1.1 christos #else 4051 1.1 christos if (tctx->ext.ticket_key_evp_cb != NULL) 4052 1.1 christos #endif 4053 1.1 christos { 4054 1.1 christos int ret = 0; 4055 1.1 christos 4056 1.1 christos if (tctx->ext.ticket_key_evp_cb != NULL) 4057 1.1 christos ret = tctx->ext.ticket_key_evp_cb(ssl, key_name, iv, ctx, 4058 1.1.1.2 christos ssl_hmac_get0_EVP_MAC_CTX(hctx), 4059 1.1.1.2 christos 1); 4060 1.1 christos #ifndef OPENSSL_NO_DEPRECATED_3_0 4061 1.1 christos else if (tctx->ext.ticket_key_cb != NULL) 4062 1.1 christos /* if 0 is returned, write an empty ticket */ 4063 1.1 christos ret = tctx->ext.ticket_key_cb(ssl, key_name, iv, ctx, 4064 1.1.1.2 christos ssl_hmac_get0_HMAC_CTX(hctx), 1); 4065 1.1 christos #endif 4066 1.1 christos 4067 1.1 christos if (ret == 0) { 4068 1.1 christos /* 4069 1.1 christos * In TLSv1.2 we construct a 0 length ticket. In TLSv1.3 a 0 4070 1.1 christos * length ticket is not allowed so we abort construction of the 4071 1.1 christos * ticket 4072 1.1 christos */ 4073 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 4074 1.1 christos ok = CON_FUNC_DONT_SEND; 4075 1.1 christos goto err; 4076 1.1 christos } 4077 1.1 christos /* Put timeout and length */ 4078 1.1 christos if (!WPACKET_put_bytes_u32(pkt, 0) 4079 1.1.1.2 christos || !WPACKET_put_bytes_u16(pkt, 0)) { 4080 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4081 1.1 christos goto err; 4082 1.1 christos } 4083 1.1 christos OPENSSL_free(senc); 4084 1.1 christos EVP_CIPHER_CTX_free(ctx); 4085 1.1 christos ssl_hmac_free(hctx); 4086 1.1 christos return CON_FUNC_SUCCESS; 4087 1.1 christos } 4088 1.1 christos if (ret < 0) { 4089 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED); 4090 1.1 christos goto err; 4091 1.1 christos } 4092 1.1 christos iv_len = EVP_CIPHER_CTX_get_iv_length(ctx); 4093 1.1 christos if (iv_len < 0) { 4094 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4095 1.1 christos goto err; 4096 1.1 christos } 4097 1.1 christos } else { 4098 1.1 christos EVP_CIPHER *cipher = EVP_CIPHER_fetch(sctx->libctx, "AES-256-CBC", 4099 1.1.1.2 christos sctx->propq); 4100 1.1 christos 4101 1.1 christos if (cipher == NULL) { 4102 1.1 christos /* Error is already recorded */ 4103 1.1 christos SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR); 4104 1.1 christos goto err; 4105 1.1 christos } 4106 1.1 christos 4107 1.1 christos iv_len = EVP_CIPHER_get_iv_length(cipher); 4108 1.1 christos if (iv_len < 0 4109 1.1.1.2 christos || RAND_bytes_ex(sctx->libctx, iv, iv_len, 0) <= 0 4110 1.1.1.2 christos || !EVP_EncryptInit_ex(ctx, cipher, NULL, 4111 1.1.1.2 christos tctx->ext.secure->tick_aes_key, iv) 4112 1.1.1.2 christos || !ssl_hmac_init(hctx, tctx->ext.secure->tick_hmac_key, 4113 1.1.1.2 christos sizeof(tctx->ext.secure->tick_hmac_key), 4114 1.1.1.2 christos "SHA256")) { 4115 1.1 christos EVP_CIPHER_free(cipher); 4116 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4117 1.1 christos goto err; 4118 1.1 christos } 4119 1.1 christos EVP_CIPHER_free(cipher); 4120 1.1 christos memcpy(key_name, tctx->ext.tick_key_name, 4121 1.1.1.2 christos sizeof(tctx->ext.tick_key_name)); 4122 1.1 christos } 4123 1.1 christos 4124 1.1 christos if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { 4125 1.1 christos /* SSLfatal() already called */ 4126 1.1 christos goto err; 4127 1.1 christos } 4128 1.1 christos 4129 1.1 christos if (!WPACKET_get_total_written(pkt, &macoffset) 4130 1.1.1.2 christos /* Output key name */ 4131 1.1.1.2 christos || !WPACKET_memcpy(pkt, key_name, sizeof(key_name)) 4132 1.1.1.2 christos /* output IV */ 4133 1.1.1.2 christos || !WPACKET_memcpy(pkt, iv, iv_len) 4134 1.1.1.2 christos || !WPACKET_reserve_bytes(pkt, slen + EVP_MAX_BLOCK_LENGTH, 4135 1.1.1.2 christos &encdata1) 4136 1.1.1.2 christos /* Encrypt session data */ 4137 1.1.1.2 christos || !EVP_EncryptUpdate(ctx, encdata1, &len, senc, slen) 4138 1.1.1.2 christos || !WPACKET_allocate_bytes(pkt, len, &encdata2) 4139 1.1.1.2 christos || encdata1 != encdata2 4140 1.1.1.2 christos || !EVP_EncryptFinal(ctx, encdata1 + len, &lenfinal) 4141 1.1.1.2 christos || !WPACKET_allocate_bytes(pkt, lenfinal, &encdata2) 4142 1.1.1.2 christos || encdata1 + len != encdata2 4143 1.1.1.2 christos || len + lenfinal > slen + EVP_MAX_BLOCK_LENGTH 4144 1.1.1.2 christos || !WPACKET_get_total_written(pkt, &macendoffset) 4145 1.1.1.2 christos || !ssl_hmac_update(hctx, 4146 1.1.1.2 christos (unsigned char *)s->init_buf->data + macoffset, 4147 1.1.1.2 christos macendoffset - macoffset) 4148 1.1.1.2 christos || !WPACKET_reserve_bytes(pkt, EVP_MAX_MD_SIZE, &macdata1) 4149 1.1.1.2 christos || !ssl_hmac_final(hctx, macdata1, &hlen, EVP_MAX_MD_SIZE) 4150 1.1.1.2 christos || hlen > EVP_MAX_MD_SIZE 4151 1.1.1.2 christos || !WPACKET_allocate_bytes(pkt, hlen, &macdata2) 4152 1.1.1.2 christos || macdata1 != macdata2) { 4153 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4154 1.1 christos goto err; 4155 1.1 christos } 4156 1.1 christos 4157 1.1 christos /* Close the sub-packet created by create_ticket_prequel() */ 4158 1.1 christos if (!WPACKET_close(pkt)) { 4159 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4160 1.1 christos goto err; 4161 1.1 christos } 4162 1.1 christos 4163 1.1 christos ok = CON_FUNC_SUCCESS; 4164 1.1.1.2 christos err: 4165 1.1 christos OPENSSL_free(senc); 4166 1.1 christos EVP_CIPHER_CTX_free(ctx); 4167 1.1 christos ssl_hmac_free(hctx); 4168 1.1 christos return ok; 4169 1.1 christos } 4170 1.1 christos 4171 1.1 christos static int construct_stateful_ticket(SSL_CONNECTION *s, WPACKET *pkt, 4172 1.1.1.2 christos uint32_t age_add, 4173 1.1.1.2 christos unsigned char *tick_nonce) 4174 1.1 christos { 4175 1.1 christos if (!create_ticket_prequel(s, pkt, age_add, tick_nonce)) { 4176 1.1 christos /* SSLfatal() already called */ 4177 1.1 christos return 0; 4178 1.1 christos } 4179 1.1 christos 4180 1.1 christos if (!WPACKET_memcpy(pkt, s->session->session_id, 4181 1.1.1.2 christos s->session->session_id_length) 4182 1.1.1.2 christos || !WPACKET_close(pkt)) { 4183 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4184 1.1 christos return 0; 4185 1.1 christos } 4186 1.1 christos 4187 1.1 christos return 1; 4188 1.1 christos } 4189 1.1 christos 4190 1.1 christos static void tls_update_ticket_counts(SSL_CONNECTION *s) 4191 1.1 christos { 4192 1.1 christos /* 4193 1.1 christos * Increment both |sent_tickets| and |next_ticket_nonce|. |sent_tickets| 4194 1.1 christos * gets reset to 0 if we send more tickets following a post-handshake 4195 1.1 christos * auth, but |next_ticket_nonce| does not. If we're sending extra 4196 1.1 christos * tickets, decrement the count of pending extra tickets. 4197 1.1 christos */ 4198 1.1 christos s->sent_tickets++; 4199 1.1 christos s->next_ticket_nonce++; 4200 1.1 christos if (s->ext.extra_tickets_expected > 0) 4201 1.1 christos s->ext.extra_tickets_expected--; 4202 1.1 christos } 4203 1.1 christos 4204 1.1 christos CON_FUNC_RETURN tls_construct_new_session_ticket(SSL_CONNECTION *s, WPACKET *pkt) 4205 1.1 christos { 4206 1.1 christos SSL_CTX *tctx = s->session_ctx; 4207 1.1 christos unsigned char tick_nonce[TICKET_NONCE_SIZE]; 4208 1.1 christos union { 4209 1.1 christos unsigned char age_add_c[sizeof(uint32_t)]; 4210 1.1 christos uint32_t age_add; 4211 1.1 christos } age_add_u; 4212 1.1 christos CON_FUNC_RETURN ret = CON_FUNC_ERROR; 4213 1.1 christos 4214 1.1 christos age_add_u.age_add = 0; 4215 1.1 christos 4216 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 4217 1.1 christos size_t i, hashlen; 4218 1.1 christos uint64_t nonce; 4219 1.1.1.2 christos /* ASCII: "resumption", in hex for EBCDIC compatibility */ 4220 1.1.1.2 christos static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D, 4221 1.1.1.2 christos 0x70, 0x74, 0x69, 0x6F, 0x6E }; 4222 1.1 christos const EVP_MD *md = ssl_handshake_md(s); 4223 1.1 christos int hashleni = EVP_MD_get_size(md); 4224 1.1 christos 4225 1.1 christos /* Ensure cast to size_t is safe */ 4226 1.1 christos if (!ossl_assert(hashleni > 0)) { 4227 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4228 1.1 christos goto err; 4229 1.1 christos } 4230 1.1 christos hashlen = (size_t)hashleni; 4231 1.1 christos 4232 1.1 christos /* 4233 1.1 christos * If we already sent one NewSessionTicket, or we resumed then 4234 1.1 christos * s->session may already be in a cache and so we must not modify it. 4235 1.1 christos * Instead we need to take a copy of it and modify that. 4236 1.1 christos */ 4237 1.1 christos if (s->sent_tickets != 0 || s->hit) { 4238 1.1 christos SSL_SESSION *new_sess = ssl_session_dup(s->session, 0); 4239 1.1 christos 4240 1.1 christos if (new_sess == NULL) { 4241 1.1 christos /* SSLfatal already called */ 4242 1.1 christos goto err; 4243 1.1 christos } 4244 1.1 christos 4245 1.1 christos SSL_SESSION_free(s->session); 4246 1.1 christos s->session = new_sess; 4247 1.1 christos } 4248 1.1 christos 4249 1.1 christos if (!ssl_generate_session_id(s, s->session)) { 4250 1.1 christos /* SSLfatal() already called */ 4251 1.1 christos goto err; 4252 1.1 christos } 4253 1.1 christos if (RAND_bytes_ex(SSL_CONNECTION_GET_CTX(s)->libctx, 4254 1.1.1.2 christos age_add_u.age_add_c, sizeof(age_add_u), 0) 4255 1.1.1.2 christos <= 0) { 4256 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4257 1.1 christos goto err; 4258 1.1 christos } 4259 1.1 christos s->session->ext.tick_age_add = age_add_u.age_add; 4260 1.1 christos 4261 1.1 christos nonce = s->next_ticket_nonce; 4262 1.1 christos for (i = TICKET_NONCE_SIZE; i > 0; i--) { 4263 1.1 christos tick_nonce[i - 1] = (unsigned char)(nonce & 0xff); 4264 1.1 christos nonce >>= 8; 4265 1.1 christos } 4266 1.1 christos 4267 1.1 christos if (!tls13_hkdf_expand(s, md, s->resumption_master_secret, 4268 1.1.1.2 christos nonce_label, 4269 1.1.1.2 christos sizeof(nonce_label), 4270 1.1.1.2 christos tick_nonce, 4271 1.1.1.2 christos TICKET_NONCE_SIZE, 4272 1.1.1.2 christos s->session->master_key, 4273 1.1.1.2 christos hashlen, 1)) { 4274 1.1 christos /* SSLfatal() already called */ 4275 1.1 christos goto err; 4276 1.1 christos } 4277 1.1 christos s->session->master_key_length = hashlen; 4278 1.1 christos 4279 1.1 christos s->session->time = ossl_time_now(); 4280 1.1 christos ssl_session_calculate_timeout(s->session); 4281 1.1 christos if (s->s3.alpn_selected != NULL) { 4282 1.1 christos OPENSSL_free(s->session->ext.alpn_selected); 4283 1.1.1.2 christos s->session->ext.alpn_selected = OPENSSL_memdup(s->s3.alpn_selected, s->s3.alpn_selected_len); 4284 1.1 christos if (s->session->ext.alpn_selected == NULL) { 4285 1.1 christos s->session->ext.alpn_selected_len = 0; 4286 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); 4287 1.1 christos goto err; 4288 1.1 christos } 4289 1.1 christos s->session->ext.alpn_selected_len = s->s3.alpn_selected_len; 4290 1.1 christos } 4291 1.1 christos s->session->ext.max_early_data = s->max_early_data; 4292 1.1 christos } 4293 1.1 christos 4294 1.1.1.2 christos if (tctx->generate_ticket_cb != NULL && tctx->generate_ticket_cb(SSL_CONNECTION_GET_USER_SSL(s), tctx->ticket_cb_data) == 0) { 4295 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4296 1.1 christos goto err; 4297 1.1 christos } 4298 1.1 christos /* 4299 1.1 christos * If we are using anti-replay protection then we behave as if 4300 1.1 christos * SSL_OP_NO_TICKET is set - we are caching tickets anyway so there 4301 1.1 christos * is no point in using full stateless tickets. 4302 1.1 christos */ 4303 1.1 christos if (SSL_CONNECTION_IS_TLS13(s) 4304 1.1.1.2 christos && ((s->options & SSL_OP_NO_TICKET) != 0 4305 1.1.1.2 christos || (s->max_early_data > 0 4306 1.1.1.2 christos && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0))) { 4307 1.1 christos if (!construct_stateful_ticket(s, pkt, age_add_u.age_add, tick_nonce)) { 4308 1.1 christos /* SSLfatal() already called */ 4309 1.1 christos goto err; 4310 1.1 christos } 4311 1.1 christos } else { 4312 1.1 christos CON_FUNC_RETURN tmpret; 4313 1.1 christos 4314 1.1 christos tmpret = construct_stateless_ticket(s, pkt, age_add_u.age_add, 4315 1.1.1.2 christos tick_nonce); 4316 1.1 christos if (tmpret != CON_FUNC_SUCCESS) { 4317 1.1 christos if (tmpret == CON_FUNC_DONT_SEND) { 4318 1.1 christos /* Non-fatal. Abort construction but continue */ 4319 1.1 christos ret = CON_FUNC_DONT_SEND; 4320 1.1 christos /* We count this as a success so update the counts anwyay */ 4321 1.1 christos tls_update_ticket_counts(s); 4322 1.1 christos } 4323 1.1 christos /* else SSLfatal() already called */ 4324 1.1 christos goto err; 4325 1.1 christos } 4326 1.1 christos } 4327 1.1 christos 4328 1.1 christos if (SSL_CONNECTION_IS_TLS13(s)) { 4329 1.1 christos if (!tls_construct_extensions(s, pkt, 4330 1.1.1.2 christos SSL_EXT_TLS1_3_NEW_SESSION_TICKET, 4331 1.1.1.2 christos NULL, 0)) { 4332 1.1 christos /* SSLfatal() already called */ 4333 1.1 christos goto err; 4334 1.1 christos } 4335 1.1 christos tls_update_ticket_counts(s); 4336 1.1 christos ssl_update_cache(s, SSL_SESS_CACHE_SERVER); 4337 1.1 christos } 4338 1.1 christos 4339 1.1 christos ret = CON_FUNC_SUCCESS; 4340 1.1.1.2 christos err: 4341 1.1 christos return ret; 4342 1.1 christos } 4343 1.1 christos 4344 1.1 christos /* 4345 1.1 christos * In TLSv1.3 this is called from the extensions code, otherwise it is used to 4346 1.1 christos * create a separate message. Returns 1 on success or 0 on failure. 4347 1.1 christos */ 4348 1.1 christos int tls_construct_cert_status_body(SSL_CONNECTION *s, WPACKET *pkt) 4349 1.1 christos { 4350 1.1 christos if (!WPACKET_put_bytes_u8(pkt, s->ext.status_type) 4351 1.1.1.2 christos || !WPACKET_sub_memcpy_u24(pkt, s->ext.ocsp.resp, 4352 1.1.1.2 christos s->ext.ocsp.resp_len)) { 4353 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4354 1.1 christos return 0; 4355 1.1 christos } 4356 1.1 christos 4357 1.1 christos return 1; 4358 1.1 christos } 4359 1.1 christos 4360 1.1 christos CON_FUNC_RETURN tls_construct_cert_status(SSL_CONNECTION *s, WPACKET *pkt) 4361 1.1 christos { 4362 1.1 christos if (!tls_construct_cert_status_body(s, pkt)) { 4363 1.1 christos /* SSLfatal() already called */ 4364 1.1 christos return CON_FUNC_ERROR; 4365 1.1 christos } 4366 1.1 christos 4367 1.1 christos return CON_FUNC_SUCCESS; 4368 1.1 christos } 4369 1.1 christos 4370 1.1 christos #ifndef OPENSSL_NO_NEXTPROTONEG 4371 1.1 christos /* 4372 1.1 christos * tls_process_next_proto reads a Next Protocol Negotiation handshake message. 4373 1.1 christos * It sets the next_proto member in s if found 4374 1.1 christos */ 4375 1.1 christos MSG_PROCESS_RETURN tls_process_next_proto(SSL_CONNECTION *s, PACKET *pkt) 4376 1.1 christos { 4377 1.1 christos PACKET next_proto, padding; 4378 1.1 christos size_t next_proto_len; 4379 1.1 christos 4380 1.1 christos /*- 4381 1.1 christos * The payload looks like: 4382 1.1 christos * uint8 proto_len; 4383 1.1 christos * uint8 proto[proto_len]; 4384 1.1 christos * uint8 padding_len; 4385 1.1 christos * uint8 padding[padding_len]; 4386 1.1 christos */ 4387 1.1 christos if (!PACKET_get_length_prefixed_1(pkt, &next_proto) 4388 1.1 christos || !PACKET_get_length_prefixed_1(pkt, &padding) 4389 1.1 christos || PACKET_remaining(pkt) > 0) { 4390 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 4391 1.1 christos return MSG_PROCESS_ERROR; 4392 1.1 christos } 4393 1.1 christos 4394 1.1 christos if (!PACKET_memdup(&next_proto, &s->ext.npn, &next_proto_len)) { 4395 1.1 christos s->ext.npn_len = 0; 4396 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4397 1.1 christos return MSG_PROCESS_ERROR; 4398 1.1 christos } 4399 1.1 christos 4400 1.1 christos s->ext.npn_len = (unsigned char)next_proto_len; 4401 1.1 christos 4402 1.1 christos return MSG_PROCESS_CONTINUE_READING; 4403 1.1 christos } 4404 1.1 christos #endif 4405 1.1 christos 4406 1.1 christos static CON_FUNC_RETURN tls_construct_encrypted_extensions(SSL_CONNECTION *s, 4407 1.1.1.2 christos WPACKET *pkt) 4408 1.1 christos { 4409 1.1 christos if (!tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, 4410 1.1.1.2 christos NULL, 0)) { 4411 1.1 christos /* SSLfatal() already called */ 4412 1.1 christos return CON_FUNC_ERROR; 4413 1.1 christos } 4414 1.1 christos 4415 1.1 christos return CON_FUNC_SUCCESS; 4416 1.1 christos } 4417 1.1 christos 4418 1.1 christos MSG_PROCESS_RETURN tls_process_end_of_early_data(SSL_CONNECTION *s, PACKET *pkt) 4419 1.1 christos { 4420 1.1 christos if (PACKET_remaining(pkt) != 0) { 4421 1.1 christos SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); 4422 1.1 christos return MSG_PROCESS_ERROR; 4423 1.1 christos } 4424 1.1 christos 4425 1.1 christos if (s->early_data_state != SSL_EARLY_DATA_READING 4426 1.1.1.2 christos && s->early_data_state != SSL_EARLY_DATA_READ_RETRY) { 4427 1.1 christos SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); 4428 1.1 christos return MSG_PROCESS_ERROR; 4429 1.1 christos } 4430 1.1 christos 4431 1.1 christos /* 4432 1.1 christos * EndOfEarlyData signals a key change so the end of the message must be on 4433 1.1 christos * a record boundary. 4434 1.1 christos */ 4435 1.1 christos if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { 4436 1.1 christos SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_NOT_ON_RECORD_BOUNDARY); 4437 1.1 christos return MSG_PROCESS_ERROR; 4438 1.1 christos } 4439 1.1 christos 4440 1.1 christos s->early_data_state = SSL_EARLY_DATA_FINISHED_READING; 4441 1.1 christos if (!SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->change_cipher_state(s, 4442 1.1.1.2 christos SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_SERVER_READ)) { 4443 1.1 christos /* SSLfatal() already called */ 4444 1.1 christos return MSG_PROCESS_ERROR; 4445 1.1 christos } 4446 1.1 christos 4447 1.1 christos return MSG_PROCESS_CONTINUE_READING; 4448 1.1 christos } 4449