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