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 <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 int ssl_set_version(SSL *s);
167 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
168 static int ssl3_check_change(SSL *s);
169 static int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
171 int (*put_cb) (const SSL_CIPHER *,
175 int ssl3_connect(SSL *s)
178 unsigned long Time = (unsigned long)time(NULL);
179 void (*cb) (const SSL *ssl, int type, int val) = NULL;
181 int new_state, state, skip = 0;
183 RAND_add(&Time, sizeof(Time), 0);
187 if (s->info_callback != NULL)
188 cb = s->info_callback;
189 else if (s->ctx->info_callback != NULL)
190 cb = s->ctx->info_callback;
193 if (!SSL_in_init(s) || SSL_in_before(s)) {
198 #ifndef OPENSSL_NO_HEARTBEATS
200 * If we're awaiting a HeartbeatResponse, pretend we already got and
201 * don't await it anymore, because Heartbeats don't make sense during
204 if (s->tlsext_hb_pending) {
205 s->tlsext_hb_pending = 0;
214 case SSL_ST_RENEGOTIATE:
216 s->state = SSL_ST_CONNECT;
217 s->ctx->stats.sess_connect_renegotiate++;
221 case SSL_ST_BEFORE | SSL_ST_CONNECT:
222 case SSL_ST_OK | SSL_ST_CONNECT:
226 cb(s, SSL_CB_HANDSHAKE_START, 1);
228 if ((s->version >> 8) != SSL3_VERSION_MAJOR
229 && s->version != TLS_ANY_VERSION) {
230 SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
231 s->state = SSL_ST_ERR;
236 if (s->version != TLS_ANY_VERSION &&
237 !ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
238 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_VERSION_TOO_LOW);
242 /* s->version=SSL3_VERSION; */
243 s->type = SSL_ST_CONNECT;
245 if (s->init_buf == NULL) {
246 if ((buf = BUF_MEM_new()) == NULL) {
248 s->state = SSL_ST_ERR;
251 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
253 s->state = SSL_ST_ERR;
260 if (!ssl3_setup_buffers(s)) {
265 /* setup buffing BIO */
266 if (!ssl_init_wbio_buffer(s, 0)) {
268 s->state = SSL_ST_ERR;
272 /* don't push the buffering BIO quite yet */
274 ssl3_init_finished_mac(s);
276 s->state = SSL3_ST_CW_CLNT_HELLO_A;
277 s->ctx->stats.sess_connect++;
280 * Should have been reset by ssl3_get_finished, too.
282 s->s3->change_cipher_spec = 0;
285 case SSL3_ST_CW_CLNT_HELLO_A:
286 case SSL3_ST_CW_CLNT_HELLO_B:
289 ret = ssl3_client_hello(s);
292 s->state = SSL3_ST_CR_SRVR_HELLO_A;
295 /* turn on buffering for the next lot of output */
296 if (s->bbio != s->wbio)
297 s->wbio = BIO_push(s->bbio, s->wbio);
301 case SSL3_ST_CR_SRVR_HELLO_A:
302 case SSL3_ST_CR_SRVR_HELLO_B:
303 ret = ssl3_get_server_hello(s);
308 s->state = SSL3_ST_CR_CHANGE_A;
309 if (s->tlsext_ticket_expected) {
310 /* receive renewed session ticket */
311 s->state = SSL3_ST_CR_SESSION_TICKET_A;
314 s->state = SSL3_ST_CR_CERT_A;
318 case SSL3_ST_CR_CERT_A:
319 case SSL3_ST_CR_CERT_B:
320 /* Noop (ret = 0) for everything but EAP-FAST. */
321 ret = ssl3_check_change(s);
326 s->state = SSL3_ST_CR_CHANGE_A;
331 /* Check if it is anon DH/ECDH, SRP auth */
333 if (!(s->s3->tmp.new_cipher->algorithm_auth &
334 (SSL_aNULL | SSL_aSRP | SSL_aPSK))) {
335 ret = ssl3_get_server_certificate(s);
339 if (s->tlsext_status_expected)
340 s->state = SSL3_ST_CR_CERT_STATUS_A;
342 s->state = SSL3_ST_CR_KEY_EXCH_A;
345 s->state = SSL3_ST_CR_KEY_EXCH_A;
351 case SSL3_ST_CR_KEY_EXCH_A:
352 case SSL3_ST_CR_KEY_EXCH_B:
353 ret = ssl3_get_key_exchange(s);
356 s->state = SSL3_ST_CR_CERT_REQ_A;
360 * at this point we check that we have the required stuff from
363 if (!ssl3_check_cert_and_algorithm(s)) {
365 s->state = SSL_ST_ERR;
370 case SSL3_ST_CR_CERT_REQ_A:
371 case SSL3_ST_CR_CERT_REQ_B:
372 ret = ssl3_get_certificate_request(s);
375 s->state = SSL3_ST_CR_SRVR_DONE_A;
379 case SSL3_ST_CR_SRVR_DONE_A:
380 case SSL3_ST_CR_SRVR_DONE_B:
381 ret = ssl3_get_server_done(s);
384 #ifndef OPENSSL_NO_SRP
385 if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
386 if ((ret = SRP_Calc_A_param(s)) <= 0) {
387 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
388 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
389 s->state = SSL_ST_ERR;
394 if (s->s3->tmp.cert_req)
395 s->state = SSL3_ST_CW_CERT_A;
397 s->state = SSL3_ST_CW_KEY_EXCH_A;
402 case SSL3_ST_CW_CERT_A:
403 case SSL3_ST_CW_CERT_B:
404 case SSL3_ST_CW_CERT_C:
405 case SSL3_ST_CW_CERT_D:
406 ret = ssl3_send_client_certificate(s);
409 s->state = SSL3_ST_CW_KEY_EXCH_A;
413 case SSL3_ST_CW_KEY_EXCH_A:
414 case SSL3_ST_CW_KEY_EXCH_B:
415 ret = ssl3_send_client_key_exchange(s);
419 * EAY EAY EAY need to check for DH fix cert sent back
422 * For TLS, cert_req is set to 2, so a cert chain of nothing is
423 * sent, but no verify packet is sent
426 * XXX: For now, we do not support client authentication in ECDH
427 * cipher suites with ECDH (rather than ECDSA) certificates. We
428 * need to skip the certificate verify message when client's
429 * ECDH public key is sent inside the client certificate.
431 if (s->s3->tmp.cert_req == 1) {
432 s->state = SSL3_ST_CW_CERT_VRFY_A;
434 s->state = SSL3_ST_CW_CHANGE_A;
436 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
437 s->state = SSL3_ST_CW_CHANGE_A;
443 case SSL3_ST_CW_CERT_VRFY_A:
444 case SSL3_ST_CW_CERT_VRFY_B:
445 ret = ssl3_send_client_verify(s);
448 s->state = SSL3_ST_CW_CHANGE_A;
452 case SSL3_ST_CW_CHANGE_A:
453 case SSL3_ST_CW_CHANGE_B:
454 ret = ssl3_send_change_cipher_spec(s,
456 SSL3_ST_CW_CHANGE_B);
460 #if defined(OPENSSL_NO_NEXTPROTONEG)
461 s->state = SSL3_ST_CW_FINISHED_A;
463 if (s->s3->next_proto_neg_seen)
464 s->state = SSL3_ST_CW_NEXT_PROTO_A;
466 s->state = SSL3_ST_CW_FINISHED_A;
470 s->session->cipher = s->s3->tmp.new_cipher;
471 #ifdef OPENSSL_NO_COMP
472 s->session->compress_meth = 0;
474 if (s->s3->tmp.new_compression == NULL)
475 s->session->compress_meth = 0;
477 s->session->compress_meth = s->s3->tmp.new_compression->id;
479 if (!s->method->ssl3_enc->setup_key_block(s)) {
481 s->state = SSL_ST_ERR;
485 if (!s->method->ssl3_enc->change_cipher_state(s,
486 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
489 s->state = SSL_ST_ERR;
495 #if !defined(OPENSSL_NO_NEXTPROTONEG)
496 case SSL3_ST_CW_NEXT_PROTO_A:
497 case SSL3_ST_CW_NEXT_PROTO_B:
498 ret = ssl3_send_next_proto(s);
501 s->state = SSL3_ST_CW_FINISHED_A;
505 case SSL3_ST_CW_FINISHED_A:
506 case SSL3_ST_CW_FINISHED_B:
507 ret = ssl3_send_finished(s,
508 SSL3_ST_CW_FINISHED_A,
509 SSL3_ST_CW_FINISHED_B,
511 ssl3_enc->client_finished_label,
513 ssl3_enc->client_finished_label_len);
516 s->state = SSL3_ST_CW_FLUSH;
519 s->s3->tmp.next_state = SSL_ST_OK;
522 * Allow NewSessionTicket if ticket expected
524 if (s->tlsext_ticket_expected)
525 s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
527 s->s3->tmp.next_state = SSL3_ST_CR_CHANGE_A;
532 case SSL3_ST_CR_SESSION_TICKET_A:
533 case SSL3_ST_CR_SESSION_TICKET_B:
534 ret = ssl3_get_new_session_ticket(s);
537 s->state = SSL3_ST_CR_CHANGE_A;
541 case SSL3_ST_CR_CERT_STATUS_A:
542 case SSL3_ST_CR_CERT_STATUS_B:
543 ret = ssl3_get_cert_status(s);
546 s->state = SSL3_ST_CR_KEY_EXCH_A;
550 case SSL3_ST_CR_CHANGE_A:
551 case SSL3_ST_CR_CHANGE_B:
552 ret = ssl3_get_change_cipher_spec(s, SSL3_ST_CR_CHANGE_A,
553 SSL3_ST_CR_CHANGE_B);
557 s->state = SSL3_ST_CR_FINISHED_A;
561 case SSL3_ST_CR_FINISHED_A:
562 case SSL3_ST_CR_FINISHED_B:
563 ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
564 SSL3_ST_CR_FINISHED_B);
569 s->state = SSL3_ST_CW_CHANGE_A;
571 s->state = SSL_ST_OK;
575 case SSL3_ST_CW_FLUSH:
576 s->rwstate = SSL_WRITING;
577 if (BIO_flush(s->wbio) <= 0) {
581 s->rwstate = SSL_NOTHING;
582 s->state = s->s3->tmp.next_state;
586 /* clean a few things up */
587 ssl3_cleanup_key_block(s);
588 BUF_MEM_free(s->init_buf);
591 /* remove the buffering */
592 ssl_free_wbio_buffer(s);
598 ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
600 s->ctx->stats.sess_hit++;
604 s->handshake_func = ssl3_connect;
605 s->ctx->stats.sess_connect_good++;
608 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
615 SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
621 /* did we do anything */
622 if (!s->s3->tmp.reuse_message && !skip) {
624 if ((ret = BIO_flush(s->wbio)) <= 0)
628 if ((cb != NULL) && (s->state != state)) {
629 new_state = s->state;
631 cb(s, SSL_CB_CONNECT_LOOP, 1);
632 s->state = new_state;
641 cb(s, SSL_CB_CONNECT_EXIT, ret);
646 * Work out what version we should be using for the initial ClientHello if
647 * the version is currently set to (D)TLS_ANY_VERSION.
648 * Returns 1 on success
651 static int ssl_set_version(SSL *s)
653 unsigned long mask, options = s->options;
655 if (s->method->version == TLS_ANY_VERSION) {
657 * SSL_OP_NO_X disables all protocols above X *if* there are
658 * some protocols below X enabled. This is required in order
659 * to maintain "version capability" vector contiguous. So
660 * that if application wants to disable TLS1.0 in favour of
661 * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
662 * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3.
664 mask = SSL_OP_NO_TLSv1_1 | SSL_OP_NO_TLSv1
665 #if !defined(OPENSSL_NO_SSL3)
669 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
670 if (options & SSL_OP_NO_TLSv1_2) {
671 if ((options & mask) != mask) {
672 s->version = TLS1_1_VERSION;
674 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
678 s->version = TLS1_2_VERSION;
681 if ((options & mask) == mask) {
682 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_NO_PROTOCOLS_AVAILABLE);
685 s->version = TLS1_1_VERSION;
688 mask &= ~SSL_OP_NO_TLSv1_1;
689 if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
690 s->version = TLS1_VERSION;
691 mask &= ~SSL_OP_NO_TLSv1;
692 #if !defined(OPENSSL_NO_SSL3)
693 if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
694 s->version = SSL3_VERSION;
697 if (s->version != TLS1_2_VERSION && tls1_suiteb(s)) {
698 SSLerr(SSL_F_SSL_SET_VERSION,
699 SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
703 if (s->version == SSL3_VERSION && FIPS_mode()) {
704 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
708 } else if (s->method->version == DTLS_ANY_VERSION) {
709 /* Determine which DTLS version to use */
710 /* If DTLS 1.2 disabled correct the version number */
711 if (options & SSL_OP_NO_DTLSv1_2) {
712 if (tls1_suiteb(s)) {
713 SSLerr(SSL_F_SSL_SET_VERSION,
714 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
718 * Disabling all versions is silly: return an error.
720 if (options & SSL_OP_NO_DTLSv1) {
721 SSLerr(SSL_F_SSL_SET_VERSION, SSL_R_WRONG_SSL_VERSION);
725 * Update method so we don't use any DTLS 1.2 features.
727 s->method = DTLSv1_client_method();
728 s->version = DTLS1_VERSION;
731 * We only support one version: update method
733 if (options & SSL_OP_NO_DTLSv1)
734 s->method = DTLSv1_2_client_method();
735 s->version = DTLS1_2_VERSION;
739 s->client_version = s->version;
744 int ssl3_client_hello(SSL *s)
747 unsigned char *p, *d;
751 #ifndef OPENSSL_NO_COMP
756 buf = (unsigned char *)s->init_buf->data;
757 if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
758 SSL_SESSION *sess = s->session;
760 /* Work out what SSL/TLS/DTLS version to use */
761 if (ssl_set_version(s) == 0)
764 if ((sess == NULL) || (sess->ssl_version != s->version) ||
766 * In the case of EAP-FAST, we can have a pre-shared
767 * "ticket" without a session ID.
769 (!sess->session_id_length && !sess->tlsext_tick) ||
770 (sess->not_resumable)) {
771 if (!ssl_get_new_session(s, 0))
774 /* else use the pre-loaded session */
776 p = s->s3->client_random;
779 * for DTLS if client_random is initialized, reuse it, we are
780 * required to use same upon reply to HelloVerify
782 if (SSL_IS_DTLS(s)) {
785 for (idx = 0; idx < sizeof(s->s3->client_random); idx++) {
794 if (i && ssl_fill_hello_random(s, 0, p,
795 sizeof(s->s3->client_random)) <= 0)
798 /* Do the message type and length last */
799 d = p = ssl_handshake_start(s);
802 * version indicates the negotiated version: for example from
803 * an SSLv2/v3 compatible client hello). The client_version
804 * field is the maximum version we permit and it is also
805 * used in RSA encrypted premaster secrets. Some servers can
806 * choke if we initially report a higher version then
807 * renegotiate to a lower one in the premaster secret. This
808 * didn't happen with TLS 1.0 as most servers supported it
809 * but it can with TLS 1.1 or later if the server only supports
812 * Possible scenario with previous logic:
813 * 1. Client hello indicates TLS 1.2
814 * 2. Server hello says TLS 1.0
815 * 3. RSA encrypted premaster secret uses 1.2.
816 * 4. Handhaked proceeds using TLS 1.0.
817 * 5. Server sends hello request to renegotiate.
818 * 6. Client hello indicates TLS v1.0 as we now
819 * know that is maximum server supports.
820 * 7. Server chokes on RSA encrypted premaster secret
821 * containing version 1.0.
823 * For interoperability it should be OK to always use the
824 * maximum version we support in client hello and then rely
825 * on the checking of version to ensure the servers isn't
826 * being inconsistent: for example initially negotiating with
827 * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
828 * client_version in client hello and not resetting it to
829 * the negotiated version.
831 *(p++) = s->client_version >> 8;
832 *(p++) = s->client_version & 0xff;
835 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
836 p += SSL3_RANDOM_SIZE;
842 i = s->session->session_id_length;
845 if (i > (int)sizeof(s->session->session_id)) {
846 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
849 memcpy(p, s->session->session_id, i);
853 /* cookie stuff for DTLS */
854 if (SSL_IS_DTLS(s)) {
855 if (s->d1->cookie_len > sizeof(s->d1->cookie)) {
856 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
859 *(p++) = s->d1->cookie_len;
860 memcpy(p, s->d1->cookie, s->d1->cookie_len);
861 p += s->d1->cookie_len;
864 /* Ciphers supported */
865 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
867 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
870 #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
872 * Some servers hang if client hello > 256 bytes as hack workaround
873 * chop number of supported ciphers to keep it well below this if we
876 if (TLS1_get_version(s) >= TLS1_2_VERSION
877 && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
878 i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
884 #ifdef OPENSSL_NO_COMP
888 if (!ssl_allow_compression(s) || !s->ctx->comp_methods)
891 j = sk_SSL_COMP_num(s->ctx->comp_methods);
893 for (i = 0; i < j; i++) {
894 comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
898 *(p++) = 0; /* Add the NULL method */
901 if (ssl_prepare_clienthello_tlsext(s) <= 0) {
902 SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
906 ssl_add_clienthello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
908 ssl3_send_alert(s, SSL3_AL_FATAL, al);
909 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
914 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_HELLO, l)) {
915 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
916 SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
919 s->state = SSL3_ST_CW_CLNT_HELLO_B;
922 /* SSL3_ST_CW_CLNT_HELLO_B */
923 return ssl_do_write(s);
925 s->state = SSL_ST_ERR;
929 int ssl3_get_server_hello(SSL *s)
931 STACK_OF(SSL_CIPHER) *sk;
933 unsigned char *p, *d;
934 int i, al = SSL_AD_INTERNAL_ERROR, ok;
937 #ifndef OPENSSL_NO_COMP
941 * Hello verify request and/or server hello version may not match so set
942 * first packet if we're negotiating version.
946 n = s->method->ssl_get_message(s,
947 SSL3_ST_CR_SRVR_HELLO_A,
948 SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
954 if (SSL_IS_DTLS(s)) {
955 if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
956 if (s->d1->send_cookie == 0) {
957 s->s3->tmp.reuse_message = 1;
959 } else { /* already sent a cookie */
961 al = SSL_AD_UNEXPECTED_MESSAGE;
962 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
968 if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
969 al = SSL_AD_UNEXPECTED_MESSAGE;
970 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
974 d = p = (unsigned char *)s->init_msg;
976 if (s->method->version == TLS_ANY_VERSION) {
977 int sversion = (p[0] << 8) | p[1];
979 #if TLS_MAX_VERSION != TLS1_2_VERSION
980 #error Code needs updating for new TLS version
982 #ifndef OPENSSL_NO_SSL3
983 if ((sversion == SSL3_VERSION) && !(s->options & SSL_OP_NO_SSLv3)) {
985 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
986 SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
987 al = SSL_AD_PROTOCOL_VERSION;
990 s->method = SSLv3_client_method();
993 if ((sversion == TLS1_VERSION) && !(s->options & SSL_OP_NO_TLSv1)) {
994 s->method = TLSv1_client_method();
995 } else if ((sversion == TLS1_1_VERSION) &&
996 !(s->options & SSL_OP_NO_TLSv1_1)) {
997 s->method = TLSv1_1_client_method();
998 } else if ((sversion == TLS1_2_VERSION) &&
999 !(s->options & SSL_OP_NO_TLSv1_2)) {
1000 s->method = TLSv1_2_client_method();
1002 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
1003 al = SSL_AD_PROTOCOL_VERSION;
1006 s->session->ssl_version = s->version = s->method->version;
1008 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
1009 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_VERSION_TOO_LOW);
1010 al = SSL_AD_PROTOCOL_VERSION;
1013 } else if (s->method->version == DTLS_ANY_VERSION) {
1014 /* Work out correct protocol version to use */
1015 int hversion = (p[0] << 8) | p[1];
1016 int options = s->options;
1017 if (hversion == DTLS1_2_VERSION && !(options & SSL_OP_NO_DTLSv1_2))
1018 s->method = DTLSv1_2_client_method();
1019 else if (tls1_suiteb(s)) {
1020 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1021 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1022 s->version = hversion;
1023 al = SSL_AD_PROTOCOL_VERSION;
1025 } else if (hversion == DTLS1_VERSION && !(options & SSL_OP_NO_DTLSv1))
1026 s->method = DTLSv1_client_method();
1028 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1029 s->version = hversion;
1030 al = SSL_AD_PROTOCOL_VERSION;
1033 s->session->ssl_version = s->version = s->method->version;
1034 } else if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
1035 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
1036 s->version = (s->version & 0xff00) | p[1];
1037 al = SSL_AD_PROTOCOL_VERSION;
1042 /* load the server hello data */
1043 /* load the server random */
1044 memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
1045 p += SSL3_RANDOM_SIZE;
1049 /* get the session-id */
1052 if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
1053 al = SSL_AD_ILLEGAL_PARAMETER;
1054 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
1059 * Check if we can resume the session based on external pre-shared secret.
1060 * EAP-FAST (RFC 4851) supports two types of session resumption.
1061 * Resumption based on server-side state works with session IDs.
1062 * Resumption based on pre-shared Protected Access Credentials (PACs)
1063 * works by overriding the SessionTicket extension at the application
1064 * layer, and does not send a session ID. (We do not know whether EAP-FAST
1065 * servers would honour the session ID.) Therefore, the session ID alone
1066 * is not a reliable indicator of session resumption, so we first check if
1067 * we can resume, and later peek at the next handshake message to see if the
1068 * server wants to resume.
1070 if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
1071 s->session->tlsext_tick) {
1072 SSL_CIPHER *pref_cipher = NULL;
1073 s->session->master_key_length = sizeof(s->session->master_key);
1074 if (s->tls_session_secret_cb(s, s->session->master_key,
1075 &s->session->master_key_length,
1077 s->tls_session_secret_cb_arg)) {
1078 s->session->cipher = pref_cipher ?
1079 pref_cipher : ssl_get_cipher_by_char(s, p + j);
1081 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1082 al = SSL_AD_INTERNAL_ERROR;
1087 if (j != 0 && j == s->session->session_id_length
1088 && memcmp(p, s->session->session_id, j) == 0) {
1089 if (s->sid_ctx_length != s->session->sid_ctx_length
1090 || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
1091 /* actually a client application bug */
1092 al = SSL_AD_ILLEGAL_PARAMETER;
1093 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1094 SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
1100 * If we were trying for session-id reuse but the server
1101 * didn't echo the ID, make a new SSL_SESSION.
1102 * In the case of EAP-FAST and PAC, we do not send a session ID,
1103 * so the PAC-based session secret is always preserved. It'll be
1104 * overwritten if the server refuses resumption.
1106 if (s->session->session_id_length > 0) {
1107 if (!ssl_get_new_session(s, 0)) {
1111 s->session->session_id_length = j;
1112 memcpy(s->session->session_id, p, j); /* j could be 0 */
1115 c = ssl_get_cipher_by_char(s, p);
1117 /* unknown cipher */
1118 al = SSL_AD_ILLEGAL_PARAMETER;
1119 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
1122 /* Set version disabled mask now we know version */
1123 if (!SSL_USE_TLS1_2_CIPHERS(s))
1124 s->s3->tmp.mask_ssl = SSL_TLSV1_2;
1126 s->s3->tmp.mask_ssl = 0;
1128 * If it is a disabled cipher we didn't send it in client hello, so
1131 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_CHECK)) {
1132 al = SSL_AD_ILLEGAL_PARAMETER;
1133 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1136 p += ssl_put_cipher_by_char(s, NULL, NULL);
1138 sk = ssl_get_ciphers_by_id(s);
1139 i = sk_SSL_CIPHER_find(sk, c);
1141 /* we did not say we would use this cipher */
1142 al = SSL_AD_ILLEGAL_PARAMETER;
1143 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
1148 * Depending on the session caching (internal/external), the cipher
1149 * and/or cipher_id values may not be set. Make sure that cipher_id is
1150 * set and use it for comparison.
1152 if (s->session->cipher)
1153 s->session->cipher_id = s->session->cipher->id;
1154 if (s->hit && (s->session->cipher_id != c->id)) {
1155 al = SSL_AD_ILLEGAL_PARAMETER;
1156 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1157 SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1160 s->s3->tmp.new_cipher = c;
1162 * Don't digest cached records if no sigalgs: we may need them for client
1165 if (!SSL_USE_SIGALGS(s) && !ssl3_digest_cached_records(s, 0))
1167 /* lets get the compression algorithm */
1169 #ifdef OPENSSL_NO_COMP
1171 al = SSL_AD_ILLEGAL_PARAMETER;
1172 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1173 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1177 * If compression is disabled we'd better not try to resume a session
1178 * using compression.
1180 if (s->session->compress_meth != 0) {
1181 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1186 if (s->hit && j != s->session->compress_meth) {
1187 al = SSL_AD_ILLEGAL_PARAMETER;
1188 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1189 SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1194 else if (!ssl_allow_compression(s)) {
1195 al = SSL_AD_ILLEGAL_PARAMETER;
1196 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1199 comp = ssl3_comp_find(s->ctx->comp_methods, j);
1201 if ((j != 0) && (comp == NULL)) {
1202 al = SSL_AD_ILLEGAL_PARAMETER;
1203 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1204 SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1207 s->s3->tmp.new_compression = comp;
1211 /* TLS extensions */
1212 if (!ssl_parse_serverhello_tlsext(s, &p, d, n)) {
1213 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1218 /* wrong packet length */
1219 al = SSL_AD_DECODE_ERROR;
1220 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1226 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1228 s->state = SSL_ST_ERR;
1232 int ssl3_get_server_certificate(SSL *s)
1234 int al, i, ok, ret = -1, exp_idx;
1235 unsigned long n, cert_list_len, cert_len;
1237 unsigned char *certstart, *certbytes;
1238 STACK_OF(X509) *sk = NULL;
1239 EVP_PKEY *pkey = NULL;
1242 n = s->method->ssl_get_message(s,
1245 -1, s->max_cert_list, &ok);
1250 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) {
1251 s->s3->tmp.reuse_message = 1;
1255 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1256 al = SSL_AD_UNEXPECTED_MESSAGE;
1257 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1261 if (!PACKET_buf_init(&pkt, s->init_msg, n)) {
1262 al = SSL_AD_INTERNAL_ERROR;
1263 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
1267 if ((sk = sk_X509_new_null()) == NULL) {
1268 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1272 if (!PACKET_get_net_3(&pkt, &cert_list_len)
1273 || PACKET_remaining(&pkt) != cert_list_len) {
1274 al = SSL_AD_DECODE_ERROR;
1275 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1278 while (PACKET_remaining(&pkt)) {
1279 if (!PACKET_get_net_3(&pkt, &cert_len)
1280 || !PACKET_get_bytes(&pkt, &certbytes, cert_len)) {
1281 al = SSL_AD_DECODE_ERROR;
1282 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1283 SSL_R_CERT_LENGTH_MISMATCH);
1287 certstart = certbytes;
1288 x = d2i_X509(NULL, (const unsigned char **)&certbytes, cert_len);
1290 al = SSL_AD_BAD_CERTIFICATE;
1291 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1294 if (certbytes != (certstart + cert_len)) {
1295 al = SSL_AD_DECODE_ERROR;
1296 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1297 SSL_R_CERT_LENGTH_MISMATCH);
1300 if (!sk_X509_push(sk, x)) {
1301 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1307 i = ssl_verify_cert_chain(s, sk);
1308 if (s->verify_mode != SSL_VERIFY_NONE && i <= 0) {
1309 al = ssl_verify_alarm_type(s->verify_result);
1310 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1311 SSL_R_CERTIFICATE_VERIFY_FAILED);
1314 ERR_clear_error(); /* but we keep s->verify_result */
1316 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, i);
1317 al = SSL_AD_HANDSHAKE_FAILURE;
1321 s->session->peer_chain = sk;
1323 * Inconsistency alert: cert_chain does include the peer's certificate,
1324 * which we don't include in s3_srvr.c
1326 x = sk_X509_value(sk, 0);
1329 * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1332 pkey = X509_get_pubkey(x);
1334 if (pkey == NULL || EVP_PKEY_missing_parameters(pkey)) {
1337 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1338 SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1342 i = ssl_cert_type(x, pkey);
1346 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1347 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1351 exp_idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
1352 if (exp_idx >= 0 && i != exp_idx) {
1354 al = SSL_AD_ILLEGAL_PARAMETER;
1355 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1356 SSL_R_WRONG_CERTIFICATE_TYPE);
1359 s->session->peer_type = i;
1361 X509_free(s->session->peer);
1362 CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1363 s->session->peer = x;
1364 s->session->verify_result = s->verify_result;
1371 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1373 s->state = SSL_ST_ERR;
1375 EVP_PKEY_free(pkey);
1377 sk_X509_pop_free(sk, X509_free);
1381 int ssl3_get_key_exchange(SSL *s)
1383 #ifndef OPENSSL_NO_RSA
1384 unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1387 unsigned char *param, *p;
1389 long i, param_len, n, alg_k, alg_a;
1390 EVP_PKEY *pkey = NULL;
1391 const EVP_MD *md = NULL;
1392 #ifndef OPENSSL_NO_RSA
1395 #ifndef OPENSSL_NO_DH
1398 #ifndef OPENSSL_NO_EC
1399 EC_KEY *ecdh = NULL;
1400 BN_CTX *bn_ctx = NULL;
1401 EC_POINT *srvr_ecpoint = NULL;
1403 int encoded_pt_len = 0;
1406 EVP_MD_CTX_init(&md_ctx);
1409 * use same message size as in ssl3_get_certificate_request() as
1410 * ServerKeyExchange message may be skipped
1412 n = s->method->ssl_get_message(s,
1413 SSL3_ST_CR_KEY_EXCH_A,
1414 SSL3_ST_CR_KEY_EXCH_B,
1415 -1, s->max_cert_list, &ok);
1419 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1421 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1423 * Can't skip server key exchange if this is an ephemeral
1426 if (alg_k & (SSL_kDHE | SSL_kECDHE | SSL_kDHEPSK | SSL_kECDHEPSK)) {
1427 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1428 al = SSL_AD_UNEXPECTED_MESSAGE;
1432 s->s3->tmp.reuse_message = 1;
1436 param = p = (unsigned char *)s->init_msg;
1438 #ifndef OPENSSL_NO_RSA
1439 RSA_free(s->s3->peer_rsa_tmp);
1440 s->s3->peer_rsa_tmp = NULL;
1442 #ifndef OPENSSL_NO_DH
1443 DH_free(s->s3->peer_dh_tmp);
1444 s->s3->peer_dh_tmp = NULL;
1446 #ifndef OPENSSL_NO_EC
1447 EC_KEY_free(s->s3->peer_ecdh_tmp);
1448 s->s3->peer_ecdh_tmp = NULL;
1451 /* Total length of the parameters including the length prefix */
1454 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1456 al = SSL_AD_DECODE_ERROR;
1458 #ifndef OPENSSL_NO_PSK
1459 /* PSK ciphersuites are preceded by an identity hint */
1460 if (alg_k & SSL_PSK) {
1463 if (param_len > n) {
1464 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1470 * Store PSK identity hint for later use, hint is used in
1471 * ssl3_send_client_key_exchange. Assume that the maximum length of
1472 * a PSK identity hint can be as long as the maximum length of a PSK
1475 if (i > PSK_MAX_IDENTITY_LEN) {
1476 al = SSL_AD_HANDSHAKE_FAILURE;
1477 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1480 if (i > n - param_len) {
1481 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1482 SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1487 OPENSSL_free(s->session->psk_identity_hint);
1489 s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1490 if (s->session->psk_identity_hint == NULL) {
1491 al = SSL_AD_HANDSHAKE_FAILURE;
1492 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1496 s->session->psk_identity_hint = NULL;
1503 /* Nothing else to do for plain PSK or RSAPSK */
1504 if (alg_k & (SSL_kPSK | SSL_kRSAPSK)) {
1506 #endif /* !OPENSSL_NO_PSK */
1507 #ifndef OPENSSL_NO_SRP
1508 if (alg_k & SSL_kSRP) {
1510 if (param_len > n) {
1511 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1516 if (i > n - param_len) {
1517 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1522 if ((s->srp_ctx.N = BN_bin2bn(p, i, NULL)) == NULL) {
1523 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1528 if (2 > n - param_len) {
1529 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1536 if (i > n - param_len) {
1537 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1542 if ((s->srp_ctx.g = BN_bin2bn(p, i, NULL)) == NULL) {
1543 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1548 if (1 > n - param_len) {
1549 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1554 i = (unsigned int)(p[0]);
1557 if (i > n - param_len) {
1558 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1563 if ((s->srp_ctx.s = BN_bin2bn(p, i, NULL)) == NULL) {
1564 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1569 if (2 > n - param_len) {
1570 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1577 if (i > n - param_len) {
1578 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1583 if ((s->srp_ctx.B = BN_bin2bn(p, i, NULL)) == NULL) {
1584 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1590 if (!srp_verify_server_param(s, &al)) {
1591 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1595 /* We must check if there is a certificate */
1596 if (alg_a & (SSL_aRSA|SSL_aDSS))
1597 pkey = X509_get_pubkey(s->session->peer);
1599 #endif /* !OPENSSL_NO_SRP */
1600 #ifndef OPENSSL_NO_RSA
1601 if (alg_k & SSL_kRSA) {
1602 /* Temporary RSA keys only allowed in export ciphersuites */
1603 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1604 al = SSL_AD_UNEXPECTED_MESSAGE;
1605 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1608 if ((rsa = RSA_new()) == NULL) {
1609 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1614 if (param_len > n) {
1615 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1620 if (i > n - param_len) {
1621 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1626 if ((rsa->n = BN_bin2bn(p, i, rsa->n)) == NULL) {
1627 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1632 if (2 > n - param_len) {
1633 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1640 if (i > n - param_len) {
1641 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1646 if ((rsa->e = BN_bin2bn(p, i, rsa->e)) == NULL) {
1647 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1653 /* this should be because we are using an export cipher */
1654 if (alg_a & SSL_aRSA)
1655 pkey = X509_get_pubkey(s->session->peer);
1657 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1661 if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1662 al = SSL_AD_UNEXPECTED_MESSAGE;
1663 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1667 s->s3->peer_rsa_tmp = rsa;
1670 #else /* OPENSSL_NO_RSA */
1673 #ifndef OPENSSL_NO_DH
1674 else if (alg_k & (SSL_kDHE | SSL_kDHEPSK)) {
1675 if ((dh = DH_new()) == NULL) {
1676 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1681 if (param_len > n) {
1682 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1687 if (i > n - param_len) {
1688 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1693 if ((dh->p = BN_bin2bn(p, i, NULL)) == NULL) {
1694 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1699 if (BN_is_zero(dh->p)) {
1700 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_VALUE);
1705 if (2 > n - param_len) {
1706 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1713 if (i > n - param_len) {
1714 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1719 if ((dh->g = BN_bin2bn(p, i, NULL)) == NULL) {
1720 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1725 if (BN_is_zero(dh->g)) {
1726 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_VALUE);
1730 if (2 > n - param_len) {
1731 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1738 if (i > n - param_len) {
1739 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1744 if ((dh->pub_key = BN_bin2bn(p, i, NULL)) == NULL) {
1745 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1751 if (BN_is_zero(dh->pub_key)) {
1752 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_VALUE);
1756 if (!ssl_security(s, SSL_SECOP_TMP_DH, DH_security_bits(dh), 0, dh)) {
1757 al = SSL_AD_HANDSHAKE_FAILURE;
1758 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DH_KEY_TOO_SMALL);
1761 if (alg_a & (SSL_aRSA|SSL_aDSS))
1762 pkey = X509_get_pubkey(s->session->peer);
1763 /* else anonymous DH, so no certificate or pkey. */
1765 s->s3->peer_dh_tmp = dh;
1768 #endif /* !OPENSSL_NO_DH */
1770 #ifndef OPENSSL_NO_EC
1771 else if (alg_k & (SSL_kECDHE | SSL_kECDHEPSK)) {
1773 const EC_GROUP *group;
1775 if ((ecdh = EC_KEY_new()) == NULL) {
1776 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1781 * Extract elliptic curve parameters and the server's ephemeral ECDH
1782 * public key. Keep accumulating lengths of various components in
1783 * param_len and make sure it never exceeds n.
1787 * XXX: For now we only support named (not generic) curves and the
1788 * ECParameters in this case is just three bytes. We also need one
1789 * byte for the length of the encoded point
1792 if (param_len > n) {
1793 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1797 * Check curve is one of our preferences, if not server has sent an
1798 * invalid curve. ECParameters is 3 bytes.
1800 if (!tls1_check_curve(s, p, 3)) {
1801 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_CURVE);
1805 if ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0) {
1806 al = SSL_AD_INTERNAL_ERROR;
1807 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1808 SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1812 ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1813 if (ngroup == NULL) {
1814 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1817 if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1818 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1821 EC_GROUP_free(ngroup);
1823 group = EC_KEY_get0_group(ecdh);
1825 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1826 (EC_GROUP_get_degree(group) > 163)) {
1827 al = SSL_AD_EXPORT_RESTRICTION;
1828 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1829 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1835 /* Next, get the encoded ECPoint */
1836 if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1837 ((bn_ctx = BN_CTX_new()) == NULL)) {
1838 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1842 encoded_pt_len = *p; /* length of encoded point */
1845 if ((encoded_pt_len > n - param_len) ||
1846 (EC_POINT_oct2point(group, srvr_ecpoint,
1847 p, encoded_pt_len, bn_ctx) == 0)) {
1848 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1851 param_len += encoded_pt_len;
1854 p += encoded_pt_len;
1857 * The ECC/TLS specification does not mention the use of DSA to sign
1858 * ECParameters in the server key exchange message. We do support RSA
1862 # ifndef OPENSSL_NO_RSA
1863 else if (alg_a & SSL_aRSA)
1864 pkey = X509_get_pubkey(s->session->peer);
1866 # ifndef OPENSSL_NO_EC
1867 else if (alg_a & SSL_aECDSA)
1868 pkey = X509_get_pubkey(s->session->peer);
1870 /* else anonymous ECDH, so no certificate or pkey. */
1871 EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1872 s->s3->peer_ecdh_tmp = ecdh;
1874 BN_CTX_free(bn_ctx);
1876 EC_POINT_free(srvr_ecpoint);
1877 srvr_ecpoint = NULL;
1879 al = SSL_AD_UNEXPECTED_MESSAGE;
1880 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1883 #endif /* !OPENSSL_NO_EC */
1885 /* p points to the next byte, there are 'n' bytes left */
1887 /* if it was signed, check the signature */
1889 if (SSL_USE_SIGALGS(s)) {
1892 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1895 rv = tls12_check_peer_sigalg(&md, s, p, pkey);
1902 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1910 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1915 j = EVP_PKEY_size(pkey);
1918 * Check signature length. If n is 0 then signature is empty
1920 if ((i != n) || (n > j) || (n <= 0)) {
1921 /* wrong packet length */
1922 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1925 #ifndef OPENSSL_NO_RSA
1926 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1932 for (num = 2; num > 0; num--) {
1933 EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1934 EVP_DigestInit_ex(&md_ctx, (num == 2)
1935 ? s->ctx->md5 : s->ctx->sha1, NULL);
1936 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1938 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1940 EVP_DigestUpdate(&md_ctx, param, param_len);
1941 EVP_DigestFinal_ex(&md_ctx, q, &size);
1945 i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1947 al = SSL_AD_DECRYPT_ERROR;
1948 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1953 al = SSL_AD_DECRYPT_ERROR;
1954 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1960 EVP_VerifyInit_ex(&md_ctx, md, NULL);
1961 EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1963 EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1965 EVP_VerifyUpdate(&md_ctx, param, param_len);
1966 if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1968 al = SSL_AD_DECRYPT_ERROR;
1969 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1974 /* aNULL, aSRP or PSK do not need public keys */
1975 if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_PSK)) {
1976 /* Might be wrong key type, check it */
1977 if (ssl3_check_cert_and_algorithm(s))
1978 /* Otherwise this shouldn't happen */
1979 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1982 /* still data left over */
1984 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1988 EVP_PKEY_free(pkey);
1989 EVP_MD_CTX_cleanup(&md_ctx);
1992 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1994 EVP_PKEY_free(pkey);
1995 #ifndef OPENSSL_NO_RSA
1998 #ifndef OPENSSL_NO_DH
2001 #ifndef OPENSSL_NO_EC
2002 BN_CTX_free(bn_ctx);
2003 EC_POINT_free(srvr_ecpoint);
2006 EVP_MD_CTX_cleanup(&md_ctx);
2007 s->state = SSL_ST_ERR;
2011 int ssl3_get_certificate_request(SSL *s)
2014 unsigned long n, nc, l;
2015 unsigned int llen, ctype_num, i;
2016 X509_NAME *xn = NULL;
2017 const unsigned char *p, *q;
2019 STACK_OF(X509_NAME) *ca_sk = NULL;
2021 n = s->method->ssl_get_message(s,
2022 SSL3_ST_CR_CERT_REQ_A,
2023 SSL3_ST_CR_CERT_REQ_B,
2024 -1, s->max_cert_list, &ok);
2029 s->s3->tmp.cert_req = 0;
2031 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
2032 s->s3->tmp.reuse_message = 1;
2034 * If we get here we don't need any cached handshake records as we
2035 * wont be doing client auth.
2037 if (!ssl3_digest_cached_records(s, 0))
2042 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
2043 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2044 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
2048 /* TLS does not like anon-DH with client cert */
2049 if (s->version > SSL3_VERSION) {
2050 if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
2051 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2052 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2053 SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
2058 p = d = (unsigned char *)s->init_msg;
2060 if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
2061 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2065 /* get the certificate types */
2067 OPENSSL_free(s->cert->ctypes);
2068 s->cert->ctypes = NULL;
2069 if (ctype_num > SSL3_CT_NUMBER) {
2070 /* If we exceed static buffer copy all to cert structure */
2071 s->cert->ctypes = OPENSSL_malloc(ctype_num);
2072 if (s->cert->ctypes == NULL) {
2073 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2076 memcpy(s->cert->ctypes, p, ctype_num);
2077 s->cert->ctype_num = (size_t)ctype_num;
2078 ctype_num = SSL3_CT_NUMBER;
2080 for (i = 0; i < ctype_num; i++)
2081 s->s3->tmp.ctype[i] = p[i];
2083 if (SSL_USE_SIGALGS(s)) {
2086 * Check we have enough room for signature algorithms and following
2089 if ((unsigned long)(p - d + llen + 2) > n) {
2090 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2091 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2092 SSL_R_DATA_LENGTH_TOO_LONG);
2095 /* Clear certificate digests and validity flags */
2096 for (i = 0; i < SSL_PKEY_NUM; i++) {
2097 s->s3->tmp.md[i] = NULL;
2098 s->s3->tmp.valid_flags[i] = 0;
2100 if ((llen & 1) || !tls1_save_sigalgs(s, p, llen)) {
2101 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2102 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2103 SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2106 if (!tls1_process_sigalgs(s)) {
2107 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2108 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2114 /* get the CA RDNs */
2117 if ((unsigned long)(p - d + llen) != n) {
2118 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2119 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2123 for (nc = 0; nc < llen;) {
2125 if ((l + nc + 2) > llen) {
2126 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2127 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2133 if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2134 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2135 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2140 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2141 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2142 SSL_R_CA_DN_LENGTH_MISMATCH);
2145 if (!sk_X509_NAME_push(ca_sk, xn)) {
2146 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2154 /* we should setup a certificate to return.... */
2155 s->s3->tmp.cert_req = 1;
2156 s->s3->tmp.ctype_num = ctype_num;
2157 sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2158 s->s3->tmp.ca_names = ca_sk;
2164 s->state = SSL_ST_ERR;
2166 sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2170 static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2172 return (X509_NAME_cmp(*a, *b));
2175 int ssl3_get_new_session_ticket(SSL *s)
2177 int ok, al, ret = 0, ticklen;
2179 const unsigned char *p;
2182 n = s->method->ssl_get_message(s,
2183 SSL3_ST_CR_SESSION_TICKET_A,
2184 SSL3_ST_CR_SESSION_TICKET_B,
2185 SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2191 /* need at least ticket_lifetime_hint + ticket length */
2192 al = SSL_AD_DECODE_ERROR;
2193 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2197 p = d = (unsigned char *)s->init_msg;
2199 if (s->session->session_id_length > 0) {
2200 int i = s->session_ctx->session_cache_mode;
2201 SSL_SESSION *new_sess;
2203 * We reused an existing session, so we need to replace it with a new
2206 if (i & SSL_SESS_CACHE_CLIENT) {
2208 * Remove the old session from the cache
2210 if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2211 if (s->session_ctx->remove_session_cb != NULL)
2212 s->session_ctx->remove_session_cb(s->session_ctx,
2215 /* We carry on if this fails */
2216 SSL_CTX_remove_session(s->session_ctx, s->session);
2220 if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2221 al = SSL_AD_INTERNAL_ERROR;
2222 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2226 SSL_SESSION_free(s->session);
2227 s->session = new_sess;
2230 n2l(p, s->session->tlsext_tick_lifetime_hint);
2232 /* ticket_lifetime_hint + ticket_length + ticket */
2233 if (ticklen + 6 != n) {
2234 al = SSL_AD_DECODE_ERROR;
2235 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2238 OPENSSL_free(s->session->tlsext_tick);
2239 s->session->tlsext_ticklen = 0;
2240 s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2241 if (!s->session->tlsext_tick) {
2242 SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2245 memcpy(s->session->tlsext_tick, p, ticklen);
2246 s->session->tlsext_ticklen = ticklen;
2248 * There are two ways to detect a resumed ticket session. One is to set
2249 * an appropriate session ID and then the server must return a match in
2250 * ServerHello. This allows the normal client session ID matching to work
2251 * and we know much earlier that the ticket has been accepted. The
2252 * other way is to set zero length session ID when the ticket is
2253 * presented and rely on the handshake to determine session resumption.
2254 * We choose the former approach because this fits in with assumptions
2255 * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2256 * SHA256 is disabled) hash of the ticket.
2258 EVP_Digest(p, ticklen,
2259 s->session->session_id, &s->session->session_id_length,
2260 EVP_sha256(), NULL);
2264 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2266 s->state = SSL_ST_ERR;
2270 int ssl3_get_cert_status(SSL *s)
2273 unsigned long resplen, n;
2274 const unsigned char *p;
2276 n = s->method->ssl_get_message(s,
2277 SSL3_ST_CR_CERT_STATUS_A,
2278 SSL3_ST_CR_CERT_STATUS_B,
2279 SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2284 /* need at least status type + length */
2285 al = SSL_AD_DECODE_ERROR;
2286 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2289 p = (unsigned char *)s->init_msg;
2290 if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2291 al = SSL_AD_DECODE_ERROR;
2292 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2296 if (resplen + 4 != n) {
2297 al = SSL_AD_DECODE_ERROR;
2298 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2301 OPENSSL_free(s->tlsext_ocsp_resp);
2302 s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2303 if (!s->tlsext_ocsp_resp) {
2304 al = SSL_AD_INTERNAL_ERROR;
2305 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2308 s->tlsext_ocsp_resplen = resplen;
2309 if (s->ctx->tlsext_status_cb) {
2311 ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2313 al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2314 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2318 al = SSL_AD_INTERNAL_ERROR;
2319 SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2325 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2326 s->state = SSL_ST_ERR;
2330 int ssl3_get_server_done(SSL *s)
2335 /* Second to last param should be very small, like 0 :-) */
2336 n = s->method->ssl_get_message(s,
2337 SSL3_ST_CR_SRVR_DONE_A,
2338 SSL3_ST_CR_SRVR_DONE_B,
2339 SSL3_MT_SERVER_DONE, 30, &ok);
2344 /* should contain no data */
2345 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2346 SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2347 s->state = SSL_ST_ERR;
2354 int ssl3_send_client_key_exchange(SSL *s)
2358 #ifndef OPENSSL_NO_PSK
2359 size_t pskhdrlen = 0;
2361 unsigned long alg_k;
2362 #ifndef OPENSSL_NO_RSA
2364 EVP_PKEY *pkey = NULL;
2366 #ifndef OPENSSL_NO_EC
2367 EC_KEY *clnt_ecdh = NULL;
2368 const EC_POINT *srvr_ecpoint = NULL;
2369 EVP_PKEY *srvr_pub_pkey = NULL;
2370 unsigned char *encodedPoint = NULL;
2371 int encoded_pt_len = 0;
2372 BN_CTX *bn_ctx = NULL;
2374 unsigned char *pms = NULL;
2376 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2378 if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2379 p = ssl_handshake_start(s);
2382 #ifndef OPENSSL_NO_PSK
2383 if (alg_k & SSL_PSK) {
2386 * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2387 * \0-terminated identity. The last byte is for us for simulating
2390 char identity[PSK_MAX_IDENTITY_LEN + 1];
2392 unsigned char psk[PSK_MAX_PSK_LEN];
2395 if (s->psk_client_callback == NULL) {
2396 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2397 SSL_R_PSK_NO_CLIENT_CB);
2401 memset(identity, 0, sizeof(identity));
2403 psklen = s->psk_client_callback(s, s->session->psk_identity_hint,
2404 identity, sizeof(identity) - 1,
2407 if (psklen > PSK_MAX_PSK_LEN) {
2408 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2409 ERR_R_INTERNAL_ERROR);
2411 } else if (psklen == 0) {
2412 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2413 SSL_R_PSK_IDENTITY_NOT_FOUND);
2417 OPENSSL_free(s->s3->tmp.psk);
2418 s->s3->tmp.psk = BUF_memdup(psk, psklen);
2419 OPENSSL_cleanse(psk, psklen);
2421 if (s->s3->tmp.psk == NULL) {
2422 OPENSSL_cleanse(identity, sizeof(identity));
2426 s->s3->tmp.psklen = psklen;
2428 identitylen = strlen(identity);
2429 if (identitylen > PSK_MAX_IDENTITY_LEN) {
2430 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2431 ERR_R_INTERNAL_ERROR);
2434 OPENSSL_free(s->session->psk_identity);
2435 s->session->psk_identity = BUF_strdup(identity);
2436 if (s->session->psk_identity == NULL) {
2437 OPENSSL_cleanse(identity, sizeof(identity));
2441 s2n(identitylen, p);
2442 memcpy(p, identity, identitylen);
2443 pskhdrlen = 2 + identitylen;
2447 OPENSSL_cleanse(identity, sizeof(identity));
2449 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2453 if (alg_k & SSL_kPSK) {
2458 /* Fool emacs indentation */
2461 #ifndef OPENSSL_NO_RSA
2462 else if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
2464 pmslen = SSL_MAX_MASTER_KEY_LENGTH;
2465 pms = OPENSSL_malloc(pmslen);
2469 if (s->session->peer == NULL) {
2471 * We should always have a server certificate with SSL_kRSA.
2473 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2474 ERR_R_INTERNAL_ERROR);
2478 if (s->s3->peer_rsa_tmp != NULL)
2479 rsa = s->s3->peer_rsa_tmp;
2481 pkey = X509_get_pubkey(s->session->peer);
2482 if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2483 || (pkey->pkey.rsa == NULL)) {
2484 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2485 ERR_R_INTERNAL_ERROR);
2488 rsa = pkey->pkey.rsa;
2489 EVP_PKEY_free(pkey);
2492 pms[0] = s->client_version >> 8;
2493 pms[1] = s->client_version & 0xff;
2494 if (RAND_bytes(pms + 2, pmslen - 2) <= 0)
2498 /* Fix buf for TLS and beyond */
2499 if (s->version > SSL3_VERSION)
2501 n = RSA_public_encrypt(pmslen, pms, p, rsa, RSA_PKCS1_PADDING);
2503 if (s->options & SSL_OP_PKCS1_CHECK_1)
2505 if (s->options & SSL_OP_PKCS1_CHECK_2)
2509 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2510 SSL_R_BAD_RSA_ENCRYPT);
2514 /* Fix buf for TLS and beyond */
2515 if (s->version > SSL3_VERSION) {
2521 #ifndef OPENSSL_NO_DH
2522 else if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd | SSL_kDHEPSK)) {
2523 DH *dh_srvr, *dh_clnt;
2524 if (s->s3->peer_dh_tmp != NULL)
2525 dh_srvr = s->s3->peer_dh_tmp;
2527 /* we get them from the cert */
2528 EVP_PKEY *spkey = NULL;
2530 spkey = X509_get_pubkey(s->session->peer);
2532 dh_srvr = EVP_PKEY_get1_DH(spkey);
2533 EVP_PKEY_free(spkey);
2535 if (dh_srvr == NULL) {
2536 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2537 ERR_R_INTERNAL_ERROR);
2541 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
2542 /* Use client certificate key */
2543 EVP_PKEY *clkey = s->cert->key->privatekey;
2546 dh_clnt = EVP_PKEY_get1_DH(clkey);
2547 if (dh_clnt == NULL) {
2548 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2549 ERR_R_INTERNAL_ERROR);
2553 /* generate a new random key */
2554 if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2555 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2558 if (!DH_generate_key(dh_clnt)) {
2559 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2565 pmslen = DH_size(dh_clnt);
2566 pms = OPENSSL_malloc(pmslen);
2571 * use the 'p' output buffer for the DH key, but make sure to
2572 * clear it out afterwards
2575 n = DH_compute_key(pms, dh_srvr->pub_key, dh_clnt);
2576 if (s->s3->peer_dh_tmp == NULL)
2580 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2586 if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY)
2589 /* send off the data */
2590 n = BN_num_bytes(dh_clnt->pub_key);
2592 BN_bn2bin(dh_clnt->pub_key, p);
2600 #ifndef OPENSSL_NO_EC
2601 else if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe | SSL_kECDHEPSK)) {
2602 const EC_GROUP *srvr_group = NULL;
2604 int ecdh_clnt_cert = 0;
2607 * Did we send out the client's ECDH share for use in premaster
2608 * computation as part of client certificate? If so, set
2609 * ecdh_clnt_cert to 1.
2611 if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2613 * XXX: For now, we do not support client
2614 * authentication using ECDH certificates.
2615 * To add such support, one needs to add
2616 * code that checks for appropriate
2617 * conditions and sets ecdh_clnt_cert to 1.
2618 * For example, the cert have an ECC
2619 * key on the same curve as the server's
2620 * and the key should be authorized for
2623 * One also needs to add code in ssl3_connect
2624 * to skip sending the certificate verify
2627 * if ((s->cert->key->privatekey != NULL) &&
2628 * (s->cert->key->privatekey->type ==
2629 * EVP_PKEY_EC) && ...)
2630 * ecdh_clnt_cert = 1;
2634 if (s->s3->peer_ecdh_tmp != NULL) {
2635 tkey = s->s3->peer_ecdh_tmp;
2637 /* Get the Server Public Key from Cert */
2638 srvr_pub_pkey = X509_get_pubkey(s->session->peer);
2639 if ((srvr_pub_pkey == NULL)
2640 || (srvr_pub_pkey->type != EVP_PKEY_EC)
2641 || (srvr_pub_pkey->pkey.ec == NULL)) {
2642 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2643 ERR_R_INTERNAL_ERROR);
2647 tkey = srvr_pub_pkey->pkey.ec;
2650 srvr_group = EC_KEY_get0_group(tkey);
2651 srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2653 if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2654 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2655 ERR_R_INTERNAL_ERROR);
2659 if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2660 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2661 ERR_R_MALLOC_FAILURE);
2665 if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2666 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2669 if (ecdh_clnt_cert) {
2671 * Reuse key info from our certificate We only need our
2672 * private key to perform the ECDH computation.
2674 const BIGNUM *priv_key;
2675 tkey = s->cert->key->privatekey->pkey.ec;
2676 priv_key = EC_KEY_get0_private_key(tkey);
2677 if (priv_key == NULL) {
2678 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2679 ERR_R_MALLOC_FAILURE);
2682 if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2683 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2687 /* Generate a new ECDH key pair */
2688 if (!(EC_KEY_generate_key(clnt_ecdh))) {
2689 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2696 * use the 'p' output buffer for the ECDH key, but make sure to
2697 * clear it out afterwards
2700 field_size = EC_GROUP_get_degree(srvr_group);
2701 if (field_size <= 0) {
2702 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2705 pmslen = (field_size + 7) / 8;
2706 pms = OPENSSL_malloc(pmslen);
2709 n = ECDH_compute_key(pms, pmslen, srvr_ecpoint, clnt_ecdh, NULL);
2710 if (n <= 0 || pmslen != (size_t)n) {
2711 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2715 if (ecdh_clnt_cert) {
2716 /* Send empty client key exch message */
2720 * First check the size of encoding and allocate memory
2724 EC_POINT_point2oct(srvr_group,
2725 EC_KEY_get0_public_key(clnt_ecdh),
2726 POINT_CONVERSION_UNCOMPRESSED,
2729 encodedPoint = (unsigned char *)
2730 OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2731 bn_ctx = BN_CTX_new();
2732 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2733 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2734 ERR_R_MALLOC_FAILURE);
2738 /* Encode the public key */
2739 n = EC_POINT_point2oct(srvr_group,
2740 EC_KEY_get0_public_key(clnt_ecdh),
2741 POINT_CONVERSION_UNCOMPRESSED,
2742 encodedPoint, encoded_pt_len, bn_ctx);
2744 *p = n; /* length of encoded point */
2745 /* Encoded point will be copied here */
2747 /* copy the point */
2748 memcpy(p, encodedPoint, n);
2749 /* increment n to account for length field */
2753 /* Free allocated memory */
2754 BN_CTX_free(bn_ctx);
2755 OPENSSL_free(encodedPoint);
2756 EC_KEY_free(clnt_ecdh);
2757 EVP_PKEY_free(srvr_pub_pkey);
2759 #endif /* !OPENSSL_NO_EC */
2760 else if (alg_k & SSL_kGOST) {
2761 /* GOST key exchange message creation */
2762 EVP_PKEY_CTX *pkey_ctx;
2765 unsigned int md_len;
2766 unsigned char shared_ukm[32], tmp[256];
2767 EVP_MD_CTX *ukm_hash;
2771 pms = OPENSSL_malloc(pmslen);
2776 * Get server sertificate PKEY and create ctx from it
2778 peer_cert = s->session->peer;
2780 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2781 SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2785 pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2786 X509_get_pubkey(peer_cert), NULL);
2788 * If we have send a certificate, and certificate key
2790 * * parameters match those of server certificate, use
2791 * certificate key for key exchange
2794 /* Otherwise, generate ephemeral key pair */
2796 EVP_PKEY_encrypt_init(pkey_ctx);
2797 /* Generate session key */
2798 if (RAND_bytes(pms, pmslen) <= 0) {
2799 EVP_PKEY_CTX_free(pkey_ctx);
2800 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2801 ERR_R_INTERNAL_ERROR);
2805 * If we have client certificate, use its secret as peer key
2807 if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2808 if (EVP_PKEY_derive_set_peer
2809 (pkey_ctx, s->cert->key->privatekey) <= 0) {
2811 * If there was an error - just ignore it. Ephemeral key
2818 * Compute shared IV and store it in algorithm-specific context
2821 ukm_hash = EVP_MD_CTX_create();
2822 EVP_DigestInit(ukm_hash,
2823 EVP_get_digestbynid(NID_id_GostR3411_94));
2824 EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2826 EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2828 EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2829 EVP_MD_CTX_destroy(ukm_hash);
2830 if (EVP_PKEY_CTX_ctrl
2831 (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2833 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2837 /* Make GOST keytransport blob message */
2839 * Encapsulate it into sequence
2841 *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2843 if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, pms, pmslen) < 0) {
2844 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2848 if (msglen >= 0x80) {
2850 *(p++) = msglen & 0xff;
2853 *(p++) = msglen & 0xff;
2856 memcpy(p, tmp, msglen);
2857 /* Check if pubkey from client certificate was used */
2858 if (EVP_PKEY_CTX_ctrl
2859 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2860 /* Set flag "skip certificate verify" */
2861 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2863 EVP_PKEY_CTX_free(pkey_ctx);
2864 EVP_PKEY_free(pub_key);
2867 #ifndef OPENSSL_NO_SRP
2868 else if (alg_k & SSL_kSRP) {
2869 if (s->srp_ctx.A != NULL) {
2870 /* send off the data */
2871 n = BN_num_bytes(s->srp_ctx.A);
2873 BN_bn2bin(s->srp_ctx.A, p);
2876 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2877 ERR_R_INTERNAL_ERROR);
2880 OPENSSL_free(s->session->srp_username);
2881 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2882 if (s->session->srp_username == NULL) {
2883 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2884 ERR_R_MALLOC_FAILURE);
2890 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2891 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2895 #ifndef OPENSSL_NO_PSK
2899 if (!ssl_set_handshake_header(s, SSL3_MT_CLIENT_KEY_EXCHANGE, n)) {
2900 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2901 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2905 s->state = SSL3_ST_CW_KEY_EXCH_B;
2908 /* SSL3_ST_CW_KEY_EXCH_B */
2909 n = ssl_do_write(s);
2910 #ifndef OPENSSL_NO_SRP
2912 if (alg_k & SSL_kSRP) {
2914 * If everything written generate master key: no need to save PMS as
2915 * srp_generate_client_master_secret generates it internally.
2918 if (!srp_generate_client_master_secret(s)) {
2919 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2920 ERR_R_INTERNAL_ERROR);
2926 /* If we haven't written everything save PMS */
2928 s->s3->tmp.pms = pms;
2929 s->s3->tmp.pmslen = pmslen;
2931 /* If we don't have a PMS restore */
2933 pms = s->s3->tmp.pms;
2934 pmslen = s->s3->tmp.pmslen;
2936 if (pms == NULL && !(alg_k & SSL_kPSK)) {
2937 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2938 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2941 if (!ssl_generate_master_secret(s, pms, pmslen, 1)) {
2942 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2943 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2949 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
2950 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2952 OPENSSL_clear_free(pms, pmslen);
2953 s->s3->tmp.pms = NULL;
2954 #ifndef OPENSSL_NO_EC
2955 BN_CTX_free(bn_ctx);
2956 OPENSSL_free(encodedPoint);
2957 EC_KEY_free(clnt_ecdh);
2958 EVP_PKEY_free(srvr_pub_pkey);
2960 #ifndef OPENSSL_NO_PSK
2961 OPENSSL_clear_free(s->s3->tmp.psk, s->s3->tmp.psklen);
2962 s->s3->tmp.psk = NULL;
2964 s->state = SSL_ST_ERR;
2968 int ssl3_send_client_verify(SSL *s)
2971 unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
2973 EVP_PKEY_CTX *pctx = NULL;
2979 EVP_MD_CTX_init(&mctx);
2981 if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
2982 p = ssl_handshake_start(s);
2983 pkey = s->cert->key->privatekey;
2984 /* Create context from key and test if sha1 is allowed as digest */
2985 pctx = EVP_PKEY_CTX_new(pkey, NULL);
2986 EVP_PKEY_sign_init(pctx);
2987 if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
2988 if (!SSL_USE_SIGALGS(s))
2989 s->method->ssl3_enc->cert_verify_mac(s,
2992 [MD5_DIGEST_LENGTH]));
2997 * For TLS v1.2 send signature algorithm and signature using agreed
2998 * digest and cached handshake records.
3000 if (SSL_USE_SIGALGS(s)) {
3003 const EVP_MD *md = s->s3->tmp.md[s->cert->key - s->cert->pkeys];
3004 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3005 if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3006 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3011 fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3014 if (!EVP_SignInit_ex(&mctx, md, NULL)
3015 || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3016 || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3017 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3022 /* Digest cached records and discard handshake buffer */
3023 if (!ssl3_digest_cached_records(s, 0))
3026 #ifndef OPENSSL_NO_RSA
3027 if (pkey->type == EVP_PKEY_RSA) {
3028 s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3029 if (RSA_sign(NID_md5_sha1, data,
3030 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3031 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3032 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3039 #ifndef OPENSSL_NO_DSA
3040 if (pkey->type == EVP_PKEY_DSA) {
3041 if (!DSA_sign(pkey->save_type,
3042 &(data[MD5_DIGEST_LENGTH]),
3043 SHA_DIGEST_LENGTH, &(p[2]),
3044 (unsigned int *)&j, pkey->pkey.dsa)) {
3045 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3052 #ifndef OPENSSL_NO_EC
3053 if (pkey->type == EVP_PKEY_EC) {
3054 if (!ECDSA_sign(pkey->save_type,
3055 &(data[MD5_DIGEST_LENGTH]),
3056 SHA_DIGEST_LENGTH, &(p[2]),
3057 (unsigned int *)&j, pkey->pkey.ec)) {
3058 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3065 if (pkey->type == NID_id_GostR3410_2001) {
3066 unsigned char signbuf[64];
3068 size_t sigsize = 64;
3069 s->method->ssl3_enc->cert_verify_mac(s,
3070 NID_id_GostR3411_94, data);
3071 if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3072 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3075 for (i = 63, j = 0; i >= 0; j++, i--) {
3076 p[2 + j] = signbuf[i];
3081 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3084 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_VERIFY, n)) {
3085 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3088 s->state = SSL3_ST_CW_CERT_VRFY_B;
3090 EVP_MD_CTX_cleanup(&mctx);
3091 EVP_PKEY_CTX_free(pctx);
3092 return ssl_do_write(s);
3094 EVP_MD_CTX_cleanup(&mctx);
3095 EVP_PKEY_CTX_free(pctx);
3096 s->state = SSL_ST_ERR;
3101 * Check a certificate can be used for client authentication. Currently check
3102 * cert exists, if we have a suitable digest for TLS 1.2 if static DH client
3103 * certificates can be used and optionally checks suitability for Suite B.
3105 static int ssl3_check_client_certificate(SSL *s)
3107 unsigned long alg_k;
3108 if (!s->cert || !s->cert->key->x509 || !s->cert->key->privatekey)
3110 /* If no suitable signature algorithm can't use certificate */
3111 if (SSL_USE_SIGALGS(s) && !s->s3->tmp.md[s->cert->key - s->cert->pkeys])
3114 * If strict mode check suitability of chain before using it. This also
3115 * adjusts suite B digest if necessary.
3117 if (s->cert->cert_flags & SSL_CERT_FLAGS_CHECK_TLS_STRICT &&
3118 !tls1_check_chain(s, NULL, NULL, NULL, -2))
3120 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3121 /* See if we can use client certificate for fixed DH */
3122 if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3123 int i = s->session->peer_type;
3124 EVP_PKEY *clkey = NULL, *spkey = NULL;
3125 clkey = s->cert->key->privatekey;
3126 /* If client key not DH assume it can be used */
3127 if (EVP_PKEY_id(clkey) != EVP_PKEY_DH)
3130 spkey = X509_get_pubkey(s->session->peer);
3132 /* Compare server and client parameters */
3133 i = EVP_PKEY_cmp_parameters(clkey, spkey);
3134 EVP_PKEY_free(spkey);
3138 s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
3143 int ssl3_send_client_certificate(SSL *s)
3146 EVP_PKEY *pkey = NULL;
3149 if (s->state == SSL3_ST_CW_CERT_A) {
3150 /* Let cert callback update client certificates if required */
3151 if (s->cert->cert_cb) {
3152 i = s->cert->cert_cb(s, s->cert->cert_cb_arg);
3154 s->rwstate = SSL_X509_LOOKUP;
3158 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3159 s->state = SSL_ST_ERR;
3162 s->rwstate = SSL_NOTHING;
3164 if (ssl3_check_client_certificate(s))
3165 s->state = SSL3_ST_CW_CERT_C;
3167 s->state = SSL3_ST_CW_CERT_B;
3170 /* We need to get a client cert */
3171 if (s->state == SSL3_ST_CW_CERT_B) {
3173 * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3174 * return(-1); We then get retied later
3177 i = ssl_do_client_cert_cb(s, &x509, &pkey);
3179 s->rwstate = SSL_X509_LOOKUP;
3182 s->rwstate = SSL_NOTHING;
3183 if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3184 s->state = SSL3_ST_CW_CERT_B;
3185 if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3187 } else if (i == 1) {
3189 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3190 SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3194 EVP_PKEY_free(pkey);
3195 if (i && !ssl3_check_client_certificate(s))
3198 if (s->version == SSL3_VERSION) {
3199 s->s3->tmp.cert_req = 0;
3200 ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3203 s->s3->tmp.cert_req = 2;
3204 if (!ssl3_digest_cached_records(s, 0)) {
3205 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3206 s->state = SSL_ST_ERR;
3212 /* Ok, we have a cert */
3213 s->state = SSL3_ST_CW_CERT_C;
3216 if (s->state == SSL3_ST_CW_CERT_C) {
3217 s->state = SSL3_ST_CW_CERT_D;
3218 if (!ssl3_output_cert_chain(s,
3219 (s->s3->tmp.cert_req ==
3220 2) ? NULL : s->cert->key)) {
3221 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3222 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3223 s->state = SSL_ST_ERR;
3227 /* SSL3_ST_CW_CERT_D */
3228 return ssl_do_write(s);
3231 #define has_bits(i,m) (((i)&(m)) == (m))
3233 int ssl3_check_cert_and_algorithm(SSL *s)
3237 EVP_PKEY *pkey = NULL;
3239 #ifndef OPENSSL_NO_RSA
3242 #ifndef OPENSSL_NO_DH
3245 int al = SSL_AD_HANDSHAKE_FAILURE;
3247 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3248 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3250 /* we don't have a certificate */
3251 if ((alg_a & SSL_aNULL) || (alg_k & SSL_kPSK))
3253 #ifndef OPENSSL_NO_RSA
3254 rsa = s->s3->peer_rsa_tmp;
3256 #ifndef OPENSSL_NO_DH
3257 dh = s->s3->peer_dh_tmp;
3260 /* This is the passed certificate */
3262 idx = s->session->peer_type;
3263 #ifndef OPENSSL_NO_EC
3264 if (idx == SSL_PKEY_ECC) {
3265 if (ssl_check_srvr_ecc_cert_and_alg(s->session->peer, s) == 0) {
3267 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3272 } else if (alg_a & SSL_aECDSA) {
3273 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3274 SSL_R_MISSING_ECDSA_SIGNING_CERT);
3276 } else if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
3277 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_MISSING_ECDH_CERT);
3281 pkey = X509_get_pubkey(s->session->peer);
3282 pkey_bits = EVP_PKEY_bits(pkey);
3283 i = X509_certificate_type(s->session->peer, pkey);
3284 EVP_PKEY_free(pkey);
3286 /* Check that we have a certificate if we require one */
3287 if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3288 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3289 SSL_R_MISSING_RSA_SIGNING_CERT);
3292 #ifndef OPENSSL_NO_DSA
3293 else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3294 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3295 SSL_R_MISSING_DSA_SIGNING_CERT);
3299 #ifndef OPENSSL_NO_RSA
3300 if (alg_k & (SSL_kRSA | SSL_kRSAPSK)) {
3301 if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3302 !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3303 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3304 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3306 } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3307 if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3308 if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3309 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3310 SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3314 /* server key exchange is not allowed. */
3315 al = SSL_AD_INTERNAL_ERROR;
3316 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3323 #ifndef OPENSSL_NO_DH
3324 if ((alg_k & SSL_kDHE) && (dh == NULL)) {
3325 al = SSL_AD_INTERNAL_ERROR;
3326 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3328 } else if ((alg_k & SSL_kDHr) && !SSL_USE_SIGALGS(s) &&
3329 !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3330 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3331 SSL_R_MISSING_DH_RSA_CERT);
3334 # ifndef OPENSSL_NO_DSA
3335 else if ((alg_k & SSL_kDHd) && !SSL_USE_SIGALGS(s) &&
3336 !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3337 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3338 SSL_R_MISSING_DH_DSA_CERT);
3344 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3345 pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3346 #ifndef OPENSSL_NO_RSA
3347 if (alg_k & SSL_kRSA) {
3349 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3350 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3352 } else if (RSA_bits(rsa) >
3353 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3354 /* We have a temporary RSA key but it's too large. */
3355 al = SSL_AD_EXPORT_RESTRICTION;
3356 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3357 SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3362 #ifndef OPENSSL_NO_DH
3363 if (alg_k & SSL_kDHE) {
3365 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3366 /* We have a temporary DH key but it's too large. */
3367 al = SSL_AD_EXPORT_RESTRICTION;
3368 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3369 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3372 } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3373 /* The cert should have had an export DH key. */
3374 al = SSL_AD_EXPORT_RESTRICTION;
3375 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3376 SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3381 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3382 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3388 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3393 * Normally, we can tell if the server is resuming the session from
3394 * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3395 * message after the ServerHello to determine if the server is resuming.
3396 * Therefore, we allow EAP-FAST to peek ahead.
3397 * ssl3_check_change returns 1 if we are resuming from an external
3398 * pre-shared secret, we have a "ticket" and the next server message
3399 * is CCS; and 0 otherwise. It returns -1 upon an error.
3401 static int ssl3_check_change(SSL *s)
3405 if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3406 !s->session->tlsext_tick)
3410 * This function is called when we might get a Certificate message instead,
3411 * so permit appropriate message length.
3412 * We ignore the return value as we're only interested in the message type
3413 * and not its length.
3415 s->method->ssl_get_message(s,
3418 -1, s->max_cert_list, &ok);
3423 s->s3->tmp.reuse_message = 1;
3425 if (s->s3->tmp.message_type == SSL3_MT_CHANGE_CIPHER_SPEC)
3431 #ifndef OPENSSL_NO_NEXTPROTONEG
3432 int ssl3_send_next_proto(SSL *s)
3434 unsigned int len, padding_len;
3437 if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
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->state = SSL3_ST_CW_NEXT_PROTO_B;
3448 s->init_num = 4 + 2 + len + padding_len;
3452 return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3456 int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3459 #ifndef OPENSSL_NO_ENGINE
3460 if (s->ctx->client_cert_engine) {
3461 i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3462 SSL_get_client_CA_list(s),
3463 px509, ppkey, NULL, NULL, NULL);
3468 if (s->ctx->client_cert_cb)
3469 i = s->ctx->client_cert_cb(s, px509, ppkey);
3473 int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk,
3475 int (*put_cb) (const SSL_CIPHER *,
3481 int empty_reneg_info_scsv = !s->renegotiate;
3482 /* Set disabled masks for this session */
3483 ssl_set_client_disabled(s);
3489 put_cb = s->method->put_cipher_by_char;
3491 for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
3492 c = sk_SSL_CIPHER_value(sk, i);
3493 /* Skip disabled ciphers */
3494 if (ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED))
3496 #ifdef OPENSSL_SSL_DEBUG_BROKEN_PROTOCOL
3497 if (c->id == SSL3_CK_SCSV) {
3498 if (!empty_reneg_info_scsv)
3501 empty_reneg_info_scsv = 0;
3508 * If p == q, no ciphers; caller indicates an error. Otherwise, add
3512 if (empty_reneg_info_scsv) {
3513 static SSL_CIPHER scsv = {
3514 0, NULL, SSL3_CK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3516 j = put_cb(&scsv, p);
3518 #ifdef OPENSSL_RI_DEBUG
3520 "TLS_EMPTY_RENEGOTIATION_INFO_SCSV sent by client\n");
3523 if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
3524 static SSL_CIPHER scsv = {
3525 0, NULL, SSL3_CK_FALLBACK_SCSV, 0, 0, 0, 0, 0, 0, 0, 0, 0
3527 j = put_cb(&scsv, p);