| 1 |
/* |
| 2 |
* Copyright 1995-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
| 4 |
* Copyright 2005 Nokia. All rights reserved. |
| 5 |
* |
| 6 |
* Licensed under the Apache License 2.0 (the "License"). You may not use |
| 7 |
* this file except in compliance with the License. You can obtain a copy |
| 8 |
* in the file LICENSE in the source distribution or at |
| 9 |
* https://www.openssl.org/source/license.html |
| 10 |
*/ |
| 11 |
|
| 12 |
#include <stdio.h> |
| 13 |
#include <time.h> |
| 14 |
#include <assert.h> |
| 15 |
#include "../ssl_local.h" |
| 16 |
#include "statem_local.h" |
| 17 |
#include <openssl/buffer.h> |
| 18 |
#include <openssl/rand.h> |
| 19 |
#include <openssl/objects.h> |
| 20 |
#include <openssl/evp.h> |
| 21 |
#include <openssl/md5.h> |
| 22 |
#include <openssl/dh.h> |
| 23 |
#include <openssl/rsa.h> |
| 24 |
#include <openssl/bn.h> |
| 25 |
#include <openssl/engine.h> |
| 26 |
#include <openssl/trace.h> |
| 27 |
#include <openssl/core_names.h> |
| 28 |
#include <openssl/param_build.h> |
| 29 |
#include "internal/cryptlib.h" |
| 30 |
#include "internal/comp.h" |
| 31 |
#include "internal/ssl_unwrap.h" |
| 32 |
#include <openssl/ocsp.h> |
| 33 |
|
| 34 |
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s, |
| 35 |
PACKET *pkt); |
| 36 |
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s, |
| 37 |
PACKET *pkt); |
| 38 |
|
| 39 |
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s); |
| 40 |
static int key_exchange_expected(SSL_CONNECTION *s); |
| 41 |
static int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk, |
| 42 |
WPACKET *pkt); |
| 43 |
|
| 44 |
static ossl_inline int received_server_cert(SSL_CONNECTION *sc) |
| 45 |
{ |
| 46 |
return sc->session->peer_rpk != NULL || sc->session->peer != NULL; |
| 47 |
} |
| 48 |
|
| 49 |
/* |
| 50 |
* Is a CertificateRequest message allowed at the moment or not? |
| 51 |
* |
| 52 |
* Return values are: |
| 53 |
* 1: Yes |
| 54 |
* 0: No |
| 55 |
*/ |
| 56 |
static ossl_inline int cert_req_allowed(SSL_CONNECTION *s) |
| 57 |
{ |
| 58 |
/* TLS does not like anon-DH with client cert */ |
| 59 |
if ((s->version > SSL3_VERSION |
| 60 |
&& (s->s3.tmp.new_cipher->algorithm_auth & SSL_aNULL)) |
| 61 |
|| (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK))) |
| 62 |
return 0; |
| 63 |
|
| 64 |
return 1; |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* Should we expect the ServerKeyExchange message or not? |
| 69 |
* |
| 70 |
* Return values are: |
| 71 |
* 1: Yes |
| 72 |
* 0: No |
| 73 |
*/ |
| 74 |
static int key_exchange_expected(SSL_CONNECTION *s) |
| 75 |
{ |
| 76 |
long alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
| 77 |
|
| 78 |
/* |
| 79 |
* Can't skip server key exchange if this is an ephemeral |
| 80 |
* ciphersuite or for SRP |
| 81 |
*/ |
| 82 |
if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK | SSL_kSRP)) { |
| 83 |
return 1; |
| 84 |
} |
| 85 |
|
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* ossl_statem_client_read_transition() encapsulates the logic for the allowed |
| 91 |
* handshake state transitions when a TLS1.3 client is reading messages from the |
| 92 |
* server. The message type that the server has sent is provided in |mt|. The |
| 93 |
* current state is in |s->statem.hand_state|. |
| 94 |
* |
| 95 |
* Return values are 1 for success (transition allowed) and 0 on error |
| 96 |
* (transition not allowed) |
| 97 |
*/ |
| 98 |
static int ossl_statem_client13_read_transition(SSL_CONNECTION *s, int mt) |
| 99 |
{ |
| 100 |
OSSL_STATEM *st = &s->statem; |
| 101 |
|
| 102 |
/* |
| 103 |
* Note: There is no case for TLS_ST_CW_CLNT_HELLO, because we haven't |
| 104 |
* yet negotiated TLSv1.3 at that point so that is handled by |
| 105 |
* ossl_statem_client_read_transition() |
| 106 |
*/ |
| 107 |
|
| 108 |
switch (st->hand_state) { |
| 109 |
default: |
| 110 |
break; |
| 111 |
|
| 112 |
case TLS_ST_CW_CLNT_HELLO: |
| 113 |
/* |
| 114 |
* This must a ClientHello following a HelloRetryRequest, so the only |
| 115 |
* thing we can get now is a ServerHello. |
| 116 |
*/ |
| 117 |
if (mt == SSL3_MT_SERVER_HELLO) { |
| 118 |
st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 119 |
return 1; |
| 120 |
} |
| 121 |
break; |
| 122 |
|
| 123 |
case TLS_ST_CR_SRVR_HELLO: |
| 124 |
if (mt == SSL3_MT_ENCRYPTED_EXTENSIONS) { |
| 125 |
st->hand_state = TLS_ST_CR_ENCRYPTED_EXTENSIONS; |
| 126 |
return 1; |
| 127 |
} |
| 128 |
break; |
| 129 |
|
| 130 |
case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
| 131 |
if (s->hit) { |
| 132 |
if (mt == SSL3_MT_FINISHED) { |
| 133 |
st->hand_state = TLS_ST_CR_FINISHED; |
| 134 |
return 1; |
| 135 |
} |
| 136 |
} else { |
| 137 |
if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
| 138 |
st->hand_state = TLS_ST_CR_CERT_REQ; |
| 139 |
return 1; |
| 140 |
} |
| 141 |
if (mt == SSL3_MT_CERTIFICATE) { |
| 142 |
st->hand_state = TLS_ST_CR_CERT; |
| 143 |
return 1; |
| 144 |
} |
| 145 |
#ifndef OPENSSL_NO_COMP_ALG |
| 146 |
if (mt == SSL3_MT_COMPRESSED_CERTIFICATE |
| 147 |
&& s->ext.compress_certificate_sent) { |
| 148 |
st->hand_state = TLS_ST_CR_COMP_CERT; |
| 149 |
return 1; |
| 150 |
} |
| 151 |
#endif |
| 152 |
} |
| 153 |
break; |
| 154 |
|
| 155 |
case TLS_ST_CR_CERT_REQ: |
| 156 |
if (mt == SSL3_MT_CERTIFICATE) { |
| 157 |
st->hand_state = TLS_ST_CR_CERT; |
| 158 |
return 1; |
| 159 |
} |
| 160 |
#ifndef OPENSSL_NO_COMP_ALG |
| 161 |
if (mt == SSL3_MT_COMPRESSED_CERTIFICATE |
| 162 |
&& s->ext.compress_certificate_sent) { |
| 163 |
st->hand_state = TLS_ST_CR_COMP_CERT; |
| 164 |
return 1; |
| 165 |
} |
| 166 |
#endif |
| 167 |
break; |
| 168 |
|
| 169 |
case TLS_ST_CR_CERT: |
| 170 |
case TLS_ST_CR_COMP_CERT: |
| 171 |
if (mt == SSL3_MT_CERTIFICATE_VERIFY) { |
| 172 |
st->hand_state = TLS_ST_CR_CERT_VRFY; |
| 173 |
return 1; |
| 174 |
} |
| 175 |
break; |
| 176 |
|
| 177 |
case TLS_ST_CR_CERT_VRFY: |
| 178 |
if (mt == SSL3_MT_FINISHED) { |
| 179 |
st->hand_state = TLS_ST_CR_FINISHED; |
| 180 |
return 1; |
| 181 |
} |
| 182 |
break; |
| 183 |
|
| 184 |
case TLS_ST_OK: |
| 185 |
if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 186 |
st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 187 |
return 1; |
| 188 |
} |
| 189 |
if (mt == SSL3_MT_KEY_UPDATE && !SSL_IS_QUIC_HANDSHAKE(s)) { |
| 190 |
st->hand_state = TLS_ST_CR_KEY_UPDATE; |
| 191 |
return 1; |
| 192 |
} |
| 193 |
if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
| 194 |
#if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION |
| 195 |
/* Restore digest for PHA before adding message.*/ |
| 196 |
#error Internal DTLS version error |
| 197 |
#endif |
| 198 |
if (!SSL_CONNECTION_IS_DTLS(s) |
| 199 |
&& s->post_handshake_auth == SSL_PHA_EXT_SENT) { |
| 200 |
s->post_handshake_auth = SSL_PHA_REQUESTED; |
| 201 |
/* |
| 202 |
* In TLS, this is called before the message is added to the |
| 203 |
* digest. In DTLS, this is expected to be called after adding |
| 204 |
* to the digest. Either move the digest restore, or add the |
| 205 |
* message here after the swap, or do it after the clientFinished? |
| 206 |
*/ |
| 207 |
if (!tls13_restore_handshake_digest_for_pha(s)) { |
| 208 |
/* SSLfatal() already called */ |
| 209 |
return 0; |
| 210 |
} |
| 211 |
st->hand_state = TLS_ST_CR_CERT_REQ; |
| 212 |
return 1; |
| 213 |
} |
| 214 |
} |
| 215 |
break; |
| 216 |
} |
| 217 |
|
| 218 |
/* No valid transition found */ |
| 219 |
return 0; |
| 220 |
} |
| 221 |
|
| 222 |
/* |
| 223 |
* ossl_statem_client_read_transition() encapsulates the logic for the allowed |
| 224 |
* handshake state transitions when the client is reading messages from the |
| 225 |
* server. The message type that the server has sent is provided in |mt|. The |
| 226 |
* current state is in |s->statem.hand_state|. |
| 227 |
* |
| 228 |
* Return values are 1 for success (transition allowed) and 0 on error |
| 229 |
* (transition not allowed) |
| 230 |
*/ |
| 231 |
int ossl_statem_client_read_transition(SSL_CONNECTION *s, int mt) |
| 232 |
{ |
| 233 |
OSSL_STATEM *st = &s->statem; |
| 234 |
int ske_expected; |
| 235 |
|
| 236 |
/* |
| 237 |
* Note that after writing the first ClientHello we don't know what version |
| 238 |
* we are going to negotiate yet, so we don't take this branch until later. |
| 239 |
*/ |
| 240 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 241 |
if (!ossl_statem_client13_read_transition(s, mt)) |
| 242 |
goto err; |
| 243 |
return 1; |
| 244 |
} |
| 245 |
|
| 246 |
switch (st->hand_state) { |
| 247 |
default: |
| 248 |
break; |
| 249 |
|
| 250 |
case TLS_ST_CW_CLNT_HELLO: |
| 251 |
if (mt == SSL3_MT_SERVER_HELLO) { |
| 252 |
st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 257 |
if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 258 |
st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; |
| 259 |
return 1; |
| 260 |
} |
| 261 |
} |
| 262 |
break; |
| 263 |
|
| 264 |
case TLS_ST_EARLY_DATA: |
| 265 |
/* |
| 266 |
* We've not actually selected TLSv1.3 yet, but we have sent early |
| 267 |
* data. The only thing allowed now is a ServerHello or a |
| 268 |
* HelloRetryRequest. |
| 269 |
*/ |
| 270 |
if (mt == SSL3_MT_SERVER_HELLO) { |
| 271 |
st->hand_state = TLS_ST_CR_SRVR_HELLO; |
| 272 |
return 1; |
| 273 |
} |
| 274 |
break; |
| 275 |
|
| 276 |
case TLS_ST_CR_SRVR_HELLO: |
| 277 |
if (s->hit) { |
| 278 |
if (s->ext.ticket_expected) { |
| 279 |
if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 280 |
st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 281 |
return 1; |
| 282 |
} |
| 283 |
} else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 284 |
st->hand_state = TLS_ST_CR_CHANGE; |
| 285 |
return 1; |
| 286 |
} |
| 287 |
} else { |
| 288 |
if (SSL_CONNECTION_IS_DTLS(s) |
| 289 |
&& mt == DTLS1_MT_HELLO_VERIFY_REQUEST) { |
| 290 |
st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST; |
| 291 |
return 1; |
| 292 |
} else if (s->version >= TLS1_VERSION |
| 293 |
&& s->ext.session_secret_cb != NULL |
| 294 |
&& s->session->ext.tick != NULL |
| 295 |
&& mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 296 |
/* |
| 297 |
* Normally, we can tell if the server is resuming the session |
| 298 |
* from the session ID. EAP-FAST (RFC 4851), however, relies on |
| 299 |
* the next server message after the ServerHello to determine if |
| 300 |
* the server is resuming. |
| 301 |
*/ |
| 302 |
s->hit = 1; |
| 303 |
st->hand_state = TLS_ST_CR_CHANGE; |
| 304 |
return 1; |
| 305 |
} else if (!(s->s3.tmp.new_cipher->algorithm_auth |
| 306 |
& (SSL_aNULL | SSL_aSRP | SSL_aPSK))) { |
| 307 |
if (mt == SSL3_MT_CERTIFICATE) { |
| 308 |
st->hand_state = TLS_ST_CR_CERT; |
| 309 |
return 1; |
| 310 |
} |
| 311 |
} else { |
| 312 |
ske_expected = key_exchange_expected(s); |
| 313 |
/* SKE is optional for some PSK ciphersuites */ |
| 314 |
if (ske_expected |
| 315 |
|| ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) |
| 316 |
&& mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
| 317 |
if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 318 |
st->hand_state = TLS_ST_CR_KEY_EXCH; |
| 319 |
return 1; |
| 320 |
} |
| 321 |
} else if (mt == SSL3_MT_CERTIFICATE_REQUEST |
| 322 |
&& cert_req_allowed(s)) { |
| 323 |
st->hand_state = TLS_ST_CR_CERT_REQ; |
| 324 |
return 1; |
| 325 |
} else if (mt == SSL3_MT_SERVER_DONE) { |
| 326 |
st->hand_state = TLS_ST_CR_SRVR_DONE; |
| 327 |
return 1; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
break; |
| 332 |
|
| 333 |
case TLS_ST_CR_CERT: |
| 334 |
case TLS_ST_CR_COMP_CERT: |
| 335 |
/* |
| 336 |
* The CertificateStatus message is optional even if |
| 337 |
* |ext.status_expected| is set |
| 338 |
*/ |
| 339 |
if (s->ext.status_expected && mt == SSL3_MT_CERTIFICATE_STATUS) { |
| 340 |
st->hand_state = TLS_ST_CR_CERT_STATUS; |
| 341 |
return 1; |
| 342 |
} |
| 343 |
/* Fall through */ |
| 344 |
|
| 345 |
case TLS_ST_CR_CERT_STATUS: |
| 346 |
ske_expected = key_exchange_expected(s); |
| 347 |
/* SKE is optional for some PSK ciphersuites */ |
| 348 |
if (ske_expected || ((s->s3.tmp.new_cipher->algorithm_mkey & SSL_PSK) && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) { |
| 349 |
if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) { |
| 350 |
st->hand_state = TLS_ST_CR_KEY_EXCH; |
| 351 |
return 1; |
| 352 |
} |
| 353 |
goto err; |
| 354 |
} |
| 355 |
/* Fall through */ |
| 356 |
|
| 357 |
case TLS_ST_CR_KEY_EXCH: |
| 358 |
if (mt == SSL3_MT_CERTIFICATE_REQUEST) { |
| 359 |
if (cert_req_allowed(s)) { |
| 360 |
st->hand_state = TLS_ST_CR_CERT_REQ; |
| 361 |
return 1; |
| 362 |
} |
| 363 |
goto err; |
| 364 |
} |
| 365 |
/* Fall through */ |
| 366 |
|
| 367 |
case TLS_ST_CR_CERT_REQ: |
| 368 |
if (mt == SSL3_MT_SERVER_DONE) { |
| 369 |
st->hand_state = TLS_ST_CR_SRVR_DONE; |
| 370 |
return 1; |
| 371 |
} |
| 372 |
break; |
| 373 |
|
| 374 |
case TLS_ST_CW_FINISHED: |
| 375 |
if (s->ext.ticket_expected) { |
| 376 |
if (mt == SSL3_MT_NEWSESSION_TICKET) { |
| 377 |
st->hand_state = TLS_ST_CR_SESSION_TICKET; |
| 378 |
return 1; |
| 379 |
} |
| 380 |
} else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 381 |
st->hand_state = TLS_ST_CR_CHANGE; |
| 382 |
return 1; |
| 383 |
} |
| 384 |
break; |
| 385 |
|
| 386 |
case TLS_ST_CR_SESSION_TICKET: |
| 387 |
if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 388 |
st->hand_state = TLS_ST_CR_CHANGE; |
| 389 |
return 1; |
| 390 |
} |
| 391 |
break; |
| 392 |
|
| 393 |
case TLS_ST_CR_CHANGE: |
| 394 |
if (mt == SSL3_MT_FINISHED) { |
| 395 |
st->hand_state = TLS_ST_CR_FINISHED; |
| 396 |
return 1; |
| 397 |
} |
| 398 |
break; |
| 399 |
|
| 400 |
case TLS_ST_OK: |
| 401 |
if (mt == SSL3_MT_HELLO_REQUEST) { |
| 402 |
st->hand_state = TLS_ST_CR_HELLO_REQ; |
| 403 |
return 1; |
| 404 |
} |
| 405 |
break; |
| 406 |
} |
| 407 |
|
| 408 |
err: |
| 409 |
/* No valid transition found */ |
| 410 |
if (SSL_CONNECTION_IS_DTLS(s) && mt == SSL3_MT_CHANGE_CIPHER_SPEC) { |
| 411 |
BIO *rbio; |
| 412 |
|
| 413 |
/* |
| 414 |
* CCS messages don't have a message sequence number so this is probably |
| 415 |
* because of an out-of-order CCS. We'll just drop it. |
| 416 |
*/ |
| 417 |
s->init_num = 0; |
| 418 |
s->rwstate = SSL_READING; |
| 419 |
rbio = SSL_get_rbio(SSL_CONNECTION_GET_SSL(s)); |
| 420 |
BIO_clear_retry_flags(rbio); |
| 421 |
BIO_set_retry_read(rbio); |
| 422 |
return 0; |
| 423 |
} |
| 424 |
SSLfatal(s, SSL3_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
| 425 |
return 0; |
| 426 |
} |
| 427 |
|
| 428 |
static int do_compressed_cert(SSL_CONNECTION *sc) |
| 429 |
{ |
| 430 |
/* If we negotiated RPK, we won't try to compress it */ |
| 431 |
return sc->ext.client_cert_type == TLSEXT_cert_type_x509 |
| 432 |
&& sc->ext.compress_certificate_from_peer[0] != TLSEXT_comp_cert_none; |
| 433 |
} |
| 434 |
|
| 435 |
/* |
| 436 |
* ossl_statem_client13_write_transition() works out what handshake state to |
| 437 |
* move to next when the TLSv1.3 client is writing messages to be sent to the |
| 438 |
* server. |
| 439 |
*/ |
| 440 |
static WRITE_TRAN ossl_statem_client13_write_transition(SSL_CONNECTION *s) |
| 441 |
{ |
| 442 |
OSSL_STATEM *st = &s->statem; |
| 443 |
|
| 444 |
/* |
| 445 |
* Note: There are no cases for TLS_ST_BEFORE because we haven't negotiated |
| 446 |
* TLSv1.3 yet at that point. They are handled by |
| 447 |
* ossl_statem_client_write_transition(). |
| 448 |
*/ |
| 449 |
switch (st->hand_state) { |
| 450 |
default: |
| 451 |
/* Shouldn't happen */ |
| 452 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 453 |
return WRITE_TRAN_ERROR; |
| 454 |
|
| 455 |
case TLS_ST_CR_CERT_REQ: |
| 456 |
if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
| 457 |
if (do_compressed_cert(s)) |
| 458 |
st->hand_state = TLS_ST_CW_COMP_CERT; |
| 459 |
else |
| 460 |
st->hand_state = TLS_ST_CW_CERT; |
| 461 |
return WRITE_TRAN_CONTINUE; |
| 462 |
} |
| 463 |
/* |
| 464 |
* We should only get here if we received a CertificateRequest after |
| 465 |
* we already sent close_notify |
| 466 |
*/ |
| 467 |
if (!ossl_assert((s->shutdown & SSL_SENT_SHUTDOWN) != 0)) { |
| 468 |
/* Shouldn't happen - same as default case */ |
| 469 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 470 |
return WRITE_TRAN_ERROR; |
| 471 |
} |
| 472 |
st->hand_state = TLS_ST_OK; |
| 473 |
return WRITE_TRAN_CONTINUE; |
| 474 |
|
| 475 |
case TLS_ST_CR_FINISHED: |
| 476 |
if (s->early_data_state == SSL_EARLY_DATA_WRITE_RETRY |
| 477 |
|| s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING) |
| 478 |
st->hand_state = TLS_ST_PENDING_EARLY_DATA_END; |
| 479 |
else if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
| 480 |
&& s->hello_retry_request == SSL_HRR_NONE) |
| 481 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 482 |
else if (s->s3.tmp.cert_req == 0) |
| 483 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 484 |
else if (do_compressed_cert(s)) |
| 485 |
st->hand_state = TLS_ST_CW_COMP_CERT; |
| 486 |
else |
| 487 |
st->hand_state = TLS_ST_CW_CERT; |
| 488 |
|
| 489 |
s->ts_msg_read = ossl_time_now(); |
| 490 |
return WRITE_TRAN_CONTINUE; |
| 491 |
|
| 492 |
case TLS_ST_PENDING_EARLY_DATA_END: |
| 493 |
if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED && !SSL_NO_EOED(s)) { |
| 494 |
st->hand_state = TLS_ST_CW_END_OF_EARLY_DATA; |
| 495 |
return WRITE_TRAN_CONTINUE; |
| 496 |
} |
| 497 |
/* Fall through */ |
| 498 |
|
| 499 |
case TLS_ST_CW_END_OF_EARLY_DATA: |
| 500 |
case TLS_ST_CW_CHANGE: |
| 501 |
if (s->s3.tmp.cert_req == 0) |
| 502 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 503 |
else if (do_compressed_cert(s)) |
| 504 |
st->hand_state = TLS_ST_CW_COMP_CERT; |
| 505 |
else |
| 506 |
st->hand_state = TLS_ST_CW_CERT; |
| 507 |
return WRITE_TRAN_CONTINUE; |
| 508 |
|
| 509 |
case TLS_ST_CW_COMP_CERT: |
| 510 |
case TLS_ST_CW_CERT: |
| 511 |
/* If a non-empty Certificate we also send CertificateVerify */ |
| 512 |
st->hand_state = (s->s3.tmp.cert_req == 1) ? TLS_ST_CW_CERT_VRFY |
| 513 |
: TLS_ST_CW_FINISHED; |
| 514 |
return WRITE_TRAN_CONTINUE; |
| 515 |
|
| 516 |
case TLS_ST_CW_CERT_VRFY: |
| 517 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 518 |
return WRITE_TRAN_CONTINUE; |
| 519 |
|
| 520 |
case TLS_ST_CR_KEY_UPDATE: |
| 521 |
case TLS_ST_CW_KEY_UPDATE: |
| 522 |
case TLS_ST_CR_SESSION_TICKET: |
| 523 |
case TLS_ST_CW_FINISHED: |
| 524 |
st->hand_state = TLS_ST_OK; |
| 525 |
return WRITE_TRAN_CONTINUE; |
| 526 |
|
| 527 |
case TLS_ST_OK: |
| 528 |
if (s->key_update != SSL_KEY_UPDATE_NONE) { |
| 529 |
st->hand_state = TLS_ST_CW_KEY_UPDATE; |
| 530 |
return WRITE_TRAN_CONTINUE; |
| 531 |
} |
| 532 |
|
| 533 |
/* Try to read from the server instead */ |
| 534 |
return WRITE_TRAN_FINISHED; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
/* |
| 539 |
* ossl_statem_client_write_transition() works out what handshake state to |
| 540 |
* move to next when the client is writing messages to be sent to the server. |
| 541 |
*/ |
| 542 |
WRITE_TRAN ossl_statem_client_write_transition(SSL_CONNECTION *s) |
| 543 |
{ |
| 544 |
OSSL_STATEM *st = &s->statem; |
| 545 |
|
| 546 |
/* |
| 547 |
* Note that immediately before/after a ClientHello we don't know what |
| 548 |
* version we are going to negotiate yet, so we don't take this branch until |
| 549 |
* later |
| 550 |
*/ |
| 551 |
if (SSL_CONNECTION_IS_TLS13(s)) |
| 552 |
return ossl_statem_client13_write_transition(s); |
| 553 |
|
| 554 |
switch (st->hand_state) { |
| 555 |
default: |
| 556 |
/* Shouldn't happen */ |
| 557 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 558 |
return WRITE_TRAN_ERROR; |
| 559 |
|
| 560 |
case TLS_ST_OK: |
| 561 |
if (!s->renegotiate) { |
| 562 |
/* |
| 563 |
* We haven't requested a renegotiation ourselves so we must have |
| 564 |
* received a message from the server. Better read it. |
| 565 |
*/ |
| 566 |
return WRITE_TRAN_FINISHED; |
| 567 |
} |
| 568 |
/* Renegotiation */ |
| 569 |
/* fall thru */ |
| 570 |
case TLS_ST_BEFORE: |
| 571 |
st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 572 |
return WRITE_TRAN_CONTINUE; |
| 573 |
|
| 574 |
case TLS_ST_CW_CLNT_HELLO: |
| 575 |
if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
| 576 |
&& !SSL_IS_QUIC_HANDSHAKE(s)) { |
| 577 |
/* |
| 578 |
* We are assuming this is a TLSv1.3 connection, although we haven't |
| 579 |
* actually selected a version yet. |
| 580 |
*/ |
| 581 |
if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) |
| 582 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 583 |
else |
| 584 |
st->hand_state = TLS_ST_EARLY_DATA; |
| 585 |
return WRITE_TRAN_CONTINUE; |
| 586 |
} |
| 587 |
/* |
| 588 |
* No transition at the end of writing because we don't know what |
| 589 |
* we will be sent |
| 590 |
*/ |
| 591 |
s->ts_msg_write = ossl_time_now(); |
| 592 |
return WRITE_TRAN_FINISHED; |
| 593 |
|
| 594 |
case TLS_ST_CR_SRVR_HELLO: |
| 595 |
/* |
| 596 |
* We only get here in TLSv1.3. We just received an HRR, so issue a |
| 597 |
* CCS unless middlebox compat mode is off, or we already issued one |
| 598 |
* because we did early data. |
| 599 |
*/ |
| 600 |
if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0 |
| 601 |
&& s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) |
| 602 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 603 |
else |
| 604 |
st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 605 |
return WRITE_TRAN_CONTINUE; |
| 606 |
|
| 607 |
case TLS_ST_EARLY_DATA: |
| 608 |
s->ts_msg_write = ossl_time_now(); |
| 609 |
return WRITE_TRAN_FINISHED; |
| 610 |
|
| 611 |
case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 612 |
st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 613 |
return WRITE_TRAN_CONTINUE; |
| 614 |
|
| 615 |
case TLS_ST_CR_SRVR_DONE: |
| 616 |
s->ts_msg_read = ossl_time_now(); |
| 617 |
if (s->s3.tmp.cert_req) |
| 618 |
st->hand_state = TLS_ST_CW_CERT; |
| 619 |
else |
| 620 |
st->hand_state = TLS_ST_CW_KEY_EXCH; |
| 621 |
return WRITE_TRAN_CONTINUE; |
| 622 |
|
| 623 |
case TLS_ST_CW_CERT: |
| 624 |
st->hand_state = TLS_ST_CW_KEY_EXCH; |
| 625 |
return WRITE_TRAN_CONTINUE; |
| 626 |
|
| 627 |
case TLS_ST_CW_KEY_EXCH: |
| 628 |
/* |
| 629 |
* For TLS, cert_req is set to 2, so a cert chain of nothing is |
| 630 |
* sent, but no verify packet is sent |
| 631 |
*/ |
| 632 |
/* |
| 633 |
* XXX: For now, we do not support client authentication in ECDH |
| 634 |
* cipher suites with ECDH (rather than ECDSA) certificates. We |
| 635 |
* need to skip the certificate verify message when client's |
| 636 |
* ECDH public key is sent inside the client certificate. |
| 637 |
*/ |
| 638 |
if (s->s3.tmp.cert_req == 1) { |
| 639 |
st->hand_state = TLS_ST_CW_CERT_VRFY; |
| 640 |
} else { |
| 641 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 642 |
} |
| 643 |
if (s->s3.flags & TLS1_FLAGS_SKIP_CERT_VERIFY) { |
| 644 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 645 |
} |
| 646 |
return WRITE_TRAN_CONTINUE; |
| 647 |
|
| 648 |
case TLS_ST_CW_CERT_VRFY: |
| 649 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 650 |
return WRITE_TRAN_CONTINUE; |
| 651 |
|
| 652 |
case TLS_ST_CW_CHANGE: |
| 653 |
if (s->hello_retry_request == SSL_HRR_PENDING) { |
| 654 |
st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 655 |
} else if (s->early_data_state == SSL_EARLY_DATA_CONNECTING) { |
| 656 |
st->hand_state = TLS_ST_EARLY_DATA; |
| 657 |
} else { |
| 658 |
#if defined(OPENSSL_NO_NEXTPROTONEG) |
| 659 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 660 |
#else |
| 661 |
if (!SSL_CONNECTION_IS_DTLS(s) && s->s3.npn_seen) |
| 662 |
st->hand_state = TLS_ST_CW_NEXT_PROTO; |
| 663 |
else |
| 664 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 665 |
#endif |
| 666 |
} |
| 667 |
return WRITE_TRAN_CONTINUE; |
| 668 |
|
| 669 |
#if !defined(OPENSSL_NO_NEXTPROTONEG) |
| 670 |
case TLS_ST_CW_NEXT_PROTO: |
| 671 |
st->hand_state = TLS_ST_CW_FINISHED; |
| 672 |
return WRITE_TRAN_CONTINUE; |
| 673 |
#endif |
| 674 |
|
| 675 |
case TLS_ST_CW_FINISHED: |
| 676 |
if (s->hit) { |
| 677 |
st->hand_state = TLS_ST_OK; |
| 678 |
return WRITE_TRAN_CONTINUE; |
| 679 |
} else { |
| 680 |
return WRITE_TRAN_FINISHED; |
| 681 |
} |
| 682 |
|
| 683 |
case TLS_ST_CR_FINISHED: |
| 684 |
if (s->hit) { |
| 685 |
st->hand_state = TLS_ST_CW_CHANGE; |
| 686 |
return WRITE_TRAN_CONTINUE; |
| 687 |
} else { |
| 688 |
st->hand_state = TLS_ST_OK; |
| 689 |
return WRITE_TRAN_CONTINUE; |
| 690 |
} |
| 691 |
|
| 692 |
case TLS_ST_CR_HELLO_REQ: |
| 693 |
/* |
| 694 |
* If we can renegotiate now then do so, otherwise wait for a more |
| 695 |
* convenient time. |
| 696 |
*/ |
| 697 |
if (ssl3_renegotiate_check(SSL_CONNECTION_GET_SSL(s), 1)) { |
| 698 |
if (!tls_setup_handshake(s)) { |
| 699 |
/* SSLfatal() already called */ |
| 700 |
return WRITE_TRAN_ERROR; |
| 701 |
} |
| 702 |
st->hand_state = TLS_ST_CW_CLNT_HELLO; |
| 703 |
return WRITE_TRAN_CONTINUE; |
| 704 |
} |
| 705 |
st->hand_state = TLS_ST_OK; |
| 706 |
return WRITE_TRAN_CONTINUE; |
| 707 |
} |
| 708 |
} |
| 709 |
|
| 710 |
/* |
| 711 |
* Perform any pre work that needs to be done prior to sending a message from |
| 712 |
* the client to the server. |
| 713 |
*/ |
| 714 |
WORK_STATE ossl_statem_client_pre_work(SSL_CONNECTION *s, WORK_STATE wst) |
| 715 |
{ |
| 716 |
OSSL_STATEM *st = &s->statem; |
| 717 |
|
| 718 |
switch (st->hand_state) { |
| 719 |
default: |
| 720 |
/* No pre work to be done */ |
| 721 |
break; |
| 722 |
|
| 723 |
case TLS_ST_CW_CLNT_HELLO: |
| 724 |
s->shutdown = 0; |
| 725 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 726 |
/* every DTLS ClientHello resets Finished MAC */ |
| 727 |
if (!ssl3_init_finished_mac(s)) { |
| 728 |
/* SSLfatal() already called */ |
| 729 |
return WORK_ERROR; |
| 730 |
} |
| 731 |
} else if (s->ext.early_data == SSL_EARLY_DATA_REJECTED) { |
| 732 |
/* |
| 733 |
* This must be a second ClientHello after an HRR following an |
| 734 |
* earlier rejected attempt to send early data. Since we were |
| 735 |
* previously encrypting the early data we now need to reset the |
| 736 |
* write record layer in order to write in plaintext again. |
| 737 |
*/ |
| 738 |
if (!ssl_set_new_record_layer(s, |
| 739 |
TLS_ANY_VERSION, |
| 740 |
OSSL_RECORD_DIRECTION_WRITE, |
| 741 |
OSSL_RECORD_PROTECTION_LEVEL_NONE, |
| 742 |
NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
| 743 |
NULL, 0, NID_undef, NULL, NULL, |
| 744 |
NULL)) { |
| 745 |
/* SSLfatal already called */ |
| 746 |
return WORK_ERROR; |
| 747 |
} |
| 748 |
} |
| 749 |
break; |
| 750 |
|
| 751 |
case TLS_ST_CW_CHANGE: |
| 752 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 753 |
if (s->hit) { |
| 754 |
/* |
| 755 |
* We're into the last flight so we don't retransmit these |
| 756 |
* messages unless we need to. |
| 757 |
*/ |
| 758 |
st->use_timer = 0; |
| 759 |
} |
| 760 |
#ifndef OPENSSL_NO_SCTP |
| 761 |
if (BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s)))) { |
| 762 |
/* Calls SSLfatal() as required */ |
| 763 |
return dtls_wait_for_dry(s); |
| 764 |
} |
| 765 |
#endif |
| 766 |
} |
| 767 |
break; |
| 768 |
|
| 769 |
case TLS_ST_PENDING_EARLY_DATA_END: |
| 770 |
/* |
| 771 |
* If we've been called by SSL_do_handshake()/SSL_write(), or we did not |
| 772 |
* attempt to write early data before calling SSL_read() then we press |
| 773 |
* on with the handshake. Otherwise we pause here. |
| 774 |
*/ |
| 775 |
if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING |
| 776 |
|| s->early_data_state == SSL_EARLY_DATA_NONE) |
| 777 |
return WORK_FINISHED_CONTINUE; |
| 778 |
/* Fall through */ |
| 779 |
|
| 780 |
case TLS_ST_EARLY_DATA: |
| 781 |
return tls_finish_handshake(s, wst, 0, 1); |
| 782 |
|
| 783 |
case TLS_ST_OK: |
| 784 |
/* Calls SSLfatal() as required */ |
| 785 |
return tls_finish_handshake(s, wst, 1, 1); |
| 786 |
} |
| 787 |
|
| 788 |
return WORK_FINISHED_CONTINUE; |
| 789 |
} |
| 790 |
|
| 791 |
/* |
| 792 |
* Perform any work that needs to be done after sending a message from the |
| 793 |
* client to the server. |
| 794 |
*/ |
| 795 |
WORK_STATE ossl_statem_client_post_work(SSL_CONNECTION *s, WORK_STATE wst) |
| 796 |
{ |
| 797 |
OSSL_STATEM *st = &s->statem; |
| 798 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 799 |
|
| 800 |
s->init_num = 0; |
| 801 |
|
| 802 |
switch (st->hand_state) { |
| 803 |
default: |
| 804 |
/* No post work to be done */ |
| 805 |
break; |
| 806 |
|
| 807 |
case TLS_ST_CW_CLNT_HELLO: |
| 808 |
if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
| 809 |
&& s->max_early_data > 0) { |
| 810 |
/* |
| 811 |
* We haven't selected TLSv1.3 yet so we don't call the change |
| 812 |
* cipher state function associated with the SSL_METHOD. Instead |
| 813 |
* we call tls13_change_cipher_state() directly. |
| 814 |
*/ |
| 815 |
if ((s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0) { |
| 816 |
if (!tls13_change_cipher_state(s, |
| 817 |
SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 818 |
/* SSLfatal() already called */ |
| 819 |
return WORK_ERROR; |
| 820 |
} |
| 821 |
} |
| 822 |
/* else we're in compat mode so we delay flushing until after CCS */ |
| 823 |
} else if (!statem_flush(s)) { |
| 824 |
return WORK_MORE_A; |
| 825 |
} |
| 826 |
|
| 827 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 828 |
/* Treat the next message as the first packet */ |
| 829 |
s->first_packet = 1; |
| 830 |
} |
| 831 |
break; |
| 832 |
|
| 833 |
case TLS_ST_CW_KEY_EXCH: |
| 834 |
if (tls_client_key_exchange_post_work(s) == 0) { |
| 835 |
/* SSLfatal() already called */ |
| 836 |
return WORK_ERROR; |
| 837 |
} |
| 838 |
break; |
| 839 |
|
| 840 |
case TLS_ST_CW_CHANGE: |
| 841 |
if (SSL_CONNECTION_IS_TLS13(s) |
| 842 |
|| s->hello_retry_request == SSL_HRR_PENDING) |
| 843 |
break; |
| 844 |
if (s->early_data_state == SSL_EARLY_DATA_CONNECTING |
| 845 |
&& s->max_early_data > 0) { |
| 846 |
/* |
| 847 |
* We haven't selected TLSv1.3 yet so we don't call the change |
| 848 |
* cipher state function associated with the SSL_METHOD. Instead |
| 849 |
* we call tls13_change_cipher_state() directly. |
| 850 |
*/ |
| 851 |
if (!tls13_change_cipher_state(s, |
| 852 |
SSL3_CC_EARLY | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) |
| 853 |
return WORK_ERROR; |
| 854 |
break; |
| 855 |
} |
| 856 |
s->session->cipher = s->s3.tmp.new_cipher; |
| 857 |
#ifdef OPENSSL_NO_COMP |
| 858 |
s->session->compress_meth = 0; |
| 859 |
#else |
| 860 |
if (s->s3.tmp.new_compression == NULL) |
| 861 |
s->session->compress_meth = 0; |
| 862 |
else |
| 863 |
s->session->compress_meth = s->s3.tmp.new_compression->id; |
| 864 |
#endif |
| 865 |
if (!ssl->method->ssl3_enc->setup_key_block(s)) { |
| 866 |
/* SSLfatal() already called */ |
| 867 |
return WORK_ERROR; |
| 868 |
} |
| 869 |
|
| 870 |
if (!ssl->method->ssl3_enc->change_cipher_state(s, |
| 871 |
SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 872 |
/* SSLfatal() already called */ |
| 873 |
return WORK_ERROR; |
| 874 |
} |
| 875 |
|
| 876 |
#ifndef OPENSSL_NO_SCTP |
| 877 |
if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
| 878 |
/* |
| 879 |
* Change to new shared key of SCTP-Auth, will be ignored if |
| 880 |
* no SCTP used. |
| 881 |
*/ |
| 882 |
BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
| 883 |
0, NULL); |
| 884 |
} |
| 885 |
#endif |
| 886 |
break; |
| 887 |
|
| 888 |
case TLS_ST_CW_FINISHED: |
| 889 |
#ifndef OPENSSL_NO_SCTP |
| 890 |
if (wst == WORK_MORE_A && SSL_CONNECTION_IS_DTLS(s) && s->hit == 0) { |
| 891 |
/* |
| 892 |
* Change to new shared key of SCTP-Auth, will be ignored if |
| 893 |
* no SCTP used. |
| 894 |
*/ |
| 895 |
BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY, |
| 896 |
0, NULL); |
| 897 |
} |
| 898 |
#endif |
| 899 |
if (statem_flush(s) != 1) |
| 900 |
return WORK_MORE_B; |
| 901 |
|
| 902 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 903 |
if (!tls13_save_handshake_digest_for_pha(s)) { |
| 904 |
/* SSLfatal() already called */ |
| 905 |
return WORK_ERROR; |
| 906 |
} |
| 907 |
if (s->post_handshake_auth != SSL_PHA_REQUESTED) { |
| 908 |
if (!ssl->method->ssl3_enc->change_cipher_state(s, |
| 909 |
SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 910 |
/* SSLfatal() already called */ |
| 911 |
return WORK_ERROR; |
| 912 |
} |
| 913 |
/* |
| 914 |
* For QUIC we deferred setting up these keys until now so |
| 915 |
* that we can ensure write keys are always set up before read |
| 916 |
* keys. |
| 917 |
*/ |
| 918 |
if (SSL_IS_QUIC_HANDSHAKE(s) |
| 919 |
&& !ssl->method->ssl3_enc->change_cipher_state(s, |
| 920 |
SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
| 921 |
/* SSLfatal() already called */ |
| 922 |
return WORK_ERROR; |
| 923 |
} |
| 924 |
} |
| 925 |
} |
| 926 |
break; |
| 927 |
|
| 928 |
case TLS_ST_CW_KEY_UPDATE: |
| 929 |
if (statem_flush(s) != 1) |
| 930 |
return WORK_MORE_A; |
| 931 |
if (!tls13_update_key(s, 1)) { |
| 932 |
/* SSLfatal() already called */ |
| 933 |
return WORK_ERROR; |
| 934 |
} |
| 935 |
break; |
| 936 |
} |
| 937 |
|
| 938 |
return WORK_FINISHED_CONTINUE; |
| 939 |
} |
| 940 |
|
| 941 |
/* |
| 942 |
* Get the message construction function and message type for sending from the |
| 943 |
* client |
| 944 |
* |
| 945 |
* Valid return values are: |
| 946 |
* 1: Success |
| 947 |
* 0: Error |
| 948 |
*/ |
| 949 |
int ossl_statem_client_construct_message(SSL_CONNECTION *s, |
| 950 |
confunc_f *confunc, int *mt) |
| 951 |
{ |
| 952 |
OSSL_STATEM *st = &s->statem; |
| 953 |
|
| 954 |
switch (st->hand_state) { |
| 955 |
default: |
| 956 |
/* Shouldn't happen */ |
| 957 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_STATE); |
| 958 |
return 0; |
| 959 |
|
| 960 |
case TLS_ST_CW_CHANGE: |
| 961 |
if (SSL_CONNECTION_IS_DTLS(s)) |
| 962 |
*confunc = dtls_construct_change_cipher_spec; |
| 963 |
else |
| 964 |
*confunc = tls_construct_change_cipher_spec; |
| 965 |
*mt = SSL3_MT_CHANGE_CIPHER_SPEC; |
| 966 |
break; |
| 967 |
|
| 968 |
case TLS_ST_CW_CLNT_HELLO: |
| 969 |
*confunc = tls_construct_client_hello; |
| 970 |
*mt = SSL3_MT_CLIENT_HELLO; |
| 971 |
break; |
| 972 |
|
| 973 |
case TLS_ST_CW_END_OF_EARLY_DATA: |
| 974 |
*confunc = tls_construct_end_of_early_data; |
| 975 |
*mt = SSL3_MT_END_OF_EARLY_DATA; |
| 976 |
break; |
| 977 |
|
| 978 |
case TLS_ST_PENDING_EARLY_DATA_END: |
| 979 |
*confunc = NULL; |
| 980 |
*mt = SSL3_MT_DUMMY; |
| 981 |
break; |
| 982 |
|
| 983 |
case TLS_ST_CW_CERT: |
| 984 |
*confunc = tls_construct_client_certificate; |
| 985 |
*mt = SSL3_MT_CERTIFICATE; |
| 986 |
break; |
| 987 |
|
| 988 |
#ifndef OPENSSL_NO_COMP_ALG |
| 989 |
case TLS_ST_CW_COMP_CERT: |
| 990 |
*confunc = tls_construct_client_compressed_certificate; |
| 991 |
*mt = SSL3_MT_COMPRESSED_CERTIFICATE; |
| 992 |
break; |
| 993 |
#endif |
| 994 |
|
| 995 |
case TLS_ST_CW_KEY_EXCH: |
| 996 |
*confunc = tls_construct_client_key_exchange; |
| 997 |
*mt = SSL3_MT_CLIENT_KEY_EXCHANGE; |
| 998 |
break; |
| 999 |
|
| 1000 |
case TLS_ST_CW_CERT_VRFY: |
| 1001 |
*confunc = tls_construct_cert_verify; |
| 1002 |
*mt = SSL3_MT_CERTIFICATE_VERIFY; |
| 1003 |
break; |
| 1004 |
|
| 1005 |
#if !defined(OPENSSL_NO_NEXTPROTONEG) |
| 1006 |
case TLS_ST_CW_NEXT_PROTO: |
| 1007 |
*confunc = tls_construct_next_proto; |
| 1008 |
*mt = SSL3_MT_NEXT_PROTO; |
| 1009 |
break; |
| 1010 |
#endif |
| 1011 |
case TLS_ST_CW_FINISHED: |
| 1012 |
*confunc = tls_construct_finished; |
| 1013 |
*mt = SSL3_MT_FINISHED; |
| 1014 |
break; |
| 1015 |
|
| 1016 |
case TLS_ST_CW_KEY_UPDATE: |
| 1017 |
*confunc = tls_construct_key_update; |
| 1018 |
*mt = SSL3_MT_KEY_UPDATE; |
| 1019 |
break; |
| 1020 |
} |
| 1021 |
|
| 1022 |
return 1; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* Returns the maximum allowed length for the current message that we are |
| 1027 |
* reading. Excludes the message header. |
| 1028 |
*/ |
| 1029 |
size_t ossl_statem_client_max_message_size(SSL_CONNECTION *s) |
| 1030 |
{ |
| 1031 |
OSSL_STATEM *st = &s->statem; |
| 1032 |
|
| 1033 |
switch (st->hand_state) { |
| 1034 |
default: |
| 1035 |
/* Shouldn't happen */ |
| 1036 |
return 0; |
| 1037 |
|
| 1038 |
case TLS_ST_CR_SRVR_HELLO: |
| 1039 |
return SERVER_HELLO_MAX_LENGTH; |
| 1040 |
|
| 1041 |
case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 1042 |
return HELLO_VERIFY_REQUEST_MAX_LENGTH; |
| 1043 |
|
| 1044 |
case TLS_ST_CR_COMP_CERT: |
| 1045 |
case TLS_ST_CR_CERT: |
| 1046 |
return s->max_cert_list; |
| 1047 |
|
| 1048 |
case TLS_ST_CR_CERT_VRFY: |
| 1049 |
return CERTIFICATE_VERIFY_MAX_LENGTH; |
| 1050 |
|
| 1051 |
case TLS_ST_CR_CERT_STATUS: |
| 1052 |
return SSL3_RT_MAX_PLAIN_LENGTH; |
| 1053 |
|
| 1054 |
case TLS_ST_CR_KEY_EXCH: |
| 1055 |
return SERVER_KEY_EXCH_MAX_LENGTH; |
| 1056 |
|
| 1057 |
case TLS_ST_CR_CERT_REQ: |
| 1058 |
/* |
| 1059 |
* Set to s->max_cert_list for compatibility with previous releases. In |
| 1060 |
* practice these messages can get quite long if servers are configured |
| 1061 |
* to provide a long list of acceptable CAs |
| 1062 |
*/ |
| 1063 |
return s->max_cert_list; |
| 1064 |
|
| 1065 |
case TLS_ST_CR_SRVR_DONE: |
| 1066 |
return SERVER_HELLO_DONE_MAX_LENGTH; |
| 1067 |
|
| 1068 |
case TLS_ST_CR_CHANGE: |
| 1069 |
if (s->version == DTLS1_BAD_VER) |
| 1070 |
return 3; |
| 1071 |
return CCS_MAX_LENGTH; |
| 1072 |
|
| 1073 |
case TLS_ST_CR_SESSION_TICKET: |
| 1074 |
return (SSL_CONNECTION_IS_TLS13(s)) ? SESSION_TICKET_MAX_LENGTH_TLS13 |
| 1075 |
: SESSION_TICKET_MAX_LENGTH_TLS12; |
| 1076 |
|
| 1077 |
case TLS_ST_CR_FINISHED: |
| 1078 |
return FINISHED_MAX_LENGTH; |
| 1079 |
|
| 1080 |
case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
| 1081 |
return ENCRYPTED_EXTENSIONS_MAX_LENGTH; |
| 1082 |
|
| 1083 |
case TLS_ST_CR_KEY_UPDATE: |
| 1084 |
return KEY_UPDATE_MAX_LENGTH; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* Process a message that the client has received from the server. |
| 1090 |
*/ |
| 1091 |
MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL_CONNECTION *s, |
| 1092 |
PACKET *pkt) |
| 1093 |
{ |
| 1094 |
OSSL_STATEM *st = &s->statem; |
| 1095 |
|
| 1096 |
switch (st->hand_state) { |
| 1097 |
default: |
| 1098 |
/* Shouldn't happen */ |
| 1099 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1100 |
return MSG_PROCESS_ERROR; |
| 1101 |
|
| 1102 |
case TLS_ST_CR_SRVR_HELLO: |
| 1103 |
return tls_process_server_hello(s, pkt); |
| 1104 |
|
| 1105 |
case DTLS_ST_CR_HELLO_VERIFY_REQUEST: |
| 1106 |
return dtls_process_hello_verify(s, pkt); |
| 1107 |
|
| 1108 |
case TLS_ST_CR_CERT: |
| 1109 |
return tls_process_server_certificate(s, pkt); |
| 1110 |
|
| 1111 |
#ifndef OPENSSL_NO_COMP_ALG |
| 1112 |
case TLS_ST_CR_COMP_CERT: |
| 1113 |
return tls_process_server_compressed_certificate(s, pkt); |
| 1114 |
#endif |
| 1115 |
|
| 1116 |
case TLS_ST_CR_CERT_VRFY: |
| 1117 |
return tls_process_cert_verify(s, pkt); |
| 1118 |
|
| 1119 |
case TLS_ST_CR_CERT_STATUS: |
| 1120 |
return tls_process_cert_status(s, pkt); |
| 1121 |
|
| 1122 |
case TLS_ST_CR_KEY_EXCH: |
| 1123 |
return tls_process_key_exchange(s, pkt); |
| 1124 |
|
| 1125 |
case TLS_ST_CR_CERT_REQ: |
| 1126 |
return tls_process_certificate_request(s, pkt); |
| 1127 |
|
| 1128 |
case TLS_ST_CR_SRVR_DONE: |
| 1129 |
return tls_process_server_done(s, pkt); |
| 1130 |
|
| 1131 |
case TLS_ST_CR_CHANGE: |
| 1132 |
return tls_process_change_cipher_spec(s, pkt); |
| 1133 |
|
| 1134 |
case TLS_ST_CR_SESSION_TICKET: |
| 1135 |
return tls_process_new_session_ticket(s, pkt); |
| 1136 |
|
| 1137 |
case TLS_ST_CR_FINISHED: |
| 1138 |
return tls_process_finished(s, pkt); |
| 1139 |
|
| 1140 |
case TLS_ST_CR_HELLO_REQ: |
| 1141 |
return tls_process_hello_req(s, pkt); |
| 1142 |
|
| 1143 |
case TLS_ST_CR_ENCRYPTED_EXTENSIONS: |
| 1144 |
return tls_process_encrypted_extensions(s, pkt); |
| 1145 |
|
| 1146 |
case TLS_ST_CR_KEY_UPDATE: |
| 1147 |
return tls_process_key_update(s, pkt); |
| 1148 |
} |
| 1149 |
} |
| 1150 |
|
| 1151 |
/* |
| 1152 |
* Perform any further processing required following the receipt of a message |
| 1153 |
* from the server |
| 1154 |
*/ |
| 1155 |
WORK_STATE ossl_statem_client_post_process_message(SSL_CONNECTION *s, |
| 1156 |
WORK_STATE wst) |
| 1157 |
{ |
| 1158 |
OSSL_STATEM *st = &s->statem; |
| 1159 |
|
| 1160 |
switch (st->hand_state) { |
| 1161 |
default: |
| 1162 |
/* Shouldn't happen */ |
| 1163 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1164 |
return WORK_ERROR; |
| 1165 |
|
| 1166 |
case TLS_ST_CR_CERT: |
| 1167 |
case TLS_ST_CR_COMP_CERT: |
| 1168 |
return tls_post_process_server_certificate(s, wst); |
| 1169 |
|
| 1170 |
case TLS_ST_CR_CERT_VRFY: |
| 1171 |
case TLS_ST_CR_CERT_REQ: |
| 1172 |
return tls_prepare_client_certificate(s, wst); |
| 1173 |
} |
| 1174 |
} |
| 1175 |
|
| 1176 |
CON_FUNC_RETURN tls_construct_client_hello(SSL_CONNECTION *s, WPACKET *pkt) |
| 1177 |
{ |
| 1178 |
unsigned char *p; |
| 1179 |
size_t sess_id_len; |
| 1180 |
int i, protverr; |
| 1181 |
#ifndef OPENSSL_NO_COMP |
| 1182 |
SSL_COMP *comp; |
| 1183 |
#endif |
| 1184 |
SSL_SESSION *sess = s->session; |
| 1185 |
unsigned char *session_id; |
| 1186 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 1187 |
|
| 1188 |
/* Work out what SSL/TLS/DTLS version to use */ |
| 1189 |
protverr = ssl_set_client_hello_version(s); |
| 1190 |
if (protverr != 0) { |
| 1191 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, protverr); |
| 1192 |
return CON_FUNC_ERROR; |
| 1193 |
} |
| 1194 |
|
| 1195 |
if (sess == NULL |
| 1196 |
|| !ssl_version_supported(s, sess->ssl_version, NULL) |
| 1197 |
|| !SSL_SESSION_is_resumable(sess)) { |
| 1198 |
if (s->hello_retry_request == SSL_HRR_NONE |
| 1199 |
&& !ssl_get_new_session(s, 0)) { |
| 1200 |
/* SSLfatal() already called */ |
| 1201 |
return CON_FUNC_ERROR; |
| 1202 |
} |
| 1203 |
} |
| 1204 |
/* else use the pre-loaded session */ |
| 1205 |
|
| 1206 |
p = s->s3.client_random; |
| 1207 |
|
| 1208 |
/* |
| 1209 |
* for DTLS if client_random is initialized, reuse it, we are |
| 1210 |
* required to use same upon reply to HelloVerify |
| 1211 |
*/ |
| 1212 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 1213 |
size_t idx; |
| 1214 |
i = 1; |
| 1215 |
for (idx = 0; idx < sizeof(s->s3.client_random); idx++) { |
| 1216 |
if (p[idx]) { |
| 1217 |
i = 0; |
| 1218 |
break; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} else { |
| 1222 |
i = (s->hello_retry_request == SSL_HRR_NONE); |
| 1223 |
} |
| 1224 |
|
| 1225 |
if (i && ssl_fill_hello_random(s, 0, p, sizeof(s->s3.client_random), DOWNGRADE_NONE) <= 0) { |
| 1226 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1227 |
return CON_FUNC_ERROR; |
| 1228 |
} |
| 1229 |
|
| 1230 |
/*- |
| 1231 |
* version indicates the negotiated version: for example from |
| 1232 |
* an SSLv2/v3 compatible client hello). The client_version |
| 1233 |
* field is the maximum version we permit and it is also |
| 1234 |
* used in RSA encrypted premaster secrets. Some servers can |
| 1235 |
* choke if we initially report a higher version then |
| 1236 |
* renegotiate to a lower one in the premaster secret. This |
| 1237 |
* didn't happen with TLS 1.0 as most servers supported it |
| 1238 |
* but it can with TLS 1.1 or later if the server only supports |
| 1239 |
* 1.0. |
| 1240 |
* |
| 1241 |
* Possible scenario with previous logic: |
| 1242 |
* 1. Client hello indicates TLS 1.2 |
| 1243 |
* 2. Server hello says TLS 1.0 |
| 1244 |
* 3. RSA encrypted premaster secret uses 1.2. |
| 1245 |
* 4. Handshake proceeds using TLS 1.0. |
| 1246 |
* 5. Server sends hello request to renegotiate. |
| 1247 |
* 6. Client hello indicates TLS v1.0 as we now |
| 1248 |
* know that is maximum server supports. |
| 1249 |
* 7. Server chokes on RSA encrypted premaster secret |
| 1250 |
* containing version 1.0. |
| 1251 |
* |
| 1252 |
* For interoperability it should be OK to always use the |
| 1253 |
* maximum version we support in client hello and then rely |
| 1254 |
* on the checking of version to ensure the servers isn't |
| 1255 |
* being inconsistent: for example initially negotiating with |
| 1256 |
* TLS 1.0 and renegotiating with TLS 1.2. We do this by using |
| 1257 |
* client_version in client hello and not resetting it to |
| 1258 |
* the negotiated version. |
| 1259 |
* |
| 1260 |
* For TLS 1.3 we always set the ClientHello version to 1.2 and rely on the |
| 1261 |
* supported_versions extension for the real supported versions. |
| 1262 |
*/ |
| 1263 |
if (!WPACKET_put_bytes_u16(pkt, s->client_version) |
| 1264 |
|| !WPACKET_memcpy(pkt, s->s3.client_random, SSL3_RANDOM_SIZE)) { |
| 1265 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1266 |
return CON_FUNC_ERROR; |
| 1267 |
} |
| 1268 |
|
| 1269 |
/* Session ID */ |
| 1270 |
session_id = s->session->session_id; |
| 1271 |
if (s->new_session || s->session->ssl_version == TLS1_3_VERSION) { |
| 1272 |
if (s->version == TLS1_3_VERSION |
| 1273 |
&& (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) { |
| 1274 |
sess_id_len = sizeof(s->tmp_session_id); |
| 1275 |
s->tmp_session_id_len = sess_id_len; |
| 1276 |
session_id = s->tmp_session_id; |
| 1277 |
if (s->hello_retry_request == SSL_HRR_NONE |
| 1278 |
&& RAND_bytes_ex(sctx->libctx, s->tmp_session_id, |
| 1279 |
sess_id_len, 0) |
| 1280 |
<= 0) { |
| 1281 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1282 |
return CON_FUNC_ERROR; |
| 1283 |
} |
| 1284 |
} else { |
| 1285 |
sess_id_len = 0; |
| 1286 |
} |
| 1287 |
} else { |
| 1288 |
assert(s->session->session_id_length <= sizeof(s->session->session_id)); |
| 1289 |
sess_id_len = s->session->session_id_length; |
| 1290 |
if (s->version == TLS1_3_VERSION) { |
| 1291 |
s->tmp_session_id_len = sess_id_len; |
| 1292 |
memcpy(s->tmp_session_id, s->session->session_id, sess_id_len); |
| 1293 |
} |
| 1294 |
} |
| 1295 |
if (!WPACKET_start_sub_packet_u8(pkt) |
| 1296 |
|| (sess_id_len != 0 && !WPACKET_memcpy(pkt, session_id, sess_id_len)) |
| 1297 |
|| !WPACKET_close(pkt)) { |
| 1298 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1299 |
return CON_FUNC_ERROR; |
| 1300 |
} |
| 1301 |
|
| 1302 |
/* cookie stuff for DTLS */ |
| 1303 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 1304 |
if (s->d1->cookie_len > sizeof(s->d1->cookie) |
| 1305 |
|| !WPACKET_sub_memcpy_u8(pkt, s->d1->cookie, |
| 1306 |
s->d1->cookie_len)) { |
| 1307 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1308 |
return CON_FUNC_ERROR; |
| 1309 |
} |
| 1310 |
} |
| 1311 |
|
| 1312 |
/* Ciphers supported */ |
| 1313 |
if (!WPACKET_start_sub_packet_u16(pkt)) { |
| 1314 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1315 |
return CON_FUNC_ERROR; |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(SSL_CONNECTION_GET_SSL(s)), |
| 1319 |
pkt)) { |
| 1320 |
/* SSLfatal() already called */ |
| 1321 |
return CON_FUNC_ERROR; |
| 1322 |
} |
| 1323 |
if (!WPACKET_close(pkt)) { |
| 1324 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1325 |
return CON_FUNC_ERROR; |
| 1326 |
} |
| 1327 |
|
| 1328 |
/* COMPRESSION */ |
| 1329 |
if (!WPACKET_start_sub_packet_u8(pkt)) { |
| 1330 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1331 |
return CON_FUNC_ERROR; |
| 1332 |
} |
| 1333 |
#ifndef OPENSSL_NO_COMP |
| 1334 |
if (ssl_allow_compression(s) |
| 1335 |
&& sctx->comp_methods |
| 1336 |
&& (SSL_CONNECTION_IS_DTLS(s) |
| 1337 |
|| s->s3.tmp.max_ver < TLS1_3_VERSION)) { |
| 1338 |
int compnum = sk_SSL_COMP_num(sctx->comp_methods); |
| 1339 |
for (i = 0; i < compnum; i++) { |
| 1340 |
comp = sk_SSL_COMP_value(sctx->comp_methods, i); |
| 1341 |
if (!WPACKET_put_bytes_u8(pkt, comp->id)) { |
| 1342 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1343 |
return CON_FUNC_ERROR; |
| 1344 |
} |
| 1345 |
} |
| 1346 |
} |
| 1347 |
#endif |
| 1348 |
/* Add the NULL method */ |
| 1349 |
if (!WPACKET_put_bytes_u8(pkt, 0) || !WPACKET_close(pkt)) { |
| 1350 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1351 |
return CON_FUNC_ERROR; |
| 1352 |
} |
| 1353 |
|
| 1354 |
/* TLS extensions */ |
| 1355 |
if (!tls_construct_extensions(s, pkt, SSL_EXT_CLIENT_HELLO, NULL, 0)) { |
| 1356 |
/* SSLfatal() already called */ |
| 1357 |
return CON_FUNC_ERROR; |
| 1358 |
} |
| 1359 |
|
| 1360 |
return CON_FUNC_SUCCESS; |
| 1361 |
} |
| 1362 |
|
| 1363 |
MSG_PROCESS_RETURN dtls_process_hello_verify(SSL_CONNECTION *s, PACKET *pkt) |
| 1364 |
{ |
| 1365 |
size_t cookie_len; |
| 1366 |
PACKET cookiepkt; |
| 1367 |
|
| 1368 |
if (!PACKET_forward(pkt, 2) |
| 1369 |
|| !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) { |
| 1370 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1371 |
return MSG_PROCESS_ERROR; |
| 1372 |
} |
| 1373 |
|
| 1374 |
cookie_len = PACKET_remaining(&cookiepkt); |
| 1375 |
if (cookie_len > sizeof(s->d1->cookie)) { |
| 1376 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_LENGTH_TOO_LONG); |
| 1377 |
return MSG_PROCESS_ERROR; |
| 1378 |
} |
| 1379 |
|
| 1380 |
if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) { |
| 1381 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1382 |
return MSG_PROCESS_ERROR; |
| 1383 |
} |
| 1384 |
s->d1->cookie_len = cookie_len; |
| 1385 |
|
| 1386 |
return MSG_PROCESS_FINISHED_READING; |
| 1387 |
} |
| 1388 |
|
| 1389 |
static int set_client_ciphersuite(SSL_CONNECTION *s, |
| 1390 |
const unsigned char *cipherchars) |
| 1391 |
{ |
| 1392 |
STACK_OF(SSL_CIPHER) *sk; |
| 1393 |
const SSL_CIPHER *c; |
| 1394 |
int i; |
| 1395 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 1396 |
|
| 1397 |
c = ssl_get_cipher_by_char(s, cipherchars, 0); |
| 1398 |
if (c == NULL) { |
| 1399 |
/* unknown cipher */ |
| 1400 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CIPHER_RETURNED); |
| 1401 |
return 0; |
| 1402 |
} |
| 1403 |
/* |
| 1404 |
* If it is a disabled cipher we either didn't send it in client hello, |
| 1405 |
* or it's not allowed for the selected protocol. So we return an error. |
| 1406 |
*/ |
| 1407 |
if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK, 1)) { |
| 1408 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
| 1409 |
return 0; |
| 1410 |
} |
| 1411 |
|
| 1412 |
sk = ssl_get_ciphers_by_id(s); |
| 1413 |
i = sk_SSL_CIPHER_find(sk, c); |
| 1414 |
if (i < 0) { |
| 1415 |
/* we did not say we would use this cipher */ |
| 1416 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
| 1417 |
return 0; |
| 1418 |
} |
| 1419 |
|
| 1420 |
if (SSL_CONNECTION_IS_TLS13(s) && s->s3.tmp.new_cipher != NULL |
| 1421 |
&& s->s3.tmp.new_cipher->id != c->id) { |
| 1422 |
/* ServerHello selected a different ciphersuite to that in the HRR */ |
| 1423 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CIPHER_RETURNED); |
| 1424 |
return 0; |
| 1425 |
} |
| 1426 |
|
| 1427 |
/* |
| 1428 |
* Depending on the session caching (internal/external), the cipher |
| 1429 |
* and/or cipher_id values may not be set. Make sure that cipher_id is |
| 1430 |
* set and use it for comparison. |
| 1431 |
*/ |
| 1432 |
if (s->session->cipher != NULL) |
| 1433 |
s->session->cipher_id = s->session->cipher->id; |
| 1434 |
if (s->hit && (s->session->cipher_id != c->id)) { |
| 1435 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 1436 |
const EVP_MD *md = ssl_md(sctx, c->algorithm2); |
| 1437 |
|
| 1438 |
if (!ossl_assert(s->session->cipher != NULL)) { |
| 1439 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1440 |
return 0; |
| 1441 |
} |
| 1442 |
/* |
| 1443 |
* In TLSv1.3 it is valid for the server to select a different |
| 1444 |
* ciphersuite as long as the hash is the same. |
| 1445 |
*/ |
| 1446 |
if (md == NULL |
| 1447 |
|| md != ssl_md(sctx, s->session->cipher->algorithm2)) { |
| 1448 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1449 |
SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED); |
| 1450 |
return 0; |
| 1451 |
} |
| 1452 |
} else { |
| 1453 |
/* |
| 1454 |
* Prior to TLSv1.3 resuming a session always meant using the same |
| 1455 |
* ciphersuite. |
| 1456 |
*/ |
| 1457 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1458 |
SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED); |
| 1459 |
return 0; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
s->s3.tmp.new_cipher = c; |
| 1463 |
|
| 1464 |
return 1; |
| 1465 |
} |
| 1466 |
|
| 1467 |
MSG_PROCESS_RETURN tls_process_server_hello(SSL_CONNECTION *s, PACKET *pkt) |
| 1468 |
{ |
| 1469 |
PACKET session_id, extpkt; |
| 1470 |
size_t session_id_len; |
| 1471 |
const unsigned char *cipherchars; |
| 1472 |
int hrr = 0; |
| 1473 |
unsigned int compression; |
| 1474 |
unsigned int sversion; |
| 1475 |
unsigned int context; |
| 1476 |
RAW_EXTENSION *extensions = NULL; |
| 1477 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 1478 |
SSL *ussl = SSL_CONNECTION_GET_USER_SSL(s); |
| 1479 |
#ifndef OPENSSL_NO_COMP |
| 1480 |
SSL_COMP *comp; |
| 1481 |
#endif |
| 1482 |
|
| 1483 |
if (!PACKET_get_net_2(pkt, &sversion)) { |
| 1484 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1485 |
goto err; |
| 1486 |
} |
| 1487 |
|
| 1488 |
/* load the server random */ |
| 1489 |
if (s->version == TLS1_3_VERSION |
| 1490 |
&& sversion == TLS1_2_VERSION |
| 1491 |
&& PACKET_remaining(pkt) >= SSL3_RANDOM_SIZE |
| 1492 |
&& memcmp(hrrrandom, PACKET_data(pkt), SSL3_RANDOM_SIZE) == 0) { |
| 1493 |
if (s->hello_retry_request != SSL_HRR_NONE) { |
| 1494 |
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
| 1495 |
goto err; |
| 1496 |
} |
| 1497 |
s->hello_retry_request = SSL_HRR_PENDING; |
| 1498 |
/* Tell the record layer that we know we're going to get TLSv1.3 */ |
| 1499 |
if (!ssl_set_record_protocol_version(s, s->version)) { |
| 1500 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1501 |
goto err; |
| 1502 |
} |
| 1503 |
hrr = 1; |
| 1504 |
if (!PACKET_forward(pkt, SSL3_RANDOM_SIZE)) { |
| 1505 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1506 |
goto err; |
| 1507 |
} |
| 1508 |
} else { |
| 1509 |
if (!PACKET_copy_bytes(pkt, s->s3.server_random, SSL3_RANDOM_SIZE)) { |
| 1510 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1511 |
goto err; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
/* Get the session-id. */ |
| 1516 |
if (!PACKET_get_length_prefixed_1(pkt, &session_id)) { |
| 1517 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1518 |
goto err; |
| 1519 |
} |
| 1520 |
session_id_len = PACKET_remaining(&session_id); |
| 1521 |
if (session_id_len > sizeof(s->session->session_id) |
| 1522 |
|| session_id_len > SSL3_SESSION_ID_SIZE) { |
| 1523 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_SSL3_SESSION_ID_TOO_LONG); |
| 1524 |
goto err; |
| 1525 |
} |
| 1526 |
|
| 1527 |
if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) { |
| 1528 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1529 |
goto err; |
| 1530 |
} |
| 1531 |
|
| 1532 |
if (!PACKET_get_1(pkt, &compression)) { |
| 1533 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 1534 |
goto err; |
| 1535 |
} |
| 1536 |
|
| 1537 |
/* TLS extensions */ |
| 1538 |
if (PACKET_remaining(pkt) == 0 && !hrr) { |
| 1539 |
PACKET_null_init(&extpkt); |
| 1540 |
} else if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
| 1541 |
|| PACKET_remaining(pkt) != 0) { |
| 1542 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
| 1543 |
goto err; |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (!hrr) { |
| 1547 |
if (!tls_collect_extensions(s, &extpkt, |
| 1548 |
SSL_EXT_TLS1_2_SERVER_HELLO |
| 1549 |
| SSL_EXT_TLS1_3_SERVER_HELLO, |
| 1550 |
&extensions, NULL, 1)) { |
| 1551 |
/* SSLfatal() already called */ |
| 1552 |
goto err; |
| 1553 |
} |
| 1554 |
|
| 1555 |
if (!ssl_choose_client_version(s, sversion, extensions)) { |
| 1556 |
/* SSLfatal() already called */ |
| 1557 |
goto err; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
|
| 1561 |
if (SSL_CONNECTION_IS_TLS13(s) || hrr) { |
| 1562 |
if (compression != 0) { |
| 1563 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1564 |
SSL_R_INVALID_COMPRESSION_ALGORITHM); |
| 1565 |
goto err; |
| 1566 |
} |
| 1567 |
|
| 1568 |
if (session_id_len != s->tmp_session_id_len |
| 1569 |
|| memcmp(PACKET_data(&session_id), s->tmp_session_id, |
| 1570 |
session_id_len) |
| 1571 |
!= 0) { |
| 1572 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_INVALID_SESSION_ID); |
| 1573 |
goto err; |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
if (hrr) { |
| 1578 |
if (!set_client_ciphersuite(s, cipherchars)) { |
| 1579 |
/* SSLfatal() already called */ |
| 1580 |
goto err; |
| 1581 |
} |
| 1582 |
|
| 1583 |
return tls_process_as_hello_retry_request(s, &extpkt); |
| 1584 |
} |
| 1585 |
|
| 1586 |
/* |
| 1587 |
* Now we have chosen the version we need to check again that the extensions |
| 1588 |
* are appropriate for this version. |
| 1589 |
*/ |
| 1590 |
context = SSL_CONNECTION_IS_TLS13(s) ? SSL_EXT_TLS1_3_SERVER_HELLO |
| 1591 |
: SSL_EXT_TLS1_2_SERVER_HELLO; |
| 1592 |
if (!tls_validate_all_contexts(s, context, extensions)) { |
| 1593 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_EXTENSION); |
| 1594 |
goto err; |
| 1595 |
} |
| 1596 |
|
| 1597 |
s->hit = 0; |
| 1598 |
|
| 1599 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 1600 |
/* |
| 1601 |
* In TLSv1.3 a ServerHello message signals a key change so the end of |
| 1602 |
* the message must be on a record boundary. |
| 1603 |
*/ |
| 1604 |
if (RECORD_LAYER_processed_read_pending(&s->rlayer)) { |
| 1605 |
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, |
| 1606 |
SSL_R_NOT_ON_RECORD_BOUNDARY); |
| 1607 |
goto err; |
| 1608 |
} |
| 1609 |
|
| 1610 |
/* This will set s->hit if we are resuming */ |
| 1611 |
if (!tls_parse_extension(s, TLSEXT_IDX_psk, |
| 1612 |
SSL_EXT_TLS1_3_SERVER_HELLO, |
| 1613 |
extensions, NULL, 0)) { |
| 1614 |
/* SSLfatal() already called */ |
| 1615 |
goto err; |
| 1616 |
} |
| 1617 |
} else { |
| 1618 |
/* |
| 1619 |
* Check if we can resume the session based on external pre-shared |
| 1620 |
* secret. EAP-FAST (RFC 4851) supports two types of session resumption. |
| 1621 |
* Resumption based on server-side state works with session IDs. |
| 1622 |
* Resumption based on pre-shared Protected Access Credentials (PACs) |
| 1623 |
* works by overriding the SessionTicket extension at the application |
| 1624 |
* layer, and does not send a session ID. (We do not know whether |
| 1625 |
* EAP-FAST servers would honour the session ID.) Therefore, the session |
| 1626 |
* ID alone is not a reliable indicator of session resumption, so we |
| 1627 |
* first check if we can resume, and later peek at the next handshake |
| 1628 |
* message to see if the server wants to resume. |
| 1629 |
*/ |
| 1630 |
if (s->version >= TLS1_VERSION |
| 1631 |
&& s->ext.session_secret_cb != NULL && s->session->ext.tick) { |
| 1632 |
const SSL_CIPHER *pref_cipher = NULL; |
| 1633 |
/* |
| 1634 |
* s->session->master_key_length is a size_t, but this is an int for |
| 1635 |
* backwards compat reasons |
| 1636 |
*/ |
| 1637 |
int master_key_length; |
| 1638 |
|
| 1639 |
master_key_length = sizeof(s->session->master_key); |
| 1640 |
if (s->ext.session_secret_cb(ussl, s->session->master_key, |
| 1641 |
&master_key_length, |
| 1642 |
NULL, &pref_cipher, |
| 1643 |
s->ext.session_secret_cb_arg) |
| 1644 |
&& master_key_length > 0) { |
| 1645 |
s->session->master_key_length = master_key_length; |
| 1646 |
s->session->cipher = pref_cipher ? pref_cipher : ssl_get_cipher_by_char(s, cipherchars, 0); |
| 1647 |
} else { |
| 1648 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1649 |
goto err; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
if (session_id_len != 0 |
| 1654 |
&& session_id_len == s->session->session_id_length |
| 1655 |
&& memcmp(PACKET_data(&session_id), s->session->session_id, |
| 1656 |
session_id_len) |
| 1657 |
== 0) |
| 1658 |
s->hit = 1; |
| 1659 |
} |
| 1660 |
|
| 1661 |
if (s->hit) { |
| 1662 |
if (s->sid_ctx_length != s->session->sid_ctx_length |
| 1663 |
|| memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) { |
| 1664 |
/* actually a client application bug */ |
| 1665 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1666 |
SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
| 1667 |
goto err; |
| 1668 |
} |
| 1669 |
} else { |
| 1670 |
/* |
| 1671 |
* If we were trying for session-id reuse but the server |
| 1672 |
* didn't resume, make a new SSL_SESSION. |
| 1673 |
* In the case of EAP-FAST and PAC, we do not send a session ID, |
| 1674 |
* so the PAC-based session secret is always preserved. It'll be |
| 1675 |
* overwritten if the server refuses resumption. |
| 1676 |
*/ |
| 1677 |
if (s->session->session_id_length > 0) { |
| 1678 |
ssl_tsan_counter(s->session_ctx, &s->session_ctx->stats.sess_miss); |
| 1679 |
if (!ssl_get_new_session(s, 0)) { |
| 1680 |
/* SSLfatal() already called */ |
| 1681 |
goto err; |
| 1682 |
} |
| 1683 |
} |
| 1684 |
|
| 1685 |
s->session->ssl_version = s->version; |
| 1686 |
/* |
| 1687 |
* In TLSv1.2 and below we save the session id we were sent so we can |
| 1688 |
* resume it later. In TLSv1.3 the session id we were sent is just an |
| 1689 |
* echo of what we originally sent in the ClientHello and should not be |
| 1690 |
* used for resumption. |
| 1691 |
*/ |
| 1692 |
if (!SSL_CONNECTION_IS_TLS13(s)) { |
| 1693 |
s->session->session_id_length = session_id_len; |
| 1694 |
/* session_id_len could be 0 */ |
| 1695 |
if (session_id_len > 0) |
| 1696 |
memcpy(s->session->session_id, PACKET_data(&session_id), |
| 1697 |
session_id_len); |
| 1698 |
} |
| 1699 |
} |
| 1700 |
|
| 1701 |
/* Session version and negotiated protocol version should match */ |
| 1702 |
if (s->version != s->session->ssl_version) { |
| 1703 |
SSLfatal(s, SSL_AD_PROTOCOL_VERSION, |
| 1704 |
SSL_R_SSL_SESSION_VERSION_MISMATCH); |
| 1705 |
goto err; |
| 1706 |
} |
| 1707 |
/* |
| 1708 |
* Now that we know the version, update the check to see if it's an allowed |
| 1709 |
* version. |
| 1710 |
*/ |
| 1711 |
s->s3.tmp.min_ver = s->version; |
| 1712 |
s->s3.tmp.max_ver = s->version; |
| 1713 |
|
| 1714 |
if (!set_client_ciphersuite(s, cipherchars)) { |
| 1715 |
/* SSLfatal() already called */ |
| 1716 |
goto err; |
| 1717 |
} |
| 1718 |
|
| 1719 |
#ifdef OPENSSL_NO_COMP |
| 1720 |
if (compression != 0) { |
| 1721 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1722 |
SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1723 |
goto err; |
| 1724 |
} |
| 1725 |
/* |
| 1726 |
* If compression is disabled we'd better not try to resume a session |
| 1727 |
* using compression. |
| 1728 |
*/ |
| 1729 |
if (s->session->compress_meth != 0) { |
| 1730 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_INCONSISTENT_COMPRESSION); |
| 1731 |
goto err; |
| 1732 |
} |
| 1733 |
#else |
| 1734 |
if (s->hit && compression != s->session->compress_meth) { |
| 1735 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1736 |
SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED); |
| 1737 |
goto err; |
| 1738 |
} |
| 1739 |
if (compression == 0) |
| 1740 |
comp = NULL; |
| 1741 |
else if (!ssl_allow_compression(s)) { |
| 1742 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_COMPRESSION_DISABLED); |
| 1743 |
goto err; |
| 1744 |
} else { |
| 1745 |
comp = ssl3_comp_find(SSL_CONNECTION_GET_CTX(s)->comp_methods, |
| 1746 |
compression); |
| 1747 |
} |
| 1748 |
|
| 1749 |
if (compression != 0 && comp == NULL) { |
| 1750 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, |
| 1751 |
SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM); |
| 1752 |
goto err; |
| 1753 |
} else { |
| 1754 |
s->s3.tmp.new_compression = comp; |
| 1755 |
} |
| 1756 |
#endif |
| 1757 |
|
| 1758 |
if (!tls_parse_all_extensions(s, context, extensions, NULL, 0, 1)) { |
| 1759 |
/* SSLfatal() already called */ |
| 1760 |
goto err; |
| 1761 |
} |
| 1762 |
|
| 1763 |
#ifndef OPENSSL_NO_SCTP |
| 1764 |
if (SSL_CONNECTION_IS_DTLS(s) && s->hit) { |
| 1765 |
unsigned char sctpauthkey[64]; |
| 1766 |
char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
| 1767 |
size_t labellen; |
| 1768 |
|
| 1769 |
/* |
| 1770 |
* Add new shared key for SCTP-Auth, will be ignored if |
| 1771 |
* no SCTP used. |
| 1772 |
*/ |
| 1773 |
memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
| 1774 |
sizeof(DTLS1_SCTP_AUTH_LABEL)); |
| 1775 |
|
| 1776 |
/* Don't include the terminating zero. */ |
| 1777 |
labellen = sizeof(labelbuffer) - 1; |
| 1778 |
if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) |
| 1779 |
labellen += 1; |
| 1780 |
|
| 1781 |
if (SSL_export_keying_material(ssl, sctpauthkey, |
| 1782 |
sizeof(sctpauthkey), |
| 1783 |
labelbuffer, |
| 1784 |
labellen, NULL, 0, 0) |
| 1785 |
<= 0) { |
| 1786 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 1787 |
goto err; |
| 1788 |
} |
| 1789 |
|
| 1790 |
BIO_ctrl(SSL_get_wbio(ssl), |
| 1791 |
BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
| 1792 |
sizeof(sctpauthkey), sctpauthkey); |
| 1793 |
} |
| 1794 |
#endif |
| 1795 |
|
| 1796 |
/* |
| 1797 |
* In TLSv1.3 we have some post-processing to change cipher state, otherwise |
| 1798 |
* we're done with this message |
| 1799 |
*/ |
| 1800 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 1801 |
if (!ssl->method->ssl3_enc->setup_key_block(s) |
| 1802 |
|| !tls13_store_handshake_traffic_hash(s)) { |
| 1803 |
/* SSLfatal() already called */ |
| 1804 |
goto err; |
| 1805 |
} |
| 1806 |
/* |
| 1807 |
* If we're not doing early-data and we're not going to send a dummy CCS |
| 1808 |
* (i.e. no middlebox compat mode) then we can change the write keys |
| 1809 |
* immediately. Otherwise we have to defer this until after all possible |
| 1810 |
* early data is written. We could just always defer until the last |
| 1811 |
* moment except QUIC needs it done at the same time as the read keys |
| 1812 |
* are changed. Since QUIC doesn't do TLS early data or need middlebox |
| 1813 |
* compat this doesn't cause a problem. |
| 1814 |
*/ |
| 1815 |
if (SSL_IS_QUIC_HANDSHAKE(s) |
| 1816 |
|| (s->early_data_state == SSL_EARLY_DATA_NONE |
| 1817 |
&& (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) == 0)) { |
| 1818 |
if (!ssl->method->ssl3_enc->change_cipher_state(s, |
| 1819 |
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE)) { |
| 1820 |
/* SSLfatal() already called */ |
| 1821 |
goto err; |
| 1822 |
} |
| 1823 |
} |
| 1824 |
if (!ssl->method->ssl3_enc->change_cipher_state(s, |
| 1825 |
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_READ)) { |
| 1826 |
/* SSLfatal() already called */ |
| 1827 |
goto err; |
| 1828 |
} |
| 1829 |
} |
| 1830 |
|
| 1831 |
OPENSSL_free(extensions); |
| 1832 |
return MSG_PROCESS_CONTINUE_READING; |
| 1833 |
err: |
| 1834 |
OPENSSL_free(extensions); |
| 1835 |
return MSG_PROCESS_ERROR; |
| 1836 |
} |
| 1837 |
|
| 1838 |
static MSG_PROCESS_RETURN tls_process_as_hello_retry_request(SSL_CONNECTION *s, |
| 1839 |
PACKET *extpkt) |
| 1840 |
{ |
| 1841 |
RAW_EXTENSION *extensions = NULL; |
| 1842 |
|
| 1843 |
/* |
| 1844 |
* If we were sending early_data then any alerts should not be sent using |
| 1845 |
* the old wrlmethod. |
| 1846 |
*/ |
| 1847 |
if (s->early_data_state == SSL_EARLY_DATA_FINISHED_WRITING |
| 1848 |
&& !ssl_set_new_record_layer(s, |
| 1849 |
TLS_ANY_VERSION, |
| 1850 |
OSSL_RECORD_DIRECTION_WRITE, |
| 1851 |
OSSL_RECORD_PROTECTION_LEVEL_NONE, |
| 1852 |
NULL, 0, NULL, 0, NULL, 0, NULL, 0, |
| 1853 |
NULL, 0, NID_undef, NULL, NULL, NULL)) { |
| 1854 |
/* SSLfatal already called */ |
| 1855 |
goto err; |
| 1856 |
} |
| 1857 |
/* We are definitely going to be using TLSv1.3 */ |
| 1858 |
s->rlayer.wrlmethod->set_protocol_version(s->rlayer.wrl, TLS1_3_VERSION); |
| 1859 |
|
| 1860 |
if (!tls_collect_extensions(s, extpkt, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
| 1861 |
&extensions, NULL, 1) |
| 1862 |
|| !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_HELLO_RETRY_REQUEST, |
| 1863 |
extensions, NULL, 0, 1)) { |
| 1864 |
/* SSLfatal() already called */ |
| 1865 |
goto err; |
| 1866 |
} |
| 1867 |
|
| 1868 |
OPENSSL_free(extensions); |
| 1869 |
extensions = NULL; |
| 1870 |
|
| 1871 |
if (s->ext.tls13_cookie_len == 0 && s->s3.tmp.pkey != NULL) { |
| 1872 |
/* |
| 1873 |
* We didn't receive a cookie or a new key_share so the next |
| 1874 |
* ClientHello will not change |
| 1875 |
*/ |
| 1876 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_NO_CHANGE_FOLLOWING_HRR); |
| 1877 |
goto err; |
| 1878 |
} |
| 1879 |
|
| 1880 |
/* |
| 1881 |
* Re-initialise the Transcript Hash. We're going to prepopulate it with |
| 1882 |
* a synthetic message_hash in place of ClientHello1. |
| 1883 |
*/ |
| 1884 |
if (!create_synthetic_message_hash(s, NULL, 0, NULL, 0)) { |
| 1885 |
/* SSLfatal() already called */ |
| 1886 |
goto err; |
| 1887 |
} |
| 1888 |
|
| 1889 |
/* |
| 1890 |
* Add this message to the Transcript Hash. Normally this is done |
| 1891 |
* automatically prior to the message processing stage. However due to the |
| 1892 |
* need to create the synthetic message hash, we defer that step until now |
| 1893 |
* for HRR messages. |
| 1894 |
*/ |
| 1895 |
if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data, |
| 1896 |
s->init_num + SSL3_HM_HEADER_LENGTH)) { |
| 1897 |
/* SSLfatal() already called */ |
| 1898 |
goto err; |
| 1899 |
} |
| 1900 |
|
| 1901 |
return MSG_PROCESS_FINISHED_READING; |
| 1902 |
err: |
| 1903 |
OPENSSL_free(extensions); |
| 1904 |
return MSG_PROCESS_ERROR; |
| 1905 |
} |
| 1906 |
|
| 1907 |
MSG_PROCESS_RETURN tls_process_server_rpk(SSL_CONNECTION *sc, PACKET *pkt) |
| 1908 |
{ |
| 1909 |
EVP_PKEY *peer_rpk = NULL; |
| 1910 |
|
| 1911 |
if (!tls_process_rpk(sc, pkt, &peer_rpk)) { |
| 1912 |
/* SSLfatal() already called */ |
| 1913 |
return MSG_PROCESS_ERROR; |
| 1914 |
} |
| 1915 |
|
| 1916 |
if (peer_rpk == NULL) { |
| 1917 |
SSLfatal(sc, SSL_AD_DECODE_ERROR, SSL_R_BAD_CERTIFICATE); |
| 1918 |
return MSG_PROCESS_ERROR; |
| 1919 |
} |
| 1920 |
|
| 1921 |
EVP_PKEY_free(sc->session->peer_rpk); |
| 1922 |
sc->session->peer_rpk = peer_rpk; |
| 1923 |
|
| 1924 |
return MSG_PROCESS_CONTINUE_PROCESSING; |
| 1925 |
} |
| 1926 |
|
| 1927 |
static WORK_STATE tls_post_process_server_rpk(SSL_CONNECTION *sc, |
| 1928 |
WORK_STATE wst) |
| 1929 |
{ |
| 1930 |
size_t certidx; |
| 1931 |
const SSL_CERT_LOOKUP *clu; |
| 1932 |
int v_ok; |
| 1933 |
|
| 1934 |
if (sc->session->peer_rpk == NULL) { |
| 1935 |
SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, |
| 1936 |
SSL_R_INVALID_RAW_PUBLIC_KEY); |
| 1937 |
return WORK_ERROR; |
| 1938 |
} |
| 1939 |
|
| 1940 |
if (sc->rwstate == SSL_RETRY_VERIFY) |
| 1941 |
sc->rwstate = SSL_NOTHING; |
| 1942 |
|
| 1943 |
ERR_set_mark(); |
| 1944 |
v_ok = ssl_verify_rpk(sc, sc->session->peer_rpk); |
| 1945 |
if (v_ok <= 0 && sc->verify_mode != SSL_VERIFY_NONE) { |
| 1946 |
ERR_clear_last_mark(); |
| 1947 |
SSLfatal(sc, ssl_x509err2alert(sc->verify_result), |
| 1948 |
SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 1949 |
return WORK_ERROR; |
| 1950 |
} |
| 1951 |
ERR_pop_to_mark(); /* but we keep s->verify_result */ |
| 1952 |
if (v_ok > 0 && sc->rwstate == SSL_RETRY_VERIFY) { |
| 1953 |
return WORK_MORE_A; |
| 1954 |
} |
| 1955 |
|
| 1956 |
if ((clu = ssl_cert_lookup_by_pkey(sc->session->peer_rpk, &certidx, |
| 1957 |
SSL_CONNECTION_GET_CTX(sc))) |
| 1958 |
== NULL) { |
| 1959 |
SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 1960 |
return WORK_ERROR; |
| 1961 |
} |
| 1962 |
|
| 1963 |
/* |
| 1964 |
* Check certificate type is consistent with ciphersuite. For TLS 1.3 |
| 1965 |
* skip check since TLS 1.3 ciphersuites can be used with any certificate |
| 1966 |
* type. |
| 1967 |
*/ |
| 1968 |
if (!SSL_CONNECTION_IS_TLS13(sc)) { |
| 1969 |
if ((clu->amask & sc->s3.tmp.new_cipher->algorithm_auth) == 0) { |
| 1970 |
SSLfatal(sc, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_RPK_TYPE); |
| 1971 |
return WORK_ERROR; |
| 1972 |
} |
| 1973 |
} |
| 1974 |
|
| 1975 |
/* Ensure there is no peer/peer_chain */ |
| 1976 |
X509_free(sc->session->peer); |
| 1977 |
sc->session->peer = NULL; |
| 1978 |
sk_X509_pop_free(sc->session->peer_chain, X509_free); |
| 1979 |
sc->session->peer_chain = NULL; |
| 1980 |
sc->session->verify_result = sc->verify_result; |
| 1981 |
|
| 1982 |
/* Save the current hash state for when we receive the CertificateVerify */ |
| 1983 |
if (SSL_CONNECTION_IS_TLS13(sc) |
| 1984 |
&& !ssl_handshake_hash(sc, sc->cert_verify_hash, |
| 1985 |
sizeof(sc->cert_verify_hash), |
| 1986 |
&sc->cert_verify_hash_len)) { |
| 1987 |
/* SSLfatal() already called */ |
| 1988 |
return WORK_ERROR; |
| 1989 |
} |
| 1990 |
|
| 1991 |
return WORK_FINISHED_CONTINUE; |
| 1992 |
} |
| 1993 |
|
| 1994 |
/* prepare server cert verification by setting s->session->peer_chain from pkt */ |
| 1995 |
MSG_PROCESS_RETURN tls_process_server_certificate(SSL_CONNECTION *s, |
| 1996 |
PACKET *pkt) |
| 1997 |
{ |
| 1998 |
unsigned long cert_list_len, cert_len; |
| 1999 |
X509 *x = NULL; |
| 2000 |
const unsigned char *certstart, *certbytes; |
| 2001 |
size_t chainidx; |
| 2002 |
unsigned int context = 0; |
| 2003 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 2004 |
|
| 2005 |
if (s->ext.server_cert_type == TLSEXT_cert_type_rpk) |
| 2006 |
return tls_process_server_rpk(s, pkt); |
| 2007 |
if (s->ext.server_cert_type != TLSEXT_cert_type_x509) { |
| 2008 |
SSLfatal(s, SSL_AD_UNSUPPORTED_CERTIFICATE, |
| 2009 |
SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 2010 |
goto err; |
| 2011 |
} |
| 2012 |
|
| 2013 |
if ((s->session->peer_chain = sk_X509_new_null()) == NULL) { |
| 2014 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 2015 |
goto err; |
| 2016 |
} |
| 2017 |
|
| 2018 |
if ((SSL_CONNECTION_IS_TLS13(s) && !PACKET_get_1(pkt, &context)) |
| 2019 |
|| context != 0 |
| 2020 |
|| !PACKET_get_net_3(pkt, &cert_list_len) |
| 2021 |
|| PACKET_remaining(pkt) != cert_list_len |
| 2022 |
|| PACKET_remaining(pkt) == 0) { |
| 2023 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2024 |
goto err; |
| 2025 |
} |
| 2026 |
for (chainidx = 0; PACKET_remaining(pkt); chainidx++) { |
| 2027 |
if (!PACKET_get_net_3(pkt, &cert_len) |
| 2028 |
|| !PACKET_get_bytes(pkt, &certbytes, cert_len)) { |
| 2029 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
| 2030 |
goto err; |
| 2031 |
} |
| 2032 |
|
| 2033 |
certstart = certbytes; |
| 2034 |
x = X509_new_ex(sctx->libctx, sctx->propq); |
| 2035 |
if (x == NULL) { |
| 2036 |
SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_ASN1_LIB); |
| 2037 |
goto err; |
| 2038 |
} |
| 2039 |
if (d2i_X509(&x, (const unsigned char **)&certbytes, |
| 2040 |
cert_len) |
| 2041 |
== NULL) { |
| 2042 |
SSLfatal(s, SSL_AD_BAD_CERTIFICATE, ERR_R_ASN1_LIB); |
| 2043 |
goto err; |
| 2044 |
} |
| 2045 |
|
| 2046 |
if (certbytes != (certstart + cert_len)) { |
| 2047 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_CERT_LENGTH_MISMATCH); |
| 2048 |
goto err; |
| 2049 |
} |
| 2050 |
|
| 2051 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 2052 |
RAW_EXTENSION *rawexts = NULL; |
| 2053 |
PACKET extensions; |
| 2054 |
|
| 2055 |
if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { |
| 2056 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
| 2057 |
goto err; |
| 2058 |
} |
| 2059 |
if (!tls_collect_extensions(s, &extensions, |
| 2060 |
SSL_EXT_TLS1_3_CERTIFICATE, &rawexts, |
| 2061 |
NULL, chainidx == 0) |
| 2062 |
|| !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE, |
| 2063 |
rawexts, x, chainidx, |
| 2064 |
PACKET_remaining(pkt) == 0)) { |
| 2065 |
OPENSSL_free(rawexts); |
| 2066 |
/* SSLfatal already called */ |
| 2067 |
goto err; |
| 2068 |
} |
| 2069 |
OPENSSL_free(rawexts); |
| 2070 |
} |
| 2071 |
|
| 2072 |
if (!sk_X509_push(s->session->peer_chain, x)) { |
| 2073 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 2074 |
goto err; |
| 2075 |
} |
| 2076 |
x = NULL; |
| 2077 |
} |
| 2078 |
return MSG_PROCESS_CONTINUE_PROCESSING; |
| 2079 |
|
| 2080 |
err: |
| 2081 |
X509_free(x); |
| 2082 |
OSSL_STACK_OF_X509_free(s->session->peer_chain); |
| 2083 |
s->session->peer_chain = NULL; |
| 2084 |
return MSG_PROCESS_ERROR; |
| 2085 |
} |
| 2086 |
|
| 2087 |
/* |
| 2088 |
* Verify the s->session->peer_chain and check server cert type. |
| 2089 |
* On success set s->session->peer and s->session->verify_result. |
| 2090 |
* Else the peer certificate verification callback may request retry. |
| 2091 |
*/ |
| 2092 |
WORK_STATE tls_post_process_server_certificate(SSL_CONNECTION *s, |
| 2093 |
WORK_STATE wst) |
| 2094 |
{ |
| 2095 |
X509 *x; |
| 2096 |
EVP_PKEY *pkey = NULL; |
| 2097 |
const SSL_CERT_LOOKUP *clu; |
| 2098 |
size_t certidx; |
| 2099 |
int i; |
| 2100 |
|
| 2101 |
if (s->ext.server_cert_type == TLSEXT_cert_type_rpk) |
| 2102 |
return tls_post_process_server_rpk(s, wst); |
| 2103 |
|
| 2104 |
if (s->rwstate == SSL_RETRY_VERIFY) |
| 2105 |
s->rwstate = SSL_NOTHING; |
| 2106 |
|
| 2107 |
/* |
| 2108 |
* The documented interface is that SSL_VERIFY_PEER should be set in order |
| 2109 |
* for client side verification of the server certificate to take place. |
| 2110 |
* However, historically the code has only checked that *any* flag is set |
| 2111 |
* to cause server verification to take place. Use of the other flags makes |
| 2112 |
* no sense in client mode. An attempt to clean up the semantics was |
| 2113 |
* reverted because at least one application *only* set |
| 2114 |
* SSL_VERIFY_FAIL_IF_NO_PEER_CERT. Prior to the clean up this still caused |
| 2115 |
* server verification to take place, after the clean up it silently did |
| 2116 |
* nothing. SSL_CTX_set_verify()/SSL_set_verify() cannot validate the flags |
| 2117 |
* sent to them because they are void functions. Therefore, we now use the |
| 2118 |
* (less clean) historic behaviour of performing validation if any flag is |
| 2119 |
* set. The *documented* interface remains the same. |
| 2120 |
*/ |
| 2121 |
ERR_set_mark(); |
| 2122 |
i = ssl_verify_cert_chain(s, s->session->peer_chain); |
| 2123 |
if (i <= 0 && s->verify_mode != SSL_VERIFY_NONE) { |
| 2124 |
ERR_clear_last_mark(); |
| 2125 |
SSLfatal(s, ssl_x509err2alert(s->verify_result), |
| 2126 |
SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2127 |
return WORK_ERROR; |
| 2128 |
} |
| 2129 |
ERR_pop_to_mark(); /* but we keep s->verify_result */ |
| 2130 |
if (i > 0 && s->rwstate == SSL_RETRY_VERIFY) |
| 2131 |
return WORK_MORE_A; |
| 2132 |
|
| 2133 |
/* |
| 2134 |
* Inconsistency alert: cert_chain does include the peer's certificate, |
| 2135 |
* which we don't include in statem_srvr.c |
| 2136 |
*/ |
| 2137 |
x = sk_X509_value(s->session->peer_chain, 0); |
| 2138 |
|
| 2139 |
pkey = X509_get0_pubkey(x); |
| 2140 |
|
| 2141 |
if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) { |
| 2142 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 2143 |
SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS); |
| 2144 |
return WORK_ERROR; |
| 2145 |
} |
| 2146 |
|
| 2147 |
if ((clu = ssl_cert_lookup_by_pkey(pkey, &certidx, |
| 2148 |
SSL_CONNECTION_GET_CTX(s))) |
| 2149 |
== NULL) { |
| 2150 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_UNKNOWN_CERTIFICATE_TYPE); |
| 2151 |
return WORK_ERROR; |
| 2152 |
} |
| 2153 |
/* |
| 2154 |
* Check certificate type is consistent with ciphersuite. For TLS 1.3 |
| 2155 |
* skip check since TLS 1.3 ciphersuites can be used with any certificate |
| 2156 |
* type. |
| 2157 |
*/ |
| 2158 |
if (!SSL_CONNECTION_IS_TLS13(s)) { |
| 2159 |
if ((clu->amask & s->s3.tmp.new_cipher->algorithm_auth) == 0) { |
| 2160 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CERTIFICATE_TYPE); |
| 2161 |
return WORK_ERROR; |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
if (!X509_up_ref(x)) { |
| 2166 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2167 |
return WORK_ERROR; |
| 2168 |
} |
| 2169 |
|
| 2170 |
X509_free(s->session->peer); |
| 2171 |
s->session->peer = x; |
| 2172 |
s->session->verify_result = s->verify_result; |
| 2173 |
/* Ensure there is no RPK */ |
| 2174 |
EVP_PKEY_free(s->session->peer_rpk); |
| 2175 |
s->session->peer_rpk = NULL; |
| 2176 |
|
| 2177 |
/* Save the current hash state for when we receive the CertificateVerify */ |
| 2178 |
if (SSL_CONNECTION_IS_TLS13(s) |
| 2179 |
&& !ssl_handshake_hash(s, s->cert_verify_hash, |
| 2180 |
sizeof(s->cert_verify_hash), |
| 2181 |
&s->cert_verify_hash_len)) { |
| 2182 |
/* SSLfatal() already called */; |
| 2183 |
return WORK_ERROR; |
| 2184 |
} |
| 2185 |
return WORK_FINISHED_CONTINUE; |
| 2186 |
} |
| 2187 |
|
| 2188 |
#ifndef OPENSSL_NO_COMP_ALG |
| 2189 |
MSG_PROCESS_RETURN tls_process_server_compressed_certificate(SSL_CONNECTION *sc, PACKET *pkt) |
| 2190 |
{ |
| 2191 |
MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR; |
| 2192 |
PACKET tmppkt; |
| 2193 |
BUF_MEM *buf = BUF_MEM_new(); |
| 2194 |
|
| 2195 |
if (tls13_process_compressed_certificate(sc, pkt, &tmppkt, buf) != MSG_PROCESS_ERROR) |
| 2196 |
ret = tls_process_server_certificate(sc, &tmppkt); |
| 2197 |
|
| 2198 |
BUF_MEM_free(buf); |
| 2199 |
return ret; |
| 2200 |
} |
| 2201 |
#endif |
| 2202 |
|
| 2203 |
static int tls_process_ske_psk_preamble(SSL_CONNECTION *s, PACKET *pkt) |
| 2204 |
{ |
| 2205 |
#ifndef OPENSSL_NO_PSK |
| 2206 |
PACKET psk_identity_hint; |
| 2207 |
|
| 2208 |
/* PSK ciphersuites are preceded by an identity hint */ |
| 2209 |
|
| 2210 |
if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) { |
| 2211 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2212 |
return 0; |
| 2213 |
} |
| 2214 |
|
| 2215 |
/* |
| 2216 |
* Store PSK identity hint for later use, hint is used in |
| 2217 |
* tls_construct_client_key_exchange. Assume that the maximum length of |
| 2218 |
* a PSK identity hint can be as long as the maximum length of a PSK |
| 2219 |
* identity. |
| 2220 |
*/ |
| 2221 |
if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) { |
| 2222 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DATA_LENGTH_TOO_LONG); |
| 2223 |
return 0; |
| 2224 |
} |
| 2225 |
|
| 2226 |
if (PACKET_remaining(&psk_identity_hint) == 0) { |
| 2227 |
OPENSSL_free(s->session->psk_identity_hint); |
| 2228 |
s->session->psk_identity_hint = NULL; |
| 2229 |
} else if (!PACKET_strndup(&psk_identity_hint, |
| 2230 |
&s->session->psk_identity_hint)) { |
| 2231 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2232 |
return 0; |
| 2233 |
} |
| 2234 |
|
| 2235 |
return 1; |
| 2236 |
#else |
| 2237 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2238 |
return 0; |
| 2239 |
#endif |
| 2240 |
} |
| 2241 |
|
| 2242 |
static int tls_process_ske_srp(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
| 2243 |
{ |
| 2244 |
#ifndef OPENSSL_NO_SRP |
| 2245 |
PACKET prime, generator, salt, server_pub; |
| 2246 |
|
| 2247 |
if (!PACKET_get_length_prefixed_2(pkt, &prime) |
| 2248 |
|| !PACKET_get_length_prefixed_2(pkt, &generator) |
| 2249 |
|| !PACKET_get_length_prefixed_1(pkt, &salt) |
| 2250 |
|| !PACKET_get_length_prefixed_2(pkt, &server_pub)) { |
| 2251 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2252 |
return 0; |
| 2253 |
} |
| 2254 |
|
| 2255 |
if ((s->srp_ctx.N = BN_bin2bn(PACKET_data(&prime), |
| 2256 |
(int)PACKET_remaining(&prime), NULL)) |
| 2257 |
== NULL |
| 2258 |
|| (s->srp_ctx.g = BN_bin2bn(PACKET_data(&generator), |
| 2259 |
(int)PACKET_remaining(&generator), NULL)) |
| 2260 |
== NULL |
| 2261 |
|| (s->srp_ctx.s = BN_bin2bn(PACKET_data(&salt), |
| 2262 |
(int)PACKET_remaining(&salt), NULL)) |
| 2263 |
== NULL |
| 2264 |
|| (s->srp_ctx.B = BN_bin2bn(PACKET_data(&server_pub), |
| 2265 |
(int)PACKET_remaining(&server_pub), NULL)) |
| 2266 |
== NULL) { |
| 2267 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
| 2268 |
return 0; |
| 2269 |
} |
| 2270 |
|
| 2271 |
if (!srp_verify_server_param(s)) { |
| 2272 |
/* SSLfatal() already called */ |
| 2273 |
return 0; |
| 2274 |
} |
| 2275 |
|
| 2276 |
/* We must check if there is a certificate */ |
| 2277 |
if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
| 2278 |
*pkey = tls_get_peer_pkey(s); |
| 2279 |
|
| 2280 |
return 1; |
| 2281 |
#else |
| 2282 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2283 |
return 0; |
| 2284 |
#endif |
| 2285 |
} |
| 2286 |
|
| 2287 |
static int tls_process_ske_dhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
| 2288 |
{ |
| 2289 |
PACKET prime, generator, pub_key; |
| 2290 |
EVP_PKEY *peer_tmp = NULL; |
| 2291 |
BIGNUM *p = NULL, *g = NULL, *bnpub_key = NULL; |
| 2292 |
EVP_PKEY_CTX *pctx = NULL; |
| 2293 |
OSSL_PARAM *params = NULL; |
| 2294 |
OSSL_PARAM_BLD *tmpl = NULL; |
| 2295 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 2296 |
int ret = 0; |
| 2297 |
|
| 2298 |
if (!PACKET_get_length_prefixed_2(pkt, &prime) |
| 2299 |
|| !PACKET_get_length_prefixed_2(pkt, &generator) |
| 2300 |
|| !PACKET_get_length_prefixed_2(pkt, &pub_key)) { |
| 2301 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2302 |
return 0; |
| 2303 |
} |
| 2304 |
|
| 2305 |
p = BN_bin2bn(PACKET_data(&prime), (int)PACKET_remaining(&prime), NULL); |
| 2306 |
g = BN_bin2bn(PACKET_data(&generator), (int)PACKET_remaining(&generator), |
| 2307 |
NULL); |
| 2308 |
bnpub_key = BN_bin2bn(PACKET_data(&pub_key), |
| 2309 |
(int)PACKET_remaining(&pub_key), NULL); |
| 2310 |
if (p == NULL || g == NULL || bnpub_key == NULL) { |
| 2311 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_BN_LIB); |
| 2312 |
goto err; |
| 2313 |
} |
| 2314 |
|
| 2315 |
tmpl = OSSL_PARAM_BLD_new(); |
| 2316 |
if (tmpl == NULL |
| 2317 |
|| !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 2318 |
|| !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) |
| 2319 |
|| !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, |
| 2320 |
bnpub_key) |
| 2321 |
|| (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
| 2322 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2323 |
goto err; |
| 2324 |
} |
| 2325 |
|
| 2326 |
pctx = EVP_PKEY_CTX_new_from_name(sctx->libctx, "DH", sctx->propq); |
| 2327 |
if (pctx == NULL) { |
| 2328 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2329 |
goto err; |
| 2330 |
} |
| 2331 |
if (EVP_PKEY_fromdata_init(pctx) <= 0 |
| 2332 |
|| EVP_PKEY_fromdata(pctx, &peer_tmp, EVP_PKEY_KEYPAIR, params) <= 0) { |
| 2333 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_DH_VALUE); |
| 2334 |
goto err; |
| 2335 |
} |
| 2336 |
|
| 2337 |
EVP_PKEY_CTX_free(pctx); |
| 2338 |
pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, peer_tmp, sctx->propq); |
| 2339 |
if (pctx == NULL |
| 2340 |
/* |
| 2341 |
* EVP_PKEY_param_check() will verify that the DH params are using |
| 2342 |
* a safe prime. In this context, because we're using ephemeral DH, |
| 2343 |
* we're ok with it not being a safe prime. |
| 2344 |
* EVP_PKEY_param_check_quick() skips the safe prime check. |
| 2345 |
*/ |
| 2346 |
|| EVP_PKEY_param_check_quick(pctx) != 1 |
| 2347 |
|| EVP_PKEY_public_check(pctx) != 1) { |
| 2348 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_DH_VALUE); |
| 2349 |
goto err; |
| 2350 |
} |
| 2351 |
|
| 2352 |
if (!ssl_security(s, SSL_SECOP_TMP_DH, |
| 2353 |
EVP_PKEY_get_security_bits(peer_tmp), |
| 2354 |
0, peer_tmp)) { |
| 2355 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_DH_KEY_TOO_SMALL); |
| 2356 |
goto err; |
| 2357 |
} |
| 2358 |
|
| 2359 |
s->s3.peer_tmp = peer_tmp; |
| 2360 |
peer_tmp = NULL; |
| 2361 |
|
| 2362 |
/* |
| 2363 |
* FIXME: This makes assumptions about which ciphersuites come with |
| 2364 |
* public keys. We should have a less ad-hoc way of doing this |
| 2365 |
*/ |
| 2366 |
if (s->s3.tmp.new_cipher->algorithm_auth & (SSL_aRSA | SSL_aDSS)) |
| 2367 |
*pkey = tls_get_peer_pkey(s); |
| 2368 |
/* else anonymous DH, so no certificate or pkey. */ |
| 2369 |
|
| 2370 |
ret = 1; |
| 2371 |
|
| 2372 |
err: |
| 2373 |
OSSL_PARAM_BLD_free(tmpl); |
| 2374 |
OSSL_PARAM_free(params); |
| 2375 |
EVP_PKEY_free(peer_tmp); |
| 2376 |
EVP_PKEY_CTX_free(pctx); |
| 2377 |
BN_free(p); |
| 2378 |
BN_free(g); |
| 2379 |
BN_free(bnpub_key); |
| 2380 |
|
| 2381 |
return ret; |
| 2382 |
} |
| 2383 |
|
| 2384 |
static int tls_process_ske_ecdhe(SSL_CONNECTION *s, PACKET *pkt, EVP_PKEY **pkey) |
| 2385 |
{ |
| 2386 |
PACKET encoded_pt; |
| 2387 |
unsigned int curve_type, curve_id; |
| 2388 |
|
| 2389 |
/* |
| 2390 |
* Extract elliptic curve parameters and the server's ephemeral ECDH |
| 2391 |
* public key. We only support named (not generic) curves and |
| 2392 |
* ECParameters in this case is just three bytes. |
| 2393 |
*/ |
| 2394 |
if (!PACKET_get_1(pkt, &curve_type) || !PACKET_get_net_2(pkt, &curve_id)) { |
| 2395 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
| 2396 |
return 0; |
| 2397 |
} |
| 2398 |
/* |
| 2399 |
* Check curve is named curve type and one of our preferences, if not |
| 2400 |
* server has sent an invalid curve. |
| 2401 |
*/ |
| 2402 |
if (curve_type != NAMED_CURVE_TYPE |
| 2403 |
|| !tls1_check_group_id(s, curve_id, 1)) { |
| 2404 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_WRONG_CURVE); |
| 2405 |
return 0; |
| 2406 |
} |
| 2407 |
|
| 2408 |
if ((s->s3.peer_tmp = ssl_generate_param_group(s, curve_id)) == NULL) { |
| 2409 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 2410 |
SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS); |
| 2411 |
return 0; |
| 2412 |
} |
| 2413 |
|
| 2414 |
if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) { |
| 2415 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2416 |
return 0; |
| 2417 |
} |
| 2418 |
|
| 2419 |
if (EVP_PKEY_set1_encoded_public_key(s->s3.peer_tmp, |
| 2420 |
PACKET_data(&encoded_pt), |
| 2421 |
PACKET_remaining(&encoded_pt)) |
| 2422 |
<= 0) { |
| 2423 |
SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_R_BAD_ECPOINT); |
| 2424 |
return 0; |
| 2425 |
} |
| 2426 |
|
| 2427 |
/* |
| 2428 |
* The ECC/TLS specification does not mention the use of DSA to sign |
| 2429 |
* ECParameters in the server key exchange message. We do support RSA |
| 2430 |
* and ECDSA. |
| 2431 |
*/ |
| 2432 |
if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) |
| 2433 |
*pkey = tls_get_peer_pkey(s); |
| 2434 |
else if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aRSA) |
| 2435 |
*pkey = tls_get_peer_pkey(s); |
| 2436 |
/* else anonymous ECDH, so no certificate or pkey. */ |
| 2437 |
|
| 2438 |
/* Cache the agreed upon group in the SSL_SESSION */ |
| 2439 |
s->session->kex_group = curve_id; |
| 2440 |
return 1; |
| 2441 |
} |
| 2442 |
|
| 2443 |
MSG_PROCESS_RETURN tls_process_key_exchange(SSL_CONNECTION *s, PACKET *pkt) |
| 2444 |
{ |
| 2445 |
long alg_k; |
| 2446 |
EVP_PKEY *pkey = NULL; |
| 2447 |
EVP_MD_CTX *md_ctx = NULL; |
| 2448 |
EVP_PKEY_CTX *pctx = NULL; |
| 2449 |
PACKET save_param_start, signature; |
| 2450 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 2451 |
|
| 2452 |
alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
| 2453 |
|
| 2454 |
save_param_start = *pkt; |
| 2455 |
|
| 2456 |
EVP_PKEY_free(s->s3.peer_tmp); |
| 2457 |
s->s3.peer_tmp = NULL; |
| 2458 |
|
| 2459 |
if (alg_k & SSL_PSK) { |
| 2460 |
if (!tls_process_ske_psk_preamble(s, pkt)) { |
| 2461 |
/* SSLfatal() already called */ |
| 2462 |
goto err; |
| 2463 |
} |
| 2464 |
} |
| 2465 |
|
| 2466 |
/* Nothing else to do for plain PSK or RSAPSK */ |
| 2467 |
if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) { |
| 2468 |
} else if (alg_k & SSL_kSRP) { |
| 2469 |
if (!tls_process_ske_srp(s, pkt, &pkey)) { |
| 2470 |
/* SSLfatal() already called */ |
| 2471 |
goto err; |
| 2472 |
} |
| 2473 |
} else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
| 2474 |
if (!tls_process_ske_dhe(s, pkt, &pkey)) { |
| 2475 |
/* SSLfatal() already called */ |
| 2476 |
goto err; |
| 2477 |
} |
| 2478 |
} else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
| 2479 |
if (!tls_process_ske_ecdhe(s, pkt, &pkey)) { |
| 2480 |
/* SSLfatal() already called */ |
| 2481 |
goto err; |
| 2482 |
} |
| 2483 |
} else if (alg_k) { |
| 2484 |
SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_R_UNEXPECTED_MESSAGE); |
| 2485 |
goto err; |
| 2486 |
} |
| 2487 |
|
| 2488 |
/* if it was signed, check the signature */ |
| 2489 |
if (pkey != NULL) { |
| 2490 |
PACKET params; |
| 2491 |
const EVP_MD *md = NULL; |
| 2492 |
unsigned char *tbs; |
| 2493 |
size_t tbslen; |
| 2494 |
int rv; |
| 2495 |
|
| 2496 |
/* |
| 2497 |
* |pkt| now points to the beginning of the signature, so the difference |
| 2498 |
* equals the length of the parameters. |
| 2499 |
*/ |
| 2500 |
if (!PACKET_get_sub_packet(&save_param_start, ¶ms, |
| 2501 |
PACKET_remaining(&save_param_start) - PACKET_remaining(pkt))) { |
| 2502 |
SSLfatal(s, SSL_AD_DECODE_ERROR, ERR_R_INTERNAL_ERROR); |
| 2503 |
goto err; |
| 2504 |
} |
| 2505 |
|
| 2506 |
if (SSL_USE_SIGALGS(s)) { |
| 2507 |
unsigned int sigalg; |
| 2508 |
|
| 2509 |
if (!PACKET_get_net_2(pkt, &sigalg)) { |
| 2510 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_TOO_SHORT); |
| 2511 |
goto err; |
| 2512 |
} |
| 2513 |
if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) { |
| 2514 |
/* SSLfatal() already called */ |
| 2515 |
goto err; |
| 2516 |
} |
| 2517 |
} else if (!tls1_set_peer_legacy_sigalg(s, pkey)) { |
| 2518 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 2519 |
SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED); |
| 2520 |
goto err; |
| 2521 |
} |
| 2522 |
|
| 2523 |
if (!tls1_lookup_md(sctx, s->s3.tmp.peer_sigalg, &md)) { |
| 2524 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 2525 |
SSL_R_NO_SUITABLE_DIGEST_ALGORITHM); |
| 2526 |
goto err; |
| 2527 |
} |
| 2528 |
if (SSL_USE_SIGALGS(s)) |
| 2529 |
OSSL_TRACE1(TLS, "USING TLSv1.2 HASH %s\n", |
| 2530 |
md == NULL ? "n/a" : EVP_MD_get0_name(md)); |
| 2531 |
|
| 2532 |
if (!PACKET_get_length_prefixed_2(pkt, &signature) |
| 2533 |
|| PACKET_remaining(pkt) != 0) { |
| 2534 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2535 |
goto err; |
| 2536 |
} |
| 2537 |
|
| 2538 |
md_ctx = EVP_MD_CTX_new(); |
| 2539 |
if (md_ctx == NULL) { |
| 2540 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 2541 |
goto err; |
| 2542 |
} |
| 2543 |
|
| 2544 |
if (EVP_DigestVerifyInit_ex(md_ctx, &pctx, |
| 2545 |
md == NULL ? NULL : EVP_MD_get0_name(md), |
| 2546 |
sctx->libctx, sctx->propq, pkey, |
| 2547 |
NULL) |
| 2548 |
<= 0) { |
| 2549 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 2550 |
goto err; |
| 2551 |
} |
| 2552 |
if (SSL_USE_PSS(s)) { |
| 2553 |
if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0 |
| 2554 |
|| EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, |
| 2555 |
RSA_PSS_SALTLEN_DIGEST) |
| 2556 |
<= 0) { |
| 2557 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 2558 |
goto err; |
| 2559 |
} |
| 2560 |
} |
| 2561 |
tbslen = construct_key_exchange_tbs(s, &tbs, PACKET_data(¶ms), |
| 2562 |
PACKET_remaining(¶ms)); |
| 2563 |
if (tbslen == 0) { |
| 2564 |
/* SSLfatal() already called */ |
| 2565 |
goto err; |
| 2566 |
} |
| 2567 |
|
| 2568 |
rv = EVP_DigestVerify(md_ctx, PACKET_data(&signature), |
| 2569 |
PACKET_remaining(&signature), tbs, tbslen); |
| 2570 |
OPENSSL_free(tbs); |
| 2571 |
if (rv <= 0) { |
| 2572 |
SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_R_BAD_SIGNATURE); |
| 2573 |
goto err; |
| 2574 |
} |
| 2575 |
EVP_MD_CTX_free(md_ctx); |
| 2576 |
md_ctx = NULL; |
| 2577 |
} else { |
| 2578 |
/* aNULL, aSRP or PSK do not need public keys */ |
| 2579 |
if (!(s->s3.tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP)) |
| 2580 |
&& !(alg_k & SSL_PSK)) { |
| 2581 |
/* Might be wrong key type, check it */ |
| 2582 |
if (ssl3_check_cert_and_algorithm(s)) { |
| 2583 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_DATA); |
| 2584 |
} |
| 2585 |
/* else this shouldn't happen, SSLfatal() already called */ |
| 2586 |
goto err; |
| 2587 |
} |
| 2588 |
/* still data left over */ |
| 2589 |
if (PACKET_remaining(pkt) != 0) { |
| 2590 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_EXTRA_DATA_IN_MESSAGE); |
| 2591 |
goto err; |
| 2592 |
} |
| 2593 |
} |
| 2594 |
|
| 2595 |
return MSG_PROCESS_CONTINUE_READING; |
| 2596 |
err: |
| 2597 |
EVP_MD_CTX_free(md_ctx); |
| 2598 |
return MSG_PROCESS_ERROR; |
| 2599 |
} |
| 2600 |
|
| 2601 |
MSG_PROCESS_RETURN tls_process_certificate_request(SSL_CONNECTION *s, |
| 2602 |
PACKET *pkt) |
| 2603 |
{ |
| 2604 |
/* Clear certificate validity flags */ |
| 2605 |
if (s->s3.tmp.valid_flags != NULL) |
| 2606 |
memset(s->s3.tmp.valid_flags, 0, s->ssl_pkey_num * sizeof(uint32_t)); |
| 2607 |
else |
| 2608 |
s->s3.tmp.valid_flags = OPENSSL_calloc(s->ssl_pkey_num, sizeof(uint32_t)); |
| 2609 |
|
| 2610 |
/* Give up for good if allocation didn't work */ |
| 2611 |
if (s->s3.tmp.valid_flags == NULL) { |
| 2612 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 2613 |
return MSG_PROCESS_ERROR; |
| 2614 |
} |
| 2615 |
|
| 2616 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 2617 |
PACKET reqctx, extensions; |
| 2618 |
RAW_EXTENSION *rawexts = NULL; |
| 2619 |
|
| 2620 |
if ((s->shutdown & SSL_SENT_SHUTDOWN) != 0) { |
| 2621 |
/* |
| 2622 |
* We already sent close_notify. This can only happen in TLSv1.3 |
| 2623 |
* post-handshake messages. We can't reasonably respond to this, so |
| 2624 |
* we just ignore it |
| 2625 |
*/ |
| 2626 |
return MSG_PROCESS_FINISHED_READING; |
| 2627 |
} |
| 2628 |
|
| 2629 |
/* Free and zero certificate types: it is not present in TLS 1.3 */ |
| 2630 |
OPENSSL_free(s->s3.tmp.ctype); |
| 2631 |
s->s3.tmp.ctype = NULL; |
| 2632 |
s->s3.tmp.ctype_len = 0; |
| 2633 |
OPENSSL_free(s->pha_context); |
| 2634 |
s->pha_context = NULL; |
| 2635 |
s->pha_context_len = 0; |
| 2636 |
|
| 2637 |
if (!PACKET_get_length_prefixed_1(pkt, &reqctx) || !PACKET_memdup(&reqctx, &s->pha_context, &s->pha_context_len)) { |
| 2638 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2639 |
return MSG_PROCESS_ERROR; |
| 2640 |
} |
| 2641 |
|
| 2642 |
if (!PACKET_get_length_prefixed_2(pkt, &extensions)) { |
| 2643 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_LENGTH); |
| 2644 |
return MSG_PROCESS_ERROR; |
| 2645 |
} |
| 2646 |
if (!tls_collect_extensions(s, &extensions, |
| 2647 |
SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
| 2648 |
&rawexts, NULL, 1) |
| 2649 |
|| !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_CERTIFICATE_REQUEST, |
| 2650 |
rawexts, NULL, 0, 1)) { |
| 2651 |
/* SSLfatal() already called */ |
| 2652 |
OPENSSL_free(rawexts); |
| 2653 |
return MSG_PROCESS_ERROR; |
| 2654 |
} |
| 2655 |
OPENSSL_free(rawexts); |
| 2656 |
if (!tls1_process_sigalgs(s)) { |
| 2657 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_LENGTH); |
| 2658 |
return MSG_PROCESS_ERROR; |
| 2659 |
} |
| 2660 |
} else { |
| 2661 |
PACKET ctypes; |
| 2662 |
|
| 2663 |
/* get the certificate types */ |
| 2664 |
if (!PACKET_get_length_prefixed_1(pkt, &ctypes)) { |
| 2665 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2666 |
return MSG_PROCESS_ERROR; |
| 2667 |
} |
| 2668 |
|
| 2669 |
if (!PACKET_memdup(&ctypes, &s->s3.tmp.ctype, &s->s3.tmp.ctype_len)) { |
| 2670 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2671 |
return MSG_PROCESS_ERROR; |
| 2672 |
} |
| 2673 |
|
| 2674 |
if (SSL_USE_SIGALGS(s)) { |
| 2675 |
PACKET sigalgs; |
| 2676 |
|
| 2677 |
if (!PACKET_get_length_prefixed_2(pkt, &sigalgs)) { |
| 2678 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2679 |
return MSG_PROCESS_ERROR; |
| 2680 |
} |
| 2681 |
|
| 2682 |
/* |
| 2683 |
* Despite this being for certificates, preserve compatibility |
| 2684 |
* with pre-TLS 1.3 and use the regular sigalgs field. |
| 2685 |
*/ |
| 2686 |
if (!tls1_save_sigalgs(s, &sigalgs, 0)) { |
| 2687 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 2688 |
SSL_R_SIGNATURE_ALGORITHMS_ERROR); |
| 2689 |
return MSG_PROCESS_ERROR; |
| 2690 |
} |
| 2691 |
if (!tls1_process_sigalgs(s)) { |
| 2692 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
| 2693 |
return MSG_PROCESS_ERROR; |
| 2694 |
} |
| 2695 |
} |
| 2696 |
|
| 2697 |
/* get the CA RDNs */ |
| 2698 |
if (!parse_ca_names(s, pkt)) { |
| 2699 |
/* SSLfatal() already called */ |
| 2700 |
return MSG_PROCESS_ERROR; |
| 2701 |
} |
| 2702 |
} |
| 2703 |
|
| 2704 |
if (PACKET_remaining(pkt) != 0) { |
| 2705 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2706 |
return MSG_PROCESS_ERROR; |
| 2707 |
} |
| 2708 |
|
| 2709 |
/* we should setup a certificate to return.... */ |
| 2710 |
s->s3.tmp.cert_req = 1; |
| 2711 |
|
| 2712 |
/* |
| 2713 |
* In TLSv1.3 we don't prepare the client certificate yet. We wait until |
| 2714 |
* after the CertificateVerify message has been received. This is because |
| 2715 |
* in TLSv1.3 the CertificateRequest arrives before the Certificate message |
| 2716 |
* but in TLSv1.2 it is the other way around. We want to make sure that |
| 2717 |
* SSL_get1_peer_certificate() returns something sensible in |
| 2718 |
* client_cert_cb. |
| 2719 |
*/ |
| 2720 |
if (SSL_CONNECTION_IS_TLS13(s) |
| 2721 |
&& s->post_handshake_auth != SSL_PHA_REQUESTED) |
| 2722 |
return MSG_PROCESS_CONTINUE_READING; |
| 2723 |
|
| 2724 |
return MSG_PROCESS_CONTINUE_PROCESSING; |
| 2725 |
} |
| 2726 |
|
| 2727 |
MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL_CONNECTION *s, |
| 2728 |
PACKET *pkt) |
| 2729 |
{ |
| 2730 |
unsigned int ticklen; |
| 2731 |
unsigned long ticket_lifetime_hint, age_add = 0; |
| 2732 |
unsigned int sess_len; |
| 2733 |
RAW_EXTENSION *exts = NULL; |
| 2734 |
PACKET nonce; |
| 2735 |
EVP_MD *sha256 = NULL; |
| 2736 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 2737 |
|
| 2738 |
PACKET_null_init(&nonce); |
| 2739 |
|
| 2740 |
if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint) |
| 2741 |
|| (SSL_CONNECTION_IS_TLS13(s) |
| 2742 |
&& (!PACKET_get_net_4(pkt, &age_add) |
| 2743 |
|| !PACKET_get_length_prefixed_1(pkt, &nonce))) |
| 2744 |
|| !PACKET_get_net_2(pkt, &ticklen) |
| 2745 |
|| (SSL_CONNECTION_IS_TLS13(s) ? (ticklen == 0 |
| 2746 |
|| PACKET_remaining(pkt) < ticklen) |
| 2747 |
: PACKET_remaining(pkt) != ticklen)) { |
| 2748 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2749 |
goto err; |
| 2750 |
} |
| 2751 |
|
| 2752 |
/* |
| 2753 |
* Server is allowed to change its mind (in <=TLSv1.2) and send an empty |
| 2754 |
* ticket. We already checked this TLSv1.3 case above, so it should never |
| 2755 |
* be 0 here in that instance |
| 2756 |
*/ |
| 2757 |
if (ticklen == 0) |
| 2758 |
return MSG_PROCESS_CONTINUE_READING; |
| 2759 |
|
| 2760 |
/* |
| 2761 |
* Sessions must be immutable once they go into the session cache. Otherwise |
| 2762 |
* we can get multi-thread problems. Therefore we don't "update" sessions, |
| 2763 |
* we replace them with a duplicate. In TLSv1.3 we need to do this every |
| 2764 |
* time a NewSessionTicket arrives because those messages arrive |
| 2765 |
* post-handshake and the session may have already gone into the session |
| 2766 |
* cache. |
| 2767 |
*/ |
| 2768 |
if (SSL_CONNECTION_IS_TLS13(s) || s->session->session_id_length > 0) { |
| 2769 |
SSL_SESSION *new_sess; |
| 2770 |
|
| 2771 |
/* |
| 2772 |
* We reused an existing session, so we need to replace it with a new |
| 2773 |
* one |
| 2774 |
*/ |
| 2775 |
if ((new_sess = ssl_session_dup(s->session, 0)) == 0) { |
| 2776 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
| 2777 |
goto err; |
| 2778 |
} |
| 2779 |
|
| 2780 |
if ((s->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) != 0 |
| 2781 |
&& !SSL_CONNECTION_IS_TLS13(s)) { |
| 2782 |
/* |
| 2783 |
* In TLSv1.2 and below the arrival of a new tickets signals that |
| 2784 |
* any old ticket we were using is now out of date, so we remove the |
| 2785 |
* old session from the cache. We carry on if this fails |
| 2786 |
*/ |
| 2787 |
SSL_CTX_remove_session(s->session_ctx, s->session); |
| 2788 |
} |
| 2789 |
|
| 2790 |
SSL_SESSION_free(s->session); |
| 2791 |
s->session = new_sess; |
| 2792 |
} |
| 2793 |
|
| 2794 |
s->session->time = ossl_time_now(); |
| 2795 |
ssl_session_calculate_timeout(s->session); |
| 2796 |
|
| 2797 |
OPENSSL_free(s->session->ext.tick); |
| 2798 |
s->session->ext.tick = NULL; |
| 2799 |
s->session->ext.ticklen = 0; |
| 2800 |
|
| 2801 |
s->session->ext.tick = OPENSSL_malloc(ticklen); |
| 2802 |
if (s->session->ext.tick == NULL) { |
| 2803 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 2804 |
goto err; |
| 2805 |
} |
| 2806 |
if (!PACKET_copy_bytes(pkt, s->session->ext.tick, ticklen)) { |
| 2807 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2808 |
goto err; |
| 2809 |
} |
| 2810 |
|
| 2811 |
s->session->ext.tick_lifetime_hint = ticket_lifetime_hint; |
| 2812 |
s->session->ext.tick_age_add = age_add; |
| 2813 |
s->session->ext.ticklen = ticklen; |
| 2814 |
|
| 2815 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 2816 |
PACKET extpkt; |
| 2817 |
|
| 2818 |
if (!PACKET_as_length_prefixed_2(pkt, &extpkt) |
| 2819 |
|| PACKET_remaining(pkt) != 0) { |
| 2820 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2821 |
goto err; |
| 2822 |
} |
| 2823 |
|
| 2824 |
if (!tls_collect_extensions(s, &extpkt, |
| 2825 |
SSL_EXT_TLS1_3_NEW_SESSION_TICKET, &exts, |
| 2826 |
NULL, 1) |
| 2827 |
|| !tls_parse_all_extensions(s, |
| 2828 |
SSL_EXT_TLS1_3_NEW_SESSION_TICKET, |
| 2829 |
exts, NULL, 0, 1)) { |
| 2830 |
/* SSLfatal() already called */ |
| 2831 |
goto err; |
| 2832 |
} |
| 2833 |
} |
| 2834 |
|
| 2835 |
/* |
| 2836 |
* There are two ways to detect a resumed ticket session. One is to set |
| 2837 |
* an appropriate session ID and then the server must return a match in |
| 2838 |
* ServerHello. This allows the normal client session ID matching to work |
| 2839 |
* and we know much earlier that the ticket has been accepted. The |
| 2840 |
* other way is to set zero length session ID when the ticket is |
| 2841 |
* presented and rely on the handshake to determine session resumption. |
| 2842 |
* We choose the former approach because this fits in with assumptions |
| 2843 |
* elsewhere in OpenSSL. The session ID is set to the SHA256 hash of the |
| 2844 |
* ticket. |
| 2845 |
*/ |
| 2846 |
sha256 = EVP_MD_fetch(sctx->libctx, "SHA2-256", sctx->propq); |
| 2847 |
if (sha256 == NULL) { |
| 2848 |
/* Error is already recorded */ |
| 2849 |
SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR); |
| 2850 |
goto err; |
| 2851 |
} |
| 2852 |
/* |
| 2853 |
* We use sess_len here because EVP_Digest expects an int |
| 2854 |
* but s->session->session_id_length is a size_t |
| 2855 |
*/ |
| 2856 |
if (!EVP_Digest(s->session->ext.tick, ticklen, |
| 2857 |
s->session->session_id, &sess_len, |
| 2858 |
sha256, NULL)) { |
| 2859 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 2860 |
goto err; |
| 2861 |
} |
| 2862 |
EVP_MD_free(sha256); |
| 2863 |
sha256 = NULL; |
| 2864 |
s->session->session_id_length = sess_len; |
| 2865 |
s->session->not_resumable = 0; |
| 2866 |
|
| 2867 |
/* This is a standalone message in TLSv1.3, so there is no more to read */ |
| 2868 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 2869 |
const EVP_MD *md = ssl_handshake_md(s); |
| 2870 |
int hashleni = EVP_MD_get_size(md); |
| 2871 |
size_t hashlen; |
| 2872 |
/* ASCII: "resumption", in hex for EBCDIC compatibility */ |
| 2873 |
static const unsigned char nonce_label[] = { 0x72, 0x65, 0x73, 0x75, 0x6D, |
| 2874 |
0x70, 0x74, 0x69, 0x6F, 0x6E }; |
| 2875 |
|
| 2876 |
/* Ensure cast to size_t is safe */ |
| 2877 |
if (!ossl_assert(hashleni > 0)) { |
| 2878 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 2879 |
goto err; |
| 2880 |
} |
| 2881 |
hashlen = (size_t)hashleni; |
| 2882 |
|
| 2883 |
if (!tls13_hkdf_expand(s, md, s->resumption_master_secret, |
| 2884 |
nonce_label, |
| 2885 |
sizeof(nonce_label), |
| 2886 |
PACKET_data(&nonce), |
| 2887 |
PACKET_remaining(&nonce), |
| 2888 |
s->session->master_key, |
| 2889 |
hashlen, 1)) { |
| 2890 |
/* SSLfatal() already called */ |
| 2891 |
goto err; |
| 2892 |
} |
| 2893 |
s->session->master_key_length = hashlen; |
| 2894 |
|
| 2895 |
OPENSSL_free(exts); |
| 2896 |
ssl_update_cache(s, SSL_SESS_CACHE_CLIENT); |
| 2897 |
return MSG_PROCESS_FINISHED_READING; |
| 2898 |
} |
| 2899 |
|
| 2900 |
return MSG_PROCESS_CONTINUE_READING; |
| 2901 |
err: |
| 2902 |
EVP_MD_free(sha256); |
| 2903 |
OPENSSL_free(exts); |
| 2904 |
return MSG_PROCESS_ERROR; |
| 2905 |
} |
| 2906 |
|
| 2907 |
/* |
| 2908 |
* In TLSv1.3 this is called from the extensions code, otherwise it is used to |
| 2909 |
* parse a separate message. Returns 1 on success or 0 on failure |
| 2910 |
*/ |
| 2911 |
int tls_process_cert_status_body(SSL_CONNECTION *s, size_t chainidx, PACKET *pkt) |
| 2912 |
{ |
| 2913 |
unsigned int type; |
| 2914 |
#ifndef OPENSSL_NO_OCSP |
| 2915 |
size_t resplen; |
| 2916 |
unsigned char *respder; |
| 2917 |
OCSP_RESPONSE *resp = NULL; |
| 2918 |
const unsigned char *p; |
| 2919 |
#endif |
| 2920 |
|
| 2921 |
if (!PACKET_get_1(pkt, &type) |
| 2922 |
|| type != TLSEXT_STATUSTYPE_ocsp) { |
| 2923 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_UNSUPPORTED_STATUS_TYPE); |
| 2924 |
return 0; |
| 2925 |
} |
| 2926 |
|
| 2927 |
#ifndef OPENSSL_NO_OCSP |
| 2928 |
OPENSSL_free(s->ext.ocsp.resp); |
| 2929 |
s->ext.ocsp.resp = NULL; |
| 2930 |
s->ext.ocsp.resp_len = 0; |
| 2931 |
|
| 2932 |
if (s->ext.ocsp.resp_ex == NULL) |
| 2933 |
s->ext.ocsp.resp_ex = sk_OCSP_RESPONSE_new_null(); |
| 2934 |
|
| 2935 |
/* |
| 2936 |
* TODO(DTLS-1.3): in future DTLS should also be considered |
| 2937 |
*/ |
| 2938 |
if (!SSL_CONNECTION_IS_TLS13(s) && type == TLSEXT_STATUSTYPE_ocsp) { |
| 2939 |
sk_OCSP_RESPONSE_pop_free(s->ext.ocsp.resp_ex, OCSP_RESPONSE_free); |
| 2940 |
s->ext.ocsp.resp_ex = sk_OCSP_RESPONSE_new_null(); |
| 2941 |
} |
| 2942 |
|
| 2943 |
if (PACKET_remaining(pkt) > 0) { |
| 2944 |
if (!PACKET_get_net_3_len(pkt, &resplen) |
| 2945 |
|| PACKET_remaining(pkt) != resplen) { |
| 2946 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2947 |
return 0; |
| 2948 |
} |
| 2949 |
|
| 2950 |
if (resplen == 0) { |
| 2951 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_BAD_PACKET); |
| 2952 |
return 0; |
| 2953 |
} |
| 2954 |
|
| 2955 |
if ((respder = OPENSSL_malloc(resplen)) == NULL) { |
| 2956 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 2957 |
return 0; |
| 2958 |
} |
| 2959 |
|
| 2960 |
if (!PACKET_copy_bytes(pkt, respder, resplen)) { |
| 2961 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 2962 |
OPENSSL_free(respder); |
| 2963 |
return 0; |
| 2964 |
} |
| 2965 |
p = respder; |
| 2966 |
resp = d2i_OCSP_RESPONSE(NULL, &p, (long)resplen); |
| 2967 |
OPENSSL_free(respder); |
| 2968 |
if (resp == NULL) { |
| 2969 |
SSLfatal(s, TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE, |
| 2970 |
SSL_R_TLSV1_BAD_CERTIFICATE_STATUS_RESPONSE); |
| 2971 |
return 0; |
| 2972 |
} |
| 2973 |
sk_OCSP_RESPONSE_insert(s->ext.ocsp.resp_ex, resp, (int)chainidx); |
| 2974 |
} |
| 2975 |
|
| 2976 |
#endif |
| 2977 |
return 1; |
| 2978 |
} |
| 2979 |
|
| 2980 |
MSG_PROCESS_RETURN tls_process_cert_status(SSL_CONNECTION *s, PACKET *pkt) |
| 2981 |
{ |
| 2982 |
if (!tls_process_cert_status_body(s, 0, pkt)) { |
| 2983 |
/* SSLfatal() already called */ |
| 2984 |
return MSG_PROCESS_ERROR; |
| 2985 |
} |
| 2986 |
|
| 2987 |
return MSG_PROCESS_CONTINUE_READING; |
| 2988 |
} |
| 2989 |
|
| 2990 |
/* |
| 2991 |
* Perform miscellaneous checks and processing after we have received the |
| 2992 |
* server's initial flight. In TLS1.3 this is after the Server Finished message. |
| 2993 |
* In <=TLS1.2 this is after the ServerDone message. Returns 1 on success or 0 |
| 2994 |
* on failure. |
| 2995 |
*/ |
| 2996 |
int tls_process_initial_server_flight(SSL_CONNECTION *s) |
| 2997 |
{ |
| 2998 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 2999 |
|
| 3000 |
/* |
| 3001 |
* at this point we check that we have the required stuff from |
| 3002 |
* the server |
| 3003 |
*/ |
| 3004 |
if (!ssl3_check_cert_and_algorithm(s)) { |
| 3005 |
/* SSLfatal() already called */ |
| 3006 |
return 0; |
| 3007 |
} |
| 3008 |
|
| 3009 |
/* |
| 3010 |
* Call the ocsp status callback if needed. The |ext.ocsp.resp| and |
| 3011 |
* |ext.ocsp.resp_len| values will be set if we actually received a status |
| 3012 |
* message, or NULL and -1 otherwise |
| 3013 |
*/ |
| 3014 |
if (s->ext.status_type != TLSEXT_STATUSTYPE_nothing |
| 3015 |
&& sctx->ext.status_cb != NULL) { |
| 3016 |
int ret = sctx->ext.status_cb(SSL_CONNECTION_GET_USER_SSL(s), |
| 3017 |
sctx->ext.status_arg); |
| 3018 |
|
| 3019 |
if (ret == 0) { |
| 3020 |
SSLfatal(s, SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE, |
| 3021 |
SSL_R_INVALID_STATUS_RESPONSE); |
| 3022 |
return 0; |
| 3023 |
} |
| 3024 |
if (ret < 0) { |
| 3025 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, |
| 3026 |
SSL_R_OCSP_CALLBACK_FAILURE); |
| 3027 |
return 0; |
| 3028 |
} |
| 3029 |
} |
| 3030 |
#ifndef OPENSSL_NO_CT |
| 3031 |
if (s->ct_validation_callback != NULL) { |
| 3032 |
/* Note we validate the SCTs whether or not we abort on error */ |
| 3033 |
if (!ssl_validate_ct(s) && (s->verify_mode & SSL_VERIFY_PEER)) { |
| 3034 |
/* SSLfatal() already called */ |
| 3035 |
return 0; |
| 3036 |
} |
| 3037 |
} |
| 3038 |
#endif |
| 3039 |
|
| 3040 |
return 1; |
| 3041 |
} |
| 3042 |
|
| 3043 |
MSG_PROCESS_RETURN tls_process_server_done(SSL_CONNECTION *s, PACKET *pkt) |
| 3044 |
{ |
| 3045 |
if (PACKET_remaining(pkt) > 0) { |
| 3046 |
/* should contain no data */ |
| 3047 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 3048 |
return MSG_PROCESS_ERROR; |
| 3049 |
} |
| 3050 |
#ifndef OPENSSL_NO_SRP |
| 3051 |
if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 3052 |
if (ssl_srp_calc_a_param_intern(s) <= 0) { |
| 3053 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_SRP_A_CALC); |
| 3054 |
return MSG_PROCESS_ERROR; |
| 3055 |
} |
| 3056 |
} |
| 3057 |
#endif |
| 3058 |
|
| 3059 |
if (!tls_process_initial_server_flight(s)) { |
| 3060 |
/* SSLfatal() already called */ |
| 3061 |
return MSG_PROCESS_ERROR; |
| 3062 |
} |
| 3063 |
|
| 3064 |
return MSG_PROCESS_FINISHED_READING; |
| 3065 |
} |
| 3066 |
|
| 3067 |
static int tls_construct_cke_psk_preamble(SSL_CONNECTION *s, WPACKET *pkt) |
| 3068 |
{ |
| 3069 |
#ifndef OPENSSL_NO_PSK |
| 3070 |
int ret = 0; |
| 3071 |
/* |
| 3072 |
* The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a |
| 3073 |
* \0-terminated identity. The last byte is for us for simulating |
| 3074 |
* strnlen. |
| 3075 |
*/ |
| 3076 |
char identity[PSK_MAX_IDENTITY_LEN + 1]; |
| 3077 |
size_t identitylen = 0; |
| 3078 |
unsigned char psk[PSK_MAX_PSK_LEN]; |
| 3079 |
unsigned char *tmppsk = NULL; |
| 3080 |
char *tmpidentity = NULL; |
| 3081 |
size_t psklen = 0; |
| 3082 |
|
| 3083 |
if (s->psk_client_callback == NULL) { |
| 3084 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_PSK_NO_CLIENT_CB); |
| 3085 |
goto err; |
| 3086 |
} |
| 3087 |
|
| 3088 |
memset(identity, 0, sizeof(identity)); |
| 3089 |
|
| 3090 |
psklen = s->psk_client_callback(SSL_CONNECTION_GET_USER_SSL(s), |
| 3091 |
s->session->psk_identity_hint, |
| 3092 |
identity, sizeof(identity) - 1, |
| 3093 |
psk, sizeof(psk)); |
| 3094 |
|
| 3095 |
if (psklen > PSK_MAX_PSK_LEN) { |
| 3096 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, ERR_R_INTERNAL_ERROR); |
| 3097 |
psklen = PSK_MAX_PSK_LEN; /* Avoid overrunning the array on cleanse */ |
| 3098 |
goto err; |
| 3099 |
} else if (psklen == 0) { |
| 3100 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 3101 |
goto err; |
| 3102 |
} |
| 3103 |
|
| 3104 |
identitylen = strlen(identity); |
| 3105 |
if (identitylen > PSK_MAX_IDENTITY_LEN) { |
| 3106 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3107 |
goto err; |
| 3108 |
} |
| 3109 |
|
| 3110 |
tmppsk = OPENSSL_memdup(psk, psklen); |
| 3111 |
tmpidentity = OPENSSL_strdup(identity); |
| 3112 |
if (tmppsk == NULL || tmpidentity == NULL) { |
| 3113 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 3114 |
goto err; |
| 3115 |
} |
| 3116 |
|
| 3117 |
OPENSSL_free(s->s3.tmp.psk); |
| 3118 |
s->s3.tmp.psk = tmppsk; |
| 3119 |
s->s3.tmp.psklen = psklen; |
| 3120 |
tmppsk = NULL; |
| 3121 |
OPENSSL_free(s->session->psk_identity); |
| 3122 |
s->session->psk_identity = tmpidentity; |
| 3123 |
tmpidentity = NULL; |
| 3124 |
|
| 3125 |
if (!WPACKET_sub_memcpy_u16(pkt, identity, identitylen)) { |
| 3126 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3127 |
goto err; |
| 3128 |
} |
| 3129 |
|
| 3130 |
ret = 1; |
| 3131 |
|
| 3132 |
err: |
| 3133 |
OPENSSL_cleanse(psk, psklen); |
| 3134 |
OPENSSL_cleanse(identity, sizeof(identity)); |
| 3135 |
OPENSSL_clear_free(tmppsk, psklen); |
| 3136 |
OPENSSL_clear_free(tmpidentity, identitylen); |
| 3137 |
|
| 3138 |
return ret; |
| 3139 |
#else |
| 3140 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3141 |
return 0; |
| 3142 |
#endif |
| 3143 |
} |
| 3144 |
|
| 3145 |
static int tls_construct_cke_rsa(SSL_CONNECTION *s, WPACKET *pkt) |
| 3146 |
{ |
| 3147 |
unsigned char *encdata = NULL; |
| 3148 |
EVP_PKEY *pkey = NULL; |
| 3149 |
EVP_PKEY_CTX *pctx = NULL; |
| 3150 |
size_t enclen; |
| 3151 |
unsigned char *pms = NULL; |
| 3152 |
size_t pmslen = 0; |
| 3153 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 3154 |
|
| 3155 |
if (!received_server_cert(s)) { |
| 3156 |
/* |
| 3157 |
* We should always have a server certificate with SSL_kRSA. |
| 3158 |
*/ |
| 3159 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3160 |
return 0; |
| 3161 |
} |
| 3162 |
|
| 3163 |
if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
| 3164 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3165 |
return 0; |
| 3166 |
} |
| 3167 |
|
| 3168 |
if (!EVP_PKEY_is_a(pkey, "RSA")) { |
| 3169 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3170 |
return 0; |
| 3171 |
} |
| 3172 |
|
| 3173 |
pmslen = SSL_MAX_MASTER_KEY_LENGTH; |
| 3174 |
pms = OPENSSL_malloc(pmslen); |
| 3175 |
if (pms == NULL) { |
| 3176 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 3177 |
return 0; |
| 3178 |
} |
| 3179 |
|
| 3180 |
pms[0] = s->client_version >> 8; |
| 3181 |
pms[1] = s->client_version & 0xff; |
| 3182 |
if (RAND_bytes_ex(sctx->libctx, pms + 2, pmslen - 2, 0) <= 0) { |
| 3183 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_RAND_LIB); |
| 3184 |
goto err; |
| 3185 |
} |
| 3186 |
|
| 3187 |
/* Fix buf for TLS and beyond */ |
| 3188 |
if (s->version > SSL3_VERSION && !WPACKET_start_sub_packet_u16(pkt)) { |
| 3189 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3190 |
goto err; |
| 3191 |
} |
| 3192 |
|
| 3193 |
pctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, pkey, sctx->propq); |
| 3194 |
if (pctx == NULL || EVP_PKEY_encrypt_init(pctx) <= 0 |
| 3195 |
|| EVP_PKEY_encrypt(pctx, NULL, &enclen, pms, pmslen) <= 0) { |
| 3196 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 3197 |
goto err; |
| 3198 |
} |
| 3199 |
if (!WPACKET_allocate_bytes(pkt, enclen, &encdata) |
| 3200 |
|| EVP_PKEY_encrypt(pctx, encdata, &enclen, pms, pmslen) <= 0) { |
| 3201 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_RSA_ENCRYPT); |
| 3202 |
goto err; |
| 3203 |
} |
| 3204 |
EVP_PKEY_CTX_free(pctx); |
| 3205 |
pctx = NULL; |
| 3206 |
|
| 3207 |
/* Fix buf for TLS and beyond */ |
| 3208 |
if (s->version > SSL3_VERSION && !WPACKET_close(pkt)) { |
| 3209 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3210 |
goto err; |
| 3211 |
} |
| 3212 |
|
| 3213 |
/* Log the premaster secret, if logging is enabled. */ |
| 3214 |
if (!ssl_log_rsa_client_key_exchange(s, encdata, enclen, pms, pmslen)) { |
| 3215 |
/* SSLfatal() already called */ |
| 3216 |
goto err; |
| 3217 |
} |
| 3218 |
|
| 3219 |
s->s3.tmp.pms = pms; |
| 3220 |
s->s3.tmp.pmslen = pmslen; |
| 3221 |
|
| 3222 |
return 1; |
| 3223 |
err: |
| 3224 |
OPENSSL_clear_free(pms, pmslen); |
| 3225 |
EVP_PKEY_CTX_free(pctx); |
| 3226 |
|
| 3227 |
return 0; |
| 3228 |
} |
| 3229 |
|
| 3230 |
static int tls_construct_cke_dhe(SSL_CONNECTION *s, WPACKET *pkt) |
| 3231 |
{ |
| 3232 |
EVP_PKEY *ckey = NULL, *skey = NULL; |
| 3233 |
unsigned char *keybytes = NULL; |
| 3234 |
int prime_len; |
| 3235 |
unsigned char *encoded_pub = NULL; |
| 3236 |
size_t encoded_pub_len, pad_len; |
| 3237 |
int ret = 0; |
| 3238 |
|
| 3239 |
skey = s->s3.peer_tmp; |
| 3240 |
if (skey == NULL) { |
| 3241 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3242 |
goto err; |
| 3243 |
} |
| 3244 |
|
| 3245 |
ckey = ssl_generate_pkey(s, skey); |
| 3246 |
if (ckey == NULL) { |
| 3247 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3248 |
goto err; |
| 3249 |
} |
| 3250 |
|
| 3251 |
if (ssl_derive(s, ckey, skey, 0) == 0) { |
| 3252 |
/* SSLfatal() already called */ |
| 3253 |
goto err; |
| 3254 |
} |
| 3255 |
|
| 3256 |
/* send off the data */ |
| 3257 |
|
| 3258 |
/* Generate encoding of server key */ |
| 3259 |
encoded_pub_len = EVP_PKEY_get1_encoded_public_key(ckey, &encoded_pub); |
| 3260 |
if (encoded_pub_len == 0) { |
| 3261 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3262 |
EVP_PKEY_free(ckey); |
| 3263 |
return EXT_RETURN_FAIL; |
| 3264 |
} |
| 3265 |
|
| 3266 |
/* |
| 3267 |
* For interoperability with some versions of the Microsoft TLS |
| 3268 |
* stack, we need to zero pad the DHE pub key to the same length |
| 3269 |
* as the prime. |
| 3270 |
*/ |
| 3271 |
prime_len = EVP_PKEY_get_size(ckey); |
| 3272 |
pad_len = prime_len - encoded_pub_len; |
| 3273 |
if (pad_len > 0) { |
| 3274 |
if (!WPACKET_sub_allocate_bytes_u16(pkt, pad_len, &keybytes)) { |
| 3275 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3276 |
goto err; |
| 3277 |
} |
| 3278 |
memset(keybytes, 0, pad_len); |
| 3279 |
} |
| 3280 |
|
| 3281 |
if (!WPACKET_sub_memcpy_u16(pkt, encoded_pub, encoded_pub_len)) { |
| 3282 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3283 |
goto err; |
| 3284 |
} |
| 3285 |
|
| 3286 |
ret = 1; |
| 3287 |
err: |
| 3288 |
OPENSSL_free(encoded_pub); |
| 3289 |
EVP_PKEY_free(ckey); |
| 3290 |
return ret; |
| 3291 |
} |
| 3292 |
|
| 3293 |
static int tls_construct_cke_ecdhe(SSL_CONNECTION *s, WPACKET *pkt) |
| 3294 |
{ |
| 3295 |
unsigned char *encodedPoint = NULL; |
| 3296 |
size_t encoded_pt_len = 0; |
| 3297 |
EVP_PKEY *ckey = NULL, *skey = NULL; |
| 3298 |
int ret = 0; |
| 3299 |
|
| 3300 |
skey = s->s3.peer_tmp; |
| 3301 |
if (skey == NULL) { |
| 3302 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3303 |
return 0; |
| 3304 |
} |
| 3305 |
|
| 3306 |
ckey = ssl_generate_pkey(s, skey); |
| 3307 |
if (ckey == NULL) { |
| 3308 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SSL_LIB); |
| 3309 |
goto err; |
| 3310 |
} |
| 3311 |
|
| 3312 |
if (ssl_derive(s, ckey, skey, 0) == 0) { |
| 3313 |
/* SSLfatal() already called */ |
| 3314 |
goto err; |
| 3315 |
} |
| 3316 |
|
| 3317 |
/* Generate encoding of client key */ |
| 3318 |
encoded_pt_len = EVP_PKEY_get1_encoded_public_key(ckey, &encodedPoint); |
| 3319 |
|
| 3320 |
if (encoded_pt_len == 0) { |
| 3321 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EC_LIB); |
| 3322 |
goto err; |
| 3323 |
} |
| 3324 |
|
| 3325 |
if (!WPACKET_sub_memcpy_u8(pkt, encodedPoint, encoded_pt_len)) { |
| 3326 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3327 |
goto err; |
| 3328 |
} |
| 3329 |
|
| 3330 |
ret = 1; |
| 3331 |
err: |
| 3332 |
OPENSSL_free(encodedPoint); |
| 3333 |
EVP_PKEY_free(ckey); |
| 3334 |
return ret; |
| 3335 |
} |
| 3336 |
|
| 3337 |
static int tls_construct_cke_gost(SSL_CONNECTION *s, WPACKET *pkt) |
| 3338 |
{ |
| 3339 |
#ifndef OPENSSL_NO_GOST |
| 3340 |
/* GOST key exchange message creation */ |
| 3341 |
EVP_PKEY_CTX *pkey_ctx = NULL; |
| 3342 |
EVP_PKEY *pkey = NULL; |
| 3343 |
size_t msglen; |
| 3344 |
unsigned int md_len; |
| 3345 |
unsigned char shared_ukm[32], tmp[256]; |
| 3346 |
EVP_MD_CTX *ukm_hash = NULL; |
| 3347 |
int dgst_nid = NID_id_GostR3411_94; |
| 3348 |
unsigned char *pms = NULL; |
| 3349 |
size_t pmslen = 0; |
| 3350 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 3351 |
|
| 3352 |
if ((s->s3.tmp.new_cipher->algorithm_auth & SSL_aGOST12) != 0) |
| 3353 |
dgst_nid = NID_id_GostR3411_2012_256; |
| 3354 |
|
| 3355 |
/* |
| 3356 |
* Get server certificate PKEY and create ctx from it |
| 3357 |
*/ |
| 3358 |
if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
| 3359 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 3360 |
SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
| 3361 |
return 0; |
| 3362 |
} |
| 3363 |
|
| 3364 |
pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, |
| 3365 |
pkey, |
| 3366 |
sctx->propq); |
| 3367 |
if (pkey_ctx == NULL) { |
| 3368 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 3369 |
return 0; |
| 3370 |
} |
| 3371 |
/* |
| 3372 |
* If we have send a certificate, and certificate key |
| 3373 |
* parameters match those of server certificate, use |
| 3374 |
* certificate key for key exchange |
| 3375 |
*/ |
| 3376 |
|
| 3377 |
/* Otherwise, generate ephemeral key pair */ |
| 3378 |
pmslen = 32; |
| 3379 |
pms = OPENSSL_malloc(pmslen); |
| 3380 |
if (pms == NULL) { |
| 3381 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 3382 |
goto err; |
| 3383 |
} |
| 3384 |
|
| 3385 |
if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0 |
| 3386 |
/* Generate session key |
| 3387 |
*/ |
| 3388 |
|| RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) { |
| 3389 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3390 |
goto err; |
| 3391 |
}; |
| 3392 |
/* |
| 3393 |
* Compute shared IV and store it in algorithm-specific context |
| 3394 |
* data |
| 3395 |
*/ |
| 3396 |
ukm_hash = EVP_MD_CTX_new(); |
| 3397 |
if (ukm_hash == NULL |
| 3398 |
|| EVP_DigestInit(ukm_hash, EVP_get_digestbynid(dgst_nid)) <= 0 |
| 3399 |
|| EVP_DigestUpdate(ukm_hash, s->s3.client_random, |
| 3400 |
SSL3_RANDOM_SIZE) |
| 3401 |
<= 0 |
| 3402 |
|| EVP_DigestUpdate(ukm_hash, s->s3.server_random, |
| 3403 |
SSL3_RANDOM_SIZE) |
| 3404 |
<= 0 |
| 3405 |
|| EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) { |
| 3406 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3407 |
goto err; |
| 3408 |
} |
| 3409 |
EVP_MD_CTX_free(ukm_hash); |
| 3410 |
ukm_hash = NULL; |
| 3411 |
if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, |
| 3412 |
EVP_PKEY_CTRL_SET_IV, 8, shared_ukm) |
| 3413 |
<= 0) { |
| 3414 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
| 3415 |
goto err; |
| 3416 |
} |
| 3417 |
/* Make GOST keytransport blob message */ |
| 3418 |
/* |
| 3419 |
* Encapsulate it into sequence |
| 3420 |
*/ |
| 3421 |
msglen = 255; |
| 3422 |
if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) { |
| 3423 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
| 3424 |
goto err; |
| 3425 |
} |
| 3426 |
|
| 3427 |
if (!WPACKET_put_bytes_u8(pkt, V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED) |
| 3428 |
|| (msglen >= 0x80 && !WPACKET_put_bytes_u8(pkt, 0x81)) |
| 3429 |
|| !WPACKET_sub_memcpy_u8(pkt, tmp, msglen)) { |
| 3430 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3431 |
goto err; |
| 3432 |
} |
| 3433 |
|
| 3434 |
EVP_PKEY_CTX_free(pkey_ctx); |
| 3435 |
s->s3.tmp.pms = pms; |
| 3436 |
s->s3.tmp.pmslen = pmslen; |
| 3437 |
|
| 3438 |
return 1; |
| 3439 |
err: |
| 3440 |
EVP_PKEY_CTX_free(pkey_ctx); |
| 3441 |
OPENSSL_clear_free(pms, pmslen); |
| 3442 |
EVP_MD_CTX_free(ukm_hash); |
| 3443 |
return 0; |
| 3444 |
#else |
| 3445 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3446 |
return 0; |
| 3447 |
#endif |
| 3448 |
} |
| 3449 |
|
| 3450 |
#ifndef OPENSSL_NO_GOST |
| 3451 |
int ossl_gost18_cke_cipher_nid(const SSL_CONNECTION *s) |
| 3452 |
{ |
| 3453 |
if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_MAGMA) != 0) |
| 3454 |
return NID_magma_ctr; |
| 3455 |
else if ((s->s3.tmp.new_cipher->algorithm_enc & SSL_KUZNYECHIK) != 0) |
| 3456 |
return NID_kuznyechik_ctr; |
| 3457 |
|
| 3458 |
return NID_undef; |
| 3459 |
} |
| 3460 |
|
| 3461 |
int ossl_gost_ukm(const SSL_CONNECTION *s, unsigned char *dgst_buf) |
| 3462 |
{ |
| 3463 |
EVP_MD_CTX *hash = NULL; |
| 3464 |
unsigned int md_len; |
| 3465 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 3466 |
const EVP_MD *md = ssl_evp_md_fetch(sctx->libctx, NID_id_GostR3411_2012_256, |
| 3467 |
sctx->propq); |
| 3468 |
|
| 3469 |
if (md == NULL) |
| 3470 |
return 0; |
| 3471 |
|
| 3472 |
if ((hash = EVP_MD_CTX_new()) == NULL |
| 3473 |
|| EVP_DigestInit(hash, md) <= 0 |
| 3474 |
|| EVP_DigestUpdate(hash, s->s3.client_random, SSL3_RANDOM_SIZE) <= 0 |
| 3475 |
|| EVP_DigestUpdate(hash, s->s3.server_random, SSL3_RANDOM_SIZE) <= 0 |
| 3476 |
|| EVP_DigestFinal_ex(hash, dgst_buf, &md_len) <= 0) { |
| 3477 |
EVP_MD_CTX_free(hash); |
| 3478 |
ssl_evp_md_free(md); |
| 3479 |
return 0; |
| 3480 |
} |
| 3481 |
|
| 3482 |
EVP_MD_CTX_free(hash); |
| 3483 |
ssl_evp_md_free(md); |
| 3484 |
return 1; |
| 3485 |
} |
| 3486 |
#endif |
| 3487 |
|
| 3488 |
static int tls_construct_cke_gost18(SSL_CONNECTION *s, WPACKET *pkt) |
| 3489 |
{ |
| 3490 |
#ifndef OPENSSL_NO_GOST |
| 3491 |
/* GOST 2018 key exchange message creation */ |
| 3492 |
unsigned char rnd_dgst[32]; |
| 3493 |
unsigned char *encdata = NULL; |
| 3494 |
EVP_PKEY_CTX *pkey_ctx = NULL; |
| 3495 |
EVP_PKEY *pkey; |
| 3496 |
unsigned char *pms = NULL; |
| 3497 |
size_t pmslen = 0; |
| 3498 |
size_t msglen; |
| 3499 |
int cipher_nid = ossl_gost18_cke_cipher_nid(s); |
| 3500 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 3501 |
|
| 3502 |
if (cipher_nid == NID_undef) { |
| 3503 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3504 |
return 0; |
| 3505 |
} |
| 3506 |
|
| 3507 |
if (ossl_gost_ukm(s, rnd_dgst) <= 0) { |
| 3508 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3509 |
goto err; |
| 3510 |
} |
| 3511 |
|
| 3512 |
/* Pre-master secret - random bytes */ |
| 3513 |
pmslen = 32; |
| 3514 |
pms = OPENSSL_malloc(pmslen); |
| 3515 |
if (pms == NULL) { |
| 3516 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 3517 |
goto err; |
| 3518 |
} |
| 3519 |
|
| 3520 |
if (RAND_bytes_ex(sctx->libctx, pms, pmslen, 0) <= 0) { |
| 3521 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3522 |
goto err; |
| 3523 |
} |
| 3524 |
|
| 3525 |
/* Get server certificate PKEY and create ctx from it */ |
| 3526 |
if ((pkey = tls_get_peer_pkey(s)) == NULL) { |
| 3527 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 3528 |
SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER); |
| 3529 |
goto err; |
| 3530 |
} |
| 3531 |
|
| 3532 |
pkey_ctx = EVP_PKEY_CTX_new_from_pkey(sctx->libctx, |
| 3533 |
pkey, |
| 3534 |
sctx->propq); |
| 3535 |
if (pkey_ctx == NULL) { |
| 3536 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 3537 |
goto err; |
| 3538 |
} |
| 3539 |
|
| 3540 |
if (EVP_PKEY_encrypt_init(pkey_ctx) <= 0) { |
| 3541 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3542 |
goto err; |
| 3543 |
}; |
| 3544 |
|
| 3545 |
/* Reuse EVP_PKEY_CTRL_SET_IV, make choice in engine code */ |
| 3546 |
if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, |
| 3547 |
EVP_PKEY_CTRL_SET_IV, 32, rnd_dgst) |
| 3548 |
<= 0) { |
| 3549 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
| 3550 |
goto err; |
| 3551 |
} |
| 3552 |
|
| 3553 |
if (EVP_PKEY_CTX_ctrl(pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, |
| 3554 |
EVP_PKEY_CTRL_CIPHER, cipher_nid, NULL) |
| 3555 |
<= 0) { |
| 3556 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG); |
| 3557 |
goto err; |
| 3558 |
} |
| 3559 |
|
| 3560 |
if (EVP_PKEY_encrypt(pkey_ctx, NULL, &msglen, pms, pmslen) <= 0) { |
| 3561 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 3562 |
goto err; |
| 3563 |
} |
| 3564 |
|
| 3565 |
if (!WPACKET_allocate_bytes(pkt, msglen, &encdata) |
| 3566 |
|| EVP_PKEY_encrypt(pkey_ctx, encdata, &msglen, pms, pmslen) <= 0) { |
| 3567 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB); |
| 3568 |
goto err; |
| 3569 |
} |
| 3570 |
|
| 3571 |
EVP_PKEY_CTX_free(pkey_ctx); |
| 3572 |
pkey_ctx = NULL; |
| 3573 |
s->s3.tmp.pms = pms; |
| 3574 |
s->s3.tmp.pmslen = pmslen; |
| 3575 |
|
| 3576 |
return 1; |
| 3577 |
err: |
| 3578 |
EVP_PKEY_CTX_free(pkey_ctx); |
| 3579 |
OPENSSL_clear_free(pms, pmslen); |
| 3580 |
return 0; |
| 3581 |
#else |
| 3582 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3583 |
return 0; |
| 3584 |
#endif |
| 3585 |
} |
| 3586 |
|
| 3587 |
static int tls_construct_cke_srp(SSL_CONNECTION *s, WPACKET *pkt) |
| 3588 |
{ |
| 3589 |
#ifndef OPENSSL_NO_SRP |
| 3590 |
unsigned char *abytes = NULL; |
| 3591 |
|
| 3592 |
if (s->srp_ctx.A == NULL |
| 3593 |
|| !WPACKET_sub_allocate_bytes_u16(pkt, BN_num_bytes(s->srp_ctx.A), |
| 3594 |
&abytes)) { |
| 3595 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3596 |
return 0; |
| 3597 |
} |
| 3598 |
BN_bn2bin(s->srp_ctx.A, abytes); |
| 3599 |
|
| 3600 |
OPENSSL_free(s->session->srp_username); |
| 3601 |
s->session->srp_username = OPENSSL_strdup(s->srp_ctx.login); |
| 3602 |
if (s->session->srp_username == NULL) { |
| 3603 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_CRYPTO_LIB); |
| 3604 |
return 0; |
| 3605 |
} |
| 3606 |
|
| 3607 |
return 1; |
| 3608 |
#else |
| 3609 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3610 |
return 0; |
| 3611 |
#endif |
| 3612 |
} |
| 3613 |
|
| 3614 |
CON_FUNC_RETURN tls_construct_client_key_exchange(SSL_CONNECTION *s, |
| 3615 |
WPACKET *pkt) |
| 3616 |
{ |
| 3617 |
unsigned long alg_k; |
| 3618 |
|
| 3619 |
alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
| 3620 |
|
| 3621 |
/* |
| 3622 |
* All of the construct functions below call SSLfatal() if necessary so |
| 3623 |
* no need to do so here. |
| 3624 |
*/ |
| 3625 |
if ((alg_k & SSL_PSK) |
| 3626 |
&& !tls_construct_cke_psk_preamble(s, pkt)) |
| 3627 |
goto err; |
| 3628 |
|
| 3629 |
if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) { |
| 3630 |
if (!tls_construct_cke_rsa(s, pkt)) |
| 3631 |
goto err; |
| 3632 |
} else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) { |
| 3633 |
if (!tls_construct_cke_dhe(s, pkt)) |
| 3634 |
goto err; |
| 3635 |
} else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) { |
| 3636 |
if (!tls_construct_cke_ecdhe(s, pkt)) |
| 3637 |
goto err; |
| 3638 |
} else if (alg_k & SSL_kGOST) { |
| 3639 |
if (!tls_construct_cke_gost(s, pkt)) |
| 3640 |
goto err; |
| 3641 |
} else if (alg_k & SSL_kGOST18) { |
| 3642 |
if (!tls_construct_cke_gost18(s, pkt)) |
| 3643 |
goto err; |
| 3644 |
} else if (alg_k & SSL_kSRP) { |
| 3645 |
if (!tls_construct_cke_srp(s, pkt)) |
| 3646 |
goto err; |
| 3647 |
} else if (!(alg_k & SSL_kPSK)) { |
| 3648 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3649 |
goto err; |
| 3650 |
} |
| 3651 |
|
| 3652 |
return CON_FUNC_SUCCESS; |
| 3653 |
err: |
| 3654 |
OPENSSL_clear_free(s->s3.tmp.pms, s->s3.tmp.pmslen); |
| 3655 |
s->s3.tmp.pms = NULL; |
| 3656 |
s->s3.tmp.pmslen = 0; |
| 3657 |
#ifndef OPENSSL_NO_PSK |
| 3658 |
OPENSSL_clear_free(s->s3.tmp.psk, s->s3.tmp.psklen); |
| 3659 |
s->s3.tmp.psk = NULL; |
| 3660 |
s->s3.tmp.psklen = 0; |
| 3661 |
#endif |
| 3662 |
return CON_FUNC_ERROR; |
| 3663 |
} |
| 3664 |
|
| 3665 |
int tls_client_key_exchange_post_work(SSL_CONNECTION *s) |
| 3666 |
{ |
| 3667 |
unsigned char *pms = NULL; |
| 3668 |
size_t pmslen = 0; |
| 3669 |
|
| 3670 |
pms = s->s3.tmp.pms; |
| 3671 |
pmslen = s->s3.tmp.pmslen; |
| 3672 |
|
| 3673 |
#ifndef OPENSSL_NO_SRP |
| 3674 |
/* Check for SRP */ |
| 3675 |
if (s->s3.tmp.new_cipher->algorithm_mkey & SSL_kSRP) { |
| 3676 |
if (!srp_generate_client_master_secret(s)) { |
| 3677 |
/* SSLfatal() already called */ |
| 3678 |
goto err; |
| 3679 |
} |
| 3680 |
return 1; |
| 3681 |
} |
| 3682 |
#endif |
| 3683 |
|
| 3684 |
if (pms == NULL && !(s->s3.tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 3685 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_PASSED_INVALID_ARGUMENT); |
| 3686 |
goto err; |
| 3687 |
} |
| 3688 |
if (!ssl_generate_master_secret(s, pms, pmslen, 1)) { |
| 3689 |
/* SSLfatal() already called */ |
| 3690 |
/* ssl_generate_master_secret frees the pms even on error */ |
| 3691 |
pms = NULL; |
| 3692 |
pmslen = 0; |
| 3693 |
goto err; |
| 3694 |
} |
| 3695 |
pms = NULL; |
| 3696 |
pmslen = 0; |
| 3697 |
|
| 3698 |
#ifndef OPENSSL_NO_SCTP |
| 3699 |
if (SSL_CONNECTION_IS_DTLS(s)) { |
| 3700 |
unsigned char sctpauthkey[64]; |
| 3701 |
char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)]; |
| 3702 |
size_t labellen; |
| 3703 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 3704 |
|
| 3705 |
/* |
| 3706 |
* Add new shared key for SCTP-Auth, will be ignored if no SCTP |
| 3707 |
* used. |
| 3708 |
*/ |
| 3709 |
memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL, |
| 3710 |
sizeof(DTLS1_SCTP_AUTH_LABEL)); |
| 3711 |
|
| 3712 |
/* Don't include the terminating zero. */ |
| 3713 |
labellen = sizeof(labelbuffer) - 1; |
| 3714 |
if (s->mode & SSL_MODE_DTLS_SCTP_LABEL_LENGTH_BUG) |
| 3715 |
labellen += 1; |
| 3716 |
|
| 3717 |
if (SSL_export_keying_material(ssl, sctpauthkey, |
| 3718 |
sizeof(sctpauthkey), labelbuffer, |
| 3719 |
labellen, NULL, 0, 0) |
| 3720 |
<= 0) { |
| 3721 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3722 |
goto err; |
| 3723 |
} |
| 3724 |
|
| 3725 |
BIO_ctrl(SSL_get_wbio(ssl), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY, |
| 3726 |
sizeof(sctpauthkey), sctpauthkey); |
| 3727 |
} |
| 3728 |
#endif |
| 3729 |
|
| 3730 |
return 1; |
| 3731 |
err: |
| 3732 |
OPENSSL_clear_free(pms, pmslen); |
| 3733 |
s->s3.tmp.pms = NULL; |
| 3734 |
s->s3.tmp.pmslen = 0; |
| 3735 |
return 0; |
| 3736 |
} |
| 3737 |
|
| 3738 |
/* |
| 3739 |
* Check a certificate can be used for client authentication. Currently check |
| 3740 |
* cert exists, if we have a suitable digest for TLS 1.2 if static DH client |
| 3741 |
* certificates can be used and optionally checks suitability for Suite B. |
| 3742 |
*/ |
| 3743 |
static int ssl3_check_client_certificate(SSL_CONNECTION *s) |
| 3744 |
{ |
| 3745 |
/* If no suitable signature algorithm can't use certificate */ |
| 3746 |
if (!tls_choose_sigalg(s, 0) || s->s3.tmp.sigalg == NULL) |
| 3747 |
return 0; |
| 3748 |
/* |
| 3749 |
* If strict mode check suitability of chain before using it. This also |
| 3750 |
* adjusts suite B digest if necessary. |
| 3751 |
*/ |
| 3752 |
if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT && !tls1_check_chain(s, NULL, NULL, NULL, -2)) |
| 3753 |
return 0; |
| 3754 |
return 1; |
| 3755 |
} |
| 3756 |
|
| 3757 |
WORK_STATE tls_prepare_client_certificate(SSL_CONNECTION *s, WORK_STATE wst) |
| 3758 |
{ |
| 3759 |
X509 *x509 = NULL; |
| 3760 |
EVP_PKEY *pkey = NULL; |
| 3761 |
int i; |
| 3762 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 3763 |
|
| 3764 |
if (wst == WORK_MORE_A) { |
| 3765 |
/* Let cert callback update client certificates if required */ |
| 3766 |
if (s->cert->cert_cb) { |
| 3767 |
i = s->cert->cert_cb(ssl, s->cert->cert_cb_arg); |
| 3768 |
if (i < 0) { |
| 3769 |
s->rwstate = SSL_X509_LOOKUP; |
| 3770 |
return WORK_MORE_A; |
| 3771 |
} |
| 3772 |
if (i == 0) { |
| 3773 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_CALLBACK_FAILED); |
| 3774 |
return WORK_ERROR; |
| 3775 |
} |
| 3776 |
s->rwstate = SSL_NOTHING; |
| 3777 |
} |
| 3778 |
if (ssl3_check_client_certificate(s)) { |
| 3779 |
if (s->post_handshake_auth == SSL_PHA_REQUESTED) { |
| 3780 |
return WORK_FINISHED_STOP; |
| 3781 |
} |
| 3782 |
return WORK_FINISHED_CONTINUE; |
| 3783 |
} |
| 3784 |
|
| 3785 |
/* Fall through to WORK_MORE_B */ |
| 3786 |
wst = WORK_MORE_B; |
| 3787 |
} |
| 3788 |
|
| 3789 |
/* We need to get a client cert */ |
| 3790 |
if (wst == WORK_MORE_B) { |
| 3791 |
/* |
| 3792 |
* If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP; |
| 3793 |
* return(-1); We then get retied later |
| 3794 |
*/ |
| 3795 |
i = ssl_do_client_cert_cb(s, &x509, &pkey); |
| 3796 |
if (i < 0) { |
| 3797 |
s->rwstate = SSL_X509_LOOKUP; |
| 3798 |
return WORK_MORE_B; |
| 3799 |
} |
| 3800 |
s->rwstate = SSL_NOTHING; |
| 3801 |
if ((i == 1) && (pkey != NULL) && (x509 != NULL)) { |
| 3802 |
if (!SSL_use_certificate(ssl, x509) |
| 3803 |
|| !SSL_use_PrivateKey(ssl, pkey)) |
| 3804 |
i = 0; |
| 3805 |
} else if (i == 1) { |
| 3806 |
i = 0; |
| 3807 |
ERR_raise(ERR_LIB_SSL, SSL_R_BAD_DATA_RETURNED_BY_CALLBACK); |
| 3808 |
} |
| 3809 |
|
| 3810 |
X509_free(x509); |
| 3811 |
EVP_PKEY_free(pkey); |
| 3812 |
if (i && !ssl3_check_client_certificate(s)) |
| 3813 |
i = 0; |
| 3814 |
if (i == 0) { |
| 3815 |
if (s->version == SSL3_VERSION) { |
| 3816 |
s->s3.tmp.cert_req = 0; |
| 3817 |
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE); |
| 3818 |
return WORK_FINISHED_CONTINUE; |
| 3819 |
} else { |
| 3820 |
s->s3.tmp.cert_req = 2; |
| 3821 |
s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; |
| 3822 |
if (!ssl3_digest_cached_records(s, 0)) { |
| 3823 |
/* SSLfatal() already called */ |
| 3824 |
return WORK_ERROR; |
| 3825 |
} |
| 3826 |
} |
| 3827 |
} |
| 3828 |
|
| 3829 |
if (!SSL_CONNECTION_IS_TLS13(s) |
| 3830 |
|| (s->options & SSL_OP_NO_TX_CERTIFICATE_COMPRESSION) != 0) |
| 3831 |
s->ext.compress_certificate_from_peer[0] = TLSEXT_comp_cert_none; |
| 3832 |
|
| 3833 |
if (s->post_handshake_auth == SSL_PHA_REQUESTED) |
| 3834 |
return WORK_FINISHED_STOP; |
| 3835 |
return WORK_FINISHED_CONTINUE; |
| 3836 |
} |
| 3837 |
|
| 3838 |
/* Shouldn't ever get here */ |
| 3839 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3840 |
return WORK_ERROR; |
| 3841 |
} |
| 3842 |
|
| 3843 |
CON_FUNC_RETURN tls_construct_client_certificate(SSL_CONNECTION *s, |
| 3844 |
WPACKET *pkt) |
| 3845 |
{ |
| 3846 |
CERT_PKEY *cpk = NULL; |
| 3847 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 3848 |
|
| 3849 |
if (SSL_CONNECTION_IS_TLS13(s)) { |
| 3850 |
if (s->pha_context == NULL) { |
| 3851 |
/* no context available, add 0-length context */ |
| 3852 |
if (!WPACKET_put_bytes_u8(pkt, 0)) { |
| 3853 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3854 |
return CON_FUNC_ERROR; |
| 3855 |
} |
| 3856 |
} else if (!WPACKET_sub_memcpy_u8(pkt, s->pha_context, s->pha_context_len)) { |
| 3857 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3858 |
return CON_FUNC_ERROR; |
| 3859 |
} |
| 3860 |
} |
| 3861 |
if (s->s3.tmp.cert_req != 2) |
| 3862 |
cpk = s->cert->key; |
| 3863 |
switch (s->ext.client_cert_type) { |
| 3864 |
case TLSEXT_cert_type_rpk: |
| 3865 |
if (!tls_output_rpk(s, pkt, cpk)) { |
| 3866 |
/* SSLfatal() already called */ |
| 3867 |
return CON_FUNC_ERROR; |
| 3868 |
} |
| 3869 |
break; |
| 3870 |
case TLSEXT_cert_type_x509: |
| 3871 |
if (!ssl3_output_cert_chain(s, pkt, cpk, 0)) { |
| 3872 |
/* SSLfatal() already called */ |
| 3873 |
return CON_FUNC_ERROR; |
| 3874 |
} |
| 3875 |
break; |
| 3876 |
default: |
| 3877 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3878 |
return CON_FUNC_ERROR; |
| 3879 |
} |
| 3880 |
|
| 3881 |
/* |
| 3882 |
* If we attempted to write early data or we're in middlebox compat mode |
| 3883 |
* then we deferred changing the handshake write keys to the last possible |
| 3884 |
* moment. We need to do it now. |
| 3885 |
*/ |
| 3886 |
if (SSL_CONNECTION_IS_TLS13(s) |
| 3887 |
&& !SSL_IS_QUIC_HANDSHAKE(s) |
| 3888 |
&& SSL_IS_FIRST_HANDSHAKE(s) |
| 3889 |
&& (s->early_data_state != SSL_EARLY_DATA_NONE |
| 3890 |
|| (s->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) |
| 3891 |
&& (!ssl->method->ssl3_enc->change_cipher_state(s, |
| 3892 |
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { |
| 3893 |
/* |
| 3894 |
* This is a fatal error, which leaves enc_write_ctx in an inconsistent |
| 3895 |
* state and thus ssl3_send_alert may crash. |
| 3896 |
*/ |
| 3897 |
SSLfatal(s, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER); |
| 3898 |
return CON_FUNC_ERROR; |
| 3899 |
} |
| 3900 |
|
| 3901 |
return CON_FUNC_SUCCESS; |
| 3902 |
} |
| 3903 |
|
| 3904 |
#ifndef OPENSSL_NO_COMP_ALG |
| 3905 |
CON_FUNC_RETURN tls_construct_client_compressed_certificate(SSL_CONNECTION *sc, |
| 3906 |
WPACKET *pkt) |
| 3907 |
{ |
| 3908 |
SSL *ssl = SSL_CONNECTION_GET_SSL(sc); |
| 3909 |
WPACKET tmppkt; |
| 3910 |
BUF_MEM *buf = NULL; |
| 3911 |
size_t length; |
| 3912 |
size_t max_length; |
| 3913 |
COMP_METHOD *method; |
| 3914 |
COMP_CTX *comp = NULL; |
| 3915 |
int comp_len; |
| 3916 |
int ret = 0; |
| 3917 |
int alg = sc->ext.compress_certificate_from_peer[0]; |
| 3918 |
|
| 3919 |
/* Note that sc->s3.tmp.cert_req == 2 is checked in write transition */ |
| 3920 |
|
| 3921 |
if ((buf = BUF_MEM_new()) == NULL || !WPACKET_init(&tmppkt, buf)) |
| 3922 |
goto err; |
| 3923 |
|
| 3924 |
/* Use the |tmppkt| for the to-be-compressed data */ |
| 3925 |
if (sc->pha_context == NULL) { |
| 3926 |
/* no context available, add 0-length context */ |
| 3927 |
if (!WPACKET_put_bytes_u8(&tmppkt, 0)) |
| 3928 |
goto err; |
| 3929 |
} else if (!WPACKET_sub_memcpy_u8(&tmppkt, sc->pha_context, sc->pha_context_len)) |
| 3930 |
goto err; |
| 3931 |
|
| 3932 |
if (!ssl3_output_cert_chain(sc, &tmppkt, sc->cert->key, 0)) { |
| 3933 |
/* SSLfatal() already called */ |
| 3934 |
goto out; |
| 3935 |
} |
| 3936 |
|
| 3937 |
/* continue with the real |pkt| */ |
| 3938 |
if (!WPACKET_put_bytes_u16(pkt, alg) |
| 3939 |
|| !WPACKET_get_total_written(&tmppkt, &length) |
| 3940 |
|| !WPACKET_put_bytes_u24(pkt, length)) |
| 3941 |
goto err; |
| 3942 |
|
| 3943 |
switch (alg) { |
| 3944 |
case TLSEXT_comp_cert_zlib: |
| 3945 |
method = COMP_zlib_oneshot(); |
| 3946 |
break; |
| 3947 |
case TLSEXT_comp_cert_brotli: |
| 3948 |
method = COMP_brotli_oneshot(); |
| 3949 |
break; |
| 3950 |
case TLSEXT_comp_cert_zstd: |
| 3951 |
method = COMP_zstd_oneshot(); |
| 3952 |
break; |
| 3953 |
default: |
| 3954 |
goto err; |
| 3955 |
} |
| 3956 |
max_length = ossl_calculate_comp_expansion(alg, length); |
| 3957 |
|
| 3958 |
if ((comp = COMP_CTX_new(method)) == NULL |
| 3959 |
|| !WPACKET_start_sub_packet_u24(pkt) |
| 3960 |
|| !WPACKET_reserve_bytes(pkt, max_length, NULL)) |
| 3961 |
goto err; |
| 3962 |
|
| 3963 |
comp_len = COMP_compress_block(comp, WPACKET_get_curr(pkt), (int)max_length, |
| 3964 |
(unsigned char *)buf->data, (int)length); |
| 3965 |
if (comp_len <= 0) |
| 3966 |
goto err; |
| 3967 |
|
| 3968 |
if (!WPACKET_allocate_bytes(pkt, comp_len, NULL) |
| 3969 |
|| !WPACKET_close(pkt)) |
| 3970 |
goto err; |
| 3971 |
|
| 3972 |
/* |
| 3973 |
* If we attempted to write early data or we're in middlebox compat mode |
| 3974 |
* then we deferred changing the handshake write keys to the last possible |
| 3975 |
* moment. We need to do it now. |
| 3976 |
*/ |
| 3977 |
if (SSL_IS_FIRST_HANDSHAKE(sc) |
| 3978 |
&& !SSL_IS_QUIC_HANDSHAKE(sc) |
| 3979 |
&& (sc->early_data_state != SSL_EARLY_DATA_NONE |
| 3980 |
|| (sc->options & SSL_OP_ENABLE_MIDDLEBOX_COMPAT) != 0) |
| 3981 |
&& (!ssl->method->ssl3_enc->change_cipher_state(sc, |
| 3982 |
SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) { |
| 3983 |
/* |
| 3984 |
* This is a fatal error, which leaves sc->enc_write_ctx in an |
| 3985 |
* inconsistent state and thus ssl3_send_alert may crash. |
| 3986 |
*/ |
| 3987 |
SSLfatal(sc, SSL_AD_NO_ALERT, SSL_R_CANNOT_CHANGE_CIPHER); |
| 3988 |
goto out; |
| 3989 |
} |
| 3990 |
ret = 1; |
| 3991 |
goto out; |
| 3992 |
|
| 3993 |
err: |
| 3994 |
SSLfatal(sc, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 3995 |
out: |
| 3996 |
if (buf != NULL) { |
| 3997 |
/* If |buf| is NULL, then |tmppkt| could not have been initialized */ |
| 3998 |
WPACKET_cleanup(&tmppkt); |
| 3999 |
} |
| 4000 |
BUF_MEM_free(buf); |
| 4001 |
COMP_CTX_free(comp); |
| 4002 |
return ret; |
| 4003 |
} |
| 4004 |
#endif |
| 4005 |
|
| 4006 |
int ssl3_check_cert_and_algorithm(SSL_CONNECTION *s) |
| 4007 |
{ |
| 4008 |
const SSL_CERT_LOOKUP *clu; |
| 4009 |
size_t idx; |
| 4010 |
long alg_k, alg_a; |
| 4011 |
EVP_PKEY *pkey; |
| 4012 |
|
| 4013 |
alg_k = s->s3.tmp.new_cipher->algorithm_mkey; |
| 4014 |
alg_a = s->s3.tmp.new_cipher->algorithm_auth; |
| 4015 |
|
| 4016 |
/* we don't have a certificate */ |
| 4017 |
if (!(alg_a & SSL_aCERT)) |
| 4018 |
return 1; |
| 4019 |
|
| 4020 |
/* This is the passed certificate */ |
| 4021 |
pkey = tls_get_peer_pkey(s); |
| 4022 |
clu = ssl_cert_lookup_by_pkey(pkey, &idx, SSL_CONNECTION_GET_CTX(s)); |
| 4023 |
|
| 4024 |
/* Check certificate is recognised and suitable for cipher */ |
| 4025 |
if (clu == NULL || (alg_a & clu->amask) == 0) { |
| 4026 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_MISSING_SIGNING_CERT); |
| 4027 |
return 0; |
| 4028 |
} |
| 4029 |
|
| 4030 |
if (alg_k & (SSL_kRSA | SSL_kRSAPSK) && idx != SSL_PKEY_RSA) { |
| 4031 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, |
| 4032 |
SSL_R_MISSING_RSA_ENCRYPTING_CERT); |
| 4033 |
return 0; |
| 4034 |
} |
| 4035 |
|
| 4036 |
if ((alg_k & SSL_kDHE) && (s->s3.peer_tmp == NULL)) { |
| 4037 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4038 |
return 0; |
| 4039 |
} |
| 4040 |
|
| 4041 |
/* Early out to skip the checks below */ |
| 4042 |
if (s->session->peer_rpk != NULL) |
| 4043 |
return 1; |
| 4044 |
|
| 4045 |
if (clu->amask & SSL_aECDSA) { |
| 4046 |
if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s)) |
| 4047 |
return 1; |
| 4048 |
SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_R_BAD_ECC_CERT); |
| 4049 |
return 0; |
| 4050 |
} |
| 4051 |
|
| 4052 |
return 1; |
| 4053 |
} |
| 4054 |
|
| 4055 |
#ifndef OPENSSL_NO_NEXTPROTONEG |
| 4056 |
CON_FUNC_RETURN tls_construct_next_proto(SSL_CONNECTION *s, WPACKET *pkt) |
| 4057 |
{ |
| 4058 |
size_t len, padding_len; |
| 4059 |
unsigned char *padding = NULL; |
| 4060 |
|
| 4061 |
len = s->ext.npn_len; |
| 4062 |
padding_len = 32 - ((len + 2) % 32); |
| 4063 |
|
| 4064 |
if (!WPACKET_sub_memcpy_u8(pkt, s->ext.npn, len) |
| 4065 |
|| !WPACKET_sub_allocate_bytes_u8(pkt, padding_len, &padding)) { |
| 4066 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4067 |
return CON_FUNC_ERROR; |
| 4068 |
} |
| 4069 |
|
| 4070 |
memset(padding, 0, padding_len); |
| 4071 |
|
| 4072 |
return CON_FUNC_SUCCESS; |
| 4073 |
} |
| 4074 |
#endif |
| 4075 |
|
| 4076 |
MSG_PROCESS_RETURN tls_process_hello_req(SSL_CONNECTION *s, PACKET *pkt) |
| 4077 |
{ |
| 4078 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 4079 |
|
| 4080 |
if (PACKET_remaining(pkt) > 0) { |
| 4081 |
/* should contain no data */ |
| 4082 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 4083 |
return MSG_PROCESS_ERROR; |
| 4084 |
} |
| 4085 |
|
| 4086 |
if ((s->options & SSL_OP_NO_RENEGOTIATION)) { |
| 4087 |
ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_RENEGOTIATION); |
| 4088 |
return MSG_PROCESS_FINISHED_READING; |
| 4089 |
} |
| 4090 |
|
| 4091 |
/* |
| 4092 |
* This is a historical discrepancy (not in the RFC) maintained for |
| 4093 |
* compatibility reasons. If a TLS client receives a HelloRequest it will |
| 4094 |
* attempt an abbreviated handshake. However if a DTLS client receives a |
| 4095 |
* HelloRequest it will do a full handshake. Either behaviour is reasonable |
| 4096 |
* but doing one for TLS and another for DTLS is odd. |
| 4097 |
*/ |
| 4098 |
if (SSL_CONNECTION_IS_DTLS(s)) |
| 4099 |
SSL_renegotiate(ssl); |
| 4100 |
else |
| 4101 |
SSL_renegotiate_abbreviated(ssl); |
| 4102 |
|
| 4103 |
return MSG_PROCESS_FINISHED_READING; |
| 4104 |
} |
| 4105 |
|
| 4106 |
static MSG_PROCESS_RETURN tls_process_encrypted_extensions(SSL_CONNECTION *s, |
| 4107 |
PACKET *pkt) |
| 4108 |
{ |
| 4109 |
PACKET extensions; |
| 4110 |
RAW_EXTENSION *rawexts = NULL; |
| 4111 |
|
| 4112 |
if (!PACKET_as_length_prefixed_2(pkt, &extensions) |
| 4113 |
|| PACKET_remaining(pkt) != 0) { |
| 4114 |
SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_R_LENGTH_MISMATCH); |
| 4115 |
goto err; |
| 4116 |
} |
| 4117 |
|
| 4118 |
if (!tls_collect_extensions(s, &extensions, |
| 4119 |
SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, &rawexts, |
| 4120 |
NULL, 1) |
| 4121 |
|| !tls_parse_all_extensions(s, SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS, |
| 4122 |
rawexts, NULL, 0, 1)) { |
| 4123 |
/* SSLfatal() already called */ |
| 4124 |
goto err; |
| 4125 |
} |
| 4126 |
|
| 4127 |
OPENSSL_free(rawexts); |
| 4128 |
return MSG_PROCESS_CONTINUE_READING; |
| 4129 |
|
| 4130 |
err: |
| 4131 |
OPENSSL_free(rawexts); |
| 4132 |
return MSG_PROCESS_ERROR; |
| 4133 |
} |
| 4134 |
|
| 4135 |
int ssl_do_client_cert_cb(SSL_CONNECTION *s, X509 **px509, EVP_PKEY **ppkey) |
| 4136 |
{ |
| 4137 |
int i = 0; |
| 4138 |
SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s); |
| 4139 |
|
| 4140 |
#ifndef OPENSSL_NO_ENGINE |
| 4141 |
if (sctx->client_cert_engine) { |
| 4142 |
i = tls_engine_load_ssl_client_cert(s, px509, ppkey); |
| 4143 |
if (i != 0) |
| 4144 |
return i; |
| 4145 |
} |
| 4146 |
#endif |
| 4147 |
if (sctx->client_cert_cb) |
| 4148 |
i = sctx->client_cert_cb(SSL_CONNECTION_GET_USER_SSL(s), px509, ppkey); |
| 4149 |
return i; |
| 4150 |
} |
| 4151 |
|
| 4152 |
int ssl_cipher_list_to_bytes(SSL_CONNECTION *s, STACK_OF(SSL_CIPHER) *sk, |
| 4153 |
WPACKET *pkt) |
| 4154 |
{ |
| 4155 |
int i; |
| 4156 |
size_t totlen = 0, len, maxlen, maxverok = 0; |
| 4157 |
int empty_reneg_info_scsv = !s->renegotiate |
| 4158 |
&& !SSL_CONNECTION_IS_DTLS(s) |
| 4159 |
&& ssl_security(s, SSL_SECOP_VERSION, 0, TLS1_VERSION, NULL) |
| 4160 |
&& s->min_proto_version <= TLS1_VERSION; |
| 4161 |
SSL *ssl = SSL_CONNECTION_GET_SSL(s); |
| 4162 |
|
| 4163 |
/* Set disabled masks for this session */ |
| 4164 |
if (!ssl_set_client_disabled(s)) { |
| 4165 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_PROTOCOLS_AVAILABLE); |
| 4166 |
return 0; |
| 4167 |
} |
| 4168 |
|
| 4169 |
if (sk == NULL) { |
| 4170 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4171 |
return 0; |
| 4172 |
} |
| 4173 |
|
| 4174 |
#ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH |
| 4175 |
#if OPENSSL_MAX_TLS1_2_CIPHER_LENGTH < 6 |
| 4176 |
#error Max cipher length too short |
| 4177 |
#endif |
| 4178 |
/* |
| 4179 |
* Some servers hang if client hello > 256 bytes as hack workaround |
| 4180 |
* chop number of supported ciphers to keep it well below this if we |
| 4181 |
* use TLS v1.2 |
| 4182 |
*/ |
| 4183 |
if (TLS1_get_version(ssl) >= TLS1_2_VERSION) |
| 4184 |
maxlen = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1; |
| 4185 |
else |
| 4186 |
#endif |
| 4187 |
/* Maximum length that can be stored in 2 bytes. Length must be even */ |
| 4188 |
maxlen = 0xfffe; |
| 4189 |
|
| 4190 |
if (empty_reneg_info_scsv) |
| 4191 |
maxlen -= 2; |
| 4192 |
if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) |
| 4193 |
maxlen -= 2; |
| 4194 |
|
| 4195 |
for (i = 0; i < sk_SSL_CIPHER_num(sk) && totlen < maxlen; i++) { |
| 4196 |
const SSL_CIPHER *c; |
| 4197 |
|
| 4198 |
c = sk_SSL_CIPHER_value(sk, i); |
| 4199 |
/* Skip disabled ciphers */ |
| 4200 |
if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) |
| 4201 |
continue; |
| 4202 |
|
| 4203 |
if (!ssl->method->put_cipher_by_char(c, pkt, &len)) { |
| 4204 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4205 |
return 0; |
| 4206 |
} |
| 4207 |
|
| 4208 |
/* Sanity check that the maximum version we offer has ciphers enabled */ |
| 4209 |
if (!maxverok) { |
| 4210 |
int minproto = SSL_CONNECTION_IS_DTLS(s) ? c->min_dtls : c->min_tls; |
| 4211 |
int maxproto = SSL_CONNECTION_IS_DTLS(s) ? c->max_dtls : c->max_tls; |
| 4212 |
|
| 4213 |
if (ssl_version_cmp(s, maxproto, s->s3.tmp.max_ver) >= 0 |
| 4214 |
&& ssl_version_cmp(s, minproto, s->s3.tmp.max_ver) <= 0) |
| 4215 |
maxverok = 1; |
| 4216 |
} |
| 4217 |
|
| 4218 |
totlen += len; |
| 4219 |
} |
| 4220 |
|
| 4221 |
if (totlen == 0 || !maxverok) { |
| 4222 |
const char *maxvertext = !maxverok |
| 4223 |
? "No ciphers enabled for max supported SSL/TLS version" |
| 4224 |
: NULL; |
| 4225 |
|
| 4226 |
SSLfatal_data(s, SSL_AD_INTERNAL_ERROR, SSL_R_NO_CIPHERS_AVAILABLE, |
| 4227 |
maxvertext); |
| 4228 |
return 0; |
| 4229 |
} |
| 4230 |
|
| 4231 |
if (totlen != 0) { |
| 4232 |
if (empty_reneg_info_scsv) { |
| 4233 |
static const SSL_CIPHER scsv = { |
| 4234 |
0, NULL, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 4235 |
}; |
| 4236 |
if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) { |
| 4237 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4238 |
return 0; |
| 4239 |
} |
| 4240 |
} |
| 4241 |
if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) { |
| 4242 |
static const SSL_CIPHER scsv = { |
| 4243 |
0, NULL, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
| 4244 |
}; |
| 4245 |
if (!ssl->method->put_cipher_by_char(&scsv, pkt, &len)) { |
| 4246 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR); |
| 4247 |
return 0; |
| 4248 |
} |
| 4249 |
} |
| 4250 |
} |
| 4251 |
|
| 4252 |
return 1; |
| 4253 |
} |
| 4254 |
|
| 4255 |
CON_FUNC_RETURN tls_construct_end_of_early_data(SSL_CONNECTION *s, WPACKET *pkt) |
| 4256 |
{ |
| 4257 |
if (s->early_data_state != SSL_EARLY_DATA_WRITE_RETRY |
| 4258 |
&& s->early_data_state != SSL_EARLY_DATA_FINISHED_WRITING) { |
| 4259 |
SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
| 4260 |
return CON_FUNC_ERROR; |
| 4261 |
} |
| 4262 |
|
| 4263 |
s->early_data_state = SSL_EARLY_DATA_FINISHED_WRITING; |
| 4264 |
return CON_FUNC_SUCCESS; |
| 4265 |
} |