1 /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
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
153 #include "ssl_locl.h"
154 #include "internal/constant_time_locl.h"
155 #include <openssl/buffer.h>
156 #include <openssl/rand.h>
157 #include <openssl/objects.h>
158 #include <openssl/evp.h>
159 #include <openssl/hmac.h>
160 #include <openssl/x509.h>
161 #ifndef OPENSSL_NO_DH
162 # include <openssl/dh.h>
164 #include <openssl/bn.h>
165 #include <openssl/md5.h>
167 static STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
168 int num, STACK_OF(SSL_CIPHER) **skp, int sslv2format);
170 #ifndef OPENSSL_NO_SSL3_METHOD
171 static const SSL_METHOD *ssl3_get_server_method(int ver);
173 static const SSL_METHOD *ssl3_get_server_method(int ver)
175 if (ver == SSL3_VERSION)
176 return (SSLv3_server_method());
181 IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
183 ssl_undefined_function, ssl3_get_server_method)
185 #ifndef OPENSSL_NO_SRP
186 static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
188 int ret = SSL_ERROR_NONE;
190 *al = SSL_AD_UNRECOGNIZED_NAME;
192 if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
193 (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
194 if (s->srp_ctx.login == NULL) {
196 * RFC 5054 says SHOULD reject, we do so if There is no srp
200 *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
202 ret = SSL_srp_server_param_with_username(s, al);
209 int ssl3_accept(SSL *s)
212 unsigned long alg_k, Time = (unsigned long)time(NULL);
213 void (*cb) (const SSL *ssl, int type, int val) = NULL;
215 int new_state, state, skip = 0;
217 RAND_add(&Time, sizeof(Time), 0);
221 if (s->info_callback != NULL)
222 cb = s->info_callback;
223 else if (s->ctx->info_callback != NULL)
224 cb = s->ctx->info_callback;
226 /* init things to blank */
228 if (!SSL_in_init(s) || SSL_in_before(s)) {
233 #ifndef OPENSSL_NO_HEARTBEATS
235 * If we're awaiting a HeartbeatResponse, pretend we already got and
236 * don't await it anymore, because Heartbeats don't make sense during
239 if (s->tlsext_hb_pending) {
240 s->tlsext_hb_pending = 0;
249 case SSL_ST_RENEGOTIATE:
251 /* s->state=SSL_ST_ACCEPT; */
255 case SSL_ST_BEFORE | SSL_ST_ACCEPT:
256 case SSL_ST_OK | SSL_ST_ACCEPT:
260 cb(s, SSL_CB_HANDSHAKE_START, 1);
262 if ((s->version >> 8 != 3) && s->version != TLS_ANY_VERSION) {
263 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
264 s->state = SSL_ST_ERR;
268 if (!ssl_security(s, SSL_SECOP_VERSION, 0, s->version, NULL)) {
269 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_VERSION_TOO_LOW);
273 s->type = SSL_ST_ACCEPT;
275 if (s->init_buf == NULL) {
276 if ((buf = BUF_MEM_new()) == NULL) {
278 s->state = SSL_ST_ERR;
281 if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
284 s->state = SSL_ST_ERR;
290 if (!ssl3_setup_buffers(s)) {
292 s->state = SSL_ST_ERR;
297 s->s3->flags &= ~TLS1_FLAGS_SKIP_CERT_VERIFY;
298 s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
300 * Should have been reset by ssl3_get_finished, too.
302 s->s3->change_cipher_spec = 0;
304 if (s->state != SSL_ST_RENEGOTIATE) {
306 * Ok, we now need to push on a buffering BIO so that the
307 * output is sent in a way that TCP likes :-)
309 if (!ssl_init_wbio_buffer(s, 1)) {
311 s->state = SSL_ST_ERR;
315 ssl3_init_finished_mac(s);
316 s->state = SSL3_ST_SR_CLNT_HELLO_A;
317 s->ctx->stats.sess_accept++;
318 } else if (!s->s3->send_connection_binding &&
320 SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
322 * Server attempting to renegotiate with client that doesn't
323 * support secure renegotiation.
325 SSLerr(SSL_F_SSL3_ACCEPT,
326 SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
327 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
329 s->state = SSL_ST_ERR;
333 * s->state == SSL_ST_RENEGOTIATE, we will just send a
336 s->ctx->stats.sess_accept_renegotiate++;
337 s->state = SSL3_ST_SW_HELLO_REQ_A;
341 case SSL3_ST_SW_HELLO_REQ_A:
342 case SSL3_ST_SW_HELLO_REQ_B:
345 ret = ssl3_send_hello_request(s);
348 s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
349 s->state = SSL3_ST_SW_FLUSH;
352 ssl3_init_finished_mac(s);
355 case SSL3_ST_SW_HELLO_REQ_C:
356 s->state = SSL_ST_OK;
359 case SSL3_ST_SR_CLNT_HELLO_A:
360 case SSL3_ST_SR_CLNT_HELLO_B:
361 case SSL3_ST_SR_CLNT_HELLO_C:
363 ret = ssl3_get_client_hello(s);
366 #ifndef OPENSSL_NO_SRP
367 s->state = SSL3_ST_SR_CLNT_HELLO_D;
368 case SSL3_ST_SR_CLNT_HELLO_D:
371 if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
373 * callback indicates firther work to be done
375 s->rwstate = SSL_X509_LOOKUP;
378 if (ret != SSL_ERROR_NONE) {
379 ssl3_send_alert(s, SSL3_AL_FATAL, al);
381 * This is not really an error but the only means to for
382 * a client to detect whether srp is supported.
384 if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
385 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
386 ret = SSL_TLSEXT_ERR_ALERT_FATAL;
388 s->state = SSL_ST_ERR;
395 s->state = SSL3_ST_SW_SRVR_HELLO_A;
399 case SSL3_ST_SW_SRVR_HELLO_A:
400 case SSL3_ST_SW_SRVR_HELLO_B:
401 ret = ssl3_send_server_hello(s);
404 #ifndef OPENSSL_NO_TLSEXT
406 if (s->tlsext_ticket_expected)
407 s->state = SSL3_ST_SW_SESSION_TICKET_A;
409 s->state = SSL3_ST_SW_CHANGE_A;
413 s->state = SSL3_ST_SW_CHANGE_A;
416 s->state = SSL3_ST_SW_CERT_A;
420 case SSL3_ST_SW_CERT_A:
421 case SSL3_ST_SW_CERT_B:
422 /* Check if it is anon DH or anon ECDH, */
423 /* normal PSK or SRP */
426 new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
427 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
428 ret = ssl3_send_server_certificate(s);
431 #ifndef OPENSSL_NO_TLSEXT
432 if (s->tlsext_status_expected)
433 s->state = SSL3_ST_SW_CERT_STATUS_A;
435 s->state = SSL3_ST_SW_KEY_EXCH_A;
438 s->state = SSL3_ST_SW_KEY_EXCH_A;
444 s->state = SSL3_ST_SW_KEY_EXCH_A;
449 case SSL3_ST_SW_KEY_EXCH_A:
450 case SSL3_ST_SW_KEY_EXCH_B:
451 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
454 * clear this, it may get reset by
455 * send_server_key_exchange
457 s->s3->tmp.use_rsa_tmp = 0;
460 * only send if a DH key exchange, fortezza or RSA but we have a
461 * sign only certificate PSK: may send PSK identity hints For
462 * ECC ciphersuites, we send a serverKeyExchange message only if
463 * the cipher suite is either ECDH-anon or ECDHE. In other cases,
464 * the server certificate contains the server's public key for
469 * PSK: send ServerKeyExchange if PSK identity hint if
472 #ifndef OPENSSL_NO_PSK
473 || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
475 #ifndef OPENSSL_NO_SRP
476 /* SRP: send ServerKeyExchange */
477 || (alg_k & SSL_kSRP)
479 || (alg_k & SSL_kDHE)
480 || (alg_k & SSL_kECDHE)
481 || ((alg_k & SSL_kRSA)
482 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
483 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
484 && EVP_PKEY_size(s->cert->pkeys
485 [SSL_PKEY_RSA_ENC].privatekey) *
486 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
491 ret = ssl3_send_server_key_exchange(s);
497 s->state = SSL3_ST_SW_CERT_REQ_A;
501 case SSL3_ST_SW_CERT_REQ_A:
502 case SSL3_ST_SW_CERT_REQ_B:
503 if ( /* don't request cert unless asked for it: */
504 !(s->verify_mode & SSL_VERIFY_PEER) ||
506 * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
507 * during re-negotiation:
509 ((s->session->peer != NULL) &&
510 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
512 * never request cert in anonymous ciphersuites (see
513 * section "Certificate request" in SSL 3 drafts and in
516 ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
518 * ... except when the application insists on
519 * verification (against the specs, but s3_clnt.c accepts
522 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
523 /* don't request certificate for SRP auth */
524 (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
526 * With normal PSK Certificates and Certificate Requests
529 || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
530 /* no cert request */
532 s->s3->tmp.cert_request = 0;
533 s->state = SSL3_ST_SW_SRVR_DONE_A;
534 if (s->s3->handshake_buffer) {
535 if (!ssl3_digest_cached_records(s)) {
536 s->state = SSL_ST_ERR;
541 s->s3->tmp.cert_request = 1;
542 ret = ssl3_send_certificate_request(s);
545 s->state = SSL3_ST_SW_SRVR_DONE_A;
550 case SSL3_ST_SW_SRVR_DONE_A:
551 case SSL3_ST_SW_SRVR_DONE_B:
552 ret = ssl3_send_server_done(s);
555 s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
556 s->state = SSL3_ST_SW_FLUSH;
560 case SSL3_ST_SW_FLUSH:
563 * This code originally checked to see if any data was pending
564 * using BIO_CTRL_INFO and then flushed. This caused problems as
565 * documented in PR#1939. The proposed fix doesn't completely
566 * resolve this issue as buggy implementations of
567 * BIO_CTRL_PENDING still exist. So instead we just flush
571 s->rwstate = SSL_WRITING;
572 if (BIO_flush(s->wbio) <= 0) {
576 s->rwstate = SSL_NOTHING;
578 s->state = s->s3->tmp.next_state;
581 case SSL3_ST_SR_CERT_A:
582 case SSL3_ST_SR_CERT_B:
583 if (s->s3->tmp.cert_request) {
584 ret = ssl3_get_client_certificate(s);
589 s->state = SSL3_ST_SR_KEY_EXCH_A;
592 case SSL3_ST_SR_KEY_EXCH_A:
593 case SSL3_ST_SR_KEY_EXCH_B:
594 ret = ssl3_get_client_key_exchange(s);
599 * For the ECDH ciphersuites when the client sends its ECDH
600 * pub key in a certificate, the CertificateVerify message is
601 * not sent. Also for GOST ciphersuites when the client uses
602 * its key from the certificate for key exchange.
604 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
605 s->state = SSL3_ST_SR_FINISHED_A;
607 if (s->s3->next_proto_neg_seen)
608 s->state = SSL3_ST_SR_NEXT_PROTO_A;
610 s->state = SSL3_ST_SR_FINISHED_A;
613 } else if (SSL_USE_SIGALGS(s)) {
614 s->state = SSL3_ST_SR_CERT_VRFY_A;
616 if (!s->session->peer)
618 if (!s->s3->handshake_buffer) {
619 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
620 s->state = SSL_ST_ERR;
624 * For sigalgs freeze the handshake buffer. If we support
625 * extms we've done this already.
627 if (!(s->s3->flags & SSL_SESS_FLAG_EXTMS)) {
628 s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
629 if (!ssl3_digest_cached_records(s)) {
630 s->state = SSL_ST_ERR;
638 s->state = SSL3_ST_SR_CERT_VRFY_A;
642 * We need to get hashes here so if there is a client cert,
643 * it can be verified FIXME - digest processing for
644 * CertificateVerify should be generalized. But it is next
647 if (s->s3->handshake_buffer) {
648 if (!ssl3_digest_cached_records(s)) {
649 s->state = SSL_ST_ERR;
653 for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
654 if (s->s3->handshake_dgst[dgst_num]) {
657 s->method->ssl3_enc->cert_verify_mac(s,
666 EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
668 s->state = SSL_ST_ERR;
677 case SSL3_ST_SR_CERT_VRFY_A:
678 case SSL3_ST_SR_CERT_VRFY_B:
679 ret = ssl3_get_cert_verify(s);
683 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
684 s->state = SSL3_ST_SR_FINISHED_A;
686 if (s->s3->next_proto_neg_seen)
687 s->state = SSL3_ST_SR_NEXT_PROTO_A;
689 s->state = SSL3_ST_SR_FINISHED_A;
694 #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
695 case SSL3_ST_SR_NEXT_PROTO_A:
696 case SSL3_ST_SR_NEXT_PROTO_B:
698 * Enable CCS for NPN. Receiving a CCS clears the flag, so make
699 * sure not to re-enable it to ban duplicates. This *should* be the
700 * first time we have received one - but we check anyway to be
702 * s->s3->change_cipher_spec is set when a CCS is
703 * processed in s3_pkt.c, and remains set until
704 * the client's Finished message is read.
706 if (!s->s3->change_cipher_spec)
707 s->s3->flags |= SSL3_FLAGS_CCS_OK;
709 ret = ssl3_get_next_proto(s);
713 s->state = SSL3_ST_SR_FINISHED_A;
717 case SSL3_ST_SR_FINISHED_A:
718 case SSL3_ST_SR_FINISHED_B:
720 * Enable CCS for handshakes without NPN. In NPN the CCS flag has
721 * already been set. Receiving a CCS clears the flag, so make
722 * sure not to re-enable it to ban duplicates.
723 * s->s3->change_cipher_spec is set when a CCS is
724 * processed in s3_pkt.c, and remains set until
725 * the client's Finished message is read.
727 if (!s->s3->change_cipher_spec)
728 s->s3->flags |= SSL3_FLAGS_CCS_OK;
729 ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
730 SSL3_ST_SR_FINISHED_B);
734 s->state = SSL_ST_OK;
735 #ifndef OPENSSL_NO_TLSEXT
736 else if (s->tlsext_ticket_expected)
737 s->state = SSL3_ST_SW_SESSION_TICKET_A;
740 s->state = SSL3_ST_SW_CHANGE_A;
744 #ifndef OPENSSL_NO_TLSEXT
745 case SSL3_ST_SW_SESSION_TICKET_A:
746 case SSL3_ST_SW_SESSION_TICKET_B:
747 ret = ssl3_send_newsession_ticket(s);
750 s->state = SSL3_ST_SW_CHANGE_A;
754 case SSL3_ST_SW_CERT_STATUS_A:
755 case SSL3_ST_SW_CERT_STATUS_B:
756 ret = ssl3_send_cert_status(s);
759 s->state = SSL3_ST_SW_KEY_EXCH_A;
765 case SSL3_ST_SW_CHANGE_A:
766 case SSL3_ST_SW_CHANGE_B:
768 s->session->cipher = s->s3->tmp.new_cipher;
769 if (!s->method->ssl3_enc->setup_key_block(s)) {
771 s->state = SSL_ST_ERR;
775 ret = ssl3_send_change_cipher_spec(s,
777 SSL3_ST_SW_CHANGE_B);
781 s->state = SSL3_ST_SW_FINISHED_A;
784 if (!s->method->ssl3_enc->change_cipher_state(s,
785 SSL3_CHANGE_CIPHER_SERVER_WRITE))
788 s->state = SSL_ST_ERR;
794 case SSL3_ST_SW_FINISHED_A:
795 case SSL3_ST_SW_FINISHED_B:
796 ret = ssl3_send_finished(s,
797 SSL3_ST_SW_FINISHED_A,
798 SSL3_ST_SW_FINISHED_B,
800 ssl3_enc->server_finished_label,
802 ssl3_enc->server_finished_label_len);
805 s->state = SSL3_ST_SW_FLUSH;
807 #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
808 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
810 if (s->s3->next_proto_neg_seen) {
811 s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
813 s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
816 s->s3->tmp.next_state = SSL_ST_OK;
821 /* clean a few things up */
822 ssl3_cleanup_key_block(s);
824 BUF_MEM_free(s->init_buf);
827 /* remove buffering on output */
828 ssl_free_wbio_buffer(s);
832 if (s->renegotiate == 2) { /* skipped if we just sent a
837 ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
839 s->ctx->stats.sess_accept_good++;
841 s->handshake_func = ssl3_accept;
844 cb(s, SSL_CB_HANDSHAKE_DONE, 1);
853 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
859 if (!s->s3->tmp.reuse_message && !skip) {
861 if ((ret = BIO_flush(s->wbio)) <= 0)
865 if ((cb != NULL) && (s->state != state)) {
866 new_state = s->state;
868 cb(s, SSL_CB_ACCEPT_LOOP, 1);
869 s->state = new_state;
875 /* BIO_flush(s->wbio); */
879 cb(s, SSL_CB_ACCEPT_EXIT, ret);
883 int ssl3_send_hello_request(SSL *s)
886 if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
887 if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
888 SSLerr(SSL_F_SSL3_SEND_HELLO_REQUEST, ERR_R_INTERNAL_ERROR);
891 s->state = SSL3_ST_SW_HELLO_REQ_B;
894 /* SSL3_ST_SW_HELLO_REQ_B */
895 return ssl_do_write(s);
898 int ssl3_get_client_hello(SSL *s)
900 int i, j, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1;
901 unsigned int cookie_len;
904 unsigned char *p, *d;
906 #ifndef OPENSSL_NO_COMP
907 unsigned char *q = NULL;
908 SSL_COMP *comp = NULL;
910 STACK_OF(SSL_CIPHER) *ciphers = NULL;
913 if (s->state == SSL3_ST_SR_CLNT_HELLO_C && !s->first_packet)
917 * We do this so that we will respond with our native type. If we are
918 * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
919 * switching should be handled by a different method. If we are SSLv3, we
920 * will respond with SSLv3, even if prompted with TLSv1.
922 if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
923 s->state = SSL3_ST_SR_CLNT_HELLO_B;
926 n = s->method->ssl_get_message(s,
927 SSL3_ST_SR_CLNT_HELLO_B,
928 SSL3_ST_SR_CLNT_HELLO_C,
929 SSL3_MT_CLIENT_HELLO,
930 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
935 d = p = (unsigned char *)s->init_msg;
937 /* First lets get s->client_version set correctly */
938 if (!s->read_hash && !s->enc_read_ctx
939 && RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
940 if (n < MIN_SSL2_RECORD_LEN) {
941 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
942 al = SSL_AD_DECODE_ERROR;
946 * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
947 * header is sent directly on the wire, not wrapped as a TLS
948 * record. Our record layer just processes the message length and passes
949 * the rest right through. Its format is:
951 * 0-1 msg_length - decoded by the record layer
952 * 2 msg_type - s->init_msg points here
954 * 5-6 cipher_spec_length
955 * 7-8 session_id_length
956 * 9-10 challenge_length
960 if (p[0] != SSL2_MT_CLIENT_HELLO) {
962 * Should never happen. We should have tested this in the record
963 * layer in order to have determined that this is a SSLv2 record
966 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
970 if ((p[1] == 0x00) && (p[2] == 0x02)) {
971 /* This is real SSLv2. We don't support it. */
972 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
974 } else if (p[1] == SSL3_VERSION_MAJOR) {
976 s->client_version = (((int)p[1]) << 8) | (int)p[2];
978 /* No idea what protocol this is */
979 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
984 * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
985 * for session id length
987 if (n < 2 + SSL3_RANDOM_SIZE + 1) {
988 al = SSL_AD_DECODE_ERROR;
989 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
994 * use version from inside client hello, not from record header (may
995 * differ: see RFC 2246, Appendix E, second paragraph)
997 s->client_version = (((int)p[0]) << 8) | (int)p[1];
1000 /* Do SSL/TLS version negotiation if applicable */
1001 if (!SSL_IS_DTLS(s)) {
1002 if (s->version != TLS_ANY_VERSION) {
1003 if (s->client_version >= s->version
1004 && (((s->client_version >> 8) & 0xff) == SSL3_VERSION_MAJOR)) {
1007 } else if (((s->client_version >> 8) & 0xff) == SSL3_VERSION_MAJOR) {
1008 switch(s->client_version) {
1010 case TLS1_2_VERSION:
1011 if(!(s->options & SSL_OP_NO_TLSv1_2)) {
1012 s->version = TLS1_2_VERSION;
1013 s->method = TLSv1_2_server_method();
1017 /* Deliberately fall through */
1018 case TLS1_1_VERSION:
1019 if(!(s->options & SSL_OP_NO_TLSv1_1)) {
1020 s->version = TLS1_1_VERSION;
1021 s->method = TLSv1_1_server_method();
1025 /* Deliberately fall through */
1027 if(!(s->options & SSL_OP_NO_TLSv1)) {
1028 s->version = TLS1_VERSION;
1029 s->method = TLSv1_server_method();
1033 /* Deliberately fall through */
1035 if(!(s->options & SSL_OP_NO_SSLv3)) {
1036 s->version = SSL3_VERSION;
1037 s->method = SSLv3_server_method();
1043 } else if (((s->client_version >> 8) & 0xff) == DTLS1_VERSION_MAJOR &&
1044 (s->client_version <= s->version
1045 || s->method->version == DTLS_ANY_VERSION)) {
1047 * For DTLS we just check versions are potentially compatible. Version
1048 * negotiation comes later.
1054 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
1055 if ((!s->enc_write_ctx && !s->write_hash)) {
1057 * similar to ssl3_get_record, send alert using remote version
1060 s->version = s->client_version;
1062 al = SSL_AD_PROTOCOL_VERSION;
1066 if (RECORD_LAYER_is_sslv2_record(&s->rlayer)) {
1068 * Handle an SSLv2 backwards compatible ClientHello
1069 * Note, this is only for SSLv3+ using the backward compatible format.
1070 * Real SSLv2 is not supported, and is rejected above.
1072 unsigned int csl, sil, cl;
1079 if (csl + sil + cl + MIN_SSL2_RECORD_LEN != (unsigned int) n) {
1080 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_RECORD_LENGTH_MISMATCH);
1081 al = SSL_AD_DECODE_ERROR;
1086 /* we need at least one cipher */
1087 al = SSL_AD_ILLEGAL_PARAMETER;
1088 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1092 if (ssl_bytes_to_cipher_list(s, p, csl, &(ciphers), 1) == NULL) {
1097 * Ignore any session id. We don't allow resumption in a backwards
1098 * compatible ClientHello
1102 if (!ssl_get_new_session(s, 1))
1105 /* Load the client random */
1106 i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl;
1107 memset(s->s3->client_random, 0, SSL3_RANDOM_SIZE);
1108 memcpy(s->s3->client_random, &(p[csl + sil]), i);
1110 /* Set p to end of packet to ensure we don't look for extensions */
1113 /* No compression, so set i to 0 */
1116 /* If we get here we've got SSLv3+ in an SSLv3+ record */
1121 * If we require cookies and this ClientHello doesn't contain one, just
1122 * return since we do not want to allocate any memory yet. So check
1125 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1126 unsigned int session_length, cookie_length;
1128 session_length = *(p + SSL3_RANDOM_SIZE);
1130 if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
1131 al = SSL_AD_DECODE_ERROR;
1132 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1135 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
1137 if (cookie_length == 0)
1141 /* load the client random */
1142 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
1143 p += SSL3_RANDOM_SIZE;
1145 /* get the session-id */
1148 if (p + j > d + n) {
1149 al = SSL_AD_DECODE_ERROR;
1150 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1156 * Versions before 0.9.7 always allow clients to resume sessions in
1157 * renegotiation. 0.9.7 and later allow this by default, but optionally
1158 * ignore resumption requests with flag
1159 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1160 * than a change to default behavior so that applications relying on
1161 * this for security won't even compile against older library versions).
1162 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1163 * request renegotiation but not a new session (s->new_session remains
1164 * unset): for servers, this essentially just means that the
1165 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be
1169 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1170 if (!ssl_get_new_session(s, 1))
1173 i = ssl_get_prev_session(s, p, j, d + n);
1175 * Only resume if the session's version matches the negotiated
1177 * RFC 5246 does not provide much useful advice on resumption
1178 * with a different protocol version. It doesn't forbid it but
1179 * the sanity of such behaviour would be questionable.
1180 * In practice, clients do not accept a version mismatch and
1181 * will abort the handshake with an error.
1183 if (i == 1 && s->version == s->session->ssl_version) {
1184 /* previous session */
1190 if (!ssl_get_new_session(s, 1))
1197 if (SSL_IS_DTLS(s)) {
1199 if (p + 1 > d + n) {
1200 al = SSL_AD_DECODE_ERROR;
1201 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1204 cookie_len = *(p++);
1206 if (p + cookie_len > d + n) {
1207 al = SSL_AD_DECODE_ERROR;
1208 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1213 * The ClientHello may contain a cookie even if the
1214 * HelloVerify message has not been sent--make sure that it
1215 * does not cause an overflow.
1217 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1219 al = SSL_AD_DECODE_ERROR;
1220 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1224 /* verify the cookie if appropriate option is set. */
1225 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE)
1226 && cookie_len > 0) {
1227 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1229 if (s->ctx->app_verify_cookie_cb != NULL) {
1230 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1232 al = SSL_AD_HANDSHAKE_FAILURE;
1233 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1234 SSL_R_COOKIE_MISMATCH);
1237 /* else cookie verification succeeded */
1239 /* default verification */
1240 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1241 s->d1->cookie_len) != 0) {
1242 al = SSL_AD_HANDSHAKE_FAILURE;
1243 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1246 /* Set to -2 so if successful we return 2 */
1251 if (s->method->version == DTLS_ANY_VERSION) {
1252 /* Select version to use */
1253 if (s->client_version <= DTLS1_2_VERSION &&
1254 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1255 s->version = DTLS1_2_VERSION;
1256 s->method = DTLSv1_2_server_method();
1257 } else if (tls1_suiteb(s)) {
1258 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1259 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1260 s->version = s->client_version;
1261 al = SSL_AD_PROTOCOL_VERSION;
1263 } else if (s->client_version <= DTLS1_VERSION &&
1264 !(s->options & SSL_OP_NO_DTLSv1)) {
1265 s->version = DTLS1_VERSION;
1266 s->method = DTLSv1_server_method();
1268 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1269 SSL_R_WRONG_VERSION_NUMBER);
1270 s->version = s->client_version;
1271 al = SSL_AD_PROTOCOL_VERSION;
1274 s->session->ssl_version = s->version;
1278 if (p + 2 > d + n) {
1279 al = SSL_AD_DECODE_ERROR;
1280 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1286 al = SSL_AD_ILLEGAL_PARAMETER;
1287 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1291 /* i bytes of cipher data + 1 byte for compression length later */
1292 if ((p + i + 1) > (d + n)) {
1293 /* not enough data */
1294 al = SSL_AD_DECODE_ERROR;
1295 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1298 if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers), 0) == NULL) {
1303 /* If it is a hit, check that the cipher is in the list */
1306 id = s->session->cipher->id;
1309 fprintf(stderr, "client sent %d ciphers\n",
1310 sk_SSL_CIPHER_num(ciphers));
1312 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1313 c = sk_SSL_CIPHER_value(ciphers, i);
1315 fprintf(stderr, "client [%2d of %2d]:%s\n",
1316 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1324 * Disabled because it can be used in a ciphersuite downgrade
1329 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1330 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1332 * Special case as client bug workaround: the previously used
1333 * cipher may not be in the current list, the client instead
1334 * might be trying to continue using a cipher that before wasn't
1335 * chosen due to server preferences. We'll have to reject the
1336 * connection if the cipher is not enabled, though.
1338 c = sk_SSL_CIPHER_value(ciphers, 0);
1339 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1340 s->session->cipher = c;
1347 * we need to have the cipher in the cipher list if we are asked
1350 al = SSL_AD_ILLEGAL_PARAMETER;
1351 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1352 SSL_R_REQUIRED_CIPHER_MISSING);
1359 if ((p + i) > (d + n)) {
1360 /* not enough data */
1361 al = SSL_AD_DECODE_ERROR;
1362 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1365 #ifndef OPENSSL_NO_COMP
1368 for (j = 0; j < i; j++) {
1376 al = SSL_AD_DECODE_ERROR;
1377 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1382 #ifndef OPENSSL_NO_TLSEXT
1383 /* TLS extensions */
1384 if (s->version >= SSL3_VERSION) {
1385 if (!ssl_parse_clienthello_tlsext(s, &p, d, n)) {
1386 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1392 * Check if we want to use external pre-shared secret for this handshake
1393 * for not reused session only. We need to generate server_random before
1394 * calling tls_session_secret_cb in order to allow SessionTicket
1395 * processing to use it in key derivation.
1399 pos = s->s3->server_random;
1400 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1405 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1406 SSL_CIPHER *pref_cipher = NULL;
1408 s->session->master_key_length = sizeof(s->session->master_key);
1409 if (s->tls_session_secret_cb(s, s->session->master_key,
1410 &s->session->master_key_length, ciphers,
1412 s->tls_session_secret_cb_arg)) {
1414 s->session->ciphers = ciphers;
1415 s->session->verify_result = X509_V_OK;
1419 /* check if some cipher was preferred by call back */
1421 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1426 if (pref_cipher == NULL) {
1427 al = SSL_AD_HANDSHAKE_FAILURE;
1428 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1432 s->session->cipher = pref_cipher;
1433 sk_SSL_CIPHER_free(s->cipher_list);
1434 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1435 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1436 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1442 * Worst case, we will use the NULL compression, but if we have other
1443 * options, we will now look for them. We have i-1 compression
1444 * algorithms from the client, starting at q.
1446 s->s3->tmp.new_compression = NULL;
1447 #ifndef OPENSSL_NO_COMP
1448 /* This only happens if we have a cache hit */
1449 if (s->session->compress_meth != 0) {
1450 int m, comp_id = s->session->compress_meth;
1451 /* Perform sanity checks on resumed compression algorithm */
1452 /* Can't disable compression */
1453 if (!ssl_allow_compression(s)) {
1454 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1455 SSL_R_INCONSISTENT_COMPRESSION);
1458 /* Look for resumed compression method */
1459 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1460 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1461 if (comp_id == comp->id) {
1462 s->s3->tmp.new_compression = comp;
1466 if (s->s3->tmp.new_compression == NULL) {
1467 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1468 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1471 /* Look for resumed method in compression list */
1472 for (m = 0; m < i; m++) {
1473 if (q[m] == comp_id)
1477 al = SSL_AD_ILLEGAL_PARAMETER;
1478 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1479 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1484 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
1485 /* See if we have a match */
1486 int m, nn, o, v, done = 0;
1488 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1489 for (m = 0; m < nn; m++) {
1490 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1492 for (o = 0; o < i; o++) {
1502 s->s3->tmp.new_compression = comp;
1508 * If compression is disabled we'd better not try to resume a session
1509 * using compression.
1511 if (s->session->compress_meth != 0) {
1512 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1518 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1522 #ifdef OPENSSL_NO_COMP
1523 s->session->compress_meth = 0;
1525 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1527 sk_SSL_CIPHER_free(s->session->ciphers);
1528 s->session->ciphers = ciphers;
1529 if (ciphers == NULL) {
1530 al = SSL_AD_INTERNAL_ERROR;
1531 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1535 if (!tls1_set_server_sigalgs(s)) {
1536 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1539 /* Let cert callback update server certificates if required */
1541 if (s->cert->cert_cb) {
1542 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1544 al = SSL_AD_INTERNAL_ERROR;
1545 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1549 s->rwstate = SSL_X509_LOOKUP;
1552 s->rwstate = SSL_NOTHING;
1554 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1557 al = SSL_AD_HANDSHAKE_FAILURE;
1558 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1561 s->s3->tmp.new_cipher = c;
1562 /* check whether we should disable session resumption */
1563 if (s->not_resumable_session_cb != NULL)
1564 s->session->not_resumable = s->not_resumable_session_cb(s,
1565 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))
1567 if (s->session->not_resumable)
1568 /* do not send a session ticket */
1569 s->tlsext_ticket_expected = 0;
1571 /* Session-id reuse */
1572 s->s3->tmp.new_cipher = s->session->cipher;
1575 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1576 if (!ssl3_digest_cached_records(s))
1581 * we now have the following setup.
1583 * cipher_list - our prefered list of ciphers
1584 * ciphers - the clients prefered list of ciphers
1585 * compression - basically ignored right now
1586 * ssl version is set - sslv3
1587 * s->session - The ssl session has been setup.
1588 * s->hit - session reuse flag
1589 * s->s3->tmp.new_cipher- the new cipher to use.
1592 /* Handles TLS extensions that we couldn't check earlier */
1593 if (s->version >= SSL3_VERSION) {
1594 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1595 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1604 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1606 s->state = SSL_ST_ERR;
1609 sk_SSL_CIPHER_free(ciphers);
1610 return ret < 0 ? -1 : ret;
1613 int ssl3_send_server_hello(SSL *s)
1616 unsigned char *p, *d;
1621 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1622 buf = (unsigned char *)s->init_buf->data;
1623 #ifdef OPENSSL_NO_TLSEXT
1624 p = s->s3->server_random;
1625 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) {
1626 s->state = SSL_ST_ERR;
1630 /* Do the message type and length last */
1631 d = p = ssl_handshake_start(s);
1633 *(p++) = s->version >> 8;
1634 *(p++) = s->version & 0xff;
1637 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1638 p += SSL3_RANDOM_SIZE;
1641 * There are several cases for the session ID to send
1642 * back in the server hello:
1643 * - For session reuse from the session cache,
1644 * we send back the old session ID.
1645 * - If stateless session reuse (using a session ticket)
1646 * is successful, we send back the client's "session ID"
1647 * (which doesn't actually identify the session).
1648 * - If it is a new session, we send back the new
1650 * - However, if we want the new session to be single-use,
1651 * we send back a 0-length session ID.
1652 * s->hit is non-zero in either case of session reuse,
1653 * so the following won't overwrite an ID that we're supposed
1656 if (s->session->not_resumable ||
1657 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1659 s->session->session_id_length = 0;
1661 sl = s->session->session_id_length;
1662 if (sl > (int)sizeof(s->session->session_id)) {
1663 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1664 s->state = SSL_ST_ERR;
1668 memcpy(p, s->session->session_id, sl);
1671 /* put the cipher */
1672 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1675 /* put the compression method */
1676 #ifdef OPENSSL_NO_COMP
1679 if (s->s3->tmp.new_compression == NULL)
1682 *(p++) = s->s3->tmp.new_compression->id;
1684 #ifndef OPENSSL_NO_TLSEXT
1685 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1686 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1687 s->state = SSL_ST_ERR;
1691 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1693 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1694 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1695 s->state = SSL_ST_ERR;
1701 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1702 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1705 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1708 /* SSL3_ST_SW_SRVR_HELLO_B */
1709 return ssl_do_write(s);
1712 int ssl3_send_server_done(SSL *s)
1715 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1716 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1717 SSLerr(SSL_F_SSL3_SEND_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1720 s->state = SSL3_ST_SW_SRVR_DONE_B;
1723 /* SSL3_ST_SW_SRVR_DONE_B */
1724 return ssl_do_write(s);
1727 int ssl3_send_server_key_exchange(SSL *s)
1729 #ifndef OPENSSL_NO_RSA
1733 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1736 #ifndef OPENSSL_NO_DH
1737 DH *dh = NULL, *dhp;
1739 #ifndef OPENSSL_NO_EC
1740 EC_KEY *ecdh = NULL, *ecdhp;
1741 unsigned char *encodedPoint = NULL;
1744 BN_CTX *bn_ctx = NULL;
1747 const EVP_MD *md = NULL;
1748 unsigned char *p, *d;
1758 EVP_MD_CTX_init(&md_ctx);
1759 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1760 type = s->s3->tmp.new_cipher->algorithm_mkey;
1765 r[0] = r[1] = r[2] = r[3] = NULL;
1767 #ifndef OPENSSL_NO_RSA
1768 if (type & SSL_kRSA) {
1769 rsa = cert->rsa_tmp;
1770 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1771 rsa = s->cert->rsa_tmp_cb(s,
1772 SSL_C_IS_EXPORT(s->s3->
1774 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1777 al = SSL_AD_HANDSHAKE_FAILURE;
1778 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1779 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1783 cert->rsa_tmp = rsa;
1786 al = SSL_AD_HANDSHAKE_FAILURE;
1787 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1788 SSL_R_MISSING_TMP_RSA_KEY);
1793 s->s3->tmp.use_rsa_tmp = 1;
1796 #ifndef OPENSSL_NO_DH
1797 if (type & SSL_kDHE) {
1798 if (s->cert->dh_tmp_auto) {
1799 dhp = ssl_get_auto_dh(s);
1801 al = SSL_AD_INTERNAL_ERROR;
1802 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1803 ERR_R_INTERNAL_ERROR);
1808 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1809 dhp = s->cert->dh_tmp_cb(s,
1810 SSL_C_IS_EXPORT(s->s3->
1812 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1815 al = SSL_AD_HANDSHAKE_FAILURE;
1816 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1817 SSL_R_MISSING_TMP_DH_KEY);
1820 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1821 DH_security_bits(dhp), 0, dhp)) {
1822 al = SSL_AD_HANDSHAKE_FAILURE;
1823 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1824 SSL_R_DH_KEY_TOO_SMALL);
1827 if (s->s3->tmp.dh != NULL) {
1828 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1829 ERR_R_INTERNAL_ERROR);
1833 if (s->cert->dh_tmp_auto)
1835 else if ((dh = DHparams_dup(dhp)) == NULL) {
1836 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1841 if ((dhp->pub_key == NULL ||
1842 dhp->priv_key == NULL ||
1843 (s->options & SSL_OP_SINGLE_DH_USE))) {
1844 if (!DH_generate_key(dh)) {
1845 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1849 dh->pub_key = BN_dup(dhp->pub_key);
1850 dh->priv_key = BN_dup(dhp->priv_key);
1851 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1852 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1861 #ifndef OPENSSL_NO_EC
1862 if (type & SSL_kECDHE) {
1863 const EC_GROUP *group;
1865 ecdhp = cert->ecdh_tmp;
1866 if (s->cert->ecdh_tmp_auto) {
1867 /* Get NID of appropriate shared curve */
1868 int nid = tls1_shared_curve(s, -2);
1869 if (nid != NID_undef)
1870 ecdhp = EC_KEY_new_by_curve_name(nid);
1871 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1872 ecdhp = s->cert->ecdh_tmp_cb(s,
1873 SSL_C_IS_EXPORT(s->s3->
1875 SSL_C_EXPORT_PKEYLENGTH(s->
1876 s3->tmp.new_cipher));
1878 if (ecdhp == NULL) {
1879 al = SSL_AD_HANDSHAKE_FAILURE;
1880 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1881 SSL_R_MISSING_TMP_ECDH_KEY);
1885 if (s->s3->tmp.ecdh != NULL) {
1886 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1887 ERR_R_INTERNAL_ERROR);
1891 /* Duplicate the ECDH structure. */
1892 if (ecdhp == NULL) {
1893 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1896 if (s->cert->ecdh_tmp_auto)
1898 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1899 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1903 s->s3->tmp.ecdh = ecdh;
1904 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1905 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1906 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1907 if (!EC_KEY_generate_key(ecdh)) {
1908 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1914 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1915 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1916 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1917 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1921 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1922 (EC_GROUP_get_degree(group) > 163)) {
1923 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1924 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1929 * XXX: For now, we only support ephemeral ECDH keys over named
1930 * (not generic) curves. For supported named curves, curve_id is
1934 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1936 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1937 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1942 * Encode the public key. First check the size of encoding and
1943 * allocate memory accordingly.
1945 encodedlen = EC_POINT_point2oct(group,
1946 EC_KEY_get0_public_key(ecdh),
1947 POINT_CONVERSION_UNCOMPRESSED,
1950 encodedPoint = (unsigned char *)
1951 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1952 bn_ctx = BN_CTX_new();
1953 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1954 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1955 ERR_R_MALLOC_FAILURE);
1959 encodedlen = EC_POINT_point2oct(group,
1960 EC_KEY_get0_public_key(ecdh),
1961 POINT_CONVERSION_UNCOMPRESSED,
1962 encodedPoint, encodedlen, bn_ctx);
1964 if (encodedlen == 0) {
1965 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1969 BN_CTX_free(bn_ctx);
1973 * XXX: For now, we only support named (not generic) curves in
1974 * ECDH ephemeral key exchanges. In this situation, we need four
1975 * additional bytes to encode the entire ServerECDHParams
1981 * We'll generate the serverKeyExchange message explicitly so we
1982 * can set these to NULLs
1989 #endif /* !OPENSSL_NO_EC */
1990 #ifndef OPENSSL_NO_PSK
1991 if (type & SSL_kPSK) {
1993 * reserve size for record length and PSK identity hint
1995 n += 2 + strlen(s->ctx->psk_identity_hint);
1997 #endif /* !OPENSSL_NO_PSK */
1998 #ifndef OPENSSL_NO_SRP
1999 if (type & SSL_kSRP) {
2000 if ((s->srp_ctx.N == NULL) ||
2001 (s->srp_ctx.g == NULL) ||
2002 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
2003 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2004 SSL_R_MISSING_SRP_PARAM);
2007 r[0] = s->srp_ctx.N;
2008 r[1] = s->srp_ctx.g;
2009 r[2] = s->srp_ctx.s;
2010 r[3] = s->srp_ctx.B;
2014 al = SSL_AD_HANDSHAKE_FAILURE;
2015 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2016 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
2019 for (i = 0; i < 4 && r[i] != NULL; i++) {
2020 nr[i] = BN_num_bytes(r[i]);
2021 #ifndef OPENSSL_NO_SRP
2022 if ((i == 2) && (type & SSL_kSRP))
2029 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
2030 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
2031 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
2033 al = SSL_AD_DECODE_ERROR;
2036 kn = EVP_PKEY_size(pkey);
2042 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
2043 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
2046 d = p = ssl_handshake_start(s);
2048 for (i = 0; i < 4 && r[i] != NULL; i++) {
2049 #ifndef OPENSSL_NO_SRP
2050 if ((i == 2) && (type & SSL_kSRP)) {
2060 #ifndef OPENSSL_NO_EC
2061 if (type & SSL_kECDHE) {
2063 * XXX: For now, we only support named (not generic) curves. In
2064 * this situation, the serverKeyExchange message has: [1 byte
2065 * CurveType], [2 byte CurveName] [1 byte length of encoded
2066 * point], followed by the actual encoded point itself
2068 *p = NAMED_CURVE_TYPE;
2076 memcpy(p, encodedPoint, encodedlen);
2077 OPENSSL_free(encodedPoint);
2078 encodedPoint = NULL;
2083 #ifndef OPENSSL_NO_PSK
2084 if (type & SSL_kPSK) {
2085 /* copy PSK identity hint */
2086 s2n(strlen(s->ctx->psk_identity_hint), p);
2087 strncpy((char *)p, s->ctx->psk_identity_hint,
2088 strlen(s->ctx->psk_identity_hint));
2089 p += strlen(s->ctx->psk_identity_hint);
2096 * n is the length of the params, they start at &(d[4]) and p
2097 * points to the space at the end.
2099 #ifndef OPENSSL_NO_RSA
2100 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
2103 for (num = 2; num > 0; num--) {
2104 EVP_MD_CTX_set_flags(&md_ctx,
2105 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
2106 EVP_DigestInit_ex(&md_ctx, (num == 2)
2107 ? s->ctx->md5 : s->ctx->sha1, NULL);
2108 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2110 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2112 EVP_DigestUpdate(&md_ctx, d, n);
2113 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2117 if (RSA_sign(NID_md5_sha1, md_buf, j,
2118 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2119 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2127 /* send signature algorithm */
2128 if (SSL_USE_SIGALGS(s)) {
2129 if (!tls12_get_sigandhash(p, pkey, md)) {
2130 /* Should never happen */
2131 al = SSL_AD_INTERNAL_ERROR;
2132 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2133 ERR_R_INTERNAL_ERROR);
2139 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2141 EVP_SignInit_ex(&md_ctx, md, NULL);
2142 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2144 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2146 EVP_SignUpdate(&md_ctx, d, n);
2147 if (!EVP_SignFinal(&md_ctx, &(p[2]),
2148 (unsigned int *)&i, pkey)) {
2149 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2154 if (SSL_USE_SIGALGS(s))
2157 /* Is this error check actually needed? */
2158 al = SSL_AD_HANDSHAKE_FAILURE;
2159 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2160 SSL_R_UNKNOWN_PKEY_TYPE);
2165 if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
2166 al = SSL_AD_HANDSHAKE_FAILURE;
2167 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2172 s->state = SSL3_ST_SW_KEY_EXCH_B;
2173 EVP_MD_CTX_cleanup(&md_ctx);
2174 return ssl_do_write(s);
2176 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2178 #ifndef OPENSSL_NO_EC
2179 OPENSSL_free(encodedPoint);
2180 BN_CTX_free(bn_ctx);
2182 EVP_MD_CTX_cleanup(&md_ctx);
2183 s->state = SSL_ST_ERR;
2187 int ssl3_send_certificate_request(SSL *s)
2189 unsigned char *p, *d;
2190 int i, j, nl, off, n;
2191 STACK_OF(X509_NAME) *sk = NULL;
2195 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2198 d = p = ssl_handshake_start(s);
2200 /* get the list of acceptable cert types */
2202 n = ssl3_get_req_cert_type(s, p);
2207 if (SSL_USE_SIGALGS(s)) {
2208 const unsigned char *psigs;
2209 unsigned char *etmp = p;
2210 nl = tls12_get_psigalgs(s, &psigs);
2211 /* Skip over length for now */
2213 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2214 /* Now fill in length */
2224 sk = SSL_get_client_CA_list(s);
2227 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2228 name = sk_X509_NAME_value(sk, i);
2229 j = i2d_X509_NAME(name, NULL);
2230 if (!BUF_MEM_grow_clean
2231 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2232 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2236 p = ssl_handshake_start(s) + n;
2238 i2d_X509_NAME(name, &p);
2243 /* else no CA names */
2244 p = ssl_handshake_start(s) + off;
2247 if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2248 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2252 s->state = SSL3_ST_SW_CERT_REQ_B;
2255 /* SSL3_ST_SW_CERT_REQ_B */
2256 return ssl_do_write(s);
2258 s->state = SSL_ST_ERR;
2262 int ssl3_get_client_key_exchange(SSL *s)
2266 unsigned long alg_k;
2268 #ifndef OPENSSL_NO_RSA
2270 EVP_PKEY *pkey = NULL;
2272 #ifndef OPENSSL_NO_DH
2274 DH *dh_srvr, *dh_clnt = NULL;
2277 #ifndef OPENSSL_NO_EC
2278 EC_KEY *srvr_ecdh = NULL;
2279 EVP_PKEY *clnt_pub_pkey = NULL;
2280 EC_POINT *clnt_ecpoint = NULL;
2281 BN_CTX *bn_ctx = NULL;
2284 n = s->method->ssl_get_message(s,
2285 SSL3_ST_SR_KEY_EXCH_A,
2286 SSL3_ST_SR_KEY_EXCH_B,
2287 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2291 p = (unsigned char *)s->init_msg;
2293 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2295 #ifndef OPENSSL_NO_RSA
2296 if (alg_k & SSL_kRSA) {
2297 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2299 unsigned char decrypt_good, version_good;
2302 /* FIX THIS UP EAY EAY EAY EAY */
2303 if (s->s3->tmp.use_rsa_tmp) {
2304 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2305 rsa = s->cert->rsa_tmp;
2307 * Don't do a callback because rsa_tmp should be sent already
2310 al = SSL_AD_HANDSHAKE_FAILURE;
2311 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2312 SSL_R_MISSING_TMP_RSA_PKEY);
2317 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2318 if ((pkey == NULL) ||
2319 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2320 al = SSL_AD_HANDSHAKE_FAILURE;
2321 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2322 SSL_R_MISSING_RSA_CERTIFICATE);
2325 rsa = pkey->pkey.rsa;
2328 /* TLS and [incidentally] DTLS{0xFEFF} */
2329 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2332 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2333 al = SSL_AD_DECODE_ERROR;
2334 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2335 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2344 * Reject overly short RSA ciphertext because we want to be sure
2345 * that the buffer size makes it safe to iterate over the entire
2346 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2347 * actual expected size is larger due to RSA padding, but the
2348 * bound is sufficient to be safe.
2350 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2351 al = SSL_AD_DECRYPT_ERROR;
2352 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2353 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2358 * We must not leak whether a decryption failure occurs because of
2359 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2360 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2361 * generates a random premaster secret for the case that the decrypt
2362 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2365 if (RAND_bytes(rand_premaster_secret,
2366 sizeof(rand_premaster_secret)) <= 0)
2369 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2373 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2374 * be 0xff if so and zero otherwise.
2377 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2380 * If the version in the decrypted pre-master secret is correct then
2381 * version_good will be 0xff, otherwise it'll be zero. The
2382 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2383 * (http://eprint.iacr.org/2003/052/) exploits the version number
2384 * check as a "bad version oracle". Thus version checks are done in
2385 * constant time and are treated like any other decryption error.
2388 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2390 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2393 * The premaster secret must contain the same version number as the
2394 * ClientHello to detect version rollback attacks (strangely, the
2395 * protocol does not offer such protection for DH ciphersuites).
2396 * However, buggy clients exist that send the negotiated protocol
2397 * version instead if the server does not support the requested
2398 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2401 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2402 unsigned char workaround_good;
2404 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2406 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2407 version_good |= workaround_good;
2411 * Both decryption and version must be good for decrypt_good to
2412 * remain non-zero (0xff).
2414 decrypt_good &= version_good;
2417 * Now copy rand_premaster_secret over from p using
2418 * decrypt_good_mask. If decryption failed, then p does not
2419 * contain valid plaintext, however, a check above guarantees
2420 * it is still sufficiently large to read from.
2422 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2423 p[j] = constant_time_select_8(decrypt_good, p[j],
2424 rand_premaster_secret[j]);
2427 s->session->master_key_length =
2428 s->method->ssl3_enc->generate_master_secret(s,
2430 session->master_key,
2433 (rand_premaster_secret));
2434 OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2435 if (s->session->master_key_length < 0) {
2436 al = SSL_AD_INTERNAL_ERROR;
2437 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2442 #ifndef OPENSSL_NO_DH
2443 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2445 EVP_PKEY *skey = NULL;
2449 if (alg_k & SSL_kDHE) {
2450 al = SSL_AD_HANDSHAKE_FAILURE;
2451 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2452 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2457 if (n && n != i + 2) {
2458 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2459 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2460 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2467 if (alg_k & SSL_kDHr)
2468 idx = SSL_PKEY_DH_RSA;
2469 else if (alg_k & SSL_kDHd)
2470 idx = SSL_PKEY_DH_DSA;
2472 skey = s->cert->pkeys[idx].privatekey;
2473 if ((skey == NULL) ||
2474 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2475 al = SSL_AD_HANDSHAKE_FAILURE;
2476 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2477 SSL_R_MISSING_RSA_CERTIFICATE);
2480 dh_srvr = skey->pkey.dh;
2481 } else if (s->s3->tmp.dh == NULL) {
2482 al = SSL_AD_HANDSHAKE_FAILURE;
2483 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2484 SSL_R_MISSING_TMP_DH_KEY);
2487 dh_srvr = s->s3->tmp.dh;
2490 /* Get pubkey from cert */
2491 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2493 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2494 dh_clnt = EVP_PKEY_get1_DH(clkey);
2496 if (dh_clnt == NULL) {
2497 al = SSL_AD_HANDSHAKE_FAILURE;
2498 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2499 SSL_R_MISSING_TMP_DH_KEY);
2502 EVP_PKEY_free(clkey);
2503 pub = dh_clnt->pub_key;
2505 pub = BN_bin2bn(p, i, NULL);
2507 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2511 i = DH_compute_key(p, pub, dh_srvr);
2514 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2519 DH_free(s->s3->tmp.dh);
2520 s->s3->tmp.dh = NULL;
2526 s->session->master_key_length =
2527 s->method->ssl3_enc->generate_master_secret(s,
2529 session->master_key,
2531 OPENSSL_cleanse(p, i);
2532 if (s->session->master_key_length < 0) {
2533 al = SSL_AD_INTERNAL_ERROR;
2534 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2542 #ifndef OPENSSL_NO_EC
2543 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2547 const EC_GROUP *group;
2548 const BIGNUM *priv_key;
2550 /* initialize structures for server's ECDH key pair */
2551 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2552 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2556 /* Let's get server private key and group information */
2557 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2558 /* use the certificate */
2559 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2562 * use the ephermeral values we saved when generating the
2563 * ServerKeyExchange msg.
2565 tkey = s->s3->tmp.ecdh;
2568 group = EC_KEY_get0_group(tkey);
2569 priv_key = EC_KEY_get0_private_key(tkey);
2571 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2572 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2573 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2577 /* Let's get client's public key */
2578 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2579 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2584 /* Client Publickey was in Client Certificate */
2586 if (alg_k & SSL_kECDHE) {
2587 al = SSL_AD_HANDSHAKE_FAILURE;
2588 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2589 SSL_R_MISSING_TMP_ECDH_KEY);
2592 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2593 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2595 * XXX: For now, we do not support client authentication
2596 * using ECDH certificates so this branch (n == 0L) of the
2597 * code is never executed. When that support is added, we
2598 * ought to ensure the key received in the certificate is
2599 * authorized for key agreement. ECDH_compute_key implicitly
2600 * checks that the two ECDH shares are for the same group.
2602 al = SSL_AD_HANDSHAKE_FAILURE;
2603 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2604 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2608 if (EC_POINT_copy(clnt_ecpoint,
2609 EC_KEY_get0_public_key(clnt_pub_pkey->
2611 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2614 ret = 2; /* Skip certificate verify processing */
2617 * Get client's public key from encoded point in the
2618 * ClientKeyExchange message.
2620 if ((bn_ctx = BN_CTX_new()) == NULL) {
2621 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2622 ERR_R_MALLOC_FAILURE);
2626 /* Get encoded point length */
2630 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2633 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2634 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2638 * p is pointing to somewhere in the buffer currently, so set it
2641 p = (unsigned char *)s->init_buf->data;
2644 /* Compute the shared pre-master secret */
2645 field_size = EC_GROUP_get_degree(group);
2646 if (field_size <= 0) {
2647 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2650 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2653 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2657 EVP_PKEY_free(clnt_pub_pkey);
2658 EC_POINT_free(clnt_ecpoint);
2659 EC_KEY_free(srvr_ecdh);
2660 BN_CTX_free(bn_ctx);
2661 EC_KEY_free(s->s3->tmp.ecdh);
2662 s->s3->tmp.ecdh = NULL;
2664 /* Compute the master secret */
2665 s->session->master_key_length =
2666 s->method->ssl3_enc->generate_master_secret(s,
2668 session->master_key,
2671 OPENSSL_cleanse(p, i);
2672 if (s->session->master_key_length < 0) {
2673 al = SSL_AD_INTERNAL_ERROR;
2674 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2680 #ifndef OPENSSL_NO_PSK
2681 if (alg_k & SSL_kPSK) {
2682 unsigned char *t = NULL;
2683 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2684 unsigned int pre_ms_len = 0, psk_len = 0;
2686 char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2688 al = SSL_AD_HANDSHAKE_FAILURE;
2692 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2695 if (i > PSK_MAX_IDENTITY_LEN) {
2696 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2697 SSL_R_DATA_LENGTH_TOO_LONG);
2700 if (s->psk_server_callback == NULL) {
2701 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2702 SSL_R_PSK_NO_SERVER_CB);
2707 * Create guaranteed NULL-terminated identity string for the callback
2709 memcpy(tmp_id, p, i);
2710 memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2711 psk_len = s->psk_server_callback(s, tmp_id,
2713 sizeof(psk_or_pre_ms));
2714 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2716 if (psk_len > PSK_MAX_PSK_LEN) {
2717 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2719 } else if (psk_len == 0) {
2721 * PSK related to the given identity not found
2723 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2724 SSL_R_PSK_IDENTITY_NOT_FOUND);
2725 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2729 /* create PSK pre_master_secret */
2730 pre_ms_len = 2 + psk_len + 2 + psk_len;
2732 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2734 memset(t, 0, psk_len);
2738 OPENSSL_free(s->session->psk_identity);
2739 s->session->psk_identity = BUF_strdup((char *)p);
2740 if (s->session->psk_identity == NULL) {
2741 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2745 OPENSSL_free(s->session->psk_identity_hint);
2746 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2747 if (s->ctx->psk_identity_hint != NULL &&
2748 s->session->psk_identity_hint == NULL) {
2749 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2753 s->session->master_key_length =
2754 s->method->ssl3_enc->generate_master_secret(s,
2756 session->master_key,
2759 if (s->session->master_key_length < 0) {
2760 al = SSL_AD_INTERNAL_ERROR;
2761 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2766 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2771 #ifndef OPENSSL_NO_SRP
2772 if (alg_k & SSL_kSRP) {
2777 if (param_len > n) {
2778 al = SSL_AD_DECODE_ERROR;
2779 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2780 SSL_R_BAD_SRP_A_LENGTH);
2783 if ((s->srp_ctx.A = BN_bin2bn(p, i, NULL)) == NULL) {
2784 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2787 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2788 || BN_is_zero(s->srp_ctx.A)) {
2789 al = SSL_AD_ILLEGAL_PARAMETER;
2790 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2791 SSL_R_BAD_SRP_PARAMETERS);
2794 OPENSSL_free(s->session->srp_username);
2795 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2796 if (s->session->srp_username == NULL) {
2797 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2801 if ((s->session->master_key_length =
2802 SRP_generate_server_master_secret(s,
2803 s->session->master_key)) < 0) {
2804 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2810 #endif /* OPENSSL_NO_SRP */
2811 if (alg_k & SSL_kGOST) {
2813 EVP_PKEY_CTX *pkey_ctx;
2814 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2815 unsigned char premaster_secret[32], *start;
2816 size_t outlen = 32, inlen;
2817 unsigned long alg_a;
2821 /* Get our certificate private key */
2822 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2823 if (alg_a & SSL_aGOST94)
2824 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2825 else if (alg_a & SSL_aGOST01)
2826 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2828 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2829 EVP_PKEY_decrypt_init(pkey_ctx);
2831 * If client certificate is present and is of the same type, maybe
2832 * use it for key exchange. Don't mind errors from
2833 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2834 * client certificate for authorization only.
2836 client_pub_pkey = X509_get_pubkey(s->session->peer);
2837 if (client_pub_pkey) {
2838 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2841 /* Decrypt session key */
2843 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2844 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2845 || Tclass != V_ASN1_UNIVERSAL) {
2846 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2847 SSL_R_DECRYPTION_FAILED);
2852 if (EVP_PKEY_decrypt
2853 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2854 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2855 SSL_R_DECRYPTION_FAILED);
2858 /* Generate master secret */
2859 s->session->master_key_length =
2860 s->method->ssl3_enc->generate_master_secret(s,
2862 session->master_key,
2863 premaster_secret, 32);
2864 if (s->session->master_key_length < 0) {
2865 al = SSL_AD_INTERNAL_ERROR;
2866 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2869 /* Check if pubkey from client certificate was used */
2870 if (EVP_PKEY_CTX_ctrl
2871 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2876 EVP_PKEY_free(client_pub_pkey);
2877 EVP_PKEY_CTX_free(pkey_ctx);
2882 al = SSL_AD_HANDSHAKE_FAILURE;
2883 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2889 ssl3_send_alert(s, SSL3_AL_FATAL, al);
2890 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
2893 #ifndef OPENSSL_NO_EC
2894 EVP_PKEY_free(clnt_pub_pkey);
2895 EC_POINT_free(clnt_ecpoint);
2896 EC_KEY_free(srvr_ecdh);
2897 BN_CTX_free(bn_ctx);
2899 s->state = SSL_ST_ERR;
2903 int ssl3_get_cert_verify(SSL *s)
2905 EVP_PKEY *pkey = NULL;
2907 int al, ok, ret = 0;
2911 const EVP_MD *md = NULL;
2913 EVP_MD_CTX_init(&mctx);
2916 * We should only process a CertificateVerify message if we have received
2917 * a Certificate from the client. If so then |s->session->peer| will be non
2918 * NULL. In some instances a CertificateVerify message is not required even
2919 * if the peer has sent a Certificate (e.g. such as in the case of static
2920 * DH). In that case the ClientKeyExchange processing will skip the
2921 * CertificateVerify state so we should not arrive here.
2923 if (s->session->peer == NULL) {
2928 n = s->method->ssl_get_message(s,
2929 SSL3_ST_SR_CERT_VRFY_A,
2930 SSL3_ST_SR_CERT_VRFY_B,
2931 SSL3_MT_CERTIFICATE_VERIFY,
2932 SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2937 peer = s->session->peer;
2938 pkey = X509_get_pubkey(peer);
2939 type = X509_certificate_type(peer, pkey);
2941 if (!(type & EVP_PKT_SIGN)) {
2942 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2943 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2944 al = SSL_AD_ILLEGAL_PARAMETER;
2948 /* we now have a signature that we need to verify */
2949 p = (unsigned char *)s->init_msg;
2950 /* Check for broken implementations of GOST ciphersuites */
2952 * If key is GOST and n is exactly 64, it is bare signature without
2955 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2956 pkey->type == NID_id_GostR3410_2001)) {
2959 if (SSL_USE_SIGALGS(s)) {
2960 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
2962 al = SSL_AD_INTERNAL_ERROR;
2964 } else if (rv == 0) {
2965 al = SSL_AD_DECODE_ERROR;
2969 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
2977 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2978 al = SSL_AD_DECODE_ERROR;
2982 j = EVP_PKEY_size(pkey);
2983 if ((i > j) || (n > j) || (n <= 0)) {
2984 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2985 al = SSL_AD_DECODE_ERROR;
2989 if (SSL_USE_SIGALGS(s)) {
2992 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2993 if (hdatalen <= 0) {
2994 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
2995 al = SSL_AD_INTERNAL_ERROR;
2999 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3002 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3003 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3004 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3005 al = SSL_AD_INTERNAL_ERROR;
3009 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
3010 al = SSL_AD_DECRYPT_ERROR;
3011 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3015 #ifndef OPENSSL_NO_RSA
3016 if (pkey->type == EVP_PKEY_RSA) {
3017 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3018 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
3021 al = SSL_AD_DECRYPT_ERROR;
3022 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3026 al = SSL_AD_DECRYPT_ERROR;
3027 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3032 #ifndef OPENSSL_NO_DSA
3033 if (pkey->type == EVP_PKEY_DSA) {
3034 j = DSA_verify(pkey->save_type,
3035 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3036 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3039 al = SSL_AD_DECRYPT_ERROR;
3040 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3045 #ifndef OPENSSL_NO_EC
3046 if (pkey->type == EVP_PKEY_EC) {
3047 j = ECDSA_verify(pkey->save_type,
3048 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3049 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3052 al = SSL_AD_DECRYPT_ERROR;
3053 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3058 if (pkey->type == NID_id_GostR3410_94
3059 || pkey->type == NID_id_GostR3410_2001) {
3060 unsigned char signature[64];
3062 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3063 EVP_PKEY_verify_init(pctx);
3065 fprintf(stderr, "GOST signature length is %d", i);
3067 for (idx = 0; idx < 64; idx++) {
3068 signature[63 - idx] = p[idx];
3070 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3072 EVP_PKEY_CTX_free(pctx);
3074 al = SSL_AD_DECRYPT_ERROR;
3075 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3079 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3080 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3087 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3088 s->state = SSL_ST_ERR;
3091 BIO_free(s->s3->handshake_buffer);
3092 s->s3->handshake_buffer = NULL;
3093 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3094 EVP_MD_CTX_cleanup(&mctx);
3095 EVP_PKEY_free(pkey);
3099 int ssl3_get_client_certificate(SSL *s)
3101 int i, ok, al, ret = -1;
3103 unsigned long l, nc, llen, n;
3104 const unsigned char *p, *q;
3106 STACK_OF(X509) *sk = NULL;
3108 n = s->method->ssl_get_message(s,
3111 -1, s->max_cert_list, &ok);
3116 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3117 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3118 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3119 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3120 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3121 al = SSL_AD_HANDSHAKE_FAILURE;
3125 * If tls asked for a client cert, the client must return a 0 list
3127 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3128 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3129 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3130 al = SSL_AD_UNEXPECTED_MESSAGE;
3133 s->s3->tmp.reuse_message = 1;
3137 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3138 al = SSL_AD_UNEXPECTED_MESSAGE;
3139 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3142 p = d = (unsigned char *)s->init_msg;
3144 if ((sk = sk_X509_new_null()) == NULL) {
3145 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3150 if (llen + 3 != n) {
3151 al = SSL_AD_DECODE_ERROR;
3152 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3155 for (nc = 0; nc < llen;) {
3157 if ((l + nc + 3) > llen) {
3158 al = SSL_AD_DECODE_ERROR;
3159 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3160 SSL_R_CERT_LENGTH_MISMATCH);
3165 x = d2i_X509(NULL, &p, l);
3167 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3171 al = SSL_AD_DECODE_ERROR;
3172 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3173 SSL_R_CERT_LENGTH_MISMATCH);
3176 if (!sk_X509_push(sk, x)) {
3177 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3184 if (sk_X509_num(sk) <= 0) {
3185 /* TLS does not mind 0 certs returned */
3186 if (s->version == SSL3_VERSION) {
3187 al = SSL_AD_HANDSHAKE_FAILURE;
3188 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3189 SSL_R_NO_CERTIFICATES_RETURNED);
3192 /* Fail for TLS only if we required a certificate */
3193 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3194 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3195 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3196 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3197 al = SSL_AD_HANDSHAKE_FAILURE;
3200 /* No client certificate so digest cached records */
3201 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3202 al = SSL_AD_INTERNAL_ERROR;
3207 i = ssl_verify_cert_chain(s, sk);
3209 al = ssl_verify_alarm_type(s->verify_result);
3210 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3211 SSL_R_CERTIFICATE_VERIFY_FAILED);
3215 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3216 al = SSL_AD_HANDSHAKE_FAILURE;
3219 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3221 al = SSL3_AD_HANDSHAKE_FAILURE;
3222 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3223 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3226 EVP_PKEY_free(pkey);
3229 X509_free(s->session->peer);
3230 s->session->peer = sk_X509_shift(sk);
3231 s->session->verify_result = s->verify_result;
3234 * With the current implementation, sess_cert will always be NULL when we
3237 if (s->session->sess_cert == NULL) {
3238 s->session->sess_cert = ssl_sess_cert_new();
3239 if (s->session->sess_cert == NULL) {
3240 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3244 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3245 s->session->sess_cert->cert_chain = sk;
3247 * Inconsistency alert: cert_chain does *not* include the peer's own
3248 * certificate, while we do include it in s3_clnt.c
3255 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3257 s->state = SSL_ST_ERR;
3259 sk_X509_pop_free(sk, X509_free);
3263 int ssl3_send_server_certificate(SSL *s)
3267 if (s->state == SSL3_ST_SW_CERT_A) {
3268 cpk = ssl_get_server_send_pkey(s);
3270 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3271 s->state = SSL_ST_ERR;
3275 if (!ssl3_output_cert_chain(s, cpk)) {
3276 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3277 s->state = SSL_ST_ERR;
3280 s->state = SSL3_ST_SW_CERT_B;
3283 /* SSL3_ST_SW_CERT_B */
3284 return ssl_do_write(s);
3287 #ifndef OPENSSL_NO_TLSEXT
3288 /* send a new session ticket (not necessarily for a new session) */
3289 int ssl3_send_newsession_ticket(SSL *s)
3291 unsigned char *senc = NULL;
3295 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3296 unsigned char *p, *macstart;
3297 const unsigned char *const_p;
3298 int len, slen_full, slen;
3301 SSL_CTX *tctx = s->initial_ctx;
3302 unsigned char iv[EVP_MAX_IV_LENGTH];
3303 unsigned char key_name[16];
3305 /* get session encoding length */
3306 slen_full = i2d_SSL_SESSION(s->session, NULL);
3308 * Some length values are 16 bits, so forget it if session is too
3311 if (slen_full == 0 || slen_full > 0xFF00) {
3312 s->state = SSL_ST_ERR;
3315 senc = OPENSSL_malloc(slen_full);
3317 s->state = SSL_ST_ERR;
3321 EVP_CIPHER_CTX_init(&ctx);
3322 HMAC_CTX_init(&hctx);
3325 if (!i2d_SSL_SESSION(s->session, &p))
3329 * create a fresh copy (not shared with other threads) to clean up
3332 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3335 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3337 slen = i2d_SSL_SESSION(sess, NULL);
3338 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3339 SSL_SESSION_free(sess);
3343 if (!i2d_SSL_SESSION(sess, &p)) {
3344 SSL_SESSION_free(sess);
3347 SSL_SESSION_free(sess);
3350 * Grow buffer if need be: the length calculation is as
3351 * follows handshake_header_length +
3352 * 4 (ticket lifetime hint) + 2 (ticket length) +
3353 * 16 (key name) + max_iv_len (iv length) +
3354 * session_length + max_enc_block_size (max encrypted session
3355 * length) + max_md_size (HMAC).
3357 if (!BUF_MEM_grow(s->init_buf,
3358 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3359 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
3362 p = ssl_handshake_start(s);
3364 * Initialize HMAC and cipher contexts. If callback present it does
3365 * all the work otherwise use generated values from parent ctx.
3367 if (tctx->tlsext_ticket_key_cb) {
3368 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3372 if (RAND_bytes(iv, 16) <= 0)
3374 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3375 tctx->tlsext_tick_aes_key, iv))
3377 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3378 EVP_sha256(), NULL))
3380 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3384 * Ticket lifetime hint (advisory only): We leave this unspecified
3385 * for resumed session (for simplicity), and guess that tickets for
3386 * new sessions will live as long as their sessions.
3388 l2n(s->hit ? 0 : s->session->timeout, p);
3390 /* Skip ticket length for now */
3392 /* Output key name */
3394 memcpy(p, key_name, 16);
3397 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3398 p += EVP_CIPHER_CTX_iv_length(&ctx);
3399 /* Encrypt session data */
3400 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3403 if (!EVP_EncryptFinal(&ctx, p, &len))
3407 if (!HMAC_Update(&hctx, macstart, p - macstart))
3409 if (!HMAC_Final(&hctx, p, &hlen))
3412 EVP_CIPHER_CTX_cleanup(&ctx);
3413 HMAC_CTX_cleanup(&hctx);
3416 /* Now write out lengths: p points to end of data written */
3418 len = p - ssl_handshake_start(s);
3419 /* Skip ticket lifetime hint */
3420 p = ssl_handshake_start(s) + 4;
3422 if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3424 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3428 /* SSL3_ST_SW_SESSION_TICKET_B */
3429 return ssl_do_write(s);
3432 EVP_CIPHER_CTX_cleanup(&ctx);
3433 HMAC_CTX_cleanup(&hctx);
3434 s->state = SSL_ST_ERR;
3438 int ssl3_send_cert_status(SSL *s)
3440 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3443 * Grow buffer if need be: the length calculation is as
3444 * follows 1 (message type) + 3 (message length) +
3445 * 1 (ocsp response type) + 3 (ocsp response length)
3448 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3449 s->state = SSL_ST_ERR;
3453 p = (unsigned char *)s->init_buf->data;
3456 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3457 /* message length */
3458 l2n3(s->tlsext_ocsp_resplen + 4, p);
3460 *(p++) = s->tlsext_status_type;
3461 /* length of OCSP response */
3462 l2n3(s->tlsext_ocsp_resplen, p);
3463 /* actual response */
3464 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3465 /* number of bytes to write */
3466 s->init_num = 8 + s->tlsext_ocsp_resplen;
3467 s->state = SSL3_ST_SW_CERT_STATUS_B;
3471 /* SSL3_ST_SW_CERT_STATUS_B */
3472 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3475 # ifndef OPENSSL_NO_NEXTPROTONEG
3477 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3478 * It sets the next_proto member in s if found
3480 int ssl3_get_next_proto(SSL *s)
3483 int proto_len, padding_len;
3485 const unsigned char *p;
3488 * Clients cannot send a NextProtocol message if we didn't see the
3489 * extension in their ClientHello
3491 if (!s->s3->next_proto_neg_seen) {
3492 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3493 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3494 s->state = SSL_ST_ERR;
3498 /* See the payload format below */
3499 n = s->method->ssl_get_message(s,
3500 SSL3_ST_SR_NEXT_PROTO_A,
3501 SSL3_ST_SR_NEXT_PROTO_B,
3502 SSL3_MT_NEXT_PROTO, 514, &ok);
3508 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3509 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3510 * ssl3_get_finished).
3512 if (!s->s3->change_cipher_spec) {
3513 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3514 s->state = SSL_ST_ERR;
3519 s->state = SSL_ST_ERR;
3520 return 0; /* The body must be > 1 bytes long */
3523 p = (unsigned char *)s->init_msg;
3526 * The payload looks like:
3528 * uint8 proto[proto_len];
3529 * uint8 padding_len;
3530 * uint8 padding[padding_len];
3533 if (proto_len + 2 > s->init_num) {
3534 s->state = SSL_ST_ERR;
3537 padding_len = p[proto_len + 1];
3538 if (proto_len + padding_len + 2 != s->init_num) {
3539 s->state = SSL_ST_ERR;
3543 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3544 if (!s->next_proto_negotiated) {
3545 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3546 s->state = SSL_ST_ERR;
3549 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3550 s->next_proto_negotiated_len = proto_len;
3558 #define SSLV2_CIPHER_LEN 3
3560 STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, unsigned char *p,
3562 STACK_OF(SSL_CIPHER) **skp,
3565 const SSL_CIPHER *c;
3566 STACK_OF(SSL_CIPHER) *sk;
3570 s->s3->send_connection_binding = 0;
3573 n = SSLV2_CIPHER_LEN;
3575 n = ssl_put_cipher_by_char(s, NULL, NULL);
3577 if (n == 0 || (num % n) != 0) {
3578 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3579 SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
3582 if ((skp == NULL) || (*skp == NULL)) {
3583 sk = sk_SSL_CIPHER_new_null(); /* change perhaps later */
3585 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3590 sk_SSL_CIPHER_zero(sk);
3593 OPENSSL_free(s->cert->ciphers_raw);
3594 s->cert->ciphers_raw = BUF_memdup(p, num);
3595 if (s->cert->ciphers_raw == NULL) {
3596 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3599 s->cert->ciphers_rawlen = (size_t)num;
3601 for (i = 0; i < num; i += n) {
3602 /* Check for TLS_EMPTY_RENEGOTIATION_INFO_SCSV */
3603 if (s->s3 && (n != 3 || !p[0]) &&
3604 (p[n - 2] == ((SSL3_CK_SCSV >> 8) & 0xff)) &&
3605 (p[n - 1] == (SSL3_CK_SCSV & 0xff))) {
3606 /* SCSV fatal if renegotiating */
3607 if (s->renegotiate) {
3608 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3609 SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
3610 ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3613 s->s3->send_connection_binding = 1;
3615 #ifdef OPENSSL_RI_DEBUG
3616 fprintf(stderr, "SCSV received by server\n");
3621 /* Check for TLS_FALLBACK_SCSV */
3622 if ((n != 3 || !p[0]) &&
3623 (p[n - 2] == ((SSL3_CK_FALLBACK_SCSV >> 8) & 0xff)) &&
3624 (p[n - 1] == (SSL3_CK_FALLBACK_SCSV & 0xff))) {
3626 * The SCSV indicates that the client previously tried a higher
3627 * version. Fail if the current version is an unexpected
3630 if (!SSL_ctrl(s, SSL_CTRL_CHECK_PROTO_VERSION, 0, NULL)) {
3631 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST,
3632 SSL_R_INAPPROPRIATE_FALLBACK);
3634 ssl3_send_alert(s, SSL3_AL_FATAL,
3635 SSL_AD_INAPPROPRIATE_FALLBACK);
3644 * We only support SSLv2 format ciphers in SSLv3+ using a
3645 * SSLv2 backward compatible ClientHello. In this case the first
3646 * byte is always 0 for SSLv3 compatible ciphers. Anything else
3647 * is an SSLv2 cipher and we ignore it
3650 c = ssl_get_cipher_by_char(s, &p[1]);
3654 c = ssl_get_cipher_by_char(s, p);
3658 if (!sk_SSL_CIPHER_push(sk, c)) {
3659 SSLerr(SSL_F_SSL_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
3669 if ((skp == NULL) || (*skp == NULL))
3670 sk_SSL_CIPHER_free(sk);