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