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