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