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;
1045 s->ext.ticket_expected = 0;
1047 ssl3_cleanup_key_block(s);
1051 * In TLSv1.3 we update the cache as part of constructing the
1054 if (!SSL_IS_TLS13(s))
1055 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
1057 /* N.B. s->ctx may not equal s->session_ctx */
1058 CRYPTO_atomic_add(&s->ctx->stats.sess_accept_good, 1, &discard,
1060 s->handshake_func = ossl_statem_accept;
1062 if (SSL_IS_DTLS(s) && !s->hit) {
1064 * We are finishing after the client. We start the timer going
1065 * in case there are any retransmits of our final flight
1068 dtls1_start_timer(s);
1071 if (SSL_IS_TLS13(s)) {
1073 * We encourage applications to only use TLSv1.3 tickets once,
1074 * so we remove this one from the cache.
1076 if ((s->session_ctx->session_cache_mode
1077 & SSL_SESS_CACHE_CLIENT) != 0)
1078 SSL_CTX_remove_session(s->session_ctx, s->session);
1081 * In TLSv1.3 we update the cache as part of processing the
1084 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
1087 CRYPTO_atomic_add(&s->session_ctx->stats.sess_hit, 1, &discard,
1088 s->session_ctx->lock);
1090 s->handshake_func = ossl_statem_connect;
1091 CRYPTO_atomic_add(&s->session_ctx->stats.sess_connect_good, 1,
1092 &discard, s->session_ctx->lock);
1094 if (SSL_IS_DTLS(s) && s->hit) {
1096 * We are finishing after the server. We start the timer going
1097 * in case there are any retransmits of our final flight
1100 dtls1_start_timer(s);
1104 if (SSL_IS_DTLS(s)) {
1105 /* done with handshaking */
1106 s->d1->handshake_read_seq = 0;
1107 s->d1->handshake_write_seq = 0;
1108 s->d1->next_handshake_write_seq = 0;
1109 dtls1_clear_received_buffer(s);
1113 if (s->info_callback != NULL)
1114 cb = s->info_callback;
1115 else if (s->ctx->info_callback != NULL)
1116 cb = s->ctx->info_callback;
1118 /* The callback may expect us to not be in init at handshake done */
1119 ossl_statem_set_in_init(s, 0);
1122 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
1125 /* If we've got more work to do we go back into init */
1126 ossl_statem_set_in_init(s, 1);
1127 return WORK_FINISHED_CONTINUE;
1130 return WORK_FINISHED_STOP;
1133 int tls_get_message_header(SSL *s, int *mt)
1135 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1136 int skip_message, i, recvd_type;
1138 size_t l, readbytes;
1140 p = (unsigned char *)s->init_buf->data;
1143 while (s->init_num < SSL3_HM_HEADER_LENGTH) {
1144 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, &recvd_type,
1146 SSL3_HM_HEADER_LENGTH - s->init_num,
1149 s->rwstate = SSL_READING;
1152 if (recvd_type == SSL3_RT_CHANGE_CIPHER_SPEC) {
1154 * A ChangeCipherSpec must be a single byte and may not occur
1155 * in the middle of a handshake message.
1157 if (s->init_num != 0 || readbytes != 1 || p[0] != SSL3_MT_CCS) {
1158 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1159 SSL_F_TLS_GET_MESSAGE_HEADER,
1160 SSL_R_BAD_CHANGE_CIPHER_SPEC);
1163 if (s->statem.hand_state == TLS_ST_BEFORE
1164 && (s->s3->flags & TLS1_FLAGS_STATELESS) != 0) {
1166 * We are stateless and we received a CCS. Probably this is
1167 * from a client between the first and second ClientHellos.
1168 * We should ignore this, but return an error because we do
1169 * not return success until we see the second ClientHello
1170 * with a valid cookie.
1174 s->s3->tmp.message_type = *mt = SSL3_MT_CHANGE_CIPHER_SPEC;
1175 s->init_num = readbytes - 1;
1176 s->init_msg = s->init_buf->data;
1177 s->s3->tmp.message_size = readbytes;
1179 } else if (recvd_type != SSL3_RT_HANDSHAKE) {
1180 SSLfatal(s, SSL_AD_UNEXPECTED_MESSAGE,
1181 SSL_F_TLS_GET_MESSAGE_HEADER,
1182 SSL_R_CCS_RECEIVED_EARLY);
1185 s->init_num += readbytes;
1190 if (s->statem.hand_state != TLS_ST_OK
1191 && p[0] == SSL3_MT_HELLO_REQUEST)
1193 * The server may always send 'Hello Request' messages --
1194 * we are doing a handshake anyway now, so ignore them if
1195 * their format is correct. Does not count for 'Finished'
1198 if (p[1] == 0 && p[2] == 0 && p[3] == 0) {
1202 if (s->msg_callback)
1203 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE,
1204 p, SSL3_HM_HEADER_LENGTH, s,
1205 s->msg_callback_arg);
1207 } while (skip_message);
1208 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1211 s->s3->tmp.message_type = *(p++);
1213 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1215 * Only happens with SSLv3+ in an SSLv2 backward compatible
1218 * Total message size is the remaining record bytes to read
1219 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1221 l = RECORD_LAYER_get_rrec_length(&s->rlayer)
1222 + SSL3_HM_HEADER_LENGTH;
1223 s->s3->tmp.message_size = l;
1225 s->init_msg = s->init_buf->data;
1226 s->init_num = SSL3_HM_HEADER_LENGTH;
1229 /* BUF_MEM_grow takes an 'int' parameter */
1230 if (l > (INT_MAX - SSL3_HM_HEADER_LENGTH)) {
1231 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_TLS_GET_MESSAGE_HEADER,
1232 SSL_R_EXCESSIVE_MESSAGE_SIZE);
1235 s->s3->tmp.message_size = l;
1237 s->init_msg = s->init_buf->data + SSL3_HM_HEADER_LENGTH;
1244 int tls_get_message_body(SSL *s, size_t *len)
1246 size_t n, readbytes;
1250 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC) {
1251 /* We've already read everything in */
1252 *len = (unsigned long)s->init_num;
1257 n = s->s3->tmp.message_size - s->init_num;
1259 i = s->method->ssl_read_bytes(s, SSL3_RT_HANDSHAKE, NULL,
1260 &p[s->init_num], n, 0, &readbytes);
1262 s->rwstate = SSL_READING;
1266 s->init_num += readbytes;
1271 * If receiving Finished, record MAC of prior handshake messages for
1272 * Finished verification.
1274 if (*(s->init_buf->data) == SSL3_MT_FINISHED && !ssl3_take_mac(s)) {
1275 /* SSLfatal() already called */
1280 /* Feed this message into MAC computation. */
1281 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1282 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1284 /* SSLfatal() already called */
1288 if (s->msg_callback)
1289 s->msg_callback(0, SSL2_VERSION, 0, s->init_buf->data,
1290 (size_t)s->init_num, s, s->msg_callback_arg);
1293 * We defer feeding in the HRR until later. We'll do it as part of
1294 * processing the message
1295 * The TLsv1.3 handshake transcript stops at the ClientFinished
1298 #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1299 /* KeyUpdate and NewSessionTicket do not need to be added */
1300 if (!SSL_IS_TLS13(s) || (s->s3->tmp.message_type != SSL3_MT_NEWSESSION_TICKET
1301 && s->s3->tmp.message_type != SSL3_MT_KEY_UPDATE)) {
1302 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO
1303 || s->init_num < SERVER_HELLO_RANDOM_OFFSET + SSL3_RANDOM_SIZE
1304 || memcmp(hrrrandom,
1305 s->init_buf->data + SERVER_HELLO_RANDOM_OFFSET,
1306 SSL3_RANDOM_SIZE) != 0) {
1307 if (!ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
1308 s->init_num + SSL3_HM_HEADER_LENGTH)) {
1309 /* SSLfatal() already called */
1315 if (s->msg_callback)
1316 s->msg_callback(0, s->version, SSL3_RT_HANDSHAKE, s->init_buf->data,
1317 (size_t)s->init_num + SSL3_HM_HEADER_LENGTH, s,
1318 s->msg_callback_arg);
1325 static const X509ERR2ALERT x509table[] = {
1326 {X509_V_ERR_APPLICATION_VERIFICATION, SSL_AD_HANDSHAKE_FAILURE},
1327 {X509_V_ERR_CA_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1328 {X509_V_ERR_CA_MD_TOO_WEAK, SSL_AD_BAD_CERTIFICATE},
1329 {X509_V_ERR_CERT_CHAIN_TOO_LONG, SSL_AD_UNKNOWN_CA},
1330 {X509_V_ERR_CERT_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1331 {X509_V_ERR_CERT_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1332 {X509_V_ERR_CERT_REJECTED, SSL_AD_BAD_CERTIFICATE},
1333 {X509_V_ERR_CERT_REVOKED, SSL_AD_CERTIFICATE_REVOKED},
1334 {X509_V_ERR_CERT_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1335 {X509_V_ERR_CERT_UNTRUSTED, SSL_AD_BAD_CERTIFICATE},
1336 {X509_V_ERR_CRL_HAS_EXPIRED, SSL_AD_CERTIFICATE_EXPIRED},
1337 {X509_V_ERR_CRL_NOT_YET_VALID, SSL_AD_BAD_CERTIFICATE},
1338 {X509_V_ERR_CRL_SIGNATURE_FAILURE, SSL_AD_DECRYPT_ERROR},
1339 {X509_V_ERR_DANE_NO_MATCH, SSL_AD_BAD_CERTIFICATE},
1340 {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT, SSL_AD_UNKNOWN_CA},
1341 {X509_V_ERR_EE_KEY_TOO_SMALL, SSL_AD_BAD_CERTIFICATE},
1342 {X509_V_ERR_EMAIL_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1343 {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD, SSL_AD_BAD_CERTIFICATE},
1344 {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD, SSL_AD_BAD_CERTIFICATE},
1345 {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1346 {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD, SSL_AD_BAD_CERTIFICATE},
1347 {X509_V_ERR_HOSTNAME_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1348 {X509_V_ERR_INVALID_CA, SSL_AD_UNKNOWN_CA},
1349 {X509_V_ERR_INVALID_CALL, SSL_AD_INTERNAL_ERROR},
1350 {X509_V_ERR_INVALID_PURPOSE, SSL_AD_UNSUPPORTED_CERTIFICATE},
1351 {X509_V_ERR_IP_ADDRESS_MISMATCH, SSL_AD_BAD_CERTIFICATE},
1352 {X509_V_ERR_OUT_OF_MEM, SSL_AD_INTERNAL_ERROR},
1353 {X509_V_ERR_PATH_LENGTH_EXCEEDED, SSL_AD_UNKNOWN_CA},
1354 {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN, SSL_AD_UNKNOWN_CA},
1355 {X509_V_ERR_STORE_LOOKUP, SSL_AD_INTERNAL_ERROR},
1356 {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, SSL_AD_BAD_CERTIFICATE},
1357 {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1358 {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE, SSL_AD_BAD_CERTIFICATE},
1359 {X509_V_ERR_UNABLE_TO_GET_CRL, SSL_AD_UNKNOWN_CA},
1360 {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER, SSL_AD_UNKNOWN_CA},
1361 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT, SSL_AD_UNKNOWN_CA},
1362 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, SSL_AD_UNKNOWN_CA},
1363 {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE, SSL_AD_UNKNOWN_CA},
1364 {X509_V_ERR_UNSPECIFIED, SSL_AD_INTERNAL_ERROR},
1366 /* Last entry; return this if we don't find the value above. */
1367 {X509_V_OK, SSL_AD_CERTIFICATE_UNKNOWN}
1370 int ssl_x509err2alert(int x509err)
1372 const X509ERR2ALERT *tp;
1374 for (tp = x509table; tp->x509err != X509_V_OK; ++tp)
1375 if (tp->x509err == x509err)
1380 int ssl_allow_compression(SSL *s)
1382 if (s->options & SSL_OP_NO_COMPRESSION)
1384 return ssl_security(s, SSL_SECOP_COMPRESSION, 0, 0, NULL);
1387 static int version_cmp(const SSL *s, int a, int b)
1389 int dtls = SSL_IS_DTLS(s);
1394 return a < b ? -1 : 1;
1395 return DTLS_VERSION_LT(a, b) ? -1 : 1;
1400 const SSL_METHOD *(*cmeth) (void);
1401 const SSL_METHOD *(*smeth) (void);
1404 #if TLS_MAX_VERSION != TLS1_3_VERSION
1405 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1408 /* Must be in order high to low */
1409 static const version_info tls_version_table[] = {
1410 #ifndef OPENSSL_NO_TLS1_3
1411 {TLS1_3_VERSION, tlsv1_3_client_method, tlsv1_3_server_method},
1413 {TLS1_3_VERSION, NULL, NULL},
1415 #ifndef OPENSSL_NO_TLS1_2
1416 {TLS1_2_VERSION, tlsv1_2_client_method, tlsv1_2_server_method},
1418 {TLS1_2_VERSION, NULL, NULL},
1420 #ifndef OPENSSL_NO_TLS1_1
1421 {TLS1_1_VERSION, tlsv1_1_client_method, tlsv1_1_server_method},
1423 {TLS1_1_VERSION, NULL, NULL},
1425 #ifndef OPENSSL_NO_TLS1
1426 {TLS1_VERSION, tlsv1_client_method, tlsv1_server_method},
1428 {TLS1_VERSION, NULL, NULL},
1430 #ifndef OPENSSL_NO_SSL3
1431 {SSL3_VERSION, sslv3_client_method, sslv3_server_method},
1433 {SSL3_VERSION, NULL, NULL},
1438 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1439 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1442 /* Must be in order high to low */
1443 static const version_info dtls_version_table[] = {
1444 #ifndef OPENSSL_NO_DTLS1_2
1445 {DTLS1_2_VERSION, dtlsv1_2_client_method, dtlsv1_2_server_method},
1447 {DTLS1_2_VERSION, NULL, NULL},
1449 #ifndef OPENSSL_NO_DTLS1
1450 {DTLS1_VERSION, dtlsv1_client_method, dtlsv1_server_method},
1451 {DTLS1_BAD_VER, dtls_bad_ver_client_method, NULL},
1453 {DTLS1_VERSION, NULL, NULL},
1454 {DTLS1_BAD_VER, NULL, NULL},
1460 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1462 * @s: The SSL handle for the candidate method
1463 * @method: the intended method.
1465 * Returns 0 on success, or an SSL error reason on failure.
1467 static int ssl_method_error(const SSL *s, const SSL_METHOD *method)
1469 int version = method->version;
1471 if ((s->min_proto_version != 0 &&
1472 version_cmp(s, version, s->min_proto_version) < 0) ||
1473 ssl_security(s, SSL_SECOP_VERSION, 0, version, NULL) == 0)
1474 return SSL_R_VERSION_TOO_LOW;
1476 if (s->max_proto_version != 0 &&
1477 version_cmp(s, version, s->max_proto_version) > 0)
1478 return SSL_R_VERSION_TOO_HIGH;
1480 if ((s->options & method->mask) != 0)
1481 return SSL_R_UNSUPPORTED_PROTOCOL;
1482 if ((method->flags & SSL_METHOD_NO_SUITEB) != 0 && tls1_suiteb(s))
1483 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE;
1489 * ssl_version_supported - Check that the specified `version` is supported by
1492 * @s: The SSL handle for the candidate method
1493 * @version: Protocol version to test against
1495 * Returns 1 when supported, otherwise 0
1497 int ssl_version_supported(const SSL *s, int version)
1499 const version_info *vent;
1500 const version_info *table;
1502 switch (s->method->version) {
1504 /* Version should match method version for non-ANY method */
1505 return version_cmp(s, version, s->version) == 0;
1506 case TLS_ANY_VERSION:
1507 table = tls_version_table;
1509 case DTLS_ANY_VERSION:
1510 table = dtls_version_table;
1515 vent->version != 0 && version_cmp(s, version, vent->version) <= 0;
1517 if (vent->cmeth != NULL &&
1518 version_cmp(s, version, vent->version) == 0 &&
1519 ssl_method_error(s, vent->cmeth()) == 0) {
1527 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1528 * fallback indication from a client check whether we're using the highest
1529 * supported protocol version.
1531 * @s server SSL handle.
1533 * Returns 1 when using the highest enabled version, 0 otherwise.
1535 int ssl_check_version_downgrade(SSL *s)
1537 const version_info *vent;
1538 const version_info *table;
1541 * Check that the current protocol is the highest enabled version
1542 * (according to s->ctx->method, as version negotiation may have changed
1545 if (s->version == s->ctx->method->version)
1549 * Apparently we're using a version-flexible SSL_METHOD (not at its
1550 * highest protocol version).
1552 if (s->ctx->method->version == TLS_method()->version)
1553 table = tls_version_table;
1554 else if (s->ctx->method->version == DTLS_method()->version)
1555 table = dtls_version_table;
1557 /* Unexpected state; fail closed. */
1561 for (vent = table; vent->version != 0; ++vent) {
1562 if (vent->smeth != NULL && ssl_method_error(s, vent->smeth()) == 0)
1563 return s->version == vent->version;
1569 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1570 * protocols, provided the initial (D)TLS method is version-flexible. This
1571 * function sanity-checks the proposed value and makes sure the method is
1572 * version-flexible, then sets the limit if all is well.
1574 * @method_version: The version of the current SSL_METHOD.
1575 * @version: the intended limit.
1576 * @bound: pointer to limit to be updated.
1578 * Returns 1 on success, 0 on failure.
1580 int ssl_set_version_bound(int method_version, int version, int *bound)
1588 * Restrict TLS methods to TLS protocol versions.
1589 * Restrict DTLS methods to DTLS protocol versions.
1590 * Note, DTLS version numbers are decreasing, use comparison macros.
1592 * Note that for both lower-bounds we use explicit versions, not
1593 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1594 * configurations. If the MIN (supported) version ever rises, the user's
1595 * "floor" remains valid even if no longer available. We don't expect the
1596 * MAX ceiling to ever get lower, so making that variable makes sense.
1598 switch (method_version) {
1601 * XXX For fixed version methods, should we always fail and not set any
1602 * bounds, always succeed and not set any bounds, or set the bounds and
1603 * arrange to fail later if they are not met? At present fixed-version
1604 * methods are not subject to controls that disable individual protocol
1609 case TLS_ANY_VERSION:
1610 if (version < SSL3_VERSION || version > TLS_MAX_VERSION)
1614 case DTLS_ANY_VERSION:
1615 if (DTLS_VERSION_GT(version, DTLS_MAX_VERSION) ||
1616 DTLS_VERSION_LT(version, DTLS1_BAD_VER))
1625 static void check_for_downgrade(SSL *s, int vers, DOWNGRADE *dgrd)
1627 if (vers == TLS1_2_VERSION
1628 && ssl_version_supported(s, TLS1_3_VERSION)) {
1629 *dgrd = DOWNGRADE_TO_1_2;
1630 } else if (!SSL_IS_DTLS(s) && vers < TLS1_2_VERSION
1631 && (ssl_version_supported(s, TLS1_2_VERSION)
1632 || ssl_version_supported(s, TLS1_3_VERSION))) {
1633 *dgrd = DOWNGRADE_TO_1_1;
1635 *dgrd = DOWNGRADE_NONE;
1640 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1641 * client HELLO is received to select the final server protocol version and
1642 * the version specific method.
1644 * @s: server SSL handle.
1646 * Returns 0 on success or an SSL error reason number on failure.
1648 int ssl_choose_server_version(SSL *s, CLIENTHELLO_MSG *hello, DOWNGRADE *dgrd)
1651 * With version-flexible methods we have an initial state with:
1653 * s->method->version == (D)TLS_ANY_VERSION,
1654 * s->version == (D)TLS_MAX_VERSION.
1656 * So we detect version-flexible methods via the method version, not the
1659 int server_version = s->method->version;
1660 int client_version = hello->legacy_version;
1661 const version_info *vent;
1662 const version_info *table;
1664 RAW_EXTENSION *suppversions;
1666 s->client_version = client_version;
1668 switch (server_version) {
1670 if (!SSL_IS_TLS13(s)) {
1671 if (version_cmp(s, client_version, s->version) < 0)
1672 return SSL_R_WRONG_SSL_VERSION;
1673 *dgrd = DOWNGRADE_NONE;
1675 * If this SSL handle is not from a version flexible method we don't
1676 * (and never did) check min/max FIPS or Suite B constraints. Hope
1677 * that's OK. It is up to the caller to not choose fixed protocol
1678 * versions they don't want. If not, then easy to fix, just return
1679 * ssl_method_error(s, s->method)
1684 * Fall through if we are TLSv1.3 already (this means we must be after
1685 * a HelloRetryRequest
1688 case TLS_ANY_VERSION:
1689 table = tls_version_table;
1691 case DTLS_ANY_VERSION:
1692 table = dtls_version_table;
1696 suppversions = &hello->pre_proc_exts[TLSEXT_IDX_supported_versions];
1698 /* If we did an HRR then supported versions is mandatory */
1699 if (!suppversions->present && s->hello_retry_request != SSL_HRR_NONE)
1700 return SSL_R_UNSUPPORTED_PROTOCOL;
1702 if (suppversions->present && !SSL_IS_DTLS(s)) {
1703 unsigned int candidate_vers = 0;
1704 unsigned int best_vers = 0;
1705 const SSL_METHOD *best_method = NULL;
1706 PACKET versionslist;
1707 /* TODO(TLS1.3): Remove this before release */
1708 unsigned int orig_candidate = 0;
1710 suppversions->parsed = 1;
1712 if (!PACKET_as_length_prefixed_1(&suppversions->data, &versionslist)) {
1713 /* Trailing or invalid data? */
1714 return SSL_R_LENGTH_MISMATCH;
1717 while (PACKET_get_net_2(&versionslist, &candidate_vers)) {
1718 /* TODO(TLS1.3): Remove this before release */
1719 if (candidate_vers == TLS1_3_VERSION_DRAFT
1720 || candidate_vers == TLS1_3_VERSION_DRAFT_27
1721 || candidate_vers == TLS1_3_VERSION_DRAFT_26) {
1722 if (best_vers == TLS1_3_VERSION
1723 && orig_candidate > candidate_vers)
1725 orig_candidate = candidate_vers;
1726 candidate_vers = TLS1_3_VERSION;
1727 } else if (candidate_vers == TLS1_3_VERSION) {
1728 /* Don't actually accept real TLSv1.3 */
1732 * TODO(TLS1.3): There is some discussion on the TLS list about
1733 * whether to ignore versions <TLS1.2 in supported_versions. At the
1734 * moment we honour them if present. To be reviewed later
1736 if (version_cmp(s, candidate_vers, best_vers) <= 0)
1739 vent->version != 0 && vent->version != (int)candidate_vers;
1742 if (vent->version != 0 && vent->smeth != NULL) {
1743 const SSL_METHOD *method;
1745 method = vent->smeth();
1746 if (ssl_method_error(s, method) == 0) {
1747 best_vers = candidate_vers;
1748 best_method = method;
1752 if (PACKET_remaining(&versionslist) != 0) {
1753 /* Trailing data? */
1754 return SSL_R_LENGTH_MISMATCH;
1757 if (best_vers > 0) {
1758 if (s->hello_retry_request != SSL_HRR_NONE) {
1760 * This is after a HelloRetryRequest so we better check that we
1761 * negotiated TLSv1.3
1763 if (best_vers != TLS1_3_VERSION)
1764 return SSL_R_UNSUPPORTED_PROTOCOL;
1767 check_for_downgrade(s, best_vers, dgrd);
1768 s->version = best_vers;
1769 /* TODO(TLS1.3): Remove this before release */
1770 if (best_vers == TLS1_3_VERSION)
1771 s->version_draft = orig_candidate;
1772 s->method = best_method;
1775 return SSL_R_UNSUPPORTED_PROTOCOL;
1779 * If the supported versions extension isn't present, then the highest
1780 * version we can negotiate is TLSv1.2
1782 if (version_cmp(s, client_version, TLS1_3_VERSION) >= 0)
1783 client_version = TLS1_2_VERSION;
1786 * No supported versions extension, so we just use the version supplied in
1789 for (vent = table; vent->version != 0; ++vent) {
1790 const SSL_METHOD *method;
1792 if (vent->smeth == NULL ||
1793 version_cmp(s, client_version, vent->version) < 0)
1795 method = vent->smeth();
1796 if (ssl_method_error(s, method) == 0) {
1797 check_for_downgrade(s, vent->version, dgrd);
1798 s->version = vent->version;
1804 return disabled ? SSL_R_UNSUPPORTED_PROTOCOL : SSL_R_VERSION_TOO_LOW;
1808 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1809 * server HELLO is received to select the final client protocol version and
1810 * the version specific method.
1812 * @s: client SSL handle.
1813 * @version: The proposed version from the server's HELLO.
1814 * @extensions: The extensions received
1816 * Returns 1 on success or 0 on error.
1818 int ssl_choose_client_version(SSL *s, int version, RAW_EXTENSION *extensions)
1820 const version_info *vent;
1821 const version_info *table;
1826 s->version = version;
1828 /* This will overwrite s->version if the extension is present */
1829 if (!tls_parse_extension(s, TLSEXT_IDX_supported_versions,
1830 SSL_EXT_TLS1_2_SERVER_HELLO
1831 | SSL_EXT_TLS1_3_SERVER_HELLO, extensions,
1837 if (s->hello_retry_request != SSL_HRR_NONE
1838 && s->version != TLS1_3_VERSION) {
1840 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1841 SSL_R_WRONG_SSL_VERSION);
1845 switch (s->method->version) {
1847 if (s->version != s->method->version) {
1849 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1850 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1851 SSL_R_WRONG_SSL_VERSION);
1855 * If this SSL handle is not from a version flexible method we don't
1856 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1857 * that's OK. It is up to the caller to not choose fixed protocol
1858 * versions they don't want. If not, then easy to fix, just return
1859 * ssl_method_error(s, s->method)
1862 case TLS_ANY_VERSION:
1863 table = tls_version_table;
1865 case DTLS_ANY_VERSION:
1866 table = dtls_version_table;
1870 for (vent = table; vent->version != 0; ++vent) {
1871 const SSL_METHOD *method;
1874 if (vent->cmeth == NULL)
1877 if (highver != 0 && s->version != vent->version)
1880 method = vent->cmeth();
1881 err = ssl_method_error(s, method);
1883 if (s->version == vent->version) {
1885 SSLfatal(s, SSL_AD_PROTOCOL_VERSION,
1886 SSL_F_SSL_CHOOSE_CLIENT_VERSION, err);
1893 highver = vent->version;
1895 if (s->version != vent->version)
1898 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1899 /* Check for downgrades */
1900 if (s->version == TLS1_2_VERSION && highver > s->version) {
1901 if (memcmp(tls12downgrade,
1902 s->s3->server_random + SSL3_RANDOM_SIZE
1903 - sizeof(tls12downgrade),
1904 sizeof(tls12downgrade)) == 0) {
1906 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1907 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1908 SSL_R_INAPPROPRIATE_FALLBACK);
1911 } else if (!SSL_IS_DTLS(s)
1912 && s->version < TLS1_2_VERSION
1913 && highver > s->version) {
1914 if (memcmp(tls11downgrade,
1915 s->s3->server_random + SSL3_RANDOM_SIZE
1916 - sizeof(tls11downgrade),
1917 sizeof(tls11downgrade)) == 0) {
1919 SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER,
1920 SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1921 SSL_R_INAPPROPRIATE_FALLBACK);
1932 SSLfatal(s, SSL_AD_PROTOCOL_VERSION, SSL_F_SSL_CHOOSE_CLIENT_VERSION,
1933 SSL_R_UNSUPPORTED_PROTOCOL);
1938 * ssl_get_min_max_version - get minimum and maximum protocol version
1939 * @s: The SSL connection
1940 * @min_version: The minimum supported version
1941 * @max_version: The maximum supported version
1943 * Work out what version we should be using for the initial ClientHello if the
1944 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1945 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1946 * constraints and any floor imposed by the security level here,
1947 * so we don't advertise the wrong protocol version to only reject the outcome later.
1949 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1950 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1951 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1953 * Returns 0 on success or an SSL error reason number on failure. On failure
1954 * min_version and max_version will also be set to 0.
1956 int ssl_get_min_max_version(const SSL *s, int *min_version, int *max_version)
1960 const SSL_METHOD *single = NULL;
1961 const SSL_METHOD *method;
1962 const version_info *table;
1963 const version_info *vent;
1965 switch (s->method->version) {
1968 * If this SSL handle is not from a version flexible method we don't
1969 * (and never did) check min/max FIPS or Suite B constraints. Hope
1970 * that's OK. It is up to the caller to not choose fixed protocol
1971 * versions they don't want. If not, then easy to fix, just return
1972 * ssl_method_error(s, s->method)
1974 *min_version = *max_version = s->version;
1976 case TLS_ANY_VERSION:
1977 table = tls_version_table;
1979 case DTLS_ANY_VERSION:
1980 table = dtls_version_table;
1985 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1986 * below X enabled. This is required in order to maintain the "version
1987 * capability" vector contiguous. Any versions with a NULL client method
1988 * (protocol version client is disabled at compile-time) is also a "hole".
1990 * Our initial state is hole == 1, version == 0. That is, versions above
1991 * the first version in the method table are disabled (a "hole" above
1992 * the valid protocol entries) and we don't have a selected version yet.
1994 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1995 * the selected version, and the method becomes a candidate "single"
1996 * method. We're no longer in a hole, so "hole" becomes 0.
1998 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1999 * as we support a contiguous range of at least two methods. If we hit
2000 * a disabled method, then hole becomes true again, but nothing else
2001 * changes yet, because all the remaining methods may be disabled too.
2002 * If we again hit an enabled method after the new hole, it becomes
2003 * selected, as we start from scratch.
2005 *min_version = version = 0;
2007 for (vent = table; vent->version != 0; ++vent) {
2009 * A table entry with a NULL client method is still a hole in the
2010 * "version capability" vector.
2012 if (vent->cmeth == NULL) {
2016 method = vent->cmeth();
2017 if (ssl_method_error(s, method) != 0) {
2021 *min_version = method->version;
2023 version = (single = method)->version;
2024 *min_version = version;
2029 *max_version = version;
2031 /* Fail if everything is disabled */
2033 return SSL_R_NO_PROTOCOLS_AVAILABLE;
2039 * ssl_set_client_hello_version - Work out what version we should be using for
2040 * the initial ClientHello.legacy_version field.
2042 * @s: client SSL handle.
2044 * Returns 0 on success or an SSL error reason number on failure.
2046 int ssl_set_client_hello_version(SSL *s)
2048 int ver_min, ver_max, ret;
2051 * In a renegotiation we always send the same client_version that we sent
2052 * last time, regardless of which version we eventually negotiated.
2054 if (!SSL_IS_FIRST_HANDSHAKE(s))
2057 ret = ssl_get_min_max_version(s, &ver_min, &ver_max);
2062 s->version = ver_max;
2064 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2065 if (!SSL_IS_DTLS(s) && ver_max > TLS1_2_VERSION)
2066 ver_max = TLS1_2_VERSION;
2068 s->client_version = ver_max;
2073 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2074 * and |checkallow| is 1 then additionally check if the group is allowed to be
2075 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2076 * 1) or 0 otherwise.
2078 #ifndef OPENSSL_NO_EC
2079 int check_in_list(SSL *s, uint16_t group_id, const uint16_t *groups,
2080 size_t num_groups, int checkallow)
2084 if (groups == NULL || num_groups == 0)
2087 for (i = 0; i < num_groups; i++) {
2088 uint16_t group = groups[i];
2090 if (group_id == group
2092 || tls_curve_allowed(s, group, SSL_SECOP_CURVE_CHECK))) {
2101 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2102 int create_synthetic_message_hash(SSL *s, const unsigned char *hashval,
2103 size_t hashlen, const unsigned char *hrr,
2106 unsigned char hashvaltmp[EVP_MAX_MD_SIZE];
2107 unsigned char msghdr[SSL3_HM_HEADER_LENGTH];
2109 memset(msghdr, 0, sizeof(msghdr));
2111 if (hashval == NULL) {
2112 hashval = hashvaltmp;
2114 /* Get the hash of the initial ClientHello */
2115 if (!ssl3_digest_cached_records(s, 0)
2116 || !ssl_handshake_hash(s, hashvaltmp, sizeof(hashvaltmp),
2118 /* SSLfatal() already called */
2123 /* Reinitialise the transcript hash */
2124 if (!ssl3_init_finished_mac(s)) {
2125 /* SSLfatal() already called */
2129 /* Inject the synthetic message_hash message */
2130 msghdr[0] = SSL3_MT_MESSAGE_HASH;
2131 msghdr[SSL3_HM_HEADER_LENGTH - 1] = (unsigned char)hashlen;
2132 if (!ssl3_finish_mac(s, msghdr, SSL3_HM_HEADER_LENGTH)
2133 || !ssl3_finish_mac(s, hashval, hashlen)) {
2134 /* SSLfatal() already called */
2139 * Now re-inject the HRR and current message if appropriate (we just deleted
2140 * it when we reinitialised the transcript hash above). Only necessary after
2141 * receiving a ClientHello2 with a cookie.
2144 && (!ssl3_finish_mac(s, hrr, hrrlen)
2145 || !ssl3_finish_mac(s, (unsigned char *)s->init_buf->data,
2146 s->s3->tmp.message_size
2147 + SSL3_HM_HEADER_LENGTH))) {
2148 /* SSLfatal() already called */
2155 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2157 return X509_NAME_cmp(*a, *b);
2160 int parse_ca_names(SSL *s, PACKET *pkt)
2162 STACK_OF(X509_NAME) *ca_sk = sk_X509_NAME_new(ca_dn_cmp);
2163 X509_NAME *xn = NULL;
2166 if (ca_sk == NULL) {
2167 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2168 ERR_R_MALLOC_FAILURE);
2171 /* get the CA RDNs */
2172 if (!PACKET_get_length_prefixed_2(pkt, &cadns)) {
2173 SSLfatal(s, SSL_AD_DECODE_ERROR,SSL_F_PARSE_CA_NAMES,
2174 SSL_R_LENGTH_MISMATCH);
2178 while (PACKET_remaining(&cadns)) {
2179 const unsigned char *namestart, *namebytes;
2180 unsigned int name_len;
2182 if (!PACKET_get_net_2(&cadns, &name_len)
2183 || !PACKET_get_bytes(&cadns, &namebytes, name_len)) {
2184 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2185 SSL_R_LENGTH_MISMATCH);
2189 namestart = namebytes;
2190 if ((xn = d2i_X509_NAME(NULL, &namebytes, name_len)) == NULL) {
2191 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2195 if (namebytes != (namestart + name_len)) {
2196 SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_PARSE_CA_NAMES,
2197 SSL_R_CA_DN_LENGTH_MISMATCH);
2201 if (!sk_X509_NAME_push(ca_sk, xn)) {
2202 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_PARSE_CA_NAMES,
2203 ERR_R_MALLOC_FAILURE);
2209 sk_X509_NAME_pop_free(s->s3->tmp.peer_ca_names, X509_NAME_free);
2210 s->s3->tmp.peer_ca_names = ca_sk;
2215 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2220 int construct_ca_names(SSL *s, WPACKET *pkt)
2222 const STACK_OF(X509_NAME) *ca_sk = SSL_get0_CA_list(s);
2224 /* Start sub-packet for client CA list */
2225 if (!WPACKET_start_sub_packet_u16(pkt)) {
2226 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2227 ERR_R_INTERNAL_ERROR);
2231 if (ca_sk != NULL) {
2234 for (i = 0; i < sk_X509_NAME_num(ca_sk); i++) {
2235 unsigned char *namebytes;
2236 X509_NAME *name = sk_X509_NAME_value(ca_sk, i);
2240 || (namelen = i2d_X509_NAME(name, NULL)) < 0
2241 || !WPACKET_sub_allocate_bytes_u16(pkt, namelen,
2243 || i2d_X509_NAME(name, &namebytes) != namelen) {
2244 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2245 ERR_R_INTERNAL_ERROR);
2251 if (!WPACKET_close(pkt)) {
2252 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_CA_NAMES,
2253 ERR_R_INTERNAL_ERROR);
2260 /* Create a buffer containing data to be signed for server key exchange */
2261 size_t construct_key_exchange_tbs(SSL *s, unsigned char **ptbs,
2262 const void *param, size_t paramlen)
2264 size_t tbslen = 2 * SSL3_RANDOM_SIZE + paramlen;
2265 unsigned char *tbs = OPENSSL_malloc(tbslen);
2268 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_CONSTRUCT_KEY_EXCHANGE_TBS,
2269 ERR_R_MALLOC_FAILURE);
2272 memcpy(tbs, s->s3->client_random, SSL3_RANDOM_SIZE);
2273 memcpy(tbs + SSL3_RANDOM_SIZE, s->s3->server_random, SSL3_RANDOM_SIZE);
2275 memcpy(tbs + SSL3_RANDOM_SIZE * 2, param, paramlen);
2282 * Saves the current handshake digest for Post-Handshake Auth,
2283 * Done after ClientFinished is processed, done exactly once
2285 int tls13_save_handshake_digest_for_pha(SSL *s)
2287 if (s->pha_dgst == NULL) {
2288 if (!ssl3_digest_cached_records(s, 1))
2289 /* SSLfatal() already called */
2292 s->pha_dgst = EVP_MD_CTX_new();
2293 if (s->pha_dgst == NULL) {
2294 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2295 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2296 ERR_R_INTERNAL_ERROR);
2299 if (!EVP_MD_CTX_copy_ex(s->pha_dgst,
2300 s->s3->handshake_dgst)) {
2301 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2302 SSL_F_TLS13_SAVE_HANDSHAKE_DIGEST_FOR_PHA,
2303 ERR_R_INTERNAL_ERROR);
2311 * Restores the Post-Handshake Auth handshake digest
2312 * Done just before sending/processing the Cert Request
2314 int tls13_restore_handshake_digest_for_pha(SSL *s)
2316 if (s->pha_dgst == NULL) {
2317 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2318 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2319 ERR_R_INTERNAL_ERROR);
2322 if (!EVP_MD_CTX_copy_ex(s->s3->handshake_dgst,
2324 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
2325 SSL_F_TLS13_RESTORE_HANDSHAKE_DIGEST_FOR_PHA,
2326 ERR_R_INTERNAL_ERROR);