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