1 /* ssl/statem/statem_clnt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
58 /* ====================================================================
59 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in
70 * the documentation and/or other materials provided with the
73 * 3. All advertising materials mentioning features or use of this
74 * software must display the following acknowledgment:
75 * "This product includes software developed by the OpenSSL Project
76 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
78 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79 * endorse or promote products derived from this software without
80 * prior written permission. For written permission, please contact
81 * openssl-core@openssl.org.
83 * 5. Products derived from this software may not be called "OpenSSL"
84 * nor may "OpenSSL" appear in their names without prior written
85 * permission of the OpenSSL Project.
87 * 6. Redistributions of any form whatsoever must retain the following
89 * "This product includes software developed by the OpenSSL Project
90 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
92 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103 * OF THE POSSIBILITY OF SUCH DAMAGE.
104 * ====================================================================
106 * This product includes cryptographic software written by Eric Young
107 * (eay@cryptsoft.com). This product includes software written by Tim
108 * Hudson (tjh@cryptsoft.com).
111 /* ====================================================================
112 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
114 * Portions of the attached software ("Contribution") are developed by
115 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
117 * The Contribution is licensed pursuant to the OpenSSL open source
118 * license provided above.
120 * ECC cipher suite support in OpenSSL originally written by
121 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
124 /* ====================================================================
125 * Copyright 2005 Nokia. All rights reserved.
127 * The portions of the attached software ("Contribution") is developed by
128 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
131 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133 * support (see RFC 4279) to OpenSSL.
135 * No patent licenses or other rights except those expressly stated in
136 * the OpenSSL open source license shall be deemed granted or received
137 * expressly, by implication, estoppel, or otherwise.
139 * No assurances are provided by Nokia that the Contribution does not
140 * infringe the patent or other intellectual property rights of any third
141 * party or that the license provides you with all the necessary rights
142 * to make use of the Contribution.
144 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
152 #include "../ssl_locl.h"
153 #include "statem_locl.h"
154 #include <openssl/buffer.h>
155 #include <openssl/rand.h>
156 #include <openssl/objects.h>
157 #include <openssl/evp.h>
158 #include <openssl/md5.h>
159 #ifndef OPENSSL_NO_DH
160 # include <openssl/dh.h>
162 #include <openssl/bn.h>
163 #ifndef OPENSSL_NO_ENGINE
164 # include <openssl/engine.h>
167 static inline int cert_req_allowed(SSL *s);
168 static int key_exchange_expected(SSL *s);
169 static int ssl_set_version(SSL *s);
170 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
171 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
176 * Is a CertificateRequest message allowed at the moment or not?
182 static inline int cert_req_allowed(SSL *s)
184 /* TLS does not like anon-DH with client cert */
185 if (s->version > SSL3_VERSION
186 && (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL))
193 * Should we expect the ServerKeyExchange message or not?
200 static int key_exchange_expected(SSL *s)
202 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
205 * Can't skip server key exchange if this is an ephemeral
206 * ciphersuite or for SRP
208 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
214 * Export ciphersuites may have temporary RSA keys if the public key in the
215 * server certificate is longer than the maximum export strength
217 if ((alg_k & SSL_kRSA) && SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
220 pkey = X509_get_pubkey(s->session->peer);
225 * If the public key in the certificate is shorter than or equal to the
226 * maximum export strength then a temporary RSA key is not allowed
228 if (EVP_PKEY_bits(pkey)
229 <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
241 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
242 * handshake state transitions when the client is reading messages from the
243 * server. The message type that the server has sent is provided in |mt|. The
244 * current state is in |s->statem.hand_state|.
247 * 1: Success (transition allowed)
248 * 0: Error (transition not allowed)
250 int ossl_statem_client_read_transition(SSL *s, int mt)
252 OSSL_STATEM *st = &s->statem;
255 switch(st->hand_state) {
256 case TLS_ST_CW_CLNT_HELLO:
257 if (mt == SSL3_MT_SERVER_HELLO) {
258 st->hand_state = TLS_ST_CR_SRVR_HELLO;
262 if (SSL_IS_DTLS(s)) {
263 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
264 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
270 case TLS_ST_CR_SRVR_HELLO:
272 if (s->tlsext_ticket_expected) {
273 if (mt == SSL3_MT_NEWSESSION_TICKET) {
274 st->hand_state = TLS_ST_CR_SESSION_TICKET;
277 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
278 st->hand_state = TLS_ST_CR_CHANGE;
282 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
283 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
285 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
286 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
287 if (mt == SSL3_MT_CERTIFICATE) {
288 st->hand_state = TLS_ST_CR_CERT;
292 ske_expected = key_exchange_expected(s);
293 if (ske_expected < 0)
295 /* SKE is optional for some PSK ciphersuites */
297 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
298 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
299 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
300 st->hand_state = TLS_ST_CR_KEY_EXCH;
303 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
304 && cert_req_allowed(s)) {
305 st->hand_state = TLS_ST_CR_CERT_REQ;
307 } else if (mt == SSL3_MT_SERVER_DONE) {
308 st->hand_state = TLS_ST_CR_SRVR_DONE;
316 if (s->tlsext_status_expected) {
317 if (mt == SSL3_MT_CERTIFICATE_STATUS) {
318 st->hand_state = TLS_ST_CR_CERT_STATUS;
325 case TLS_ST_CR_CERT_STATUS:
326 ske_expected = key_exchange_expected(s);
327 if (ske_expected < 0)
329 /* SKE is optional for some PSK ciphersuites */
331 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
332 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
333 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
334 st->hand_state = TLS_ST_CR_KEY_EXCH;
341 case TLS_ST_CR_KEY_EXCH:
342 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
343 if (cert_req_allowed(s)) {
344 st->hand_state = TLS_ST_CR_CERT_REQ;
351 case TLS_ST_CR_CERT_REQ:
352 if (mt == SSL3_MT_SERVER_DONE) {
353 st->hand_state = TLS_ST_CR_SRVR_DONE;
358 case TLS_ST_CW_FINISHED:
359 if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
360 st->hand_state = TLS_ST_CR_SESSION_TICKET;
362 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
363 st->hand_state = TLS_ST_CR_CHANGE;
368 case TLS_ST_CR_SESSION_TICKET:
369 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
370 st->hand_state = TLS_ST_CR_CHANGE;
375 case TLS_ST_CR_CHANGE:
376 if (mt == SSL3_MT_FINISHED) {
377 st->hand_state = TLS_ST_CR_FINISHED;
386 /* No valid transition found */
391 * client_write_transition() works out what handshake state to move to next
392 * when the client is writing messages to be sent to the server.
394 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
396 OSSL_STATEM *st = &s->statem;
398 switch(st->hand_state) {
400 /* Renegotiation - fall through */
402 st->hand_state = TLS_ST_CW_CLNT_HELLO;
403 return WRITE_TRAN_CONTINUE;
405 case TLS_ST_CW_CLNT_HELLO:
407 * No transition at the end of writing because we don't know what
410 return WRITE_TRAN_FINISHED;
412 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
413 st->hand_state = TLS_ST_CW_CLNT_HELLO;
414 return WRITE_TRAN_CONTINUE;
416 case TLS_ST_CR_SRVR_DONE:
417 if (s->s3->tmp.cert_req)
418 st->hand_state = TLS_ST_CW_CERT;
420 st->hand_state = TLS_ST_CW_KEY_EXCH;
421 return WRITE_TRAN_CONTINUE;
424 st->hand_state = TLS_ST_CW_KEY_EXCH;
425 return WRITE_TRAN_CONTINUE;
427 case TLS_ST_CW_KEY_EXCH:
429 * For TLS, cert_req is set to 2, so a cert chain of nothing is
430 * sent, but no verify packet is sent
433 * XXX: For now, we do not support client authentication in ECDH
434 * cipher suites with ECDH (rather than ECDSA) certificates. We
435 * need to skip the certificate verify message when client's
436 * ECDH public key is sent inside the client certificate.
438 if (s->s3->tmp.cert_req == 1) {
439 st->hand_state = TLS_ST_CW_CERT_VRFY;
441 st->hand_state = TLS_ST_CW_CHANGE;
443 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
444 st->hand_state = TLS_ST_CW_CHANGE;
446 return WRITE_TRAN_CONTINUE;
448 case TLS_ST_CW_CERT_VRFY:
449 st->hand_state = TLS_ST_CW_CHANGE;
450 return WRITE_TRAN_CONTINUE;
452 case TLS_ST_CW_CHANGE:
453 #if defined(OPENSSL_NO_NEXTPROTONEG)
454 st->hand_state = TLS_ST_CW_FINISHED;
456 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
457 st->hand_state = TLS_ST_CW_NEXT_PROTO;
459 st->hand_state = TLS_ST_CW_FINISHED;
461 return WRITE_TRAN_CONTINUE;
463 #if !defined(OPENSSL_NO_NEXTPROTONEG)
464 case TLS_ST_CW_NEXT_PROTO:
465 st->hand_state = TLS_ST_CW_FINISHED;
466 return WRITE_TRAN_CONTINUE;
469 case TLS_ST_CW_FINISHED:
471 st->hand_state = TLS_ST_OK;
472 ossl_statem_set_in_init(s, 0);
473 return WRITE_TRAN_CONTINUE;
475 return WRITE_TRAN_FINISHED;
478 case TLS_ST_CR_FINISHED:
480 st->hand_state = TLS_ST_CW_CHANGE;
481 return WRITE_TRAN_CONTINUE;
483 st->hand_state = TLS_ST_OK;
484 ossl_statem_set_in_init(s, 0);
485 return WRITE_TRAN_CONTINUE;
489 /* Shouldn't happen */
490 return WRITE_TRAN_ERROR;
495 * Perform any pre work that needs to be done prior to sending a message from
496 * the client to the server.
498 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
500 OSSL_STATEM *st = &s->statem;
502 switch(st->hand_state) {
503 case TLS_ST_CW_CLNT_HELLO:
505 if (SSL_IS_DTLS(s)) {
506 /* every DTLS ClientHello resets Finished MAC */
507 ssl3_init_finished_mac(s);
512 return tls_prepare_client_certificate(s, wst);
514 case TLS_ST_CW_CHANGE:
515 if (SSL_IS_DTLS(s)) {
518 * We're into the last flight so we don't retransmit these
519 * messages unless we need to.
523 #ifndef OPENSSL_NO_SCTP
524 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
525 return dtls_wait_for_dry(s);
528 return WORK_FINISHED_CONTINUE;
531 return tls_finish_handshake(s, wst);
534 /* No pre work to be done */
538 return WORK_FINISHED_CONTINUE;
542 * Perform any work that needs to be done after sending a message from the
543 * client to the server.
545 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
547 OSSL_STATEM *st = &s->statem;
551 switch(st->hand_state) {
552 case TLS_ST_CW_CLNT_HELLO:
553 if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
555 #ifndef OPENSSL_NO_SCTP
556 /* Disable buffering for SCTP */
557 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
560 * turn on buffering for the next lot of output
562 if (s->bbio != s->wbio)
563 s->wbio = BIO_push(s->bbio, s->wbio);
564 #ifndef OPENSSL_NO_SCTP
567 if (SSL_IS_DTLS(s)) {
568 /* Treat the next message as the first packet */
573 case TLS_ST_CW_KEY_EXCH:
574 if (tls_client_key_exchange_post_work(s) == 0)
578 case TLS_ST_CW_CHANGE:
579 s->session->cipher = s->s3->tmp.new_cipher;
580 #ifdef OPENSSL_NO_COMP
581 s->session->compress_meth = 0;
583 if (s->s3->tmp.new_compression == NULL)
584 s->session->compress_meth = 0;
586 s->session->compress_meth = s->s3->tmp.new_compression->id;
588 if (!s->method->ssl3_enc->setup_key_block(s))
591 if (!s->method->ssl3_enc->change_cipher_state(s,
592 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
595 if (SSL_IS_DTLS(s)) {
596 #ifndef OPENSSL_NO_SCTP
599 * Change to new shared key of SCTP-Auth, will be ignored if
602 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
607 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
611 case TLS_ST_CW_FINISHED:
612 #ifndef OPENSSL_NO_SCTP
613 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
615 * Change to new shared key of SCTP-Auth, will be ignored if
618 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
622 if (statem_flush(s) != 1)
625 if (s->hit && tls_finish_handshake(s, WORK_MORE_A) != 1)
630 /* No post work to be done */
634 return WORK_FINISHED_CONTINUE;
638 * Construct a message to be sent from the client to the server.
640 * Valid return values are:
644 int ossl_statem_client_construct_message(SSL *s)
646 OSSL_STATEM *st = &s->statem;
648 switch(st->hand_state) {
649 case TLS_ST_CW_CLNT_HELLO:
650 return tls_construct_client_hello(s);
653 return tls_construct_client_certificate(s);
655 case TLS_ST_CW_KEY_EXCH:
656 return tls_construct_client_key_exchange(s);
658 case TLS_ST_CW_CERT_VRFY:
659 return tls_construct_client_verify(s);
661 case TLS_ST_CW_CHANGE:
663 return dtls_construct_change_cipher_spec(s);
665 return tls_construct_change_cipher_spec(s);
667 #if !defined(OPENSSL_NO_NEXTPROTONEG)
668 case TLS_ST_CW_NEXT_PROTO:
669 return tls_construct_next_proto(s);
671 case TLS_ST_CW_FINISHED:
672 return tls_construct_finished(s,
674 ssl3_enc->client_finished_label,
676 ssl3_enc->client_finished_label_len);
679 /* Shouldn't happen */
687 * Returns the maximum allowed length for the current message that we are
688 * reading. Excludes the message header.
690 unsigned long ossl_statem_client_max_message_size(SSL *s)
692 OSSL_STATEM *st = &s->statem;
694 switch(st->hand_state) {
695 case TLS_ST_CR_SRVR_HELLO:
696 return SERVER_HELLO_MAX_LENGTH;
698 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
699 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
702 return s->max_cert_list;
704 case TLS_ST_CR_CERT_STATUS:
705 return SSL3_RT_MAX_PLAIN_LENGTH;
707 case TLS_ST_CR_KEY_EXCH:
708 return SERVER_KEY_EXCH_MAX_LENGTH;
710 case TLS_ST_CR_CERT_REQ:
711 return SSL3_RT_MAX_PLAIN_LENGTH;
713 case TLS_ST_CR_SRVR_DONE:
714 return SERVER_HELLO_DONE_MAX_LENGTH;
716 case TLS_ST_CR_CHANGE:
717 return CCS_MAX_LENGTH;
719 case TLS_ST_CR_SESSION_TICKET:
720 return SSL3_RT_MAX_PLAIN_LENGTH;
722 case TLS_ST_CR_FINISHED:
723 return FINISHED_MAX_LENGTH;
726 /* Shouldn't happen */
734 * Process a message that the client has been received from the server.
736 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
738 OSSL_STATEM *st = &s->statem;
740 switch(st->hand_state) {
741 case TLS_ST_CR_SRVR_HELLO:
742 return tls_process_server_hello(s, pkt);
744 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
745 return dtls_process_hello_verify(s, pkt);
748 return tls_process_server_certificate(s, pkt);
750 case TLS_ST_CR_CERT_STATUS:
751 return tls_process_cert_status(s, pkt);
753 case TLS_ST_CR_KEY_EXCH:
754 return tls_process_key_exchange(s, pkt);
756 case TLS_ST_CR_CERT_REQ:
757 return tls_process_certificate_request(s, pkt);
759 case TLS_ST_CR_SRVR_DONE:
760 return tls_process_server_done(s, pkt);
762 case TLS_ST_CR_CHANGE:
763 return tls_process_change_cipher_spec(s, pkt);
765 case TLS_ST_CR_SESSION_TICKET:
766 return tls_process_new_session_ticket(s, pkt);
768 case TLS_ST_CR_FINISHED:
769 return tls_process_finished(s, pkt);
772 /* Shouldn't happen */
776 return MSG_PROCESS_ERROR;
780 * Perform any further processing required following the receipt of a message
783 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
785 OSSL_STATEM *st = &s->statem;
787 switch(st->hand_state) {
788 #ifndef OPENSSL_NO_SCTP
789 case TLS_ST_CR_SRVR_DONE:
790 /* We only get here if we are using SCTP and we are renegotiating */
791 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
792 s->s3->in_read_app_data = 2;
793 s->rwstate = SSL_READING;
794 BIO_clear_retry_flags(SSL_get_rbio(s));
795 BIO_set_retry_read(SSL_get_rbio(s));
796 ossl_statem_set_sctp_read_sock(s, 1);
799 ossl_statem_set_sctp_read_sock(s, 0);
800 return WORK_FINISHED_STOP;
803 case TLS_ST_CR_FINISHED:
805 return tls_finish_handshake(s, wst);
807 return WORK_FINISHED_STOP;
812 /* Shouldn't happen */
817 * Work out what version we should be using for the initial ClientHello if
818 * the version is currently set to (D)TLS_ANY_VERSION.
819 * Returns 1 on success
822 static int ssl_set_version(SSL *s)
824 unsigned long mask, options = s->options;
826 if (s->method->version == TLS_ANY_VERSION) {
828 * SSL_OP_NO_X disables all protocols above X *if* there are
829 * some protocols below X enabled. This is required in order
830 * to maintain "version capability" vector contiguous. So
831 * that if application wants to disable TLS1.0 in favour of
832 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
833 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
835 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
836 #if !defined(OPENSSL_NO_SSL3)
840 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
841 if (options & SSL_OP_NO_TLSv1_2) {
842 if ((options & mask) != mask) {
843 s->version = TLS1_1_VERSION;
845 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
849 s->version = TLS1_2_VERSION;
852 if ((options & mask) == mask) {
853 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
856 s->version = TLS1_1_VERSION;
859 mask &= ~SSL_OP_NO_TLSv1_1;
860 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
861 s->version = TLS1_VERSION;
862 mask &= ~SSL_OP_NO_TLSv1;
863 #if !defined(OPENSSL_NO_SSL3)
864 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
865 s->version = SSL3_VERSION;
868 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
869 SSLerr(SSL_F_SSL_SET_VERSION,
870 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
874 if (s->version == SSL3_VERSION && FIPS_mode()) {
875 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
879 } else if (s->method->version == DTLS_ANY_VERSION) {
880 /* Determine which DTLS version to use */
881 /* If DTLS 1.2 disabled correct the version number */
882 if (options & SSL_OP_NO_DTLSv1_2) {
883 if (tls1_suiteb(s)) {
884 SSLerr(SSL_F_SSL_SET_VERSION,
885 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
889 * Disabling all versions is silly: return an error.
891 if (options & SSL_OP_NO_DTLSv1) {
892 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
896 * Update method so we don't use any DTLS 1.2 features.
898 s->method = DTLSv1_client_method();
899 s->version = DTLS1_VERSION;
902 * We only support one version: update method
904 if (options & SSL_OP_NO_DTLSv1)
905 s->method = DTLSv1_2_client_method();
906 s->version = DTLS1_2_VERSION;
910 s->client_version = s->version;
915 int tls_construct_client_hello(SSL *s)
918 unsigned char *p, *d;
922 #ifndef OPENSSL_NO_COMP
926 SSL_SESSION *sess = s->session;
928 buf = (unsigned char *)s->init_buf->data;
930 /* Work out what SSL/TLS/DTLS version to use */
931 if (ssl_set_version(s) == 0)
934 if ((sess == NULL) || (sess->ssl_version != s->version) ||
936 * In the case of EAP-FAST, we can have a pre-shared
937 * "ticket" without a session ID.
939 (!sess->session_id_length && !sess->tlsext_tick) ||
940 (sess->not_resumable)) {
941 if (!ssl_get_new_session(s, 0))
944 /* else use the pre-loaded session */
946 p = s->s3->client_random;
949 * for DTLS if client_random is initialized, reuse it, we are
950 * required to use same upon reply to HelloVerify
952 if (SSL_IS_DTLS(s)) {
955 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
964 if (i && ssl_fill_hello_random(s, 0, p,
965 sizeof(s->s3->client_random)) <= 0)
968 /* Do the message type and length last */
969 d = p = ssl_handshake_start(s);
972 * version indicates the negotiated version: for example from
973 * an SSLv2/v3 compatible client hello). The client_version
974 * field is the maximum version we permit and it is also
975 * used in RSA encrypted premaster secrets. Some servers can
976 * choke if we initially report a higher version then
977 * renegotiate to a lower one in the premaster secret. This
978 * didn't happen with TLS 1.0 as most servers supported it
979 * but it can with TLS 1.1 or later if the server only supports
982 * Possible scenario with previous logic:
983 * 1. Client hello indicates TLS 1.2
984 * 2. Server hello says TLS 1.0
985 * 3. RSA encrypted premaster secret uses 1.2.
986 * 4. Handhaked proceeds using TLS 1.0.
987 * 5. Server sends hello request to renegotiate.
988 * 6. Client hello indicates TLS v1.0 as we now
989 * know that is maximum server supports.
990 * 7. Server chokes on RSA encrypted premaster secret
991 * containing version 1.0.
993 * For interoperability it should be OK to always use the
994 * maximum version we support in client hello and then rely
995 * on the checking of version to ensure the servers isn't
996 * being inconsistent: for example initially negotiating with
997 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
998 * client_version in client hello and not resetting it to
999 * the negotiated version.
1001 *(p++) = s->client_version >> 8;
1002 *(p++) = s->client_version & 0xff;
1005 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
1006 p += SSL3_RANDOM_SIZE;
1012 i = s->session->session_id_length;
1015 if (i > (int)sizeof(s->session->session_id)) {
1016 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1019 memcpy(p, s->session->session_id, i);
1023 /* cookie stuff for DTLS */
1024 if (SSL_IS_DTLS(s)) {
1025 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
1026 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1029 *(p++) = s->d1->cookie_len;
1030 memcpy(p, s->d1->cookie, s->d1->cookie_len);
1031 p += s->d1->cookie_len;
1034 /* Ciphers supported */
1035 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
1037 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
1040 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
1042 * Some servers hang if client hello > 256 bytes as hack workaround
1043 * chop number of supported ciphers to keep it well below this if we
1046 if (TLS1_get_version(s) >= TLS1_2_VERSION
1047 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
1048 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
1054 #ifdef OPENSSL_NO_COMP
1058 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
1061 j = sk_SSL_COMP_num(s->ctx->comp_methods);
1063 for (i = 0; i < j; i++) {
1064 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1068 *(p++) = 0; /* Add the NULL method */
1070 /* TLS extensions */
1071 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1072 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1076 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1078 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1079 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1084 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
1085 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1086 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1092 ossl_statem_set_error(s);
1096 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1099 unsigned int cookie_len;
1102 if (!PACKET_forward(pkt, 2)
1103 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1104 al = SSL_AD_DECODE_ERROR;
1105 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1109 cookie_len = PACKET_remaining(&cookiepkt);
1110 if (cookie_len > sizeof(s->d1->cookie)) {
1111 al = SSL_AD_ILLEGAL_PARAMETER;
1112 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1116 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1117 al = SSL_AD_DECODE_ERROR;
1118 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1121 s->d1->cookie_len = cookie_len;
1123 return MSG_PROCESS_FINISHED_READING;
1125 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1126 ossl_statem_set_error(s);
1127 return MSG_PROCESS_ERROR;
1130 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
1132 STACK_OF(SSL_CIPHER) *sk;
1133 const SSL_CIPHER *c;
1135 size_t session_id_len;
1136 unsigned char *cipherchars;
1137 int i, al = SSL_AD_INTERNAL_ERROR;
1138 unsigned int compression;
1139 #ifndef OPENSSL_NO_COMP
1143 if (s->method->version == TLS_ANY_VERSION) {
1144 unsigned int sversion;
1146 if (!PACKET_get_net_2(pkt, &sversion)) {
1147 al = SSL_AD_DECODE_ERROR;
1148 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1152 #if TLS_MAX_VERSION != TLS1_2_VERSION
1153 #error Code needs updating for new TLS version
1155 #ifndef OPENSSL_NO_SSL3
1156 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1158 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1159 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1160 al = SSL_AD_PROTOCOL_VERSION;
1163 s->method = SSLv3_client_method();
1166 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1167 s->method = TLSv1_client_method();
1168 } else if ((sversion == TLS1_1_VERSION) &&
1169 !(s->options & SSL_OP_NO_TLSv1_1)) {
1170 s->method = TLSv1_1_client_method();
1171 } else if ((sversion == TLS1_2_VERSION) &&
1172 !(s->options & SSL_OP_NO_TLSv1_2)) {
1173 s->method = TLSv1_2_client_method();
1175 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1176 al = SSL_AD_PROTOCOL_VERSION;
1179 s->session->ssl_version = s->version = s->method->version;
1181 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1182 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1183 al = SSL_AD_PROTOCOL_VERSION;
1186 } else if (s->method->version == DTLS_ANY_VERSION) {
1187 /* Work out correct protocol version to use */
1188 unsigned int hversion;
1191 if (!PACKET_get_net_2(pkt, &hversion)) {
1192 al = SSL_AD_DECODE_ERROR;
1193 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1197 options = s->options;
1198 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1199 s->method = DTLSv1_2_client_method();
1200 else if (tls1_suiteb(s)) {
1201 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1202 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1203 s->version = hversion;
1204 al = SSL_AD_PROTOCOL_VERSION;
1206 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1207 s->method = DTLSv1_client_method();
1209 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1210 s->version = hversion;
1211 al = SSL_AD_PROTOCOL_VERSION;
1214 s->session->ssl_version = s->version = s->method->version;
1216 unsigned char *vers;
1218 if (!PACKET_get_bytes(pkt, &vers, 2)) {
1219 al = SSL_AD_DECODE_ERROR;
1220 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1223 if ((vers[0] != (s->version >> 8))
1224 || (vers[1] != (s->version & 0xff))) {
1225 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1226 s->version = (s->version & 0xff00) | vers[1];
1227 al = SSL_AD_PROTOCOL_VERSION;
1232 /* load the server hello data */
1233 /* load the server random */
1234 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1235 al = SSL_AD_DECODE_ERROR;
1236 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1242 /* Get the session-id. */
1243 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1244 al = SSL_AD_DECODE_ERROR;
1245 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1248 session_id_len = PACKET_remaining(&session_id);
1249 if (session_id_len > sizeof s->session->session_id
1250 || session_id_len > SSL3_SESSION_ID_SIZE) {
1251 al = SSL_AD_ILLEGAL_PARAMETER;
1252 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1256 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1257 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1258 al = SSL_AD_DECODE_ERROR;
1263 * Check if we can resume the session based on external pre-shared secret.
1264 * EAP-FAST (RFC 4851) supports two types of session resumption.
1265 * Resumption based on server-side state works with session IDs.
1266 * Resumption based on pre-shared Protected Access Credentials (PACs)
1267 * works by overriding the SessionTicket extension at the application
1268 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1269 * servers would honour the session ID.) Therefore, the session ID alone
1270 * is not a reliable indicator of session resumption, so we first check if
1271 * we can resume, and later peek at the next handshake message to see if the
1272 * server wants to resume.
1274 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1275 s->session->tlsext_tick) {
1276 SSL_CIPHER *pref_cipher = NULL;
1277 s->session->master_key_length = sizeof(s->session->master_key);
1278 if (s->tls_session_secret_cb(s, s->session->master_key,
1279 &s->session->master_key_length,
1281 s->tls_session_secret_cb_arg)) {
1282 s->session->cipher = pref_cipher ?
1283 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1285 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1286 al = SSL_AD_INTERNAL_ERROR;
1291 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1292 && memcmp(PACKET_data(&session_id), s->session->session_id,
1293 session_id_len) == 0) {
1294 if (s->sid_ctx_length != s->session->sid_ctx_length
1295 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1296 /* actually a client application bug */
1297 al = SSL_AD_ILLEGAL_PARAMETER;
1298 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1299 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1305 * If we were trying for session-id reuse but the server
1306 * didn't echo the ID, make a new SSL_SESSION.
1307 * In the case of EAP-FAST and PAC, we do not send a session ID,
1308 * so the PAC-based session secret is always preserved. It'll be
1309 * overwritten if the server refuses resumption.
1311 if (s->session->session_id_length > 0) {
1312 if (!ssl_get_new_session(s, 0)) {
1317 s->session->session_id_length = session_id_len;
1318 /* session_id_len could be 0 */
1319 memcpy(s->session->session_id, PACKET_data(&session_id),
1323 c = ssl_get_cipher_by_char(s, cipherchars);
1325 /* unknown cipher */
1326 al = SSL_AD_ILLEGAL_PARAMETER;
1327 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1330 /* Set version disabled mask now we know version */
1331 if (!SSL_USE_TLS1_2_CIPHERS(s))
1332 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1334 s->s3->tmp.mask_ssl = 0;
1336 * If it is a disabled cipher we didn't send it in client hello, so
1339 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1340 al = SSL_AD_ILLEGAL_PARAMETER;
1341 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1345 sk = ssl_get_ciphers_by_id(s);
1346 i = sk_SSL_CIPHER_find(sk, c);
1348 /* we did not say we would use this cipher */
1349 al = SSL_AD_ILLEGAL_PARAMETER;
1350 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1355 * Depending on the session caching (internal/external), the cipher
1356 * and/or cipher_id values may not be set. Make sure that cipher_id is
1357 * set and use it for comparison.
1359 if (s->session->cipher)
1360 s->session->cipher_id = s->session->cipher->id;
1361 if (s->hit && (s->session->cipher_id != c->id)) {
1362 al = SSL_AD_ILLEGAL_PARAMETER;
1363 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1364 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1367 s->s3->tmp.new_cipher = c;
1369 * Don't digest cached records if no sigalgs: we may need them for client
1372 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1374 /* lets get the compression algorithm */
1376 if (!PACKET_get_1(pkt, &compression)) {
1377 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1378 al = SSL_AD_DECODE_ERROR;
1381 #ifdef OPENSSL_NO_COMP
1382 if (compression != 0) {
1383 al = SSL_AD_ILLEGAL_PARAMETER;
1384 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1385 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1389 * If compression is disabled we'd better not try to resume a session
1390 * using compression.
1392 if (s->session->compress_meth != 0) {
1393 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1397 if (s->hit && compression != s->session->compress_meth) {
1398 al = SSL_AD_ILLEGAL_PARAMETER;
1399 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1400 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1403 if (compression == 0)
1405 else if (!ssl_allow_compression(s)) {
1406 al = SSL_AD_ILLEGAL_PARAMETER;
1407 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1410 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1413 if (compression != 0 && comp == NULL) {
1414 al = SSL_AD_ILLEGAL_PARAMETER;
1415 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1416 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1419 s->s3->tmp.new_compression = comp;
1423 /* TLS extensions */
1424 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1425 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1429 if (PACKET_remaining(pkt) != 0) {
1430 /* wrong packet length */
1431 al = SSL_AD_DECODE_ERROR;
1432 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1436 #ifndef OPENSSL_NO_SCTP
1437 if (SSL_IS_DTLS(s) && s->hit) {
1438 unsigned char sctpauthkey[64];
1439 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1442 * Add new shared key for SCTP-Auth, will be ignored if
1445 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1446 sizeof(DTLS1_SCTP_AUTH_LABEL));
1448 if (SSL_export_keying_material(s, sctpauthkey,
1449 sizeof(sctpauthkey),
1451 sizeof(labelbuffer), NULL, 0,
1455 BIO_ctrl(SSL_get_wbio(s),
1456 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1457 sizeof(sctpauthkey), sctpauthkey);
1461 return MSG_PROCESS_CONTINUE_READING;
1463 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1465 ossl_statem_set_error(s);
1466 return MSG_PROCESS_ERROR;
1469 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1471 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1472 unsigned long cert_list_len, cert_len;
1474 unsigned char *certstart, *certbytes;
1475 STACK_OF(X509) *sk = NULL;
1476 EVP_PKEY *pkey = NULL;
1478 if ((sk = sk_X509_new_null()) == NULL) {
1479 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1483 if (!PACKET_get_net_3(pkt, &cert_list_len)
1484 || PACKET_remaining(pkt) != cert_list_len) {
1485 al = SSL_AD_DECODE_ERROR;
1486 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1489 while (PACKET_remaining(pkt)) {
1490 if (!PACKET_get_net_3(pkt, &cert_len)
1491 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1492 al = SSL_AD_DECODE_ERROR;
1493 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1494 SSL_R_CERT_LENGTH_MISMATCH);
1498 certstart = certbytes;
1499 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1501 al = SSL_AD_BAD_CERTIFICATE;
1502 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1505 if (certbytes != (certstart + cert_len)) {
1506 al = SSL_AD_DECODE_ERROR;
1507 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1508 SSL_R_CERT_LENGTH_MISMATCH);
1511 if (!sk_X509_push(sk, x)) {
1512 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1518 i = ssl_verify_cert_chain(s, sk);
1519 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1520 al = ssl_verify_alarm_type(s->verify_result);
1521 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1522 SSL_R_CERTIFICATE_VERIFY_FAILED);
1525 ERR_clear_error(); /* but we keep s->verify_result */
1527 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1528 al = SSL_AD_HANDSHAKE_FAILURE;
1532 s->session->peer_chain = sk;
1534 * Inconsistency alert: cert_chain does include the peer's certificate,
1535 * which we don't include in s3_srvr.c
1537 x = sk_X509_value(sk, 0);
1540 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1543 pkey = X509_get_pubkey(x);
1545 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1548 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1549 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1553 i = ssl_cert_type(x, pkey);
1557 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1558 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1562 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1563 if (exp_idx >= 0 && i != exp_idx) {
1565 al = SSL_AD_ILLEGAL_PARAMETER;
1566 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1567 SSL_R_WRONG_CERTIFICATE_TYPE);
1570 s->session->peer_type = i;
1572 X509_free(s->session->peer);
1574 s->session->peer = x;
1575 s->session->verify_result = s->verify_result;
1578 ret = MSG_PROCESS_CONTINUE_READING;
1582 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1584 ossl_statem_set_error(s);
1586 EVP_PKEY_free(pkey);
1588 sk_X509_pop_free(sk, X509_free);
1592 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1594 #ifndef OPENSSL_NO_RSA
1595 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1598 int al, j, verify_ret;
1600 EVP_PKEY *pkey = NULL;
1601 const EVP_MD *md = NULL;
1602 #ifndef OPENSSL_NO_RSA
1605 #ifndef OPENSSL_NO_DH
1608 #ifndef OPENSSL_NO_EC
1609 EC_KEY *ecdh = NULL;
1610 BN_CTX *bn_ctx = NULL;
1611 EC_POINT *srvr_ecpoint = NULL;
1614 PACKET save_param_start, signature;
1616 EVP_MD_CTX_init(&md_ctx);
1618 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1620 save_param_start = *pkt;
1622 #ifndef OPENSSL_NO_RSA
1623 RSA_free(s->s3->peer_rsa_tmp);
1624 s->s3->peer_rsa_tmp = NULL;
1626 #ifndef OPENSSL_NO_DH
1627 DH_free(s->s3->peer_dh_tmp);
1628 s->s3->peer_dh_tmp = NULL;
1630 #ifndef OPENSSL_NO_EC
1631 EC_KEY_free(s->s3->peer_ecdh_tmp);
1632 s->s3->peer_ecdh_tmp = NULL;
1635 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1637 al = SSL_AD_DECODE_ERROR;
1639 #ifndef OPENSSL_NO_PSK
1640 /* PSK ciphersuites are preceded by an identity hint */
1641 if (alg_k & SSL_PSK) {
1642 PACKET psk_identity_hint;
1643 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1644 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1649 * Store PSK identity hint for later use, hint is used in
1650 * ssl3_send_client_key_exchange. Assume that the maximum length of
1651 * a PSK identity hint can be as long as the maximum length of a PSK
1654 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1655 al = SSL_AD_HANDSHAKE_FAILURE;
1656 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1660 if (!PACKET_strndup(&psk_identity_hint,
1661 &s->session->psk_identity_hint)) {
1662 al = SSL_AD_INTERNAL_ERROR;
1667 /* Nothing else to do for plain PSK or RSAPSK */
1668 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1670 #endif /* !OPENSSL_NO_PSK */
1671 #ifndef OPENSSL_NO_SRP
1672 if (alg_k & SSL_kSRP) {
1673 PACKET prime, generator, salt, server_pub;
1674 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1675 || !PACKET_get_length_prefixed_2(pkt, &generator)
1676 || !PACKET_get_length_prefixed_1(pkt, &salt)
1677 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1678 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1683 BN_bin2bn(PACKET_data(&prime),
1684 PACKET_remaining(&prime), NULL)) == NULL
1686 BN_bin2bn(PACKET_data(&generator),
1687 PACKET_remaining(&generator), NULL)) == NULL
1689 BN_bin2bn(PACKET_data(&salt),
1690 PACKET_remaining(&salt), NULL)) == NULL
1692 BN_bin2bn(PACKET_data(&server_pub),
1693 PACKET_remaining(&server_pub), NULL)) == NULL) {
1694 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1698 if (!srp_verify_server_param(s, &al)) {
1699 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1703 /* We must check if there is a certificate */
1704 if (alg_a & (SSL_aRSA|SSL_aDSS))
1705 pkey = X509_get_pubkey(s->session->peer);
1707 #endif /* !OPENSSL_NO_SRP */
1708 #ifndef OPENSSL_NO_RSA
1709 if (alg_k & SSL_kRSA) {
1711 /* Temporary RSA keys only allowed in export ciphersuites */
1712 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1713 al = SSL_AD_UNEXPECTED_MESSAGE;
1714 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1718 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1719 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
1720 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1724 if ((rsa = RSA_new()) == NULL) {
1725 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1729 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1731 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1733 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1737 /* this should be because we are using an export cipher */
1738 if (alg_a & SSL_aRSA)
1739 pkey = X509_get_pubkey(s->session->peer);
1741 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1745 s->s3->peer_rsa_tmp = rsa;
1748 #else /* OPENSSL_NO_RSA */
1751 #ifndef OPENSSL_NO_DH
1752 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1753 PACKET prime, generator, pub_key;
1755 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1756 || !PACKET_get_length_prefixed_2(pkt, &generator)
1757 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1758 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1762 if ((dh = DH_new()) == NULL) {
1763 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_DH_LIB);
1767 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1768 PACKET_remaining(&prime), NULL)) == NULL
1769 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1770 PACKET_remaining(&generator), NULL)) == NULL
1772 BN_bin2bn(PACKET_data(&pub_key),
1773 PACKET_remaining(&pub_key), NULL)) == NULL) {
1774 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1778 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1779 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1783 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1784 al = SSL_AD_HANDSHAKE_FAILURE;
1785 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1788 if (alg_a & (SSL_aRSA|SSL_aDSS))
1789 pkey = X509_get_pubkey(s->session->peer);
1790 /* else anonymous DH, so no certificate or pkey. */
1792 s->s3->peer_dh_tmp = dh;
1795 #endif /* !OPENSSL_NO_DH */
1797 #ifndef OPENSSL_NO_EC
1798 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1800 const EC_GROUP *group;
1802 unsigned char *ecparams;
1804 if ((ecdh = EC_KEY_new()) == NULL) {
1805 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1810 * Extract elliptic curve parameters and the server's ephemeral ECDH
1811 * public key. For now we only support named (not generic) curves and
1812 * ECParameters in this case is just three bytes.
1814 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1815 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1819 * Check curve is one of our preferences, if not server has sent an
1820 * invalid curve. ECParameters is 3 bytes.
1822 if (!tls1_check_curve(s, ecparams, 3)) {
1823 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1827 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1828 al = SSL_AD_INTERNAL_ERROR;
1829 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1830 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1834 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1835 if (ngroup == NULL) {
1836 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1839 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1840 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1843 EC_GROUP_free(ngroup);
1845 group = EC_KEY_get0_group(ecdh);
1847 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1848 (EC_GROUP_get_degree(group) > 163)) {
1849 al = SSL_AD_EXPORT_RESTRICTION;
1850 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1851 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1855 /* Next, get the encoded ECPoint */
1856 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1857 ((bn_ctx = BN_CTX_new()) == NULL)) {
1858 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1862 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1863 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1867 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1868 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1869 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1874 * The ECC/TLS specification does not mention the use of DSA to sign
1875 * ECParameters in the server key exchange message. We do support RSA
1879 # ifndef OPENSSL_NO_RSA
1880 else if (alg_a & SSL_aRSA)
1881 pkey = X509_get_pubkey(s->session->peer);
1883 # ifndef OPENSSL_NO_EC
1884 else if (alg_a & SSL_aECDSA)
1885 pkey = X509_get_pubkey(s->session->peer);
1887 /* else anonymous ECDH, so no certificate or pkey. */
1888 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1889 s->s3->peer_ecdh_tmp = ecdh;
1891 BN_CTX_free(bn_ctx);
1893 EC_POINT_free(srvr_ecpoint);
1894 srvr_ecpoint = NULL;
1896 al = SSL_AD_UNEXPECTED_MESSAGE;
1897 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1900 #endif /* !OPENSSL_NO_EC */
1902 /* if it was signed, check the signature */
1906 * |pkt| now points to the beginning of the signature, so the difference
1907 * equals the length of the parameters.
1909 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1910 PACKET_remaining(&save_param_start) -
1911 PACKET_remaining(pkt))) {
1912 al = SSL_AD_INTERNAL_ERROR;
1913 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1917 if (SSL_USE_SIGALGS(s)) {
1918 unsigned char *sigalgs;
1920 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1921 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1924 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1931 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1937 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1938 || PACKET_remaining(pkt) != 0) {
1939 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1942 j = EVP_PKEY_size(pkey);
1944 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1949 * Check signature length
1951 if (PACKET_remaining(&signature) > (size_t)j) {
1952 /* wrong packet length */
1953 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1956 #ifndef OPENSSL_NO_RSA
1957 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1963 for (num = 2; num > 0; num--) {
1964 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1965 EVP_DigestInit_ex(&md_ctx, (num == 2)
1966 ? s->ctx->md5 : s->ctx->sha1, NULL);
1967 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1969 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1971 EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1972 PACKET_remaining(¶ms));
1973 EVP_DigestFinal_ex(&md_ctx, q, &size);
1978 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1979 PACKET_remaining(&signature), pkey->pkey.rsa);
1980 if (verify_ret < 0) {
1981 al = SSL_AD_DECRYPT_ERROR;
1982 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1985 if (verify_ret == 0) {
1987 al = SSL_AD_DECRYPT_ERROR;
1988 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1994 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1995 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1997 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1999 EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
2000 PACKET_remaining(¶ms));
2001 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
2002 PACKET_remaining(&signature), pkey) <= 0) {
2004 al = SSL_AD_DECRYPT_ERROR;
2005 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2010 /* aNULL, aSRP or PSK do not need public keys */
2011 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
2012 /* Might be wrong key type, check it */
2013 if (ssl3_check_cert_and_algorithm(s))
2014 /* Otherwise this shouldn't happen */
2015 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2018 /* still data left over */
2019 if (PACKET_remaining(pkt) != 0) {
2020 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2024 EVP_PKEY_free(pkey);
2025 EVP_MD_CTX_cleanup(&md_ctx);
2026 return MSG_PROCESS_CONTINUE_READING;
2028 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2030 EVP_PKEY_free(pkey);
2031 #ifndef OPENSSL_NO_RSA
2034 #ifndef OPENSSL_NO_DH
2037 #ifndef OPENSSL_NO_EC
2038 BN_CTX_free(bn_ctx);
2039 EC_POINT_free(srvr_ecpoint);
2042 EVP_MD_CTX_cleanup(&md_ctx);
2043 ossl_statem_set_error(s);
2044 return MSG_PROCESS_ERROR;
2047 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
2049 int ret = MSG_PROCESS_ERROR;
2050 unsigned int list_len, ctype_num, i, name_len;
2051 X509_NAME *xn = NULL;
2052 unsigned char *data;
2053 unsigned char *namestart, *namebytes;
2054 STACK_OF(X509_NAME) *ca_sk = NULL;
2056 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2057 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2061 /* get the certificate types */
2062 if (!PACKET_get_1(pkt, &ctype_num)
2063 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
2064 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2065 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2068 OPENSSL_free(s->cert->ctypes);
2069 s->cert->ctypes = NULL;
2070 if (ctype_num > SSL3_CT_NUMBER) {
2071 /* If we exceed static buffer copy all to cert structure */
2072 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2073 if (s->cert->ctypes == NULL) {
2074 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2077 memcpy(s->cert->ctypes, data, ctype_num);
2078 s->cert->ctype_num = (size_t)ctype_num;
2079 ctype_num = SSL3_CT_NUMBER;
2081 for (i = 0; i < ctype_num; i++)
2082 s->s3->tmp.ctype[i] = data[i];
2084 if (SSL_USE_SIGALGS(s)) {
2085 if (!PACKET_get_net_2(pkt, &list_len)
2086 || !PACKET_get_bytes(pkt, &data, list_len)) {
2087 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2088 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2089 SSL_R_LENGTH_MISMATCH);
2093 /* Clear certificate digests and validity flags */
2094 for (i = 0; i < SSL_PKEY_NUM; i++) {
2095 s->s3->tmp.md[i] = NULL;
2096 s->s3->tmp.valid_flags[i] = 0;
2098 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2099 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2100 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2101 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2104 if (!tls1_process_sigalgs(s)) {
2105 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2106 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2111 /* get the CA RDNs */
2112 if (!PACKET_get_net_2(pkt, &list_len)
2113 || PACKET_remaining(pkt) != list_len) {
2114 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2115 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2119 while (PACKET_remaining(pkt)) {
2120 if (!PACKET_get_net_2(pkt, &name_len)
2121 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
2122 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2123 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2124 SSL_R_LENGTH_MISMATCH);
2128 namestart = namebytes;
2130 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2131 name_len)) == NULL) {
2132 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2133 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2137 if (namebytes != (namestart + name_len)) {
2138 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2139 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2140 SSL_R_CA_DN_LENGTH_MISMATCH);
2143 if (!sk_X509_NAME_push(ca_sk, xn)) {
2144 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2149 /* we should setup a certificate to return.... */
2150 s->s3->tmp.cert_req = 1;
2151 s->s3->tmp.ctype_num = ctype_num;
2152 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2153 s->s3->tmp.ca_names = ca_sk;
2156 ret = MSG_PROCESS_CONTINUE_READING;
2159 ossl_statem_set_error(s);
2161 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2165 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2167 return (X509_NAME_cmp(*a, *b));
2170 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
2173 unsigned int ticklen;
2174 unsigned long ticket_lifetime_hint;
2176 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2177 || !PACKET_get_net_2(pkt, &ticklen)
2178 || PACKET_remaining(pkt) != ticklen) {
2179 al = SSL_AD_DECODE_ERROR;
2180 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2184 /* Server is allowed to change its mind and send an empty ticket. */
2186 return MSG_PROCESS_CONTINUE_READING;
2188 if (s->session->session_id_length > 0) {
2189 int i = s->session_ctx->session_cache_mode;
2190 SSL_SESSION *new_sess;
2192 * We reused an existing session, so we need to replace it with a new
2195 if (i & SSL_SESS_CACHE_CLIENT) {
2197 * Remove the old session from the cache
2199 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2200 if (s->session_ctx->remove_session_cb != NULL)
2201 s->session_ctx->remove_session_cb(s->session_ctx,
2204 /* We carry on if this fails */
2205 SSL_CTX_remove_session(s->session_ctx, s->session);
2209 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2210 al = SSL_AD_INTERNAL_ERROR;
2211 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2215 SSL_SESSION_free(s->session);
2216 s->session = new_sess;
2219 OPENSSL_free(s->session->tlsext_tick);
2220 s->session->tlsext_ticklen = 0;
2222 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2223 if (!s->session->tlsext_tick) {
2224 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2227 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
2228 al = SSL_AD_DECODE_ERROR;
2229 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2233 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2234 s->session->tlsext_ticklen = ticklen;
2236 * There are two ways to detect a resumed ticket session. One is to set
2237 * an appropriate session ID and then the server must return a match in
2238 * ServerHello. This allows the normal client session ID matching to work
2239 * and we know much earlier that the ticket has been accepted. The
2240 * other way is to set zero length session ID when the ticket is
2241 * presented and rely on the handshake to determine session resumption.
2242 * We choose the former approach because this fits in with assumptions
2243 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2244 * SHA256 is disabled) hash of the ticket.
2246 EVP_Digest(s->session->tlsext_tick, ticklen,
2247 s->session->session_id, &s->session->session_id_length,
2248 EVP_sha256(), NULL);
2249 return MSG_PROCESS_CONTINUE_READING;
2251 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2253 ossl_statem_set_error(s);
2254 return MSG_PROCESS_ERROR;
2257 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
2260 unsigned long resplen;
2263 if (!PACKET_get_1(pkt, &type)
2264 || type != TLSEXT_STATUSTYPE_ocsp) {
2265 al = SSL_AD_DECODE_ERROR;
2266 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2269 if (!PACKET_get_net_3(pkt, &resplen)
2270 || PACKET_remaining(pkt) != resplen) {
2271 al = SSL_AD_DECODE_ERROR;
2272 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2275 OPENSSL_free(s->tlsext_ocsp_resp);
2276 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2277 if (!s->tlsext_ocsp_resp) {
2278 al = SSL_AD_INTERNAL_ERROR;
2279 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2282 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2283 al = SSL_AD_DECODE_ERROR;
2284 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2287 s->tlsext_ocsp_resplen = resplen;
2288 if (s->ctx->tlsext_status_cb) {
2290 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2292 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2293 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2297 al = SSL_AD_INTERNAL_ERROR;
2298 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2302 return MSG_PROCESS_CONTINUE_READING;
2304 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2305 ossl_statem_set_error(s);
2306 return MSG_PROCESS_ERROR;
2309 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2311 if (PACKET_remaining(pkt) > 0) {
2312 /* should contain no data */
2313 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2314 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2315 ossl_statem_set_error(s);
2316 return MSG_PROCESS_ERROR;
2319 #ifndef OPENSSL_NO_SRP
2320 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2321 if (SRP_Calc_A_param(s) <= 0) {
2322 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2323 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2324 ossl_statem_set_error(s);
2325 return MSG_PROCESS_ERROR;
2331 * at this point we check that we have the required stuff from
2334 if (!ssl3_check_cert_and_algorithm(s)) {
2335 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2336 ossl_statem_set_error(s);
2337 return MSG_PROCESS_ERROR;
2340 #ifndef OPENSSL_NO_SCTP
2341 /* Only applies to renegotiation */
2342 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2343 && s->renegotiate != 0)
2344 return MSG_PROCESS_CONTINUE_PROCESSING;
2347 return MSG_PROCESS_FINISHED_READING;
2350 int tls_construct_client_key_exchange(SSL *s)
2354 #ifndef OPENSSL_NO_PSK
2355 size_t pskhdrlen = 0;
2357 unsigned long alg_k;
2358 #ifndef OPENSSL_NO_RSA
2360 EVP_PKEY *pkey = NULL;
2362 #ifndef OPENSSL_NO_EC
2363 EC_KEY *clnt_ecdh = NULL;
2364 const EC_POINT *srvr_ecpoint = NULL;
2365 EVP_PKEY *srvr_pub_pkey = NULL;
2366 unsigned char *encodedPoint = NULL;
2367 int encoded_pt_len = 0;
2368 BN_CTX *bn_ctx = NULL;
2370 unsigned char *pms = NULL;
2372 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2374 p = ssl_handshake_start(s);
2377 #ifndef OPENSSL_NO_PSK
2378 if (alg_k & SSL_PSK) {
2381 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2382 * \0-terminated identity. The last byte is for us for simulating
2385 char identity[PSK_MAX_IDENTITY_LEN + 1];
2387 unsigned char psk[PSK_MAX_PSK_LEN];
2390 if (s->psk_client_callback == NULL) {
2391 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2392 SSL_R_PSK_NO_CLIENT_CB);
2396 memset(identity, 0, sizeof(identity));
2398 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2399 identity, sizeof(identity) - 1,
2402 if (psklen > PSK_MAX_PSK_LEN) {
2403 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2404 ERR_R_INTERNAL_ERROR);
2406 } else if (psklen == 0) {
2407 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2408 SSL_R_PSK_IDENTITY_NOT_FOUND);
2412 OPENSSL_free(s->s3->tmp.psk);
2413 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2414 OPENSSL_cleanse(psk, psklen);
2416 if (s->s3->tmp.psk == NULL) {
2417 OPENSSL_cleanse(identity, sizeof(identity));
2421 s->s3->tmp.psklen = psklen;
2423 identitylen = strlen(identity);
2424 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2425 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2426 ERR_R_INTERNAL_ERROR);
2429 OPENSSL_free(s->session->psk_identity);
2430 s->session->psk_identity = BUF_strdup(identity);
2431 if (s->session->psk_identity == NULL) {
2432 OPENSSL_cleanse(identity, sizeof(identity));
2436 s2n(identitylen, p);
2437 memcpy(p, identity, identitylen);
2438 pskhdrlen = 2 + identitylen;
2442 OPENSSL_cleanse(identity, sizeof(identity));
2444 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2448 if (alg_k & SSL_kPSK) {
2453 /* Fool emacs indentation */
2456 #ifndef OPENSSL_NO_RSA
2457 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2459 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2460 pms = OPENSSL_malloc(pmslen);
2464 if (s->session->peer == NULL) {
2466 * We should always have a server certificate with SSL_kRSA.
2468 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2469 ERR_R_INTERNAL_ERROR);
2473 if (s->s3->peer_rsa_tmp != NULL)
2474 rsa = s->s3->peer_rsa_tmp;
2476 pkey = X509_get_pubkey(s->session->peer);
2477 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2478 || (pkey->pkey.rsa == NULL)) {
2479 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2480 ERR_R_INTERNAL_ERROR);
2481 EVP_PKEY_free(pkey);
2484 rsa = pkey->pkey.rsa;
2485 EVP_PKEY_free(pkey);
2488 pms[0] = s->client_version >> 8;
2489 pms[1] = s->client_version & 0xff;
2490 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2494 /* Fix buf for TLS and beyond */
2495 if (s->version > SSL3_VERSION)
2497 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2499 if (s->options & SSL_OP_PKCS1_CHECK_1)
2501 if (s->options & SSL_OP_PKCS1_CHECK_2)
2505 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2506 SSL_R_BAD_RSA_ENCRYPT);
2510 /* Fix buf for TLS and beyond */
2511 if (s->version > SSL3_VERSION) {
2517 #ifndef OPENSSL_NO_DH
2518 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2519 DH *dh_srvr, *dh_clnt;
2520 if (s->s3->peer_dh_tmp != NULL)
2521 dh_srvr = s->s3->peer_dh_tmp;
2523 /* we get them from the cert */
2524 EVP_PKEY *spkey = NULL;
2526 spkey = X509_get_pubkey(s->session->peer);
2528 dh_srvr = EVP_PKEY_get1_DH(spkey);
2529 EVP_PKEY_free(spkey);
2531 if (dh_srvr == NULL) {
2532 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2533 ERR_R_INTERNAL_ERROR);
2537 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2538 /* Use client certificate key */
2539 EVP_PKEY *clkey = s->cert->key->privatekey;
2542 dh_clnt = EVP_PKEY_get1_DH(clkey);
2543 if (dh_clnt == NULL) {
2544 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2545 ERR_R_INTERNAL_ERROR);
2549 /* generate a new random key */
2550 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2551 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2554 if (!DH_generate_key(dh_clnt)) {
2555 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2561 pmslen = DH_size(dh_clnt);
2562 pms = OPENSSL_malloc(pmslen);
2567 * use the 'p' output buffer for the DH key, but make sure to
2568 * clear it out afterwards
2571 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2572 if (s->s3->peer_dh_tmp == NULL)
2576 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2582 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2585 /* send off the data */
2586 n = BN_num_bytes(dh_clnt->pub_key);
2588 BN_bn2bin(dh_clnt->pub_key, p);
2596 #ifndef OPENSSL_NO_EC
2597 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2598 const EC_GROUP *srvr_group = NULL;
2600 int ecdh_clnt_cert = 0;
2603 * Did we send out the client's ECDH share for use in premaster
2604 * computation as part of client certificate? If so, set
2605 * ecdh_clnt_cert to 1.
2607 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2609 * XXX: For now, we do not support client
2610 * authentication using ECDH certificates.
2611 * To add such support, one needs to add
2612 * code that checks for appropriate
2613 * conditions and sets ecdh_clnt_cert to 1.
2614 * For example, the cert have an ECC
2615 * key on the same curve as the server's
2616 * and the key should be authorized for
2619 * One also needs to add code in ssl3_connect
2620 * to skip sending the certificate verify
2623 * if ((s->cert->key->privatekey != NULL) &&
2624 * (s->cert->key->privatekey->type ==
2625 * EVP_PKEY_EC) && ...)
2626 * ecdh_clnt_cert = 1;
2630 if (s->s3->peer_ecdh_tmp != NULL) {
2631 tkey = s->s3->peer_ecdh_tmp;
2633 /* Get the Server Public Key from Cert */
2634 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2635 if ((srvr_pub_pkey == NULL)
2636 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2637 || (srvr_pub_pkey->pkey.ec == NULL)) {
2638 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2639 ERR_R_INTERNAL_ERROR);
2643 tkey = srvr_pub_pkey->pkey.ec;
2646 srvr_group = EC_KEY_get0_group(tkey);
2647 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2649 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2650 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2651 ERR_R_INTERNAL_ERROR);
2655 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2656 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2657 ERR_R_MALLOC_FAILURE);
2661 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2662 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2665 if (ecdh_clnt_cert) {
2667 * Reuse key info from our certificate We only need our
2668 * private key to perform the ECDH computation.
2670 const BIGNUM *priv_key;
2671 tkey = s->cert->key->privatekey->pkey.ec;
2672 priv_key = EC_KEY_get0_private_key(tkey);
2673 if (priv_key == NULL) {
2674 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2675 ERR_R_MALLOC_FAILURE);
2678 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2679 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2683 /* Generate a new ECDH key pair */
2684 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2685 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2692 * use the 'p' output buffer for the ECDH key, but make sure to
2693 * clear it out afterwards
2696 field_size = EC_GROUP_get_degree(srvr_group);
2697 if (field_size <= 0) {
2698 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2701 pmslen = (field_size + 7) / 8;
2702 pms = OPENSSL_malloc(pmslen);
2705 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2706 if (n <= 0 || pmslen != (size_t)n) {
2707 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2711 if (ecdh_clnt_cert) {
2712 /* Send empty client key exch message */
2716 * First check the size of encoding and allocate memory
2720 EC_POINT_point2oct(srvr_group,
2721 EC_KEY_get0_public_key(clnt_ecdh),
2722 POINT_CONVERSION_UNCOMPRESSED,
2725 encodedPoint = (unsigned char *)
2726 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2727 bn_ctx = BN_CTX_new();
2728 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2729 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2730 ERR_R_MALLOC_FAILURE);
2734 /* Encode the public key */
2735 n = EC_POINT_point2oct(srvr_group,
2736 EC_KEY_get0_public_key(clnt_ecdh),
2737 POINT_CONVERSION_UNCOMPRESSED,
2738 encodedPoint, encoded_pt_len, bn_ctx);
2740 *p = n; /* length of encoded point */
2741 /* Encoded point will be copied here */
2743 /* copy the point */
2744 memcpy(p, encodedPoint, n);
2745 /* increment n to account for length field */
2749 /* Free allocated memory */
2750 BN_CTX_free(bn_ctx);
2751 OPENSSL_free(encodedPoint);
2752 EC_KEY_free(clnt_ecdh);
2753 EVP_PKEY_free(srvr_pub_pkey);
2755 #endif /* !OPENSSL_NO_EC */
2756 else if (alg_k & SSL_kGOST) {
2757 /* GOST key exchange message creation */
2758 EVP_PKEY_CTX *pkey_ctx;
2761 unsigned int md_len;
2762 unsigned char shared_ukm[32], tmp[256];
2763 EVP_MD_CTX *ukm_hash;
2767 pms = OPENSSL_malloc(pmslen);
2772 * Get server sertificate PKEY and create ctx from it
2774 peer_cert = s->session->peer;
2776 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2777 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2781 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2782 X509_get_pubkey(peer_cert), NULL);
2784 * If we have send a certificate, and certificate key
2786 * * parameters match those of server certificate, use
2787 * certificate key for key exchange
2790 /* Otherwise, generate ephemeral key pair */
2792 EVP_PKEY_encrypt_init(pkey_ctx);
2793 /* Generate session key */
2794 if (RAND_bytes(pms, pmslen) <= 0) {
2795 EVP_PKEY_CTX_free(pkey_ctx);
2796 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2797 ERR_R_INTERNAL_ERROR);
2801 * If we have client certificate, use its secret as peer key
2803 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2804 if (EVP_PKEY_derive_set_peer
2805 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2807 * If there was an error - just ignore it. Ephemeral key
2814 * Compute shared IV and store it in algorithm-specific context
2817 ukm_hash = EVP_MD_CTX_create();
2818 EVP_DigestInit(ukm_hash,
2819 EVP_get_digestbynid(NID_id_GostR3411_94));
2820 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2822 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2824 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2825 EVP_MD_CTX_destroy(ukm_hash);
2826 if (EVP_PKEY_CTX_ctrl
2827 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2829 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2833 /* Make GOST keytransport blob message */
2835 * Encapsulate it into sequence
2837 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2839 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2840 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2844 if (msglen >= 0x80) {
2846 *(p++) = msglen & 0xff;
2849 *(p++) = msglen & 0xff;
2852 memcpy(p, tmp, msglen);
2853 /* Check if pubkey from client certificate was used */
2854 if (EVP_PKEY_CTX_ctrl
2855 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2856 /* Set flag "skip certificate verify" */
2857 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2859 EVP_PKEY_CTX_free(pkey_ctx);
2860 EVP_PKEY_free(pub_key);
2863 #ifndef OPENSSL_NO_SRP
2864 else if (alg_k & SSL_kSRP) {
2865 if (s->srp_ctx.A != NULL) {
2866 /* send off the data */
2867 n = BN_num_bytes(s->srp_ctx.A);
2869 BN_bn2bin(s->srp_ctx.A, p);
2872 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2873 ERR_R_INTERNAL_ERROR);
2876 OPENSSL_free(s->session->srp_username);
2877 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2878 if (s->session->srp_username == NULL) {
2879 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2880 ERR_R_MALLOC_FAILURE);
2886 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2887 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2891 #ifndef OPENSSL_NO_PSK
2895 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2896 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2897 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2901 s->s3->tmp.pms = pms;
2902 s->s3->tmp.pmslen = pmslen;
2906 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2907 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2909 OPENSSL_clear_free(pms, pmslen);
2910 s->s3->tmp.pms = NULL;
2911 #ifndef OPENSSL_NO_EC
2912 BN_CTX_free(bn_ctx);
2913 OPENSSL_free(encodedPoint);
2914 EC_KEY_free(clnt_ecdh);
2915 EVP_PKEY_free(srvr_pub_pkey);
2917 #ifndef OPENSSL_NO_PSK
2918 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2919 s->s3->tmp.psk = NULL;
2921 ossl_statem_set_error(s);
2925 int tls_client_key_exchange_post_work(SSL *s)
2927 unsigned char *pms = NULL;
2930 #ifndef OPENSSL_NO_SRP
2932 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2933 if (!srp_generate_client_master_secret(s)) {
2934 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2935 ERR_R_INTERNAL_ERROR);
2941 pms = s->s3->tmp.pms;
2942 pmslen = s->s3->tmp.pmslen;
2944 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2945 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2946 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2949 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2950 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2951 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2955 #ifndef OPENSSL_NO_SCTP
2956 if (SSL_IS_DTLS(s)) {
2957 unsigned char sctpauthkey[64];
2958 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2961 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2964 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2965 sizeof(DTLS1_SCTP_AUTH_LABEL));
2967 if (SSL_export_keying_material(s, sctpauthkey,
2968 sizeof(sctpauthkey), labelbuffer,
2969 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2972 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2973 sizeof(sctpauthkey), sctpauthkey);
2979 OPENSSL_clear_free(pms, pmslen);
2980 s->s3->tmp.pms = NULL;
2984 int tls_construct_client_verify(SSL *s)
2987 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2989 EVP_PKEY_CTX *pctx = NULL;
2995 EVP_MD_CTX_init(&mctx);
2997 p = ssl_handshake_start(s);
2998 pkey = s->cert->key->privatekey;
2999 /* Create context from key and test if sha1 is allowed as digest */
3000 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3001 EVP_PKEY_sign_init(pctx);
3002 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3003 if (!SSL_USE_SIGALGS(s))
3004 s->method->ssl3_enc->cert_verify_mac(s,
3007 [MD5_DIGEST_LENGTH]));
3012 * For TLS v1.2 send signature algorithm and signature using agreed
3013 * digest and cached handshake records.
3015 if (SSL_USE_SIGALGS(s)) {
3018 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3019 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3020 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3021 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3026 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3029 if (!EVP_SignInit_ex(&mctx, md, NULL)
3030 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3031 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3032 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3037 /* Digest cached records and discard handshake buffer */
3038 if (!ssl3_digest_cached_records(s, 0))
3041 #ifndef OPENSSL_NO_RSA
3042 if (pkey->type == EVP_PKEY_RSA) {
3043 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3044 if (RSA_sign(NID_md5_sha1, data,
3045 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3046 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3047 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3054 #ifndef OPENSSL_NO_DSA
3055 if (pkey->type == EVP_PKEY_DSA) {
3056 if (!DSA_sign(pkey->save_type,
3057 &(data[MD5_DIGEST_LENGTH]),
3058 SHA_DIGEST_LENGTH, &(p[2]),
3059 (unsigned int *)&j, pkey->pkey.dsa)) {
3060 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3067 #ifndef OPENSSL_NO_EC
3068 if (pkey->type == EVP_PKEY_EC) {
3069 if (!ECDSA_sign(pkey->save_type,
3070 &(data[MD5_DIGEST_LENGTH]),
3071 SHA_DIGEST_LENGTH, &(p[2]),
3072 (unsigned int *)&j, pkey->pkey.ec)) {
3073 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3080 if (pkey->type == NID_id_GostR3410_2001) {
3081 unsigned char signbuf[64];
3083 size_t sigsize = 64;
3084 s->method->ssl3_enc->cert_verify_mac(s,
3085 NID_id_GostR3411_94, data);
3086 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3087 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3090 for (i = 63, j = 0; i >= 0; j++, i--) {
3091 p[2 + j] = signbuf[i];
3096 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3099 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3100 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3104 EVP_MD_CTX_cleanup(&mctx);
3105 EVP_PKEY_CTX_free(pctx);
3108 EVP_MD_CTX_cleanup(&mctx);
3109 EVP_PKEY_CTX_free(pctx);
3114 * Check a certificate can be used for client authentication. Currently check
3115 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3116 * certificates can be used and optionally checks suitability for Suite B.
3118 static int ssl3_check_client_certificate(SSL *s)
3120 unsigned long alg_k;
3121 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3123 /* If no suitable signature algorithm can't use certificate */
3124 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3127 * If strict mode check suitability of chain before using it. This also
3128 * adjusts suite B digest if necessary.
3130 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3131 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3133 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3134 /* See if we can use client certificate for fixed DH */
3135 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3136 int i = s->session->peer_type;
3137 EVP_PKEY *clkey = NULL, *spkey = NULL;
3138 clkey = s->cert->key->privatekey;
3139 /* If client key not DH assume it can be used */
3140 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3143 spkey = X509_get_pubkey(s->session->peer);
3145 /* Compare server and client parameters */
3146 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3147 EVP_PKEY_free(spkey);
3151 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3156 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
3159 EVP_PKEY *pkey = NULL;
3162 if (wst == WORK_MORE_A) {
3163 /* Let cert callback update client certificates if required */
3164 if (s->cert->cert_cb) {
3165 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3167 s->rwstate = SSL_X509_LOOKUP;
3171 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3172 ossl_statem_set_error(s);
3175 s->rwstate = SSL_NOTHING;
3177 if (ssl3_check_client_certificate(s))
3178 return WORK_FINISHED_CONTINUE;
3180 /* Fall through to WORK_MORE_B */
3184 /* We need to get a client cert */
3185 if (wst == WORK_MORE_B) {
3187 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3188 * return(-1); We then get retied later
3190 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3192 s->rwstate = SSL_X509_LOOKUP;
3195 s->rwstate = SSL_NOTHING;
3196 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3197 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3199 } else if (i == 1) {
3201 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
3202 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3206 EVP_PKEY_free(pkey);
3207 if (i && !ssl3_check_client_certificate(s))
3210 if (s->version == SSL3_VERSION) {
3211 s->s3->tmp.cert_req = 0;
3212 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3213 return WORK_FINISHED_CONTINUE;
3215 s->s3->tmp.cert_req = 2;
3216 if (!ssl3_digest_cached_records(s, 0)) {
3217 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3218 ossl_statem_set_error(s);
3224 return WORK_FINISHED_CONTINUE;
3227 /* Shouldn't ever get here */
3231 int tls_construct_client_certificate(SSL *s)
3233 if (!ssl3_output_cert_chain(s,
3234 (s->s3->tmp.cert_req ==
3235 2) ? NULL : s->cert->key)) {
3236 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3237 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3238 ossl_statem_set_error(s);
3245 #define has_bits(i,m) (((i)&(m)) == (m))
3247 int ssl3_check_cert_and_algorithm(SSL *s)
3251 EVP_PKEY *pkey = NULL;
3253 #ifndef OPENSSL_NO_RSA
3256 #ifndef OPENSSL_NO_DH
3259 int al = SSL_AD_HANDSHAKE_FAILURE;
3261 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3262 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3264 /* we don't have a certificate */
3265 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3267 #ifndef OPENSSL_NO_RSA
3268 rsa = s->s3->peer_rsa_tmp;
3270 #ifndef OPENSSL_NO_DH
3271 dh = s->s3->peer_dh_tmp;
3274 /* This is the passed certificate */
3276 idx = s->session->peer_type;
3277 #ifndef OPENSSL_NO_EC
3278 if (idx == SSL_PKEY_ECC) {
3279 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3281 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3286 } else if (alg_a & SSL_aECDSA) {
3287 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3288 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3290 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3291 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3295 pkey = X509_get_pubkey(s->session->peer);
3296 pkey_bits = EVP_PKEY_bits(pkey);
3297 i = X509_certificate_type(s->session->peer, pkey);
3298 EVP_PKEY_free(pkey);
3300 /* Check that we have a certificate if we require one */
3301 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3302 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3303 SSL_R_MISSING_RSA_SIGNING_CERT);
3306 #ifndef OPENSSL_NO_DSA
3307 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3308 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3309 SSL_R_MISSING_DSA_SIGNING_CERT);
3313 #ifndef OPENSSL_NO_RSA
3314 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3315 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3316 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3317 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3318 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3320 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3321 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3322 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3323 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3324 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3328 /* server key exchange is not allowed. */
3329 al = SSL_AD_INTERNAL_ERROR;
3330 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3337 #ifndef OPENSSL_NO_DH
3338 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3339 al = SSL_AD_INTERNAL_ERROR;
3340 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3342 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3343 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3344 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3345 SSL_R_MISSING_DH_RSA_CERT);
3348 # ifndef OPENSSL_NO_DSA
3349 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3350 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3351 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3352 SSL_R_MISSING_DH_DSA_CERT);
3358 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3359 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3360 #ifndef OPENSSL_NO_RSA
3361 if (alg_k & SSL_kRSA) {
3363 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3364 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3366 } else if (RSA_bits(rsa) >
3367 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3368 /* We have a temporary RSA key but it's too large. */
3369 al = SSL_AD_EXPORT_RESTRICTION;
3370 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3371 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3376 #ifndef OPENSSL_NO_DH
3377 if (alg_k & SSL_kDHE) {
3379 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3380 /* We have a temporary DH key but it's too large. */
3381 al = SSL_AD_EXPORT_RESTRICTION;
3382 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3383 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3386 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3387 /* The cert should have had an export DH key. */
3388 al = SSL_AD_EXPORT_RESTRICTION;
3389 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3390 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3395 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3396 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3402 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3406 #ifndef OPENSSL_NO_NEXTPROTONEG
3407 int tls_construct_next_proto(SSL *s)
3409 unsigned int len, padding_len;
3412 len = s->next_proto_negotiated_len;
3413 padding_len = 32 - ((len + 2) % 32);
3414 d = (unsigned char *)s->init_buf->data;
3416 memcpy(d + 5, s->next_proto_negotiated, len);
3417 d[5 + len] = padding_len;
3418 memset(d + 6 + len, 0, padding_len);
3419 *(d++) = SSL3_MT_NEXT_PROTO;
3420 l2n3(2 + len + padding_len, d);
3421 s->init_num = 4 + 2 + len + padding_len;
3428 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3431 #ifndef OPENSSL_NO_ENGINE
3432 if (s->ctx->client_cert_engine) {
3433 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3434 SSL_get_client_CA_list(s),
3435 px509, ppkey, NULL, NULL, NULL);
3440 if (s->ctx->client_cert_cb)
3441 i = s->ctx->client_cert_cb(s, px509, ppkey);
3445 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3451 int empty_reneg_info_scsv = !s->renegotiate;
3452 /* Set disabled masks for this session */
3453 ssl_set_client_disabled(s);
3459 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3460 c = sk_SSL_CIPHER_value(sk, i);
3461 /* Skip disabled ciphers */
3462 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3464 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3465 if (c->id == SSL3_CK_SCSV) {
3466 if (!empty_reneg_info_scsv)
3469 empty_reneg_info_scsv = 0;
3472 j = s->method->put_cipher_by_char(c, p);
3476 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3480 if (empty_reneg_info_scsv) {
3481 static SSL_CIPHER scsv = {
3482 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3484 j = s->method->put_cipher_by_char(&scsv, p);
3486 #ifdef OPENSSL_RI_DEBUG
3488 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3491 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3492 static SSL_CIPHER scsv = {
3493 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3495 j = s->method->put_cipher_by_char(&scsv, p);