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