]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/s3_srvr.c
free NULL cleanup
[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
MC
229 if (!SSL_in_init(s) || SSL_in_before(s)) {
230 if(!SSL_clear(s))
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) {
77d514c5
MC
885 if(!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) {
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
934 /*
935 * use version from inside client hello, not from record header (may
936 * differ: see RFC 2246, Appendix E, second paragraph)
937 */
938 s->client_version = (((int)p[0]) << 8) | (int)p[1];
939 p += 2;
940
941 if (SSL_IS_DTLS(s) ? (s->client_version > s->version &&
942 s->method->version != DTLS_ANY_VERSION)
943 : (s->client_version < s->version)) {
944 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
945 if ((s->client_version >> 8) == SSL3_VERSION_MAJOR &&
946 !s->enc_write_ctx && !s->write_hash) {
947 /*
948 * similar to ssl3_get_record, send alert using remote version
949 * number
950 */
951 s->version = s->client_version;
952 }
953 al = SSL_AD_PROTOCOL_VERSION;
954 goto f_err;
955 }
956
957 /*
958 * If we require cookies and this ClientHello doesn't contain one, just
959 * return since we do not want to allocate any memory yet. So check
960 * cookie length...
961 */
962 if (SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) {
963 unsigned int session_length, cookie_length;
964
965 session_length = *(p + SSL3_RANDOM_SIZE);
966 cookie_length = *(p + SSL3_RANDOM_SIZE + session_length + 1);
967
968 if (cookie_length == 0)
969 return 1;
970 }
971
972 /* load the client random */
973 memcpy(s->s3->client_random, p, SSL3_RANDOM_SIZE);
974 p += SSL3_RANDOM_SIZE;
975
976 /* get the session-id */
977 j = *(p++);
978
979 s->hit = 0;
980 /*
981 * Versions before 0.9.7 always allow clients to resume sessions in
982 * renegotiation. 0.9.7 and later allow this by default, but optionally
983 * ignore resumption requests with flag
984 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION (it's a new flag rather
985 * than a change to default behavior so that applications relying on this
986 * for security won't even compile against older library versions).
987 * 1.0.1 and later also have a function SSL_renegotiate_abbreviated() to
988 * request renegotiation but not a new session (s->new_session remains
989 * unset): for servers, this essentially just means that the
990 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION setting will be ignored.
991 */
992 if ((s->new_session
993 && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION))) {
994 if (!ssl_get_new_session(s, 1))
995 goto err;
996 } else {
997 i = ssl_get_prev_session(s, p, j, d + n);
998 /*
999 * Only resume if the session's version matches the negotiated
1000 * version.
1001 * RFC 5246 does not provide much useful advice on resumption
1002 * with a different protocol version. It doesn't forbid it but
1003 * the sanity of such behaviour would be questionable.
1004 * In practice, clients do not accept a version mismatch and
1005 * will abort the handshake with an error.
1006 */
1007 if (i == 1 && s->version == s->session->ssl_version) { /* previous
1008 * session */
1009 s->hit = 1;
1010 } else if (i == -1)
1011 goto err;
1012 else { /* i == 0 */
1013
1014 if (!ssl_get_new_session(s, 1))
1015 goto err;
1016 }
1017 }
1018
1019 p += j;
1020
1021 if (SSL_IS_DTLS(s)) {
1022 /* cookie stuff */
1023 cookie_len = *(p++);
1024
1025 /*
1026 * The ClientHello may contain a cookie even if the
1027 * HelloVerify message has not been sent--make sure that it
1028 * does not cause an overflow.
1029 */
1030 if (cookie_len > sizeof(s->d1->rcvd_cookie)) {
1031 /* too much data */
1032 al = SSL_AD_DECODE_ERROR;
1033 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1034 goto f_err;
1035 }
1036
1037 /* verify the cookie if appropriate option is set. */
1038 if ((SSL_get_options(s) & SSL_OP_COOKIE_EXCHANGE) && cookie_len > 0) {
1039 memcpy(s->d1->rcvd_cookie, p, cookie_len);
1040
1041 if (s->ctx->app_verify_cookie_cb != NULL) {
1042 if (s->ctx->app_verify_cookie_cb(s, s->d1->rcvd_cookie,
1043 cookie_len) == 0) {
1044 al = SSL_AD_HANDSHAKE_FAILURE;
1045 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1046 SSL_R_COOKIE_MISMATCH);
1047 goto f_err;
1048 }
1049 /* else cookie verification succeeded */
1050 }
1051 /* default verification */
1052 else if (memcmp(s->d1->rcvd_cookie, s->d1->cookie,
1053 s->d1->cookie_len) != 0) {
1054 al = SSL_AD_HANDSHAKE_FAILURE;
1055 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_COOKIE_MISMATCH);
1056 goto f_err;
1057 }
1058 /* Set to -2 so if successful we return 2 */
1059 ret = -2;
1060 }
1061
1062 p += cookie_len;
1063 if (s->method->version == DTLS_ANY_VERSION) {
1064 /* Select version to use */
1065 if (s->client_version <= DTLS1_2_VERSION &&
1066 !(s->options & SSL_OP_NO_DTLSv1_2)) {
1067 s->version = DTLS1_2_VERSION;
1068 s->method = DTLSv1_2_server_method();
1069 } else if (tls1_suiteb(s)) {
1070 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1071 SSL_R_ONLY_DTLS_1_2_ALLOWED_IN_SUITEB_MODE);
1072 s->version = s->client_version;
1073 al = SSL_AD_PROTOCOL_VERSION;
1074 goto f_err;
1075 } else if (s->client_version <= DTLS1_VERSION &&
1076 !(s->options & SSL_OP_NO_DTLSv1)) {
1077 s->version = DTLS1_VERSION;
1078 s->method = DTLSv1_server_method();
1079 } else {
1080 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1081 SSL_R_WRONG_VERSION_NUMBER);
1082 s->version = s->client_version;
1083 al = SSL_AD_PROTOCOL_VERSION;
1084 goto f_err;
1085 }
1086 s->session->ssl_version = s->version;
1087 }
1088 }
1089
1090 n2s(p, i);
1091 if ((i == 0) && (j != 0)) {
1092 /* we need a cipher if we are not resuming a session */
1093 al = SSL_AD_ILLEGAL_PARAMETER;
1094 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_SPECIFIED);
1095 goto f_err;
1096 }
1097 if ((p + i) >= (d + n)) {
1098 /* not enough data */
1099 al = SSL_AD_DECODE_ERROR;
1100 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1101 goto f_err;
1102 }
1103 if ((i > 0) && (ssl_bytes_to_cipher_list(s, p, i, &(ciphers))
1104 == NULL)) {
1105 goto err;
1106 }
1107 p += i;
1108
1109 /* If it is a hit, check that the cipher is in the list */
1110 if ((s->hit) && (i > 0)) {
1111 j = 0;
1112 id = s->session->cipher->id;
d02b48c6 1113
413c4f45 1114#ifdef CIPHER_DEBUG
0f113f3e
MC
1115 fprintf(stderr, "client sent %d ciphers\n",
1116 sk_SSL_CIPHER_num(ciphers));
413c4f45 1117#endif
0f113f3e
MC
1118 for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
1119 c = sk_SSL_CIPHER_value(ciphers, i);
413c4f45 1120#ifdef CIPHER_DEBUG
0f113f3e
MC
1121 fprintf(stderr, "client [%2d of %2d]:%s\n",
1122 i, sk_SSL_CIPHER_num(ciphers), SSL_CIPHER_get_name(c));
413c4f45 1123#endif
0f113f3e
MC
1124 if (c->id == id) {
1125 j = 1;
1126 break;
1127 }
1128 }
1129 /*
1130 * Disabled because it can be used in a ciphersuite downgrade attack:
1131 * CVE-2010-4180.
1132 */
88f2a4cf 1133#if 0
0f113f3e
MC
1134 if (j == 0 && (s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG)
1135 && (sk_SSL_CIPHER_num(ciphers) == 1)) {
1136 /*
1137 * Special case as client bug workaround: the previously used
1138 * cipher may not be in the current list, the client instead
1139 * might be trying to continue using a cipher that before wasn't
1140 * chosen due to server preferences. We'll have to reject the
1141 * connection if the cipher is not enabled, though.
1142 */
1143 c = sk_SSL_CIPHER_value(ciphers, 0);
1144 if (sk_SSL_CIPHER_find(SSL_get_ciphers(s), c) >= 0) {
1145 s->session->cipher = c;
1146 j = 1;
1147 }
1148 }
88f2a4cf 1149#endif
0f113f3e
MC
1150 if (j == 0) {
1151 /*
1152 * we need to have the cipher in the cipher list if we are asked
1153 * to reuse it
1154 */
1155 al = SSL_AD_ILLEGAL_PARAMETER;
1156 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1157 SSL_R_REQUIRED_CIPHER_MISSING);
1158 goto f_err;
1159 }
1160 }
1161
1162 /* compression */
1163 i = *(p++);
1164 if ((p + i) > (d + n)) {
1165 /* not enough data */
1166 al = SSL_AD_DECODE_ERROR;
1167 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_LENGTH_MISMATCH);
1168 goto f_err;
1169 }
a4c4a7d5 1170#ifndef OPENSSL_NO_COMP
0f113f3e 1171 q = p;
a4c4a7d5 1172#endif
0f113f3e
MC
1173 for (j = 0; j < i; j++) {
1174 if (p[j] == 0)
1175 break;
1176 }
1177
1178 p += i;
1179 if (j >= i) {
1180 /* no compress */
1181 al = SSL_AD_DECODE_ERROR;
1182 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_COMPRESSION_SPECIFIED);
1183 goto f_err;
1184 }
58ece833 1185#ifndef OPENSSL_NO_TLSEXT
0f113f3e
MC
1186 /* TLS extensions */
1187 if (s->version >= SSL3_VERSION) {
1188 if (!ssl_parse_clienthello_tlsext(s, &p, d, n)) {
1189 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_PARSE_TLSEXT);
1190 goto err;
1191 }
1192 }
1193
1194 /*
1195 * Check if we want to use external pre-shared secret for this handshake
1196 * for not reused session only. We need to generate server_random before
1197 * calling tls_session_secret_cb in order to allow SessionTicket
1198 * processing to use it in key derivation.
1199 */
1200 {
1201 unsigned char *pos;
1202 pos = s->s3->server_random;
1203 if (ssl_fill_hello_random(s, 1, pos, SSL3_RANDOM_SIZE) <= 0) {
1204 goto f_err;
1205 }
1206 }
1207
1208 if (!s->hit && s->version >= TLS1_VERSION && s->tls_session_secret_cb) {
1209 SSL_CIPHER *pref_cipher = NULL;
1210
1211 s->session->master_key_length = sizeof(s->session->master_key);
1212 if (s->tls_session_secret_cb(s, s->session->master_key,
1213 &s->session->master_key_length, ciphers,
1214 &pref_cipher,
1215 s->tls_session_secret_cb_arg)) {
1216 s->hit = 1;
1217 s->session->ciphers = ciphers;
1218 s->session->verify_result = X509_V_OK;
1219
1220 ciphers = NULL;
1221
1222 /* check if some cipher was preferred by call back */
1223 pref_cipher =
1224 pref_cipher ? pref_cipher : ssl3_choose_cipher(s,
1225 s->
1226 session->ciphers,
1227 SSL_get_ciphers
1228 (s));
1229 if (pref_cipher == NULL) {
1230 al = SSL_AD_HANDSHAKE_FAILURE;
1231 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1232 goto f_err;
1233 }
1234
1235 s->session->cipher = pref_cipher;
1236
1237 if (s->cipher_list)
1238 sk_SSL_CIPHER_free(s->cipher_list);
1239
1240 if (s->cipher_list_by_id)
1241 sk_SSL_CIPHER_free(s->cipher_list_by_id);
1242
1243 s->cipher_list = sk_SSL_CIPHER_dup(s->session->ciphers);
1244 s->cipher_list_by_id = sk_SSL_CIPHER_dup(s->session->ciphers);
1245 }
1246 }
58ece833
BM
1247#endif
1248
0f113f3e
MC
1249 /*
1250 * Worst case, we will use the NULL compression, but if we have other
1251 * options, we will now look for them. We have i-1 compression
1252 * algorithms from the client, starting at q.
1253 */
1254 s->s3->tmp.new_compression = NULL;
09b6c2ef 1255#ifndef OPENSSL_NO_COMP
0f113f3e
MC
1256 /* This only happens if we have a cache hit */
1257 if (s->session->compress_meth != 0) {
1258 int m, comp_id = s->session->compress_meth;
1259 /* Perform sanity checks on resumed compression algorithm */
1260 /* Can't disable compression */
1261 if (!ssl_allow_compression(s)) {
1262 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1263 SSL_R_INCONSISTENT_COMPRESSION);
1264 goto f_err;
1265 }
1266 /* Look for resumed compression method */
1267 for (m = 0; m < sk_SSL_COMP_num(s->ctx->comp_methods); m++) {
1268 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1269 if (comp_id == comp->id) {
1270 s->s3->tmp.new_compression = comp;
1271 break;
1272 }
1273 }
1274 if (s->s3->tmp.new_compression == NULL) {
1275 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1276 SSL_R_INVALID_COMPRESSION_ALGORITHM);
1277 goto f_err;
1278 }
1279 /* Look for resumed method in compression list */
1280 for (m = 0; m < i; m++) {
1281 if (q[m] == comp_id)
1282 break;
1283 }
1284 if (m >= i) {
1285 al = SSL_AD_ILLEGAL_PARAMETER;
1286 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,
1287 SSL_R_REQUIRED_COMPRESSSION_ALGORITHM_MISSING);
1288 goto f_err;
1289 }
1290 } else if (s->hit)
1291 comp = NULL;
1292 else if (ssl_allow_compression(s) && s->ctx->comp_methods) {
df6741c9 1293 /* See if we have a match */
0f113f3e
MC
1294 int m, nn, o, v, done = 0;
1295
1296 nn = sk_SSL_COMP_num(s->ctx->comp_methods);
1297 for (m = 0; m < nn; m++) {
1298 comp = sk_SSL_COMP_value(s->ctx->comp_methods, m);
1299 v = comp->id;
1300 for (o = 0; o < i; o++) {
1301 if (v == q[o]) {
1302 done = 1;
1303 break;
1304 }
1305 }
1306 if (done)
1307 break;
1308 }
1309 if (done)
1310 s->s3->tmp.new_compression = comp;
1311 else
1312 comp = NULL;
1313 }
e6f418bc 1314#else
0f113f3e
MC
1315 /*
1316 * If compression is disabled we'd better not try to resume a session
1317 * using compression.
1318 */
1319 if (s->session->compress_meth != 0) {
1320 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_INCONSISTENT_COMPRESSION);
1321 goto f_err;
1322 }
09b6c2ef 1323#endif
413c4f45 1324
0f113f3e
MC
1325 /*
1326 * Given s->session->ciphers and SSL_get_ciphers, we must pick a cipher
1327 */
d02b48c6 1328
0f113f3e 1329 if (!s->hit) {
09b6c2ef 1330#ifdef OPENSSL_NO_COMP
0f113f3e 1331 s->session->compress_meth = 0;
09b6c2ef 1332#else
0f113f3e 1333 s->session->compress_meth = (comp == NULL) ? 0 : comp->id;
09b6c2ef 1334#endif
0f113f3e
MC
1335 if (s->session->ciphers != NULL)
1336 sk_SSL_CIPHER_free(s->session->ciphers);
1337 s->session->ciphers = ciphers;
1338 if (ciphers == NULL) {
1339 al = SSL_AD_ILLEGAL_PARAMETER;
1340 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_CIPHERS_PASSED);
1341 goto f_err;
1342 }
1343 ciphers = NULL;
1344 if (!tls1_set_server_sigalgs(s)) {
1345 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1346 goto err;
1347 }
1348 /* Let cert callback update server certificates if required */
1349 retry_cert:
1350 if (s->cert->cert_cb) {
1351 int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg);
1352 if (rv == 0) {
1353 al = SSL_AD_INTERNAL_ERROR;
1354 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CERT_CB_ERROR);
1355 goto f_err;
1356 }
1357 if (rv < 0) {
1358 s->rwstate = SSL_X509_LOOKUP;
1359 return -1;
1360 }
1361 s->rwstate = SSL_NOTHING;
1362 }
1363 c = ssl3_choose_cipher(s, s->session->ciphers, SSL_get_ciphers(s));
1364
1365 if (c == NULL) {
1366 al = SSL_AD_HANDSHAKE_FAILURE;
1367 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_NO_SHARED_CIPHER);
1368 goto f_err;
1369 }
1370 s->s3->tmp.new_cipher = c;
1371 /* check whether we should disable session resumption */
1372 if (s->not_resumable_session_cb != NULL)
1373 s->session->not_resumable = s->not_resumable_session_cb(s,
1374 ((c->algorithm_mkey & (SSL_kDHE | SSL_kECDHE))
1375 != 0));
1376 if (s->session->not_resumable)
1377 /* do not send a session ticket */
1378 s->tlsext_ticket_expected = 0;
1379 } else {
1380 /* Session-id reuse */
7a4dadc3 1381 s->s3->tmp.new_cipher = s->session->cipher;
0f113f3e
MC
1382 }
1383
1384 if (!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) {
1385 if (!ssl3_digest_cached_records(s))
1386 goto f_err;
1387 }
1388
50e735f9
MC
1389 /*-
1390 * we now have the following setup.
1391 * client_random
1392 * cipher_list - our prefered list of ciphers
1393 * ciphers - the clients prefered list of ciphers
1394 * compression - basically ignored right now
1395 * ssl version is set - sslv3
1396 * s->session - The ssl session has been setup.
1397 * s->hit - session reuse flag
1398 * s->s3->tmp.new_cipher- the new cipher to use.
1399 */
0f113f3e
MC
1400
1401 /* Handles TLS extensions that we couldn't check earlier */
1402 if (s->version >= SSL3_VERSION) {
1403 if (ssl_check_clienthello_tlsext_late(s) <= 0) {
1404 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_CLIENTHELLO_TLSEXT);
1405 goto err;
1406 }
1407 }
1408
1409 if (ret < 0)
1410 ret = -ret;
1411 if (0) {
1412 f_err:
1413 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1414 }
1415 err:
1416 if (ciphers != NULL)
1417 sk_SSL_CIPHER_free(ciphers);
1418 return ret < 0 ? -1 : ret;
1419}
d02b48c6 1420
36d16f8e 1421int ssl3_send_server_hello(SSL *s)
0f113f3e
MC
1422{
1423 unsigned char *buf;
1424 unsigned char *p, *d;
1425 int i, sl;
1426 int al = 0;
1427 unsigned long l;
1428
1429 if (s->state == SSL3_ST_SW_SRVR_HELLO_A) {
1430 buf = (unsigned char *)s->init_buf->data;
12bf56c0 1431#ifdef OPENSSL_NO_TLSEXT
0f113f3e
MC
1432 p = s->s3->server_random;
1433 if (ssl_fill_hello_random(s, 1, p, SSL3_RANDOM_SIZE) <= 0)
1434 return -1;
12bf56c0 1435#endif
0f113f3e
MC
1436 /* Do the message type and length last */
1437 d = p = ssl_handshake_start(s);
1438
1439 *(p++) = s->version >> 8;
1440 *(p++) = s->version & 0xff;
1441
1442 /* Random stuff */
1443 memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE);
1444 p += SSL3_RANDOM_SIZE;
1445
50e735f9
MC
1446 /*-
1447 * There are several cases for the session ID to send
1448 * back in the server hello:
1449 * - For session reuse from the session cache,
1450 * we send back the old session ID.
1451 * - If stateless session reuse (using a session ticket)
1452 * is successful, we send back the client's "session ID"
1453 * (which doesn't actually identify the session).
1454 * - If it is a new session, we send back the new
1455 * session ID.
1456 * - However, if we want the new session to be single-use,
1457 * we send back a 0-length session ID.
1458 * s->hit is non-zero in either case of session reuse,
1459 * so the following won't overwrite an ID that we're supposed
1460 * to send back.
1461 */
0f113f3e
MC
1462 if (s->session->not_resumable ||
1463 (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)
1464 && !s->hit))
1465 s->session->session_id_length = 0;
1466
1467 sl = s->session->session_id_length;
1468 if (sl > (int)sizeof(s->session->session_id)) {
1469 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1470 return -1;
1471 }
1472 *(p++) = sl;
1473 memcpy(p, s->session->session_id, sl);
1474 p += sl;
1475
1476 /* put the cipher */
1477 i = ssl3_put_cipher_by_char(s->s3->tmp.new_cipher, p);
1478 p += i;
1479
1480 /* put the compression method */
09b6c2ef 1481#ifdef OPENSSL_NO_COMP
0f113f3e 1482 *(p++) = 0;
09b6c2ef 1483#else
0f113f3e
MC
1484 if (s->s3->tmp.new_compression == NULL)
1485 *(p++) = 0;
1486 else
1487 *(p++) = s->s3->tmp.new_compression->id;
09b6c2ef 1488#endif
ed3883d2 1489#ifndef OPENSSL_NO_TLSEXT
0f113f3e
MC
1490 if (ssl_prepare_serverhello_tlsext(s) <= 0) {
1491 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, SSL_R_SERVERHELLO_TLSEXT);
1492 return -1;
1493 }
1494 if ((p =
1495 ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH,
1496 &al)) == NULL) {
1497 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1498 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1499 return -1;
1500 }
ed3883d2 1501#endif
0f113f3e
MC
1502 /* do the header */
1503 l = (p - d);
77d514c5
MC
1504 if(!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) {
1505 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
1506 return -1;
1507 }
0f113f3e
MC
1508 s->state = SSL3_ST_SW_SRVR_HELLO_B;
1509 }
d02b48c6 1510
0f113f3e
MC
1511 /* SSL3_ST_SW_SRVR_HELLO_B */
1512 return ssl_do_write(s);
1513}
d02b48c6 1514
36d16f8e 1515int ssl3_send_server_done(SSL *s)
0f113f3e 1516{
d02b48c6 1517
0f113f3e 1518 if (s->state == SSL3_ST_SW_SRVR_DONE_A) {
77d514c5
MC
1519 if(!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) {
1520 SSLerr(SSL_F_SSL3_SEND_SERVER_DONE, ERR_R_INTERNAL_ERROR);
1521 return -1;
1522 }
0f113f3e
MC
1523 s->state = SSL3_ST_SW_SRVR_DONE_B;
1524 }
d02b48c6 1525
0f113f3e
MC
1526 /* SSL3_ST_SW_SRVR_DONE_B */
1527 return ssl_do_write(s);
1528}
d02b48c6 1529
36d16f8e 1530int ssl3_send_server_key_exchange(SSL *s)
0f113f3e 1531{
bc36ee62 1532#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1533 unsigned char *q;
1534 int j, num;
1535 RSA *rsa;
1536 unsigned char md_buf[MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH];
1537 unsigned int u;
d02b48c6 1538#endif
bc36ee62 1539#ifndef OPENSSL_NO_DH
0f113f3e 1540 DH *dh = NULL, *dhp;
ea262260 1541#endif
10bf4fc2 1542#ifndef OPENSSL_NO_EC
0f113f3e
MC
1543 EC_KEY *ecdh = NULL, *ecdhp;
1544 unsigned char *encodedPoint = NULL;
1545 int encodedlen = 0;
1546 int curve_id = 0;
1547 BN_CTX *bn_ctx = NULL;
d02b48c6 1548#endif
0f113f3e
MC
1549 EVP_PKEY *pkey;
1550 const EVP_MD *md = NULL;
1551 unsigned char *p, *d;
1552 int al, i;
1553 unsigned long type;
1554 int n;
1555 CERT *cert;
1556 BIGNUM *r[4];
1557 int nr[4], kn;
1558 BUF_MEM *buf;
1559 EVP_MD_CTX md_ctx;
1560
1561 EVP_MD_CTX_init(&md_ctx);
1562 if (s->state == SSL3_ST_SW_KEY_EXCH_A) {
1563 type = s->s3->tmp.new_cipher->algorithm_mkey;
1564 cert = s->cert;
1565
1566 buf = s->init_buf;
1567
1568 r[0] = r[1] = r[2] = r[3] = NULL;
1569 n = 0;
bc36ee62 1570#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1571 if (type & SSL_kRSA) {
1572 rsa = cert->rsa_tmp;
1573 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL)) {
1574 rsa = s->cert->rsa_tmp_cb(s,
1575 SSL_C_IS_EXPORT(s->s3->
1576 tmp.new_cipher),
1577 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1578 tmp.new_cipher));
1579 if (rsa == NULL) {
1580 al = SSL_AD_HANDSHAKE_FAILURE;
1581 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1582 SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1583 goto f_err;
1584 }
1585 RSA_up_ref(rsa);
1586 cert->rsa_tmp = rsa;
1587 }
1588 if (rsa == NULL) {
1589 al = SSL_AD_HANDSHAKE_FAILURE;
1590 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1591 SSL_R_MISSING_TMP_RSA_KEY);
1592 goto f_err;
1593 }
1594 r[0] = rsa->n;
1595 r[1] = rsa->e;
1596 s->s3->tmp.use_rsa_tmp = 1;
1597 } else
d02b48c6 1598#endif
bc36ee62 1599#ifndef OPENSSL_NO_DH
0f113f3e
MC
1600 if (type & SSL_kDHE) {
1601 if (s->cert->dh_tmp_auto) {
1602 dhp = ssl_get_auto_dh(s);
1603 if (dhp == NULL) {
1604 al = SSL_AD_INTERNAL_ERROR;
1605 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1606 ERR_R_INTERNAL_ERROR);
1607 goto f_err;
1608 }
1609 } else
1610 dhp = cert->dh_tmp;
1611 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1612 dhp = s->cert->dh_tmp_cb(s,
1613 SSL_C_IS_EXPORT(s->s3->
1614 tmp.new_cipher),
1615 SSL_C_EXPORT_PKEYLENGTH(s->s3->
1616 tmp.new_cipher));
1617 if (dhp == NULL) {
1618 al = SSL_AD_HANDSHAKE_FAILURE;
1619 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1620 SSL_R_MISSING_TMP_DH_KEY);
1621 goto f_err;
1622 }
1623 if (!ssl_security(s, SSL_SECOP_TMP_DH,
1624 DH_security_bits(dhp), 0, dhp)) {
1625 al = SSL_AD_HANDSHAKE_FAILURE;
1626 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1627 SSL_R_DH_KEY_TOO_SMALL);
1628 goto f_err;
1629 }
1630 if (s->s3->tmp.dh != NULL) {
1631 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1632 ERR_R_INTERNAL_ERROR);
1633 goto err;
1634 }
1635
1636 if (s->cert->dh_tmp_auto)
1637 dh = dhp;
1638 else if ((dh = DHparams_dup(dhp)) == NULL) {
1639 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1640 goto err;
1641 }
1642
1643 s->s3->tmp.dh = dh;
1644 if ((dhp->pub_key == NULL ||
1645 dhp->priv_key == NULL ||
1646 (s->options & SSL_OP_SINGLE_DH_USE))) {
1647 if (!DH_generate_key(dh)) {
1648 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1649 goto err;
1650 }
1651 } else {
1652 dh->pub_key = BN_dup(dhp->pub_key);
1653 dh->priv_key = BN_dup(dhp->priv_key);
1654 if ((dh->pub_key == NULL) || (dh->priv_key == NULL)) {
1655 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_DH_LIB);
1656 goto err;
1657 }
1658 }
1659 r[0] = dh->p;
1660 r[1] = dh->g;
1661 r[2] = dh->pub_key;
1662 } else
d02b48c6 1663#endif
10bf4fc2 1664#ifndef OPENSSL_NO_EC
0f113f3e
MC
1665 if (type & SSL_kECDHE) {
1666 const EC_GROUP *group;
1667
1668 ecdhp = cert->ecdh_tmp;
1669 if (s->cert->ecdh_tmp_auto) {
1670 /* Get NID of appropriate shared curve */
1671 int nid = tls1_shared_curve(s, -2);
1672 if (nid != NID_undef)
1673 ecdhp = EC_KEY_new_by_curve_name(nid);
1674 } else if ((ecdhp == NULL) && s->cert->ecdh_tmp_cb) {
1675 ecdhp = s->cert->ecdh_tmp_cb(s,
1676 SSL_C_IS_EXPORT(s->s3->
1677 tmp.new_cipher),
1678 SSL_C_EXPORT_PKEYLENGTH(s->
1679 s3->tmp.new_cipher));
1680 }
1681 if (ecdhp == NULL) {
1682 al = SSL_AD_HANDSHAKE_FAILURE;
1683 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1684 SSL_R_MISSING_TMP_ECDH_KEY);
1685 goto f_err;
1686 }
1687
1688 if (s->s3->tmp.ecdh != NULL) {
1689 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1690 ERR_R_INTERNAL_ERROR);
1691 goto err;
1692 }
1693
1694 /* Duplicate the ECDH structure. */
1695 if (ecdhp == NULL) {
1696 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1697 goto err;
1698 }
1699 if (s->cert->ecdh_tmp_auto)
1700 ecdh = ecdhp;
1701 else if ((ecdh = EC_KEY_dup(ecdhp)) == NULL) {
1702 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1703 goto err;
1704 }
1705
1706 s->s3->tmp.ecdh = ecdh;
1707 if ((EC_KEY_get0_public_key(ecdh) == NULL) ||
1708 (EC_KEY_get0_private_key(ecdh) == NULL) ||
1709 (s->options & SSL_OP_SINGLE_ECDH_USE)) {
1710 if (!EC_KEY_generate_key(ecdh)) {
1711 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1712 ERR_R_ECDH_LIB);
1713 goto err;
1714 }
1715 }
1716
1717 if (((group = EC_KEY_get0_group(ecdh)) == NULL) ||
1718 (EC_KEY_get0_public_key(ecdh) == NULL) ||
1719 (EC_KEY_get0_private_key(ecdh) == NULL)) {
1720 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1721 goto err;
1722 }
1723
1724 if (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) &&
1725 (EC_GROUP_get_degree(group) > 163)) {
1726 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1727 SSL_R_ECGROUP_TOO_LARGE_FOR_CIPHER);
1728 goto err;
1729 }
1730
1731 /*
1732 * XXX: For now, we only support ephemeral ECDH keys over named
1733 * (not generic) curves. For supported named curves, curve_id is
1734 * non-zero.
1735 */
1736 if ((curve_id =
1737 tls1_ec_nid2curve_id(EC_GROUP_get_curve_name(group)))
1738 == 0) {
1739 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1740 SSL_R_UNSUPPORTED_ELLIPTIC_CURVE);
1741 goto err;
1742 }
1743
1744 /*
1745 * Encode the public key. First check the size of encoding and
1746 * allocate memory accordingly.
1747 */
1748 encodedlen = EC_POINT_point2oct(group,
1749 EC_KEY_get0_public_key(ecdh),
1750 POINT_CONVERSION_UNCOMPRESSED,
1751 NULL, 0, NULL);
1752
1753 encodedPoint = (unsigned char *)
1754 OPENSSL_malloc(encodedlen * sizeof(unsigned char));
1755 bn_ctx = BN_CTX_new();
1756 if ((encodedPoint == NULL) || (bn_ctx == NULL)) {
1757 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1758 ERR_R_MALLOC_FAILURE);
1759 goto err;
1760 }
1761
1762 encodedlen = EC_POINT_point2oct(group,
1763 EC_KEY_get0_public_key(ecdh),
1764 POINT_CONVERSION_UNCOMPRESSED,
1765 encodedPoint, encodedlen, bn_ctx);
1766
1767 if (encodedlen == 0) {
1768 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_ECDH_LIB);
1769 goto err;
1770 }
1771
1772 BN_CTX_free(bn_ctx);
1773 bn_ctx = NULL;
1774
1775 /*
1776 * XXX: For now, we only support named (not generic) curves in
1777 * ECDH ephemeral key exchanges. In this situation, we need four
1778 * additional bytes to encode the entire ServerECDHParams
1779 * structure.
1780 */
1781 n = 4 + encodedlen;
1782
1783 /*
1784 * We'll generate the serverKeyExchange message explicitly so we
1785 * can set these to NULLs
1786 */
1787 r[0] = NULL;
1788 r[1] = NULL;
1789 r[2] = NULL;
1790 r[3] = NULL;
1791 } else
10bf4fc2 1792#endif /* !OPENSSL_NO_EC */
ddac1974 1793#ifndef OPENSSL_NO_PSK
0f113f3e
MC
1794 if (type & SSL_kPSK) {
1795 /*
1796 * reserve size for record length and PSK identity hint
1797 */
1798 n += 2 + strlen(s->ctx->psk_identity_hint);
1799 } else
1800#endif /* !OPENSSL_NO_PSK */
edc032b5 1801#ifndef OPENSSL_NO_SRP
0f113f3e
MC
1802 if (type & SSL_kSRP) {
1803 if ((s->srp_ctx.N == NULL) ||
1804 (s->srp_ctx.g == NULL) ||
1805 (s->srp_ctx.s == NULL) || (s->srp_ctx.B == NULL)) {
1806 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1807 SSL_R_MISSING_SRP_PARAM);
1808 goto err;
1809 }
1810 r[0] = s->srp_ctx.N;
1811 r[1] = s->srp_ctx.g;
1812 r[2] = s->srp_ctx.s;
1813 r[3] = s->srp_ctx.B;
1814 } else
edc032b5 1815#endif
0f113f3e
MC
1816 {
1817 al = SSL_AD_HANDSHAKE_FAILURE;
1818 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1819 SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1820 goto f_err;
1821 }
1822 for (i = 0; i < 4 && r[i] != NULL; i++) {
1823 nr[i] = BN_num_bytes(r[i]);
edc032b5 1824#ifndef OPENSSL_NO_SRP
0f113f3e
MC
1825 if ((i == 2) && (type & SSL_kSRP))
1826 n += 1 + nr[i];
1827 else
edc032b5 1828#endif
0f113f3e
MC
1829 n += 2 + nr[i];
1830 }
1831
1832 if (!(s->s3->tmp.new_cipher->algorithm_auth & (SSL_aNULL | SSL_aSRP))
1833 && !(s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) {
1834 if ((pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher, &md))
1835 == NULL) {
1836 al = SSL_AD_DECODE_ERROR;
1837 goto f_err;
1838 }
1839 kn = EVP_PKEY_size(pkey);
1840 } else {
1841 pkey = NULL;
1842 kn = 0;
1843 }
1844
1845 if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) {
1846 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_BUF);
1847 goto err;
1848 }
1849 d = p = ssl_handshake_start(s);
1850
1851 for (i = 0; i < 4 && r[i] != NULL; i++) {
edc032b5 1852#ifndef OPENSSL_NO_SRP
0f113f3e
MC
1853 if ((i == 2) && (type & SSL_kSRP)) {
1854 *p = nr[i];
1855 p++;
1856 } else
edc032b5 1857#endif
0f113f3e
MC
1858 s2n(nr[i], p);
1859 BN_bn2bin(r[i], p);
1860 p += nr[i];
1861 }
d02b48c6 1862
10bf4fc2 1863#ifndef OPENSSL_NO_EC
0f113f3e
MC
1864 if (type & SSL_kECDHE) {
1865 /*
1866 * XXX: For now, we only support named (not generic) curves. In
1867 * this situation, the serverKeyExchange message has: [1 byte
1868 * CurveType], [2 byte CurveName] [1 byte length of encoded
1869 * point], followed by the actual encoded point itself
1870 */
1871 *p = NAMED_CURVE_TYPE;
1872 p += 1;
1873 *p = 0;
1874 p += 1;
1875 *p = curve_id;
1876 p += 1;
1877 *p = encodedlen;
1878 p += 1;
1879 memcpy((unsigned char *)p,
1880 (unsigned char *)encodedPoint, encodedlen);
1881 OPENSSL_free(encodedPoint);
1882 encodedPoint = NULL;
1883 p += encodedlen;
1884 }
ea262260
BM
1885#endif
1886
ddac1974 1887#ifndef OPENSSL_NO_PSK
0f113f3e
MC
1888 if (type & SSL_kPSK) {
1889 /* copy PSK identity hint */
1890 s2n(strlen(s->ctx->psk_identity_hint), p);
1891 strncpy((char *)p, s->ctx->psk_identity_hint,
1892 strlen(s->ctx->psk_identity_hint));
1893 p += strlen(s->ctx->psk_identity_hint);
1894 }
ddac1974
NL
1895#endif
1896
0f113f3e
MC
1897 /* not anonymous */
1898 if (pkey != NULL) {
1899 /*
1900 * n is the length of the params, they start at &(d[4]) and p
1901 * points to the space at the end.
1902 */
bc36ee62 1903#ifndef OPENSSL_NO_RSA
0f113f3e
MC
1904 if (pkey->type == EVP_PKEY_RSA && !SSL_USE_SIGALGS(s)) {
1905 q = md_buf;
1906 j = 0;
1907 for (num = 2; num > 0; num--) {
1908 EVP_MD_CTX_set_flags(&md_ctx,
1909 EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
1910 EVP_DigestInit_ex(&md_ctx, (num == 2)
1911 ? s->ctx->md5 : s->ctx->sha1, NULL);
1912 EVP_DigestUpdate(&md_ctx, &(s->s3->client_random[0]),
1913 SSL3_RANDOM_SIZE);
1914 EVP_DigestUpdate(&md_ctx, &(s->s3->server_random[0]),
1915 SSL3_RANDOM_SIZE);
1916 EVP_DigestUpdate(&md_ctx, d, n);
1917 EVP_DigestFinal_ex(&md_ctx, q, (unsigned int *)&i);
1918 q += i;
1919 j += i;
1920 }
1921 if (RSA_sign(NID_md5_sha1, md_buf, j,
1922 &(p[2]), &u, pkey->pkey.rsa) <= 0) {
1923 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_RSA);
1924 goto err;
1925 }
1926 s2n(u, p);
1927 n += u + 2;
1928 } else
d02b48c6 1929#endif
0f113f3e
MC
1930 if (md) {
1931 /* send signature algorithm */
1932 if (SSL_USE_SIGALGS(s)) {
1933 if (!tls12_get_sigandhash(p, pkey, md)) {
1934 /* Should never happen */
1935 al = SSL_AD_INTERNAL_ERROR;
1936 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1937 ERR_R_INTERNAL_ERROR);
1938 goto f_err;
1939 }
1940 p += 2;
1941 }
a2f9200f 1942#ifdef SSL_DEBUG
0f113f3e 1943 fprintf(stderr, "Using hash %s\n", EVP_MD_name(md));
a2f9200f 1944#endif
0f113f3e
MC
1945 EVP_SignInit_ex(&md_ctx, md, NULL);
1946 EVP_SignUpdate(&md_ctx, &(s->s3->client_random[0]),
1947 SSL3_RANDOM_SIZE);
1948 EVP_SignUpdate(&md_ctx, &(s->s3->server_random[0]),
1949 SSL3_RANDOM_SIZE);
1950 EVP_SignUpdate(&md_ctx, d, n);
1951 if (!EVP_SignFinal(&md_ctx, &(p[2]),
1952 (unsigned int *)&i, pkey)) {
1953 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_LIB_EVP);
1954 goto err;
1955 }
1956 s2n(i, p);
1957 n += i + 2;
1958 if (SSL_USE_SIGALGS(s))
1959 n += 2;
1960 } else {
1961 /* Is this error check actually needed? */
1962 al = SSL_AD_HANDSHAKE_FAILURE;
1963 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1964 SSL_R_UNKNOWN_PKEY_TYPE);
1965 goto f_err;
1966 }
1967 }
1968
77d514c5
MC
1969 if(!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) {
1970 al = SSL_AD_HANDSHAKE_FAILURE;
1971 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1972 goto f_err;
1973 }
0f113f3e
MC
1974 }
1975
1976 s->state = SSL3_ST_SW_KEY_EXCH_B;
1977 EVP_MD_CTX_cleanup(&md_ctx);
1978 return ssl_do_write(s);
1979 f_err:
1980 ssl3_send_alert(s, SSL3_AL_FATAL, al);
1981 err:
556efe79 1982#ifndef OPENSSL_NO_EC
0f113f3e
MC
1983 if (encodedPoint != NULL)
1984 OPENSSL_free(encodedPoint);
1985 BN_CTX_free(bn_ctx);
ea262260 1986#endif
0f113f3e
MC
1987 EVP_MD_CTX_cleanup(&md_ctx);
1988 return (-1);
1989}
d02b48c6 1990
36d16f8e 1991int ssl3_send_certificate_request(SSL *s)
0f113f3e
MC
1992{
1993 unsigned char *p, *d;
1994 int i, j, nl, off, n;
1995 STACK_OF(X509_NAME) *sk = NULL;
1996 X509_NAME *name;
1997 BUF_MEM *buf;
1998
1999 if (s->state == SSL3_ST_SW_CERT_REQ_A) {
2000 buf = s->init_buf;
2001
2002 d = p = ssl_handshake_start(s);
2003
2004 /* get the list of acceptable cert types */
2005 p++;
2006 n = ssl3_get_req_cert_type(s, p);
2007 d[0] = n;
2008 p += n;
2009 n++;
2010
2011 if (SSL_USE_SIGALGS(s)) {
2012 const unsigned char *psigs;
2013 unsigned char *etmp = p;
2014 nl = tls12_get_psigalgs(s, &psigs);
2015 /* Skip over length for now */
2016 p += 2;
2017 nl = tls12_copy_sigalgs(s, p, psigs, nl);
2018 /* Now fill in length */
2019 s2n(nl, etmp);
2020 p += nl;
2021 n += nl + 2;
2022 }
2023
2024 off = n;
2025 p += 2;
2026 n += 2;
2027
2028 sk = SSL_get_client_CA_list(s);
2029 nl = 0;
2030 if (sk != NULL) {
2031 for (i = 0; i < sk_X509_NAME_num(sk); i++) {
2032 name = sk_X509_NAME_value(sk, i);
2033 j = i2d_X509_NAME(name, NULL);
2034 if (!BUF_MEM_grow_clean
2035 (buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) {
2036 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,
2037 ERR_R_BUF_LIB);
2038 goto err;
2039 }
2040 p = ssl_handshake_start(s) + n;
3c33c6f6
MC
2041 s2n(j, p);
2042 i2d_X509_NAME(name, &p);
2043 n += 2 + j;
2044 nl += 2 + j;
0f113f3e
MC
2045 }
2046 }
2047 /* else no CA names */
2048 p = ssl_handshake_start(s) + off;
2049 s2n(nl, p);
2050
77d514c5
MC
2051 if(!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) {
2052 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST, ERR_R_INTERNAL_ERROR);
2053 return -1;
2054 }
d02b48c6 2055
0f113f3e
MC
2056 s->state = SSL3_ST_SW_CERT_REQ_B;
2057 }
d02b48c6 2058
0f113f3e
MC
2059 /* SSL3_ST_SW_CERT_REQ_B */
2060 return ssl_do_write(s);
2061 err:
2062 return (-1);
2063}
d02b48c6 2064
36d16f8e 2065int ssl3_get_client_key_exchange(SSL *s)
0f113f3e
MC
2066{
2067 int i, al, ok;
2068 long n;
2069 unsigned long alg_k;
2070 unsigned char *p;
bc36ee62 2071#ifndef OPENSSL_NO_RSA
0f113f3e
MC
2072 RSA *rsa = NULL;
2073 EVP_PKEY *pkey = NULL;
79df9d62 2074#endif
bc36ee62 2075#ifndef OPENSSL_NO_DH
0f113f3e
MC
2076 BIGNUM *pub = NULL;
2077 DH *dh_srvr, *dh_clnt = NULL;
58964a49 2078#endif
bc36ee62 2079#ifndef OPENSSL_NO_KRB5
0f113f3e
MC
2080 KSSL_ERR kssl_err;
2081#endif /* OPENSSL_NO_KRB5 */
d02b48c6 2082
556efe79 2083#ifndef OPENSSL_NO_EC
0f113f3e
MC
2084 EC_KEY *srvr_ecdh = NULL;
2085 EVP_PKEY *clnt_pub_pkey = NULL;
2086 EC_POINT *clnt_ecpoint = NULL;
2087 BN_CTX *bn_ctx = NULL;
ea262260
BM
2088#endif
2089
0f113f3e
MC
2090 n = s->method->ssl_get_message(s,
2091 SSL3_ST_SR_KEY_EXCH_A,
2092 SSL3_ST_SR_KEY_EXCH_B,
2093 SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, &ok);
d02b48c6 2094
0f113f3e
MC
2095 if (!ok)
2096 return ((int)n);
2097 p = (unsigned char *)s->init_msg;
d02b48c6 2098
0f113f3e 2099 alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
d02b48c6 2100
bc36ee62 2101#ifndef OPENSSL_NO_RSA
0f113f3e
MC
2102 if (alg_k & SSL_kRSA) {
2103 unsigned char rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH];
2104 int decrypt_len;
2105 unsigned char decrypt_good, version_good;
2106 size_t j;
2107
2108 /* FIX THIS UP EAY EAY EAY EAY */
2109 if (s->s3->tmp.use_rsa_tmp) {
2110 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
2111 rsa = s->cert->rsa_tmp;
2112 /*
2113 * Don't do a callback because rsa_tmp should be sent already
2114 */
2115 if (rsa == NULL) {
2116 al = SSL_AD_HANDSHAKE_FAILURE;
2117 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2118 SSL_R_MISSING_TMP_RSA_PKEY);
2119 goto f_err;
2120
2121 }
2122 } else {
2123 pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
2124 if ((pkey == NULL) ||
2125 (pkey->type != EVP_PKEY_RSA) || (pkey->pkey.rsa == NULL)) {
2126 al = SSL_AD_HANDSHAKE_FAILURE;
2127 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2128 SSL_R_MISSING_RSA_CERTIFICATE);
2129 goto f_err;
2130 }
2131 rsa = pkey->pkey.rsa;
2132 }
2133
2134 /* TLS and [incidentally] DTLS{0xFEFF} */
2135 if (s->version > SSL3_VERSION && s->version != DTLS1_BAD_VER) {
2136 n2s(p, i);
2137 if (n != i + 2) {
2138 if (!(s->options & SSL_OP_TLS_D5_BUG)) {
2139 al = SSL_AD_DECODE_ERROR;
2140 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2141 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2142 goto f_err;
2143 } else
2144 p -= 2;
2145 } else
2146 n = i;
2147 }
2148
2149 /*
2150 * Reject overly short RSA ciphertext because we want to be sure
2151 * that the buffer size makes it safe to iterate over the entire
2152 * size of a premaster secret (SSL_MAX_MASTER_KEY_LENGTH). The
2153 * actual expected size is larger due to RSA padding, but the
2154 * bound is sufficient to be safe.
2155 */
2156 if (n < SSL_MAX_MASTER_KEY_LENGTH) {
2157 al = SSL_AD_DECRYPT_ERROR;
2158 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2159 SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
2160 goto f_err;
2161 }
2162
2163 /*
2164 * We must not leak whether a decryption failure occurs because of
2165 * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246,
2166 * section 7.4.7.1). The code follows that advice of the TLS RFC and
2167 * generates a random premaster secret for the case that the decrypt
2168 * fails. See https://tools.ietf.org/html/rfc5246#section-7.4.7.1
2169 */
2170
266483d2 2171 if (RAND_bytes(rand_premaster_secret,
0f113f3e
MC
2172 sizeof(rand_premaster_secret)) <= 0)
2173 goto err;
2174 decrypt_len =
2175 RSA_private_decrypt((int)n, p, p, rsa, RSA_PKCS1_PADDING);
2176 ERR_clear_error();
2177
2178 /*
2179 * decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. decrypt_good will
2180 * be 0xff if so and zero otherwise.
2181 */
2182 decrypt_good =
2183 constant_time_eq_int_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH);
2184
2185 /*
2186 * If the version in the decrypted pre-master secret is correct then
2187 * version_good will be 0xff, otherwise it'll be zero. The
2188 * Klima-Pokorny-Rosa extension of Bleichenbacher's attack
2189 * (http://eprint.iacr.org/2003/052/) exploits the version number
2190 * check as a "bad version oracle". Thus version checks are done in
2191 * constant time and are treated like any other decryption error.
2192 */
2193 version_good =
2194 constant_time_eq_8(p[0], (unsigned)(s->client_version >> 8));
2195 version_good &=
2196 constant_time_eq_8(p[1], (unsigned)(s->client_version & 0xff));
2197
2198 /*
2199 * The premaster secret must contain the same version number as the
2200 * ClientHello to detect version rollback attacks (strangely, the
2201 * protocol does not offer such protection for DH ciphersuites).
2202 * However, buggy clients exist that send the negotiated protocol
2203 * version instead if the server does not support the requested
2204 * protocol version. If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such
2205 * clients.
2206 */
2207 if (s->options & SSL_OP_TLS_ROLLBACK_BUG) {
2208 unsigned char workaround_good;
2209 workaround_good =
2210 constant_time_eq_8(p[0], (unsigned)(s->version >> 8));
2211 workaround_good &=
2212 constant_time_eq_8(p[1], (unsigned)(s->version & 0xff));
2213 version_good |= workaround_good;
2214 }
2215
2216 /*
2217 * Both decryption and version must be good for decrypt_good to
2218 * remain non-zero (0xff).
2219 */
2220 decrypt_good &= version_good;
2221
2222 /*
2223 * Now copy rand_premaster_secret over from p using
2224 * decrypt_good_mask. If decryption failed, then p does not
2225 * contain valid plaintext, however, a check above guarantees
2226 * it is still sufficiently large to read from.
2227 */
2228 for (j = 0; j < sizeof(rand_premaster_secret); j++) {
2229 p[j] = constant_time_select_8(decrypt_good, p[j],
2230 rand_premaster_secret[j]);
2231 }
2232
2233 s->session->master_key_length =
2234 s->method->ssl3_enc->generate_master_secret(s,
2235 s->
2236 session->master_key,
2237 p,
2238 sizeof
2239 (rand_premaster_secret));
2240 OPENSSL_cleanse(p, sizeof(rand_premaster_secret));
69f68237
MC
2241 if(s->session->master_key_length < 0) {
2242 al = SSL_AD_INTERNAL_ERROR;
2243 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2244 goto f_err;
2245 }
0f113f3e 2246 } else
4c5fac4a 2247#endif
bc36ee62 2248#ifndef OPENSSL_NO_DH
0f113f3e
MC
2249 if (alg_k & (SSL_kDHE | SSL_kDHr | SSL_kDHd)) {
2250 int idx = -1;
2251 EVP_PKEY *skey = NULL;
d3cc5e61 2252 if (n > 1) {
0f113f3e 2253 n2s(p, i);
d3cc5e61
MC
2254 } else {
2255 if (alg_k & SSL_kDHE) {
2256 al = SSL_AD_HANDSHAKE_FAILURE;
2257 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2258 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2259 goto f_err;
2260 }
0f113f3e 2261 i = 0;
d3cc5e61 2262 }
0f113f3e
MC
2263 if (n && n != i + 2) {
2264 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG)) {
2265 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2266 SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
2267 goto err;
2268 } else {
2269 p -= 2;
2270 i = (int)n;
2271 }
2272 }
2273 if (alg_k & SSL_kDHr)
2274 idx = SSL_PKEY_DH_RSA;
2275 else if (alg_k & SSL_kDHd)
2276 idx = SSL_PKEY_DH_DSA;
2277 if (idx >= 0) {
2278 skey = s->cert->pkeys[idx].privatekey;
2279 if ((skey == NULL) ||
2280 (skey->type != EVP_PKEY_DH) || (skey->pkey.dh == NULL)) {
2281 al = SSL_AD_HANDSHAKE_FAILURE;
2282 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2283 SSL_R_MISSING_RSA_CERTIFICATE);
2284 goto f_err;
2285 }
2286 dh_srvr = skey->pkey.dh;
2287 } else if (s->s3->tmp.dh == NULL) {
2288 al = SSL_AD_HANDSHAKE_FAILURE;
2289 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2290 SSL_R_MISSING_TMP_DH_KEY);
2291 goto f_err;
2292 } else
2293 dh_srvr = s->s3->tmp.dh;
2294
2295 if (n == 0L) {
2296 /* Get pubkey from cert */
2297 EVP_PKEY *clkey = X509_get_pubkey(s->session->peer);
2298 if (clkey) {
2299 if (EVP_PKEY_cmp_parameters(clkey, skey) == 1)
2300 dh_clnt = EVP_PKEY_get1_DH(clkey);
2301 }
2302 if (dh_clnt == NULL) {
2303 al = SSL_AD_HANDSHAKE_FAILURE;
2304 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2305 SSL_R_MISSING_TMP_DH_KEY);
2306 goto f_err;
2307 }
2308 EVP_PKEY_free(clkey);
2309 pub = dh_clnt->pub_key;
2310 } else
2311 pub = BN_bin2bn(p, i, NULL);
2312 if (pub == NULL) {
2313 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_BN_LIB);
2314 goto err;
2315 }
2316
2317 i = DH_compute_key(p, pub, dh_srvr);
2318
2319 if (i <= 0) {
2320 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_DH_LIB);
2321 BN_clear_free(pub);
2322 goto err;
2323 }
2324
2325 DH_free(s->s3->tmp.dh);
2326 s->s3->tmp.dh = NULL;
2327 if (dh_clnt)
2328 DH_free(dh_clnt);
2329 else
2330 BN_clear_free(pub);
2331 pub = NULL;
2332 s->session->master_key_length =
2333 s->method->ssl3_enc->generate_master_secret(s,
2334 s->
2335 session->master_key,
2336 p, i);
2337 OPENSSL_cleanse(p, i);
69f68237
MC
2338 if(s->session->master_key_length < 0) {
2339 al = SSL_AD_INTERNAL_ERROR;
2340 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2341 goto f_err;
2342 }
0f113f3e
MC
2343 if (dh_clnt)
2344 return 2;
2345 } else
d02b48c6 2346#endif
bc36ee62 2347#ifndef OPENSSL_NO_KRB5
0f113f3e
MC
2348 if (alg_k & SSL_kKRB5) {
2349 krb5_error_code krb5rc;
2350 krb5_data enc_ticket;
2351 krb5_data authenticator;
2352 krb5_data enc_pms;
2353 KSSL_CTX *kssl_ctx = s->kssl_ctx;
2354 EVP_CIPHER_CTX ciph_ctx;
2355 const EVP_CIPHER *enc = NULL;
2356 unsigned char iv[EVP_MAX_IV_LENGTH];
2357 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH + EVP_MAX_BLOCK_LENGTH];
2358 int padl, outl;
2359 krb5_timestamp authtime = 0;
2360 krb5_ticket_times ttimes;
2361
2362 EVP_CIPHER_CTX_init(&ciph_ctx);
2363
2364 if (!kssl_ctx)
2365 kssl_ctx = kssl_ctx_new();
2366
2367 n2s(p, i);
2368 enc_ticket.length = i;
2369
2370 if (n < (long)(enc_ticket.length + 6)) {
2371 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2372 SSL_R_DATA_LENGTH_TOO_LONG);
2373 goto err;
2374 }
2375
2376 enc_ticket.data = (char *)p;
2377 p += enc_ticket.length;
2378
2379 n2s(p, i);
2380 authenticator.length = i;
2381
2382 if (n < (long)(enc_ticket.length + authenticator.length + 6)) {
2383 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2384 SSL_R_DATA_LENGTH_TOO_LONG);
2385 goto err;
2386 }
2387
2388 authenticator.data = (char *)p;
2389 p += authenticator.length;
2390
2391 n2s(p, i);
2392 enc_pms.length = i;
2393 enc_pms.data = (char *)p;
2394 p += enc_pms.length;
2395
2396 /*
2397 * Note that the length is checked again below, ** after decryption
2398 */
2399 if (enc_pms.length > sizeof pms) {
2400 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2401 SSL_R_DATA_LENGTH_TOO_LONG);
2402 goto err;
2403 }
2404
2405 if (n != (long)(enc_ticket.length + authenticator.length +
2406 enc_pms.length + 6)) {
2407 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2408 SSL_R_DATA_LENGTH_TOO_LONG);
2409 goto err;
2410 }
2411
2412 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
2413 &kssl_err)) != 0) {
2414# ifdef KSSL_DEBUG
2415 fprintf(stderr, "kssl_sget_tkt rtn %d [%d]\n",
2416 krb5rc, kssl_err.reason);
2417 if (kssl_err.text)
2418 fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2419# endif /* KSSL_DEBUG */
2420 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2421 goto err;
2422 }
2423
2424 /*
2425 * Note: no authenticator is not considered an error, ** but will
2426 * return authtime == 0.
2427 */
2428 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
2429 &authtime, &kssl_err)) != 0) {
2430# ifdef KSSL_DEBUG
2431 fprintf(stderr, "kssl_check_authent rtn %d [%d]\n",
2432 krb5rc, kssl_err.reason);
2433 if (kssl_err.text)
2434 fprintf(stderr, "kssl_err text= %s\n", kssl_err.text);
2435# endif /* KSSL_DEBUG */
2436 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, kssl_err.reason);
2437 goto err;
2438 }
2439
2440 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0) {
2441 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, krb5rc);
2442 goto err;
2443 }
2444# ifdef KSSL_DEBUG
2445 kssl_ctx_show(kssl_ctx);
2446# endif /* KSSL_DEBUG */
2447
2448 enc = kssl_map_enc(kssl_ctx->enctype);
2449 if (enc == NULL)
2450 goto err;
2451
2452 memset(iv, 0, sizeof iv); /* per RFC 1510 */
2453
2454 if (!EVP_DecryptInit_ex(&ciph_ctx, enc, NULL, kssl_ctx->key, iv)) {
2455 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2456 SSL_R_DECRYPTION_FAILED);
2457 goto err;
2458 }
2459 if (!EVP_DecryptUpdate(&ciph_ctx, pms, &outl,
2460 (unsigned char *)enc_pms.data, enc_pms.length))
2461 {
2462 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2463 SSL_R_DECRYPTION_FAILED);
2464 goto err;
2465 }
2466 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2467 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2468 SSL_R_DATA_LENGTH_TOO_LONG);
2469 goto err;
2470 }
2471 if (!EVP_DecryptFinal_ex(&ciph_ctx, &(pms[outl]), &padl)) {
2472 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2473 SSL_R_DECRYPTION_FAILED);
2474 goto err;
2475 }
2476 outl += padl;
2477 if (outl > SSL_MAX_MASTER_KEY_LENGTH) {
2478 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2479 SSL_R_DATA_LENGTH_TOO_LONG);
2480 goto err;
2481 }
2482 if (!((pms[0] == (s->client_version >> 8))
2483 && (pms[1] == (s->client_version & 0xff)))) {
2484 /*
2485 * The premaster secret must contain the same version number as
2486 * the ClientHello to detect version rollback attacks (strangely,
2487 * the protocol does not offer such protection for DH
2488 * ciphersuites). However, buggy clients exist that send random
2489 * bytes instead of the protocol version. If
2490 * SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients.
2491 * (Perhaps we should have a separate BUG value for the Kerberos
2492 * cipher)
2493 */
2494 if (!(s->options & SSL_OP_TLS_ROLLBACK_BUG)) {
2495 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2496 SSL_AD_DECODE_ERROR);
2497 goto err;
2498 }
2499 }
2500
2501 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
2502
2503 s->session->master_key_length =
2504 s->method->ssl3_enc->generate_master_secret(s,
2505 s->
2506 session->master_key,
2507 pms, outl);
69f68237
MC
2508 if(s->session->master_key_length < 0) {
2509 al = SSL_INTERNAL_ERROR;
2510 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2511 goto f_err;
2512 }
0f113f3e
MC
2513
2514 if (kssl_ctx->client_princ) {
2515 size_t len = strlen(kssl_ctx->client_princ);
2516 if (len < SSL_MAX_KRB5_PRINCIPAL_LENGTH) {
2517 s->session->krb5_client_princ_len = len;
2518 memcpy(s->session->krb5_client_princ, kssl_ctx->client_princ,
2519 len);
2520 }
2521 }
2522
50e735f9
MC
2523 /*- Was doing kssl_ctx_free() here,
2524 * but it caused problems for apache.
2525 * kssl_ctx = kssl_ctx_free(kssl_ctx);
2526 * if (s->kssl_ctx) s->kssl_ctx = NULL;
2527 */
0f113f3e
MC
2528 } else
2529#endif /* OPENSSL_NO_KRB5 */
ea262260 2530
556efe79 2531#ifndef OPENSSL_NO_EC
0f113f3e
MC
2532 if (alg_k & (SSL_kECDHE | SSL_kECDHr | SSL_kECDHe)) {
2533 int ret = 1;
2534 int field_size = 0;
2535 const EC_KEY *tkey;
2536 const EC_GROUP *group;
2537 const BIGNUM *priv_key;
2538
2539 /* initialize structures for server's ECDH key pair */
2540 if ((srvr_ecdh = EC_KEY_new()) == NULL) {
2541 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2542 goto err;
2543 }
2544
2545 /* Let's get server private key and group information */
2546 if (alg_k & (SSL_kECDHr | SSL_kECDHe)) {
2547 /* use the certificate */
2548 tkey = s->cert->pkeys[SSL_PKEY_ECC].privatekey->pkey.ec;
2549 } else {
2550 /*
2551 * use the ephermeral values we saved when generating the
2552 * ServerKeyExchange msg.
2553 */
2554 tkey = s->s3->tmp.ecdh;
2555 }
2556
2557 group = EC_KEY_get0_group(tkey);
2558 priv_key = EC_KEY_get0_private_key(tkey);
2559
2560 if (!EC_KEY_set_group(srvr_ecdh, group) ||
2561 !EC_KEY_set_private_key(srvr_ecdh, priv_key)) {
2562 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2563 goto err;
2564 }
2565
2566 /* Let's get client's public key */
2567 if ((clnt_ecpoint = EC_POINT_new(group)) == NULL) {
2568 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2569 goto err;
2570 }
2571
2572 if (n == 0L) {
2573 /* Client Publickey was in Client Certificate */
2574
2575 if (alg_k & SSL_kECDHE) {
2576 al = SSL_AD_HANDSHAKE_FAILURE;
2577 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2578 SSL_R_MISSING_TMP_ECDH_KEY);
2579 goto f_err;
2580 }
2581 if (((clnt_pub_pkey = X509_get_pubkey(s->session->peer))
2582 == NULL) || (clnt_pub_pkey->type != EVP_PKEY_EC)) {
2583 /*
2584 * XXX: For now, we do not support client authentication
2585 * using ECDH certificates so this branch (n == 0L) of the
2586 * code is never executed. When that support is added, we
2587 * ought to ensure the key received in the certificate is
2588 * authorized for key agreement. ECDH_compute_key implicitly
2589 * checks that the two ECDH shares are for the same group.
2590 */
2591 al = SSL_AD_HANDSHAKE_FAILURE;
2592 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2593 SSL_R_UNABLE_TO_DECODE_ECDH_CERTS);
2594 goto f_err;
2595 }
2596
2597 if (EC_POINT_copy(clnt_ecpoint,
2598 EC_KEY_get0_public_key(clnt_pub_pkey->
2599 pkey.ec)) == 0) {
2600 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2601 goto err;
2602 }
2603 ret = 2; /* Skip certificate verify processing */
2604 } else {
2605 /*
2606 * Get client's public key from encoded point in the
2607 * ClientKeyExchange message.
2608 */
2609 if ((bn_ctx = BN_CTX_new()) == NULL) {
2610 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2611 ERR_R_MALLOC_FAILURE);
2612 goto err;
2613 }
2614
2615 /* Get encoded point length */
2616 i = *p;
2617 p += 1;
2618 if (n != 1 + i) {
2619 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2620 goto err;
2621 }
2622 if (EC_POINT_oct2point(group, clnt_ecpoint, p, i, bn_ctx) == 0) {
2623 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_EC_LIB);
2624 goto err;
2625 }
2626 /*
2627 * p is pointing to somewhere in the buffer currently, so set it
2628 * to the start
2629 */
2630 p = (unsigned char *)s->init_buf->data;
2631 }
2632
2633 /* Compute the shared pre-master secret */
2634 field_size = EC_GROUP_get_degree(group);
2635 if (field_size <= 0) {
2636 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2637 goto err;
2638 }
2639 i = ECDH_compute_key(p, (field_size + 7) / 8, clnt_ecpoint, srvr_ecdh,
2640 NULL);
2641 if (i <= 0) {
2642 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_ECDH_LIB);
2643 goto err;
2644 }
2645
2646 EVP_PKEY_free(clnt_pub_pkey);
2647 EC_POINT_free(clnt_ecpoint);
2648 EC_KEY_free(srvr_ecdh);
2649 BN_CTX_free(bn_ctx);
2650 EC_KEY_free(s->s3->tmp.ecdh);
2651 s->s3->tmp.ecdh = NULL;
2652
2653 /* Compute the master secret */
2654 s->session->master_key_length =
2655 s->method->ssl3_enc->generate_master_secret(s,
2656 s->
2657 session->master_key,
2658 p, i);
2659
2660 OPENSSL_cleanse(p, i);
69f68237
MC
2661 if(s->session->master_key_length < 0) {
2662 al = SSL_AD_INTERNAL_ERROR;
2663 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2664 goto f_err;
2665 }
0f113f3e
MC
2666 return (ret);
2667 } else
ddac1974
NL
2668#endif
2669#ifndef OPENSSL_NO_PSK
0f113f3e
MC
2670 if (alg_k & SSL_kPSK) {
2671 unsigned char *t = NULL;
2672 unsigned char psk_or_pre_ms[PSK_MAX_PSK_LEN * 2 + 4];
2673 unsigned int pre_ms_len = 0, psk_len = 0;
2674 int psk_err = 1;
2675 char tmp_id[PSK_MAX_IDENTITY_LEN + 1];
2676
2677 al = SSL_AD_HANDSHAKE_FAILURE;
2678
2679 n2s(p, i);
2680 if (n != i + 2) {
2681 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_LENGTH_MISMATCH);
2682 goto psk_err;
2683 }
2684 if (i > PSK_MAX_IDENTITY_LEN) {
2685 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2686 SSL_R_DATA_LENGTH_TOO_LONG);
2687 goto psk_err;
2688 }
2689 if (s->psk_server_callback == NULL) {
2690 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2691 SSL_R_PSK_NO_SERVER_CB);
2692 goto psk_err;
2693 }
2694
2695 /*
2696 * Create guaranteed NULL-terminated identity string for the callback
2697 */
2698 memcpy(tmp_id, p, i);
2699 memset(tmp_id + i, 0, PSK_MAX_IDENTITY_LEN + 1 - i);
2700 psk_len = s->psk_server_callback(s, tmp_id,
2701 psk_or_pre_ms,
2702 sizeof(psk_or_pre_ms));
2703 OPENSSL_cleanse(tmp_id, PSK_MAX_IDENTITY_LEN + 1);
2704
2705 if (psk_len > PSK_MAX_PSK_LEN) {
2706 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2707 goto psk_err;
2708 } else if (psk_len == 0) {
2709 /*
2710 * PSK related to the given identity not found
2711 */
2712 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2713 SSL_R_PSK_IDENTITY_NOT_FOUND);
2714 al = SSL_AD_UNKNOWN_PSK_IDENTITY;
2715 goto psk_err;
2716 }
2717
2718 /* create PSK pre_master_secret */
2719 pre_ms_len = 2 + psk_len + 2 + psk_len;
2720 t = psk_or_pre_ms;
2721 memmove(psk_or_pre_ms + psk_len + 4, psk_or_pre_ms, psk_len);
2722 s2n(psk_len, t);
2723 memset(t, 0, psk_len);
2724 t += psk_len;
2725 s2n(psk_len, t);
2726
2727 if (s->session->psk_identity != NULL)
2728 OPENSSL_free(s->session->psk_identity);
2729 s->session->psk_identity = BUF_strdup((char *)p);
2730 if (s->session->psk_identity == NULL) {
2731 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2732 goto psk_err;
2733 }
2734
2735 if (s->session->psk_identity_hint != NULL)
2736 OPENSSL_free(s->session->psk_identity_hint);
2737 s->session->psk_identity_hint = BUF_strdup(s->ctx->psk_identity_hint);
2738 if (s->ctx->psk_identity_hint != NULL &&
2739 s->session->psk_identity_hint == NULL) {
2740 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2741 goto psk_err;
2742 }
2743
2744 s->session->master_key_length =
2745 s->method->ssl3_enc->generate_master_secret(s,
2746 s->
2747 session->master_key,
2748 psk_or_pre_ms,
2749 pre_ms_len);
69f68237
MC
2750 if(s->session->master_key_length < 0) {
2751 al = SSL_AD_INTERNAL_ERROR;
2752 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2753 goto psk_err;
2754 }
0f113f3e
MC
2755 psk_err = 0;
2756 psk_err:
2757 OPENSSL_cleanse(psk_or_pre_ms, sizeof(psk_or_pre_ms));
2758 if (psk_err != 0)
2759 goto f_err;
2760 } else
ea262260 2761#endif
edc032b5 2762#ifndef OPENSSL_NO_SRP
0f113f3e
MC
2763 if (alg_k & SSL_kSRP) {
2764 int param_len;
2765
2766 n2s(p, i);
2767 param_len = i + 2;
2768 if (param_len > n) {
2769 al = SSL_AD_DECODE_ERROR;
2770 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2771 SSL_R_BAD_SRP_A_LENGTH);
2772 goto f_err;
2773 }
2774 if (!(s->srp_ctx.A = BN_bin2bn(p, i, NULL))) {
2775 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_BN_LIB);
2776 goto err;
2777 }
2778 if (BN_ucmp(s->srp_ctx.A, s->srp_ctx.N) >= 0
2779 || BN_is_zero(s->srp_ctx.A)) {
2780 al = SSL_AD_ILLEGAL_PARAMETER;
2781 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2782 SSL_R_BAD_SRP_PARAMETERS);
2783 goto f_err;
2784 }
2785 if (s->session->srp_username != NULL)
2786 OPENSSL_free(s->session->srp_username);
2787 s->session->srp_username = BUF_strdup(s->srp_ctx.login);
2788 if (s->session->srp_username == NULL) {
2789 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_MALLOC_FAILURE);
2790 goto err;
2791 }
2792
2793 if ((s->session->master_key_length =
2794 SRP_generate_server_master_secret(s,
2795 s->session->master_key)) < 0) {
2796 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2797 goto err;
2798 }
2799
2800 p += i;
2801 } else
2802#endif /* OPENSSL_NO_SRP */
2803 if (alg_k & SSL_kGOST) {
2804 int ret = 0;
2805 EVP_PKEY_CTX *pkey_ctx;
2806 EVP_PKEY *client_pub_pkey = NULL, *pk = NULL;
2807 unsigned char premaster_secret[32], *start;
2808 size_t outlen = 32, inlen;
2809 unsigned long alg_a;
2810 int Ttag, Tclass;
2811 long Tlen;
2812
2813 /* Get our certificate private key */
2814 alg_a = s->s3->tmp.new_cipher->algorithm_auth;
2815 if (alg_a & SSL_aGOST94)
2816 pk = s->cert->pkeys[SSL_PKEY_GOST94].privatekey;
2817 else if (alg_a & SSL_aGOST01)
2818 pk = s->cert->pkeys[SSL_PKEY_GOST01].privatekey;
2819
2820 pkey_ctx = EVP_PKEY_CTX_new(pk, NULL);
2821 EVP_PKEY_decrypt_init(pkey_ctx);
2822 /*
2823 * If client certificate is present and is of the same type, maybe
2824 * use it for key exchange. Don't mind errors from
2825 * EVP_PKEY_derive_set_peer, because it is completely valid to use a
2826 * client certificate for authorization only.
2827 */
2828 client_pub_pkey = X509_get_pubkey(s->session->peer);
2829 if (client_pub_pkey) {
2830 if (EVP_PKEY_derive_set_peer(pkey_ctx, client_pub_pkey) <= 0)
2831 ERR_clear_error();
2832 }
2833 /* Decrypt session key */
2834 if (ASN1_get_object
2835 ((const unsigned char **)&p, &Tlen, &Ttag, &Tclass,
2836 n) != V_ASN1_CONSTRUCTED || Ttag != V_ASN1_SEQUENCE
2837 || Tclass != V_ASN1_UNIVERSAL) {
2838 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2839 SSL_R_DECRYPTION_FAILED);
2840 goto gerr;
2841 }
2842 start = p;
2843 inlen = Tlen;
2844 if (EVP_PKEY_decrypt
2845 (pkey_ctx, premaster_secret, &outlen, start, inlen) <= 0) {
2846 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
2847 SSL_R_DECRYPTION_FAILED);
2848 goto gerr;
2849 }
2850 /* Generate master secret */
2851 s->session->master_key_length =
2852 s->method->ssl3_enc->generate_master_secret(s,
2853 s->
2854 session->master_key,
2855 premaster_secret, 32);
69f68237
MC
2856 if(s->session->master_key_length < 0) {
2857 al = SSL_AD_INTERNAL_ERROR;
2858 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
2859 goto f_err;
2860 }
0f113f3e
MC
2861 /* Check if pubkey from client certificate was used */
2862 if (EVP_PKEY_CTX_ctrl
2863 (pkey_ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 2, NULL) > 0)
2864 ret = 2;
2865 else
2866 ret = 1;
2867 gerr:
2868 EVP_PKEY_free(client_pub_pkey);
2869 EVP_PKEY_CTX_free(pkey_ctx);
2870 if (ret)
2871 return ret;
c5ba2d99 2872 goto err;
0f113f3e
MC
2873 } else {
2874 al = SSL_AD_HANDSHAKE_FAILURE;
2875 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE, SSL_R_UNKNOWN_CIPHER_TYPE);
2876 goto f_err;
2877 }
2878
2879 return (1);
2880 f_err:
2881 ssl3_send_alert(s, SSL3_AL_FATAL, al);
556efe79 2882#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA) || !defined(OPENSSL_NO_EC) || defined(OPENSSL_NO_SRP)
0f113f3e 2883 err:
ea262260 2884#endif
556efe79 2885#ifndef OPENSSL_NO_EC
0f113f3e
MC
2886 EVP_PKEY_free(clnt_pub_pkey);
2887 EC_POINT_free(clnt_ecpoint);
8fdc3734 2888 EC_KEY_free(srvr_ecdh);
0f113f3e 2889 BN_CTX_free(bn_ctx);
58964a49 2890#endif
0f113f3e
MC
2891 return (-1);
2892}
d02b48c6 2893
36d16f8e 2894int ssl3_get_cert_verify(SSL *s)
0f113f3e
MC
2895{
2896 EVP_PKEY *pkey = NULL;
2897 unsigned char *p;
2898 int al, ok, ret = 0;
2899 long n;
2900 int type = 0, i, j;
2901 X509 *peer;
2902 const EVP_MD *md = NULL;
2903 EVP_MD_CTX mctx;
2904 EVP_MD_CTX_init(&mctx);
2905
2906 n = s->method->ssl_get_message(s,
2907 SSL3_ST_SR_CERT_VRFY_A,
2908 SSL3_ST_SR_CERT_VRFY_B,
2909 -1, SSL3_RT_MAX_PLAIN_LENGTH, &ok);
2910
2911 if (!ok)
2912 return ((int)n);
2913
2914 if (s->session->peer != NULL) {
2915 peer = s->session->peer;
2916 pkey = X509_get_pubkey(peer);
2917 type = X509_certificate_type(peer, pkey);
2918 } else {
2919 peer = NULL;
2920 pkey = NULL;
2921 }
2922
2923 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY) {
2924 s->s3->tmp.reuse_message = 1;
2925 if (peer != NULL) {
2926 al = SSL_AD_UNEXPECTED_MESSAGE;
2927 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_MISSING_VERIFY_MESSAGE);
2928 goto f_err;
2929 }
2930 ret = 1;
2931 goto end;
2932 }
2933
2934 if (peer == NULL) {
2935 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_NO_CLIENT_CERT_RECEIVED);
2936 al = SSL_AD_UNEXPECTED_MESSAGE;
2937 goto f_err;
2938 }
2939
2940 if (!(type & EVP_PKT_SIGN)) {
2941 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,
2942 SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
2943 al = SSL_AD_ILLEGAL_PARAMETER;
2944 goto f_err;
2945 }
2946
2947 if (s->s3->change_cipher_spec) {
2948 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_CCS_RECEIVED_EARLY);
2949 al = SSL_AD_UNEXPECTED_MESSAGE;
2950 goto f_err;
2951 }
2952
2953 /* we now have a signature that we need to verify */
2954 p = (unsigned char *)s->init_msg;
2955 /* Check for broken implementations of GOST ciphersuites */
2956 /*
2957 * If key is GOST and n is exactly 64, it is bare signature without
2958 * length field
2959 */
2960 if (n == 64 && (pkey->type == NID_id_GostR3410_94 ||
2961 pkey->type == NID_id_GostR3410_2001)) {
2962 i = 64;
2963 } else {
2964 if (SSL_USE_SIGALGS(s)) {
2965 int rv = tls12_check_peer_sigalg(&md, s, p, pkey);
2966 if (rv == -1) {
2967 al = SSL_AD_INTERNAL_ERROR;
2968 goto f_err;
2969 } else if (rv == 0) {
2970 al = SSL_AD_DECODE_ERROR;
2971 goto f_err;
2972 }
f37f20ff 2973#ifdef SSL_DEBUG
0f113f3e 2974 fprintf(stderr, "USING TLSv1.2 HASH %s\n", EVP_MD_name(md));
f37f20ff 2975#endif
0f113f3e
MC
2976 p += 2;
2977 n -= 2;
2978 }
2979 n2s(p, i);
2980 n -= 2;
2981 if (i > n) {
2982 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_LENGTH_MISMATCH);
2983 al = SSL_AD_DECODE_ERROR;
2984 goto f_err;
2985 }
2986 }
2987 j = EVP_PKEY_size(pkey);
2988 if ((i > j) || (n > j) || (n <= 0)) {
2989 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_WRONG_SIGNATURE_SIZE);
2990 al = SSL_AD_DECODE_ERROR;
2991 goto f_err;
2992 }
2993
2994 if (SSL_USE_SIGALGS(s)) {
2995 long hdatalen = 0;
2996 void *hdata;
2997 hdatalen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
2998 if (hdatalen <= 0) {
2999 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3000 al = SSL_AD_INTERNAL_ERROR;
3001 goto f_err;
3002 }
f37f20ff 3003#ifdef SSL_DEBUG
0f113f3e
MC
3004 fprintf(stderr, "Using TLS 1.2 with client verify alg %s\n",
3005 EVP_MD_name(md));
f37f20ff 3006#endif
0f113f3e
MC
3007 if (!EVP_VerifyInit_ex(&mctx, md, NULL)
3008 || !EVP_VerifyUpdate(&mctx, hdata, hdatalen)) {
3009 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_EVP_LIB);
3010 al = SSL_AD_INTERNAL_ERROR;
3011 goto f_err;
3012 }
3013
3014 if (EVP_VerifyFinal(&mctx, p, i, pkey) <= 0) {
3015 al = SSL_AD_DECRYPT_ERROR;
3016 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_SIGNATURE);
3017 goto f_err;
3018 }
3019 } else
3020#ifndef OPENSSL_NO_RSA
3021 if (pkey->type == EVP_PKEY_RSA) {
3022 i = RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
3023 MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH, p, i,
3024 pkey->pkey.rsa);
3025 if (i < 0) {
3026 al = SSL_AD_DECRYPT_ERROR;
3027 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_DECRYPT);
3028 goto f_err;
3029 }
3030 if (i == 0) {
3031 al = SSL_AD_DECRYPT_ERROR;
3032 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_RSA_SIGNATURE);
3033 goto f_err;
3034 }
3035 } else
d02b48c6 3036#endif
bc36ee62 3037#ifndef OPENSSL_NO_DSA
0f113f3e
MC
3038 if (pkey->type == EVP_PKEY_DSA) {
3039 j = DSA_verify(pkey->save_type,
3040 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3041 SHA_DIGEST_LENGTH, p, i, pkey->pkey.dsa);
3042 if (j <= 0) {
3043 /* bad signature */
3044 al = SSL_AD_DECRYPT_ERROR;
3045 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_DSA_SIGNATURE);
3046 goto f_err;
3047 }
3048 } else
ea262260 3049#endif
10bf4fc2 3050#ifndef OPENSSL_NO_EC
0f113f3e
MC
3051 if (pkey->type == EVP_PKEY_EC) {
3052 j = ECDSA_verify(pkey->save_type,
3053 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
3054 SHA_DIGEST_LENGTH, p, i, pkey->pkey.ec);
3055 if (j <= 0) {
3056 /* bad signature */
3057 al = SSL_AD_DECRYPT_ERROR;
3058 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3059 goto f_err;
3060 }
3061 } else
d02b48c6 3062#endif
0f113f3e
MC
3063 if (pkey->type == NID_id_GostR3410_94
3064 || pkey->type == NID_id_GostR3410_2001) {
3065 unsigned char signature[64];
3066 int idx;
3067 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(pkey, NULL);
3068 EVP_PKEY_verify_init(pctx);
3069 if (i != 64) {
3070 fprintf(stderr, "GOST signature length is %d", i);
3071 }
3072 for (idx = 0; idx < 64; idx++) {
3073 signature[63 - idx] = p[idx];
3074 }
3075 j = EVP_PKEY_verify(pctx, signature, 64, s->s3->tmp.cert_verify_md,
3076 32);
3077 EVP_PKEY_CTX_free(pctx);
3078 if (j <= 0) {
3079 al = SSL_AD_DECRYPT_ERROR;
3080 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, SSL_R_BAD_ECDSA_SIGNATURE);
3081 goto f_err;
3082 }
3083 } else {
3084 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY, ERR_R_INTERNAL_ERROR);
3085 al = SSL_AD_UNSUPPORTED_CERTIFICATE;
3086 goto f_err;
3087 }
3088
3089 ret = 1;
3090 if (0) {
3091 f_err:
3092 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3093 }
3094 end:
3095 if (s->s3->handshake_buffer) {
3096 BIO_free(s->s3->handshake_buffer);
3097 s->s3->handshake_buffer = NULL;
3098 s->s3->flags &= ~TLS1_FLAGS_KEEP_HANDSHAKE;
3099 }
3100 EVP_MD_CTX_cleanup(&mctx);
3101 EVP_PKEY_free(pkey);
3102 return (ret);
3103}
d02b48c6 3104
36d16f8e 3105int ssl3_get_client_certificate(SSL *s)
0f113f3e
MC
3106{
3107 int i, ok, al, ret = -1;
3108 X509 *x = NULL;
3109 unsigned long l, nc, llen, n;
3110 const unsigned char *p, *q;
3111 unsigned char *d;
3112 STACK_OF(X509) *sk = NULL;
3113
3114 n = s->method->ssl_get_message(s,
3115 SSL3_ST_SR_CERT_A,
3116 SSL3_ST_SR_CERT_B,
3117 -1, s->max_cert_list, &ok);
3118
3119 if (!ok)
3120 return ((int)n);
3121
3122 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) {
3123 if ((s->verify_mode & SSL_VERIFY_PEER) &&
3124 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3125 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3126 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3127 al = SSL_AD_HANDSHAKE_FAILURE;
3128 goto f_err;
3129 }
3130 /*
3131 * If tls asked for a client cert, the client must return a 0 list
3132 */
3133 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request) {
3134 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3135 SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
3136 al = SSL_AD_UNEXPECTED_MESSAGE;
3137 goto f_err;
3138 }
3139 s->s3->tmp.reuse_message = 1;
3140 return (1);
3141 }
3142
3143 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) {
3144 al = SSL_AD_UNEXPECTED_MESSAGE;
3145 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_WRONG_MESSAGE_TYPE);
3146 goto f_err;
3147 }
3148 p = d = (unsigned char *)s->init_msg;
3149
3150 if ((sk = sk_X509_new_null()) == NULL) {
3151 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3152 goto err;
3153 }
3154
3155 n2l3(p, llen);
3156 if (llen + 3 != n) {
3157 al = SSL_AD_DECODE_ERROR;
3158 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, SSL_R_LENGTH_MISMATCH);
3159 goto f_err;
3160 }
3161 for (nc = 0; nc < llen;) {
3162 n2l3(p, l);
3163 if ((l + nc + 3) > llen) {
3164 al = SSL_AD_DECODE_ERROR;
3165 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3166 SSL_R_CERT_LENGTH_MISMATCH);
3167 goto f_err;
3168 }
3169
3170 q = p;
3171 x = d2i_X509(NULL, &p, l);
3172 if (x == NULL) {
3173 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_ASN1_LIB);
3174 goto err;
3175 }
3176 if (p != (q + l)) {
3177 al = SSL_AD_DECODE_ERROR;
3178 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3179 SSL_R_CERT_LENGTH_MISMATCH);
3180 goto f_err;
3181 }
3182 if (!sk_X509_push(sk, x)) {
3183 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3184 goto err;
3185 }
3186 x = NULL;
3187 nc += l + 3;
3188 }
3189
3190 if (sk_X509_num(sk) <= 0) {
3191 /* TLS does not mind 0 certs returned */
3192 if (s->version == SSL3_VERSION) {
3193 al = SSL_AD_HANDSHAKE_FAILURE;
3194 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3195 SSL_R_NO_CERTIFICATES_RETURNED);
3196 goto f_err;
3197 }
3198 /* Fail for TLS only if we required a certificate */
3199 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
3200 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) {
3201 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3202 SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
3203 al = SSL_AD_HANDSHAKE_FAILURE;
3204 goto f_err;
3205 }
3206 /* No client certificate so digest cached records */
3207 if (s->s3->handshake_buffer && !ssl3_digest_cached_records(s)) {
3208 al = SSL_AD_INTERNAL_ERROR;
3209 goto f_err;
3210 }
3211 } else {
3212 EVP_PKEY *pkey;
3213 i = ssl_verify_cert_chain(s, sk);
3214 if (i <= 0) {
3215 al = ssl_verify_alarm_type(s->verify_result);
3216 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3217 SSL_R_CERTIFICATE_VERIFY_FAILED);
3218 goto f_err;
3219 }
3220 if (i > 1) {
3221 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, i);
3222 al = SSL_AD_HANDSHAKE_FAILURE;
3223 goto f_err;
3224 }
3225 pkey = X509_get_pubkey(sk_X509_value(sk, 0));
3226 if (pkey == NULL) {
3227 al = SSL3_AD_HANDSHAKE_FAILURE;
3228 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,
3229 SSL_R_UNKNOWN_CERTIFICATE_TYPE);
3230 goto f_err;
3231 }
3232 EVP_PKEY_free(pkey);
3233 }
3234
3235 if (s->session->peer != NULL) /* This should not be needed */
3236 X509_free(s->session->peer);
3237 s->session->peer = sk_X509_shift(sk);
3238 s->session->verify_result = s->verify_result;
3239
3240 /*
3241 * With the current implementation, sess_cert will always be NULL when we
3242 * arrive here.
3243 */
3244 if (s->session->sess_cert == NULL) {
3245 s->session->sess_cert = ssl_sess_cert_new();
3246 if (s->session->sess_cert == NULL) {
3247 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
3248 goto err;
3249 }
3250 }
3251 if (s->session->sess_cert->cert_chain != NULL)
3252 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
3253 s->session->sess_cert->cert_chain = sk;
3254 /*
3255 * Inconsistency alert: cert_chain does *not* include the peer's own
3256 * certificate, while we do include it in s3_clnt.c
3257 */
3258
3259 sk = NULL;
3260
3261 ret = 1;
3262 if (0) {
3263 f_err:
3264 ssl3_send_alert(s, SSL3_AL_FATAL, al);
3265 }
3266 err:
3267 if (x != NULL)
3268 X509_free(x);
3269 if (sk != NULL)
3270 sk_X509_pop_free(sk, X509_free);
3271 return (ret);
3272}
d02b48c6 3273
6b691a5c 3274int ssl3_send_server_certificate(SSL *s)
0f113f3e
MC
3275{
3276 CERT_PKEY *cpk;
3277
3278 if (s->state == SSL3_ST_SW_CERT_A) {
3279 cpk = ssl_get_server_send_pkey(s);
3280 if (cpk == NULL) {
3281 /* VRS: allow null cert if auth == KRB5 */
3282 if ((s->s3->tmp.new_cipher->algorithm_auth != SSL_aKRB5) ||
3283 (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kKRB5)) {
3284 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,
3285 ERR_R_INTERNAL_ERROR);
3286 return (0);
3287 }
3288 }
3289
3290 if (!ssl3_output_cert_chain(s, cpk)) {
3291 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE, ERR_R_INTERNAL_ERROR);
3292 return (0);
3293 }
3294 s->state = SSL3_ST_SW_CERT_B;
3295 }
3296
3297 /* SSL3_ST_SW_CERT_B */
3298 return ssl_do_write(s);
3299}
c519e89f 3300
ddd3a617 3301#ifndef OPENSSL_NO_TLSEXT
c519e89f 3302/* send a new session ticket (not necessarily for a new session) */
6434abbf 3303int ssl3_send_newsession_ticket(SSL *s)
0f113f3e 3304{
687eaf27
MC
3305 unsigned char *senc = NULL;
3306 EVP_CIPHER_CTX ctx;
3307 HMAC_CTX hctx;
3308
0f113f3e 3309 if (s->state == SSL3_ST_SW_SESSION_TICKET_A) {
687eaf27 3310 unsigned char *p, *macstart;
0f113f3e
MC
3311 const unsigned char *const_p;
3312 int len, slen_full, slen;
3313 SSL_SESSION *sess;
3314 unsigned int hlen;
0f113f3e
MC
3315 SSL_CTX *tctx = s->initial_ctx;
3316 unsigned char iv[EVP_MAX_IV_LENGTH];
3317 unsigned char key_name[16];
3318
3319 /* get session encoding length */
3320 slen_full = i2d_SSL_SESSION(s->session, NULL);
3321 /*
3322 * Some length values are 16 bits, so forget it if session is too
3323 * long
3324 */
687eaf27 3325 if (slen_full == 0 || slen_full > 0xFF00)
0f113f3e
MC
3326 return -1;
3327 senc = OPENSSL_malloc(slen_full);
3328 if (!senc)
3329 return -1;
687eaf27
MC
3330
3331 EVP_CIPHER_CTX_init(&ctx);
3332 HMAC_CTX_init(&hctx);
3333
0f113f3e 3334 p = senc;
687eaf27
MC
3335 if (!i2d_SSL_SESSION(s->session, &p))
3336 goto err;
0f113f3e
MC
3337
3338 /*
3339 * create a fresh copy (not shared with other threads) to clean up
3340 */
3341 const_p = senc;
3342 sess = d2i_SSL_SESSION(NULL, &const_p, slen_full);
687eaf27
MC
3343 if (sess == NULL)
3344 goto err;
0f113f3e
MC
3345 sess->session_id_length = 0; /* ID is irrelevant for the ticket */
3346
3347 slen = i2d_SSL_SESSION(sess, NULL);
687eaf27
MC
3348 if (slen == 0 || slen > slen_full) { /* shouldn't ever happen */
3349 SSL_SESSION_free(sess);
3350 goto err;
0f113f3e
MC
3351 }
3352 p = senc;
687eaf27
MC
3353 if (!i2d_SSL_SESSION(sess, &p)) {
3354 SSL_SESSION_free(sess);
3355 goto err;
3356 }
0f113f3e
MC
3357 SSL_SESSION_free(sess);
3358
50e735f9
MC
3359 /*-
3360 * Grow buffer if need be: the length calculation is as
3361 * follows handshake_header_length +
3362 * 4 (ticket lifetime hint) + 2 (ticket length) +
3363 * 16 (key name) + max_iv_len (iv length) +
3364 * session_length + max_enc_block_size (max encrypted session
3365 * length) + max_md_size (HMAC).
3366 */
0f113f3e
MC
3367 if (!BUF_MEM_grow(s->init_buf,
3368 SSL_HM_HEADER_LENGTH(s) + 22 + EVP_MAX_IV_LENGTH +
3369 EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE + slen))
687eaf27
MC
3370 goto err;
3371
0f113f3e 3372 p = ssl_handshake_start(s);
0f113f3e
MC
3373 /*
3374 * Initialize HMAC and cipher contexts. If callback present it does
3375 * all the work otherwise use generated values from parent ctx.
3376 */
3377 if (tctx->tlsext_ticket_key_cb) {
3378 if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx,
687eaf27
MC
3379 &hctx, 1) < 0)
3380 goto err;
0f113f3e 3381 } else {
687eaf27
MC
3382 if (RAND_bytes(iv, 16) <= 0)
3383 goto err;
3384 if (!EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
3385 tctx->tlsext_tick_aes_key, iv))
3386 goto err;
3387 if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16,
3388 EVP_sha256(), NULL))
3389 goto err;
0f113f3e
MC
3390 memcpy(key_name, tctx->tlsext_tick_key_name, 16);
3391 }
3392
3393 /*
3394 * Ticket lifetime hint (advisory only): We leave this unspecified
3395 * for resumed session (for simplicity), and guess that tickets for
3396 * new sessions will live as long as their sessions.
3397 */
3398 l2n(s->hit ? 0 : s->session->timeout, p);
3399
3400 /* Skip ticket length for now */
3401 p += 2;
3402 /* Output key name */
3403 macstart = p;
3404 memcpy(p, key_name, 16);
3405 p += 16;
3406 /* output IV */
3407 memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx));
3408 p += EVP_CIPHER_CTX_iv_length(&ctx);
3409 /* Encrypt session data */
687eaf27
MC
3410 if (!EVP_EncryptUpdate(&ctx, p, &len, senc, slen))
3411 goto err;
0f113f3e 3412 p += len;
687eaf27
MC
3413 if (!EVP_EncryptFinal(&ctx, p, &len))
3414 goto err;
0f113f3e 3415 p += len;
0f113f3e 3416
687eaf27
MC
3417 if (!HMAC_Update(&hctx, macstart, p - macstart))
3418 goto err;
3419 if (!HMAC_Final(&hctx, p, &hlen))
3420 goto err;
3421
3422 EVP_CIPHER_CTX_cleanup(&ctx);
0f113f3e
MC
3423 HMAC_CTX_cleanup(&hctx);
3424
3425 p += hlen;
3426 /* Now write out lengths: p points to end of data written */
3427 /* Total length */
3428 len = p - ssl_handshake_start(s);
0f113f3e
MC
3429 /* Skip ticket lifetime hint */
3430 p = ssl_handshake_start(s) + 4;
3431 s2n(len - 6, p);
4f9fab6b
MC
3432 if(!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len))
3433 goto err;
0f113f3e
MC
3434 s->state = SSL3_ST_SW_SESSION_TICKET_B;
3435 OPENSSL_free(senc);
3436 }
3437
3438 /* SSL3_ST_SW_SESSION_TICKET_B */
3439 return ssl_do_write(s);
687eaf27
MC
3440 err:
3441 if (senc)
3442 OPENSSL_free(senc);
3443 EVP_CIPHER_CTX_cleanup(&ctx);
3444 HMAC_CTX_cleanup(&hctx);
3445 return -1;
0f113f3e 3446}
67c8e7f4
DSH
3447
3448int ssl3_send_cert_status(SSL *s)
0f113f3e
MC
3449{
3450 if (s->state == SSL3_ST_SW_CERT_STATUS_A) {
3451 unsigned char *p;
50e735f9
MC
3452 /*-
3453 * Grow buffer if need be: the length calculation is as
3454 * follows 1 (message type) + 3 (message length) +
3455 * 1 (ocsp response type) + 3 (ocsp response length)
3456 * + (ocsp response)
3457 */
0f113f3e
MC
3458 if (!BUF_MEM_grow(s->init_buf, 8 + s->tlsext_ocsp_resplen))
3459 return -1;
3460
3461 p = (unsigned char *)s->init_buf->data;
3462
3463 /* do the header */
3464 *(p++) = SSL3_MT_CERTIFICATE_STATUS;
3465 /* message length */
3466 l2n3(s->tlsext_ocsp_resplen + 4, p);
3467 /* status type */
3468 *(p++) = s->tlsext_status_type;
3469 /* length of OCSP response */
3470 l2n3(s->tlsext_ocsp_resplen, p);
3471 /* actual response */
3472 memcpy(p, s->tlsext_ocsp_resp, s->tlsext_ocsp_resplen);
3473 /* number of bytes to write */
3474 s->init_num = 8 + s->tlsext_ocsp_resplen;
3475 s->state = SSL3_ST_SW_CERT_STATUS_B;
3476 s->init_off = 0;
3477 }
3478
3479 /* SSL3_ST_SW_CERT_STATUS_B */
3480 return (ssl3_do_write(s, SSL3_RT_HANDSHAKE));
3481}
ee2ffc27 3482
71fa4513 3483# ifndef OPENSSL_NO_NEXTPROTONEG
0f113f3e
MC
3484/*
3485 * ssl3_get_next_proto reads a Next Protocol Negotiation handshake message.
3486 * It sets the next_proto member in s if found
3487 */
ee2ffc27 3488int ssl3_get_next_proto(SSL *s)
0f113f3e
MC
3489{
3490 int ok;
3491 int proto_len, padding_len;
3492 long n;
3493 const unsigned char *p;
3494
3495 /*
3496 * Clients cannot send a NextProtocol message if we didn't see the
3497 * extension in their ClientHello
3498 */
3499 if (!s->s3->next_proto_neg_seen) {
3500 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO,
3501 SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION);
3502 return -1;
3503 }
3504
3505 /* See the payload format below */
3506 n = s->method->ssl_get_message(s,
3507 SSL3_ST_SR_NEXT_PROTO_A,
3508 SSL3_ST_SR_NEXT_PROTO_B,
3509 SSL3_MT_NEXT_PROTO, 514, &ok);
3510
3511 if (!ok)
3512 return ((int)n);
3513
3514 /*
3515 * s->state doesn't reflect whether ChangeCipherSpec has been received in
3516 * this handshake, but s->s3->change_cipher_spec does (will be reset by
3517 * ssl3_get_finished).
3518 */
3519 if (!s->s3->change_cipher_spec) {
3520 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS);
3521 return -1;
3522 }
3523
3524 if (n < 2)
3525 return 0; /* The body must be > 1 bytes long */
3526
3527 p = (unsigned char *)s->init_msg;
3528
50e735f9
MC
3529 /*-
3530 * The payload looks like:
3531 * uint8 proto_len;
3532 * uint8 proto[proto_len];
3533 * uint8 padding_len;
3534 * uint8 padding[padding_len];
3535 */
0f113f3e
MC
3536 proto_len = p[0];
3537 if (proto_len + 2 > s->init_num)
3538 return 0;
3539 padding_len = p[proto_len + 1];
3540 if (proto_len + padding_len + 2 != s->init_num)
3541 return 0;
3542
3543 s->next_proto_negotiated = OPENSSL_malloc(proto_len);
3544 if (!s->next_proto_negotiated) {
3545 SSLerr(SSL_F_SSL3_GET_NEXT_PROTO, ERR_R_MALLOC_FAILURE);
3546 return 0;
3547 }
3548 memcpy(s->next_proto_negotiated, p + 1, proto_len);
3549 s->next_proto_negotiated_len = proto_len;
3550
3551 return 1;
3552}
ee2ffc27 3553# endif
a9e1c50b 3554
6434abbf 3555#endif