2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the OpenSSL license (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
14 #include "../ssl_locl.h"
15 #include "statem_locl.h"
16 #include "internal/cryptlib.h"
17 #include <openssl/buffer.h>
18 #include <openssl/objects.h>
19 #include <openssl/evp.h>
20 #include <openssl/x509.h>
22 /* Fixed value used in the ServerHello random field to identify an HRR */
23 const unsigned char hrrrandom[] = {
24 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
25 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
26 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
30 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
31 * SSL3_RT_CHANGE_CIPHER_SPEC)
33 int ssl3_do_write(SSL *s, int type)
38 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
39 s->init_num, &written);
42 if (type == SSL3_RT_HANDSHAKE)
44 * should not be done for 'Hello Request's, but in that case we'll
45 * ignore the result anyway
46 * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
48 if (!SSL_IS_TLS13(s) || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET
49 && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE
50 && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE))
51 if (!ssl3_finish_mac(s,
52 (unsigned char *)&s->init_buf->data[s->init_off],
55 if (written == s->init_num) {
57 s->msg_callback(1, s->version, type, s->init_buf->data,
58 (size_t)(s->init_off + s->init_num), s,
62 s->init_off += written;
63 s->init_num -= written;
67 int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
71 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
72 || !WPACKET_get_length(pkt, &msglen)
75 s->init_num = (int)msglen;
81 int tls_setup_handshake(SSL *s)
83 if (!ssl3_init_finished_mac(s)) {
84 /* SSLfatal() already called */
88 /* Reset any extension flags */
89 memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
92 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s);
93 int i, ver_min, ver_max, ok = 0;
96 * Sanity check that the maximum version we accept has ciphers
97 * enabled. For clients we do this check during construction of the
100 if (ssl_get_min_max_version(s, &ver_min, &ver_max) != 0) {
101 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_SETUP_HANDSHAKE,
102 ERR_R_INTERNAL_ERROR);
105 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
106 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
108 if (SSL_IS_DTLS(s)) {
109 if (DTLS_VERSION_GE(ver_max, c->min_dtls) &&
110 DTLS_VERSION_LE(ver_max, c->max_dtls))
112 } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) {
119 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_SETUP_HANDSHAKE,
120 SSL_R_NO_CIPHERS_AVAILABLE);
121 ERR_add_error_data(1, "No ciphers enabled for max supported "
125 if (SSL_IS_FIRST_HANDSHAKE(s)) {
126 /* N.B. s->session_ctx == s->ctx here */
127 CRYPTO_atomic_add(&s->session_ctx->stats.sess_accept, 1, &i,
128 s->session_ctx->lock);
130 /* N.B. s->ctx may not equal s->session_ctx */
131 CRYPTO_atomic_add(&s->ctx->stats.sess_accept_renegotiate, 1, &i,
134 s->s3->tmp.cert_request = 0;
138 if (SSL_IS_FIRST_HANDSHAKE(s))
139 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect, 1, &discard,
140 s->session_ctx->lock);
142 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_renegotiate,
143 1, &discard, s->session_ctx->lock);
145 /* mark client_random uninitialized */
146 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
149 s->s3->tmp.cert_req = 0;
152 s->statem.use_timer = 1;
159 * Size of the to-be-signed TLS13 data, without the hash size itself:
160 * 64 bytes of value 32, 33 context bytes, 1 byte separator
162 #define TLS13_TBS_START_SIZE 64
163 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
165 static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
166 void **hdata, size_t *hdatalen)
168 static const char *servercontext = "TLS 1.3, server CertificateVerify";
169 static const char *clientcontext = "TLS 1.3, client CertificateVerify";
171 if (SSL_IS_TLS13(s)) {
174 /* Set the first 64 bytes of to-be-signed data to octet 32 */
175 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
176 /* This copies the 33 bytes of context plus the 0 separator byte */
177 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
178 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
179 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
181 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
184 * If we're currently reading then we need to use the saved handshake
185 * hash value. We can't use the current handshake hash state because
186 * that includes the CertVerify itself.
188 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
189 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
190 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
191 s->cert_verify_hash_len);
192 hashlen = s->cert_verify_hash_len;
193 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
194 EVP_MAX_MD_SIZE, &hashlen)) {
195 /* SSLfatal() already called */
200 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
204 retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
206 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_GET_CERT_VERIFY_TBS_DATA,
207 ERR_R_INTERNAL_ERROR);
216 int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
218 EVP_PKEY *pkey = NULL;
219 const EVP_MD *md = NULL;
220 EVP_MD_CTX *mctx = NULL;
221 EVP_PKEY_CTX *pctx = NULL;
222 size_t hdatalen = 0, siglen = 0;
224 unsigned char *sig = NULL;
225 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
226 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
228 if (lu == NULL || s->s3->tmp.cert == NULL) {
229 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
230 ERR_R_INTERNAL_ERROR);
233 pkey = s->s3->tmp.cert->privatekey;
235 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
236 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
237 ERR_R_INTERNAL_ERROR);
241 mctx = EVP_MD_CTX_new();
243 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
244 ERR_R_MALLOC_FAILURE);
248 /* Get the data to be signed */
249 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
250 /* SSLfatal() already called */
254 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
255 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
256 ERR_R_INTERNAL_ERROR);
259 siglen = EVP_PKEY_size(pkey);
260 sig = OPENSSL_malloc(siglen);
262 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
263 ERR_R_MALLOC_FAILURE);
267 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) {
268 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
273 if (lu->sig == EVP_PKEY_RSA_PSS) {
274 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
275 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
276 RSA_PSS_SALTLEN_DIGEST) <= 0) {
277 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
282 if (s->version == SSL3_VERSION) {
283 if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
284 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
285 (int)s->session->master_key_length,
286 s->session->master_key)
287 || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
289 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
293 } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
294 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
299 #ifndef OPENSSL_NO_GOST
301 int pktype = lu->sig;
303 if (pktype == NID_id_GostR3410_2001
304 || pktype == NID_id_GostR3410_2012_256
305 || pktype == NID_id_GostR3410_2012_512)
306 BUF_reverse(sig, NULL, siglen);
310 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
311 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
312 ERR_R_INTERNAL_ERROR);
316 /* Digest cached records and discard handshake buffer */
317 if (!ssl3_digest_cached_records(s, 0)) {
318 /* SSLfatal() already called */
323 EVP_MD_CTX_free(mctx);
327 EVP_MD_CTX_free(mctx);
331 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
333 EVP_PKEY *pkey = NULL;
334 const unsigned char *data;
335 #ifndef OPENSSL_NO_GOST
336 unsigned char *gost_data = NULL;
338 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
342 const EVP_MD *md = NULL;
345 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
346 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
347 EVP_PKEY_CTX *pctx = NULL;
350 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
351 ERR_R_MALLOC_FAILURE);
355 peer = s->session->peer;
356 pkey = X509_get0_pubkey(peer);
358 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
359 ERR_R_INTERNAL_ERROR);
363 if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
364 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,
365 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
369 if (SSL_USE_SIGALGS(s)) {
372 if (!PACKET_get_net_2(pkt, &sigalg)) {
373 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
377 if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
378 /* SSLfatal() already called */
382 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
384 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
385 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
386 ERR_R_INTERNAL_ERROR);
390 if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
391 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
392 ERR_R_INTERNAL_ERROR);
396 /* Check for broken implementations of GOST ciphersuites */
398 * If key is GOST and len is exactly 64 or 128, it is signature without
399 * length field (CryptoPro implementations at least till TLS 1.2)
401 #ifndef OPENSSL_NO_GOST
402 if (!SSL_USE_SIGALGS(s)
403 && ((PACKET_remaining(pkt) == 64
404 && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
405 || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
406 || (PACKET_remaining(pkt) == 128
407 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
408 len = PACKET_remaining(pkt);
411 if (!PACKET_get_net_2(pkt, &len)) {
412 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
413 SSL_R_LENGTH_MISMATCH);
417 j = EVP_PKEY_size(pkey);
418 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
419 || (PACKET_remaining(pkt) == 0)) {
420 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
421 SSL_R_WRONG_SIGNATURE_SIZE);
424 if (!PACKET_get_bytes(pkt, &data, len)) {
425 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
426 SSL_R_LENGTH_MISMATCH);
430 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
431 /* SSLfatal() already called */
436 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
438 if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
439 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
443 #ifndef OPENSSL_NO_GOST
445 int pktype = EVP_PKEY_id(pkey);
446 if (pktype == NID_id_GostR3410_2001
447 || pktype == NID_id_GostR3410_2012_256
448 || pktype == NID_id_GostR3410_2012_512) {
449 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
450 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
451 SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
454 BUF_reverse(gost_data, data, len);
460 if (SSL_USE_PSS(s)) {
461 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
462 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
463 RSA_PSS_SALTLEN_DIGEST) <= 0) {
464 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
469 if (s->version == SSL3_VERSION) {
470 if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
471 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
472 (int)s->session->master_key_length,
473 s->session->master_key)) {
474 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
478 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
479 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
480 SSL_R_BAD_SIGNATURE);
484 j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
486 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
487 SSL_R_BAD_SIGNATURE);
492 ret = MSG_PROCESS_CONTINUE_READING;
494 BIO_free(s->s3->handshake_buffer);
495 s->s3->handshake_buffer = NULL;
496 EVP_MD_CTX_free(mctx);
497 #ifndef OPENSSL_NO_GOST
498 OPENSSL_free(gost_data);
503 int tls_construct_finished(SSL *s, WPACKET *pkt)
505 size_t finish_md_len;
509 /* This is a real handshake so make sure we clean it up at the end */
510 if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
511 s->statem.cleanuphand = 1;
514 * We only change the keys if we didn't already do this when we sent the
519 && s->s3->tmp.cert_req == 0
520 && (!s->method->ssl3_enc->change_cipher_state(s,
521 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
522 /* SSLfatal() already called */
527 sender = s->method->ssl3_enc->server_finished_label;
528 slen = s->method->ssl3_enc->server_finished_label_len;
530 sender = s->method->ssl3_enc->client_finished_label;
531 slen = s->method->ssl3_enc->client_finished_label_len;
534 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
536 s->s3->tmp.finish_md);
537 if (finish_md_len == 0) {
538 /* SSLfatal() already called */
542 s->s3->tmp.finish_md_len = finish_md_len;
544 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
545 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
546 ERR_R_INTERNAL_ERROR);
551 * Log the master secret, if logging is enabled. We don't log it for
552 * TLSv1.3: there's a different key schedule for that.
554 if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
555 s->session->master_key,
556 s->session->master_key_length)) {
557 /* SSLfatal() already called */
562 * Copy the finished so we can use it for renegotiation checks
564 if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
565 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
566 ERR_R_INTERNAL_ERROR);
570 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
572 s->s3->previous_client_finished_len = finish_md_len;
574 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
576 s->s3->previous_server_finished_len = finish_md_len;
582 int tls_construct_key_update(SSL *s, WPACKET *pkt)
584 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
585 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_KEY_UPDATE,
586 ERR_R_INTERNAL_ERROR);
590 s->key_update = SSL_KEY_UPDATE_NONE;
594 MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
596 unsigned int updatetype;
598 s->key_update_count++;
599 if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
600 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
601 SSL_R_TOO_MANY_KEY_UPDATES);
602 return MSG_PROCESS_ERROR;
606 * A KeyUpdate message signals a key change so the end of the message must
607 * be on a record boundary.
609 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
610 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_UPDATE,
611 SSL_R_NOT_ON_RECORD_BOUNDARY);
612 return MSG_PROCESS_ERROR;
615 if (!PACKET_get_1(pkt, &updatetype)
616 || PACKET_remaining(pkt) != 0) {
617 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_UPDATE,
618 SSL_R_BAD_KEY_UPDATE);
619 return MSG_PROCESS_ERROR;
623 * There are only two defined key update types. Fail if we get a value we
626 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
627 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
628 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
629 SSL_R_BAD_KEY_UPDATE);
630 return MSG_PROCESS_ERROR;
634 * If we get a request for us to update our sending keys too then, we need
635 * to additionally send a KeyUpdate message. However that message should
636 * not also request an update (otherwise we get into an infinite loop).
638 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
639 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
641 if (!tls13_update_key(s, 0)) {
642 /* SSLfatal() already called */
643 return MSG_PROCESS_ERROR;
646 return MSG_PROCESS_FINISHED_READING;
650 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
653 int ssl3_take_mac(SSL *s)
659 sender = s->method->ssl3_enc->server_finished_label;
660 slen = s->method->ssl3_enc->server_finished_label_len;
662 sender = s->method->ssl3_enc->client_finished_label;
663 slen = s->method->ssl3_enc->client_finished_label_len;
666 s->s3->tmp.peer_finish_md_len =
667 s->method->ssl3_enc->final_finish_mac(s, sender, slen,
668 s->s3->tmp.peer_finish_md);
670 if (s->s3->tmp.peer_finish_md_len == 0) {
671 /* SSLfatal() already called */
678 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
682 remain = PACKET_remaining(pkt);
684 * 'Change Cipher Spec' is just a single byte, which should already have
685 * been consumed by ssl_get_message() so there should be no bytes left,
686 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
688 if (SSL_IS_DTLS(s)) {
689 if ((s->version == DTLS1_BAD_VER
690 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
691 || (s->version != DTLS1_BAD_VER
692 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
693 SSLfatal(s, SSL_AD_DECODE_ERROR,
694 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
695 SSL_R_BAD_CHANGE_CIPHER_SPEC);
696 return MSG_PROCESS_ERROR;
700 SSLfatal(s, SSL_AD_DECODE_ERROR,
701 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
702 SSL_R_BAD_CHANGE_CIPHER_SPEC);
703 return MSG_PROCESS_ERROR;
707 /* Check we have a cipher to change to */
708 if (s->s3->tmp.new_cipher == NULL) {
709 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
710 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
711 return MSG_PROCESS_ERROR;
714 s->s3->change_cipher_spec = 1;
715 if (!ssl3_do_change_cipher_spec(s)) {
716 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
717 ERR_R_INTERNAL_ERROR);
718 return MSG_PROCESS_ERROR;
721 if (SSL_IS_DTLS(s)) {
722 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
724 if (s->version == DTLS1_BAD_VER)
725 s->d1->handshake_read_seq++;
727 #ifndef OPENSSL_NO_SCTP
729 * Remember that a CCS has been received, so that an old key of
730 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
733 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
737 return MSG_PROCESS_CONTINUE_READING;
740 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
745 /* This is a real handshake so make sure we clean it up at the end */
747 if (s->post_handshake_auth != SSL_PHA_REQUESTED)
748 s->statem.cleanuphand = 1;
749 if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) {
750 /* SSLfatal() already called */
751 return MSG_PROCESS_ERROR;
756 * In TLSv1.3 a Finished message signals a key change so the end of the
757 * message must be on a record boundary.
759 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
760 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
761 SSL_R_NOT_ON_RECORD_BOUNDARY);
762 return MSG_PROCESS_ERROR;
765 /* If this occurs, we have missed a message */
766 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
767 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
768 SSL_R_GOT_A_FIN_BEFORE_A_CCS);
769 return MSG_PROCESS_ERROR;
771 s->s3->change_cipher_spec = 0;
773 md_len = s->s3->tmp.peer_finish_md_len;
775 if (md_len != PACKET_remaining(pkt)) {
776 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_FINISHED,
777 SSL_R_BAD_DIGEST_LENGTH);
778 return MSG_PROCESS_ERROR;
781 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
783 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_FINISHED,
784 SSL_R_DIGEST_CHECK_FAILED);
785 return MSG_PROCESS_ERROR;
789 * Copy the finished so we can use it for renegotiation checks
791 if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
792 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_FINISHED,
793 ERR_R_INTERNAL_ERROR);
794 return MSG_PROCESS_ERROR;
797 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
799 s->s3->previous_client_finished_len = md_len;
801 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
803 s->s3->previous_server_finished_len = md_len;
807 * In TLS1.3 we also have to change cipher state and do any final processing
808 * of the initial server flight (if we are a client)
810 if (SSL_IS_TLS13(s)) {
812 if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
813 !s->method->ssl3_enc->change_cipher_state(s,
814 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
815 /* SSLfatal() already called */
816 return MSG_PROCESS_ERROR;
819 if (!s->method->ssl3_enc->generate_master_secret(s,
820 s->master_secret, s->handshake_secret, 0,
821 &s->session->master_key_length)) {
822 /* SSLfatal() already called */
823 return MSG_PROCESS_ERROR;
825 if (!s->method->ssl3_enc->change_cipher_state(s,
826 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
827 /* SSLfatal() already called */
828 return MSG_PROCESS_ERROR;
830 if (!tls_process_initial_server_flight(s)) {
831 /* SSLfatal() already called */
832 return MSG_PROCESS_ERROR;
837 return MSG_PROCESS_FINISHED_READING;
840 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
842 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
843 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
844 SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
851 /* Add a certificate to the WPACKET */
852 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain)
855 unsigned char *outbytes;
857 len = i2d_X509(x, NULL);
859 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
863 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
864 || i2d_X509(x, &outbytes) != len) {
865 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
866 ERR_R_INTERNAL_ERROR);
871 && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
873 /* SSLfatal() already called */
880 /* Add certificate chain to provided WPACKET */
881 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
885 STACK_OF(X509) *extra_certs;
886 STACK_OF(X509) *chain = NULL;
887 X509_STORE *chain_store;
889 if (cpk == NULL || cpk->x509 == NULL)
895 * If we have a certificate specific chain use it, else use parent ctx.
897 if (cpk->chain != NULL)
898 extra_certs = cpk->chain;
900 extra_certs = s->ctx->extra_certs;
902 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
904 else if (s->cert->chain_store)
905 chain_store = s->cert->chain_store;
907 chain_store = s->ctx->cert_store;
909 if (chain_store != NULL) {
910 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
912 if (xs_ctx == NULL) {
913 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
914 ERR_R_MALLOC_FAILURE);
917 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
918 X509_STORE_CTX_free(xs_ctx);
919 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
924 * It is valid for the chain not to be complete (because normally we
925 * don't include the root cert in the chain). Therefore we deliberately
926 * ignore the error return from this call. We're not actually verifying
927 * the cert - we're just building as much of the chain as we can
929 (void)X509_verify_cert(xs_ctx);
930 /* Don't leave errors in the queue */
932 chain = X509_STORE_CTX_get0_chain(xs_ctx);
933 i = ssl_security_cert_chain(s, chain, NULL, 0);
936 /* Dummy error calls so mkerr generates them */
937 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
938 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
939 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
941 X509_STORE_CTX_free(xs_ctx);
942 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
945 chain_count = sk_X509_num(chain);
946 for (i = 0; i < chain_count; i++) {
947 x = sk_X509_value(chain, i);
949 if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) {
950 /* SSLfatal() already called */
951 X509_STORE_CTX_free(xs_ctx);
955 X509_STORE_CTX_free(xs_ctx);
957 i = ssl_security_cert_chain(s, extra_certs, x, 0);
959 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
962 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) {
963 /* SSLfatal() already called */
966 for (i = 0; i < sk_X509_num(extra_certs); i++) {
967 x = sk_X509_value(extra_certs, i);
968 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) {
969 /* SSLfatal() already called */
977 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
979 if (!WPACKET_start_sub_packet_u24(pkt)) {
980 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
981 ERR_R_INTERNAL_ERROR);
985 if (!ssl_add_cert_chain(s, pkt, cpk))
988 if (!WPACKET_close(pkt)) {
989 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
990 ERR_R_INTERNAL_ERROR);
998 * Tidy up after the end of a handshake. In the case of SCTP this may result
999 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1002 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs, int stop)
1005 void (*cb) (const SSL *ssl, int type, int val) = NULL;
1008 if (!SSL_IS_DTLS(s)) {
1010 * We don't do this in DTLS because we may still need the init_buf
1011 * in case there are any unexpected retransmits
1013 BUF_MEM_free(s->init_buf);
1016 if (!ssl_free_wbio_buffer(s)) {
1017 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_FINISH_HANDSHAKE,
1018 ERR_R_INTERNAL_ERROR);
1024 if (SSL_IS_TLS13(s) && !s->server
1025 && s->post_handshake_auth == SSL_PHA_REQUESTED)
1026 s->post_handshake_auth = SSL_PHA_EXT_SENT;
1028 if (s->statem.cleanuphand) {
1029 /* skipped if we just sent a HelloRequest */
1032 s->statem.cleanuphand = 0;
1034 ssl3_cleanup_key_block(s);
1038 * In TLSv1.3 we update the cache as part of constructing the
1041 if (!SSL_IS_TLS13(s))
1042 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1044 /* N.B. s->ctx may not equal s->session_ctx */
1045 CRYPTO_atomic_add(&s->ctx->stats.sess_accept_good, 1, &discard,
1047 s->handshake_func = ossl_statem_accept;
1050 * In TLSv1.3 we update the cache as part of processing the
1053 if (!SSL_IS_TLS13(s))
1054 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1056 CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
1057 s->session_ctx->lock);
1059 s->handshake_func = ossl_statem_connect;
1060 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_good, 1,
1061 &discard, s->session_ctx->lock);
1064 if (s->info_callback != NULL)
1065 cb = s->info_callback;
1066 else if (s->ctx->info_callback != NULL)
1067 cb = s->ctx->info_callback;
1070 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1072 if (SSL_IS_DTLS(s)) {
1073 /* done with handshaking */
1074 s->d1->handshake_read_seq = 0;
1075 s->d1->handshake_write_seq = 0;
1076 s->d1->next_handshake_write_seq = 0;
1077 dtls1_clear_received_buffer(s);
1082 return WORK_FINISHED_CONTINUE;
1084 ossl_statem_set_in_init(s, 0);
1085 return WORK_FINISHED_STOP;
1088 int tls_get_message_header(SSL *s, int *mt)
1090 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1091 int skip_message, i, recvd_type;
1093 size_t l, readbytes;
1095 p = (unsigned char *)s->init_buf->data;
1098 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1099 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1101 SSL3_HM_HEADER_LENGTH - s->init_num,
1104 s->rwstate = SSL_READING;
1107 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1109 * A ChangeCipherSpec must be a single byte and may not occur
1110 * in the middle of a handshake message.
1112 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1113 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1114 SSL_F_TLS_GET_MESSAGE_HEADER,
1115 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1118 if (s->statem.hand_state == TLS_ST_BEFORE
1119 && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) {
1121 * We are stateless and we received a CCS. Probably this is
1122 * from a client between the first and second ClientHellos.
1123 * We should ignore this, but return an error because we do
1124 * not return success until we see the second ClientHello
1125 * with a valid cookie.
1129 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1130 s->init_num = readbytes - 1;
1131 s->init_msg = s->init_buf->data;
1132 s->s3->tmp.message_size = readbytes;
1134 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1135 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1136 SSL_F_TLS_GET_MESSAGE_HEADER,
1137 SSL_R_CCS_RECEIVED_EARLY);
1140 s->init_num += readbytes;
1145 if (s->statem.hand_state != TLS_ST_OK
1146 && p[0] == SSL3_MT_HELLO_REQUEST)
1148 * The server may always send 'Hello Request' messages --
1149 * we are doing a handshake anyway now, so ignore them if
1150 * their format is correct. Does not count for 'Finished'
1153 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1157 if (s->msg_callback)
1158 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1159 p, SSL3_HM_HEADER_LENGTH, s,
1160 s->msg_callback_arg);
1162 } while (skip_message);
1163 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1166 s->s3->tmp.message_type = *(p++);
1168 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1170 * Only happens with SSLv3+ in an SSLv2 backward compatible
1173 * Total message size is the remaining record bytes to read
1174 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1176 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1177 + SSL3_HM_HEADER_LENGTH;
1178 s->s3->tmp.message_size = l;
1180 s->init_msg = s->init_buf->data;
1181 s->init_num = SSL3_HM_HEADER_LENGTH;
1184 /* BUF_MEM_grow takes an 'int' parameter */
1185 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1186 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER,
1187 SSL_R_EXCESSIVE_MESSAGE_SIZE);
1190 s->s3->tmp.message_size = l;
1192 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1199 int tls_get_message_body(SSL *s, size_t *len)
1201 size_t n, readbytes;
1205 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1206 /* We've already read everything in */
1207 *len = (unsigned long)s->init_num;
1212 n = s->s3->tmp.message_size - s->init_num;
1214 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1215 &p[s->init_num], n, 0, &readbytes);
1217 s->rwstate = SSL_READING;
1221 s->init_num += readbytes;
1226 * If receiving Finished, record MAC of prior handshake messages for
1227 * Finished verification.
1229 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1230 /* SSLfatal() already called */
1235 /* Feed this message into MAC computation. */
1236 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1237 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1239 /* SSLfatal() already called */
1243 if (s->msg_callback)
1244 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1245 (size_t)s->init_num, s, s->msg_callback_arg);
1248 * We defer feeding in the HRR until later. We'll do it as part of
1249 * processing the message
1250 * The TLsv1.3 handshake transcript stops at the ClientFinished
1253 #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1254 /* KeyUpdate and NewSessionTicket do not need to be added */
1255 if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1256 && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1257 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
1258 || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1259 || memcmp(hrrrandom,
1260 s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1261 SSL3_RANDOM_SIZE) != 0) {
1262 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1263 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1264 /* SSLfatal() already called */
1270 if (s->msg_callback)
1271 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1272 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1273 s->msg_callback_arg);
1280 int ssl_verify_alarm_type(long type)
1285 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT:
1286 case X509_V_ERR_UNABLE_TO_GET_CRL:
1287 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER:
1288 al = SSL_AD_UNKNOWN_CA;
1290 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE:
1291 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE:
1292 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY:
1293 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD:
1294 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD:
1295 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD:
1296 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD:
1297 case X509_V_ERR_CERT_NOT_YET_VALID:
1298 case X509_V_ERR_CRL_NOT_YET_VALID:
1299 case X509_V_ERR_CERT_UNTRUSTED:
1300 case X509_V_ERR_CERT_REJECTED:
1301 case X509_V_ERR_HOSTNAME_MISMATCH:
1302 case X509_V_ERR_EMAIL_MISMATCH:
1303 case X509_V_ERR_IP_ADDRESS_MISMATCH:
1304 case X509_V_ERR_DANE_NO_MATCH:
1305 case X509_V_ERR_EE_KEY_TOO_SMALL:
1306 case X509_V_ERR_CA_KEY_TOO_SMALL:
1307 case X509_V_ERR_CA_MD_TOO_WEAK:
1308 al = SSL_AD_BAD_CERTIFICATE;
1310 case X509_V_ERR_CERT_SIGNATURE_FAILURE:
1311 case X509_V_ERR_CRL_SIGNATURE_FAILURE:
1312 al = SSL_AD_DECRYPT_ERROR;
1314 case X509_V_ERR_CERT_HAS_EXPIRED:
1315 case X509_V_ERR_CRL_HAS_EXPIRED:
1316 al = SSL_AD_CERTIFICATE_EXPIRED;
1318 case X509_V_ERR_CERT_REVOKED:
1319 al = SSL_AD_CERTIFICATE_REVOKED;
1321 case X509_V_ERR_UNSPECIFIED:
1322 case X509_V_ERR_OUT_OF_MEM:
1323 case X509_V_ERR_INVALID_CALL:
1324 case X509_V_ERR_STORE_LOOKUP:
1325 al = SSL_AD_INTERNAL_ERROR;
1327 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT:
1328 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN:
1329 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY:
1330 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE:
1331 case X509_V_ERR_CERT_CHAIN_TOO_LONG:
1332 case X509_V_ERR_PATH_LENGTH_EXCEEDED:
1333 case X509_V_ERR_INVALID_CA:
1334 al = SSL_AD_UNKNOWN_CA;
1336 case X509_V_ERR_APPLICATION_VERIFICATION:
1337 al = SSL_AD_HANDSHAKE_FAILURE;
1339 case X509_V_ERR_INVALID_PURPOSE:
1340 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
1343 al = SSL_AD_CERTIFICATE_UNKNOWN;
1349 int ssl_allow_compression(SSL *s)
1351 if (s->options & SSL_OP_NO_COMPRESSION)
1353 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1356 static int version_cmp(const SSL *s, int a, int b)
1358 int dtls = SSL_IS_DTLS(s);
1363 return a < b ? -1 : 1;
1364 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1369 const SSL_METHOD *(*cmeth) (void);
1370 const SSL_METHOD *(*smeth) (void);
1373 #if TLS_MAX_VERSION != TLS1_3_VERSION
1374 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1377 /* Must be in order high to low */
1378 static const version_info tls_version_table[] = {
1379 #ifndef OPENSSL_NO_TLS1_3
1380 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1382 {TLS1_3_VERSION, NULL, NULL},
1384 #ifndef OPENSSL_NO_TLS1_2
1385 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1387 {TLS1_2_VERSION, NULL, NULL},
1389 #ifndef OPENSSL_NO_TLS1_1
1390 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1392 {TLS1_1_VERSION, NULL, NULL},
1394 #ifndef OPENSSL_NO_TLS1
1395 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1397 {TLS1_VERSION, NULL, NULL},
1399 #ifndef OPENSSL_NO_SSL3
1400 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1402 {SSL3_VERSION, NULL, NULL},
1407 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1408 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1411 /* Must be in order high to low */
1412 static const version_info dtls_version_table[] = {
1413 #ifndef OPENSSL_NO_DTLS1_2
1414 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1416 {DTLS1_2_VERSION, NULL, NULL},
1418 #ifndef OPENSSL_NO_DTLS1
1419 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1420 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1422 {DTLS1_VERSION, NULL, NULL},
1423 {DTLS1_BAD_VER, NULL, NULL},
1429 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1431 * @s: The SSL handle for the candidate method
1432 * @method: the intended method.
1434 * Returns 0 on success, or an SSL error reason on failure.
1436 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1438 int version = method->version;
1440 if ((s->min_proto_version != 0 &&
1441 version_cmp(s, version, s->min_proto_version) < 0) ||
1442 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1443 return SSL_R_VERSION_TOO_LOW;
1445 if (s->max_proto_version != 0 &&
1446 version_cmp(s, version, s->max_proto_version) > 0)
1447 return SSL_R_VERSION_TOO_HIGH;
1449 if ((s->options & method->mask) != 0)
1450 return SSL_R_UNSUPPORTED_PROTOCOL;
1451 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1452 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1458 * ssl_version_supported - Check that the specified `version` is supported by
1461 * @s: The SSL handle for the candidate method
1462 * @version: Protocol version to test against
1464 * Returns 1 when supported, otherwise 0
1466 int ssl_version_supported(const SSL *s, int version)
1468 const version_info *vent;
1469 const version_info *table;
1471 switch (s->method->version) {
1473 /* Version should match method version for non-ANY method */
1474 return version_cmp(s, version, s->version) == 0;
1475 case TLS_ANY_VERSION:
1476 table = tls_version_table;
1478 case DTLS_ANY_VERSION:
1479 table = dtls_version_table;
1484 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1486 if (vent->cmeth != NULL &&
1487 version_cmp(s, version, vent->version) == 0 &&
1488 ssl_method_error(s, vent->cmeth()) == 0) {
1496 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1497 * fallback indication from a client check whether we're using the highest
1498 * supported protocol version.
1500 * @s server SSL handle.
1502 * Returns 1 when using the highest enabled version, 0 otherwise.
1504 int ssl_check_version_downgrade(SSL *s)
1506 const version_info *vent;
1507 const version_info *table;
1510 * Check that the current protocol is the highest enabled version
1511 * (according to s->ctx->method, as version negotiation may have changed
1514 if (s->version == s->ctx->method->version)
1518 * Apparently we're using a version-flexible SSL_METHOD (not at its
1519 * highest protocol version).
1521 if (s->ctx->method->version == TLS_method()->version)
1522 table = tls_version_table;
1523 else if (s->ctx->method->version == DTLS_method()->version)
1524 table = dtls_version_table;
1526 /* Unexpected state; fail closed. */
1530 for (vent = table; vent->version != 0; ++vent) {
1531 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1532 return s->version == vent->version;
1538 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1539 * protocols, provided the initial (D)TLS method is version-flexible. This
1540 * function sanity-checks the proposed value and makes sure the method is
1541 * version-flexible, then sets the limit if all is well.
1543 * @method_version: The version of the current SSL_METHOD.
1544 * @version: the intended limit.
1545 * @bound: pointer to limit to be updated.
1547 * Returns 1 on success, 0 on failure.
1549 int ssl_set_version_bound(int method_version, int version, int *bound)
1557 * Restrict TLS methods to TLS protocol versions.
1558 * Restrict DTLS methods to DTLS protocol versions.
1559 * Note, DTLS version numbers are decreasing, use comparison macros.
1561 * Note that for both lower-bounds we use explicit versions, not
1562 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1563 * configurations. If the MIN (supported) version ever rises, the user's
1564 * "floor" remains valid even if no longer available. We don't expect the
1565 * MAX ceiling to ever get lower, so making that variable makes sense.
1567 switch (method_version) {
1570 * XXX For fixed version methods, should we always fail and not set any
1571 * bounds, always succeed and not set any bounds, or set the bounds and
1572 * arrange to fail later if they are not met? At present fixed-version
1573 * methods are not subject to controls that disable individual protocol
1578 case TLS_ANY_VERSION:
1579 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1583 case DTLS_ANY_VERSION:
1584 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1585 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1594 static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1596 if (vers == TLS1_2_VERSION
1597 && ssl_version_supported(s, TLS1_3_VERSION)) {
1598 *dgrd = DOWNGRADE_TO_1_2;
1599 } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
1600 && (ssl_version_supported(s, TLS1_2_VERSION)
1601 || ssl_version_supported(s, TLS1_3_VERSION))) {
1602 *dgrd = DOWNGRADE_TO_1_1;
1604 *dgrd = DOWNGRADE_NONE;
1609 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1610 * client HELLO is received to select the final server protocol version and
1611 * the version specific method.
1613 * @s: server SSL handle.
1615 * Returns 0 on success or an SSL error reason number on failure.
1617 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1620 * With version-flexible methods we have an initial state with:
1622 * s->method->version == (D)TLS_ANY_VERSION,
1623 * s->version == (D)TLS_MAX_VERSION.
1625 * So we detect version-flexible methods via the method version, not the
1628 int server_version = s->method->version;
1629 int client_version = hello->legacy_version;
1630 const version_info *vent;
1631 const version_info *table;
1633 RAW_EXTENSION *suppversions;
1635 s->client_version = client_version;
1637 switch (server_version) {
1639 if (!SSL_IS_TLS13(s)) {
1640 if (version_cmp(s, client_version, s->version) < 0)
1641 return SSL_R_WRONG_SSL_VERSION;
1642 *dgrd = DOWNGRADE_NONE;
1644 * If this SSL handle is not from a version flexible method we don't
1645 * (and never did) check min/max FIPS or Suite B constraints. Hope
1646 * that's OK. It is up to the caller to not choose fixed protocol
1647 * versions they don't want. If not, then easy to fix, just return
1648 * ssl_method_error(s, s->method)
1653 * Fall through if we are TLSv1.3 already (this means we must be after
1654 * a HelloRetryRequest
1657 case TLS_ANY_VERSION:
1658 table = tls_version_table;
1660 case DTLS_ANY_VERSION:
1661 table = dtls_version_table;
1665 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1667 /* If we did an HRR then supported versions is mandatory */
1668 if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
1669 return SSL_R_UNSUPPORTED_PROTOCOL;
1671 if (suppversions->present && !SSL_IS_DTLS(s)) {
1672 unsigned int candidate_vers = 0;
1673 unsigned int best_vers = 0;
1674 const SSL_METHOD *best_method = NULL;
1675 PACKET versionslist;
1677 suppversions->parsed = 1;
1679 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1680 /* Trailing or invalid data? */
1681 return SSL_R_LENGTH_MISMATCH;
1684 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1685 /* TODO(TLS1.3): Remove this before release */
1686 if (candidate_vers == TLS1_3_VERSION_DRAFT)
1687 candidate_vers = TLS1_3_VERSION;
1689 * TODO(TLS1.3): There is some discussion on the TLS list about
1690 * whether to ignore versions <TLS1.2 in supported_versions. At the
1691 * moment we honour them if present. To be reviewed later
1693 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1696 vent->version != 0 && vent->version != (int)candidate_vers;
1699 if (vent->version != 0 && vent->smeth != NULL) {
1700 const SSL_METHOD *method;
1702 method = vent->smeth();
1703 if (ssl_method_error(s, method) == 0) {
1704 best_vers = candidate_vers;
1705 best_method = method;
1709 if (PACKET_remaining(&versionslist) != 0) {
1710 /* Trailing data? */
1711 return SSL_R_LENGTH_MISMATCH;
1714 if (best_vers > 0) {
1715 if (s->hello_retry_request != SSL_HRR_NONE) {
1717 * This is after a HelloRetryRequest so we better check that we
1718 * negotiated TLSv1.3
1720 if (best_vers != TLS1_3_VERSION)
1721 return SSL_R_UNSUPPORTED_PROTOCOL;
1724 check_for_downgrade(s, best_vers, dgrd);
1725 s->version = best_vers;
1726 s->method = best_method;
1729 return SSL_R_UNSUPPORTED_PROTOCOL;
1733 * If the supported versions extension isn't present, then the highest
1734 * version we can negotiate is TLSv1.2
1736 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1737 client_version = TLS1_2_VERSION;
1740 * No supported versions extension, so we just use the version supplied in
1743 for (vent = table; vent->version != 0; ++vent) {
1744 const SSL_METHOD *method;
1746 if (vent->smeth == NULL ||
1747 version_cmp(s, client_version, vent->version) < 0)
1749 method = vent->smeth();
1750 if (ssl_method_error(s, method) == 0) {
1751 check_for_downgrade(s, vent->version, dgrd);
1752 s->version = vent->version;
1758 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1762 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1763 * server HELLO is received to select the final client protocol version and
1764 * the version specific method.
1766 * @s: client SSL handle.
1767 * @version: The proposed version from the server's HELLO.
1768 * @extensions: The extensions received
1770 * Returns 1 on success or 0 on error.
1772 int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
1774 const version_info *vent;
1775 const version_info *table;
1780 s->version = version;
1782 /* This will overwrite s->version if the extension is present */
1783 if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
1784 SSL_EXT_TLS1_2_SERVER_HELLO
1785 | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
1791 if (s->hello_retry_request != SSL_HRR_NONE
1792 && s->version != TLS1_3_VERSION) {
1794 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1795 SSL_R_WRONG_SSL_VERSION);
1799 switch (s->method->version) {
1801 if (s->version != s->method->version) {
1803 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1804 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1805 SSL_R_WRONG_SSL_VERSION);
1809 * If this SSL handle is not from a version flexible method we don't
1810 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1811 * that's OK. It is up to the caller to not choose fixed protocol
1812 * versions they don't want. If not, then easy to fix, just return
1813 * ssl_method_error(s, s->method)
1816 case TLS_ANY_VERSION:
1817 table = tls_version_table;
1819 case DTLS_ANY_VERSION:
1820 table = dtls_version_table;
1824 for (vent = table; vent->version != 0; ++vent) {
1825 const SSL_METHOD *method;
1828 if (vent->cmeth == NULL)
1831 if (highver != 0 && s->version != vent->version)
1834 method = vent->cmeth();
1835 err = ssl_method_error(s, method);
1837 if (s->version == vent->version) {
1839 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1840 SSL_F_SSL_CHOOSE_CLIENT_VERSION, err);
1847 highver = vent->version;
1849 if (s->version != vent->version)
1852 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1853 /* Check for downgrades */
1854 if (s->version == TLS1_2_VERSION && highver > s->version) {
1855 if (memcmp(tls12downgrade,
1856 s->s3->server_random + SSL3_RANDOM_SIZE
1857 - sizeof(tls12downgrade),
1858 sizeof(tls12downgrade)) == 0) {
1860 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1861 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1862 SSL_R_INAPPROPRIATE_FALLBACK);
1865 } else if (!SSL_IS_DTLS(s)
1866 && s->version < TLS1_2_VERSION
1867 && highver > s->version) {
1868 if (memcmp(tls11downgrade,
1869 s->s3->server_random + SSL3_RANDOM_SIZE
1870 - sizeof(tls11downgrade),
1871 sizeof(tls11downgrade)) == 0) {
1873 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1874 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1875 SSL_R_INAPPROPRIATE_FALLBACK);
1886 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1887 SSL_R_UNSUPPORTED_PROTOCOL);
1892 * ssl_get_min_max_version - get minimum and maximum protocol version
1893 * @s: The SSL connection
1894 * @min_version: The minimum supported version
1895 * @max_version: The maximum supported version
1897 * Work out what version we should be using for the initial ClientHello if the
1898 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1899 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1900 * constraints and any floor imposed by the security level here,
1901 * so we don't advertise the wrong protocol version to only reject the outcome later.
1903 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1904 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1905 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1907 * Returns 0 on success or an SSL error reason number on failure. On failure
1908 * min_version and max_version will also be set to 0.
1910 int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
1914 const SSL_METHOD *single = NULL;
1915 const SSL_METHOD *method;
1916 const version_info *table;
1917 const version_info *vent;
1919 switch (s->method->version) {
1922 * If this SSL handle is not from a version flexible method we don't
1923 * (and never did) check min/max FIPS or Suite B constraints. Hope
1924 * that's OK. It is up to the caller to not choose fixed protocol
1925 * versions they don't want. If not, then easy to fix, just return
1926 * ssl_method_error(s, s->method)
1928 *min_version = *max_version = s->version;
1930 case TLS_ANY_VERSION:
1931 table = tls_version_table;
1933 case DTLS_ANY_VERSION:
1934 table = dtls_version_table;
1939 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1940 * below X enabled. This is required in order to maintain the "version
1941 * capability" vector contiguous. Any versions with a NULL client method
1942 * (protocol version client is disabled at compile-time) is also a "hole".
1944 * Our initial state is hole == 1, version == 0. That is, versions above
1945 * the first version in the method table are disabled (a "hole" above
1946 * the valid protocol entries) and we don't have a selected version yet.
1948 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1949 * the selected version, and the method becomes a candidate "single"
1950 * method. We're no longer in a hole, so "hole" becomes 0.
1952 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1953 * as we support a contiguous range of at least two methods. If we hit
1954 * a disabled method, then hole becomes true again, but nothing else
1955 * changes yet, because all the remaining methods may be disabled too.
1956 * If we again hit an enabled method after the new hole, it becomes
1957 * selected, as we start from scratch.
1959 *min_version = version = 0;
1961 for (vent = table; vent->version != 0; ++vent) {
1963 * A table entry with a NULL client method is still a hole in the
1964 * "version capability" vector.
1966 if (vent->cmeth == NULL) {
1970 method = vent->cmeth();
1971 if (ssl_method_error(s, method) != 0) {
1975 *min_version = method->version;
1977 version = (single = method)->version;
1978 *min_version = version;
1983 *max_version = version;
1985 /* Fail if everything is disabled */
1987 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1993 * ssl_set_client_hello_version - Work out what version we should be using for
1994 * the initial ClientHello.legacy_version field.
1996 * @s: client SSL handle.
1998 * Returns 0 on success or an SSL error reason number on failure.
2000 int ssl_set_client_hello_version(SSL *s)
2002 int ver_min, ver_max, ret;
2004 ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
2009 s->version = ver_max;
2011 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2012 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
2013 ver_max = TLS1_2_VERSION;
2015 s->client_version = ver_max;
2020 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2021 * and |checkallow| is 1 then additionally check if the group is allowed to be
2022 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2023 * 1) or 0 otherwise.
2025 #ifndef OPENSSL_NO_EC
2026 int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
2027 size_t num_groups, int checkallow)
2031 if (groups == NULL || num_groups == 0)
2034 for (i = 0; i < num_groups; i++) {
2035 uint16_t group = groups[i];
2037 if (group_id == group
2039 || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2048 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2049 int create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
2050 size_t hashlen, const unsigned char *hrr,
2053 unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2054 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2056 memset(msghdr, 0, sizeof(msghdr));
2058 if (hashval == NULL) {
2059 hashval = hashvaltmp;
2061 /* Get the hash of the initial ClientHello */
2062 if (!ssl3_digest_cached_records(s, 0)
2063 || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2065 /* SSLfatal() already called */
2070 /* Reinitialise the transcript hash */
2071 if (!ssl3_init_finished_mac(s)) {
2072 /* SSLfatal() already called */
2076 /* Inject the synthetic message_hash message */
2077 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2078 msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2079 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2080 || !ssl3_finish_mac(s, hashval, hashlen)) {
2081 /* SSLfatal() already called */
2086 * Now re-inject the HRR and current message if appropriate (we just deleted
2087 * it when we reinitialised the transcript hash above). Only necessary after
2088 * receiving a ClientHello2 with a cookie.
2091 && (!ssl3_finish_mac(s, hrr, hrrlen)
2092 || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2093 s->s3->tmp.message_size
2094 + SSL3_HM_HEADER_LENGTH))) {
2095 /* SSLfatal() already called */
2102 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2104 return X509_NAME_cmp(*a, *b);
2107 int parse_ca_names(SSL *s, PACKET *pkt)
2109 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2110 X509_NAME *xn = NULL;
2113 if (ca_sk == NULL) {
2114 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2115 ERR_R_MALLOC_FAILURE);
2118 /* get the CA RDNs */
2119 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2120 SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES,
2121 SSL_R_LENGTH_MISMATCH);
2125 while (PACKET_remaining(&cadns)) {
2126 const unsigned char *namestart, *namebytes;
2127 unsigned int name_len;
2129 if (!PACKET_get_net_2(&cadns, &name_len)
2130 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2131 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2132 SSL_R_LENGTH_MISMATCH);
2136 namestart = namebytes;
2137 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2138 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2142 if (namebytes != (namestart + name_len)) {
2143 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2144 SSL_R_CA_DN_LENGTH_MISMATCH);
2148 if (!sk_X509_NAME_push(ca_sk, xn)) {
2149 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2150 ERR_R_MALLOC_FAILURE);
2156 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
2157 s->s3->tmp.peer_ca_names = ca_sk;
2162 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2167 int construct_ca_names(SSL *s, WPACKET *pkt)
2169 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
2171 /* Start sub-packet for client CA list */
2172 if (!WPACKET_start_sub_packet_u16(pkt)) {
2173 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2174 ERR_R_INTERNAL_ERROR);
2178 if (ca_sk != NULL) {
2181 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2182 unsigned char *namebytes;
2183 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2187 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2188 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2190 || i2d_X509_NAME(name, &namebytes) != namelen) {
2191 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2192 ERR_R_INTERNAL_ERROR);
2198 if (!WPACKET_close(pkt)) {
2199 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2200 ERR_R_INTERNAL_ERROR);
2207 /* Create a buffer containing data to be signed for server key exchange */
2208 size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
2209 const void *param, size_t paramlen)
2211 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2212 unsigned char *tbs = OPENSSL_malloc(tbslen);
2215 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS,
2216 ERR_R_MALLOC_FAILURE);
2219 memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
2220 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
2222 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2229 * Saves the current handshake digest for Post-Handshake Auth,
2230 * Done after ClientFinished is processed, done exactly once
2232 int tls13_save_handshake_digest_for_pha(SSL *s)
2234 if (s->pha_dgst == NULL) {
2235 if (!ssl3_digest_cached_records(s, 1))
2236 /* SSLfatal() already called */
2239 s->pha_dgst = EVP_MD_CTX_new();
2240 if (s->pha_dgst == NULL) {
2241 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2242 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2243 ERR_R_INTERNAL_ERROR);
2246 if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2247 s->s3->handshake_dgst)) {
2248 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2249 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2250 ERR_R_INTERNAL_ERROR);
2258 * Restores the Post-Handshake Auth handshake digest
2259 * Done just before sending/processing the Cert Request
2261 int tls13_restore_handshake_digest_for_pha(SSL *s)
2263 if (s->pha_dgst == NULL) {
2264 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2265 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2266 ERR_R_INTERNAL_ERROR);
2269 if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst,
2271 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2272 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2273 ERR_R_INTERNAL_ERROR);