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))
187 || (s->s3->tmp.new_cipher->algorithm_auth & (SSL_aSRP | SSL_aPSK)))
194 * Should we expect the ServerKeyExchange message or not?
201 static int key_exchange_expected(SSL *s)
203 long alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
206 * Can't skip server key exchange if this is an ephemeral
207 * ciphersuite or for SRP
209 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK
215 * Export ciphersuites may have temporary RSA keys if the public key in the
216 * server certificate is longer than the maximum export strength
218 if ((alg_k & SSL_kRSA) && SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
221 pkey = X509_get_pubkey(s->session->peer);
226 * If the public key in the certificate is shorter than or equal to the
227 * maximum export strength then a temporary RSA key is not allowed
229 if (EVP_PKEY_bits(pkey)
230 <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher))
242 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
243 * handshake state transitions when the client is reading messages from the
244 * server. The message type that the server has sent is provided in |mt|. The
245 * current state is in |s->statem.hand_state|.
248 * 1: Success (transition allowed)
249 * 0: Error (transition not allowed)
251 int ossl_statem_client_read_transition(SSL *s, int mt)
253 OSSL_STATEM *st = &s->statem;
256 switch(st->hand_state) {
257 case TLS_ST_CW_CLNT_HELLO:
258 if (mt == SSL3_MT_SERVER_HELLO) {
259 st->hand_state = TLS_ST_CR_SRVR_HELLO;
263 if (SSL_IS_DTLS(s)) {
264 if (mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
265 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
271 case TLS_ST_CR_SRVR_HELLO:
273 if (s->tlsext_ticket_expected) {
274 if (mt == SSL3_MT_NEWSESSION_TICKET) {
275 st->hand_state = TLS_ST_CR_SESSION_TICKET;
278 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
279 st->hand_state = TLS_ST_CR_CHANGE;
283 if (SSL_IS_DTLS(s) && mt == DTLS1_MT_HELLO_VERIFY_REQUEST) {
284 st->hand_state = DTLS_ST_CR_HELLO_VERIFY_REQUEST;
286 } else if (!(s->s3->tmp.new_cipher->algorithm_auth
287 & (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
288 if (mt == SSL3_MT_CERTIFICATE) {
289 st->hand_state = TLS_ST_CR_CERT;
293 ske_expected = key_exchange_expected(s);
294 if (ske_expected < 0)
296 /* SKE is optional for some PSK ciphersuites */
298 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
299 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
300 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
301 st->hand_state = TLS_ST_CR_KEY_EXCH;
304 } else if (mt == SSL3_MT_CERTIFICATE_REQUEST
305 && cert_req_allowed(s)) {
306 st->hand_state = TLS_ST_CR_CERT_REQ;
308 } else if (mt == SSL3_MT_SERVER_DONE) {
309 st->hand_state = TLS_ST_CR_SRVR_DONE;
317 if (s->tlsext_status_expected) {
318 if (mt == SSL3_MT_CERTIFICATE_STATUS) {
319 st->hand_state = TLS_ST_CR_CERT_STATUS;
326 case TLS_ST_CR_CERT_STATUS:
327 ske_expected = key_exchange_expected(s);
328 if (ske_expected < 0)
330 /* SKE is optional for some PSK ciphersuites */
332 || ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_PSK)
333 && mt == SSL3_MT_SERVER_KEY_EXCHANGE)) {
334 if (mt == SSL3_MT_SERVER_KEY_EXCHANGE) {
335 st->hand_state = TLS_ST_CR_KEY_EXCH;
342 case TLS_ST_CR_KEY_EXCH:
343 if (mt == SSL3_MT_CERTIFICATE_REQUEST) {
344 if (cert_req_allowed(s)) {
345 st->hand_state = TLS_ST_CR_CERT_REQ;
352 case TLS_ST_CR_CERT_REQ:
353 if (mt == SSL3_MT_SERVER_DONE) {
354 st->hand_state = TLS_ST_CR_SRVR_DONE;
359 case TLS_ST_CW_FINISHED:
360 if (mt == SSL3_MT_NEWSESSION_TICKET && s->tlsext_ticket_expected) {
361 st->hand_state = TLS_ST_CR_SESSION_TICKET;
363 } else if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
364 st->hand_state = TLS_ST_CR_CHANGE;
369 case TLS_ST_CR_SESSION_TICKET:
370 if (mt == SSL3_MT_CHANGE_CIPHER_SPEC) {
371 st->hand_state = TLS_ST_CR_CHANGE;
376 case TLS_ST_CR_CHANGE:
377 if (mt == SSL3_MT_FINISHED) {
378 st->hand_state = TLS_ST_CR_FINISHED;
387 /* No valid transition found */
392 * client_write_transition() works out what handshake state to move to next
393 * when the client is writing messages to be sent to the server.
395 WRITE_TRAN ossl_statem_client_write_transition(SSL *s)
397 OSSL_STATEM *st = &s->statem;
399 switch(st->hand_state) {
401 /* Renegotiation - fall through */
403 st->hand_state = TLS_ST_CW_CLNT_HELLO;
404 return WRITE_TRAN_CONTINUE;
406 case TLS_ST_CW_CLNT_HELLO:
408 * No transition at the end of writing because we don't know what
411 return WRITE_TRAN_FINISHED;
413 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
414 st->hand_state = TLS_ST_CW_CLNT_HELLO;
415 return WRITE_TRAN_CONTINUE;
417 case TLS_ST_CR_SRVR_DONE:
418 if (s->s3->tmp.cert_req)
419 st->hand_state = TLS_ST_CW_CERT;
421 st->hand_state = TLS_ST_CW_KEY_EXCH;
422 return WRITE_TRAN_CONTINUE;
425 st->hand_state = TLS_ST_CW_KEY_EXCH;
426 return WRITE_TRAN_CONTINUE;
428 case TLS_ST_CW_KEY_EXCH:
430 * For TLS, cert_req is set to 2, so a cert chain of nothing is
431 * sent, but no verify packet is sent
434 * XXX: For now, we do not support client authentication in ECDH
435 * cipher suites with ECDH (rather than ECDSA) certificates. We
436 * need to skip the certificate verify message when client's
437 * ECDH public key is sent inside the client certificate.
439 if (s->s3->tmp.cert_req == 1) {
440 st->hand_state = TLS_ST_CW_CERT_VRFY;
442 st->hand_state = TLS_ST_CW_CHANGE;
444 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
445 st->hand_state = TLS_ST_CW_CHANGE;
447 return WRITE_TRAN_CONTINUE;
449 case TLS_ST_CW_CERT_VRFY:
450 st->hand_state = TLS_ST_CW_CHANGE;
451 return WRITE_TRAN_CONTINUE;
453 case TLS_ST_CW_CHANGE:
454 #if defined(OPENSSL_NO_NEXTPROTONEG)
455 st->hand_state = TLS_ST_CW_FINISHED;
457 if (!SSL_IS_DTLS(s) && s->s3->next_proto_neg_seen)
458 st->hand_state = TLS_ST_CW_NEXT_PROTO;
460 st->hand_state = TLS_ST_CW_FINISHED;
462 return WRITE_TRAN_CONTINUE;
464 #if !defined(OPENSSL_NO_NEXTPROTONEG)
465 case TLS_ST_CW_NEXT_PROTO:
466 st->hand_state = TLS_ST_CW_FINISHED;
467 return WRITE_TRAN_CONTINUE;
470 case TLS_ST_CW_FINISHED:
472 st->hand_state = TLS_ST_OK;
473 ossl_statem_set_in_init(s, 0);
474 return WRITE_TRAN_CONTINUE;
476 return WRITE_TRAN_FINISHED;
479 case TLS_ST_CR_FINISHED:
481 st->hand_state = TLS_ST_CW_CHANGE;
482 return WRITE_TRAN_CONTINUE;
484 st->hand_state = TLS_ST_OK;
485 ossl_statem_set_in_init(s, 0);
486 return WRITE_TRAN_CONTINUE;
490 /* Shouldn't happen */
491 return WRITE_TRAN_ERROR;
496 * Perform any pre work that needs to be done prior to sending a message from
497 * the client to the server.
499 WORK_STATE ossl_statem_client_pre_work(SSL *s, WORK_STATE wst)
501 OSSL_STATEM *st = &s->statem;
503 switch(st->hand_state) {
504 case TLS_ST_CW_CLNT_HELLO:
506 if (SSL_IS_DTLS(s)) {
507 /* every DTLS ClientHello resets Finished MAC */
508 ssl3_init_finished_mac(s);
513 return tls_prepare_client_certificate(s, wst);
515 case TLS_ST_CW_CHANGE:
516 if (SSL_IS_DTLS(s)) {
519 * We're into the last flight so we don't retransmit these
520 * messages unless we need to.
524 #ifndef OPENSSL_NO_SCTP
525 if (BIO_dgram_is_sctp(SSL_get_wbio(s)))
526 return dtls_wait_for_dry(s);
529 return WORK_FINISHED_CONTINUE;
532 return tls_finish_handshake(s, wst);
535 /* No pre work to be done */
539 return WORK_FINISHED_CONTINUE;
543 * Perform any work that needs to be done after sending a message from the
544 * client to the server.
546 WORK_STATE ossl_statem_client_post_work(SSL *s, WORK_STATE wst)
548 OSSL_STATEM *st = &s->statem;
552 switch(st->hand_state) {
553 case TLS_ST_CW_CLNT_HELLO:
554 if (SSL_IS_DTLS(s) && s->d1->cookie_len > 0 && statem_flush(s) != 1)
556 #ifndef OPENSSL_NO_SCTP
557 /* Disable buffering for SCTP */
558 if (!SSL_IS_DTLS(s) || !BIO_dgram_is_sctp(SSL_get_wbio(s))) {
561 * turn on buffering for the next lot of output
563 if (s->bbio != s->wbio)
564 s->wbio = BIO_push(s->bbio, s->wbio);
565 #ifndef OPENSSL_NO_SCTP
568 if (SSL_IS_DTLS(s)) {
569 /* Treat the next message as the first packet */
574 case TLS_ST_CW_KEY_EXCH:
575 if (tls_client_key_exchange_post_work(s) == 0)
579 case TLS_ST_CW_CHANGE:
580 s->session->cipher = s->s3->tmp.new_cipher;
581 #ifdef OPENSSL_NO_COMP
582 s->session->compress_meth = 0;
584 if (s->s3->tmp.new_compression == NULL)
585 s->session->compress_meth = 0;
587 s->session->compress_meth = s->s3->tmp.new_compression->id;
589 if (!s->method->ssl3_enc->setup_key_block(s))
592 if (!s->method->ssl3_enc->change_cipher_state(s,
593 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
596 if (SSL_IS_DTLS(s)) {
597 #ifndef OPENSSL_NO_SCTP
600 * Change to new shared key of SCTP-Auth, will be ignored if
603 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
608 dtls1_reset_seq_numbers(s, SSL3_CC_WRITE);
612 case TLS_ST_CW_FINISHED:
613 #ifndef OPENSSL_NO_SCTP
614 if (wst == WORK_MORE_A && SSL_IS_DTLS(s) && s->hit == 0) {
616 * Change to new shared key of SCTP-Auth, will be ignored if
619 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY,
623 if (statem_flush(s) != 1)
628 /* No post work to be done */
632 return WORK_FINISHED_CONTINUE;
636 * Construct a message to be sent from the client to the server.
638 * Valid return values are:
642 int ossl_statem_client_construct_message(SSL *s)
644 OSSL_STATEM *st = &s->statem;
646 switch(st->hand_state) {
647 case TLS_ST_CW_CLNT_HELLO:
648 return tls_construct_client_hello(s);
651 return tls_construct_client_certificate(s);
653 case TLS_ST_CW_KEY_EXCH:
654 return tls_construct_client_key_exchange(s);
656 case TLS_ST_CW_CERT_VRFY:
657 return tls_construct_client_verify(s);
659 case TLS_ST_CW_CHANGE:
661 return dtls_construct_change_cipher_spec(s);
663 return tls_construct_change_cipher_spec(s);
665 #if !defined(OPENSSL_NO_NEXTPROTONEG)
666 case TLS_ST_CW_NEXT_PROTO:
667 return tls_construct_next_proto(s);
669 case TLS_ST_CW_FINISHED:
670 return tls_construct_finished(s,
672 ssl3_enc->client_finished_label,
674 ssl3_enc->client_finished_label_len);
677 /* Shouldn't happen */
685 * Returns the maximum allowed length for the current message that we are
686 * reading. Excludes the message header.
688 unsigned long ossl_statem_client_max_message_size(SSL *s)
690 OSSL_STATEM *st = &s->statem;
692 switch(st->hand_state) {
693 case TLS_ST_CR_SRVR_HELLO:
694 return SERVER_HELLO_MAX_LENGTH;
696 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
697 return HELLO_VERIFY_REQUEST_MAX_LENGTH;
700 return s->max_cert_list;
702 case TLS_ST_CR_CERT_STATUS:
703 return SSL3_RT_MAX_PLAIN_LENGTH;
705 case TLS_ST_CR_KEY_EXCH:
706 return SERVER_KEY_EXCH_MAX_LENGTH;
708 case TLS_ST_CR_CERT_REQ:
709 return SSL3_RT_MAX_PLAIN_LENGTH;
711 case TLS_ST_CR_SRVR_DONE:
712 return SERVER_HELLO_DONE_MAX_LENGTH;
714 case TLS_ST_CR_CHANGE:
715 return CCS_MAX_LENGTH;
717 case TLS_ST_CR_SESSION_TICKET:
718 return SSL3_RT_MAX_PLAIN_LENGTH;
720 case TLS_ST_CR_FINISHED:
721 return FINISHED_MAX_LENGTH;
724 /* Shouldn't happen */
732 * Process a message that the client has been received from the server.
734 MSG_PROCESS_RETURN ossl_statem_client_process_message(SSL *s, PACKET *pkt)
736 OSSL_STATEM *st = &s->statem;
738 switch(st->hand_state) {
739 case TLS_ST_CR_SRVR_HELLO:
740 return tls_process_server_hello(s, pkt);
742 case DTLS_ST_CR_HELLO_VERIFY_REQUEST:
743 return dtls_process_hello_verify(s, pkt);
746 return tls_process_server_certificate(s, pkt);
748 case TLS_ST_CR_CERT_STATUS:
749 return tls_process_cert_status(s, pkt);
751 case TLS_ST_CR_KEY_EXCH:
752 return tls_process_key_exchange(s, pkt);
754 case TLS_ST_CR_CERT_REQ:
755 return tls_process_certificate_request(s, pkt);
757 case TLS_ST_CR_SRVR_DONE:
758 return tls_process_server_done(s, pkt);
760 case TLS_ST_CR_CHANGE:
761 return tls_process_change_cipher_spec(s, pkt);
763 case TLS_ST_CR_SESSION_TICKET:
764 return tls_process_new_session_ticket(s, pkt);
766 case TLS_ST_CR_FINISHED:
767 return tls_process_finished(s, pkt);
770 /* Shouldn't happen */
774 return MSG_PROCESS_ERROR;
778 * Perform any further processing required following the receipt of a message
781 WORK_STATE ossl_statem_client_post_process_message(SSL *s, WORK_STATE wst)
783 OSSL_STATEM *st = &s->statem;
785 switch(st->hand_state) {
786 #ifndef OPENSSL_NO_SCTP
787 case TLS_ST_CR_SRVR_DONE:
788 /* We only get here if we are using SCTP and we are renegotiating */
789 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s))) {
790 s->s3->in_read_app_data = 2;
791 s->rwstate = SSL_READING;
792 BIO_clear_retry_flags(SSL_get_rbio(s));
793 BIO_set_retry_read(SSL_get_rbio(s));
794 ossl_statem_set_sctp_read_sock(s, 1);
797 ossl_statem_set_sctp_read_sock(s, 0);
798 return WORK_FINISHED_STOP;
805 /* Shouldn't happen */
810 * Work out what version we should be using for the initial ClientHello if
811 * the version is currently set to (D)TLS_ANY_VERSION.
812 * Returns 1 on success
815 static int ssl_set_version(SSL *s)
817 unsigned long mask, options = s->options;
819 if (s->method->version == TLS_ANY_VERSION) {
821 * SSL_OP_NO_X disables all protocols above X *if* there are
822 * some protocols below X enabled. This is required in order
823 * to maintain "version capability" vector contiguous. So
824 * that if application wants to disable TLS1.0 in favour of
825 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
826 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
828 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
829 #if !defined(OPENSSL_NO_SSL3)
833 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
834 if (options & SSL_OP_NO_TLSv1_2) {
835 if ((options & mask) != mask) {
836 s->version = TLS1_1_VERSION;
838 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
842 s->version = TLS1_2_VERSION;
845 if ((options & mask) == mask) {
846 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
849 s->version = TLS1_1_VERSION;
852 mask &= ~SSL_OP_NO_TLSv1_1;
853 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
854 s->version = TLS1_VERSION;
855 mask &= ~SSL_OP_NO_TLSv1;
856 #if !defined(OPENSSL_NO_SSL3)
857 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
858 s->version = SSL3_VERSION;
861 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
862 SSLerr(SSL_F_SSL_SET_VERSION,
863 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
867 if (s->version == SSL3_VERSION && FIPS_mode()) {
868 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
872 } else if (s->method->version == DTLS_ANY_VERSION) {
873 /* Determine which DTLS version to use */
874 /* If DTLS 1.2 disabled correct the version number */
875 if (options & SSL_OP_NO_DTLSv1_2) {
876 if (tls1_suiteb(s)) {
877 SSLerr(SSL_F_SSL_SET_VERSION,
878 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
882 * Disabling all versions is silly: return an error.
884 if (options & SSL_OP_NO_DTLSv1) {
885 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
889 * Update method so we don't use any DTLS 1.2 features.
891 s->method = DTLSv1_client_method();
892 s->version = DTLS1_VERSION;
895 * We only support one version: update method
897 if (options & SSL_OP_NO_DTLSv1)
898 s->method = DTLSv1_2_client_method();
899 s->version = DTLS1_2_VERSION;
903 s->client_version = s->version;
908 int tls_construct_client_hello(SSL *s)
911 unsigned char *p, *d;
915 #ifndef OPENSSL_NO_COMP
919 SSL_SESSION *sess = s->session;
921 buf = (unsigned char *)s->init_buf->data;
923 /* Work out what SSL/TLS/DTLS version to use */
924 if (ssl_set_version(s) == 0)
927 if ((sess == NULL) || (sess->ssl_version != s->version) ||
929 * In the case of EAP-FAST, we can have a pre-shared
930 * "ticket" without a session ID.
932 (!sess->session_id_length && !sess->tlsext_tick) ||
933 (sess->not_resumable)) {
934 if (!ssl_get_new_session(s, 0))
937 /* else use the pre-loaded session */
939 p = s->s3->client_random;
942 * for DTLS if client_random is initialized, reuse it, we are
943 * required to use same upon reply to HelloVerify
945 if (SSL_IS_DTLS(s)) {
948 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
957 if (i && ssl_fill_hello_random(s, 0, p,
958 sizeof(s->s3->client_random)) <= 0)
961 /* Do the message type and length last */
962 d = p = ssl_handshake_start(s);
965 * version indicates the negotiated version: for example from
966 * an SSLv2/v3 compatible client hello). The client_version
967 * field is the maximum version we permit and it is also
968 * used in RSA encrypted premaster secrets. Some servers can
969 * choke if we initially report a higher version then
970 * renegotiate to a lower one in the premaster secret. This
971 * didn't happen with TLS 1.0 as most servers supported it
972 * but it can with TLS 1.1 or later if the server only supports
975 * Possible scenario with previous logic:
976 * 1. Client hello indicates TLS 1.2
977 * 2. Server hello says TLS 1.0
978 * 3. RSA encrypted premaster secret uses 1.2.
979 * 4. Handhaked proceeds using TLS 1.0.
980 * 5. Server sends hello request to renegotiate.
981 * 6. Client hello indicates TLS v1.0 as we now
982 * know that is maximum server supports.
983 * 7. Server chokes on RSA encrypted premaster secret
984 * containing version 1.0.
986 * For interoperability it should be OK to always use the
987 * maximum version we support in client hello and then rely
988 * on the checking of version to ensure the servers isn't
989 * being inconsistent: for example initially negotiating with
990 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
991 * client_version in client hello and not resetting it to
992 * the negotiated version.
994 *(p++) = s->client_version >> 8;
995 *(p++) = s->client_version & 0xff;
998 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
999 p += SSL3_RANDOM_SIZE;
1005 i = s->session->session_id_length;
1008 if (i > (int)sizeof(s->session->session_id)) {
1009 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1012 memcpy(p, s->session->session_id, i);
1016 /* cookie stuff for DTLS */
1017 if (SSL_IS_DTLS(s)) {
1018 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
1019 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1022 *(p++) = s->d1->cookie_len;
1023 memcpy(p, s->d1->cookie, s->d1->cookie_len);
1024 p += s->d1->cookie_len;
1027 /* Ciphers supported */
1028 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]));
1030 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
1033 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
1035 * Some servers hang if client hello > 256 bytes as hack workaround
1036 * chop number of supported ciphers to keep it well below this if we
1039 if (TLS1_get_version(s) >= TLS1_2_VERSION
1040 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
1041 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
1047 #ifdef OPENSSL_NO_COMP
1051 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
1054 j = sk_SSL_COMP_num(s->ctx->comp_methods);
1056 for (i = 0; i < j; i++) {
1057 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
1061 *(p++) = 0; /* Add the NULL method */
1063 /* TLS extensions */
1064 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
1065 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1069 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1071 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1072 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1077 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
1078 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
1079 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1085 ossl_statem_set_error(s);
1089 MSG_PROCESS_RETURN dtls_process_hello_verify(SSL *s, PACKET *pkt)
1092 unsigned int cookie_len;
1095 if (!PACKET_forward(pkt, 2)
1096 || !PACKET_get_length_prefixed_1(pkt, &cookiepkt)) {
1097 al = SSL_AD_DECODE_ERROR;
1098 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1102 cookie_len = PACKET_remaining(&cookiepkt);
1103 if (cookie_len > sizeof(s->d1->cookie)) {
1104 al = SSL_AD_ILLEGAL_PARAMETER;
1105 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_TOO_LONG);
1109 if (!PACKET_copy_bytes(&cookiepkt, s->d1->cookie, cookie_len)) {
1110 al = SSL_AD_DECODE_ERROR;
1111 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY, SSL_R_LENGTH_MISMATCH);
1114 s->d1->cookie_len = cookie_len;
1116 return MSG_PROCESS_FINISHED_READING;
1118 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1119 ossl_statem_set_error(s);
1120 return MSG_PROCESS_ERROR;
1123 MSG_PROCESS_RETURN tls_process_server_hello(SSL *s, PACKET *pkt)
1125 STACK_OF(SSL_CIPHER) *sk;
1126 const SSL_CIPHER *c;
1128 size_t session_id_len;
1129 unsigned char *cipherchars;
1130 int i, al = SSL_AD_INTERNAL_ERROR;
1131 unsigned int compression;
1132 #ifndef OPENSSL_NO_COMP
1136 if (s->method->version == TLS_ANY_VERSION) {
1137 unsigned int sversion;
1139 if (!PACKET_get_net_2(pkt, &sversion)) {
1140 al = SSL_AD_DECODE_ERROR;
1141 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1145 #if TLS_MAX_VERSION != TLS1_2_VERSION
1146 #error Code needs updating for new TLS version
1148 #ifndef OPENSSL_NO_SSL3
1149 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
1151 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1152 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
1153 al = SSL_AD_PROTOCOL_VERSION;
1156 s->method = SSLv3_client_method();
1159 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
1160 s->method = TLSv1_client_method();
1161 } else if ((sversion == TLS1_1_VERSION) &&
1162 !(s->options & SSL_OP_NO_TLSv1_1)) {
1163 s->method = TLSv1_1_client_method();
1164 } else if ((sversion == TLS1_2_VERSION) &&
1165 !(s->options & SSL_OP_NO_TLSv1_2)) {
1166 s->method = TLSv1_2_client_method();
1168 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1169 al = SSL_AD_PROTOCOL_VERSION;
1172 s->session->ssl_version = s->version = s->method->version;
1174 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1175 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1176 al = SSL_AD_PROTOCOL_VERSION;
1179 } else if (s->method->version == DTLS_ANY_VERSION) {
1180 /* Work out correct protocol version to use */
1181 unsigned int hversion;
1184 if (!PACKET_get_net_2(pkt, &hversion)) {
1185 al = SSL_AD_DECODE_ERROR;
1186 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1190 options = s->options;
1191 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1192 s->method = DTLSv1_2_client_method();
1193 else if (tls1_suiteb(s)) {
1194 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1195 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1196 s->version = hversion;
1197 al = SSL_AD_PROTOCOL_VERSION;
1199 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1200 s->method = DTLSv1_client_method();
1202 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1203 s->version = hversion;
1204 al = SSL_AD_PROTOCOL_VERSION;
1207 s->session->ssl_version = s->version = s->method->version;
1209 unsigned char *vers;
1211 if (!PACKET_get_bytes(pkt, &vers, 2)) {
1212 al = SSL_AD_DECODE_ERROR;
1213 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1216 if ((vers[0] != (s->version >> 8))
1217 || (vers[1] != (s->version & 0xff))) {
1218 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1219 s->version = (s->version & 0xff00) | vers[1];
1220 al = SSL_AD_PROTOCOL_VERSION;
1225 /* load the server hello data */
1226 /* load the server random */
1227 if (!PACKET_copy_bytes(pkt, s->s3->server_random, SSL3_RANDOM_SIZE)) {
1228 al = SSL_AD_DECODE_ERROR;
1229 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1235 /* Get the session-id. */
1236 if (!PACKET_get_length_prefixed_1(pkt, &session_id)) {
1237 al = SSL_AD_DECODE_ERROR;
1238 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1241 session_id_len = PACKET_remaining(&session_id);
1242 if (session_id_len > sizeof s->session->session_id
1243 || session_id_len > SSL3_SESSION_ID_SIZE) {
1244 al = SSL_AD_ILLEGAL_PARAMETER;
1245 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1249 if (!PACKET_get_bytes(pkt, &cipherchars, TLS_CIPHER_LEN)) {
1250 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1251 al = SSL_AD_DECODE_ERROR;
1256 * Check if we can resume the session based on external pre-shared secret.
1257 * EAP-FAST (RFC 4851) supports two types of session resumption.
1258 * Resumption based on server-side state works with session IDs.
1259 * Resumption based on pre-shared Protected Access Credentials (PACs)
1260 * works by overriding the SessionTicket extension at the application
1261 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1262 * servers would honour the session ID.) Therefore, the session ID alone
1263 * is not a reliable indicator of session resumption, so we first check if
1264 * we can resume, and later peek at the next handshake message to see if the
1265 * server wants to resume.
1267 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1268 s->session->tlsext_tick) {
1269 SSL_CIPHER *pref_cipher = NULL;
1270 s->session->master_key_length = sizeof(s->session->master_key);
1271 if (s->tls_session_secret_cb(s, s->session->master_key,
1272 &s->session->master_key_length,
1274 s->tls_session_secret_cb_arg)) {
1275 s->session->cipher = pref_cipher ?
1276 pref_cipher : ssl_get_cipher_by_char(s, cipherchars);
1278 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1279 al = SSL_AD_INTERNAL_ERROR;
1284 if (session_id_len != 0 && session_id_len == s->session->session_id_length
1285 && memcmp(PACKET_data(&session_id), s->session->session_id,
1286 session_id_len) == 0) {
1287 if (s->sid_ctx_length != s->session->sid_ctx_length
1288 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1289 /* actually a client application bug */
1290 al = SSL_AD_ILLEGAL_PARAMETER;
1291 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1292 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1298 * If we were trying for session-id reuse but the server
1299 * didn't echo the ID, make a new SSL_SESSION.
1300 * In the case of EAP-FAST and PAC, we do not send a session ID,
1301 * so the PAC-based session secret is always preserved. It'll be
1302 * overwritten if the server refuses resumption.
1304 if (s->session->session_id_length > 0) {
1305 if (!ssl_get_new_session(s, 0)) {
1310 s->session->session_id_length = session_id_len;
1311 /* session_id_len could be 0 */
1312 memcpy(s->session->session_id, PACKET_data(&session_id),
1316 c = ssl_get_cipher_by_char(s, cipherchars);
1318 /* unknown cipher */
1319 al = SSL_AD_ILLEGAL_PARAMETER;
1320 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1323 /* Set version disabled mask now we know version */
1324 if (!SSL_USE_TLS1_2_CIPHERS(s))
1325 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1327 s->s3->tmp.mask_ssl = 0;
1328 /* Skip TLS v1.0 ciphersuites if SSLv3 */
1329 if ((c->algorithm_ssl & SSL_TLSV1) && s->version == SSL3_VERSION)
1330 s->s3->tmp.mask_ssl |= SSL_TLSV1;
1332 * If it is a disabled cipher we didn't send it in client hello, so
1335 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1336 al = SSL_AD_ILLEGAL_PARAMETER;
1337 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1341 sk = ssl_get_ciphers_by_id(s);
1342 i = sk_SSL_CIPHER_find(sk, c);
1344 /* we did not say we would use this cipher */
1345 al = SSL_AD_ILLEGAL_PARAMETER;
1346 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1351 * Depending on the session caching (internal/external), the cipher
1352 * and/or cipher_id values may not be set. Make sure that cipher_id is
1353 * set and use it for comparison.
1355 if (s->session->cipher)
1356 s->session->cipher_id = s->session->cipher->id;
1357 if (s->hit && (s->session->cipher_id != c->id)) {
1358 al = SSL_AD_ILLEGAL_PARAMETER;
1359 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1360 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1363 s->s3->tmp.new_cipher = c;
1365 * Don't digest cached records if no sigalgs: we may need them for client
1368 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1370 /* lets get the compression algorithm */
1372 if (!PACKET_get_1(pkt, &compression)) {
1373 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_LENGTH_MISMATCH);
1374 al = SSL_AD_DECODE_ERROR;
1377 #ifdef OPENSSL_NO_COMP
1378 if (compression != 0) {
1379 al = SSL_AD_ILLEGAL_PARAMETER;
1380 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1381 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1385 * If compression is disabled we'd better not try to resume a session
1386 * using compression.
1388 if (s->session->compress_meth != 0) {
1389 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1393 if (s->hit && compression != s->session->compress_meth) {
1394 al = SSL_AD_ILLEGAL_PARAMETER;
1395 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1396 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1399 if (compression == 0)
1401 else if (!ssl_allow_compression(s)) {
1402 al = SSL_AD_ILLEGAL_PARAMETER;
1403 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1406 comp = ssl3_comp_find(s->ctx->comp_methods, compression);
1409 if (compression != 0 && comp == NULL) {
1410 al = SSL_AD_ILLEGAL_PARAMETER;
1411 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO,
1412 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1415 s->s3->tmp.new_compression = comp;
1419 /* TLS extensions */
1420 if (!ssl_parse_serverhello_tlsext(s, pkt)) {
1421 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1425 if (PACKET_remaining(pkt) != 0) {
1426 /* wrong packet length */
1427 al = SSL_AD_DECODE_ERROR;
1428 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1432 #ifndef OPENSSL_NO_SCTP
1433 if (SSL_IS_DTLS(s) && s->hit) {
1434 unsigned char sctpauthkey[64];
1435 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
1438 * Add new shared key for SCTP-Auth, will be ignored if
1441 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
1442 sizeof(DTLS1_SCTP_AUTH_LABEL));
1444 if (SSL_export_keying_material(s, sctpauthkey,
1445 sizeof(sctpauthkey),
1447 sizeof(labelbuffer), NULL, 0,
1451 BIO_ctrl(SSL_get_wbio(s),
1452 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
1453 sizeof(sctpauthkey), sctpauthkey);
1457 return MSG_PROCESS_CONTINUE_READING;
1459 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1461 ossl_statem_set_error(s);
1462 return MSG_PROCESS_ERROR;
1465 MSG_PROCESS_RETURN tls_process_server_certificate(SSL *s, PACKET *pkt)
1467 int al, i, ret = MSG_PROCESS_ERROR, exp_idx;
1468 unsigned long cert_list_len, cert_len;
1470 unsigned char *certstart, *certbytes;
1471 STACK_OF(X509) *sk = NULL;
1472 EVP_PKEY *pkey = NULL;
1474 if ((sk = sk_X509_new_null()) == NULL) {
1475 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1479 if (!PACKET_get_net_3(pkt, &cert_list_len)
1480 || PACKET_remaining(pkt) != cert_list_len) {
1481 al = SSL_AD_DECODE_ERROR;
1482 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1485 while (PACKET_remaining(pkt)) {
1486 if (!PACKET_get_net_3(pkt, &cert_len)
1487 || !PACKET_get_bytes(pkt, &certbytes, cert_len)) {
1488 al = SSL_AD_DECODE_ERROR;
1489 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1490 SSL_R_CERT_LENGTH_MISMATCH);
1494 certstart = certbytes;
1495 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1497 al = SSL_AD_BAD_CERTIFICATE;
1498 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1501 if (certbytes != (certstart + cert_len)) {
1502 al = SSL_AD_DECODE_ERROR;
1503 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1504 SSL_R_CERT_LENGTH_MISMATCH);
1507 if (!sk_X509_push(sk, x)) {
1508 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1514 i = ssl_verify_cert_chain(s, sk);
1515 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1516 al = ssl_verify_alarm_type(s->verify_result);
1517 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1518 SSL_R_CERTIFICATE_VERIFY_FAILED);
1521 ERR_clear_error(); /* but we keep s->verify_result */
1523 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE, i);
1524 al = SSL_AD_HANDSHAKE_FAILURE;
1528 s->session->peer_chain = sk;
1530 * Inconsistency alert: cert_chain does include the peer's certificate,
1531 * which we don't include in s3_srvr.c
1533 x = sk_X509_value(sk, 0);
1536 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1539 pkey = X509_get_pubkey(x);
1541 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1544 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1545 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1549 i = ssl_cert_type(x, pkey);
1553 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1554 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1558 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1559 if (exp_idx >= 0 && i != exp_idx) {
1561 al = SSL_AD_ILLEGAL_PARAMETER;
1562 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE,
1563 SSL_R_WRONG_CERTIFICATE_TYPE);
1566 s->session->peer_type = i;
1568 X509_free(s->session->peer);
1570 s->session->peer = x;
1571 s->session->verify_result = s->verify_result;
1574 ret = MSG_PROCESS_CONTINUE_READING;
1578 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1580 ossl_statem_set_error(s);
1582 EVP_PKEY_free(pkey);
1584 sk_X509_pop_free(sk, X509_free);
1588 MSG_PROCESS_RETURN tls_process_key_exchange(SSL *s, PACKET *pkt)
1590 #ifndef OPENSSL_NO_RSA
1591 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1594 int al, j, verify_ret;
1596 EVP_PKEY *pkey = NULL;
1597 const EVP_MD *md = NULL;
1598 #ifndef OPENSSL_NO_RSA
1601 #ifndef OPENSSL_NO_DH
1604 #ifndef OPENSSL_NO_EC
1605 EC_KEY *ecdh = NULL;
1606 BN_CTX *bn_ctx = NULL;
1607 EC_POINT *srvr_ecpoint = NULL;
1610 PACKET save_param_start, signature;
1612 EVP_MD_CTX_init(&md_ctx);
1614 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1616 save_param_start = *pkt;
1618 #ifndef OPENSSL_NO_RSA
1619 RSA_free(s->s3->peer_rsa_tmp);
1620 s->s3->peer_rsa_tmp = NULL;
1622 #ifndef OPENSSL_NO_DH
1623 DH_free(s->s3->peer_dh_tmp);
1624 s->s3->peer_dh_tmp = NULL;
1626 #ifndef OPENSSL_NO_EC
1627 EC_KEY_free(s->s3->peer_ecdh_tmp);
1628 s->s3->peer_ecdh_tmp = NULL;
1631 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1633 al = SSL_AD_DECODE_ERROR;
1635 #ifndef OPENSSL_NO_PSK
1636 /* PSK ciphersuites are preceded by an identity hint */
1637 if (alg_k & SSL_PSK) {
1638 PACKET psk_identity_hint;
1639 if (!PACKET_get_length_prefixed_2(pkt, &psk_identity_hint)) {
1640 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1645 * Store PSK identity hint for later use, hint is used in
1646 * ssl3_send_client_key_exchange. Assume that the maximum length of
1647 * a PSK identity hint can be as long as the maximum length of a PSK
1650 if (PACKET_remaining(&psk_identity_hint) > PSK_MAX_IDENTITY_LEN) {
1651 al = SSL_AD_HANDSHAKE_FAILURE;
1652 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1656 if (PACKET_remaining(&psk_identity_hint) == 0) {
1657 OPENSSL_free(s->session->psk_identity_hint);
1658 s->session->psk_identity_hint = NULL;
1659 } else if (!PACKET_strndup(&psk_identity_hint,
1660 &s->session->psk_identity_hint)) {
1661 al = SSL_AD_INTERNAL_ERROR;
1666 /* Nothing else to do for plain PSK or RSAPSK */
1667 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1669 #endif /* !OPENSSL_NO_PSK */
1670 #ifndef OPENSSL_NO_SRP
1671 if (alg_k & SSL_kSRP) {
1672 PACKET prime, generator, salt, server_pub;
1673 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1674 || !PACKET_get_length_prefixed_2(pkt, &generator)
1675 || !PACKET_get_length_prefixed_1(pkt, &salt)
1676 || !PACKET_get_length_prefixed_2(pkt, &server_pub)) {
1677 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1682 BN_bin2bn(PACKET_data(&prime),
1683 PACKET_remaining(&prime), NULL)) == NULL
1685 BN_bin2bn(PACKET_data(&generator),
1686 PACKET_remaining(&generator), NULL)) == NULL
1688 BN_bin2bn(PACKET_data(&salt),
1689 PACKET_remaining(&salt), NULL)) == NULL
1691 BN_bin2bn(PACKET_data(&server_pub),
1692 PACKET_remaining(&server_pub), NULL)) == NULL) {
1693 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1697 if (!srp_verify_server_param(s, &al)) {
1698 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1702 /* We must check if there is a certificate */
1703 if (alg_a & (SSL_aRSA|SSL_aDSS))
1704 pkey = X509_get_pubkey(s->session->peer);
1706 #endif /* !OPENSSL_NO_SRP */
1707 #ifndef OPENSSL_NO_RSA
1708 if (alg_k & SSL_kRSA) {
1710 /* Temporary RSA keys only allowed in export ciphersuites */
1711 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1712 al = SSL_AD_UNEXPECTED_MESSAGE;
1713 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1717 if (!PACKET_get_length_prefixed_2(pkt, &mod)
1718 || !PACKET_get_length_prefixed_2(pkt, &exp)) {
1719 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1723 if ((rsa = RSA_new()) == NULL) {
1724 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1728 if ((rsa->n = BN_bin2bn(PACKET_data(&mod), PACKET_remaining(&mod),
1730 || (rsa->e = BN_bin2bn(PACKET_data(&exp), PACKET_remaining(&exp),
1732 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1736 /* this should be because we are using an export cipher */
1737 if (alg_a & SSL_aRSA)
1738 pkey = X509_get_pubkey(s->session->peer);
1740 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1744 s->s3->peer_rsa_tmp = rsa;
1747 #else /* OPENSSL_NO_RSA */
1750 #ifndef OPENSSL_NO_DH
1751 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1752 PACKET prime, generator, pub_key;
1754 if (!PACKET_get_length_prefixed_2(pkt, &prime)
1755 || !PACKET_get_length_prefixed_2(pkt, &generator)
1756 || !PACKET_get_length_prefixed_2(pkt, &pub_key)) {
1757 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1761 if ((dh = DH_new()) == NULL) {
1762 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_DH_LIB);
1766 if ((dh->p = BN_bin2bn(PACKET_data(&prime),
1767 PACKET_remaining(&prime), NULL)) == NULL
1768 || (dh->g = BN_bin2bn(PACKET_data(&generator),
1769 PACKET_remaining(&generator), NULL)) == NULL
1771 BN_bin2bn(PACKET_data(&pub_key),
1772 PACKET_remaining(&pub_key), NULL)) == NULL) {
1773 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_BN_LIB);
1777 if (BN_is_zero(dh->p) || BN_is_zero(dh->g) || BN_is_zero(dh->pub_key)) {
1778 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_DH_VALUE);
1782 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1783 al = SSL_AD_HANDSHAKE_FAILURE;
1784 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1787 if (alg_a & (SSL_aRSA|SSL_aDSS))
1788 pkey = X509_get_pubkey(s->session->peer);
1789 /* else anonymous DH, so no certificate or pkey. */
1791 s->s3->peer_dh_tmp = dh;
1794 #endif /* !OPENSSL_NO_DH */
1796 #ifndef OPENSSL_NO_EC
1797 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1799 const EC_GROUP *group;
1801 unsigned char *ecparams;
1803 if ((ecdh = EC_KEY_new()) == NULL) {
1804 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1809 * Extract elliptic curve parameters and the server's ephemeral ECDH
1810 * public key. For now we only support named (not generic) curves and
1811 * ECParameters in this case is just three bytes.
1813 if (!PACKET_get_bytes(pkt, &ecparams, 3)) {
1814 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1818 * Check curve is one of our preferences, if not server has sent an
1819 * invalid curve. ECParameters is 3 bytes.
1821 if (!tls1_check_curve(s, ecparams, 3)) {
1822 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1826 if ((curve_nid = tls1_ec_curve_id2nid(*(ecparams + 2))) == 0) {
1827 al = SSL_AD_INTERNAL_ERROR;
1828 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1829 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1833 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1834 if (ngroup == NULL) {
1835 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1838 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1839 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EC_LIB);
1842 EC_GROUP_free(ngroup);
1844 group = EC_KEY_get0_group(ecdh);
1846 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1847 (EC_GROUP_get_degree(group) > 163)) {
1848 al = SSL_AD_EXPORT_RESTRICTION;
1849 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1850 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1854 /* Next, get the encoded ECPoint */
1855 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1856 ((bn_ctx = BN_CTX_new()) == NULL)) {
1857 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1861 if (!PACKET_get_length_prefixed_1(pkt, &encoded_pt)) {
1862 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1866 if (EC_POINT_oct2point(group, srvr_ecpoint, PACKET_data(&encoded_pt),
1867 PACKET_remaining(&encoded_pt), bn_ctx) == 0) {
1868 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1873 * The ECC/TLS specification does not mention the use of DSA to sign
1874 * ECParameters in the server key exchange message. We do support RSA
1878 # ifndef OPENSSL_NO_RSA
1879 else if (alg_a & SSL_aRSA)
1880 pkey = X509_get_pubkey(s->session->peer);
1882 # ifndef OPENSSL_NO_EC
1883 else if (alg_a & SSL_aECDSA)
1884 pkey = X509_get_pubkey(s->session->peer);
1886 /* else anonymous ECDH, so no certificate or pkey. */
1887 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1888 s->s3->peer_ecdh_tmp = ecdh;
1890 BN_CTX_free(bn_ctx);
1892 EC_POINT_free(srvr_ecpoint);
1893 srvr_ecpoint = NULL;
1895 al = SSL_AD_UNEXPECTED_MESSAGE;
1896 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1899 #endif /* !OPENSSL_NO_EC */
1901 /* if it was signed, check the signature */
1905 * |pkt| now points to the beginning of the signature, so the difference
1906 * equals the length of the parameters.
1908 if (!PACKET_get_sub_packet(&save_param_start, ¶ms,
1909 PACKET_remaining(&save_param_start) -
1910 PACKET_remaining(pkt))) {
1911 al = SSL_AD_INTERNAL_ERROR;
1912 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1916 if (SSL_USE_SIGALGS(s)) {
1917 unsigned char *sigalgs;
1919 if (!PACKET_get_bytes(pkt, &sigalgs, 2)) {
1920 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1923 rv = tls12_check_peer_sigalg(&md, s, sigalgs, pkey);
1930 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1936 if (!PACKET_get_length_prefixed_2(pkt, &signature)
1937 || PACKET_remaining(pkt) != 0) {
1938 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
1941 j = EVP_PKEY_size(pkey);
1943 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1948 * Check signature length
1950 if (PACKET_remaining(&signature) > (size_t)j) {
1951 /* wrong packet length */
1952 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1955 #ifndef OPENSSL_NO_RSA
1956 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1962 for (num = 2; num > 0; num--) {
1963 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1964 if (EVP_DigestInit_ex(&md_ctx,
1965 (num == 2) ? s->ctx->md5 : s->ctx->sha1,
1967 || EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1968 SSL3_RANDOM_SIZE) <= 0
1969 || EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1970 SSL3_RANDOM_SIZE) <= 0
1971 || EVP_DigestUpdate(&md_ctx, PACKET_data(¶ms),
1972 PACKET_remaining(¶ms)) <= 0
1973 || EVP_DigestFinal_ex(&md_ctx, q, &size) <= 0) {
1974 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE,
1975 ERR_R_INTERNAL_ERROR);
1976 al = SSL_AD_INTERNAL_ERROR;
1983 RSA_verify(NID_md5_sha1, md_buf, j, PACKET_data(&signature),
1984 PACKET_remaining(&signature), pkey->pkey.rsa);
1985 if (verify_ret < 0) {
1986 al = SSL_AD_DECRYPT_ERROR;
1987 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1990 if (verify_ret == 0) {
1992 al = SSL_AD_DECRYPT_ERROR;
1993 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1999 if (EVP_VerifyInit_ex(&md_ctx, md, NULL) <= 0
2000 || EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
2001 SSL3_RANDOM_SIZE) <= 0
2002 || EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
2003 SSL3_RANDOM_SIZE) <= 0
2004 || EVP_VerifyUpdate(&md_ctx, PACKET_data(¶ms),
2005 PACKET_remaining(¶ms)) <= 0) {
2006 al = SSL_AD_INTERNAL_ERROR;
2007 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_EVP_LIB);
2010 if (EVP_VerifyFinal(&md_ctx, PACKET_data(&signature),
2011 PACKET_remaining(&signature), pkey) <= 0) {
2013 al = SSL_AD_DECRYPT_ERROR;
2014 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
2019 /* aNULL, aSRP or PSK do not need public keys */
2020 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
2021 /* Might be wrong key type, check it */
2022 if (ssl3_check_cert_and_algorithm(s))
2023 /* Otherwise this shouldn't happen */
2024 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2027 /* still data left over */
2028 if (PACKET_remaining(pkt) != 0) {
2029 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
2033 EVP_PKEY_free(pkey);
2034 EVP_MD_CTX_cleanup(&md_ctx);
2035 return MSG_PROCESS_CONTINUE_READING;
2037 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2039 EVP_PKEY_free(pkey);
2040 #ifndef OPENSSL_NO_RSA
2043 #ifndef OPENSSL_NO_DH
2046 #ifndef OPENSSL_NO_EC
2047 BN_CTX_free(bn_ctx);
2048 EC_POINT_free(srvr_ecpoint);
2051 EVP_MD_CTX_cleanup(&md_ctx);
2052 ossl_statem_set_error(s);
2053 return MSG_PROCESS_ERROR;
2056 MSG_PROCESS_RETURN tls_process_certificate_request(SSL *s, PACKET *pkt)
2058 int ret = MSG_PROCESS_ERROR;
2059 unsigned int list_len, ctype_num, i, name_len;
2060 X509_NAME *xn = NULL;
2061 unsigned char *data;
2062 unsigned char *namestart, *namebytes;
2063 STACK_OF(X509_NAME) *ca_sk = NULL;
2065 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2066 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2070 /* get the certificate types */
2071 if (!PACKET_get_1(pkt, &ctype_num)
2072 || !PACKET_get_bytes(pkt, &data, ctype_num)) {
2073 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2074 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2077 OPENSSL_free(s->cert->ctypes);
2078 s->cert->ctypes = NULL;
2079 if (ctype_num > SSL3_CT_NUMBER) {
2080 /* If we exceed static buffer copy all to cert structure */
2081 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2082 if (s->cert->ctypes == NULL) {
2083 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2086 memcpy(s->cert->ctypes, data, ctype_num);
2087 s->cert->ctype_num = (size_t)ctype_num;
2088 ctype_num = SSL3_CT_NUMBER;
2090 for (i = 0; i < ctype_num; i++)
2091 s->s3->tmp.ctype[i] = data[i];
2093 if (SSL_USE_SIGALGS(s)) {
2094 if (!PACKET_get_net_2(pkt, &list_len)
2095 || !PACKET_get_bytes(pkt, &data, list_len)) {
2096 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2097 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2098 SSL_R_LENGTH_MISMATCH);
2102 /* Clear certificate digests and validity flags */
2103 for (i = 0; i < SSL_PKEY_NUM; i++) {
2104 s->s3->tmp.md[i] = NULL;
2105 s->s3->tmp.valid_flags[i] = 0;
2107 if ((list_len & 1) || !tls1_save_sigalgs(s, data, list_len)) {
2108 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2109 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2110 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2113 if (!tls1_process_sigalgs(s)) {
2114 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2115 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2120 /* get the CA RDNs */
2121 if (!PACKET_get_net_2(pkt, &list_len)
2122 || PACKET_remaining(pkt) != list_len) {
2123 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2124 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2128 while (PACKET_remaining(pkt)) {
2129 if (!PACKET_get_net_2(pkt, &name_len)
2130 || !PACKET_get_bytes(pkt, &namebytes, name_len)) {
2131 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2132 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2133 SSL_R_LENGTH_MISMATCH);
2137 namestart = namebytes;
2139 if ((xn = d2i_X509_NAME(NULL, (const unsigned char **)&namebytes,
2140 name_len)) == NULL) {
2141 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2142 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2146 if (namebytes != (namestart + name_len)) {
2147 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2148 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST,
2149 SSL_R_CA_DN_LENGTH_MISMATCH);
2152 if (!sk_X509_NAME_push(ca_sk, xn)) {
2153 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2158 /* we should setup a certificate to return.... */
2159 s->s3->tmp.cert_req = 1;
2160 s->s3->tmp.ctype_num = ctype_num;
2161 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2162 s->s3->tmp.ca_names = ca_sk;
2165 ret = MSG_PROCESS_CONTINUE_READING;
2168 ossl_statem_set_error(s);
2170 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2174 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2176 return (X509_NAME_cmp(*a, *b));
2179 MSG_PROCESS_RETURN tls_process_new_session_ticket(SSL *s, PACKET *pkt)
2182 unsigned int ticklen;
2183 unsigned long ticket_lifetime_hint;
2185 if (!PACKET_get_net_4(pkt, &ticket_lifetime_hint)
2186 || !PACKET_get_net_2(pkt, &ticklen)
2187 || PACKET_remaining(pkt) != ticklen) {
2188 al = SSL_AD_DECODE_ERROR;
2189 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2193 /* Server is allowed to change its mind and send an empty ticket. */
2195 return MSG_PROCESS_CONTINUE_READING;
2197 if (s->session->session_id_length > 0) {
2198 int i = s->session_ctx->session_cache_mode;
2199 SSL_SESSION *new_sess;
2201 * We reused an existing session, so we need to replace it with a new
2204 if (i & SSL_SESS_CACHE_CLIENT) {
2206 * Remove the old session from the cache
2208 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2209 if (s->session_ctx->remove_session_cb != NULL)
2210 s->session_ctx->remove_session_cb(s->session_ctx,
2213 /* We carry on if this fails */
2214 SSL_CTX_remove_session(s->session_ctx, s->session);
2218 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2219 al = SSL_AD_INTERNAL_ERROR;
2220 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2224 SSL_SESSION_free(s->session);
2225 s->session = new_sess;
2228 OPENSSL_free(s->session->tlsext_tick);
2229 s->session->tlsext_ticklen = 0;
2231 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2232 if (s->session->tlsext_tick == NULL) {
2233 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2236 if (!PACKET_copy_bytes(pkt, s->session->tlsext_tick, ticklen)) {
2237 al = SSL_AD_DECODE_ERROR;
2238 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2242 s->session->tlsext_tick_lifetime_hint = ticket_lifetime_hint;
2243 s->session->tlsext_ticklen = ticklen;
2245 * There are two ways to detect a resumed ticket session. One is to set
2246 * an appropriate session ID and then the server must return a match in
2247 * ServerHello. This allows the normal client session ID matching to work
2248 * and we know much earlier that the ticket has been accepted. The
2249 * other way is to set zero length session ID when the ticket is
2250 * presented and rely on the handshake to determine session resumption.
2251 * We choose the former approach because this fits in with assumptions
2252 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2253 * SHA256 is disabled) hash of the ticket.
2255 EVP_Digest(s->session->tlsext_tick, ticklen,
2256 s->session->session_id, &s->session->session_id_length,
2257 EVP_sha256(), NULL);
2258 return MSG_PROCESS_CONTINUE_READING;
2260 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2262 ossl_statem_set_error(s);
2263 return MSG_PROCESS_ERROR;
2266 MSG_PROCESS_RETURN tls_process_cert_status(SSL *s, PACKET *pkt)
2269 unsigned long resplen;
2272 if (!PACKET_get_1(pkt, &type)
2273 || type != TLSEXT_STATUSTYPE_ocsp) {
2274 al = SSL_AD_DECODE_ERROR;
2275 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2278 if (!PACKET_get_net_3(pkt, &resplen)
2279 || PACKET_remaining(pkt) != resplen) {
2280 al = SSL_AD_DECODE_ERROR;
2281 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2284 OPENSSL_free(s->tlsext_ocsp_resp);
2285 s->tlsext_ocsp_resp = OPENSSL_malloc(resplen);
2286 if (s->tlsext_ocsp_resp == NULL) {
2287 al = SSL_AD_INTERNAL_ERROR;
2288 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2291 if (!PACKET_copy_bytes(pkt, s->tlsext_ocsp_resp, resplen)) {
2292 al = SSL_AD_DECODE_ERROR;
2293 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2296 s->tlsext_ocsp_resplen = resplen;
2297 if (s->ctx->tlsext_status_cb) {
2299 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2301 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2302 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2306 al = SSL_AD_INTERNAL_ERROR;
2307 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2311 return MSG_PROCESS_CONTINUE_READING;
2313 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2314 ossl_statem_set_error(s);
2315 return MSG_PROCESS_ERROR;
2318 MSG_PROCESS_RETURN tls_process_server_done(SSL *s, PACKET *pkt)
2320 if (PACKET_remaining(pkt) > 0) {
2321 /* should contain no data */
2322 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2323 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2324 ossl_statem_set_error(s);
2325 return MSG_PROCESS_ERROR;
2328 #ifndef OPENSSL_NO_SRP
2329 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2330 if (SRP_Calc_A_param(s) <= 0) {
2331 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE, SSL_R_SRP_A_CALC);
2332 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2333 ossl_statem_set_error(s);
2334 return MSG_PROCESS_ERROR;
2340 * at this point we check that we have the required stuff from
2343 if (!ssl3_check_cert_and_algorithm(s)) {
2344 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2345 ossl_statem_set_error(s);
2346 return MSG_PROCESS_ERROR;
2349 #ifndef OPENSSL_NO_SCTP
2350 /* Only applies to renegotiation */
2351 if (SSL_IS_DTLS(s) && BIO_dgram_is_sctp(SSL_get_wbio(s))
2352 && s->renegotiate != 0)
2353 return MSG_PROCESS_CONTINUE_PROCESSING;
2356 return MSG_PROCESS_FINISHED_READING;
2359 int tls_construct_client_key_exchange(SSL *s)
2363 #ifndef OPENSSL_NO_PSK
2364 size_t pskhdrlen = 0;
2366 unsigned long alg_k;
2367 #ifndef OPENSSL_NO_RSA
2369 EVP_PKEY *pkey = NULL;
2371 #ifndef OPENSSL_NO_EC
2372 EC_KEY *clnt_ecdh = NULL;
2373 const EC_POINT *srvr_ecpoint = NULL;
2374 EVP_PKEY *srvr_pub_pkey = NULL;
2375 unsigned char *encodedPoint = NULL;
2376 int encoded_pt_len = 0;
2377 BN_CTX *bn_ctx = NULL;
2379 unsigned char *pms = NULL;
2381 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2383 p = ssl_handshake_start(s);
2386 #ifndef OPENSSL_NO_PSK
2387 if (alg_k & SSL_PSK) {
2390 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2391 * \0-terminated identity. The last byte is for us for simulating
2394 char identity[PSK_MAX_IDENTITY_LEN + 1];
2396 unsigned char psk[PSK_MAX_PSK_LEN];
2399 if (s->psk_client_callback == NULL) {
2400 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2401 SSL_R_PSK_NO_CLIENT_CB);
2405 memset(identity, 0, sizeof(identity));
2407 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2408 identity, sizeof(identity) - 1,
2411 if (psklen > PSK_MAX_PSK_LEN) {
2412 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2413 ERR_R_INTERNAL_ERROR);
2415 } else if (psklen == 0) {
2416 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2417 SSL_R_PSK_IDENTITY_NOT_FOUND);
2421 OPENSSL_free(s->s3->tmp.psk);
2422 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2423 OPENSSL_cleanse(psk, psklen);
2425 if (s->s3->tmp.psk == NULL) {
2426 OPENSSL_cleanse(identity, sizeof(identity));
2430 s->s3->tmp.psklen = psklen;
2432 identitylen = strlen(identity);
2433 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2434 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2435 ERR_R_INTERNAL_ERROR);
2438 OPENSSL_free(s->session->psk_identity);
2439 s->session->psk_identity = BUF_strdup(identity);
2440 if (s->session->psk_identity == NULL) {
2441 OPENSSL_cleanse(identity, sizeof(identity));
2445 s2n(identitylen, p);
2446 memcpy(p, identity, identitylen);
2447 pskhdrlen = 2 + identitylen;
2451 OPENSSL_cleanse(identity, sizeof(identity));
2453 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2457 if (alg_k & SSL_kPSK) {
2462 /* Fool emacs indentation */
2465 #ifndef OPENSSL_NO_RSA
2466 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2468 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2469 pms = OPENSSL_malloc(pmslen);
2473 if (s->session->peer == NULL) {
2475 * We should always have a server certificate with SSL_kRSA.
2477 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2478 ERR_R_INTERNAL_ERROR);
2482 if (s->s3->peer_rsa_tmp != NULL)
2483 rsa = s->s3->peer_rsa_tmp;
2485 pkey = X509_get_pubkey(s->session->peer);
2486 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2487 || (pkey->pkey.rsa == NULL)) {
2488 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2489 ERR_R_INTERNAL_ERROR);
2490 EVP_PKEY_free(pkey);
2493 rsa = pkey->pkey.rsa;
2494 EVP_PKEY_free(pkey);
2497 pms[0] = s->client_version >> 8;
2498 pms[1] = s->client_version & 0xff;
2499 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2503 /* Fix buf for TLS and beyond */
2504 if (s->version > SSL3_VERSION)
2506 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2508 if (s->options & SSL_OP_PKCS1_CHECK_1)
2510 if (s->options & SSL_OP_PKCS1_CHECK_2)
2514 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2515 SSL_R_BAD_RSA_ENCRYPT);
2519 /* Fix buf for TLS and beyond */
2520 if (s->version > SSL3_VERSION) {
2526 #ifndef OPENSSL_NO_DH
2527 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2528 DH *dh_srvr, *dh_clnt;
2529 if (s->s3->peer_dh_tmp != NULL)
2530 dh_srvr = s->s3->peer_dh_tmp;
2532 /* we get them from the cert */
2533 EVP_PKEY *spkey = NULL;
2535 spkey = X509_get_pubkey(s->session->peer);
2537 dh_srvr = EVP_PKEY_get1_DH(spkey);
2538 EVP_PKEY_free(spkey);
2540 if (dh_srvr == NULL) {
2541 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2542 ERR_R_INTERNAL_ERROR);
2546 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2547 /* Use client certificate key */
2548 EVP_PKEY *clkey = s->cert->key->privatekey;
2551 dh_clnt = EVP_PKEY_get1_DH(clkey);
2552 if (dh_clnt == NULL) {
2553 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2554 ERR_R_INTERNAL_ERROR);
2558 /* generate a new random key */
2559 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2560 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2563 if (!DH_generate_key(dh_clnt)) {
2564 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2570 pmslen = DH_size(dh_clnt);
2571 pms = OPENSSL_malloc(pmslen);
2576 * use the 'p' output buffer for the DH key, but make sure to
2577 * clear it out afterwards
2580 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2581 if (s->s3->peer_dh_tmp == NULL)
2585 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2591 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2594 /* send off the data */
2595 n = BN_num_bytes(dh_clnt->pub_key);
2597 BN_bn2bin(dh_clnt->pub_key, p);
2605 #ifndef OPENSSL_NO_EC
2606 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2607 const EC_GROUP *srvr_group = NULL;
2609 int ecdh_clnt_cert = 0;
2612 * Did we send out the client's ECDH share for use in premaster
2613 * computation as part of client certificate? If so, set
2614 * ecdh_clnt_cert to 1.
2616 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2618 * XXX: For now, we do not support client
2619 * authentication using ECDH certificates.
2620 * To add such support, one needs to add
2621 * code that checks for appropriate
2622 * conditions and sets ecdh_clnt_cert to 1.
2623 * For example, the cert have an ECC
2624 * key on the same curve as the server's
2625 * and the key should be authorized for
2628 * One also needs to add code in ssl3_connect
2629 * to skip sending the certificate verify
2632 * if ((s->cert->key->privatekey != NULL) &&
2633 * (s->cert->key->privatekey->type ==
2634 * EVP_PKEY_EC) && ...)
2635 * ecdh_clnt_cert = 1;
2639 if (s->s3->peer_ecdh_tmp != NULL) {
2640 tkey = s->s3->peer_ecdh_tmp;
2642 /* Get the Server Public Key from Cert */
2643 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2644 if ((srvr_pub_pkey == NULL)
2645 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2646 || (srvr_pub_pkey->pkey.ec == NULL)) {
2647 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2648 ERR_R_INTERNAL_ERROR);
2652 tkey = srvr_pub_pkey->pkey.ec;
2655 srvr_group = EC_KEY_get0_group(tkey);
2656 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2658 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2659 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2660 ERR_R_INTERNAL_ERROR);
2664 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2665 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2666 ERR_R_MALLOC_FAILURE);
2670 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2671 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2674 if (ecdh_clnt_cert) {
2676 * Reuse key info from our certificate We only need our
2677 * private key to perform the ECDH computation.
2679 const BIGNUM *priv_key;
2680 tkey = s->cert->key->privatekey->pkey.ec;
2681 priv_key = EC_KEY_get0_private_key(tkey);
2682 if (priv_key == NULL) {
2683 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2684 ERR_R_MALLOC_FAILURE);
2687 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2688 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2692 /* Generate a new ECDH key pair */
2693 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2694 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2701 * use the 'p' output buffer for the ECDH key, but make sure to
2702 * clear it out afterwards
2705 field_size = EC_GROUP_get_degree(srvr_group);
2706 if (field_size <= 0) {
2707 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2710 pmslen = (field_size + 7) / 8;
2711 pms = OPENSSL_malloc(pmslen);
2714 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2715 if (n <= 0 || pmslen != (size_t)n) {
2716 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2720 if (ecdh_clnt_cert) {
2721 /* Send empty client key exch message */
2725 * First check the size of encoding and allocate memory
2729 EC_POINT_point2oct(srvr_group,
2730 EC_KEY_get0_public_key(clnt_ecdh),
2731 POINT_CONVERSION_UNCOMPRESSED,
2734 encodedPoint = (unsigned char *)
2735 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2736 bn_ctx = BN_CTX_new();
2737 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2738 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2739 ERR_R_MALLOC_FAILURE);
2743 /* Encode the public key */
2744 n = EC_POINT_point2oct(srvr_group,
2745 EC_KEY_get0_public_key(clnt_ecdh),
2746 POINT_CONVERSION_UNCOMPRESSED,
2747 encodedPoint, encoded_pt_len, bn_ctx);
2749 *p = n; /* length of encoded point */
2750 /* Encoded point will be copied here */
2752 /* copy the point */
2753 memcpy(p, encodedPoint, n);
2754 /* increment n to account for length field */
2758 /* Free allocated memory */
2759 BN_CTX_free(bn_ctx);
2760 OPENSSL_free(encodedPoint);
2761 EC_KEY_free(clnt_ecdh);
2762 EVP_PKEY_free(srvr_pub_pkey);
2764 #endif /* !OPENSSL_NO_EC */
2765 else if (alg_k & SSL_kGOST) {
2766 /* GOST key exchange message creation */
2767 EVP_PKEY_CTX *pkey_ctx;
2770 unsigned int md_len;
2771 unsigned char shared_ukm[32], tmp[256];
2772 EVP_MD_CTX *ukm_hash;
2776 pms = OPENSSL_malloc(pmslen);
2781 * Get server sertificate PKEY and create ctx from it
2783 peer_cert = s->session->peer;
2785 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2786 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2790 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2791 X509_get_pubkey(peer_cert), NULL);
2792 if (pkey_ctx == NULL) {
2793 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2794 ERR_R_MALLOC_FAILURE);
2798 * If we have send a certificate, and certificate key
2799 * parameters match those of server certificate, use
2800 * certificate key for key exchange
2803 /* Otherwise, generate ephemeral key pair */
2805 if (pkey_ctx == NULL
2806 || EVP_PKEY_encrypt_init(pkey_ctx) <= 0
2807 /* Generate session key */
2808 || RAND_bytes(pms, pmslen) <= 0) {
2809 EVP_PKEY_CTX_free(pkey_ctx);
2810 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2811 ERR_R_INTERNAL_ERROR);
2815 * If we have client certificate, use its secret as peer key
2817 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2818 if (EVP_PKEY_derive_set_peer
2819 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2821 * If there was an error - just ignore it. Ephemeral key
2828 * Compute shared IV and store it in algorithm-specific context
2831 ukm_hash = EVP_MD_CTX_create();
2832 if (EVP_DigestInit(ukm_hash,
2833 EVP_get_digestbynid(NID_id_GostR3411_94)) <= 0
2834 || EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2835 SSL3_RANDOM_SIZE) <= 0
2836 || EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2837 SSL3_RANDOM_SIZE) <= 0
2838 || EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len) <= 0) {
2839 EVP_MD_CTX_destroy(ukm_hash);
2840 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2841 ERR_R_INTERNAL_ERROR);
2844 EVP_MD_CTX_destroy(ukm_hash);
2845 if (EVP_PKEY_CTX_ctrl
2846 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2848 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2852 /* Make GOST keytransport blob message */
2854 * Encapsulate it into sequence
2856 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2858 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) <= 0) {
2859 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2863 if (msglen >= 0x80) {
2865 *(p++) = msglen & 0xff;
2868 *(p++) = msglen & 0xff;
2871 memcpy(p, tmp, msglen);
2872 /* Check if pubkey from client certificate was used */
2873 if (EVP_PKEY_CTX_ctrl
2874 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2875 /* Set flag "skip certificate verify" */
2876 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2878 EVP_PKEY_CTX_free(pkey_ctx);
2879 EVP_PKEY_free(pub_key);
2882 #ifndef OPENSSL_NO_SRP
2883 else if (alg_k & SSL_kSRP) {
2884 if (s->srp_ctx.A != NULL) {
2885 /* send off the data */
2886 n = BN_num_bytes(s->srp_ctx.A);
2888 BN_bn2bin(s->srp_ctx.A, p);
2891 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2892 ERR_R_INTERNAL_ERROR);
2895 OPENSSL_free(s->session->srp_username);
2896 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2897 if (s->session->srp_username == NULL) {
2898 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE,
2899 ERR_R_MALLOC_FAILURE);
2905 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2906 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2910 #ifndef OPENSSL_NO_PSK
2914 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2915 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2916 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2920 s->s3->tmp.pms = pms;
2921 s->s3->tmp.pmslen = pmslen;
2925 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2926 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2928 OPENSSL_clear_free(pms, pmslen);
2929 s->s3->tmp.pms = NULL;
2930 #ifndef OPENSSL_NO_EC
2931 BN_CTX_free(bn_ctx);
2932 OPENSSL_free(encodedPoint);
2933 EC_KEY_free(clnt_ecdh);
2934 EVP_PKEY_free(srvr_pub_pkey);
2936 #ifndef OPENSSL_NO_PSK
2937 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2938 s->s3->tmp.psk = NULL;
2940 ossl_statem_set_error(s);
2944 int tls_client_key_exchange_post_work(SSL *s)
2946 unsigned char *pms = NULL;
2949 #ifndef OPENSSL_NO_SRP
2951 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
2952 if (!srp_generate_client_master_secret(s)) {
2953 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK,
2954 ERR_R_INTERNAL_ERROR);
2960 pms = s->s3->tmp.pms;
2961 pmslen = s->s3->tmp.pmslen;
2963 if (pms == NULL && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2964 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2965 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_MALLOC_FAILURE);
2968 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2969 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2970 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK, ERR_R_INTERNAL_ERROR);
2974 #ifndef OPENSSL_NO_SCTP
2975 if (SSL_IS_DTLS(s)) {
2976 unsigned char sctpauthkey[64];
2977 char labelbuffer[sizeof(DTLS1_SCTP_AUTH_LABEL)];
2980 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2983 memcpy(labelbuffer, DTLS1_SCTP_AUTH_LABEL,
2984 sizeof(DTLS1_SCTP_AUTH_LABEL));
2986 if (SSL_export_keying_material(s, sctpauthkey,
2987 sizeof(sctpauthkey), labelbuffer,
2988 sizeof(labelbuffer), NULL, 0, 0) <= 0)
2991 BIO_ctrl(SSL_get_wbio(s), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY,
2992 sizeof(sctpauthkey), sctpauthkey);
2998 OPENSSL_clear_free(pms, pmslen);
2999 s->s3->tmp.pms = NULL;
3003 int tls_construct_client_verify(SSL *s)
3006 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3008 EVP_PKEY_CTX *pctx = NULL;
3014 EVP_MD_CTX_init(&mctx);
3016 p = ssl_handshake_start(s);
3017 pkey = s->cert->key->privatekey;
3018 /* Create context from key and test if sha1 is allowed as digest */
3019 pctx = EVP_PKEY_CTX_new(pkey, NULL);
3021 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_MALLOC_FAILURE);
3024 if (EVP_PKEY_sign_init(pctx) <= 0) {
3025 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3028 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3029 if (!SSL_USE_SIGALGS(s))
3030 s->method->ssl3_enc->cert_verify_mac(s,
3033 [MD5_DIGEST_LENGTH]));
3038 * For TLS v1.2 send signature algorithm and signature using agreed
3039 * digest and cached handshake records.
3041 if (SSL_USE_SIGALGS(s)) {
3044 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3045 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3046 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3047 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3052 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3055 if (!EVP_SignInit_ex(&mctx, md, NULL)
3056 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3057 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3058 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_EVP_LIB);
3063 /* Digest cached records and discard handshake buffer */
3064 if (!ssl3_digest_cached_records(s, 0))
3067 #ifndef OPENSSL_NO_RSA
3068 if (pkey->type == EVP_PKEY_RSA) {
3069 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3070 if (RSA_sign(NID_md5_sha1, data,
3071 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3072 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3073 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_RSA_LIB);
3080 #ifndef OPENSSL_NO_DSA
3081 if (pkey->type == EVP_PKEY_DSA) {
3082 if (!DSA_sign(pkey->save_type,
3083 &(data[MD5_DIGEST_LENGTH]),
3084 SHA_DIGEST_LENGTH, &(p[2]),
3085 (unsigned int *)&j, pkey->pkey.dsa)) {
3086 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_DSA_LIB);
3093 #ifndef OPENSSL_NO_EC
3094 if (pkey->type == EVP_PKEY_EC) {
3095 if (!ECDSA_sign(pkey->save_type,
3096 &(data[MD5_DIGEST_LENGTH]),
3097 SHA_DIGEST_LENGTH, &(p[2]),
3098 (unsigned int *)&j, pkey->pkey.ec)) {
3099 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3106 if (pkey->type == NID_id_GostR3410_2001) {
3107 unsigned char signbuf[64];
3109 size_t sigsize = 64;
3110 s->method->ssl3_enc->cert_verify_mac(s,
3111 NID_id_GostR3411_94, data);
3112 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3113 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3116 for (i = 63, j = 0; i >= 0; j++, i--) {
3117 p[2 + j] = signbuf[i];
3122 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3125 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3126 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3130 EVP_MD_CTX_cleanup(&mctx);
3131 EVP_PKEY_CTX_free(pctx);
3134 EVP_MD_CTX_cleanup(&mctx);
3135 EVP_PKEY_CTX_free(pctx);
3140 * Check a certificate can be used for client authentication. Currently check
3141 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3142 * certificates can be used and optionally checks suitability for Suite B.
3144 static int ssl3_check_client_certificate(SSL *s)
3146 unsigned long alg_k;
3147 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3149 /* If no suitable signature algorithm can't use certificate */
3150 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3153 * If strict mode check suitability of chain before using it. This also
3154 * adjusts suite B digest if necessary.
3156 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3157 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3159 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3160 /* See if we can use client certificate for fixed DH */
3161 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3162 int i = s->session->peer_type;
3163 EVP_PKEY *clkey = NULL, *spkey = NULL;
3164 clkey = s->cert->key->privatekey;
3165 /* If client key not DH assume it can be used */
3166 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3169 spkey = X509_get_pubkey(s->session->peer);
3171 /* Compare server and client parameters */
3172 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3173 EVP_PKEY_free(spkey);
3177 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3182 WORK_STATE tls_prepare_client_certificate(SSL *s, WORK_STATE wst)
3185 EVP_PKEY *pkey = NULL;
3188 if (wst == WORK_MORE_A) {
3189 /* Let cert callback update client certificates if required */
3190 if (s->cert->cert_cb) {
3191 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3193 s->rwstate = SSL_X509_LOOKUP;
3197 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3198 ossl_statem_set_error(s);
3201 s->rwstate = SSL_NOTHING;
3203 if (ssl3_check_client_certificate(s))
3204 return WORK_FINISHED_CONTINUE;
3206 /* Fall through to WORK_MORE_B */
3210 /* We need to get a client cert */
3211 if (wst == WORK_MORE_B) {
3213 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3214 * return(-1); We then get retied later
3216 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3218 s->rwstate = SSL_X509_LOOKUP;
3221 s->rwstate = SSL_NOTHING;
3222 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3223 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3225 } else if (i == 1) {
3227 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE,
3228 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3232 EVP_PKEY_free(pkey);
3233 if (i && !ssl3_check_client_certificate(s))
3236 if (s->version == SSL3_VERSION) {
3237 s->s3->tmp.cert_req = 0;
3238 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3239 return WORK_FINISHED_CONTINUE;
3241 s->s3->tmp.cert_req = 2;
3242 if (!ssl3_digest_cached_records(s, 0)) {
3243 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3244 ossl_statem_set_error(s);
3250 return WORK_FINISHED_CONTINUE;
3253 /* Shouldn't ever get here */
3257 int tls_construct_client_certificate(SSL *s)
3259 if (!ssl3_output_cert_chain(s,
3260 (s->s3->tmp.cert_req ==
3261 2) ? NULL : s->cert->key)) {
3262 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3263 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3264 ossl_statem_set_error(s);
3271 #define has_bits(i,m) (((i)&(m)) == (m))
3273 int ssl3_check_cert_and_algorithm(SSL *s)
3277 EVP_PKEY *pkey = NULL;
3279 #ifndef OPENSSL_NO_RSA
3282 #ifndef OPENSSL_NO_DH
3285 int al = SSL_AD_HANDSHAKE_FAILURE;
3287 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3288 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3290 /* we don't have a certificate */
3291 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3293 #ifndef OPENSSL_NO_RSA
3294 rsa = s->s3->peer_rsa_tmp;
3296 #ifndef OPENSSL_NO_DH
3297 dh = s->s3->peer_dh_tmp;
3300 /* This is the passed certificate */
3302 idx = s->session->peer_type;
3303 #ifndef OPENSSL_NO_EC
3304 if (idx == SSL_PKEY_ECC) {
3305 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3307 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3312 } else if (alg_a & SSL_aECDSA) {
3313 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3314 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3316 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3317 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3321 pkey = X509_get_pubkey(s->session->peer);
3322 pkey_bits = EVP_PKEY_bits(pkey);
3323 i = X509_certificate_type(s->session->peer, pkey);
3324 EVP_PKEY_free(pkey);
3326 /* Check that we have a certificate if we require one */
3327 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3328 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3329 SSL_R_MISSING_RSA_SIGNING_CERT);
3332 #ifndef OPENSSL_NO_DSA
3333 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3334 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3335 SSL_R_MISSING_DSA_SIGNING_CERT);
3339 #ifndef OPENSSL_NO_RSA
3340 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3341 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3342 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3343 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3344 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3346 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3347 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3348 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3349 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3350 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3354 /* server key exchange is not allowed. */
3355 al = SSL_AD_INTERNAL_ERROR;
3356 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3363 #ifndef OPENSSL_NO_DH
3364 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3365 al = SSL_AD_INTERNAL_ERROR;
3366 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3368 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3369 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3370 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3371 SSL_R_MISSING_DH_RSA_CERT);
3374 # ifndef OPENSSL_NO_DSA
3375 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3376 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3377 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3378 SSL_R_MISSING_DH_DSA_CERT);
3384 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3385 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3386 #ifndef OPENSSL_NO_RSA
3387 if (alg_k & SSL_kRSA) {
3389 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3390 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3392 } else if (RSA_bits(rsa) >
3393 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3394 /* We have a temporary RSA key but it's too large. */
3395 al = SSL_AD_EXPORT_RESTRICTION;
3396 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3397 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3402 #ifndef OPENSSL_NO_DH
3403 if (alg_k & SSL_kDHE) {
3405 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3406 /* We have a temporary DH key but it's too large. */
3407 al = SSL_AD_EXPORT_RESTRICTION;
3408 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3409 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3412 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3413 /* The cert should have had an export DH key. */
3414 al = SSL_AD_EXPORT_RESTRICTION;
3415 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3416 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3421 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3422 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3428 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3432 #ifndef OPENSSL_NO_NEXTPROTONEG
3433 int tls_construct_next_proto(SSL *s)
3435 unsigned int len, padding_len;
3438 len = s->next_proto_negotiated_len;
3439 padding_len = 32 - ((len + 2) % 32);
3440 d = (unsigned char *)s->init_buf->data;
3442 memcpy(d + 5, s->next_proto_negotiated, len);
3443 d[5 + len] = padding_len;
3444 memset(d + 6 + len, 0, padding_len);
3445 *(d++) = SSL3_MT_NEXT_PROTO;
3446 l2n3(2 + len + padding_len, d);
3447 s->init_num = 4 + 2 + len + padding_len;
3454 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3457 #ifndef OPENSSL_NO_ENGINE
3458 if (s->ctx->client_cert_engine) {
3459 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3460 SSL_get_client_CA_list(s),
3461 px509, ppkey, NULL, NULL, NULL);
3466 if (s->ctx->client_cert_cb)
3467 i = s->ctx->client_cert_cb(s, px509, ppkey);
3471 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3477 int empty_reneg_info_scsv = !s->renegotiate;
3478 /* Set disabled masks for this session */
3479 ssl_set_client_disabled(s);
3485 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3486 c = sk_SSL_CIPHER_value(sk, i);
3487 /* Skip disabled ciphers */
3488 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3490 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3491 if (c->id == SSL3_CK_SCSV) {
3492 if (!empty_reneg_info_scsv)
3495 empty_reneg_info_scsv = 0;
3498 j = s->method->put_cipher_by_char(c, p);
3502 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3506 if (empty_reneg_info_scsv) {
3507 static SSL_CIPHER scsv = {
3508 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3510 j = s->method->put_cipher_by_char(&scsv, p);
3512 #ifdef OPENSSL_RI_DEBUG
3514 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3517 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3518 static SSL_CIPHER scsv = {
3519 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3521 j = s->method->put_cipher_by_char(&scsv, p);