source: openssl/trunk/ssl/s3_srvr.c@ 808

Last change on this file since 808 was 808, checked in by dmik, 11 years ago

openssl: Merge version 1.0.0n from vendor to trunk.

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