1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
4 * This package is an SSL implementation written
5 * by Eric Young (eay@cryptsoft.com).
6 * The implementation was written so as to conform with Netscapes SSL.
8 * This library is free for commercial and non-commercial use as long as
9 * the following conditions are aheared to. The following conditions
10 * apply to all code found in this distribution, be it the RC4, RSA,
11 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
12 * included with this distribution is covered by the same copyright terms
13 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 * Copyright remains Eric Young's, and as such any Copyright notices in
16 * the code are not to be removed.
17 * If this package is used in a product, Eric Young should be given attribution
18 * as the author of the parts of the library used.
19 * This can be in the form of a textual message at program startup or
20 * in documentation (online or textual) provided with the package.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * "This product includes cryptographic software written by
33 * Eric Young (eay@cryptsoft.com)"
34 * The word 'cryptographic' can be left out if the rouines from the library
35 * being used are not cryptographic related :-).
36 * 4. If you include any Windows specific code (or a derivative thereof) from
37 * the apps directory (application code) you must include an acknowledgement:
38 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52 * The licence and distribution terms for any publically available version or
53 * derivative of this code cannot be changed. i.e. this code cannot simply be
54 * copied and put under another distribution licence
55 * [including the GNU Public Licence.]
57 /* ====================================================================
58 * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in
69 * the documentation and/or other materials provided with the
72 * 3. All advertising materials mentioning features or use of this
73 * software must display the following acknowledgment:
74 * "This product includes software developed by the OpenSSL Project
75 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78 * endorse or promote products derived from this software without
79 * prior written permission. For written permission, please contact
80 * openssl-core@openssl.org.
82 * 5. Products derived from this software may not be called "OpenSSL"
83 * nor may "OpenSSL" appear in their names without prior written
84 * permission of the OpenSSL Project.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
95 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102 * OF THE POSSIBILITY OF SUCH DAMAGE.
103 * ====================================================================
105 * This product includes cryptographic software written by Eric Young
106 * (eay@cryptsoft.com). This product includes software written by Tim
107 * Hudson (tjh@cryptsoft.com).
110 /* ====================================================================
111 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113 * Portions of the attached software ("Contribution") are developed by
114 * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116 * The Contribution is licensed pursuant to the OpenSSL open source
117 * license provided above.
119 * ECC cipher suite support in OpenSSL originally written by
120 * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
123 /* ====================================================================
124 * Copyright 2005 Nokia. All rights reserved.
126 * The portions of the attached software ("Contribution") is developed by
127 * Nokia Corporation and is licensed pursuant to the OpenSSL open source
130 * The Contribution, originally written by Mika Kousa and Pasi Eronen of
131 * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
132 * support (see RFC 4279) to OpenSSL.
134 * No patent licenses or other rights except those expressly stated in
135 * the OpenSSL open source license shall be deemed granted or received
136 * expressly, by implication, estoppel, or otherwise.
138 * No assurances are provided by Nokia that the Contribution does not
139 * infringe the patent or other intellectual property rights of any third
140 * party or that the license provides you with all the necessary rights
141 * to make use of the Contribution.
143 * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
144 * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
145 * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
146 * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
151 #include "../ssl_locl.h"
152 #include "statem_locl.h"
153 #include <openssl/buffer.h>
154 #include <openssl/rand.h>
155 #include <openssl/objects.h>
156 #include <openssl/evp.h>
157 #include <openssl/md5.h>
158 #ifndef OPENSSL_NO_DH
159 # include <openssl/dh.h>
161 #include <openssl/bn.h>
162 #ifndef OPENSSL_NO_ENGINE
163 # include <openssl/engine.h>
166 static ossl_inline
int cert_req_allowed(SSL
*s
);
167 static int key_exchange_expected(SSL
*s
);
168 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
);
169 static int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
,
174 * Is a CertificateRequest message allowed at the moment or not?
180 static ossl_inline
int cert_req_allowed(SSL
*s
)
182 /* TLS does not like anon-DH with client cert */
183 if ((s
->version
> SSL3_VERSION
184 && (s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aNULL
))
185 || (s
->s3
->tmp
.new_cipher
->algorithm_auth
& (SSL_aSRP
| SSL_aPSK
)))
192 * Should we expect the ServerKeyExchange message or not?
199 static int key_exchange_expected(SSL
*s
)
201 long alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
204 * Can't skip server key exchange if this is an ephemeral
205 * ciphersuite or for SRP
207 if (alg_k
& (SSL_kDHE
| SSL_kECDHE
| SSL_kDHEPSK
| SSL_kECDHEPSK
216 * ossl_statem_client_read_transition() encapsulates the logic for the allowed
217 * handshake state transitions when the client is reading messages from the
218 * server. The message type that the server has sent is provided in |mt|. The
219 * current state is in |s->statem.hand_state|.
222 * 1: Success (transition allowed)
223 * 0: Error (transition not allowed)
225 int ossl_statem_client_read_transition(SSL
*s
, int mt
)
227 OSSL_STATEM
*st
= &s
->statem
;
230 switch(st
->hand_state
) {
231 case TLS_ST_CW_CLNT_HELLO
:
232 if (mt
== SSL3_MT_SERVER_HELLO
) {
233 st
->hand_state
= TLS_ST_CR_SRVR_HELLO
;
237 if (SSL_IS_DTLS(s
)) {
238 if (mt
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
239 st
->hand_state
= DTLS_ST_CR_HELLO_VERIFY_REQUEST
;
245 case TLS_ST_CR_SRVR_HELLO
:
247 if (s
->tlsext_ticket_expected
) {
248 if (mt
== SSL3_MT_NEWSESSION_TICKET
) {
249 st
->hand_state
= TLS_ST_CR_SESSION_TICKET
;
252 } else if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
253 st
->hand_state
= TLS_ST_CR_CHANGE
;
257 if (SSL_IS_DTLS(s
) && mt
== DTLS1_MT_HELLO_VERIFY_REQUEST
) {
258 st
->hand_state
= DTLS_ST_CR_HELLO_VERIFY_REQUEST
;
260 } else if (s
->version
>= TLS1_VERSION
261 && s
->tls_session_secret_cb
!= NULL
262 && s
->session
->tlsext_tick
!= NULL
263 && mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
265 * Normally, we can tell if the server is resuming the session
266 * from the session ID. EAP-FAST (RFC 4851), however, relies on
267 * the next server message after the ServerHello to determine if
268 * the server is resuming.
271 st
->hand_state
= TLS_ST_CR_CHANGE
;
273 } else if (!(s
->s3
->tmp
.new_cipher
->algorithm_auth
274 & (SSL_aNULL
| SSL_aSRP
| SSL_aPSK
))) {
275 if (mt
== SSL3_MT_CERTIFICATE
) {
276 st
->hand_state
= TLS_ST_CR_CERT
;
280 ske_expected
= key_exchange_expected(s
);
281 if (ske_expected
< 0)
283 /* SKE is optional for some PSK ciphersuites */
285 || ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)
286 && mt
== SSL3_MT_SERVER_KEY_EXCHANGE
)) {
287 if (mt
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
288 st
->hand_state
= TLS_ST_CR_KEY_EXCH
;
291 } else if (mt
== SSL3_MT_CERTIFICATE_REQUEST
292 && cert_req_allowed(s
)) {
293 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
295 } else if (mt
== SSL3_MT_SERVER_DONE
) {
296 st
->hand_state
= TLS_ST_CR_SRVR_DONE
;
305 * The CertificateStatus message is optional even if
306 * |tlsext_status_expected| is set
308 if (s
->tlsext_status_expected
&& mt
== SSL3_MT_CERTIFICATE_STATUS
) {
309 st
->hand_state
= TLS_ST_CR_CERT_STATUS
;
314 case TLS_ST_CR_CERT_STATUS
:
315 ske_expected
= key_exchange_expected(s
);
316 if (ske_expected
< 0)
318 /* SKE is optional for some PSK ciphersuites */
320 || ((s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_PSK
)
321 && mt
== SSL3_MT_SERVER_KEY_EXCHANGE
)) {
322 if (mt
== SSL3_MT_SERVER_KEY_EXCHANGE
) {
323 st
->hand_state
= TLS_ST_CR_KEY_EXCH
;
330 case TLS_ST_CR_KEY_EXCH
:
331 if (mt
== SSL3_MT_CERTIFICATE_REQUEST
) {
332 if (cert_req_allowed(s
)) {
333 st
->hand_state
= TLS_ST_CR_CERT_REQ
;
340 case TLS_ST_CR_CERT_REQ
:
341 if (mt
== SSL3_MT_SERVER_DONE
) {
342 st
->hand_state
= TLS_ST_CR_SRVR_DONE
;
347 case TLS_ST_CW_FINISHED
:
348 if (mt
== SSL3_MT_NEWSESSION_TICKET
&& s
->tlsext_ticket_expected
) {
349 st
->hand_state
= TLS_ST_CR_SESSION_TICKET
;
351 } else if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
352 st
->hand_state
= TLS_ST_CR_CHANGE
;
357 case TLS_ST_CR_SESSION_TICKET
:
358 if (mt
== SSL3_MT_CHANGE_CIPHER_SPEC
) {
359 st
->hand_state
= TLS_ST_CR_CHANGE
;
364 case TLS_ST_CR_CHANGE
:
365 if (mt
== SSL3_MT_FINISHED
) {
366 st
->hand_state
= TLS_ST_CR_FINISHED
;
375 /* No valid transition found */
380 * client_write_transition() works out what handshake state to move to next
381 * when the client is writing messages to be sent to the server.
383 WRITE_TRAN
ossl_statem_client_write_transition(SSL
*s
)
385 OSSL_STATEM
*st
= &s
->statem
;
387 switch(st
->hand_state
) {
389 /* Renegotiation - fall through */
391 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
392 return WRITE_TRAN_CONTINUE
;
394 case TLS_ST_CW_CLNT_HELLO
:
396 * No transition at the end of writing because we don't know what
399 return WRITE_TRAN_FINISHED
;
401 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
402 st
->hand_state
= TLS_ST_CW_CLNT_HELLO
;
403 return WRITE_TRAN_CONTINUE
;
405 case TLS_ST_CR_SRVR_DONE
:
406 if (s
->s3
->tmp
.cert_req
)
407 st
->hand_state
= TLS_ST_CW_CERT
;
409 st
->hand_state
= TLS_ST_CW_KEY_EXCH
;
410 return WRITE_TRAN_CONTINUE
;
413 st
->hand_state
= TLS_ST_CW_KEY_EXCH
;
414 return WRITE_TRAN_CONTINUE
;
416 case TLS_ST_CW_KEY_EXCH
:
418 * For TLS, cert_req is set to 2, so a cert chain of nothing is
419 * sent, but no verify packet is sent
422 * XXX: For now, we do not support client authentication in ECDH
423 * cipher suites with ECDH (rather than ECDSA) certificates. We
424 * need to skip the certificate verify message when client's
425 * ECDH public key is sent inside the client certificate.
427 if (s
->s3
->tmp
.cert_req
== 1) {
428 st
->hand_state
= TLS_ST_CW_CERT_VRFY
;
430 st
->hand_state
= TLS_ST_CW_CHANGE
;
432 if (s
->s3
->flags
& TLS1_FLAGS_SKIP_CERT_VERIFY
) {
433 st
->hand_state
= TLS_ST_CW_CHANGE
;
435 return WRITE_TRAN_CONTINUE
;
437 case TLS_ST_CW_CERT_VRFY
:
438 st
->hand_state
= TLS_ST_CW_CHANGE
;
439 return WRITE_TRAN_CONTINUE
;
441 case TLS_ST_CW_CHANGE
:
442 #if defined(OPENSSL_NO_NEXTPROTONEG)
443 st
->hand_state
= TLS_ST_CW_FINISHED
;
445 if (!SSL_IS_DTLS(s
) && s
->s3
->next_proto_neg_seen
)
446 st
->hand_state
= TLS_ST_CW_NEXT_PROTO
;
448 st
->hand_state
= TLS_ST_CW_FINISHED
;
450 return WRITE_TRAN_CONTINUE
;
452 #if !defined(OPENSSL_NO_NEXTPROTONEG)
453 case TLS_ST_CW_NEXT_PROTO
:
454 st
->hand_state
= TLS_ST_CW_FINISHED
;
455 return WRITE_TRAN_CONTINUE
;
458 case TLS_ST_CW_FINISHED
:
460 st
->hand_state
= TLS_ST_OK
;
461 ossl_statem_set_in_init(s
, 0);
462 return WRITE_TRAN_CONTINUE
;
464 return WRITE_TRAN_FINISHED
;
467 case TLS_ST_CR_FINISHED
:
469 st
->hand_state
= TLS_ST_CW_CHANGE
;
470 return WRITE_TRAN_CONTINUE
;
472 st
->hand_state
= TLS_ST_OK
;
473 ossl_statem_set_in_init(s
, 0);
474 return WRITE_TRAN_CONTINUE
;
478 /* Shouldn't happen */
479 return WRITE_TRAN_ERROR
;
484 * Perform any pre work that needs to be done prior to sending a message from
485 * the client to the server.
487 WORK_STATE
ossl_statem_client_pre_work(SSL
*s
, WORK_STATE wst
)
489 OSSL_STATEM
*st
= &s
->statem
;
491 switch(st
->hand_state
) {
492 case TLS_ST_CW_CLNT_HELLO
:
494 if (SSL_IS_DTLS(s
)) {
495 /* every DTLS ClientHello resets Finished MAC */
496 ssl3_init_finished_mac(s
);
501 return tls_prepare_client_certificate(s
, wst
);
503 case TLS_ST_CW_CHANGE
:
504 if (SSL_IS_DTLS(s
)) {
507 * We're into the last flight so we don't retransmit these
508 * messages unless we need to.
512 #ifndef OPENSSL_NO_SCTP
513 if (BIO_dgram_is_sctp(SSL_get_wbio(s
)))
514 return dtls_wait_for_dry(s
);
517 return WORK_FINISHED_CONTINUE
;
520 return tls_finish_handshake(s
, wst
);
523 /* No pre work to be done */
527 return WORK_FINISHED_CONTINUE
;
531 * Perform any work that needs to be done after sending a message from the
532 * client to the server.
534 WORK_STATE
ossl_statem_client_post_work(SSL
*s
, WORK_STATE wst
)
536 OSSL_STATEM
*st
= &s
->statem
;
540 switch(st
->hand_state
) {
541 case TLS_ST_CW_CLNT_HELLO
:
542 if (SSL_IS_DTLS(s
) && s
->d1
->cookie_len
> 0 && statem_flush(s
) != 1)
544 #ifndef OPENSSL_NO_SCTP
545 /* Disable buffering for SCTP */
546 if (!SSL_IS_DTLS(s
) || !BIO_dgram_is_sctp(SSL_get_wbio(s
))) {
549 * turn on buffering for the next lot of output
551 if (s
->bbio
!= s
->wbio
)
552 s
->wbio
= BIO_push(s
->bbio
, s
->wbio
);
553 #ifndef OPENSSL_NO_SCTP
556 if (SSL_IS_DTLS(s
)) {
557 /* Treat the next message as the first packet */
562 case TLS_ST_CW_KEY_EXCH
:
563 if (tls_client_key_exchange_post_work(s
) == 0)
567 case TLS_ST_CW_CHANGE
:
568 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
569 #ifdef OPENSSL_NO_COMP
570 s
->session
->compress_meth
= 0;
572 if (s
->s3
->tmp
.new_compression
== NULL
)
573 s
->session
->compress_meth
= 0;
575 s
->session
->compress_meth
= s
->s3
->tmp
.new_compression
->id
;
577 if (!s
->method
->ssl3_enc
->setup_key_block(s
))
580 if (!s
->method
->ssl3_enc
->change_cipher_state(s
,
581 SSL3_CHANGE_CIPHER_CLIENT_WRITE
))
584 if (SSL_IS_DTLS(s
)) {
585 #ifndef OPENSSL_NO_SCTP
588 * Change to new shared key of SCTP-Auth, will be ignored if
591 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
596 dtls1_reset_seq_numbers(s
, SSL3_CC_WRITE
);
600 case TLS_ST_CW_FINISHED
:
601 #ifndef OPENSSL_NO_SCTP
602 if (wst
== WORK_MORE_A
&& SSL_IS_DTLS(s
) && s
->hit
== 0) {
604 * Change to new shared key of SCTP-Auth, will be ignored if
607 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_NEXT_AUTH_KEY
,
611 if (statem_flush(s
) != 1)
616 /* No post work to be done */
620 return WORK_FINISHED_CONTINUE
;
624 * Construct a message to be sent from the client to the server.
626 * Valid return values are:
630 int ossl_statem_client_construct_message(SSL
*s
)
632 OSSL_STATEM
*st
= &s
->statem
;
634 switch(st
->hand_state
) {
635 case TLS_ST_CW_CLNT_HELLO
:
636 return tls_construct_client_hello(s
);
639 return tls_construct_client_certificate(s
);
641 case TLS_ST_CW_KEY_EXCH
:
642 return tls_construct_client_key_exchange(s
);
644 case TLS_ST_CW_CERT_VRFY
:
645 return tls_construct_client_verify(s
);
647 case TLS_ST_CW_CHANGE
:
649 return dtls_construct_change_cipher_spec(s
);
651 return tls_construct_change_cipher_spec(s
);
653 #if !defined(OPENSSL_NO_NEXTPROTONEG)
654 case TLS_ST_CW_NEXT_PROTO
:
655 return tls_construct_next_proto(s
);
657 case TLS_ST_CW_FINISHED
:
658 return tls_construct_finished(s
,
660 ssl3_enc
->client_finished_label
,
662 ssl3_enc
->client_finished_label_len
);
665 /* Shouldn't happen */
673 * Returns the maximum allowed length for the current message that we are
674 * reading. Excludes the message header.
676 unsigned long ossl_statem_client_max_message_size(SSL
*s
)
678 OSSL_STATEM
*st
= &s
->statem
;
680 switch(st
->hand_state
) {
681 case TLS_ST_CR_SRVR_HELLO
:
682 return SERVER_HELLO_MAX_LENGTH
;
684 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
685 return HELLO_VERIFY_REQUEST_MAX_LENGTH
;
688 return s
->max_cert_list
;
690 case TLS_ST_CR_CERT_STATUS
:
691 return SSL3_RT_MAX_PLAIN_LENGTH
;
693 case TLS_ST_CR_KEY_EXCH
:
694 return SERVER_KEY_EXCH_MAX_LENGTH
;
696 case TLS_ST_CR_CERT_REQ
:
697 /* Set to s->max_cert_list for compatibility with previous releases.
698 * In practice these messages can get quite long if servers are
699 * configured to provide a long list of acceptable CAs
701 return s
->max_cert_list
;
703 case TLS_ST_CR_SRVR_DONE
:
704 return SERVER_HELLO_DONE_MAX_LENGTH
;
706 case TLS_ST_CR_CHANGE
:
707 return CCS_MAX_LENGTH
;
709 case TLS_ST_CR_SESSION_TICKET
:
710 return SSL3_RT_MAX_PLAIN_LENGTH
;
712 case TLS_ST_CR_FINISHED
:
713 return FINISHED_MAX_LENGTH
;
716 /* Shouldn't happen */
724 * Process a message that the client has been received from the server.
726 MSG_PROCESS_RETURN
ossl_statem_client_process_message(SSL
*s
, PACKET
*pkt
)
728 OSSL_STATEM
*st
= &s
->statem
;
730 switch(st
->hand_state
) {
731 case TLS_ST_CR_SRVR_HELLO
:
732 return tls_process_server_hello(s
, pkt
);
734 case DTLS_ST_CR_HELLO_VERIFY_REQUEST
:
735 return dtls_process_hello_verify(s
, pkt
);
738 return tls_process_server_certificate(s
, pkt
);
740 case TLS_ST_CR_CERT_STATUS
:
741 return tls_process_cert_status(s
, pkt
);
743 case TLS_ST_CR_KEY_EXCH
:
744 return tls_process_key_exchange(s
, pkt
);
746 case TLS_ST_CR_CERT_REQ
:
747 return tls_process_certificate_request(s
, pkt
);
749 case TLS_ST_CR_SRVR_DONE
:
750 return tls_process_server_done(s
, pkt
);
752 case TLS_ST_CR_CHANGE
:
753 return tls_process_change_cipher_spec(s
, pkt
);
755 case TLS_ST_CR_SESSION_TICKET
:
756 return tls_process_new_session_ticket(s
, pkt
);
758 case TLS_ST_CR_FINISHED
:
759 return tls_process_finished(s
, pkt
);
762 /* Shouldn't happen */
766 return MSG_PROCESS_ERROR
;
770 * Perform any further processing required following the receipt of a message
773 WORK_STATE
ossl_statem_client_post_process_message(SSL
*s
, WORK_STATE wst
)
775 OSSL_STATEM
*st
= &s
->statem
;
777 switch(st
->hand_state
) {
778 #ifndef OPENSSL_NO_SCTP
779 case TLS_ST_CR_SRVR_DONE
:
780 /* We only get here if we are using SCTP and we are renegotiating */
781 if (BIO_dgram_sctp_msg_waiting(SSL_get_rbio(s
))) {
782 s
->s3
->in_read_app_data
= 2;
783 s
->rwstate
= SSL_READING
;
784 BIO_clear_retry_flags(SSL_get_rbio(s
));
785 BIO_set_retry_read(SSL_get_rbio(s
));
786 ossl_statem_set_sctp_read_sock(s
, 1);
789 ossl_statem_set_sctp_read_sock(s
, 0);
790 return WORK_FINISHED_STOP
;
797 /* Shouldn't happen */
801 int tls_construct_client_hello(SSL
*s
)
804 unsigned char *p
, *d
;
809 #ifndef OPENSSL_NO_COMP
813 SSL_SESSION
*sess
= s
->session
;
815 buf
= (unsigned char *)s
->init_buf
->data
;
817 /* Work out what SSL/TLS/DTLS version to use */
818 protverr
= ssl_set_client_hello_version(s
);
820 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, protverr
);
824 if ((sess
== NULL
) || (sess
->ssl_version
!= s
->version
) ||
826 * In the case of EAP-FAST, we can have a pre-shared
827 * "ticket" without a session ID.
829 (!sess
->session_id_length
&& !sess
->tlsext_tick
) ||
830 (sess
->not_resumable
)) {
831 if (!ssl_get_new_session(s
, 0))
834 /* else use the pre-loaded session */
836 p
= s
->s3
->client_random
;
839 * for DTLS if client_random is initialized, reuse it, we are
840 * required to use same upon reply to HelloVerify
842 if (SSL_IS_DTLS(s
)) {
845 for (idx
= 0; idx
< sizeof(s
->s3
->client_random
); idx
++) {
854 if (i
&& ssl_fill_hello_random(s
, 0, p
,
855 sizeof(s
->s3
->client_random
)) <= 0)
858 /* Do the message type and length last */
859 d
= p
= ssl_handshake_start(s
);
862 * version indicates the negotiated version: for example from
863 * an SSLv2/v3 compatible client hello). The client_version
864 * field is the maximum version we permit and it is also
865 * used in RSA encrypted premaster secrets. Some servers can
866 * choke if we initially report a higher version then
867 * renegotiate to a lower one in the premaster secret. This
868 * didn't happen with TLS 1.0 as most servers supported it
869 * but it can with TLS 1.1 or later if the server only supports
872 * Possible scenario with previous logic:
873 * 1. Client hello indicates TLS 1.2
874 * 2. Server hello says TLS 1.0
875 * 3. RSA encrypted premaster secret uses 1.2.
876 * 4. Handhaked proceeds using TLS 1.0.
877 * 5. Server sends hello request to renegotiate.
878 * 6. Client hello indicates TLS v1.0 as we now
879 * know that is maximum server supports.
880 * 7. Server chokes on RSA encrypted premaster secret
881 * containing version 1.0.
883 * For interoperability it should be OK to always use the
884 * maximum version we support in client hello and then rely
885 * on the checking of version to ensure the servers isn't
886 * being inconsistent: for example initially negotiating with
887 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
888 * client_version in client hello and not resetting it to
889 * the negotiated version.
891 *(p
++) = s
->client_version
>> 8;
892 *(p
++) = s
->client_version
& 0xff;
895 memcpy(p
, s
->s3
->client_random
, SSL3_RANDOM_SIZE
);
896 p
+= SSL3_RANDOM_SIZE
;
902 i
= s
->session
->session_id_length
;
905 if (i
> (int)sizeof(s
->session
->session_id
)) {
906 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
909 memcpy(p
, s
->session
->session_id
, i
);
913 /* cookie stuff for DTLS */
914 if (SSL_IS_DTLS(s
)) {
915 if (s
->d1
->cookie_len
> sizeof(s
->d1
->cookie
)) {
916 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
919 *(p
++) = s
->d1
->cookie_len
;
920 memcpy(p
, s
->d1
->cookie
, s
->d1
->cookie_len
);
921 p
+= s
->d1
->cookie_len
;
924 /* Ciphers supported */
925 i
= ssl_cipher_list_to_bytes(s
, SSL_get_ciphers(s
), &(p
[2]));
927 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, SSL_R_NO_CIPHERS_AVAILABLE
);
930 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
932 * Some servers hang if client hello > 256 bytes as hack workaround
933 * chop number of supported ciphers to keep it well below this if we
936 if (TLS1_get_version(s
) >= TLS1_2_VERSION
937 && i
> OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
)
938 i
= OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
& ~1;
944 #ifdef OPENSSL_NO_COMP
948 if (!ssl_allow_compression(s
) || !s
->ctx
->comp_methods
)
951 j
= sk_SSL_COMP_num(s
->ctx
->comp_methods
);
953 for (i
= 0; i
< j
; i
++) {
954 comp
= sk_SSL_COMP_value(s
->ctx
->comp_methods
, i
);
958 *(p
++) = 0; /* Add the NULL method */
961 if (ssl_prepare_clienthello_tlsext(s
) <= 0) {
962 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, SSL_R_CLIENTHELLO_TLSEXT
);
966 ssl_add_clienthello_tlsext(s
, p
, buf
+ SSL3_RT_MAX_PLAIN_LENGTH
,
968 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
969 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
974 if (!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_HELLO
, l
)) {
975 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
976 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_HELLO
, ERR_R_INTERNAL_ERROR
);
982 ossl_statem_set_error(s
);
986 MSG_PROCESS_RETURN
dtls_process_hello_verify(SSL
*s
, PACKET
*pkt
)
989 unsigned int cookie_len
;
992 if (!PACKET_forward(pkt
, 2)
993 || !PACKET_get_length_prefixed_1(pkt
, &cookiepkt
)) {
994 al
= SSL_AD_DECODE_ERROR
;
995 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY
, SSL_R_LENGTH_MISMATCH
);
999 cookie_len
= PACKET_remaining(&cookiepkt
);
1000 if (cookie_len
> sizeof(s
->d1
->cookie
)) {
1001 al
= SSL_AD_ILLEGAL_PARAMETER
;
1002 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY
, SSL_R_LENGTH_TOO_LONG
);
1006 if (!PACKET_copy_bytes(&cookiepkt
, s
->d1
->cookie
, cookie_len
)) {
1007 al
= SSL_AD_DECODE_ERROR
;
1008 SSLerr(SSL_F_DTLS_PROCESS_HELLO_VERIFY
, SSL_R_LENGTH_MISMATCH
);
1011 s
->d1
->cookie_len
= cookie_len
;
1013 return MSG_PROCESS_FINISHED_READING
;
1015 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1016 ossl_statem_set_error(s
);
1017 return MSG_PROCESS_ERROR
;
1020 MSG_PROCESS_RETURN
tls_process_server_hello(SSL
*s
, PACKET
*pkt
)
1022 STACK_OF(SSL_CIPHER
) *sk
;
1023 const SSL_CIPHER
*c
;
1025 size_t session_id_len
;
1026 unsigned char *cipherchars
;
1027 int i
, al
= SSL_AD_INTERNAL_ERROR
;
1028 unsigned int compression
;
1029 unsigned int sversion
;
1031 #ifndef OPENSSL_NO_COMP
1035 if (!PACKET_get_net_2(pkt
, &sversion
)) {
1036 al
= SSL_AD_DECODE_ERROR
;
1037 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_LENGTH_MISMATCH
);
1041 protverr
= ssl_choose_client_version(s
, sversion
);
1042 if (protverr
!= 0) {
1043 al
= SSL_AD_PROTOCOL_VERSION
;
1044 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, protverr
);
1048 /* load the server hello data */
1049 /* load the server random */
1050 if (!PACKET_copy_bytes(pkt
, s
->s3
->server_random
, SSL3_RANDOM_SIZE
)) {
1051 al
= SSL_AD_DECODE_ERROR
;
1052 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_LENGTH_MISMATCH
);
1058 /* Get the session-id. */
1059 if (!PACKET_get_length_prefixed_1(pkt
, &session_id
)) {
1060 al
= SSL_AD_DECODE_ERROR
;
1061 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_LENGTH_MISMATCH
);
1064 session_id_len
= PACKET_remaining(&session_id
);
1065 if (session_id_len
> sizeof s
->session
->session_id
1066 || session_id_len
> SSL3_SESSION_ID_SIZE
) {
1067 al
= SSL_AD_ILLEGAL_PARAMETER
;
1068 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_SSL3_SESSION_ID_TOO_LONG
);
1072 if (!PACKET_get_bytes(pkt
, &cipherchars
, TLS_CIPHER_LEN
)) {
1073 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_LENGTH_MISMATCH
);
1074 al
= SSL_AD_DECODE_ERROR
;
1079 * Check if we can resume the session based on external pre-shared secret.
1080 * EAP-FAST (RFC 4851) supports two types of session resumption.
1081 * Resumption based on server-side state works with session IDs.
1082 * Resumption based on pre-shared Protected Access Credentials (PACs)
1083 * works by overriding the SessionTicket extension at the application
1084 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1085 * servers would honour the session ID.) Therefore, the session ID alone
1086 * is not a reliable indicator of session resumption, so we first check if
1087 * we can resume, and later peek at the next handshake message to see if the
1088 * server wants to resume.
1090 if (s
->version
>= TLS1_VERSION
&& s
->tls_session_secret_cb
&&
1091 s
->session
->tlsext_tick
) {
1092 const SSL_CIPHER
*pref_cipher
= NULL
;
1093 s
->session
->master_key_length
= sizeof(s
->session
->master_key
);
1094 if (s
->tls_session_secret_cb(s
, s
->session
->master_key
,
1095 &s
->session
->master_key_length
,
1097 s
->tls_session_secret_cb_arg
)) {
1098 s
->session
->cipher
= pref_cipher
?
1099 pref_cipher
: ssl_get_cipher_by_char(s
, cipherchars
);
1101 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, ERR_R_INTERNAL_ERROR
);
1102 al
= SSL_AD_INTERNAL_ERROR
;
1107 if (session_id_len
!= 0 && session_id_len
== s
->session
->session_id_length
1108 && memcmp(PACKET_data(&session_id
), s
->session
->session_id
,
1109 session_id_len
) == 0) {
1110 if (s
->sid_ctx_length
!= s
->session
->sid_ctx_length
1111 || memcmp(s
->session
->sid_ctx
, s
->sid_ctx
, s
->sid_ctx_length
)) {
1112 /* actually a client application bug */
1113 al
= SSL_AD_ILLEGAL_PARAMETER
;
1114 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
,
1115 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT
);
1121 * If we were trying for session-id reuse but the server
1122 * didn't echo the ID, make a new SSL_SESSION.
1123 * In the case of EAP-FAST and PAC, we do not send a session ID,
1124 * so the PAC-based session secret is always preserved. It'll be
1125 * overwritten if the server refuses resumption.
1127 if (s
->session
->session_id_length
> 0) {
1128 if (!ssl_get_new_session(s
, 0)) {
1133 s
->session
->session_id_length
= session_id_len
;
1134 /* session_id_len could be 0 */
1135 memcpy(s
->session
->session_id
, PACKET_data(&session_id
),
1139 c
= ssl_get_cipher_by_char(s
, cipherchars
);
1141 /* unknown cipher */
1142 al
= SSL_AD_ILLEGAL_PARAMETER
;
1143 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_UNKNOWN_CIPHER_RETURNED
);
1146 /* Set version disabled mask now we know version */
1147 if (!SSL_USE_TLS1_2_CIPHERS(s
))
1148 s
->s3
->tmp
.mask_ssl
= SSL_TLSV1_2
;
1150 s
->s3
->tmp
.mask_ssl
= 0;
1151 /* Skip TLS v1.0 ciphersuites if SSLv3 */
1152 if ((c
->algorithm_ssl
& SSL_TLSV1
) && s
->version
== SSL3_VERSION
)
1153 s
->s3
->tmp
.mask_ssl
|= SSL_TLSV1
;
1155 * If it is a disabled cipher we didn't send it in client hello, so
1158 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_CHECK
)) {
1159 al
= SSL_AD_ILLEGAL_PARAMETER
;
1160 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1164 sk
= ssl_get_ciphers_by_id(s
);
1165 i
= sk_SSL_CIPHER_find(sk
, c
);
1167 /* we did not say we would use this cipher */
1168 al
= SSL_AD_ILLEGAL_PARAMETER
;
1169 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_WRONG_CIPHER_RETURNED
);
1174 * Depending on the session caching (internal/external), the cipher
1175 * and/or cipher_id values may not be set. Make sure that cipher_id is
1176 * set and use it for comparison.
1178 if (s
->session
->cipher
)
1179 s
->session
->cipher_id
= s
->session
->cipher
->id
;
1180 if (s
->hit
&& (s
->session
->cipher_id
!= c
->id
)) {
1181 al
= SSL_AD_ILLEGAL_PARAMETER
;
1182 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
,
1183 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED
);
1186 s
->s3
->tmp
.new_cipher
= c
;
1187 /* lets get the compression algorithm */
1189 if (!PACKET_get_1(pkt
, &compression
)) {
1190 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_LENGTH_MISMATCH
);
1191 al
= SSL_AD_DECODE_ERROR
;
1194 #ifdef OPENSSL_NO_COMP
1195 if (compression
!= 0) {
1196 al
= SSL_AD_ILLEGAL_PARAMETER
;
1197 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
,
1198 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1202 * If compression is disabled we'd better not try to resume a session
1203 * using compression.
1205 if (s
->session
->compress_meth
!= 0) {
1206 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_INCONSISTENT_COMPRESSION
);
1210 if (s
->hit
&& compression
!= s
->session
->compress_meth
) {
1211 al
= SSL_AD_ILLEGAL_PARAMETER
;
1212 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
,
1213 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED
);
1216 if (compression
== 0)
1218 else if (!ssl_allow_compression(s
)) {
1219 al
= SSL_AD_ILLEGAL_PARAMETER
;
1220 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_COMPRESSION_DISABLED
);
1223 comp
= ssl3_comp_find(s
->ctx
->comp_methods
, compression
);
1226 if (compression
!= 0 && comp
== NULL
) {
1227 al
= SSL_AD_ILLEGAL_PARAMETER
;
1228 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
,
1229 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM
);
1232 s
->s3
->tmp
.new_compression
= comp
;
1236 /* TLS extensions */
1237 if (!ssl_parse_serverhello_tlsext(s
, pkt
)) {
1238 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_PARSE_TLSEXT
);
1242 if (PACKET_remaining(pkt
) != 0) {
1243 /* wrong packet length */
1244 al
= SSL_AD_DECODE_ERROR
;
1245 SSLerr(SSL_F_TLS_PROCESS_SERVER_HELLO
, SSL_R_BAD_PACKET_LENGTH
);
1249 #ifndef OPENSSL_NO_SCTP
1250 if (SSL_IS_DTLS(s
) && s
->hit
) {
1251 unsigned char sctpauthkey
[64];
1252 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
1255 * Add new shared key for SCTP-Auth, will be ignored if
1258 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
1259 sizeof(DTLS1_SCTP_AUTH_LABEL
));
1261 if (SSL_export_keying_material(s
, sctpauthkey
,
1262 sizeof(sctpauthkey
),
1264 sizeof(labelbuffer
), NULL
, 0,
1268 BIO_ctrl(SSL_get_wbio(s
),
1269 BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
1270 sizeof(sctpauthkey
), sctpauthkey
);
1274 return MSG_PROCESS_CONTINUE_READING
;
1276 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1278 ossl_statem_set_error(s
);
1279 return MSG_PROCESS_ERROR
;
1282 MSG_PROCESS_RETURN
tls_process_server_certificate(SSL
*s
, PACKET
*pkt
)
1284 int al
, i
, ret
= MSG_PROCESS_ERROR
, exp_idx
;
1285 unsigned long cert_list_len
, cert_len
;
1287 unsigned char *certstart
, *certbytes
;
1288 STACK_OF(X509
) *sk
= NULL
;
1289 EVP_PKEY
*pkey
= NULL
;
1291 if ((sk
= sk_X509_new_null()) == NULL
) {
1292 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1296 if (!PACKET_get_net_3(pkt
, &cert_list_len
)
1297 || PACKET_remaining(pkt
) != cert_list_len
) {
1298 al
= SSL_AD_DECODE_ERROR
;
1299 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
, SSL_R_LENGTH_MISMATCH
);
1302 while (PACKET_remaining(pkt
)) {
1303 if (!PACKET_get_net_3(pkt
, &cert_len
)
1304 || !PACKET_get_bytes(pkt
, &certbytes
, cert_len
)) {
1305 al
= SSL_AD_DECODE_ERROR
;
1306 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1307 SSL_R_CERT_LENGTH_MISMATCH
);
1311 certstart
= certbytes
;
1312 x
= d2i_X509(NULL
, (const unsigned char **)&certbytes
, cert_len
);
1314 al
= SSL_AD_BAD_CERTIFICATE
;
1315 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
, ERR_R_ASN1_LIB
);
1318 if (certbytes
!= (certstart
+ cert_len
)) {
1319 al
= SSL_AD_DECODE_ERROR
;
1320 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1321 SSL_R_CERT_LENGTH_MISMATCH
);
1324 if (!sk_X509_push(sk
, x
)) {
1325 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
, ERR_R_MALLOC_FAILURE
);
1331 i
= ssl_verify_cert_chain(s
, sk
);
1332 if (s
->verify_mode
!= SSL_VERIFY_NONE
&& i
<= 0) {
1333 al
= ssl_verify_alarm_type(s
->verify_result
);
1334 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1335 SSL_R_CERTIFICATE_VERIFY_FAILED
);
1338 ERR_clear_error(); /* but we keep s->verify_result */
1340 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
, i
);
1341 al
= SSL_AD_HANDSHAKE_FAILURE
;
1345 s
->session
->peer_chain
= sk
;
1347 * Inconsistency alert: cert_chain does include the peer's certificate,
1348 * which we don't include in s3_srvr.c
1350 x
= sk_X509_value(sk
, 0);
1353 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1356 pkey
= X509_get0_pubkey(x
);
1358 if (pkey
== NULL
|| EVP_PKEY_missing_parameters(pkey
)) {
1361 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1362 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS
);
1366 i
= ssl_cert_type(x
, pkey
);
1370 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1371 SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
1375 exp_idx
= ssl_cipher_get_cert_index(s
->s3
->tmp
.new_cipher
);
1376 if (exp_idx
>= 0 && i
!= exp_idx
1377 && (exp_idx
!= SSL_PKEY_GOST_EC
||
1378 (i
!= SSL_PKEY_GOST12_512
&& i
!= SSL_PKEY_GOST12_256
1379 && i
!= SSL_PKEY_GOST01
))) {
1381 al
= SSL_AD_ILLEGAL_PARAMETER
;
1382 SSLerr(SSL_F_TLS_PROCESS_SERVER_CERTIFICATE
,
1383 SSL_R_WRONG_CERTIFICATE_TYPE
);
1386 s
->session
->peer_type
= i
;
1388 X509_free(s
->session
->peer
);
1390 s
->session
->peer
= x
;
1391 s
->session
->verify_result
= s
->verify_result
;
1394 ret
= MSG_PROCESS_CONTINUE_READING
;
1398 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1400 ossl_statem_set_error(s
);
1403 sk_X509_pop_free(sk
, X509_free
);
1407 MSG_PROCESS_RETURN
tls_process_key_exchange(SSL
*s
, PACKET
*pkt
)
1412 EVP_PKEY
*pkey
= NULL
;
1413 const EVP_MD
*md
= NULL
;
1414 #ifndef OPENSSL_NO_RSA
1417 #ifndef OPENSSL_NO_EC
1418 EVP_PKEY_CTX
*pctx
= NULL
;
1420 PACKET save_param_start
, signature
;
1422 md_ctx
= EVP_MD_CTX_new();
1423 if (md_ctx
== NULL
) {
1424 al
= SSL_AD_INTERNAL_ERROR
;
1425 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1429 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
1431 save_param_start
= *pkt
;
1433 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
1434 EVP_PKEY_free(s
->s3
->peer_tmp
);
1435 s
->s3
->peer_tmp
= NULL
;
1438 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
1440 al
= SSL_AD_DECODE_ERROR
;
1442 #ifndef OPENSSL_NO_PSK
1443 /* PSK ciphersuites are preceded by an identity hint */
1444 if (alg_k
& SSL_PSK
) {
1445 PACKET psk_identity_hint
;
1446 if (!PACKET_get_length_prefixed_2(pkt
, &psk_identity_hint
)) {
1447 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
1452 * Store PSK identity hint for later use, hint is used in
1453 * ssl3_send_client_key_exchange. Assume that the maximum length of
1454 * a PSK identity hint can be as long as the maximum length of a PSK
1457 if (PACKET_remaining(&psk_identity_hint
) > PSK_MAX_IDENTITY_LEN
) {
1458 al
= SSL_AD_HANDSHAKE_FAILURE
;
1459 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_DATA_LENGTH_TOO_LONG
);
1463 if (PACKET_remaining(&psk_identity_hint
) == 0) {
1464 OPENSSL_free(s
->session
->psk_identity_hint
);
1465 s
->session
->psk_identity_hint
= NULL
;
1466 } else if (!PACKET_strndup(&psk_identity_hint
,
1467 &s
->session
->psk_identity_hint
)) {
1468 al
= SSL_AD_INTERNAL_ERROR
;
1473 /* Nothing else to do for plain PSK or RSAPSK */
1474 if (alg_k
& (SSL_kPSK
| SSL_kRSAPSK
)) {
1476 #endif /* !OPENSSL_NO_PSK */
1478 * Dummy "if" to ensure sane C code in the event of various OPENSSL_NO_*
1483 #ifndef OPENSSL_NO_SRP
1484 else if (alg_k
& SSL_kSRP
) {
1485 PACKET prime
, generator
, salt
, server_pub
;
1486 if (!PACKET_get_length_prefixed_2(pkt
, &prime
)
1487 || !PACKET_get_length_prefixed_2(pkt
, &generator
)
1488 || !PACKET_get_length_prefixed_1(pkt
, &salt
)
1489 || !PACKET_get_length_prefixed_2(pkt
, &server_pub
)) {
1490 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
1495 BN_bin2bn(PACKET_data(&prime
),
1496 PACKET_remaining(&prime
), NULL
)) == NULL
1498 BN_bin2bn(PACKET_data(&generator
),
1499 PACKET_remaining(&generator
), NULL
)) == NULL
1501 BN_bin2bn(PACKET_data(&salt
),
1502 PACKET_remaining(&salt
), NULL
)) == NULL
1504 BN_bin2bn(PACKET_data(&server_pub
),
1505 PACKET_remaining(&server_pub
), NULL
)) == NULL
) {
1506 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1510 if (!srp_verify_server_param(s
, &al
)) {
1511 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_BAD_SRP_PARAMETERS
);
1515 /* We must check if there is a certificate */
1516 if (alg_a
& (SSL_aRSA
|SSL_aDSS
))
1517 pkey
= X509_get0_pubkey(s
->session
->peer
);
1519 #endif /* !OPENSSL_NO_SRP */
1520 #ifndef OPENSSL_NO_DH
1521 else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
1522 PACKET prime
, generator
, pub_key
;
1526 if (!PACKET_get_length_prefixed_2(pkt
, &prime
)
1527 || !PACKET_get_length_prefixed_2(pkt
, &generator
)
1528 || !PACKET_get_length_prefixed_2(pkt
, &pub_key
)) {
1529 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
1533 s
->s3
->peer_tmp
= EVP_PKEY_new();
1536 if (s
->s3
->peer_tmp
== NULL
|| dh
== NULL
) {
1537 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
1542 if (EVP_PKEY_assign_DH(s
->s3
->peer_tmp
, dh
) == 0) {
1543 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
1548 if ((dh
->p
= BN_bin2bn(PACKET_data(&prime
),
1549 PACKET_remaining(&prime
), NULL
)) == NULL
1550 || (dh
->g
= BN_bin2bn(PACKET_data(&generator
),
1551 PACKET_remaining(&generator
), NULL
)) == NULL
1553 BN_bin2bn(PACKET_data(&pub_key
),
1554 PACKET_remaining(&pub_key
), NULL
)) == NULL
) {
1555 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_BN_LIB
);
1559 if (BN_is_zero(dh
->p
) || BN_is_zero(dh
->g
) || BN_is_zero(dh
->pub_key
)) {
1560 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_BAD_DH_VALUE
);
1564 if (!ssl_security(s
, SSL_SECOP_TMP_DH
, DH_security_bits(dh
), 0, dh
)) {
1565 al
= SSL_AD_HANDSHAKE_FAILURE
;
1566 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_DH_KEY_TOO_SMALL
);
1569 if (alg_a
& (SSL_aRSA
|SSL_aDSS
))
1570 pkey
= X509_get0_pubkey(s
->session
->peer
);
1571 /* else anonymous DH, so no certificate or pkey. */
1573 #endif /* !OPENSSL_NO_DH */
1575 #ifndef OPENSSL_NO_EC
1576 else if (alg_k
& (SSL_kECDHE
| SSL_kECDHEPSK
)) {
1578 unsigned char *ecparams
;
1582 * Extract elliptic curve parameters and the server's ephemeral ECDH
1583 * public key. For now we only support named (not generic) curves and
1584 * ECParameters in this case is just three bytes.
1586 if (!PACKET_get_bytes(pkt
, &ecparams
, 3)) {
1587 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1591 * Check curve is one of our preferences, if not server has sent an
1592 * invalid curve. ECParameters is 3 bytes.
1594 if (!tls1_check_curve(s
, ecparams
, 3)) {
1595 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_WRONG_CURVE
);
1599 curve_nid
= tls1_ec_curve_id2nid(*(ecparams
+ 2));
1600 if (curve_nid
== 0) {
1601 al
= SSL_AD_INTERNAL_ERROR
;
1602 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
,
1603 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS
);
1607 /* Set up EVP_PKEY with named curve as parameters */
1608 pctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_EC
, NULL
);
1610 || EVP_PKEY_paramgen_init(pctx
) <= 0
1611 || EVP_PKEY_CTX_set_ec_paramgen_curve_nid(pctx
, curve_nid
) <= 0
1612 || EVP_PKEY_paramgen(pctx
, &s
->s3
->peer_tmp
) <= 0) {
1613 al
= SSL_AD_INTERNAL_ERROR
;
1614 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
1617 EVP_PKEY_CTX_free(pctx
);
1620 if (!PACKET_get_length_prefixed_1(pkt
, &encoded_pt
)) {
1621 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
1625 if (EC_KEY_oct2key(EVP_PKEY_get0_EC_KEY(s
->s3
->peer_tmp
),
1626 PACKET_data(&encoded_pt
),
1627 PACKET_remaining(&encoded_pt
), NULL
) == 0) {
1628 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_BAD_ECPOINT
);
1633 * The ECC/TLS specification does not mention the use of DSA to sign
1634 * ECParameters in the server key exchange message. We do support RSA
1638 # ifndef OPENSSL_NO_RSA
1639 else if (alg_a
& SSL_aRSA
)
1640 pkey
= X509_get0_pubkey(s
->session
->peer
);
1642 # ifndef OPENSSL_NO_EC
1643 else if (alg_a
& SSL_aECDSA
)
1644 pkey
= X509_get0_pubkey(s
->session
->peer
);
1646 /* else anonymous ECDH, so no certificate or pkey. */
1648 al
= SSL_AD_UNEXPECTED_MESSAGE
;
1649 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_UNEXPECTED_MESSAGE
);
1652 #endif /* !OPENSSL_NO_EC */
1654 /* if it was signed, check the signature */
1658 * |pkt| now points to the beginning of the signature, so the difference
1659 * equals the length of the parameters.
1661 if (!PACKET_get_sub_packet(&save_param_start
, ¶ms
,
1662 PACKET_remaining(&save_param_start
) -
1663 PACKET_remaining(pkt
))) {
1664 al
= SSL_AD_INTERNAL_ERROR
;
1665 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1669 if (SSL_USE_SIGALGS(s
)) {
1670 unsigned char *sigalgs
;
1672 if (!PACKET_get_bytes(pkt
, &sigalgs
, 2)) {
1673 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_TOO_SHORT
);
1676 rv
= tls12_check_peer_sigalg(&md
, s
, sigalgs
, pkey
);
1683 fprintf(stderr
, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md
));
1685 } else if (EVP_PKEY_id(pkey
) == EVP_PKEY_RSA
) {
1686 md
= EVP_md5_sha1();
1691 if (!PACKET_get_length_prefixed_2(pkt
, &signature
)
1692 || PACKET_remaining(pkt
) != 0) {
1693 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_LENGTH_MISMATCH
);
1696 j
= EVP_PKEY_size(pkey
);
1698 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1703 * Check signature length
1705 if (PACKET_remaining(&signature
) > (size_t)j
) {
1706 /* wrong packet length */
1707 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_WRONG_SIGNATURE_LENGTH
);
1710 if (EVP_VerifyInit_ex(md_ctx
, md
, NULL
) <= 0
1711 || EVP_VerifyUpdate(md_ctx
, &(s
->s3
->client_random
[0]),
1712 SSL3_RANDOM_SIZE
) <= 0
1713 || EVP_VerifyUpdate(md_ctx
, &(s
->s3
->server_random
[0]),
1714 SSL3_RANDOM_SIZE
) <= 0
1715 || EVP_VerifyUpdate(md_ctx
, PACKET_data(¶ms
),
1716 PACKET_remaining(¶ms
)) <= 0) {
1717 al
= SSL_AD_INTERNAL_ERROR
;
1718 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
1721 if (EVP_VerifyFinal(md_ctx
, PACKET_data(&signature
),
1722 PACKET_remaining(&signature
), pkey
) <= 0) {
1724 al
= SSL_AD_DECRYPT_ERROR
;
1725 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_BAD_SIGNATURE
);
1729 /* aNULL, aSRP or PSK do not need public keys */
1730 if (!(alg_a
& (SSL_aNULL
| SSL_aSRP
)) && !(alg_k
& SSL_PSK
)) {
1731 /* Might be wrong key type, check it */
1732 if (ssl3_check_cert_and_algorithm(s
))
1733 /* Otherwise this shouldn't happen */
1734 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
1737 /* still data left over */
1738 if (PACKET_remaining(pkt
) != 0) {
1739 SSLerr(SSL_F_TLS_PROCESS_KEY_EXCHANGE
, SSL_R_EXTRA_DATA_IN_MESSAGE
);
1743 EVP_MD_CTX_free(md_ctx
);
1744 return MSG_PROCESS_CONTINUE_READING
;
1746 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1748 #ifndef OPENSSL_NO_RSA
1751 #ifndef OPENSSL_NO_EC
1752 EVP_PKEY_CTX_free(pctx
);
1754 EVP_MD_CTX_free(md_ctx
);
1755 ossl_statem_set_error(s
);
1756 return MSG_PROCESS_ERROR
;
1759 MSG_PROCESS_RETURN
tls_process_certificate_request(SSL
*s
, PACKET
*pkt
)
1761 int ret
= MSG_PROCESS_ERROR
;
1762 unsigned int list_len
, ctype_num
, i
, name_len
;
1763 X509_NAME
*xn
= NULL
;
1764 unsigned char *data
;
1765 unsigned char *namestart
, *namebytes
;
1766 STACK_OF(X509_NAME
) *ca_sk
= NULL
;
1768 if ((ca_sk
= sk_X509_NAME_new(ca_dn_cmp
)) == NULL
) {
1769 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1773 /* get the certificate types */
1774 if (!PACKET_get_1(pkt
, &ctype_num
)
1775 || !PACKET_get_bytes(pkt
, &data
, ctype_num
)) {
1776 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1777 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
1780 OPENSSL_free(s
->cert
->ctypes
);
1781 s
->cert
->ctypes
= NULL
;
1782 if (ctype_num
> SSL3_CT_NUMBER
) {
1783 /* If we exceed static buffer copy all to cert structure */
1784 s
->cert
->ctypes
= OPENSSL_malloc(ctype_num
);
1785 if (s
->cert
->ctypes
== NULL
) {
1786 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1789 memcpy(s
->cert
->ctypes
, data
, ctype_num
);
1790 s
->cert
->ctype_num
= (size_t)ctype_num
;
1791 ctype_num
= SSL3_CT_NUMBER
;
1793 for (i
= 0; i
< ctype_num
; i
++)
1794 s
->s3
->tmp
.ctype
[i
] = data
[i
];
1796 if (SSL_USE_SIGALGS(s
)) {
1797 if (!PACKET_get_net_2(pkt
, &list_len
)
1798 || !PACKET_get_bytes(pkt
, &data
, list_len
)) {
1799 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1800 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
,
1801 SSL_R_LENGTH_MISMATCH
);
1805 /* Clear certificate digests and validity flags */
1806 for (i
= 0; i
< SSL_PKEY_NUM
; i
++) {
1807 s
->s3
->tmp
.md
[i
] = NULL
;
1808 s
->s3
->tmp
.valid_flags
[i
] = 0;
1810 if ((list_len
& 1) || !tls1_save_sigalgs(s
, data
, list_len
)) {
1811 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1812 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
,
1813 SSL_R_SIGNATURE_ALGORITHMS_ERROR
);
1816 if (!tls1_process_sigalgs(s
)) {
1817 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
1818 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1822 ssl_set_default_md(s
);
1825 /* get the CA RDNs */
1826 if (!PACKET_get_net_2(pkt
, &list_len
)
1827 || PACKET_remaining(pkt
) != list_len
) {
1828 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1829 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, SSL_R_LENGTH_MISMATCH
);
1833 while (PACKET_remaining(pkt
)) {
1834 if (!PACKET_get_net_2(pkt
, &name_len
)
1835 || !PACKET_get_bytes(pkt
, &namebytes
, name_len
)) {
1836 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1837 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
,
1838 SSL_R_LENGTH_MISMATCH
);
1842 namestart
= namebytes
;
1844 if ((xn
= d2i_X509_NAME(NULL
, (const unsigned char **)&namebytes
,
1845 name_len
)) == NULL
) {
1846 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1847 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, ERR_R_ASN1_LIB
);
1851 if (namebytes
!= (namestart
+ name_len
)) {
1852 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
1853 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
,
1854 SSL_R_CA_DN_LENGTH_MISMATCH
);
1857 if (!sk_X509_NAME_push(ca_sk
, xn
)) {
1858 SSLerr(SSL_F_TLS_PROCESS_CERTIFICATE_REQUEST
, ERR_R_MALLOC_FAILURE
);
1863 /* we should setup a certificate to return.... */
1864 s
->s3
->tmp
.cert_req
= 1;
1865 s
->s3
->tmp
.ctype_num
= ctype_num
;
1866 sk_X509_NAME_pop_free(s
->s3
->tmp
.ca_names
, X509_NAME_free
);
1867 s
->s3
->tmp
.ca_names
= ca_sk
;
1870 ret
= MSG_PROCESS_CONTINUE_READING
;
1873 ossl_statem_set_error(s
);
1875 sk_X509_NAME_pop_free(ca_sk
, X509_NAME_free
);
1879 static int ca_dn_cmp(const X509_NAME
*const *a
, const X509_NAME
*const *b
)
1881 return (X509_NAME_cmp(*a
, *b
));
1884 MSG_PROCESS_RETURN
tls_process_new_session_ticket(SSL
*s
, PACKET
*pkt
)
1887 unsigned int ticklen
;
1888 unsigned long ticket_lifetime_hint
;
1890 if (!PACKET_get_net_4(pkt
, &ticket_lifetime_hint
)
1891 || !PACKET_get_net_2(pkt
, &ticklen
)
1892 || PACKET_remaining(pkt
) != ticklen
) {
1893 al
= SSL_AD_DECODE_ERROR
;
1894 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
1898 /* Server is allowed to change its mind and send an empty ticket. */
1900 return MSG_PROCESS_CONTINUE_READING
;
1902 if (s
->session
->session_id_length
> 0) {
1903 int i
= s
->session_ctx
->session_cache_mode
;
1904 SSL_SESSION
*new_sess
;
1906 * We reused an existing session, so we need to replace it with a new
1909 if (i
& SSL_SESS_CACHE_CLIENT
) {
1911 * Remove the old session from the cache
1913 if (i
& SSL_SESS_CACHE_NO_INTERNAL_STORE
) {
1914 if (s
->session_ctx
->remove_session_cb
!= NULL
)
1915 s
->session_ctx
->remove_session_cb(s
->session_ctx
,
1918 /* We carry on if this fails */
1919 SSL_CTX_remove_session(s
->session_ctx
, s
->session
);
1923 if ((new_sess
= ssl_session_dup(s
->session
, 0)) == 0) {
1924 al
= SSL_AD_INTERNAL_ERROR
;
1925 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
1929 SSL_SESSION_free(s
->session
);
1930 s
->session
= new_sess
;
1933 OPENSSL_free(s
->session
->tlsext_tick
);
1934 s
->session
->tlsext_ticklen
= 0;
1936 s
->session
->tlsext_tick
= OPENSSL_malloc(ticklen
);
1937 if (s
->session
->tlsext_tick
== NULL
) {
1938 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET
, ERR_R_MALLOC_FAILURE
);
1941 if (!PACKET_copy_bytes(pkt
, s
->session
->tlsext_tick
, ticklen
)) {
1942 al
= SSL_AD_DECODE_ERROR
;
1943 SSLerr(SSL_F_TLS_PROCESS_NEW_SESSION_TICKET
, SSL_R_LENGTH_MISMATCH
);
1947 s
->session
->tlsext_tick_lifetime_hint
= ticket_lifetime_hint
;
1948 s
->session
->tlsext_ticklen
= ticklen
;
1950 * There are two ways to detect a resumed ticket session. One is to set
1951 * an appropriate session ID and then the server must return a match in
1952 * ServerHello. This allows the normal client session ID matching to work
1953 * and we know much earlier that the ticket has been accepted. The
1954 * other way is to set zero length session ID when the ticket is
1955 * presented and rely on the handshake to determine session resumption.
1956 * We choose the former approach because this fits in with assumptions
1957 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
1958 * SHA256 is disabled) hash of the ticket.
1960 EVP_Digest(s
->session
->tlsext_tick
, ticklen
,
1961 s
->session
->session_id
, &s
->session
->session_id_length
,
1962 EVP_sha256(), NULL
);
1963 return MSG_PROCESS_CONTINUE_READING
;
1965 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
1967 ossl_statem_set_error(s
);
1968 return MSG_PROCESS_ERROR
;
1971 MSG_PROCESS_RETURN
tls_process_cert_status(SSL
*s
, PACKET
*pkt
)
1974 unsigned long resplen
;
1977 if (!PACKET_get_1(pkt
, &type
)
1978 || type
!= TLSEXT_STATUSTYPE_ocsp
) {
1979 al
= SSL_AD_DECODE_ERROR
;
1980 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS
, SSL_R_UNSUPPORTED_STATUS_TYPE
);
1983 if (!PACKET_get_net_3(pkt
, &resplen
)
1984 || PACKET_remaining(pkt
) != resplen
) {
1985 al
= SSL_AD_DECODE_ERROR
;
1986 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
1989 s
->tlsext_ocsp_resp
= OPENSSL_malloc(resplen
);
1990 if (s
->tlsext_ocsp_resp
== NULL
) {
1991 al
= SSL_AD_INTERNAL_ERROR
;
1992 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS
, ERR_R_MALLOC_FAILURE
);
1995 if (!PACKET_copy_bytes(pkt
, s
->tlsext_ocsp_resp
, resplen
)) {
1996 al
= SSL_AD_DECODE_ERROR
;
1997 SSLerr(SSL_F_TLS_PROCESS_CERT_STATUS
, SSL_R_LENGTH_MISMATCH
);
2000 s
->tlsext_ocsp_resplen
= resplen
;
2001 return MSG_PROCESS_CONTINUE_READING
;
2003 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2004 ossl_statem_set_error(s
);
2005 return MSG_PROCESS_ERROR
;
2008 MSG_PROCESS_RETURN
tls_process_server_done(SSL
*s
, PACKET
*pkt
)
2010 if (PACKET_remaining(pkt
) > 0) {
2011 /* should contain no data */
2012 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_DECODE_ERROR
);
2013 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE
, SSL_R_LENGTH_MISMATCH
);
2014 ossl_statem_set_error(s
);
2015 return MSG_PROCESS_ERROR
;
2018 #ifndef OPENSSL_NO_SRP
2019 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
2020 if (SRP_Calc_A_param(s
) <= 0) {
2021 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE
, SSL_R_SRP_A_CALC
);
2022 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2023 ossl_statem_set_error(s
);
2024 return MSG_PROCESS_ERROR
;
2030 * at this point we check that we have the required stuff from
2033 if (!ssl3_check_cert_and_algorithm(s
)) {
2034 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2035 ossl_statem_set_error(s
);
2036 return MSG_PROCESS_ERROR
;
2040 * Call the ocsp status callback if needed. The |tlsext_ocsp_resp| and
2041 * |tlsext_ocsp_resplen| values will be set if we actually received a status
2042 * message, or NULL and -1 otherwise
2044 if (s
->tlsext_status_type
!= -1 && s
->ctx
->tlsext_status_cb
!= NULL
) {
2046 ret
= s
->ctx
->tlsext_status_cb(s
, s
->ctx
->tlsext_status_arg
);
2048 ssl3_send_alert(s
, SSL3_AL_FATAL
,
2049 SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
);
2050 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE
,
2051 SSL_R_INVALID_STATUS_RESPONSE
);
2052 return MSG_PROCESS_ERROR
;
2055 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2056 SSLerr(SSL_F_TLS_PROCESS_SERVER_DONE
, ERR_R_MALLOC_FAILURE
);
2057 return MSG_PROCESS_ERROR
;
2061 #ifndef OPENSSL_NO_SCTP
2062 /* Only applies to renegotiation */
2063 if (SSL_IS_DTLS(s
) && BIO_dgram_is_sctp(SSL_get_wbio(s
))
2064 && s
->renegotiate
!= 0)
2065 return MSG_PROCESS_CONTINUE_PROCESSING
;
2068 return MSG_PROCESS_FINISHED_READING
;
2071 int tls_construct_client_key_exchange(SSL
*s
)
2075 #ifndef OPENSSL_NO_PSK
2076 size_t pskhdrlen
= 0;
2078 unsigned long alg_k
;
2079 #ifndef OPENSSL_NO_RSA
2081 EVP_PKEY
*pkey
= NULL
;
2082 EVP_PKEY_CTX
*pctx
= NULL
;
2084 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2085 EVP_PKEY
*ckey
= NULL
, *skey
= NULL
;
2087 #ifndef OPENSSL_NO_EC
2088 unsigned char *encodedPoint
= NULL
;
2089 int encoded_pt_len
= 0;
2091 unsigned char *pms
= NULL
;
2093 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2095 p
= ssl_handshake_start(s
);
2098 #ifndef OPENSSL_NO_PSK
2099 if (alg_k
& SSL_PSK
) {
2102 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2103 * \0-terminated identity. The last byte is for us for simulating
2106 char identity
[PSK_MAX_IDENTITY_LEN
+ 1];
2108 unsigned char psk
[PSK_MAX_PSK_LEN
];
2111 if (s
->psk_client_callback
== NULL
) {
2112 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2113 SSL_R_PSK_NO_CLIENT_CB
);
2117 memset(identity
, 0, sizeof(identity
));
2119 psklen
= s
->psk_client_callback(s
, s
->session
->psk_identity_hint
,
2120 identity
, sizeof(identity
) - 1,
2123 if (psklen
> PSK_MAX_PSK_LEN
) {
2124 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2125 ERR_R_INTERNAL_ERROR
);
2127 } else if (psklen
== 0) {
2128 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2129 SSL_R_PSK_IDENTITY_NOT_FOUND
);
2132 OPENSSL_free(s
->s3
->tmp
.psk
);
2133 s
->s3
->tmp
.psk
= OPENSSL_memdup(psk
, psklen
);
2134 OPENSSL_cleanse(psk
, psklen
);
2136 if (s
->s3
->tmp
.psk
== NULL
) {
2137 OPENSSL_cleanse(identity
, sizeof(identity
));
2141 s
->s3
->tmp
.psklen
= psklen
;
2142 identitylen
= strlen(identity
);
2143 if (identitylen
> PSK_MAX_IDENTITY_LEN
) {
2144 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2145 ERR_R_INTERNAL_ERROR
);
2148 OPENSSL_free(s
->session
->psk_identity
);
2149 s
->session
->psk_identity
= OPENSSL_strdup(identity
);
2150 if (s
->session
->psk_identity
== NULL
) {
2151 OPENSSL_cleanse(identity
, sizeof(identity
));
2155 s2n(identitylen
, p
);
2156 memcpy(p
, identity
, identitylen
);
2157 pskhdrlen
= 2 + identitylen
;
2161 OPENSSL_cleanse(identity
, sizeof(identity
));
2163 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2167 if (alg_k
& SSL_kPSK
) {
2172 /* Fool emacs indentation */
2175 #ifndef OPENSSL_NO_RSA
2176 else if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
)) {
2178 pmslen
= SSL_MAX_MASTER_KEY_LENGTH
;
2179 pms
= OPENSSL_malloc(pmslen
);
2183 if (s
->session
->peer
== NULL
) {
2185 * We should always have a server certificate with SSL_kRSA.
2187 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2188 ERR_R_INTERNAL_ERROR
);
2192 pkey
= X509_get0_pubkey(s
->session
->peer
);
2193 if (EVP_PKEY_get0_RSA(pkey
) == NULL
) {
2194 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2195 ERR_R_INTERNAL_ERROR
);
2199 pms
[0] = s
->client_version
>> 8;
2200 pms
[1] = s
->client_version
& 0xff;
2201 if (RAND_bytes(pms
+ 2, pmslen
- 2) <= 0)
2205 /* Fix buf for TLS and beyond */
2206 if (s
->version
> SSL3_VERSION
)
2208 pctx
= EVP_PKEY_CTX_new(pkey
, NULL
);
2209 if (pctx
== NULL
|| EVP_PKEY_encrypt_init(pctx
) <= 0
2210 || EVP_PKEY_encrypt(pctx
, NULL
, &enclen
, pms
, pmslen
) <= 0) {
2211 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2215 if (EVP_PKEY_encrypt(pctx
, p
, &enclen
, pms
, pmslen
) <= 0) {
2216 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2217 SSL_R_BAD_RSA_ENCRYPT
);
2221 EVP_PKEY_CTX_free(pctx
);
2224 if (s
->options
& SSL_OP_PKCS1_CHECK_1
)
2226 if (s
->options
& SSL_OP_PKCS1_CHECK_2
)
2230 /* Fix buf for TLS and beyond */
2231 if (s
->version
> SSL3_VERSION
) {
2237 #ifndef OPENSSL_NO_DH
2238 else if (alg_k
& (SSL_kDHE
| SSL_kDHEPSK
)) {
2240 skey
= s
->s3
->peer_tmp
;
2242 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2243 ERR_R_INTERNAL_ERROR
);
2246 ckey
= ssl_generate_pkey(skey
, NID_undef
);
2247 dh_clnt
= EVP_PKEY_get0_DH(ckey
);
2249 if (dh_clnt
== NULL
|| ssl_derive(s
, ckey
, skey
) == 0) {
2250 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2251 ERR_R_INTERNAL_ERROR
);
2256 /* send off the data */
2257 n
= BN_num_bytes(dh_clnt
->pub_key
);
2259 BN_bn2bin(dh_clnt
->pub_key
, p
);
2261 EVP_PKEY_free(ckey
);
2266 #ifndef OPENSSL_NO_EC
2267 else if (alg_k
& (SSL_kECDHE
| SSL_kECDHr
| SSL_kECDHe
| SSL_kECDHEPSK
)) {
2269 if (s
->s3
->peer_tmp
!= NULL
) {
2270 skey
= s
->s3
->peer_tmp
;
2272 /* Get the Server Public Key from Cert */
2273 skey
= X509_get0_pubkey(s
->session
->peer
);
2274 if ((skey
== NULL
) || EVP_PKEY_get0_EC_KEY(skey
) == NULL
) {
2275 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2276 ERR_R_INTERNAL_ERROR
);
2281 ckey
= ssl_generate_pkey(skey
, NID_undef
);
2283 if (ssl_derive(s
, ckey
, skey
) == 0) {
2284 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
, ERR_R_EVP_LIB
);
2288 /* Generate encoding of client key */
2289 encoded_pt_len
= EC_KEY_key2buf(EVP_PKEY_get0_EC_KEY(ckey
),
2290 POINT_CONVERSION_UNCOMPRESSED
,
2291 &encodedPoint
, NULL
);
2293 if (encoded_pt_len
== 0) {
2294 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
, ERR_R_EC_LIB
);
2298 EVP_PKEY_free(ckey
);
2303 *p
= n
; /* length of encoded point */
2304 /* Encoded point will be copied here */
2306 /* copy the point */
2307 memcpy(p
, encodedPoint
, n
);
2308 /* increment n to account for length field */
2311 /* Free allocated memory */
2312 OPENSSL_free(encodedPoint
);
2314 #endif /* !OPENSSL_NO_EC */
2315 #ifndef OPENSSL_NO_GOST
2316 else if (alg_k
& SSL_kGOST
) {
2317 /* GOST key exchange message creation */
2318 EVP_PKEY_CTX
*pkey_ctx
;
2321 unsigned int md_len
;
2322 unsigned char shared_ukm
[32], tmp
[256];
2323 EVP_MD_CTX
*ukm_hash
;
2324 int dgst_nid
= NID_id_GostR3411_94
;
2325 if ((s
->s3
->tmp
.new_cipher
->algorithm_auth
& SSL_aGOST12
) != 0)
2326 dgst_nid
= NID_id_GostR3411_2012_256
;
2330 pms
= OPENSSL_malloc(pmslen
);
2335 * Get server sertificate PKEY and create ctx from it
2337 peer_cert
= s
->session
->peer
;
2339 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2340 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER
);
2344 pkey_ctx
= EVP_PKEY_CTX_new(X509_get0_pubkey(peer_cert
), NULL
);
2345 if (pkey_ctx
== NULL
) {
2346 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2347 ERR_R_MALLOC_FAILURE
);
2351 * If we have send a certificate, and certificate key
2352 * parameters match those of server certificate, use
2353 * certificate key for key exchange
2356 /* Otherwise, generate ephemeral key pair */
2358 if (pkey_ctx
== NULL
2359 || EVP_PKEY_encrypt_init(pkey_ctx
) <= 0
2360 /* Generate session key */
2361 || RAND_bytes(pms
, pmslen
) <= 0) {
2362 EVP_PKEY_CTX_free(pkey_ctx
);
2363 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2364 ERR_R_INTERNAL_ERROR
);
2368 * If we have client certificate, use its secret as peer key
2370 if (s
->s3
->tmp
.cert_req
&& s
->cert
->key
->privatekey
) {
2371 if (EVP_PKEY_derive_set_peer
2372 (pkey_ctx
, s
->cert
->key
->privatekey
) <= 0) {
2374 * If there was an error - just ignore it. Ephemeral key
2381 * Compute shared IV and store it in algorithm-specific context
2384 ukm_hash
= EVP_MD_CTX_new();
2385 if (EVP_DigestInit(ukm_hash
,
2386 EVP_get_digestbynid(dgst_nid
)) <= 0
2387 || EVP_DigestUpdate(ukm_hash
, s
->s3
->client_random
,
2388 SSL3_RANDOM_SIZE
) <= 0
2389 || EVP_DigestUpdate(ukm_hash
, s
->s3
->server_random
,
2390 SSL3_RANDOM_SIZE
) <= 0
2391 || EVP_DigestFinal_ex(ukm_hash
, shared_ukm
, &md_len
) <= 0) {
2392 EVP_MD_CTX_free(ukm_hash
);
2393 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2394 ERR_R_INTERNAL_ERROR
);
2397 EVP_MD_CTX_free(ukm_hash
);
2398 if (EVP_PKEY_CTX_ctrl
2399 (pkey_ctx
, -1, EVP_PKEY_OP_ENCRYPT
, EVP_PKEY_CTRL_SET_IV
, 8,
2401 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2405 /* Make GOST keytransport blob message */
2407 * Encapsulate it into sequence
2409 *(p
++) = V_ASN1_SEQUENCE
| V_ASN1_CONSTRUCTED
;
2411 if (EVP_PKEY_encrypt(pkey_ctx
, tmp
, &msglen
, pms
, pmslen
) <= 0) {
2412 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2416 if (msglen
>= 0x80) {
2418 *(p
++) = msglen
& 0xff;
2421 *(p
++) = msglen
& 0xff;
2424 memcpy(p
, tmp
, msglen
);
2425 /* Check if pubkey from client certificate was used */
2426 if (EVP_PKEY_CTX_ctrl
2427 (pkey_ctx
, -1, -1, EVP_PKEY_CTRL_PEER_KEY
, 2, NULL
) > 0) {
2428 /* Set flag "skip certificate verify" */
2429 s
->s3
->flags
|= TLS1_FLAGS_SKIP_CERT_VERIFY
;
2431 EVP_PKEY_CTX_free(pkey_ctx
);
2435 #ifndef OPENSSL_NO_SRP
2436 else if (alg_k
& SSL_kSRP
) {
2437 if (s
->srp_ctx
.A
!= NULL
) {
2438 /* send off the data */
2439 n
= BN_num_bytes(s
->srp_ctx
.A
);
2441 BN_bn2bin(s
->srp_ctx
.A
, p
);
2444 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2445 ERR_R_INTERNAL_ERROR
);
2448 OPENSSL_free(s
->session
->srp_username
);
2449 s
->session
->srp_username
= OPENSSL_strdup(s
->srp_ctx
.login
);
2450 if (s
->session
->srp_username
== NULL
) {
2451 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
,
2452 ERR_R_MALLOC_FAILURE
);
2458 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2459 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2463 #ifndef OPENSSL_NO_PSK
2467 if (!ssl_set_handshake_header(s
, SSL3_MT_CLIENT_KEY_EXCHANGE
, n
)) {
2468 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_HANDSHAKE_FAILURE
);
2469 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
, ERR_R_INTERNAL_ERROR
);
2474 s
->s3
->tmp
.pms
= pms
;
2475 s
->s3
->tmp
.pmslen
= pmslen
;
2480 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2481 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_KEY_EXCHANGE
, ERR_R_MALLOC_FAILURE
);
2483 OPENSSL_clear_free(pms
, pmslen
);
2484 s
->s3
->tmp
.pms
= NULL
;
2485 #ifndef OPENSSL_NO_RSA
2486 EVP_PKEY_CTX_free(pctx
);
2488 #ifndef OPENSSL_NO_EC
2489 OPENSSL_free(encodedPoint
);
2491 #if !defined(OPENSSL_NO_EC) || !defined(OPENSSL_NO_DH)
2492 EVP_PKEY_free(ckey
);
2494 #ifndef OPENSSL_NO_PSK
2495 OPENSSL_clear_free(s
->s3
->tmp
.psk
, s
->s3
->tmp
.psklen
);
2496 s
->s3
->tmp
.psk
= NULL
;
2498 ossl_statem_set_error(s
);
2502 int tls_client_key_exchange_post_work(SSL
*s
)
2504 unsigned char *pms
= NULL
;
2507 #ifndef OPENSSL_NO_SRP
2509 if (s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kSRP
) {
2510 if (!srp_generate_client_master_secret(s
)) {
2511 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK
,
2512 ERR_R_INTERNAL_ERROR
);
2518 pms
= s
->s3
->tmp
.pms
;
2519 pmslen
= s
->s3
->tmp
.pmslen
;
2521 if (pms
== NULL
&& !(s
->s3
->tmp
.new_cipher
->algorithm_mkey
& SSL_kPSK
)) {
2522 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2523 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK
, ERR_R_MALLOC_FAILURE
);
2526 if (!ssl_generate_master_secret(s
, pms
, pmslen
, 1)) {
2527 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2528 SSLerr(SSL_F_TLS_CLIENT_KEY_EXCHANGE_POST_WORK
, ERR_R_INTERNAL_ERROR
);
2532 #ifndef OPENSSL_NO_SCTP
2533 if (SSL_IS_DTLS(s
)) {
2534 unsigned char sctpauthkey
[64];
2535 char labelbuffer
[sizeof(DTLS1_SCTP_AUTH_LABEL
)];
2538 * Add new shared key for SCTP-Auth, will be ignored if no SCTP
2541 memcpy(labelbuffer
, DTLS1_SCTP_AUTH_LABEL
,
2542 sizeof(DTLS1_SCTP_AUTH_LABEL
));
2544 if (SSL_export_keying_material(s
, sctpauthkey
,
2545 sizeof(sctpauthkey
), labelbuffer
,
2546 sizeof(labelbuffer
), NULL
, 0, 0) <= 0)
2549 BIO_ctrl(SSL_get_wbio(s
), BIO_CTRL_DGRAM_SCTP_ADD_AUTH_KEY
,
2550 sizeof(sctpauthkey
), sctpauthkey
);
2556 OPENSSL_clear_free(pms
, pmslen
);
2557 s
->s3
->tmp
.pms
= NULL
;
2561 int tls_construct_client_verify(SSL
*s
)
2565 const EVP_MD
*md
= s
->s3
->tmp
.md
[s
->cert
->key
- s
->cert
->pkeys
];
2568 unsigned long n
= 0;
2572 mctx
= EVP_MD_CTX_new();
2574 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY
, ERR_R_MALLOC_FAILURE
);
2578 p
= ssl_handshake_start(s
);
2579 pkey
= s
->cert
->key
->privatekey
;
2581 hdatalen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
2582 if (hdatalen
<= 0) {
2583 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2586 if (SSL_USE_SIGALGS(s
)) {
2587 if (!tls12_get_sigandhash(p
, pkey
, md
)) {
2588 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2595 fprintf(stderr
, "Using client alg %s\n", EVP_MD_name(md
));
2597 if (!EVP_SignInit_ex(mctx
, md
, NULL
)
2598 || !EVP_SignUpdate(mctx
, hdata
, hdatalen
)
2599 || (s
->version
== SSL3_VERSION
2600 && !EVP_MD_CTX_ctrl(mctx
, EVP_CTRL_SSL3_MASTER_SECRET
,
2601 s
->session
->master_key_length
,
2602 s
->session
->master_key
))
2603 || !EVP_SignFinal(mctx
, p
+ 2, &u
, pkey
)) {
2604 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY
, ERR_R_EVP_LIB
);
2607 #ifndef OPENSSL_NO_GOST
2609 int pktype
= EVP_PKEY_id(pkey
);
2610 if (pktype
== NID_id_GostR3410_2001
2611 || pktype
== NID_id_GostR3410_2012_256
2612 || pktype
== NID_id_GostR3410_2012_512
)
2613 BUF_reverse(p
+ 2, NULL
, u
);
2619 /* Digest cached records and discard handshake buffer */
2620 if (!ssl3_digest_cached_records(s
, 0))
2622 if (!ssl_set_handshake_header(s
, SSL3_MT_CERTIFICATE_VERIFY
, n
)) {
2623 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_VERIFY
, ERR_R_INTERNAL_ERROR
);
2627 EVP_MD_CTX_free(mctx
);
2630 EVP_MD_CTX_free(mctx
);
2635 * Check a certificate can be used for client authentication. Currently check
2636 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
2637 * certificates can be used and optionally checks suitability for Suite B.
2639 static int ssl3_check_client_certificate(SSL
*s
)
2641 if (!s
->cert
|| !s
->cert
->key
->x509
|| !s
->cert
->key
->privatekey
)
2643 /* If no suitable signature algorithm can't use certificate */
2644 if (SSL_USE_SIGALGS(s
) && !s
->s3
->tmp
.md
[s
->cert
->key
- s
->cert
->pkeys
])
2647 * If strict mode check suitability of chain before using it. This also
2648 * adjusts suite B digest if necessary.
2650 if (s
->cert
->cert_flags
& SSL_CERT_FLAGS_CHECK_TLS_STRICT
&&
2651 !tls1_check_chain(s
, NULL
, NULL
, NULL
, -2))
2656 WORK_STATE
tls_prepare_client_certificate(SSL
*s
, WORK_STATE wst
)
2659 EVP_PKEY
*pkey
= NULL
;
2662 if (wst
== WORK_MORE_A
) {
2663 /* Let cert callback update client certificates if required */
2664 if (s
->cert
->cert_cb
) {
2665 i
= s
->cert
->cert_cb(s
, s
->cert
->cert_cb_arg
);
2667 s
->rwstate
= SSL_X509_LOOKUP
;
2671 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2672 ossl_statem_set_error(s
);
2675 s
->rwstate
= SSL_NOTHING
;
2677 if (ssl3_check_client_certificate(s
))
2678 return WORK_FINISHED_CONTINUE
;
2680 /* Fall through to WORK_MORE_B */
2684 /* We need to get a client cert */
2685 if (wst
== WORK_MORE_B
) {
2687 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
2688 * return(-1); We then get retied later
2690 i
= ssl_do_client_cert_cb(s
, &x509
, &pkey
);
2692 s
->rwstate
= SSL_X509_LOOKUP
;
2695 s
->rwstate
= SSL_NOTHING
;
2696 if ((i
== 1) && (pkey
!= NULL
) && (x509
!= NULL
)) {
2697 if (!SSL_use_certificate(s
, x509
) || !SSL_use_PrivateKey(s
, pkey
))
2699 } else if (i
== 1) {
2701 SSLerr(SSL_F_TLS_PREPARE_CLIENT_CERTIFICATE
,
2702 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK
);
2706 EVP_PKEY_free(pkey
);
2707 if (i
&& !ssl3_check_client_certificate(s
))
2710 if (s
->version
== SSL3_VERSION
) {
2711 s
->s3
->tmp
.cert_req
= 0;
2712 ssl3_send_alert(s
, SSL3_AL_WARNING
, SSL_AD_NO_CERTIFICATE
);
2713 return WORK_FINISHED_CONTINUE
;
2715 s
->s3
->tmp
.cert_req
= 2;
2716 if (!ssl3_digest_cached_records(s
, 0)) {
2717 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2718 ossl_statem_set_error(s
);
2724 return WORK_FINISHED_CONTINUE
;
2727 /* Shouldn't ever get here */
2731 int tls_construct_client_certificate(SSL
*s
)
2733 if (!ssl3_output_cert_chain(s
,
2734 (s
->s3
->tmp
.cert_req
==
2735 2) ? NULL
: s
->cert
->key
)) {
2736 SSLerr(SSL_F_TLS_CONSTRUCT_CLIENT_CERTIFICATE
, ERR_R_INTERNAL_ERROR
);
2737 ssl3_send_alert(s
, SSL3_AL_FATAL
, SSL_AD_INTERNAL_ERROR
);
2738 ossl_statem_set_error(s
);
2745 #define has_bits(i,m) (((i)&(m)) == (m))
2747 int ssl3_check_cert_and_algorithm(SSL
*s
)
2750 #ifndef OPENSSL_NO_EC
2754 EVP_PKEY
*pkey
= NULL
;
2755 int al
= SSL_AD_HANDSHAKE_FAILURE
;
2757 alg_k
= s
->s3
->tmp
.new_cipher
->algorithm_mkey
;
2758 alg_a
= s
->s3
->tmp
.new_cipher
->algorithm_auth
;
2760 /* we don't have a certificate */
2761 if ((alg_a
& SSL_aNULL
) || (alg_k
& SSL_kPSK
))
2764 /* This is the passed certificate */
2766 #ifndef OPENSSL_NO_EC
2767 idx
= s
->session
->peer_type
;
2768 if (idx
== SSL_PKEY_ECC
) {
2769 if (ssl_check_srvr_ecc_cert_and_alg(s
->session
->peer
, s
) == 0) {
2771 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_BAD_ECC_CERT
);
2776 } else if (alg_a
& SSL_aECDSA
) {
2777 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2778 SSL_R_MISSING_ECDSA_SIGNING_CERT
);
2780 } else if (alg_k
& (SSL_kECDHr
| SSL_kECDHe
)) {
2781 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, SSL_R_MISSING_ECDH_CERT
);
2785 pkey
= X509_get0_pubkey(s
->session
->peer
);
2786 i
= X509_certificate_type(s
->session
->peer
, pkey
);
2788 /* Check that we have a certificate if we require one */
2789 if ((alg_a
& SSL_aRSA
) && !has_bits(i
, EVP_PK_RSA
| EVP_PKT_SIGN
)) {
2790 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2791 SSL_R_MISSING_RSA_SIGNING_CERT
);
2794 #ifndef OPENSSL_NO_DSA
2795 else if ((alg_a
& SSL_aDSS
) && !has_bits(i
, EVP_PK_DSA
| EVP_PKT_SIGN
)) {
2796 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2797 SSL_R_MISSING_DSA_SIGNING_CERT
);
2801 #ifndef OPENSSL_NO_RSA
2802 if (alg_k
& (SSL_kRSA
| SSL_kRSAPSK
) &&
2803 !has_bits(i
, EVP_PK_RSA
| EVP_PKT_ENC
)) {
2804 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
,
2805 SSL_R_MISSING_RSA_ENCRYPTING_CERT
);
2809 #ifndef OPENSSL_NO_DH
2810 if ((alg_k
& SSL_kDHE
) && (s
->s3
->peer_tmp
== NULL
)) {
2811 al
= SSL_AD_INTERNAL_ERROR
;
2812 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM
, ERR_R_INTERNAL_ERROR
);
2819 ssl3_send_alert(s
, SSL3_AL_FATAL
, al
);
2823 #ifndef OPENSSL_NO_NEXTPROTONEG
2824 int tls_construct_next_proto(SSL
*s
)
2826 unsigned int len
, padding_len
;
2829 len
= s
->next_proto_negotiated_len
;
2830 padding_len
= 32 - ((len
+ 2) % 32);
2831 d
= (unsigned char *)s
->init_buf
->data
;
2833 memcpy(d
+ 5, s
->next_proto_negotiated
, len
);
2834 d
[5 + len
] = padding_len
;
2835 memset(d
+ 6 + len
, 0, padding_len
);
2836 *(d
++) = SSL3_MT_NEXT_PROTO
;
2837 l2n3(2 + len
+ padding_len
, d
);
2838 s
->init_num
= 4 + 2 + len
+ padding_len
;
2845 int ssl_do_client_cert_cb(SSL
*s
, X509
**px509
, EVP_PKEY
**ppkey
)
2848 #ifndef OPENSSL_NO_ENGINE
2849 if (s
->ctx
->client_cert_engine
) {
2850 i
= ENGINE_load_ssl_client_cert(s
->ctx
->client_cert_engine
, s
,
2851 SSL_get_client_CA_list(s
),
2852 px509
, ppkey
, NULL
, NULL
, NULL
);
2857 if (s
->ctx
->client_cert_cb
)
2858 i
= s
->ctx
->client_cert_cb(s
, px509
, ppkey
);
2862 int ssl_cipher_list_to_bytes(SSL
*s
, STACK_OF(SSL_CIPHER
) *sk
,
2866 const SSL_CIPHER
*c
;
2868 int empty_reneg_info_scsv
= !s
->renegotiate
;
2869 /* Set disabled masks for this session */
2870 ssl_set_client_disabled(s
);
2876 for (i
= 0; i
< sk_SSL_CIPHER_num(sk
); i
++) {
2877 c
= sk_SSL_CIPHER_value(sk
, i
);
2878 /* Skip disabled ciphers */
2879 if (ssl_cipher_disabled(s
, c
, SSL_SECOP_CIPHER_SUPPORTED
))
2881 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
2882 if (c
->id
== SSL3_CK_SCSV
) {
2883 if (!empty_reneg_info_scsv
)
2886 empty_reneg_info_scsv
= 0;
2889 j
= s
->method
->put_cipher_by_char(c
, p
);
2893 * If p == q, no ciphers; caller indicates an error. Otherwise, add
2897 if (empty_reneg_info_scsv
) {
2898 static SSL_CIPHER scsv
= {
2899 0, NULL
, SSL3_CK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
2901 j
= s
->method
->put_cipher_by_char(&scsv
, p
);
2903 #ifdef OPENSSL_RI_DEBUG
2905 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
2908 if (s
->mode
& SSL_MODE_SEND_FALLBACK_SCSV
) {
2909 static SSL_CIPHER scsv
= {
2910 0, NULL
, SSL3_CK_FALLBACK_SCSV
, 0, 0, 0, 0, 0, 0, 0, 0, 0
2912 j
= s
->method
->put_cipher_by_char(&scsv
, p
);