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