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>
23 * Map error codes to TLS/SSL alart types.
25 typedef struct x509err2alert_st {
30 /* Fixed value used in the ServerHello random field to identify an HRR */
31 const unsigned char hrrrandom[] = {
32 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
33 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
34 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
38 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
39 * SSL3_RT_CHANGE_CIPHER_SPEC)
41 int ssl3_do_write(SSL *s, int type)
46 ret = ssl3_write_bytes(s, type, &s->init_buf->data[s->init_off],
47 s->init_num, &written);
50 if (type == SSL3_RT_HANDSHAKE)
52 * should not be done for 'Hello Request's, but in that case we'll
53 * ignore the result anyway
54 * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
56 if (!SSL_IS_TLS13(s) || (s->statem.hand_state != TLS_ST_SW_SESSION_TICKET
57 && s->statem.hand_state != TLS_ST_CW_KEY_UPDATE
58 && s->statem.hand_state != TLS_ST_SW_KEY_UPDATE))
59 if (!ssl3_finish_mac(s,
60 (unsigned char *)&s->init_buf->data[s->init_off],
63 if (written == s->init_num) {
65 s->msg_callback(1, s->version, type, s->init_buf->data,
66 (size_t)(s->init_off + s->init_num), s,
70 s->init_off += written;
71 s->init_num -= written;
75 int tls_close_construct_packet(SSL *s, WPACKET *pkt, int htype)
79 if ((htype != SSL3_MT_CHANGE_CIPHER_SPEC && !WPACKET_close(pkt))
80 || !WPACKET_get_length(pkt, &msglen)
83 s->init_num = (int)msglen;
89 int tls_setup_handshake(SSL *s)
91 if (!ssl3_init_finished_mac(s)) {
92 /* SSLfatal() already called */
96 /* Reset any extension flags */
97 memset(s->ext.extflags, 0, sizeof(s->ext.extflags));
100 STACK_OF(SSL_CIPHER) *ciphers = SSL_get_ciphers(s);
101 int i, ver_min, ver_max, ok = 0;
104 * Sanity check that the maximum version we accept has ciphers
105 * enabled. For clients we do this check during construction of the
108 if (ssl_get_min_max_version(s, &ver_min, &ver_max) != 0) {
109 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_SETUP_HANDSHAKE,
110 ERR_R_INTERNAL_ERROR);
113 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
114 const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
116 if (SSL_IS_DTLS(s)) {
117 if (DTLS_VERSION_GE(ver_max, c->min_dtls) &&
118 DTLS_VERSION_LE(ver_max, c->max_dtls))
120 } else if (ver_max >= c->min_tls && ver_max <= c->max_tls) {
127 SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_TLS_SETUP_HANDSHAKE,
128 SSL_R_NO_CIPHERS_AVAILABLE);
129 ERR_add_error_data(1, "No ciphers enabled for max supported "
133 if (SSL_IS_FIRST_HANDSHAKE(s)) {
134 /* N.B. s->session_ctx == s->ctx here */
135 CRYPTO_atomic_add(&s->session_ctx->stats.sess_accept, 1, &i,
136 s->session_ctx->lock);
138 /* N.B. s->ctx may not equal s->session_ctx */
139 CRYPTO_atomic_add(&s->ctx->stats.sess_accept_renegotiate, 1, &i,
142 s->s3->tmp.cert_request = 0;
146 if (SSL_IS_FIRST_HANDSHAKE(s))
147 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect, 1, &discard,
148 s->session_ctx->lock);
150 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_renegotiate,
151 1, &discard, s->session_ctx->lock);
153 /* mark client_random uninitialized */
154 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
157 s->s3->tmp.cert_req = 0;
160 s->statem.use_timer = 1;
167 * Size of the to-be-signed TLS13 data, without the hash size itself:
168 * 64 bytes of value 32, 33 context bytes, 1 byte separator
170 #define TLS13_TBS_START_SIZE 64
171 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
173 static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
174 void **hdata, size_t *hdatalen)
176 static const char *servercontext = "TLS 1.3, server CertificateVerify";
177 static const char *clientcontext = "TLS 1.3, client CertificateVerify";
179 if (SSL_IS_TLS13(s)) {
182 /* Set the first 64 bytes of to-be-signed data to octet 32 */
183 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
184 /* This copies the 33 bytes of context plus the 0 separator byte */
185 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
186 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
187 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
189 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
192 * If we're currently reading then we need to use the saved handshake
193 * hash value. We can't use the current handshake hash state because
194 * that includes the CertVerify itself.
196 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
197 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
198 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
199 s->cert_verify_hash_len);
200 hashlen = s->cert_verify_hash_len;
201 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
202 EVP_MAX_MD_SIZE, &hashlen)) {
203 /* SSLfatal() already called */
208 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
212 retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
214 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_GET_CERT_VERIFY_TBS_DATA,
215 ERR_R_INTERNAL_ERROR);
224 int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
226 EVP_PKEY *pkey = NULL;
227 const EVP_MD *md = NULL;
228 EVP_MD_CTX *mctx = NULL;
229 EVP_PKEY_CTX *pctx = NULL;
230 size_t hdatalen = 0, siglen = 0;
232 unsigned char *sig = NULL;
233 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
234 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
236 if (lu == NULL || s->s3->tmp.cert == NULL) {
237 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
238 ERR_R_INTERNAL_ERROR);
241 pkey = s->s3->tmp.cert->privatekey;
243 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
244 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
245 ERR_R_INTERNAL_ERROR);
249 mctx = EVP_MD_CTX_new();
251 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
252 ERR_R_MALLOC_FAILURE);
256 /* Get the data to be signed */
257 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
258 /* SSLfatal() already called */
262 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
263 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
264 ERR_R_INTERNAL_ERROR);
267 siglen = EVP_PKEY_size(pkey);
268 sig = OPENSSL_malloc(siglen);
270 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
271 ERR_R_MALLOC_FAILURE);
275 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) {
276 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
281 if (lu->sig == EVP_PKEY_RSA_PSS) {
282 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
283 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
284 RSA_PSS_SALTLEN_DIGEST) <= 0) {
285 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
290 if (s->version == SSL3_VERSION) {
291 if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
292 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
293 (int)s->session->master_key_length,
294 s->session->master_key)
295 || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
297 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
301 } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
302 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
307 #ifndef OPENSSL_NO_GOST
309 int pktype = lu->sig;
311 if (pktype == NID_id_GostR3410_2001
312 || pktype == NID_id_GostR3410_2012_256
313 || pktype == NID_id_GostR3410_2012_512)
314 BUF_reverse(sig, NULL, siglen);
318 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
319 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
320 ERR_R_INTERNAL_ERROR);
324 /* Digest cached records and discard handshake buffer */
325 if (!ssl3_digest_cached_records(s, 0)) {
326 /* SSLfatal() already called */
331 EVP_MD_CTX_free(mctx);
335 EVP_MD_CTX_free(mctx);
339 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
341 EVP_PKEY *pkey = NULL;
342 const unsigned char *data;
343 #ifndef OPENSSL_NO_GOST
344 unsigned char *gost_data = NULL;
346 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
350 const EVP_MD *md = NULL;
353 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
354 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
355 EVP_PKEY_CTX *pctx = NULL;
358 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
359 ERR_R_MALLOC_FAILURE);
363 peer = s->session->peer;
364 pkey = X509_get0_pubkey(peer);
366 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
367 ERR_R_INTERNAL_ERROR);
371 if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
372 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,
373 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
377 if (SSL_USE_SIGALGS(s)) {
380 if (!PACKET_get_net_2(pkt, &sigalg)) {
381 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
385 if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
386 /* SSLfatal() already called */
390 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
392 } else if (!tls1_set_peer_legacy_sigalg(s, pkey)) {
393 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
394 ERR_R_INTERNAL_ERROR);
398 if (!tls1_lookup_md(s->s3->tmp.peer_sigalg, &md)) {
399 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
400 ERR_R_INTERNAL_ERROR);
404 /* Check for broken implementations of GOST ciphersuites */
406 * If key is GOST and len is exactly 64 or 128, it is signature without
407 * length field (CryptoPro implementations at least till TLS 1.2)
409 #ifndef OPENSSL_NO_GOST
410 if (!SSL_USE_SIGALGS(s)
411 && ((PACKET_remaining(pkt) == 64
412 && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
413 || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
414 || (PACKET_remaining(pkt) == 128
415 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
416 len = PACKET_remaining(pkt);
419 if (!PACKET_get_net_2(pkt, &len)) {
420 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
421 SSL_R_LENGTH_MISMATCH);
425 j = EVP_PKEY_size(pkey);
426 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
427 || (PACKET_remaining(pkt) == 0)) {
428 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
429 SSL_R_WRONG_SIGNATURE_SIZE);
432 if (!PACKET_get_bytes(pkt, &data, len)) {
433 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
434 SSL_R_LENGTH_MISMATCH);
438 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
439 /* SSLfatal() already called */
444 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
446 if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
447 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
451 #ifndef OPENSSL_NO_GOST
453 int pktype = EVP_PKEY_id(pkey);
454 if (pktype == NID_id_GostR3410_2001
455 || pktype == NID_id_GostR3410_2012_256
456 || pktype == NID_id_GostR3410_2012_512) {
457 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
458 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
459 SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
462 BUF_reverse(gost_data, data, len);
468 if (SSL_USE_PSS(s)) {
469 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
470 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
471 RSA_PSS_SALTLEN_DIGEST) <= 0) {
472 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
477 if (s->version == SSL3_VERSION) {
478 if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
479 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
480 (int)s->session->master_key_length,
481 s->session->master_key)) {
482 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
486 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
487 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
488 SSL_R_BAD_SIGNATURE);
492 j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
494 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
495 SSL_R_BAD_SIGNATURE);
500 ret = MSG_PROCESS_CONTINUE_READING;
502 BIO_free(s->s3->handshake_buffer);
503 s->s3->handshake_buffer = NULL;
504 EVP_MD_CTX_free(mctx);
505 #ifndef OPENSSL_NO_GOST
506 OPENSSL_free(gost_data);
511 int tls_construct_finished(SSL *s, WPACKET *pkt)
513 size_t finish_md_len;
517 /* This is a real handshake so make sure we clean it up at the end */
518 if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
519 s->statem.cleanuphand = 1;
522 * We only change the keys if we didn't already do this when we sent the
527 && s->s3->tmp.cert_req == 0
528 && (!s->method->ssl3_enc->change_cipher_state(s,
529 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
530 /* SSLfatal() already called */
535 sender = s->method->ssl3_enc->server_finished_label;
536 slen = s->method->ssl3_enc->server_finished_label_len;
538 sender = s->method->ssl3_enc->client_finished_label;
539 slen = s->method->ssl3_enc->client_finished_label_len;
542 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
544 s->s3->tmp.finish_md);
545 if (finish_md_len == 0) {
546 /* SSLfatal() already called */
550 s->s3->tmp.finish_md_len = finish_md_len;
552 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
553 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
554 ERR_R_INTERNAL_ERROR);
559 * Log the master secret, if logging is enabled. We don't log it for
560 * TLSv1.3: there's a different key schedule for that.
562 if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
563 s->session->master_key,
564 s->session->master_key_length)) {
565 /* SSLfatal() already called */
570 * Copy the finished so we can use it for renegotiation checks
572 if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
573 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
574 ERR_R_INTERNAL_ERROR);
578 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
580 s->s3->previous_client_finished_len = finish_md_len;
582 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
584 s->s3->previous_server_finished_len = finish_md_len;
590 int tls_construct_key_update(SSL *s, WPACKET *pkt)
592 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
593 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_KEY_UPDATE,
594 ERR_R_INTERNAL_ERROR);
598 s->key_update = SSL_KEY_UPDATE_NONE;
602 MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
604 unsigned int updatetype;
606 s->key_update_count++;
607 if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
608 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
609 SSL_R_TOO_MANY_KEY_UPDATES);
610 return MSG_PROCESS_ERROR;
614 * A KeyUpdate message signals a key change so the end of the message must
615 * be on a record boundary.
617 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
618 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_UPDATE,
619 SSL_R_NOT_ON_RECORD_BOUNDARY);
620 return MSG_PROCESS_ERROR;
623 if (!PACKET_get_1(pkt, &updatetype)
624 || PACKET_remaining(pkt) != 0) {
625 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_UPDATE,
626 SSL_R_BAD_KEY_UPDATE);
627 return MSG_PROCESS_ERROR;
631 * There are only two defined key update types. Fail if we get a value we
634 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
635 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
636 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
637 SSL_R_BAD_KEY_UPDATE);
638 return MSG_PROCESS_ERROR;
642 * If we get a request for us to update our sending keys too then, we need
643 * to additionally send a KeyUpdate message. However that message should
644 * not also request an update (otherwise we get into an infinite loop).
646 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
647 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
649 if (!tls13_update_key(s, 0)) {
650 /* SSLfatal() already called */
651 return MSG_PROCESS_ERROR;
654 return MSG_PROCESS_FINISHED_READING;
658 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
661 int ssl3_take_mac(SSL *s)
667 sender = s->method->ssl3_enc->server_finished_label;
668 slen = s->method->ssl3_enc->server_finished_label_len;
670 sender = s->method->ssl3_enc->client_finished_label;
671 slen = s->method->ssl3_enc->client_finished_label_len;
674 s->s3->tmp.peer_finish_md_len =
675 s->method->ssl3_enc->final_finish_mac(s, sender, slen,
676 s->s3->tmp.peer_finish_md);
678 if (s->s3->tmp.peer_finish_md_len == 0) {
679 /* SSLfatal() already called */
686 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
690 remain = PACKET_remaining(pkt);
692 * 'Change Cipher Spec' is just a single byte, which should already have
693 * been consumed by ssl_get_message() so there should be no bytes left,
694 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
696 if (SSL_IS_DTLS(s)) {
697 if ((s->version == DTLS1_BAD_VER
698 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
699 || (s->version != DTLS1_BAD_VER
700 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
701 SSLfatal(s, SSL_AD_DECODE_ERROR,
702 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
703 SSL_R_BAD_CHANGE_CIPHER_SPEC);
704 return MSG_PROCESS_ERROR;
708 SSLfatal(s, SSL_AD_DECODE_ERROR,
709 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
710 SSL_R_BAD_CHANGE_CIPHER_SPEC);
711 return MSG_PROCESS_ERROR;
715 /* Check we have a cipher to change to */
716 if (s->s3->tmp.new_cipher == NULL) {
717 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
718 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
719 return MSG_PROCESS_ERROR;
722 s->s3->change_cipher_spec = 1;
723 if (!ssl3_do_change_cipher_spec(s)) {
724 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
725 ERR_R_INTERNAL_ERROR);
726 return MSG_PROCESS_ERROR;
729 if (SSL_IS_DTLS(s)) {
730 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
732 if (s->version == DTLS1_BAD_VER)
733 s->d1->handshake_read_seq++;
735 #ifndef OPENSSL_NO_SCTP
737 * Remember that a CCS has been received, so that an old key of
738 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
741 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
745 return MSG_PROCESS_CONTINUE_READING;
748 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
753 /* This is a real handshake so make sure we clean it up at the end */
755 if (s->post_handshake_auth != SSL_PHA_REQUESTED)
756 s->statem.cleanuphand = 1;
757 if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) {
758 /* SSLfatal() already called */
759 return MSG_PROCESS_ERROR;
764 * In TLSv1.3 a Finished message signals a key change so the end of the
765 * message must be on a record boundary.
767 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
768 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
769 SSL_R_NOT_ON_RECORD_BOUNDARY);
770 return MSG_PROCESS_ERROR;
773 /* If this occurs, we have missed a message */
774 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
775 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
776 SSL_R_GOT_A_FIN_BEFORE_A_CCS);
777 return MSG_PROCESS_ERROR;
779 s->s3->change_cipher_spec = 0;
781 md_len = s->s3->tmp.peer_finish_md_len;
783 if (md_len != PACKET_remaining(pkt)) {
784 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_FINISHED,
785 SSL_R_BAD_DIGEST_LENGTH);
786 return MSG_PROCESS_ERROR;
789 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
791 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_FINISHED,
792 SSL_R_DIGEST_CHECK_FAILED);
793 return MSG_PROCESS_ERROR;
797 * Copy the finished so we can use it for renegotiation checks
799 if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
800 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_FINISHED,
801 ERR_R_INTERNAL_ERROR);
802 return MSG_PROCESS_ERROR;
805 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
807 s->s3->previous_client_finished_len = md_len;
809 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
811 s->s3->previous_server_finished_len = md_len;
815 * In TLS1.3 we also have to change cipher state and do any final processing
816 * of the initial server flight (if we are a client)
818 if (SSL_IS_TLS13(s)) {
820 if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
821 !s->method->ssl3_enc->change_cipher_state(s,
822 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
823 /* SSLfatal() already called */
824 return MSG_PROCESS_ERROR;
827 if (!s->method->ssl3_enc->generate_master_secret(s,
828 s->master_secret, s->handshake_secret, 0,
829 &s->session->master_key_length)) {
830 /* SSLfatal() already called */
831 return MSG_PROCESS_ERROR;
833 if (!s->method->ssl3_enc->change_cipher_state(s,
834 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
835 /* SSLfatal() already called */
836 return MSG_PROCESS_ERROR;
838 if (!tls_process_initial_server_flight(s)) {
839 /* SSLfatal() already called */
840 return MSG_PROCESS_ERROR;
845 return MSG_PROCESS_FINISHED_READING;
848 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
850 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
851 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
852 SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
859 /* Add a certificate to the WPACKET */
860 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain)
863 unsigned char *outbytes;
865 len = i2d_X509(x, NULL);
867 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
871 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
872 || i2d_X509(x, &outbytes) != len) {
873 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
874 ERR_R_INTERNAL_ERROR);
879 && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
881 /* SSLfatal() already called */
888 /* Add certificate chain to provided WPACKET */
889 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
893 STACK_OF(X509) *extra_certs;
894 STACK_OF(X509) *chain = NULL;
895 X509_STORE *chain_store;
897 if (cpk == NULL || cpk->x509 == NULL)
903 * If we have a certificate specific chain use it, else use parent ctx.
905 if (cpk->chain != NULL)
906 extra_certs = cpk->chain;
908 extra_certs = s->ctx->extra_certs;
910 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
912 else if (s->cert->chain_store)
913 chain_store = s->cert->chain_store;
915 chain_store = s->ctx->cert_store;
917 if (chain_store != NULL) {
918 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
920 if (xs_ctx == NULL) {
921 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
922 ERR_R_MALLOC_FAILURE);
925 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
926 X509_STORE_CTX_free(xs_ctx);
927 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
932 * It is valid for the chain not to be complete (because normally we
933 * don't include the root cert in the chain). Therefore we deliberately
934 * ignore the error return from this call. We're not actually verifying
935 * the cert - we're just building as much of the chain as we can
937 (void)X509_verify_cert(xs_ctx);
938 /* Don't leave errors in the queue */
940 chain = X509_STORE_CTX_get0_chain(xs_ctx);
941 i = ssl_security_cert_chain(s, chain, NULL, 0);
944 /* Dummy error calls so mkerr generates them */
945 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
946 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
947 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
949 X509_STORE_CTX_free(xs_ctx);
950 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
953 chain_count = sk_X509_num(chain);
954 for (i = 0; i < chain_count; i++) {
955 x = sk_X509_value(chain, i);
957 if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) {
958 /* SSLfatal() already called */
959 X509_STORE_CTX_free(xs_ctx);
963 X509_STORE_CTX_free(xs_ctx);
965 i = ssl_security_cert_chain(s, extra_certs, x, 0);
967 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
970 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) {
971 /* SSLfatal() already called */
974 for (i = 0; i < sk_X509_num(extra_certs); i++) {
975 x = sk_X509_value(extra_certs, i);
976 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) {
977 /* SSLfatal() already called */
985 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
987 if (!WPACKET_start_sub_packet_u24(pkt)) {
988 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
989 ERR_R_INTERNAL_ERROR);
993 if (!ssl_add_cert_chain(s, pkt, cpk))
996 if (!WPACKET_close(pkt)) {
997 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
998 ERR_R_INTERNAL_ERROR);
1006 * Tidy up after the end of a handshake. In the case of SCTP this may result
1007 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1010 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs, int stop)
1013 void (*cb) (const SSL *ssl, int type, int val) = NULL;
1016 if (!SSL_IS_DTLS(s)) {
1018 * We don't do this in DTLS because we may still need the init_buf
1019 * in case there are any unexpected retransmits
1021 BUF_MEM_free(s->init_buf);
1024 if (!ssl_free_wbio_buffer(s)) {
1025 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_FINISH_HANDSHAKE,
1026 ERR_R_INTERNAL_ERROR);
1032 if (SSL_IS_TLS13(s) && !s->server
1033 && s->post_handshake_auth == SSL_PHA_REQUESTED)
1034 s->post_handshake_auth = SSL_PHA_EXT_SENT;
1037 * Only set if there was a Finished message and this isn't after a TLSv1.3
1038 * post handshake exchange
1040 if (s->statem.cleanuphand) {
1041 /* skipped if we just sent a HelloRequest */
1044 s->statem.cleanuphand = 0;
1046 ssl3_cleanup_key_block(s);
1050 * In TLSv1.3 we update the cache as part of constructing the
1053 if (!SSL_IS_TLS13(s))
1054 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1056 /* N.B. s->ctx may not equal s->session_ctx */
1057 CRYPTO_atomic_add(&s->ctx->stats.sess_accept_good, 1, &discard,
1059 s->handshake_func = ossl_statem_accept;
1062 * In TLSv1.3 we update the cache as part of processing the
1065 if (!SSL_IS_TLS13(s))
1066 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1068 CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
1069 s->session_ctx->lock);
1071 s->handshake_func = ossl_statem_connect;
1072 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_good, 1,
1073 &discard, s->session_ctx->lock);
1077 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1079 if (SSL_IS_DTLS(s)) {
1080 /* done with handshaking */
1081 s->d1->handshake_read_seq = 0;
1082 s->d1->handshake_write_seq = 0;
1083 s->d1->next_handshake_write_seq = 0;
1084 dtls1_clear_received_buffer(s);
1088 if (s->info_callback != NULL)
1089 cb = s->info_callback;
1090 else if (s->ctx->info_callback != NULL)
1091 cb = s->ctx->info_callback;
1093 /* The callback may expect us to not be in init at handshake done */
1094 ossl_statem_set_in_init(s, 0);
1097 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1100 /* If we've got more work to do we go back into init */
1101 ossl_statem_set_in_init(s, 1);
1102 return WORK_FINISHED_CONTINUE;
1105 return WORK_FINISHED_STOP;
1108 int tls_get_message_header(SSL *s, int *mt)
1110 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1111 int skip_message, i, recvd_type;
1113 size_t l, readbytes;
1115 p = (unsigned char *)s->init_buf->data;
1118 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1119 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1121 SSL3_HM_HEADER_LENGTH - s->init_num,
1124 s->rwstate = SSL_READING;
1127 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1129 * A ChangeCipherSpec must be a single byte and may not occur
1130 * in the middle of a handshake message.
1132 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1133 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1134 SSL_F_TLS_GET_MESSAGE_HEADER,
1135 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1138 if (s->statem.hand_state == TLS_ST_BEFORE
1139 && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) {
1141 * We are stateless and we received a CCS. Probably this is
1142 * from a client between the first and second ClientHellos.
1143 * We should ignore this, but return an error because we do
1144 * not return success until we see the second ClientHello
1145 * with a valid cookie.
1149 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1150 s->init_num = readbytes - 1;
1151 s->init_msg = s->init_buf->data;
1152 s->s3->tmp.message_size = readbytes;
1154 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1155 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1156 SSL_F_TLS_GET_MESSAGE_HEADER,
1157 SSL_R_CCS_RECEIVED_EARLY);
1160 s->init_num += readbytes;
1165 if (s->statem.hand_state != TLS_ST_OK
1166 && p[0] == SSL3_MT_HELLO_REQUEST)
1168 * The server may always send 'Hello Request' messages --
1169 * we are doing a handshake anyway now, so ignore them if
1170 * their format is correct. Does not count for 'Finished'
1173 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1177 if (s->msg_callback)
1178 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1179 p, SSL3_HM_HEADER_LENGTH, s,
1180 s->msg_callback_arg);
1182 } while (skip_message);
1183 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1186 s->s3->tmp.message_type = *(p++);
1188 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1190 * Only happens with SSLv3+ in an SSLv2 backward compatible
1193 * Total message size is the remaining record bytes to read
1194 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1196 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1197 + SSL3_HM_HEADER_LENGTH;
1198 s->s3->tmp.message_size = l;
1200 s->init_msg = s->init_buf->data;
1201 s->init_num = SSL3_HM_HEADER_LENGTH;
1204 /* BUF_MEM_grow takes an 'int' parameter */
1205 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1206 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER,
1207 SSL_R_EXCESSIVE_MESSAGE_SIZE);
1210 s->s3->tmp.message_size = l;
1212 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1219 int tls_get_message_body(SSL *s, size_t *len)
1221 size_t n, readbytes;
1225 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1226 /* We've already read everything in */
1227 *len = (unsigned long)s->init_num;
1232 n = s->s3->tmp.message_size - s->init_num;
1234 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1235 &p[s->init_num], n, 0, &readbytes);
1237 s->rwstate = SSL_READING;
1241 s->init_num += readbytes;
1246 * If receiving Finished, record MAC of prior handshake messages for
1247 * Finished verification.
1249 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1250 /* SSLfatal() already called */
1255 /* Feed this message into MAC computation. */
1256 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1257 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1259 /* SSLfatal() already called */
1263 if (s->msg_callback)
1264 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1265 (size_t)s->init_num, s, s->msg_callback_arg);
1268 * We defer feeding in the HRR until later. We'll do it as part of
1269 * processing the message
1270 * The TLsv1.3 handshake transcript stops at the ClientFinished
1273 #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1274 /* KeyUpdate and NewSessionTicket do not need to be added */
1275 if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1276 && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1277 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
1278 || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1279 || memcmp(hrrrandom,
1280 s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1281 SSL3_RANDOM_SIZE) != 0) {
1282 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1283 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1284 /* SSLfatal() already called */
1290 if (s->msg_callback)
1291 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1292 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1293 s->msg_callback_arg);
1300 static const X509ERR2ALERT x509table[] = {
1301 {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
1302 {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1303 {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
1304 {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
1305 {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1306 {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1307 {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
1308 {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
1309 {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1310 {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
1311 {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1312 {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1313 {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1314 {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
1315 {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
1316 {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1317 {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1318 {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
1319 {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
1320 {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1321 {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1322 {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1323 {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
1324 {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
1325 {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
1326 {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1327 {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
1328 {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
1329 {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
1330 {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
1331 {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
1332 {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1333 {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1334 {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
1335 {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
1336 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
1337 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
1338 {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
1339 {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
1341 /* Last entry; return this if we don't find the value above. */
1342 {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
1345 int ssl_x509err2alert(int x509err)
1347 const X509ERR2ALERT *tp;
1349 for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
1350 if (tp->x509err == x509err)
1355 int ssl_allow_compression(SSL *s)
1357 if (s->options & SSL_OP_NO_COMPRESSION)
1359 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1362 static int version_cmp(const SSL *s, int a, int b)
1364 int dtls = SSL_IS_DTLS(s);
1369 return a < b ? -1 : 1;
1370 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1375 const SSL_METHOD *(*cmeth) (void);
1376 const SSL_METHOD *(*smeth) (void);
1379 #if TLS_MAX_VERSION != TLS1_3_VERSION
1380 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1383 /* Must be in order high to low */
1384 static const version_info tls_version_table[] = {
1385 #ifndef OPENSSL_NO_TLS1_3
1386 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1388 {TLS1_3_VERSION, NULL, NULL},
1390 #ifndef OPENSSL_NO_TLS1_2
1391 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1393 {TLS1_2_VERSION, NULL, NULL},
1395 #ifndef OPENSSL_NO_TLS1_1
1396 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1398 {TLS1_1_VERSION, NULL, NULL},
1400 #ifndef OPENSSL_NO_TLS1
1401 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1403 {TLS1_VERSION, NULL, NULL},
1405 #ifndef OPENSSL_NO_SSL3
1406 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1408 {SSL3_VERSION, NULL, NULL},
1413 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1414 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1417 /* Must be in order high to low */
1418 static const version_info dtls_version_table[] = {
1419 #ifndef OPENSSL_NO_DTLS1_2
1420 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1422 {DTLS1_2_VERSION, NULL, NULL},
1424 #ifndef OPENSSL_NO_DTLS1
1425 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1426 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1428 {DTLS1_VERSION, NULL, NULL},
1429 {DTLS1_BAD_VER, NULL, NULL},
1435 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1437 * @s: The SSL handle for the candidate method
1438 * @method: the intended method.
1440 * Returns 0 on success, or an SSL error reason on failure.
1442 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1444 int version = method->version;
1446 if ((s->min_proto_version != 0 &&
1447 version_cmp(s, version, s->min_proto_version) < 0) ||
1448 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1449 return SSL_R_VERSION_TOO_LOW;
1451 if (s->max_proto_version != 0 &&
1452 version_cmp(s, version, s->max_proto_version) > 0)
1453 return SSL_R_VERSION_TOO_HIGH;
1455 if ((s->options & method->mask) != 0)
1456 return SSL_R_UNSUPPORTED_PROTOCOL;
1457 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1458 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1464 * ssl_version_supported - Check that the specified `version` is supported by
1467 * @s: The SSL handle for the candidate method
1468 * @version: Protocol version to test against
1470 * Returns 1 when supported, otherwise 0
1472 int ssl_version_supported(const SSL *s, int version)
1474 const version_info *vent;
1475 const version_info *table;
1477 switch (s->method->version) {
1479 /* Version should match method version for non-ANY method */
1480 return version_cmp(s, version, s->version) == 0;
1481 case TLS_ANY_VERSION:
1482 table = tls_version_table;
1484 case DTLS_ANY_VERSION:
1485 table = dtls_version_table;
1490 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1492 if (vent->cmeth != NULL &&
1493 version_cmp(s, version, vent->version) == 0 &&
1494 ssl_method_error(s, vent->cmeth()) == 0) {
1502 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1503 * fallback indication from a client check whether we're using the highest
1504 * supported protocol version.
1506 * @s server SSL handle.
1508 * Returns 1 when using the highest enabled version, 0 otherwise.
1510 int ssl_check_version_downgrade(SSL *s)
1512 const version_info *vent;
1513 const version_info *table;
1516 * Check that the current protocol is the highest enabled version
1517 * (according to s->ctx->method, as version negotiation may have changed
1520 if (s->version == s->ctx->method->version)
1524 * Apparently we're using a version-flexible SSL_METHOD (not at its
1525 * highest protocol version).
1527 if (s->ctx->method->version == TLS_method()->version)
1528 table = tls_version_table;
1529 else if (s->ctx->method->version == DTLS_method()->version)
1530 table = dtls_version_table;
1532 /* Unexpected state; fail closed. */
1536 for (vent = table; vent->version != 0; ++vent) {
1537 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1538 return s->version == vent->version;
1544 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1545 * protocols, provided the initial (D)TLS method is version-flexible. This
1546 * function sanity-checks the proposed value and makes sure the method is
1547 * version-flexible, then sets the limit if all is well.
1549 * @method_version: The version of the current SSL_METHOD.
1550 * @version: the intended limit.
1551 * @bound: pointer to limit to be updated.
1553 * Returns 1 on success, 0 on failure.
1555 int ssl_set_version_bound(int method_version, int version, int *bound)
1563 * Restrict TLS methods to TLS protocol versions.
1564 * Restrict DTLS methods to DTLS protocol versions.
1565 * Note, DTLS version numbers are decreasing, use comparison macros.
1567 * Note that for both lower-bounds we use explicit versions, not
1568 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1569 * configurations. If the MIN (supported) version ever rises, the user's
1570 * "floor" remains valid even if no longer available. We don't expect the
1571 * MAX ceiling to ever get lower, so making that variable makes sense.
1573 switch (method_version) {
1576 * XXX For fixed version methods, should we always fail and not set any
1577 * bounds, always succeed and not set any bounds, or set the bounds and
1578 * arrange to fail later if they are not met? At present fixed-version
1579 * methods are not subject to controls that disable individual protocol
1584 case TLS_ANY_VERSION:
1585 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1589 case DTLS_ANY_VERSION:
1590 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1591 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1600 static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1602 if (vers == TLS1_2_VERSION
1603 && ssl_version_supported(s, TLS1_3_VERSION)) {
1604 *dgrd = DOWNGRADE_TO_1_2;
1605 } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
1606 && (ssl_version_supported(s, TLS1_2_VERSION)
1607 || ssl_version_supported(s, TLS1_3_VERSION))) {
1608 *dgrd = DOWNGRADE_TO_1_1;
1610 *dgrd = DOWNGRADE_NONE;
1615 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1616 * client HELLO is received to select the final server protocol version and
1617 * the version specific method.
1619 * @s: server SSL handle.
1621 * Returns 0 on success or an SSL error reason number on failure.
1623 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1626 * With version-flexible methods we have an initial state with:
1628 * s->method->version == (D)TLS_ANY_VERSION,
1629 * s->version == (D)TLS_MAX_VERSION.
1631 * So we detect version-flexible methods via the method version, not the
1634 int server_version = s->method->version;
1635 int client_version = hello->legacy_version;
1636 const version_info *vent;
1637 const version_info *table;
1639 RAW_EXTENSION *suppversions;
1641 s->client_version = client_version;
1643 switch (server_version) {
1645 if (!SSL_IS_TLS13(s)) {
1646 if (version_cmp(s, client_version, s->version) < 0)
1647 return SSL_R_WRONG_SSL_VERSION;
1648 *dgrd = DOWNGRADE_NONE;
1650 * If this SSL handle is not from a version flexible method we don't
1651 * (and never did) check min/max FIPS or Suite B constraints. Hope
1652 * that's OK. It is up to the caller to not choose fixed protocol
1653 * versions they don't want. If not, then easy to fix, just return
1654 * ssl_method_error(s, s->method)
1659 * Fall through if we are TLSv1.3 already (this means we must be after
1660 * a HelloRetryRequest
1663 case TLS_ANY_VERSION:
1664 table = tls_version_table;
1666 case DTLS_ANY_VERSION:
1667 table = dtls_version_table;
1671 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1673 /* If we did an HRR then supported versions is mandatory */
1674 if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
1675 return SSL_R_UNSUPPORTED_PROTOCOL;
1677 if (suppversions->present && !SSL_IS_DTLS(s)) {
1678 unsigned int candidate_vers = 0;
1679 unsigned int best_vers = 0;
1680 const SSL_METHOD *best_method = NULL;
1681 PACKET versionslist;
1683 suppversions->parsed = 1;
1685 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1686 /* Trailing or invalid data? */
1687 return SSL_R_LENGTH_MISMATCH;
1690 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1691 /* TODO(TLS1.3): Remove this before release */
1692 if (candidate_vers == TLS1_3_VERSION_DRAFT)
1693 candidate_vers = TLS1_3_VERSION;
1695 * TODO(TLS1.3): There is some discussion on the TLS list about
1696 * whether to ignore versions <TLS1.2 in supported_versions. At the
1697 * moment we honour them if present. To be reviewed later
1699 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1702 vent->version != 0 && vent->version != (int)candidate_vers;
1705 if (vent->version != 0 && vent->smeth != NULL) {
1706 const SSL_METHOD *method;
1708 method = vent->smeth();
1709 if (ssl_method_error(s, method) == 0) {
1710 best_vers = candidate_vers;
1711 best_method = method;
1715 if (PACKET_remaining(&versionslist) != 0) {
1716 /* Trailing data? */
1717 return SSL_R_LENGTH_MISMATCH;
1720 if (best_vers > 0) {
1721 if (s->hello_retry_request != SSL_HRR_NONE) {
1723 * This is after a HelloRetryRequest so we better check that we
1724 * negotiated TLSv1.3
1726 if (best_vers != TLS1_3_VERSION)
1727 return SSL_R_UNSUPPORTED_PROTOCOL;
1730 check_for_downgrade(s, best_vers, dgrd);
1731 s->version = best_vers;
1732 s->method = best_method;
1735 return SSL_R_UNSUPPORTED_PROTOCOL;
1739 * If the supported versions extension isn't present, then the highest
1740 * version we can negotiate is TLSv1.2
1742 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1743 client_version = TLS1_2_VERSION;
1746 * No supported versions extension, so we just use the version supplied in
1749 for (vent = table; vent->version != 0; ++vent) {
1750 const SSL_METHOD *method;
1752 if (vent->smeth == NULL ||
1753 version_cmp(s, client_version, vent->version) < 0)
1755 method = vent->smeth();
1756 if (ssl_method_error(s, method) == 0) {
1757 check_for_downgrade(s, vent->version, dgrd);
1758 s->version = vent->version;
1764 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1768 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1769 * server HELLO is received to select the final client protocol version and
1770 * the version specific method.
1772 * @s: client SSL handle.
1773 * @version: The proposed version from the server's HELLO.
1774 * @extensions: The extensions received
1776 * Returns 1 on success or 0 on error.
1778 int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
1780 const version_info *vent;
1781 const version_info *table;
1786 s->version = version;
1788 /* This will overwrite s->version if the extension is present */
1789 if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
1790 SSL_EXT_TLS1_2_SERVER_HELLO
1791 | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
1797 if (s->hello_retry_request != SSL_HRR_NONE
1798 && s->version != TLS1_3_VERSION) {
1800 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1801 SSL_R_WRONG_SSL_VERSION);
1805 switch (s->method->version) {
1807 if (s->version != s->method->version) {
1809 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1810 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1811 SSL_R_WRONG_SSL_VERSION);
1815 * If this SSL handle is not from a version flexible method we don't
1816 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1817 * that's OK. It is up to the caller to not choose fixed protocol
1818 * versions they don't want. If not, then easy to fix, just return
1819 * ssl_method_error(s, s->method)
1822 case TLS_ANY_VERSION:
1823 table = tls_version_table;
1825 case DTLS_ANY_VERSION:
1826 table = dtls_version_table;
1830 for (vent = table; vent->version != 0; ++vent) {
1831 const SSL_METHOD *method;
1834 if (vent->cmeth == NULL)
1837 if (highver != 0 && s->version != vent->version)
1840 method = vent->cmeth();
1841 err = ssl_method_error(s, method);
1843 if (s->version == vent->version) {
1845 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1846 SSL_F_SSL_CHOOSE_CLIENT_VERSION, err);
1853 highver = vent->version;
1855 if (s->version != vent->version)
1858 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1859 /* Check for downgrades */
1860 if (s->version == TLS1_2_VERSION && highver > s->version) {
1861 if (memcmp(tls12downgrade,
1862 s->s3->server_random + SSL3_RANDOM_SIZE
1863 - sizeof(tls12downgrade),
1864 sizeof(tls12downgrade)) == 0) {
1866 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1867 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1868 SSL_R_INAPPROPRIATE_FALLBACK);
1871 } else if (!SSL_IS_DTLS(s)
1872 && s->version < TLS1_2_VERSION
1873 && highver > s->version) {
1874 if (memcmp(tls11downgrade,
1875 s->s3->server_random + SSL3_RANDOM_SIZE
1876 - sizeof(tls11downgrade),
1877 sizeof(tls11downgrade)) == 0) {
1879 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1880 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1881 SSL_R_INAPPROPRIATE_FALLBACK);
1892 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1893 SSL_R_UNSUPPORTED_PROTOCOL);
1898 * ssl_get_min_max_version - get minimum and maximum protocol version
1899 * @s: The SSL connection
1900 * @min_version: The minimum supported version
1901 * @max_version: The maximum supported version
1903 * Work out what version we should be using for the initial ClientHello if the
1904 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1905 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1906 * constraints and any floor imposed by the security level here,
1907 * so we don't advertise the wrong protocol version to only reject the outcome later.
1909 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1910 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1911 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1913 * Returns 0 on success or an SSL error reason number on failure. On failure
1914 * min_version and max_version will also be set to 0.
1916 int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
1920 const SSL_METHOD *single = NULL;
1921 const SSL_METHOD *method;
1922 const version_info *table;
1923 const version_info *vent;
1925 switch (s->method->version) {
1928 * If this SSL handle is not from a version flexible method we don't
1929 * (and never did) check min/max FIPS or Suite B constraints. Hope
1930 * that's OK. It is up to the caller to not choose fixed protocol
1931 * versions they don't want. If not, then easy to fix, just return
1932 * ssl_method_error(s, s->method)
1934 *min_version = *max_version = s->version;
1936 case TLS_ANY_VERSION:
1937 table = tls_version_table;
1939 case DTLS_ANY_VERSION:
1940 table = dtls_version_table;
1945 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1946 * below X enabled. This is required in order to maintain the "version
1947 * capability" vector contiguous. Any versions with a NULL client method
1948 * (protocol version client is disabled at compile-time) is also a "hole".
1950 * Our initial state is hole == 1, version == 0. That is, versions above
1951 * the first version in the method table are disabled (a "hole" above
1952 * the valid protocol entries) and we don't have a selected version yet.
1954 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1955 * the selected version, and the method becomes a candidate "single"
1956 * method. We're no longer in a hole, so "hole" becomes 0.
1958 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1959 * as we support a contiguous range of at least two methods. If we hit
1960 * a disabled method, then hole becomes true again, but nothing else
1961 * changes yet, because all the remaining methods may be disabled too.
1962 * If we again hit an enabled method after the new hole, it becomes
1963 * selected, as we start from scratch.
1965 *min_version = version = 0;
1967 for (vent = table; vent->version != 0; ++vent) {
1969 * A table entry with a NULL client method is still a hole in the
1970 * "version capability" vector.
1972 if (vent->cmeth == NULL) {
1976 method = vent->cmeth();
1977 if (ssl_method_error(s, method) != 0) {
1981 *min_version = method->version;
1983 version = (single = method)->version;
1984 *min_version = version;
1989 *max_version = version;
1991 /* Fail if everything is disabled */
1993 return SSL_R_NO_PROTOCOLS_AVAILABLE;
1999 * ssl_set_client_hello_version - Work out what version we should be using for
2000 * the initial ClientHello.legacy_version field.
2002 * @s: client SSL handle.
2004 * Returns 0 on success or an SSL error reason number on failure.
2006 int ssl_set_client_hello_version(SSL *s)
2008 int ver_min, ver_max, ret;
2010 ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
2015 s->version = ver_max;
2017 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2018 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
2019 ver_max = TLS1_2_VERSION;
2021 s->client_version = ver_max;
2026 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2027 * and |checkallow| is 1 then additionally check if the group is allowed to be
2028 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2029 * 1) or 0 otherwise.
2031 #ifndef OPENSSL_NO_EC
2032 int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
2033 size_t num_groups, int checkallow)
2037 if (groups == NULL || num_groups == 0)
2040 for (i = 0; i < num_groups; i++) {
2041 uint16_t group = groups[i];
2043 if (group_id == group
2045 || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2054 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2055 int create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
2056 size_t hashlen, const unsigned char *hrr,
2059 unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2060 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2062 memset(msghdr, 0, sizeof(msghdr));
2064 if (hashval == NULL) {
2065 hashval = hashvaltmp;
2067 /* Get the hash of the initial ClientHello */
2068 if (!ssl3_digest_cached_records(s, 0)
2069 || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2071 /* SSLfatal() already called */
2076 /* Reinitialise the transcript hash */
2077 if (!ssl3_init_finished_mac(s)) {
2078 /* SSLfatal() already called */
2082 /* Inject the synthetic message_hash message */
2083 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2084 msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2085 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2086 || !ssl3_finish_mac(s, hashval, hashlen)) {
2087 /* SSLfatal() already called */
2092 * Now re-inject the HRR and current message if appropriate (we just deleted
2093 * it when we reinitialised the transcript hash above). Only necessary after
2094 * receiving a ClientHello2 with a cookie.
2097 && (!ssl3_finish_mac(s, hrr, hrrlen)
2098 || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2099 s->s3->tmp.message_size
2100 + SSL3_HM_HEADER_LENGTH))) {
2101 /* SSLfatal() already called */
2108 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2110 return X509_NAME_cmp(*a, *b);
2113 int parse_ca_names(SSL *s, PACKET *pkt)
2115 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2116 X509_NAME *xn = NULL;
2119 if (ca_sk == NULL) {
2120 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2121 ERR_R_MALLOC_FAILURE);
2124 /* get the CA RDNs */
2125 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2126 SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES,
2127 SSL_R_LENGTH_MISMATCH);
2131 while (PACKET_remaining(&cadns)) {
2132 const unsigned char *namestart, *namebytes;
2133 unsigned int name_len;
2135 if (!PACKET_get_net_2(&cadns, &name_len)
2136 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2137 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2138 SSL_R_LENGTH_MISMATCH);
2142 namestart = namebytes;
2143 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2144 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2148 if (namebytes != (namestart + name_len)) {
2149 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2150 SSL_R_CA_DN_LENGTH_MISMATCH);
2154 if (!sk_X509_NAME_push(ca_sk, xn)) {
2155 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2156 ERR_R_MALLOC_FAILURE);
2162 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
2163 s->s3->tmp.peer_ca_names = ca_sk;
2168 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2173 int construct_ca_names(SSL *s, WPACKET *pkt)
2175 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
2177 /* Start sub-packet for client CA list */
2178 if (!WPACKET_start_sub_packet_u16(pkt)) {
2179 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2180 ERR_R_INTERNAL_ERROR);
2184 if (ca_sk != NULL) {
2187 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2188 unsigned char *namebytes;
2189 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2193 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2194 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2196 || i2d_X509_NAME(name, &namebytes) != namelen) {
2197 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2198 ERR_R_INTERNAL_ERROR);
2204 if (!WPACKET_close(pkt)) {
2205 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2206 ERR_R_INTERNAL_ERROR);
2213 /* Create a buffer containing data to be signed for server key exchange */
2214 size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
2215 const void *param, size_t paramlen)
2217 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2218 unsigned char *tbs = OPENSSL_malloc(tbslen);
2221 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS,
2222 ERR_R_MALLOC_FAILURE);
2225 memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
2226 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
2228 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2235 * Saves the current handshake digest for Post-Handshake Auth,
2236 * Done after ClientFinished is processed, done exactly once
2238 int tls13_save_handshake_digest_for_pha(SSL *s)
2240 if (s->pha_dgst == NULL) {
2241 if (!ssl3_digest_cached_records(s, 1))
2242 /* SSLfatal() already called */
2245 s->pha_dgst = EVP_MD_CTX_new();
2246 if (s->pha_dgst == NULL) {
2247 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2248 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2249 ERR_R_INTERNAL_ERROR);
2252 if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2253 s->s3->handshake_dgst)) {
2254 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2255 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2256 ERR_R_INTERNAL_ERROR);
2264 * Restores the Post-Handshake Auth handshake digest
2265 * Done just before sending/processing the Cert Request
2267 int tls13_restore_handshake_digest_for_pha(SSL *s)
2269 if (s->pha_dgst == NULL) {
2270 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2271 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2272 ERR_R_INTERNAL_ERROR);
2275 if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst,
2277 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2278 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2279 ERR_R_INTERNAL_ERROR);