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 tsan_counter(&s->session_ctx->stats.sess_accept);
137 /* N.B. s->ctx may not equal s->session_ctx */
138 tsan_counter(&s->ctx->stats.sess_accept_renegotiate);
140 s->s3->tmp.cert_request = 0;
143 if (SSL_IS_FIRST_HANDSHAKE(s))
144 tsan_counter(&s->session_ctx->stats.sess_connect);
146 tsan_counter(&s->session_ctx->stats.sess_connect_renegotiate);
148 /* mark client_random uninitialized */
149 memset(s->s3->client_random, 0, sizeof(s->s3->client_random));
152 s->s3->tmp.cert_req = 0;
155 s->statem.use_timer = 1;
162 * Size of the to-be-signed TLS13 data, without the hash size itself:
163 * 64 bytes of value 32, 33 context bytes, 1 byte separator
165 #define TLS13_TBS_START_SIZE 64
166 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
168 static int get_cert_verify_tbs_data(SSL *s, unsigned char *tls13tbs,
169 void **hdata, size_t *hdatalen)
171 static const char *servercontext = "TLS 1.3, server CertificateVerify";
172 static const char *clientcontext = "TLS 1.3, client CertificateVerify";
174 if (SSL_IS_TLS13(s)) {
177 /* Set the first 64 bytes of to-be-signed data to octet 32 */
178 memset(tls13tbs, 32, TLS13_TBS_START_SIZE);
179 /* This copies the 33 bytes of context plus the 0 separator byte */
180 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
181 || s->statem.hand_state == TLS_ST_SW_CERT_VRFY)
182 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, servercontext);
184 strcpy((char *)tls13tbs + TLS13_TBS_START_SIZE, clientcontext);
187 * If we're currently reading then we need to use the saved handshake
188 * hash value. We can't use the current handshake hash state because
189 * that includes the CertVerify itself.
191 if (s->statem.hand_state == TLS_ST_CR_CERT_VRFY
192 || s->statem.hand_state == TLS_ST_SR_CERT_VRFY) {
193 memcpy(tls13tbs + TLS13_TBS_PREAMBLE_SIZE, s->cert_verify_hash,
194 s->cert_verify_hash_len);
195 hashlen = s->cert_verify_hash_len;
196 } else if (!ssl_handshake_hash(s, tls13tbs + TLS13_TBS_PREAMBLE_SIZE,
197 EVP_MAX_MD_SIZE, &hashlen)) {
198 /* SSLfatal() already called */
203 *hdatalen = TLS13_TBS_PREAMBLE_SIZE + hashlen;
207 retlen = BIO_get_mem_data(s->s3->handshake_buffer, hdata);
209 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_GET_CERT_VERIFY_TBS_DATA,
210 ERR_R_INTERNAL_ERROR);
219 int tls_construct_cert_verify(SSL *s, WPACKET *pkt)
221 EVP_PKEY *pkey = NULL;
222 const EVP_MD *md = NULL;
223 EVP_MD_CTX *mctx = NULL;
224 EVP_PKEY_CTX *pctx = NULL;
225 size_t hdatalen = 0, siglen = 0;
227 unsigned char *sig = NULL;
228 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
229 const SIGALG_LOOKUP *lu = s->s3->tmp.sigalg;
231 if (lu == NULL || s->s3->tmp.cert == NULL) {
232 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
233 ERR_R_INTERNAL_ERROR);
236 pkey = s->s3->tmp.cert->privatekey;
238 if (pkey == NULL || !tls1_lookup_md(lu, &md)) {
239 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
240 ERR_R_INTERNAL_ERROR);
244 mctx = EVP_MD_CTX_new();
246 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
247 ERR_R_MALLOC_FAILURE);
251 /* Get the data to be signed */
252 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
253 /* SSLfatal() already called */
257 if (SSL_USE_SIGALGS(s) && !WPACKET_put_bytes_u16(pkt, lu->sigalg)) {
258 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
259 ERR_R_INTERNAL_ERROR);
262 siglen = EVP_PKEY_size(pkey);
263 sig = OPENSSL_malloc(siglen);
265 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
266 ERR_R_MALLOC_FAILURE);
270 if (EVP_DigestSignInit(mctx, &pctx, md, NULL, pkey) <= 0) {
271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
276 if (lu->sig == EVP_PKEY_RSA_PSS) {
277 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
278 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
279 RSA_PSS_SALTLEN_DIGEST) <= 0) {
280 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
285 if (s->version == SSL3_VERSION) {
286 if (EVP_DigestSignUpdate(mctx, hdata, hdatalen) <= 0
287 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
288 (int)s->session->master_key_length,
289 s->session->master_key)
290 || EVP_DigestSignFinal(mctx, sig, &siglen) <= 0) {
292 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
296 } else if (EVP_DigestSign(mctx, sig, &siglen, hdata, hdatalen) <= 0) {
297 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
302 #ifndef OPENSSL_NO_GOST
304 int pktype = lu->sig;
306 if (pktype == NID_id_GostR3410_2001
307 || pktype == NID_id_GostR3410_2012_256
308 || pktype == NID_id_GostR3410_2012_512)
309 BUF_reverse(sig, NULL, siglen);
313 if (!WPACKET_sub_memcpy_u16(pkt, sig, siglen)) {
314 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_CERT_VERIFY,
315 ERR_R_INTERNAL_ERROR);
319 /* Digest cached records and discard handshake buffer */
320 if (!ssl3_digest_cached_records(s, 0)) {
321 /* SSLfatal() already called */
326 EVP_MD_CTX_free(mctx);
330 EVP_MD_CTX_free(mctx);
334 MSG_PROCESS_RETURN tls_process_cert_verify(SSL *s, PACKET *pkt)
336 EVP_PKEY *pkey = NULL;
337 const unsigned char *data;
338 #ifndef OPENSSL_NO_GOST
339 unsigned char *gost_data = NULL;
341 MSG_PROCESS_RETURN ret = MSG_PROCESS_ERROR;
345 const EVP_MD *md = NULL;
348 unsigned char tls13tbs[TLS13_TBS_PREAMBLE_SIZE + EVP_MAX_MD_SIZE];
349 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
350 EVP_PKEY_CTX *pctx = NULL;
353 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
354 ERR_R_MALLOC_FAILURE);
358 peer = s->session->peer;
359 pkey = X509_get0_pubkey(peer);
361 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
362 ERR_R_INTERNAL_ERROR);
366 if (ssl_cert_lookup_by_pkey(pkey, NULL) == NULL) {
367 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_CERT_VERIFY,
368 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
372 if (SSL_USE_SIGALGS(s)) {
375 if (!PACKET_get_net_2(pkt, &sigalg)) {
376 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
380 if (tls12_check_peer_sigalg(s, sigalg, pkey) <= 0) {
381 /* SSLfatal() already called */
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);
397 if (SSL_USE_SIGALGS(s))
398 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
401 /* Check for broken implementations of GOST ciphersuites */
403 * If key is GOST and len is exactly 64 or 128, it is signature without
404 * length field (CryptoPro implementations at least till TLS 1.2)
406 #ifndef OPENSSL_NO_GOST
407 if (!SSL_USE_SIGALGS(s)
408 && ((PACKET_remaining(pkt) == 64
409 && (EVP_PKEY_id(pkey) == NID_id_GostR3410_2001
410 || EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_256))
411 || (PACKET_remaining(pkt) == 128
412 && EVP_PKEY_id(pkey) == NID_id_GostR3410_2012_512))) {
413 len = PACKET_remaining(pkt);
416 if (!PACKET_get_net_2(pkt, &len)) {
417 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
418 SSL_R_LENGTH_MISMATCH);
422 j = EVP_PKEY_size(pkey);
423 if (((int)len > j) || ((int)PACKET_remaining(pkt) > j)
424 || (PACKET_remaining(pkt) == 0)) {
425 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
426 SSL_R_WRONG_SIGNATURE_SIZE);
429 if (!PACKET_get_bytes(pkt, &data, len)) {
430 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
431 SSL_R_LENGTH_MISMATCH);
435 if (!get_cert_verify_tbs_data(s, tls13tbs, &hdata, &hdatalen)) {
436 /* SSLfatal() already called */
441 fprintf(stderr, "Using client verify alg %s\n", EVP_MD_name(md));
443 if (EVP_DigestVerifyInit(mctx, &pctx, md, NULL, pkey) <= 0) {
444 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
448 #ifndef OPENSSL_NO_GOST
450 int pktype = EVP_PKEY_id(pkey);
451 if (pktype == NID_id_GostR3410_2001
452 || pktype == NID_id_GostR3410_2012_256
453 || pktype == NID_id_GostR3410_2012_512) {
454 if ((gost_data = OPENSSL_malloc(len)) == NULL) {
455 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
456 SSL_F_TLS_PROCESS_CERT_VERIFY, ERR_R_MALLOC_FAILURE);
459 BUF_reverse(gost_data, data, len);
465 if (SSL_USE_PSS(s)) {
466 if (EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING) <= 0
467 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx,
468 RSA_PSS_SALTLEN_DIGEST) <= 0) {
469 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
474 if (s->version == SSL3_VERSION) {
475 if (EVP_DigestVerifyUpdate(mctx, hdata, hdatalen) <= 0
476 || !EVP_MD_CTX_ctrl(mctx, EVP_CTRL_SSL3_MASTER_SECRET,
477 (int)s->session->master_key_length,
478 s->session->master_key)) {
479 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
483 if (EVP_DigestVerifyFinal(mctx, data, len) <= 0) {
484 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
485 SSL_R_BAD_SIGNATURE);
489 j = EVP_DigestVerify(mctx, data, len, hdata, hdatalen);
491 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_CERT_VERIFY,
492 SSL_R_BAD_SIGNATURE);
497 ret = MSG_PROCESS_CONTINUE_READING;
499 BIO_free(s->s3->handshake_buffer);
500 s->s3->handshake_buffer = NULL;
501 EVP_MD_CTX_free(mctx);
502 #ifndef OPENSSL_NO_GOST
503 OPENSSL_free(gost_data);
508 int tls_construct_finished(SSL *s, WPACKET *pkt)
510 size_t finish_md_len;
514 /* This is a real handshake so make sure we clean it up at the end */
515 if (!s->server && s->post_handshake_auth != SSL_PHA_REQUESTED)
516 s->statem.cleanuphand = 1;
519 * We only change the keys if we didn't already do this when we sent the
524 && s->s3->tmp.cert_req == 0
525 && (!s->method->ssl3_enc->change_cipher_state(s,
526 SSL3_CC_HANDSHAKE | SSL3_CHANGE_CIPHER_CLIENT_WRITE))) {;
527 /* SSLfatal() already called */
532 sender = s->method->ssl3_enc->server_finished_label;
533 slen = s->method->ssl3_enc->server_finished_label_len;
535 sender = s->method->ssl3_enc->client_finished_label;
536 slen = s->method->ssl3_enc->client_finished_label_len;
539 finish_md_len = s->method->ssl3_enc->final_finish_mac(s,
541 s->s3->tmp.finish_md);
542 if (finish_md_len == 0) {
543 /* SSLfatal() already called */
547 s->s3->tmp.finish_md_len = finish_md_len;
549 if (!WPACKET_memcpy(pkt, s->s3->tmp.finish_md, finish_md_len)) {
550 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
551 ERR_R_INTERNAL_ERROR);
556 * Log the master secret, if logging is enabled. We don't log it for
557 * TLSv1.3: there's a different key schedule for that.
559 if (!SSL_IS_TLS13(s) && !ssl_log_secret(s, MASTER_SECRET_LABEL,
560 s->session->master_key,
561 s->session->master_key_length)) {
562 /* SSLfatal() already called */
567 * Copy the finished so we can use it for renegotiation checks
569 if (!ossl_assert(finish_md_len <= EVP_MAX_MD_SIZE)) {
570 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_FINISHED,
571 ERR_R_INTERNAL_ERROR);
575 memcpy(s->s3->previous_client_finished, s->s3->tmp.finish_md,
577 s->s3->previous_client_finished_len = finish_md_len;
579 memcpy(s->s3->previous_server_finished, s->s3->tmp.finish_md,
581 s->s3->previous_server_finished_len = finish_md_len;
587 int tls_construct_key_update(SSL *s, WPACKET *pkt)
589 if (!WPACKET_put_bytes_u8(pkt, s->key_update)) {
590 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_CONSTRUCT_KEY_UPDATE,
591 ERR_R_INTERNAL_ERROR);
595 s->key_update = SSL_KEY_UPDATE_NONE;
599 MSG_PROCESS_RETURN tls_process_key_update(SSL *s, PACKET *pkt)
601 unsigned int updatetype;
603 s->key_update_count++;
604 if (s->key_update_count > MAX_KEY_UPDATE_MESSAGES) {
605 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
606 SSL_R_TOO_MANY_KEY_UPDATES);
607 return MSG_PROCESS_ERROR;
611 * A KeyUpdate message signals a key change so the end of the message must
612 * be on a record boundary.
614 if (RECORD_LAYER_processed_read_pending(&s->rlayer)) {
615 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_KEY_UPDATE,
616 SSL_R_NOT_ON_RECORD_BOUNDARY);
617 return MSG_PROCESS_ERROR;
620 if (!PACKET_get_1(pkt, &updatetype)
621 || PACKET_remaining(pkt) != 0) {
622 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_KEY_UPDATE,
623 SSL_R_BAD_KEY_UPDATE);
624 return MSG_PROCESS_ERROR;
628 * There are only two defined key update types. Fail if we get a value we
631 if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
632 && updatetype != SSL_KEY_UPDATE_REQUESTED) {
633 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_PROCESS_KEY_UPDATE,
634 SSL_R_BAD_KEY_UPDATE);
635 return MSG_PROCESS_ERROR;
639 * If we get a request for us to update our sending keys too then, we need
640 * to additionally send a KeyUpdate message. However that message should
641 * not also request an update (otherwise we get into an infinite loop).
643 if (updatetype == SSL_KEY_UPDATE_REQUESTED)
644 s->key_update = SSL_KEY_UPDATE_NOT_REQUESTED;
646 if (!tls13_update_key(s, 0)) {
647 /* SSLfatal() already called */
648 return MSG_PROCESS_ERROR;
651 return MSG_PROCESS_FINISHED_READING;
655 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
658 int ssl3_take_mac(SSL *s)
664 sender = s->method->ssl3_enc->server_finished_label;
665 slen = s->method->ssl3_enc->server_finished_label_len;
667 sender = s->method->ssl3_enc->client_finished_label;
668 slen = s->method->ssl3_enc->client_finished_label_len;
671 s->s3->tmp.peer_finish_md_len =
672 s->method->ssl3_enc->final_finish_mac(s, sender, slen,
673 s->s3->tmp.peer_finish_md);
675 if (s->s3->tmp.peer_finish_md_len == 0) {
676 /* SSLfatal() already called */
683 MSG_PROCESS_RETURN tls_process_change_cipher_spec(SSL *s, PACKET *pkt)
687 remain = PACKET_remaining(pkt);
689 * 'Change Cipher Spec' is just a single byte, which should already have
690 * been consumed by ssl_get_message() so there should be no bytes left,
691 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
693 if (SSL_IS_DTLS(s)) {
694 if ((s->version == DTLS1_BAD_VER
695 && remain != DTLS1_CCS_HEADER_LENGTH + 1)
696 || (s->version != DTLS1_BAD_VER
697 && remain != DTLS1_CCS_HEADER_LENGTH - 1)) {
698 SSLfatal(s, SSL_AD_DECODE_ERROR,
699 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
700 SSL_R_BAD_CHANGE_CIPHER_SPEC);
701 return MSG_PROCESS_ERROR;
705 SSLfatal(s, SSL_AD_DECODE_ERROR,
706 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
707 SSL_R_BAD_CHANGE_CIPHER_SPEC);
708 return MSG_PROCESS_ERROR;
712 /* Check we have a cipher to change to */
713 if (s->s3->tmp.new_cipher == NULL) {
714 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
715 SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC, SSL_R_CCS_RECEIVED_EARLY);
716 return MSG_PROCESS_ERROR;
719 s->s3->change_cipher_spec = 1;
720 if (!ssl3_do_change_cipher_spec(s)) {
721 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC,
722 ERR_R_INTERNAL_ERROR);
723 return MSG_PROCESS_ERROR;
726 if (SSL_IS_DTLS(s)) {
727 dtls1_reset_seq_numbers(s, SSL3_CC_READ);
729 if (s->version == DTLS1_BAD_VER)
730 s->d1->handshake_read_seq++;
732 #ifndef OPENSSL_NO_SCTP
734 * Remember that a CCS has been received, so that an old key of
735 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
738 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD, 1, NULL);
742 return MSG_PROCESS_CONTINUE_READING;
745 MSG_PROCESS_RETURN tls_process_finished(SSL *s, PACKET *pkt)
750 /* This is a real handshake so make sure we clean it up at the end */
753 * To get this far we must have read encrypted data from the client. We
754 * no longer tolerate unencrypted alerts. This value is ignored if less
757 s->statem.enc_read_state = ENC_READ_STATE_VALID;
758 if (s->post_handshake_auth != SSL_PHA_REQUESTED)
759 s->statem.cleanuphand = 1;
760 if (SSL_IS_TLS13(s) && !tls13_save_handshake_digest_for_pha(s)) {
761 /* SSLfatal() already called */
762 return MSG_PROCESS_ERROR;
767 * In TLSv1.3 a Finished message signals a key change so the end of the
768 * message must be on a record boundary.
770 if (SSL_IS_TLS13(s) && RECORD_LAYER_processed_read_pending(&s->rlayer)) {
771 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
772 SSL_R_NOT_ON_RECORD_BOUNDARY);
773 return MSG_PROCESS_ERROR;
776 /* If this occurs, we have missed a message */
777 if (!SSL_IS_TLS13(s) && !s->s3->change_cipher_spec) {
778 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE, SSL_F_TLS_PROCESS_FINISHED,
779 SSL_R_GOT_A_FIN_BEFORE_A_CCS);
780 return MSG_PROCESS_ERROR;
782 s->s3->change_cipher_spec = 0;
784 md_len = s->s3->tmp.peer_finish_md_len;
786 if (md_len != PACKET_remaining(pkt)) {
787 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_TLS_PROCESS_FINISHED,
788 SSL_R_BAD_DIGEST_LENGTH);
789 return MSG_PROCESS_ERROR;
792 if (CRYPTO_memcmp(PACKET_data(pkt), s->s3->tmp.peer_finish_md,
794 SSLfatal(s, SSL_AD_DECRYPT_ERROR, SSL_F_TLS_PROCESS_FINISHED,
795 SSL_R_DIGEST_CHECK_FAILED);
796 return MSG_PROCESS_ERROR;
800 * Copy the finished so we can use it for renegotiation checks
802 if (!ossl_assert(md_len <= EVP_MAX_MD_SIZE)) {
803 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_PROCESS_FINISHED,
804 ERR_R_INTERNAL_ERROR);
805 return MSG_PROCESS_ERROR;
808 memcpy(s->s3->previous_client_finished, s->s3->tmp.peer_finish_md,
810 s->s3->previous_client_finished_len = md_len;
812 memcpy(s->s3->previous_server_finished, s->s3->tmp.peer_finish_md,
814 s->s3->previous_server_finished_len = md_len;
818 * In TLS1.3 we also have to change cipher state and do any final processing
819 * of the initial server flight (if we are a client)
821 if (SSL_IS_TLS13(s)) {
823 if (s->post_handshake_auth != SSL_PHA_REQUESTED &&
824 !s->method->ssl3_enc->change_cipher_state(s,
825 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_SERVER_READ)) {
826 /* SSLfatal() already called */
827 return MSG_PROCESS_ERROR;
830 if (!s->method->ssl3_enc->generate_master_secret(s,
831 s->master_secret, s->handshake_secret, 0,
832 &s->session->master_key_length)) {
833 /* SSLfatal() already called */
834 return MSG_PROCESS_ERROR;
836 if (!s->method->ssl3_enc->change_cipher_state(s,
837 SSL3_CC_APPLICATION | SSL3_CHANGE_CIPHER_CLIENT_READ)) {
838 /* SSLfatal() already called */
839 return MSG_PROCESS_ERROR;
841 if (!tls_process_initial_server_flight(s)) {
842 /* SSLfatal() already called */
843 return MSG_PROCESS_ERROR;
848 return MSG_PROCESS_FINISHED_READING;
851 int tls_construct_change_cipher_spec(SSL *s, WPACKET *pkt)
853 if (!WPACKET_put_bytes_u8(pkt, SSL3_MT_CCS)) {
854 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
855 SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC, ERR_R_INTERNAL_ERROR);
862 /* Add a certificate to the WPACKET */
863 static int ssl_add_cert_to_wpacket(SSL *s, WPACKET *pkt, X509 *x, int chain)
866 unsigned char *outbytes;
868 len = i2d_X509(x, NULL);
870 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
874 if (!WPACKET_sub_allocate_bytes_u24(pkt, len, &outbytes)
875 || i2d_X509(x, &outbytes) != len) {
876 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_TO_WPACKET,
877 ERR_R_INTERNAL_ERROR);
882 && !tls_construct_extensions(s, pkt, SSL_EXT_TLS1_3_CERTIFICATE, x,
884 /* SSLfatal() already called */
891 /* Add certificate chain to provided WPACKET */
892 static int ssl_add_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
896 STACK_OF(X509) *extra_certs;
897 STACK_OF(X509) *chain = NULL;
898 X509_STORE *chain_store;
900 if (cpk == NULL || cpk->x509 == NULL)
906 * If we have a certificate specific chain use it, else use parent ctx.
908 if (cpk->chain != NULL)
909 extra_certs = cpk->chain;
911 extra_certs = s->ctx->extra_certs;
913 if ((s->mode & SSL_MODE_NO_AUTO_CHAIN) || extra_certs)
915 else if (s->cert->chain_store)
916 chain_store = s->cert->chain_store;
918 chain_store = s->ctx->cert_store;
920 if (chain_store != NULL) {
921 X509_STORE_CTX *xs_ctx = X509_STORE_CTX_new();
923 if (xs_ctx == NULL) {
924 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
925 ERR_R_MALLOC_FAILURE);
928 if (!X509_STORE_CTX_init(xs_ctx, chain_store, x, NULL)) {
929 X509_STORE_CTX_free(xs_ctx);
930 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN,
935 * It is valid for the chain not to be complete (because normally we
936 * don't include the root cert in the chain). Therefore we deliberately
937 * ignore the error return from this call. We're not actually verifying
938 * the cert - we're just building as much of the chain as we can
940 (void)X509_verify_cert(xs_ctx);
941 /* Don't leave errors in the queue */
943 chain = X509_STORE_CTX_get0_chain(xs_ctx);
944 i = ssl_security_cert_chain(s, chain, NULL, 0);
947 /* Dummy error calls so mkerr generates them */
948 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_EE_KEY_TOO_SMALL);
949 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_KEY_TOO_SMALL);
950 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN, SSL_R_CA_MD_TOO_WEAK);
952 X509_STORE_CTX_free(xs_ctx);
953 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
956 chain_count = sk_X509_num(chain);
957 for (i = 0; i < chain_count; i++) {
958 x = sk_X509_value(chain, i);
960 if (!ssl_add_cert_to_wpacket(s, pkt, x, i)) {
961 /* SSLfatal() already called */
962 X509_STORE_CTX_free(xs_ctx);
966 X509_STORE_CTX_free(xs_ctx);
968 i = ssl_security_cert_chain(s, extra_certs, x, 0);
970 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_ADD_CERT_CHAIN, i);
973 if (!ssl_add_cert_to_wpacket(s, pkt, x, 0)) {
974 /* SSLfatal() already called */
977 for (i = 0; i < sk_X509_num(extra_certs); i++) {
978 x = sk_X509_value(extra_certs, i);
979 if (!ssl_add_cert_to_wpacket(s, pkt, x, i + 1)) {
980 /* SSLfatal() already called */
988 unsigned long ssl3_output_cert_chain(SSL *s, WPACKET *pkt, CERT_PKEY *cpk)
990 if (!WPACKET_start_sub_packet_u24(pkt)) {
991 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
992 ERR_R_INTERNAL_ERROR);
996 if (!ssl_add_cert_chain(s, pkt, cpk))
999 if (!WPACKET_close(pkt)) {
1000 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL3_OUTPUT_CERT_CHAIN,
1001 ERR_R_INTERNAL_ERROR);
1009 * Tidy up after the end of a handshake. In the case of SCTP this may result
1010 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1013 WORK_STATE tls_finish_handshake(SSL *s, WORK_STATE wst, int clearbufs, int stop)
1015 void (*cb) (const SSL *ssl, int type, int val) = NULL;
1018 if (!SSL_IS_DTLS(s)) {
1020 * We don't do this in DTLS because we may still need the init_buf
1021 * in case there are any unexpected retransmits
1023 BUF_MEM_free(s->init_buf);
1026 if (!ssl_free_wbio_buffer(s)) {
1027 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS_FINISH_HANDSHAKE,
1028 ERR_R_INTERNAL_ERROR);
1034 if (SSL_IS_TLS13(s) && !s->server
1035 && s->post_handshake_auth == SSL_PHA_REQUESTED)
1036 s->post_handshake_auth = SSL_PHA_EXT_SENT;
1039 * Only set if there was a Finished message and this isn't after a TLSv1.3
1040 * post handshake exchange
1042 if (s->statem.cleanuphand) {
1043 /* skipped if we just sent a HelloRequest */
1046 s->statem.cleanuphand = 0;
1047 s->ext.ticket_expected = 0;
1049 ssl3_cleanup_key_block(s);
1053 * In TLSv1.3 we update the cache as part of constructing the
1056 if (!SSL_IS_TLS13(s))
1057 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1059 /* N.B. s->ctx may not equal s->session_ctx */
1060 tsan_counter(&s->ctx->stats.sess_accept_good);
1061 s->handshake_func = ossl_statem_accept;
1063 if (SSL_IS_DTLS(s) && !s->hit) {
1065 * We are finishing after the client. We start the timer going
1066 * in case there are any retransmits of our final flight
1069 dtls1_start_timer(s);
1072 if (SSL_IS_TLS13(s)) {
1074 * We encourage applications to only use TLSv1.3 tickets once,
1075 * so we remove this one from the cache.
1077 if ((s->session_ctx->session_cache_mode
1078 & SSL_SESS_CACHE_CLIENT) != 0)
1079 SSL_CTX_remove_session(s->session_ctx, s->session);
1082 * In TLSv1.3 we update the cache as part of processing the
1085 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1088 tsan_counter(&s->session_ctx->stats.sess_hit);
1090 s->handshake_func = ossl_statem_connect;
1091 tsan_counter(&s->session_ctx->stats.sess_connect_good);
1093 if (SSL_IS_DTLS(s) && s->hit) {
1095 * We are finishing after the server. We start the timer going
1096 * in case there are any retransmits of our final flight
1099 dtls1_start_timer(s);
1103 if (SSL_IS_DTLS(s)) {
1104 /* done with handshaking */
1105 s->d1->handshake_read_seq = 0;
1106 s->d1->handshake_write_seq = 0;
1107 s->d1->next_handshake_write_seq = 0;
1108 dtls1_clear_received_buffer(s);
1112 if (s->info_callback != NULL)
1113 cb = s->info_callback;
1114 else if (s->ctx->info_callback != NULL)
1115 cb = s->ctx->info_callback;
1117 /* The callback may expect us to not be in init at handshake done */
1118 ossl_statem_set_in_init(s, 0);
1121 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1124 /* If we've got more work to do we go back into init */
1125 ossl_statem_set_in_init(s, 1);
1126 return WORK_FINISHED_CONTINUE;
1129 return WORK_FINISHED_STOP;
1132 int tls_get_message_header(SSL *s, int *mt)
1134 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1135 int skip_message, i, recvd_type;
1137 size_t l, readbytes;
1139 p = (unsigned char *)s->init_buf->data;
1142 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1143 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1145 SSL3_HM_HEADER_LENGTH - s->init_num,
1148 s->rwstate = SSL_READING;
1151 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1153 * A ChangeCipherSpec must be a single byte and may not occur
1154 * in the middle of a handshake message.
1156 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1157 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1158 SSL_F_TLS_GET_MESSAGE_HEADER,
1159 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1162 if (s->statem.hand_state == TLS_ST_BEFORE
1163 && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) {
1165 * We are stateless and we received a CCS. Probably this is
1166 * from a client between the first and second ClientHellos.
1167 * We should ignore this, but return an error because we do
1168 * not return success until we see the second ClientHello
1169 * with a valid cookie.
1173 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1174 s->init_num = readbytes - 1;
1175 s->init_msg = s->init_buf->data;
1176 s->s3->tmp.message_size = readbytes;
1178 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1179 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1180 SSL_F_TLS_GET_MESSAGE_HEADER,
1181 SSL_R_CCS_RECEIVED_EARLY);
1184 s->init_num += readbytes;
1189 if (s->statem.hand_state != TLS_ST_OK
1190 && p[0] == SSL3_MT_HELLO_REQUEST)
1192 * The server may always send 'Hello Request' messages --
1193 * we are doing a handshake anyway now, so ignore them if
1194 * their format is correct. Does not count for 'Finished'
1197 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1201 if (s->msg_callback)
1202 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1203 p, SSL3_HM_HEADER_LENGTH, s,
1204 s->msg_callback_arg);
1206 } while (skip_message);
1207 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1210 s->s3->tmp.message_type = *(p++);
1212 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1214 * Only happens with SSLv3+ in an SSLv2 backward compatible
1217 * Total message size is the remaining record bytes to read
1218 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1220 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1221 + SSL3_HM_HEADER_LENGTH;
1222 s->s3->tmp.message_size = l;
1224 s->init_msg = s->init_buf->data;
1225 s->init_num = SSL3_HM_HEADER_LENGTH;
1228 /* BUF_MEM_grow takes an 'int' parameter */
1229 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1230 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER,
1231 SSL_R_EXCESSIVE_MESSAGE_SIZE);
1234 s->s3->tmp.message_size = l;
1236 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1243 int tls_get_message_body(SSL *s, size_t *len)
1245 size_t n, readbytes;
1249 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1250 /* We've already read everything in */
1251 *len = (unsigned long)s->init_num;
1256 n = s->s3->tmp.message_size - s->init_num;
1258 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1259 &p[s->init_num], n, 0, &readbytes);
1261 s->rwstate = SSL_READING;
1265 s->init_num += readbytes;
1270 * If receiving Finished, record MAC of prior handshake messages for
1271 * Finished verification.
1273 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1274 /* SSLfatal() already called */
1279 /* Feed this message into MAC computation. */
1280 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1281 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1283 /* SSLfatal() already called */
1287 if (s->msg_callback)
1288 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1289 (size_t)s->init_num, s, s->msg_callback_arg);
1292 * We defer feeding in the HRR until later. We'll do it as part of
1293 * processing the message
1294 * The TLsv1.3 handshake transcript stops at the ClientFinished
1297 #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1298 /* KeyUpdate and NewSessionTicket do not need to be added */
1299 if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1300 && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1301 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
1302 || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1303 || memcmp(hrrrandom,
1304 s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1305 SSL3_RANDOM_SIZE) != 0) {
1306 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1307 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1308 /* SSLfatal() already called */
1314 if (s->msg_callback)
1315 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1316 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1317 s->msg_callback_arg);
1324 static const X509ERR2ALERT x509table[] = {
1325 {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
1326 {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1327 {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
1328 {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
1329 {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1330 {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1331 {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
1332 {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
1333 {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1334 {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
1335 {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1336 {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1337 {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1338 {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
1339 {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
1340 {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1341 {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1342 {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
1343 {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
1344 {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1345 {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1346 {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1347 {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
1348 {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
1349 {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
1350 {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1351 {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
1352 {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
1353 {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
1354 {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
1355 {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
1356 {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1357 {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1358 {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
1359 {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
1360 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
1361 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
1362 {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
1363 {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
1365 /* Last entry; return this if we don't find the value above. */
1366 {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
1369 int ssl_x509err2alert(int x509err)
1371 const X509ERR2ALERT *tp;
1373 for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
1374 if (tp->x509err == x509err)
1379 int ssl_allow_compression(SSL *s)
1381 if (s->options & SSL_OP_NO_COMPRESSION)
1383 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1386 static int version_cmp(const SSL *s, int a, int b)
1388 int dtls = SSL_IS_DTLS(s);
1393 return a < b ? -1 : 1;
1394 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1399 const SSL_METHOD *(*cmeth) (void);
1400 const SSL_METHOD *(*smeth) (void);
1403 #if TLS_MAX_VERSION != TLS1_3_VERSION
1404 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1407 /* Must be in order high to low */
1408 static const version_info tls_version_table[] = {
1409 #ifndef OPENSSL_NO_TLS1_3
1410 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1412 {TLS1_3_VERSION, NULL, NULL},
1414 #ifndef OPENSSL_NO_TLS1_2
1415 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1417 {TLS1_2_VERSION, NULL, NULL},
1419 #ifndef OPENSSL_NO_TLS1_1
1420 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1422 {TLS1_1_VERSION, NULL, NULL},
1424 #ifndef OPENSSL_NO_TLS1
1425 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1427 {TLS1_VERSION, NULL, NULL},
1429 #ifndef OPENSSL_NO_SSL3
1430 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1432 {SSL3_VERSION, NULL, NULL},
1437 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1438 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1441 /* Must be in order high to low */
1442 static const version_info dtls_version_table[] = {
1443 #ifndef OPENSSL_NO_DTLS1_2
1444 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1446 {DTLS1_2_VERSION, NULL, NULL},
1448 #ifndef OPENSSL_NO_DTLS1
1449 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1450 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1452 {DTLS1_VERSION, NULL, NULL},
1453 {DTLS1_BAD_VER, NULL, NULL},
1459 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1461 * @s: The SSL handle for the candidate method
1462 * @method: the intended method.
1464 * Returns 0 on success, or an SSL error reason on failure.
1466 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1468 int version = method->version;
1470 if ((s->min_proto_version != 0 &&
1471 version_cmp(s, version, s->min_proto_version) < 0) ||
1472 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1473 return SSL_R_VERSION_TOO_LOW;
1475 if (s->max_proto_version != 0 &&
1476 version_cmp(s, version, s->max_proto_version) > 0)
1477 return SSL_R_VERSION_TOO_HIGH;
1479 if ((s->options & method->mask) != 0)
1480 return SSL_R_UNSUPPORTED_PROTOCOL;
1481 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1482 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1488 * Only called by servers. Returns 1 if the server has a TLSv1.3 capable
1489 * certificate type, or has PSK configured. Otherwise returns 0.
1491 static int is_tls13_capable(const SSL *s)
1495 #ifndef OPENSSL_NO_PSK
1496 if (s->psk_server_callback != NULL)
1500 if (s->psk_find_session_cb != NULL)
1503 for (i = 0; i < SSL_PKEY_NUM; i++) {
1504 /* Skip over certs disallowed for TLSv1.3 */
1506 case SSL_PKEY_DSA_SIGN:
1507 case SSL_PKEY_GOST01:
1508 case SSL_PKEY_GOST12_256:
1509 case SSL_PKEY_GOST12_512:
1514 if (ssl_has_cert(s, i))
1522 * ssl_version_supported - Check that the specified `version` is supported by
1525 * @s: The SSL handle for the candidate method
1526 * @version: Protocol version to test against
1528 * Returns 1 when supported, otherwise 0
1530 int ssl_version_supported(const SSL *s, int version, const SSL_METHOD **meth)
1532 const version_info *vent;
1533 const version_info *table;
1535 switch (s->method->version) {
1537 /* Version should match method version for non-ANY method */
1538 return version_cmp(s, version, s->version) == 0;
1539 case TLS_ANY_VERSION:
1540 table = tls_version_table;
1542 case DTLS_ANY_VERSION:
1543 table = dtls_version_table;
1548 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1550 if (vent->cmeth != NULL
1551 && version_cmp(s, version, vent->version) == 0
1552 && ssl_method_error(s, vent->cmeth()) == 0
1554 || version != TLS1_3_VERSION
1555 || is_tls13_capable(s))) {
1557 *meth = vent->cmeth();
1565 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1566 * fallback indication from a client check whether we're using the highest
1567 * supported protocol version.
1569 * @s server SSL handle.
1571 * Returns 1 when using the highest enabled version, 0 otherwise.
1573 int ssl_check_version_downgrade(SSL *s)
1575 const version_info *vent;
1576 const version_info *table;
1579 * Check that the current protocol is the highest enabled version
1580 * (according to s->ctx->method, as version negotiation may have changed
1583 if (s->version == s->ctx->method->version)
1587 * Apparently we're using a version-flexible SSL_METHOD (not at its
1588 * highest protocol version).
1590 if (s->ctx->method->version == TLS_method()->version)
1591 table = tls_version_table;
1592 else if (s->ctx->method->version == DTLS_method()->version)
1593 table = dtls_version_table;
1595 /* Unexpected state; fail closed. */
1599 for (vent = table; vent->version != 0; ++vent) {
1600 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1601 return s->version == vent->version;
1607 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1608 * protocols, provided the initial (D)TLS method is version-flexible. This
1609 * function sanity-checks the proposed value and makes sure the method is
1610 * version-flexible, then sets the limit if all is well.
1612 * @method_version: The version of the current SSL_METHOD.
1613 * @version: the intended limit.
1614 * @bound: pointer to limit to be updated.
1616 * Returns 1 on success, 0 on failure.
1618 int ssl_set_version_bound(int method_version, int version, int *bound)
1626 * Restrict TLS methods to TLS protocol versions.
1627 * Restrict DTLS methods to DTLS protocol versions.
1628 * Note, DTLS version numbers are decreasing, use comparison macros.
1630 * Note that for both lower-bounds we use explicit versions, not
1631 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1632 * configurations. If the MIN (supported) version ever rises, the user's
1633 * "floor" remains valid even if no longer available. We don't expect the
1634 * MAX ceiling to ever get lower, so making that variable makes sense.
1636 switch (method_version) {
1639 * XXX For fixed version methods, should we always fail and not set any
1640 * bounds, always succeed and not set any bounds, or set the bounds and
1641 * arrange to fail later if they are not met? At present fixed-version
1642 * methods are not subject to controls that disable individual protocol
1647 case TLS_ANY_VERSION:
1648 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1652 case DTLS_ANY_VERSION:
1653 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1654 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1663 static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1665 if (vers == TLS1_2_VERSION
1666 && ssl_version_supported(s, TLS1_3_VERSION, NULL)) {
1667 *dgrd = DOWNGRADE_TO_1_2;
1668 } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
1669 && (ssl_version_supported(s, TLS1_2_VERSION, NULL)
1670 || ssl_version_supported(s, TLS1_3_VERSION, NULL))) {
1671 *dgrd = DOWNGRADE_TO_1_1;
1673 *dgrd = DOWNGRADE_NONE;
1678 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1679 * client HELLO is received to select the final server protocol version and
1680 * the version specific method.
1682 * @s: server SSL handle.
1684 * Returns 0 on success or an SSL error reason number on failure.
1686 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1689 * With version-flexible methods we have an initial state with:
1691 * s->method->version == (D)TLS_ANY_VERSION,
1692 * s->version == (D)TLS_MAX_VERSION.
1694 * So we detect version-flexible methods via the method version, not the
1697 int server_version = s->method->version;
1698 int client_version = hello->legacy_version;
1699 const version_info *vent;
1700 const version_info *table;
1702 RAW_EXTENSION *suppversions;
1704 s->client_version = client_version;
1706 switch (server_version) {
1708 if (!SSL_IS_TLS13(s)) {
1709 if (version_cmp(s, client_version, s->version) < 0)
1710 return SSL_R_WRONG_SSL_VERSION;
1711 *dgrd = DOWNGRADE_NONE;
1713 * If this SSL handle is not from a version flexible method we don't
1714 * (and never did) check min/max FIPS or Suite B constraints. Hope
1715 * that's OK. It is up to the caller to not choose fixed protocol
1716 * versions they don't want. If not, then easy to fix, just return
1717 * ssl_method_error(s, s->method)
1722 * Fall through if we are TLSv1.3 already (this means we must be after
1723 * a HelloRetryRequest
1726 case TLS_ANY_VERSION:
1727 table = tls_version_table;
1729 case DTLS_ANY_VERSION:
1730 table = dtls_version_table;
1734 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1736 /* If we did an HRR then supported versions is mandatory */
1737 if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
1738 return SSL_R_UNSUPPORTED_PROTOCOL;
1740 if (suppversions->present && !SSL_IS_DTLS(s)) {
1741 unsigned int candidate_vers = 0;
1742 unsigned int best_vers = 0;
1743 const SSL_METHOD *best_method = NULL;
1744 PACKET versionslist;
1746 suppversions->parsed = 1;
1748 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1749 /* Trailing or invalid data? */
1750 return SSL_R_LENGTH_MISMATCH;
1754 * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION.
1755 * The spec only requires servers to check that it isn't SSLv3:
1756 * "Any endpoint receiving a Hello message with
1757 * ClientHello.legacy_version or ServerHello.legacy_version set to
1758 * 0x0300 MUST abort the handshake with a "protocol_version" alert."
1759 * We are slightly stricter and require that it isn't SSLv3 or lower.
1760 * We tolerate TLSv1 and TLSv1.1.
1762 if (client_version <= SSL3_VERSION)
1763 return SSL_R_BAD_LEGACY_VERSION;
1765 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1766 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1768 if (ssl_version_supported(s, candidate_vers, &best_method))
1769 best_vers = candidate_vers;
1771 if (PACKET_remaining(&versionslist) != 0) {
1772 /* Trailing data? */
1773 return SSL_R_LENGTH_MISMATCH;
1776 if (best_vers > 0) {
1777 if (s->hello_retry_request != SSL_HRR_NONE) {
1779 * This is after a HelloRetryRequest so we better check that we
1780 * negotiated TLSv1.3
1782 if (best_vers != TLS1_3_VERSION)
1783 return SSL_R_UNSUPPORTED_PROTOCOL;
1786 check_for_downgrade(s, best_vers, dgrd);
1787 s->version = best_vers;
1788 s->method = best_method;
1791 return SSL_R_UNSUPPORTED_PROTOCOL;
1795 * If the supported versions extension isn't present, then the highest
1796 * version we can negotiate is TLSv1.2
1798 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1799 client_version = TLS1_2_VERSION;
1802 * No supported versions extension, so we just use the version supplied in
1805 for (vent = table; vent->version != 0; ++vent) {
1806 const SSL_METHOD *method;
1808 if (vent->smeth == NULL ||
1809 version_cmp(s, client_version, vent->version) < 0)
1811 method = vent->smeth();
1812 if (ssl_method_error(s, method) == 0) {
1813 check_for_downgrade(s, vent->version, dgrd);
1814 s->version = vent->version;
1820 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1824 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1825 * server HELLO is received to select the final client protocol version and
1826 * the version specific method.
1828 * @s: client SSL handle.
1829 * @version: The proposed version from the server's HELLO.
1830 * @extensions: The extensions received
1832 * Returns 1 on success or 0 on error.
1834 int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
1836 const version_info *vent;
1837 const version_info *table;
1842 s->version = version;
1844 /* This will overwrite s->version if the extension is present */
1845 if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
1846 SSL_EXT_TLS1_2_SERVER_HELLO
1847 | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
1853 if (s->hello_retry_request != SSL_HRR_NONE
1854 && s->version != TLS1_3_VERSION) {
1856 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1857 SSL_R_WRONG_SSL_VERSION);
1861 switch (s->method->version) {
1863 if (s->version != s->method->version) {
1865 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1866 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1867 SSL_R_WRONG_SSL_VERSION);
1871 * If this SSL handle is not from a version flexible method we don't
1872 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1873 * that's OK. It is up to the caller to not choose fixed protocol
1874 * versions they don't want. If not, then easy to fix, just return
1875 * ssl_method_error(s, s->method)
1878 case TLS_ANY_VERSION:
1879 table = tls_version_table;
1881 case DTLS_ANY_VERSION:
1882 table = dtls_version_table;
1886 for (vent = table; vent->version != 0; ++vent) {
1887 const SSL_METHOD *method;
1890 if (vent->cmeth == NULL)
1893 if (highver != 0 && s->version != vent->version)
1896 if (highver == 0 && (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) != 0)
1897 highver = vent->version;
1899 method = vent->cmeth();
1900 err = ssl_method_error(s, method);
1902 if (s->version == vent->version) {
1904 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1905 SSL_F_SSL_CHOOSE_CLIENT_VERSION, err);
1912 highver = vent->version;
1914 if (s->version != vent->version)
1917 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1918 /* Check for downgrades */
1919 if (s->version == TLS1_2_VERSION && highver > s->version) {
1920 if (memcmp(tls12downgrade,
1921 s->s3->server_random + SSL3_RANDOM_SIZE
1922 - sizeof(tls12downgrade),
1923 sizeof(tls12downgrade)) == 0) {
1925 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1926 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1927 SSL_R_INAPPROPRIATE_FALLBACK);
1930 } else if (!SSL_IS_DTLS(s)
1931 && s->version < TLS1_2_VERSION
1932 && highver > s->version) {
1933 if (memcmp(tls11downgrade,
1934 s->s3->server_random + SSL3_RANDOM_SIZE
1935 - sizeof(tls11downgrade),
1936 sizeof(tls11downgrade)) == 0) {
1938 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1939 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1940 SSL_R_INAPPROPRIATE_FALLBACK);
1951 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1952 SSL_R_UNSUPPORTED_PROTOCOL);
1957 * ssl_get_min_max_version - get minimum and maximum protocol version
1958 * @s: The SSL connection
1959 * @min_version: The minimum supported version
1960 * @max_version: The maximum supported version
1962 * Work out what version we should be using for the initial ClientHello if the
1963 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1964 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1965 * constraints and any floor imposed by the security level here,
1966 * so we don't advertise the wrong protocol version to only reject the outcome later.
1968 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1969 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1970 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1972 * Returns 0 on success or an SSL error reason number on failure. On failure
1973 * min_version and max_version will also be set to 0.
1975 int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
1979 const SSL_METHOD *single = NULL;
1980 const SSL_METHOD *method;
1981 const version_info *table;
1982 const version_info *vent;
1984 switch (s->method->version) {
1987 * If this SSL handle is not from a version flexible method we don't
1988 * (and never did) check min/max FIPS or Suite B constraints. Hope
1989 * that's OK. It is up to the caller to not choose fixed protocol
1990 * versions they don't want. If not, then easy to fix, just return
1991 * ssl_method_error(s, s->method)
1993 *min_version = *max_version = s->version;
1995 case TLS_ANY_VERSION:
1996 table = tls_version_table;
1998 case DTLS_ANY_VERSION:
1999 table = dtls_version_table;
2004 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
2005 * below X enabled. This is required in order to maintain the "version
2006 * capability" vector contiguous. Any versions with a NULL client method
2007 * (protocol version client is disabled at compile-time) is also a "hole".
2009 * Our initial state is hole == 1, version == 0. That is, versions above
2010 * the first version in the method table are disabled (a "hole" above
2011 * the valid protocol entries) and we don't have a selected version yet.
2013 * Whenever "hole == 1", and we hit an enabled method, its version becomes
2014 * the selected version, and the method becomes a candidate "single"
2015 * method. We're no longer in a hole, so "hole" becomes 0.
2017 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
2018 * as we support a contiguous range of at least two methods. If we hit
2019 * a disabled method, then hole becomes true again, but nothing else
2020 * changes yet, because all the remaining methods may be disabled too.
2021 * If we again hit an enabled method after the new hole, it becomes
2022 * selected, as we start from scratch.
2024 *min_version = version = 0;
2026 for (vent = table; vent->version != 0; ++vent) {
2028 * A table entry with a NULL client method is still a hole in the
2029 * "version capability" vector.
2031 if (vent->cmeth == NULL) {
2035 method = vent->cmeth();
2036 if (ssl_method_error(s, method) != 0) {
2040 *min_version = method->version;
2042 version = (single = method)->version;
2043 *min_version = version;
2048 *max_version = version;
2050 /* Fail if everything is disabled */
2052 return SSL_R_NO_PROTOCOLS_AVAILABLE;
2058 * ssl_set_client_hello_version - Work out what version we should be using for
2059 * the initial ClientHello.legacy_version field.
2061 * @s: client SSL handle.
2063 * Returns 0 on success or an SSL error reason number on failure.
2065 int ssl_set_client_hello_version(SSL *s)
2067 int ver_min, ver_max, ret;
2070 * In a renegotiation we always send the same client_version that we sent
2071 * last time, regardless of which version we eventually negotiated.
2073 if (!SSL_IS_FIRST_HANDSHAKE(s))
2076 ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
2081 s->version = ver_max;
2083 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2084 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
2085 ver_max = TLS1_2_VERSION;
2087 s->client_version = ver_max;
2092 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2093 * and |checkallow| is 1 then additionally check if the group is allowed to be
2094 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2095 * 1) or 0 otherwise.
2097 #ifndef OPENSSL_NO_EC
2098 int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
2099 size_t num_groups, int checkallow)
2103 if (groups == NULL || num_groups == 0)
2106 for (i = 0; i < num_groups; i++) {
2107 uint16_t group = groups[i];
2109 if (group_id == group
2111 || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2120 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2121 int create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
2122 size_t hashlen, const unsigned char *hrr,
2125 unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2126 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2128 memset(msghdr, 0, sizeof(msghdr));
2130 if (hashval == NULL) {
2131 hashval = hashvaltmp;
2133 /* Get the hash of the initial ClientHello */
2134 if (!ssl3_digest_cached_records(s, 0)
2135 || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2137 /* SSLfatal() already called */
2142 /* Reinitialise the transcript hash */
2143 if (!ssl3_init_finished_mac(s)) {
2144 /* SSLfatal() already called */
2148 /* Inject the synthetic message_hash message */
2149 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2150 msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2151 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2152 || !ssl3_finish_mac(s, hashval, hashlen)) {
2153 /* SSLfatal() already called */
2158 * Now re-inject the HRR and current message if appropriate (we just deleted
2159 * it when we reinitialised the transcript hash above). Only necessary after
2160 * receiving a ClientHello2 with a cookie.
2163 && (!ssl3_finish_mac(s, hrr, hrrlen)
2164 || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2165 s->s3->tmp.message_size
2166 + SSL3_HM_HEADER_LENGTH))) {
2167 /* SSLfatal() already called */
2174 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2176 return X509_NAME_cmp(*a, *b);
2179 int parse_ca_names(SSL *s, PACKET *pkt)
2181 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2182 X509_NAME *xn = NULL;
2185 if (ca_sk == NULL) {
2186 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2187 ERR_R_MALLOC_FAILURE);
2190 /* get the CA RDNs */
2191 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2192 SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES,
2193 SSL_R_LENGTH_MISMATCH);
2197 while (PACKET_remaining(&cadns)) {
2198 const unsigned char *namestart, *namebytes;
2199 unsigned int name_len;
2201 if (!PACKET_get_net_2(&cadns, &name_len)
2202 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2203 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2204 SSL_R_LENGTH_MISMATCH);
2208 namestart = namebytes;
2209 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2210 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2214 if (namebytes != (namestart + name_len)) {
2215 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2216 SSL_R_CA_DN_LENGTH_MISMATCH);
2220 if (!sk_X509_NAME_push(ca_sk, xn)) {
2221 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2222 ERR_R_MALLOC_FAILURE);
2228 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
2229 s->s3->tmp.peer_ca_names = ca_sk;
2234 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2239 int construct_ca_names(SSL *s, WPACKET *pkt)
2241 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
2243 /* Start sub-packet for client CA list */
2244 if (!WPACKET_start_sub_packet_u16(pkt)) {
2245 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2246 ERR_R_INTERNAL_ERROR);
2250 if (ca_sk != NULL) {
2253 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2254 unsigned char *namebytes;
2255 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2259 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2260 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2262 || i2d_X509_NAME(name, &namebytes) != namelen) {
2263 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2264 ERR_R_INTERNAL_ERROR);
2270 if (!WPACKET_close(pkt)) {
2271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2272 ERR_R_INTERNAL_ERROR);
2279 /* Create a buffer containing data to be signed for server key exchange */
2280 size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
2281 const void *param, size_t paramlen)
2283 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2284 unsigned char *tbs = OPENSSL_malloc(tbslen);
2287 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS,
2288 ERR_R_MALLOC_FAILURE);
2291 memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
2292 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
2294 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2301 * Saves the current handshake digest for Post-Handshake Auth,
2302 * Done after ClientFinished is processed, done exactly once
2304 int tls13_save_handshake_digest_for_pha(SSL *s)
2306 if (s->pha_dgst == NULL) {
2307 if (!ssl3_digest_cached_records(s, 1))
2308 /* SSLfatal() already called */
2311 s->pha_dgst = EVP_MD_CTX_new();
2312 if (s->pha_dgst == NULL) {
2313 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2314 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2315 ERR_R_INTERNAL_ERROR);
2318 if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2319 s->s3->handshake_dgst)) {
2320 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2321 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2322 ERR_R_INTERNAL_ERROR);
2330 * Restores the Post-Handshake Auth handshake digest
2331 * Done just before sending/processing the Cert Request
2333 int tls13_restore_handshake_digest_for_pha(SSL *s)
2335 if (s->pha_dgst == NULL) {
2336 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2337 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2338 ERR_R_INTERNAL_ERROR);
2341 if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst,
2343 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2344 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2345 ERR_R_INTERNAL_ERROR);