2 * Copyright 1995-2016 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 * send s->init_buf in records of type 'type' (SSL3_RT_HANDSHAKE or
24 * SSL3_RT_CHANGE_CIPHER_SPEC)
26 int ssl3_do_write(SSL
*s
, int type
)
31 ret
= ssl3_write_bytes(s
, type
, &s
->init_buf
->data
[s
->init_off
],
32 s
->init_num
, &written
);
35 if (type
== SSL3_RT_HANDSHAKE
)
37 * should not be done for 'Hello Request's, but in that case we'll
38 * ignore the result anyway
40 if (!ssl3_finish_mac(s
,
41 (unsigned char *)&s
->init_buf
->data
[s
->init_off
],
45 if (written
== s
->init_num
) {
47 s
->msg_callback(1, s
->version
, type
, s
->init_buf
->data
,
48 (size_t)(s
->init_off
+ s
->init_num
), s
,
52 s
->init_off
+= written
;
53 s
->init_num
-= written
;
57 int tls_close_construct_packet(SSL
*s
, WPACKET
*pkt
, int htype
)
61 if ((htype
!= SSL3_MT_CHANGE_CIPHER_SPEC
&& !WPACKET_close(pkt
))
62 || !WPACKET_get_length(pkt
, &msglen
)
65 s
->init_num
= (int)msglen
;
71 int tls_setup_handshake(SSL
*s
)
73 if (!ssl3_init_finished_mac(s
))
76 /* Reset any extension flags */
77 memset(s
->ext
.extflags
, 0, sizeof(s
->ext
.extflags
));
80 STACK_OF(SSL_CIPHER
) *ciphers
= SSL_get_ciphers(s
);
81 int i
, ver_min
, ver_max
, ok
= 0;
84 * Sanity check that the maximum version we accept has ciphers
85 * enabled. For clients we do this check during construction of the
88 if (ssl_get_min_max_version(s
, &ver_min
, &ver_max
) != 0) {
89 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE
, ERR_R_INTERNAL_ERROR
);
90 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
93 for (i
= 0; i
< sk_SSL_CIPHER_num(ciphers
); i
++) {
94 const SSL_CIPHER
*c
= sk_SSL_CIPHER_value(ciphers
, i
);
97 if (DTLS_VERSION_GE(ver_max
, c
->min_dtls
) &&
98 DTLS_VERSION_LE(ver_max
, c
->max_dtls
))
100 } else if (ver_max
>= c
->min_tls
&& ver_max
<= c
->max_tls
) {
107 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE
, SSL_R_NO_CIPHERS_AVAILABLE
);
108 ERR_add_error_data(1, "No ciphers enabled for max supported "
110 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
113 if (SSL_IS_FIRST_HANDSHAKE(s
)) {
114 s
->ctx
->stats
.sess_accept
++;
115 } else if ((s
->options
& SSL_OP_NO_RENEGOTIATION
)) {
116 /* Renegotiation is disabled */
117 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_RENEGOTIATION
);
119 } else if (!s
->s3
->send_connection_binding
&&
121 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION
)) {
123 * Server attempting to renegotiate with client that doesn't
124 * support secure renegotiation.
126 SSLerr(SSL_F_TLS_SETUP_HANDSHAKE
,
127 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED
);
128 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
131 s
->ctx
->stats
.sess_accept_renegotiate
++;
133 s
->s3
->tmp
.cert_request
= 0;
136 if (SSL_IS_FIRST_HANDSHAKE(s
))
137 s
->ctx
->stats
.sess_connect
++;
139 s
->ctx
->stats
.sess_connect_renegotiate
++;
141 /* mark client_random uninitialized */
142 memset(s
->s3
->client_random
, 0, sizeof(s
->s3
->client_random
));
145 s
->s3
->tmp
.cert_req
= 0;
148 s
->statem
.use_timer
= 1;
155 * Size of the to-be-signed TLS13 data, without the hash size itself:
156 * 64 bytes of value 32, 33 context bytes, 1 byte separator
158 #define TLS13_TBS_START_SIZE 64
159 #define TLS13_TBS_PREAMBLE_SIZE (TLS13_TBS_START_SIZE + 33 + 1)
161 static int get_cert_verify_tbs_data(SSL
*s
, unsigned char *tls13tbs
,
162 void **hdata
, size_t *hdatalen
)
164 static const char *servercontext
= "TLS 1.3, server CertificateVerify";
165 static const char *clientcontext
= "TLS 1.3, client CertificateVerify";
167 if (SSL_IS_TLS13(s
)) {
170 /* Set the first 64 bytes of to-be-signed data to octet 32 */
171 memset(tls13tbs
, 32, TLS13_TBS_START_SIZE
);
172 /* This copies the 33 bytes of context plus the 0 separator byte */
173 if (s
->statem
.hand_state
== TLS_ST_CR_CERT_VRFY
174 || s
->statem
.hand_state
== TLS_ST_SW_CERT_VRFY
)
175 strcpy((char *)tls13tbs
+ TLS13_TBS_START_SIZE
, servercontext
);
177 strcpy((char *)tls13tbs
+ TLS13_TBS_START_SIZE
, clientcontext
);
180 * If we're currently reading then we need to use the saved handshake
181 * hash value. We can't use the current handshake hash state because
182 * that includes the CertVerify itself.
184 if (s
->statem
.hand_state
== TLS_ST_CR_CERT_VRFY
185 || s
->statem
.hand_state
== TLS_ST_SR_CERT_VRFY
) {
186 memcpy(tls13tbs
+ TLS13_TBS_PREAMBLE_SIZE
, s
->cert_verify_hash
,
187 s
->cert_verify_hash_len
);
188 hashlen
= s
->cert_verify_hash_len
;
189 } else if (!ssl_handshake_hash(s
, tls13tbs
+ TLS13_TBS_PREAMBLE_SIZE
,
190 EVP_MAX_MD_SIZE
, &hashlen
)) {
195 *hdatalen
= TLS13_TBS_PREAMBLE_SIZE
+ hashlen
;
199 retlen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, hdata
);
208 int tls_construct_cert_verify(SSL
*s
, WPACKET
*pkt
)
210 EVP_PKEY
*pkey
= NULL
;
211 const EVP_MD
*md
= NULL
;
212 EVP_MD_CTX
*mctx
= NULL
;
213 EVP_PKEY_CTX
*pctx
= NULL
;
214 size_t hdatalen
= 0, siglen
= 0;
216 unsigned char *sig
= NULL
;
217 unsigned char tls13tbs
[TLS13_TBS_PREAMBLE_SIZE
+ EVP_MAX_MD_SIZE
];
218 const SIGALG_LOOKUP
*lu
= s
->s3
->tmp
.sigalg
;
220 if (lu
== NULL
|| s
->s3
->tmp
.cert
== NULL
) {
221 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
224 pkey
= s
->s3
->tmp
.cert
->privatekey
;
226 if (pkey
== NULL
|| !tls1_lookup_md(lu
, &md
)) {
227 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
231 mctx
= EVP_MD_CTX_new();
233 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
237 /* Get the data to be signed */
238 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
239 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
243 if (SSL_USE_SIGALGS(s
) && !WPACKET_put_bytes_u16(pkt
, lu
->sigalg
)) {
244 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
247 siglen
= EVP_PKEY_size(pkey
);
248 sig
= OPENSSL_malloc(siglen
);
250 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
254 if (EVP_DigestSignInit(mctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
255 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
259 if (lu
->sig
== EVP_PKEY_RSA_PSS
) {
260 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
261 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
262 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
263 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
267 if (s
->version
== SSL3_VERSION
) {
268 if (EVP_DigestSignUpdate(mctx
, hdata
, hdatalen
) <= 0
269 || !EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
270 (int)s
->session
->master_key_length
,
271 s
->session
->master_key
)
272 || EVP_DigestSignFinal(mctx
, sig
, &siglen
) <= 0) {
274 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
277 } else if (EVP_DigestSign(mctx
, sig
, &siglen
, hdata
, hdatalen
) <= 0) {
278 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_EVP_LIB
);
282 #ifndef OPENSSL_NO_GOST
284 int pktype
= lu
->sig
;
286 if (pktype
== NID_id_GostR3410_2001
287 || pktype
== NID_id_GostR3410_2012_256
288 || pktype
== NID_id_GostR3410_2012_512
)
289 BUF_reverse(sig
, NULL
, siglen
);
293 if (!WPACKET_sub_memcpy_u16(pkt
, sig
, siglen
)) {
294 SSLerr(SSL_F_TLS_CONSTRUCT_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
298 /* Digest cached records and discard handshake buffer */
299 if (!ssl3_digest_cached_records(s
, 0))
303 EVP_MD_CTX_free(mctx
);
307 EVP_MD_CTX_free(mctx
);
308 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
312 MSG_PROCESS_RETURN
tls_process_cert_verify(SSL
*s
, PACKET
*pkt
)
314 EVP_PKEY
*pkey
= NULL
;
315 const unsigned char *data
;
316 #ifndef OPENSSL_NO_GOST
317 unsigned char *gost_data
= NULL
;
319 int al
= SSL_AD_INTERNAL_ERROR
;
320 MSG_PROCESS_RETURN ret
= MSG_PROCESS_ERROR
;
324 const EVP_MD
*md
= NULL
;
327 unsigned char tls13tbs
[TLS13_TBS_PREAMBLE_SIZE
+ EVP_MAX_MD_SIZE
];
328 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
329 EVP_PKEY_CTX
*pctx
= NULL
;
332 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
336 peer
= s
->session
->peer
;
337 pkey
= X509_get0_pubkey(peer
);
341 if (ssl_cert_lookup_by_pkey(pkey
, NULL
) == NULL
) {
342 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
,
343 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE
);
344 al
= SSL_AD_ILLEGAL_PARAMETER
;
348 if (SSL_USE_SIGALGS(s
)) {
352 if (!PACKET_get_net_2(pkt
, &sigalg
)) {
353 al
= SSL_AD_DECODE_ERROR
;
356 rv
= tls12_check_peer_sigalg(s
, sigalg
, pkey
);
359 } else if (rv
== 0) {
360 al
= SSL_AD_DECODE_ERROR
;
364 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
366 } else if (!tls1_set_peer_legacy_sigalg(s
, pkey
)) {
367 al
= SSL_AD_INTERNAL_ERROR
;
371 if (!tls1_lookup_md(s
->s3
->tmp
.peer_sigalg
, &md
)) {
372 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
373 al
= SSL_AD_INTERNAL_ERROR
;
377 /* Check for broken implementations of GOST ciphersuites */
379 * If key is GOST and len is exactly 64 or 128, it is signature without
380 * length field (CryptoPro implementations at least till TLS 1.2)
382 #ifndef OPENSSL_NO_GOST
383 if (!SSL_USE_SIGALGS(s
)
384 && ((PACKET_remaining(pkt
) == 64
385 && (EVP_PKEY_id(pkey
) == NID_id_GostR3410_2001
386 || EVP_PKEY_id(pkey
) == NID_id_GostR3410_2012_256
))
387 || (PACKET_remaining(pkt
) == 128
388 && EVP_PKEY_id(pkey
) == NID_id_GostR3410_2012_512
))) {
389 len
= PACKET_remaining(pkt
);
392 if (!PACKET_get_net_2(pkt
, &len
)) {
393 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
394 al
= SSL_AD_DECODE_ERROR
;
398 j
= EVP_PKEY_size(pkey
);
399 if (((int)len
> j
) || ((int)PACKET_remaining(pkt
) > j
)
400 || (PACKET_remaining(pkt
) == 0)) {
401 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_WRONG_SIGNATURE_SIZE
);
402 al
= SSL_AD_DECODE_ERROR
;
405 if (!PACKET_get_bytes(pkt
, &data
, len
)) {
406 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_LENGTH_MISMATCH
);
407 al
= SSL_AD_DECODE_ERROR
;
411 if (!get_cert_verify_tbs_data(s
, tls13tbs
, &hdata
, &hdatalen
)) {
412 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_INTERNAL_ERROR
);
417 fprintf(stderr
, "Using client verify alg %s\n", EVP_MD_name(md
));
419 if (EVP_DigestVerifyInit(mctx
, &pctx
, md
, NULL
, pkey
) <= 0) {
420 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
423 #ifndef OPENSSL_NO_GOST
425 int pktype
= EVP_PKEY_id(pkey
);
426 if (pktype
== NID_id_GostR3410_2001
427 || pktype
== NID_id_GostR3410_2012_256
428 || pktype
== NID_id_GostR3410_2012_512
) {
429 if ((gost_data
= OPENSSL_malloc(len
)) == NULL
) {
430 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_MALLOC_FAILURE
);
433 BUF_reverse(gost_data
, data
, len
);
439 if (SSL_USE_PSS(s
)) {
440 if (EVP_PKEY_CTX_set_rsa_padding(pctx
, RSA_PKCS1_PSS_PADDING
) <= 0
441 || EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx
,
442 RSA_PSS_SALTLEN_DIGEST
) <= 0) {
443 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
447 if (s
->version
== SSL3_VERSION
) {
448 if (EVP_DigestVerifyUpdate(mctx
, hdata
, hdatalen
) <= 0
449 || !EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
450 (int)s
->session
->master_key_length
,
451 s
->session
->master_key
)) {
452 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, ERR_R_EVP_LIB
);
455 if (EVP_DigestVerifyFinal(mctx
, data
, len
) <= 0) {
456 al
= SSL_AD_DECRYPT_ERROR
;
457 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_BAD_SIGNATURE
);
461 j
= EVP_DigestVerify(mctx
, data
, len
, hdata
, hdatalen
);
463 al
= SSL_AD_DECRYPT_ERROR
;
464 SSLerr(SSL_F_TLS_PROCESS_CERT_VERIFY
, SSL_R_BAD_SIGNATURE
);
469 ret
= MSG_PROCESS_CONTINUE_READING
;
472 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
473 ossl_statem_set_error(s
);
475 BIO_free(s
->s3
->handshake_buffer
);
476 s
->s3
->handshake_buffer
= NULL
;
477 EVP_MD_CTX_free(mctx
);
478 #ifndef OPENSSL_NO_GOST
479 OPENSSL_free(gost_data
);
484 int tls_construct_finished(SSL
*s
, WPACKET
*pkt
)
486 size_t finish_md_len
;
490 /* This is a real handshake so make sure we clean it up at the end */
492 s
->statem
.cleanuphand
= 1;
495 * We only change the keys if we didn't already do this when we sent the
500 && s
->s3
->tmp
.cert_req
== 0
501 && (!s
->method
->ssl3_enc
->change_cipher_state(s
,
502 SSL3_CC_HANDSHAKE
| SSL3_CHANGE_CIPHER_CLIENT_WRITE
))) {
503 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
505 * This is a fatal error, which leaves
506 * enc_write_ctx in an inconsistent state
507 * and thus ssl3_send_alert may crash.
513 sender
= s
->method
->ssl3_enc
->server_finished_label
;
514 slen
= s
->method
->ssl3_enc
->server_finished_label_len
;
516 sender
= s
->method
->ssl3_enc
->client_finished_label
;
517 slen
= s
->method
->ssl3_enc
->client_finished_label_len
;
520 finish_md_len
= s
->method
->ssl3_enc
->final_finish_mac(s
,
522 s
->s3
->tmp
.finish_md
);
523 if (finish_md_len
== 0) {
524 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
528 s
->s3
->tmp
.finish_md_len
= finish_md_len
;
530 if (!WPACKET_memcpy(pkt
, s
->s3
->tmp
.finish_md
, finish_md_len
)) {
531 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
536 * Log the master secret, if logging is enabled. We don't log it for
537 * TLSv1.3: there's a different key schedule for that.
539 if (!SSL_IS_TLS13(s
) && !ssl_log_secret(s
, MASTER_SECRET_LABEL
,
540 s
->session
->master_key
,
541 s
->session
->master_key_length
)) {
542 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
547 * Copy the finished so we can use it for renegotiation checks
549 if (!ossl_assert(finish_md_len
<= EVP_MAX_MD_SIZE
)) {
550 SSLerr(SSL_F_TLS_CONSTRUCT_FINISHED
, ERR_R_INTERNAL_ERROR
);
554 memcpy(s
->s3
->previous_client_finished
, s
->s3
->tmp
.finish_md
,
556 s
->s3
->previous_client_finished_len
= finish_md_len
;
558 memcpy(s
->s3
->previous_server_finished
, s
->s3
->tmp
.finish_md
,
560 s
->s3
->previous_server_finished_len
= finish_md_len
;
565 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
569 int tls_construct_key_update(SSL
*s
, WPACKET
*pkt
)
571 if (!WPACKET_put_bytes_u8(pkt
, s
->key_update
)) {
572 SSLerr(SSL_F_TLS_CONSTRUCT_KEY_UPDATE
, ERR_R_INTERNAL_ERROR
);
576 s
->key_update
= SSL_KEY_UPDATE_NONE
;
580 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
584 MSG_PROCESS_RETURN
tls_process_key_update(SSL
*s
, PACKET
*pkt
)
587 unsigned int updatetype
;
589 s
->key_update_count
++;
590 if (s
->key_update_count
> MAX_KEY_UPDATE_MESSAGES
) {
591 al
= SSL_AD_ILLEGAL_PARAMETER
;
592 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE
, SSL_R_TOO_MANY_KEY_UPDATES
);
597 * A KeyUpdate message signals a key change so the end of the message must
598 * be on a record boundary.
600 if (RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
601 al
= SSL_AD_UNEXPECTED_MESSAGE
;
602 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
606 if (!PACKET_get_1(pkt
, &updatetype
)
607 || PACKET_remaining(pkt
) != 0) {
608 al
= SSL_AD_DECODE_ERROR
;
609 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE
, SSL_R_BAD_KEY_UPDATE
);
614 * There are only two defined key update types. Fail if we get a value we
617 if (updatetype
!= SSL_KEY_UPDATE_NOT_REQUESTED
618 && updatetype
!= SSL_KEY_UPDATE_REQUESTED
) {
619 al
= SSL_AD_ILLEGAL_PARAMETER
;
620 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE
, SSL_R_BAD_KEY_UPDATE
);
625 * If we get a request for us to update our sending keys too then, we need
626 * to additionally send a KeyUpdate message. However that message should
627 * not also request an update (otherwise we get into an infinite loop).
629 if (updatetype
== SSL_KEY_UPDATE_REQUESTED
)
630 s
->key_update
= SSL_KEY_UPDATE_NOT_REQUESTED
;
632 if (!tls13_update_key(s
, 0)) {
633 al
= SSL_AD_INTERNAL_ERROR
;
634 SSLerr(SSL_F_TLS_PROCESS_KEY_UPDATE
, ERR_R_INTERNAL_ERROR
);
638 return MSG_PROCESS_FINISHED_READING
;
640 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
641 ossl_statem_set_error(s
);
642 return MSG_PROCESS_ERROR
;
645 #ifndef OPENSSL_NO_NEXTPROTONEG
647 * ssl3_take_mac calculates the Finished MAC for the handshakes messages seen
650 static void ssl3_take_mac(SSL
*s
)
655 * If no new cipher setup return immediately: other functions will set
656 * the appropriate error.
658 if (s
->s3
->tmp
.new_cipher
== NULL
)
661 sender
= s
->method
->ssl3_enc
->server_finished_label
;
662 slen
= s
->method
->ssl3_enc
->server_finished_label_len
;
664 sender
= s
->method
->ssl3_enc
->client_finished_label
;
665 slen
= s
->method
->ssl3_enc
->client_finished_label_len
;
668 s
->s3
->tmp
.peer_finish_md_len
= s
->method
->ssl3_enc
->final_finish_mac(s
,
671 s
->s3
->tmp
.peer_finish_md
);
675 MSG_PROCESS_RETURN
tls_process_change_cipher_spec(SSL
*s
, PACKET
*pkt
)
680 remain
= PACKET_remaining(pkt
);
682 * 'Change Cipher Spec' is just a single byte, which should already have
683 * been consumed by ssl_get_message() so there should be no bytes left,
684 * unless we're using DTLS1_BAD_VER, which has an extra 2 bytes
686 if (SSL_IS_DTLS(s
)) {
687 if ((s
->version
== DTLS1_BAD_VER
688 && remain
!= DTLS1_CCS_HEADER_LENGTH
+ 1)
689 || (s
->version
!= DTLS1_BAD_VER
690 && remain
!= DTLS1_CCS_HEADER_LENGTH
- 1)) {
691 al
= SSL_AD_DECODE_ERROR
;
692 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
,
693 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
698 al
= SSL_AD_DECODE_ERROR
;
699 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
,
700 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
705 /* Check we have a cipher to change to */
706 if (s
->s3
->tmp
.new_cipher
== NULL
) {
707 al
= SSL_AD_UNEXPECTED_MESSAGE
;
708 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
, SSL_R_CCS_RECEIVED_EARLY
);
712 s
->s3
->change_cipher_spec
= 1;
713 if (!ssl3_do_change_cipher_spec(s
)) {
714 al
= SSL_AD_INTERNAL_ERROR
;
715 SSLerr(SSL_F_TLS_PROCESS_CHANGE_CIPHER_SPEC
, ERR_R_INTERNAL_ERROR
);
719 if (SSL_IS_DTLS(s
)) {
720 dtls1_reset_seq_numbers(s
, SSL3_CC_READ
);
722 if (s
->version
== DTLS1_BAD_VER
)
723 s
->d1
->handshake_read_seq
++;
725 #ifndef OPENSSL_NO_SCTP
727 * Remember that a CCS has been received, so that an old key of
728 * SCTP-Auth can be deleted when a CCS is sent. Will be ignored if no
731 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_AUTH_CCS_RCVD
, 1, NULL
);
735 return MSG_PROCESS_CONTINUE_READING
;
737 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
738 ossl_statem_set_error(s
);
739 return MSG_PROCESS_ERROR
;
742 MSG_PROCESS_RETURN
tls_process_finished(SSL
*s
, PACKET
*pkt
)
744 int al
= SSL_AD_INTERNAL_ERROR
;
748 /* This is a real handshake so make sure we clean it up at the end */
750 s
->statem
.cleanuphand
= 1;
753 * In TLSv1.3 a Finished message signals a key change so the end of the
754 * message must be on a record boundary.
756 if (SSL_IS_TLS13(s
) && RECORD_LAYER_processed_read_pending(&s
->rlayer
)) {
757 al
= SSL_AD_UNEXPECTED_MESSAGE
;
758 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_NOT_ON_RECORD_BOUNDARY
);
762 /* If this occurs, we have missed a message */
763 if (!SSL_IS_TLS13(s
) && !s
->s3
->change_cipher_spec
) {
764 al
= SSL_AD_UNEXPECTED_MESSAGE
;
765 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_GOT_A_FIN_BEFORE_A_CCS
);
768 s
->s3
->change_cipher_spec
= 0;
770 md_len
= s
->s3
->tmp
.peer_finish_md_len
;
772 if (md_len
!= PACKET_remaining(pkt
)) {
773 al
= SSL_AD_DECODE_ERROR
;
774 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_BAD_DIGEST_LENGTH
);
778 if (CRYPTO_memcmp(PACKET_data(pkt
), s
->s3
->tmp
.peer_finish_md
,
780 al
= SSL_AD_DECRYPT_ERROR
;
781 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_DIGEST_CHECK_FAILED
);
786 * Copy the finished so we can use it for renegotiation checks
788 if (!ossl_assert(md_len
<= EVP_MAX_MD_SIZE
)) {
789 al
= SSL_AD_INTERNAL_ERROR
;
790 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, ERR_R_INTERNAL_ERROR
);
794 memcpy(s
->s3
->previous_client_finished
, s
->s3
->tmp
.peer_finish_md
,
796 s
->s3
->previous_client_finished_len
= md_len
;
798 memcpy(s
->s3
->previous_server_finished
, s
->s3
->tmp
.peer_finish_md
,
800 s
->s3
->previous_server_finished_len
= md_len
;
804 * In TLS1.3 we also have to change cipher state and do any final processing
805 * of the initial server flight (if we are a client)
807 if (SSL_IS_TLS13(s
)) {
809 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
810 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_SERVER_READ
)) {
811 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
815 if (!s
->method
->ssl3_enc
->generate_master_secret(s
,
816 s
->master_secret
, s
->handshake_secret
, 0,
817 &s
->session
->master_key_length
)) {
818 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
821 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
822 SSL3_CC_APPLICATION
| SSL3_CHANGE_CIPHER_CLIENT_READ
)) {
823 SSLerr(SSL_F_TLS_PROCESS_FINISHED
, SSL_R_CANNOT_CHANGE_CIPHER
);
826 if (!tls_process_initial_server_flight(s
, &al
))
831 return MSG_PROCESS_FINISHED_READING
;
833 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
834 ossl_statem_set_error(s
);
835 return MSG_PROCESS_ERROR
;
838 int tls_construct_change_cipher_spec(SSL
*s
, WPACKET
*pkt
)
840 if (!WPACKET_put_bytes_u8(pkt
, SSL3_MT_CCS
)) {
841 SSLerr(SSL_F_TLS_CONSTRUCT_CHANGE_CIPHER_SPEC
, ERR_R_INTERNAL_ERROR
);
842 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
849 /* Add a certificate to the WPACKET */
850 static int ssl_add_cert_to_wpacket(SSL
*s
, WPACKET
*pkt
, X509
*x
, int chain
,
854 unsigned char *outbytes
;
856 len
= i2d_X509(x
, NULL
);
858 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET
, ERR_R_BUF_LIB
);
859 *al
= SSL_AD_INTERNAL_ERROR
;
862 if (!WPACKET_sub_allocate_bytes_u24(pkt
, len
, &outbytes
)
863 || i2d_X509(x
, &outbytes
) != len
) {
864 SSLerr(SSL_F_SSL_ADD_CERT_TO_WPACKET
, ERR_R_INTERNAL_ERROR
);
865 *al
= SSL_AD_INTERNAL_ERROR
;
870 && !tls_construct_extensions(s
, pkt
, SSL_EXT_TLS1_3_CERTIFICATE
, x
,
877 /* Add certificate chain to provided WPACKET */
878 static int ssl_add_cert_chain(SSL
*s
, WPACKET
*pkt
, CERT_PKEY
*cpk
, int *al
)
882 STACK_OF(X509
) *extra_certs
;
883 STACK_OF(X509
) *chain
= NULL
;
884 X509_STORE
*chain_store
;
885 int tmpal
= SSL_AD_INTERNAL_ERROR
;
887 if (cpk
== NULL
|| cpk
->x509
== NULL
)
893 * If we have a certificate specific chain use it, else use parent ctx.
895 if (cpk
->chain
!= NULL
)
896 extra_certs
= cpk
->chain
;
898 extra_certs
= s
->ctx
->extra_certs
;
900 if ((s
->mode
& SSL_MODE_NO_AUTO_CHAIN
) || extra_certs
)
902 else if (s
->cert
->chain_store
)
903 chain_store
= s
->cert
->chain_store
;
905 chain_store
= s
->ctx
->cert_store
;
907 if (chain_store
!= NULL
) {
908 X509_STORE_CTX
*xs_ctx
= X509_STORE_CTX_new();
910 if (xs_ctx
== NULL
) {
911 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, ERR_R_MALLOC_FAILURE
);
914 if (!X509_STORE_CTX_init(xs_ctx
, chain_store
, x
, NULL
)) {
915 X509_STORE_CTX_free(xs_ctx
);
916 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, ERR_R_X509_LIB
);
920 * It is valid for the chain not to be complete (because normally we
921 * don't include the root cert in the chain). Therefore we deliberately
922 * ignore the error return from this call. We're not actually verifying
923 * the cert - we're just building as much of the chain as we can
925 (void)X509_verify_cert(xs_ctx
);
926 /* Don't leave errors in the queue */
928 chain
= X509_STORE_CTX_get0_chain(xs_ctx
);
929 i
= ssl_security_cert_chain(s
, chain
, NULL
, 0);
932 /* Dummy error calls so mkerr generates them */
933 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_EE_KEY_TOO_SMALL
);
934 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_CA_KEY_TOO_SMALL
);
935 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, SSL_R_CA_MD_TOO_WEAK
);
937 X509_STORE_CTX_free(xs_ctx
);
938 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, i
);
941 chain_count
= sk_X509_num(chain
);
942 for (i
= 0; i
< chain_count
; i
++) {
943 x
= sk_X509_value(chain
, i
);
945 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
, &tmpal
)) {
946 X509_STORE_CTX_free(xs_ctx
);
950 X509_STORE_CTX_free(xs_ctx
);
952 i
= ssl_security_cert_chain(s
, extra_certs
, x
, 0);
954 SSLerr(SSL_F_SSL_ADD_CERT_CHAIN
, i
);
957 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, 0, &tmpal
))
959 for (i
= 0; i
< sk_X509_num(extra_certs
); i
++) {
960 x
= sk_X509_value(extra_certs
, i
);
961 if (!ssl_add_cert_to_wpacket(s
, pkt
, x
, i
+ 1, &tmpal
))
972 unsigned long ssl3_output_cert_chain(SSL
*s
, WPACKET
*pkt
, CERT_PKEY
*cpk
,
975 int tmpal
= SSL_AD_INTERNAL_ERROR
;
977 if (!WPACKET_start_sub_packet_u24(pkt
)
978 || !ssl_add_cert_chain(s
, pkt
, cpk
, &tmpal
)
979 || !WPACKET_close(pkt
)) {
980 SSLerr(SSL_F_SSL3_OUTPUT_CERT_CHAIN
, ERR_R_INTERNAL_ERROR
);
988 * Tidy up after the end of a handshake. In the case of SCTP this may result
989 * in NBIO events. If |clearbufs| is set then init_buf and the wbio buffer is
992 WORK_STATE
tls_finish_handshake(SSL
*s
, WORK_STATE wst
, int clearbufs
)
994 void (*cb
) (const SSL
*ssl
, int type
, int val
) = NULL
;
996 #ifndef OPENSSL_NO_SCTP
997 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
999 ret
= dtls_wait_for_dry(s
);
1000 if (ret
!= WORK_FINISHED_CONTINUE
)
1006 if (!SSL_IS_DTLS(s
)) {
1008 * We don't do this in DTLS because we may still need the init_buf
1009 * in case there are any unexpected retransmits
1011 BUF_MEM_free(s
->init_buf
);
1014 if (!ssl_free_wbio_buffer(s
))
1019 if (s
->statem
.cleanuphand
) {
1020 /* skipped if we just sent a HelloRequest */
1023 s
->statem
.cleanuphand
= 0;
1025 ssl3_cleanup_key_block(s
);
1028 ssl_update_cache(s
, SSL_SESS_CACHE_SERVER
);
1030 s
->ctx
->stats
.sess_accept_good
++;
1031 s
->handshake_func
= ossl_statem_accept
;
1034 * In TLSv1.3 we update the cache as part of processing the
1037 if (!SSL_IS_TLS13(s
))
1038 ssl_update_cache(s
, SSL_SESS_CACHE_CLIENT
);
1040 s
->ctx
->stats
.sess_hit
++;
1042 s
->handshake_func
= ossl_statem_connect
;
1043 s
->ctx
->stats
.sess_connect_good
++;
1046 if (s
->info_callback
!= NULL
)
1047 cb
= s
->info_callback
;
1048 else if (s
->ctx
->info_callback
!= NULL
)
1049 cb
= s
->ctx
->info_callback
;
1052 cb(s
, SSL_CB_HANDSHAKE_DONE
, 1);
1054 if (SSL_IS_DTLS(s
)) {
1055 /* done with handshaking */
1056 s
->d1
->handshake_read_seq
= 0;
1057 s
->d1
->handshake_write_seq
= 0;
1058 s
->d1
->next_handshake_write_seq
= 0;
1059 dtls1_clear_received_buffer(s
);
1064 * If we've not cleared the buffers its because we've got more work to do,
1068 return WORK_FINISHED_CONTINUE
;
1070 ossl_statem_set_in_init(s
, 0);
1071 return WORK_FINISHED_STOP
;
1074 int tls_get_message_header(SSL
*s
, int *mt
)
1076 /* s->init_num < SSL3_HM_HEADER_LENGTH */
1077 int skip_message
, i
, recvd_type
, al
;
1079 size_t l
, readbytes
;
1081 p
= (unsigned char *)s
->init_buf
->data
;
1084 while (s
->init_num
< SSL3_HM_HEADER_LENGTH
) {
1085 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, &recvd_type
,
1087 SSL3_HM_HEADER_LENGTH
- s
->init_num
,
1090 s
->rwstate
= SSL_READING
;
1093 if (recvd_type
== SSL3_RT_CHANGE_CIPHER_SPEC
) {
1095 * A ChangeCipherSpec must be a single byte and may not occur
1096 * in the middle of a handshake message.
1098 if (s
->init_num
!= 0 || readbytes
!= 1 || p
[0] != SSL3_MT_CCS
) {
1099 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1100 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
,
1101 SSL_R_BAD_CHANGE_CIPHER_SPEC
);
1104 s
->s3
->tmp
.message_type
= *mt
= SSL3_MT_CHANGE_CIPHER_SPEC
;
1105 s
->init_num
= readbytes
- 1;
1106 s
->init_msg
= s
->init_buf
->data
;
1107 s
->s3
->tmp
.message_size
= readbytes
;
1109 } else if (recvd_type
!= SSL3_RT_HANDSHAKE
) {
1110 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1111 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
, SSL_R_CCS_RECEIVED_EARLY
);
1114 s
->init_num
+= readbytes
;
1119 if (s
->statem
.hand_state
!= TLS_ST_OK
1120 && p
[0] == SSL3_MT_HELLO_REQUEST
)
1122 * The server may always send 'Hello Request' messages --
1123 * we are doing a handshake anyway now, so ignore them if
1124 * their format is correct. Does not count for 'Finished'
1127 if (p
[1] == 0 && p
[2] == 0 && p
[3] == 0) {
1131 if (s
->msg_callback
)
1132 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
,
1133 p
, SSL3_HM_HEADER_LENGTH
, s
,
1134 s
->msg_callback_arg
);
1136 } while (skip_message
);
1137 /* s->init_num == SSL3_HM_HEADER_LENGTH */
1140 s
->s3
->tmp
.message_type
= *(p
++);
1142 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
1144 * Only happens with SSLv3+ in an SSLv2 backward compatible
1147 * Total message size is the remaining record bytes to read
1148 * plus the SSL3_HM_HEADER_LENGTH bytes that we already read
1150 l
= RECORD_LAYER_get_rrec_length(&s
->rlayer
)
1151 + SSL3_HM_HEADER_LENGTH
;
1152 s
->s3
->tmp
.message_size
= l
;
1154 s
->init_msg
= s
->init_buf
->data
;
1155 s
->init_num
= SSL3_HM_HEADER_LENGTH
;
1158 /* BUF_MEM_grow takes an 'int' parameter */
1159 if (l
> (INT_MAX
- SSL3_HM_HEADER_LENGTH
)) {
1160 al
= SSL_AD_ILLEGAL_PARAMETER
;
1161 SSLerr(SSL_F_TLS_GET_MESSAGE_HEADER
, SSL_R_EXCESSIVE_MESSAGE_SIZE
);
1164 s
->s3
->tmp
.message_size
= l
;
1166 s
->init_msg
= s
->init_buf
->data
+ SSL3_HM_HEADER_LENGTH
;
1172 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1176 int tls_get_message_body(SSL
*s
, size_t *len
)
1178 size_t n
, readbytes
;
1182 if (s
->s3
->tmp
.message_type
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
1183 /* We've already read everything in */
1184 *len
= (unsigned long)s
->init_num
;
1189 n
= s
->s3
->tmp
.message_size
- s
->init_num
;
1191 i
= s
->method
->ssl_read_bytes(s
, SSL3_RT_HANDSHAKE
, NULL
,
1192 &p
[s
->init_num
], n
, 0, &readbytes
);
1194 s
->rwstate
= SSL_READING
;
1198 s
->init_num
+= readbytes
;
1202 #ifndef OPENSSL_NO_NEXTPROTONEG
1204 * If receiving Finished, record MAC of prior handshake messages for
1205 * Finished verification.
1207 if (*s
->init_buf
->data
== SSL3_MT_FINISHED
)
1211 /* Feed this message into MAC computation. */
1212 if (RECORD_LAYER_is_sslv2_record(&s
->rlayer
)) {
1213 if (!ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1215 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY
, ERR_R_EVP_LIB
);
1216 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
1220 if (s
->msg_callback
)
1221 s
->msg_callback(0, SSL2_VERSION
, 0, s
->init_buf
->data
,
1222 (size_t)s
->init_num
, s
, s
->msg_callback_arg
);
1225 * We defer feeding in the HRR until later. We'll do it as part of
1226 * processing the message
1228 if (s
->s3
->tmp
.message_type
!= SSL3_MT_HELLO_RETRY_REQUEST
1229 && !ssl3_finish_mac(s
, (unsigned char *)s
->init_buf
->data
,
1230 s
->init_num
+ SSL3_HM_HEADER_LENGTH
)) {
1231 SSLerr(SSL_F_TLS_GET_MESSAGE_BODY
, ERR_R_EVP_LIB
);
1232 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
1236 if (s
->msg_callback
)
1237 s
->msg_callback(0, s
->version
, SSL3_RT_HANDSHAKE
, s
->init_buf
->data
,
1238 (size_t)s
->init_num
+ SSL3_HM_HEADER_LENGTH
, s
,
1239 s
->msg_callback_arg
);
1246 int ssl_verify_alarm_type(long type
)
1251 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT
:
1252 case X509_V_ERR_UNABLE_TO_GET_CRL
:
1253 case X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER
:
1254 al
= SSL_AD_UNKNOWN_CA
;
1256 case X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE
:
1257 case X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE
:
1258 case X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY
:
1259 case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD
:
1260 case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD
:
1261 case X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD
:
1262 case X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD
:
1263 case X509_V_ERR_CERT_NOT_YET_VALID
:
1264 case X509_V_ERR_CRL_NOT_YET_VALID
:
1265 case X509_V_ERR_CERT_UNTRUSTED
:
1266 case X509_V_ERR_CERT_REJECTED
:
1267 case X509_V_ERR_HOSTNAME_MISMATCH
:
1268 case X509_V_ERR_EMAIL_MISMATCH
:
1269 case X509_V_ERR_IP_ADDRESS_MISMATCH
:
1270 case X509_V_ERR_DANE_NO_MATCH
:
1271 case X509_V_ERR_EE_KEY_TOO_SMALL
:
1272 case X509_V_ERR_CA_KEY_TOO_SMALL
:
1273 case X509_V_ERR_CA_MD_TOO_WEAK
:
1274 al
= SSL_AD_BAD_CERTIFICATE
;
1276 case X509_V_ERR_CERT_SIGNATURE_FAILURE
:
1277 case X509_V_ERR_CRL_SIGNATURE_FAILURE
:
1278 al
= SSL_AD_DECRYPT_ERROR
;
1280 case X509_V_ERR_CERT_HAS_EXPIRED
:
1281 case X509_V_ERR_CRL_HAS_EXPIRED
:
1282 al
= SSL_AD_CERTIFICATE_EXPIRED
;
1284 case X509_V_ERR_CERT_REVOKED
:
1285 al
= SSL_AD_CERTIFICATE_REVOKED
;
1287 case X509_V_ERR_UNSPECIFIED
:
1288 case X509_V_ERR_OUT_OF_MEM
:
1289 case X509_V_ERR_INVALID_CALL
:
1290 case X509_V_ERR_STORE_LOOKUP
:
1291 al
= SSL_AD_INTERNAL_ERROR
;
1293 case X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT
:
1294 case X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN
:
1295 case X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
:
1296 case X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE
:
1297 case X509_V_ERR_CERT_CHAIN_TOO_LONG
:
1298 case X509_V_ERR_PATH_LENGTH_EXCEEDED
:
1299 case X509_V_ERR_INVALID_CA
:
1300 al
= SSL_AD_UNKNOWN_CA
;
1302 case X509_V_ERR_APPLICATION_VERIFICATION
:
1303 al
= SSL_AD_HANDSHAKE_FAILURE
;
1305 case X509_V_ERR_INVALID_PURPOSE
:
1306 al
= SSL_AD_UNSUPPORTED_CERTIFICATE
;
1309 al
= SSL_AD_CERTIFICATE_UNKNOWN
;
1315 int ssl_allow_compression(SSL
*s
)
1317 if (s
->options
& SSL_OP_NO_COMPRESSION
)
1319 return ssl_security(s
, SSL_SECOP_COMPRESSION
, 0, 0, NULL
);
1322 static int version_cmp(const SSL
*s
, int a
, int b
)
1324 int dtls
= SSL_IS_DTLS(s
);
1329 return a
< b
? -1 : 1;
1330 return DTLS_VERSION_LT(a
, b
) ? -1 : 1;
1335 const SSL_METHOD
*(*cmeth
) (void);
1336 const SSL_METHOD
*(*smeth
) (void);
1339 #if TLS_MAX_VERSION != TLS1_3_VERSION
1340 # error Code needs update for TLS_method() support beyond TLS1_3_VERSION.
1343 /* Must be in order high to low */
1344 static const version_info tls_version_table
[] = {
1345 #ifndef OPENSSL_NO_TLS1_3
1346 {TLS1_3_VERSION
, tlsv1_3_client_method
, tlsv1_3_server_method
},
1348 {TLS1_3_VERSION
, NULL
, NULL
},
1350 #ifndef OPENSSL_NO_TLS1_2
1351 {TLS1_2_VERSION
, tlsv1_2_client_method
, tlsv1_2_server_method
},
1353 {TLS1_2_VERSION
, NULL
, NULL
},
1355 #ifndef OPENSSL_NO_TLS1_1
1356 {TLS1_1_VERSION
, tlsv1_1_client_method
, tlsv1_1_server_method
},
1358 {TLS1_1_VERSION
, NULL
, NULL
},
1360 #ifndef OPENSSL_NO_TLS1
1361 {TLS1_VERSION
, tlsv1_client_method
, tlsv1_server_method
},
1363 {TLS1_VERSION
, NULL
, NULL
},
1365 #ifndef OPENSSL_NO_SSL3
1366 {SSL3_VERSION
, sslv3_client_method
, sslv3_server_method
},
1368 {SSL3_VERSION
, NULL
, NULL
},
1373 #if DTLS_MAX_VERSION != DTLS1_2_VERSION
1374 # error Code needs update for DTLS_method() support beyond DTLS1_2_VERSION.
1377 /* Must be in order high to low */
1378 static const version_info dtls_version_table
[] = {
1379 #ifndef OPENSSL_NO_DTLS1_2
1380 {DTLS1_2_VERSION
, dtlsv1_2_client_method
, dtlsv1_2_server_method
},
1382 {DTLS1_2_VERSION
, NULL
, NULL
},
1384 #ifndef OPENSSL_NO_DTLS1
1385 {DTLS1_VERSION
, dtlsv1_client_method
, dtlsv1_server_method
},
1386 {DTLS1_BAD_VER
, dtls_bad_ver_client_method
, NULL
},
1388 {DTLS1_VERSION
, NULL
, NULL
},
1389 {DTLS1_BAD_VER
, NULL
, NULL
},
1395 * ssl_method_error - Check whether an SSL_METHOD is enabled.
1397 * @s: The SSL handle for the candidate method
1398 * @method: the intended method.
1400 * Returns 0 on success, or an SSL error reason on failure.
1402 static int ssl_method_error(const SSL
*s
, const SSL_METHOD
*method
)
1404 int version
= method
->version
;
1406 if ((s
->min_proto_version
!= 0 &&
1407 version_cmp(s
, version
, s
->min_proto_version
) < 0) ||
1408 ssl_security(s
, SSL_SECOP_VERSION
, 0, version
, NULL
) == 0)
1409 return SSL_R_VERSION_TOO_LOW
;
1411 if (s
->max_proto_version
!= 0 &&
1412 version_cmp(s
, version
, s
->max_proto_version
) > 0)
1413 return SSL_R_VERSION_TOO_HIGH
;
1415 if ((s
->options
& method
->mask
) != 0)
1416 return SSL_R_UNSUPPORTED_PROTOCOL
;
1417 if ((method
->flags
& SSL_METHOD_NO_SUITEB
) != 0 && tls1_suiteb(s
))
1418 return SSL_R_AT_LEAST_TLS_1_2_NEEDED_IN_SUITEB_MODE
;
1424 * ssl_version_supported - Check that the specified `version` is supported by
1427 * @s: The SSL handle for the candidate method
1428 * @version: Protocol version to test against
1430 * Returns 1 when supported, otherwise 0
1432 int ssl_version_supported(const SSL
*s
, int version
)
1434 const version_info
*vent
;
1435 const version_info
*table
;
1437 switch (s
->method
->version
) {
1439 /* Version should match method version for non-ANY method */
1440 return version_cmp(s
, version
, s
->version
) == 0;
1441 case TLS_ANY_VERSION
:
1442 table
= tls_version_table
;
1444 case DTLS_ANY_VERSION
:
1445 table
= dtls_version_table
;
1450 vent
->version
!= 0 && version_cmp(s
, version
, vent
->version
) <= 0;
1452 if (vent
->cmeth
!= NULL
&&
1453 version_cmp(s
, version
, vent
->version
) == 0 &&
1454 ssl_method_error(s
, vent
->cmeth()) == 0) {
1462 * ssl_check_version_downgrade - In response to RFC7507 SCSV version
1463 * fallback indication from a client check whether we're using the highest
1464 * supported protocol version.
1466 * @s server SSL handle.
1468 * Returns 1 when using the highest enabled version, 0 otherwise.
1470 int ssl_check_version_downgrade(SSL
*s
)
1472 const version_info
*vent
;
1473 const version_info
*table
;
1476 * Check that the current protocol is the highest enabled version
1477 * (according to s->ctx->method, as version negotiation may have changed
1480 if (s
->version
== s
->ctx
->method
->version
)
1484 * Apparently we're using a version-flexible SSL_METHOD (not at its
1485 * highest protocol version).
1487 if (s
->ctx
->method
->version
== TLS_method()->version
)
1488 table
= tls_version_table
;
1489 else if (s
->ctx
->method
->version
== DTLS_method()->version
)
1490 table
= dtls_version_table
;
1492 /* Unexpected state; fail closed. */
1496 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1497 if (vent
->smeth
!= NULL
&& ssl_method_error(s
, vent
->smeth()) == 0)
1498 return s
->version
== vent
->version
;
1504 * ssl_set_version_bound - set an upper or lower bound on the supported (D)TLS
1505 * protocols, provided the initial (D)TLS method is version-flexible. This
1506 * function sanity-checks the proposed value and makes sure the method is
1507 * version-flexible, then sets the limit if all is well.
1509 * @method_version: The version of the current SSL_METHOD.
1510 * @version: the intended limit.
1511 * @bound: pointer to limit to be updated.
1513 * Returns 1 on success, 0 on failure.
1515 int ssl_set_version_bound(int method_version
, int version
, int *bound
)
1523 * Restrict TLS methods to TLS protocol versions.
1524 * Restrict DTLS methods to DTLS protocol versions.
1525 * Note, DTLS version numbers are decreasing, use comparison macros.
1527 * Note that for both lower-bounds we use explicit versions, not
1528 * (D)TLS_MIN_VERSION. This is because we don't want to break user
1529 * configurations. If the MIN (supported) version ever rises, the user's
1530 * "floor" remains valid even if no longer available. We don't expect the
1531 * MAX ceiling to ever get lower, so making that variable makes sense.
1533 switch (method_version
) {
1536 * XXX For fixed version methods, should we always fail and not set any
1537 * bounds, always succeed and not set any bounds, or set the bounds and
1538 * arrange to fail later if they are not met? At present fixed-version
1539 * methods are not subject to controls that disable individual protocol
1544 case TLS_ANY_VERSION
:
1545 if (version
< SSL3_VERSION
|| version
> TLS_MAX_VERSION
)
1549 case DTLS_ANY_VERSION
:
1550 if (DTLS_VERSION_GT(version
, DTLS_MAX_VERSION
) ||
1551 DTLS_VERSION_LT(version
, DTLS1_BAD_VER
))
1560 static void check_for_downgrade(SSL
*s
, int vers
, DOWNGRADE
*dgrd
)
1562 if (vers
== TLS1_2_VERSION
1563 && ssl_version_supported(s
, TLS1_3_VERSION
)) {
1564 *dgrd
= DOWNGRADE_TO_1_2
;
1565 } else if (!SSL_IS_DTLS(s
) && vers
< TLS1_2_VERSION
1566 && (ssl_version_supported(s
, TLS1_2_VERSION
)
1567 || ssl_version_supported(s
, TLS1_3_VERSION
))) {
1568 *dgrd
= DOWNGRADE_TO_1_1
;
1570 *dgrd
= DOWNGRADE_NONE
;
1575 * ssl_choose_server_version - Choose server (D)TLS version. Called when the
1576 * client HELLO is received to select the final server protocol version and
1577 * the version specific method.
1579 * @s: server SSL handle.
1581 * Returns 0 on success or an SSL error reason number on failure.
1583 int ssl_choose_server_version(SSL
*s
, CLIENTHELLO_MSG
*hello
, DOWNGRADE
*dgrd
)
1586 * With version-flexible methods we have an initial state with:
1588 * s->method->version == (D)TLS_ANY_VERSION,
1589 * s->version == (D)TLS_MAX_VERSION.
1591 * So we detect version-flexible methods via the method version, not the
1594 int server_version
= s
->method
->version
;
1595 int client_version
= hello
->legacy_version
;
1596 const version_info
*vent
;
1597 const version_info
*table
;
1599 RAW_EXTENSION
*suppversions
;
1601 s
->client_version
= client_version
;
1603 switch (server_version
) {
1605 if (!SSL_IS_TLS13(s
)) {
1606 if (version_cmp(s
, client_version
, s
->version
) < 0)
1607 return SSL_R_WRONG_SSL_VERSION
;
1608 *dgrd
= DOWNGRADE_NONE
;
1610 * If this SSL handle is not from a version flexible method we don't
1611 * (and never did) check min/max FIPS or Suite B constraints. Hope
1612 * that's OK. It is up to the caller to not choose fixed protocol
1613 * versions they don't want. If not, then easy to fix, just return
1614 * ssl_method_error(s, s->method)
1619 * Fall through if we are TLSv1.3 already (this means we must be after
1620 * a HelloRetryRequest
1623 case TLS_ANY_VERSION
:
1624 table
= tls_version_table
;
1626 case DTLS_ANY_VERSION
:
1627 table
= dtls_version_table
;
1631 suppversions
= &hello
->pre_proc_exts
[TLSEXT_IDX_supported_versions
];
1633 if (suppversions
->present
&& !SSL_IS_DTLS(s
)) {
1634 unsigned int candidate_vers
= 0;
1635 unsigned int best_vers
= 0;
1636 const SSL_METHOD
*best_method
= NULL
;
1637 PACKET versionslist
;
1639 suppversions
->parsed
= 1;
1641 if (!PACKET_as_length_prefixed_1(&suppversions
->data
, &versionslist
)) {
1642 /* Trailing or invalid data? */
1643 return SSL_R_LENGTH_MISMATCH
;
1646 while (PACKET_get_net_2(&versionslist
, &candidate_vers
)) {
1647 /* TODO(TLS1.3): Remove this before release */
1648 if (candidate_vers
== TLS1_3_VERSION_DRAFT
)
1649 candidate_vers
= TLS1_3_VERSION
;
1651 * TODO(TLS1.3): There is some discussion on the TLS list about
1652 * whether to ignore versions <TLS1.2 in supported_versions. At the
1653 * moment we honour them if present. To be reviewed later
1655 if (version_cmp(s
, candidate_vers
, best_vers
) <= 0)
1658 vent
->version
!= 0 && vent
->version
!= (int)candidate_vers
;
1661 if (vent
->version
!= 0 && vent
->smeth
!= NULL
) {
1662 const SSL_METHOD
*method
;
1664 method
= vent
->smeth();
1665 if (ssl_method_error(s
, method
) == 0) {
1666 best_vers
= candidate_vers
;
1667 best_method
= method
;
1671 if (PACKET_remaining(&versionslist
) != 0) {
1672 /* Trailing data? */
1673 return SSL_R_LENGTH_MISMATCH
;
1676 if (best_vers
> 0) {
1677 if (SSL_IS_TLS13(s
)) {
1679 * We get here if this is after a HelloRetryRequest. In this
1680 * case we just check that we still negotiated TLSv1.3
1682 if (best_vers
!= TLS1_3_VERSION
)
1683 return SSL_R_UNSUPPORTED_PROTOCOL
;
1686 check_for_downgrade(s
, best_vers
, dgrd
);
1687 s
->version
= best_vers
;
1688 s
->method
= best_method
;
1691 return SSL_R_UNSUPPORTED_PROTOCOL
;
1695 * If the supported versions extension isn't present, then the highest
1696 * version we can negotiate is TLSv1.2
1698 if (version_cmp(s
, client_version
, TLS1_3_VERSION
) >= 0)
1699 client_version
= TLS1_2_VERSION
;
1702 * No supported versions extension, so we just use the version supplied in
1705 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1706 const SSL_METHOD
*method
;
1708 if (vent
->smeth
== NULL
||
1709 version_cmp(s
, client_version
, vent
->version
) < 0)
1711 method
= vent
->smeth();
1712 if (ssl_method_error(s
, method
) == 0) {
1713 check_for_downgrade(s
, vent
->version
, dgrd
);
1714 s
->version
= vent
->version
;
1720 return disabled
? SSL_R_UNSUPPORTED_PROTOCOL
: SSL_R_VERSION_TOO_LOW
;
1724 * ssl_choose_client_version - Choose client (D)TLS version. Called when the
1725 * server HELLO is received to select the final client protocol version and
1726 * the version specific method.
1728 * @s: client SSL handle.
1729 * @version: The proposed version from the server's HELLO.
1730 * @checkdgrd: Whether to check the downgrade sentinels in the server_random
1731 * @al: Where to store any alert value that may be generated
1733 * Returns 0 on success or an SSL error reason number on failure.
1735 int ssl_choose_client_version(SSL
*s
, int version
, int checkdgrd
, int *al
)
1737 const version_info
*vent
;
1738 const version_info
*table
;
1741 /* TODO(TLS1.3): Remove this before release */
1742 if (version
== TLS1_3_VERSION_DRAFT
)
1743 version
= TLS1_3_VERSION
;
1745 if (s
->hello_retry_request
&& version
!= TLS1_3_VERSION
) {
1746 *al
= SSL_AD_PROTOCOL_VERSION
;
1747 return SSL_R_WRONG_SSL_VERSION
;
1750 switch (s
->method
->version
) {
1752 if (version
!= s
->version
) {
1753 *al
= SSL_AD_PROTOCOL_VERSION
;
1754 return SSL_R_WRONG_SSL_VERSION
;
1757 * If this SSL handle is not from a version flexible method we don't
1758 * (and never did) check min/max, FIPS or Suite B constraints. Hope
1759 * that's OK. It is up to the caller to not choose fixed protocol
1760 * versions they don't want. If not, then easy to fix, just return
1761 * ssl_method_error(s, s->method)
1764 case TLS_ANY_VERSION
:
1765 table
= tls_version_table
;
1767 case DTLS_ANY_VERSION
:
1768 table
= dtls_version_table
;
1772 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1773 const SSL_METHOD
*method
;
1776 if (vent
->cmeth
== NULL
)
1779 if (highver
!= 0 && version
!= vent
->version
)
1782 method
= vent
->cmeth();
1783 err
= ssl_method_error(s
, method
);
1785 if (version
== vent
->version
) {
1786 *al
= SSL_AD_PROTOCOL_VERSION
;
1793 highver
= vent
->version
;
1795 if (version
!= vent
->version
)
1798 #ifndef OPENSSL_NO_TLS13DOWNGRADE
1799 /* Check for downgrades */
1801 if (version
== TLS1_2_VERSION
&& highver
> version
) {
1802 if (memcmp(tls12downgrade
,
1803 s
->s3
->server_random
+ SSL3_RANDOM_SIZE
1804 - sizeof(tls12downgrade
),
1805 sizeof(tls12downgrade
)) == 0) {
1806 *al
= SSL_AD_ILLEGAL_PARAMETER
;
1807 return SSL_R_INAPPROPRIATE_FALLBACK
;
1809 } else if (!SSL_IS_DTLS(s
)
1810 && version
< TLS1_2_VERSION
1811 && highver
> version
) {
1812 if (memcmp(tls11downgrade
,
1813 s
->s3
->server_random
+ SSL3_RANDOM_SIZE
1814 - sizeof(tls11downgrade
),
1815 sizeof(tls11downgrade
)) == 0) {
1816 *al
= SSL_AD_ILLEGAL_PARAMETER
;
1817 return SSL_R_INAPPROPRIATE_FALLBACK
;
1824 s
->version
= version
;
1828 *al
= SSL_AD_PROTOCOL_VERSION
;
1829 return SSL_R_UNSUPPORTED_PROTOCOL
;
1833 * ssl_get_min_max_version - get minimum and maximum protocol version
1834 * @s: The SSL connection
1835 * @min_version: The minimum supported version
1836 * @max_version: The maximum supported version
1838 * Work out what version we should be using for the initial ClientHello if the
1839 * version is initially (D)TLS_ANY_VERSION. We apply any explicit SSL_OP_NO_xxx
1840 * options, the MinProtocol and MaxProtocol configuration commands, any Suite B
1841 * constraints and any floor imposed by the security level here,
1842 * so we don't advertise the wrong protocol version to only reject the outcome later.
1844 * Computing the right floor matters. If, e.g., TLS 1.0 and 1.2 are enabled,
1845 * TLS 1.1 is disabled, but the security level, Suite-B and/or MinProtocol
1846 * only allow TLS 1.2, we want to advertise TLS1.2, *not* TLS1.
1848 * Returns 0 on success or an SSL error reason number on failure. On failure
1849 * min_version and max_version will also be set to 0.
1851 int ssl_get_min_max_version(const SSL
*s
, int *min_version
, int *max_version
)
1855 const SSL_METHOD
*single
= NULL
;
1856 const SSL_METHOD
*method
;
1857 const version_info
*table
;
1858 const version_info
*vent
;
1860 switch (s
->method
->version
) {
1863 * If this SSL handle is not from a version flexible method we don't
1864 * (and never did) check min/max FIPS or Suite B constraints. Hope
1865 * that's OK. It is up to the caller to not choose fixed protocol
1866 * versions they don't want. If not, then easy to fix, just return
1867 * ssl_method_error(s, s->method)
1869 *min_version
= *max_version
= s
->version
;
1871 case TLS_ANY_VERSION
:
1872 table
= tls_version_table
;
1874 case DTLS_ANY_VERSION
:
1875 table
= dtls_version_table
;
1880 * SSL_OP_NO_X disables all protocols above X *if* there are some protocols
1881 * below X enabled. This is required in order to maintain the "version
1882 * capability" vector contiguous. Any versions with a NULL client method
1883 * (protocol version client is disabled at compile-time) is also a "hole".
1885 * Our initial state is hole == 1, version == 0. That is, versions above
1886 * the first version in the method table are disabled (a "hole" above
1887 * the valid protocol entries) and we don't have a selected version yet.
1889 * Whenever "hole == 1", and we hit an enabled method, its version becomes
1890 * the selected version, and the method becomes a candidate "single"
1891 * method. We're no longer in a hole, so "hole" becomes 0.
1893 * If "hole == 0" and we hit an enabled method, then "single" is cleared,
1894 * as we support a contiguous range of at least two methods. If we hit
1895 * a disabled method, then hole becomes true again, but nothing else
1896 * changes yet, because all the remaining methods may be disabled too.
1897 * If we again hit an enabled method after the new hole, it becomes
1898 * selected, as we start from scratch.
1900 *min_version
= version
= 0;
1902 for (vent
= table
; vent
->version
!= 0; ++vent
) {
1904 * A table entry with a NULL client method is still a hole in the
1905 * "version capability" vector.
1907 if (vent
->cmeth
== NULL
) {
1911 method
= vent
->cmeth();
1912 if (ssl_method_error(s
, method
) != 0) {
1916 *min_version
= method
->version
;
1918 version
= (single
= method
)->version
;
1919 *min_version
= version
;
1924 *max_version
= version
;
1926 /* Fail if everything is disabled */
1928 return SSL_R_NO_PROTOCOLS_AVAILABLE
;
1934 * ssl_set_client_hello_version - Work out what version we should be using for
1935 * the initial ClientHello.legacy_version field.
1937 * @s: client SSL handle.
1939 * Returns 0 on success or an SSL error reason number on failure.
1941 int ssl_set_client_hello_version(SSL
*s
)
1943 int ver_min
, ver_max
, ret
;
1945 ret
= ssl_get_min_max_version(s
, &ver_min
, &ver_max
);
1950 s
->version
= ver_max
;
1952 /* TLS1.3 always uses TLS1.2 in the legacy_version field */
1953 if (!SSL_IS_DTLS(s
) && ver_max
> TLS1_2_VERSION
)
1954 ver_max
= TLS1_2_VERSION
;
1956 s
->client_version
= ver_max
;
1961 * Checks a list of |groups| to determine if the |group_id| is in it. If it is
1962 * and |checkallow| is 1 then additionally check if the group is allowed to be
1963 * used. Returns 1 if the group is in the list (and allowed if |checkallow| is
1964 * 1) or 0 otherwise.
1966 #ifndef OPENSSL_NO_EC
1967 int check_in_list(SSL
*s
, uint16_t group_id
, const uint16_t *groups
,
1968 size_t num_groups
, int checkallow
)
1972 if (groups
== NULL
|| num_groups
== 0)
1975 for (i
= 0; i
< num_groups
; i
++) {
1976 uint16_t group
= groups
[i
];
1978 if (group_id
== group
1980 || tls_curve_allowed(s
, group
, SSL_SECOP_CURVE_CHECK
))) {
1989 /* Replace ClientHello1 in the transcript hash with a synthetic message */
1990 int create_synthetic_message_hash(SSL
*s
)
1992 unsigned char hashval
[EVP_MAX_MD_SIZE
];
1994 unsigned char msghdr
[SSL3_HM_HEADER_LENGTH
];
1996 memset(msghdr
, 0, sizeof(msghdr
));
1998 /* Get the hash of the initial ClientHello */
1999 if (!ssl3_digest_cached_records(s
, 0)
2000 || !ssl_handshake_hash(s
, hashval
, sizeof(hashval
), &hashlen
)) {
2001 SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH
, ERR_R_INTERNAL_ERROR
);
2005 /* Reinitialise the transcript hash */
2006 if (!ssl3_init_finished_mac(s
))
2009 /* Inject the synthetic message_hash message */
2010 msghdr
[0] = SSL3_MT_MESSAGE_HASH
;
2011 msghdr
[SSL3_HM_HEADER_LENGTH
- 1] = hashlen
;
2012 if (!ssl3_finish_mac(s
, msghdr
, SSL3_HM_HEADER_LENGTH
)
2013 || !ssl3_finish_mac(s
, hashval
, hashlen
)) {
2014 SSLerr(SSL_F_CREATE_SYNTHETIC_MESSAGE_HASH
, ERR_R_INTERNAL_ERROR
);
2021 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
2023 return X509_NAME_cmp(*a
, *b
);
2026 int parse_ca_names(SSL
*s
, PACKET
*pkt
, int *al
)
2028 STACK_OF(X509_NAME
) *ca_sk
= sk_X509_NAME_new(ca_dn_cmp
);
2029 X509_NAME
*xn
= NULL
;
2032 if (ca_sk
== NULL
) {
2033 SSLerr(SSL_F_PARSE_CA_NAMES
, ERR_R_MALLOC_FAILURE
);
2036 /* get the CA RDNs */
2037 if (!PACKET_get_length_prefixed_2(pkt
, &cadns
)) {
2038 *al
= SSL_AD_DECODE_ERROR
;
2039 SSLerr(SSL_F_PARSE_CA_NAMES
, SSL_R_LENGTH_MISMATCH
);
2043 while (PACKET_remaining(&cadns
)) {
2044 const unsigned char *namestart
, *namebytes
;
2045 unsigned int name_len
;
2047 if (!PACKET_get_net_2(&cadns
, &name_len
)
2048 || !PACKET_get_bytes(&cadns
, &namebytes
, name_len
)) {
2049 SSLerr(SSL_F_PARSE_CA_NAMES
, SSL_R_LENGTH_MISMATCH
);
2053 namestart
= namebytes
;
2054 if ((xn
= d2i_X509_NAME(NULL
, &namebytes
, name_len
)) == NULL
) {
2055 SSLerr(SSL_F_PARSE_CA_NAMES
, ERR_R_ASN1_LIB
);
2058 if (namebytes
!= (namestart
+ name_len
)) {
2059 SSLerr(SSL_F_PARSE_CA_NAMES
, SSL_R_CA_DN_LENGTH_MISMATCH
);
2063 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
2064 SSLerr(SSL_F_PARSE_CA_NAMES
, ERR_R_MALLOC_FAILURE
);
2065 *al
= SSL_AD_INTERNAL_ERROR
;
2071 sk_X509_NAME_pop_free(s
->s3
->tmp
.peer_ca_names
, X509_NAME_free
);
2072 s
->s3
->tmp
.peer_ca_names
= ca_sk
;
2077 *al
= SSL_AD_DECODE_ERROR
;
2079 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
2084 int construct_ca_names(SSL
*s
, WPACKET
*pkt
)
2086 const STACK_OF(X509_NAME
) *ca_sk
= SSL_get0_CA_list(s
);
2088 /* Start sub-packet for client CA list */
2089 if (!WPACKET_start_sub_packet_u16(pkt
))
2092 if (ca_sk
!= NULL
) {
2095 for (i
= 0; i
< sk_X509_NAME_num(ca_sk
); i
++) {
2096 unsigned char *namebytes
;
2097 X509_NAME
*name
= sk_X509_NAME_value(ca_sk
, i
);
2101 || (namelen
= i2d_X509_NAME(name
, NULL
)) < 0
2102 || !WPACKET_sub_allocate_bytes_u16(pkt
, namelen
,
2104 || i2d_X509_NAME(name
, &namebytes
) != namelen
) {
2110 if (!WPACKET_close(pkt
))
2116 /* Create a buffer containing data to be signed for server key exchange */
2117 size_t construct_key_exchange_tbs(const SSL
*s
, unsigned char **ptbs
,
2118 const void *param
, size_t paramlen
)
2120 size_t tbslen
= 2 * SSL3_RANDOM_SIZE
+ paramlen
;
2121 unsigned char *tbs
= OPENSSL_malloc(tbslen
);
2125 memcpy(tbs
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
2126 memcpy(tbs
+ SSL3_RANDOM_SIZE
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
);
2128 memcpy(tbs
+ SSL3_RANDOM_SIZE
* 2, param
, paramlen
);