1ebfedea0SLionel Sambuc /* ssl/s3_clnt.c */
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 #include <stdio.h>
152ebfedea0SLionel Sambuc #include "ssl_locl.h"
153ebfedea0SLionel Sambuc #include "kssl_lcl.h"
154ebfedea0SLionel Sambuc #include <openssl/buffer.h>
155ebfedea0SLionel Sambuc #include <openssl/rand.h>
156ebfedea0SLionel Sambuc #include <openssl/objects.h>
157ebfedea0SLionel Sambuc #include <openssl/evp.h>
158ebfedea0SLionel Sambuc #include <openssl/md5.h>
159ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
160ebfedea0SLionel Sambuc # include <openssl/fips.h>
161ebfedea0SLionel Sambuc #endif
162ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
163ebfedea0SLionel Sambuc # include <openssl/dh.h>
164ebfedea0SLionel Sambuc #endif
165ebfedea0SLionel Sambuc #include <openssl/bn.h>
166ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
167ebfedea0SLionel Sambuc # include <openssl/engine.h>
168ebfedea0SLionel Sambuc #endif
169ebfedea0SLionel Sambuc
170ebfedea0SLionel Sambuc static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b);
171*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
172*0a6a1f1dSLionel Sambuc static int ssl3_check_finished(SSL *s);
173*0a6a1f1dSLionel Sambuc #endif
174ebfedea0SLionel Sambuc
175*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SSL3_METHOD
ssl3_get_client_method(int ver)176ebfedea0SLionel Sambuc static const SSL_METHOD *ssl3_get_client_method(int ver)
177ebfedea0SLionel Sambuc {
178ebfedea0SLionel Sambuc if (ver == SSL3_VERSION)
179ebfedea0SLionel Sambuc return (SSLv3_client_method());
180ebfedea0SLionel Sambuc else
181ebfedea0SLionel Sambuc return (NULL);
182ebfedea0SLionel Sambuc }
183ebfedea0SLionel Sambuc
IMPLEMENT_ssl3_meth_func(SSLv3_client_method,ssl_undefined_function,ssl3_connect,ssl3_get_client_method)184ebfedea0SLionel Sambuc IMPLEMENT_ssl3_meth_func(SSLv3_client_method,
185ebfedea0SLionel Sambuc ssl_undefined_function,
186*0a6a1f1dSLionel Sambuc ssl3_connect, ssl3_get_client_method)
187*0a6a1f1dSLionel Sambuc #endif
188ebfedea0SLionel Sambuc int ssl3_connect(SSL *s)
189ebfedea0SLionel Sambuc {
190ebfedea0SLionel Sambuc BUF_MEM *buf = NULL;
191ebfedea0SLionel Sambuc unsigned long Time = (unsigned long)time(NULL);
192ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val) = NULL;
193ebfedea0SLionel Sambuc int ret = -1;
194ebfedea0SLionel Sambuc int new_state, state, skip = 0;
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc RAND_add(&Time, sizeof(Time), 0);
197ebfedea0SLionel Sambuc ERR_clear_error();
198ebfedea0SLionel Sambuc clear_sys_error();
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc if (s->info_callback != NULL)
201ebfedea0SLionel Sambuc cb = s->info_callback;
202ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
203ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc s->in_handshake++;
206*0a6a1f1dSLionel Sambuc if (!SSL_in_init(s) || SSL_in_before(s))
207*0a6a1f1dSLionel Sambuc SSL_clear(s);
208ebfedea0SLionel Sambuc
209ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_HEARTBEATS
210*0a6a1f1dSLionel Sambuc /*
211*0a6a1f1dSLionel Sambuc * If we're awaiting a HeartbeatResponse, pretend we already got and
212*0a6a1f1dSLionel Sambuc * don't await it anymore, because Heartbeats don't make sense during
213*0a6a1f1dSLionel Sambuc * handshakes anyway.
214ebfedea0SLionel Sambuc */
215*0a6a1f1dSLionel Sambuc if (s->tlsext_hb_pending) {
216ebfedea0SLionel Sambuc s->tlsext_hb_pending = 0;
217ebfedea0SLionel Sambuc s->tlsext_hb_seq++;
218ebfedea0SLionel Sambuc }
219ebfedea0SLionel Sambuc #endif
220ebfedea0SLionel Sambuc
221*0a6a1f1dSLionel Sambuc for (;;) {
222ebfedea0SLionel Sambuc state = s->state;
223ebfedea0SLionel Sambuc
224*0a6a1f1dSLionel Sambuc switch (s->state) {
225ebfedea0SLionel Sambuc case SSL_ST_RENEGOTIATE:
226ebfedea0SLionel Sambuc s->renegotiate = 1;
227ebfedea0SLionel Sambuc s->state = SSL_ST_CONNECT;
228ebfedea0SLionel Sambuc s->ctx->stats.sess_connect_renegotiate++;
229ebfedea0SLionel Sambuc /* break */
230ebfedea0SLionel Sambuc case SSL_ST_BEFORE:
231ebfedea0SLionel Sambuc case SSL_ST_CONNECT:
232ebfedea0SLionel Sambuc case SSL_ST_BEFORE | SSL_ST_CONNECT:
233ebfedea0SLionel Sambuc case SSL_ST_OK | SSL_ST_CONNECT:
234ebfedea0SLionel Sambuc
235ebfedea0SLionel Sambuc s->server = 0;
236*0a6a1f1dSLionel Sambuc if (cb != NULL)
237*0a6a1f1dSLionel Sambuc cb(s, SSL_CB_HANDSHAKE_START, 1);
238ebfedea0SLionel Sambuc
239*0a6a1f1dSLionel Sambuc if ((s->version & 0xff00) != 0x0300) {
240ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CONNECT, ERR_R_INTERNAL_ERROR);
241*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
242ebfedea0SLionel Sambuc ret = -1;
243ebfedea0SLionel Sambuc goto end;
244ebfedea0SLionel Sambuc }
245ebfedea0SLionel Sambuc
246ebfedea0SLionel Sambuc /* s->version=SSL3_VERSION; */
247ebfedea0SLionel Sambuc s->type = SSL_ST_CONNECT;
248ebfedea0SLionel Sambuc
249*0a6a1f1dSLionel Sambuc if (s->init_buf == NULL) {
250*0a6a1f1dSLionel Sambuc if ((buf = BUF_MEM_new()) == NULL) {
251ebfedea0SLionel Sambuc ret = -1;
252*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
253ebfedea0SLionel Sambuc goto end;
254ebfedea0SLionel Sambuc }
255*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
256ebfedea0SLionel Sambuc ret = -1;
257*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
258ebfedea0SLionel Sambuc goto end;
259ebfedea0SLionel Sambuc }
260ebfedea0SLionel Sambuc s->init_buf = buf;
261ebfedea0SLionel Sambuc buf = NULL;
262ebfedea0SLionel Sambuc }
263ebfedea0SLionel Sambuc
264*0a6a1f1dSLionel Sambuc if (!ssl3_setup_buffers(s)) {
265*0a6a1f1dSLionel Sambuc ret = -1;
266*0a6a1f1dSLionel Sambuc goto end;
267*0a6a1f1dSLionel Sambuc }
268ebfedea0SLionel Sambuc
269ebfedea0SLionel Sambuc /* setup buffing BIO */
270*0a6a1f1dSLionel Sambuc if (!ssl_init_wbio_buffer(s, 0)) {
271*0a6a1f1dSLionel Sambuc ret = -1;
272*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
273*0a6a1f1dSLionel Sambuc goto end;
274*0a6a1f1dSLionel Sambuc }
275ebfedea0SLionel Sambuc
276ebfedea0SLionel Sambuc /* don't push the buffering BIO quite yet */
277ebfedea0SLionel Sambuc
278ebfedea0SLionel Sambuc ssl3_init_finished_mac(s);
279ebfedea0SLionel Sambuc
280ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CLNT_HELLO_A;
281ebfedea0SLionel Sambuc s->ctx->stats.sess_connect++;
282ebfedea0SLionel Sambuc s->init_num = 0;
283*0a6a1f1dSLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
284*0a6a1f1dSLionel Sambuc /*
285*0a6a1f1dSLionel Sambuc * Should have been reset by ssl3_get_finished, too.
286*0a6a1f1dSLionel Sambuc */
287*0a6a1f1dSLionel Sambuc s->s3->change_cipher_spec = 0;
288ebfedea0SLionel Sambuc break;
289ebfedea0SLionel Sambuc
290ebfedea0SLionel Sambuc case SSL3_ST_CW_CLNT_HELLO_A:
291ebfedea0SLionel Sambuc case SSL3_ST_CW_CLNT_HELLO_B:
292ebfedea0SLionel Sambuc
293ebfedea0SLionel Sambuc s->shutdown = 0;
294ebfedea0SLionel Sambuc ret = ssl3_client_hello(s);
295*0a6a1f1dSLionel Sambuc if (ret <= 0)
296*0a6a1f1dSLionel Sambuc goto end;
297ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_SRVR_HELLO_A;
298ebfedea0SLionel Sambuc s->init_num = 0;
299ebfedea0SLionel Sambuc
300ebfedea0SLionel Sambuc /* turn on buffering for the next lot of output */
301ebfedea0SLionel Sambuc if (s->bbio != s->wbio)
302ebfedea0SLionel Sambuc s->wbio = BIO_push(s->bbio, s->wbio);
303ebfedea0SLionel Sambuc
304ebfedea0SLionel Sambuc break;
305ebfedea0SLionel Sambuc
306ebfedea0SLionel Sambuc case SSL3_ST_CR_SRVR_HELLO_A:
307ebfedea0SLionel Sambuc case SSL3_ST_CR_SRVR_HELLO_B:
308ebfedea0SLionel Sambuc ret = ssl3_get_server_hello(s);
309*0a6a1f1dSLionel Sambuc if (ret <= 0)
310*0a6a1f1dSLionel Sambuc goto end;
311ebfedea0SLionel Sambuc
312*0a6a1f1dSLionel Sambuc if (s->hit) {
313ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_FINISHED_A;
314ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
315*0a6a1f1dSLionel Sambuc if (s->tlsext_ticket_expected) {
316ebfedea0SLionel Sambuc /* receive renewed session ticket */
317ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_SESSION_TICKET_A;
318ebfedea0SLionel Sambuc }
319ebfedea0SLionel Sambuc #endif
320*0a6a1f1dSLionel Sambuc } else
321ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_CERT_A;
322ebfedea0SLionel Sambuc s->init_num = 0;
323ebfedea0SLionel Sambuc break;
324ebfedea0SLionel Sambuc
325ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_A:
326ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_B:
327ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
328*0a6a1f1dSLionel Sambuc /* Noop (ret = 0) for everything but EAP-FAST. */
329ebfedea0SLionel Sambuc ret = ssl3_check_finished(s);
330*0a6a1f1dSLionel Sambuc if (ret < 0)
331*0a6a1f1dSLionel Sambuc goto end;
332*0a6a1f1dSLionel Sambuc if (ret == 1) {
333ebfedea0SLionel Sambuc s->hit = 1;
334ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_FINISHED_A;
335ebfedea0SLionel Sambuc s->init_num = 0;
336ebfedea0SLionel Sambuc break;
337ebfedea0SLionel Sambuc }
338ebfedea0SLionel Sambuc #endif
339*0a6a1f1dSLionel Sambuc /* Check if it is anon DH/ECDH, SRP auth */
340ebfedea0SLionel Sambuc /* or PSK */
341*0a6a1f1dSLionel Sambuc if (!
342*0a6a1f1dSLionel Sambuc (s->s3->tmp.
343*0a6a1f1dSLionel Sambuc new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
344*0a6a1f1dSLionel Sambuc && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
345ebfedea0SLionel Sambuc ret = ssl3_get_server_certificate(s);
346*0a6a1f1dSLionel Sambuc if (ret <= 0)
347*0a6a1f1dSLionel Sambuc goto end;
348ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
349ebfedea0SLionel Sambuc if (s->tlsext_status_expected)
350ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_CERT_STATUS_A;
351ebfedea0SLionel Sambuc else
352ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_KEY_EXCH_A;
353*0a6a1f1dSLionel Sambuc } else {
354ebfedea0SLionel Sambuc skip = 1;
355ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_KEY_EXCH_A;
356ebfedea0SLionel Sambuc }
357ebfedea0SLionel Sambuc #else
358*0a6a1f1dSLionel Sambuc } else
359ebfedea0SLionel Sambuc skip = 1;
360ebfedea0SLionel Sambuc
361ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_KEY_EXCH_A;
362ebfedea0SLionel Sambuc #endif
363ebfedea0SLionel Sambuc s->init_num = 0;
364ebfedea0SLionel Sambuc break;
365ebfedea0SLionel Sambuc
366ebfedea0SLionel Sambuc case SSL3_ST_CR_KEY_EXCH_A:
367ebfedea0SLionel Sambuc case SSL3_ST_CR_KEY_EXCH_B:
368ebfedea0SLionel Sambuc ret = ssl3_get_key_exchange(s);
369*0a6a1f1dSLionel Sambuc if (ret <= 0)
370*0a6a1f1dSLionel Sambuc goto end;
371ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_CERT_REQ_A;
372ebfedea0SLionel Sambuc s->init_num = 0;
373ebfedea0SLionel Sambuc
374*0a6a1f1dSLionel Sambuc /*
375*0a6a1f1dSLionel Sambuc * at this point we check that we have the required stuff from
376*0a6a1f1dSLionel Sambuc * the server
377*0a6a1f1dSLionel Sambuc */
378*0a6a1f1dSLionel Sambuc if (!ssl3_check_cert_and_algorithm(s)) {
379ebfedea0SLionel Sambuc ret = -1;
380*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
381ebfedea0SLionel Sambuc goto end;
382ebfedea0SLionel Sambuc }
383ebfedea0SLionel Sambuc break;
384ebfedea0SLionel Sambuc
385ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_REQ_A:
386ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_REQ_B:
387ebfedea0SLionel Sambuc ret = ssl3_get_certificate_request(s);
388*0a6a1f1dSLionel Sambuc if (ret <= 0)
389*0a6a1f1dSLionel Sambuc goto end;
390ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_SRVR_DONE_A;
391ebfedea0SLionel Sambuc s->init_num = 0;
392ebfedea0SLionel Sambuc break;
393ebfedea0SLionel Sambuc
394ebfedea0SLionel Sambuc case SSL3_ST_CR_SRVR_DONE_A:
395ebfedea0SLionel Sambuc case SSL3_ST_CR_SRVR_DONE_B:
396ebfedea0SLionel Sambuc ret = ssl3_get_server_done(s);
397*0a6a1f1dSLionel Sambuc if (ret <= 0)
398*0a6a1f1dSLionel Sambuc goto end;
399ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
400*0a6a1f1dSLionel Sambuc if (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kSRP) {
401*0a6a1f1dSLionel Sambuc if ((ret = SRP_Calc_A_param(s)) <= 0) {
402ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CONNECT, SSL_R_SRP_A_CALC);
403ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
404*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
405ebfedea0SLionel Sambuc goto end;
406ebfedea0SLionel Sambuc }
407ebfedea0SLionel Sambuc }
408ebfedea0SLionel Sambuc #endif
409ebfedea0SLionel Sambuc if (s->s3->tmp.cert_req)
410ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_A;
411ebfedea0SLionel Sambuc else
412ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_KEY_EXCH_A;
413ebfedea0SLionel Sambuc s->init_num = 0;
414ebfedea0SLionel Sambuc
415ebfedea0SLionel Sambuc break;
416ebfedea0SLionel Sambuc
417ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_A:
418ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_B:
419ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_C:
420ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_D:
421ebfedea0SLionel Sambuc ret = ssl3_send_client_certificate(s);
422*0a6a1f1dSLionel Sambuc if (ret <= 0)
423*0a6a1f1dSLionel Sambuc goto end;
424ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_KEY_EXCH_A;
425ebfedea0SLionel Sambuc s->init_num = 0;
426ebfedea0SLionel Sambuc break;
427ebfedea0SLionel Sambuc
428ebfedea0SLionel Sambuc case SSL3_ST_CW_KEY_EXCH_A:
429ebfedea0SLionel Sambuc case SSL3_ST_CW_KEY_EXCH_B:
430ebfedea0SLionel Sambuc ret = ssl3_send_client_key_exchange(s);
431*0a6a1f1dSLionel Sambuc if (ret <= 0)
432*0a6a1f1dSLionel Sambuc goto end;
433*0a6a1f1dSLionel Sambuc /*
434*0a6a1f1dSLionel Sambuc * EAY EAY EAY need to check for DH fix cert sent back
435ebfedea0SLionel Sambuc */
436*0a6a1f1dSLionel Sambuc /*
437*0a6a1f1dSLionel Sambuc * For TLS, cert_req is set to 2, so a cert chain of nothing is
438*0a6a1f1dSLionel Sambuc * sent, but no verify packet is sent
439*0a6a1f1dSLionel Sambuc */
440*0a6a1f1dSLionel Sambuc /*
441*0a6a1f1dSLionel Sambuc * XXX: For now, we do not support client authentication in ECDH
442*0a6a1f1dSLionel Sambuc * cipher suites with ECDH (rather than ECDSA) certificates. We
443*0a6a1f1dSLionel Sambuc * need to skip the certificate verify message when client's
444*0a6a1f1dSLionel Sambuc * ECDH public key is sent inside the client certificate.
445*0a6a1f1dSLionel Sambuc */
446*0a6a1f1dSLionel Sambuc if (s->s3->tmp.cert_req == 1) {
447ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_VRFY_A;
448*0a6a1f1dSLionel Sambuc } else {
449ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CHANGE_A;
450ebfedea0SLionel Sambuc }
451*0a6a1f1dSLionel Sambuc if (s->s3->flags & TLS1_FLAGS_SKIP_CERT_VERIFY) {
452ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CHANGE_A;
453ebfedea0SLionel Sambuc }
454ebfedea0SLionel Sambuc
455ebfedea0SLionel Sambuc s->init_num = 0;
456ebfedea0SLionel Sambuc break;
457ebfedea0SLionel Sambuc
458ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_VRFY_A:
459ebfedea0SLionel Sambuc case SSL3_ST_CW_CERT_VRFY_B:
460ebfedea0SLionel Sambuc ret = ssl3_send_client_verify(s);
461*0a6a1f1dSLionel Sambuc if (ret <= 0)
462*0a6a1f1dSLionel Sambuc goto end;
463ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CHANGE_A;
464ebfedea0SLionel Sambuc s->init_num = 0;
465ebfedea0SLionel Sambuc break;
466ebfedea0SLionel Sambuc
467ebfedea0SLionel Sambuc case SSL3_ST_CW_CHANGE_A:
468ebfedea0SLionel Sambuc case SSL3_ST_CW_CHANGE_B:
469ebfedea0SLionel Sambuc ret = ssl3_send_change_cipher_spec(s,
470*0a6a1f1dSLionel Sambuc SSL3_ST_CW_CHANGE_A,
471*0a6a1f1dSLionel Sambuc SSL3_ST_CW_CHANGE_B);
472*0a6a1f1dSLionel Sambuc if (ret <= 0)
473*0a6a1f1dSLionel Sambuc goto end;
474ebfedea0SLionel Sambuc
475ebfedea0SLionel Sambuc #if defined(OPENSSL_NO_TLSEXT) || defined(OPENSSL_NO_NEXTPROTONEG)
476ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_FINISHED_A;
477ebfedea0SLionel Sambuc #else
478ebfedea0SLionel Sambuc if (s->s3->next_proto_neg_seen)
479ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_NEXT_PROTO_A;
480ebfedea0SLionel Sambuc else
481ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_FINISHED_A;
482ebfedea0SLionel Sambuc #endif
483ebfedea0SLionel Sambuc s->init_num = 0;
484ebfedea0SLionel Sambuc
485ebfedea0SLionel Sambuc s->session->cipher = s->s3->tmp.new_cipher;
486ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
487ebfedea0SLionel Sambuc s->session->compress_meth = 0;
488ebfedea0SLionel Sambuc #else
489ebfedea0SLionel Sambuc if (s->s3->tmp.new_compression == NULL)
490ebfedea0SLionel Sambuc s->session->compress_meth = 0;
491ebfedea0SLionel Sambuc else
492*0a6a1f1dSLionel Sambuc s->session->compress_meth = s->s3->tmp.new_compression->id;
493ebfedea0SLionel Sambuc #endif
494*0a6a1f1dSLionel Sambuc if (!s->method->ssl3_enc->setup_key_block(s)) {
495ebfedea0SLionel Sambuc ret = -1;
496*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
497ebfedea0SLionel Sambuc goto end;
498ebfedea0SLionel Sambuc }
499ebfedea0SLionel Sambuc
500ebfedea0SLionel Sambuc if (!s->method->ssl3_enc->change_cipher_state(s,
501ebfedea0SLionel Sambuc SSL3_CHANGE_CIPHER_CLIENT_WRITE))
502ebfedea0SLionel Sambuc {
503ebfedea0SLionel Sambuc ret = -1;
504*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
505ebfedea0SLionel Sambuc goto end;
506ebfedea0SLionel Sambuc }
507ebfedea0SLionel Sambuc
508ebfedea0SLionel Sambuc break;
509ebfedea0SLionel Sambuc
510ebfedea0SLionel Sambuc #if !defined(OPENSSL_NO_TLSEXT) && !defined(OPENSSL_NO_NEXTPROTONEG)
511ebfedea0SLionel Sambuc case SSL3_ST_CW_NEXT_PROTO_A:
512ebfedea0SLionel Sambuc case SSL3_ST_CW_NEXT_PROTO_B:
513ebfedea0SLionel Sambuc ret = ssl3_send_next_proto(s);
514*0a6a1f1dSLionel Sambuc if (ret <= 0)
515*0a6a1f1dSLionel Sambuc goto end;
516ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_FINISHED_A;
517ebfedea0SLionel Sambuc break;
518ebfedea0SLionel Sambuc #endif
519ebfedea0SLionel Sambuc
520ebfedea0SLionel Sambuc case SSL3_ST_CW_FINISHED_A:
521ebfedea0SLionel Sambuc case SSL3_ST_CW_FINISHED_B:
522ebfedea0SLionel Sambuc ret = ssl3_send_finished(s,
523*0a6a1f1dSLionel Sambuc SSL3_ST_CW_FINISHED_A,
524*0a6a1f1dSLionel Sambuc SSL3_ST_CW_FINISHED_B,
525*0a6a1f1dSLionel Sambuc s->method->
526*0a6a1f1dSLionel Sambuc ssl3_enc->client_finished_label,
527*0a6a1f1dSLionel Sambuc s->method->
528*0a6a1f1dSLionel Sambuc ssl3_enc->client_finished_label_len);
529*0a6a1f1dSLionel Sambuc if (ret <= 0)
530*0a6a1f1dSLionel Sambuc goto end;
531ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_FLUSH;
532ebfedea0SLionel Sambuc
533ebfedea0SLionel Sambuc /* clear flags */
534ebfedea0SLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_POP_BUFFER;
535*0a6a1f1dSLionel Sambuc if (s->hit) {
536ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL_ST_OK;
537*0a6a1f1dSLionel Sambuc if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED) {
538ebfedea0SLionel Sambuc s->state = SSL_ST_OK;
539ebfedea0SLionel Sambuc s->s3->flags |= SSL3_FLAGS_POP_BUFFER;
540ebfedea0SLionel Sambuc s->s3->delay_buf_pop_ret = 0;
541ebfedea0SLionel Sambuc }
542*0a6a1f1dSLionel Sambuc } else {
543ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
544*0a6a1f1dSLionel Sambuc /*
545*0a6a1f1dSLionel Sambuc * Allow NewSessionTicket if ticket expected
546*0a6a1f1dSLionel Sambuc */
547ebfedea0SLionel Sambuc if (s->tlsext_ticket_expected)
548ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_CR_SESSION_TICKET_A;
549ebfedea0SLionel Sambuc else
550ebfedea0SLionel Sambuc #endif
551ebfedea0SLionel Sambuc
552ebfedea0SLionel Sambuc s->s3->tmp.next_state = SSL3_ST_CR_FINISHED_A;
553ebfedea0SLionel Sambuc }
554ebfedea0SLionel Sambuc s->init_num = 0;
555ebfedea0SLionel Sambuc break;
556ebfedea0SLionel Sambuc
557ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
558ebfedea0SLionel Sambuc case SSL3_ST_CR_SESSION_TICKET_A:
559ebfedea0SLionel Sambuc case SSL3_ST_CR_SESSION_TICKET_B:
560ebfedea0SLionel Sambuc ret = ssl3_get_new_session_ticket(s);
561*0a6a1f1dSLionel Sambuc if (ret <= 0)
562*0a6a1f1dSLionel Sambuc goto end;
563ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_FINISHED_A;
564ebfedea0SLionel Sambuc s->init_num = 0;
565ebfedea0SLionel Sambuc break;
566ebfedea0SLionel Sambuc
567ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_STATUS_A:
568ebfedea0SLionel Sambuc case SSL3_ST_CR_CERT_STATUS_B:
569ebfedea0SLionel Sambuc ret = ssl3_get_cert_status(s);
570*0a6a1f1dSLionel Sambuc if (ret <= 0)
571*0a6a1f1dSLionel Sambuc goto end;
572ebfedea0SLionel Sambuc s->state = SSL3_ST_CR_KEY_EXCH_A;
573ebfedea0SLionel Sambuc s->init_num = 0;
574ebfedea0SLionel Sambuc break;
575ebfedea0SLionel Sambuc #endif
576ebfedea0SLionel Sambuc
577ebfedea0SLionel Sambuc case SSL3_ST_CR_FINISHED_A:
578ebfedea0SLionel Sambuc case SSL3_ST_CR_FINISHED_B:
579*0a6a1f1dSLionel Sambuc if (!s->s3->change_cipher_spec)
580*0a6a1f1dSLionel Sambuc s->s3->flags |= SSL3_FLAGS_CCS_OK;
581ebfedea0SLionel Sambuc ret = ssl3_get_finished(s, SSL3_ST_CR_FINISHED_A,
582ebfedea0SLionel Sambuc SSL3_ST_CR_FINISHED_B);
583*0a6a1f1dSLionel Sambuc if (ret <= 0)
584*0a6a1f1dSLionel Sambuc goto end;
585ebfedea0SLionel Sambuc
586ebfedea0SLionel Sambuc if (s->hit)
587ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CHANGE_A;
588ebfedea0SLionel Sambuc else
589ebfedea0SLionel Sambuc s->state = SSL_ST_OK;
590ebfedea0SLionel Sambuc s->init_num = 0;
591ebfedea0SLionel Sambuc break;
592ebfedea0SLionel Sambuc
593ebfedea0SLionel Sambuc case SSL3_ST_CW_FLUSH:
594ebfedea0SLionel Sambuc s->rwstate = SSL_WRITING;
595*0a6a1f1dSLionel Sambuc if (BIO_flush(s->wbio) <= 0) {
596ebfedea0SLionel Sambuc ret = -1;
597ebfedea0SLionel Sambuc goto end;
598ebfedea0SLionel Sambuc }
599ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
600ebfedea0SLionel Sambuc s->state = s->s3->tmp.next_state;
601ebfedea0SLionel Sambuc break;
602ebfedea0SLionel Sambuc
603ebfedea0SLionel Sambuc case SSL_ST_OK:
604ebfedea0SLionel Sambuc /* clean a few things up */
605ebfedea0SLionel Sambuc ssl3_cleanup_key_block(s);
606ebfedea0SLionel Sambuc
607*0a6a1f1dSLionel Sambuc if (s->init_buf != NULL) {
608ebfedea0SLionel Sambuc BUF_MEM_free(s->init_buf);
609ebfedea0SLionel Sambuc s->init_buf = NULL;
610ebfedea0SLionel Sambuc }
611ebfedea0SLionel Sambuc
612*0a6a1f1dSLionel Sambuc /*
613*0a6a1f1dSLionel Sambuc * If we are not 'joining' the last two packets, remove the
614*0a6a1f1dSLionel Sambuc * buffering now
615*0a6a1f1dSLionel Sambuc */
616ebfedea0SLionel Sambuc if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
617ebfedea0SLionel Sambuc ssl_free_wbio_buffer(s);
618ebfedea0SLionel Sambuc /* else do it later in ssl3_write */
619ebfedea0SLionel Sambuc
620ebfedea0SLionel Sambuc s->init_num = 0;
621ebfedea0SLionel Sambuc s->renegotiate = 0;
622ebfedea0SLionel Sambuc s->new_session = 0;
623ebfedea0SLionel Sambuc
624ebfedea0SLionel Sambuc ssl_update_cache(s, SSL_SESS_CACHE_CLIENT);
625*0a6a1f1dSLionel Sambuc if (s->hit)
626*0a6a1f1dSLionel Sambuc s->ctx->stats.sess_hit++;
627ebfedea0SLionel Sambuc
628ebfedea0SLionel Sambuc ret = 1;
629ebfedea0SLionel Sambuc /* s->server=0; */
630ebfedea0SLionel Sambuc s->handshake_func = ssl3_connect;
631ebfedea0SLionel Sambuc s->ctx->stats.sess_connect_good++;
632ebfedea0SLionel Sambuc
633*0a6a1f1dSLionel Sambuc if (cb != NULL)
634*0a6a1f1dSLionel Sambuc cb(s, SSL_CB_HANDSHAKE_DONE, 1);
635ebfedea0SLionel Sambuc
636ebfedea0SLionel Sambuc goto end;
637ebfedea0SLionel Sambuc /* break; */
638ebfedea0SLionel Sambuc
639*0a6a1f1dSLionel Sambuc case SSL_ST_ERR:
640ebfedea0SLionel Sambuc default:
641ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CONNECT, SSL_R_UNKNOWN_STATE);
642ebfedea0SLionel Sambuc ret = -1;
643ebfedea0SLionel Sambuc goto end;
644ebfedea0SLionel Sambuc /* break; */
645ebfedea0SLionel Sambuc }
646ebfedea0SLionel Sambuc
647ebfedea0SLionel Sambuc /* did we do anything */
648*0a6a1f1dSLionel Sambuc if (!s->s3->tmp.reuse_message && !skip) {
649*0a6a1f1dSLionel Sambuc if (s->debug) {
650ebfedea0SLionel Sambuc if ((ret = BIO_flush(s->wbio)) <= 0)
651ebfedea0SLionel Sambuc goto end;
652ebfedea0SLionel Sambuc }
653ebfedea0SLionel Sambuc
654*0a6a1f1dSLionel Sambuc if ((cb != NULL) && (s->state != state)) {
655ebfedea0SLionel Sambuc new_state = s->state;
656ebfedea0SLionel Sambuc s->state = state;
657ebfedea0SLionel Sambuc cb(s, SSL_CB_CONNECT_LOOP, 1);
658ebfedea0SLionel Sambuc s->state = new_state;
659ebfedea0SLionel Sambuc }
660ebfedea0SLionel Sambuc }
661ebfedea0SLionel Sambuc skip = 0;
662ebfedea0SLionel Sambuc }
663ebfedea0SLionel Sambuc end:
664ebfedea0SLionel Sambuc s->in_handshake--;
665ebfedea0SLionel Sambuc if (buf != NULL)
666ebfedea0SLionel Sambuc BUF_MEM_free(buf);
667ebfedea0SLionel Sambuc if (cb != NULL)
668ebfedea0SLionel Sambuc cb(s, SSL_CB_CONNECT_EXIT, ret);
669ebfedea0SLionel Sambuc return (ret);
670ebfedea0SLionel Sambuc }
671ebfedea0SLionel Sambuc
ssl3_client_hello(SSL * s)672ebfedea0SLionel Sambuc int ssl3_client_hello(SSL *s)
673ebfedea0SLionel Sambuc {
674ebfedea0SLionel Sambuc unsigned char *buf;
675ebfedea0SLionel Sambuc unsigned char *p, *d;
676ebfedea0SLionel Sambuc int i;
677*0a6a1f1dSLionel Sambuc unsigned long l;
678ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
679ebfedea0SLionel Sambuc int j;
680ebfedea0SLionel Sambuc SSL_COMP *comp;
681ebfedea0SLionel Sambuc #endif
682ebfedea0SLionel Sambuc
683ebfedea0SLionel Sambuc buf = (unsigned char *)s->init_buf->data;
684*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_CLNT_HELLO_A) {
685ebfedea0SLionel Sambuc SSL_SESSION *sess = s->session;
686*0a6a1f1dSLionel Sambuc if ((sess == NULL) || (sess->ssl_version != s->version) ||
687ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_TLSEXT
688ebfedea0SLionel Sambuc !sess->session_id_length ||
689ebfedea0SLionel Sambuc #else
690*0a6a1f1dSLionel Sambuc /*
691*0a6a1f1dSLionel Sambuc * In the case of EAP-FAST, we can have a pre-shared
692*0a6a1f1dSLionel Sambuc * "ticket" without a session ID.
693*0a6a1f1dSLionel Sambuc */
694ebfedea0SLionel Sambuc (!sess->session_id_length && !sess->tlsext_tick) ||
695ebfedea0SLionel Sambuc #endif
696*0a6a1f1dSLionel Sambuc (sess->not_resumable)) {
697ebfedea0SLionel Sambuc if (!ssl_get_new_session(s, 0))
698ebfedea0SLionel Sambuc goto err;
699ebfedea0SLionel Sambuc }
700ebfedea0SLionel Sambuc /* else use the pre-loaded session */
701ebfedea0SLionel Sambuc
702ebfedea0SLionel Sambuc p = s->s3->client_random;
703*0a6a1f1dSLionel Sambuc
704*0a6a1f1dSLionel Sambuc if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
705ebfedea0SLionel Sambuc goto err;
706ebfedea0SLionel Sambuc
707ebfedea0SLionel Sambuc /* Do the message type and length last */
708ebfedea0SLionel Sambuc d = p = &(buf[4]);
709ebfedea0SLionel Sambuc
710*0a6a1f1dSLionel Sambuc /*-
711*0a6a1f1dSLionel Sambuc * version indicates the negotiated version: for example from
712ebfedea0SLionel Sambuc * an SSLv2/v3 compatible client hello). The client_version
713ebfedea0SLionel Sambuc * field is the maximum version we permit and it is also
714ebfedea0SLionel Sambuc * used in RSA encrypted premaster secrets. Some servers can
715ebfedea0SLionel Sambuc * choke if we initially report a higher version then
716ebfedea0SLionel Sambuc * renegotiate to a lower one in the premaster secret. This
717ebfedea0SLionel Sambuc * didn't happen with TLS 1.0 as most servers supported it
718ebfedea0SLionel Sambuc * but it can with TLS 1.1 or later if the server only supports
719ebfedea0SLionel Sambuc * 1.0.
720ebfedea0SLionel Sambuc *
721ebfedea0SLionel Sambuc * Possible scenario with previous logic:
722ebfedea0SLionel Sambuc * 1. Client hello indicates TLS 1.2
723ebfedea0SLionel Sambuc * 2. Server hello says TLS 1.0
724ebfedea0SLionel Sambuc * 3. RSA encrypted premaster secret uses 1.2.
725ebfedea0SLionel Sambuc * 4. Handhaked proceeds using TLS 1.0.
726ebfedea0SLionel Sambuc * 5. Server sends hello request to renegotiate.
727ebfedea0SLionel Sambuc * 6. Client hello indicates TLS v1.0 as we now
728ebfedea0SLionel Sambuc * know that is maximum server supports.
729ebfedea0SLionel Sambuc * 7. Server chokes on RSA encrypted premaster secret
730ebfedea0SLionel Sambuc * containing version 1.0.
731ebfedea0SLionel Sambuc *
732ebfedea0SLionel Sambuc * For interoperability it should be OK to always use the
733ebfedea0SLionel Sambuc * maximum version we support in client hello and then rely
734ebfedea0SLionel Sambuc * on the checking of version to ensure the servers isn't
735ebfedea0SLionel Sambuc * being inconsistent: for example initially negotiating with
736ebfedea0SLionel Sambuc * TLS 1.0 and renegotiating with TLS 1.2. We do this by using
737ebfedea0SLionel Sambuc * client_version in client hello and not resetting it to
738ebfedea0SLionel Sambuc * the negotiated version.
739ebfedea0SLionel Sambuc */
740ebfedea0SLionel Sambuc #if 0
741ebfedea0SLionel Sambuc *(p++) = s->version >> 8;
742ebfedea0SLionel Sambuc *(p++) = s->version & 0xff;
743ebfedea0SLionel Sambuc s->client_version = s->version;
744ebfedea0SLionel Sambuc #else
745ebfedea0SLionel Sambuc *(p++) = s->client_version >> 8;
746ebfedea0SLionel Sambuc *(p++) = s->client_version & 0xff;
747ebfedea0SLionel Sambuc #endif
748ebfedea0SLionel Sambuc
749ebfedea0SLionel Sambuc /* Random stuff */
750ebfedea0SLionel Sambuc memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
751ebfedea0SLionel Sambuc p += SSL3_RANDOM_SIZE;
752ebfedea0SLionel Sambuc
753ebfedea0SLionel Sambuc /* Session ID */
754ebfedea0SLionel Sambuc if (s->new_session)
755ebfedea0SLionel Sambuc i = 0;
756ebfedea0SLionel Sambuc else
757ebfedea0SLionel Sambuc i = s->session->session_id_length;
758ebfedea0SLionel Sambuc *(p++) = i;
759*0a6a1f1dSLionel Sambuc if (i != 0) {
760*0a6a1f1dSLionel Sambuc if (i > (int)sizeof(s->session->session_id)) {
761ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
762ebfedea0SLionel Sambuc goto err;
763ebfedea0SLionel Sambuc }
764ebfedea0SLionel Sambuc memcpy(p, s->session->session_id, i);
765ebfedea0SLionel Sambuc p += i;
766ebfedea0SLionel Sambuc }
767ebfedea0SLionel Sambuc
768ebfedea0SLionel Sambuc /* Ciphers supported */
769ebfedea0SLionel Sambuc i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &(p[2]), 0);
770*0a6a1f1dSLionel Sambuc if (i == 0) {
771ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_NO_CIPHERS_AVAILABLE);
772ebfedea0SLionel Sambuc goto err;
773ebfedea0SLionel Sambuc }
774ebfedea0SLionel Sambuc #ifdef OPENSSL_MAX_TLS1_2_CIPHER_LENGTH
775*0a6a1f1dSLionel Sambuc /*
776*0a6a1f1dSLionel Sambuc * Some servers hang if client hello > 256 bytes as hack workaround
777*0a6a1f1dSLionel Sambuc * chop number of supported ciphers to keep it well below this if we
778*0a6a1f1dSLionel Sambuc * use TLS v1.2
779ebfedea0SLionel Sambuc */
780ebfedea0SLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION
781ebfedea0SLionel Sambuc && i > OPENSSL_MAX_TLS1_2_CIPHER_LENGTH)
782ebfedea0SLionel Sambuc i = OPENSSL_MAX_TLS1_2_CIPHER_LENGTH & ~1;
783ebfedea0SLionel Sambuc #endif
784ebfedea0SLionel Sambuc s2n(i, p);
785ebfedea0SLionel Sambuc p += i;
786ebfedea0SLionel Sambuc
787ebfedea0SLionel Sambuc /* COMPRESSION */
788ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
789ebfedea0SLionel Sambuc *(p++) = 1;
790ebfedea0SLionel Sambuc #else
791ebfedea0SLionel Sambuc
792ebfedea0SLionel Sambuc if ((s->options & SSL_OP_NO_COMPRESSION)
793ebfedea0SLionel Sambuc || !s->ctx->comp_methods)
794ebfedea0SLionel Sambuc j = 0;
795ebfedea0SLionel Sambuc else
796ebfedea0SLionel Sambuc j = sk_SSL_COMP_num(s->ctx->comp_methods);
797ebfedea0SLionel Sambuc *(p++) = 1 + j;
798*0a6a1f1dSLionel Sambuc for (i = 0; i < j; i++) {
799ebfedea0SLionel Sambuc comp = sk_SSL_COMP_value(s->ctx->comp_methods, i);
800ebfedea0SLionel Sambuc *(p++) = comp->id;
801ebfedea0SLionel Sambuc }
802ebfedea0SLionel Sambuc #endif
803ebfedea0SLionel Sambuc *(p++) = 0; /* Add the NULL method */
804ebfedea0SLionel Sambuc
805ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
806ebfedea0SLionel Sambuc /* TLS extensions */
807*0a6a1f1dSLionel Sambuc if (ssl_prepare_clienthello_tlsext(s) <= 0) {
808ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
809ebfedea0SLionel Sambuc goto err;
810ebfedea0SLionel Sambuc }
811*0a6a1f1dSLionel Sambuc if ((p =
812*0a6a1f1dSLionel Sambuc ssl_add_clienthello_tlsext(s, p,
813*0a6a1f1dSLionel Sambuc buf + SSL3_RT_MAX_PLAIN_LENGTH)) ==
814*0a6a1f1dSLionel Sambuc NULL) {
815ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CLIENT_HELLO, ERR_R_INTERNAL_ERROR);
816ebfedea0SLionel Sambuc goto err;
817ebfedea0SLionel Sambuc }
818ebfedea0SLionel Sambuc #endif
819ebfedea0SLionel Sambuc
820ebfedea0SLionel Sambuc l = (p - d);
821ebfedea0SLionel Sambuc d = buf;
822ebfedea0SLionel Sambuc *(d++) = SSL3_MT_CLIENT_HELLO;
823ebfedea0SLionel Sambuc l2n3(l, d);
824ebfedea0SLionel Sambuc
825ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CLNT_HELLO_B;
826ebfedea0SLionel Sambuc /* number of bytes to write */
827ebfedea0SLionel Sambuc s->init_num = p - buf;
828ebfedea0SLionel Sambuc s->init_off = 0;
829ebfedea0SLionel Sambuc }
830ebfedea0SLionel Sambuc
831ebfedea0SLionel Sambuc /* SSL3_ST_CW_CLNT_HELLO_B */
832ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
833ebfedea0SLionel Sambuc err:
834*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
835ebfedea0SLionel Sambuc return (-1);
836ebfedea0SLionel Sambuc }
837ebfedea0SLionel Sambuc
ssl3_get_server_hello(SSL * s)838ebfedea0SLionel Sambuc int ssl3_get_server_hello(SSL *s)
839ebfedea0SLionel Sambuc {
840ebfedea0SLionel Sambuc STACK_OF(SSL_CIPHER) *sk;
841ebfedea0SLionel Sambuc const SSL_CIPHER *c;
842ebfedea0SLionel Sambuc unsigned char *p, *d;
843ebfedea0SLionel Sambuc int i, al, ok;
844ebfedea0SLionel Sambuc unsigned int j;
845ebfedea0SLionel Sambuc long n;
846ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
847ebfedea0SLionel Sambuc SSL_COMP *comp;
848ebfedea0SLionel Sambuc #endif
849ebfedea0SLionel Sambuc
850ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
851ebfedea0SLionel Sambuc SSL3_ST_CR_SRVR_HELLO_A,
852*0a6a1f1dSLionel Sambuc SSL3_ST_CR_SRVR_HELLO_B, -1, 20000, &ok);
853ebfedea0SLionel Sambuc
854*0a6a1f1dSLionel Sambuc if (!ok)
855*0a6a1f1dSLionel Sambuc return ((int)n);
856ebfedea0SLionel Sambuc
857*0a6a1f1dSLionel Sambuc if (SSL_version(s) == DTLS1_VERSION || SSL_version(s) == DTLS1_BAD_VER) {
858*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type == DTLS1_MT_HELLO_VERIFY_REQUEST) {
859*0a6a1f1dSLionel Sambuc if (s->d1->send_cookie == 0) {
860ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
861ebfedea0SLionel Sambuc return 1;
862*0a6a1f1dSLionel Sambuc } else { /* already sent a cookie */
863*0a6a1f1dSLionel Sambuc
864ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
865ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
866ebfedea0SLionel Sambuc goto f_err;
867ebfedea0SLionel Sambuc }
868ebfedea0SLionel Sambuc }
869ebfedea0SLionel Sambuc }
870ebfedea0SLionel Sambuc
871*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type != SSL3_MT_SERVER_HELLO) {
872ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
873ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_MESSAGE_TYPE);
874ebfedea0SLionel Sambuc goto f_err;
875ebfedea0SLionel Sambuc }
876ebfedea0SLionel Sambuc
877ebfedea0SLionel Sambuc d = p = (unsigned char *)s->init_msg;
878ebfedea0SLionel Sambuc
879*0a6a1f1dSLionel Sambuc if ((p[0] != (s->version >> 8)) || (p[1] != (s->version & 0xff))) {
880ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_SSL_VERSION);
881ebfedea0SLionel Sambuc s->version = (s->version & 0xff00) | p[1];
882ebfedea0SLionel Sambuc al = SSL_AD_PROTOCOL_VERSION;
883ebfedea0SLionel Sambuc goto f_err;
884ebfedea0SLionel Sambuc }
885ebfedea0SLionel Sambuc p += 2;
886ebfedea0SLionel Sambuc
887ebfedea0SLionel Sambuc /* load the server hello data */
888ebfedea0SLionel Sambuc /* load the server random */
889ebfedea0SLionel Sambuc memcpy(s->s3->server_random, p, SSL3_RANDOM_SIZE);
890ebfedea0SLionel Sambuc p += SSL3_RANDOM_SIZE;
891ebfedea0SLionel Sambuc
892*0a6a1f1dSLionel Sambuc s->hit = 0;
893*0a6a1f1dSLionel Sambuc
894ebfedea0SLionel Sambuc /* get the session-id */
895ebfedea0SLionel Sambuc j = *(p++);
896ebfedea0SLionel Sambuc
897*0a6a1f1dSLionel Sambuc if ((j > sizeof s->session->session_id) || (j > SSL3_SESSION_ID_SIZE)) {
898ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
899ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SSL3_SESSION_ID_TOO_LONG);
900ebfedea0SLionel Sambuc goto f_err;
901ebfedea0SLionel Sambuc }
902ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
903*0a6a1f1dSLionel Sambuc /*
904*0a6a1f1dSLionel Sambuc * Check if we can resume the session based on external pre-shared secret.
905*0a6a1f1dSLionel Sambuc * EAP-FAST (RFC 4851) supports two types of session resumption.
906*0a6a1f1dSLionel Sambuc * Resumption based on server-side state works with session IDs.
907*0a6a1f1dSLionel Sambuc * Resumption based on pre-shared Protected Access Credentials (PACs)
908*0a6a1f1dSLionel Sambuc * works by overriding the SessionTicket extension at the application
909*0a6a1f1dSLionel Sambuc * layer, and does not send a session ID. (We do not know whether EAP-FAST
910*0a6a1f1dSLionel Sambuc * servers would honour the session ID.) Therefore, the session ID alone
911*0a6a1f1dSLionel Sambuc * is not a reliable indicator of session resumption, so we first check if
912*0a6a1f1dSLionel Sambuc * we can resume, and later peek at the next handshake message to see if the
913*0a6a1f1dSLionel Sambuc * server wants to resume.
914*0a6a1f1dSLionel Sambuc */
915*0a6a1f1dSLionel Sambuc if (s->version >= TLS1_VERSION && s->tls_session_secret_cb &&
916*0a6a1f1dSLionel Sambuc s->session->tlsext_tick) {
917ebfedea0SLionel Sambuc SSL_CIPHER *pref_cipher = NULL;
918ebfedea0SLionel Sambuc s->session->master_key_length = sizeof(s->session->master_key);
919ebfedea0SLionel Sambuc if (s->tls_session_secret_cb(s, s->session->master_key,
920ebfedea0SLionel Sambuc &s->session->master_key_length,
921ebfedea0SLionel Sambuc NULL, &pref_cipher,
922*0a6a1f1dSLionel Sambuc s->tls_session_secret_cb_arg)) {
923ebfedea0SLionel Sambuc s->session->cipher = pref_cipher ?
924ebfedea0SLionel Sambuc pref_cipher : ssl_get_cipher_by_char(s, p + j);
925*0a6a1f1dSLionel Sambuc } else {
926*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
927*0a6a1f1dSLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
928*0a6a1f1dSLionel Sambuc goto f_err;
929ebfedea0SLionel Sambuc }
930ebfedea0SLionel Sambuc }
931ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_TLSEXT */
932ebfedea0SLionel Sambuc
933ebfedea0SLionel Sambuc if (j != 0 && j == s->session->session_id_length
934*0a6a1f1dSLionel Sambuc && memcmp(p, s->session->session_id, j) == 0) {
935ebfedea0SLionel Sambuc if (s->sid_ctx_length != s->session->sid_ctx_length
936*0a6a1f1dSLionel Sambuc || memcmp(s->session->sid_ctx, s->sid_ctx, s->sid_ctx_length)) {
937ebfedea0SLionel Sambuc /* actually a client application bug */
938ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
939*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
940*0a6a1f1dSLionel Sambuc SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
941ebfedea0SLionel Sambuc goto f_err;
942ebfedea0SLionel Sambuc }
943ebfedea0SLionel Sambuc s->hit = 1;
944*0a6a1f1dSLionel Sambuc } else {
945*0a6a1f1dSLionel Sambuc /*
946*0a6a1f1dSLionel Sambuc * If we were trying for session-id reuse but the server
947*0a6a1f1dSLionel Sambuc * didn't echo the ID, make a new SSL_SESSION.
948*0a6a1f1dSLionel Sambuc * In the case of EAP-FAST and PAC, we do not send a session ID,
949*0a6a1f1dSLionel Sambuc * so the PAC-based session secret is always preserved. It'll be
950*0a6a1f1dSLionel Sambuc * overwritten if the server refuses resumption.
951*0a6a1f1dSLionel Sambuc */
952*0a6a1f1dSLionel Sambuc if (s->session->session_id_length > 0) {
953*0a6a1f1dSLionel Sambuc if (!ssl_get_new_session(s, 0)) {
954ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
955ebfedea0SLionel Sambuc goto f_err;
956ebfedea0SLionel Sambuc }
957ebfedea0SLionel Sambuc }
958ebfedea0SLionel Sambuc s->session->session_id_length = j;
959ebfedea0SLionel Sambuc memcpy(s->session->session_id, p, j); /* j could be 0 */
960ebfedea0SLionel Sambuc }
961ebfedea0SLionel Sambuc p += j;
962ebfedea0SLionel Sambuc c = ssl_get_cipher_by_char(s, p);
963*0a6a1f1dSLionel Sambuc if (c == NULL) {
964ebfedea0SLionel Sambuc /* unknown cipher */
965ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
966ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_UNKNOWN_CIPHER_RETURNED);
967ebfedea0SLionel Sambuc goto f_err;
968ebfedea0SLionel Sambuc }
969ebfedea0SLionel Sambuc /* TLS v1.2 only ciphersuites require v1.2 or later */
970ebfedea0SLionel Sambuc if ((c->algorithm_ssl & SSL_TLSV1_2) &&
971*0a6a1f1dSLionel Sambuc (TLS1_get_version(s) < TLS1_2_VERSION)) {
972ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
973ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
974ebfedea0SLionel Sambuc goto f_err;
975ebfedea0SLionel Sambuc }
976*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SRP
977*0a6a1f1dSLionel Sambuc if (((c->algorithm_mkey & SSL_kSRP) || (c->algorithm_auth & SSL_aSRP)) &&
978*0a6a1f1dSLionel Sambuc !(s->srp_ctx.srp_Mask & SSL_kSRP)) {
979*0a6a1f1dSLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
980*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
981*0a6a1f1dSLionel Sambuc goto f_err;
982*0a6a1f1dSLionel Sambuc }
983*0a6a1f1dSLionel Sambuc #endif /* OPENSSL_NO_SRP */
984ebfedea0SLionel Sambuc p += ssl_put_cipher_by_char(s, NULL, NULL);
985ebfedea0SLionel Sambuc
986ebfedea0SLionel Sambuc sk = ssl_get_ciphers_by_id(s);
987ebfedea0SLionel Sambuc i = sk_SSL_CIPHER_find(sk, c);
988*0a6a1f1dSLionel Sambuc if (i < 0) {
989ebfedea0SLionel Sambuc /* we did not say we would use this cipher */
990ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
991ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_WRONG_CIPHER_RETURNED);
992ebfedea0SLionel Sambuc goto f_err;
993ebfedea0SLionel Sambuc }
994ebfedea0SLionel Sambuc
995*0a6a1f1dSLionel Sambuc /*
996*0a6a1f1dSLionel Sambuc * Depending on the session caching (internal/external), the cipher
997*0a6a1f1dSLionel Sambuc * and/or cipher_id values may not be set. Make sure that cipher_id is
998*0a6a1f1dSLionel Sambuc * set and use it for comparison.
999*0a6a1f1dSLionel Sambuc */
1000ebfedea0SLionel Sambuc if (s->session->cipher)
1001ebfedea0SLionel Sambuc s->session->cipher_id = s->session->cipher->id;
1002*0a6a1f1dSLionel Sambuc if (s->hit && (s->session->cipher_id != c->id)) {
1003ebfedea0SLionel Sambuc /* Workaround is now obsolete */
1004ebfedea0SLionel Sambuc #if 0
1005*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
1006ebfedea0SLionel Sambuc #endif
1007ebfedea0SLionel Sambuc {
1008ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1009*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1010*0a6a1f1dSLionel Sambuc SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
1011ebfedea0SLionel Sambuc goto f_err;
1012ebfedea0SLionel Sambuc }
1013ebfedea0SLionel Sambuc }
1014ebfedea0SLionel Sambuc s->s3->tmp.new_cipher = c;
1015*0a6a1f1dSLionel Sambuc /*
1016*0a6a1f1dSLionel Sambuc * Don't digest cached records if TLS v1.2: we may need them for client
1017*0a6a1f1dSLionel Sambuc * authentication.
1018ebfedea0SLionel Sambuc */
1019*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) < TLS1_2_VERSION
1020*0a6a1f1dSLionel Sambuc && !ssl3_digest_cached_records(s)) {
1021ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1022ebfedea0SLionel Sambuc goto f_err;
1023ebfedea0SLionel Sambuc }
1024ebfedea0SLionel Sambuc /* lets get the compression algorithm */
1025ebfedea0SLionel Sambuc /* COMPRESSION */
1026ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_COMP
1027*0a6a1f1dSLionel Sambuc if (*(p++) != 0) {
1028ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1029*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1030*0a6a1f1dSLionel Sambuc SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1031ebfedea0SLionel Sambuc goto f_err;
1032ebfedea0SLionel Sambuc }
1033*0a6a1f1dSLionel Sambuc /*
1034*0a6a1f1dSLionel Sambuc * If compression is disabled we'd better not try to resume a session
1035ebfedea0SLionel Sambuc * using compression.
1036ebfedea0SLionel Sambuc */
1037*0a6a1f1dSLionel Sambuc if (s->session->compress_meth != 0) {
1038ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1039ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1040ebfedea0SLionel Sambuc goto f_err;
1041ebfedea0SLionel Sambuc }
1042ebfedea0SLionel Sambuc #else
1043ebfedea0SLionel Sambuc j = *(p++);
1044*0a6a1f1dSLionel Sambuc if (s->hit && j != s->session->compress_meth) {
1045ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1046*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1047*0a6a1f1dSLionel Sambuc SSL_R_OLD_SESSION_COMPRESSION_ALGORITHM_NOT_RETURNED);
1048ebfedea0SLionel Sambuc goto f_err;
1049ebfedea0SLionel Sambuc }
1050ebfedea0SLionel Sambuc if (j == 0)
1051ebfedea0SLionel Sambuc comp = NULL;
1052*0a6a1f1dSLionel Sambuc else if (s->options & SSL_OP_NO_COMPRESSION) {
1053ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1054ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_COMPRESSION_DISABLED);
1055ebfedea0SLionel Sambuc goto f_err;
1056*0a6a1f1dSLionel Sambuc } else
1057ebfedea0SLionel Sambuc comp = ssl3_comp_find(s->ctx->comp_methods, j);
1058ebfedea0SLionel Sambuc
1059*0a6a1f1dSLionel Sambuc if ((j != 0) && (comp == NULL)) {
1060ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1061*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,
1062*0a6a1f1dSLionel Sambuc SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
1063ebfedea0SLionel Sambuc goto f_err;
1064*0a6a1f1dSLionel Sambuc } else {
1065ebfedea0SLionel Sambuc s->s3->tmp.new_compression = comp;
1066ebfedea0SLionel Sambuc }
1067ebfedea0SLionel Sambuc #endif
1068ebfedea0SLionel Sambuc
1069ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
1070ebfedea0SLionel Sambuc /* TLS extensions */
1071*0a6a1f1dSLionel Sambuc if (s->version >= SSL3_VERSION) {
1072*0a6a1f1dSLionel Sambuc if (!ssl_parse_serverhello_tlsext(s, &p, d, n, &al)) {
1073ebfedea0SLionel Sambuc /* 'al' set by ssl_parse_serverhello_tlsext */
1074ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_PARSE_TLSEXT);
1075ebfedea0SLionel Sambuc goto f_err;
1076ebfedea0SLionel Sambuc }
1077*0a6a1f1dSLionel Sambuc if (ssl_check_serverhello_tlsext(s) <= 0) {
1078ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1079ebfedea0SLionel Sambuc goto err;
1080ebfedea0SLionel Sambuc }
1081ebfedea0SLionel Sambuc }
1082ebfedea0SLionel Sambuc #endif
1083ebfedea0SLionel Sambuc
1084*0a6a1f1dSLionel Sambuc if (p != (d + n)) {
1085ebfedea0SLionel Sambuc /* wrong packet length */
1086ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1087ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_HELLO, SSL_R_BAD_PACKET_LENGTH);
1088ebfedea0SLionel Sambuc goto f_err;
1089ebfedea0SLionel Sambuc }
1090ebfedea0SLionel Sambuc
1091ebfedea0SLionel Sambuc return (1);
1092ebfedea0SLionel Sambuc f_err:
1093ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1094ebfedea0SLionel Sambuc err:
1095*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1096ebfedea0SLionel Sambuc return (-1);
1097ebfedea0SLionel Sambuc }
1098ebfedea0SLionel Sambuc
ssl3_get_server_certificate(SSL * s)1099ebfedea0SLionel Sambuc int ssl3_get_server_certificate(SSL *s)
1100ebfedea0SLionel Sambuc {
1101ebfedea0SLionel Sambuc int al, i, ok, ret = -1;
1102ebfedea0SLionel Sambuc unsigned long n, nc, llen, l;
1103ebfedea0SLionel Sambuc X509 *x = NULL;
1104ebfedea0SLionel Sambuc const unsigned char *q, *p;
1105ebfedea0SLionel Sambuc unsigned char *d;
1106ebfedea0SLionel Sambuc STACK_OF(X509) *sk = NULL;
1107ebfedea0SLionel Sambuc SESS_CERT *sc;
1108ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
1109*0a6a1f1dSLionel Sambuc int need_cert = 1; /* VRS: 0=> will allow null cert if auth ==
1110*0a6a1f1dSLionel Sambuc * KRB5 */
1111ebfedea0SLionel Sambuc
1112ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
1113ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_A,
1114ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_B,
1115*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
1116ebfedea0SLionel Sambuc
1117*0a6a1f1dSLionel Sambuc if (!ok)
1118*0a6a1f1dSLionel Sambuc return ((int)n);
1119ebfedea0SLionel Sambuc
1120ebfedea0SLionel Sambuc if ((s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE) ||
1121ebfedea0SLionel Sambuc ((s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5) &&
1122*0a6a1f1dSLionel Sambuc (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE))) {
1123ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
1124ebfedea0SLionel Sambuc return (1);
1125ebfedea0SLionel Sambuc }
1126ebfedea0SLionel Sambuc
1127*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
1128ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1129ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_BAD_MESSAGE_TYPE);
1130ebfedea0SLionel Sambuc goto f_err;
1131ebfedea0SLionel Sambuc }
1132ebfedea0SLionel Sambuc p = d = (unsigned char *)s->init_msg;
1133ebfedea0SLionel Sambuc
1134*0a6a1f1dSLionel Sambuc if ((sk = sk_X509_new_null()) == NULL) {
1135ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1136ebfedea0SLionel Sambuc goto err;
1137ebfedea0SLionel Sambuc }
1138ebfedea0SLionel Sambuc
1139ebfedea0SLionel Sambuc n2l3(p, llen);
1140*0a6a1f1dSLionel Sambuc if (llen + 3 != n) {
1141ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1142ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
1143ebfedea0SLionel Sambuc goto f_err;
1144ebfedea0SLionel Sambuc }
1145*0a6a1f1dSLionel Sambuc for (nc = 0; nc < llen;) {
1146ebfedea0SLionel Sambuc n2l3(p, l);
1147*0a6a1f1dSLionel Sambuc if ((l + nc + 3) > llen) {
1148ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1149*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1150*0a6a1f1dSLionel Sambuc SSL_R_CERT_LENGTH_MISMATCH);
1151ebfedea0SLionel Sambuc goto f_err;
1152ebfedea0SLionel Sambuc }
1153ebfedea0SLionel Sambuc
1154ebfedea0SLionel Sambuc q = p;
1155ebfedea0SLionel Sambuc x = d2i_X509(NULL, &q, l);
1156*0a6a1f1dSLionel Sambuc if (x == NULL) {
1157ebfedea0SLionel Sambuc al = SSL_AD_BAD_CERTIFICATE;
1158ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_ASN1_LIB);
1159ebfedea0SLionel Sambuc goto f_err;
1160ebfedea0SLionel Sambuc }
1161*0a6a1f1dSLionel Sambuc if (q != (p + l)) {
1162ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1163*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1164*0a6a1f1dSLionel Sambuc SSL_R_CERT_LENGTH_MISMATCH);
1165ebfedea0SLionel Sambuc goto f_err;
1166ebfedea0SLionel Sambuc }
1167*0a6a1f1dSLionel Sambuc if (!sk_X509_push(sk, x)) {
1168ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1169ebfedea0SLionel Sambuc goto err;
1170ebfedea0SLionel Sambuc }
1171ebfedea0SLionel Sambuc x = NULL;
1172ebfedea0SLionel Sambuc nc += l + 3;
1173ebfedea0SLionel Sambuc p = q;
1174ebfedea0SLionel Sambuc }
1175ebfedea0SLionel Sambuc
1176ebfedea0SLionel Sambuc i = ssl_verify_cert_chain(s, sk);
1177ebfedea0SLionel Sambuc if ((s->verify_mode != SSL_VERIFY_NONE) && (i <= 0)
1178ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
1179ebfedea0SLionel Sambuc && !((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1180ebfedea0SLionel Sambuc (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1181ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
1182*0a6a1f1dSLionel Sambuc ) {
1183ebfedea0SLionel Sambuc al = ssl_verify_alarm_type(s->verify_result);
1184*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1185*0a6a1f1dSLionel Sambuc SSL_R_CERTIFICATE_VERIFY_FAILED);
1186ebfedea0SLionel Sambuc goto f_err;
1187ebfedea0SLionel Sambuc }
1188ebfedea0SLionel Sambuc ERR_clear_error(); /* but we keep s->verify_result */
1189ebfedea0SLionel Sambuc
1190ebfedea0SLionel Sambuc sc = ssl_sess_cert_new();
1191*0a6a1f1dSLionel Sambuc if (sc == NULL)
1192*0a6a1f1dSLionel Sambuc goto err;
1193ebfedea0SLionel Sambuc
1194*0a6a1f1dSLionel Sambuc if (s->session->sess_cert)
1195*0a6a1f1dSLionel Sambuc ssl_sess_cert_free(s->session->sess_cert);
1196ebfedea0SLionel Sambuc s->session->sess_cert = sc;
1197ebfedea0SLionel Sambuc
1198ebfedea0SLionel Sambuc sc->cert_chain = sk;
1199*0a6a1f1dSLionel Sambuc /*
1200*0a6a1f1dSLionel Sambuc * Inconsistency alert: cert_chain does include the peer's certificate,
1201*0a6a1f1dSLionel Sambuc * which we don't include in s3_srvr.c
1202*0a6a1f1dSLionel Sambuc */
1203ebfedea0SLionel Sambuc x = sk_X509_value(sk, 0);
1204ebfedea0SLionel Sambuc sk = NULL;
1205*0a6a1f1dSLionel Sambuc /*
1206*0a6a1f1dSLionel Sambuc * VRS 19990621: possible memory leak; sk=null ==> !sk_pop_free() @end
1207*0a6a1f1dSLionel Sambuc */
1208ebfedea0SLionel Sambuc
1209ebfedea0SLionel Sambuc pkey = X509_get_pubkey(x);
1210ebfedea0SLionel Sambuc
1211ebfedea0SLionel Sambuc /* VRS: allow null cert if auth == KRB5 */
1212ebfedea0SLionel Sambuc need_cert = ((s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5) &&
1213ebfedea0SLionel Sambuc (s->s3->tmp.new_cipher->algorithm_auth & SSL_aKRB5))
1214ebfedea0SLionel Sambuc ? 0 : 1;
1215ebfedea0SLionel Sambuc
1216ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
1217*0a6a1f1dSLionel Sambuc fprintf(stderr, "pkey,x = %p, %p\n", pkey, x);
1218*0a6a1f1dSLionel Sambuc fprintf(stderr, "ssl_cert_type(x,pkey) = %d\n", ssl_cert_type(x, pkey));
1219*0a6a1f1dSLionel Sambuc fprintf(stderr, "cipher, alg, nc = %s, %lx, %lx, %d\n",
1220*0a6a1f1dSLionel Sambuc s->s3->tmp.new_cipher->name,
1221*0a6a1f1dSLionel Sambuc s->s3->tmp.new_cipher->algorithm_mkey,
1222*0a6a1f1dSLionel Sambuc s->s3->tmp.new_cipher->algorithm_auth, need_cert);
1223ebfedea0SLionel Sambuc #endif /* KSSL_DEBUG */
1224ebfedea0SLionel Sambuc
1225*0a6a1f1dSLionel Sambuc if (need_cert && ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))) {
1226ebfedea0SLionel Sambuc x = NULL;
1227ebfedea0SLionel Sambuc al = SSL3_AL_FATAL;
1228ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1229ebfedea0SLionel Sambuc SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
1230ebfedea0SLionel Sambuc goto f_err;
1231ebfedea0SLionel Sambuc }
1232ebfedea0SLionel Sambuc
1233ebfedea0SLionel Sambuc i = ssl_cert_type(x, pkey);
1234*0a6a1f1dSLionel Sambuc if (need_cert && i < 0) {
1235ebfedea0SLionel Sambuc x = NULL;
1236ebfedea0SLionel Sambuc al = SSL3_AL_FATAL;
1237ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,
1238ebfedea0SLionel Sambuc SSL_R_UNKNOWN_CERTIFICATE_TYPE);
1239ebfedea0SLionel Sambuc goto f_err;
1240ebfedea0SLionel Sambuc }
1241ebfedea0SLionel Sambuc
1242*0a6a1f1dSLionel Sambuc if (need_cert) {
1243ebfedea0SLionel Sambuc sc->peer_cert_type = i;
1244ebfedea0SLionel Sambuc CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1245*0a6a1f1dSLionel Sambuc /*
1246*0a6a1f1dSLionel Sambuc * Why would the following ever happen? We just created sc a couple
1247*0a6a1f1dSLionel Sambuc * of lines ago.
1248*0a6a1f1dSLionel Sambuc */
1249ebfedea0SLionel Sambuc if (sc->peer_pkeys[i].x509 != NULL)
1250ebfedea0SLionel Sambuc X509_free(sc->peer_pkeys[i].x509);
1251ebfedea0SLionel Sambuc sc->peer_pkeys[i].x509 = x;
1252ebfedea0SLionel Sambuc sc->peer_key = &(sc->peer_pkeys[i]);
1253ebfedea0SLionel Sambuc
1254ebfedea0SLionel Sambuc if (s->session->peer != NULL)
1255ebfedea0SLionel Sambuc X509_free(s->session->peer);
1256ebfedea0SLionel Sambuc CRYPTO_add(&x->references, 1, CRYPTO_LOCK_X509);
1257ebfedea0SLionel Sambuc s->session->peer = x;
1258*0a6a1f1dSLionel Sambuc } else {
1259ebfedea0SLionel Sambuc sc->peer_cert_type = i;
1260ebfedea0SLionel Sambuc sc->peer_key = NULL;
1261ebfedea0SLionel Sambuc
1262ebfedea0SLionel Sambuc if (s->session->peer != NULL)
1263ebfedea0SLionel Sambuc X509_free(s->session->peer);
1264ebfedea0SLionel Sambuc s->session->peer = NULL;
1265ebfedea0SLionel Sambuc }
1266ebfedea0SLionel Sambuc s->session->verify_result = s->verify_result;
1267ebfedea0SLionel Sambuc
1268ebfedea0SLionel Sambuc x = NULL;
1269ebfedea0SLionel Sambuc ret = 1;
1270ebfedea0SLionel Sambuc
1271*0a6a1f1dSLionel Sambuc if (0) {
1272ebfedea0SLionel Sambuc f_err:
1273ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1274ebfedea0SLionel Sambuc err:
1275*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1276*0a6a1f1dSLionel Sambuc }
1277*0a6a1f1dSLionel Sambuc
1278ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
1279ebfedea0SLionel Sambuc X509_free(x);
1280ebfedea0SLionel Sambuc sk_X509_pop_free(sk, X509_free);
1281ebfedea0SLionel Sambuc return (ret);
1282ebfedea0SLionel Sambuc }
1283ebfedea0SLionel Sambuc
ssl3_get_key_exchange(SSL * s)1284ebfedea0SLionel Sambuc int ssl3_get_key_exchange(SSL *s)
1285ebfedea0SLionel Sambuc {
1286ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1287ebfedea0SLionel Sambuc unsigned char *q, md_buf[EVP_MAX_MD_SIZE * 2];
1288ebfedea0SLionel Sambuc #endif
1289ebfedea0SLionel Sambuc EVP_MD_CTX md_ctx;
1290ebfedea0SLionel Sambuc unsigned char *param, *p;
1291*0a6a1f1dSLionel Sambuc int al, j, ok;
1292*0a6a1f1dSLionel Sambuc long i, param_len, n, alg_k, alg_a;
1293ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
1294ebfedea0SLionel Sambuc const EVP_MD *md = NULL;
1295ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1296ebfedea0SLionel Sambuc RSA *rsa = NULL;
1297ebfedea0SLionel Sambuc #endif
1298ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1299ebfedea0SLionel Sambuc DH *dh = NULL;
1300ebfedea0SLionel Sambuc #endif
1301ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1302ebfedea0SLionel Sambuc EC_KEY *ecdh = NULL;
1303ebfedea0SLionel Sambuc BN_CTX *bn_ctx = NULL;
1304ebfedea0SLionel Sambuc EC_POINT *srvr_ecpoint = NULL;
1305ebfedea0SLionel Sambuc int curve_nid = 0;
1306ebfedea0SLionel Sambuc int encoded_pt_len = 0;
1307ebfedea0SLionel Sambuc #endif
1308ebfedea0SLionel Sambuc
1309*0a6a1f1dSLionel Sambuc EVP_MD_CTX_init(&md_ctx);
1310*0a6a1f1dSLionel Sambuc
1311*0a6a1f1dSLionel Sambuc /*
1312*0a6a1f1dSLionel Sambuc * use same message size as in ssl3_get_certificate_request() as
1313*0a6a1f1dSLionel Sambuc * ServerKeyExchange message may be skipped
1314*0a6a1f1dSLionel Sambuc */
1315ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
1316ebfedea0SLionel Sambuc SSL3_ST_CR_KEY_EXCH_A,
1317ebfedea0SLionel Sambuc SSL3_ST_CR_KEY_EXCH_B,
1318*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
1319*0a6a1f1dSLionel Sambuc if (!ok)
1320*0a6a1f1dSLionel Sambuc return ((int)n);
1321ebfedea0SLionel Sambuc
1322*0a6a1f1dSLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
1323*0a6a1f1dSLionel Sambuc
1324*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE) {
1325*0a6a1f1dSLionel Sambuc /*
1326*0a6a1f1dSLionel Sambuc * Can't skip server key exchange if this is an ephemeral
1327*0a6a1f1dSLionel Sambuc * ciphersuite.
1328*0a6a1f1dSLionel Sambuc */
1329*0a6a1f1dSLionel Sambuc if (alg_k & (SSL_kEDH | SSL_kEECDH)) {
1330*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1331*0a6a1f1dSLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1332*0a6a1f1dSLionel Sambuc goto f_err;
1333*0a6a1f1dSLionel Sambuc }
1334ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1335*0a6a1f1dSLionel Sambuc /*
1336*0a6a1f1dSLionel Sambuc * In plain PSK ciphersuite, ServerKeyExchange can be omitted if no
1337*0a6a1f1dSLionel Sambuc * identity hint is sent. Set session->sess_cert anyway to avoid
1338*0a6a1f1dSLionel Sambuc * problems later.
1339*0a6a1f1dSLionel Sambuc */
1340*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kPSK) {
1341ebfedea0SLionel Sambuc s->session->sess_cert = ssl_sess_cert_new();
1342ebfedea0SLionel Sambuc if (s->ctx->psk_identity_hint)
1343ebfedea0SLionel Sambuc OPENSSL_free(s->ctx->psk_identity_hint);
1344ebfedea0SLionel Sambuc s->ctx->psk_identity_hint = NULL;
1345ebfedea0SLionel Sambuc }
1346ebfedea0SLionel Sambuc #endif
1347ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
1348ebfedea0SLionel Sambuc return (1);
1349ebfedea0SLionel Sambuc }
1350ebfedea0SLionel Sambuc
1351ebfedea0SLionel Sambuc param = p = (unsigned char *)s->init_msg;
1352*0a6a1f1dSLionel Sambuc if (s->session->sess_cert != NULL) {
1353ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1354*0a6a1f1dSLionel Sambuc if (s->session->sess_cert->peer_rsa_tmp != NULL) {
1355ebfedea0SLionel Sambuc RSA_free(s->session->sess_cert->peer_rsa_tmp);
1356ebfedea0SLionel Sambuc s->session->sess_cert->peer_rsa_tmp = NULL;
1357ebfedea0SLionel Sambuc }
1358ebfedea0SLionel Sambuc #endif
1359ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1360*0a6a1f1dSLionel Sambuc if (s->session->sess_cert->peer_dh_tmp) {
1361ebfedea0SLionel Sambuc DH_free(s->session->sess_cert->peer_dh_tmp);
1362ebfedea0SLionel Sambuc s->session->sess_cert->peer_dh_tmp = NULL;
1363ebfedea0SLionel Sambuc }
1364ebfedea0SLionel Sambuc #endif
1365ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1366*0a6a1f1dSLionel Sambuc if (s->session->sess_cert->peer_ecdh_tmp) {
1367ebfedea0SLionel Sambuc EC_KEY_free(s->session->sess_cert->peer_ecdh_tmp);
1368ebfedea0SLionel Sambuc s->session->sess_cert->peer_ecdh_tmp = NULL;
1369ebfedea0SLionel Sambuc }
1370ebfedea0SLionel Sambuc #endif
1371*0a6a1f1dSLionel Sambuc } else {
1372ebfedea0SLionel Sambuc s->session->sess_cert = ssl_sess_cert_new();
1373ebfedea0SLionel Sambuc }
1374ebfedea0SLionel Sambuc
1375*0a6a1f1dSLionel Sambuc /* Total length of the parameters including the length prefix */
1376ebfedea0SLionel Sambuc param_len = 0;
1377*0a6a1f1dSLionel Sambuc
1378ebfedea0SLionel Sambuc alg_a = s->s3->tmp.new_cipher->algorithm_auth;
1379*0a6a1f1dSLionel Sambuc
1380*0a6a1f1dSLionel Sambuc al = SSL_AD_DECODE_ERROR;
1381ebfedea0SLionel Sambuc
1382ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
1383*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kPSK) {
1384*0a6a1f1dSLionel Sambuc param_len = 2;
1385*0a6a1f1dSLionel Sambuc if (param_len > n) {
1386*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1387ebfedea0SLionel Sambuc goto f_err;
1388ebfedea0SLionel Sambuc }
1389*0a6a1f1dSLionel Sambuc n2s(p, i);
1390*0a6a1f1dSLionel Sambuc
1391*0a6a1f1dSLionel Sambuc /*
1392*0a6a1f1dSLionel Sambuc * Store PSK identity hint for later use, hint is used in
1393*0a6a1f1dSLionel Sambuc * ssl3_send_client_key_exchange. Assume that the maximum length of
1394*0a6a1f1dSLionel Sambuc * a PSK identity hint can be as long as the maximum length of a PSK
1395*0a6a1f1dSLionel Sambuc * identity.
1396*0a6a1f1dSLionel Sambuc */
1397*0a6a1f1dSLionel Sambuc if (i > PSK_MAX_IDENTITY_LEN) {
1398*0a6a1f1dSLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1399*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_DATA_LENGTH_TOO_LONG);
1400*0a6a1f1dSLionel Sambuc goto f_err;
1401*0a6a1f1dSLionel Sambuc }
1402*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1403ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1404ebfedea0SLionel Sambuc SSL_R_BAD_PSK_IDENTITY_HINT_LENGTH);
1405ebfedea0SLionel Sambuc goto f_err;
1406ebfedea0SLionel Sambuc }
1407*0a6a1f1dSLionel Sambuc param_len += i;
1408*0a6a1f1dSLionel Sambuc
1409*0a6a1f1dSLionel Sambuc s->session->psk_identity_hint = BUF_strndup((char *)p, i);
1410*0a6a1f1dSLionel Sambuc if (s->session->psk_identity_hint == NULL) {
1411*0a6a1f1dSLionel Sambuc al = SSL_AD_HANDSHAKE_FAILURE;
1412ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1413ebfedea0SLionel Sambuc goto f_err;
1414ebfedea0SLionel Sambuc }
1415ebfedea0SLionel Sambuc
1416ebfedea0SLionel Sambuc p += i;
1417ebfedea0SLionel Sambuc n -= param_len;
1418*0a6a1f1dSLionel Sambuc } else
1419ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_PSK */
1420ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
1421*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kSRP) {
1422*0a6a1f1dSLionel Sambuc param_len = 2;
1423*0a6a1f1dSLionel Sambuc if (param_len > n) {
1424*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1425*0a6a1f1dSLionel Sambuc goto f_err;
1426*0a6a1f1dSLionel Sambuc }
1427ebfedea0SLionel Sambuc n2s(p, i);
1428*0a6a1f1dSLionel Sambuc
1429*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1430ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_N_LENGTH);
1431ebfedea0SLionel Sambuc goto f_err;
1432ebfedea0SLionel Sambuc }
1433*0a6a1f1dSLionel Sambuc param_len += i;
1434*0a6a1f1dSLionel Sambuc
1435*0a6a1f1dSLionel Sambuc if (!(s->srp_ctx.N = BN_bin2bn(p, i, NULL))) {
1436ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1437ebfedea0SLionel Sambuc goto err;
1438ebfedea0SLionel Sambuc }
1439ebfedea0SLionel Sambuc p += i;
1440ebfedea0SLionel Sambuc
1441*0a6a1f1dSLionel Sambuc if (2 > n - param_len) {
1442*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1443*0a6a1f1dSLionel Sambuc goto f_err;
1444*0a6a1f1dSLionel Sambuc }
1445*0a6a1f1dSLionel Sambuc param_len += 2;
1446*0a6a1f1dSLionel Sambuc
1447ebfedea0SLionel Sambuc n2s(p, i);
1448*0a6a1f1dSLionel Sambuc
1449*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1450ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_G_LENGTH);
1451ebfedea0SLionel Sambuc goto f_err;
1452ebfedea0SLionel Sambuc }
1453*0a6a1f1dSLionel Sambuc param_len += i;
1454*0a6a1f1dSLionel Sambuc
1455*0a6a1f1dSLionel Sambuc if (!(s->srp_ctx.g = BN_bin2bn(p, i, NULL))) {
1456ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1457ebfedea0SLionel Sambuc goto err;
1458ebfedea0SLionel Sambuc }
1459ebfedea0SLionel Sambuc p += i;
1460ebfedea0SLionel Sambuc
1461*0a6a1f1dSLionel Sambuc if (1 > n - param_len) {
1462*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1463*0a6a1f1dSLionel Sambuc goto f_err;
1464*0a6a1f1dSLionel Sambuc }
1465*0a6a1f1dSLionel Sambuc param_len += 1;
1466*0a6a1f1dSLionel Sambuc
1467ebfedea0SLionel Sambuc i = (unsigned int)(p[0]);
1468ebfedea0SLionel Sambuc p++;
1469*0a6a1f1dSLionel Sambuc
1470*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1471ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_S_LENGTH);
1472ebfedea0SLionel Sambuc goto f_err;
1473ebfedea0SLionel Sambuc }
1474*0a6a1f1dSLionel Sambuc param_len += i;
1475*0a6a1f1dSLionel Sambuc
1476*0a6a1f1dSLionel Sambuc if (!(s->srp_ctx.s = BN_bin2bn(p, i, NULL))) {
1477ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1478ebfedea0SLionel Sambuc goto err;
1479ebfedea0SLionel Sambuc }
1480ebfedea0SLionel Sambuc p += i;
1481ebfedea0SLionel Sambuc
1482*0a6a1f1dSLionel Sambuc if (2 > n - param_len) {
1483*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1484*0a6a1f1dSLionel Sambuc goto f_err;
1485*0a6a1f1dSLionel Sambuc }
1486*0a6a1f1dSLionel Sambuc param_len += 2;
1487*0a6a1f1dSLionel Sambuc
1488ebfedea0SLionel Sambuc n2s(p, i);
1489*0a6a1f1dSLionel Sambuc
1490*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1491ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_B_LENGTH);
1492ebfedea0SLionel Sambuc goto f_err;
1493ebfedea0SLionel Sambuc }
1494*0a6a1f1dSLionel Sambuc param_len += i;
1495*0a6a1f1dSLionel Sambuc
1496*0a6a1f1dSLionel Sambuc if (!(s->srp_ctx.B = BN_bin2bn(p, i, NULL))) {
1497ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1498ebfedea0SLionel Sambuc goto err;
1499ebfedea0SLionel Sambuc }
1500ebfedea0SLionel Sambuc p += i;
1501ebfedea0SLionel Sambuc n -= param_len;
1502ebfedea0SLionel Sambuc
1503*0a6a1f1dSLionel Sambuc if (!srp_verify_server_param(s, &al)) {
1504*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SRP_PARAMETERS);
1505*0a6a1f1dSLionel Sambuc goto f_err;
1506*0a6a1f1dSLionel Sambuc }
1507*0a6a1f1dSLionel Sambuc
1508ebfedea0SLionel Sambuc /* We must check if there is a certificate */
1509ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
1510ebfedea0SLionel Sambuc if (alg_a & SSL_aRSA)
1511*0a6a1f1dSLionel Sambuc pkey =
1512*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1513*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1514ebfedea0SLionel Sambuc # else
1515*0a6a1f1dSLionel Sambuc if (0) ;
1516ebfedea0SLionel Sambuc # endif
1517ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
1518ebfedea0SLionel Sambuc else if (alg_a & SSL_aDSS)
1519*0a6a1f1dSLionel Sambuc pkey =
1520*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1521*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1522*0a6a1f1dSLionel Sambuc x509);
1523ebfedea0SLionel Sambuc # endif
1524*0a6a1f1dSLionel Sambuc } else
1525ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_SRP */
1526ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1527*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kRSA) {
1528*0a6a1f1dSLionel Sambuc /* Temporary RSA keys only allowed in export ciphersuites */
1529*0a6a1f1dSLionel Sambuc if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
1530*0a6a1f1dSLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1531*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1532*0a6a1f1dSLionel Sambuc goto f_err;
1533*0a6a1f1dSLionel Sambuc }
1534*0a6a1f1dSLionel Sambuc if ((rsa = RSA_new()) == NULL) {
1535ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1536ebfedea0SLionel Sambuc goto err;
1537ebfedea0SLionel Sambuc }
1538*0a6a1f1dSLionel Sambuc
1539*0a6a1f1dSLionel Sambuc param_len = 2;
1540*0a6a1f1dSLionel Sambuc if (param_len > n) {
1541*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1542*0a6a1f1dSLionel Sambuc goto f_err;
1543*0a6a1f1dSLionel Sambuc }
1544ebfedea0SLionel Sambuc n2s(p, i);
1545*0a6a1f1dSLionel Sambuc
1546*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1547ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_MODULUS_LENGTH);
1548ebfedea0SLionel Sambuc goto f_err;
1549ebfedea0SLionel Sambuc }
1550*0a6a1f1dSLionel Sambuc param_len += i;
1551*0a6a1f1dSLionel Sambuc
1552*0a6a1f1dSLionel Sambuc if (!(rsa->n = BN_bin2bn(p, i, rsa->n))) {
1553ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1554ebfedea0SLionel Sambuc goto err;
1555ebfedea0SLionel Sambuc }
1556ebfedea0SLionel Sambuc p += i;
1557ebfedea0SLionel Sambuc
1558*0a6a1f1dSLionel Sambuc if (2 > n - param_len) {
1559*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1560*0a6a1f1dSLionel Sambuc goto f_err;
1561*0a6a1f1dSLionel Sambuc }
1562*0a6a1f1dSLionel Sambuc param_len += 2;
1563*0a6a1f1dSLionel Sambuc
1564ebfedea0SLionel Sambuc n2s(p, i);
1565*0a6a1f1dSLionel Sambuc
1566*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1567ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_E_LENGTH);
1568ebfedea0SLionel Sambuc goto f_err;
1569ebfedea0SLionel Sambuc }
1570*0a6a1f1dSLionel Sambuc param_len += i;
1571*0a6a1f1dSLionel Sambuc
1572*0a6a1f1dSLionel Sambuc if (!(rsa->e = BN_bin2bn(p, i, rsa->e))) {
1573ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1574ebfedea0SLionel Sambuc goto err;
1575ebfedea0SLionel Sambuc }
1576ebfedea0SLionel Sambuc p += i;
1577ebfedea0SLionel Sambuc n -= param_len;
1578ebfedea0SLionel Sambuc
1579ebfedea0SLionel Sambuc /* this should be because we are using an export cipher */
1580ebfedea0SLionel Sambuc if (alg_a & SSL_aRSA)
1581*0a6a1f1dSLionel Sambuc pkey =
1582*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1583*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1584*0a6a1f1dSLionel Sambuc else {
1585ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1586ebfedea0SLionel Sambuc goto err;
1587ebfedea0SLionel Sambuc }
1588*0a6a1f1dSLionel Sambuc
1589*0a6a1f1dSLionel Sambuc if (EVP_PKEY_bits(pkey) <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
1590*0a6a1f1dSLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1591*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1592*0a6a1f1dSLionel Sambuc goto f_err;
1593*0a6a1f1dSLionel Sambuc }
1594*0a6a1f1dSLionel Sambuc
1595ebfedea0SLionel Sambuc s->session->sess_cert->peer_rsa_tmp = rsa;
1596ebfedea0SLionel Sambuc rsa = NULL;
1597ebfedea0SLionel Sambuc }
1598ebfedea0SLionel Sambuc #else /* OPENSSL_NO_RSA */
1599*0a6a1f1dSLionel Sambuc if (0) ;
1600ebfedea0SLionel Sambuc #endif
1601ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1602*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kEDH) {
1603*0a6a1f1dSLionel Sambuc if ((dh = DH_new()) == NULL) {
1604ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_DH_LIB);
1605ebfedea0SLionel Sambuc goto err;
1606ebfedea0SLionel Sambuc }
1607*0a6a1f1dSLionel Sambuc
1608*0a6a1f1dSLionel Sambuc param_len = 2;
1609*0a6a1f1dSLionel Sambuc if (param_len > n) {
1610*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1611*0a6a1f1dSLionel Sambuc goto f_err;
1612*0a6a1f1dSLionel Sambuc }
1613ebfedea0SLionel Sambuc n2s(p, i);
1614*0a6a1f1dSLionel Sambuc
1615*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1616ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_P_LENGTH);
1617ebfedea0SLionel Sambuc goto f_err;
1618ebfedea0SLionel Sambuc }
1619*0a6a1f1dSLionel Sambuc param_len += i;
1620*0a6a1f1dSLionel Sambuc
1621*0a6a1f1dSLionel Sambuc if (!(dh->p = BN_bin2bn(p, i, NULL))) {
1622ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1623ebfedea0SLionel Sambuc goto err;
1624ebfedea0SLionel Sambuc }
1625ebfedea0SLionel Sambuc p += i;
1626ebfedea0SLionel Sambuc
1627*0a6a1f1dSLionel Sambuc if (2 > n - param_len) {
1628*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1629*0a6a1f1dSLionel Sambuc goto f_err;
1630*0a6a1f1dSLionel Sambuc }
1631*0a6a1f1dSLionel Sambuc param_len += 2;
1632*0a6a1f1dSLionel Sambuc
1633ebfedea0SLionel Sambuc n2s(p, i);
1634*0a6a1f1dSLionel Sambuc
1635*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1636ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_G_LENGTH);
1637ebfedea0SLionel Sambuc goto f_err;
1638ebfedea0SLionel Sambuc }
1639*0a6a1f1dSLionel Sambuc param_len += i;
1640*0a6a1f1dSLionel Sambuc
1641*0a6a1f1dSLionel Sambuc if (!(dh->g = BN_bin2bn(p, i, NULL))) {
1642ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1643ebfedea0SLionel Sambuc goto err;
1644ebfedea0SLionel Sambuc }
1645ebfedea0SLionel Sambuc p += i;
1646ebfedea0SLionel Sambuc
1647*0a6a1f1dSLionel Sambuc if (2 > n - param_len) {
1648*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1649*0a6a1f1dSLionel Sambuc goto f_err;
1650*0a6a1f1dSLionel Sambuc }
1651*0a6a1f1dSLionel Sambuc param_len += 2;
1652*0a6a1f1dSLionel Sambuc
1653ebfedea0SLionel Sambuc n2s(p, i);
1654*0a6a1f1dSLionel Sambuc
1655*0a6a1f1dSLionel Sambuc if (i > n - param_len) {
1656ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_DH_PUB_KEY_LENGTH);
1657ebfedea0SLionel Sambuc goto f_err;
1658ebfedea0SLionel Sambuc }
1659*0a6a1f1dSLionel Sambuc param_len += i;
1660*0a6a1f1dSLionel Sambuc
1661*0a6a1f1dSLionel Sambuc if (!(dh->pub_key = BN_bin2bn(p, i, NULL))) {
1662ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_BN_LIB);
1663ebfedea0SLionel Sambuc goto err;
1664ebfedea0SLionel Sambuc }
1665ebfedea0SLionel Sambuc p += i;
1666ebfedea0SLionel Sambuc n -= param_len;
1667ebfedea0SLionel Sambuc
1668ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
1669ebfedea0SLionel Sambuc if (alg_a & SSL_aRSA)
1670*0a6a1f1dSLionel Sambuc pkey =
1671*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1672*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1673ebfedea0SLionel Sambuc # else
1674*0a6a1f1dSLionel Sambuc if (0) ;
1675ebfedea0SLionel Sambuc # endif
1676ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
1677ebfedea0SLionel Sambuc else if (alg_a & SSL_aDSS)
1678*0a6a1f1dSLionel Sambuc pkey =
1679*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1680*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_DSA_SIGN].
1681*0a6a1f1dSLionel Sambuc x509);
1682ebfedea0SLionel Sambuc # endif
1683ebfedea0SLionel Sambuc /* else anonymous DH, so no certificate or pkey. */
1684ebfedea0SLionel Sambuc
1685ebfedea0SLionel Sambuc s->session->sess_cert->peer_dh_tmp = dh;
1686ebfedea0SLionel Sambuc dh = NULL;
1687*0a6a1f1dSLionel Sambuc } else if ((alg_k & SSL_kDHr) || (alg_k & SSL_kDHd)) {
1688ebfedea0SLionel Sambuc al = SSL_AD_ILLEGAL_PARAMETER;
1689*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1690*0a6a1f1dSLionel Sambuc SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1691ebfedea0SLionel Sambuc goto f_err;
1692ebfedea0SLionel Sambuc }
1693ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_DH */
1694ebfedea0SLionel Sambuc
1695ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1696*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kEECDH) {
1697ebfedea0SLionel Sambuc EC_GROUP *ngroup;
1698ebfedea0SLionel Sambuc const EC_GROUP *group;
1699ebfedea0SLionel Sambuc
1700*0a6a1f1dSLionel Sambuc if ((ecdh = EC_KEY_new()) == NULL) {
1701ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1702ebfedea0SLionel Sambuc goto err;
1703ebfedea0SLionel Sambuc }
1704ebfedea0SLionel Sambuc
1705*0a6a1f1dSLionel Sambuc /*
1706*0a6a1f1dSLionel Sambuc * Extract elliptic curve parameters and the server's ephemeral ECDH
1707*0a6a1f1dSLionel Sambuc * public key. Keep accumulating lengths of various components in
1708ebfedea0SLionel Sambuc * param_len and make sure it never exceeds n.
1709ebfedea0SLionel Sambuc */
1710ebfedea0SLionel Sambuc
1711*0a6a1f1dSLionel Sambuc /*
1712*0a6a1f1dSLionel Sambuc * XXX: For now we only support named (not generic) curves and the
1713*0a6a1f1dSLionel Sambuc * ECParameters in this case is just three bytes. We also need one
1714*0a6a1f1dSLionel Sambuc * byte for the length of the encoded point
1715ebfedea0SLionel Sambuc */
1716*0a6a1f1dSLionel Sambuc param_len = 4;
1717*0a6a1f1dSLionel Sambuc if (param_len > n) {
1718*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1719*0a6a1f1dSLionel Sambuc goto f_err;
1720*0a6a1f1dSLionel Sambuc }
1721*0a6a1f1dSLionel Sambuc
1722*0a6a1f1dSLionel Sambuc if ((*p != NAMED_CURVE_TYPE) ||
1723*0a6a1f1dSLionel Sambuc ((curve_nid = tls1_ec_curve_id2nid(*(p + 2))) == 0)) {
1724ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
1725*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1726*0a6a1f1dSLionel Sambuc SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS);
1727ebfedea0SLionel Sambuc goto f_err;
1728ebfedea0SLionel Sambuc }
1729ebfedea0SLionel Sambuc
1730ebfedea0SLionel Sambuc ngroup = EC_GROUP_new_by_curve_name(curve_nid);
1731*0a6a1f1dSLionel Sambuc if (ngroup == NULL) {
1732ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1733ebfedea0SLionel Sambuc goto err;
1734ebfedea0SLionel Sambuc }
1735*0a6a1f1dSLionel Sambuc if (EC_KEY_set_group(ecdh, ngroup) == 0) {
1736ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_EC_LIB);
1737ebfedea0SLionel Sambuc goto err;
1738ebfedea0SLionel Sambuc }
1739ebfedea0SLionel Sambuc EC_GROUP_free(ngroup);
1740ebfedea0SLionel Sambuc
1741ebfedea0SLionel Sambuc group = EC_KEY_get0_group(ecdh);
1742ebfedea0SLionel Sambuc
1743ebfedea0SLionel Sambuc if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1744*0a6a1f1dSLionel Sambuc (EC_GROUP_get_degree(group) > 163)) {
1745ebfedea0SLionel Sambuc al = SSL_AD_EXPORT_RESTRICTION;
1746*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1747*0a6a1f1dSLionel Sambuc SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1748ebfedea0SLionel Sambuc goto f_err;
1749ebfedea0SLionel Sambuc }
1750ebfedea0SLionel Sambuc
1751ebfedea0SLionel Sambuc p += 3;
1752ebfedea0SLionel Sambuc
1753ebfedea0SLionel Sambuc /* Next, get the encoded ECPoint */
1754ebfedea0SLionel Sambuc if (((srvr_ecpoint = EC_POINT_new(group)) == NULL) ||
1755*0a6a1f1dSLionel Sambuc ((bn_ctx = BN_CTX_new()) == NULL)) {
1756ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
1757ebfedea0SLionel Sambuc goto err;
1758ebfedea0SLionel Sambuc }
1759ebfedea0SLionel Sambuc
1760ebfedea0SLionel Sambuc encoded_pt_len = *p; /* length of encoded point */
1761ebfedea0SLionel Sambuc p += 1;
1762*0a6a1f1dSLionel Sambuc
1763*0a6a1f1dSLionel Sambuc if ((encoded_pt_len > n - param_len) ||
1764ebfedea0SLionel Sambuc (EC_POINT_oct2point(group, srvr_ecpoint,
1765*0a6a1f1dSLionel Sambuc p, encoded_pt_len, bn_ctx) == 0)) {
1766ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_ECPOINT);
1767ebfedea0SLionel Sambuc goto f_err;
1768ebfedea0SLionel Sambuc }
1769*0a6a1f1dSLionel Sambuc param_len += encoded_pt_len;
1770ebfedea0SLionel Sambuc
1771ebfedea0SLionel Sambuc n -= param_len;
1772ebfedea0SLionel Sambuc p += encoded_pt_len;
1773ebfedea0SLionel Sambuc
1774*0a6a1f1dSLionel Sambuc /*
1775*0a6a1f1dSLionel Sambuc * The ECC/TLS specification does not mention the use of DSA to sign
1776*0a6a1f1dSLionel Sambuc * ECParameters in the server key exchange message. We do support RSA
1777*0a6a1f1dSLionel Sambuc * and ECDSA.
1778ebfedea0SLionel Sambuc */
1779ebfedea0SLionel Sambuc if (0) ;
1780ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_RSA
1781ebfedea0SLionel Sambuc else if (alg_a & SSL_aRSA)
1782*0a6a1f1dSLionel Sambuc pkey =
1783*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1784*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].x509);
1785ebfedea0SLionel Sambuc # endif
1786ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_ECDSA
1787ebfedea0SLionel Sambuc else if (alg_a & SSL_aECDSA)
1788*0a6a1f1dSLionel Sambuc pkey =
1789*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
1790*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
1791ebfedea0SLionel Sambuc # endif
1792ebfedea0SLionel Sambuc /* else anonymous ECDH, so no certificate or pkey. */
1793ebfedea0SLionel Sambuc EC_KEY_set_public_key(ecdh, srvr_ecpoint);
1794ebfedea0SLionel Sambuc s->session->sess_cert->peer_ecdh_tmp = ecdh;
1795ebfedea0SLionel Sambuc ecdh = NULL;
1796ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
1797ebfedea0SLionel Sambuc bn_ctx = NULL;
1798ebfedea0SLionel Sambuc EC_POINT_free(srvr_ecpoint);
1799ebfedea0SLionel Sambuc srvr_ecpoint = NULL;
1800*0a6a1f1dSLionel Sambuc } else if (alg_k) {
1801ebfedea0SLionel Sambuc al = SSL_AD_UNEXPECTED_MESSAGE;
1802ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNEXPECTED_MESSAGE);
1803ebfedea0SLionel Sambuc goto f_err;
1804ebfedea0SLionel Sambuc }
1805ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_ECDH */
1806ebfedea0SLionel Sambuc
1807ebfedea0SLionel Sambuc /* p points to the next byte, there are 'n' bytes left */
1808ebfedea0SLionel Sambuc
1809ebfedea0SLionel Sambuc /* if it was signed, check the signature */
1810*0a6a1f1dSLionel Sambuc if (pkey != NULL) {
1811*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
1812*0a6a1f1dSLionel Sambuc int sigalg;
1813*0a6a1f1dSLionel Sambuc if (2 > n) {
1814*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1815*0a6a1f1dSLionel Sambuc goto f_err;
1816*0a6a1f1dSLionel Sambuc }
1817*0a6a1f1dSLionel Sambuc
1818*0a6a1f1dSLionel Sambuc sigalg = tls12_get_sigid(pkey);
1819ebfedea0SLionel Sambuc /* Should never happen */
1820*0a6a1f1dSLionel Sambuc if (sigalg == -1) {
1821ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1822ebfedea0SLionel Sambuc goto err;
1823ebfedea0SLionel Sambuc }
1824ebfedea0SLionel Sambuc /* Check key type is consistent with signature */
1825*0a6a1f1dSLionel Sambuc if (sigalg != (int)p[1]) {
1826*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,
1827*0a6a1f1dSLionel Sambuc SSL_R_WRONG_SIGNATURE_TYPE);
1828ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
1829ebfedea0SLionel Sambuc goto f_err;
1830ebfedea0SLionel Sambuc }
1831ebfedea0SLionel Sambuc md = tls12_get_hash(p[0]);
1832*0a6a1f1dSLionel Sambuc if (md == NULL) {
1833ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_UNKNOWN_DIGEST);
1834ebfedea0SLionel Sambuc goto f_err;
1835ebfedea0SLionel Sambuc }
1836ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
1837ebfedea0SLionel Sambuc fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
1838ebfedea0SLionel Sambuc #endif
1839ebfedea0SLionel Sambuc p += 2;
1840ebfedea0SLionel Sambuc n -= 2;
1841*0a6a1f1dSLionel Sambuc } else
1842ebfedea0SLionel Sambuc md = EVP_sha1();
1843ebfedea0SLionel Sambuc
1844*0a6a1f1dSLionel Sambuc if (2 > n) {
1845*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_LENGTH_TOO_SHORT);
1846*0a6a1f1dSLionel Sambuc goto f_err;
1847*0a6a1f1dSLionel Sambuc }
1848ebfedea0SLionel Sambuc n2s(p, i);
1849ebfedea0SLionel Sambuc n -= 2;
1850ebfedea0SLionel Sambuc j = EVP_PKEY_size(pkey);
1851ebfedea0SLionel Sambuc
1852*0a6a1f1dSLionel Sambuc /*
1853*0a6a1f1dSLionel Sambuc * Check signature length. If n is 0 then signature is empty
1854*0a6a1f1dSLionel Sambuc */
1855*0a6a1f1dSLionel Sambuc if ((i != n) || (n > j) || (n <= 0)) {
1856ebfedea0SLionel Sambuc /* wrong packet length */
1857ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_WRONG_SIGNATURE_LENGTH);
1858ebfedea0SLionel Sambuc goto f_err;
1859ebfedea0SLionel Sambuc }
1860ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1861*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_RSA
1862*0a6a1f1dSLionel Sambuc && TLS1_get_version(s) < TLS1_2_VERSION) {
1863ebfedea0SLionel Sambuc int num;
1864*0a6a1f1dSLionel Sambuc unsigned int size;
1865ebfedea0SLionel Sambuc
1866ebfedea0SLionel Sambuc j = 0;
1867ebfedea0SLionel Sambuc q = md_buf;
1868*0a6a1f1dSLionel Sambuc for (num = 2; num > 0; num--) {
1869*0a6a1f1dSLionel Sambuc EVP_MD_CTX_set_flags(&md_ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1870ebfedea0SLionel Sambuc EVP_DigestInit_ex(&md_ctx, (num == 2)
1871ebfedea0SLionel Sambuc ? s->ctx->md5 : s->ctx->sha1, NULL);
1872*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1873*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
1874*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1875*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
1876ebfedea0SLionel Sambuc EVP_DigestUpdate(&md_ctx, param, param_len);
1877*0a6a1f1dSLionel Sambuc EVP_DigestFinal_ex(&md_ctx, q, &size);
1878*0a6a1f1dSLionel Sambuc q += size;
1879*0a6a1f1dSLionel Sambuc j += size;
1880ebfedea0SLionel Sambuc }
1881*0a6a1f1dSLionel Sambuc i = RSA_verify(NID_md5_sha1, md_buf, j, p, n, pkey->pkey.rsa);
1882*0a6a1f1dSLionel Sambuc if (i < 0) {
1883ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
1884ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_RSA_DECRYPT);
1885ebfedea0SLionel Sambuc goto f_err;
1886ebfedea0SLionel Sambuc }
1887*0a6a1f1dSLionel Sambuc if (i == 0) {
1888ebfedea0SLionel Sambuc /* bad signature */
1889ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
1890ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1891ebfedea0SLionel Sambuc goto f_err;
1892ebfedea0SLionel Sambuc }
1893*0a6a1f1dSLionel Sambuc } else
1894ebfedea0SLionel Sambuc #endif
1895ebfedea0SLionel Sambuc {
1896ebfedea0SLionel Sambuc EVP_VerifyInit_ex(&md_ctx, md, NULL);
1897*0a6a1f1dSLionel Sambuc EVP_VerifyUpdate(&md_ctx, &(s->s3->client_random[0]),
1898*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
1899*0a6a1f1dSLionel Sambuc EVP_VerifyUpdate(&md_ctx, &(s->s3->server_random[0]),
1900*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
1901ebfedea0SLionel Sambuc EVP_VerifyUpdate(&md_ctx, param, param_len);
1902*0a6a1f1dSLionel Sambuc if (EVP_VerifyFinal(&md_ctx, p, (int)n, pkey) <= 0) {
1903ebfedea0SLionel Sambuc /* bad signature */
1904ebfedea0SLionel Sambuc al = SSL_AD_DECRYPT_ERROR;
1905ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_BAD_SIGNATURE);
1906ebfedea0SLionel Sambuc goto f_err;
1907ebfedea0SLionel Sambuc }
1908ebfedea0SLionel Sambuc }
1909*0a6a1f1dSLionel Sambuc } else {
1910*0a6a1f1dSLionel Sambuc /* aNULL, aSRP or kPSK do not need public keys */
1911*0a6a1f1dSLionel Sambuc if (!(alg_a & (SSL_aNULL | SSL_aSRP)) && !(alg_k & SSL_kPSK)) {
1912ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1913ebfedea0SLionel Sambuc goto err;
1914ebfedea0SLionel Sambuc }
1915ebfedea0SLionel Sambuc /* still data left over */
1916*0a6a1f1dSLionel Sambuc if (n != 0) {
1917ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE, SSL_R_EXTRA_DATA_IN_MESSAGE);
1918ebfedea0SLionel Sambuc goto f_err;
1919ebfedea0SLionel Sambuc }
1920ebfedea0SLionel Sambuc }
1921ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
1922ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&md_ctx);
1923ebfedea0SLionel Sambuc return (1);
1924ebfedea0SLionel Sambuc f_err:
1925ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
1926ebfedea0SLionel Sambuc err:
1927ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
1928ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
1929ebfedea0SLionel Sambuc if (rsa != NULL)
1930ebfedea0SLionel Sambuc RSA_free(rsa);
1931ebfedea0SLionel Sambuc #endif
1932ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
1933ebfedea0SLionel Sambuc if (dh != NULL)
1934ebfedea0SLionel Sambuc DH_free(dh);
1935ebfedea0SLionel Sambuc #endif
1936ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
1937ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
1938ebfedea0SLionel Sambuc EC_POINT_free(srvr_ecpoint);
1939ebfedea0SLionel Sambuc if (ecdh != NULL)
1940ebfedea0SLionel Sambuc EC_KEY_free(ecdh);
1941ebfedea0SLionel Sambuc #endif
1942ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&md_ctx);
1943*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
1944ebfedea0SLionel Sambuc return (-1);
1945ebfedea0SLionel Sambuc }
1946ebfedea0SLionel Sambuc
ssl3_get_certificate_request(SSL * s)1947ebfedea0SLionel Sambuc int ssl3_get_certificate_request(SSL *s)
1948ebfedea0SLionel Sambuc {
1949ebfedea0SLionel Sambuc int ok, ret = 0;
1950ebfedea0SLionel Sambuc unsigned long n, nc, l;
1951ebfedea0SLionel Sambuc unsigned int llen, ctype_num, i;
1952ebfedea0SLionel Sambuc X509_NAME *xn = NULL;
1953ebfedea0SLionel Sambuc const unsigned char *p, *q;
1954ebfedea0SLionel Sambuc unsigned char *d;
1955ebfedea0SLionel Sambuc STACK_OF(X509_NAME) *ca_sk = NULL;
1956ebfedea0SLionel Sambuc
1957ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
1958ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_REQ_A,
1959ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_REQ_B,
1960*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
1961ebfedea0SLionel Sambuc
1962*0a6a1f1dSLionel Sambuc if (!ok)
1963*0a6a1f1dSLionel Sambuc return ((int)n);
1964ebfedea0SLionel Sambuc
1965ebfedea0SLionel Sambuc s->s3->tmp.cert_req = 0;
1966ebfedea0SLionel Sambuc
1967*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE) {
1968ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
1969*0a6a1f1dSLionel Sambuc /*
1970*0a6a1f1dSLionel Sambuc * If we get here we don't need any cached handshake records as we
1971*0a6a1f1dSLionel Sambuc * wont be doing client auth.
1972ebfedea0SLionel Sambuc */
1973*0a6a1f1dSLionel Sambuc if (s->s3->handshake_buffer) {
1974ebfedea0SLionel Sambuc if (!ssl3_digest_cached_records(s))
1975ebfedea0SLionel Sambuc goto err;
1976ebfedea0SLionel Sambuc }
1977ebfedea0SLionel Sambuc return (1);
1978ebfedea0SLionel Sambuc }
1979ebfedea0SLionel Sambuc
1980*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST) {
1981ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1982ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_WRONG_MESSAGE_TYPE);
1983ebfedea0SLionel Sambuc goto err;
1984ebfedea0SLionel Sambuc }
1985ebfedea0SLionel Sambuc
1986ebfedea0SLionel Sambuc /* TLS does not like anon-DH with client cert */
1987*0a6a1f1dSLionel Sambuc if (s->version > SSL3_VERSION) {
1988*0a6a1f1dSLionel Sambuc if (s->s3->tmp.new_cipher->algorithm_auth & SSL_aNULL) {
1989ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
1990*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
1991*0a6a1f1dSLionel Sambuc SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1992ebfedea0SLionel Sambuc goto err;
1993ebfedea0SLionel Sambuc }
1994ebfedea0SLionel Sambuc }
1995ebfedea0SLionel Sambuc
1996ebfedea0SLionel Sambuc p = d = (unsigned char *)s->init_msg;
1997ebfedea0SLionel Sambuc
1998*0a6a1f1dSLionel Sambuc if ((ca_sk = sk_X509_NAME_new(ca_dn_cmp)) == NULL) {
1999ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2000ebfedea0SLionel Sambuc goto err;
2001ebfedea0SLionel Sambuc }
2002ebfedea0SLionel Sambuc
2003ebfedea0SLionel Sambuc /* get the certificate types */
2004ebfedea0SLionel Sambuc ctype_num = *(p++);
2005ebfedea0SLionel Sambuc if (ctype_num > SSL3_CT_NUMBER)
2006ebfedea0SLionel Sambuc ctype_num = SSL3_CT_NUMBER;
2007ebfedea0SLionel Sambuc for (i = 0; i < ctype_num; i++)
2008ebfedea0SLionel Sambuc s->s3->tmp.ctype[i] = p[i];
2009ebfedea0SLionel Sambuc p += ctype_num;
2010*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
2011ebfedea0SLionel Sambuc n2s(p, llen);
2012*0a6a1f1dSLionel Sambuc /*
2013*0a6a1f1dSLionel Sambuc * Check we have enough room for signature algorithms and following
2014*0a6a1f1dSLionel Sambuc * length value.
2015ebfedea0SLionel Sambuc */
2016*0a6a1f1dSLionel Sambuc if ((unsigned long)(p - d + llen + 2) > n) {
2017ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2018*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2019*0a6a1f1dSLionel Sambuc SSL_R_DATA_LENGTH_TOO_LONG);
2020ebfedea0SLionel Sambuc goto err;
2021ebfedea0SLionel Sambuc }
2022*0a6a1f1dSLionel Sambuc if ((llen & 1) || !tls1_process_sigalgs(s, p, llen)) {
2023ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2024*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2025*0a6a1f1dSLionel Sambuc SSL_R_SIGNATURE_ALGORITHMS_ERROR);
2026ebfedea0SLionel Sambuc goto err;
2027ebfedea0SLionel Sambuc }
2028ebfedea0SLionel Sambuc p += llen;
2029ebfedea0SLionel Sambuc }
2030ebfedea0SLionel Sambuc
2031ebfedea0SLionel Sambuc /* get the CA RDNs */
2032ebfedea0SLionel Sambuc n2s(p, llen);
2033ebfedea0SLionel Sambuc #if 0
2034ebfedea0SLionel Sambuc {
2035ebfedea0SLionel Sambuc FILE *out;
2036ebfedea0SLionel Sambuc out = fopen("/tmp/vsign.der", "w");
2037ebfedea0SLionel Sambuc fwrite(p, 1, llen, out);
2038ebfedea0SLionel Sambuc fclose(out);
2039ebfedea0SLionel Sambuc }
2040ebfedea0SLionel Sambuc #endif
2041ebfedea0SLionel Sambuc
2042*0a6a1f1dSLionel Sambuc if ((unsigned long)(p - d + llen) != n) {
2043ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2044ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_LENGTH_MISMATCH);
2045ebfedea0SLionel Sambuc goto err;
2046ebfedea0SLionel Sambuc }
2047ebfedea0SLionel Sambuc
2048*0a6a1f1dSLionel Sambuc for (nc = 0; nc < llen;) {
2049ebfedea0SLionel Sambuc n2s(p, l);
2050*0a6a1f1dSLionel Sambuc if ((l + nc + 2) > llen) {
2051ebfedea0SLionel Sambuc if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
2052ebfedea0SLionel Sambuc goto cont; /* netscape bugs */
2053ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2054ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, SSL_R_CA_DN_TOO_LONG);
2055ebfedea0SLionel Sambuc goto err;
2056ebfedea0SLionel Sambuc }
2057ebfedea0SLionel Sambuc
2058ebfedea0SLionel Sambuc q = p;
2059ebfedea0SLionel Sambuc
2060*0a6a1f1dSLionel Sambuc if ((xn = d2i_X509_NAME(NULL, &q, l)) == NULL) {
2061ebfedea0SLionel Sambuc /* If netscape tolerance is on, ignore errors */
2062ebfedea0SLionel Sambuc if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
2063ebfedea0SLionel Sambuc goto cont;
2064*0a6a1f1dSLionel Sambuc else {
2065ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2066ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_ASN1_LIB);
2067ebfedea0SLionel Sambuc goto err;
2068ebfedea0SLionel Sambuc }
2069ebfedea0SLionel Sambuc }
2070ebfedea0SLionel Sambuc
2071*0a6a1f1dSLionel Sambuc if (q != (p + l)) {
2072ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2073*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,
2074*0a6a1f1dSLionel Sambuc SSL_R_CA_DN_LENGTH_MISMATCH);
2075ebfedea0SLionel Sambuc goto err;
2076ebfedea0SLionel Sambuc }
2077*0a6a1f1dSLionel Sambuc if (!sk_X509_NAME_push(ca_sk, xn)) {
2078ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST, ERR_R_MALLOC_FAILURE);
2079ebfedea0SLionel Sambuc goto err;
2080ebfedea0SLionel Sambuc }
2081ebfedea0SLionel Sambuc
2082ebfedea0SLionel Sambuc p += l;
2083ebfedea0SLionel Sambuc nc += l + 2;
2084ebfedea0SLionel Sambuc }
2085ebfedea0SLionel Sambuc
2086*0a6a1f1dSLionel Sambuc if (0) {
2087ebfedea0SLionel Sambuc cont:
2088ebfedea0SLionel Sambuc ERR_clear_error();
2089ebfedea0SLionel Sambuc }
2090ebfedea0SLionel Sambuc
2091ebfedea0SLionel Sambuc /* we should setup a certificate to return.... */
2092ebfedea0SLionel Sambuc s->s3->tmp.cert_req = 1;
2093ebfedea0SLionel Sambuc s->s3->tmp.ctype_num = ctype_num;
2094ebfedea0SLionel Sambuc if (s->s3->tmp.ca_names != NULL)
2095ebfedea0SLionel Sambuc sk_X509_NAME_pop_free(s->s3->tmp.ca_names, X509_NAME_free);
2096ebfedea0SLionel Sambuc s->s3->tmp.ca_names = ca_sk;
2097ebfedea0SLionel Sambuc ca_sk = NULL;
2098ebfedea0SLionel Sambuc
2099ebfedea0SLionel Sambuc ret = 1;
2100*0a6a1f1dSLionel Sambuc goto done;
2101ebfedea0SLionel Sambuc err:
2102*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2103*0a6a1f1dSLionel Sambuc done:
2104*0a6a1f1dSLionel Sambuc if (ca_sk != NULL)
2105*0a6a1f1dSLionel Sambuc sk_X509_NAME_pop_free(ca_sk, X509_NAME_free);
2106ebfedea0SLionel Sambuc return (ret);
2107ebfedea0SLionel Sambuc }
2108ebfedea0SLionel Sambuc
ca_dn_cmp(const X509_NAME * const * a,const X509_NAME * const * b)2109ebfedea0SLionel Sambuc static int ca_dn_cmp(const X509_NAME *const *a, const X509_NAME *const *b)
2110ebfedea0SLionel Sambuc {
2111ebfedea0SLionel Sambuc return (X509_NAME_cmp(*a, *b));
2112ebfedea0SLionel Sambuc }
2113*0a6a1f1dSLionel Sambuc
2114ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
ssl3_get_new_session_ticket(SSL * s)2115ebfedea0SLionel Sambuc int ssl3_get_new_session_ticket(SSL *s)
2116ebfedea0SLionel Sambuc {
2117ebfedea0SLionel Sambuc int ok, al, ret = 0, ticklen;
2118ebfedea0SLionel Sambuc long n;
2119ebfedea0SLionel Sambuc const unsigned char *p;
2120ebfedea0SLionel Sambuc unsigned char *d;
2121ebfedea0SLionel Sambuc
2122ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
2123ebfedea0SLionel Sambuc SSL3_ST_CR_SESSION_TICKET_A,
2124ebfedea0SLionel Sambuc SSL3_ST_CR_SESSION_TICKET_B,
2125*0a6a1f1dSLionel Sambuc SSL3_MT_NEWSESSION_TICKET, 16384, &ok);
2126ebfedea0SLionel Sambuc
2127ebfedea0SLionel Sambuc if (!ok)
2128ebfedea0SLionel Sambuc return ((int)n);
2129ebfedea0SLionel Sambuc
2130*0a6a1f1dSLionel Sambuc if (n < 6) {
2131ebfedea0SLionel Sambuc /* need at least ticket_lifetime_hint + ticket length */
2132ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2133ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2134ebfedea0SLionel Sambuc goto f_err;
2135ebfedea0SLionel Sambuc }
2136ebfedea0SLionel Sambuc
2137ebfedea0SLionel Sambuc p = d = (unsigned char *)s->init_msg;
2138*0a6a1f1dSLionel Sambuc
2139*0a6a1f1dSLionel Sambuc if (s->session->session_id_length > 0) {
2140*0a6a1f1dSLionel Sambuc int i = s->session_ctx->session_cache_mode;
2141*0a6a1f1dSLionel Sambuc SSL_SESSION *new_sess;
2142*0a6a1f1dSLionel Sambuc /*
2143*0a6a1f1dSLionel Sambuc * We reused an existing session, so we need to replace it with a new
2144*0a6a1f1dSLionel Sambuc * one
2145*0a6a1f1dSLionel Sambuc */
2146*0a6a1f1dSLionel Sambuc if (i & SSL_SESS_CACHE_CLIENT) {
2147*0a6a1f1dSLionel Sambuc /*
2148*0a6a1f1dSLionel Sambuc * Remove the old session from the cache
2149*0a6a1f1dSLionel Sambuc */
2150*0a6a1f1dSLionel Sambuc if (i & SSL_SESS_CACHE_NO_INTERNAL_STORE) {
2151*0a6a1f1dSLionel Sambuc if (s->session_ctx->remove_session_cb != NULL)
2152*0a6a1f1dSLionel Sambuc s->session_ctx->remove_session_cb(s->session_ctx,
2153*0a6a1f1dSLionel Sambuc s->session);
2154*0a6a1f1dSLionel Sambuc } else {
2155*0a6a1f1dSLionel Sambuc /* We carry on if this fails */
2156*0a6a1f1dSLionel Sambuc SSL_CTX_remove_session(s->session_ctx, s->session);
2157*0a6a1f1dSLionel Sambuc }
2158*0a6a1f1dSLionel Sambuc }
2159*0a6a1f1dSLionel Sambuc
2160*0a6a1f1dSLionel Sambuc if ((new_sess = ssl_session_dup(s->session, 0)) == 0) {
2161*0a6a1f1dSLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
2162*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2163*0a6a1f1dSLionel Sambuc goto f_err;
2164*0a6a1f1dSLionel Sambuc }
2165*0a6a1f1dSLionel Sambuc
2166*0a6a1f1dSLionel Sambuc SSL_SESSION_free(s->session);
2167*0a6a1f1dSLionel Sambuc s->session = new_sess;
2168*0a6a1f1dSLionel Sambuc }
2169*0a6a1f1dSLionel Sambuc
2170ebfedea0SLionel Sambuc n2l(p, s->session->tlsext_tick_lifetime_hint);
2171ebfedea0SLionel Sambuc n2s(p, ticklen);
2172ebfedea0SLionel Sambuc /* ticket_lifetime_hint + ticket_length + ticket */
2173*0a6a1f1dSLionel Sambuc if (ticklen + 6 != n) {
2174ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2175ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, SSL_R_LENGTH_MISMATCH);
2176ebfedea0SLionel Sambuc goto f_err;
2177ebfedea0SLionel Sambuc }
2178*0a6a1f1dSLionel Sambuc if (s->session->tlsext_tick) {
2179ebfedea0SLionel Sambuc OPENSSL_free(s->session->tlsext_tick);
2180ebfedea0SLionel Sambuc s->session->tlsext_ticklen = 0;
2181ebfedea0SLionel Sambuc }
2182ebfedea0SLionel Sambuc s->session->tlsext_tick = OPENSSL_malloc(ticklen);
2183*0a6a1f1dSLionel Sambuc if (!s->session->tlsext_tick) {
2184ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_NEW_SESSION_TICKET, ERR_R_MALLOC_FAILURE);
2185ebfedea0SLionel Sambuc goto err;
2186ebfedea0SLionel Sambuc }
2187ebfedea0SLionel Sambuc memcpy(s->session->tlsext_tick, p, ticklen);
2188ebfedea0SLionel Sambuc s->session->tlsext_ticklen = ticklen;
2189*0a6a1f1dSLionel Sambuc /*
2190*0a6a1f1dSLionel Sambuc * There are two ways to detect a resumed ticket session. One is to set
2191*0a6a1f1dSLionel Sambuc * an appropriate session ID and then the server must return a match in
2192*0a6a1f1dSLionel Sambuc * ServerHello. This allows the normal client session ID matching to work
2193*0a6a1f1dSLionel Sambuc * and we know much earlier that the ticket has been accepted. The
2194*0a6a1f1dSLionel Sambuc * other way is to set zero length session ID when the ticket is
2195*0a6a1f1dSLionel Sambuc * presented and rely on the handshake to determine session resumption.
2196*0a6a1f1dSLionel Sambuc * We choose the former approach because this fits in with assumptions
2197*0a6a1f1dSLionel Sambuc * elsewhere in OpenSSL. The session ID is set to the SHA256 (or SHA1 is
2198*0a6a1f1dSLionel Sambuc * SHA256 is disabled) hash of the ticket.
2199ebfedea0SLionel Sambuc */
2200ebfedea0SLionel Sambuc EVP_Digest(p, ticklen,
2201ebfedea0SLionel Sambuc s->session->session_id, &s->session->session_id_length,
2202ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_SHA256
2203ebfedea0SLionel Sambuc EVP_sha256(), NULL);
2204ebfedea0SLionel Sambuc # else
2205ebfedea0SLionel Sambuc EVP_sha1(), NULL);
2206ebfedea0SLionel Sambuc # endif
2207ebfedea0SLionel Sambuc ret = 1;
2208ebfedea0SLionel Sambuc return (ret);
2209ebfedea0SLionel Sambuc f_err:
2210ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
2211ebfedea0SLionel Sambuc err:
2212*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2213ebfedea0SLionel Sambuc return (-1);
2214ebfedea0SLionel Sambuc }
2215ebfedea0SLionel Sambuc
ssl3_get_cert_status(SSL * s)2216ebfedea0SLionel Sambuc int ssl3_get_cert_status(SSL *s)
2217ebfedea0SLionel Sambuc {
2218ebfedea0SLionel Sambuc int ok, al;
2219ebfedea0SLionel Sambuc unsigned long resplen, n;
2220ebfedea0SLionel Sambuc const unsigned char *p;
2221ebfedea0SLionel Sambuc
2222ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
2223ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_STATUS_A,
2224ebfedea0SLionel Sambuc SSL3_ST_CR_CERT_STATUS_B,
2225*0a6a1f1dSLionel Sambuc SSL3_MT_CERTIFICATE_STATUS, 16384, &ok);
2226ebfedea0SLionel Sambuc
2227*0a6a1f1dSLionel Sambuc if (!ok)
2228*0a6a1f1dSLionel Sambuc return ((int)n);
2229*0a6a1f1dSLionel Sambuc if (n < 4) {
2230ebfedea0SLionel Sambuc /* need at least status type + length */
2231ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2232ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2233ebfedea0SLionel Sambuc goto f_err;
2234ebfedea0SLionel Sambuc }
2235ebfedea0SLionel Sambuc p = (unsigned char *)s->init_msg;
2236*0a6a1f1dSLionel Sambuc if (*p++ != TLSEXT_STATUSTYPE_ocsp) {
2237ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2238ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_UNSUPPORTED_STATUS_TYPE);
2239ebfedea0SLionel Sambuc goto f_err;
2240ebfedea0SLionel Sambuc }
2241ebfedea0SLionel Sambuc n2l3(p, resplen);
2242*0a6a1f1dSLionel Sambuc if (resplen + 4 != n) {
2243ebfedea0SLionel Sambuc al = SSL_AD_DECODE_ERROR;
2244ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_LENGTH_MISMATCH);
2245ebfedea0SLionel Sambuc goto f_err;
2246ebfedea0SLionel Sambuc }
2247ebfedea0SLionel Sambuc if (s->tlsext_ocsp_resp)
2248ebfedea0SLionel Sambuc OPENSSL_free(s->tlsext_ocsp_resp);
2249ebfedea0SLionel Sambuc s->tlsext_ocsp_resp = BUF_memdup(p, resplen);
2250*0a6a1f1dSLionel Sambuc if (!s->tlsext_ocsp_resp) {
2251ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
2252ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2253ebfedea0SLionel Sambuc goto f_err;
2254ebfedea0SLionel Sambuc }
2255ebfedea0SLionel Sambuc s->tlsext_ocsp_resplen = resplen;
2256*0a6a1f1dSLionel Sambuc if (s->ctx->tlsext_status_cb) {
2257ebfedea0SLionel Sambuc int ret;
2258ebfedea0SLionel Sambuc ret = s->ctx->tlsext_status_cb(s, s->ctx->tlsext_status_arg);
2259*0a6a1f1dSLionel Sambuc if (ret == 0) {
2260ebfedea0SLionel Sambuc al = SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
2261ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, SSL_R_INVALID_STATUS_RESPONSE);
2262ebfedea0SLionel Sambuc goto f_err;
2263ebfedea0SLionel Sambuc }
2264*0a6a1f1dSLionel Sambuc if (ret < 0) {
2265ebfedea0SLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
2266ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_CERT_STATUS, ERR_R_MALLOC_FAILURE);
2267ebfedea0SLionel Sambuc goto f_err;
2268ebfedea0SLionel Sambuc }
2269ebfedea0SLionel Sambuc }
2270ebfedea0SLionel Sambuc return 1;
2271ebfedea0SLionel Sambuc f_err:
2272ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
2273*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2274ebfedea0SLionel Sambuc return (-1);
2275ebfedea0SLionel Sambuc }
2276ebfedea0SLionel Sambuc #endif
2277ebfedea0SLionel Sambuc
ssl3_get_server_done(SSL * s)2278ebfedea0SLionel Sambuc int ssl3_get_server_done(SSL *s)
2279ebfedea0SLionel Sambuc {
2280ebfedea0SLionel Sambuc int ok, ret = 0;
2281ebfedea0SLionel Sambuc long n;
2282ebfedea0SLionel Sambuc
2283*0a6a1f1dSLionel Sambuc /* Second to last param should be very small, like 0 :-) */
2284ebfedea0SLionel Sambuc n = s->method->ssl_get_message(s,
2285ebfedea0SLionel Sambuc SSL3_ST_CR_SRVR_DONE_A,
2286ebfedea0SLionel Sambuc SSL3_ST_CR_SRVR_DONE_B,
2287*0a6a1f1dSLionel Sambuc SSL3_MT_SERVER_DONE, 30, &ok);
2288ebfedea0SLionel Sambuc
2289*0a6a1f1dSLionel Sambuc if (!ok)
2290*0a6a1f1dSLionel Sambuc return ((int)n);
2291*0a6a1f1dSLionel Sambuc if (n > 0) {
2292ebfedea0SLionel Sambuc /* should contain no data */
2293ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
2294ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_GET_SERVER_DONE, SSL_R_LENGTH_MISMATCH);
2295*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
2296ebfedea0SLionel Sambuc return -1;
2297ebfedea0SLionel Sambuc }
2298ebfedea0SLionel Sambuc ret = 1;
2299ebfedea0SLionel Sambuc return (ret);
2300ebfedea0SLionel Sambuc }
2301ebfedea0SLionel Sambuc
ssl3_send_client_key_exchange(SSL * s)2302ebfedea0SLionel Sambuc int ssl3_send_client_key_exchange(SSL *s)
2303ebfedea0SLionel Sambuc {
2304ebfedea0SLionel Sambuc unsigned char *p, *d;
2305ebfedea0SLionel Sambuc int n;
2306ebfedea0SLionel Sambuc unsigned long alg_k;
2307ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
2308ebfedea0SLionel Sambuc unsigned char *q;
2309ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
2310ebfedea0SLionel Sambuc #endif
2311ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
2312ebfedea0SLionel Sambuc KSSL_ERR kssl_err;
2313ebfedea0SLionel Sambuc #endif /* OPENSSL_NO_KRB5 */
2314ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2315ebfedea0SLionel Sambuc EC_KEY *clnt_ecdh = NULL;
2316ebfedea0SLionel Sambuc const EC_POINT *srvr_ecpoint = NULL;
2317ebfedea0SLionel Sambuc EVP_PKEY *srvr_pub_pkey = NULL;
2318ebfedea0SLionel Sambuc unsigned char *encodedPoint = NULL;
2319ebfedea0SLionel Sambuc int encoded_pt_len = 0;
2320ebfedea0SLionel Sambuc BN_CTX *bn_ctx = NULL;
2321ebfedea0SLionel Sambuc #endif
2322ebfedea0SLionel Sambuc
2323*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_KEY_EXCH_A) {
2324ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
2325ebfedea0SLionel Sambuc p = &(d[4]);
2326ebfedea0SLionel Sambuc
2327ebfedea0SLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
2328ebfedea0SLionel Sambuc
2329ebfedea0SLionel Sambuc /* Fool emacs indentation */
2330*0a6a1f1dSLionel Sambuc if (0) {
2331*0a6a1f1dSLionel Sambuc }
2332ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
2333*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kRSA) {
2334ebfedea0SLionel Sambuc RSA *rsa;
2335ebfedea0SLionel Sambuc unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2336ebfedea0SLionel Sambuc
2337*0a6a1f1dSLionel Sambuc if (s->session->sess_cert == NULL) {
2338*0a6a1f1dSLionel Sambuc /*
2339*0a6a1f1dSLionel Sambuc * We should always have a server certificate with SSL_kRSA.
2340*0a6a1f1dSLionel Sambuc */
2341*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2342*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2343*0a6a1f1dSLionel Sambuc goto err;
2344*0a6a1f1dSLionel Sambuc }
2345*0a6a1f1dSLionel Sambuc
2346ebfedea0SLionel Sambuc if (s->session->sess_cert->peer_rsa_tmp != NULL)
2347ebfedea0SLionel Sambuc rsa = s->session->sess_cert->peer_rsa_tmp;
2348*0a6a1f1dSLionel Sambuc else {
2349*0a6a1f1dSLionel Sambuc pkey =
2350*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
2351*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_RSA_ENC].
2352*0a6a1f1dSLionel Sambuc x509);
2353*0a6a1f1dSLionel Sambuc if ((pkey == NULL) || (pkey->type != EVP_PKEY_RSA)
2354*0a6a1f1dSLionel Sambuc || (pkey->pkey.rsa == NULL)) {
2355*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2356*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2357ebfedea0SLionel Sambuc goto err;
2358ebfedea0SLionel Sambuc }
2359ebfedea0SLionel Sambuc rsa = pkey->pkey.rsa;
2360ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
2361ebfedea0SLionel Sambuc }
2362ebfedea0SLionel Sambuc
2363ebfedea0SLionel Sambuc tmp_buf[0] = s->client_version >> 8;
2364ebfedea0SLionel Sambuc tmp_buf[1] = s->client_version & 0xff;
2365ebfedea0SLionel Sambuc if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2366ebfedea0SLionel Sambuc goto err;
2367ebfedea0SLionel Sambuc
2368ebfedea0SLionel Sambuc s->session->master_key_length = sizeof tmp_buf;
2369ebfedea0SLionel Sambuc
2370ebfedea0SLionel Sambuc q = p;
2371ebfedea0SLionel Sambuc /* Fix buf for TLS and beyond */
2372ebfedea0SLionel Sambuc if (s->version > SSL3_VERSION)
2373ebfedea0SLionel Sambuc p += 2;
2374ebfedea0SLionel Sambuc n = RSA_public_encrypt(sizeof tmp_buf,
2375ebfedea0SLionel Sambuc tmp_buf, p, rsa, RSA_PKCS1_PADDING);
2376ebfedea0SLionel Sambuc # ifdef PKCS1_CHECK
2377*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_PKCS1_CHECK_1)
2378*0a6a1f1dSLionel Sambuc p[1]++;
2379*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_PKCS1_CHECK_2)
2380*0a6a1f1dSLionel Sambuc tmp_buf[0] = 0x70;
2381ebfedea0SLionel Sambuc # endif
2382*0a6a1f1dSLionel Sambuc if (n <= 0) {
2383*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2384*0a6a1f1dSLionel Sambuc SSL_R_BAD_RSA_ENCRYPT);
2385ebfedea0SLionel Sambuc goto err;
2386ebfedea0SLionel Sambuc }
2387ebfedea0SLionel Sambuc
2388ebfedea0SLionel Sambuc /* Fix buf for TLS and beyond */
2389*0a6a1f1dSLionel Sambuc if (s->version > SSL3_VERSION) {
2390ebfedea0SLionel Sambuc s2n(n, q);
2391ebfedea0SLionel Sambuc n += 2;
2392ebfedea0SLionel Sambuc }
2393ebfedea0SLionel Sambuc
2394ebfedea0SLionel Sambuc s->session->master_key_length =
2395ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2396*0a6a1f1dSLionel Sambuc s->
2397*0a6a1f1dSLionel Sambuc session->master_key,
2398*0a6a1f1dSLionel Sambuc tmp_buf,
2399*0a6a1f1dSLionel Sambuc sizeof tmp_buf);
2400ebfedea0SLionel Sambuc OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2401ebfedea0SLionel Sambuc }
2402ebfedea0SLionel Sambuc #endif
2403ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_KRB5
2404*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kKRB5) {
2405ebfedea0SLionel Sambuc krb5_error_code krb5rc;
2406ebfedea0SLionel Sambuc KSSL_CTX *kssl_ctx = s->kssl_ctx;
2407ebfedea0SLionel Sambuc /* krb5_data krb5_ap_req; */
2408ebfedea0SLionel Sambuc krb5_data *enc_ticket;
2409ebfedea0SLionel Sambuc krb5_data authenticator, *authp = NULL;
2410ebfedea0SLionel Sambuc EVP_CIPHER_CTX ciph_ctx;
2411ebfedea0SLionel Sambuc const EVP_CIPHER *enc = NULL;
2412ebfedea0SLionel Sambuc unsigned char iv[EVP_MAX_IV_LENGTH];
2413ebfedea0SLionel Sambuc unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
2414*0a6a1f1dSLionel Sambuc unsigned char epms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_IV_LENGTH];
2415ebfedea0SLionel Sambuc int padl, outl = sizeof(epms);
2416ebfedea0SLionel Sambuc
2417ebfedea0SLionel Sambuc EVP_CIPHER_CTX_init(&ciph_ctx);
2418ebfedea0SLionel Sambuc
2419ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
2420*0a6a1f1dSLionel Sambuc fprintf(stderr, "ssl3_send_client_key_exchange(%lx & %lx)\n",
2421ebfedea0SLionel Sambuc alg_k, SSL_kKRB5);
2422ebfedea0SLionel Sambuc # endif /* KSSL_DEBUG */
2423ebfedea0SLionel Sambuc
2424ebfedea0SLionel Sambuc authp = NULL;
2425ebfedea0SLionel Sambuc # ifdef KRB5SENDAUTH
2426*0a6a1f1dSLionel Sambuc if (KRB5SENDAUTH)
2427*0a6a1f1dSLionel Sambuc authp = &authenticator;
2428ebfedea0SLionel Sambuc # endif /* KRB5SENDAUTH */
2429ebfedea0SLionel Sambuc
2430*0a6a1f1dSLionel Sambuc krb5rc = kssl_cget_tkt(kssl_ctx, &enc_ticket, authp, &kssl_err);
2431ebfedea0SLionel Sambuc enc = kssl_map_enc(kssl_ctx->enctype);
2432ebfedea0SLionel Sambuc if (enc == NULL)
2433ebfedea0SLionel Sambuc goto err;
2434ebfedea0SLionel Sambuc # ifdef KSSL_DEBUG
2435ebfedea0SLionel Sambuc {
2436*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_cget_tkt rtn %d\n", krb5rc);
2437ebfedea0SLionel Sambuc if (krb5rc && kssl_err.text)
2438*0a6a1f1dSLionel Sambuc fprintf(stderr, "kssl_cget_tkt kssl_err=%s\n",
2439*0a6a1f1dSLionel Sambuc kssl_err.text);
2440ebfedea0SLionel Sambuc }
2441ebfedea0SLionel Sambuc # endif /* KSSL_DEBUG */
2442ebfedea0SLionel Sambuc
2443*0a6a1f1dSLionel Sambuc if (krb5rc) {
2444*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2445*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2446ebfedea0SLionel Sambuc goto err;
2447ebfedea0SLionel Sambuc }
2448ebfedea0SLionel Sambuc
2449*0a6a1f1dSLionel Sambuc /*-
2450*0a6a1f1dSLionel Sambuc * 20010406 VRS - Earlier versions used KRB5 AP_REQ
2451*0a6a1f1dSLionel Sambuc * in place of RFC 2712 KerberosWrapper, as in:
2452*0a6a1f1dSLionel Sambuc *
2453*0a6a1f1dSLionel Sambuc * Send ticket (copy to *p, set n = length)
2454*0a6a1f1dSLionel Sambuc * n = krb5_ap_req.length;
2455*0a6a1f1dSLionel Sambuc * memcpy(p, krb5_ap_req.data, krb5_ap_req.length);
2456*0a6a1f1dSLionel Sambuc * if (krb5_ap_req.data)
2457*0a6a1f1dSLionel Sambuc * kssl_krb5_free_data_contents(NULL,&krb5_ap_req);
2458*0a6a1f1dSLionel Sambuc *
2459*0a6a1f1dSLionel Sambuc * Now using real RFC 2712 KerberosWrapper
2460*0a6a1f1dSLionel Sambuc * (Thanks to Simon Wilkinson <sxw@sxw.org.uk>)
2461*0a6a1f1dSLionel Sambuc * Note: 2712 "opaque" types are here replaced
2462*0a6a1f1dSLionel Sambuc * with a 2-byte length followed by the value.
2463*0a6a1f1dSLionel Sambuc * Example:
2464*0a6a1f1dSLionel Sambuc * KerberosWrapper= xx xx asn1ticket 0 0 xx xx encpms
2465*0a6a1f1dSLionel Sambuc * Where "xx xx" = length bytes. Shown here with
2466*0a6a1f1dSLionel Sambuc * optional authenticator omitted.
2467ebfedea0SLionel Sambuc */
2468ebfedea0SLionel Sambuc
2469ebfedea0SLionel Sambuc /* KerberosWrapper.Ticket */
2470ebfedea0SLionel Sambuc s2n(enc_ticket->length, p);
2471ebfedea0SLionel Sambuc memcpy(p, enc_ticket->data, enc_ticket->length);
2472ebfedea0SLionel Sambuc p += enc_ticket->length;
2473ebfedea0SLionel Sambuc n = enc_ticket->length + 2;
2474ebfedea0SLionel Sambuc
2475ebfedea0SLionel Sambuc /* KerberosWrapper.Authenticator */
2476*0a6a1f1dSLionel Sambuc if (authp && authp->length) {
2477ebfedea0SLionel Sambuc s2n(authp->length, p);
2478ebfedea0SLionel Sambuc memcpy(p, authp->data, authp->length);
2479ebfedea0SLionel Sambuc p += authp->length;
2480ebfedea0SLionel Sambuc n += authp->length + 2;
2481ebfedea0SLionel Sambuc
2482ebfedea0SLionel Sambuc free(authp->data);
2483ebfedea0SLionel Sambuc authp->data = NULL;
2484ebfedea0SLionel Sambuc authp->length = 0;
2485*0a6a1f1dSLionel Sambuc } else {
2486ebfedea0SLionel Sambuc s2n(0, p); /* null authenticator length */
2487ebfedea0SLionel Sambuc n += 2;
2488ebfedea0SLionel Sambuc }
2489ebfedea0SLionel Sambuc
2490ebfedea0SLionel Sambuc tmp_buf[0] = s->client_version >> 8;
2491ebfedea0SLionel Sambuc tmp_buf[1] = s->client_version & 0xff;
2492ebfedea0SLionel Sambuc if (RAND_bytes(&(tmp_buf[2]), sizeof tmp_buf - 2) <= 0)
2493ebfedea0SLionel Sambuc goto err;
2494ebfedea0SLionel Sambuc
2495*0a6a1f1dSLionel Sambuc /*-
2496*0a6a1f1dSLionel Sambuc * 20010420 VRS. Tried it this way; failed.
2497*0a6a1f1dSLionel Sambuc * EVP_EncryptInit_ex(&ciph_ctx,enc, NULL,NULL);
2498*0a6a1f1dSLionel Sambuc * EVP_CIPHER_CTX_set_key_length(&ciph_ctx,
2499*0a6a1f1dSLionel Sambuc * kssl_ctx->length);
2500*0a6a1f1dSLionel Sambuc * EVP_EncryptInit_ex(&ciph_ctx,NULL, key,iv);
2501ebfedea0SLionel Sambuc */
2502ebfedea0SLionel Sambuc
2503ebfedea0SLionel Sambuc memset(iv, 0, sizeof iv); /* per RFC 1510 */
2504*0a6a1f1dSLionel Sambuc EVP_EncryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv);
2505ebfedea0SLionel Sambuc EVP_EncryptUpdate(&ciph_ctx, epms, &outl, tmp_buf,
2506ebfedea0SLionel Sambuc sizeof tmp_buf);
2507ebfedea0SLionel Sambuc EVP_EncryptFinal_ex(&ciph_ctx, &(epms[outl]), &padl);
2508ebfedea0SLionel Sambuc outl += padl;
2509*0a6a1f1dSLionel Sambuc if (outl > (int)sizeof epms) {
2510*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2511*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2512ebfedea0SLionel Sambuc goto err;
2513ebfedea0SLionel Sambuc }
2514ebfedea0SLionel Sambuc EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2515ebfedea0SLionel Sambuc
2516ebfedea0SLionel Sambuc /* KerberosWrapper.EncryptedPreMasterSecret */
2517ebfedea0SLionel Sambuc s2n(outl, p);
2518ebfedea0SLionel Sambuc memcpy(p, epms, outl);
2519ebfedea0SLionel Sambuc p += outl;
2520ebfedea0SLionel Sambuc n += outl + 2;
2521ebfedea0SLionel Sambuc
2522ebfedea0SLionel Sambuc s->session->master_key_length =
2523ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2524*0a6a1f1dSLionel Sambuc s->
2525*0a6a1f1dSLionel Sambuc session->master_key,
2526*0a6a1f1dSLionel Sambuc tmp_buf,
2527*0a6a1f1dSLionel Sambuc sizeof tmp_buf);
2528ebfedea0SLionel Sambuc
2529ebfedea0SLionel Sambuc OPENSSL_cleanse(tmp_buf, sizeof tmp_buf);
2530ebfedea0SLionel Sambuc OPENSSL_cleanse(epms, outl);
2531ebfedea0SLionel Sambuc }
2532ebfedea0SLionel Sambuc #endif
2533ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
2534*0a6a1f1dSLionel Sambuc else if (alg_k & (SSL_kEDH | SSL_kDHr | SSL_kDHd)) {
2535ebfedea0SLionel Sambuc DH *dh_srvr, *dh_clnt;
2536ebfedea0SLionel Sambuc
2537*0a6a1f1dSLionel Sambuc if (s->session->sess_cert == NULL) {
2538ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2539*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2540*0a6a1f1dSLionel Sambuc SSL_R_UNEXPECTED_MESSAGE);
2541ebfedea0SLionel Sambuc goto err;
2542ebfedea0SLionel Sambuc }
2543ebfedea0SLionel Sambuc
2544ebfedea0SLionel Sambuc if (s->session->sess_cert->peer_dh_tmp != NULL)
2545ebfedea0SLionel Sambuc dh_srvr = s->session->sess_cert->peer_dh_tmp;
2546*0a6a1f1dSLionel Sambuc else {
2547ebfedea0SLionel Sambuc /* we get them from the cert */
2548ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
2549*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2550*0a6a1f1dSLionel Sambuc SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
2551ebfedea0SLionel Sambuc goto err;
2552ebfedea0SLionel Sambuc }
2553ebfedea0SLionel Sambuc
2554ebfedea0SLionel Sambuc /* generate a new random key */
2555*0a6a1f1dSLionel Sambuc if ((dh_clnt = DHparams_dup(dh_srvr)) == NULL) {
2556ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2557ebfedea0SLionel Sambuc goto err;
2558ebfedea0SLionel Sambuc }
2559*0a6a1f1dSLionel Sambuc if (!DH_generate_key(dh_clnt)) {
2560ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2561ebfedea0SLionel Sambuc DH_free(dh_clnt);
2562ebfedea0SLionel Sambuc goto err;
2563ebfedea0SLionel Sambuc }
2564ebfedea0SLionel Sambuc
2565*0a6a1f1dSLionel Sambuc /*
2566*0a6a1f1dSLionel Sambuc * use the 'p' output buffer for the DH key, but make sure to
2567*0a6a1f1dSLionel Sambuc * clear it out afterwards
2568*0a6a1f1dSLionel Sambuc */
2569ebfedea0SLionel Sambuc
2570ebfedea0SLionel Sambuc n = DH_compute_key(p, dh_srvr->pub_key, dh_clnt);
2571ebfedea0SLionel Sambuc
2572*0a6a1f1dSLionel Sambuc if (n <= 0) {
2573ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2574ebfedea0SLionel Sambuc DH_free(dh_clnt);
2575ebfedea0SLionel Sambuc goto err;
2576ebfedea0SLionel Sambuc }
2577ebfedea0SLionel Sambuc
2578ebfedea0SLionel Sambuc /* generate master key from the result */
2579ebfedea0SLionel Sambuc s->session->master_key_length =
2580ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2581*0a6a1f1dSLionel Sambuc s->
2582*0a6a1f1dSLionel Sambuc session->master_key,
2583*0a6a1f1dSLionel Sambuc p, n);
2584ebfedea0SLionel Sambuc /* clean up */
2585ebfedea0SLionel Sambuc memset(p, 0, n);
2586ebfedea0SLionel Sambuc
2587ebfedea0SLionel Sambuc /* send off the data */
2588ebfedea0SLionel Sambuc n = BN_num_bytes(dh_clnt->pub_key);
2589ebfedea0SLionel Sambuc s2n(n, p);
2590ebfedea0SLionel Sambuc BN_bn2bin(dh_clnt->pub_key, p);
2591ebfedea0SLionel Sambuc n += 2;
2592ebfedea0SLionel Sambuc
2593ebfedea0SLionel Sambuc DH_free(dh_clnt);
2594ebfedea0SLionel Sambuc }
2595ebfedea0SLionel Sambuc #endif
2596ebfedea0SLionel Sambuc
2597ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
2598*0a6a1f1dSLionel Sambuc else if (alg_k & (SSL_kEECDH | SSL_kECDHr | SSL_kECDHe)) {
2599ebfedea0SLionel Sambuc const EC_GROUP *srvr_group = NULL;
2600ebfedea0SLionel Sambuc EC_KEY *tkey;
2601ebfedea0SLionel Sambuc int ecdh_clnt_cert = 0;
2602ebfedea0SLionel Sambuc int field_size = 0;
2603ebfedea0SLionel Sambuc
2604*0a6a1f1dSLionel Sambuc if (s->session->sess_cert == NULL) {
2605*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
2606*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2607*0a6a1f1dSLionel Sambuc SSL_R_UNEXPECTED_MESSAGE);
2608*0a6a1f1dSLionel Sambuc goto err;
2609*0a6a1f1dSLionel Sambuc }
2610*0a6a1f1dSLionel Sambuc
2611*0a6a1f1dSLionel Sambuc /*
2612*0a6a1f1dSLionel Sambuc * Did we send out the client's ECDH share for use in premaster
2613*0a6a1f1dSLionel Sambuc * computation as part of client certificate? If so, set
2614*0a6a1f1dSLionel Sambuc * ecdh_clnt_cert to 1.
2615ebfedea0SLionel Sambuc */
2616*0a6a1f1dSLionel Sambuc if ((alg_k & (SSL_kECDHr | SSL_kECDHe)) && (s->cert != NULL)) {
2617*0a6a1f1dSLionel Sambuc /*-
2618*0a6a1f1dSLionel Sambuc * XXX: For now, we do not support client
2619ebfedea0SLionel Sambuc * authentication using ECDH certificates.
2620ebfedea0SLionel Sambuc * To add such support, one needs to add
2621ebfedea0SLionel Sambuc * code that checks for appropriate
2622ebfedea0SLionel Sambuc * conditions and sets ecdh_clnt_cert to 1.
2623ebfedea0SLionel Sambuc * For example, the cert have an ECC
2624ebfedea0SLionel Sambuc * key on the same curve as the server's
2625ebfedea0SLionel Sambuc * and the key should be authorized for
2626ebfedea0SLionel Sambuc * key agreement.
2627ebfedea0SLionel Sambuc *
2628ebfedea0SLionel Sambuc * One also needs to add code in ssl3_connect
2629ebfedea0SLionel Sambuc * to skip sending the certificate verify
2630ebfedea0SLionel Sambuc * message.
2631ebfedea0SLionel Sambuc *
2632ebfedea0SLionel Sambuc * if ((s->cert->key->privatekey != NULL) &&
2633ebfedea0SLionel Sambuc * (s->cert->key->privatekey->type ==
2634ebfedea0SLionel Sambuc * EVP_PKEY_EC) && ...)
2635ebfedea0SLionel Sambuc * ecdh_clnt_cert = 1;
2636ebfedea0SLionel Sambuc */
2637ebfedea0SLionel Sambuc }
2638ebfedea0SLionel Sambuc
2639*0a6a1f1dSLionel Sambuc if (s->session->sess_cert->peer_ecdh_tmp != NULL) {
2640ebfedea0SLionel Sambuc tkey = s->session->sess_cert->peer_ecdh_tmp;
2641*0a6a1f1dSLionel Sambuc } else {
2642ebfedea0SLionel Sambuc /* Get the Server Public Key from Cert */
2643*0a6a1f1dSLionel Sambuc srvr_pub_pkey =
2644*0a6a1f1dSLionel Sambuc X509_get_pubkey(s->session->
2645ebfedea0SLionel Sambuc sess_cert->peer_pkeys[SSL_PKEY_ECC].x509);
2646*0a6a1f1dSLionel Sambuc if ((srvr_pub_pkey == NULL)
2647*0a6a1f1dSLionel Sambuc || (srvr_pub_pkey->type != EVP_PKEY_EC)
2648*0a6a1f1dSLionel Sambuc || (srvr_pub_pkey->pkey.ec == NULL)) {
2649ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2650ebfedea0SLionel Sambuc ERR_R_INTERNAL_ERROR);
2651ebfedea0SLionel Sambuc goto err;
2652ebfedea0SLionel Sambuc }
2653ebfedea0SLionel Sambuc
2654ebfedea0SLionel Sambuc tkey = srvr_pub_pkey->pkey.ec;
2655ebfedea0SLionel Sambuc }
2656ebfedea0SLionel Sambuc
2657ebfedea0SLionel Sambuc srvr_group = EC_KEY_get0_group(tkey);
2658ebfedea0SLionel Sambuc srvr_ecpoint = EC_KEY_get0_public_key(tkey);
2659ebfedea0SLionel Sambuc
2660*0a6a1f1dSLionel Sambuc if ((srvr_group == NULL) || (srvr_ecpoint == NULL)) {
2661ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2662ebfedea0SLionel Sambuc ERR_R_INTERNAL_ERROR);
2663ebfedea0SLionel Sambuc goto err;
2664ebfedea0SLionel Sambuc }
2665ebfedea0SLionel Sambuc
2666*0a6a1f1dSLionel Sambuc if ((clnt_ecdh = EC_KEY_new()) == NULL) {
2667*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2668*0a6a1f1dSLionel Sambuc ERR_R_MALLOC_FAILURE);
2669ebfedea0SLionel Sambuc goto err;
2670ebfedea0SLionel Sambuc }
2671ebfedea0SLionel Sambuc
2672*0a6a1f1dSLionel Sambuc if (!EC_KEY_set_group(clnt_ecdh, srvr_group)) {
2673ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2674ebfedea0SLionel Sambuc goto err;
2675ebfedea0SLionel Sambuc }
2676*0a6a1f1dSLionel Sambuc if (ecdh_clnt_cert) {
2677*0a6a1f1dSLionel Sambuc /*
2678*0a6a1f1dSLionel Sambuc * Reuse key info from our certificate We only need our
2679*0a6a1f1dSLionel Sambuc * private key to perform the ECDH computation.
2680ebfedea0SLionel Sambuc */
2681ebfedea0SLionel Sambuc const BIGNUM *priv_key;
2682ebfedea0SLionel Sambuc tkey = s->cert->key->privatekey->pkey.ec;
2683ebfedea0SLionel Sambuc priv_key = EC_KEY_get0_private_key(tkey);
2684*0a6a1f1dSLionel Sambuc if (priv_key == NULL) {
2685*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2686*0a6a1f1dSLionel Sambuc ERR_R_MALLOC_FAILURE);
2687ebfedea0SLionel Sambuc goto err;
2688ebfedea0SLionel Sambuc }
2689*0a6a1f1dSLionel Sambuc if (!EC_KEY_set_private_key(clnt_ecdh, priv_key)) {
2690ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2691ebfedea0SLionel Sambuc goto err;
2692ebfedea0SLionel Sambuc }
2693*0a6a1f1dSLionel Sambuc } else {
2694ebfedea0SLionel Sambuc /* Generate a new ECDH key pair */
2695*0a6a1f1dSLionel Sambuc if (!(EC_KEY_generate_key(clnt_ecdh))) {
2696*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2697*0a6a1f1dSLionel Sambuc ERR_R_ECDH_LIB);
2698ebfedea0SLionel Sambuc goto err;
2699ebfedea0SLionel Sambuc }
2700ebfedea0SLionel Sambuc }
2701ebfedea0SLionel Sambuc
2702*0a6a1f1dSLionel Sambuc /*
2703*0a6a1f1dSLionel Sambuc * use the 'p' output buffer for the ECDH key, but make sure to
2704*0a6a1f1dSLionel Sambuc * clear it out afterwards
2705ebfedea0SLionel Sambuc */
2706ebfedea0SLionel Sambuc
2707ebfedea0SLionel Sambuc field_size = EC_GROUP_get_degree(srvr_group);
2708*0a6a1f1dSLionel Sambuc if (field_size <= 0) {
2709*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2710ebfedea0SLionel Sambuc goto err;
2711ebfedea0SLionel Sambuc }
2712*0a6a1f1dSLionel Sambuc n = ECDH_compute_key(p, (field_size + 7) / 8, srvr_ecpoint,
2713*0a6a1f1dSLionel Sambuc clnt_ecdh, NULL);
2714*0a6a1f1dSLionel Sambuc if (n <= 0) {
2715*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2716ebfedea0SLionel Sambuc goto err;
2717ebfedea0SLionel Sambuc }
2718ebfedea0SLionel Sambuc
2719ebfedea0SLionel Sambuc /* generate master key from the result */
2720*0a6a1f1dSLionel Sambuc s->session->master_key_length =
2721*0a6a1f1dSLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2722*0a6a1f1dSLionel Sambuc s->
2723*0a6a1f1dSLionel Sambuc session->master_key,
2724ebfedea0SLionel Sambuc p, n);
2725ebfedea0SLionel Sambuc
2726ebfedea0SLionel Sambuc memset(p, 0, n); /* clean up */
2727ebfedea0SLionel Sambuc
2728*0a6a1f1dSLionel Sambuc if (ecdh_clnt_cert) {
2729ebfedea0SLionel Sambuc /* Send empty client key exch message */
2730ebfedea0SLionel Sambuc n = 0;
2731*0a6a1f1dSLionel Sambuc } else {
2732*0a6a1f1dSLionel Sambuc /*
2733*0a6a1f1dSLionel Sambuc * First check the size of encoding and allocate memory
2734*0a6a1f1dSLionel Sambuc * accordingly.
2735ebfedea0SLionel Sambuc */
2736ebfedea0SLionel Sambuc encoded_pt_len =
2737ebfedea0SLionel Sambuc EC_POINT_point2oct(srvr_group,
2738ebfedea0SLionel Sambuc EC_KEY_get0_public_key(clnt_ecdh),
2739ebfedea0SLionel Sambuc POINT_CONVERSION_UNCOMPRESSED,
2740ebfedea0SLionel Sambuc NULL, 0, NULL);
2741ebfedea0SLionel Sambuc
2742ebfedea0SLionel Sambuc encodedPoint = (unsigned char *)
2743*0a6a1f1dSLionel Sambuc OPENSSL_malloc(encoded_pt_len * sizeof(unsigned char));
2744ebfedea0SLionel Sambuc bn_ctx = BN_CTX_new();
2745*0a6a1f1dSLionel Sambuc if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
2746*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2747*0a6a1f1dSLionel Sambuc ERR_R_MALLOC_FAILURE);
2748ebfedea0SLionel Sambuc goto err;
2749ebfedea0SLionel Sambuc }
2750ebfedea0SLionel Sambuc
2751ebfedea0SLionel Sambuc /* Encode the public key */
2752ebfedea0SLionel Sambuc n = EC_POINT_point2oct(srvr_group,
2753ebfedea0SLionel Sambuc EC_KEY_get0_public_key(clnt_ecdh),
2754ebfedea0SLionel Sambuc POINT_CONVERSION_UNCOMPRESSED,
2755ebfedea0SLionel Sambuc encodedPoint, encoded_pt_len, bn_ctx);
2756ebfedea0SLionel Sambuc
2757ebfedea0SLionel Sambuc *p = n; /* length of encoded point */
2758ebfedea0SLionel Sambuc /* Encoded point will be copied here */
2759ebfedea0SLionel Sambuc p += 1;
2760ebfedea0SLionel Sambuc /* copy the point */
2761ebfedea0SLionel Sambuc memcpy((unsigned char *)p, encodedPoint, n);
2762ebfedea0SLionel Sambuc /* increment n to account for length field */
2763ebfedea0SLionel Sambuc n += 1;
2764ebfedea0SLionel Sambuc }
2765ebfedea0SLionel Sambuc
2766ebfedea0SLionel Sambuc /* Free allocated memory */
2767ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
2768*0a6a1f1dSLionel Sambuc if (encodedPoint != NULL)
2769*0a6a1f1dSLionel Sambuc OPENSSL_free(encodedPoint);
2770ebfedea0SLionel Sambuc if (clnt_ecdh != NULL)
2771ebfedea0SLionel Sambuc EC_KEY_free(clnt_ecdh);
2772ebfedea0SLionel Sambuc EVP_PKEY_free(srvr_pub_pkey);
2773ebfedea0SLionel Sambuc }
2774ebfedea0SLionel Sambuc #endif /* !OPENSSL_NO_ECDH */
2775*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kGOST) {
2776ebfedea0SLionel Sambuc /* GOST key exchange message creation */
2777ebfedea0SLionel Sambuc EVP_PKEY_CTX *pkey_ctx;
2778ebfedea0SLionel Sambuc X509 *peer_cert;
2779ebfedea0SLionel Sambuc size_t msglen;
2780ebfedea0SLionel Sambuc unsigned int md_len;
2781ebfedea0SLionel Sambuc int keytype;
2782ebfedea0SLionel Sambuc unsigned char premaster_secret[32], shared_ukm[32], tmp[256];
2783ebfedea0SLionel Sambuc EVP_MD_CTX *ukm_hash;
2784ebfedea0SLionel Sambuc EVP_PKEY *pub_key;
2785ebfedea0SLionel Sambuc
2786*0a6a1f1dSLionel Sambuc /*
2787*0a6a1f1dSLionel Sambuc * Get server sertificate PKEY and create ctx from it
2788*0a6a1f1dSLionel Sambuc */
2789*0a6a1f1dSLionel Sambuc peer_cert =
2790*0a6a1f1dSLionel Sambuc s->session->
2791*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST01)].x509;
2792ebfedea0SLionel Sambuc if (!peer_cert)
2793*0a6a1f1dSLionel Sambuc peer_cert =
2794*0a6a1f1dSLionel Sambuc s->session->
2795*0a6a1f1dSLionel Sambuc sess_cert->peer_pkeys[(keytype = SSL_PKEY_GOST94)].x509;
2796ebfedea0SLionel Sambuc if (!peer_cert) {
2797*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2798*0a6a1f1dSLionel Sambuc SSL_R_NO_GOST_CERTIFICATE_SENT_BY_PEER);
2799ebfedea0SLionel Sambuc goto err;
2800ebfedea0SLionel Sambuc }
2801ebfedea0SLionel Sambuc
2802*0a6a1f1dSLionel Sambuc pkey_ctx = EVP_PKEY_CTX_new(pub_key =
2803*0a6a1f1dSLionel Sambuc X509_get_pubkey(peer_cert), NULL);
2804*0a6a1f1dSLionel Sambuc /*
2805*0a6a1f1dSLionel Sambuc * If we have send a certificate, and certificate key
2806*0a6a1f1dSLionel Sambuc *
2807*0a6a1f1dSLionel Sambuc * * parameters match those of server certificate, use
2808ebfedea0SLionel Sambuc * certificate key for key exchange
2809ebfedea0SLionel Sambuc */
2810ebfedea0SLionel Sambuc
2811ebfedea0SLionel Sambuc /* Otherwise, generate ephemeral key pair */
2812ebfedea0SLionel Sambuc
2813ebfedea0SLionel Sambuc EVP_PKEY_encrypt_init(pkey_ctx);
2814ebfedea0SLionel Sambuc /* Generate session key */
2815*0a6a1f1dSLionel Sambuc if (RAND_bytes(premaster_secret, 32) <= 0) {
2816*0a6a1f1dSLionel Sambuc EVP_PKEY_CTX_free(pkey_ctx);
2817*0a6a1f1dSLionel Sambuc goto err;
2818*0a6a1f1dSLionel Sambuc }
2819*0a6a1f1dSLionel Sambuc /*
2820*0a6a1f1dSLionel Sambuc * If we have client certificate, use its secret as peer key
2821*0a6a1f1dSLionel Sambuc */
2822ebfedea0SLionel Sambuc if (s->s3->tmp.cert_req && s->cert->key->privatekey) {
2823*0a6a1f1dSLionel Sambuc if (EVP_PKEY_derive_set_peer
2824*0a6a1f1dSLionel Sambuc (pkey_ctx, s->cert->key->privatekey) <= 0) {
2825*0a6a1f1dSLionel Sambuc /*
2826*0a6a1f1dSLionel Sambuc * If there was an error - just ignore it. Ephemeral key
2827*0a6a1f1dSLionel Sambuc * * would be used
2828ebfedea0SLionel Sambuc */
2829ebfedea0SLionel Sambuc ERR_clear_error();
2830ebfedea0SLionel Sambuc }
2831ebfedea0SLionel Sambuc }
2832*0a6a1f1dSLionel Sambuc /*
2833*0a6a1f1dSLionel Sambuc * Compute shared IV and store it in algorithm-specific context
2834*0a6a1f1dSLionel Sambuc * data
2835*0a6a1f1dSLionel Sambuc */
2836ebfedea0SLionel Sambuc ukm_hash = EVP_MD_CTX_create();
2837*0a6a1f1dSLionel Sambuc EVP_DigestInit(ukm_hash,
2838*0a6a1f1dSLionel Sambuc EVP_get_digestbynid(NID_id_GostR3411_94));
2839*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(ukm_hash, s->s3->client_random,
2840*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2841*0a6a1f1dSLionel Sambuc EVP_DigestUpdate(ukm_hash, s->s3->server_random,
2842*0a6a1f1dSLionel Sambuc SSL3_RANDOM_SIZE);
2843ebfedea0SLionel Sambuc EVP_DigestFinal_ex(ukm_hash, shared_ukm, &md_len);
2844ebfedea0SLionel Sambuc EVP_MD_CTX_destroy(ukm_hash);
2845*0a6a1f1dSLionel Sambuc if (EVP_PKEY_CTX_ctrl
2846*0a6a1f1dSLionel Sambuc (pkey_ctx, -1, EVP_PKEY_OP_ENCRYPT, EVP_PKEY_CTRL_SET_IV, 8,
2847*0a6a1f1dSLionel Sambuc shared_ukm) < 0) {
2848ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2849ebfedea0SLionel Sambuc SSL_R_LIBRARY_BUG);
2850ebfedea0SLionel Sambuc goto err;
2851ebfedea0SLionel Sambuc }
2852ebfedea0SLionel Sambuc /* Make GOST keytransport blob message */
2853*0a6a1f1dSLionel Sambuc /*
2854*0a6a1f1dSLionel Sambuc * Encapsulate it into sequence
2855*0a6a1f1dSLionel Sambuc */
2856ebfedea0SLionel Sambuc *(p++) = V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED;
2857ebfedea0SLionel Sambuc msglen = 255;
2858*0a6a1f1dSLionel Sambuc if (EVP_PKEY_encrypt(pkey_ctx, tmp, &msglen, premaster_secret, 32)
2859*0a6a1f1dSLionel Sambuc < 0) {
2860ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2861ebfedea0SLionel Sambuc SSL_R_LIBRARY_BUG);
2862ebfedea0SLionel Sambuc goto err;
2863ebfedea0SLionel Sambuc }
2864*0a6a1f1dSLionel Sambuc if (msglen >= 0x80) {
2865ebfedea0SLionel Sambuc *(p++) = 0x81;
2866ebfedea0SLionel Sambuc *(p++) = msglen & 0xff;
2867ebfedea0SLionel Sambuc n = msglen + 3;
2868*0a6a1f1dSLionel Sambuc } else {
2869ebfedea0SLionel Sambuc *(p++) = msglen & 0xff;
2870ebfedea0SLionel Sambuc n = msglen + 2;
2871ebfedea0SLionel Sambuc }
2872ebfedea0SLionel Sambuc memcpy(p, tmp, msglen);
2873ebfedea0SLionel Sambuc /* Check if pubkey from client certificate was used */
2874*0a6a1f1dSLionel Sambuc if (EVP_PKEY_CTX_ctrl
2875*0a6a1f1dSLionel Sambuc (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0) {
2876ebfedea0SLionel Sambuc /* Set flag "skip certificate verify" */
2877ebfedea0SLionel Sambuc s->s3->flags |= TLS1_FLAGS_SKIP_CERT_VERIFY;
2878ebfedea0SLionel Sambuc }
2879ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(pkey_ctx);
2880ebfedea0SLionel Sambuc s->session->master_key_length =
2881ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2882*0a6a1f1dSLionel Sambuc s->
2883*0a6a1f1dSLionel Sambuc session->master_key,
2884*0a6a1f1dSLionel Sambuc premaster_secret,
2885*0a6a1f1dSLionel Sambuc 32);
2886ebfedea0SLionel Sambuc EVP_PKEY_free(pub_key);
2887ebfedea0SLionel Sambuc
2888ebfedea0SLionel Sambuc }
2889ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
2890*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kSRP) {
2891*0a6a1f1dSLionel Sambuc if (s->srp_ctx.A != NULL) {
2892ebfedea0SLionel Sambuc /* send off the data */
2893ebfedea0SLionel Sambuc n = BN_num_bytes(s->srp_ctx.A);
2894ebfedea0SLionel Sambuc s2n(n, p);
2895ebfedea0SLionel Sambuc BN_bn2bin(s->srp_ctx.A, p);
2896ebfedea0SLionel Sambuc n += 2;
2897*0a6a1f1dSLionel Sambuc } else {
2898*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2899*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2900ebfedea0SLionel Sambuc goto err;
2901ebfedea0SLionel Sambuc }
2902ebfedea0SLionel Sambuc if (s->session->srp_username != NULL)
2903ebfedea0SLionel Sambuc OPENSSL_free(s->session->srp_username);
2904ebfedea0SLionel Sambuc s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2905*0a6a1f1dSLionel Sambuc if (s->session->srp_username == NULL) {
2906ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2907ebfedea0SLionel Sambuc ERR_R_MALLOC_FAILURE);
2908ebfedea0SLionel Sambuc goto err;
2909ebfedea0SLionel Sambuc }
2910ebfedea0SLionel Sambuc
2911*0a6a1f1dSLionel Sambuc if ((s->session->master_key_length =
2912*0a6a1f1dSLionel Sambuc SRP_generate_client_master_secret(s,
2913*0a6a1f1dSLionel Sambuc s->session->master_key)) <
2914*0a6a1f1dSLionel Sambuc 0) {
2915*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2916*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2917ebfedea0SLionel Sambuc goto err;
2918ebfedea0SLionel Sambuc }
2919ebfedea0SLionel Sambuc }
2920ebfedea0SLionel Sambuc #endif
2921ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_PSK
2922*0a6a1f1dSLionel Sambuc else if (alg_k & SSL_kPSK) {
2923*0a6a1f1dSLionel Sambuc /*
2924*0a6a1f1dSLionel Sambuc * The callback needs PSK_MAX_IDENTITY_LEN + 1 bytes to return a
2925*0a6a1f1dSLionel Sambuc * \0-terminated identity. The last byte is for us for simulating
2926*0a6a1f1dSLionel Sambuc * strnlen.
2927*0a6a1f1dSLionel Sambuc */
2928*0a6a1f1dSLionel Sambuc char identity[PSK_MAX_IDENTITY_LEN + 2];
2929*0a6a1f1dSLionel Sambuc size_t identity_len;
2930ebfedea0SLionel Sambuc unsigned char *t = NULL;
2931ebfedea0SLionel Sambuc unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2932ebfedea0SLionel Sambuc unsigned int pre_ms_len = 0, psk_len = 0;
2933ebfedea0SLionel Sambuc int psk_err = 1;
2934ebfedea0SLionel Sambuc
2935ebfedea0SLionel Sambuc n = 0;
2936*0a6a1f1dSLionel Sambuc if (s->psk_client_callback == NULL) {
2937ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2938ebfedea0SLionel Sambuc SSL_R_PSK_NO_CLIENT_CB);
2939ebfedea0SLionel Sambuc goto err;
2940ebfedea0SLionel Sambuc }
2941ebfedea0SLionel Sambuc
2942*0a6a1f1dSLionel Sambuc memset(identity, 0, sizeof(identity));
2943*0a6a1f1dSLionel Sambuc psk_len = s->psk_client_callback(s, s->session->psk_identity_hint,
2944*0a6a1f1dSLionel Sambuc identity, sizeof(identity) - 1,
2945*0a6a1f1dSLionel Sambuc psk_or_pre_ms,
2946*0a6a1f1dSLionel Sambuc sizeof(psk_or_pre_ms));
2947*0a6a1f1dSLionel Sambuc if (psk_len > PSK_MAX_PSK_LEN) {
2948ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2949ebfedea0SLionel Sambuc ERR_R_INTERNAL_ERROR);
2950ebfedea0SLionel Sambuc goto psk_err;
2951*0a6a1f1dSLionel Sambuc } else if (psk_len == 0) {
2952ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2953ebfedea0SLionel Sambuc SSL_R_PSK_IDENTITY_NOT_FOUND);
2954ebfedea0SLionel Sambuc goto psk_err;
2955ebfedea0SLionel Sambuc }
2956*0a6a1f1dSLionel Sambuc identity[PSK_MAX_IDENTITY_LEN + 1] = '\0';
2957*0a6a1f1dSLionel Sambuc identity_len = strlen(identity);
2958*0a6a1f1dSLionel Sambuc if (identity_len > PSK_MAX_IDENTITY_LEN) {
2959*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2960*0a6a1f1dSLionel Sambuc ERR_R_INTERNAL_ERROR);
2961*0a6a1f1dSLionel Sambuc goto psk_err;
2962*0a6a1f1dSLionel Sambuc }
2963ebfedea0SLionel Sambuc /* create PSK pre_master_secret */
2964ebfedea0SLionel Sambuc pre_ms_len = 2 + psk_len + 2 + psk_len;
2965ebfedea0SLionel Sambuc t = psk_or_pre_ms;
2966ebfedea0SLionel Sambuc memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2967ebfedea0SLionel Sambuc s2n(psk_len, t);
2968ebfedea0SLionel Sambuc memset(t, 0, psk_len);
2969ebfedea0SLionel Sambuc t += psk_len;
2970ebfedea0SLionel Sambuc s2n(psk_len, t);
2971ebfedea0SLionel Sambuc
2972ebfedea0SLionel Sambuc if (s->session->psk_identity_hint != NULL)
2973ebfedea0SLionel Sambuc OPENSSL_free(s->session->psk_identity_hint);
2974*0a6a1f1dSLionel Sambuc s->session->psk_identity_hint =
2975*0a6a1f1dSLionel Sambuc BUF_strdup(s->ctx->psk_identity_hint);
2976*0a6a1f1dSLionel Sambuc if (s->ctx->psk_identity_hint != NULL
2977*0a6a1f1dSLionel Sambuc && s->session->psk_identity_hint == NULL) {
2978ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2979ebfedea0SLionel Sambuc ERR_R_MALLOC_FAILURE);
2980ebfedea0SLionel Sambuc goto psk_err;
2981ebfedea0SLionel Sambuc }
2982ebfedea0SLionel Sambuc
2983ebfedea0SLionel Sambuc if (s->session->psk_identity != NULL)
2984ebfedea0SLionel Sambuc OPENSSL_free(s->session->psk_identity);
2985ebfedea0SLionel Sambuc s->session->psk_identity = BUF_strdup(identity);
2986*0a6a1f1dSLionel Sambuc if (s->session->psk_identity == NULL) {
2987ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
2988ebfedea0SLionel Sambuc ERR_R_MALLOC_FAILURE);
2989ebfedea0SLionel Sambuc goto psk_err;
2990ebfedea0SLionel Sambuc }
2991ebfedea0SLionel Sambuc
2992ebfedea0SLionel Sambuc s->session->master_key_length =
2993ebfedea0SLionel Sambuc s->method->ssl3_enc->generate_master_secret(s,
2994*0a6a1f1dSLionel Sambuc s->
2995*0a6a1f1dSLionel Sambuc session->master_key,
2996*0a6a1f1dSLionel Sambuc psk_or_pre_ms,
2997*0a6a1f1dSLionel Sambuc pre_ms_len);
2998*0a6a1f1dSLionel Sambuc s2n(identity_len, p);
2999*0a6a1f1dSLionel Sambuc memcpy(p, identity, identity_len);
3000*0a6a1f1dSLionel Sambuc n = 2 + identity_len;
3001ebfedea0SLionel Sambuc psk_err = 0;
3002ebfedea0SLionel Sambuc psk_err:
3003*0a6a1f1dSLionel Sambuc OPENSSL_cleanse(identity, sizeof(identity));
3004ebfedea0SLionel Sambuc OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
3005*0a6a1f1dSLionel Sambuc if (psk_err != 0) {
3006ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3007ebfedea0SLionel Sambuc goto err;
3008ebfedea0SLionel Sambuc }
3009ebfedea0SLionel Sambuc }
3010ebfedea0SLionel Sambuc #endif
3011*0a6a1f1dSLionel Sambuc else {
3012*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
3013*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
3014ebfedea0SLionel Sambuc goto err;
3015ebfedea0SLionel Sambuc }
3016ebfedea0SLionel Sambuc
3017ebfedea0SLionel Sambuc *(d++) = SSL3_MT_CLIENT_KEY_EXCHANGE;
3018ebfedea0SLionel Sambuc l2n3(n, d);
3019ebfedea0SLionel Sambuc
3020ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_KEY_EXCH_B;
3021ebfedea0SLionel Sambuc /* number of bytes to write */
3022ebfedea0SLionel Sambuc s->init_num = n + 4;
3023ebfedea0SLionel Sambuc s->init_off = 0;
3024ebfedea0SLionel Sambuc }
3025ebfedea0SLionel Sambuc
3026ebfedea0SLionel Sambuc /* SSL3_ST_CW_KEY_EXCH_B */
3027ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3028ebfedea0SLionel Sambuc err:
3029ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
3030ebfedea0SLionel Sambuc BN_CTX_free(bn_ctx);
3031*0a6a1f1dSLionel Sambuc if (encodedPoint != NULL)
3032*0a6a1f1dSLionel Sambuc OPENSSL_free(encodedPoint);
3033ebfedea0SLionel Sambuc if (clnt_ecdh != NULL)
3034ebfedea0SLionel Sambuc EC_KEY_free(clnt_ecdh);
3035ebfedea0SLionel Sambuc EVP_PKEY_free(srvr_pub_pkey);
3036ebfedea0SLionel Sambuc #endif
3037*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3038ebfedea0SLionel Sambuc return (-1);
3039ebfedea0SLionel Sambuc }
3040ebfedea0SLionel Sambuc
ssl3_send_client_verify(SSL * s)3041ebfedea0SLionel Sambuc int ssl3_send_client_verify(SSL *s)
3042ebfedea0SLionel Sambuc {
3043ebfedea0SLionel Sambuc unsigned char *p, *d;
3044ebfedea0SLionel Sambuc unsigned char data[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
3045ebfedea0SLionel Sambuc EVP_PKEY *pkey;
3046ebfedea0SLionel Sambuc EVP_PKEY_CTX *pctx = NULL;
3047ebfedea0SLionel Sambuc EVP_MD_CTX mctx;
3048ebfedea0SLionel Sambuc unsigned u = 0;
3049ebfedea0SLionel Sambuc unsigned long n;
3050ebfedea0SLionel Sambuc int j;
3051ebfedea0SLionel Sambuc
3052ebfedea0SLionel Sambuc EVP_MD_CTX_init(&mctx);
3053ebfedea0SLionel Sambuc
3054*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_CERT_VRFY_A) {
3055ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
3056ebfedea0SLionel Sambuc p = &(d[4]);
3057ebfedea0SLionel Sambuc pkey = s->cert->key->privatekey;
3058ebfedea0SLionel Sambuc /* Create context from key and test if sha1 is allowed as digest */
3059ebfedea0SLionel Sambuc pctx = EVP_PKEY_CTX_new(pkey, NULL);
3060ebfedea0SLionel Sambuc EVP_PKEY_sign_init(pctx);
3061*0a6a1f1dSLionel Sambuc if (EVP_PKEY_CTX_set_signature_md(pctx, EVP_sha1()) > 0) {
3062ebfedea0SLionel Sambuc if (TLS1_get_version(s) < TLS1_2_VERSION)
3063ebfedea0SLionel Sambuc s->method->ssl3_enc->cert_verify_mac(s,
3064ebfedea0SLionel Sambuc NID_sha1,
3065*0a6a1f1dSLionel Sambuc &(data
3066*0a6a1f1dSLionel Sambuc [MD5_DIGEST_LENGTH]));
3067*0a6a1f1dSLionel Sambuc } else {
3068ebfedea0SLionel Sambuc ERR_clear_error();
3069ebfedea0SLionel Sambuc }
3070*0a6a1f1dSLionel Sambuc /*
3071*0a6a1f1dSLionel Sambuc * For TLS v1.2 send signature algorithm and signature using agreed
3072*0a6a1f1dSLionel Sambuc * digest and cached handshake records.
3073ebfedea0SLionel Sambuc */
3074*0a6a1f1dSLionel Sambuc if (TLS1_get_version(s) >= TLS1_2_VERSION) {
3075ebfedea0SLionel Sambuc long hdatalen = 0;
3076ebfedea0SLionel Sambuc void *hdata;
3077ebfedea0SLionel Sambuc const EVP_MD *md = s->cert->key->digest;
3078*0a6a1f1dSLionel Sambuc hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
3079*0a6a1f1dSLionel Sambuc if (hdatalen <= 0 || !tls12_get_sigandhash(p, pkey, md)) {
3080*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3081ebfedea0SLionel Sambuc goto err;
3082ebfedea0SLionel Sambuc }
3083ebfedea0SLionel Sambuc p += 2;
3084ebfedea0SLionel Sambuc #ifdef SSL_DEBUG
3085ebfedea0SLionel Sambuc fprintf(stderr, "Using TLS 1.2 with client alg %s\n",
3086ebfedea0SLionel Sambuc EVP_MD_name(md));
3087ebfedea0SLionel Sambuc #endif
3088ebfedea0SLionel Sambuc if (!EVP_SignInit_ex(&mctx, md, NULL)
3089ebfedea0SLionel Sambuc || !EVP_SignUpdate(&mctx, hdata, hdatalen)
3090*0a6a1f1dSLionel Sambuc || !EVP_SignFinal(&mctx, p + 2, &u, pkey)) {
3091*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_EVP_LIB);
3092ebfedea0SLionel Sambuc goto err;
3093ebfedea0SLionel Sambuc }
3094ebfedea0SLionel Sambuc s2n(u, p);
3095ebfedea0SLionel Sambuc n = u + 4;
3096ebfedea0SLionel Sambuc if (!ssl3_digest_cached_records(s))
3097ebfedea0SLionel Sambuc goto err;
3098*0a6a1f1dSLionel Sambuc } else
3099ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
3100*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_RSA) {
3101*0a6a1f1dSLionel Sambuc s->method->ssl3_enc->cert_verify_mac(s, NID_md5, &(data[0]));
3102ebfedea0SLionel Sambuc if (RSA_sign(NID_md5_sha1, data,
3103ebfedea0SLionel Sambuc MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH,
3104*0a6a1f1dSLionel Sambuc &(p[2]), &u, pkey->pkey.rsa) <= 0) {
3105ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_RSA_LIB);
3106ebfedea0SLionel Sambuc goto err;
3107ebfedea0SLionel Sambuc }
3108ebfedea0SLionel Sambuc s2n(u, p);
3109ebfedea0SLionel Sambuc n = u + 2;
3110*0a6a1f1dSLionel Sambuc } else
3111ebfedea0SLionel Sambuc #endif
3112ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
3113*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_DSA) {
3114ebfedea0SLionel Sambuc if (!DSA_sign(pkey->save_type,
3115ebfedea0SLionel Sambuc &(data[MD5_DIGEST_LENGTH]),
3116ebfedea0SLionel Sambuc SHA_DIGEST_LENGTH, &(p[2]),
3117*0a6a1f1dSLionel Sambuc (unsigned int *)&j, pkey->pkey.dsa)) {
3118ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_DSA_LIB);
3119ebfedea0SLionel Sambuc goto err;
3120ebfedea0SLionel Sambuc }
3121ebfedea0SLionel Sambuc s2n(j, p);
3122ebfedea0SLionel Sambuc n = j + 2;
3123*0a6a1f1dSLionel Sambuc } else
3124ebfedea0SLionel Sambuc #endif
3125ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDSA
3126*0a6a1f1dSLionel Sambuc if (pkey->type == EVP_PKEY_EC) {
3127ebfedea0SLionel Sambuc if (!ECDSA_sign(pkey->save_type,
3128ebfedea0SLionel Sambuc &(data[MD5_DIGEST_LENGTH]),
3129ebfedea0SLionel Sambuc SHA_DIGEST_LENGTH, &(p[2]),
3130*0a6a1f1dSLionel Sambuc (unsigned int *)&j, pkey->pkey.ec)) {
3131*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_ECDSA_LIB);
3132ebfedea0SLionel Sambuc goto err;
3133ebfedea0SLionel Sambuc }
3134ebfedea0SLionel Sambuc s2n(j, p);
3135ebfedea0SLionel Sambuc n = j + 2;
3136*0a6a1f1dSLionel Sambuc } else
3137ebfedea0SLionel Sambuc #endif
3138*0a6a1f1dSLionel Sambuc if (pkey->type == NID_id_GostR3410_94
3139*0a6a1f1dSLionel Sambuc || pkey->type == NID_id_GostR3410_2001) {
3140ebfedea0SLionel Sambuc unsigned char signbuf[64];
3141ebfedea0SLionel Sambuc int i;
3142ebfedea0SLionel Sambuc size_t sigsize = 64;
3143ebfedea0SLionel Sambuc s->method->ssl3_enc->cert_verify_mac(s,
3144*0a6a1f1dSLionel Sambuc NID_id_GostR3411_94, data);
3145ebfedea0SLionel Sambuc if (EVP_PKEY_sign(pctx, signbuf, &sigsize, data, 32) <= 0) {
3146*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3147ebfedea0SLionel Sambuc goto err;
3148ebfedea0SLionel Sambuc }
3149ebfedea0SLionel Sambuc for (i = 63, j = 0; i >= 0; j++, i--) {
3150ebfedea0SLionel Sambuc p[2 + j] = signbuf[i];
3151ebfedea0SLionel Sambuc }
3152ebfedea0SLionel Sambuc s2n(j, p);
3153ebfedea0SLionel Sambuc n = j + 2;
3154*0a6a1f1dSLionel Sambuc } else {
3155ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY, ERR_R_INTERNAL_ERROR);
3156ebfedea0SLionel Sambuc goto err;
3157ebfedea0SLionel Sambuc }
3158ebfedea0SLionel Sambuc *(d++) = SSL3_MT_CERTIFICATE_VERIFY;
3159ebfedea0SLionel Sambuc l2n3(n, d);
3160ebfedea0SLionel Sambuc
3161ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_VRFY_B;
3162ebfedea0SLionel Sambuc s->init_num = (int)n + 4;
3163ebfedea0SLionel Sambuc s->init_off = 0;
3164ebfedea0SLionel Sambuc }
3165ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
3166ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(pctx);
3167ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3168ebfedea0SLionel Sambuc err:
3169ebfedea0SLionel Sambuc EVP_MD_CTX_cleanup(&mctx);
3170ebfedea0SLionel Sambuc EVP_PKEY_CTX_free(pctx);
3171*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3172ebfedea0SLionel Sambuc return (-1);
3173ebfedea0SLionel Sambuc }
3174ebfedea0SLionel Sambuc
ssl3_send_client_certificate(SSL * s)3175ebfedea0SLionel Sambuc int ssl3_send_client_certificate(SSL *s)
3176ebfedea0SLionel Sambuc {
3177ebfedea0SLionel Sambuc X509 *x509 = NULL;
3178ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
3179ebfedea0SLionel Sambuc int i;
3180ebfedea0SLionel Sambuc unsigned long l;
3181ebfedea0SLionel Sambuc
3182*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_CERT_A) {
3183ebfedea0SLionel Sambuc if ((s->cert == NULL) ||
3184ebfedea0SLionel Sambuc (s->cert->key->x509 == NULL) ||
3185ebfedea0SLionel Sambuc (s->cert->key->privatekey == NULL))
3186ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_B;
3187ebfedea0SLionel Sambuc else
3188ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_C;
3189ebfedea0SLionel Sambuc }
3190ebfedea0SLionel Sambuc
3191ebfedea0SLionel Sambuc /* We need to get a client cert */
3192*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_CERT_B) {
3193*0a6a1f1dSLionel Sambuc /*
3194*0a6a1f1dSLionel Sambuc * If we get an error, we need to ssl->rwstate=SSL_X509_LOOKUP;
3195*0a6a1f1dSLionel Sambuc * return(-1); We then get retied later
3196*0a6a1f1dSLionel Sambuc */
3197ebfedea0SLionel Sambuc i = 0;
3198ebfedea0SLionel Sambuc i = ssl_do_client_cert_cb(s, &x509, &pkey);
3199*0a6a1f1dSLionel Sambuc if (i < 0) {
3200ebfedea0SLionel Sambuc s->rwstate = SSL_X509_LOOKUP;
3201ebfedea0SLionel Sambuc return (-1);
3202ebfedea0SLionel Sambuc }
3203ebfedea0SLionel Sambuc s->rwstate = SSL_NOTHING;
3204*0a6a1f1dSLionel Sambuc if ((i == 1) && (pkey != NULL) && (x509 != NULL)) {
3205ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_B;
3206*0a6a1f1dSLionel Sambuc if (!SSL_use_certificate(s, x509) || !SSL_use_PrivateKey(s, pkey))
3207ebfedea0SLionel Sambuc i = 0;
3208*0a6a1f1dSLionel Sambuc } else if (i == 1) {
3209ebfedea0SLionel Sambuc i = 0;
3210*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,
3211*0a6a1f1dSLionel Sambuc SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
3212ebfedea0SLionel Sambuc }
3213ebfedea0SLionel Sambuc
3214*0a6a1f1dSLionel Sambuc if (x509 != NULL)
3215*0a6a1f1dSLionel Sambuc X509_free(x509);
3216*0a6a1f1dSLionel Sambuc if (pkey != NULL)
3217*0a6a1f1dSLionel Sambuc EVP_PKEY_free(pkey);
3218*0a6a1f1dSLionel Sambuc if (i == 0) {
3219*0a6a1f1dSLionel Sambuc if (s->version == SSL3_VERSION) {
3220ebfedea0SLionel Sambuc s->s3->tmp.cert_req = 0;
3221ebfedea0SLionel Sambuc ssl3_send_alert(s, SSL3_AL_WARNING, SSL_AD_NO_CERTIFICATE);
3222ebfedea0SLionel Sambuc return (1);
3223*0a6a1f1dSLionel Sambuc } else {
3224ebfedea0SLionel Sambuc s->s3->tmp.cert_req = 2;
3225ebfedea0SLionel Sambuc }
3226ebfedea0SLionel Sambuc }
3227ebfedea0SLionel Sambuc
3228ebfedea0SLionel Sambuc /* Ok, we have a cert */
3229ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_C;
3230ebfedea0SLionel Sambuc }
3231ebfedea0SLionel Sambuc
3232*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_CERT_C) {
3233ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_CERT_D;
3234ebfedea0SLionel Sambuc l = ssl3_output_cert_chain(s,
3235*0a6a1f1dSLionel Sambuc (s->s3->tmp.cert_req ==
3236*0a6a1f1dSLionel Sambuc 2) ? NULL : s->cert->key->x509);
3237*0a6a1f1dSLionel Sambuc if (!l) {
3238*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3239*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
3240*0a6a1f1dSLionel Sambuc s->state = SSL_ST_ERR;
3241*0a6a1f1dSLionel Sambuc return 0;
3242*0a6a1f1dSLionel Sambuc }
3243ebfedea0SLionel Sambuc s->init_num = (int)l;
3244ebfedea0SLionel Sambuc s->init_off = 0;
3245ebfedea0SLionel Sambuc }
3246ebfedea0SLionel Sambuc /* SSL3_ST_CW_CERT_D */
3247ebfedea0SLionel Sambuc return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3248ebfedea0SLionel Sambuc }
3249ebfedea0SLionel Sambuc
3250ebfedea0SLionel Sambuc #define has_bits(i,m) (((i)&(m)) == (m))
3251ebfedea0SLionel Sambuc
ssl3_check_cert_and_algorithm(SSL * s)3252ebfedea0SLionel Sambuc int ssl3_check_cert_and_algorithm(SSL *s)
3253ebfedea0SLionel Sambuc {
3254ebfedea0SLionel Sambuc int i, idx;
3255ebfedea0SLionel Sambuc long alg_k, alg_a;
3256ebfedea0SLionel Sambuc EVP_PKEY *pkey = NULL;
3257*0a6a1f1dSLionel Sambuc int pkey_bits;
3258ebfedea0SLionel Sambuc SESS_CERT *sc;
3259ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
3260ebfedea0SLionel Sambuc RSA *rsa;
3261ebfedea0SLionel Sambuc #endif
3262ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
3263ebfedea0SLionel Sambuc DH *dh;
3264ebfedea0SLionel Sambuc #endif
3265*0a6a1f1dSLionel Sambuc int al = SSL_AD_HANDSHAKE_FAILURE;
3266ebfedea0SLionel Sambuc
3267ebfedea0SLionel Sambuc alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
3268ebfedea0SLionel Sambuc alg_a = s->s3->tmp.new_cipher->algorithm_auth;
3269ebfedea0SLionel Sambuc
3270ebfedea0SLionel Sambuc /* we don't have a certificate */
3271ebfedea0SLionel Sambuc if ((alg_a & (SSL_aDH | SSL_aNULL | SSL_aKRB5)) || (alg_k & SSL_kPSK))
3272ebfedea0SLionel Sambuc return (1);
3273ebfedea0SLionel Sambuc
3274ebfedea0SLionel Sambuc sc = s->session->sess_cert;
3275*0a6a1f1dSLionel Sambuc if (sc == NULL) {
3276ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3277ebfedea0SLionel Sambuc goto err;
3278ebfedea0SLionel Sambuc }
3279ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
3280ebfedea0SLionel Sambuc rsa = s->session->sess_cert->peer_rsa_tmp;
3281ebfedea0SLionel Sambuc #endif
3282ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
3283ebfedea0SLionel Sambuc dh = s->session->sess_cert->peer_dh_tmp;
3284ebfedea0SLionel Sambuc #endif
3285ebfedea0SLionel Sambuc
3286ebfedea0SLionel Sambuc /* This is the passed certificate */
3287ebfedea0SLionel Sambuc
3288ebfedea0SLionel Sambuc idx = sc->peer_cert_type;
3289ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ECDH
3290*0a6a1f1dSLionel Sambuc if (idx == SSL_PKEY_ECC) {
3291*0a6a1f1dSLionel Sambuc if (ssl_check_srvr_ecc_cert_and_alg(sc->peer_pkeys[idx].x509, s) == 0) {
3292*0a6a1f1dSLionel Sambuc /* check failed */
3293ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_BAD_ECC_CERT);
3294ebfedea0SLionel Sambuc goto f_err;
3295*0a6a1f1dSLionel Sambuc } else {
3296ebfedea0SLionel Sambuc return 1;
3297ebfedea0SLionel Sambuc }
3298ebfedea0SLionel Sambuc }
3299ebfedea0SLionel Sambuc #endif
3300ebfedea0SLionel Sambuc pkey = X509_get_pubkey(sc->peer_pkeys[idx].x509);
3301*0a6a1f1dSLionel Sambuc pkey_bits = EVP_PKEY_bits(pkey);
3302ebfedea0SLionel Sambuc i = X509_certificate_type(sc->peer_pkeys[idx].x509, pkey);
3303ebfedea0SLionel Sambuc EVP_PKEY_free(pkey);
3304ebfedea0SLionel Sambuc
3305ebfedea0SLionel Sambuc /* Check that we have a certificate if we require one */
3306*0a6a1f1dSLionel Sambuc if ((alg_a & SSL_aRSA) && !has_bits(i, EVP_PK_RSA | EVP_PKT_SIGN)) {
3307*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3308*0a6a1f1dSLionel Sambuc SSL_R_MISSING_RSA_SIGNING_CERT);
3309ebfedea0SLionel Sambuc goto f_err;
3310ebfedea0SLionel Sambuc }
3311ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DSA
3312*0a6a1f1dSLionel Sambuc else if ((alg_a & SSL_aDSS) && !has_bits(i, EVP_PK_DSA | EVP_PKT_SIGN)) {
3313*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3314*0a6a1f1dSLionel Sambuc SSL_R_MISSING_DSA_SIGNING_CERT);
3315ebfedea0SLionel Sambuc goto f_err;
3316ebfedea0SLionel Sambuc }
3317ebfedea0SLionel Sambuc #endif
3318ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_RSA
3319*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kRSA) {
3320*0a6a1f1dSLionel Sambuc if (!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3321*0a6a1f1dSLionel Sambuc !has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3322*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3323*0a6a1f1dSLionel Sambuc SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3324ebfedea0SLionel Sambuc goto f_err;
3325*0a6a1f1dSLionel Sambuc } else if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)) {
3326*0a6a1f1dSLionel Sambuc if (pkey_bits <= SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3327*0a6a1f1dSLionel Sambuc if (!has_bits(i, EVP_PK_RSA | EVP_PKT_ENC)) {
3328*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3329*0a6a1f1dSLionel Sambuc SSL_R_MISSING_RSA_ENCRYPTING_CERT);
3330*0a6a1f1dSLionel Sambuc goto f_err;
3331*0a6a1f1dSLionel Sambuc }
3332*0a6a1f1dSLionel Sambuc if (rsa != NULL) {
3333*0a6a1f1dSLionel Sambuc /* server key exchange is not allowed. */
3334*0a6a1f1dSLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3335*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3336*0a6a1f1dSLionel Sambuc goto f_err;
3337*0a6a1f1dSLionel Sambuc }
3338*0a6a1f1dSLionel Sambuc }
3339*0a6a1f1dSLionel Sambuc }
3340ebfedea0SLionel Sambuc }
3341ebfedea0SLionel Sambuc #endif
3342ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
3343*0a6a1f1dSLionel Sambuc if ((alg_k & SSL_kEDH) && dh == NULL) {
3344*0a6a1f1dSLionel Sambuc al = SSL_AD_INTERNAL_ERROR;
3345*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, ERR_R_INTERNAL_ERROR);
3346ebfedea0SLionel Sambuc goto f_err;
3347ebfedea0SLionel Sambuc }
3348*0a6a1f1dSLionel Sambuc if ((alg_k & SSL_kDHr) && !has_bits(i, EVP_PK_DH | EVP_PKS_RSA)) {
3349*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3350*0a6a1f1dSLionel Sambuc SSL_R_MISSING_DH_RSA_CERT);
3351ebfedea0SLionel Sambuc goto f_err;
3352ebfedea0SLionel Sambuc }
3353ebfedea0SLionel Sambuc # ifndef OPENSSL_NO_DSA
3354*0a6a1f1dSLionel Sambuc if ((alg_k & SSL_kDHd) && !has_bits(i, EVP_PK_DH | EVP_PKS_DSA)) {
3355*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3356*0a6a1f1dSLionel Sambuc SSL_R_MISSING_DH_DSA_CERT);
3357ebfedea0SLionel Sambuc goto f_err;
3358ebfedea0SLionel Sambuc }
3359ebfedea0SLionel Sambuc # endif
3360ebfedea0SLionel Sambuc
3361*0a6a1f1dSLionel Sambuc /* Check DHE only: static DH not implemented. */
3362*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kEDH) {
3363*0a6a1f1dSLionel Sambuc int dh_size = BN_num_bits(dh->p);
3364*0a6a1f1dSLionel Sambuc if ((!SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 768)
3365*0a6a1f1dSLionel Sambuc || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) && dh_size < 512)) {
3366*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM, SSL_R_DH_KEY_TOO_SMALL);
3367ebfedea0SLionel Sambuc goto f_err;
3368ebfedea0SLionel Sambuc }
3369ebfedea0SLionel Sambuc }
3370*0a6a1f1dSLionel Sambuc #endif /* !OPENSSL_NO_DH */
3371*0a6a1f1dSLionel Sambuc
3372*0a6a1f1dSLionel Sambuc if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
3373*0a6a1f1dSLionel Sambuc pkey_bits > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3374*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_RSA
3375*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kRSA) {
3376*0a6a1f1dSLionel Sambuc if (rsa == NULL) {
3377*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3378*0a6a1f1dSLionel Sambuc SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3379*0a6a1f1dSLionel Sambuc goto f_err;
3380*0a6a1f1dSLionel Sambuc } else if (BN_num_bits(rsa->n) >
3381*0a6a1f1dSLionel Sambuc SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3382*0a6a1f1dSLionel Sambuc /* We have a temporary RSA key but it's too large. */
3383*0a6a1f1dSLionel Sambuc al = SSL_AD_EXPORT_RESTRICTION;
3384*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3385*0a6a1f1dSLionel Sambuc SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
3386*0a6a1f1dSLionel Sambuc goto f_err;
3387*0a6a1f1dSLionel Sambuc }
3388*0a6a1f1dSLionel Sambuc } else
3389ebfedea0SLionel Sambuc #endif
3390ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_DH
3391*0a6a1f1dSLionel Sambuc if (alg_k & SSL_kEDH) {
3392*0a6a1f1dSLionel Sambuc if (BN_num_bits(dh->p) >
3393*0a6a1f1dSLionel Sambuc SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)) {
3394*0a6a1f1dSLionel Sambuc /* We have a temporary DH key but it's too large. */
3395*0a6a1f1dSLionel Sambuc al = SSL_AD_EXPORT_RESTRICTION;
3396*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3397*0a6a1f1dSLionel Sambuc SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3398ebfedea0SLionel Sambuc goto f_err;
3399ebfedea0SLionel Sambuc }
3400*0a6a1f1dSLionel Sambuc } else if (alg_k & (SSL_kDHr | SSL_kDHd)) {
3401*0a6a1f1dSLionel Sambuc /* The cert should have had an export DH key. */
3402*0a6a1f1dSLionel Sambuc al = SSL_AD_EXPORT_RESTRICTION;
3403*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3404*0a6a1f1dSLionel Sambuc SSL_R_MISSING_EXPORT_TMP_DH_KEY);
3405*0a6a1f1dSLionel Sambuc goto f_err;
3406*0a6a1f1dSLionel Sambuc } else
3407ebfedea0SLionel Sambuc #endif
3408ebfedea0SLionel Sambuc {
3409*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,
3410*0a6a1f1dSLionel Sambuc SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
3411ebfedea0SLionel Sambuc goto f_err;
3412ebfedea0SLionel Sambuc }
3413ebfedea0SLionel Sambuc }
3414ebfedea0SLionel Sambuc return (1);
3415ebfedea0SLionel Sambuc f_err:
3416*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, al);
3417ebfedea0SLionel Sambuc err:
3418ebfedea0SLionel Sambuc return (0);
3419ebfedea0SLionel Sambuc }
3420ebfedea0SLionel Sambuc
3421*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_TLSEXT
3422*0a6a1f1dSLionel Sambuc /*
3423*0a6a1f1dSLionel Sambuc * Normally, we can tell if the server is resuming the session from
3424*0a6a1f1dSLionel Sambuc * the session ID. EAP-FAST (RFC 4851), however, relies on the next server
3425*0a6a1f1dSLionel Sambuc * message after the ServerHello to determine if the server is resuming.
3426*0a6a1f1dSLionel Sambuc * Therefore, we allow EAP-FAST to peek ahead.
3427*0a6a1f1dSLionel Sambuc * ssl3_check_finished returns 1 if we are resuming from an external
3428*0a6a1f1dSLionel Sambuc * pre-shared secret, we have a "ticket" and the next server handshake message
3429*0a6a1f1dSLionel Sambuc * is Finished; and 0 otherwise. It returns -1 upon an error.
3430*0a6a1f1dSLionel Sambuc */
ssl3_check_finished(SSL * s)3431*0a6a1f1dSLionel Sambuc static int ssl3_check_finished(SSL *s)
3432*0a6a1f1dSLionel Sambuc {
3433*0a6a1f1dSLionel Sambuc int ok = 0;
3434*0a6a1f1dSLionel Sambuc
3435*0a6a1f1dSLionel Sambuc if (s->version < TLS1_VERSION || !s->tls_session_secret_cb ||
3436*0a6a1f1dSLionel Sambuc !s->session->tlsext_tick)
3437*0a6a1f1dSLionel Sambuc return 0;
3438*0a6a1f1dSLionel Sambuc
3439*0a6a1f1dSLionel Sambuc /* Need to permit this temporarily, in case the next message is Finished. */
3440*0a6a1f1dSLionel Sambuc s->s3->flags |= SSL3_FLAGS_CCS_OK;
3441*0a6a1f1dSLionel Sambuc /*
3442*0a6a1f1dSLionel Sambuc * This function is called when we might get a Certificate message instead,
3443*0a6a1f1dSLionel Sambuc * so permit appropriate message length.
3444*0a6a1f1dSLionel Sambuc * We ignore the return value as we're only interested in the message type
3445*0a6a1f1dSLionel Sambuc * and not its length.
3446*0a6a1f1dSLionel Sambuc */
3447*0a6a1f1dSLionel Sambuc s->method->ssl_get_message(s,
3448*0a6a1f1dSLionel Sambuc SSL3_ST_CR_CERT_A,
3449*0a6a1f1dSLionel Sambuc SSL3_ST_CR_CERT_B,
3450*0a6a1f1dSLionel Sambuc -1, s->max_cert_list, &ok);
3451*0a6a1f1dSLionel Sambuc s->s3->flags &= ~SSL3_FLAGS_CCS_OK;
3452*0a6a1f1dSLionel Sambuc
3453*0a6a1f1dSLionel Sambuc if (!ok)
3454*0a6a1f1dSLionel Sambuc return -1;
3455*0a6a1f1dSLionel Sambuc
3456*0a6a1f1dSLionel Sambuc s->s3->tmp.reuse_message = 1;
3457*0a6a1f1dSLionel Sambuc
3458*0a6a1f1dSLionel Sambuc if (s->s3->tmp.message_type == SSL3_MT_FINISHED)
3459*0a6a1f1dSLionel Sambuc return 1;
3460*0a6a1f1dSLionel Sambuc
3461*0a6a1f1dSLionel Sambuc /* If we're not done, then the CCS arrived early and we should bail. */
3462*0a6a1f1dSLionel Sambuc if (s->s3->change_cipher_spec) {
3463*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL3_CHECK_FINISHED, SSL_R_CCS_RECEIVED_EARLY);
3464*0a6a1f1dSLionel Sambuc ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
3465*0a6a1f1dSLionel Sambuc return -1;
3466*0a6a1f1dSLionel Sambuc }
3467*0a6a1f1dSLionel Sambuc
3468*0a6a1f1dSLionel Sambuc return 0;
3469*0a6a1f1dSLionel Sambuc }
3470*0a6a1f1dSLionel Sambuc
3471*0a6a1f1dSLionel Sambuc # ifndef OPENSSL_NO_NEXTPROTONEG
ssl3_send_next_proto(SSL * s)3472ebfedea0SLionel Sambuc int ssl3_send_next_proto(SSL *s)
3473ebfedea0SLionel Sambuc {
3474ebfedea0SLionel Sambuc unsigned int len, padding_len;
3475ebfedea0SLionel Sambuc unsigned char *d;
3476ebfedea0SLionel Sambuc
3477*0a6a1f1dSLionel Sambuc if (s->state == SSL3_ST_CW_NEXT_PROTO_A) {
3478ebfedea0SLionel Sambuc len = s->next_proto_negotiated_len;
3479ebfedea0SLionel Sambuc padding_len = 32 - ((len + 2) % 32);
3480ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
3481ebfedea0SLionel Sambuc d[4] = len;
3482ebfedea0SLionel Sambuc memcpy(d + 5, s->next_proto_negotiated, len);
3483ebfedea0SLionel Sambuc d[5 + len] = padding_len;
3484ebfedea0SLionel Sambuc memset(d + 6 + len, 0, padding_len);
3485ebfedea0SLionel Sambuc *(d++) = SSL3_MT_NEXT_PROTO;
3486ebfedea0SLionel Sambuc l2n3(2 + len + padding_len, d);
3487ebfedea0SLionel Sambuc s->state = SSL3_ST_CW_NEXT_PROTO_B;
3488ebfedea0SLionel Sambuc s->init_num = 4 + 2 + len + padding_len;
3489ebfedea0SLionel Sambuc s->init_off = 0;
3490ebfedea0SLionel Sambuc }
3491ebfedea0SLionel Sambuc
3492ebfedea0SLionel Sambuc return ssl3_do_write(s, SSL3_RT_HANDSHAKE);
3493ebfedea0SLionel Sambuc }
3494*0a6a1f1dSLionel Sambuc #endif /* !OPENSSL_NO_NEXTPROTONEG */
3495*0a6a1f1dSLionel Sambuc #endif /* !OPENSSL_NO_TLSEXT */
3496ebfedea0SLionel Sambuc
ssl_do_client_cert_cb(SSL * s,X509 ** px509,EVP_PKEY ** ppkey)3497ebfedea0SLionel Sambuc int ssl_do_client_cert_cb(SSL *s, X509 **px509, EVP_PKEY **ppkey)
3498ebfedea0SLionel Sambuc {
3499ebfedea0SLionel Sambuc int i = 0;
3500ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
3501*0a6a1f1dSLionel Sambuc if (s->ctx->client_cert_engine) {
3502ebfedea0SLionel Sambuc i = ENGINE_load_ssl_client_cert(s->ctx->client_cert_engine, s,
3503ebfedea0SLionel Sambuc SSL_get_client_CA_list(s),
3504ebfedea0SLionel Sambuc px509, ppkey, NULL, NULL, NULL);
3505ebfedea0SLionel Sambuc if (i != 0)
3506ebfedea0SLionel Sambuc return i;
3507ebfedea0SLionel Sambuc }
3508ebfedea0SLionel Sambuc #endif
3509ebfedea0SLionel Sambuc if (s->ctx->client_cert_cb)
3510ebfedea0SLionel Sambuc i = s->ctx->client_cert_cb(s, px509, ppkey);
3511ebfedea0SLionel Sambuc return i;
3512ebfedea0SLionel Sambuc }
3513