1ebfedea0SLionel Sambuc /* ssl/s3_srvr.c -*- mode:C; c-file-style: "eay" -*- */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
60ebfedea0SLionel Sambuc *
61ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc * are met:
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc * distribution.
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc * software must display the following acknowledgment:
75ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc *
78ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc * openssl-core@openssl.org.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc *
87ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc * acknowledgment:
89ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc *
92ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc * ====================================================================
105ebfedea0SLionel Sambuc *
106ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
108ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc */
111ebfedea0SLionel Sambuc /* ====================================================================
112ebfedea0SLionel Sambuc * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
113ebfedea0SLionel Sambuc *
114ebfedea0SLionel Sambuc * Portions of the attached software ("Contribution") are developed by
115ebfedea0SLionel Sambuc * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
116ebfedea0SLionel Sambuc *
117ebfedea0SLionel Sambuc * The Contribution is licensed pursuant to the OpenSSL open source
118ebfedea0SLionel Sambuc * license provided above.
119ebfedea0SLionel Sambuc *
120ebfedea0SLionel Sambuc * ECC cipher suite support in OpenSSL originally written by
121ebfedea0SLionel Sambuc * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories.
122ebfedea0SLionel Sambuc *
123ebfedea0SLionel Sambuc */
124ebfedea0SLionel Sambuc /* ====================================================================
125ebfedea0SLionel Sambuc * Copyright 2005 Nokia. All rights reserved.
126ebfedea0SLionel Sambuc *
127ebfedea0SLionel Sambuc * The portions of the attached software ("Contribution") is developed by
128ebfedea0SLionel Sambuc * Nokia Corporation and is licensed pursuant to the OpenSSL open source
129ebfedea0SLionel Sambuc * license.
130ebfedea0SLionel Sambuc *
131ebfedea0SLionel Sambuc * The Contribution, originally written by Mika Kousa and Pasi Eronen of
132ebfedea0SLionel Sambuc * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
133ebfedea0SLionel Sambuc * support (see RFC 4279) to OpenSSL.
134ebfedea0SLionel Sambuc *
135ebfedea0SLionel Sambuc * No patent licenses or other rights except those expressly stated in
136ebfedea0SLionel Sambuc * the OpenSSL open source license shall be deemed granted or received
137ebfedea0SLionel Sambuc * expressly, by implication, estoppel, or otherwise.
138ebfedea0SLionel Sambuc *
139ebfedea0SLionel Sambuc * No assurances are provided by Nokia that the Contribution does not
140ebfedea0SLionel Sambuc * infringe the patent or other intellectual property rights of any third
141ebfedea0SLionel Sambuc * party or that the license provides you with all the necessary rights
142ebfedea0SLionel Sambuc * to make use of the Contribution.
143ebfedea0SLionel Sambuc *
144ebfedea0SLionel Sambuc * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
145ebfedea0SLionel Sambuc * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
146ebfedea0SLionel Sambuc * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
147ebfedea0SLionel Sambuc * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
148ebfedea0SLionel Sambuc * OTHERWISE.
149ebfedea0SLionel Sambuc */
150ebfedea0SLionel Sambuc
151ebfedea0SLionel Sambuc #define REUSE_CIPHER_BUG
152ebfedea0SLionel Sambuc #define NETSCAPE_HANG_BUG
153ebfedea0SLionel Sambuc
154ebfedea0SLionel Sambuc #include <stdio.h>
155ebfedea0SLionel Sambuc #include "ssl_locl.h"
156ebfedea0SLionel Sambuc #include "kssl_lcl.h"
157*0a6a1f1dSLionel Sambuc #include "../crypto/constant_time_locl.h"
158ebfedea0SLionel Sambuc #include <openssl/buffer.h>
159ebfedea0SLionel Sambuc #include <openssl/rand.h>
160ebfedea0SLionel Sambuc #include <openssl/objects.h>
161ebfedea0SLionel Sambuc #include <openssl/evp.h>
162ebfedea0SLionel Sambuc #include <openssl/hmac.h>
163ebfedea0SLionel Sambuc #include <openssl/x509.h>
164ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
165ebfedea0SLionel Sambuc # include <openssl/dh.h>
166ebfedea0SLionel Sambuc #endif
167ebfedea0SLionel Sambuc #include <openssl/bn.h>
168ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
169ebfedea0SLionel Sambuc # include <openssl/krb5_asn.h>
170ebfedea0SLionel Sambuc #endif
171ebfedea0SLionel Sambuc #include <openssl/md5.h>
172ebfedea0SLionel Sambuc
173*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SSL3_METHOD
174ebfedea0SLionel Sambuc static const SSL_METHOD *ssl3_get_server_method(int ver);
175ebfedea0SLionel Sambuc
ssl3_get_server_method(int ver)176ebfedea0SLionel Sambuc static const SSL_METHOD *ssl3_get_server_method(int ver)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc if (ver == SSL3_VERSION)
179ebfedea0SLionel Sambuc return (SSLv3_server_method());
180ebfedea0SLionel Sambuc else
181ebfedea0SLionel Sambuc return (NULL);
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc
IMPLEMENT_ssl3_meth_func(SSLv3_server_method,ssl3_accept,ssl_undefined_function,ssl3_get_server_method)184*0a6a1f1dSLionel Sambuc IMPLEMENT_ssl3_meth_func(SSLv3_server_method,
185*0a6a1f1dSLionel Sambuc ssl3_accept,
186*0a6a1f1dSLionel Sambuc ssl_undefined_function, ssl3_get_server_method)
187*0a6a1f1dSLionel Sambuc #endif
188ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
189ebfedea0SLionel Sambuc static int ssl_check_srp_ext_ClientHello(SSL *s, int *al)
190ebfedea0SLionel Sambuc {
191ebfedea0SLionel Sambuc int ret = SSL_ERROR_NONE;
192ebfedea0SLionel Sambuc
193ebfedea0SLionel Sambuc *al = SSL_AD_UNRECOGNIZED_NAME;
194ebfedea0SLionel Sambuc
195ebfedea0SLionel Sambuc if ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) &&
196*0a6a1f1dSLionel Sambuc (s->srp_ctx.TLS_ext_srp_username_callback != NULL)) {
197*0a6a1f1dSLionel Sambuc if (s->srp_ctx.login == NULL) {
198*0a6a1f1dSLionel Sambuc /*
199*0a6a1f1dSLionel Sambuc * RFC 5054 says SHOULD reject, we do so if There is no srp
200*0a6a1f1dSLionel Sambuc * login name
201*0a6a1f1dSLionel Sambuc */
202ebfedea0SLionel Sambuc ret = SSL3_AL_FATAL;
203ebfedea0SLionel Sambuc *al = SSL_AD_UNKNOWN_PSK_IDENTITY;
204*0a6a1f1dSLionel Sambuc } else {
205ebfedea0SLionel Sambuc ret = SSL_srp_server_param_with_username(s, al);
206ebfedea0SLionel Sambuc }
207ebfedea0SLionel Sambuc }
208ebfedea0SLionel Sambuc return ret;
209ebfedea0SLionel Sambuc }
210ebfedea0SLionel Sambuc #endif
211ebfedea0SLionel Sambuc
ssl3_accept(SSL * s)212ebfedea0SLionel Sambuc int ssl3_accept(SSL *s)
213ebfedea0SLionel Sambuc {
214ebfedea0SLionel Sambuc BUF_MEM *buf;
215ebfedea0SLionel Sambuc unsigned long alg_k, Time = (unsigned long)time(NULL);
216ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val) = NULL;
217ebfedea0SLionel Sambuc int ret = -1;
218ebfedea0SLionel Sambuc int new_state, state, skip = 0;
219ebfedea0SLionel Sambuc
220ebfedea0SLionel Sambuc RAND_add(&Time, sizeof(Time), 0);
221ebfedea0SLionel Sambuc ERR_clear_error();
222ebfedea0SLionel Sambuc clear_sys_error();
223ebfedea0SLionel Sambuc
224ebfedea0SLionel Sambuc if (s->info_callback != NULL)
225ebfedea0SLionel Sambuc cb = s->info_callback;
226ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
227ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
228ebfedea0SLionel Sambuc
229ebfedea0SLionel Sambuc /* init things to blank */
230ebfedea0SLionel Sambuc s->in_handshake++;
231*0a6a1f1dSLionel Sambuc if (!SSL_in_init(s) || SSL_in_before(s))
232*0a6a1f1dSLionel Sambuc SSL_clear(s);
233ebfedea0SLionel Sambuc
234*0a6a1f1dSLionel Sambuc if (s->cert == NULL) {
235ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_NO_CERTIFICATE_SET);
236ebfedea0SLionel Sambuc return (-1);
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
239*0a6a1f1dSLionel Sambuc /*
240*0a6a1f1dSLionel Sambuc * If we're awaiting a HeartbeatResponse, pretend we already got and
241*0a6a1f1dSLionel Sambuc * don't await it anymore, because Heartbeats don't make sense during
242*0a6a1f1dSLionel Sambuc * handshakes anyway.
243ebfedea0SLionel Sambuc */
244*0a6a1f1dSLionel Sambuc if (s->tlsext_hb_pending) {
245ebfedea0SLionel Sambuc s->tlsext_hb_pending = 0;
246ebfedea0SLionel Sambuc s->tlsext_hb_seq++;
247ebfedea0SLionel Sambuc }
248ebfedea0SLionel Sambuc #endif
249ebfedea0SLionel Sambuc
250*0a6a1f1dSLionel Sambuc for (;;) {
251ebfedea0SLionel Sambuc state = s->state;
252ebfedea0SLionel Sambuc
253*0a6a1f1dSLionel Sambuc switch (s->state) {
254ebfedea0SLionel Sambuc case SSL_ST_RENEGOTIATE:
255ebfedea0SLionel Sambuc s->renegotiate = 1;
256ebfedea0SLionel Sambuc /* s->state=SSL_ST_ACCEPT; */
257ebfedea0SLionel Sambuc
258ebfedea0SLionel Sambuc case SSL_ST_BEFORE:
259ebfedea0SLionel Sambuc case SSL_ST_ACCEPT:
260ebfedea0SLionel Sambuc case SSL_ST_BEFORE | SSL_ST_ACCEPT:
261ebfedea0SLionel Sambuc case SSL_ST_OK | SSL_ST_ACCEPT:
262ebfedea0SLionel Sambuc
263ebfedea0SLionel Sambuc s->server = 1;
264*0a6a1f1dSLionel Sambuc if (cb != NULL)
265*0a6a1f1dSLionel Sambuc cb(s, SSL_CB_HANDSHAKE_START, 1);
266ebfedea0SLionel Sambuc
267*0a6a1f1dSLionel Sambuc if ((s->version >> 8) != 3) {
268ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
269*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
270ebfedea0SLionel Sambuc return -1;
271ebfedea0SLionel Sambuc }
272ebfedea0SLionel Sambuc s->type = SSL_ST_ACCEPT;
273ebfedea0SLionel Sambuc
274*0a6a1f1dSLionel Sambuc if (s->init_buf == NULL) {
275*0a6a1f1dSLionel Sambuc if ((buf = BUF_MEM_new()) == NULL) {
276ebfedea0SLionel Sambuc ret = -1;
277*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
278ebfedea0SLionel Sambuc goto end;
279ebfedea0SLionel Sambuc }
280*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
281*0a6a1f1dSLionel Sambuc BUF_MEM_free(buf);
282ebfedea0SLionel Sambuc ret = -1;
283*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
284ebfedea0SLionel Sambuc goto end;
285ebfedea0SLionel Sambuc }
286ebfedea0SLionel Sambuc s->init_buf = buf;
287ebfedea0SLionel Sambuc }
288ebfedea0SLionel Sambuc
289*0a6a1f1dSLionel Sambuc if (!ssl3_setup_buffers(s)) {
290ebfedea0SLionel Sambuc ret = -1;
291*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
292ebfedea0SLionel Sambuc goto end;
293ebfedea0SLionel Sambuc }
294ebfedea0SLionel Sambuc
295ebfedea0SLionel Sambuc s->init_num = 0;
296ebfedea0SLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_SGC_RESTART_DONE;
297*0a6a1f1dSLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
298*0a6a1f1dSLionel Sambuc /*
299*0a6a1f1dSLionel Sambuc * Should have been reset by ssl3_get_finished, too.
300ebfedea0SLionel Sambuc */
301*0a6a1f1dSLionel Sambuc s->s3->change_cipher_spec = 0;
302*0a6a1f1dSLionel Sambuc
303*0a6a1f1dSLionel Sambuc if (s->state != SSL_ST_RENEGOTIATE) {
304*0a6a1f1dSLionel Sambuc /*
305*0a6a1f1dSLionel Sambuc * Ok, we now need to push on a buffering BIO so that the
306*0a6a1f1dSLionel Sambuc * output is sent in a way that TCP likes :-)
307*0a6a1f1dSLionel Sambuc */
308*0a6a1f1dSLionel Sambuc if (!ssl_init_wbio_buffer(s, 1)) {
309*0a6a1f1dSLionel Sambuc ret = -1;
310*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
311*0a6a1f1dSLionel Sambuc goto end;
312*0a6a1f1dSLionel Sambuc }
313ebfedea0SLionel Sambuc
314ebfedea0SLionel Sambuc ssl3_init_finished_mac(s);
315ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CLNT_HELLO_A;
316ebfedea0SLionel Sambuc s->ctx->stats.sess_accept++;
317*0a6a1f1dSLionel Sambuc } else if (!s->s3->send_connection_binding &&
318*0a6a1f1dSLionel Sambuc !(s->options &
319*0a6a1f1dSLionel Sambuc SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
320*0a6a1f1dSLionel Sambuc /*
321*0a6a1f1dSLionel Sambuc * Server attempting to renegotiate with client that doesn't
322*0a6a1f1dSLionel Sambuc * support secure renegotiation.
323ebfedea0SLionel Sambuc */
324*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT,
325*0a6a1f1dSLionel Sambuc SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
326ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
327ebfedea0SLionel Sambuc ret = -1;
328*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
329ebfedea0SLionel Sambuc goto end;
330*0a6a1f1dSLionel Sambuc } else {
331*0a6a1f1dSLionel Sambuc /*
332*0a6a1f1dSLionel Sambuc * s->state == SSL_ST_RENEGOTIATE, we will just send a
333*0a6a1f1dSLionel Sambuc * HelloRequest
334*0a6a1f1dSLionel Sambuc */
335ebfedea0SLionel Sambuc s->ctx->stats.sess_accept_renegotiate++;
336ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_HELLO_REQ_A;
337ebfedea0SLionel Sambuc }
338ebfedea0SLionel Sambuc break;
339ebfedea0SLionel Sambuc
340ebfedea0SLionel Sambuc case SSL3_ST_SW_HELLO_REQ_A:
341ebfedea0SLionel Sambuc case SSL3_ST_SW_HELLO_REQ_B:
342ebfedea0SLionel Sambuc
343ebfedea0SLionel Sambuc s->shutdown = 0;
344ebfedea0SLionel Sambuc ret = ssl3_send_hello_request(s);
345*0a6a1f1dSLionel Sambuc if (ret <= 0)
346*0a6a1f1dSLionel Sambuc goto end;
347ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C;
348ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_FLUSH;
349ebfedea0SLionel Sambuc s->init_num = 0;
350ebfedea0SLionel Sambuc
351ebfedea0SLionel Sambuc ssl3_init_finished_mac(s);
352ebfedea0SLionel Sambuc break;
353ebfedea0SLionel Sambuc
354ebfedea0SLionel Sambuc case SSL3_ST_SW_HELLO_REQ_C:
355ebfedea0SLionel Sambuc s->state = SSL_ST_OK;
356ebfedea0SLionel Sambuc break;
357ebfedea0SLionel Sambuc
358ebfedea0SLionel Sambuc case SSL3_ST_SR_CLNT_HELLO_A:
359ebfedea0SLionel Sambuc case SSL3_ST_SR_CLNT_HELLO_B:
360ebfedea0SLionel Sambuc case SSL3_ST_SR_CLNT_HELLO_C:
361ebfedea0SLionel Sambuc
362ebfedea0SLionel Sambuc s->shutdown = 0;
363*0a6a1f1dSLionel Sambuc if (s->rwstate != SSL_X509_LOOKUP) {
364ebfedea0SLionel Sambuc ret = ssl3_get_client_hello(s);
365*0a6a1f1dSLionel Sambuc if (ret <= 0)
366*0a6a1f1dSLionel Sambuc goto end;
367ebfedea0SLionel Sambuc }
368ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
369ebfedea0SLionel Sambuc {
370ebfedea0SLionel Sambuc int al;
371*0a6a1f1dSLionel Sambuc if ((ret = ssl_check_srp_ext_ClientHello(s, &al)) < 0) {
372*0a6a1f1dSLionel Sambuc /*
373*0a6a1f1dSLionel Sambuc * callback indicates firther work to be done
374*0a6a1f1dSLionel Sambuc */
375ebfedea0SLionel Sambuc s->rwstate = SSL_X509_LOOKUP;
376ebfedea0SLionel Sambuc goto end;
377ebfedea0SLionel Sambuc }
378*0a6a1f1dSLionel Sambuc if (ret != SSL_ERROR_NONE) {
379ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
380*0a6a1f1dSLionel Sambuc /*
381*0a6a1f1dSLionel Sambuc * This is not really an error but the only means to for
382*0a6a1f1dSLionel Sambuc * a client to detect whether srp is supported.
383*0a6a1f1dSLionel Sambuc */
384ebfedea0SLionel Sambuc if (al != TLS1_AD_UNKNOWN_PSK_IDENTITY)
385ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_CLIENTHELLO_TLSEXT);
386ebfedea0SLionel Sambuc ret = SSL_TLSEXT_ERR_ALERT_FATAL;
387ebfedea0SLionel Sambuc ret = -1;
388*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
389ebfedea0SLionel Sambuc goto end;
390ebfedea0SLionel Sambuc }
391ebfedea0SLionel Sambuc }
392ebfedea0SLionel Sambuc #endif
393ebfedea0SLionel Sambuc
394ebfedea0SLionel Sambuc s->renegotiate = 2;
395ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SRVR_HELLO_A;
396ebfedea0SLionel Sambuc s->init_num = 0;
397ebfedea0SLionel Sambuc break;
398ebfedea0SLionel Sambuc
399ebfedea0SLionel Sambuc case SSL3_ST_SW_SRVR_HELLO_A:
400ebfedea0SLionel Sambuc case SSL3_ST_SW_SRVR_HELLO_B:
401ebfedea0SLionel Sambuc ret = ssl3_send_server_hello(s);
402*0a6a1f1dSLionel Sambuc if (ret <= 0)
403*0a6a1f1dSLionel Sambuc goto end;
404ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
405*0a6a1f1dSLionel Sambuc if (s->hit) {
406ebfedea0SLionel Sambuc if (s->tlsext_ticket_expected)
407ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SESSION_TICKET_A;
408ebfedea0SLionel Sambuc else
409ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CHANGE_A;
410ebfedea0SLionel Sambuc }
411ebfedea0SLionel Sambuc #else
412ebfedea0SLionel Sambuc if (s->hit)
413ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CHANGE_A;
414ebfedea0SLionel Sambuc #endif
415ebfedea0SLionel Sambuc else
416ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_A;
417ebfedea0SLionel Sambuc s->init_num = 0;
418ebfedea0SLionel Sambuc break;
419ebfedea0SLionel Sambuc
420ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_A:
421ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_B:
422ebfedea0SLionel Sambuc /* Check if it is anon DH or anon ECDH, */
423ebfedea0SLionel Sambuc /* normal PSK or KRB5 or SRP */
424*0a6a1f1dSLionel Sambuc if (!
425*0a6a1f1dSLionel Sambuc (s->s3->tmp.
426*0a6a1f1dSLionel Sambuc new_cipher->algorithm_auth & (SSL_aNULL | SSL_aKRB5 |
427*0a6a1f1dSLionel Sambuc SSL_aSRP))
428*0a6a1f1dSLionel Sambuc && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
429ebfedea0SLionel Sambuc ret = ssl3_send_server_certificate(s);
430*0a6a1f1dSLionel Sambuc if (ret <= 0)
431*0a6a1f1dSLionel Sambuc goto end;
432ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
433ebfedea0SLionel Sambuc if (s->tlsext_status_expected)
434ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_STATUS_A;
435ebfedea0SLionel Sambuc else
436ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_KEY_EXCH_A;
437*0a6a1f1dSLionel Sambuc } else {
438ebfedea0SLionel Sambuc skip = 1;
439ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_KEY_EXCH_A;
440ebfedea0SLionel Sambuc }
441ebfedea0SLionel Sambuc #else
442*0a6a1f1dSLionel Sambuc } else
443ebfedea0SLionel Sambuc skip = 1;
444ebfedea0SLionel Sambuc
445ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_KEY_EXCH_A;
446ebfedea0SLionel Sambuc #endif
447ebfedea0SLionel Sambuc s->init_num = 0;
448ebfedea0SLionel Sambuc break;
449ebfedea0SLionel Sambuc
450ebfedea0SLionel Sambuc case SSL3_ST_SW_KEY_EXCH_A:
451ebfedea0SLionel Sambuc case SSL3_ST_SW_KEY_EXCH_B:
452ebfedea0SLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
453ebfedea0SLionel Sambuc
454*0a6a1f1dSLionel Sambuc /*
455*0a6a1f1dSLionel Sambuc * clear this, it may get reset by
456*0a6a1f1dSLionel Sambuc * send_server_key_exchange
457*0a6a1f1dSLionel Sambuc */
458ebfedea0SLionel Sambuc s->s3->tmp.use_rsa_tmp = 0;
459ebfedea0SLionel Sambuc
460*0a6a1f1dSLionel Sambuc /*
461*0a6a1f1dSLionel Sambuc * only send if a DH key exchange, fortezza or RSA but we have a
462*0a6a1f1dSLionel Sambuc * sign only certificate PSK: may send PSK identity hints For
463*0a6a1f1dSLionel Sambuc * ECC ciphersuites, we send a serverKeyExchange message only if
464*0a6a1f1dSLionel Sambuc * the cipher suite is either ECDH-anon or ECDHE. In other cases,
465*0a6a1f1dSLionel Sambuc * the server certificate contains the server's public key for
466*0a6a1f1dSLionel Sambuc * key exchange.
467ebfedea0SLionel Sambuc */
468*0a6a1f1dSLionel Sambuc if (0
469*0a6a1f1dSLionel Sambuc /*
470*0a6a1f1dSLionel Sambuc * PSK: send ServerKeyExchange if PSK identity hint if
471*0a6a1f1dSLionel Sambuc * provided
472*0a6a1f1dSLionel Sambuc */
473ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
474ebfedea0SLionel Sambuc || ((alg_k & SSL_kPSK) && s->ctx->psk_identity_hint)
475ebfedea0SLionel Sambuc #endif
476ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
477ebfedea0SLionel Sambuc /* SRP: send ServerKeyExchange */
478ebfedea0SLionel Sambuc || (alg_k & SSL_kSRP)
479ebfedea0SLionel Sambuc #endif
480ebfedea0SLionel Sambuc || (alg_k & (SSL_kDHr | SSL_kDHd | SSL_kEDH))
481ebfedea0SLionel Sambuc || (alg_k & SSL_kEECDH)
482ebfedea0SLionel Sambuc || ((alg_k & SSL_kRSA)
483ebfedea0SLionel Sambuc && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
484ebfedea0SLionel Sambuc || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
485*0a6a1f1dSLionel Sambuc && EVP_PKEY_size(s->cert->pkeys
486*0a6a1f1dSLionel Sambuc [SSL_PKEY_RSA_ENC].privatekey) *
487*0a6a1f1dSLionel Sambuc 8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
488ebfedea0SLionel Sambuc )
489ebfedea0SLionel Sambuc )
490ebfedea0SLionel Sambuc )
491*0a6a1f1dSLionel Sambuc ) {
492ebfedea0SLionel Sambuc ret = ssl3_send_server_key_exchange(s);
493*0a6a1f1dSLionel Sambuc if (ret <= 0)
494*0a6a1f1dSLionel Sambuc goto end;
495*0a6a1f1dSLionel Sambuc } else
496ebfedea0SLionel Sambuc skip = 1;
497ebfedea0SLionel Sambuc
498ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_REQ_A;
499ebfedea0SLionel Sambuc s->init_num = 0;
500ebfedea0SLionel Sambuc break;
501ebfedea0SLionel Sambuc
502ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_REQ_A:
503ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_REQ_B:
504ebfedea0SLionel Sambuc if ( /* don't request cert unless asked for it: */
505ebfedea0SLionel Sambuc !(s->verify_mode & SSL_VERIFY_PEER) ||
506*0a6a1f1dSLionel Sambuc /*
507*0a6a1f1dSLionel Sambuc * if SSL_VERIFY_CLIENT_ONCE is set, don't request cert
508*0a6a1f1dSLionel Sambuc * during re-negotiation:
509*0a6a1f1dSLionel Sambuc */
510ebfedea0SLionel Sambuc ((s->session->peer != NULL) &&
511ebfedea0SLionel Sambuc (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
512*0a6a1f1dSLionel Sambuc /*
513*0a6a1f1dSLionel Sambuc * never request cert in anonymous ciphersuites (see
514*0a6a1f1dSLionel Sambuc * section "Certificate request" in SSL 3 drafts and in
515*0a6a1f1dSLionel Sambuc * RFC 2246):
516*0a6a1f1dSLionel Sambuc */
517ebfedea0SLionel Sambuc ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) &&
518*0a6a1f1dSLionel Sambuc /*
519*0a6a1f1dSLionel Sambuc * ... except when the application insists on
520*0a6a1f1dSLionel Sambuc * verification (against the specs, but s3_clnt.c accepts
521*0a6a1f1dSLionel Sambuc * this for SSL 3)
522*0a6a1f1dSLionel Sambuc */
523ebfedea0SLionel Sambuc !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
524*0a6a1f1dSLionel Sambuc /*
525*0a6a1f1dSLionel Sambuc * never request cert in Kerberos ciphersuites
526*0a6a1f1dSLionel Sambuc */
527*0a6a1f1dSLionel Sambuc (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) ||
528*0a6a1f1dSLionel Sambuc /* don't request certificate for SRP auth */
529*0a6a1f1dSLionel Sambuc (s->s3->tmp.new_cipher->algorithm_auth & SSL_aSRP)
530*0a6a1f1dSLionel Sambuc /*
531*0a6a1f1dSLionel Sambuc * With normal PSK Certificates and Certificate Requests
532*0a6a1f1dSLionel Sambuc * are omitted
533*0a6a1f1dSLionel Sambuc */
534*0a6a1f1dSLionel Sambuc || (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
535ebfedea0SLionel Sambuc /* no cert request */
536ebfedea0SLionel Sambuc skip = 1;
537ebfedea0SLionel Sambuc s->s3->tmp.cert_request = 0;
538ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SRVR_DONE_A;
539*0a6a1f1dSLionel Sambuc if (s->s3->handshake_buffer) {
540*0a6a1f1dSLionel Sambuc if (!ssl3_digest_cached_records(s)) {
541*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
542ebfedea0SLionel Sambuc return -1;
543ebfedea0SLionel Sambuc }
544*0a6a1f1dSLionel Sambuc }
545*0a6a1f1dSLionel Sambuc } else {
546ebfedea0SLionel Sambuc s->s3->tmp.cert_request = 1;
547ebfedea0SLionel Sambuc ret = ssl3_send_certificate_request(s);
548*0a6a1f1dSLionel Sambuc if (ret <= 0)
549*0a6a1f1dSLionel Sambuc goto end;
550ebfedea0SLionel Sambuc #ifndef NETSCAPE_HANG_BUG
551ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SRVR_DONE_A;
552ebfedea0SLionel Sambuc #else
553ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_FLUSH;
554ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
555ebfedea0SLionel Sambuc #endif
556ebfedea0SLionel Sambuc s->init_num = 0;
557ebfedea0SLionel Sambuc }
558ebfedea0SLionel Sambuc break;
559ebfedea0SLionel Sambuc
560ebfedea0SLionel Sambuc case SSL3_ST_SW_SRVR_DONE_A:
561ebfedea0SLionel Sambuc case SSL3_ST_SW_SRVR_DONE_B:
562ebfedea0SLionel Sambuc ret = ssl3_send_server_done(s);
563*0a6a1f1dSLionel Sambuc if (ret <= 0)
564*0a6a1f1dSLionel Sambuc goto end;
565ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SR_CERT_A;
566ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_FLUSH;
567ebfedea0SLionel Sambuc s->init_num = 0;
568ebfedea0SLionel Sambuc break;
569ebfedea0SLionel Sambuc
570ebfedea0SLionel Sambuc case SSL3_ST_SW_FLUSH:
571ebfedea0SLionel Sambuc
572*0a6a1f1dSLionel Sambuc /*
573*0a6a1f1dSLionel Sambuc * This code originally checked to see if any data was pending
574*0a6a1f1dSLionel Sambuc * using BIO_CTRL_INFO and then flushed. This caused problems as
575*0a6a1f1dSLionel Sambuc * documented in PR#1939. The proposed fix doesn't completely
576*0a6a1f1dSLionel Sambuc * resolve this issue as buggy implementations of
577*0a6a1f1dSLionel Sambuc * BIO_CTRL_PENDING still exist. So instead we just flush
578ebfedea0SLionel Sambuc * unconditionally.
579ebfedea0SLionel Sambuc */
580ebfedea0SLionel Sambuc
581ebfedea0SLionel Sambuc s->rwstate = SSL_WRITING;
582*0a6a1f1dSLionel Sambuc if (BIO_flush(s->wbio) <= 0) {
583ebfedea0SLionel Sambuc ret = -1;
584ebfedea0SLionel Sambuc goto end;
585ebfedea0SLionel Sambuc }
586ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
587ebfedea0SLionel Sambuc
588ebfedea0SLionel Sambuc s->state = s->s3->tmp.next_state;
589ebfedea0SLionel Sambuc break;
590ebfedea0SLionel Sambuc
591ebfedea0SLionel Sambuc case SSL3_ST_SR_CERT_A:
592ebfedea0SLionel Sambuc case SSL3_ST_SR_CERT_B:
593ebfedea0SLionel Sambuc /* Check for second client hello (MS SGC) */
594ebfedea0SLionel Sambuc ret = ssl3_check_client_hello(s);
595ebfedea0SLionel Sambuc if (ret <= 0)
596ebfedea0SLionel Sambuc goto end;
597ebfedea0SLionel Sambuc if (ret == 2)
598ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CLNT_HELLO_C;
599ebfedea0SLionel Sambuc else {
600*0a6a1f1dSLionel Sambuc if (s->s3->tmp.cert_request) {
601ebfedea0SLionel Sambuc ret = ssl3_get_client_certificate(s);
602*0a6a1f1dSLionel Sambuc if (ret <= 0)
603*0a6a1f1dSLionel Sambuc goto end;
604ebfedea0SLionel Sambuc }
605ebfedea0SLionel Sambuc s->init_num = 0;
606ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_KEY_EXCH_A;
607ebfedea0SLionel Sambuc }
608ebfedea0SLionel Sambuc break;
609ebfedea0SLionel Sambuc
610ebfedea0SLionel Sambuc case SSL3_ST_SR_KEY_EXCH_A:
611ebfedea0SLionel Sambuc case SSL3_ST_SR_KEY_EXCH_B:
612ebfedea0SLionel Sambuc ret = ssl3_get_client_key_exchange(s);
613ebfedea0SLionel Sambuc if (ret <= 0)
614ebfedea0SLionel Sambuc goto end;
615*0a6a1f1dSLionel Sambuc if (ret == 2) {
616*0a6a1f1dSLionel Sambuc /*
617*0a6a1f1dSLionel Sambuc * For the ECDH ciphersuites when the client sends its ECDH
618*0a6a1f1dSLionel Sambuc * pub key in a certificate, the CertificateVerify message is
619*0a6a1f1dSLionel Sambuc * not sent. Also for GOST ciphersuites when the client uses
620*0a6a1f1dSLionel Sambuc * its key from the certificate for key exchange.
621ebfedea0SLionel Sambuc */
622ebfedea0SLionel Sambuc #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
623ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_FINISHED_A;
624ebfedea0SLionel Sambuc #else
625ebfedea0SLionel Sambuc if (s->s3->next_proto_neg_seen)
626ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_NEXT_PROTO_A;
627ebfedea0SLionel Sambuc else
628ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_FINISHED_A;
629ebfedea0SLionel Sambuc #endif
630ebfedea0SLionel Sambuc s->init_num = 0;
631*0a6a1f1dSLionel Sambuc } else if (TLS1_get_version(s) >= TLS1_2_VERSION) {
632ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CERT_VRFY_A;
633ebfedea0SLionel Sambuc s->init_num = 0;
634ebfedea0SLionel Sambuc if (!s->session->peer)
635ebfedea0SLionel Sambuc break;
636*0a6a1f1dSLionel Sambuc /*
637*0a6a1f1dSLionel Sambuc * For TLS v1.2 freeze the handshake buffer at this point and
638*0a6a1f1dSLionel Sambuc * digest cached records.
639ebfedea0SLionel Sambuc */
640*0a6a1f1dSLionel Sambuc if (!s->s3->handshake_buffer) {
641ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
642*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
643ebfedea0SLionel Sambuc return -1;
644ebfedea0SLionel Sambuc }
645ebfedea0SLionel Sambuc s->s3->flags |= TLS1_FLAGS_KEEP_HANDSHAKE;
646*0a6a1f1dSLionel Sambuc if (!ssl3_digest_cached_records(s)) {
647*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
648ebfedea0SLionel Sambuc return -1;
649ebfedea0SLionel Sambuc }
650*0a6a1f1dSLionel Sambuc } else {
651ebfedea0SLionel Sambuc int offset = 0;
652ebfedea0SLionel Sambuc int dgst_num;
653ebfedea0SLionel Sambuc
654ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CERT_VRFY_A;
655ebfedea0SLionel Sambuc s->init_num = 0;
656ebfedea0SLionel Sambuc
657*0a6a1f1dSLionel Sambuc /*
658*0a6a1f1dSLionel Sambuc * We need to get hashes here so if there is a client cert,
659*0a6a1f1dSLionel Sambuc * it can be verified FIXME - digest processing for
660*0a6a1f1dSLionel Sambuc * CertificateVerify should be generalized. But it is next
661*0a6a1f1dSLionel Sambuc * step
662ebfedea0SLionel Sambuc */
663*0a6a1f1dSLionel Sambuc if (s->s3->handshake_buffer) {
664*0a6a1f1dSLionel Sambuc if (!ssl3_digest_cached_records(s)) {
665*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
666ebfedea0SLionel Sambuc return -1;
667*0a6a1f1dSLionel Sambuc }
668*0a6a1f1dSLionel Sambuc }
669ebfedea0SLionel Sambuc for (dgst_num = 0; dgst_num < SSL_MAX_DIGEST; dgst_num++)
670*0a6a1f1dSLionel Sambuc if (s->s3->handshake_dgst[dgst_num]) {
671ebfedea0SLionel Sambuc int dgst_size;
672ebfedea0SLionel Sambuc
673*0a6a1f1dSLionel Sambuc s->method->ssl3_enc->cert_verify_mac(s,
674*0a6a1f1dSLionel Sambuc EVP_MD_CTX_type
675*0a6a1f1dSLionel Sambuc (s->
676*0a6a1f1dSLionel Sambuc s3->handshake_dgst
677*0a6a1f1dSLionel Sambuc [dgst_num]),
678*0a6a1f1dSLionel Sambuc &(s->s3->
679*0a6a1f1dSLionel Sambuc tmp.cert_verify_md
680*0a6a1f1dSLionel Sambuc [offset]));
681*0a6a1f1dSLionel Sambuc dgst_size =
682*0a6a1f1dSLionel Sambuc EVP_MD_CTX_size(s->s3->handshake_dgst[dgst_num]);
683*0a6a1f1dSLionel Sambuc if (dgst_size < 0) {
684*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
685ebfedea0SLionel Sambuc ret = -1;
686ebfedea0SLionel Sambuc goto end;
687ebfedea0SLionel Sambuc }
688ebfedea0SLionel Sambuc offset += dgst_size;
689ebfedea0SLionel Sambuc }
690ebfedea0SLionel Sambuc }
691ebfedea0SLionel Sambuc break;
692ebfedea0SLionel Sambuc
693ebfedea0SLionel Sambuc case SSL3_ST_SR_CERT_VRFY_A:
694ebfedea0SLionel Sambuc case SSL3_ST_SR_CERT_VRFY_B:
695ebfedea0SLionel Sambuc ret = ssl3_get_cert_verify(s);
696*0a6a1f1dSLionel Sambuc if (ret <= 0)
697*0a6a1f1dSLionel Sambuc goto end;
698ebfedea0SLionel Sambuc
699ebfedea0SLionel Sambuc #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
700ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_FINISHED_A;
701ebfedea0SLionel Sambuc #else
702ebfedea0SLionel Sambuc if (s->s3->next_proto_neg_seen)
703ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_NEXT_PROTO_A;
704ebfedea0SLionel Sambuc else
705ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_FINISHED_A;
706ebfedea0SLionel Sambuc #endif
707ebfedea0SLionel Sambuc s->init_num = 0;
708ebfedea0SLionel Sambuc break;
709ebfedea0SLionel Sambuc
710ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
711ebfedea0SLionel Sambuc case SSL3_ST_SR_NEXT_PROTO_A:
712ebfedea0SLionel Sambuc case SSL3_ST_SR_NEXT_PROTO_B:
713*0a6a1f1dSLionel Sambuc /*
714*0a6a1f1dSLionel Sambuc * Enable CCS for NPN. Receiving a CCS clears the flag, so make
715*0a6a1f1dSLionel Sambuc * sure not to re-enable it to ban duplicates. This *should* be the
716*0a6a1f1dSLionel Sambuc * first time we have received one - but we check anyway to be
717*0a6a1f1dSLionel Sambuc * cautious.
718*0a6a1f1dSLionel Sambuc * s->s3->change_cipher_spec is set when a CCS is
719*0a6a1f1dSLionel Sambuc * processed in s3_pkt.c, and remains set until
720*0a6a1f1dSLionel Sambuc * the client's Finished message is read.
721*0a6a1f1dSLionel Sambuc */
722*0a6a1f1dSLionel Sambuc if (!s->s3->change_cipher_spec)
723*0a6a1f1dSLionel Sambuc s->s3->flags |= SSL3_FLAGS_CCS_OK;
724*0a6a1f1dSLionel Sambuc
725ebfedea0SLionel Sambuc ret = ssl3_get_next_proto(s);
726*0a6a1f1dSLionel Sambuc if (ret <= 0)
727*0a6a1f1dSLionel Sambuc goto end;
728ebfedea0SLionel Sambuc s->init_num = 0;
729ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_FINISHED_A;
730ebfedea0SLionel Sambuc break;
731ebfedea0SLionel Sambuc #endif
732ebfedea0SLionel Sambuc
733ebfedea0SLionel Sambuc case SSL3_ST_SR_FINISHED_A:
734ebfedea0SLionel Sambuc case SSL3_ST_SR_FINISHED_B:
735*0a6a1f1dSLionel Sambuc /*
736*0a6a1f1dSLionel Sambuc * Enable CCS for handshakes without NPN. In NPN the CCS flag has
737*0a6a1f1dSLionel Sambuc * already been set. Receiving a CCS clears the flag, so make
738*0a6a1f1dSLionel Sambuc * sure not to re-enable it to ban duplicates.
739*0a6a1f1dSLionel Sambuc * s->s3->change_cipher_spec is set when a CCS is
740*0a6a1f1dSLionel Sambuc * processed in s3_pkt.c, and remains set until
741*0a6a1f1dSLionel Sambuc * the client's Finished message is read.
742*0a6a1f1dSLionel Sambuc */
743*0a6a1f1dSLionel Sambuc if (!s->s3->change_cipher_spec)
744*0a6a1f1dSLionel Sambuc s->s3->flags |= SSL3_FLAGS_CCS_OK;
745ebfedea0SLionel Sambuc ret = ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A,
746ebfedea0SLionel Sambuc SSL3_ST_SR_FINISHED_B);
747*0a6a1f1dSLionel Sambuc if (ret <= 0)
748*0a6a1f1dSLionel Sambuc goto end;
749ebfedea0SLionel Sambuc if (s->hit)
750ebfedea0SLionel Sambuc s->state = SSL_ST_OK;
751ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
752ebfedea0SLionel Sambuc else if (s->tlsext_ticket_expected)
753ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SESSION_TICKET_A;
754ebfedea0SLionel Sambuc #endif
755ebfedea0SLionel Sambuc else
756ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CHANGE_A;
757ebfedea0SLionel Sambuc s->init_num = 0;
758ebfedea0SLionel Sambuc break;
759ebfedea0SLionel Sambuc
760ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
761ebfedea0SLionel Sambuc case SSL3_ST_SW_SESSION_TICKET_A:
762ebfedea0SLionel Sambuc case SSL3_ST_SW_SESSION_TICKET_B:
763ebfedea0SLionel Sambuc ret = ssl3_send_newsession_ticket(s);
764*0a6a1f1dSLionel Sambuc if (ret <= 0)
765*0a6a1f1dSLionel Sambuc goto end;
766ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CHANGE_A;
767ebfedea0SLionel Sambuc s->init_num = 0;
768ebfedea0SLionel Sambuc break;
769ebfedea0SLionel Sambuc
770ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_STATUS_A:
771ebfedea0SLionel Sambuc case SSL3_ST_SW_CERT_STATUS_B:
772ebfedea0SLionel Sambuc ret = ssl3_send_cert_status(s);
773*0a6a1f1dSLionel Sambuc if (ret <= 0)
774*0a6a1f1dSLionel Sambuc goto end;
775ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_KEY_EXCH_A;
776ebfedea0SLionel Sambuc s->init_num = 0;
777ebfedea0SLionel Sambuc break;
778ebfedea0SLionel Sambuc
779ebfedea0SLionel Sambuc #endif
780ebfedea0SLionel Sambuc
781ebfedea0SLionel Sambuc case SSL3_ST_SW_CHANGE_A:
782ebfedea0SLionel Sambuc case SSL3_ST_SW_CHANGE_B:
783ebfedea0SLionel Sambuc
784ebfedea0SLionel Sambuc s->session->cipher = s->s3->tmp.new_cipher;
785*0a6a1f1dSLionel Sambuc if (!s->method->ssl3_enc->setup_key_block(s)) {
786*0a6a1f1dSLionel Sambuc ret = -1;
787*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
788*0a6a1f1dSLionel Sambuc goto end;
789*0a6a1f1dSLionel Sambuc }
790ebfedea0SLionel Sambuc
791ebfedea0SLionel Sambuc ret = ssl3_send_change_cipher_spec(s,
792*0a6a1f1dSLionel Sambuc SSL3_ST_SW_CHANGE_A,
793*0a6a1f1dSLionel Sambuc SSL3_ST_SW_CHANGE_B);
794ebfedea0SLionel Sambuc
795*0a6a1f1dSLionel Sambuc if (ret <= 0)
796*0a6a1f1dSLionel Sambuc goto end;
797ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_FINISHED_A;
798ebfedea0SLionel Sambuc s->init_num = 0;
799ebfedea0SLionel Sambuc
800ebfedea0SLionel Sambuc if (!s->method->ssl3_enc->change_cipher_state(s,
801ebfedea0SLionel Sambuc SSL3_CHANGE_CIPHER_SERVER_WRITE))
802ebfedea0SLionel Sambuc {
803ebfedea0SLionel Sambuc ret = -1;
804*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
805ebfedea0SLionel Sambuc goto end;
806ebfedea0SLionel Sambuc }
807ebfedea0SLionel Sambuc
808ebfedea0SLionel Sambuc break;
809ebfedea0SLionel Sambuc
810ebfedea0SLionel Sambuc case SSL3_ST_SW_FINISHED_A:
811ebfedea0SLionel Sambuc case SSL3_ST_SW_FINISHED_B:
812ebfedea0SLionel Sambuc ret = ssl3_send_finished(s,
813*0a6a1f1dSLionel Sambuc SSL3_ST_SW_FINISHED_A,
814*0a6a1f1dSLionel Sambuc SSL3_ST_SW_FINISHED_B,
815*0a6a1f1dSLionel Sambuc s->method->
816*0a6a1f1dSLionel Sambuc ssl3_enc->server_finished_label,
817*0a6a1f1dSLionel Sambuc s->method->
818*0a6a1f1dSLionel Sambuc ssl3_enc->server_finished_label_len);
819*0a6a1f1dSLionel Sambuc if (ret <= 0)
820*0a6a1f1dSLionel Sambuc goto end;
821ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_FLUSH;
822*0a6a1f1dSLionel Sambuc if (s->hit) {
823ebfedea0SLionel Sambuc #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
824ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
825ebfedea0SLionel Sambuc #else
826*0a6a1f1dSLionel Sambuc if (s->s3->next_proto_neg_seen) {
827ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SR_NEXT_PROTO_A;
828*0a6a1f1dSLionel Sambuc } else
829ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_SR_FINISHED_A;
830ebfedea0SLionel Sambuc #endif
831*0a6a1f1dSLionel Sambuc } else
832ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL_ST_OK;
833ebfedea0SLionel Sambuc s->init_num = 0;
834ebfedea0SLionel Sambuc break;
835ebfedea0SLionel Sambuc
836ebfedea0SLionel Sambuc case SSL_ST_OK:
837ebfedea0SLionel Sambuc /* clean a few things up */
838ebfedea0SLionel Sambuc ssl3_cleanup_key_block(s);
839ebfedea0SLionel Sambuc
840ebfedea0SLionel Sambuc BUF_MEM_free(s->init_buf);
841ebfedea0SLionel Sambuc s->init_buf = NULL;
842ebfedea0SLionel Sambuc
843ebfedea0SLionel Sambuc /* remove buffering on output */
844ebfedea0SLionel Sambuc ssl_free_wbio_buffer(s);
845ebfedea0SLionel Sambuc
846ebfedea0SLionel Sambuc s->init_num = 0;
847ebfedea0SLionel Sambuc
848*0a6a1f1dSLionel Sambuc if (s->renegotiate == 2) { /* skipped if we just sent a
849*0a6a1f1dSLionel Sambuc * HelloRequest */
850ebfedea0SLionel Sambuc s->renegotiate = 0;
851ebfedea0SLionel Sambuc s->new_session = 0;
852ebfedea0SLionel Sambuc
853ebfedea0SLionel Sambuc ssl_update_cache(s, SSL_SESS_CACHE_SERVER);
854ebfedea0SLionel Sambuc
855ebfedea0SLionel Sambuc s->ctx->stats.sess_accept_good++;
856ebfedea0SLionel Sambuc /* s->server=1; */
857ebfedea0SLionel Sambuc s->handshake_func = ssl3_accept;
858ebfedea0SLionel Sambuc
859*0a6a1f1dSLionel Sambuc if (cb != NULL)
860*0a6a1f1dSLionel Sambuc cb(s, SSL_CB_HANDSHAKE_DONE, 1);
861ebfedea0SLionel Sambuc }
862ebfedea0SLionel Sambuc
863ebfedea0SLionel Sambuc ret = 1;
864ebfedea0SLionel Sambuc goto end;
865ebfedea0SLionel Sambuc /* break; */
866ebfedea0SLionel Sambuc
867*0a6a1f1dSLionel Sambuc case SSL_ST_ERR:
868ebfedea0SLionel Sambuc default:
869ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_UNKNOWN_STATE);
870ebfedea0SLionel Sambuc ret = -1;
871ebfedea0SLionel Sambuc goto end;
872ebfedea0SLionel Sambuc /* break; */
873ebfedea0SLionel Sambuc }
874ebfedea0SLionel Sambuc
875*0a6a1f1dSLionel Sambuc if (!s->s3->tmp.reuse_message && !skip) {
876*0a6a1f1dSLionel Sambuc if (s->debug) {
877ebfedea0SLionel Sambuc if ((ret = BIO_flush(s->wbio)) <= 0)
878ebfedea0SLionel Sambuc goto end;
879ebfedea0SLionel Sambuc }
880ebfedea0SLionel Sambuc
881*0a6a1f1dSLionel Sambuc if ((cb != NULL) && (s->state != state)) {
882ebfedea0SLionel Sambuc new_state = s->state;
883ebfedea0SLionel Sambuc s->state = state;
884ebfedea0SLionel Sambuc cb(s, SSL_CB_ACCEPT_LOOP, 1);
885ebfedea0SLionel Sambuc s->state = new_state;
886ebfedea0SLionel Sambuc }
887ebfedea0SLionel Sambuc }
888ebfedea0SLionel Sambuc skip = 0;
889ebfedea0SLionel Sambuc }
890ebfedea0SLionel Sambuc end:
891ebfedea0SLionel Sambuc /* BIO_flush(s->wbio); */
892ebfedea0SLionel Sambuc
893ebfedea0SLionel Sambuc s->in_handshake--;
894ebfedea0SLionel Sambuc if (cb != NULL)
895ebfedea0SLionel Sambuc cb(s, SSL_CB_ACCEPT_EXIT, ret);
896ebfedea0SLionel Sambuc return (ret);
897ebfedea0SLionel Sambuc }
898ebfedea0SLionel Sambuc
ssl3_send_hello_request(SSL * s)899ebfedea0SLionel Sambuc int ssl3_send_hello_request(SSL *s)
900ebfedea0SLionel Sambuc {
901ebfedea0SLionel Sambuc unsigned char *p;
902ebfedea0SLionel Sambuc
903*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_HELLO_REQ_A) {
904ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
905ebfedea0SLionel Sambuc *(p++) = SSL3_MT_HELLO_REQUEST;
906ebfedea0SLionel Sambuc *(p++) = 0;
907ebfedea0SLionel Sambuc *(p++) = 0;
908ebfedea0SLionel Sambuc *(p++) = 0;
909ebfedea0SLionel Sambuc
910ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_HELLO_REQ_B;
911ebfedea0SLionel Sambuc /* number of bytes to write */
912ebfedea0SLionel Sambuc s->init_num = 4;
913ebfedea0SLionel Sambuc s->init_off = 0;
914ebfedea0SLionel Sambuc }
915ebfedea0SLionel Sambuc
916ebfedea0SLionel Sambuc /* SSL3_ST_SW_HELLO_REQ_B */
917ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
918ebfedea0SLionel Sambuc }
919ebfedea0SLionel Sambuc
ssl3_check_client_hello(SSL * s)920ebfedea0SLionel Sambuc int ssl3_check_client_hello(SSL *s)
921ebfedea0SLionel Sambuc {
922ebfedea0SLionel Sambuc int ok;
923ebfedea0SLionel Sambuc long n;
924ebfedea0SLionel Sambuc
925ebfedea0SLionel Sambuc /* We only allow the client to restart the handshake once per
926ebfedea0SLionel Sambuc * negotiation. */
927*0a6a1f1dSLionel Sambuc if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
928ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO, SSL_R_MULTIPLE_SGC_RESTARTS);
929ebfedea0SLionel Sambuc return -1;
930ebfedea0SLionel Sambuc }
931ebfedea0SLionel Sambuc
932*0a6a1f1dSLionel Sambuc /*
933*0a6a1f1dSLionel Sambuc * this function is called when we really expect a Certificate message,
934*0a6a1f1dSLionel Sambuc * so permit appropriate message length
935*0a6a1f1dSLionel Sambuc */
936ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
937ebfedea0SLionel Sambuc SSL3_ST_SR_CERT_A,
938ebfedea0SLionel Sambuc SSL3_ST_SR_CERT_B,
939*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
940*0a6a1f1dSLionel Sambuc if (!ok)
941*0a6a1f1dSLionel Sambuc return ((int)n);
942ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
943*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO) {
944*0a6a1f1dSLionel Sambuc /*
945*0a6a1f1dSLionel Sambuc * We only allow the client to restart the handshake once per
946*0a6a1f1dSLionel Sambuc * negotiation.
947*0a6a1f1dSLionel Sambuc */
948*0a6a1f1dSLionel Sambuc if (s->s3->flags & SSL3_FLAGS_SGC_RESTART_DONE) {
949*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CLIENT_HELLO,
950*0a6a1f1dSLionel Sambuc SSL_R_MULTIPLE_SGC_RESTARTS);
951ebfedea0SLionel Sambuc return -1;
952ebfedea0SLionel Sambuc }
953*0a6a1f1dSLionel Sambuc /*
954*0a6a1f1dSLionel Sambuc * Throw away what we have done so far in the current handshake,
955*0a6a1f1dSLionel Sambuc * which will now be aborted. (A full SSL_clear would be too much.)
956*0a6a1f1dSLionel Sambuc */
957ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
958*0a6a1f1dSLionel Sambuc if (s->s3->tmp.dh != NULL) {
959ebfedea0SLionel Sambuc DH_free(s->s3->tmp.dh);
960ebfedea0SLionel Sambuc s->s3->tmp.dh = NULL;
961ebfedea0SLionel Sambuc }
962ebfedea0SLionel Sambuc #endif
963ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
964*0a6a1f1dSLionel Sambuc if (s->s3->tmp.ecdh != NULL) {
965ebfedea0SLionel Sambuc EC_KEY_free(s->s3->tmp.ecdh);
966ebfedea0SLionel Sambuc s->s3->tmp.ecdh = NULL;
967ebfedea0SLionel Sambuc }
968ebfedea0SLionel Sambuc #endif
969ebfedea0SLionel Sambuc s->s3->flags |= SSL3_FLAGS_SGC_RESTART_DONE;
970ebfedea0SLionel Sambuc return 2;
971ebfedea0SLionel Sambuc }
972ebfedea0SLionel Sambuc return 1;
973ebfedea0SLionel Sambuc }
974ebfedea0SLionel Sambuc
ssl3_get_client_hello(SSL * s)975ebfedea0SLionel Sambuc int ssl3_get_client_hello(SSL *s)
976ebfedea0SLionel Sambuc {
977ebfedea0SLionel Sambuc int i, j, ok, al, ret = -1;
978ebfedea0SLionel Sambuc unsigned int cookie_len;
979ebfedea0SLionel Sambuc long n;
980ebfedea0SLionel Sambuc unsigned long id;
981ebfedea0SLionel Sambuc unsigned char *p, *d, *q;
982ebfedea0SLionel Sambuc SSL_CIPHER *c;
983ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
984ebfedea0SLionel Sambuc SSL_COMP *comp = NULL;
985ebfedea0SLionel Sambuc #endif
986ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *ciphers = NULL;
987ebfedea0SLionel Sambuc
988*0a6a1f1dSLionel Sambuc /*
989*0a6a1f1dSLionel Sambuc * We do this so that we will respond with our native type. If we are
990*0a6a1f1dSLionel Sambuc * TLSv1 and we get SSLv3, we will respond with TLSv1, This down
991*0a6a1f1dSLionel Sambuc * switching should be handled by a different method. If we are SSLv3, we
992*0a6a1f1dSLionel Sambuc * will respond with SSLv3, even if prompted with TLSv1.
993ebfedea0SLionel Sambuc */
994*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SR_CLNT_HELLO_A) {
995ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CLNT_HELLO_B;
996ebfedea0SLionel Sambuc }
997ebfedea0SLionel Sambuc s->first_packet = 1;
998ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
999ebfedea0SLionel Sambuc SSL3_ST_SR_CLNT_HELLO_B,
1000ebfedea0SLionel Sambuc SSL3_ST_SR_CLNT_HELLO_C,
1001ebfedea0SLionel Sambuc SSL3_MT_CLIENT_HELLO,
1002*0a6a1f1dSLionel Sambuc SSL3_RT_MAX_PLAIN_LENGTH, &ok);
1003ebfedea0SLionel Sambuc
1004*0a6a1f1dSLionel Sambuc if (!ok)
1005*0a6a1f1dSLionel Sambuc return ((int)n);
1006ebfedea0SLionel Sambuc s->first_packet = 0;
1007ebfedea0SLionel Sambuc d = p = (unsigned char *)s->init_msg;
1008ebfedea0SLionel Sambuc
1009*0a6a1f1dSLionel Sambuc /*
1010*0a6a1f1dSLionel Sambuc * 2 bytes for client version, SSL3_RANDOM_SIZE bytes for random, 1 byte
1011*0a6a1f1dSLionel Sambuc * for session id length
1012*0a6a1f1dSLionel Sambuc */
1013*0a6a1f1dSLionel Sambuc if (n < 2 + SSL3_RANDOM_SIZE + 1) {
1014*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1015*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1016*0a6a1f1dSLionel Sambuc goto f_err;
1017*0a6a1f1dSLionel Sambuc }
1018*0a6a1f1dSLionel Sambuc
1019*0a6a1f1dSLionel Sambuc /*
1020*0a6a1f1dSLionel Sambuc * use version from inside client hello, not from record header (may
1021*0a6a1f1dSLionel Sambuc * differ: see RFC 2246, Appendix E, second paragraph)
1022*0a6a1f1dSLionel Sambuc */
1023ebfedea0SLionel Sambuc s->client_version = (((int)p[0]) << 8) | (int)p[1];
1024ebfedea0SLionel Sambuc p += 2;
1025ebfedea0SLionel Sambuc
1026ebfedea0SLionel Sambuc if ((s->version == DTLS1_VERSION && s->client_version > s->version) ||
1027*0a6a1f1dSLionel Sambuc (s->version != DTLS1_VERSION && s->client_version < s->version)) {
1028ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
1029*0a6a1f1dSLionel Sambuc if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
1030*0a6a1f1dSLionel Sambuc !s->enc_write_ctx && !s->write_hash) {
1031*0a6a1f1dSLionel Sambuc /*
1032*0a6a1f1dSLionel Sambuc * similar to ssl3_get_record, send alert using remote version
1033*0a6a1f1dSLionel Sambuc * number
1034*0a6a1f1dSLionel Sambuc */
1035ebfedea0SLionel Sambuc s->version = s->client_version;
1036ebfedea0SLionel Sambuc }
1037ebfedea0SLionel Sambuc al = SSL_AD_PROTOCOL_VERSION;
1038ebfedea0SLionel Sambuc goto f_err;
1039ebfedea0SLionel Sambuc }
1040ebfedea0SLionel Sambuc
1041*0a6a1f1dSLionel Sambuc /*
1042*0a6a1f1dSLionel Sambuc * If we require cookies and this ClientHello doesn't contain one, just
1043*0a6a1f1dSLionel Sambuc * return since we do not want to allocate any memory yet. So check
1044*0a6a1f1dSLionel Sambuc * cookie length...
1045ebfedea0SLionel Sambuc */
1046*0a6a1f1dSLionel Sambuc if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
1047ebfedea0SLionel Sambuc unsigned int session_length, cookie_length;
1048ebfedea0SLionel Sambuc
1049ebfedea0SLionel Sambuc session_length = *(p + SSL3_RANDOM_SIZE);
1050*0a6a1f1dSLionel Sambuc
1051*0a6a1f1dSLionel Sambuc if (p + SSL3_RANDOM_SIZE + session_length + 1 >= d + n) {
1052*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1053*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1054*0a6a1f1dSLionel Sambuc goto f_err;
1055*0a6a1f1dSLionel Sambuc }
1056ebfedea0SLionel Sambuc cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
1057ebfedea0SLionel Sambuc
1058ebfedea0SLionel Sambuc if (cookie_length == 0)
1059ebfedea0SLionel Sambuc return 1;
1060ebfedea0SLionel Sambuc }
1061ebfedea0SLionel Sambuc
1062ebfedea0SLionel Sambuc /* load the client random */
1063ebfedea0SLionel Sambuc memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
1064ebfedea0SLionel Sambuc p += SSL3_RANDOM_SIZE;
1065ebfedea0SLionel Sambuc
1066ebfedea0SLionel Sambuc /* get the session-id */
1067ebfedea0SLionel Sambuc j = *(p++);
1068ebfedea0SLionel Sambuc
1069*0a6a1f1dSLionel Sambuc if (p + j > d + n) {
1070*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1071*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1072*0a6a1f1dSLionel Sambuc goto f_err;
1073*0a6a1f1dSLionel Sambuc }
1074*0a6a1f1dSLionel Sambuc
1075ebfedea0SLionel Sambuc s->hit = 0;
1076*0a6a1f1dSLionel Sambuc /*
1077*0a6a1f1dSLionel Sambuc * Versions before 0.9.7 always allow clients to resume sessions in
1078*0a6a1f1dSLionel Sambuc * renegotiation. 0.9.7 and later allow this by default, but optionally
1079*0a6a1f1dSLionel Sambuc * ignore resumption requests with flag
1080*0a6a1f1dSLionel Sambuc * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
1081*0a6a1f1dSLionel Sambuc * than a change to default behavior so that applications relying on this
1082*0a6a1f1dSLionel Sambuc * for security won't even compile against older library versions).
1083*0a6a1f1dSLionel Sambuc * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
1084*0a6a1f1dSLionel Sambuc * request renegotiation but not a new session (s->new_session remains
1085*0a6a1f1dSLionel Sambuc * unset): for servers, this essentially just means that the
1086*0a6a1f1dSLionel Sambuc * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored.
1087ebfedea0SLionel Sambuc */
1088*0a6a1f1dSLionel Sambuc if ((s->new_session
1089*0a6a1f1dSLionel Sambuc && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
1090ebfedea0SLionel Sambuc if (!ssl_get_new_session(s, 1))
1091ebfedea0SLionel Sambuc goto err;
1092*0a6a1f1dSLionel Sambuc } else {
1093ebfedea0SLionel Sambuc i = ssl_get_prev_session(s, p, j, d + n);
1094*0a6a1f1dSLionel Sambuc /*
1095*0a6a1f1dSLionel Sambuc * Only resume if the session's version matches the negotiated
1096*0a6a1f1dSLionel Sambuc * version.
1097*0a6a1f1dSLionel Sambuc * RFC 5246 does not provide much useful advice on resumption
1098*0a6a1f1dSLionel Sambuc * with a different protocol version. It doesn't forbid it but
1099*0a6a1f1dSLionel Sambuc * the sanity of such behaviour would be questionable.
1100*0a6a1f1dSLionel Sambuc * In practice, clients do not accept a version mismatch and
1101*0a6a1f1dSLionel Sambuc * will abort the handshake with an error.
1102*0a6a1f1dSLionel Sambuc */
1103*0a6a1f1dSLionel Sambuc if (i == 1 && s->version == s->session->ssl_version) { /* previous
1104*0a6a1f1dSLionel Sambuc * session */
1105ebfedea0SLionel Sambuc s->hit = 1;
1106*0a6a1f1dSLionel Sambuc } else if (i == -1)
1107ebfedea0SLionel Sambuc goto err;
1108*0a6a1f1dSLionel Sambuc else { /* i == 0 */
1109*0a6a1f1dSLionel Sambuc
1110ebfedea0SLionel Sambuc if (!ssl_get_new_session(s, 1))
1111ebfedea0SLionel Sambuc goto err;
1112ebfedea0SLionel Sambuc }
1113ebfedea0SLionel Sambuc }
1114ebfedea0SLionel Sambuc
1115ebfedea0SLionel Sambuc p += j;
1116ebfedea0SLionel Sambuc
1117*0a6a1f1dSLionel Sambuc if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER) {
1118ebfedea0SLionel Sambuc /* cookie stuff */
1119*0a6a1f1dSLionel Sambuc if (p + 1 > d + n) {
1120*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1121*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1122*0a6a1f1dSLionel Sambuc goto f_err;
1123*0a6a1f1dSLionel Sambuc }
1124ebfedea0SLionel Sambuc cookie_len = *(p++);
1125ebfedea0SLionel Sambuc
1126*0a6a1f1dSLionel Sambuc if (p + cookie_len > d + n) {
1127*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1128*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1129*0a6a1f1dSLionel Sambuc goto f_err;
1130*0a6a1f1dSLionel Sambuc }
1131*0a6a1f1dSLionel Sambuc
1132ebfedea0SLionel Sambuc /*
1133ebfedea0SLionel Sambuc * The ClientHello may contain a cookie even if the
1134ebfedea0SLionel Sambuc * HelloVerify message has not been sent--make sure that it
1135ebfedea0SLionel Sambuc * does not cause an overflow.
1136ebfedea0SLionel Sambuc */
1137*0a6a1f1dSLionel Sambuc if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1138ebfedea0SLionel Sambuc /* too much data */
1139ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1140ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1141ebfedea0SLionel Sambuc goto f_err;
1142ebfedea0SLionel Sambuc }
1143ebfedea0SLionel Sambuc
1144ebfedea0SLionel Sambuc /* verify the cookie if appropriate option is set. */
1145*0a6a1f1dSLionel Sambuc if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
1146ebfedea0SLionel Sambuc memcpy(s->d1->rcvd_cookie, p, cookie_len);
1147ebfedea0SLionel Sambuc
1148*0a6a1f1dSLionel Sambuc if (s->ctx->app_verify_cookie_cb != NULL) {
1149ebfedea0SLionel Sambuc if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1150*0a6a1f1dSLionel Sambuc cookie_len) == 0) {
1151ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1152ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1153ebfedea0SLionel Sambuc SSL_R_COOKIE_MISMATCH);
1154ebfedea0SLionel Sambuc goto f_err;
1155ebfedea0SLionel Sambuc }
1156ebfedea0SLionel Sambuc /* else cookie verification succeeded */
1157ebfedea0SLionel Sambuc }
1158*0a6a1f1dSLionel Sambuc /* default verification */
1159ebfedea0SLionel Sambuc else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1160*0a6a1f1dSLionel Sambuc s->d1->cookie_len) != 0) {
1161ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1162*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1163ebfedea0SLionel Sambuc goto f_err;
1164ebfedea0SLionel Sambuc }
1165ebfedea0SLionel Sambuc
1166ebfedea0SLionel Sambuc ret = 2;
1167ebfedea0SLionel Sambuc }
1168ebfedea0SLionel Sambuc
1169ebfedea0SLionel Sambuc p += cookie_len;
1170ebfedea0SLionel Sambuc }
1171ebfedea0SLionel Sambuc
1172*0a6a1f1dSLionel Sambuc if (p + 2 > d + n) {
1173*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1174*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_TOO_SHORT);
1175*0a6a1f1dSLionel Sambuc goto f_err;
1176*0a6a1f1dSLionel Sambuc }
1177ebfedea0SLionel Sambuc n2s(p, i);
1178*0a6a1f1dSLionel Sambuc
1179*0a6a1f1dSLionel Sambuc if (i == 0) {
1180ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1181ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1182ebfedea0SLionel Sambuc goto f_err;
1183ebfedea0SLionel Sambuc }
1184*0a6a1f1dSLionel Sambuc
1185*0a6a1f1dSLionel Sambuc /* i bytes of cipher data + 1 byte for compression length later */
1186*0a6a1f1dSLionel Sambuc if ((p + i + 1) > (d + n)) {
1187ebfedea0SLionel Sambuc /* not enough data */
1188ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1189ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1190ebfedea0SLionel Sambuc goto f_err;
1191ebfedea0SLionel Sambuc }
1192*0a6a1f1dSLionel Sambuc if (ssl_bytes_to_cipher_list(s, p, i, &(ciphers)) == NULL) {
1193ebfedea0SLionel Sambuc goto err;
1194ebfedea0SLionel Sambuc }
1195ebfedea0SLionel Sambuc p += i;
1196ebfedea0SLionel Sambuc
1197ebfedea0SLionel Sambuc /* If it is a hit, check that the cipher is in the list */
1198*0a6a1f1dSLionel Sambuc if (s->hit) {
1199ebfedea0SLionel Sambuc j = 0;
1200ebfedea0SLionel Sambuc id = s->session->cipher->id;
1201ebfedea0SLionel Sambuc
1202ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
1203*0a6a1f1dSLionel Sambuc fprintf(stderr, "client sent %d ciphers\n",
1204*0a6a1f1dSLionel Sambuc sk_SSL_CIPHER_num(ciphers));
1205ebfedea0SLionel Sambuc #endif
1206*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1207ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(ciphers, i);
1208ebfedea0SLionel Sambuc #ifdef CIPHER_DEBUG
1209*0a6a1f1dSLionel Sambuc fprintf(stderr, "client [%2d of %2d]:%s\n",
1210*0a6a1f1dSLionel Sambuc i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
1211ebfedea0SLionel Sambuc #endif
1212*0a6a1f1dSLionel Sambuc if (c->id == id) {
1213ebfedea0SLionel Sambuc j = 1;
1214ebfedea0SLionel Sambuc break;
1215ebfedea0SLionel Sambuc }
1216ebfedea0SLionel Sambuc }
1217*0a6a1f1dSLionel Sambuc /*
1218*0a6a1f1dSLionel Sambuc * Disabled because it can be used in a ciphersuite downgrade attack:
1219*0a6a1f1dSLionel Sambuc * CVE-2010-4180.
1220ebfedea0SLionel Sambuc */
1221ebfedea0SLionel Sambuc #if 0
1222*0a6a1f1dSLionel Sambuc if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1223*0a6a1f1dSLionel Sambuc && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1224*0a6a1f1dSLionel Sambuc /*
1225*0a6a1f1dSLionel Sambuc * Special case as client bug workaround: the previously used
1226*0a6a1f1dSLionel Sambuc * cipher may not be in the current list, the client instead
1227*0a6a1f1dSLionel Sambuc * might be trying to continue using a cipher that before wasn't
1228*0a6a1f1dSLionel Sambuc * chosen due to server preferences. We'll have to reject the
1229*0a6a1f1dSLionel Sambuc * connection if the cipher is not enabled, though.
1230*0a6a1f1dSLionel Sambuc */
1231ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(ciphers, 0);
1232*0a6a1f1dSLionel Sambuc if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1233ebfedea0SLionel Sambuc s->session->cipher = c;
1234ebfedea0SLionel Sambuc j = 1;
1235ebfedea0SLionel Sambuc }
1236ebfedea0SLionel Sambuc }
1237ebfedea0SLionel Sambuc #endif
1238*0a6a1f1dSLionel Sambuc if (j == 0) {
1239*0a6a1f1dSLionel Sambuc /*
1240*0a6a1f1dSLionel Sambuc * we need to have the cipher in the cipher list if we are asked
1241*0a6a1f1dSLionel Sambuc * to reuse it
1242*0a6a1f1dSLionel Sambuc */
1243ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1244*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1245*0a6a1f1dSLionel Sambuc SSL_R_REQUIRED_CIPHER_MISSING);
1246ebfedea0SLionel Sambuc goto f_err;
1247ebfedea0SLionel Sambuc }
1248ebfedea0SLionel Sambuc }
1249ebfedea0SLionel Sambuc
1250ebfedea0SLionel Sambuc /* compression */
1251ebfedea0SLionel Sambuc i = *(p++);
1252*0a6a1f1dSLionel Sambuc if ((p + i) > (d + n)) {
1253ebfedea0SLionel Sambuc /* not enough data */
1254ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1255ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1256ebfedea0SLionel Sambuc goto f_err;
1257ebfedea0SLionel Sambuc }
1258ebfedea0SLionel Sambuc q = p;
1259*0a6a1f1dSLionel Sambuc for (j = 0; j < i; j++) {
1260*0a6a1f1dSLionel Sambuc if (p[j] == 0)
1261*0a6a1f1dSLionel Sambuc break;
1262ebfedea0SLionel Sambuc }
1263ebfedea0SLionel Sambuc
1264ebfedea0SLionel Sambuc p += i;
1265*0a6a1f1dSLionel Sambuc if (j >= i) {
1266ebfedea0SLionel Sambuc /* no compress */
1267ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1268ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1269ebfedea0SLionel Sambuc goto f_err;
1270ebfedea0SLionel Sambuc }
1271ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1272ebfedea0SLionel Sambuc /* TLS extensions */
1273*0a6a1f1dSLionel Sambuc if (s->version >= SSL3_VERSION) {
1274*0a6a1f1dSLionel Sambuc if (!ssl_parse_clienthello_tlsext(s, &p, d, n, &al)) {
1275ebfedea0SLionel Sambuc /* 'al' set by ssl_parse_clienthello_tlsext */
1276ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1277ebfedea0SLionel Sambuc goto f_err;
1278ebfedea0SLionel Sambuc }
1279ebfedea0SLionel Sambuc }
1280ebfedea0SLionel Sambuc if (ssl_check_clienthello_tlsext_early(s) <= 0) {
1281ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1282ebfedea0SLionel Sambuc goto err;
1283ebfedea0SLionel Sambuc }
1284ebfedea0SLionel Sambuc
1285*0a6a1f1dSLionel Sambuc /*
1286*0a6a1f1dSLionel Sambuc * Check if we want to use external pre-shared secret for this handshake
1287*0a6a1f1dSLionel Sambuc * for not reused session only. We need to generate server_random before
1288*0a6a1f1dSLionel Sambuc * calling tls_session_secret_cb in order to allow SessionTicket
1289*0a6a1f1dSLionel Sambuc * processing to use it in key derivation.
1290*0a6a1f1dSLionel Sambuc */
1291ebfedea0SLionel Sambuc {
1292ebfedea0SLionel Sambuc unsigned char *pos;
1293ebfedea0SLionel Sambuc pos = s->s3->server_random;
1294*0a6a1f1dSLionel Sambuc if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1295ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1296ebfedea0SLionel Sambuc goto f_err;
1297ebfedea0SLionel Sambuc }
1298ebfedea0SLionel Sambuc }
1299ebfedea0SLionel Sambuc
1300*0a6a1f1dSLionel Sambuc if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1301ebfedea0SLionel Sambuc SSL_CIPHER *pref_cipher = NULL;
1302ebfedea0SLionel Sambuc
1303ebfedea0SLionel Sambuc s->session->master_key_length = sizeof(s->session->master_key);
1304*0a6a1f1dSLionel Sambuc if (s->tls_session_secret_cb(s, s->session->master_key,
1305*0a6a1f1dSLionel Sambuc &s->session->master_key_length, ciphers,
1306*0a6a1f1dSLionel Sambuc &pref_cipher,
1307*0a6a1f1dSLionel Sambuc s->tls_session_secret_cb_arg)) {
1308ebfedea0SLionel Sambuc s->hit = 1;
1309ebfedea0SLionel Sambuc s->session->ciphers = ciphers;
1310ebfedea0SLionel Sambuc s->session->verify_result = X509_V_OK;
1311ebfedea0SLionel Sambuc
1312ebfedea0SLionel Sambuc ciphers = NULL;
1313ebfedea0SLionel Sambuc
1314ebfedea0SLionel Sambuc /* check if some cipher was preferred by call back */
1315*0a6a1f1dSLionel Sambuc pref_cipher =
1316*0a6a1f1dSLionel Sambuc pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1317*0a6a1f1dSLionel Sambuc s->
1318*0a6a1f1dSLionel Sambuc session->ciphers,
1319*0a6a1f1dSLionel Sambuc SSL_get_ciphers
1320*0a6a1f1dSLionel Sambuc (s));
1321*0a6a1f1dSLionel Sambuc if (pref_cipher == NULL) {
1322ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1323ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1324ebfedea0SLionel Sambuc goto f_err;
1325ebfedea0SLionel Sambuc }
1326ebfedea0SLionel Sambuc
1327ebfedea0SLionel Sambuc s->session->cipher = pref_cipher;
1328ebfedea0SLionel Sambuc
1329ebfedea0SLionel Sambuc if (s->cipher_list)
1330ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(s->cipher_list);
1331ebfedea0SLionel Sambuc
1332ebfedea0SLionel Sambuc if (s->cipher_list_by_id)
1333ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(s->cipher_list_by_id);
1334ebfedea0SLionel Sambuc
1335ebfedea0SLionel Sambuc s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1336ebfedea0SLionel Sambuc s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1337ebfedea0SLionel Sambuc }
1338ebfedea0SLionel Sambuc }
1339ebfedea0SLionel Sambuc #endif
1340ebfedea0SLionel Sambuc
1341*0a6a1f1dSLionel Sambuc /*
1342*0a6a1f1dSLionel Sambuc * Worst case, we will use the NULL compression, but if we have other
1343ebfedea0SLionel Sambuc * options, we will now look for them. We have i-1 compression
1344*0a6a1f1dSLionel Sambuc * algorithms from the client, starting at q.
1345*0a6a1f1dSLionel Sambuc */
1346ebfedea0SLionel Sambuc s->s3->tmp.new_compression = NULL;
1347ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
1348ebfedea0SLionel Sambuc /* This only happens if we have a cache hit */
1349*0a6a1f1dSLionel Sambuc if (s->session->compress_meth != 0) {
1350ebfedea0SLionel Sambuc int m, comp_id = s->session->compress_meth;
1351ebfedea0SLionel Sambuc /* Perform sanity checks on resumed compression algorithm */
1352ebfedea0SLionel Sambuc /* Can't disable compression */
1353*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_NO_COMPRESSION) {
1354ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1355*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1356*0a6a1f1dSLionel Sambuc SSL_R_INCONSISTENT_COMPRESSION);
1357ebfedea0SLionel Sambuc goto f_err;
1358ebfedea0SLionel Sambuc }
1359ebfedea0SLionel Sambuc /* Look for resumed compression method */
1360*0a6a1f1dSLionel Sambuc for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1361ebfedea0SLionel Sambuc comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1362*0a6a1f1dSLionel Sambuc if (comp_id == comp->id) {
1363ebfedea0SLionel Sambuc s->s3->tmp.new_compression = comp;
1364ebfedea0SLionel Sambuc break;
1365ebfedea0SLionel Sambuc }
1366ebfedea0SLionel Sambuc }
1367*0a6a1f1dSLionel Sambuc if (s->s3->tmp.new_compression == NULL) {
1368ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1369*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1370*0a6a1f1dSLionel Sambuc SSL_R_INVALID_COMPRESSION_ALGORITHM);
1371ebfedea0SLionel Sambuc goto f_err;
1372ebfedea0SLionel Sambuc }
1373ebfedea0SLionel Sambuc /* Look for resumed method in compression list */
1374*0a6a1f1dSLionel Sambuc for (m = 0; m < i; m++) {
1375ebfedea0SLionel Sambuc if (q[m] == comp_id)
1376ebfedea0SLionel Sambuc break;
1377ebfedea0SLionel Sambuc }
1378*0a6a1f1dSLionel Sambuc if (m >= i) {
1379ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1380*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1381*0a6a1f1dSLionel Sambuc SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1382ebfedea0SLionel Sambuc goto f_err;
1383ebfedea0SLionel Sambuc }
1384*0a6a1f1dSLionel Sambuc } else if (s->hit)
1385ebfedea0SLionel Sambuc comp = NULL;
1386*0a6a1f1dSLionel Sambuc else if (!(s->options & SSL_OP_NO_COMPRESSION) && s->ctx->comp_methods) {
1387*0a6a1f1dSLionel Sambuc /* See if we have a match */
1388ebfedea0SLionel Sambuc int m, nn, o, v, done = 0;
1389ebfedea0SLionel Sambuc
1390ebfedea0SLionel Sambuc nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1391*0a6a1f1dSLionel Sambuc for (m = 0; m < nn; m++) {
1392ebfedea0SLionel Sambuc comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1393ebfedea0SLionel Sambuc v = comp->id;
1394*0a6a1f1dSLionel Sambuc for (o = 0; o < i; o++) {
1395*0a6a1f1dSLionel Sambuc if (v == q[o]) {
1396ebfedea0SLionel Sambuc done = 1;
1397ebfedea0SLionel Sambuc break;
1398ebfedea0SLionel Sambuc }
1399ebfedea0SLionel Sambuc }
1400*0a6a1f1dSLionel Sambuc if (done)
1401*0a6a1f1dSLionel Sambuc break;
1402ebfedea0SLionel Sambuc }
1403ebfedea0SLionel Sambuc if (done)
1404ebfedea0SLionel Sambuc s->s3->tmp.new_compression = comp;
1405ebfedea0SLionel Sambuc else
1406ebfedea0SLionel Sambuc comp = NULL;
1407ebfedea0SLionel Sambuc }
1408ebfedea0SLionel Sambuc #else
1409*0a6a1f1dSLionel Sambuc /*
1410*0a6a1f1dSLionel Sambuc * If compression is disabled we'd better not try to resume a session
1411ebfedea0SLionel Sambuc * using compression.
1412ebfedea0SLionel Sambuc */
1413*0a6a1f1dSLionel Sambuc if (s->session->compress_meth != 0) {
1414ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1415ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1416ebfedea0SLionel Sambuc goto f_err;
1417ebfedea0SLionel Sambuc }
1418ebfedea0SLionel Sambuc #endif
1419ebfedea0SLionel Sambuc
1420*0a6a1f1dSLionel Sambuc /*
1421*0a6a1f1dSLionel Sambuc * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1422*0a6a1f1dSLionel Sambuc */
1423ebfedea0SLionel Sambuc
1424*0a6a1f1dSLionel Sambuc if (!s->hit) {
1425ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
1426ebfedea0SLionel Sambuc s->session->compress_meth = 0;
1427ebfedea0SLionel Sambuc #else
1428ebfedea0SLionel Sambuc s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
1429ebfedea0SLionel Sambuc #endif
1430ebfedea0SLionel Sambuc if (s->session->ciphers != NULL)
1431ebfedea0SLionel Sambuc sk_SSL_CIPHER_free(s->session->ciphers);
1432ebfedea0SLionel Sambuc s->session->ciphers = ciphers;
1433*0a6a1f1dSLionel Sambuc if (ciphers == NULL) {
1434*0a6a1f1dSLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1435*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
1436ebfedea0SLionel Sambuc goto f_err;
1437ebfedea0SLionel Sambuc }
1438ebfedea0SLionel Sambuc ciphers = NULL;
1439*0a6a1f1dSLionel Sambuc c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1440ebfedea0SLionel Sambuc
1441*0a6a1f1dSLionel Sambuc if (c == NULL) {
1442ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1443ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1444ebfedea0SLionel Sambuc goto f_err;
1445ebfedea0SLionel Sambuc }
1446ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = c;
1447*0a6a1f1dSLionel Sambuc } else {
1448ebfedea0SLionel Sambuc /* Session-id reuse */
1449ebfedea0SLionel Sambuc #ifdef REUSE_CIPHER_BUG
1450ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
1451ebfedea0SLionel Sambuc SSL_CIPHER *nc = NULL;
1452ebfedea0SLionel Sambuc SSL_CIPHER *ec = NULL;
1453ebfedea0SLionel Sambuc
1454*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG) {
1455ebfedea0SLionel Sambuc sk = s->session->ciphers;
1456*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
1457ebfedea0SLionel Sambuc c = sk_SSL_CIPHER_value(sk, i);
1458ebfedea0SLionel Sambuc if (c->algorithm_enc & SSL_eNULL)
1459ebfedea0SLionel Sambuc nc = c;
1460ebfedea0SLionel Sambuc if (SSL_C_IS_EXPORT(c))
1461ebfedea0SLionel Sambuc ec = c;
1462ebfedea0SLionel Sambuc }
1463ebfedea0SLionel Sambuc if (nc != NULL)
1464ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = nc;
1465ebfedea0SLionel Sambuc else if (ec != NULL)
1466ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = ec;
1467ebfedea0SLionel Sambuc else
1468ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = s->session->cipher;
1469*0a6a1f1dSLionel Sambuc } else
1470ebfedea0SLionel Sambuc #endif
1471ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = s->session->cipher;
1472ebfedea0SLionel Sambuc }
1473ebfedea0SLionel Sambuc
1474*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) < TLS1_2_VERSION
1475*0a6a1f1dSLionel Sambuc || !(s->verify_mode & SSL_VERIFY_PEER)) {
1476*0a6a1f1dSLionel Sambuc if (!ssl3_digest_cached_records(s)) {
1477ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1478ebfedea0SLionel Sambuc goto f_err;
1479ebfedea0SLionel Sambuc }
1480ebfedea0SLionel Sambuc }
1481ebfedea0SLionel Sambuc
1482*0a6a1f1dSLionel Sambuc /*-
1483*0a6a1f1dSLionel Sambuc * we now have the following setup.
1484ebfedea0SLionel Sambuc * client_random
1485ebfedea0SLionel Sambuc * cipher_list - our prefered list of ciphers
1486ebfedea0SLionel Sambuc * ciphers - the clients prefered list of ciphers
1487ebfedea0SLionel Sambuc * compression - basically ignored right now
1488ebfedea0SLionel Sambuc * ssl version is set - sslv3
1489ebfedea0SLionel Sambuc * s->session - The ssl session has been setup.
1490ebfedea0SLionel Sambuc * s->hit - session reuse flag
1491ebfedea0SLionel Sambuc * s->tmp.new_cipher - the new cipher to use.
1492ebfedea0SLionel Sambuc */
1493ebfedea0SLionel Sambuc
1494ebfedea0SLionel Sambuc /* Handles TLS extensions that we couldn't check earlier */
1495*0a6a1f1dSLionel Sambuc if (s->version >= SSL3_VERSION) {
1496*0a6a1f1dSLionel Sambuc if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1497ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1498ebfedea0SLionel Sambuc goto err;
1499ebfedea0SLionel Sambuc }
1500ebfedea0SLionel Sambuc }
1501ebfedea0SLionel Sambuc
1502*0a6a1f1dSLionel Sambuc if (ret < 0)
1503*0a6a1f1dSLionel Sambuc ret = 1;
1504*0a6a1f1dSLionel Sambuc if (0) {
1505ebfedea0SLionel Sambuc f_err:
1506ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1507ebfedea0SLionel Sambuc err:
1508*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1509*0a6a1f1dSLionel Sambuc }
1510*0a6a1f1dSLionel Sambuc
1511*0a6a1f1dSLionel Sambuc if (ciphers != NULL)
1512*0a6a1f1dSLionel Sambuc sk_SSL_CIPHER_free(ciphers);
1513ebfedea0SLionel Sambuc return (ret);
1514ebfedea0SLionel Sambuc }
1515ebfedea0SLionel Sambuc
ssl3_send_server_hello(SSL * s)1516ebfedea0SLionel Sambuc int ssl3_send_server_hello(SSL *s)
1517ebfedea0SLionel Sambuc {
1518ebfedea0SLionel Sambuc unsigned char *buf;
1519ebfedea0SLionel Sambuc unsigned char *p, *d;
1520ebfedea0SLionel Sambuc int i, sl;
1521ebfedea0SLionel Sambuc unsigned long l;
1522ebfedea0SLionel Sambuc
1523*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1524ebfedea0SLionel Sambuc buf = (unsigned char *)s->init_buf->data;
1525ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_TLSEXT
1526ebfedea0SLionel Sambuc p = s->s3->server_random;
1527*0a6a1f1dSLionel Sambuc if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0) {
1528*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1529ebfedea0SLionel Sambuc return -1;
1530*0a6a1f1dSLionel Sambuc }
1531ebfedea0SLionel Sambuc #endif
1532ebfedea0SLionel Sambuc /* Do the message type and length last */
1533ebfedea0SLionel Sambuc d = p = &(buf[4]);
1534ebfedea0SLionel Sambuc
1535ebfedea0SLionel Sambuc *(p++) = s->version >> 8;
1536ebfedea0SLionel Sambuc *(p++) = s->version & 0xff;
1537ebfedea0SLionel Sambuc
1538ebfedea0SLionel Sambuc /* Random stuff */
1539ebfedea0SLionel Sambuc memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1540ebfedea0SLionel Sambuc p += SSL3_RANDOM_SIZE;
1541ebfedea0SLionel Sambuc
1542*0a6a1f1dSLionel Sambuc /*-
1543*0a6a1f1dSLionel Sambuc * There are several cases for the session ID to send
1544ebfedea0SLionel Sambuc * back in the server hello:
1545ebfedea0SLionel Sambuc * - For session reuse from the session cache,
1546ebfedea0SLionel Sambuc * we send back the old session ID.
1547ebfedea0SLionel Sambuc * - If stateless session reuse (using a session ticket)
1548ebfedea0SLionel Sambuc * is successful, we send back the client's "session ID"
1549ebfedea0SLionel Sambuc * (which doesn't actually identify the session).
1550ebfedea0SLionel Sambuc * - If it is a new session, we send back the new
1551ebfedea0SLionel Sambuc * session ID.
1552ebfedea0SLionel Sambuc * - However, if we want the new session to be single-use,
1553ebfedea0SLionel Sambuc * we send back a 0-length session ID.
1554ebfedea0SLionel Sambuc * s->hit is non-zero in either case of session reuse,
1555ebfedea0SLionel Sambuc * so the following won't overwrite an ID that we're supposed
1556ebfedea0SLionel Sambuc * to send back.
1557ebfedea0SLionel Sambuc */
1558ebfedea0SLionel Sambuc if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1559ebfedea0SLionel Sambuc && !s->hit)
1560ebfedea0SLionel Sambuc s->session->session_id_length = 0;
1561ebfedea0SLionel Sambuc
1562ebfedea0SLionel Sambuc sl = s->session->session_id_length;
1563*0a6a1f1dSLionel Sambuc if (sl > (int)sizeof(s->session->session_id)) {
1564ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1565*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1566ebfedea0SLionel Sambuc return -1;
1567ebfedea0SLionel Sambuc }
1568ebfedea0SLionel Sambuc *(p++) = sl;
1569ebfedea0SLionel Sambuc memcpy(p, s->session->session_id, sl);
1570ebfedea0SLionel Sambuc p += sl;
1571ebfedea0SLionel Sambuc
1572ebfedea0SLionel Sambuc /* put the cipher */
1573ebfedea0SLionel Sambuc i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1574ebfedea0SLionel Sambuc p += i;
1575ebfedea0SLionel Sambuc
1576ebfedea0SLionel Sambuc /* put the compression method */
1577ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
1578ebfedea0SLionel Sambuc *(p++) = 0;
1579ebfedea0SLionel Sambuc #else
1580ebfedea0SLionel Sambuc if (s->s3->tmp.new_compression == NULL)
1581ebfedea0SLionel Sambuc *(p++) = 0;
1582ebfedea0SLionel Sambuc else
1583ebfedea0SLionel Sambuc *(p++) = s->s3->tmp.new_compression->id;
1584ebfedea0SLionel Sambuc #endif
1585ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1586*0a6a1f1dSLionel Sambuc if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1587ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1588*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1589ebfedea0SLionel Sambuc return -1;
1590ebfedea0SLionel Sambuc }
1591*0a6a1f1dSLionel Sambuc if ((p =
1592*0a6a1f1dSLionel Sambuc ssl_add_serverhello_tlsext(s, p,
1593*0a6a1f1dSLionel Sambuc buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
1594*0a6a1f1dSLionel Sambuc NULL) {
1595ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1596*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1597ebfedea0SLionel Sambuc return -1;
1598ebfedea0SLionel Sambuc }
1599ebfedea0SLionel Sambuc #endif
1600ebfedea0SLionel Sambuc /* do the header */
1601ebfedea0SLionel Sambuc l = (p - d);
1602ebfedea0SLionel Sambuc d = buf;
1603ebfedea0SLionel Sambuc *(d++) = SSL3_MT_SERVER_HELLO;
1604ebfedea0SLionel Sambuc l2n3(l, d);
1605ebfedea0SLionel Sambuc
1606ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SRVR_HELLO_B;
1607ebfedea0SLionel Sambuc /* number of bytes to write */
1608ebfedea0SLionel Sambuc s->init_num = p - buf;
1609ebfedea0SLionel Sambuc s->init_off = 0;
1610ebfedea0SLionel Sambuc }
1611ebfedea0SLionel Sambuc
1612ebfedea0SLionel Sambuc /* SSL3_ST_SW_SRVR_HELLO_B */
1613ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1614ebfedea0SLionel Sambuc }
1615ebfedea0SLionel Sambuc
ssl3_send_server_done(SSL * s)1616ebfedea0SLionel Sambuc int ssl3_send_server_done(SSL *s)
1617ebfedea0SLionel Sambuc {
1618ebfedea0SLionel Sambuc unsigned char *p;
1619ebfedea0SLionel Sambuc
1620*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
1621ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
1622ebfedea0SLionel Sambuc
1623ebfedea0SLionel Sambuc /* do the header */
1624ebfedea0SLionel Sambuc *(p++) = SSL3_MT_SERVER_DONE;
1625ebfedea0SLionel Sambuc *(p++) = 0;
1626ebfedea0SLionel Sambuc *(p++) = 0;
1627ebfedea0SLionel Sambuc *(p++) = 0;
1628ebfedea0SLionel Sambuc
1629ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SRVR_DONE_B;
1630ebfedea0SLionel Sambuc /* number of bytes to write */
1631ebfedea0SLionel Sambuc s->init_num = 4;
1632ebfedea0SLionel Sambuc s->init_off = 0;
1633ebfedea0SLionel Sambuc }
1634ebfedea0SLionel Sambuc
1635ebfedea0SLionel Sambuc /* SSL3_ST_SW_SRVR_DONE_B */
1636ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
1637ebfedea0SLionel Sambuc }
1638ebfedea0SLionel Sambuc
ssl3_send_server_key_exchange(SSL * s)1639ebfedea0SLionel Sambuc int ssl3_send_server_key_exchange(SSL *s)
1640ebfedea0SLionel Sambuc {
1641ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1642ebfedea0SLionel Sambuc unsigned char *q;
1643ebfedea0SLionel Sambuc int j, num;
1644ebfedea0SLionel Sambuc RSA *rsa;
1645ebfedea0SLionel Sambuc unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1646ebfedea0SLionel Sambuc unsigned int u;
1647ebfedea0SLionel Sambuc #endif
1648ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1649ebfedea0SLionel Sambuc DH *dh = NULL, *dhp;
1650ebfedea0SLionel Sambuc #endif
1651ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1652ebfedea0SLionel Sambuc EC_KEY *ecdh = NULL, *ecdhp;
1653ebfedea0SLionel Sambuc unsigned char *encodedPoint = NULL;
1654ebfedea0SLionel Sambuc int encodedlen = 0;
1655ebfedea0SLionel Sambuc int curve_id = 0;
1656ebfedea0SLionel Sambuc BN_CTX *bn_ctx = NULL;
1657ebfedea0SLionel Sambuc #endif
1658ebfedea0SLionel Sambuc EVP_PKEY *pkey;
1659ebfedea0SLionel Sambuc const EVP_MD *md = NULL;
1660ebfedea0SLionel Sambuc unsigned char *p, *d;
1661ebfedea0SLionel Sambuc int al, i;
1662ebfedea0SLionel Sambuc unsigned long type;
1663ebfedea0SLionel Sambuc int n;
1664ebfedea0SLionel Sambuc CERT *cert;
1665ebfedea0SLionel Sambuc BIGNUM *r[4];
1666ebfedea0SLionel Sambuc int nr[4], kn;
1667ebfedea0SLionel Sambuc BUF_MEM *buf;
1668ebfedea0SLionel Sambuc EVP_MD_CTX md_ctx;
1669ebfedea0SLionel Sambuc
1670ebfedea0SLionel Sambuc EVP_MD_CTX_init(&md_ctx);
1671*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1672ebfedea0SLionel Sambuc type = s->s3->tmp.new_cipher->algorithm_mkey;
1673ebfedea0SLionel Sambuc cert = s->cert;
1674ebfedea0SLionel Sambuc
1675ebfedea0SLionel Sambuc buf = s->init_buf;
1676ebfedea0SLionel Sambuc
1677ebfedea0SLionel Sambuc r[0] = r[1] = r[2] = r[3] = NULL;
1678ebfedea0SLionel Sambuc n = 0;
1679ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1680*0a6a1f1dSLionel Sambuc if (type & SSL_kRSA) {
1681ebfedea0SLionel Sambuc rsa = cert->rsa_tmp;
1682*0a6a1f1dSLionel Sambuc if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1683ebfedea0SLionel Sambuc rsa = s->cert->rsa_tmp_cb(s,
1684*0a6a1f1dSLionel Sambuc SSL_C_IS_EXPORT(s->s3->
1685*0a6a1f1dSLionel Sambuc tmp.new_cipher),
1686*0a6a1f1dSLionel Sambuc SSL_C_EXPORT_PKEYLENGTH(s->s3->
1687*0a6a1f1dSLionel Sambuc tmp.new_cipher));
1688*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
1689ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1690*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1691*0a6a1f1dSLionel Sambuc SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1692ebfedea0SLionel Sambuc goto f_err;
1693ebfedea0SLionel Sambuc }
1694ebfedea0SLionel Sambuc RSA_up_ref(rsa);
1695ebfedea0SLionel Sambuc cert->rsa_tmp = rsa;
1696ebfedea0SLionel Sambuc }
1697*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
1698ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1699*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1700*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_RSA_KEY);
1701ebfedea0SLionel Sambuc goto f_err;
1702ebfedea0SLionel Sambuc }
1703ebfedea0SLionel Sambuc r[0] = rsa->n;
1704ebfedea0SLionel Sambuc r[1] = rsa->e;
1705ebfedea0SLionel Sambuc s->s3->tmp.use_rsa_tmp = 1;
1706*0a6a1f1dSLionel Sambuc } else
1707ebfedea0SLionel Sambuc #endif
1708ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1709*0a6a1f1dSLionel Sambuc if (type & SSL_kEDH) {
1710ebfedea0SLionel Sambuc dhp = cert->dh_tmp;
1711ebfedea0SLionel Sambuc if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1712ebfedea0SLionel Sambuc dhp = s->cert->dh_tmp_cb(s,
1713*0a6a1f1dSLionel Sambuc SSL_C_IS_EXPORT(s->s3->
1714*0a6a1f1dSLionel Sambuc tmp.new_cipher),
1715*0a6a1f1dSLionel Sambuc SSL_C_EXPORT_PKEYLENGTH(s->s3->
1716*0a6a1f1dSLionel Sambuc tmp.new_cipher));
1717*0a6a1f1dSLionel Sambuc if (dhp == NULL) {
1718ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1719*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1720*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_DH_KEY);
1721ebfedea0SLionel Sambuc goto f_err;
1722ebfedea0SLionel Sambuc }
1723ebfedea0SLionel Sambuc
1724*0a6a1f1dSLionel Sambuc if (s->s3->tmp.dh != NULL) {
1725*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1726*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
1727ebfedea0SLionel Sambuc goto err;
1728ebfedea0SLionel Sambuc }
1729ebfedea0SLionel Sambuc
1730*0a6a1f1dSLionel Sambuc if ((dh = DHparams_dup(dhp)) == NULL) {
1731ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1732ebfedea0SLionel Sambuc goto err;
1733ebfedea0SLionel Sambuc }
1734ebfedea0SLionel Sambuc
1735ebfedea0SLionel Sambuc s->s3->tmp.dh = dh;
1736ebfedea0SLionel Sambuc if ((dhp->pub_key == NULL ||
1737ebfedea0SLionel Sambuc dhp->priv_key == NULL ||
1738*0a6a1f1dSLionel Sambuc (s->options & SSL_OP_SINGLE_DH_USE))) {
1739*0a6a1f1dSLionel Sambuc if (!DH_generate_key(dh)) {
1740*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1741ebfedea0SLionel Sambuc goto err;
1742ebfedea0SLionel Sambuc }
1743*0a6a1f1dSLionel Sambuc } else {
1744ebfedea0SLionel Sambuc dh->pub_key = BN_dup(dhp->pub_key);
1745ebfedea0SLionel Sambuc dh->priv_key = BN_dup(dhp->priv_key);
1746*0a6a1f1dSLionel Sambuc if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1747ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1748ebfedea0SLionel Sambuc goto err;
1749ebfedea0SLionel Sambuc }
1750ebfedea0SLionel Sambuc }
1751ebfedea0SLionel Sambuc r[0] = dh->p;
1752ebfedea0SLionel Sambuc r[1] = dh->g;
1753ebfedea0SLionel Sambuc r[2] = dh->pub_key;
1754*0a6a1f1dSLionel Sambuc } else
1755ebfedea0SLionel Sambuc #endif
1756ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1757*0a6a1f1dSLionel Sambuc if (type & SSL_kEECDH) {
1758ebfedea0SLionel Sambuc const EC_GROUP *group;
1759ebfedea0SLionel Sambuc
1760ebfedea0SLionel Sambuc ecdhp = cert->ecdh_tmp;
1761*0a6a1f1dSLionel Sambuc if ((ecdhp == NULL) && (s->cert->ecdh_tmp_cb != NULL)) {
1762ebfedea0SLionel Sambuc ecdhp = s->cert->ecdh_tmp_cb(s,
1763*0a6a1f1dSLionel Sambuc SSL_C_IS_EXPORT(s->s3->
1764*0a6a1f1dSLionel Sambuc tmp.new_cipher),
1765*0a6a1f1dSLionel Sambuc SSL_C_EXPORT_PKEYLENGTH(s->
1766*0a6a1f1dSLionel Sambuc s3->tmp.new_cipher));
1767ebfedea0SLionel Sambuc }
1768*0a6a1f1dSLionel Sambuc if (ecdhp == NULL) {
1769ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1770*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1771*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_ECDH_KEY);
1772ebfedea0SLionel Sambuc goto f_err;
1773ebfedea0SLionel Sambuc }
1774ebfedea0SLionel Sambuc
1775*0a6a1f1dSLionel Sambuc if (s->s3->tmp.ecdh != NULL) {
1776*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1777*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
1778ebfedea0SLionel Sambuc goto err;
1779ebfedea0SLionel Sambuc }
1780ebfedea0SLionel Sambuc
1781ebfedea0SLionel Sambuc /* Duplicate the ECDH structure. */
1782*0a6a1f1dSLionel Sambuc if (ecdhp == NULL) {
1783ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1784ebfedea0SLionel Sambuc goto err;
1785ebfedea0SLionel Sambuc }
1786*0a6a1f1dSLionel Sambuc if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1787ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1788ebfedea0SLionel Sambuc goto err;
1789ebfedea0SLionel Sambuc }
1790ebfedea0SLionel Sambuc
1791ebfedea0SLionel Sambuc s->s3->tmp.ecdh = ecdh;
1792ebfedea0SLionel Sambuc if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1793ebfedea0SLionel Sambuc (EC_KEY_get0_private_key(ecdh) == NULL) ||
1794*0a6a1f1dSLionel Sambuc (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1795*0a6a1f1dSLionel Sambuc if (!EC_KEY_generate_key(ecdh)) {
1796*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1797*0a6a1f1dSLionel Sambuc ERR_R_ECDH_LIB);
1798ebfedea0SLionel Sambuc goto err;
1799ebfedea0SLionel Sambuc }
1800ebfedea0SLionel Sambuc }
1801ebfedea0SLionel Sambuc
1802ebfedea0SLionel Sambuc if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1803ebfedea0SLionel Sambuc (EC_KEY_get0_public_key(ecdh) == NULL) ||
1804*0a6a1f1dSLionel Sambuc (EC_KEY_get0_private_key(ecdh) == NULL)) {
1805ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1806ebfedea0SLionel Sambuc goto err;
1807ebfedea0SLionel Sambuc }
1808ebfedea0SLionel Sambuc
1809ebfedea0SLionel Sambuc if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1810*0a6a1f1dSLionel Sambuc (EC_GROUP_get_degree(group) > 163)) {
1811*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1812*0a6a1f1dSLionel Sambuc SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1813ebfedea0SLionel Sambuc goto err;
1814ebfedea0SLionel Sambuc }
1815ebfedea0SLionel Sambuc
1816*0a6a1f1dSLionel Sambuc /*
1817*0a6a1f1dSLionel Sambuc * XXX: For now, we only support ephemeral ECDH keys over named
1818*0a6a1f1dSLionel Sambuc * (not generic) curves. For supported named curves, curve_id is
1819*0a6a1f1dSLionel Sambuc * non-zero.
1820ebfedea0SLionel Sambuc */
1821ebfedea0SLionel Sambuc if ((curve_id =
1822ebfedea0SLionel Sambuc tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1823*0a6a1f1dSLionel Sambuc == 0) {
1824*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1825*0a6a1f1dSLionel Sambuc SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1826ebfedea0SLionel Sambuc goto err;
1827ebfedea0SLionel Sambuc }
1828ebfedea0SLionel Sambuc
1829*0a6a1f1dSLionel Sambuc /*
1830*0a6a1f1dSLionel Sambuc * Encode the public key. First check the size of encoding and
1831ebfedea0SLionel Sambuc * allocate memory accordingly.
1832ebfedea0SLionel Sambuc */
1833ebfedea0SLionel Sambuc encodedlen = EC_POINT_point2oct(group,
1834ebfedea0SLionel Sambuc EC_KEY_get0_public_key(ecdh),
1835ebfedea0SLionel Sambuc POINT_CONVERSION_UNCOMPRESSED,
1836ebfedea0SLionel Sambuc NULL, 0, NULL);
1837ebfedea0SLionel Sambuc
1838ebfedea0SLionel Sambuc encodedPoint = (unsigned char *)
1839ebfedea0SLionel Sambuc OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1840ebfedea0SLionel Sambuc bn_ctx = BN_CTX_new();
1841*0a6a1f1dSLionel Sambuc if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1842*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1843*0a6a1f1dSLionel Sambuc ERR_R_MALLOC_FAILURE);
1844ebfedea0SLionel Sambuc goto err;
1845ebfedea0SLionel Sambuc }
1846ebfedea0SLionel Sambuc
1847ebfedea0SLionel Sambuc encodedlen = EC_POINT_point2oct(group,
1848ebfedea0SLionel Sambuc EC_KEY_get0_public_key(ecdh),
1849ebfedea0SLionel Sambuc POINT_CONVERSION_UNCOMPRESSED,
1850ebfedea0SLionel Sambuc encodedPoint, encodedlen, bn_ctx);
1851ebfedea0SLionel Sambuc
1852*0a6a1f1dSLionel Sambuc if (encodedlen == 0) {
1853ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1854ebfedea0SLionel Sambuc goto err;
1855ebfedea0SLionel Sambuc }
1856ebfedea0SLionel Sambuc
1857*0a6a1f1dSLionel Sambuc BN_CTX_free(bn_ctx);
1858*0a6a1f1dSLionel Sambuc bn_ctx = NULL;
1859ebfedea0SLionel Sambuc
1860*0a6a1f1dSLionel Sambuc /*
1861*0a6a1f1dSLionel Sambuc * XXX: For now, we only support named (not generic) curves in
1862*0a6a1f1dSLionel Sambuc * ECDH ephemeral key exchanges. In this situation, we need four
1863*0a6a1f1dSLionel Sambuc * additional bytes to encode the entire ServerECDHParams
1864ebfedea0SLionel Sambuc * structure.
1865ebfedea0SLionel Sambuc */
1866ebfedea0SLionel Sambuc n = 4 + encodedlen;
1867ebfedea0SLionel Sambuc
1868*0a6a1f1dSLionel Sambuc /*
1869*0a6a1f1dSLionel Sambuc * We'll generate the serverKeyExchange message explicitly so we
1870*0a6a1f1dSLionel Sambuc * can set these to NULLs
1871ebfedea0SLionel Sambuc */
1872ebfedea0SLionel Sambuc r[0] = NULL;
1873ebfedea0SLionel Sambuc r[1] = NULL;
1874ebfedea0SLionel Sambuc r[2] = NULL;
1875ebfedea0SLionel Sambuc r[3] = NULL;
1876*0a6a1f1dSLionel Sambuc } else
1877ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_ECDH */
1878ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1879*0a6a1f1dSLionel Sambuc if (type & SSL_kPSK) {
1880*0a6a1f1dSLionel Sambuc /*
1881*0a6a1f1dSLionel Sambuc * reserve size for record length and PSK identity hint
1882*0a6a1f1dSLionel Sambuc */
1883ebfedea0SLionel Sambuc n += 2 + strlen(s->ctx->psk_identity_hint);
1884*0a6a1f1dSLionel Sambuc } else
1885ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_PSK */
1886ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1887*0a6a1f1dSLionel Sambuc if (type & SSL_kSRP) {
1888ebfedea0SLionel Sambuc if ((s->srp_ctx.N == NULL) ||
1889ebfedea0SLionel Sambuc (s->srp_ctx.g == NULL) ||
1890*0a6a1f1dSLionel Sambuc (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1891*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1892*0a6a1f1dSLionel Sambuc SSL_R_MISSING_SRP_PARAM);
1893ebfedea0SLionel Sambuc goto err;
1894ebfedea0SLionel Sambuc }
1895ebfedea0SLionel Sambuc r[0] = s->srp_ctx.N;
1896ebfedea0SLionel Sambuc r[1] = s->srp_ctx.g;
1897ebfedea0SLionel Sambuc r[2] = s->srp_ctx.s;
1898ebfedea0SLionel Sambuc r[3] = s->srp_ctx.B;
1899*0a6a1f1dSLionel Sambuc } else
1900ebfedea0SLionel Sambuc #endif
1901ebfedea0SLionel Sambuc {
1902ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1903*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1904*0a6a1f1dSLionel Sambuc SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1905ebfedea0SLionel Sambuc goto f_err;
1906ebfedea0SLionel Sambuc }
1907*0a6a1f1dSLionel Sambuc for (i = 0; i < 4 && r[i] != NULL; i++) {
1908ebfedea0SLionel Sambuc nr[i] = BN_num_bytes(r[i]);
1909ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1910ebfedea0SLionel Sambuc if ((i == 2) && (type & SSL_kSRP))
1911ebfedea0SLionel Sambuc n += 1 + nr[i];
1912ebfedea0SLionel Sambuc else
1913ebfedea0SLionel Sambuc #endif
1914ebfedea0SLionel Sambuc n += 2 + nr[i];
1915ebfedea0SLionel Sambuc }
1916ebfedea0SLionel Sambuc
1917*0a6a1f1dSLionel Sambuc if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1918*0a6a1f1dSLionel Sambuc && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1919ebfedea0SLionel Sambuc if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1920*0a6a1f1dSLionel Sambuc == NULL) {
1921ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1922ebfedea0SLionel Sambuc goto f_err;
1923ebfedea0SLionel Sambuc }
1924ebfedea0SLionel Sambuc kn = EVP_PKEY_size(pkey);
1925*0a6a1f1dSLionel Sambuc } else {
1926ebfedea0SLionel Sambuc pkey = NULL;
1927ebfedea0SLionel Sambuc kn = 0;
1928ebfedea0SLionel Sambuc }
1929ebfedea0SLionel Sambuc
1930*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow_clean(buf, n + 4 + kn)) {
1931ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1932ebfedea0SLionel Sambuc goto err;
1933ebfedea0SLionel Sambuc }
1934ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
1935ebfedea0SLionel Sambuc p = &(d[4]);
1936ebfedea0SLionel Sambuc
1937*0a6a1f1dSLionel Sambuc for (i = 0; i < 4 && r[i] != NULL; i++) {
1938ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1939*0a6a1f1dSLionel Sambuc if ((i == 2) && (type & SSL_kSRP)) {
1940ebfedea0SLionel Sambuc *p = nr[i];
1941ebfedea0SLionel Sambuc p++;
1942*0a6a1f1dSLionel Sambuc } else
1943ebfedea0SLionel Sambuc #endif
1944ebfedea0SLionel Sambuc s2n(nr[i], p);
1945ebfedea0SLionel Sambuc BN_bn2bin(r[i], p);
1946ebfedea0SLionel Sambuc p += nr[i];
1947ebfedea0SLionel Sambuc }
1948ebfedea0SLionel Sambuc
1949ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1950*0a6a1f1dSLionel Sambuc if (type & SSL_kEECDH) {
1951*0a6a1f1dSLionel Sambuc /*
1952*0a6a1f1dSLionel Sambuc * XXX: For now, we only support named (not generic) curves. In
1953*0a6a1f1dSLionel Sambuc * this situation, the serverKeyExchange message has: [1 byte
1954*0a6a1f1dSLionel Sambuc * CurveType], [2 byte CurveName] [1 byte length of encoded
1955*0a6a1f1dSLionel Sambuc * point], followed by the actual encoded point itself
1956ebfedea0SLionel Sambuc */
1957ebfedea0SLionel Sambuc *p = NAMED_CURVE_TYPE;
1958ebfedea0SLionel Sambuc p += 1;
1959ebfedea0SLionel Sambuc *p = 0;
1960ebfedea0SLionel Sambuc p += 1;
1961ebfedea0SLionel Sambuc *p = curve_id;
1962ebfedea0SLionel Sambuc p += 1;
1963ebfedea0SLionel Sambuc *p = encodedlen;
1964ebfedea0SLionel Sambuc p += 1;
1965ebfedea0SLionel Sambuc memcpy((unsigned char *)p,
1966*0a6a1f1dSLionel Sambuc (unsigned char *)encodedPoint, encodedlen);
1967ebfedea0SLionel Sambuc OPENSSL_free(encodedPoint);
1968ebfedea0SLionel Sambuc encodedPoint = NULL;
1969ebfedea0SLionel Sambuc p += encodedlen;
1970ebfedea0SLionel Sambuc }
1971ebfedea0SLionel Sambuc #endif
1972ebfedea0SLionel Sambuc
1973ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1974*0a6a1f1dSLionel Sambuc if (type & SSL_kPSK) {
1975ebfedea0SLionel Sambuc /* copy PSK identity hint */
1976ebfedea0SLionel Sambuc s2n(strlen(s->ctx->psk_identity_hint), p);
1977*0a6a1f1dSLionel Sambuc strncpy((char *)p, s->ctx->psk_identity_hint,
1978*0a6a1f1dSLionel Sambuc strlen(s->ctx->psk_identity_hint));
1979ebfedea0SLionel Sambuc p += strlen(s->ctx->psk_identity_hint);
1980ebfedea0SLionel Sambuc }
1981ebfedea0SLionel Sambuc #endif
1982ebfedea0SLionel Sambuc
1983ebfedea0SLionel Sambuc /* not anonymous */
1984*0a6a1f1dSLionel Sambuc if (pkey != NULL) {
1985*0a6a1f1dSLionel Sambuc /*
1986*0a6a1f1dSLionel Sambuc * n is the length of the params, they start at &(d[4]) and p
1987*0a6a1f1dSLionel Sambuc * points to the space at the end.
1988*0a6a1f1dSLionel Sambuc */
1989ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1990ebfedea0SLionel Sambuc if (pkey->type == EVP_PKEY_RSA
1991*0a6a1f1dSLionel Sambuc && TLS1_get_version(s) < TLS1_2_VERSION) {
1992ebfedea0SLionel Sambuc q = md_buf;
1993ebfedea0SLionel Sambuc j = 0;
1994*0a6a1f1dSLionel Sambuc for (num = 2; num > 0; num--) {
1995ebfedea0SLionel Sambuc EVP_MD_CTX_set_flags(&md_ctx,
1996ebfedea0SLionel Sambuc EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1997ebfedea0SLionel Sambuc EVP_DigestInit_ex(&md_ctx, (num == 2)
1998ebfedea0SLionel Sambuc ? s->ctx->md5 : s->ctx->sha1, NULL);
1999*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
2000*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2001*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
2002*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2003ebfedea0SLionel Sambuc EVP_DigestUpdate(&md_ctx, &(d[4]), n);
2004*0a6a1f1dSLionel Sambuc EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
2005ebfedea0SLionel Sambuc q += i;
2006ebfedea0SLionel Sambuc j += i;
2007ebfedea0SLionel Sambuc }
2008ebfedea0SLionel Sambuc if (RSA_sign(NID_md5_sha1, md_buf, j,
2009*0a6a1f1dSLionel Sambuc &(p[2]), &u, pkey->pkey.rsa) <= 0) {
2010ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
2011ebfedea0SLionel Sambuc goto err;
2012ebfedea0SLionel Sambuc }
2013ebfedea0SLionel Sambuc s2n(u, p);
2014ebfedea0SLionel Sambuc n += u + 2;
2015*0a6a1f1dSLionel Sambuc } else
2016ebfedea0SLionel Sambuc #endif
2017*0a6a1f1dSLionel Sambuc if (md) {
2018*0a6a1f1dSLionel Sambuc /*
2019*0a6a1f1dSLionel Sambuc * For TLS1.2 and later send signature algorithm
2020*0a6a1f1dSLionel Sambuc */
2021*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
2022*0a6a1f1dSLionel Sambuc if (!tls12_get_sigandhash(p, pkey, md)) {
2023ebfedea0SLionel Sambuc /* Should never happen */
2024ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
2025*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2026*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2027ebfedea0SLionel Sambuc goto f_err;
2028ebfedea0SLionel Sambuc }
2029ebfedea0SLionel Sambuc p += 2;
2030ebfedea0SLionel Sambuc }
2031ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
2032*0a6a1f1dSLionel Sambuc fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
2033ebfedea0SLionel Sambuc #endif
2034ebfedea0SLionel Sambuc EVP_SignInit_ex(&md_ctx, md, NULL);
2035*0a6a1f1dSLionel Sambuc EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
2036*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2037*0a6a1f1dSLionel Sambuc EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
2038*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2039ebfedea0SLionel Sambuc EVP_SignUpdate(&md_ctx, &(d[4]), n);
2040ebfedea0SLionel Sambuc if (!EVP_SignFinal(&md_ctx, &(p[2]),
2041*0a6a1f1dSLionel Sambuc (unsigned int *)&i, pkey)) {
2042ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
2043ebfedea0SLionel Sambuc goto err;
2044ebfedea0SLionel Sambuc }
2045ebfedea0SLionel Sambuc s2n(i, p);
2046ebfedea0SLionel Sambuc n += i + 2;
2047ebfedea0SLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION)
2048ebfedea0SLionel Sambuc n += 2;
2049*0a6a1f1dSLionel Sambuc } else {
2050ebfedea0SLionel Sambuc /* Is this error check actually needed? */
2051ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2052*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
2053*0a6a1f1dSLionel Sambuc SSL_R_UNKNOWN_PKEY_TYPE);
2054ebfedea0SLionel Sambuc goto f_err;
2055ebfedea0SLionel Sambuc }
2056ebfedea0SLionel Sambuc }
2057ebfedea0SLionel Sambuc
2058ebfedea0SLionel Sambuc *(d++) = SSL3_MT_SERVER_KEY_EXCHANGE;
2059ebfedea0SLionel Sambuc l2n3(n, d);
2060ebfedea0SLionel Sambuc
2061*0a6a1f1dSLionel Sambuc /*
2062*0a6a1f1dSLionel Sambuc * we should now have things packed up, so lets send it off
2063*0a6a1f1dSLionel Sambuc */
2064ebfedea0SLionel Sambuc s->init_num = n + 4;
2065ebfedea0SLionel Sambuc s->init_off = 0;
2066ebfedea0SLionel Sambuc }
2067ebfedea0SLionel Sambuc
2068ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_KEY_EXCH_B;
2069ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&md_ctx);
2070ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2071ebfedea0SLionel Sambuc f_err:
2072ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
2073ebfedea0SLionel Sambuc err:
2074ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2075*0a6a1f1dSLionel Sambuc if (encodedPoint != NULL)
2076*0a6a1f1dSLionel Sambuc OPENSSL_free(encodedPoint);
2077ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
2078ebfedea0SLionel Sambuc #endif
2079ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&md_ctx);
2080*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2081ebfedea0SLionel Sambuc return (-1);
2082ebfedea0SLionel Sambuc }
2083ebfedea0SLionel Sambuc
ssl3_send_certificate_request(SSL * s)2084ebfedea0SLionel Sambuc int ssl3_send_certificate_request(SSL *s)
2085ebfedea0SLionel Sambuc {
2086ebfedea0SLionel Sambuc unsigned char *p, *d;
2087ebfedea0SLionel Sambuc int i, j, nl, off, n;
2088ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *sk = NULL;
2089ebfedea0SLionel Sambuc X509_NAME *name;
2090ebfedea0SLionel Sambuc BUF_MEM *buf;
2091ebfedea0SLionel Sambuc
2092*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2093ebfedea0SLionel Sambuc buf = s->init_buf;
2094ebfedea0SLionel Sambuc
2095ebfedea0SLionel Sambuc d = p = (unsigned char *)&(buf->data[4]);
2096ebfedea0SLionel Sambuc
2097ebfedea0SLionel Sambuc /* get the list of acceptable cert types */
2098ebfedea0SLionel Sambuc p++;
2099ebfedea0SLionel Sambuc n = ssl3_get_req_cert_type(s, p);
2100ebfedea0SLionel Sambuc d[0] = n;
2101ebfedea0SLionel Sambuc p += n;
2102ebfedea0SLionel Sambuc n++;
2103ebfedea0SLionel Sambuc
2104*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
2105ebfedea0SLionel Sambuc nl = tls12_get_req_sig_algs(s, p + 2);
2106ebfedea0SLionel Sambuc s2n(nl, p);
2107ebfedea0SLionel Sambuc p += nl + 2;
2108ebfedea0SLionel Sambuc n += nl + 2;
2109ebfedea0SLionel Sambuc }
2110ebfedea0SLionel Sambuc
2111ebfedea0SLionel Sambuc off = n;
2112ebfedea0SLionel Sambuc p += 2;
2113ebfedea0SLionel Sambuc n += 2;
2114ebfedea0SLionel Sambuc
2115ebfedea0SLionel Sambuc sk = SSL_get_client_CA_list(s);
2116ebfedea0SLionel Sambuc nl = 0;
2117*0a6a1f1dSLionel Sambuc if (sk != NULL) {
2118*0a6a1f1dSLionel Sambuc for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2119ebfedea0SLionel Sambuc name = sk_X509_NAME_value(sk, i);
2120ebfedea0SLionel Sambuc j = i2d_X509_NAME(name, NULL);
2121*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow_clean(buf, 4 + n + j + 2)) {
2122*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2123*0a6a1f1dSLionel Sambuc ERR_R_BUF_LIB);
2124ebfedea0SLionel Sambuc goto err;
2125ebfedea0SLionel Sambuc }
2126ebfedea0SLionel Sambuc p = (unsigned char *)&(buf->data[4 + n]);
2127*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG)) {
2128ebfedea0SLionel Sambuc s2n(j, p);
2129ebfedea0SLionel Sambuc i2d_X509_NAME(name, &p);
2130ebfedea0SLionel Sambuc n += 2 + j;
2131ebfedea0SLionel Sambuc nl += 2 + j;
2132*0a6a1f1dSLionel Sambuc } else {
2133ebfedea0SLionel Sambuc d = p;
2134ebfedea0SLionel Sambuc i2d_X509_NAME(name, &p);
2135*0a6a1f1dSLionel Sambuc j -= 2;
2136*0a6a1f1dSLionel Sambuc s2n(j, d);
2137*0a6a1f1dSLionel Sambuc j += 2;
2138ebfedea0SLionel Sambuc n += j;
2139ebfedea0SLionel Sambuc nl += j;
2140ebfedea0SLionel Sambuc }
2141ebfedea0SLionel Sambuc }
2142ebfedea0SLionel Sambuc }
2143ebfedea0SLionel Sambuc /* else no CA names */
2144ebfedea0SLionel Sambuc p = (unsigned char *)&(buf->data[4 + off]);
2145ebfedea0SLionel Sambuc s2n(nl, p);
2146ebfedea0SLionel Sambuc
2147ebfedea0SLionel Sambuc d = (unsigned char *)buf->data;
2148ebfedea0SLionel Sambuc *(d++) = SSL3_MT_CERTIFICATE_REQUEST;
2149ebfedea0SLionel Sambuc l2n3(n, d);
2150ebfedea0SLionel Sambuc
2151*0a6a1f1dSLionel Sambuc /*
2152*0a6a1f1dSLionel Sambuc * we should now have things packed up, so lets send it off
2153*0a6a1f1dSLionel Sambuc */
2154ebfedea0SLionel Sambuc
2155ebfedea0SLionel Sambuc s->init_num = n + 4;
2156ebfedea0SLionel Sambuc s->init_off = 0;
2157ebfedea0SLionel Sambuc #ifdef NETSCAPE_HANG_BUG
2158*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow_clean(buf, s->init_num + 4)) {
2159*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_BUF_LIB);
2160*0a6a1f1dSLionel Sambuc goto err;
2161*0a6a1f1dSLionel Sambuc }
2162ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data + s->init_num;
2163ebfedea0SLionel Sambuc
2164ebfedea0SLionel Sambuc /* do the header */
2165ebfedea0SLionel Sambuc *(p++) = SSL3_MT_SERVER_DONE;
2166ebfedea0SLionel Sambuc *(p++) = 0;
2167ebfedea0SLionel Sambuc *(p++) = 0;
2168ebfedea0SLionel Sambuc *(p++) = 0;
2169ebfedea0SLionel Sambuc s->init_num += 4;
2170ebfedea0SLionel Sambuc #endif
2171ebfedea0SLionel Sambuc
2172ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_REQ_B;
2173ebfedea0SLionel Sambuc }
2174ebfedea0SLionel Sambuc
2175ebfedea0SLionel Sambuc /* SSL3_ST_SW_CERT_REQ_B */
2176ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
2177ebfedea0SLionel Sambuc err:
2178*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2179ebfedea0SLionel Sambuc return (-1);
2180ebfedea0SLionel Sambuc }
2181ebfedea0SLionel Sambuc
ssl3_get_client_key_exchange(SSL * s)2182ebfedea0SLionel Sambuc int ssl3_get_client_key_exchange(SSL *s)
2183ebfedea0SLionel Sambuc {
2184ebfedea0SLionel Sambuc int i, al, ok;
2185ebfedea0SLionel Sambuc long n;
2186ebfedea0SLionel Sambuc unsigned long alg_k;
2187ebfedea0SLionel Sambuc unsigned char *p;
2188ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
2189ebfedea0SLionel Sambuc RSA *rsa = NULL;
2190ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
2191ebfedea0SLionel Sambuc #endif
2192ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
2193ebfedea0SLionel Sambuc BIGNUM *pub = NULL;
2194ebfedea0SLionel Sambuc DH *dh_srvr;
2195ebfedea0SLionel Sambuc #endif
2196ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
2197ebfedea0SLionel Sambuc KSSL_ERR kssl_err;
2198ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
2199ebfedea0SLionel Sambuc
2200ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2201ebfedea0SLionel Sambuc EC_KEY *srvr_ecdh = NULL;
2202ebfedea0SLionel Sambuc EVP_PKEY *clnt_pub_pkey = NULL;
2203ebfedea0SLionel Sambuc EC_POINT *clnt_ecpoint = NULL;
2204ebfedea0SLionel Sambuc BN_CTX *bn_ctx = NULL;
2205ebfedea0SLionel Sambuc #endif
2206ebfedea0SLionel Sambuc
2207ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
2208ebfedea0SLionel Sambuc SSL3_ST_SR_KEY_EXCH_A,
2209ebfedea0SLionel Sambuc SSL3_ST_SR_KEY_EXCH_B,
2210*0a6a1f1dSLionel Sambuc SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
2211ebfedea0SLionel Sambuc
2212*0a6a1f1dSLionel Sambuc if (!ok)
2213*0a6a1f1dSLionel Sambuc return ((int)n);
2214ebfedea0SLionel Sambuc p = (unsigned char *)s->init_msg;
2215ebfedea0SLionel Sambuc
2216ebfedea0SLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2217ebfedea0SLionel Sambuc
2218ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
2219*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kRSA) {
2220*0a6a1f1dSLionel Sambuc unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2221*0a6a1f1dSLionel Sambuc int decrypt_len;
2222*0a6a1f1dSLionel Sambuc unsigned char decrypt_good, version_good;
2223*0a6a1f1dSLionel Sambuc size_t j;
2224*0a6a1f1dSLionel Sambuc
2225ebfedea0SLionel Sambuc /* FIX THIS UP EAY EAY EAY EAY */
2226*0a6a1f1dSLionel Sambuc if (s->s3->tmp.use_rsa_tmp) {
2227ebfedea0SLionel Sambuc if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2228ebfedea0SLionel Sambuc rsa = s->cert->rsa_tmp;
2229*0a6a1f1dSLionel Sambuc /*
2230*0a6a1f1dSLionel Sambuc * Don't do a callback because rsa_tmp should be sent already
2231*0a6a1f1dSLionel Sambuc */
2232*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
2233ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2234*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2235*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_RSA_PKEY);
2236ebfedea0SLionel Sambuc goto f_err;
2237ebfedea0SLionel Sambuc
2238ebfedea0SLionel Sambuc }
2239*0a6a1f1dSLionel Sambuc } else {
2240ebfedea0SLionel Sambuc pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2241ebfedea0SLionel Sambuc if ((pkey == NULL) ||
2242*0a6a1f1dSLionel Sambuc (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2243ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2244*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2245*0a6a1f1dSLionel Sambuc SSL_R_MISSING_RSA_CERTIFICATE);
2246ebfedea0SLionel Sambuc goto f_err;
2247ebfedea0SLionel Sambuc }
2248ebfedea0SLionel Sambuc rsa = pkey->pkey.rsa;
2249ebfedea0SLionel Sambuc }
2250ebfedea0SLionel Sambuc
2251ebfedea0SLionel Sambuc /* TLS and [incidentally] DTLS{0xFEFF} */
2252*0a6a1f1dSLionel Sambuc if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2253ebfedea0SLionel Sambuc n2s(p, i);
2254*0a6a1f1dSLionel Sambuc if (n != i + 2) {
2255*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2256*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
2257*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2258*0a6a1f1dSLionel Sambuc SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2259*0a6a1f1dSLionel Sambuc goto f_err;
2260*0a6a1f1dSLionel Sambuc } else
2261ebfedea0SLionel Sambuc p -= 2;
2262*0a6a1f1dSLionel Sambuc } else
2263ebfedea0SLionel Sambuc n = i;
2264ebfedea0SLionel Sambuc }
2265ebfedea0SLionel Sambuc
2266*0a6a1f1dSLionel Sambuc /*
2267*0a6a1f1dSLionel Sambuc * Reject overly short RSA ciphertext because we want to be sure
2268*0a6a1f1dSLionel Sambuc * that the buffer size makes it safe to iterate over the entire
2269*0a6a1f1dSLionel Sambuc * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2270*0a6a1f1dSLionel Sambuc * actual expected size is larger due to RSA padding, but the
2271*0a6a1f1dSLionel Sambuc * bound is sufficient to be safe.
2272*0a6a1f1dSLionel Sambuc */
2273*0a6a1f1dSLionel Sambuc if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2274*0a6a1f1dSLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
2275*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2276*0a6a1f1dSLionel Sambuc SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2277*0a6a1f1dSLionel Sambuc goto f_err;
2278ebfedea0SLionel Sambuc }
2279ebfedea0SLionel Sambuc
2280*0a6a1f1dSLionel Sambuc /*
2281*0a6a1f1dSLionel Sambuc * We must not leak whether a decryption failure occurs because of
2282*0a6a1f1dSLionel Sambuc * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2283*0a6a1f1dSLionel Sambuc * section 7.4.7.1). The code follows that advice of the TLS RFC and
2284*0a6a1f1dSLionel Sambuc * generates a random premaster secret for the case that the decrypt
2285*0a6a1f1dSLionel Sambuc * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2286*0a6a1f1dSLionel Sambuc */
2287*0a6a1f1dSLionel Sambuc
2288*0a6a1f1dSLionel Sambuc /*
2289*0a6a1f1dSLionel Sambuc * should be RAND_bytes, but we cannot work around a failure.
2290*0a6a1f1dSLionel Sambuc */
2291*0a6a1f1dSLionel Sambuc if (RAND_pseudo_bytes(rand_premaster_secret,
2292*0a6a1f1dSLionel Sambuc sizeof(rand_premaster_secret)) <= 0)
2293*0a6a1f1dSLionel Sambuc goto err;
2294*0a6a1f1dSLionel Sambuc decrypt_len =
2295*0a6a1f1dSLionel Sambuc RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2296*0a6a1f1dSLionel Sambuc ERR_clear_error();
2297*0a6a1f1dSLionel Sambuc
2298*0a6a1f1dSLionel Sambuc /*
2299*0a6a1f1dSLionel Sambuc * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2300*0a6a1f1dSLionel Sambuc * be 0xff if so and zero otherwise.
2301*0a6a1f1dSLionel Sambuc */
2302*0a6a1f1dSLionel Sambuc decrypt_good =
2303*0a6a1f1dSLionel Sambuc constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2304*0a6a1f1dSLionel Sambuc
2305*0a6a1f1dSLionel Sambuc /*
2306*0a6a1f1dSLionel Sambuc * If the version in the decrypted pre-master secret is correct then
2307*0a6a1f1dSLionel Sambuc * version_good will be 0xff, otherwise it'll be zero. The
2308*0a6a1f1dSLionel Sambuc * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2309*0a6a1f1dSLionel Sambuc * (http://eprint.iacr.org/2003/052/) exploits the version number
2310*0a6a1f1dSLionel Sambuc * check as a "bad version oracle". Thus version checks are done in
2311*0a6a1f1dSLionel Sambuc * constant time and are treated like any other decryption error.
2312*0a6a1f1dSLionel Sambuc */
2313*0a6a1f1dSLionel Sambuc version_good =
2314*0a6a1f1dSLionel Sambuc constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2315*0a6a1f1dSLionel Sambuc version_good &=
2316*0a6a1f1dSLionel Sambuc constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2317*0a6a1f1dSLionel Sambuc
2318*0a6a1f1dSLionel Sambuc /*
2319*0a6a1f1dSLionel Sambuc * The premaster secret must contain the same version number as the
2320ebfedea0SLionel Sambuc * ClientHello to detect version rollback attacks (strangely, the
2321ebfedea0SLionel Sambuc * protocol does not offer such protection for DH ciphersuites).
2322ebfedea0SLionel Sambuc * However, buggy clients exist that send the negotiated protocol
2323ebfedea0SLionel Sambuc * version instead if the server does not support the requested
2324*0a6a1f1dSLionel Sambuc * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2325*0a6a1f1dSLionel Sambuc * clients.
2326*0a6a1f1dSLionel Sambuc */
2327*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2328*0a6a1f1dSLionel Sambuc unsigned char workaround_good;
2329*0a6a1f1dSLionel Sambuc workaround_good =
2330*0a6a1f1dSLionel Sambuc constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2331*0a6a1f1dSLionel Sambuc workaround_good &=
2332*0a6a1f1dSLionel Sambuc constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2333*0a6a1f1dSLionel Sambuc version_good |= workaround_good;
2334ebfedea0SLionel Sambuc }
2335ebfedea0SLionel Sambuc
2336*0a6a1f1dSLionel Sambuc /*
2337*0a6a1f1dSLionel Sambuc * Both decryption and version must be good for decrypt_good to
2338*0a6a1f1dSLionel Sambuc * remain non-zero (0xff).
2339*0a6a1f1dSLionel Sambuc */
2340*0a6a1f1dSLionel Sambuc decrypt_good &= version_good;
2341*0a6a1f1dSLionel Sambuc
2342*0a6a1f1dSLionel Sambuc /*
2343*0a6a1f1dSLionel Sambuc * Now copy rand_premaster_secret over from p using
2344*0a6a1f1dSLionel Sambuc * decrypt_good_mask. If decryption failed, then p does not
2345*0a6a1f1dSLionel Sambuc * contain valid plaintext, however, a check above guarantees
2346*0a6a1f1dSLionel Sambuc * it is still sufficiently large to read from.
2347*0a6a1f1dSLionel Sambuc */
2348*0a6a1f1dSLionel Sambuc for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2349*0a6a1f1dSLionel Sambuc p[j] = constant_time_select_8(decrypt_good, p[j],
2350*0a6a1f1dSLionel Sambuc rand_premaster_secret[j]);
2351ebfedea0SLionel Sambuc }
2352ebfedea0SLionel Sambuc
2353ebfedea0SLionel Sambuc s->session->master_key_length =
2354ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2355*0a6a1f1dSLionel Sambuc s->
2356*0a6a1f1dSLionel Sambuc session->master_key,
2357*0a6a1f1dSLionel Sambuc p,
2358*0a6a1f1dSLionel Sambuc sizeof
2359*0a6a1f1dSLionel Sambuc (rand_premaster_secret));
2360*0a6a1f1dSLionel Sambuc OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
2361*0a6a1f1dSLionel Sambuc } else
2362ebfedea0SLionel Sambuc #endif
2363ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
2364*0a6a1f1dSLionel Sambuc if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2365ebfedea0SLionel Sambuc n2s(p, i);
2366*0a6a1f1dSLionel Sambuc if (n != i + 2) {
2367*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2368*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2369*0a6a1f1dSLionel Sambuc SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2370ebfedea0SLionel Sambuc goto err;
2371*0a6a1f1dSLionel Sambuc } else {
2372ebfedea0SLionel Sambuc p -= 2;
2373ebfedea0SLionel Sambuc i = (int)n;
2374ebfedea0SLionel Sambuc }
2375ebfedea0SLionel Sambuc }
2376ebfedea0SLionel Sambuc
2377*0a6a1f1dSLionel Sambuc if (n == 0L) { /* the parameters are in the cert */
2378ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2379*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2380*0a6a1f1dSLionel Sambuc SSL_R_UNABLE_TO_DECODE_DH_CERTS);
2381ebfedea0SLionel Sambuc goto f_err;
2382*0a6a1f1dSLionel Sambuc } else {
2383*0a6a1f1dSLionel Sambuc if (s->s3->tmp.dh == NULL) {
2384ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2385*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2386*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_DH_KEY);
2387ebfedea0SLionel Sambuc goto f_err;
2388*0a6a1f1dSLionel Sambuc } else
2389ebfedea0SLionel Sambuc dh_srvr = s->s3->tmp.dh;
2390ebfedea0SLionel Sambuc }
2391ebfedea0SLionel Sambuc
2392ebfedea0SLionel Sambuc pub = BN_bin2bn(p, i, NULL);
2393*0a6a1f1dSLionel Sambuc if (pub == NULL) {
2394ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2395ebfedea0SLionel Sambuc goto err;
2396ebfedea0SLionel Sambuc }
2397ebfedea0SLionel Sambuc
2398ebfedea0SLionel Sambuc i = DH_compute_key(p, pub, dh_srvr);
2399ebfedea0SLionel Sambuc
2400*0a6a1f1dSLionel Sambuc if (i <= 0) {
2401ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2402ebfedea0SLionel Sambuc BN_clear_free(pub);
2403ebfedea0SLionel Sambuc goto err;
2404ebfedea0SLionel Sambuc }
2405ebfedea0SLionel Sambuc
2406ebfedea0SLionel Sambuc DH_free(s->s3->tmp.dh);
2407ebfedea0SLionel Sambuc s->s3->tmp.dh = NULL;
2408ebfedea0SLionel Sambuc
2409ebfedea0SLionel Sambuc BN_clear_free(pub);
2410ebfedea0SLionel Sambuc pub = NULL;
2411ebfedea0SLionel Sambuc s->session->master_key_length =
2412ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2413*0a6a1f1dSLionel Sambuc s->
2414*0a6a1f1dSLionel Sambuc session->master_key,
2415*0a6a1f1dSLionel Sambuc p, i);
2416ebfedea0SLionel Sambuc OPENSSL_cleanse(p, i);
2417*0a6a1f1dSLionel Sambuc } else
2418ebfedea0SLionel Sambuc #endif
2419ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
2420*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kKRB5) {
2421ebfedea0SLionel Sambuc krb5_error_code krb5rc;
2422ebfedea0SLionel Sambuc krb5_data enc_ticket;
2423ebfedea0SLionel Sambuc krb5_data authenticator;
2424ebfedea0SLionel Sambuc krb5_data enc_pms;
2425ebfedea0SLionel Sambuc KSSL_CTX *kssl_ctx = s->kssl_ctx;
2426ebfedea0SLionel Sambuc EVP_CIPHER_CTX ciph_ctx;
2427ebfedea0SLionel Sambuc const EVP_CIPHER *enc = NULL;
2428ebfedea0SLionel Sambuc unsigned char iv[EVP_MAX_IV_LENGTH];
2429*0a6a1f1dSLionel Sambuc unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2430ebfedea0SLionel Sambuc int padl, outl;
2431ebfedea0SLionel Sambuc krb5_timestamp authtime = 0;
2432ebfedea0SLionel Sambuc krb5_ticket_times ttimes;
2433*0a6a1f1dSLionel Sambuc int kerr = 0;
2434ebfedea0SLionel Sambuc
2435ebfedea0SLionel Sambuc EVP_CIPHER_CTX_init(&ciph_ctx);
2436ebfedea0SLionel Sambuc
2437*0a6a1f1dSLionel Sambuc if (!kssl_ctx)
2438*0a6a1f1dSLionel Sambuc kssl_ctx = kssl_ctx_new();
2439ebfedea0SLionel Sambuc
2440ebfedea0SLionel Sambuc n2s(p, i);
2441ebfedea0SLionel Sambuc enc_ticket.length = i;
2442ebfedea0SLionel Sambuc
2443*0a6a1f1dSLionel Sambuc if (n < (long)(enc_ticket.length + 6)) {
2444ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2445ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2446ebfedea0SLionel Sambuc goto err;
2447ebfedea0SLionel Sambuc }
2448ebfedea0SLionel Sambuc
2449ebfedea0SLionel Sambuc enc_ticket.data = (char *)p;
2450ebfedea0SLionel Sambuc p += enc_ticket.length;
2451ebfedea0SLionel Sambuc
2452ebfedea0SLionel Sambuc n2s(p, i);
2453ebfedea0SLionel Sambuc authenticator.length = i;
2454ebfedea0SLionel Sambuc
2455*0a6a1f1dSLionel Sambuc if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
2456ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2457ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2458ebfedea0SLionel Sambuc goto err;
2459ebfedea0SLionel Sambuc }
2460ebfedea0SLionel Sambuc
2461ebfedea0SLionel Sambuc authenticator.data = (char *)p;
2462ebfedea0SLionel Sambuc p += authenticator.length;
2463ebfedea0SLionel Sambuc
2464ebfedea0SLionel Sambuc n2s(p, i);
2465ebfedea0SLionel Sambuc enc_pms.length = i;
2466ebfedea0SLionel Sambuc enc_pms.data = (char *)p;
2467ebfedea0SLionel Sambuc p += enc_pms.length;
2468ebfedea0SLionel Sambuc
2469*0a6a1f1dSLionel Sambuc /*
2470*0a6a1f1dSLionel Sambuc * Note that the length is checked again below, ** after decryption
2471ebfedea0SLionel Sambuc */
2472*0a6a1f1dSLionel Sambuc if (enc_pms.length > sizeof pms) {
2473ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2474ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2475ebfedea0SLionel Sambuc goto err;
2476ebfedea0SLionel Sambuc }
2477ebfedea0SLionel Sambuc
2478ebfedea0SLionel Sambuc if (n != (long)(enc_ticket.length + authenticator.length +
2479*0a6a1f1dSLionel Sambuc enc_pms.length + 6)) {
2480ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2481ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2482ebfedea0SLionel Sambuc goto err;
2483ebfedea0SLionel Sambuc }
2484ebfedea0SLionel Sambuc
2485ebfedea0SLionel Sambuc if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2486*0a6a1f1dSLionel Sambuc &kssl_err)) != 0) {
2487ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
2488*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_sget_tkt rtn %d [%d]\n",
2489ebfedea0SLionel Sambuc krb5rc, kssl_err.reason);
2490ebfedea0SLionel Sambuc if (kssl_err.text)
2491*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2492ebfedea0SLionel Sambuc # endif /* KSSL_DEBUG */
2493*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2494ebfedea0SLionel Sambuc goto err;
2495ebfedea0SLionel Sambuc }
2496ebfedea0SLionel Sambuc
2497*0a6a1f1dSLionel Sambuc /*
2498*0a6a1f1dSLionel Sambuc * Note: no authenticator is not considered an error, ** but will
2499*0a6a1f1dSLionel Sambuc * return authtime == 0.
2500ebfedea0SLionel Sambuc */
2501ebfedea0SLionel Sambuc if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2502*0a6a1f1dSLionel Sambuc &authtime, &kssl_err)) != 0) {
2503ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
2504*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_check_authent rtn %d [%d]\n",
2505ebfedea0SLionel Sambuc krb5rc, kssl_err.reason);
2506ebfedea0SLionel Sambuc if (kssl_err.text)
2507*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2508ebfedea0SLionel Sambuc # endif /* KSSL_DEBUG */
2509*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2510ebfedea0SLionel Sambuc goto err;
2511ebfedea0SLionel Sambuc }
2512ebfedea0SLionel Sambuc
2513*0a6a1f1dSLionel Sambuc if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2514ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2515ebfedea0SLionel Sambuc goto err;
2516ebfedea0SLionel Sambuc }
2517ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
2518ebfedea0SLionel Sambuc kssl_ctx_show(kssl_ctx);
2519ebfedea0SLionel Sambuc # endif /* KSSL_DEBUG */
2520ebfedea0SLionel Sambuc
2521ebfedea0SLionel Sambuc enc = kssl_map_enc(kssl_ctx->enctype);
2522ebfedea0SLionel Sambuc if (enc == NULL)
2523ebfedea0SLionel Sambuc goto err;
2524ebfedea0SLionel Sambuc
2525ebfedea0SLionel Sambuc memset(iv, 0, sizeof iv); /* per RFC 1510 */
2526ebfedea0SLionel Sambuc
2527*0a6a1f1dSLionel Sambuc if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2528ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2529ebfedea0SLionel Sambuc SSL_R_DECRYPTION_FAILED);
2530ebfedea0SLionel Sambuc goto err;
2531ebfedea0SLionel Sambuc }
2532ebfedea0SLionel Sambuc if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2533ebfedea0SLionel Sambuc (unsigned char *)enc_pms.data, enc_pms.length))
2534ebfedea0SLionel Sambuc {
2535ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2536ebfedea0SLionel Sambuc SSL_R_DECRYPTION_FAILED);
2537*0a6a1f1dSLionel Sambuc kerr = 1;
2538*0a6a1f1dSLionel Sambuc goto kclean;
2539ebfedea0SLionel Sambuc }
2540*0a6a1f1dSLionel Sambuc if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2541ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2542ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2543*0a6a1f1dSLionel Sambuc kerr = 1;
2544*0a6a1f1dSLionel Sambuc goto kclean;
2545ebfedea0SLionel Sambuc }
2546*0a6a1f1dSLionel Sambuc if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2547ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2548ebfedea0SLionel Sambuc SSL_R_DECRYPTION_FAILED);
2549*0a6a1f1dSLionel Sambuc kerr = 1;
2550*0a6a1f1dSLionel Sambuc goto kclean;
2551ebfedea0SLionel Sambuc }
2552ebfedea0SLionel Sambuc outl += padl;
2553*0a6a1f1dSLionel Sambuc if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2554ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2555ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2556*0a6a1f1dSLionel Sambuc kerr = 1;
2557*0a6a1f1dSLionel Sambuc goto kclean;
2558ebfedea0SLionel Sambuc }
2559*0a6a1f1dSLionel Sambuc if (!((pms[0] == (s->client_version >> 8))
2560*0a6a1f1dSLionel Sambuc && (pms[1] == (s->client_version & 0xff)))) {
2561*0a6a1f1dSLionel Sambuc /*
2562*0a6a1f1dSLionel Sambuc * The premaster secret must contain the same version number as
2563*0a6a1f1dSLionel Sambuc * the ClientHello to detect version rollback attacks (strangely,
2564*0a6a1f1dSLionel Sambuc * the protocol does not offer such protection for DH
2565*0a6a1f1dSLionel Sambuc * ciphersuites). However, buggy clients exist that send random
2566*0a6a1f1dSLionel Sambuc * bytes instead of the protocol version. If
2567*0a6a1f1dSLionel Sambuc * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2568*0a6a1f1dSLionel Sambuc * (Perhaps we should have a separate BUG value for the Kerberos
2569*0a6a1f1dSLionel Sambuc * cipher)
2570ebfedea0SLionel Sambuc */
2571*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2572ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2573ebfedea0SLionel Sambuc SSL_AD_DECODE_ERROR);
2574*0a6a1f1dSLionel Sambuc kerr = 1;
2575*0a6a1f1dSLionel Sambuc goto kclean;
2576ebfedea0SLionel Sambuc }
2577ebfedea0SLionel Sambuc }
2578ebfedea0SLionel Sambuc
2579ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2580ebfedea0SLionel Sambuc
2581ebfedea0SLionel Sambuc s->session->master_key_length =
2582ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2583*0a6a1f1dSLionel Sambuc s->
2584*0a6a1f1dSLionel Sambuc session->master_key,
2585*0a6a1f1dSLionel Sambuc pms, outl);
2586ebfedea0SLionel Sambuc
2587*0a6a1f1dSLionel Sambuc if (kssl_ctx->client_princ) {
2588ebfedea0SLionel Sambuc size_t len = strlen(kssl_ctx->client_princ);
2589*0a6a1f1dSLionel Sambuc if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2590ebfedea0SLionel Sambuc s->session->krb5_client_princ_len = len;
2591*0a6a1f1dSLionel Sambuc memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2592*0a6a1f1dSLionel Sambuc len);
2593ebfedea0SLionel Sambuc }
2594ebfedea0SLionel Sambuc }
2595ebfedea0SLionel Sambuc
2596*0a6a1f1dSLionel Sambuc /*- Was doing kssl_ctx_free() here,
2597*0a6a1f1dSLionel Sambuc * but it caused problems for apache.
2598*0a6a1f1dSLionel Sambuc * kssl_ctx = kssl_ctx_free(kssl_ctx);
2599*0a6a1f1dSLionel Sambuc * if (s->kssl_ctx) s->kssl_ctx = NULL;
2600ebfedea0SLionel Sambuc */
2601*0a6a1f1dSLionel Sambuc
2602*0a6a1f1dSLionel Sambuc kclean:
2603*0a6a1f1dSLionel Sambuc OPENSSL_cleanse(pms, sizeof(pms));
2604*0a6a1f1dSLionel Sambuc if (kerr)
2605*0a6a1f1dSLionel Sambuc goto err;
2606*0a6a1f1dSLionel Sambuc } else
2607ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
2608ebfedea0SLionel Sambuc
2609ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2610*0a6a1f1dSLionel Sambuc if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2611ebfedea0SLionel Sambuc int ret = 1;
2612ebfedea0SLionel Sambuc int field_size = 0;
2613ebfedea0SLionel Sambuc const EC_KEY *tkey;
2614ebfedea0SLionel Sambuc const EC_GROUP *group;
2615ebfedea0SLionel Sambuc const BIGNUM *priv_key;
2616ebfedea0SLionel Sambuc
2617ebfedea0SLionel Sambuc /* initialize structures for server's ECDH key pair */
2618*0a6a1f1dSLionel Sambuc if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2619*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2620ebfedea0SLionel Sambuc goto err;
2621ebfedea0SLionel Sambuc }
2622ebfedea0SLionel Sambuc
2623ebfedea0SLionel Sambuc /* Let's get server private key and group information */
2624*0a6a1f1dSLionel Sambuc if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2625ebfedea0SLionel Sambuc /* use the certificate */
2626ebfedea0SLionel Sambuc tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2627*0a6a1f1dSLionel Sambuc } else {
2628*0a6a1f1dSLionel Sambuc /*
2629*0a6a1f1dSLionel Sambuc * use the ephermeral values we saved when generating the
2630*0a6a1f1dSLionel Sambuc * ServerKeyExchange msg.
2631ebfedea0SLionel Sambuc */
2632ebfedea0SLionel Sambuc tkey = s->s3->tmp.ecdh;
2633ebfedea0SLionel Sambuc }
2634ebfedea0SLionel Sambuc
2635ebfedea0SLionel Sambuc group = EC_KEY_get0_group(tkey);
2636ebfedea0SLionel Sambuc priv_key = EC_KEY_get0_private_key(tkey);
2637ebfedea0SLionel Sambuc
2638ebfedea0SLionel Sambuc if (!EC_KEY_set_group(srvr_ecdh, group) ||
2639*0a6a1f1dSLionel Sambuc !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2640*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2641ebfedea0SLionel Sambuc goto err;
2642ebfedea0SLionel Sambuc }
2643ebfedea0SLionel Sambuc
2644ebfedea0SLionel Sambuc /* Let's get client's public key */
2645*0a6a1f1dSLionel Sambuc if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2646*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2647ebfedea0SLionel Sambuc goto err;
2648ebfedea0SLionel Sambuc }
2649ebfedea0SLionel Sambuc
2650*0a6a1f1dSLionel Sambuc if (n == 0L) {
2651ebfedea0SLionel Sambuc /* Client Publickey was in Client Certificate */
2652ebfedea0SLionel Sambuc
2653*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kEECDH) {
2654ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2655*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2656*0a6a1f1dSLionel Sambuc SSL_R_MISSING_TMP_ECDH_KEY);
2657ebfedea0SLionel Sambuc goto f_err;
2658ebfedea0SLionel Sambuc }
2659ebfedea0SLionel Sambuc if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2660*0a6a1f1dSLionel Sambuc == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2661*0a6a1f1dSLionel Sambuc /*
2662*0a6a1f1dSLionel Sambuc * XXX: For now, we do not support client authentication
2663*0a6a1f1dSLionel Sambuc * using ECDH certificates so this branch (n == 0L) of the
2664*0a6a1f1dSLionel Sambuc * code is never executed. When that support is added, we
2665*0a6a1f1dSLionel Sambuc * ought to ensure the key received in the certificate is
2666*0a6a1f1dSLionel Sambuc * authorized for key agreement. ECDH_compute_key implicitly
2667*0a6a1f1dSLionel Sambuc * checks that the two ECDH shares are for the same group.
2668ebfedea0SLionel Sambuc */
2669ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2670ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2671ebfedea0SLionel Sambuc SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2672ebfedea0SLionel Sambuc goto f_err;
2673ebfedea0SLionel Sambuc }
2674ebfedea0SLionel Sambuc
2675ebfedea0SLionel Sambuc if (EC_POINT_copy(clnt_ecpoint,
2676*0a6a1f1dSLionel Sambuc EC_KEY_get0_public_key(clnt_pub_pkey->
2677*0a6a1f1dSLionel Sambuc pkey.ec)) == 0) {
2678*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2679ebfedea0SLionel Sambuc goto err;
2680ebfedea0SLionel Sambuc }
2681ebfedea0SLionel Sambuc ret = 2; /* Skip certificate verify processing */
2682*0a6a1f1dSLionel Sambuc } else {
2683*0a6a1f1dSLionel Sambuc /*
2684*0a6a1f1dSLionel Sambuc * Get client's public key from encoded point in the
2685*0a6a1f1dSLionel Sambuc * ClientKeyExchange message.
2686ebfedea0SLionel Sambuc */
2687*0a6a1f1dSLionel Sambuc if ((bn_ctx = BN_CTX_new()) == NULL) {
2688ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2689ebfedea0SLionel Sambuc ERR_R_MALLOC_FAILURE);
2690ebfedea0SLionel Sambuc goto err;
2691ebfedea0SLionel Sambuc }
2692ebfedea0SLionel Sambuc
2693ebfedea0SLionel Sambuc /* Get encoded point length */
2694ebfedea0SLionel Sambuc i = *p;
2695ebfedea0SLionel Sambuc p += 1;
2696*0a6a1f1dSLionel Sambuc if (n != 1 + i) {
2697*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2698ebfedea0SLionel Sambuc goto err;
2699ebfedea0SLionel Sambuc }
2700*0a6a1f1dSLionel Sambuc if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2701*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2702ebfedea0SLionel Sambuc goto err;
2703ebfedea0SLionel Sambuc }
2704*0a6a1f1dSLionel Sambuc /*
2705*0a6a1f1dSLionel Sambuc * p is pointing to somewhere in the buffer currently, so set it
2706*0a6a1f1dSLionel Sambuc * to the start
2707ebfedea0SLionel Sambuc */
2708ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
2709ebfedea0SLionel Sambuc }
2710ebfedea0SLionel Sambuc
2711ebfedea0SLionel Sambuc /* Compute the shared pre-master secret */
2712ebfedea0SLionel Sambuc field_size = EC_GROUP_get_degree(group);
2713*0a6a1f1dSLionel Sambuc if (field_size <= 0) {
2714*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2715ebfedea0SLionel Sambuc goto err;
2716ebfedea0SLionel Sambuc }
2717*0a6a1f1dSLionel Sambuc i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2718*0a6a1f1dSLionel Sambuc NULL);
2719*0a6a1f1dSLionel Sambuc if (i <= 0) {
2720*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2721ebfedea0SLionel Sambuc goto err;
2722ebfedea0SLionel Sambuc }
2723ebfedea0SLionel Sambuc
2724ebfedea0SLionel Sambuc EVP_PKEY_free(clnt_pub_pkey);
2725ebfedea0SLionel Sambuc EC_POINT_free(clnt_ecpoint);
2726ebfedea0SLionel Sambuc EC_KEY_free(srvr_ecdh);
2727ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
2728ebfedea0SLionel Sambuc EC_KEY_free(s->s3->tmp.ecdh);
2729ebfedea0SLionel Sambuc s->s3->tmp.ecdh = NULL;
2730ebfedea0SLionel Sambuc
2731ebfedea0SLionel Sambuc /* Compute the master secret */
2732*0a6a1f1dSLionel Sambuc s->session->master_key_length =
2733*0a6a1f1dSLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2734*0a6a1f1dSLionel Sambuc s->
2735*0a6a1f1dSLionel Sambuc session->master_key,
2736*0a6a1f1dSLionel Sambuc p, i);
2737ebfedea0SLionel Sambuc
2738ebfedea0SLionel Sambuc OPENSSL_cleanse(p, i);
2739ebfedea0SLionel Sambuc return (ret);
2740*0a6a1f1dSLionel Sambuc } else
2741ebfedea0SLionel Sambuc #endif
2742ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
2743*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kPSK) {
2744ebfedea0SLionel Sambuc unsigned char *t = NULL;
2745ebfedea0SLionel Sambuc unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2746ebfedea0SLionel Sambuc unsigned int pre_ms_len = 0, psk_len = 0;
2747ebfedea0SLionel Sambuc int psk_err = 1;
2748ebfedea0SLionel Sambuc char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2749ebfedea0SLionel Sambuc
2750ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2751ebfedea0SLionel Sambuc
2752ebfedea0SLionel Sambuc n2s(p, i);
2753*0a6a1f1dSLionel Sambuc if (n != i + 2) {
2754*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2755ebfedea0SLionel Sambuc goto psk_err;
2756ebfedea0SLionel Sambuc }
2757*0a6a1f1dSLionel Sambuc if (i > PSK_MAX_IDENTITY_LEN) {
2758ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2759ebfedea0SLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2760ebfedea0SLionel Sambuc goto psk_err;
2761ebfedea0SLionel Sambuc }
2762*0a6a1f1dSLionel Sambuc if (s->psk_server_callback == NULL) {
2763ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2764ebfedea0SLionel Sambuc SSL_R_PSK_NO_SERVER_CB);
2765ebfedea0SLionel Sambuc goto psk_err;
2766ebfedea0SLionel Sambuc }
2767ebfedea0SLionel Sambuc
2768*0a6a1f1dSLionel Sambuc /*
2769*0a6a1f1dSLionel Sambuc * Create guaranteed NULL-terminated identity string for the callback
2770*0a6a1f1dSLionel Sambuc */
2771ebfedea0SLionel Sambuc memcpy(tmp_id, p, i);
2772ebfedea0SLionel Sambuc memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2773ebfedea0SLionel Sambuc psk_len = s->psk_server_callback(s, tmp_id,
2774*0a6a1f1dSLionel Sambuc psk_or_pre_ms,
2775*0a6a1f1dSLionel Sambuc sizeof(psk_or_pre_ms));
2776ebfedea0SLionel Sambuc OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2777ebfedea0SLionel Sambuc
2778*0a6a1f1dSLionel Sambuc if (psk_len > PSK_MAX_PSK_LEN) {
2779*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2780ebfedea0SLionel Sambuc goto psk_err;
2781*0a6a1f1dSLionel Sambuc } else if (psk_len == 0) {
2782*0a6a1f1dSLionel Sambuc /*
2783*0a6a1f1dSLionel Sambuc * PSK related to the given identity not found
2784*0a6a1f1dSLionel Sambuc */
2785ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2786ebfedea0SLionel Sambuc SSL_R_PSK_IDENTITY_NOT_FOUND);
2787ebfedea0SLionel Sambuc al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2788ebfedea0SLionel Sambuc goto psk_err;
2789ebfedea0SLionel Sambuc }
2790ebfedea0SLionel Sambuc
2791ebfedea0SLionel Sambuc /* create PSK pre_master_secret */
2792ebfedea0SLionel Sambuc pre_ms_len = 2 + psk_len + 2 + psk_len;
2793ebfedea0SLionel Sambuc t = psk_or_pre_ms;
2794ebfedea0SLionel Sambuc memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2795ebfedea0SLionel Sambuc s2n(psk_len, t);
2796ebfedea0SLionel Sambuc memset(t, 0, psk_len);
2797ebfedea0SLionel Sambuc t += psk_len;
2798ebfedea0SLionel Sambuc s2n(psk_len, t);
2799ebfedea0SLionel Sambuc
2800ebfedea0SLionel Sambuc if (s->session->psk_identity != NULL)
2801ebfedea0SLionel Sambuc OPENSSL_free(s->session->psk_identity);
2802*0a6a1f1dSLionel Sambuc s->session->psk_identity = BUF_strndup((char *)p, i);
2803*0a6a1f1dSLionel Sambuc if (s->session->psk_identity == NULL) {
2804*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2805ebfedea0SLionel Sambuc goto psk_err;
2806ebfedea0SLionel Sambuc }
2807ebfedea0SLionel Sambuc
2808ebfedea0SLionel Sambuc if (s->session->psk_identity_hint != NULL)
2809ebfedea0SLionel Sambuc OPENSSL_free(s->session->psk_identity_hint);
2810ebfedea0SLionel Sambuc s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2811ebfedea0SLionel Sambuc if (s->ctx->psk_identity_hint != NULL &&
2812*0a6a1f1dSLionel Sambuc s->session->psk_identity_hint == NULL) {
2813*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2814ebfedea0SLionel Sambuc goto psk_err;
2815ebfedea0SLionel Sambuc }
2816ebfedea0SLionel Sambuc
2817ebfedea0SLionel Sambuc s->session->master_key_length =
2818ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2819*0a6a1f1dSLionel Sambuc s->
2820*0a6a1f1dSLionel Sambuc session->master_key,
2821*0a6a1f1dSLionel Sambuc psk_or_pre_ms,
2822*0a6a1f1dSLionel Sambuc pre_ms_len);
2823ebfedea0SLionel Sambuc psk_err = 0;
2824ebfedea0SLionel Sambuc psk_err:
2825ebfedea0SLionel Sambuc OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2826ebfedea0SLionel Sambuc if (psk_err != 0)
2827ebfedea0SLionel Sambuc goto f_err;
2828*0a6a1f1dSLionel Sambuc } else
2829ebfedea0SLionel Sambuc #endif
2830ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
2831*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kSRP) {
2832ebfedea0SLionel Sambuc int param_len;
2833ebfedea0SLionel Sambuc
2834ebfedea0SLionel Sambuc n2s(p, i);
2835ebfedea0SLionel Sambuc param_len = i + 2;
2836*0a6a1f1dSLionel Sambuc if (param_len > n) {
2837ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2838*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2839*0a6a1f1dSLionel Sambuc SSL_R_BAD_SRP_A_LENGTH);
2840ebfedea0SLionel Sambuc goto f_err;
2841ebfedea0SLionel Sambuc }
2842*0a6a1f1dSLionel Sambuc if (!(s->srp_ctx.A = BN_bin2bn(p, i, NULL))) {
2843ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2844ebfedea0SLionel Sambuc goto err;
2845ebfedea0SLionel Sambuc }
2846*0a6a1f1dSLionel Sambuc if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2847*0a6a1f1dSLionel Sambuc || BN_is_zero(s->srp_ctx.A)) {
2848*0a6a1f1dSLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
2849*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2850*0a6a1f1dSLionel Sambuc SSL_R_BAD_SRP_PARAMETERS);
2851*0a6a1f1dSLionel Sambuc goto f_err;
2852*0a6a1f1dSLionel Sambuc }
2853ebfedea0SLionel Sambuc if (s->session->srp_username != NULL)
2854ebfedea0SLionel Sambuc OPENSSL_free(s->session->srp_username);
2855ebfedea0SLionel Sambuc s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2856*0a6a1f1dSLionel Sambuc if (s->session->srp_username == NULL) {
2857*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2858ebfedea0SLionel Sambuc goto err;
2859ebfedea0SLionel Sambuc }
2860ebfedea0SLionel Sambuc
2861*0a6a1f1dSLionel Sambuc if ((s->session->master_key_length =
2862*0a6a1f1dSLionel Sambuc SRP_generate_server_master_secret(s,
2863*0a6a1f1dSLionel Sambuc s->session->master_key)) < 0) {
2864ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2865ebfedea0SLionel Sambuc goto err;
2866ebfedea0SLionel Sambuc }
2867ebfedea0SLionel Sambuc
2868ebfedea0SLionel Sambuc p += i;
2869*0a6a1f1dSLionel Sambuc } else
2870ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_SRP */
2871*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kGOST) {
2872ebfedea0SLionel Sambuc int ret = 0;
2873ebfedea0SLionel Sambuc EVP_PKEY_CTX *pkey_ctx;
2874ebfedea0SLionel Sambuc EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2875ebfedea0SLionel Sambuc unsigned char premaster_secret[32], *start;
2876ebfedea0SLionel Sambuc size_t outlen = 32, inlen;
2877ebfedea0SLionel Sambuc unsigned long alg_a;
2878*0a6a1f1dSLionel Sambuc int Ttag, Tclass;
2879*0a6a1f1dSLionel Sambuc long Tlen;
2880ebfedea0SLionel Sambuc
2881ebfedea0SLionel Sambuc /* Get our certificate private key */
2882ebfedea0SLionel Sambuc alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2883ebfedea0SLionel Sambuc if (alg_a & SSL_aGOST94)
2884ebfedea0SLionel Sambuc pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2885ebfedea0SLionel Sambuc else if (alg_a & SSL_aGOST01)
2886ebfedea0SLionel Sambuc pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2887ebfedea0SLionel Sambuc
2888ebfedea0SLionel Sambuc pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2889ebfedea0SLionel Sambuc EVP_PKEY_decrypt_init(pkey_ctx);
2890*0a6a1f1dSLionel Sambuc /*
2891*0a6a1f1dSLionel Sambuc * If client certificate is present and is of the same type, maybe
2892ebfedea0SLionel Sambuc * use it for key exchange. Don't mind errors from
2893*0a6a1f1dSLionel Sambuc * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2894*0a6a1f1dSLionel Sambuc * client certificate for authorization only.
2895*0a6a1f1dSLionel Sambuc */
2896ebfedea0SLionel Sambuc client_pub_pkey = X509_get_pubkey(s->session->peer);
2897*0a6a1f1dSLionel Sambuc if (client_pub_pkey) {
2898ebfedea0SLionel Sambuc if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2899ebfedea0SLionel Sambuc ERR_clear_error();
2900ebfedea0SLionel Sambuc }
2901ebfedea0SLionel Sambuc /* Decrypt session key */
2902*0a6a1f1dSLionel Sambuc if (ASN1_get_object
2903*0a6a1f1dSLionel Sambuc ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2904*0a6a1f1dSLionel Sambuc n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2905*0a6a1f1dSLionel Sambuc || Tclass != V_ASN1_UNIVERSAL) {
2906*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2907*0a6a1f1dSLionel Sambuc SSL_R_DECRYPTION_FAILED);
2908ebfedea0SLionel Sambuc goto gerr;
2909ebfedea0SLionel Sambuc }
2910*0a6a1f1dSLionel Sambuc start = p;
2911*0a6a1f1dSLionel Sambuc inlen = Tlen;
2912*0a6a1f1dSLionel Sambuc if (EVP_PKEY_decrypt
2913*0a6a1f1dSLionel Sambuc (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2914*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2915*0a6a1f1dSLionel Sambuc SSL_R_DECRYPTION_FAILED);
2916ebfedea0SLionel Sambuc goto gerr;
2917ebfedea0SLionel Sambuc }
2918ebfedea0SLionel Sambuc /* Generate master secret */
2919ebfedea0SLionel Sambuc s->session->master_key_length =
2920ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2921*0a6a1f1dSLionel Sambuc s->
2922*0a6a1f1dSLionel Sambuc session->master_key,
2923*0a6a1f1dSLionel Sambuc premaster_secret, 32);
2924*0a6a1f1dSLionel Sambuc OPENSSL_cleanse(premaster_secret, sizeof(premaster_secret));
2925ebfedea0SLionel Sambuc /* Check if pubkey from client certificate was used */
2926*0a6a1f1dSLionel Sambuc if (EVP_PKEY_CTX_ctrl
2927*0a6a1f1dSLionel Sambuc (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2928ebfedea0SLionel Sambuc ret = 2;
2929ebfedea0SLionel Sambuc else
2930ebfedea0SLionel Sambuc ret = 1;
2931ebfedea0SLionel Sambuc gerr:
2932ebfedea0SLionel Sambuc EVP_PKEY_free(client_pub_pkey);
2933ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(pkey_ctx);
2934ebfedea0SLionel Sambuc if (ret)
2935ebfedea0SLionel Sambuc return ret;
2936ebfedea0SLionel Sambuc else
2937ebfedea0SLionel Sambuc goto err;
2938*0a6a1f1dSLionel Sambuc } else {
2939ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
2940*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2941ebfedea0SLionel Sambuc goto f_err;
2942ebfedea0SLionel Sambuc }
2943ebfedea0SLionel Sambuc
2944ebfedea0SLionel Sambuc return (1);
2945ebfedea0SLionel Sambuc f_err:
2946ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
2947ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_ECDH) || defined(OPENSSL_NO_SRP)
2948ebfedea0SLionel Sambuc err:
2949ebfedea0SLionel Sambuc #endif
2950ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2951ebfedea0SLionel Sambuc EVP_PKEY_free(clnt_pub_pkey);
2952ebfedea0SLionel Sambuc EC_POINT_free(clnt_ecpoint);
2953ebfedea0SLionel Sambuc if (srvr_ecdh != NULL)
2954ebfedea0SLionel Sambuc EC_KEY_free(srvr_ecdh);
2955ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
2956ebfedea0SLionel Sambuc #endif
2957*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2958ebfedea0SLionel Sambuc return (-1);
2959ebfedea0SLionel Sambuc }
2960ebfedea0SLionel Sambuc
ssl3_get_cert_verify(SSL * s)2961ebfedea0SLionel Sambuc int ssl3_get_cert_verify(SSL *s)
2962ebfedea0SLionel Sambuc {
2963ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
2964ebfedea0SLionel Sambuc unsigned char *p;
2965ebfedea0SLionel Sambuc int al, ok, ret = 0;
2966ebfedea0SLionel Sambuc long n;
2967ebfedea0SLionel Sambuc int type = 0, i, j;
2968ebfedea0SLionel Sambuc X509 *peer;
2969ebfedea0SLionel Sambuc const EVP_MD *md = NULL;
2970ebfedea0SLionel Sambuc EVP_MD_CTX mctx;
2971ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx);
2972ebfedea0SLionel Sambuc
2973*0a6a1f1dSLionel Sambuc /*
2974*0a6a1f1dSLionel Sambuc * We should only process a CertificateVerify message if we have received
2975*0a6a1f1dSLionel Sambuc * a Certificate from the client. If so then |s->session->peer| will be non
2976*0a6a1f1dSLionel Sambuc * NULL. In some instances a CertificateVerify message is not required even
2977*0a6a1f1dSLionel Sambuc * if the peer has sent a Certificate (e.g. such as in the case of static
2978*0a6a1f1dSLionel Sambuc * DH). In that case the ClientKeyExchange processing will skip the
2979*0a6a1f1dSLionel Sambuc * CertificateVerify state so we should not arrive here.
2980*0a6a1f1dSLionel Sambuc */
2981*0a6a1f1dSLionel Sambuc if (s->session->peer == NULL) {
2982ebfedea0SLionel Sambuc ret = 1;
2983ebfedea0SLionel Sambuc goto end;
2984ebfedea0SLionel Sambuc }
2985ebfedea0SLionel Sambuc
2986*0a6a1f1dSLionel Sambuc n = s->method->ssl_get_message(s,
2987*0a6a1f1dSLionel Sambuc SSL3_ST_SR_CERT_VRFY_A,
2988*0a6a1f1dSLionel Sambuc SSL3_ST_SR_CERT_VRFY_B,
2989*0a6a1f1dSLionel Sambuc SSL3_MT_CERTIFICATE_VERIFY,
2990*0a6a1f1dSLionel Sambuc SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2991ebfedea0SLionel Sambuc
2992*0a6a1f1dSLionel Sambuc if (!ok)
2993*0a6a1f1dSLionel Sambuc return ((int)n);
2994*0a6a1f1dSLionel Sambuc
2995*0a6a1f1dSLionel Sambuc peer = s->session->peer;
2996*0a6a1f1dSLionel Sambuc pkey = X509_get_pubkey(peer);
2997*0a6a1f1dSLionel Sambuc type = X509_certificate_type(peer, pkey);
2998*0a6a1f1dSLionel Sambuc
2999*0a6a1f1dSLionel Sambuc if (!(type & EVP_PKT_SIGN)) {
3000*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3001*0a6a1f1dSLionel Sambuc SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
3002ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
3003ebfedea0SLionel Sambuc goto f_err;
3004ebfedea0SLionel Sambuc }
3005ebfedea0SLionel Sambuc
3006ebfedea0SLionel Sambuc /* we now have a signature that we need to verify */
3007ebfedea0SLionel Sambuc p = (unsigned char *)s->init_msg;
3008ebfedea0SLionel Sambuc /* Check for broken implementations of GOST ciphersuites */
3009*0a6a1f1dSLionel Sambuc /*
3010*0a6a1f1dSLionel Sambuc * If key is GOST and n is exactly 64, it is bare signature without
3011*0a6a1f1dSLionel Sambuc * length field
3012*0a6a1f1dSLionel Sambuc */
3013ebfedea0SLionel Sambuc if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
3014*0a6a1f1dSLionel Sambuc pkey->type == NID_id_GostR3410_2001)) {
3015ebfedea0SLionel Sambuc i = 64;
3016*0a6a1f1dSLionel Sambuc } else {
3017*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3018ebfedea0SLionel Sambuc int sigalg = tls12_get_sigid(pkey);
3019ebfedea0SLionel Sambuc /* Should never happen */
3020*0a6a1f1dSLionel Sambuc if (sigalg == -1) {
3021ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3022ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3023ebfedea0SLionel Sambuc goto f_err;
3024ebfedea0SLionel Sambuc }
3025ebfedea0SLionel Sambuc /* Check key type is consistent with signature */
3026*0a6a1f1dSLionel Sambuc if (sigalg != (int)p[1]) {
3027*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
3028*0a6a1f1dSLionel Sambuc SSL_R_WRONG_SIGNATURE_TYPE);
3029ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3030ebfedea0SLionel Sambuc goto f_err;
3031ebfedea0SLionel Sambuc }
3032ebfedea0SLionel Sambuc md = tls12_get_hash(p[0]);
3033*0a6a1f1dSLionel Sambuc if (md == NULL) {
3034ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_UNKNOWN_DIGEST);
3035ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3036ebfedea0SLionel Sambuc goto f_err;
3037ebfedea0SLionel Sambuc }
3038ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
3039ebfedea0SLionel Sambuc fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
3040ebfedea0SLionel Sambuc #endif
3041ebfedea0SLionel Sambuc p += 2;
3042ebfedea0SLionel Sambuc n -= 2;
3043ebfedea0SLionel Sambuc }
3044ebfedea0SLionel Sambuc n2s(p, i);
3045ebfedea0SLionel Sambuc n -= 2;
3046*0a6a1f1dSLionel Sambuc if (i > n) {
3047ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
3048ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3049ebfedea0SLionel Sambuc goto f_err;
3050ebfedea0SLionel Sambuc }
3051ebfedea0SLionel Sambuc }
3052ebfedea0SLionel Sambuc j = EVP_PKEY_size(pkey);
3053*0a6a1f1dSLionel Sambuc if ((i > j) || (n > j) || (n <= 0)) {
3054ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
3055ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3056ebfedea0SLionel Sambuc goto f_err;
3057ebfedea0SLionel Sambuc }
3058ebfedea0SLionel Sambuc
3059*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3060ebfedea0SLionel Sambuc long hdatalen = 0;
3061ebfedea0SLionel Sambuc void *hdata;
3062ebfedea0SLionel Sambuc hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3063*0a6a1f1dSLionel Sambuc if (hdatalen <= 0) {
3064ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3065ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3066ebfedea0SLionel Sambuc goto f_err;
3067ebfedea0SLionel Sambuc }
3068ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
3069ebfedea0SLionel Sambuc fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3070ebfedea0SLionel Sambuc EVP_MD_name(md));
3071ebfedea0SLionel Sambuc #endif
3072ebfedea0SLionel Sambuc if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3073*0a6a1f1dSLionel Sambuc || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3074ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3075ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3076ebfedea0SLionel Sambuc goto f_err;
3077ebfedea0SLionel Sambuc }
3078ebfedea0SLionel Sambuc
3079*0a6a1f1dSLionel Sambuc if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
3080ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3081ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3082ebfedea0SLionel Sambuc goto f_err;
3083ebfedea0SLionel Sambuc }
3084*0a6a1f1dSLionel Sambuc } else
3085ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
3086*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_RSA) {
3087ebfedea0SLionel Sambuc i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3088ebfedea0SLionel Sambuc MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
3089ebfedea0SLionel Sambuc pkey->pkey.rsa);
3090*0a6a1f1dSLionel Sambuc if (i < 0) {
3091ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3092ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3093ebfedea0SLionel Sambuc goto f_err;
3094ebfedea0SLionel Sambuc }
3095*0a6a1f1dSLionel Sambuc if (i == 0) {
3096ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3097ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3098ebfedea0SLionel Sambuc goto f_err;
3099ebfedea0SLionel Sambuc }
3100*0a6a1f1dSLionel Sambuc } else
3101ebfedea0SLionel Sambuc #endif
3102ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
3103*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_DSA) {
3104ebfedea0SLionel Sambuc j = DSA_verify(pkey->save_type,
3105ebfedea0SLionel Sambuc &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3106ebfedea0SLionel Sambuc SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3107*0a6a1f1dSLionel Sambuc if (j <= 0) {
3108ebfedea0SLionel Sambuc /* bad signature */
3109ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3110ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3111ebfedea0SLionel Sambuc goto f_err;
3112ebfedea0SLionel Sambuc }
3113*0a6a1f1dSLionel Sambuc } else
3114ebfedea0SLionel Sambuc #endif
3115ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
3116*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_EC) {
3117ebfedea0SLionel Sambuc j = ECDSA_verify(pkey->save_type,
3118ebfedea0SLionel Sambuc &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3119ebfedea0SLionel Sambuc SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3120*0a6a1f1dSLionel Sambuc if (j <= 0) {
3121ebfedea0SLionel Sambuc /* bad signature */
3122ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3123*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3124ebfedea0SLionel Sambuc goto f_err;
3125ebfedea0SLionel Sambuc }
3126*0a6a1f1dSLionel Sambuc } else
3127ebfedea0SLionel Sambuc #endif
3128*0a6a1f1dSLionel Sambuc if (pkey->type == NID_id_GostR3410_94
3129*0a6a1f1dSLionel Sambuc || pkey->type == NID_id_GostR3410_2001) {
3130*0a6a1f1dSLionel Sambuc unsigned char signature[64];
3131ebfedea0SLionel Sambuc int idx;
3132ebfedea0SLionel Sambuc EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3133ebfedea0SLionel Sambuc EVP_PKEY_verify_init(pctx);
3134ebfedea0SLionel Sambuc if (i != 64) {
3135ebfedea0SLionel Sambuc fprintf(stderr, "GOST signature length is %d", i);
3136ebfedea0SLionel Sambuc }
3137ebfedea0SLionel Sambuc for (idx = 0; idx < 64; idx++) {
3138ebfedea0SLionel Sambuc signature[63 - idx] = p[idx];
3139ebfedea0SLionel Sambuc }
3140*0a6a1f1dSLionel Sambuc j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3141*0a6a1f1dSLionel Sambuc 32);
3142ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(pctx);
3143*0a6a1f1dSLionel Sambuc if (j <= 0) {
3144ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
3145*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3146ebfedea0SLionel Sambuc goto f_err;
3147ebfedea0SLionel Sambuc }
3148*0a6a1f1dSLionel Sambuc } else {
3149ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3150ebfedea0SLionel Sambuc al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3151ebfedea0SLionel Sambuc goto f_err;
3152ebfedea0SLionel Sambuc }
3153ebfedea0SLionel Sambuc
3154ebfedea0SLionel Sambuc ret = 1;
3155*0a6a1f1dSLionel Sambuc if (0) {
3156ebfedea0SLionel Sambuc f_err:
3157ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
3158*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3159ebfedea0SLionel Sambuc }
3160ebfedea0SLionel Sambuc end:
3161*0a6a1f1dSLionel Sambuc if (s->s3->handshake_buffer) {
3162ebfedea0SLionel Sambuc BIO_free(s->s3->handshake_buffer);
3163ebfedea0SLionel Sambuc s->s3->handshake_buffer = NULL;
3164ebfedea0SLionel Sambuc s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3165ebfedea0SLionel Sambuc }
3166ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
3167ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
3168ebfedea0SLionel Sambuc return (ret);
3169ebfedea0SLionel Sambuc }
3170ebfedea0SLionel Sambuc
ssl3_get_client_certificate(SSL * s)3171ebfedea0SLionel Sambuc int ssl3_get_client_certificate(SSL *s)
3172ebfedea0SLionel Sambuc {
3173ebfedea0SLionel Sambuc int i, ok, al, ret = -1;
3174ebfedea0SLionel Sambuc X509 *x = NULL;
3175ebfedea0SLionel Sambuc unsigned long l, nc, llen, n;
3176ebfedea0SLionel Sambuc const unsigned char *p, *q;
3177ebfedea0SLionel Sambuc unsigned char *d;
3178ebfedea0SLionel Sambuc STACK_OF(X509) *sk = NULL;
3179ebfedea0SLionel Sambuc
3180ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
3181ebfedea0SLionel Sambuc SSL3_ST_SR_CERT_A,
3182ebfedea0SLionel Sambuc SSL3_ST_SR_CERT_B,
3183*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
3184ebfedea0SLionel Sambuc
3185*0a6a1f1dSLionel Sambuc if (!ok)
3186*0a6a1f1dSLionel Sambuc return ((int)n);
3187ebfedea0SLionel Sambuc
3188*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3189ebfedea0SLionel Sambuc if ((s->verify_mode & SSL_VERIFY_PEER) &&
3190*0a6a1f1dSLionel Sambuc (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3191*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3192*0a6a1f1dSLionel Sambuc SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3193ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
3194ebfedea0SLionel Sambuc goto f_err;
3195ebfedea0SLionel Sambuc }
3196*0a6a1f1dSLionel Sambuc /*
3197*0a6a1f1dSLionel Sambuc * If tls asked for a client cert, the client must return a 0 list
3198*0a6a1f1dSLionel Sambuc */
3199*0a6a1f1dSLionel Sambuc if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3200*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3201*0a6a1f1dSLionel Sambuc SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3202ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
3203ebfedea0SLionel Sambuc goto f_err;
3204ebfedea0SLionel Sambuc }
3205ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
3206ebfedea0SLionel Sambuc return (1);
3207ebfedea0SLionel Sambuc }
3208ebfedea0SLionel Sambuc
3209*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3210ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
3211ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3212ebfedea0SLionel Sambuc goto f_err;
3213ebfedea0SLionel Sambuc }
3214ebfedea0SLionel Sambuc p = d = (unsigned char *)s->init_msg;
3215ebfedea0SLionel Sambuc
3216*0a6a1f1dSLionel Sambuc if ((sk = sk_X509_new_null()) == NULL) {
3217ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3218ebfedea0SLionel Sambuc goto err;
3219ebfedea0SLionel Sambuc }
3220ebfedea0SLionel Sambuc
3221ebfedea0SLionel Sambuc n2l3(p, llen);
3222*0a6a1f1dSLionel Sambuc if (llen + 3 != n) {
3223ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3224ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3225ebfedea0SLionel Sambuc goto f_err;
3226ebfedea0SLionel Sambuc }
3227*0a6a1f1dSLionel Sambuc for (nc = 0; nc < llen;) {
3228ebfedea0SLionel Sambuc n2l3(p, l);
3229*0a6a1f1dSLionel Sambuc if ((l + nc + 3) > llen) {
3230ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3231*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3232*0a6a1f1dSLionel Sambuc SSL_R_CERT_LENGTH_MISMATCH);
3233ebfedea0SLionel Sambuc goto f_err;
3234ebfedea0SLionel Sambuc }
3235ebfedea0SLionel Sambuc
3236ebfedea0SLionel Sambuc q = p;
3237ebfedea0SLionel Sambuc x = d2i_X509(NULL, &p, l);
3238*0a6a1f1dSLionel Sambuc if (x == NULL) {
3239ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3240ebfedea0SLionel Sambuc goto err;
3241ebfedea0SLionel Sambuc }
3242*0a6a1f1dSLionel Sambuc if (p != (q + l)) {
3243ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
3244*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3245*0a6a1f1dSLionel Sambuc SSL_R_CERT_LENGTH_MISMATCH);
3246ebfedea0SLionel Sambuc goto f_err;
3247ebfedea0SLionel Sambuc }
3248*0a6a1f1dSLionel Sambuc if (!sk_X509_push(sk, x)) {
3249ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3250ebfedea0SLionel Sambuc goto err;
3251ebfedea0SLionel Sambuc }
3252ebfedea0SLionel Sambuc x = NULL;
3253ebfedea0SLionel Sambuc nc += l + 3;
3254ebfedea0SLionel Sambuc }
3255ebfedea0SLionel Sambuc
3256*0a6a1f1dSLionel Sambuc if (sk_X509_num(sk) <= 0) {
3257ebfedea0SLionel Sambuc /* TLS does not mind 0 certs returned */
3258*0a6a1f1dSLionel Sambuc if (s->version == SSL3_VERSION) {
3259ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
3260*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3261*0a6a1f1dSLionel Sambuc SSL_R_NO_CERTIFICATES_RETURNED);
3262ebfedea0SLionel Sambuc goto f_err;
3263ebfedea0SLionel Sambuc }
3264ebfedea0SLionel Sambuc /* Fail for TLS only if we required a certificate */
3265ebfedea0SLionel Sambuc else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3266*0a6a1f1dSLionel Sambuc (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3267*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3268*0a6a1f1dSLionel Sambuc SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3269ebfedea0SLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
3270ebfedea0SLionel Sambuc goto f_err;
3271ebfedea0SLionel Sambuc }
3272ebfedea0SLionel Sambuc /* No client certificate so digest cached records */
3273*0a6a1f1dSLionel Sambuc if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3274ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3275ebfedea0SLionel Sambuc goto f_err;
3276ebfedea0SLionel Sambuc }
3277*0a6a1f1dSLionel Sambuc } else {
3278ebfedea0SLionel Sambuc i = ssl_verify_cert_chain(s, sk);
3279*0a6a1f1dSLionel Sambuc if (i <= 0) {
3280ebfedea0SLionel Sambuc al = ssl_verify_alarm_type(s->verify_result);
3281*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3282*0a6a1f1dSLionel Sambuc SSL_R_NO_CERTIFICATE_RETURNED);
3283ebfedea0SLionel Sambuc goto f_err;
3284ebfedea0SLionel Sambuc }
3285ebfedea0SLionel Sambuc }
3286ebfedea0SLionel Sambuc
3287ebfedea0SLionel Sambuc if (s->session->peer != NULL) /* This should not be needed */
3288ebfedea0SLionel Sambuc X509_free(s->session->peer);
3289ebfedea0SLionel Sambuc s->session->peer = sk_X509_shift(sk);
3290ebfedea0SLionel Sambuc s->session->verify_result = s->verify_result;
3291ebfedea0SLionel Sambuc
3292*0a6a1f1dSLionel Sambuc /*
3293*0a6a1f1dSLionel Sambuc * With the current implementation, sess_cert will always be NULL when we
3294*0a6a1f1dSLionel Sambuc * arrive here.
3295*0a6a1f1dSLionel Sambuc */
3296*0a6a1f1dSLionel Sambuc if (s->session->sess_cert == NULL) {
3297ebfedea0SLionel Sambuc s->session->sess_cert = ssl_sess_cert_new();
3298*0a6a1f1dSLionel Sambuc if (s->session->sess_cert == NULL) {
3299ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3300ebfedea0SLionel Sambuc goto err;
3301ebfedea0SLionel Sambuc }
3302ebfedea0SLionel Sambuc }
3303ebfedea0SLionel Sambuc if (s->session->sess_cert->cert_chain != NULL)
3304ebfedea0SLionel Sambuc sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3305ebfedea0SLionel Sambuc s->session->sess_cert->cert_chain = sk;
3306*0a6a1f1dSLionel Sambuc /*
3307*0a6a1f1dSLionel Sambuc * Inconsistency alert: cert_chain does *not* include the peer's own
3308*0a6a1f1dSLionel Sambuc * certificate, while we do include it in s3_clnt.c
3309*0a6a1f1dSLionel Sambuc */
3310ebfedea0SLionel Sambuc
3311ebfedea0SLionel Sambuc sk = NULL;
3312ebfedea0SLionel Sambuc
3313ebfedea0SLionel Sambuc ret = 1;
3314*0a6a1f1dSLionel Sambuc if (0) {
3315ebfedea0SLionel Sambuc f_err:
3316ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
3317ebfedea0SLionel Sambuc err:
3318*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3319*0a6a1f1dSLionel Sambuc }
3320*0a6a1f1dSLionel Sambuc
3321*0a6a1f1dSLionel Sambuc if (x != NULL)
3322*0a6a1f1dSLionel Sambuc X509_free(x);
3323*0a6a1f1dSLionel Sambuc if (sk != NULL)
3324*0a6a1f1dSLionel Sambuc sk_X509_pop_free(sk, X509_free);
3325ebfedea0SLionel Sambuc return (ret);
3326ebfedea0SLionel Sambuc }
3327ebfedea0SLionel Sambuc
ssl3_send_server_certificate(SSL * s)3328ebfedea0SLionel Sambuc int ssl3_send_server_certificate(SSL *s)
3329ebfedea0SLionel Sambuc {
3330ebfedea0SLionel Sambuc unsigned long l;
3331ebfedea0SLionel Sambuc X509 *x;
3332ebfedea0SLionel Sambuc
3333*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_CERT_A) {
3334ebfedea0SLionel Sambuc x = ssl_get_server_send_cert(s);
3335*0a6a1f1dSLionel Sambuc if (x == NULL) {
3336ebfedea0SLionel Sambuc /* VRS: allow null cert if auth == KRB5 */
3337ebfedea0SLionel Sambuc if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3338*0a6a1f1dSLionel Sambuc (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
3339*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
3340*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
3341*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3342ebfedea0SLionel Sambuc return (0);
3343ebfedea0SLionel Sambuc }
3344ebfedea0SLionel Sambuc }
3345ebfedea0SLionel Sambuc
3346ebfedea0SLionel Sambuc l = ssl3_output_cert_chain(s, x);
3347*0a6a1f1dSLionel Sambuc if (!l) {
3348*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3349*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3350*0a6a1f1dSLionel Sambuc return (0);
3351*0a6a1f1dSLionel Sambuc }
3352ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_B;
3353ebfedea0SLionel Sambuc s->init_num = (int)l;
3354ebfedea0SLionel Sambuc s->init_off = 0;
3355ebfedea0SLionel Sambuc }
3356ebfedea0SLionel Sambuc
3357ebfedea0SLionel Sambuc /* SSL3_ST_SW_CERT_B */
3358ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3359ebfedea0SLionel Sambuc }
3360ebfedea0SLionel Sambuc
3361ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
3362ebfedea0SLionel Sambuc /* send a new session ticket (not necessarily for a new session) */
ssl3_send_newsession_ticket(SSL * s)3363ebfedea0SLionel Sambuc int ssl3_send_newsession_ticket(SSL *s)
3364ebfedea0SLionel Sambuc {
3365*0a6a1f1dSLionel Sambuc unsigned char *senc = NULL;
3366*0a6a1f1dSLionel Sambuc EVP_CIPHER_CTX ctx;
3367*0a6a1f1dSLionel Sambuc HMAC_CTX hctx;
3368*0a6a1f1dSLionel Sambuc
3369*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
3370*0a6a1f1dSLionel Sambuc unsigned char *p, *macstart;
3371ebfedea0SLionel Sambuc const unsigned char *const_p;
3372ebfedea0SLionel Sambuc int len, slen_full, slen;
3373ebfedea0SLionel Sambuc SSL_SESSION *sess;
3374ebfedea0SLionel Sambuc unsigned int hlen;
3375ebfedea0SLionel Sambuc SSL_CTX *tctx = s->initial_ctx;
3376ebfedea0SLionel Sambuc unsigned char iv[EVP_MAX_IV_LENGTH];
3377ebfedea0SLionel Sambuc unsigned char key_name[16];
3378ebfedea0SLionel Sambuc
3379ebfedea0SLionel Sambuc /* get session encoding length */
3380ebfedea0SLionel Sambuc slen_full = i2d_SSL_SESSION(s->session, NULL);
3381*0a6a1f1dSLionel Sambuc /*
3382*0a6a1f1dSLionel Sambuc * Some length values are 16 bits, so forget it if session is too
3383*0a6a1f1dSLionel Sambuc * long
3384ebfedea0SLionel Sambuc */
3385*0a6a1f1dSLionel Sambuc if (slen_full == 0 || slen_full > 0xFF00) {
3386*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3387ebfedea0SLionel Sambuc return -1;
3388*0a6a1f1dSLionel Sambuc }
3389ebfedea0SLionel Sambuc senc = OPENSSL_malloc(slen_full);
3390*0a6a1f1dSLionel Sambuc if (!senc) {
3391*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3392ebfedea0SLionel Sambuc return -1;
3393*0a6a1f1dSLionel Sambuc }
3394ebfedea0SLionel Sambuc
3395*0a6a1f1dSLionel Sambuc EVP_CIPHER_CTX_init(&ctx);
3396*0a6a1f1dSLionel Sambuc HMAC_CTX_init(&hctx);
3397*0a6a1f1dSLionel Sambuc
3398*0a6a1f1dSLionel Sambuc p = senc;
3399*0a6a1f1dSLionel Sambuc if (!i2d_SSL_SESSION(s->session, &p))
3400*0a6a1f1dSLionel Sambuc goto err;
3401*0a6a1f1dSLionel Sambuc
3402*0a6a1f1dSLionel Sambuc /*
3403*0a6a1f1dSLionel Sambuc * create a fresh copy (not shared with other threads) to clean up
3404*0a6a1f1dSLionel Sambuc */
3405ebfedea0SLionel Sambuc const_p = senc;
3406ebfedea0SLionel Sambuc sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
3407ebfedea0SLionel Sambuc if (sess == NULL)
3408*0a6a1f1dSLionel Sambuc goto err;
3409ebfedea0SLionel Sambuc sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3410ebfedea0SLionel Sambuc
3411ebfedea0SLionel Sambuc slen = i2d_SSL_SESSION(sess, NULL);
3412*0a6a1f1dSLionel Sambuc if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3413*0a6a1f1dSLionel Sambuc SSL_SESSION_free(sess);
3414*0a6a1f1dSLionel Sambuc goto err;
3415ebfedea0SLionel Sambuc }
3416ebfedea0SLionel Sambuc p = senc;
3417*0a6a1f1dSLionel Sambuc if (!i2d_SSL_SESSION(sess, &p)) {
3418*0a6a1f1dSLionel Sambuc SSL_SESSION_free(sess);
3419*0a6a1f1dSLionel Sambuc goto err;
3420*0a6a1f1dSLionel Sambuc }
3421ebfedea0SLionel Sambuc SSL_SESSION_free(sess);
3422ebfedea0SLionel Sambuc
3423*0a6a1f1dSLionel Sambuc /*-
3424*0a6a1f1dSLionel Sambuc * Grow buffer if need be: the length calculation is as
3425ebfedea0SLionel Sambuc * follows 1 (size of message name) + 3 (message length
3426ebfedea0SLionel Sambuc * bytes) + 4 (ticket lifetime hint) + 2 (ticket length) +
3427ebfedea0SLionel Sambuc * 16 (key name) + max_iv_len (iv length) +
3428ebfedea0SLionel Sambuc * session_length + max_enc_block_size (max encrypted session
3429ebfedea0SLionel Sambuc * length) + max_md_size (HMAC).
3430ebfedea0SLionel Sambuc */
3431ebfedea0SLionel Sambuc if (!BUF_MEM_grow(s->init_buf,
3432ebfedea0SLionel Sambuc 26 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH +
3433ebfedea0SLionel Sambuc EVP_MAX_MD_SIZE + slen))
3434*0a6a1f1dSLionel Sambuc goto err;
3435ebfedea0SLionel Sambuc
3436ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
3437ebfedea0SLionel Sambuc /* do the header */
3438ebfedea0SLionel Sambuc *(p++) = SSL3_MT_NEWSESSION_TICKET;
3439ebfedea0SLionel Sambuc /* Skip message length for now */
3440ebfedea0SLionel Sambuc p += 3;
3441*0a6a1f1dSLionel Sambuc /*
3442*0a6a1f1dSLionel Sambuc * Initialize HMAC and cipher contexts. If callback present it does
3443*0a6a1f1dSLionel Sambuc * all the work otherwise use generated values from parent ctx.
3444ebfedea0SLionel Sambuc */
3445*0a6a1f1dSLionel Sambuc if (tctx->tlsext_ticket_key_cb) {
3446ebfedea0SLionel Sambuc if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
3447ebfedea0SLionel Sambuc &hctx, 1) < 0)
3448*0a6a1f1dSLionel Sambuc goto err;
3449*0a6a1f1dSLionel Sambuc } else {
3450*0a6a1f1dSLionel Sambuc if (RAND_bytes(iv, 16) <= 0)
3451*0a6a1f1dSLionel Sambuc goto err;
3452*0a6a1f1dSLionel Sambuc if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3453*0a6a1f1dSLionel Sambuc tctx->tlsext_tick_aes_key, iv))
3454*0a6a1f1dSLionel Sambuc goto err;
3455*0a6a1f1dSLionel Sambuc if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3456*0a6a1f1dSLionel Sambuc tlsext_tick_md(), NULL))
3457*0a6a1f1dSLionel Sambuc goto err;
3458ebfedea0SLionel Sambuc memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3459ebfedea0SLionel Sambuc }
3460ebfedea0SLionel Sambuc
3461*0a6a1f1dSLionel Sambuc /*
3462*0a6a1f1dSLionel Sambuc * Ticket lifetime hint (advisory only): We leave this unspecified
3463*0a6a1f1dSLionel Sambuc * for resumed session (for simplicity), and guess that tickets for
3464*0a6a1f1dSLionel Sambuc * new sessions will live as long as their sessions.
3465*0a6a1f1dSLionel Sambuc */
3466ebfedea0SLionel Sambuc l2n(s->hit ? 0 : s->session->timeout, p);
3467ebfedea0SLionel Sambuc
3468ebfedea0SLionel Sambuc /* Skip ticket length for now */
3469ebfedea0SLionel Sambuc p += 2;
3470ebfedea0SLionel Sambuc /* Output key name */
3471ebfedea0SLionel Sambuc macstart = p;
3472ebfedea0SLionel Sambuc memcpy(p, key_name, 16);
3473ebfedea0SLionel Sambuc p += 16;
3474ebfedea0SLionel Sambuc /* output IV */
3475ebfedea0SLionel Sambuc memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3476ebfedea0SLionel Sambuc p += EVP_CIPHER_CTX_iv_length(&ctx);
3477ebfedea0SLionel Sambuc /* Encrypt session data */
3478*0a6a1f1dSLionel Sambuc if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3479*0a6a1f1dSLionel Sambuc goto err;
3480ebfedea0SLionel Sambuc p += len;
3481*0a6a1f1dSLionel Sambuc if (!EVP_EncryptFinal(&ctx, p, &len))
3482*0a6a1f1dSLionel Sambuc goto err;
3483ebfedea0SLionel Sambuc p += len;
3484ebfedea0SLionel Sambuc
3485*0a6a1f1dSLionel Sambuc if (!HMAC_Update(&hctx, macstart, p - macstart))
3486*0a6a1f1dSLionel Sambuc goto err;
3487*0a6a1f1dSLionel Sambuc if (!HMAC_Final(&hctx, p, &hlen))
3488*0a6a1f1dSLionel Sambuc goto err;
3489*0a6a1f1dSLionel Sambuc
3490*0a6a1f1dSLionel Sambuc EVP_CIPHER_CTX_cleanup(&ctx);
3491ebfedea0SLionel Sambuc HMAC_CTX_cleanup(&hctx);
3492ebfedea0SLionel Sambuc
3493ebfedea0SLionel Sambuc p += hlen;
3494ebfedea0SLionel Sambuc /* Now write out lengths: p points to end of data written */
3495ebfedea0SLionel Sambuc /* Total length */
3496ebfedea0SLionel Sambuc len = p - (unsigned char *)s->init_buf->data;
3497ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data + 1;
3498ebfedea0SLionel Sambuc l2n3(len - 4, p); /* Message length */
3499ebfedea0SLionel Sambuc p += 4;
3500ebfedea0SLionel Sambuc s2n(len - 10, p); /* Ticket length */
3501ebfedea0SLionel Sambuc
3502ebfedea0SLionel Sambuc /* number of bytes to write */
3503ebfedea0SLionel Sambuc s->init_num = len;
3504ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_SESSION_TICKET_B;
3505ebfedea0SLionel Sambuc s->init_off = 0;
3506ebfedea0SLionel Sambuc OPENSSL_free(senc);
3507ebfedea0SLionel Sambuc }
3508ebfedea0SLionel Sambuc
3509ebfedea0SLionel Sambuc /* SSL3_ST_SW_SESSION_TICKET_B */
3510ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3511*0a6a1f1dSLionel Sambuc err:
3512*0a6a1f1dSLionel Sambuc if (senc)
3513*0a6a1f1dSLionel Sambuc OPENSSL_free(senc);
3514*0a6a1f1dSLionel Sambuc EVP_CIPHER_CTX_cleanup(&ctx);
3515*0a6a1f1dSLionel Sambuc HMAC_CTX_cleanup(&hctx);
3516*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3517*0a6a1f1dSLionel Sambuc return -1;
3518ebfedea0SLionel Sambuc }
3519ebfedea0SLionel Sambuc
ssl3_send_cert_status(SSL * s)3520ebfedea0SLionel Sambuc int ssl3_send_cert_status(SSL *s)
3521ebfedea0SLionel Sambuc {
3522*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3523ebfedea0SLionel Sambuc unsigned char *p;
3524*0a6a1f1dSLionel Sambuc /*-
3525*0a6a1f1dSLionel Sambuc * Grow buffer if need be: the length calculation is as
3526ebfedea0SLionel Sambuc * follows 1 (message type) + 3 (message length) +
3527ebfedea0SLionel Sambuc * 1 (ocsp response type) + 3 (ocsp response length)
3528ebfedea0SLionel Sambuc * + (ocsp response)
3529ebfedea0SLionel Sambuc */
3530*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen)) {
3531*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3532ebfedea0SLionel Sambuc return -1;
3533*0a6a1f1dSLionel Sambuc }
3534ebfedea0SLionel Sambuc
3535ebfedea0SLionel Sambuc p = (unsigned char *)s->init_buf->data;
3536ebfedea0SLionel Sambuc
3537ebfedea0SLionel Sambuc /* do the header */
3538ebfedea0SLionel Sambuc *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3539ebfedea0SLionel Sambuc /* message length */
3540ebfedea0SLionel Sambuc l2n3(s->tlsext_ocsp_resplen + 4, p);
3541ebfedea0SLionel Sambuc /* status type */
3542ebfedea0SLionel Sambuc *(p++) = s->tlsext_status_type;
3543ebfedea0SLionel Sambuc /* length of OCSP response */
3544ebfedea0SLionel Sambuc l2n3(s->tlsext_ocsp_resplen, p);
3545ebfedea0SLionel Sambuc /* actual response */
3546ebfedea0SLionel Sambuc memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3547ebfedea0SLionel Sambuc /* number of bytes to write */
3548ebfedea0SLionel Sambuc s->init_num = 8 + s->tlsext_ocsp_resplen;
3549ebfedea0SLionel Sambuc s->state = SSL3_ST_SW_CERT_STATUS_B;
3550ebfedea0SLionel Sambuc s->init_off = 0;
3551ebfedea0SLionel Sambuc }
3552ebfedea0SLionel Sambuc
3553ebfedea0SLionel Sambuc /* SSL3_ST_SW_CERT_STATUS_B */
3554ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3555ebfedea0SLionel Sambuc }
3556ebfedea0SLionel Sambuc
3557ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
3558*0a6a1f1dSLionel Sambuc /*
3559*0a6a1f1dSLionel Sambuc * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3560*0a6a1f1dSLionel Sambuc * It sets the next_proto member in s if found
3561*0a6a1f1dSLionel Sambuc */
ssl3_get_next_proto(SSL * s)3562ebfedea0SLionel Sambuc int ssl3_get_next_proto(SSL *s)
3563ebfedea0SLionel Sambuc {
3564ebfedea0SLionel Sambuc int ok;
3565ebfedea0SLionel Sambuc int proto_len, padding_len;
3566ebfedea0SLionel Sambuc long n;
3567ebfedea0SLionel Sambuc const unsigned char *p;
3568ebfedea0SLionel Sambuc
3569*0a6a1f1dSLionel Sambuc /*
3570*0a6a1f1dSLionel Sambuc * Clients cannot send a NextProtocol message if we didn't see the
3571*0a6a1f1dSLionel Sambuc * extension in their ClientHello
3572*0a6a1f1dSLionel Sambuc */
3573*0a6a1f1dSLionel Sambuc if (!s->s3->next_proto_neg_seen) {
3574*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3575*0a6a1f1dSLionel Sambuc SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3576*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3577ebfedea0SLionel Sambuc return -1;
3578ebfedea0SLionel Sambuc }
3579ebfedea0SLionel Sambuc
3580*0a6a1f1dSLionel Sambuc /* See the payload format below */
3581ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
3582ebfedea0SLionel Sambuc SSL3_ST_SR_NEXT_PROTO_A,
3583ebfedea0SLionel Sambuc SSL3_ST_SR_NEXT_PROTO_B,
3584*0a6a1f1dSLionel Sambuc SSL3_MT_NEXT_PROTO, 514, &ok);
3585ebfedea0SLionel Sambuc
3586ebfedea0SLionel Sambuc if (!ok)
3587ebfedea0SLionel Sambuc return ((int)n);
3588ebfedea0SLionel Sambuc
3589*0a6a1f1dSLionel Sambuc /*
3590*0a6a1f1dSLionel Sambuc * s->state doesn't reflect whether ChangeCipherSpec has been received in
3591*0a6a1f1dSLionel Sambuc * this handshake, but s->s3->change_cipher_spec does (will be reset by
3592*0a6a1f1dSLionel Sambuc * ssl3_get_finished).
3593*0a6a1f1dSLionel Sambuc */
3594*0a6a1f1dSLionel Sambuc if (!s->s3->change_cipher_spec) {
3595ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3596*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3597ebfedea0SLionel Sambuc return -1;
3598ebfedea0SLionel Sambuc }
3599ebfedea0SLionel Sambuc
3600*0a6a1f1dSLionel Sambuc if (n < 2) {
3601*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3602ebfedea0SLionel Sambuc return 0; /* The body must be > 1 bytes long */
3603*0a6a1f1dSLionel Sambuc }
3604ebfedea0SLionel Sambuc
3605ebfedea0SLionel Sambuc p = (unsigned char *)s->init_msg;
3606ebfedea0SLionel Sambuc
3607*0a6a1f1dSLionel Sambuc /*-
3608*0a6a1f1dSLionel Sambuc * The payload looks like:
3609ebfedea0SLionel Sambuc * uint8 proto_len;
3610ebfedea0SLionel Sambuc * uint8 proto[proto_len];
3611ebfedea0SLionel Sambuc * uint8 padding_len;
3612ebfedea0SLionel Sambuc * uint8 padding[padding_len];
3613ebfedea0SLionel Sambuc */
3614ebfedea0SLionel Sambuc proto_len = p[0];
3615*0a6a1f1dSLionel Sambuc if (proto_len + 2 > s->init_num) {
3616*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3617ebfedea0SLionel Sambuc return 0;
3618*0a6a1f1dSLionel Sambuc }
3619ebfedea0SLionel Sambuc padding_len = p[proto_len + 1];
3620*0a6a1f1dSLionel Sambuc if (proto_len + padding_len + 2 != s->init_num) {
3621*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3622ebfedea0SLionel Sambuc return 0;
3623*0a6a1f1dSLionel Sambuc }
3624ebfedea0SLionel Sambuc
3625ebfedea0SLionel Sambuc s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3626*0a6a1f1dSLionel Sambuc if (!s->next_proto_negotiated) {
3627ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3628*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3629ebfedea0SLionel Sambuc return 0;
3630ebfedea0SLionel Sambuc }
3631ebfedea0SLionel Sambuc memcpy(s->next_proto_negotiated, p + 1, proto_len);
3632ebfedea0SLionel Sambuc s->next_proto_negotiated_len = proto_len;
3633ebfedea0SLionel Sambuc
3634ebfedea0SLionel Sambuc return 1;
3635ebfedea0SLionel Sambuc }
3636ebfedea0SLionel Sambuc # endif
3637ebfedea0SLionel Sambuc #endif
3638