2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (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_local.h"
15 #include "statem_local.h"
16 #include "internal/cryptlib.h"
17 #include "internal/ssl_unwrap.h"
18 #include <openssl/buffer.h>
19 #include <openssl/objects.h>
20 #include <openssl/evp.h>
21 #include <openssl/rsa.h>
22 #include <openssl/x509.h>
23 #include <openssl/trace.h>
24 #include <openssl/encoder.h>
27 * Map error codes to TLS/SSL alart types.
29 typedef struct x509err2alert_st
{
34 /* Fixed value used in the ServerHello random field to identify an HRR */
35 const unsigned char hrrrandom
[] = {
36 0xcf, 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02,
37 0x1e, 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e,
38 0x07, 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c
41 int ossl_statem_set_mutator(SSL
*s
,
42 ossl_statem_mutate_handshake_cb mutate_handshake_cb
,
43 ossl_statem_finish_mutate_handshake_cb finish_mutate_handshake_cb
,
46 SSL_CONNECTION
*sc
= SSL_CONNECTION_FROM_SSL(s
);
51 sc
->statem
.mutate_handshake_cb
= mutate_handshake_cb
;
52 sc
->statem
.mutatearg
= mutatearg
;
53 sc
->statem
.finish_mutate_handshake_cb
= finish_mutate_handshake_cb
;
59 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
60 * SSL3_RT_CHANGE_CIPHER_SPEC)
62 int ssl3_do_write(SSL_CONNECTION
*s
, uint8_t type
)
66 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
67 SSL
*ussl
= SSL_CONNECTION_GET_USER_SSL(s
);
70 * If we're running the test suite then we may need to mutate the message
71 * we've been asked to write. Does not happen in normal operation.
73 if (s
->statem
.mutate_handshake_cb
!= NULL
74 && !s
->statem
.write_in_progress
75 && type
== SSL3_RT_HANDSHAKE
76 && s
->init_num
>= SSL3_HM_HEADER_LENGTH
) {
80 if (!s
->statem
.mutate_handshake_cb((unsigned char *)s
->init_buf
->data
,
85 if (msglen
< SSL3_HM_HEADER_LENGTH
86 || !BUF_MEM_grow(s
->init_buf
, msglen
))
88 memcpy(s
->init_buf
->data
, msg
, msglen
);
90 s
->init_msg
= s
->init_buf
->data
+ SSL3_HM_HEADER_LENGTH
;
91 s
->statem
.finish_mutate_handshake_cb(s
->statem
.mutatearg
);
92 s
->statem
.write_in_progress
= 1;
95 ret
= ssl3_write_bytes(ssl
, type
, &s
->init_buf
->data
[s
->init_off
],
96 s
->init_num
, &written
);
99 if (type
== SSL3_RT_HANDSHAKE
)
101 * should not be done for 'Hello Request's, but in that case we'll
102 * ignore the result anyway
103 * TLS1.3 KeyUpdate and NewSessionTicket do not need to be added
105 if (!SSL_CONNECTION_IS_TLS13(s
)
106 || (s
->statem
.hand_state
!= TLS_ST_SW_SESSION_TICKET
107 && s
->statem
.hand_state
!= TLS_ST_CW_KEY_UPDATE
108 && s
->statem
.hand_state
!= TLS_ST_SW_KEY_UPDATE
))
109 if (!ssl3_finish_mac(s
,
110 (unsigned char *)&s
->init_buf
->data
[s
->init_off
],
113 if (written
== s
->init_num
) {
114 s
->statem
.write_in_progress
= 0;
116 s
->msg_callback(1, s
->version
, type
, s
->init_buf
->data
,
117 (size_t)(s
->init_off
+ s
->init_num
), ussl
,
118 s
->msg_callback_arg
);
121 s
->init_off
+= written
;
122 s
->init_num
-= written
;
126 int tls_close_construct_packet(SSL_CONNECTION
*s
, WPACKET
*pkt
, int htype
)
130 if ((htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
&& !WPACKET_close(pkt
))
131 || !WPACKET_get_length(pkt
, &msglen
)
134 s
->init_num
= (int)msglen
;
140 int tls_setup_handshake(SSL_CONNECTION
*s
)
142 int ver_min
, ver_max
, ok
;
143 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
144 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
146 if (!ssl3_init_finished_mac(s
)) {
147 /* SSLfatal() already called */
151 /* Reset any extension flags */
152 memset(s
->ext
.extflags
, 0, sizeof(s
->ext
.extflags
));
154 if (ssl_get_min_max_version(s
, &ver_min
, &ver_max
, NULL
) != 0) {
155 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_NO_PROTOCOLS_AVAILABLE
);
159 /* Sanity check that we have MD5-SHA1 if we need it */
160 if (sctx
->ssl_digest_methods
[SSL_MD_MD5_SHA1_IDX
] == NULL
) {
161 int negotiated_minversion
;
162 int md5sha1_needed_maxversion
= SSL_CONNECTION_IS_DTLS(s
)
163 ? DTLS1_VERSION
: TLS1_1_VERSION
;
165 /* We don't have MD5-SHA1 - do we need it? */
166 if (ssl_version_cmp(s
, ver_max
, md5sha1_needed_maxversion
) <= 0) {
167 SSLfatal_data(s
, SSL_AD_HANDSHAKE_FAILURE
,
168 SSL_R_NO_SUITABLE_DIGEST_ALGORITHM
,
169 "The max supported SSL/TLS version needs the"
170 " MD5-SHA1 digest but it is not available"
171 " in the loaded providers. Use (D)TLSv1.2 or"
172 " above, or load different providers");
178 /* Don't allow TLSv1.1 or below to be negotiated */
179 negotiated_minversion
= SSL_CONNECTION_IS_DTLS(s
) ?
180 DTLS1_2_VERSION
: TLS1_2_VERSION
;
181 if (ssl_version_cmp(s
, ver_min
, negotiated_minversion
) < 0)
182 ok
= SSL_set_min_proto_version(ssl
, negotiated_minversion
);
184 /* Shouldn't happen */
185 SSLfatal(s
, SSL_AD_HANDSHAKE_FAILURE
, ERR_R_INTERNAL_ERROR
);
192 STACK_OF(SSL_CIPHER
) *ciphers
= SSL_get_ciphers(ssl
);
196 * Sanity check that the maximum version we accept has ciphers
197 * enabled. For clients we do this check during construction of the
200 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
201 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(ciphers
, i
);
202 int cipher_minprotover
= SSL_CONNECTION_IS_DTLS(s
)
203 ? c
->min_dtls
: c
->min_tls
;
204 int cipher_maxprotover
= SSL_CONNECTION_IS_DTLS(s
)
205 ? c
->max_dtls
: c
->max_tls
;
207 if (ssl_version_cmp(s
, ver_max
, cipher_minprotover
) >= 0
208 && ssl_version_cmp(s
, ver_max
, cipher_maxprotover
) <= 0) {
214 SSLfatal_data(s
, SSL_AD_HANDSHAKE_FAILURE
,
215 SSL_R_NO_CIPHERS_AVAILABLE
,
216 "No ciphers enabled for max supported "
220 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
221 /* N.B. s->session_ctx == s->ctx here */
222 ssl_tsan_counter(s
->session_ctx
, &s
->session_ctx
->stats
.sess_accept
);
224 /* N.B. s->ctx may not equal s->session_ctx */
225 ssl_tsan_counter(sctx
, &sctx
->stats
.sess_accept_renegotiate
);
227 s
->s3
.tmp
.cert_request
= 0;
230 if (SSL_IS_FIRST_HANDSHAKE(s
))
231 ssl_tsan_counter(s
->session_ctx
, &s
->session_ctx
->stats
.sess_connect
);
233 ssl_tsan_counter(s
->session_ctx
,
234 &s
->session_ctx
->stats
.sess_connect_renegotiate
);
236 /* mark client_random uninitialized */
237 memset(s
->s3
.client_random
, 0, sizeof(s
->s3
.client_random
));
240 s
->s3
.tmp
.cert_req
= 0;
242 if (SSL_CONNECTION_IS_DTLS(s
))
243 s
->statem
.use_timer
= 1;
250 * Size of the to-be-signed TLS13 data, without the hash size itself:
251 * 64 bytes of value 32, 33 context bytes, 1 byte separator
253 #define TLS13_TBS_START_SIZE 64
254 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
256 static int get_cert_verify_tbs_data(SSL_CONNECTION
*s
, unsigned char *tls13tbs
,
257 void **hdata
, size_t *hdatalen
)
259 /* ASCII: "TLS 1.3, server CertificateVerify", in hex for EBCDIC compatibility */
260 static const char servercontext
[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x73\x65\x72"
261 "\x76\x65\x72\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79";
262 /* ASCII: "TLS 1.3, client CertificateVerify", in hex for EBCDIC compatibility */
263 static const char clientcontext
[] = "\x54\x4c\x53\x20\x31\x2e\x33\x2c\x20\x63\x6c\x69"
264 "\x65\x6e\x74\x20\x43\x65\x72\x74\x69\x66\x69\x63\x61\x74\x65\x56\x65\x72\x69\x66\x79";
266 if (SSL_CONNECTION_IS_TLS13(s
)) {
269 /* Set the first 64 bytes of to-be-signed data to octet 32 */
270 memset(tls13tbs
, 32, TLS13_TBS_START_SIZE
);
271 /* This copies the 33 bytes of context plus the 0 separator byte */
272 if (s
->statem
.hand_state
== TLS_ST_CR_CERT_VRFY
273 || s
->statem
.hand_state
== TLS_ST_SW_CERT_VRFY
)
274 strcpy((char *)tls13tbs
+ TLS13_TBS_START_SIZE
, servercontext
);
276 strcpy((char *)tls13tbs
+ TLS13_TBS_START_SIZE
, clientcontext
);
279 * If we're currently reading then we need to use the saved handshake
280 * hash value. We can't use the current handshake hash state because
281 * that includes the CertVerify itself.
283 if (s
->statem
.hand_state
== TLS_ST_CR_CERT_VRFY
284 || s
->statem
.hand_state
== TLS_ST_SR_CERT_VRFY
) {
285 memcpy(tls13tbs
+ TLS13_TBS_PREAMBLE_SIZE
, s
->cert_verify_hash
,
286 s
->cert_verify_hash_len
);
287 hashlen
= s
->cert_verify_hash_len
;
288 } else if (!ssl_handshake_hash(s
, tls13tbs
+ TLS13_TBS_PREAMBLE_SIZE
,
289 EVP_MAX_MD_SIZE
, &hashlen
)) {
290 /* SSLfatal() already called */
295 *hdatalen
= TLS13_TBS_PREAMBLE_SIZE
+ hashlen
;
300 retlen
= retlen_l
= BIO_get_mem_data(s
->s3
.handshake_buffer
, hdata
);
302 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
311 CON_FUNC_RETURN
tls_construct_cert_verify(SSL_CONNECTION
*s
, WPACKET
*pkt
)
313 EVP_PKEY
*pkey
= NULL
;
314 const EVP_MD
*md
= NULL
;
315 EVP_MD_CTX
*mctx
= NULL
;
316 EVP_PKEY_CTX
*pctx
= NULL
;
317 size_t hdatalen
= 0, siglen
= 0;
319 unsigned char *sig
= NULL
;
320 unsigned char tls13tbs
[TLS13_TBS_PREAMBLE_SIZE
+ EVP_MAX_MD_SIZE
];
321 const SIGALG_LOOKUP
*lu
= s
->s3
.tmp
.sigalg
;
322 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
324 if (lu
== NULL
|| s
->s3
.tmp
.cert
== NULL
) {
325 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
328 pkey
= s
->s3
.tmp
.cert
->privatekey
;
330 if (pkey
== NULL
|| !tls1_lookup_md(sctx
, lu
, &md
)) {
331 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
335 mctx
= EVP_MD_CTX_new();
337 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
341 /* Get the data to be signed */
342 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
343 /* SSLfatal() already called */
347 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
348 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
352 if (EVP_DigestSignInit_ex(mctx
, &pctx
,
353 md
== NULL
? NULL
: EVP_MD_get0_name(md
),
354 sctx
->libctx
, sctx
->propq
, pkey
,
356 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
360 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
361 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
362 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
363 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
364 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
368 if (s
->version
== SSL3_VERSION
) {
370 * Here we use EVP_DigestSignUpdate followed by EVP_DigestSignFinal
371 * in order to add the EVP_CTRL_SSL3_MASTER_SECRET call between them.
373 if (EVP_DigestSignUpdate(mctx
, hdata
, hdatalen
) <= 0
374 || EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
375 (int)s
->session
->master_key_length
,
376 s
->session
->master_key
) <= 0
377 || EVP_DigestSignFinal(mctx
, NULL
, &siglen
) <= 0) {
379 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
382 sig
= OPENSSL_malloc(siglen
);
384 || EVP_DigestSignFinal(mctx
, sig
, &siglen
) <= 0) {
385 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
390 * Here we *must* use EVP_DigestSign() because Ed25519/Ed448 does not
391 * support streaming via EVP_DigestSignUpdate/EVP_DigestSignFinal
393 if (EVP_DigestSign(mctx
, NULL
, &siglen
, hdata
, hdatalen
) <= 0) {
394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
397 sig
= OPENSSL_malloc(siglen
);
399 || EVP_DigestSign(mctx
, sig
, &siglen
, hdata
, hdatalen
) <= 0) {
400 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
405 #ifndef OPENSSL_NO_GOST
407 int pktype
= lu
->sig
;
409 if (pktype
== NID_id_GostR3410_2001
410 || pktype
== NID_id_GostR3410_2012_256
411 || pktype
== NID_id_GostR3410_2012_512
)
412 BUF_reverse(sig
, NULL
, siglen
);
416 if (!WPACKET_sub_memcpy_u16(pkt
, sig
, siglen
)) {
417 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
421 /* Digest cached records and discard handshake buffer */
422 if (!ssl3_digest_cached_records(s
, 0)) {
423 /* SSLfatal() already called */
428 EVP_MD_CTX_free(mctx
);
429 return CON_FUNC_SUCCESS
;
432 EVP_MD_CTX_free(mctx
);
433 return CON_FUNC_ERROR
;
436 MSG_PROCESS_RETURN
tls_process_cert_verify(SSL_CONNECTION
*s
, PACKET
*pkt
)
438 EVP_PKEY
*pkey
= NULL
;
439 const unsigned char *data
;
440 #ifndef OPENSSL_NO_GOST
441 unsigned char *gost_data
= NULL
;
443 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
446 const EVP_MD
*md
= NULL
;
449 unsigned char tls13tbs
[TLS13_TBS_PREAMBLE_SIZE
+ EVP_MAX_MD_SIZE
];
450 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
451 EVP_PKEY_CTX
*pctx
= NULL
;
452 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
455 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
459 pkey
= tls_get_peer_pkey(s
);
461 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
465 if (ssl_cert_lookup_by_pkey(pkey
, NULL
, sctx
) == NULL
) {
466 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
467 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
471 if (SSL_USE_SIGALGS(s
)) {
474 if (!PACKET_get_net_2(pkt
, &sigalg
)) {
475 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_PACKET
);
478 if (tls12_check_peer_sigalg(s
, sigalg
, pkey
) <= 0) {
479 /* SSLfatal() already called */
482 } else if (!tls1_set_peer_legacy_sigalg(s
, pkey
)) {
483 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
484 SSL_R_LEGACY_SIGALG_DISALLOWED_OR_UNSUPPORTED
);
488 if (!tls1_lookup_md(sctx
, s
->s3
.tmp
.peer_sigalg
, &md
)) {
489 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
493 if (SSL_USE_SIGALGS(s
))
494 OSSL_TRACE1(TLS
, "USING TLSv1.2 HASH %s\n",
495 md
== NULL
? "n/a" : EVP_MD_get0_name(md
));
497 /* Check for broken implementations of GOST ciphersuites */
499 * If key is GOST and len is exactly 64 or 128, it is signature without
500 * length field (CryptoPro implementations at least till TLS 1.2)
502 #ifndef OPENSSL_NO_GOST
503 if (!SSL_USE_SIGALGS(s
)
504 && ((PACKET_remaining(pkt
) == 64
505 && (EVP_PKEY_get_id(pkey
) == NID_id_GostR3410_2001
506 || EVP_PKEY_get_id(pkey
) == NID_id_GostR3410_2012_256
))
507 || (PACKET_remaining(pkt
) == 128
508 && EVP_PKEY_get_id(pkey
) == NID_id_GostR3410_2012_512
))) {
509 len
= (unsigned int)PACKET_remaining(pkt
);
512 if (!PACKET_get_net_2(pkt
, &len
)) {
513 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
517 if (!PACKET_get_bytes(pkt
, &data
, len
)) {
518 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
521 if (PACKET_remaining(pkt
) != 0) {
522 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
526 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
527 /* SSLfatal() already called */
531 OSSL_TRACE1(TLS
, "Using client verify alg %s\n",
532 md
== NULL
? "n/a" : EVP_MD_get0_name(md
));
534 if (EVP_DigestVerifyInit_ex(mctx
, &pctx
,
535 md
== NULL
? NULL
: EVP_MD_get0_name(md
),
536 sctx
->libctx
, sctx
->propq
, pkey
,
538 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
541 #ifndef OPENSSL_NO_GOST
543 int pktype
= EVP_PKEY_get_id(pkey
);
544 if (pktype
== NID_id_GostR3410_2001
545 || pktype
== NID_id_GostR3410_2012_256
546 || pktype
== NID_id_GostR3410_2012_512
) {
547 if ((gost_data
= OPENSSL_malloc(len
)) == NULL
)
549 BUF_reverse(gost_data
, data
, len
);
555 if (SSL_USE_PSS(s
)) {
556 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
557 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
558 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
559 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
563 if (s
->version
== SSL3_VERSION
) {
564 if (EVP_DigestVerifyUpdate(mctx
, hdata
, hdatalen
) <= 0
565 || EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
566 (int)s
->session
->master_key_length
,
567 s
->session
->master_key
) <= 0) {
568 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
571 if (EVP_DigestVerifyFinal(mctx
, data
, len
) <= 0) {
572 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_BAD_SIGNATURE
);
576 j
= EVP_DigestVerify(mctx
, data
, len
, hdata
, hdatalen
);
577 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
578 /* Ignore bad signatures when fuzzing */
579 if (SSL_IS_QUIC_HANDSHAKE(s
))
583 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_BAD_SIGNATURE
);
589 * In TLSv1.3 on the client side we make sure we prepare the client
590 * certificate after the CertVerify instead of when we get the
591 * CertificateRequest. This is because in TLSv1.3 the CertificateRequest
592 * comes *before* the Certificate message. In TLSv1.2 it comes after. We
593 * want to make sure that SSL_get1_peer_certificate() will return the actual
594 * server certificate from the client_cert_cb callback.
596 if (!s
->server
&& SSL_CONNECTION_IS_TLS13(s
) && s
->s3
.tmp
.cert_req
== 1)
597 ret
= MSG_PROCESS_CONTINUE_PROCESSING
;
599 ret
= MSG_PROCESS_CONTINUE_READING
;
601 BIO_free(s
->s3
.handshake_buffer
);
602 s
->s3
.handshake_buffer
= NULL
;
603 EVP_MD_CTX_free(mctx
);
604 #ifndef OPENSSL_NO_GOST
605 OPENSSL_free(gost_data
);
610 CON_FUNC_RETURN
tls_construct_finished(SSL_CONNECTION
*s
, WPACKET
*pkt
)
612 size_t finish_md_len
;
615 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
617 /* This is a real handshake so make sure we clean it up at the end */
618 if (!s
->server
&& s
->post_handshake_auth
!= SSL_PHA_REQUESTED
)
619 s
->statem
.cleanuphand
= 1;
622 * If we attempted to write early data or we're in middlebox compat mode
623 * then we deferred changing the handshake write keys to the last possible
624 * moment. If we didn't already do this when we sent the client certificate
625 * then we need to do it now.
627 if (SSL_CONNECTION_IS_TLS13(s
)
629 && !SSL_IS_QUIC_HANDSHAKE(s
)
630 && (s
->early_data_state
!= SSL_EARLY_DATA_NONE
631 || (s
->options
& SSL_OP_ENABLE_MIDDLEBOX_COMPAT
) != 0)
632 && s
->s3
.tmp
.cert_req
== 0
633 && (!ssl
->method
->ssl3_enc
->change_cipher_state(s
,
634 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
))) {;
635 /* SSLfatal() already called */
636 return CON_FUNC_ERROR
;
640 sender
= ssl
->method
->ssl3_enc
->server_finished_label
;
641 slen
= ssl
->method
->ssl3_enc
->server_finished_label_len
;
643 sender
= ssl
->method
->ssl3_enc
->client_finished_label
;
644 slen
= ssl
->method
->ssl3_enc
->client_finished_label_len
;
647 finish_md_len
= ssl
->method
->ssl3_enc
->final_finish_mac(s
,
649 s
->s3
.tmp
.finish_md
);
650 if (finish_md_len
== 0) {
651 /* SSLfatal() already called */
652 return CON_FUNC_ERROR
;
655 s
->s3
.tmp
.finish_md_len
= finish_md_len
;
657 if (!WPACKET_memcpy(pkt
, s
->s3
.tmp
.finish_md
, finish_md_len
)) {
658 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
659 return CON_FUNC_ERROR
;
663 * Log the master secret, if logging is enabled. We don't log it for
664 * TLSv1.3: there's a different key schedule for that.
666 if (!SSL_CONNECTION_IS_TLS13(s
)
667 && !ssl_log_secret(s
, MASTER_SECRET_LABEL
, s
->session
->master_key
,
668 s
->session
->master_key_length
)) {
669 /* SSLfatal() already called */
670 return CON_FUNC_ERROR
;
674 * Copy the finished so we can use it for renegotiation checks
676 if (!ossl_assert(finish_md_len
<= EVP_MAX_MD_SIZE
)) {
677 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
678 return CON_FUNC_ERROR
;
681 memcpy(s
->s3
.previous_client_finished
, s
->s3
.tmp
.finish_md
,
683 s
->s3
.previous_client_finished_len
= finish_md_len
;
685 memcpy(s
->s3
.previous_server_finished
, s
->s3
.tmp
.finish_md
,
687 s
->s3
.previous_server_finished_len
= finish_md_len
;
690 return CON_FUNC_SUCCESS
;
693 CON_FUNC_RETURN
tls_construct_key_update(SSL_CONNECTION
*s
, WPACKET
*pkt
)
695 if (!WPACKET_put_bytes_u8(pkt
, s
->key_update
)) {
696 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
697 return CON_FUNC_ERROR
;
700 s
->key_update
= SSL_KEY_UPDATE_NONE
;
701 return CON_FUNC_SUCCESS
;
704 MSG_PROCESS_RETURN
tls_process_key_update(SSL_CONNECTION
*s
, PACKET
*pkt
)
706 unsigned int updatetype
;
709 * A KeyUpdate message signals a key change so the end of the message must
710 * be on a record boundary.
712 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
713 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
714 return MSG_PROCESS_ERROR
;
717 if (!PACKET_get_1(pkt
, &updatetype
)
718 || PACKET_remaining(pkt
) != 0) {
719 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_KEY_UPDATE
);
720 return MSG_PROCESS_ERROR
;
724 * There are only two defined key update types. Fail if we get a value we
727 if (updatetype
!= SSL_KEY_UPDATE_NOT_REQUESTED
728 && updatetype
!= SSL_KEY_UPDATE_REQUESTED
) {
729 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_KEY_UPDATE
);
730 return MSG_PROCESS_ERROR
;
734 * If we get a request for us to update our sending keys too then, we need
735 * to additionally send a KeyUpdate message. However that message should
736 * not also request an update (otherwise we get into an infinite loop).
738 if (updatetype
== SSL_KEY_UPDATE_REQUESTED
)
739 s
->key_update
= SSL_KEY_UPDATE_NOT_REQUESTED
;
741 if (!tls13_update_key(s
, 0)) {
742 /* SSLfatal() already called */
743 return MSG_PROCESS_ERROR
;
746 return MSG_PROCESS_FINISHED_READING
;
750 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
753 int ssl3_take_mac(SSL_CONNECTION
*s
)
757 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
760 sender
= ssl
->method
->ssl3_enc
->server_finished_label
;
761 slen
= ssl
->method
->ssl3_enc
->server_finished_label_len
;
763 sender
= ssl
->method
->ssl3_enc
->client_finished_label
;
764 slen
= ssl
->method
->ssl3_enc
->client_finished_label_len
;
767 s
->s3
.tmp
.peer_finish_md_len
=
768 ssl
->method
->ssl3_enc
->final_finish_mac(s
, sender
, slen
,
769 s
->s3
.tmp
.peer_finish_md
);
771 if (s
->s3
.tmp
.peer_finish_md_len
== 0) {
772 /* SSLfatal() already called */
779 MSG_PROCESS_RETURN
tls_process_change_cipher_spec(SSL_CONNECTION
*s
,
784 remain
= PACKET_remaining(pkt
);
786 * 'Change Cipher Spec' is just a single byte, which should already have
787 * been consumed by ssl_get_message() so there should be no bytes left,
788 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
790 if (SSL_CONNECTION_IS_DTLS(s
)) {
791 if ((s
->version
== DTLS1_BAD_VER
792 && remain
!= DTLS1_CCS_HEADER_LENGTH
+ 1)
793 || (s
->version
!= DTLS1_BAD_VER
794 && remain
!= DTLS1_CCS_HEADER_LENGTH
- 1)) {
795 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_CHANGE_CIPHER_SPEC
);
796 return MSG_PROCESS_ERROR
;
800 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_CHANGE_CIPHER_SPEC
);
801 return MSG_PROCESS_ERROR
;
805 /* Check we have a cipher to change to */
806 if (s
->s3
.tmp
.new_cipher
== NULL
) {
807 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_CCS_RECEIVED_EARLY
);
808 return MSG_PROCESS_ERROR
;
811 s
->s3
.change_cipher_spec
= 1;
812 if (!ssl3_do_change_cipher_spec(s
)) {
813 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
814 return MSG_PROCESS_ERROR
;
817 if (SSL_CONNECTION_IS_DTLS(s
)) {
818 if (s
->version
== DTLS1_BAD_VER
)
819 s
->d1
->handshake_read_seq
++;
821 #ifndef OPENSSL_NO_SCTP
823 * Remember that a CCS has been received, so that an old key of
824 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
827 BIO_ctrl(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s
)),
828 BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
, 1, NULL
);
832 return MSG_PROCESS_CONTINUE_READING
;
835 MSG_PROCESS_RETURN
tls_process_finished(SSL_CONNECTION
*s
, PACKET
*pkt
)
838 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
839 int was_first
= SSL_IS_FIRST_HANDSHAKE(s
);
843 /* This is a real handshake so make sure we clean it up at the end */
846 * To get this far we must have read encrypted data from the client. We
847 * no longer tolerate unencrypted alerts. This is ignored if less than
850 if (s
->rlayer
.rrlmethod
->set_plain_alerts
!= NULL
)
851 s
->rlayer
.rrlmethod
->set_plain_alerts(s
->rlayer
.rrl
, 0);
852 if (s
->post_handshake_auth
!= SSL_PHA_REQUESTED
)
853 s
->statem
.cleanuphand
= 1;
854 if (SSL_CONNECTION_IS_TLS13(s
)
855 && !tls13_save_handshake_digest_for_pha(s
)) {
856 /* SSLfatal() already called */
857 return MSG_PROCESS_ERROR
;
862 * In TLSv1.3 a Finished message signals a key change so the end of the
863 * message must be on a record boundary.
865 if (SSL_CONNECTION_IS_TLS13(s
)
866 && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
867 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
868 return MSG_PROCESS_ERROR
;
871 /* If this occurs, we have missed a message */
872 if (!SSL_CONNECTION_IS_TLS13(s
) && !s
->s3
.change_cipher_spec
) {
873 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
, SSL_R_GOT_A_FIN_BEFORE_A_CCS
);
874 return MSG_PROCESS_ERROR
;
876 s
->s3
.change_cipher_spec
= 0;
878 md_len
= s
->s3
.tmp
.peer_finish_md_len
;
880 if (md_len
!= PACKET_remaining(pkt
)) {
881 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_DIGEST_LENGTH
);
882 return MSG_PROCESS_ERROR
;
885 ok
= CRYPTO_memcmp(PACKET_data(pkt
), s
->s3
.tmp
.peer_finish_md
,
887 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
889 if ((PACKET_data(pkt
)[0] ^ s
->s3
.tmp
.peer_finish_md
[0]) != 0xFF) {
895 SSLfatal(s
, SSL_AD_DECRYPT_ERROR
, SSL_R_DIGEST_CHECK_FAILED
);
896 return MSG_PROCESS_ERROR
;
900 * Copy the finished so we can use it for renegotiation checks
902 if (!ossl_assert(md_len
<= EVP_MAX_MD_SIZE
)) {
903 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
904 return MSG_PROCESS_ERROR
;
907 memcpy(s
->s3
.previous_client_finished
, s
->s3
.tmp
.peer_finish_md
,
909 s
->s3
.previous_client_finished_len
= md_len
;
911 memcpy(s
->s3
.previous_server_finished
, s
->s3
.tmp
.peer_finish_md
,
913 s
->s3
.previous_server_finished_len
= md_len
;
917 * In TLS1.3 we also have to change cipher state and do any final processing
918 * of the initial server flight (if we are a client)
920 if (SSL_CONNECTION_IS_TLS13(s
)) {
922 if (s
->post_handshake_auth
!= SSL_PHA_REQUESTED
&&
923 !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
924 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
925 /* SSLfatal() already called */
926 return MSG_PROCESS_ERROR
;
929 /* TLS 1.3 gets the secret size from the handshake md */
931 if (!ssl
->method
->ssl3_enc
->generate_master_secret(s
,
932 s
->master_secret
, s
->handshake_secret
, 0,
934 /* SSLfatal() already called */
935 return MSG_PROCESS_ERROR
;
937 if (!tls13_store_server_finished_hash(s
)) {
938 /* SSLfatal() already called */
939 return MSG_PROCESS_ERROR
;
943 * For non-QUIC we set up the client's app data read keys now, so
944 * that we can go straight into reading 0.5RTT data from the server.
945 * For QUIC we don't do that, and instead defer setting up the keys
946 * until after we have set up the write keys in order to ensure that
947 * write keys are always set up before read keys (so that if we read
948 * a message we have the correct keys in place to ack it)
950 if (!SSL_IS_QUIC_HANDSHAKE(s
)
951 && !ssl
->method
->ssl3_enc
->change_cipher_state(s
,
952 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_CLIENT_READ
)) {
953 /* SSLfatal() already called */
954 return MSG_PROCESS_ERROR
;
956 if (!tls_process_initial_server_flight(s
)) {
957 /* SSLfatal() already called */
958 return MSG_PROCESS_ERROR
;
964 && !SSL_IS_FIRST_HANDSHAKE(s
)
965 && s
->rlayer
.rrlmethod
->set_first_handshake
!= NULL
)
966 s
->rlayer
.rrlmethod
->set_first_handshake(s
->rlayer
.rrl
, 0);
968 return MSG_PROCESS_FINISHED_READING
;
971 CON_FUNC_RETURN
tls_construct_change_cipher_spec(SSL_CONNECTION
*s
, WPACKET
*pkt
)
973 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
)) {
974 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
975 return CON_FUNC_ERROR
;
978 return CON_FUNC_SUCCESS
;
981 /* Add a certificate to the WPACKET */
982 static int ssl_add_cert_to_wpacket(SSL_CONNECTION
*s
, WPACKET
*pkt
,
983 X509
*x
, int chain
, int for_comp
)
986 unsigned char *outbytes
;
987 int context
= SSL_EXT_TLS1_3_CERTIFICATE
;
990 context
|= SSL_EXT_TLS1_3_CERTIFICATE_COMPRESSION
;
992 len
= i2d_X509(x
, NULL
);
995 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_BUF_LIB
);
998 if (!WPACKET_sub_allocate_bytes_u24(pkt
, len
, &outbytes
)
999 || i2d_X509(x
, &outbytes
) != len
) {
1001 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1005 if ((SSL_CONNECTION_IS_TLS13(s
) || for_comp
)
1006 && !tls_construct_extensions(s
, pkt
, context
, x
, chain
)) {
1007 /* SSLfatal() already called */
1014 /* Add certificate chain to provided WPACKET */
1015 static int ssl_add_cert_chain(SSL_CONNECTION
*s
, WPACKET
*pkt
, CERT_PKEY
*cpk
, int for_comp
)
1019 STACK_OF(X509
) *extra_certs
;
1020 STACK_OF(X509
) *chain
= NULL
;
1021 X509_STORE
*chain_store
;
1022 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1024 if (cpk
== NULL
|| cpk
->x509
== NULL
)
1030 * If we have a certificate specific chain use it, else use parent ctx.
1032 if (cpk
->chain
!= NULL
)
1033 extra_certs
= cpk
->chain
;
1035 extra_certs
= sctx
->extra_certs
;
1037 if ((s
->mode
& SSL_MODE_NO_AUTO_CHAIN
) || extra_certs
)
1039 else if (s
->cert
->chain_store
)
1040 chain_store
= s
->cert
->chain_store
;
1042 chain_store
= sctx
->cert_store
;
1044 if (chain_store
!= NULL
) {
1045 X509_STORE_CTX
*xs_ctx
= X509_STORE_CTX_new_ex(sctx
->libctx
,
1048 if (xs_ctx
== NULL
) {
1050 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_X509_LIB
);
1053 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, x
, NULL
)) {
1054 X509_STORE_CTX_free(xs_ctx
);
1056 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_X509_LIB
);
1060 * It is valid for the chain not to be complete (because normally we
1061 * don't include the root cert in the chain). Therefore we deliberately
1062 * ignore the error return from this call. We're not actually verifying
1063 * the cert - we're just building as much of the chain as we can
1065 (void)X509_verify_cert(xs_ctx
);
1066 /* Don't leave errors in the queue */
1068 chain
= X509_STORE_CTX_get0_chain(xs_ctx
);
1069 i
= ssl_security_cert_chain(s
, chain
, NULL
, 0);
1072 /* Dummy error calls so mkerr generates them */
1073 ERR_raise(ERR_LIB_SSL
, SSL_R_EE_KEY_TOO_SMALL
);
1074 ERR_raise(ERR_LIB_SSL
, SSL_R_CA_KEY_TOO_SMALL
);
1075 ERR_raise(ERR_LIB_SSL
, SSL_R_CA_MD_TOO_WEAK
);
1077 X509_STORE_CTX_free(xs_ctx
);
1079 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, i
);
1082 chain_count
= sk_X509_num(chain
);
1083 for (i
= 0; i
< chain_count
; i
++) {
1084 x
= sk_X509_value(chain
, i
);
1086 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
, for_comp
)) {
1087 /* SSLfatal() already called */
1088 X509_STORE_CTX_free(xs_ctx
);
1092 X509_STORE_CTX_free(xs_ctx
);
1094 i
= ssl_security_cert_chain(s
, extra_certs
, x
, 0);
1097 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, i
);
1100 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, 0, for_comp
)) {
1101 /* SSLfatal() already called */
1104 for (i
= 0; i
< sk_X509_num(extra_certs
); i
++) {
1105 x
= sk_X509_value(extra_certs
, i
);
1106 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
+ 1, for_comp
)) {
1107 /* SSLfatal() already called */
1115 EVP_PKEY
* tls_get_peer_pkey(const SSL_CONNECTION
*sc
)
1117 if (sc
->session
->peer_rpk
!= NULL
)
1118 return sc
->session
->peer_rpk
;
1119 if (sc
->session
->peer
!= NULL
)
1120 return X509_get0_pubkey(sc
->session
->peer
);
1124 int tls_process_rpk(SSL_CONNECTION
*sc
, PACKET
*pkt
, EVP_PKEY
**peer_rpk
)
1126 EVP_PKEY
*pkey
= NULL
;
1128 RAW_EXTENSION
*rawexts
= NULL
;
1131 unsigned long cert_len
= 0, spki_len
= 0;
1132 const unsigned char *spki
, *spkistart
;
1133 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(sc
);
1136 * ----------------------------
1137 * TLS 1.3 Certificate message:
1138 * ----------------------------
1139 * https://datatracker.ietf.org/doc/html/rfc8446#section-4.4.2
1145 * } CertificateType;
1148 * select (certificate_type) {
1149 * case RawPublicKey:
1150 * // From RFC 7250 ASN.1_subjectPublicKeyInfo
1151 * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
1154 * opaque cert_data<1..2^24-1>;
1156 * Extension extensions<0..2^16-1>;
1157 * } CertificateEntry;
1160 * opaque certificate_request_context<0..2^8-1>;
1161 * CertificateEntry certificate_list<0..2^24-1>;
1164 * The client MUST send a Certificate message if and only if the server
1165 * has requested client authentication via a CertificateRequest message
1166 * (Section 4.3.2). If the server requests client authentication but no
1167 * suitable certificate is available, the client MUST send a Certificate
1168 * message containing no certificates (i.e., with the "certificate_list"
1169 * field having length 0).
1171 * ----------------------------
1172 * TLS 1.2 Certificate message:
1173 * ----------------------------
1174 * https://datatracker.ietf.org/doc/html/rfc7250#section-3
1176 * opaque ASN.1Cert<1..2^24-1>;
1179 * select(certificate_type){
1181 * // certificate type defined in this document.
1182 * case RawPublicKey:
1183 * opaque ASN.1_subjectPublicKeyInfo<1..2^24-1>;
1185 * // X.509 certificate defined in RFC 5246
1187 * ASN.1Cert certificate_list<0..2^24-1>;
1189 * // Additional certificate type based on
1190 * // "TLS Certificate Types" subregistry
1197 * After the (TLS 1.3 only) context octet string (1 byte length + data) the
1198 * Certificate message has a 3-byte length that is zero in the client to
1199 * server message when the client has no RPK to send. In that case, there
1200 * are no (TLS 1.3 only) per-certificate extensions either, because the
1201 * [CertificateEntry] list is empty.
1203 * In the server to client direction, or when the client had an RPK to send,
1204 * the TLS 1.3 message just prepends the length of the RPK+extensions,
1205 * while TLS <= 1.2 sends just the RPK (octet-string).
1207 * The context must be zero-length in the server to client direction, and
1208 * must match the value recorded in the certificate request in the client
1209 * to server direction.
1211 if (SSL_CONNECTION_IS_TLS13(sc
)) {
1212 if (!PACKET_get_length_prefixed_1(pkt
, &context
)) {
1213 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
1217 if (sc
->pha_context
== NULL
) {
1218 if (PACKET_remaining(&context
) != 0) {
1219 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
1223 if (!PACKET_equal(&context
, sc
->pha_context
, sc
->pha_context_len
)) {
1224 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
1229 if (PACKET_remaining(&context
) != 0) {
1230 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_INVALID_CONTEXT
);
1236 if (!PACKET_get_net_3(pkt
, &cert_len
)
1237 || PACKET_remaining(pkt
) != cert_len
) {
1238 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1243 * The list length may be zero when there is no RPK. In the case of TLS
1244 * 1.2 this is actually the RPK length, which cannot be zero as specified,
1245 * but that breaks the ability of the client to decline client auth. We
1246 * overload the 0 RPK length to mean "no RPK". This interpretation is
1247 * also used some other (reference?) implementations, but is not supported
1248 * by the verbatim RFC7250 text.
1253 if (SSL_CONNECTION_IS_TLS13(sc
)) {
1255 * With TLS 1.3, a non-empty explicit-length RPK octet-string followed
1256 * by a possibly empty extension block.
1258 if (!PACKET_get_net_3(pkt
, &spki_len
)) {
1259 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1262 if (spki_len
== 0) {
1264 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_EMPTY_RAW_PUBLIC_KEY
);
1268 spki_len
= cert_len
;
1271 if (!PACKET_get_bytes(pkt
, &spki
, spki_len
)) {
1272 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1276 if ((pkey
= d2i_PUBKEY_ex(NULL
, &spki
, spki_len
, sctx
->libctx
, sctx
->propq
)) == NULL
1277 || spki
!= (spkistart
+ spki_len
)) {
1278 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1281 if (EVP_PKEY_missing_parameters(pkey
)) {
1282 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
,
1283 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1287 /* Process the Extensions block */
1288 if (SSL_CONNECTION_IS_TLS13(sc
)) {
1289 if (PACKET_remaining(pkt
) != (cert_len
- 3 - spki_len
)) {
1290 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_LENGTH
);
1293 if (!PACKET_as_length_prefixed_2(pkt
, &extensions
)
1294 || PACKET_remaining(pkt
) != 0) {
1295 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
1298 if (!tls_collect_extensions(sc
, &extensions
, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY
,
1299 &rawexts
, NULL
, 1)) {
1300 /* SSLfatal already called */
1303 /* chain index is always zero and fin always 1 for RPK */
1304 if (!tls_parse_all_extensions(sc
, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY
,
1305 rawexts
, NULL
, 0, 1)) {
1306 /* SSLfatal already called */
1311 if (peer_rpk
!= NULL
) {
1317 OPENSSL_free(rawexts
);
1318 EVP_PKEY_free(pkey
);
1322 unsigned long tls_output_rpk(SSL_CONNECTION
*sc
, WPACKET
*pkt
, CERT_PKEY
*cpk
)
1325 unsigned char *pdata
= NULL
;
1326 X509_PUBKEY
*xpk
= NULL
;
1327 unsigned long ret
= 0;
1330 if (cpk
!= NULL
&& cpk
->x509
!= NULL
) {
1332 /* Get the RPK from the certificate */
1333 xpk
= X509_get_X509_PUBKEY(cpk
->x509
);
1335 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1338 pdata_len
= i2d_X509_PUBKEY(xpk
, &pdata
);
1339 } else if (cpk
!= NULL
&& cpk
->privatekey
!= NULL
) {
1340 /* Get the RPK from the private key */
1341 pdata_len
= i2d_PUBKEY(cpk
->privatekey
, &pdata
);
1343 /* The server RPK is not optional */
1345 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1348 /* The client can send a zero length certificate list */
1349 if (!WPACKET_sub_memcpy_u24(pkt
, pdata
, pdata_len
)) {
1350 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1356 if (pdata_len
<= 0) {
1357 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1362 * TLSv1.2 is _just_ the raw public key
1363 * TLSv1.3 includes extensions, so there's a length wrapper
1365 if (SSL_CONNECTION_IS_TLS13(sc
)) {
1366 if (!WPACKET_start_sub_packet_u24(pkt
)) {
1367 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1372 if (!WPACKET_sub_memcpy_u24(pkt
, pdata
, pdata_len
)) {
1373 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1377 if (SSL_CONNECTION_IS_TLS13(sc
)) {
1379 * Only send extensions relevant to raw public keys. Until such
1380 * extensions are defined, this will be an empty set of extensions.
1381 * |x509| may be NULL, which raw public-key extensions need to handle.
1383 if (!tls_construct_extensions(sc
, pkt
, SSL_EXT_TLS1_3_RAW_PUBLIC_KEY
,
1385 /* SSLfatal() already called */
1388 if (!WPACKET_close(pkt
)) {
1389 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1396 OPENSSL_free(pdata
);
1400 unsigned long ssl3_output_cert_chain(SSL_CONNECTION
*s
, WPACKET
*pkt
,
1401 CERT_PKEY
*cpk
, int for_comp
)
1403 if (!WPACKET_start_sub_packet_u24(pkt
)) {
1405 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1409 if (!ssl_add_cert_chain(s
, pkt
, cpk
, for_comp
))
1412 if (!WPACKET_close(pkt
)) {
1414 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1422 * Tidy up after the end of a handshake. In the case of SCTP this may result
1423 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
1426 WORK_STATE
tls_finish_handshake(SSL_CONNECTION
*s
, ossl_unused WORK_STATE wst
,
1427 int clearbufs
, int stop
)
1429 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
1430 int cleanuphand
= s
->statem
.cleanuphand
;
1431 SSL
*ssl
= SSL_CONNECTION_GET_USER_SSL(s
);
1432 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1435 if (!SSL_CONNECTION_IS_DTLS(s
)
1436 #ifndef OPENSSL_NO_SCTP
1438 * RFC6083: SCTP provides a reliable and in-sequence transport service for DTLS
1439 * messages that require it. Therefore, DTLS procedures for retransmissions
1441 * Hence the init_buf can be cleared when DTLS over SCTP as transport is used.
1443 || BIO_dgram_is_sctp(SSL_get_wbio(SSL_CONNECTION_GET_SSL(s
)))
1447 * We don't do this in DTLS over UDP because we may still need the init_buf
1448 * in case there are any unexpected retransmits
1450 BUF_MEM_free(s
->init_buf
);
1454 if (!ssl_free_wbio_buffer(s
)) {
1455 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
1461 if (SSL_CONNECTION_IS_TLS13(s
) && !s
->server
1462 && s
->post_handshake_auth
== SSL_PHA_REQUESTED
)
1463 s
->post_handshake_auth
= SSL_PHA_EXT_SENT
;
1466 * Only set if there was a Finished message and this isn't after a TLSv1.3
1467 * post handshake exchange
1470 /* skipped if we just sent a HelloRequest */
1473 s
->statem
.cleanuphand
= 0;
1474 s
->ext
.ticket_expected
= 0;
1476 ssl3_cleanup_key_block(s
);
1480 * In TLSv1.3 we update the cache as part of constructing the
1483 if (!SSL_CONNECTION_IS_TLS13(s
))
1484 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
1486 /* N.B. s->ctx may not equal s->session_ctx */
1487 ssl_tsan_counter(sctx
, &sctx
->stats
.sess_accept_good
);
1488 s
->handshake_func
= ossl_statem_accept
;
1490 if (SSL_CONNECTION_IS_TLS13(s
)) {
1492 * We encourage applications to only use TLSv1.3 tickets once,
1493 * so we remove this one from the cache.
1495 if ((s
->session_ctx
->session_cache_mode
1496 & SSL_SESS_CACHE_CLIENT
) != 0)
1497 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1500 * In TLSv1.3 we update the cache as part of processing the
1503 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
1506 ssl_tsan_counter(s
->session_ctx
,
1507 &s
->session_ctx
->stats
.sess_hit
);
1509 s
->handshake_func
= ossl_statem_connect
;
1510 ssl_tsan_counter(s
->session_ctx
,
1511 &s
->session_ctx
->stats
.sess_connect_good
);
1514 if (SSL_CONNECTION_IS_DTLS(s
)) {
1515 /* done with handshaking */
1516 s
->d1
->handshake_read_seq
= 0;
1517 s
->d1
->handshake_write_seq
= 0;
1518 s
->d1
->next_handshake_write_seq
= 0;
1519 dtls1_clear_received_buffer(s
);
1523 if (s
->info_callback
!= NULL
)
1524 cb
= s
->info_callback
;
1525 else if (sctx
->info_callback
!= NULL
)
1526 cb
= sctx
->info_callback
;
1528 /* The callback may expect us to not be in init at handshake done */
1529 ossl_statem_set_in_init(s
, 0);
1533 || !SSL_CONNECTION_IS_TLS13(s
)
1534 || SSL_IS_FIRST_HANDSHAKE(s
))
1535 cb(ssl
, SSL_CB_HANDSHAKE_DONE
, 1);
1539 /* If we've got more work to do we go back into init */
1540 ossl_statem_set_in_init(s
, 1);
1541 return WORK_FINISHED_CONTINUE
;
1544 return WORK_FINISHED_STOP
;
1547 int tls_get_message_header(SSL_CONNECTION
*s
, int *mt
)
1549 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1550 int skip_message
, i
;
1553 size_t l
, readbytes
;
1554 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1555 SSL
*ussl
= SSL_CONNECTION_GET_USER_SSL(s
);
1557 p
= (unsigned char *)s
->init_buf
->data
;
1560 while (s
->init_num
< SSL3_HM_HEADER_LENGTH
) {
1561 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, &recvd_type
,
1563 SSL3_HM_HEADER_LENGTH
- s
->init_num
,
1566 s
->rwstate
= SSL_READING
;
1569 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
1571 * A ChangeCipherSpec must be a single byte and may not occur
1572 * in the middle of a handshake message.
1574 if (s
->init_num
!= 0 || readbytes
!= 1 || p
[0] != SSL3_MT_CCS
) {
1575 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1576 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
1579 if (s
->statem
.hand_state
== TLS_ST_BEFORE
1580 && (s
->s3
.flags
& TLS1_FLAGS_STATELESS
) != 0) {
1582 * We are stateless and we received a CCS. Probably this is
1583 * from a client between the first and second ClientHellos.
1584 * We should ignore this, but return an error because we do
1585 * not return success until we see the second ClientHello
1586 * with a valid cookie.
1590 s
->s3
.tmp
.message_type
= *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1591 s
->init_num
= readbytes
- 1;
1592 s
->init_msg
= s
->init_buf
->data
;
1593 s
->s3
.tmp
.message_size
= readbytes
;
1595 } else if (recvd_type
!= SSL3_RT_HANDSHAKE
) {
1596 SSLfatal(s
, SSL_AD_UNEXPECTED_MESSAGE
,
1597 SSL_R_CCS_RECEIVED_EARLY
);
1600 s
->init_num
+= readbytes
;
1605 if (s
->statem
.hand_state
!= TLS_ST_OK
1606 && p
[0] == SSL3_MT_HELLO_REQUEST
)
1608 * The server may always send 'Hello Request' messages --
1609 * we are doing a handshake anyway now, so ignore them if
1610 * their format is correct. Does not count for 'Finished'
1613 if (p
[1] == 0 && p
[2] == 0 && p
[3] == 0) {
1617 if (s
->msg_callback
)
1618 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
1619 p
, SSL3_HM_HEADER_LENGTH
, ussl
,
1620 s
->msg_callback_arg
);
1622 } while (skip_message
);
1623 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1626 s
->s3
.tmp
.message_type
= *(p
++);
1628 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
1630 * Only happens with SSLv3+ in an SSLv2 backward compatible
1633 * Total message size is the remaining record bytes to read
1634 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1636 l
= s
->rlayer
.tlsrecs
[0].length
+ SSL3_HM_HEADER_LENGTH
;
1637 s
->s3
.tmp
.message_size
= l
;
1639 s
->init_msg
= s
->init_buf
->data
;
1640 s
->init_num
= SSL3_HM_HEADER_LENGTH
;
1643 /* BUF_MEM_grow takes an 'int' parameter */
1644 if (l
> (INT_MAX
- SSL3_HM_HEADER_LENGTH
)) {
1645 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
1646 SSL_R_EXCESSIVE_MESSAGE_SIZE
);
1649 s
->s3
.tmp
.message_size
= l
;
1651 s
->init_msg
= s
->init_buf
->data
+ SSL3_HM_HEADER_LENGTH
;
1658 int tls_get_message_body(SSL_CONNECTION
*s
, size_t *len
)
1660 size_t n
, readbytes
;
1663 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
1664 SSL
*ussl
= SSL_CONNECTION_GET_USER_SSL(s
);
1666 if (s
->s3
.tmp
.message_type
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1667 /* We've already read everything in */
1668 *len
= (unsigned long)s
->init_num
;
1673 n
= s
->s3
.tmp
.message_size
- s
->init_num
;
1675 i
= ssl
->method
->ssl_read_bytes(ssl
, SSL3_RT_HANDSHAKE
, NULL
,
1676 &p
[s
->init_num
], n
, 0, &readbytes
);
1678 s
->rwstate
= SSL_READING
;
1682 s
->init_num
+= readbytes
;
1687 * If receiving Finished, record MAC of prior handshake messages for
1688 * Finished verification.
1690 if (*(s
->init_buf
->data
) == SSL3_MT_FINISHED
&& !ssl3_take_mac(s
)) {
1691 /* SSLfatal() already called */
1696 /* Feed this message into MAC computation. */
1697 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
1698 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1700 /* SSLfatal() already called */
1704 if (s
->msg_callback
)
1705 s
->msg_callback(0, SSL2_VERSION
, 0, s
->init_buf
->data
,
1706 (size_t)s
->init_num
, ussl
, s
->msg_callback_arg
);
1709 * We defer feeding in the HRR until later. We'll do it as part of
1710 * processing the message
1711 * The TLsv1.3 handshake transcript stops at the ClientFinished
1714 #define SERVER_HELLO_RANDOM_OFFSET (SSL3_HM_HEADER_LENGTH + 2)
1715 /* KeyUpdate and NewSessionTicket do not need to be added */
1716 if (!SSL_CONNECTION_IS_TLS13(s
)
1717 || (s
->s3
.tmp
.message_type
!= SSL3_MT_NEWSESSION_TICKET
1718 && s
->s3
.tmp
.message_type
!= SSL3_MT_KEY_UPDATE
)) {
1719 if (s
->s3
.tmp
.message_type
!= SSL3_MT_SERVER_HELLO
1720 || s
->init_num
< SERVER_HELLO_RANDOM_OFFSET
+ SSL3_RANDOM_SIZE
1721 || memcmp(hrrrandom
,
1722 s
->init_buf
->data
+ SERVER_HELLO_RANDOM_OFFSET
,
1723 SSL3_RANDOM_SIZE
) != 0) {
1724 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1725 s
->init_num
+ SSL3_HM_HEADER_LENGTH
)) {
1726 /* SSLfatal() already called */
1732 if (s
->msg_callback
)
1733 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
, s
->init_buf
->data
,
1734 (size_t)s
->init_num
+ SSL3_HM_HEADER_LENGTH
, ussl
,
1735 s
->msg_callback_arg
);
1742 static const X509ERR2ALERT x509table
[] = {
1743 {X509_V_ERR_APPLICATION_VERIFICATION
, SSL_AD_HANDSHAKE_FAILURE
},
1744 {X509_V_ERR_CA_KEY_TOO_SMALL
, SSL_AD_BAD_CERTIFICATE
},
1745 {X509_V_ERR_EC_KEY_EXPLICIT_PARAMS
, SSL_AD_BAD_CERTIFICATE
},
1746 {X509_V_ERR_CA_MD_TOO_WEAK
, SSL_AD_BAD_CERTIFICATE
},
1747 {X509_V_ERR_CERT_CHAIN_TOO_LONG
, SSL_AD_UNKNOWN_CA
},
1748 {X509_V_ERR_CERT_HAS_EXPIRED
, SSL_AD_CERTIFICATE_EXPIRED
},
1749 {X509_V_ERR_CERT_NOT_YET_VALID
, SSL_AD_BAD_CERTIFICATE
},
1750 {X509_V_ERR_CERT_REJECTED
, SSL_AD_BAD_CERTIFICATE
},
1751 {X509_V_ERR_CERT_REVOKED
, SSL_AD_CERTIFICATE_REVOKED
},
1752 {X509_V_ERR_CERT_SIGNATURE_FAILURE
, SSL_AD_DECRYPT_ERROR
},
1753 {X509_V_ERR_CERT_UNTRUSTED
, SSL_AD_BAD_CERTIFICATE
},
1754 {X509_V_ERR_CRL_HAS_EXPIRED
, SSL_AD_CERTIFICATE_EXPIRED
},
1755 {X509_V_ERR_CRL_NOT_YET_VALID
, SSL_AD_BAD_CERTIFICATE
},
1756 {X509_V_ERR_CRL_SIGNATURE_FAILURE
, SSL_AD_DECRYPT_ERROR
},
1757 {X509_V_ERR_DANE_NO_MATCH
, SSL_AD_BAD_CERTIFICATE
},
1758 {X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
, SSL_AD_UNKNOWN_CA
},
1759 {X509_V_ERR_EE_KEY_TOO_SMALL
, SSL_AD_BAD_CERTIFICATE
},
1760 {X509_V_ERR_EMAIL_MISMATCH
, SSL_AD_BAD_CERTIFICATE
},
1761 {X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
, SSL_AD_BAD_CERTIFICATE
},
1762 {X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
, SSL_AD_BAD_CERTIFICATE
},
1763 {X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
, SSL_AD_BAD_CERTIFICATE
},
1764 {X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
, SSL_AD_BAD_CERTIFICATE
},
1765 {X509_V_ERR_HOSTNAME_MISMATCH
, SSL_AD_BAD_CERTIFICATE
},
1766 {X509_V_ERR_INVALID_CA
, SSL_AD_UNKNOWN_CA
},
1767 {X509_V_ERR_INVALID_CALL
, SSL_AD_INTERNAL_ERROR
},
1768 {X509_V_ERR_INVALID_PURPOSE
, SSL_AD_UNSUPPORTED_CERTIFICATE
},
1769 {X509_V_ERR_IP_ADDRESS_MISMATCH
, SSL_AD_BAD_CERTIFICATE
},
1770 {X509_V_ERR_OUT_OF_MEM
, SSL_AD_INTERNAL_ERROR
},
1771 {X509_V_ERR_PATH_LENGTH_EXCEEDED
, SSL_AD_UNKNOWN_CA
},
1772 {X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
, SSL_AD_UNKNOWN_CA
},
1773 {X509_V_ERR_STORE_LOOKUP
, SSL_AD_INTERNAL_ERROR
},
1774 {X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
, SSL_AD_BAD_CERTIFICATE
},
1775 {X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
, SSL_AD_BAD_CERTIFICATE
},
1776 {X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
, SSL_AD_BAD_CERTIFICATE
},
1777 {X509_V_ERR_UNABLE_TO_GET_CRL
, SSL_AD_UNKNOWN_CA
},
1778 {X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
, SSL_AD_UNKNOWN_CA
},
1779 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
, SSL_AD_UNKNOWN_CA
},
1780 {X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
, SSL_AD_UNKNOWN_CA
},
1781 {X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
, SSL_AD_UNKNOWN_CA
},
1782 {X509_V_ERR_UNSPECIFIED
, SSL_AD_INTERNAL_ERROR
},
1784 /* Last entry; return this if we don't find the value above. */
1785 {X509_V_OK
, SSL_AD_CERTIFICATE_UNKNOWN
}
1788 int ssl_x509err2alert(int x509err
)
1790 const X509ERR2ALERT
*tp
;
1792 for (tp
= x509table
; tp
->x509err
!= X509_V_OK
; ++tp
)
1793 if (tp
->x509err
== x509err
)
1798 int ssl_allow_compression(SSL_CONNECTION
*s
)
1800 if (s
->options
& SSL_OP_NO_COMPRESSION
)
1802 return ssl_security(s
, SSL_SECOP_COMPRESSION
, 0, 0, NULL
);
1806 * SSL/TLS/DTLS version comparison
1809 * 0 if versiona is equal to versionb
1810 * 1 if versiona is greater than versionb
1811 * -1 if versiona is less than versionb
1813 int ssl_version_cmp(const SSL_CONNECTION
*s
, int versiona
, int versionb
)
1815 int dtls
= SSL_CONNECTION_IS_DTLS(s
);
1817 if (versiona
== versionb
)
1820 return versiona
< versionb
? -1 : 1;
1821 return DTLS_VERSION_LT(versiona
, versionb
) ? -1 : 1;
1826 const SSL_METHOD
*(*cmeth
) (void);
1827 const SSL_METHOD
*(*smeth
) (void);
1830 #if TLS_MAX_VERSION_INTERNAL != TLS1_3_VERSION
1831 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1834 /* Must be in order high to low */
1835 static const version_info tls_version_table
[] = {
1836 #ifndef OPENSSL_NO_TLS1_3
1837 {TLS1_3_VERSION
, tlsv1_3_client_method
, tlsv1_3_server_method
},
1839 {TLS1_3_VERSION
, NULL
, NULL
},
1841 #ifndef OPENSSL_NO_TLS1_2
1842 {TLS1_2_VERSION
, tlsv1_2_client_method
, tlsv1_2_server_method
},
1844 {TLS1_2_VERSION
, NULL
, NULL
},
1846 #ifndef OPENSSL_NO_TLS1_1
1847 {TLS1_1_VERSION
, tlsv1_1_client_method
, tlsv1_1_server_method
},
1849 {TLS1_1_VERSION
, NULL
, NULL
},
1851 #ifndef OPENSSL_NO_TLS1
1852 {TLS1_VERSION
, tlsv1_client_method
, tlsv1_server_method
},
1854 {TLS1_VERSION
, NULL
, NULL
},
1856 #ifndef OPENSSL_NO_SSL3
1857 {SSL3_VERSION
, sslv3_client_method
, sslv3_server_method
},
1859 {SSL3_VERSION
, NULL
, NULL
},
1864 #if DTLS_MAX_VERSION_INTERNAL != DTLS1_2_VERSION
1865 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1868 /* Must be in order high to low */
1869 static const version_info dtls_version_table
[] = {
1870 #ifndef OPENSSL_NO_DTLS1_2
1871 {DTLS1_2_VERSION
, dtlsv1_2_client_method
, dtlsv1_2_server_method
},
1873 {DTLS1_2_VERSION
, NULL
, NULL
},
1875 #ifndef OPENSSL_NO_DTLS1
1876 {DTLS1_VERSION
, dtlsv1_client_method
, dtlsv1_server_method
},
1877 {DTLS1_BAD_VER
, dtls_bad_ver_client_method
, NULL
},
1879 {DTLS1_VERSION
, NULL
, NULL
},
1880 {DTLS1_BAD_VER
, NULL
, NULL
},
1886 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1888 * @s: The SSL handle for the candidate method
1889 * @method: the intended method.
1891 * Returns 0 on success, or an SSL error reason on failure.
1893 static int ssl_method_error(const SSL_CONNECTION
*s
, const SSL_METHOD
*method
)
1895 int version
= method
->version
;
1897 if ((s
->min_proto_version
!= 0 &&
1898 ssl_version_cmp(s
, version
, s
->min_proto_version
) < 0) ||
1899 ssl_security(s
, SSL_SECOP_VERSION
, 0, version
, NULL
) == 0)
1900 return SSL_R_VERSION_TOO_LOW
;
1902 if (s
->max_proto_version
!= 0 &&
1903 ssl_version_cmp(s
, version
, s
->max_proto_version
) > 0)
1904 return SSL_R_VERSION_TOO_HIGH
;
1906 if ((s
->options
& method
->mask
) != 0)
1907 return SSL_R_UNSUPPORTED_PROTOCOL
;
1908 if ((method
->flags
& SSL_METHOD_NO_SUITEB
) != 0 && tls1_suiteb(s
))
1909 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE
;
1915 * Only called by servers. Returns 1 if the server has a TLSv1.3 capable
1916 * certificate type, or has PSK or a certificate callback configured, or has
1917 * a servername callback configure. Otherwise returns 0.
1919 static int is_tls13_capable(const SSL_CONNECTION
*s
)
1923 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
1925 if (!ossl_assert(sctx
!= NULL
) || !ossl_assert(s
->session_ctx
!= NULL
))
1929 * A servername callback can change the available certs, so if a servername
1930 * cb is set then we just assume TLSv1.3 will be ok
1932 if (sctx
->ext
.servername_cb
!= NULL
1933 || s
->session_ctx
->ext
.servername_cb
!= NULL
)
1936 #ifndef OPENSSL_NO_PSK
1937 if (s
->psk_server_callback
!= NULL
)
1941 if (s
->psk_find_session_cb
!= NULL
|| s
->cert
->cert_cb
!= NULL
)
1944 /* All provider-based sig algs are required to support at least TLS1.3 */
1945 for (i
= 0; i
< s
->ssl_pkey_num
; i
++) {
1946 /* Skip over certs disallowed for TLSv1.3 */
1948 case SSL_PKEY_DSA_SIGN
:
1949 case SSL_PKEY_GOST01
:
1950 case SSL_PKEY_GOST12_256
:
1951 case SSL_PKEY_GOST12_512
:
1956 if (!ssl_has_cert(s
, (int)i
))
1958 if (i
!= SSL_PKEY_ECC
)
1961 * Prior to TLSv1.3 sig algs allowed any curve to be used. TLSv1.3 is
1962 * more restrictive so check that our sig algs are consistent with this
1963 * EC cert. See section 4.2.3 of RFC8446.
1965 curve
= ssl_get_EC_curve_nid(s
->cert
->pkeys
[SSL_PKEY_ECC
].privatekey
);
1966 if (tls_check_sigalg_curve(s
, curve
))
1974 * ssl_version_supported - Check that the specified `version` is supported by
1977 * @s: The SSL handle for the candidate method
1978 * @version: Protocol version to test against
1980 * Returns 1 when supported, otherwise 0
1982 int ssl_version_supported(const SSL_CONNECTION
*s
, int version
,
1983 const SSL_METHOD
**meth
)
1985 const version_info
*vent
;
1986 const version_info
*table
;
1988 switch (SSL_CONNECTION_GET_SSL(s
)->method
->version
) {
1990 /* Version should match method version for non-ANY method */
1991 return ssl_version_cmp(s
, version
, s
->version
) == 0;
1992 case TLS_ANY_VERSION
:
1993 table
= tls_version_table
;
1995 case DTLS_ANY_VERSION
:
1996 table
= dtls_version_table
;
2001 vent
->version
!= 0 && ssl_version_cmp(s
, version
, vent
->version
) <= 0;
2003 const SSL_METHOD
*(*thismeth
)(void) = s
->server
? vent
->smeth
2006 if (thismeth
!= NULL
2007 && ssl_version_cmp(s
, version
, vent
->version
) == 0
2008 && ssl_method_error(s
, thismeth()) == 0
2010 || version
!= TLS1_3_VERSION
2011 || is_tls13_capable(s
))) {
2021 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
2022 * fallback indication from a client check whether we're using the highest
2023 * supported protocol version.
2025 * @s server SSL handle.
2027 * Returns 1 when using the highest enabled version, 0 otherwise.
2029 int ssl_check_version_downgrade(SSL_CONNECTION
*s
)
2031 const version_info
*vent
;
2032 const version_info
*table
;
2033 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2036 * Check that the current protocol is the highest enabled version
2037 * (according to ssl->defltmethod, as version negotiation may have changed
2040 if (s
->version
== ssl
->defltmeth
->version
)
2044 * Apparently we're using a version-flexible SSL_METHOD (not at its
2045 * highest protocol version).
2047 if (ssl
->defltmeth
->version
== TLS_method()->version
)
2048 table
= tls_version_table
;
2049 else if (ssl
->defltmeth
->version
== DTLS_method()->version
)
2050 table
= dtls_version_table
;
2052 /* Unexpected state; fail closed. */
2056 for (vent
= table
; vent
->version
!= 0; ++vent
) {
2057 if (vent
->smeth
!= NULL
&& ssl_method_error(s
, vent
->smeth()) == 0)
2058 return s
->version
== vent
->version
;
2064 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
2065 * protocols, provided the initial (D)TLS method is version-flexible. This
2066 * function sanity-checks the proposed value and makes sure the method is
2067 * version-flexible, then sets the limit if all is well.
2069 * @method_version: The version of the current SSL_METHOD.
2070 * @version: the intended limit.
2071 * @bound: pointer to limit to be updated.
2073 * Returns 1 on success, 0 on failure.
2075 int ssl_set_version_bound(int method_version
, int version
, int *bound
)
2085 valid_tls
= version
>= SSL3_VERSION
&& version
<= TLS_MAX_VERSION_INTERNAL
;
2087 /* We support client side pre-standardisation version of DTLS */
2088 (version
== DTLS1_BAD_VER
)
2089 || (DTLS_VERSION_LE(version
, DTLS_MAX_VERSION_INTERNAL
)
2090 && DTLS_VERSION_GE(version
, DTLS1_VERSION
));
2092 if (!valid_tls
&& !valid_dtls
)
2096 * Restrict TLS methods to TLS protocol versions.
2097 * Restrict DTLS methods to DTLS protocol versions.
2098 * Note, DTLS version numbers are decreasing, use comparison macros.
2100 * Note that for both lower-bounds we use explicit versions, not
2101 * (D)TLS_MIN_VERSION. This is because we don't want to break user
2102 * configurations. If the MIN (supported) version ever rises, the user's
2103 * "floor" remains valid even if no longer available. We don't expect the
2104 * MAX ceiling to ever get lower, so making that variable makes sense.
2106 * We ignore attempts to set bounds on version-inflexible methods,
2107 * returning success.
2109 switch (method_version
) {
2113 case TLS_ANY_VERSION
:
2118 case DTLS_ANY_VERSION
:
2126 static void check_for_downgrade(SSL_CONNECTION
*s
, int vers
, DOWNGRADE
*dgrd
)
2128 if (vers
== TLS1_2_VERSION
2129 && ssl_version_supported(s
, TLS1_3_VERSION
, NULL
)) {
2130 *dgrd
= DOWNGRADE_TO_1_2
;
2131 } else if (!SSL_CONNECTION_IS_DTLS(s
)
2132 && vers
< TLS1_2_VERSION
2134 * We need to ensure that a server that disables TLSv1.2
2135 * (creating a hole between TLSv1.3 and TLSv1.1) can still
2136 * complete handshakes with clients that support TLSv1.2 and
2137 * below. Therefore we do not enable the sentinel if TLSv1.3 is
2138 * enabled and TLSv1.2 is not.
2140 && ssl_version_supported(s
, TLS1_2_VERSION
, NULL
)) {
2141 *dgrd
= DOWNGRADE_TO_1_1
;
2143 *dgrd
= DOWNGRADE_NONE
;
2148 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
2149 * client HELLO is received to select the final server protocol version and
2150 * the version specific method.
2152 * @s: server SSL handle.
2154 * Returns 0 on success or an SSL error reason number on failure.
2156 int ssl_choose_server_version(SSL_CONNECTION
*s
, CLIENTHELLO_MSG
*hello
,
2160 * With version-flexible methods we have an initial state with:
2162 * s->method->version == (D)TLS_ANY_VERSION,
2163 * s->version == (D)TLS_MAX_VERSION_INTERNAL.
2165 * So we detect version-flexible methods via the method version, not the
2168 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2169 int server_version
= ssl
->method
->version
;
2170 int client_version
= hello
->legacy_version
;
2171 const version_info
*vent
;
2172 const version_info
*table
;
2174 RAW_EXTENSION
*suppversions
;
2176 s
->client_version
= client_version
;
2178 switch (server_version
) {
2180 if (!SSL_CONNECTION_IS_TLS13(s
)) {
2181 if (ssl_version_cmp(s
, client_version
, s
->version
) < 0)
2182 return SSL_R_WRONG_SSL_VERSION
;
2183 *dgrd
= DOWNGRADE_NONE
;
2185 * If this SSL handle is not from a version flexible method we don't
2186 * (and never did) check min/max FIPS or Suite B constraints. Hope
2187 * that's OK. It is up to the caller to not choose fixed protocol
2188 * versions they don't want. If not, then easy to fix, just return
2189 * ssl_method_error(s, s->method)
2194 * Fall through if we are TLSv1.3 already (this means we must be after
2195 * a HelloRetryRequest
2198 case TLS_ANY_VERSION
:
2199 table
= tls_version_table
;
2201 case DTLS_ANY_VERSION
:
2202 table
= dtls_version_table
;
2206 suppversions
= &hello
->pre_proc_exts
[TLSEXT_IDX_supported_versions
];
2208 /* If we did an HRR then supported versions is mandatory */
2209 if (!suppversions
->present
&& s
->hello_retry_request
!= SSL_HRR_NONE
)
2210 return SSL_R_UNSUPPORTED_PROTOCOL
;
2212 if (suppversions
->present
&& !SSL_CONNECTION_IS_DTLS(s
)) {
2213 unsigned int candidate_vers
= 0;
2214 unsigned int best_vers
= 0;
2215 const SSL_METHOD
*best_method
= NULL
;
2216 PACKET versionslist
;
2218 suppversions
->parsed
= 1;
2220 if (!PACKET_as_length_prefixed_1(&suppversions
->data
, &versionslist
)) {
2221 /* Trailing or invalid data? */
2222 return SSL_R_LENGTH_MISMATCH
;
2226 * The TLSv1.3 spec says the client MUST set this to TLS1_2_VERSION.
2227 * The spec only requires servers to check that it isn't SSLv3:
2228 * "Any endpoint receiving a Hello message with
2229 * ClientHello.legacy_version or ServerHello.legacy_version set to
2230 * 0x0300 MUST abort the handshake with a "protocol_version" alert."
2231 * We are slightly stricter and require that it isn't SSLv3 or lower.
2232 * We tolerate TLSv1 and TLSv1.1.
2234 if (client_version
<= SSL3_VERSION
)
2235 return SSL_R_BAD_LEGACY_VERSION
;
2237 while (PACKET_get_net_2(&versionslist
, &candidate_vers
)) {
2238 if (ssl_version_cmp(s
, candidate_vers
, best_vers
) <= 0)
2240 if (ssl_version_supported(s
, candidate_vers
, &best_method
))
2241 best_vers
= candidate_vers
;
2243 if (PACKET_remaining(&versionslist
) != 0) {
2244 /* Trailing data? */
2245 return SSL_R_LENGTH_MISMATCH
;
2248 if (best_vers
> 0) {
2249 if (s
->hello_retry_request
!= SSL_HRR_NONE
) {
2251 * This is after a HelloRetryRequest so we better check that we
2252 * negotiated TLSv1.3
2254 if (best_vers
!= TLS1_3_VERSION
)
2255 return SSL_R_UNSUPPORTED_PROTOCOL
;
2258 check_for_downgrade(s
, best_vers
, dgrd
);
2259 s
->version
= best_vers
;
2260 ssl
->method
= best_method
;
2261 if (!ssl_set_record_protocol_version(s
, best_vers
))
2262 return ERR_R_INTERNAL_ERROR
;
2266 return SSL_R_UNSUPPORTED_PROTOCOL
;
2270 * If the supported versions extension isn't present, then the highest
2271 * version we can negotiate is TLSv1.2
2273 if (ssl_version_cmp(s
, client_version
, TLS1_3_VERSION
) >= 0)
2274 client_version
= TLS1_2_VERSION
;
2277 * No supported versions extension, so we just use the version supplied in
2280 for (vent
= table
; vent
->version
!= 0; ++vent
) {
2281 const SSL_METHOD
*method
;
2283 if (vent
->smeth
== NULL
||
2284 ssl_version_cmp(s
, client_version
, vent
->version
) < 0)
2286 method
= vent
->smeth();
2287 if (ssl_method_error(s
, method
) == 0) {
2288 check_for_downgrade(s
, vent
->version
, dgrd
);
2289 s
->version
= vent
->version
;
2290 ssl
->method
= method
;
2291 if (!ssl_set_record_protocol_version(s
, s
->version
))
2292 return ERR_R_INTERNAL_ERROR
;
2298 return disabled
? SSL_R_UNSUPPORTED_PROTOCOL
: SSL_R_VERSION_TOO_LOW
;
2302 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
2303 * server HELLO is received to select the final client protocol version and
2304 * the version specific method.
2306 * @s: client SSL handle.
2307 * @version: The proposed version from the server's HELLO.
2308 * @extensions: The extensions received
2310 * Returns 1 on success or 0 on error.
2312 int ssl_choose_client_version(SSL_CONNECTION
*s
, int version
,
2313 RAW_EXTENSION
*extensions
)
2315 const version_info
*vent
;
2316 const version_info
*table
;
2317 int ret
, ver_min
, ver_max
, real_max
, origv
;
2318 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2321 s
->version
= version
;
2323 /* This will overwrite s->version if the extension is present */
2324 if (!tls_parse_extension(s
, TLSEXT_IDX_supported_versions
,
2325 SSL_EXT_TLS1_2_SERVER_HELLO
2326 | SSL_EXT_TLS1_3_SERVER_HELLO
, extensions
,
2332 if (s
->hello_retry_request
!= SSL_HRR_NONE
2333 && s
->version
!= TLS1_3_VERSION
) {
2335 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_WRONG_SSL_VERSION
);
2339 switch (ssl
->method
->version
) {
2341 if (s
->version
!= ssl
->method
->version
) {
2343 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_WRONG_SSL_VERSION
);
2347 * If this SSL handle is not from a version flexible method we don't
2348 * (and never did) check min/max, FIPS or Suite B constraints. Hope
2349 * that's OK. It is up to the caller to not choose fixed protocol
2350 * versions they don't want. If not, then easy to fix, just return
2351 * ssl_method_error(s, s->method)
2353 if (!ssl_set_record_protocol_version(s
, s
->version
)) {
2354 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2358 case TLS_ANY_VERSION
:
2359 table
= tls_version_table
;
2361 case DTLS_ANY_VERSION
:
2362 table
= dtls_version_table
;
2366 ret
= ssl_get_min_max_version(s
, &ver_min
, &ver_max
, &real_max
);
2369 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, ret
);
2372 if (ssl_version_cmp(s
, s
->version
, ver_min
) < 0
2373 || ssl_version_cmp(s
, s
->version
, ver_max
) > 0) {
2375 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_UNSUPPORTED_PROTOCOL
);
2379 if ((s
->mode
& SSL_MODE_SEND_FALLBACK_SCSV
) == 0)
2382 /* Check for downgrades */
2383 /* TODO(DTLSv1.3): Update this code for DTLSv1.3 */
2384 if (!SSL_CONNECTION_IS_DTLS(s
) && real_max
> s
->version
) {
2385 /* Signal applies to all versions */
2386 if (memcmp(tls11downgrade
,
2387 s
->s3
.server_random
+ SSL3_RANDOM_SIZE
2388 - sizeof(tls11downgrade
),
2389 sizeof(tls11downgrade
)) == 0) {
2391 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2392 SSL_R_INAPPROPRIATE_FALLBACK
);
2395 /* Only when accepting TLS1.3 */
2396 if (real_max
== TLS1_3_VERSION
2397 && memcmp(tls12downgrade
,
2398 s
->s3
.server_random
+ SSL3_RANDOM_SIZE
2399 - sizeof(tls12downgrade
),
2400 sizeof(tls12downgrade
)) == 0) {
2402 SSLfatal(s
, SSL_AD_ILLEGAL_PARAMETER
,
2403 SSL_R_INAPPROPRIATE_FALLBACK
);
2408 for (vent
= table
; vent
->version
!= 0; ++vent
) {
2409 if (vent
->cmeth
== NULL
|| s
->version
!= vent
->version
)
2412 ssl
->method
= vent
->cmeth();
2413 if (!ssl_set_record_protocol_version(s
, s
->version
)) {
2414 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2421 SSLfatal(s
, SSL_AD_PROTOCOL_VERSION
, SSL_R_UNSUPPORTED_PROTOCOL
);
2426 * ssl_get_min_max_version - get minimum and maximum protocol version
2427 * @s: The SSL connection
2428 * @min_version: The minimum supported version
2429 * @max_version: The maximum supported version
2430 * @real_max: The highest version below the lowest compile time version hole
2431 * where that hole lies above at least one run-time enabled
2434 * Work out what version we should be using for the initial ClientHello if the
2435 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
2436 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
2437 * constraints and any floor imposed by the security level here,
2438 * so we don't advertise the wrong protocol version to only reject the outcome later.
2440 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
2441 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
2442 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
2444 * Returns 0 on success or an SSL error reason number on failure. On failure
2445 * min_version and max_version will also be set to 0.
2447 int ssl_get_min_max_version(const SSL_CONNECTION
*s
, int *min_version
,
2448 int *max_version
, int *real_max
)
2450 int version
, tmp_real_max
;
2452 const SSL_METHOD
*method
;
2453 const version_info
*table
;
2454 const version_info
*vent
;
2455 const SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2457 switch (ssl
->method
->version
) {
2460 * If this SSL handle is not from a version flexible method we don't
2461 * (and never did) check min/max FIPS or Suite B constraints. Hope
2462 * that's OK. It is up to the caller to not choose fixed protocol
2463 * versions they don't want. If not, then easy to fix, just return
2464 * ssl_method_error(s, s->method)
2466 *min_version
= *max_version
= s
->version
;
2468 * Providing a real_max only makes sense where we're using a version
2471 if (!ossl_assert(real_max
== NULL
))
2472 return ERR_R_INTERNAL_ERROR
;
2474 case TLS_ANY_VERSION
:
2475 table
= tls_version_table
;
2477 case DTLS_ANY_VERSION
:
2478 table
= dtls_version_table
;
2483 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
2484 * below X enabled. This is required in order to maintain the "version
2485 * capability" vector contiguous. Any versions with a NULL client method
2486 * (protocol version client is disabled at compile-time) is also a "hole".
2488 * Our initial state is hole == 1, version == 0. That is, versions above
2489 * the first version in the method table are disabled (a "hole" above
2490 * the valid protocol entries) and we don't have a selected version yet.
2492 * Whenever "hole == 1", and we hit an enabled method, its version becomes
2493 * the selected version. We're no longer in a hole, so "hole" becomes 0.
2495 * If "hole == 0" and we hit an enabled method, we support a contiguous
2496 * range of at least two methods. If we hit a disabled method,
2497 * then hole becomes true again, but nothing else changes yet,
2498 * because all the remaining methods may be disabled too.
2499 * If we again hit an enabled method after the new hole, it becomes
2500 * selected, as we start from scratch.
2502 *min_version
= version
= 0;
2504 if (real_max
!= NULL
)
2507 for (vent
= table
; vent
->version
!= 0; ++vent
) {
2509 * A table entry with a NULL client method is still a hole in the
2510 * "version capability" vector.
2512 if (vent
->cmeth
== NULL
) {
2517 method
= vent
->cmeth();
2519 if (hole
== 1 && tmp_real_max
== 0)
2520 tmp_real_max
= vent
->version
;
2522 if (ssl_method_error(s
, method
) != 0) {
2525 *min_version
= method
->version
;
2527 if (real_max
!= NULL
&& tmp_real_max
!= 0)
2528 *real_max
= tmp_real_max
;
2529 version
= method
->version
;
2530 *min_version
= version
;
2535 *max_version
= version
;
2537 /* Fail if everything is disabled */
2539 return SSL_R_NO_PROTOCOLS_AVAILABLE
;
2545 * ssl_set_client_hello_version - Work out what version we should be using for
2546 * the initial ClientHello.legacy_version field.
2548 * @s: client SSL handle.
2550 * Returns 0 on success or an SSL error reason number on failure.
2552 int ssl_set_client_hello_version(SSL_CONNECTION
*s
)
2554 int ver_min
, ver_max
, ret
;
2557 * In a renegotiation we always send the same client_version that we sent
2558 * last time, regardless of which version we eventually negotiated.
2560 if (!SSL_IS_FIRST_HANDSHAKE(s
))
2563 ret
= ssl_get_min_max_version(s
, &ver_min
, &ver_max
, NULL
);
2568 s
->version
= ver_max
;
2570 if (SSL_CONNECTION_IS_DTLS(s
)) {
2571 if (ver_max
== DTLS1_BAD_VER
) {
2573 * Even though this is technically before version negotiation,
2574 * because we have asked for DTLS1_BAD_VER we will never negotiate
2575 * anything else, and this has impacts on the record layer for when
2576 * we read the ServerHello. So we need to tell the record layer
2577 * about this immediately.
2579 if (!ssl_set_record_protocol_version(s
, ver_max
))
2582 } else if (ver_max
> TLS1_2_VERSION
) {
2583 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
2584 ver_max
= TLS1_2_VERSION
;
2587 s
->client_version
= ver_max
;
2592 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
2593 * and |checkallow| is 1 then additionally check if the group is allowed to be
2594 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
2595 * 1) or 0 otherwise. If provided a pointer it will also return the position
2596 * where the group was found.
2598 int check_in_list(SSL_CONNECTION
*s
, uint16_t group_id
, const uint16_t *groups
,
2599 size_t num_groups
, int checkallow
, size_t *pos
)
2603 if (groups
== NULL
|| num_groups
== 0)
2606 for (i
= 0; i
< num_groups
; i
++) {
2607 uint16_t group
= groups
[i
];
2609 if (group_id
== group
2611 || tls_group_allowed(s
, group
, SSL_SECOP_CURVE_CHECK
))) {
2621 /* Replace ClientHello1 in the transcript hash with a synthetic message */
2622 int create_synthetic_message_hash(SSL_CONNECTION
*s
,
2623 const unsigned char *hashval
,
2624 size_t hashlen
, const unsigned char *hrr
,
2627 unsigned char hashvaltmp
[EVP_MAX_MD_SIZE
];
2628 unsigned char msghdr
[SSL3_HM_HEADER_LENGTH
];
2630 memset(msghdr
, 0, sizeof(msghdr
));
2632 if (hashval
== NULL
) {
2633 hashval
= hashvaltmp
;
2635 /* Get the hash of the initial ClientHello */
2636 if (!ssl3_digest_cached_records(s
, 0)
2637 || !ssl_handshake_hash(s
, hashvaltmp
, sizeof(hashvaltmp
),
2639 /* SSLfatal() already called */
2644 /* Reinitialise the transcript hash */
2645 if (!ssl3_init_finished_mac(s
)) {
2646 /* SSLfatal() already called */
2650 /* Inject the synthetic message_hash message */
2651 msghdr
[0] = SSL3_MT_MESSAGE_HASH
;
2652 msghdr
[SSL3_HM_HEADER_LENGTH
- 1] = (unsigned char)hashlen
;
2653 if (!ssl3_finish_mac(s
, msghdr
, SSL3_HM_HEADER_LENGTH
)
2654 || !ssl3_finish_mac(s
, hashval
, hashlen
)) {
2655 /* SSLfatal() already called */
2660 * Now re-inject the HRR and current message if appropriate (we just deleted
2661 * it when we reinitialised the transcript hash above). Only necessary after
2662 * receiving a ClientHello2 with a cookie.
2665 && (!ssl3_finish_mac(s
, hrr
, hrrlen
)
2666 || !ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
2667 s
->s3
.tmp
.message_size
2668 + SSL3_HM_HEADER_LENGTH
))) {
2669 /* SSLfatal() already called */
2676 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
2678 return X509_NAME_cmp(*a
, *b
);
2681 int parse_ca_names(SSL_CONNECTION
*s
, PACKET
*pkt
)
2683 STACK_OF(X509_NAME
) *ca_sk
= sk_X509_NAME_new(ca_dn_cmp
);
2684 X509_NAME
*xn
= NULL
;
2687 if (ca_sk
== NULL
) {
2688 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
2691 /* get the CA RDNs */
2692 if (!PACKET_get_length_prefixed_2(pkt
, &cadns
)) {
2693 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2697 while (PACKET_remaining(&cadns
)) {
2698 const unsigned char *namestart
, *namebytes
;
2699 unsigned int name_len
;
2701 if (!PACKET_get_net_2(&cadns
, &name_len
)
2702 || !PACKET_get_bytes(&cadns
, &namebytes
, name_len
)) {
2703 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_LENGTH_MISMATCH
);
2707 namestart
= namebytes
;
2708 if ((xn
= d2i_X509_NAME(NULL
, &namebytes
, name_len
)) == NULL
) {
2709 SSLfatal(s
, SSL_AD_DECODE_ERROR
, ERR_R_ASN1_LIB
);
2712 if (namebytes
!= (namestart
+ name_len
)) {
2713 SSLfatal(s
, SSL_AD_DECODE_ERROR
, SSL_R_CA_DN_LENGTH_MISMATCH
);
2717 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
2718 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
2724 sk_X509_NAME_pop_free(s
->s3
.tmp
.peer_ca_names
, X509_NAME_free
);
2725 s
->s3
.tmp
.peer_ca_names
= ca_sk
;
2730 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
2735 const STACK_OF(X509_NAME
) *get_ca_names(SSL_CONNECTION
*s
)
2737 const STACK_OF(X509_NAME
) *ca_sk
= NULL
;
2738 SSL
*ssl
= SSL_CONNECTION_GET_SSL(s
);
2741 ca_sk
= SSL_get_client_CA_list(ssl
);
2742 if (ca_sk
!= NULL
&& sk_X509_NAME_num(ca_sk
) == 0)
2747 ca_sk
= SSL_get0_CA_list(ssl
);
2752 int construct_ca_names(SSL_CONNECTION
*s
, const STACK_OF(X509_NAME
) *ca_sk
,
2755 /* Start sub-packet for client CA list */
2756 if (!WPACKET_start_sub_packet_u16(pkt
)) {
2757 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2761 if ((ca_sk
!= NULL
) && !(s
->options
& SSL_OP_DISABLE_TLSEXT_CA_NAMES
)) {
2764 for (i
= 0; i
< sk_X509_NAME_num(ca_sk
); i
++) {
2765 unsigned char *namebytes
;
2766 X509_NAME
*name
= sk_X509_NAME_value(ca_sk
, i
);
2770 || (namelen
= i2d_X509_NAME(name
, NULL
)) < 0
2771 || !WPACKET_sub_allocate_bytes_u16(pkt
, namelen
,
2773 || i2d_X509_NAME(name
, &namebytes
) != namelen
) {
2774 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2780 if (!WPACKET_close(pkt
)) {
2781 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2788 /* Create a buffer containing data to be signed for server key exchange */
2789 size_t construct_key_exchange_tbs(SSL_CONNECTION
*s
, unsigned char **ptbs
,
2790 const void *param
, size_t paramlen
)
2792 size_t tbslen
= 2 * SSL3_RANDOM_SIZE
+ paramlen
;
2793 unsigned char *tbs
= OPENSSL_malloc(tbslen
);
2796 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_CRYPTO_LIB
);
2799 memcpy(tbs
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
);
2800 memcpy(tbs
+ SSL3_RANDOM_SIZE
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
);
2802 memcpy(tbs
+ SSL3_RANDOM_SIZE
* 2, param
, paramlen
);
2809 * Saves the current handshake digest for Post-Handshake Auth,
2810 * Done after ClientFinished is processed, done exactly once
2812 int tls13_save_handshake_digest_for_pha(SSL_CONNECTION
*s
)
2814 if (s
->pha_dgst
== NULL
) {
2815 if (!ssl3_digest_cached_records(s
, 1))
2816 /* SSLfatal() already called */
2819 s
->pha_dgst
= EVP_MD_CTX_new();
2820 if (s
->pha_dgst
== NULL
) {
2821 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2824 if (!EVP_MD_CTX_copy_ex(s
->pha_dgst
,
2825 s
->s3
.handshake_dgst
)) {
2826 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2827 EVP_MD_CTX_free(s
->pha_dgst
);
2836 * Restores the Post-Handshake Auth handshake digest
2837 * Done just before sending/processing the Cert Request
2839 int tls13_restore_handshake_digest_for_pha(SSL_CONNECTION
*s
)
2841 if (s
->pha_dgst
== NULL
) {
2842 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2845 if (!EVP_MD_CTX_copy_ex(s
->s3
.handshake_dgst
,
2847 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2853 #ifndef OPENSSL_NO_COMP_ALG
2854 MSG_PROCESS_RETURN
tls13_process_compressed_certificate(SSL_CONNECTION
*sc
,
2859 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
2861 COMP_METHOD
*method
= NULL
;
2862 COMP_CTX
*comp
= NULL
;
2863 size_t expected_length
;
2869 SSLfatal(sc
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
2872 if (!PACKET_get_net_2(pkt
, (unsigned int*)&comp_alg
)) {
2873 SSLfatal(sc
, SSL_AD_BAD_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
2876 /* If we have a prefs list, make sure the algorithm is in it */
2877 if (sc
->cert_comp_prefs
[0] != TLSEXT_comp_cert_none
) {
2878 for (i
= 0; sc
->cert_comp_prefs
[i
] != TLSEXT_comp_cert_none
; i
++) {
2879 if (sc
->cert_comp_prefs
[i
] == comp_alg
) {
2885 SSLfatal(sc
, SSL_AD_ILLEGAL_PARAMETER
, SSL_R_BAD_COMPRESSION_ALGORITHM
);
2889 if (!ossl_comp_has_alg(comp_alg
)) {
2890 SSLfatal(sc
, SSL_AD_BAD_CERTIFICATE
, SSL_R_BAD_COMPRESSION_ALGORITHM
);
2894 case TLSEXT_comp_cert_zlib
:
2895 method
= COMP_zlib_oneshot();
2897 case TLSEXT_comp_cert_brotli
:
2898 method
= COMP_brotli_oneshot();
2900 case TLSEXT_comp_cert_zstd
:
2901 method
= COMP_zstd_oneshot();
2904 SSLfatal(sc
, SSL_AD_BAD_CERTIFICATE
, SSL_R_BAD_COMPRESSION_ALGORITHM
);
2908 if ((comp
= COMP_CTX_new(method
)) == NULL
2909 || !PACKET_get_net_3_len(pkt
, &expected_length
)
2910 || !PACKET_get_net_3_len(pkt
, &comp_length
)) {
2911 SSLfatal(sc
, SSL_AD_BAD_CERTIFICATE
, SSL_R_BAD_DECOMPRESSION
);
2915 if (PACKET_remaining(pkt
) != comp_length
|| comp_length
== 0) {
2916 SSLfatal(sc
, SSL_AD_DECODE_ERROR
, SSL_R_BAD_DECOMPRESSION
);
2920 if (!BUF_MEM_grow(buf
, expected_length
)
2921 || !PACKET_buf_init(tmppkt
, (unsigned char *)buf
->data
, expected_length
)
2922 || COMP_expand_block(comp
, (unsigned char *)buf
->data
, (int)expected_length
,
2923 (unsigned char*)PACKET_data(pkt
),
2924 (int)comp_length
) != (int)expected_length
) {
2925 SSLfatal(sc
, SSL_AD_BAD_CERTIFICATE
, SSL_R_BAD_DECOMPRESSION
);
2928 ret
= MSG_PROCESS_CONTINUE_PROCESSING
;
2930 COMP_CTX_free(comp
);