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.c2055
1 files changed, 0 insertions, 2055 deletions
diff --git a/src/lib/libssl/s3_srvr.c b/src/lib/libssl/s3_srvr.c
deleted file mode 100644
index cd7b88eeb5..0000000000
--- a/src/lib/libssl/s3_srvr.c
+++ /dev/null
@@ -1,2055 +0,0 @@
1/* ssl/s3_srvr.c */
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-2002 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#define REUSE_CIPHER_BUG
113#define NETSCAPE_HANG_BUG
114
115
116#include <stdio.h>
117#include "ssl_locl.h"
118#include "kssl_lcl.h"
119#include <openssl/buffer.h>
120#include <openssl/rand.h>
121#include <openssl/objects.h>
122#include <openssl/evp.h>
123#include <openssl/x509.h>
124#ifndef OPENSSL_NO_KRB5
125#include <openssl/krb5_asn.h>
126#endif
127#include <openssl/md5.h>
128
129static SSL_METHOD *ssl3_get_server_method(int ver);
130static int ssl3_get_client_hello(SSL *s);
131static int ssl3_check_client_hello(SSL *s);
132static int ssl3_send_server_hello(SSL *s);
133static int ssl3_send_server_key_exchange(SSL *s);
134static int ssl3_send_certificate_request(SSL *s);
135static int ssl3_send_server_done(SSL *s);
136static int ssl3_get_client_key_exchange(SSL *s);
137static int ssl3_get_client_certificate(SSL *s);
138static int ssl3_get_cert_verify(SSL *s);
139static int ssl3_send_hello_request(SSL *s);
140
141static SSL_METHOD *ssl3_get_server_method(int ver)
142 {
143 if (ver == SSL3_VERSION)
144 return(SSLv3_server_method());
145 else
146 return(NULL);
147 }
148
149SSL_METHOD *SSLv3_server_method(void)
150 {
151 static int init=1;
152 static SSL_METHOD SSLv3_server_data;
153
154 if (init)
155 {
156 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
157
158 if (init)
159 {
160 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
161 sizeof(SSL_METHOD));
162 SSLv3_server_data.ssl_accept=ssl3_accept;
163 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
164 init=0;
165 }
166
167 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
168 }
169 return(&SSLv3_server_data);
170 }
171
172int ssl3_accept(SSL *s)
173 {
174 BUF_MEM *buf;
175 unsigned long l,Time=time(NULL);
176 void (*cb)(const SSL *ssl,int type,int val)=NULL;
177 long num1;
178 int ret= -1;
179 int new_state,state,skip=0;
180
181 RAND_add(&Time,sizeof(Time),0);
182 ERR_clear_error();
183 clear_sys_error();
184
185 if (s->info_callback != NULL)
186 cb=s->info_callback;
187 else if (s->ctx->info_callback != NULL)
188 cb=s->ctx->info_callback;
189
190 /* init things to blank */
191 s->in_handshake++;
192 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
193
194 if (s->cert == NULL)
195 {
196 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
197 return(-1);
198 }
199
200 for (;;)
201 {
202 state=s->state;
203
204 switch (s->state)
205 {
206 case SSL_ST_RENEGOTIATE:
207 s->new_session=1;
208 /* s->state=SSL_ST_ACCEPT; */
209
210 case SSL_ST_BEFORE:
211 case SSL_ST_ACCEPT:
212 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
213 case SSL_ST_OK|SSL_ST_ACCEPT:
214
215 s->server=1;
216 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
217
218 if ((s->version>>8) != 3)
219 {
220 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
221 return -1;
222 }
223 s->type=SSL_ST_ACCEPT;
224
225 if (s->init_buf == NULL)
226 {
227 if ((buf=BUF_MEM_new()) == NULL)
228 {
229 ret= -1;
230 goto end;
231 }
232 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
233 {
234 ret= -1;
235 goto end;
236 }
237 s->init_buf=buf;
238 }
239
240 if (!ssl3_setup_buffers(s))
241 {
242 ret= -1;
243 goto end;
244 }
245
246 s->init_num=0;
247
248 if (s->state != SSL_ST_RENEGOTIATE)
249 {
250 /* Ok, we now need to push on a buffering BIO so that
251 * the output is sent in a way that TCP likes :-)
252 */
253 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
254
255 ssl3_init_finished_mac(s);
256 s->state=SSL3_ST_SR_CLNT_HELLO_A;
257 s->ctx->stats.sess_accept++;
258 }
259 else
260 {
261 /* s->state == SSL_ST_RENEGOTIATE,
262 * we will just send a HelloRequest */
263 s->ctx->stats.sess_accept_renegotiate++;
264 s->state=SSL3_ST_SW_HELLO_REQ_A;
265 }
266 break;
267
268 case SSL3_ST_SW_HELLO_REQ_A:
269 case SSL3_ST_SW_HELLO_REQ_B:
270
271 s->shutdown=0;
272 ret=ssl3_send_hello_request(s);
273 if (ret <= 0) goto end;
274 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
275 s->state=SSL3_ST_SW_FLUSH;
276 s->init_num=0;
277
278 ssl3_init_finished_mac(s);
279 break;
280
281 case SSL3_ST_SW_HELLO_REQ_C:
282 s->state=SSL_ST_OK;
283 break;
284
285 case SSL3_ST_SR_CLNT_HELLO_A:
286 case SSL3_ST_SR_CLNT_HELLO_B:
287 case SSL3_ST_SR_CLNT_HELLO_C:
288
289 s->shutdown=0;
290 ret=ssl3_get_client_hello(s);
291 if (ret <= 0) goto end;
292 s->new_session = 2;
293 s->state=SSL3_ST_SW_SRVR_HELLO_A;
294 s->init_num=0;
295 break;
296
297 case SSL3_ST_SW_SRVR_HELLO_A:
298 case SSL3_ST_SW_SRVR_HELLO_B:
299 ret=ssl3_send_server_hello(s);
300 if (ret <= 0) goto end;
301
302 if (s->hit)
303 s->state=SSL3_ST_SW_CHANGE_A;
304 else
305 s->state=SSL3_ST_SW_CERT_A;
306 s->init_num=0;
307 break;
308
309 case SSL3_ST_SW_CERT_A:
310 case SSL3_ST_SW_CERT_B:
311 /* Check if it is anon DH */
312 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
313 {
314 ret=ssl3_send_server_certificate(s);
315 if (ret <= 0) goto end;
316 }
317 else
318 skip=1;
319 s->state=SSL3_ST_SW_KEY_EXCH_A;
320 s->init_num=0;
321 break;
322
323 case SSL3_ST_SW_KEY_EXCH_A:
324 case SSL3_ST_SW_KEY_EXCH_B:
325 l=s->s3->tmp.new_cipher->algorithms;
326
327 /* clear this, it may get reset by
328 * send_server_key_exchange */
329 if ((s->options & SSL_OP_EPHEMERAL_RSA)
330#ifndef OPENSSL_NO_KRB5
331 && !(l & SSL_KRB5)
332#endif /* OPENSSL_NO_KRB5 */
333 )
334 /* option SSL_OP_EPHEMERAL_RSA sends temporary RSA key
335 * even when forbidden by protocol specs
336 * (handshake may fail as clients are not required to
337 * be able to handle this) */
338 s->s3->tmp.use_rsa_tmp=1;
339 else
340 s->s3->tmp.use_rsa_tmp=0;
341
342 /* only send if a DH key exchange, fortezza or
343 * RSA but we have a sign only certificate */
344 if (s->s3->tmp.use_rsa_tmp
345 || (l & (SSL_DH|SSL_kFZA))
346 || ((l & SSL_kRSA)
347 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
348 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
349 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
350 )
351 )
352 )
353 )
354 {
355 ret=ssl3_send_server_key_exchange(s);
356 if (ret <= 0) goto end;
357 }
358 else
359 skip=1;
360
361 s->state=SSL3_ST_SW_CERT_REQ_A;
362 s->init_num=0;
363 break;
364
365 case SSL3_ST_SW_CERT_REQ_A:
366 case SSL3_ST_SW_CERT_REQ_B:
367 if (/* don't request cert unless asked for it: */
368 !(s->verify_mode & SSL_VERIFY_PEER) ||
369 /* if SSL_VERIFY_CLIENT_ONCE is set,
370 * don't request cert during re-negotiation: */
371 ((s->session->peer != NULL) &&
372 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
373 /* never request cert in anonymous ciphersuites
374 * (see section "Certificate request" in SSL 3 drafts
375 * and in RFC 2246): */
376 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
377 /* ... except when the application insists on verification
378 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
379 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) ||
380 /* never request cert in Kerberos ciphersuites */
381 (s->s3->tmp.new_cipher->algorithms & SSL_aKRB5))
382 {
383 /* no cert request */
384 skip=1;
385 s->s3->tmp.cert_request=0;
386 s->state=SSL3_ST_SW_SRVR_DONE_A;
387 }
388 else
389 {
390 s->s3->tmp.cert_request=1;
391 ret=ssl3_send_certificate_request(s);
392 if (ret <= 0) goto end;
393#ifndef NETSCAPE_HANG_BUG
394 s->state=SSL3_ST_SW_SRVR_DONE_A;
395#else
396 s->state=SSL3_ST_SW_FLUSH;
397 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
398#endif
399 s->init_num=0;
400 }
401 break;
402
403 case SSL3_ST_SW_SRVR_DONE_A:
404 case SSL3_ST_SW_SRVR_DONE_B:
405 ret=ssl3_send_server_done(s);
406 if (ret <= 0) goto end;
407 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
408 s->state=SSL3_ST_SW_FLUSH;
409 s->init_num=0;
410 break;
411
412 case SSL3_ST_SW_FLUSH:
413 /* number of bytes to be flushed */
414 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
415 if (num1 > 0)
416 {
417 s->rwstate=SSL_WRITING;
418 num1=BIO_flush(s->wbio);
419 if (num1 <= 0) { ret= -1; goto end; }
420 s->rwstate=SSL_NOTHING;
421 }
422
423 s->state=s->s3->tmp.next_state;
424 break;
425
426 case SSL3_ST_SR_CERT_A:
427 case SSL3_ST_SR_CERT_B:
428 /* Check for second client hello (MS SGC) */
429 ret = ssl3_check_client_hello(s);
430 if (ret <= 0)
431 goto end;
432 if (ret == 2)
433 s->state = SSL3_ST_SR_CLNT_HELLO_C;
434 else {
435 /* could be sent for a DH cert, even if we
436 * have not asked for it :-) */
437 ret=ssl3_get_client_certificate(s);
438 if (ret <= 0) goto end;
439 s->init_num=0;
440 s->state=SSL3_ST_SR_KEY_EXCH_A;
441 }
442 break;
443
444 case SSL3_ST_SR_KEY_EXCH_A:
445 case SSL3_ST_SR_KEY_EXCH_B:
446 ret=ssl3_get_client_key_exchange(s);
447 if (ret <= 0) goto end;
448 s->state=SSL3_ST_SR_CERT_VRFY_A;
449 s->init_num=0;
450
451 /* We need to get hashes here so if there is
452 * a client cert, it can be verified */
453 s->method->ssl3_enc->cert_verify_mac(s,
454 &(s->s3->finish_dgst1),
455 &(s->s3->tmp.cert_verify_md[0]));
456 s->method->ssl3_enc->cert_verify_mac(s,
457 &(s->s3->finish_dgst2),
458 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
459
460 break;
461
462 case SSL3_ST_SR_CERT_VRFY_A:
463 case SSL3_ST_SR_CERT_VRFY_B:
464
465 /* we should decide if we expected this one */
466 ret=ssl3_get_cert_verify(s);
467 if (ret <= 0) goto end;
468
469 s->state=SSL3_ST_SR_FINISHED_A;
470 s->init_num=0;
471 break;
472
473 case SSL3_ST_SR_FINISHED_A:
474 case SSL3_ST_SR_FINISHED_B:
475 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
476 SSL3_ST_SR_FINISHED_B);
477 if (ret <= 0) goto end;
478 if (s->hit)
479 s->state=SSL_ST_OK;
480 else
481 s->state=SSL3_ST_SW_CHANGE_A;
482 s->init_num=0;
483 break;
484
485 case SSL3_ST_SW_CHANGE_A:
486 case SSL3_ST_SW_CHANGE_B:
487
488 s->session->cipher=s->s3->tmp.new_cipher;
489 if (!s->method->ssl3_enc->setup_key_block(s))
490 { ret= -1; goto end; }
491
492 ret=ssl3_send_change_cipher_spec(s,
493 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
494
495 if (ret <= 0) goto end;
496 s->state=SSL3_ST_SW_FINISHED_A;
497 s->init_num=0;
498
499 if (!s->method->ssl3_enc->change_cipher_state(s,
500 SSL3_CHANGE_CIPHER_SERVER_WRITE))
501 {
502 ret= -1;
503 goto end;
504 }
505
506 break;
507
508 case SSL3_ST_SW_FINISHED_A:
509 case SSL3_ST_SW_FINISHED_B:
510 ret=ssl3_send_finished(s,
511 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
512 s->method->ssl3_enc->server_finished_label,
513 s->method->ssl3_enc->server_finished_label_len);
514 if (ret <= 0) goto end;
515 s->state=SSL3_ST_SW_FLUSH;
516 if (s->hit)
517 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
518 else
519 s->s3->tmp.next_state=SSL_ST_OK;
520 s->init_num=0;
521 break;
522
523 case SSL_ST_OK:
524 /* clean a few things up */
525 ssl3_cleanup_key_block(s);
526
527 BUF_MEM_free(s->init_buf);
528 s->init_buf=NULL;
529
530 /* remove buffering on output */
531 ssl_free_wbio_buffer(s);
532
533 s->init_num=0;
534
535 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
536 {
537 /* actually not necessarily a 'new' session unless
538 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
539
540 s->new_session=0;
541
542 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
543
544 s->ctx->stats.sess_accept_good++;
545 /* s->server=1; */
546 s->handshake_func=ssl3_accept;
547
548 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
549 }
550
551 ret = 1;
552 goto end;
553 /* break; */
554
555 default:
556 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
557 ret= -1;
558 goto end;
559 /* break; */
560 }
561
562 if (!s->s3->tmp.reuse_message && !skip)
563 {
564 if (s->debug)
565 {
566 if ((ret=BIO_flush(s->wbio)) <= 0)
567 goto end;
568 }
569
570
571 if ((cb != NULL) && (s->state != state))
572 {
573 new_state=s->state;
574 s->state=state;
575 cb(s,SSL_CB_ACCEPT_LOOP,1);
576 s->state=new_state;
577 }
578 }
579 skip=0;
580 }
581end:
582 /* BIO_flush(s->wbio); */
583
584 s->in_handshake--;
585 if (cb != NULL)
586 cb(s,SSL_CB_ACCEPT_EXIT,ret);
587 return(ret);
588 }
589
590static int ssl3_send_hello_request(SSL *s)
591 {
592 unsigned char *p;
593
594 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
595 {
596 p=(unsigned char *)s->init_buf->data;
597 *(p++)=SSL3_MT_HELLO_REQUEST;
598 *(p++)=0;
599 *(p++)=0;
600 *(p++)=0;
601
602 s->state=SSL3_ST_SW_HELLO_REQ_B;
603 /* number of bytes to write */
604 s->init_num=4;
605 s->init_off=0;
606 }
607
608 /* SSL3_ST_SW_HELLO_REQ_B */
609 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
610 }
611
612static int ssl3_check_client_hello(SSL *s)
613 {
614 int ok;
615 long n;
616
617 /* this function is called when we really expect a Certificate message,
618 * so permit appropriate message length */
619 n=ssl3_get_message(s,
620 SSL3_ST_SR_CERT_A,
621 SSL3_ST_SR_CERT_B,
622 -1,
623 s->max_cert_list,
624 &ok);
625 if (!ok) return((int)n);
626 s->s3->tmp.reuse_message = 1;
627 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
628 {
629 /* Throw away what we have done so far in the current handshake,
630 * which will now be aborted. (A full SSL_clear would be too much.)
631 * I hope that tmp.dh is the only thing that may need to be cleared
632 * when a handshake is not completed ... */
633#ifndef OPENSSL_NO_DH
634 if (s->s3->tmp.dh != NULL)
635 {
636 DH_free(s->s3->tmp.dh);
637 s->s3->tmp.dh = NULL;
638 }
639#endif
640 return 2;
641 }
642 return 1;
643}
644
645static int ssl3_get_client_hello(SSL *s)
646 {
647 int i,j,ok,al,ret= -1;
648 long n;
649 unsigned long id;
650 unsigned char *p,*d,*q;
651 SSL_CIPHER *c;
652 SSL_COMP *comp=NULL;
653 STACK_OF(SSL_CIPHER) *ciphers=NULL;
654
655 /* We do this so that we will respond with our native type.
656 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
657 * This down switching should be handled by a different method.
658 * If we are SSLv3, we will respond with SSLv3, even if prompted with
659 * TLSv1.
660 */
661 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
662 {
663 s->first_packet=1;
664 s->state=SSL3_ST_SR_CLNT_HELLO_B;
665 }
666 n=ssl3_get_message(s,
667 SSL3_ST_SR_CLNT_HELLO_B,
668 SSL3_ST_SR_CLNT_HELLO_C,
669 SSL3_MT_CLIENT_HELLO,
670 SSL3_RT_MAX_PLAIN_LENGTH,
671 &ok);
672
673 if (!ok) return((int)n);
674 d=p=(unsigned char *)s->init_msg;
675
676 /* use version from inside client hello, not from record header
677 * (may differ: see RFC 2246, Appendix E, second paragraph) */
678 s->client_version=(((int)p[0])<<8)|(int)p[1];
679 p+=2;
680
681 if (s->client_version < s->version)
682 {
683 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
684 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
685 {
686 /* similar to ssl3_get_record, send alert using remote version number */
687 s->version = s->client_version;
688 }
689 al = SSL_AD_PROTOCOL_VERSION;
690 goto f_err;
691 }
692
693 /* load the client random */
694 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
695 p+=SSL3_RANDOM_SIZE;
696
697 /* get the session-id */
698 j= *(p++);
699
700 s->hit=0;
701 /* Versions before 0.9.7 always allow session reuse during renegotiation
702 * (i.e. when s->new_session is true), option
703 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
704 * Maybe this optional behaviour should always have been the default,
705 * but we cannot safely change the default behaviour (or new applications
706 * might be written that become totally unsecure when compiled with
707 * an earlier library version)
708 */
709 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
710 {
711 if (!ssl_get_new_session(s,1))
712 goto err;
713 }
714 else
715 {
716 i=ssl_get_prev_session(s,p,j);
717 if (i == 1)
718 { /* previous session */
719 s->hit=1;
720 }
721 else if (i == -1)
722 goto err;
723 else /* i == 0 */
724 {
725 if (!ssl_get_new_session(s,1))
726 goto err;
727 }
728 }
729
730 p+=j;
731 n2s(p,i);
732 if ((i == 0) && (j != 0))
733 {
734 /* we need a cipher if we are not resuming a session */
735 al=SSL_AD_ILLEGAL_PARAMETER;
736 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
737 goto f_err;
738 }
739 if ((p+i) >= (d+n))
740 {
741 /* not enough data */
742 al=SSL_AD_DECODE_ERROR;
743 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
744 goto f_err;
745 }
746 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
747 == NULL))
748 {
749 goto err;
750 }
751 p+=i;
752
753 /* If it is a hit, check that the cipher is in the list */
754 if ((s->hit) && (i > 0))
755 {
756 j=0;
757 id=s->session->cipher->id;
758
759#ifdef CIPHER_DEBUG
760 printf("client sent %d ciphers\n",sk_num(ciphers));
761#endif
762 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
763 {
764 c=sk_SSL_CIPHER_value(ciphers,i);
765#ifdef CIPHER_DEBUG
766 printf("client [%2d of %2d]:%s\n",
767 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
768#endif
769 if (c->id == id)
770 {
771 j=1;
772 break;
773 }
774 }
775 if (j == 0)
776 {
777 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
778 {
779 /* Very bad for multi-threading.... */
780 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
781 0);
782 }
783 else
784 {
785 /* we need to have the cipher in the cipher
786 * list if we are asked to reuse it */
787 al=SSL_AD_ILLEGAL_PARAMETER;
788 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
789 goto f_err;
790 }
791 }
792 }
793
794 /* compression */
795 i= *(p++);
796 if ((p+i) > (d+n))
797 {
798 /* not enough data */
799 al=SSL_AD_DECODE_ERROR;
800 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
801 goto f_err;
802 }
803 q=p;
804 for (j=0; j<i; j++)
805 {
806 if (p[j] == 0) break;
807 }
808
809 p+=i;
810 if (j >= i)
811 {
812 /* no compress */
813 al=SSL_AD_DECODE_ERROR;
814 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
815 goto f_err;
816 }
817
818 /* Worst case, we will use the NULL compression, but if we have other
819 * options, we will now look for them. We have i-1 compression
820 * algorithms from the client, starting at q. */
821 s->s3->tmp.new_compression=NULL;
822 if (s->ctx->comp_methods != NULL)
823 { /* See if we have a match */
824 int m,nn,o,v,done=0;
825
826 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
827 for (m=0; m<nn; m++)
828 {
829 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
830 v=comp->id;
831 for (o=0; o<i; o++)
832 {
833 if (v == q[o])
834 {
835 done=1;
836 break;
837 }
838 }
839 if (done) break;
840 }
841 if (done)
842 s->s3->tmp.new_compression=comp;
843 else
844 comp=NULL;
845 }
846
847 /* TLS does not mind if there is extra stuff */
848 if (s->version == SSL3_VERSION)
849 {
850 if (p < (d+n))
851 {
852 /* wrong number of bytes,
853 * there could be more to follow */
854 al=SSL_AD_DECODE_ERROR;
855 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
856 goto f_err;
857 }
858 }
859
860 /* Given s->session->ciphers and SSL_get_ciphers, we must
861 * pick a cipher */
862
863 if (!s->hit)
864 {
865 s->session->compress_meth=(comp == NULL)?0:comp->id;
866 if (s->session->ciphers != NULL)
867 sk_SSL_CIPHER_free(s->session->ciphers);
868 s->session->ciphers=ciphers;
869 if (ciphers == NULL)
870 {
871 al=SSL_AD_ILLEGAL_PARAMETER;
872 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
873 goto f_err;
874 }
875 ciphers=NULL;
876 c=ssl3_choose_cipher(s,s->session->ciphers,
877 SSL_get_ciphers(s));
878
879 if (c == NULL)
880 {
881 al=SSL_AD_HANDSHAKE_FAILURE;
882 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
883 goto f_err;
884 }
885 s->s3->tmp.new_cipher=c;
886 }
887 else
888 {
889 /* Session-id reuse */
890#ifdef REUSE_CIPHER_BUG
891 STACK_OF(SSL_CIPHER) *sk;
892 SSL_CIPHER *nc=NULL;
893 SSL_CIPHER *ec=NULL;
894
895 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
896 {
897 sk=s->session->ciphers;
898 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
899 {
900 c=sk_SSL_CIPHER_value(sk,i);
901 if (c->algorithms & SSL_eNULL)
902 nc=c;
903 if (SSL_C_IS_EXPORT(c))
904 ec=c;
905 }
906 if (nc != NULL)
907 s->s3->tmp.new_cipher=nc;
908 else if (ec != NULL)
909 s->s3->tmp.new_cipher=ec;
910 else
911 s->s3->tmp.new_cipher=s->session->cipher;
912 }
913 else
914#endif
915 s->s3->tmp.new_cipher=s->session->cipher;
916 }
917
918 /* we now have the following setup.
919 * client_random
920 * cipher_list - our prefered list of ciphers
921 * ciphers - the clients prefered list of ciphers
922 * compression - basically ignored right now
923 * ssl version is set - sslv3
924 * s->session - The ssl session has been setup.
925 * s->hit - session reuse flag
926 * s->tmp.new_cipher - the new cipher to use.
927 */
928
929 ret=1;
930 if (0)
931 {
932f_err:
933 ssl3_send_alert(s,SSL3_AL_FATAL,al);
934 }
935err:
936 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
937 return(ret);
938 }
939
940static int ssl3_send_server_hello(SSL *s)
941 {
942 unsigned char *buf;
943 unsigned char *p,*d;
944 int i,sl;
945 unsigned long l,Time;
946
947 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
948 {
949 buf=(unsigned char *)s->init_buf->data;
950 p=s->s3->server_random;
951 Time=time(NULL); /* Time */
952 l2n(Time,p);
953 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
954 /* Do the message type and length last */
955 d=p= &(buf[4]);
956
957 *(p++)=s->version>>8;
958 *(p++)=s->version&0xff;
959
960 /* Random stuff */
961 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
962 p+=SSL3_RANDOM_SIZE;
963
964 /* now in theory we have 3 options to sending back the
965 * session id. If it is a re-use, we send back the
966 * old session-id, if it is a new session, we send
967 * back the new session-id or we send back a 0 length
968 * session-id if we want it to be single use.
969 * Currently I will not implement the '0' length session-id
970 * 12-Jan-98 - I'll now support the '0' length stuff.
971 */
972 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
973 s->session->session_id_length=0;
974
975 sl=s->session->session_id_length;
976 if (sl > sizeof s->session->session_id)
977 {
978 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
979 return -1;
980 }
981 *(p++)=sl;
982 memcpy(p,s->session->session_id,sl);
983 p+=sl;
984
985 /* put the cipher */
986 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
987 p+=i;
988
989 /* put the compression method */
990 if (s->s3->tmp.new_compression == NULL)
991 *(p++)=0;
992 else
993 *(p++)=s->s3->tmp.new_compression->id;
994
995 /* do the header */
996 l=(p-d);
997 d=buf;
998 *(d++)=SSL3_MT_SERVER_HELLO;
999 l2n3(l,d);
1000
1001 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1002 /* number of bytes to write */
1003 s->init_num=p-buf;
1004 s->init_off=0;
1005 }
1006
1007 /* SSL3_ST_CW_CLNT_HELLO_B */
1008 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1009 }
1010
1011static int ssl3_send_server_done(SSL *s)
1012 {
1013 unsigned char *p;
1014
1015 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1016 {
1017 p=(unsigned char *)s->init_buf->data;
1018
1019 /* do the header */
1020 *(p++)=SSL3_MT_SERVER_DONE;
1021 *(p++)=0;
1022 *(p++)=0;
1023 *(p++)=0;
1024
1025 s->state=SSL3_ST_SW_SRVR_DONE_B;
1026 /* number of bytes to write */
1027 s->init_num=4;
1028 s->init_off=0;
1029 }
1030
1031 /* SSL3_ST_CW_CLNT_HELLO_B */
1032 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1033 }
1034
1035static int ssl3_send_server_key_exchange(SSL *s)
1036 {
1037#ifndef OPENSSL_NO_RSA
1038 unsigned char *q;
1039 int j,num;
1040 RSA *rsa;
1041 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1042 unsigned int u;
1043#endif
1044#ifndef OPENSSL_NO_DH
1045 DH *dh=NULL,*dhp;
1046#endif
1047 EVP_PKEY *pkey;
1048 unsigned char *p,*d;
1049 int al,i;
1050 unsigned long type;
1051 int n;
1052 CERT *cert;
1053 BIGNUM *r[4];
1054 int nr[4],kn;
1055 BUF_MEM *buf;
1056 EVP_MD_CTX md_ctx;
1057
1058 EVP_MD_CTX_init(&md_ctx);
1059 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1060 {
1061 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1062 cert=s->cert;
1063
1064 buf=s->init_buf;
1065
1066 r[0]=r[1]=r[2]=r[3]=NULL;
1067 n=0;
1068#ifndef OPENSSL_NO_RSA
1069 if (type & SSL_kRSA)
1070 {
1071 rsa=cert->rsa_tmp;
1072 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1073 {
1074 rsa=s->cert->rsa_tmp_cb(s,
1075 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1076 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1077 if(rsa == NULL)
1078 {
1079 al=SSL_AD_HANDSHAKE_FAILURE;
1080 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1081 goto f_err;
1082 }
1083 RSA_up_ref(rsa);
1084 cert->rsa_tmp=rsa;
1085 }
1086 if (rsa == NULL)
1087 {
1088 al=SSL_AD_HANDSHAKE_FAILURE;
1089 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1090 goto f_err;
1091 }
1092 r[0]=rsa->n;
1093 r[1]=rsa->e;
1094 s->s3->tmp.use_rsa_tmp=1;
1095 }
1096 else
1097#endif
1098#ifndef OPENSSL_NO_DH
1099 if (type & SSL_kEDH)
1100 {
1101 dhp=cert->dh_tmp;
1102 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1103 dhp=s->cert->dh_tmp_cb(s,
1104 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1105 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1106 if (dhp == NULL)
1107 {
1108 al=SSL_AD_HANDSHAKE_FAILURE;
1109 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1110 goto f_err;
1111 }
1112
1113 if (s->s3->tmp.dh != NULL)
1114 {
1115 DH_free(dh);
1116 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1117 goto err;
1118 }
1119
1120 if ((dh=DHparams_dup(dhp)) == NULL)
1121 {
1122 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1123 goto err;
1124 }
1125
1126 s->s3->tmp.dh=dh;
1127 if ((dhp->pub_key == NULL ||
1128 dhp->priv_key == NULL ||
1129 (s->options & SSL_OP_SINGLE_DH_USE)))
1130 {
1131 if(!DH_generate_key(dh))
1132 {
1133 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1134 ERR_R_DH_LIB);
1135 goto err;
1136 }
1137 }
1138 else
1139 {
1140 dh->pub_key=BN_dup(dhp->pub_key);
1141 dh->priv_key=BN_dup(dhp->priv_key);
1142 if ((dh->pub_key == NULL) ||
1143 (dh->priv_key == NULL))
1144 {
1145 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1146 goto err;
1147 }
1148 }
1149 r[0]=dh->p;
1150 r[1]=dh->g;
1151 r[2]=dh->pub_key;
1152 }
1153 else
1154#endif
1155 {
1156 al=SSL_AD_HANDSHAKE_FAILURE;
1157 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1158 goto f_err;
1159 }
1160 for (i=0; r[i] != NULL; i++)
1161 {
1162 nr[i]=BN_num_bytes(r[i]);
1163 n+=2+nr[i];
1164 }
1165
1166 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1167 {
1168 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1169 == NULL)
1170 {
1171 al=SSL_AD_DECODE_ERROR;
1172 goto f_err;
1173 }
1174 kn=EVP_PKEY_size(pkey);
1175 }
1176 else
1177 {
1178 pkey=NULL;
1179 kn=0;
1180 }
1181
1182 if (!BUF_MEM_grow_clean(buf,n+4+kn))
1183 {
1184 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1185 goto err;
1186 }
1187 d=(unsigned char *)s->init_buf->data;
1188 p= &(d[4]);
1189
1190 for (i=0; r[i] != NULL; i++)
1191 {
1192 s2n(nr[i],p);
1193 BN_bn2bin(r[i],p);
1194 p+=nr[i];
1195 }
1196
1197 /* not anonymous */
1198 if (pkey != NULL)
1199 {
1200 /* n is the length of the params, they start at &(d[4])
1201 * and p points to the space at the end. */
1202#ifndef OPENSSL_NO_RSA
1203 if (pkey->type == EVP_PKEY_RSA)
1204 {
1205 q=md_buf;
1206 j=0;
1207 for (num=2; num > 0; num--)
1208 {
1209 EVP_DigestInit_ex(&md_ctx,(num == 2)
1210 ?s->ctx->md5:s->ctx->sha1, NULL);
1211 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1212 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1213 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1214 EVP_DigestFinal_ex(&md_ctx,q,
1215 (unsigned int *)&i);
1216 q+=i;
1217 j+=i;
1218 }
1219 if (RSA_sign(NID_md5_sha1, md_buf, j,
1220 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1221 {
1222 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1223 goto err;
1224 }
1225 s2n(u,p);
1226 n+=u+2;
1227 }
1228 else
1229#endif
1230#if !defined(OPENSSL_NO_DSA)
1231 if (pkey->type == EVP_PKEY_DSA)
1232 {
1233 /* lets do DSS */
1234 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1235 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1236 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1237 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1238 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1239 (unsigned int *)&i,pkey))
1240 {
1241 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1242 goto err;
1243 }
1244 s2n(i,p);
1245 n+=i+2;
1246 }
1247 else
1248#endif
1249 {
1250 /* Is this error check actually needed? */
1251 al=SSL_AD_HANDSHAKE_FAILURE;
1252 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1253 goto f_err;
1254 }
1255 }
1256
1257 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1258 l2n3(n,d);
1259
1260 /* we should now have things packed up, so lets send
1261 * it off */
1262 s->init_num=n+4;
1263 s->init_off=0;
1264 }
1265
1266 s->state = SSL3_ST_SW_KEY_EXCH_B;
1267 EVP_MD_CTX_cleanup(&md_ctx);
1268 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1269f_err:
1270 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1271err:
1272 EVP_MD_CTX_cleanup(&md_ctx);
1273 return(-1);
1274 }
1275
1276static int ssl3_send_certificate_request(SSL *s)
1277 {
1278 unsigned char *p,*d;
1279 int i,j,nl,off,n;
1280 STACK_OF(X509_NAME) *sk=NULL;
1281 X509_NAME *name;
1282 BUF_MEM *buf;
1283
1284 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1285 {
1286 buf=s->init_buf;
1287
1288 d=p=(unsigned char *)&(buf->data[4]);
1289
1290 /* get the list of acceptable cert types */
1291 p++;
1292 n=ssl3_get_req_cert_type(s,p);
1293 d[0]=n;
1294 p+=n;
1295 n++;
1296
1297 off=n;
1298 p+=2;
1299 n+=2;
1300
1301 sk=SSL_get_client_CA_list(s);
1302 nl=0;
1303 if (sk != NULL)
1304 {
1305 for (i=0; i<sk_X509_NAME_num(sk); i++)
1306 {
1307 name=sk_X509_NAME_value(sk,i);
1308 j=i2d_X509_NAME(name,NULL);
1309 if (!BUF_MEM_grow_clean(buf,4+n+j+2))
1310 {
1311 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1312 goto err;
1313 }
1314 p=(unsigned char *)&(buf->data[4+n]);
1315 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1316 {
1317 s2n(j,p);
1318 i2d_X509_NAME(name,&p);
1319 n+=2+j;
1320 nl+=2+j;
1321 }
1322 else
1323 {
1324 d=p;
1325 i2d_X509_NAME(name,&p);
1326 j-=2; s2n(j,d); j+=2;
1327 n+=j;
1328 nl+=j;
1329 }
1330 }
1331 }
1332 /* else no CA names */
1333 p=(unsigned char *)&(buf->data[4+off]);
1334 s2n(nl,p);
1335
1336 d=(unsigned char *)buf->data;
1337 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1338 l2n3(n,d);
1339
1340 /* we should now have things packed up, so lets send
1341 * it off */
1342
1343 s->init_num=n+4;
1344 s->init_off=0;
1345#ifdef NETSCAPE_HANG_BUG
1346 p=(unsigned char *)s->init_buf->data + s->init_num;
1347
1348 /* do the header */
1349 *(p++)=SSL3_MT_SERVER_DONE;
1350 *(p++)=0;
1351 *(p++)=0;
1352 *(p++)=0;
1353 s->init_num += 4;
1354#endif
1355
1356 }
1357
1358 /* SSL3_ST_SW_CERT_REQ_B */
1359 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1360err:
1361 return(-1);
1362 }
1363
1364static int ssl3_get_client_key_exchange(SSL *s)
1365 {
1366 int i,al,ok;
1367 long n;
1368 unsigned long l;
1369 unsigned char *p;
1370#ifndef OPENSSL_NO_RSA
1371 RSA *rsa=NULL;
1372 EVP_PKEY *pkey=NULL;
1373#endif
1374#ifndef OPENSSL_NO_DH
1375 BIGNUM *pub=NULL;
1376 DH *dh_srvr;
1377#endif
1378#ifndef OPENSSL_NO_KRB5
1379 KSSL_ERR kssl_err;
1380#endif /* OPENSSL_NO_KRB5 */
1381
1382 n=ssl3_get_message(s,
1383 SSL3_ST_SR_KEY_EXCH_A,
1384 SSL3_ST_SR_KEY_EXCH_B,
1385 SSL3_MT_CLIENT_KEY_EXCHANGE,
1386 2048, /* ??? */
1387 &ok);
1388
1389 if (!ok) return((int)n);
1390 p=(unsigned char *)s->init_msg;
1391
1392 l=s->s3->tmp.new_cipher->algorithms;
1393
1394#ifndef OPENSSL_NO_RSA
1395 if (l & SSL_kRSA)
1396 {
1397 /* FIX THIS UP EAY EAY EAY EAY */
1398 if (s->s3->tmp.use_rsa_tmp)
1399 {
1400 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1401 rsa=s->cert->rsa_tmp;
1402 /* Don't do a callback because rsa_tmp should
1403 * be sent already */
1404 if (rsa == NULL)
1405 {
1406 al=SSL_AD_HANDSHAKE_FAILURE;
1407 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1408 goto f_err;
1409
1410 }
1411 }
1412 else
1413 {
1414 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1415 if ( (pkey == NULL) ||
1416 (pkey->type != EVP_PKEY_RSA) ||
1417 (pkey->pkey.rsa == NULL))
1418 {
1419 al=SSL_AD_HANDSHAKE_FAILURE;
1420 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1421 goto f_err;
1422 }
1423 rsa=pkey->pkey.rsa;
1424 }
1425
1426 /* TLS */
1427 if (s->version > SSL3_VERSION)
1428 {
1429 n2s(p,i);
1430 if (n != i+2)
1431 {
1432 if (!(s->options & SSL_OP_TLS_D5_BUG))
1433 {
1434 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1435 goto err;
1436 }
1437 else
1438 p-=2;
1439 }
1440 else
1441 n=i;
1442 }
1443
1444 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1445
1446 al = -1;
1447
1448 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1449 {
1450 al=SSL_AD_DECODE_ERROR;
1451 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1452 }
1453
1454 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1455 {
1456 /* The premaster secret must contain the same version number as the
1457 * ClientHello to detect version rollback attacks (strangely, the
1458 * protocol does not offer such protection for DH ciphersuites).
1459 * However, buggy clients exist that send the negotiated protocol
1460 * version instead if the server does not support the requested
1461 * protocol version.
1462 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1463 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1464 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1465 {
1466 al=SSL_AD_DECODE_ERROR;
1467 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1468
1469 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1470 * (http://eprint.iacr.org/2003/052/) exploits the version
1471 * number check as a "bad version oracle" -- an alert would
1472 * reveal that the plaintext corresponding to some ciphertext
1473 * made up by the adversary is properly formatted except
1474 * that the version number is wrong. To avoid such attacks,
1475 * we should treat this just like any other decryption error. */
1476 }
1477 }
1478
1479 if (al != -1)
1480 {
1481 /* Some decryption failure -- use random value instead as countermeasure
1482 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1483 * (see RFC 2246, section 7.4.7.1). */
1484 ERR_clear_error();
1485 i = SSL_MAX_MASTER_KEY_LENGTH;
1486 p[0] = s->client_version >> 8;
1487 p[1] = s->client_version & 0xff;
1488 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1489 }
1490
1491 s->session->master_key_length=
1492 s->method->ssl3_enc->generate_master_secret(s,
1493 s->session->master_key,
1494 p,i);
1495 OPENSSL_cleanse(p,i);
1496 }
1497 else
1498#endif
1499#ifndef OPENSSL_NO_DH
1500 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1501 {
1502 n2s(p,i);
1503 if (n != i+2)
1504 {
1505 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1506 {
1507 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1508 goto err;
1509 }
1510 else
1511 {
1512 p-=2;
1513 i=(int)n;
1514 }
1515 }
1516
1517 if (n == 0L) /* the parameters are in the cert */
1518 {
1519 al=SSL_AD_HANDSHAKE_FAILURE;
1520 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1521 goto f_err;
1522 }
1523 else
1524 {
1525 if (s->s3->tmp.dh == NULL)
1526 {
1527 al=SSL_AD_HANDSHAKE_FAILURE;
1528 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1529 goto f_err;
1530 }
1531 else
1532 dh_srvr=s->s3->tmp.dh;
1533 }
1534
1535 pub=BN_bin2bn(p,i,NULL);
1536 if (pub == NULL)
1537 {
1538 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1539 goto err;
1540 }
1541
1542 i=DH_compute_key(p,pub,dh_srvr);
1543
1544 if (i <= 0)
1545 {
1546 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1547 goto err;
1548 }
1549
1550 DH_free(s->s3->tmp.dh);
1551 s->s3->tmp.dh=NULL;
1552
1553 BN_clear_free(pub);
1554 pub=NULL;
1555 s->session->master_key_length=
1556 s->method->ssl3_enc->generate_master_secret(s,
1557 s->session->master_key,p,i);
1558 OPENSSL_cleanse(p,i);
1559 }
1560 else
1561#endif
1562#ifndef OPENSSL_NO_KRB5
1563 if (l & SSL_kKRB5)
1564 {
1565 krb5_error_code krb5rc;
1566 krb5_data enc_ticket;
1567 krb5_data authenticator;
1568 krb5_data enc_pms;
1569 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1570 EVP_CIPHER_CTX ciph_ctx;
1571 EVP_CIPHER *enc = NULL;
1572 unsigned char iv[EVP_MAX_IV_LENGTH];
1573 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1574 + EVP_MAX_BLOCK_LENGTH];
1575 int padl, outl;
1576 krb5_timestamp authtime = 0;
1577 krb5_ticket_times ttimes;
1578
1579 EVP_CIPHER_CTX_init(&ciph_ctx);
1580
1581 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1582
1583 n2s(p,i);
1584 enc_ticket.length = i;
1585 enc_ticket.data = (char *)p;
1586 p+=enc_ticket.length;
1587
1588 n2s(p,i);
1589 authenticator.length = i;
1590 authenticator.data = (char *)p;
1591 p+=authenticator.length;
1592
1593 n2s(p,i);
1594 enc_pms.length = i;
1595 enc_pms.data = (char *)p;
1596 p+=enc_pms.length;
1597
1598 /* Note that the length is checked again below,
1599 ** after decryption
1600 */
1601 if(enc_pms.length > sizeof pms)
1602 {
1603 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1604 SSL_R_DATA_LENGTH_TOO_LONG);
1605 goto err;
1606 }
1607
1608 if (n != enc_ticket.length + authenticator.length +
1609 enc_pms.length + 6)
1610 {
1611 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1612 SSL_R_DATA_LENGTH_TOO_LONG);
1613 goto err;
1614 }
1615
1616 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1617 &kssl_err)) != 0)
1618 {
1619#ifdef KSSL_DEBUG
1620 printf("kssl_sget_tkt rtn %d [%d]\n",
1621 krb5rc, kssl_err.reason);
1622 if (kssl_err.text)
1623 printf("kssl_err text= %s\n", kssl_err.text);
1624#endif /* KSSL_DEBUG */
1625 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1626 kssl_err.reason);
1627 goto err;
1628 }
1629
1630 /* Note: no authenticator is not considered an error,
1631 ** but will return authtime == 0.
1632 */
1633 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
1634 &authtime, &kssl_err)) != 0)
1635 {
1636#ifdef KSSL_DEBUG
1637 printf("kssl_check_authent rtn %d [%d]\n",
1638 krb5rc, kssl_err.reason);
1639 if (kssl_err.text)
1640 printf("kssl_err text= %s\n", kssl_err.text);
1641#endif /* KSSL_DEBUG */
1642 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1643 kssl_err.reason);
1644 goto err;
1645 }
1646
1647 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
1648 {
1649 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
1650 goto err;
1651 }
1652
1653#ifdef KSSL_DEBUG
1654 kssl_ctx_show(kssl_ctx);
1655#endif /* KSSL_DEBUG */
1656
1657 enc = kssl_map_enc(kssl_ctx->enctype);
1658 if (enc == NULL)
1659 goto err;
1660
1661 memset(iv, 0, sizeof iv); /* per RFC 1510 */
1662
1663 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
1664 {
1665 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1666 SSL_R_DECRYPTION_FAILED);
1667 goto err;
1668 }
1669 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
1670 (unsigned char *)enc_pms.data, enc_pms.length))
1671 {
1672 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1673 SSL_R_DECRYPTION_FAILED);
1674 goto err;
1675 }
1676 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1677 {
1678 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1679 SSL_R_DATA_LENGTH_TOO_LONG);
1680 goto err;
1681 }
1682 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
1683 {
1684 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1685 SSL_R_DECRYPTION_FAILED);
1686 goto err;
1687 }
1688 outl += padl;
1689 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1690 {
1691 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1692 SSL_R_DATA_LENGTH_TOO_LONG);
1693 goto err;
1694 }
1695 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1696
1697 s->session->master_key_length=
1698 s->method->ssl3_enc->generate_master_secret(s,
1699 s->session->master_key, pms, outl);
1700
1701 if (kssl_ctx->client_princ)
1702 {
1703 int len = strlen(kssl_ctx->client_princ);
1704 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
1705 {
1706 s->session->krb5_client_princ_len = len;
1707 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
1708 }
1709 }
1710
1711
1712 /* Was doing kssl_ctx_free() here,
1713 ** but it caused problems for apache.
1714 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1715 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1716 */
1717 }
1718 else
1719#endif /* OPENSSL_NO_KRB5 */
1720 {
1721 al=SSL_AD_HANDSHAKE_FAILURE;
1722 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1723 SSL_R_UNKNOWN_CIPHER_TYPE);
1724 goto f_err;
1725 }
1726
1727 return(1);
1728f_err:
1729 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1730#if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1731err:
1732#endif
1733 return(-1);
1734 }
1735
1736static int ssl3_get_cert_verify(SSL *s)
1737 {
1738 EVP_PKEY *pkey=NULL;
1739 unsigned char *p;
1740 int al,ok,ret=0;
1741 long n;
1742 int type=0,i,j;
1743 X509 *peer;
1744
1745 n=ssl3_get_message(s,
1746 SSL3_ST_SR_CERT_VRFY_A,
1747 SSL3_ST_SR_CERT_VRFY_B,
1748 -1,
1749 514, /* 514? */
1750 &ok);
1751
1752 if (!ok) return((int)n);
1753
1754 if (s->session->peer != NULL)
1755 {
1756 peer=s->session->peer;
1757 pkey=X509_get_pubkey(peer);
1758 type=X509_certificate_type(peer,pkey);
1759 }
1760 else
1761 {
1762 peer=NULL;
1763 pkey=NULL;
1764 }
1765
1766 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1767 {
1768 s->s3->tmp.reuse_message=1;
1769 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1770 {
1771 al=SSL_AD_UNEXPECTED_MESSAGE;
1772 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1773 goto f_err;
1774 }
1775 ret=1;
1776 goto end;
1777 }
1778
1779 if (peer == NULL)
1780 {
1781 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1782 al=SSL_AD_UNEXPECTED_MESSAGE;
1783 goto f_err;
1784 }
1785
1786 if (!(type & EVP_PKT_SIGN))
1787 {
1788 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1789 al=SSL_AD_ILLEGAL_PARAMETER;
1790 goto f_err;
1791 }
1792
1793 if (s->s3->change_cipher_spec)
1794 {
1795 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1796 al=SSL_AD_UNEXPECTED_MESSAGE;
1797 goto f_err;
1798 }
1799
1800 /* we now have a signature that we need to verify */
1801 p=(unsigned char *)s->init_msg;
1802 n2s(p,i);
1803 n-=2;
1804 if (i > n)
1805 {
1806 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1807 al=SSL_AD_DECODE_ERROR;
1808 goto f_err;
1809 }
1810
1811 j=EVP_PKEY_size(pkey);
1812 if ((i > j) || (n > j) || (n <= 0))
1813 {
1814 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1815 al=SSL_AD_DECODE_ERROR;
1816 goto f_err;
1817 }
1818
1819#ifndef OPENSSL_NO_RSA
1820 if (pkey->type == EVP_PKEY_RSA)
1821 {
1822 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1823 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1824 pkey->pkey.rsa);
1825 if (i < 0)
1826 {
1827 al=SSL_AD_DECRYPT_ERROR;
1828 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1829 goto f_err;
1830 }
1831 if (i == 0)
1832 {
1833 al=SSL_AD_DECRYPT_ERROR;
1834 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1835 goto f_err;
1836 }
1837 }
1838 else
1839#endif
1840#ifndef OPENSSL_NO_DSA
1841 if (pkey->type == EVP_PKEY_DSA)
1842 {
1843 j=DSA_verify(pkey->save_type,
1844 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1845 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1846 if (j <= 0)
1847 {
1848 /* bad signature */
1849 al=SSL_AD_DECRYPT_ERROR;
1850 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1851 goto f_err;
1852 }
1853 }
1854 else
1855#endif
1856 {
1857 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
1858 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1859 goto f_err;
1860 }
1861
1862
1863 ret=1;
1864 if (0)
1865 {
1866f_err:
1867 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1868 }
1869end:
1870 EVP_PKEY_free(pkey);
1871 return(ret);
1872 }
1873
1874static int ssl3_get_client_certificate(SSL *s)
1875 {
1876 int i,ok,al,ret= -1;
1877 X509 *x=NULL;
1878 unsigned long l,nc,llen,n;
1879 unsigned char *p,*d,*q;
1880 STACK_OF(X509) *sk=NULL;
1881
1882 n=ssl3_get_message(s,
1883 SSL3_ST_SR_CERT_A,
1884 SSL3_ST_SR_CERT_B,
1885 -1,
1886 s->max_cert_list,
1887 &ok);
1888
1889 if (!ok) return((int)n);
1890
1891 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1892 {
1893 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1894 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1895 {
1896 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1897 al=SSL_AD_HANDSHAKE_FAILURE;
1898 goto f_err;
1899 }
1900 /* If tls asked for a client cert, the client must return a 0 list */
1901 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1902 {
1903 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1904 al=SSL_AD_UNEXPECTED_MESSAGE;
1905 goto f_err;
1906 }
1907 s->s3->tmp.reuse_message=1;
1908 return(1);
1909 }
1910
1911 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1912 {
1913 al=SSL_AD_UNEXPECTED_MESSAGE;
1914 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1915 goto f_err;
1916 }
1917 d=p=(unsigned char *)s->init_msg;
1918
1919 if ((sk=sk_X509_new_null()) == NULL)
1920 {
1921 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1922 goto err;
1923 }
1924
1925 n2l3(p,llen);
1926 if (llen+3 != n)
1927 {
1928 al=SSL_AD_DECODE_ERROR;
1929 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1930 goto f_err;
1931 }
1932 for (nc=0; nc<llen; )
1933 {
1934 n2l3(p,l);
1935 if ((l+nc+3) > llen)
1936 {
1937 al=SSL_AD_DECODE_ERROR;
1938 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1939 goto f_err;
1940 }
1941
1942 q=p;
1943 x=d2i_X509(NULL,&p,l);
1944 if (x == NULL)
1945 {
1946 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1947 goto err;
1948 }
1949 if (p != (q+l))
1950 {
1951 al=SSL_AD_DECODE_ERROR;
1952 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1953 goto f_err;
1954 }
1955 if (!sk_X509_push(sk,x))
1956 {
1957 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1958 goto err;
1959 }
1960 x=NULL;
1961 nc+=l+3;
1962 }
1963
1964 if (sk_X509_num(sk) <= 0)
1965 {
1966 /* TLS does not mind 0 certs returned */
1967 if (s->version == SSL3_VERSION)
1968 {
1969 al=SSL_AD_HANDSHAKE_FAILURE;
1970 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1971 goto f_err;
1972 }
1973 /* Fail for TLS only if we required a certificate */
1974 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1975 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1976 {
1977 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1978 al=SSL_AD_HANDSHAKE_FAILURE;
1979 goto f_err;
1980 }
1981 }
1982 else
1983 {
1984 i=ssl_verify_cert_chain(s,sk);
1985 if (!i)
1986 {
1987 al=ssl_verify_alarm_type(s->verify_result);
1988 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1989 goto f_err;
1990 }
1991 }
1992
1993 if (s->session->peer != NULL) /* This should not be needed */
1994 X509_free(s->session->peer);
1995 s->session->peer=sk_X509_shift(sk);
1996 s->session->verify_result = s->verify_result;
1997
1998 /* With the current implementation, sess_cert will always be NULL
1999 * when we arrive here. */
2000 if (s->session->sess_cert == NULL)
2001 {
2002 s->session->sess_cert = ssl_sess_cert_new();
2003 if (s->session->sess_cert == NULL)
2004 {
2005 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2006 goto err;
2007 }
2008 }
2009 if (s->session->sess_cert->cert_chain != NULL)
2010 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2011 s->session->sess_cert->cert_chain=sk;
2012 /* Inconsistency alert: cert_chain does *not* include the
2013 * peer's own certificate, while we do include it in s3_clnt.c */
2014
2015 sk=NULL;
2016
2017 ret=1;
2018 if (0)
2019 {
2020f_err:
2021 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2022 }
2023err:
2024 if (x != NULL) X509_free(x);
2025 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2026 return(ret);
2027 }
2028
2029int ssl3_send_server_certificate(SSL *s)
2030 {
2031 unsigned long l;
2032 X509 *x;
2033
2034 if (s->state == SSL3_ST_SW_CERT_A)
2035 {
2036 x=ssl_get_server_send_cert(s);
2037 if (x == NULL &&
2038 /* VRS: allow null cert if auth == KRB5 */
2039 (s->s3->tmp.new_cipher->algorithms
2040 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2041 != (SSL_aKRB5|SSL_kKRB5))
2042 {
2043 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2044 return(0);
2045 }
2046
2047 l=ssl3_output_cert_chain(s,x);
2048 s->state=SSL3_ST_SW_CERT_B;
2049 s->init_num=(int)l;
2050 s->init_off=0;
2051 }
2052
2053 /* SSL3_ST_SW_CERT_B */
2054 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
2055 }