Import OpenSSL-0.9.8i.
[dragonfly.git] / crypto / openssl-0.9.7d / ssl / s3_srvr.c
blobdeb3cffabeb5ff8d6062133fbdecc872edc64e45
1 /* ssl/s3_srvr.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
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).
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.
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)"
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.
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.]
58 /* ====================================================================
59 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
61 * Redistribution and use in source and binary forms, with or without
62 * modification, are permitted provided that the following conditions
63 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
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.
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/)"
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.
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.
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/)"
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 * ====================================================================
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).
112 #define REUSE_CIPHER_BUG
113 #define NETSCAPE_HANG_BUG
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>
129 static SSL_METHOD *ssl3_get_server_method(int ver);
130 static int ssl3_get_client_hello(SSL *s);
131 static int ssl3_check_client_hello(SSL *s);
132 static int ssl3_send_server_hello(SSL *s);
133 static int ssl3_send_server_key_exchange(SSL *s);
134 static int ssl3_send_certificate_request(SSL *s);
135 static int ssl3_send_server_done(SSL *s);
136 static int ssl3_get_client_key_exchange(SSL *s);
137 static int ssl3_get_client_certificate(SSL *s);
138 static int ssl3_get_cert_verify(SSL *s);
139 static int ssl3_send_hello_request(SSL *s);
141 static SSL_METHOD *ssl3_get_server_method(int ver)
143 if (ver == SSL3_VERSION)
144 return(SSLv3_server_method());
145 else
146 return(NULL);
149 SSL_METHOD *SSLv3_server_method(void)
151 static int init=1;
152 static SSL_METHOD SSLv3_server_data;
154 if (init)
156 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
158 if (init)
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;
167 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
169 return(&SSLv3_server_data);
172 int ssl3_accept(SSL *s)
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;
181 RAND_add(&Time,sizeof(Time),0);
182 ERR_clear_error();
183 clear_sys_error();
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;
190 /* init things to blank */
191 s->in_handshake++;
192 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
194 if (s->cert == NULL)
196 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
197 return(-1);
200 for (;;)
202 state=s->state;
204 switch (s->state)
206 case SSL_ST_RENEGOTIATE:
207 s->new_session=1;
208 /* s->state=SSL_ST_ACCEPT; */
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:
215 s->server=1;
216 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
218 if ((s->version>>8) != 3)
220 SSLerr(SSL_F_SSL3_ACCEPT, ERR_R_INTERNAL_ERROR);
221 return -1;
223 s->type=SSL_ST_ACCEPT;
225 if (s->init_buf == NULL)
227 if ((buf=BUF_MEM_new()) == NULL)
229 ret= -1;
230 goto end;
232 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
234 ret= -1;
235 goto end;
237 s->init_buf=buf;
240 if (!ssl3_setup_buffers(s))
242 ret= -1;
243 goto end;
246 s->init_num=0;
248 if (s->state != SSL_ST_RENEGOTIATE)
250 /* Ok, we now need to push on a buffering BIO so that
251 * the output is sent in a way that TCP likes :-)
253 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
255 ssl3_init_finished_mac(s);
256 s->state=SSL3_ST_SR_CLNT_HELLO_A;
257 s->ctx->stats.sess_accept++;
259 else
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;
266 break;
268 case SSL3_ST_SW_HELLO_REQ_A:
269 case SSL3_ST_SW_HELLO_REQ_B:
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;
278 ssl3_init_finished_mac(s);
279 break;
281 case SSL3_ST_SW_HELLO_REQ_C:
282 s->state=SSL_ST_OK;
283 break;
285 case SSL3_ST_SR_CLNT_HELLO_A:
286 case SSL3_ST_SR_CLNT_HELLO_B:
287 case SSL3_ST_SR_CLNT_HELLO_C:
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;
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;
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;
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))
314 ret=ssl3_send_server_certificate(s);
315 if (ret <= 0) goto end;
317 else
318 skip=1;
319 s->state=SSL3_ST_SW_KEY_EXCH_A;
320 s->init_num=0;
321 break;
323 case SSL3_ST_SW_KEY_EXCH_A:
324 case SSL3_ST_SW_KEY_EXCH_B:
325 l=s->s3->tmp.new_cipher->algorithms;
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 */
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;
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)
355 ret=ssl3_send_server_key_exchange(s);
356 if (ret <= 0) goto end;
358 else
359 skip=1;
361 s->state=SSL3_ST_SW_CERT_REQ_A;
362 s->init_num=0;
363 break;
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))
383 /* no cert request */
384 skip=1;
385 s->s3->tmp.cert_request=0;
386 s->state=SSL3_ST_SW_SRVR_DONE_A;
388 else
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;
401 break;
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;
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)
417 s->rwstate=SSL_WRITING;
418 num1=BIO_flush(s->wbio);
419 if (num1 <= 0) { ret= -1; goto end; }
420 s->rwstate=SSL_NOTHING;
423 s->state=s->s3->tmp.next_state;
424 break;
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 if (s->s3->tmp.cert_request)
437 ret=ssl3_get_client_certificate(s);
438 if (ret <= 0) goto end;
440 s->init_num=0;
441 s->state=SSL3_ST_SR_KEY_EXCH_A;
443 break;
445 case SSL3_ST_SR_KEY_EXCH_A:
446 case SSL3_ST_SR_KEY_EXCH_B:
447 ret=ssl3_get_client_key_exchange(s);
448 if (ret <= 0) goto end;
449 s->state=SSL3_ST_SR_CERT_VRFY_A;
450 s->init_num=0;
452 /* We need to get hashes here so if there is
453 * a client cert, it can be verified */
454 s->method->ssl3_enc->cert_verify_mac(s,
455 &(s->s3->finish_dgst1),
456 &(s->s3->tmp.cert_verify_md[0]));
457 s->method->ssl3_enc->cert_verify_mac(s,
458 &(s->s3->finish_dgst2),
459 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
461 break;
463 case SSL3_ST_SR_CERT_VRFY_A:
464 case SSL3_ST_SR_CERT_VRFY_B:
466 /* we should decide if we expected this one */
467 ret=ssl3_get_cert_verify(s);
468 if (ret <= 0) goto end;
470 s->state=SSL3_ST_SR_FINISHED_A;
471 s->init_num=0;
472 break;
474 case SSL3_ST_SR_FINISHED_A:
475 case SSL3_ST_SR_FINISHED_B:
476 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
477 SSL3_ST_SR_FINISHED_B);
478 if (ret <= 0) goto end;
479 if (s->hit)
480 s->state=SSL_ST_OK;
481 else
482 s->state=SSL3_ST_SW_CHANGE_A;
483 s->init_num=0;
484 break;
486 case SSL3_ST_SW_CHANGE_A:
487 case SSL3_ST_SW_CHANGE_B:
489 s->session->cipher=s->s3->tmp.new_cipher;
490 if (!s->method->ssl3_enc->setup_key_block(s))
491 { ret= -1; goto end; }
493 ret=ssl3_send_change_cipher_spec(s,
494 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
496 if (ret <= 0) goto end;
497 s->state=SSL3_ST_SW_FINISHED_A;
498 s->init_num=0;
500 if (!s->method->ssl3_enc->change_cipher_state(s,
501 SSL3_CHANGE_CIPHER_SERVER_WRITE))
503 ret= -1;
504 goto end;
507 break;
509 case SSL3_ST_SW_FINISHED_A:
510 case SSL3_ST_SW_FINISHED_B:
511 ret=ssl3_send_finished(s,
512 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
513 s->method->ssl3_enc->server_finished_label,
514 s->method->ssl3_enc->server_finished_label_len);
515 if (ret <= 0) goto end;
516 s->state=SSL3_ST_SW_FLUSH;
517 if (s->hit)
518 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
519 else
520 s->s3->tmp.next_state=SSL_ST_OK;
521 s->init_num=0;
522 break;
524 case SSL_ST_OK:
525 /* clean a few things up */
526 ssl3_cleanup_key_block(s);
528 BUF_MEM_free(s->init_buf);
529 s->init_buf=NULL;
531 /* remove buffering on output */
532 ssl_free_wbio_buffer(s);
534 s->init_num=0;
536 if (s->new_session == 2) /* skipped if we just sent a HelloRequest */
538 /* actually not necessarily a 'new' session unless
539 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is set */
541 s->new_session=0;
543 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
545 s->ctx->stats.sess_accept_good++;
546 /* s->server=1; */
547 s->handshake_func=ssl3_accept;
549 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
552 ret = 1;
553 goto end;
554 /* break; */
556 default:
557 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
558 ret= -1;
559 goto end;
560 /* break; */
563 if (!s->s3->tmp.reuse_message && !skip)
565 if (s->debug)
567 if ((ret=BIO_flush(s->wbio)) <= 0)
568 goto end;
572 if ((cb != NULL) && (s->state != state))
574 new_state=s->state;
575 s->state=state;
576 cb(s,SSL_CB_ACCEPT_LOOP,1);
577 s->state=new_state;
580 skip=0;
582 end:
583 /* BIO_flush(s->wbio); */
585 s->in_handshake--;
586 if (cb != NULL)
587 cb(s,SSL_CB_ACCEPT_EXIT,ret);
588 return(ret);
591 static int ssl3_send_hello_request(SSL *s)
593 unsigned char *p;
595 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
597 p=(unsigned char *)s->init_buf->data;
598 *(p++)=SSL3_MT_HELLO_REQUEST;
599 *(p++)=0;
600 *(p++)=0;
601 *(p++)=0;
603 s->state=SSL3_ST_SW_HELLO_REQ_B;
604 /* number of bytes to write */
605 s->init_num=4;
606 s->init_off=0;
609 /* SSL3_ST_SW_HELLO_REQ_B */
610 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
613 static int ssl3_check_client_hello(SSL *s)
615 int ok;
616 long n;
618 /* this function is called when we really expect a Certificate message,
619 * so permit appropriate message length */
620 n=ssl3_get_message(s,
621 SSL3_ST_SR_CERT_A,
622 SSL3_ST_SR_CERT_B,
624 s->max_cert_list,
625 &ok);
626 if (!ok) return((int)n);
627 s->s3->tmp.reuse_message = 1;
628 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
630 /* Throw away what we have done so far in the current handshake,
631 * which will now be aborted. (A full SSL_clear would be too much.)
632 * I hope that tmp.dh is the only thing that may need to be cleared
633 * when a handshake is not completed ... */
634 #ifndef OPENSSL_NO_DH
635 if (s->s3->tmp.dh != NULL)
637 DH_free(s->s3->tmp.dh);
638 s->s3->tmp.dh = NULL;
640 #endif
641 return 2;
643 return 1;
646 static int ssl3_get_client_hello(SSL *s)
648 int i,j,ok,al,ret= -1;
649 long n;
650 unsigned long id;
651 unsigned char *p,*d,*q;
652 SSL_CIPHER *c;
653 SSL_COMP *comp=NULL;
654 STACK_OF(SSL_CIPHER) *ciphers=NULL;
656 /* We do this so that we will respond with our native type.
657 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
658 * This down switching should be handled by a different method.
659 * If we are SSLv3, we will respond with SSLv3, even if prompted with
660 * TLSv1.
662 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
664 s->first_packet=1;
665 s->state=SSL3_ST_SR_CLNT_HELLO_B;
667 n=ssl3_get_message(s,
668 SSL3_ST_SR_CLNT_HELLO_B,
669 SSL3_ST_SR_CLNT_HELLO_C,
670 SSL3_MT_CLIENT_HELLO,
671 SSL3_RT_MAX_PLAIN_LENGTH,
672 &ok);
674 if (!ok) return((int)n);
675 d=p=(unsigned char *)s->init_msg;
677 /* use version from inside client hello, not from record header
678 * (may differ: see RFC 2246, Appendix E, second paragraph) */
679 s->client_version=(((int)p[0])<<8)|(int)p[1];
680 p+=2;
682 if (s->client_version < s->version)
684 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO, SSL_R_WRONG_VERSION_NUMBER);
685 if ((s->client_version>>8) == SSL3_VERSION_MAJOR)
687 /* similar to ssl3_get_record, send alert using remote version number */
688 s->version = s->client_version;
690 al = SSL_AD_PROTOCOL_VERSION;
691 goto f_err;
694 /* load the client random */
695 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
696 p+=SSL3_RANDOM_SIZE;
698 /* get the session-id */
699 j= *(p++);
701 s->hit=0;
702 /* Versions before 0.9.7 always allow session reuse during renegotiation
703 * (i.e. when s->new_session is true), option
704 * SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION is new with 0.9.7.
705 * Maybe this optional behaviour should always have been the default,
706 * but we cannot safely change the default behaviour (or new applications
707 * might be written that become totally unsecure when compiled with
708 * an earlier library version)
710 if (j == 0 || (s->new_session && (s->options & SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION)))
712 if (!ssl_get_new_session(s,1))
713 goto err;
715 else
717 i=ssl_get_prev_session(s,p,j);
718 if (i == 1)
719 { /* previous session */
720 s->hit=1;
722 else if (i == -1)
723 goto err;
724 else /* i == 0 */
726 if (!ssl_get_new_session(s,1))
727 goto err;
731 p+=j;
732 n2s(p,i);
733 if ((i == 0) && (j != 0))
735 /* we need a cipher if we are not resuming a session */
736 al=SSL_AD_ILLEGAL_PARAMETER;
737 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
738 goto f_err;
740 if ((p+i) >= (d+n))
742 /* not enough data */
743 al=SSL_AD_DECODE_ERROR;
744 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
745 goto f_err;
747 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
748 == NULL))
750 goto err;
752 p+=i;
754 /* If it is a hit, check that the cipher is in the list */
755 if ((s->hit) && (i > 0))
757 j=0;
758 id=s->session->cipher->id;
760 #ifdef CIPHER_DEBUG
761 printf("client sent %d ciphers\n",sk_num(ciphers));
762 #endif
763 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
765 c=sk_SSL_CIPHER_value(ciphers,i);
766 #ifdef CIPHER_DEBUG
767 printf("client [%2d of %2d]:%s\n",
768 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
769 #endif
770 if (c->id == id)
772 j=1;
773 break;
776 if (j == 0)
778 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
780 /* Very bad for multi-threading.... */
781 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
784 else
786 /* we need to have the cipher in the cipher
787 * list if we are asked to reuse it */
788 al=SSL_AD_ILLEGAL_PARAMETER;
789 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
790 goto f_err;
795 /* compression */
796 i= *(p++);
797 if ((p+i) > (d+n))
799 /* not enough data */
800 al=SSL_AD_DECODE_ERROR;
801 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
802 goto f_err;
804 q=p;
805 for (j=0; j<i; j++)
807 if (p[j] == 0) break;
810 p+=i;
811 if (j >= i)
813 /* no compress */
814 al=SSL_AD_DECODE_ERROR;
815 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
816 goto f_err;
819 /* Worst case, we will use the NULL compression, but if we have other
820 * options, we will now look for them. We have i-1 compression
821 * algorithms from the client, starting at q. */
822 s->s3->tmp.new_compression=NULL;
823 if (s->ctx->comp_methods != NULL)
824 { /* See if we have a match */
825 int m,nn,o,v,done=0;
827 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
828 for (m=0; m<nn; m++)
830 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
831 v=comp->id;
832 for (o=0; o<i; o++)
834 if (v == q[o])
836 done=1;
837 break;
840 if (done) break;
842 if (done)
843 s->s3->tmp.new_compression=comp;
844 else
845 comp=NULL;
848 /* TLS does not mind if there is extra stuff */
849 #if 0 /* SSL 3.0 does not mind either, so we should disable this test
850 * (was enabled in 0.9.6d through 0.9.6j and 0.9.7 through 0.9.7b,
851 * in earlier SSLeay/OpenSSL releases this test existed but was buggy) */
852 if (s->version == SSL3_VERSION)
854 if (p < (d+n))
856 /* wrong number of bytes,
857 * there could be more to follow */
858 al=SSL_AD_DECODE_ERROR;
859 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
860 goto f_err;
863 #endif
865 /* Given s->session->ciphers and SSL_get_ciphers, we must
866 * pick a cipher */
868 if (!s->hit)
870 s->session->compress_meth=(comp == NULL)?0:comp->id;
871 if (s->session->ciphers != NULL)
872 sk_SSL_CIPHER_free(s->session->ciphers);
873 s->session->ciphers=ciphers;
874 if (ciphers == NULL)
876 al=SSL_AD_ILLEGAL_PARAMETER;
877 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
878 goto f_err;
880 ciphers=NULL;
881 c=ssl3_choose_cipher(s,s->session->ciphers,
882 SSL_get_ciphers(s));
884 if (c == NULL)
886 al=SSL_AD_HANDSHAKE_FAILURE;
887 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
888 goto f_err;
890 s->s3->tmp.new_cipher=c;
892 else
894 /* Session-id reuse */
895 #ifdef REUSE_CIPHER_BUG
896 STACK_OF(SSL_CIPHER) *sk;
897 SSL_CIPHER *nc=NULL;
898 SSL_CIPHER *ec=NULL;
900 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
902 sk=s->session->ciphers;
903 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
905 c=sk_SSL_CIPHER_value(sk,i);
906 if (c->algorithms & SSL_eNULL)
907 nc=c;
908 if (SSL_C_IS_EXPORT(c))
909 ec=c;
911 if (nc != NULL)
912 s->s3->tmp.new_cipher=nc;
913 else if (ec != NULL)
914 s->s3->tmp.new_cipher=ec;
915 else
916 s->s3->tmp.new_cipher=s->session->cipher;
918 else
919 #endif
920 s->s3->tmp.new_cipher=s->session->cipher;
923 /* we now have the following setup.
924 * client_random
925 * cipher_list - our prefered list of ciphers
926 * ciphers - the clients prefered list of ciphers
927 * compression - basically ignored right now
928 * ssl version is set - sslv3
929 * s->session - The ssl session has been setup.
930 * s->hit - session reuse flag
931 * s->tmp.new_cipher - the new cipher to use.
934 ret=1;
935 if (0)
937 f_err:
938 ssl3_send_alert(s,SSL3_AL_FATAL,al);
940 err:
941 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
942 return(ret);
945 static int ssl3_send_server_hello(SSL *s)
947 unsigned char *buf;
948 unsigned char *p,*d;
949 int i,sl;
950 unsigned long l,Time;
952 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
954 buf=(unsigned char *)s->init_buf->data;
955 p=s->s3->server_random;
956 Time=time(NULL); /* Time */
957 l2n(Time,p);
958 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
959 /* Do the message type and length last */
960 d=p= &(buf[4]);
962 *(p++)=s->version>>8;
963 *(p++)=s->version&0xff;
965 /* Random stuff */
966 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
967 p+=SSL3_RANDOM_SIZE;
969 /* now in theory we have 3 options to sending back the
970 * session id. If it is a re-use, we send back the
971 * old session-id, if it is a new session, we send
972 * back the new session-id or we send back a 0 length
973 * session-id if we want it to be single use.
974 * Currently I will not implement the '0' length session-id
975 * 12-Jan-98 - I'll now support the '0' length stuff.
977 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
978 s->session->session_id_length=0;
980 sl=s->session->session_id_length;
981 if (sl > sizeof s->session->session_id)
983 SSLerr(SSL_F_SSL3_SEND_SERVER_HELLO, ERR_R_INTERNAL_ERROR);
984 return -1;
986 *(p++)=sl;
987 memcpy(p,s->session->session_id,sl);
988 p+=sl;
990 /* put the cipher */
991 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
992 p+=i;
994 /* put the compression method */
995 if (s->s3->tmp.new_compression == NULL)
996 *(p++)=0;
997 else
998 *(p++)=s->s3->tmp.new_compression->id;
1000 /* do the header */
1001 l=(p-d);
1002 d=buf;
1003 *(d++)=SSL3_MT_SERVER_HELLO;
1004 l2n3(l,d);
1006 s->state=SSL3_ST_CW_CLNT_HELLO_B;
1007 /* number of bytes to write */
1008 s->init_num=p-buf;
1009 s->init_off=0;
1012 /* SSL3_ST_CW_CLNT_HELLO_B */
1013 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1016 static int ssl3_send_server_done(SSL *s)
1018 unsigned char *p;
1020 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
1022 p=(unsigned char *)s->init_buf->data;
1024 /* do the header */
1025 *(p++)=SSL3_MT_SERVER_DONE;
1026 *(p++)=0;
1027 *(p++)=0;
1028 *(p++)=0;
1030 s->state=SSL3_ST_SW_SRVR_DONE_B;
1031 /* number of bytes to write */
1032 s->init_num=4;
1033 s->init_off=0;
1036 /* SSL3_ST_CW_CLNT_HELLO_B */
1037 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1040 static int ssl3_send_server_key_exchange(SSL *s)
1042 #ifndef OPENSSL_NO_RSA
1043 unsigned char *q;
1044 int j,num;
1045 RSA *rsa;
1046 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1047 unsigned int u;
1048 #endif
1049 #ifndef OPENSSL_NO_DH
1050 DH *dh=NULL,*dhp;
1051 #endif
1052 EVP_PKEY *pkey;
1053 unsigned char *p,*d;
1054 int al,i;
1055 unsigned long type;
1056 int n;
1057 CERT *cert;
1058 BIGNUM *r[4];
1059 int nr[4],kn;
1060 BUF_MEM *buf;
1061 EVP_MD_CTX md_ctx;
1063 EVP_MD_CTX_init(&md_ctx);
1064 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
1066 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
1067 cert=s->cert;
1069 buf=s->init_buf;
1071 r[0]=r[1]=r[2]=r[3]=NULL;
1072 n=0;
1073 #ifndef OPENSSL_NO_RSA
1074 if (type & SSL_kRSA)
1076 rsa=cert->rsa_tmp;
1077 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
1079 rsa=s->cert->rsa_tmp_cb(s,
1080 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1081 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1082 if(rsa == NULL)
1084 al=SSL_AD_HANDSHAKE_FAILURE;
1085 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
1086 goto f_err;
1088 RSA_up_ref(rsa);
1089 cert->rsa_tmp=rsa;
1091 if (rsa == NULL)
1093 al=SSL_AD_HANDSHAKE_FAILURE;
1094 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
1095 goto f_err;
1097 r[0]=rsa->n;
1098 r[1]=rsa->e;
1099 s->s3->tmp.use_rsa_tmp=1;
1101 else
1102 #endif
1103 #ifndef OPENSSL_NO_DH
1104 if (type & SSL_kEDH)
1106 dhp=cert->dh_tmp;
1107 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
1108 dhp=s->cert->dh_tmp_cb(s,
1109 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
1110 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
1111 if (dhp == NULL)
1113 al=SSL_AD_HANDSHAKE_FAILURE;
1114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1115 goto f_err;
1118 if (s->s3->tmp.dh != NULL)
1120 DH_free(dh);
1121 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
1122 goto err;
1125 if ((dh=DHparams_dup(dhp)) == NULL)
1127 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1128 goto err;
1131 s->s3->tmp.dh=dh;
1132 if ((dhp->pub_key == NULL ||
1133 dhp->priv_key == NULL ||
1134 (s->options & SSL_OP_SINGLE_DH_USE)))
1136 if(!DH_generate_key(dh))
1138 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1139 ERR_R_DH_LIB);
1140 goto err;
1143 else
1145 dh->pub_key=BN_dup(dhp->pub_key);
1146 dh->priv_key=BN_dup(dhp->priv_key);
1147 if ((dh->pub_key == NULL) ||
1148 (dh->priv_key == NULL))
1150 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1151 goto err;
1154 r[0]=dh->p;
1155 r[1]=dh->g;
1156 r[2]=dh->pub_key;
1158 else
1159 #endif
1161 al=SSL_AD_HANDSHAKE_FAILURE;
1162 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1163 goto f_err;
1165 for (i=0; r[i] != NULL; i++)
1167 nr[i]=BN_num_bytes(r[i]);
1168 n+=2+nr[i];
1171 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1173 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1174 == NULL)
1176 al=SSL_AD_DECODE_ERROR;
1177 goto f_err;
1179 kn=EVP_PKEY_size(pkey);
1181 else
1183 pkey=NULL;
1184 kn=0;
1187 if (!BUF_MEM_grow_clean(buf,n+4+kn))
1189 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1190 goto err;
1192 d=(unsigned char *)s->init_buf->data;
1193 p= &(d[4]);
1195 for (i=0; r[i] != NULL; i++)
1197 s2n(nr[i],p);
1198 BN_bn2bin(r[i],p);
1199 p+=nr[i];
1202 /* not anonymous */
1203 if (pkey != NULL)
1205 /* n is the length of the params, they start at &(d[4])
1206 * and p points to the space at the end. */
1207 #ifndef OPENSSL_NO_RSA
1208 if (pkey->type == EVP_PKEY_RSA)
1210 q=md_buf;
1211 j=0;
1212 for (num=2; num > 0; num--)
1214 EVP_DigestInit_ex(&md_ctx,(num == 2)
1215 ?s->ctx->md5:s->ctx->sha1, NULL);
1216 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1217 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1218 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1219 EVP_DigestFinal_ex(&md_ctx,q,
1220 (unsigned int *)&i);
1221 q+=i;
1222 j+=i;
1224 if (RSA_sign(NID_md5_sha1, md_buf, j,
1225 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1227 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1228 goto err;
1230 s2n(u,p);
1231 n+=u+2;
1233 else
1234 #endif
1235 #if !defined(OPENSSL_NO_DSA)
1236 if (pkey->type == EVP_PKEY_DSA)
1238 /* lets do DSS */
1239 EVP_SignInit_ex(&md_ctx,EVP_dss1(), NULL);
1240 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1241 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1242 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1243 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1244 (unsigned int *)&i,pkey))
1246 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1247 goto err;
1249 s2n(i,p);
1250 n+=i+2;
1252 else
1253 #endif
1255 /* Is this error check actually needed? */
1256 al=SSL_AD_HANDSHAKE_FAILURE;
1257 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1258 goto f_err;
1262 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1263 l2n3(n,d);
1265 /* we should now have things packed up, so lets send
1266 * it off */
1267 s->init_num=n+4;
1268 s->init_off=0;
1271 s->state = SSL3_ST_SW_KEY_EXCH_B;
1272 EVP_MD_CTX_cleanup(&md_ctx);
1273 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1274 f_err:
1275 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1276 err:
1277 EVP_MD_CTX_cleanup(&md_ctx);
1278 return(-1);
1281 static int ssl3_send_certificate_request(SSL *s)
1283 unsigned char *p,*d;
1284 int i,j,nl,off,n;
1285 STACK_OF(X509_NAME) *sk=NULL;
1286 X509_NAME *name;
1287 BUF_MEM *buf;
1289 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1291 buf=s->init_buf;
1293 d=p=(unsigned char *)&(buf->data[4]);
1295 /* get the list of acceptable cert types */
1296 p++;
1297 n=ssl3_get_req_cert_type(s,p);
1298 d[0]=n;
1299 p+=n;
1300 n++;
1302 off=n;
1303 p+=2;
1304 n+=2;
1306 sk=SSL_get_client_CA_list(s);
1307 nl=0;
1308 if (sk != NULL)
1310 for (i=0; i<sk_X509_NAME_num(sk); i++)
1312 name=sk_X509_NAME_value(sk,i);
1313 j=i2d_X509_NAME(name,NULL);
1314 if (!BUF_MEM_grow_clean(buf,4+n+j+2))
1316 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1317 goto err;
1319 p=(unsigned char *)&(buf->data[4+n]);
1320 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1322 s2n(j,p);
1323 i2d_X509_NAME(name,&p);
1324 n+=2+j;
1325 nl+=2+j;
1327 else
1329 d=p;
1330 i2d_X509_NAME(name,&p);
1331 j-=2; s2n(j,d); j+=2;
1332 n+=j;
1333 nl+=j;
1337 /* else no CA names */
1338 p=(unsigned char *)&(buf->data[4+off]);
1339 s2n(nl,p);
1341 d=(unsigned char *)buf->data;
1342 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1343 l2n3(n,d);
1345 /* we should now have things packed up, so lets send
1346 * it off */
1348 s->init_num=n+4;
1349 s->init_off=0;
1350 #ifdef NETSCAPE_HANG_BUG
1351 p=(unsigned char *)s->init_buf->data + s->init_num;
1353 /* do the header */
1354 *(p++)=SSL3_MT_SERVER_DONE;
1355 *(p++)=0;
1356 *(p++)=0;
1357 *(p++)=0;
1358 s->init_num += 4;
1359 #endif
1361 s->state = SSL3_ST_SW_CERT_REQ_B;
1364 /* SSL3_ST_SW_CERT_REQ_B */
1365 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1366 err:
1367 return(-1);
1370 static int ssl3_get_client_key_exchange(SSL *s)
1372 int i,al,ok;
1373 long n;
1374 unsigned long l;
1375 unsigned char *p;
1376 #ifndef OPENSSL_NO_RSA
1377 RSA *rsa=NULL;
1378 EVP_PKEY *pkey=NULL;
1379 #endif
1380 #ifndef OPENSSL_NO_DH
1381 BIGNUM *pub=NULL;
1382 DH *dh_srvr;
1383 #endif
1384 #ifndef OPENSSL_NO_KRB5
1385 KSSL_ERR kssl_err;
1386 #endif /* OPENSSL_NO_KRB5 */
1388 n=ssl3_get_message(s,
1389 SSL3_ST_SR_KEY_EXCH_A,
1390 SSL3_ST_SR_KEY_EXCH_B,
1391 SSL3_MT_CLIENT_KEY_EXCHANGE,
1392 2048, /* ??? */
1393 &ok);
1395 if (!ok) return((int)n);
1396 p=(unsigned char *)s->init_msg;
1398 l=s->s3->tmp.new_cipher->algorithms;
1400 #ifndef OPENSSL_NO_RSA
1401 if (l & SSL_kRSA)
1403 /* FIX THIS UP EAY EAY EAY EAY */
1404 if (s->s3->tmp.use_rsa_tmp)
1406 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1407 rsa=s->cert->rsa_tmp;
1408 /* Don't do a callback because rsa_tmp should
1409 * be sent already */
1410 if (rsa == NULL)
1412 al=SSL_AD_HANDSHAKE_FAILURE;
1413 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1414 goto f_err;
1418 else
1420 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1421 if ( (pkey == NULL) ||
1422 (pkey->type != EVP_PKEY_RSA) ||
1423 (pkey->pkey.rsa == NULL))
1425 al=SSL_AD_HANDSHAKE_FAILURE;
1426 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1427 goto f_err;
1429 rsa=pkey->pkey.rsa;
1432 /* TLS */
1433 if (s->version > SSL3_VERSION)
1435 n2s(p,i);
1436 if (n != i+2)
1438 if (!(s->options & SSL_OP_TLS_D5_BUG))
1440 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1441 goto err;
1443 else
1444 p-=2;
1446 else
1447 n=i;
1450 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1452 al = -1;
1454 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1456 al=SSL_AD_DECODE_ERROR;
1457 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT); */
1460 if ((al == -1) && !((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1462 /* The premaster secret must contain the same version number as the
1463 * ClientHello to detect version rollback attacks (strangely, the
1464 * protocol does not offer such protection for DH ciphersuites).
1465 * However, buggy clients exist that send the negotiated protocol
1466 * version instead if the server does not support the requested
1467 * protocol version.
1468 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1469 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1470 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1472 al=SSL_AD_DECODE_ERROR;
1473 /* SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER); */
1475 /* The Klima-Pokorny-Rosa extension of Bleichenbacher's attack
1476 * (http://eprint.iacr.org/2003/052/) exploits the version
1477 * number check as a "bad version oracle" -- an alert would
1478 * reveal that the plaintext corresponding to some ciphertext
1479 * made up by the adversary is properly formatted except
1480 * that the version number is wrong. To avoid such attacks,
1481 * we should treat this just like any other decryption error. */
1485 if (al != -1)
1487 /* Some decryption failure -- use random value instead as countermeasure
1488 * against Bleichenbacher's attack on PKCS #1 v1.5 RSA padding
1489 * (see RFC 2246, section 7.4.7.1). */
1490 ERR_clear_error();
1491 i = SSL_MAX_MASTER_KEY_LENGTH;
1492 p[0] = s->client_version >> 8;
1493 p[1] = s->client_version & 0xff;
1494 RAND_pseudo_bytes(p+2, i-2); /* should be RAND_bytes, but we cannot work around a failure */
1497 s->session->master_key_length=
1498 s->method->ssl3_enc->generate_master_secret(s,
1499 s->session->master_key,
1500 p,i);
1501 OPENSSL_cleanse(p,i);
1503 else
1504 #endif
1505 #ifndef OPENSSL_NO_DH
1506 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1508 n2s(p,i);
1509 if (n != i+2)
1511 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1513 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1514 goto err;
1516 else
1518 p-=2;
1519 i=(int)n;
1523 if (n == 0L) /* the parameters are in the cert */
1525 al=SSL_AD_HANDSHAKE_FAILURE;
1526 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1527 goto f_err;
1529 else
1531 if (s->s3->tmp.dh == NULL)
1533 al=SSL_AD_HANDSHAKE_FAILURE;
1534 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1535 goto f_err;
1537 else
1538 dh_srvr=s->s3->tmp.dh;
1541 pub=BN_bin2bn(p,i,NULL);
1542 if (pub == NULL)
1544 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1545 goto err;
1548 i=DH_compute_key(p,pub,dh_srvr);
1550 if (i <= 0)
1552 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1553 goto err;
1556 DH_free(s->s3->tmp.dh);
1557 s->s3->tmp.dh=NULL;
1559 BN_clear_free(pub);
1560 pub=NULL;
1561 s->session->master_key_length=
1562 s->method->ssl3_enc->generate_master_secret(s,
1563 s->session->master_key,p,i);
1564 OPENSSL_cleanse(p,i);
1566 else
1567 #endif
1568 #ifndef OPENSSL_NO_KRB5
1569 if (l & SSL_kKRB5)
1571 krb5_error_code krb5rc;
1572 krb5_data enc_ticket;
1573 krb5_data authenticator;
1574 krb5_data enc_pms;
1575 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1576 EVP_CIPHER_CTX ciph_ctx;
1577 EVP_CIPHER *enc = NULL;
1578 unsigned char iv[EVP_MAX_IV_LENGTH];
1579 unsigned char pms[SSL_MAX_MASTER_KEY_LENGTH
1580 + EVP_MAX_BLOCK_LENGTH];
1581 int padl, outl;
1582 krb5_timestamp authtime = 0;
1583 krb5_ticket_times ttimes;
1585 EVP_CIPHER_CTX_init(&ciph_ctx);
1587 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1589 n2s(p,i);
1590 enc_ticket.length = i;
1592 if (n < enc_ticket.length + 6)
1594 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1595 SSL_R_DATA_LENGTH_TOO_LONG);
1596 goto err;
1599 enc_ticket.data = (char *)p;
1600 p+=enc_ticket.length;
1602 n2s(p,i);
1603 authenticator.length = i;
1605 if (n < enc_ticket.length + authenticator.length + 6)
1607 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1608 SSL_R_DATA_LENGTH_TOO_LONG);
1609 goto err;
1612 authenticator.data = (char *)p;
1613 p+=authenticator.length;
1615 n2s(p,i);
1616 enc_pms.length = i;
1617 enc_pms.data = (char *)p;
1618 p+=enc_pms.length;
1620 /* Note that the length is checked again below,
1621 ** after decryption
1623 if(enc_pms.length > sizeof pms)
1625 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1626 SSL_R_DATA_LENGTH_TOO_LONG);
1627 goto err;
1630 if (n != enc_ticket.length + authenticator.length +
1631 enc_pms.length + 6)
1633 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1634 SSL_R_DATA_LENGTH_TOO_LONG);
1635 goto err;
1638 if ((krb5rc = kssl_sget_tkt(kssl_ctx, &enc_ticket, &ttimes,
1639 &kssl_err)) != 0)
1641 #ifdef KSSL_DEBUG
1642 printf("kssl_sget_tkt rtn %d [%d]\n",
1643 krb5rc, kssl_err.reason);
1644 if (kssl_err.text)
1645 printf("kssl_err text= %s\n", kssl_err.text);
1646 #endif /* KSSL_DEBUG */
1647 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1648 kssl_err.reason);
1649 goto err;
1652 /* Note: no authenticator is not considered an error,
1653 ** but will return authtime == 0.
1655 if ((krb5rc = kssl_check_authent(kssl_ctx, &authenticator,
1656 &authtime, &kssl_err)) != 0)
1658 #ifdef KSSL_DEBUG
1659 printf("kssl_check_authent rtn %d [%d]\n",
1660 krb5rc, kssl_err.reason);
1661 if (kssl_err.text)
1662 printf("kssl_err text= %s\n", kssl_err.text);
1663 #endif /* KSSL_DEBUG */
1664 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1665 kssl_err.reason);
1666 goto err;
1669 if ((krb5rc = kssl_validate_times(authtime, &ttimes)) != 0)
1671 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE, krb5rc);
1672 goto err;
1675 #ifdef KSSL_DEBUG
1676 kssl_ctx_show(kssl_ctx);
1677 #endif /* KSSL_DEBUG */
1679 enc = kssl_map_enc(kssl_ctx->enctype);
1680 if (enc == NULL)
1681 goto err;
1683 memset(iv, 0, sizeof iv); /* per RFC 1510 */
1685 if (!EVP_DecryptInit_ex(&ciph_ctx,enc,NULL,kssl_ctx->key,iv))
1687 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1688 SSL_R_DECRYPTION_FAILED);
1689 goto err;
1691 if (!EVP_DecryptUpdate(&ciph_ctx, pms,&outl,
1692 (unsigned char *)enc_pms.data, enc_pms.length))
1694 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1695 SSL_R_DECRYPTION_FAILED);
1696 goto err;
1698 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1700 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1701 SSL_R_DATA_LENGTH_TOO_LONG);
1702 goto err;
1704 if (!EVP_DecryptFinal_ex(&ciph_ctx,&(pms[outl]),&padl))
1706 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1707 SSL_R_DECRYPTION_FAILED);
1708 goto err;
1710 outl += padl;
1711 if (outl > SSL_MAX_MASTER_KEY_LENGTH)
1713 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1714 SSL_R_DATA_LENGTH_TOO_LONG);
1715 goto err;
1717 EVP_CIPHER_CTX_cleanup(&ciph_ctx);
1719 s->session->master_key_length=
1720 s->method->ssl3_enc->generate_master_secret(s,
1721 s->session->master_key, pms, outl);
1723 if (kssl_ctx->client_princ)
1725 int len = strlen(kssl_ctx->client_princ);
1726 if ( len < SSL_MAX_KRB5_PRINCIPAL_LENGTH )
1728 s->session->krb5_client_princ_len = len;
1729 memcpy(s->session->krb5_client_princ,kssl_ctx->client_princ,len);
1734 /* Was doing kssl_ctx_free() here,
1735 ** but it caused problems for apache.
1736 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1737 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1740 else
1741 #endif /* OPENSSL_NO_KRB5 */
1743 al=SSL_AD_HANDSHAKE_FAILURE;
1744 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,
1745 SSL_R_UNKNOWN_CIPHER_TYPE);
1746 goto f_err;
1749 return(1);
1750 f_err:
1751 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1752 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_RSA)
1753 err:
1754 #endif
1755 return(-1);
1758 static int ssl3_get_cert_verify(SSL *s)
1760 EVP_PKEY *pkey=NULL;
1761 unsigned char *p;
1762 int al,ok,ret=0;
1763 long n;
1764 int type=0,i,j;
1765 X509 *peer;
1767 n=ssl3_get_message(s,
1768 SSL3_ST_SR_CERT_VRFY_A,
1769 SSL3_ST_SR_CERT_VRFY_B,
1771 514, /* 514? */
1772 &ok);
1774 if (!ok) return((int)n);
1776 if (s->session->peer != NULL)
1778 peer=s->session->peer;
1779 pkey=X509_get_pubkey(peer);
1780 type=X509_certificate_type(peer,pkey);
1782 else
1784 peer=NULL;
1785 pkey=NULL;
1788 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1790 s->s3->tmp.reuse_message=1;
1791 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1793 al=SSL_AD_UNEXPECTED_MESSAGE;
1794 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1795 goto f_err;
1797 ret=1;
1798 goto end;
1801 if (peer == NULL)
1803 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1804 al=SSL_AD_UNEXPECTED_MESSAGE;
1805 goto f_err;
1808 if (!(type & EVP_PKT_SIGN))
1810 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1811 al=SSL_AD_ILLEGAL_PARAMETER;
1812 goto f_err;
1815 if (s->s3->change_cipher_spec)
1817 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1818 al=SSL_AD_UNEXPECTED_MESSAGE;
1819 goto f_err;
1822 /* we now have a signature that we need to verify */
1823 p=(unsigned char *)s->init_msg;
1824 n2s(p,i);
1825 n-=2;
1826 if (i > n)
1828 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1829 al=SSL_AD_DECODE_ERROR;
1830 goto f_err;
1833 j=EVP_PKEY_size(pkey);
1834 if ((i > j) || (n > j) || (n <= 0))
1836 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1837 al=SSL_AD_DECODE_ERROR;
1838 goto f_err;
1841 #ifndef OPENSSL_NO_RSA
1842 if (pkey->type == EVP_PKEY_RSA)
1844 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1845 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1846 pkey->pkey.rsa);
1847 if (i < 0)
1849 al=SSL_AD_DECRYPT_ERROR;
1850 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1851 goto f_err;
1853 if (i == 0)
1855 al=SSL_AD_DECRYPT_ERROR;
1856 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1857 goto f_err;
1860 else
1861 #endif
1862 #ifndef OPENSSL_NO_DSA
1863 if (pkey->type == EVP_PKEY_DSA)
1865 j=DSA_verify(pkey->save_type,
1866 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1867 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1868 if (j <= 0)
1870 /* bad signature */
1871 al=SSL_AD_DECRYPT_ERROR;
1872 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1873 goto f_err;
1876 else
1877 #endif
1879 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,ERR_R_INTERNAL_ERROR);
1880 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1881 goto f_err;
1885 ret=1;
1886 if (0)
1888 f_err:
1889 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1891 end:
1892 EVP_PKEY_free(pkey);
1893 return(ret);
1896 static int ssl3_get_client_certificate(SSL *s)
1898 int i,ok,al,ret= -1;
1899 X509 *x=NULL;
1900 unsigned long l,nc,llen,n;
1901 unsigned char *p,*d,*q;
1902 STACK_OF(X509) *sk=NULL;
1904 n=ssl3_get_message(s,
1905 SSL3_ST_SR_CERT_A,
1906 SSL3_ST_SR_CERT_B,
1908 s->max_cert_list,
1909 &ok);
1911 if (!ok) return((int)n);
1913 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1915 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1916 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1918 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1919 al=SSL_AD_HANDSHAKE_FAILURE;
1920 goto f_err;
1922 /* If tls asked for a client cert, the client must return a 0 list */
1923 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1925 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1926 al=SSL_AD_UNEXPECTED_MESSAGE;
1927 goto f_err;
1929 s->s3->tmp.reuse_message=1;
1930 return(1);
1933 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1935 al=SSL_AD_UNEXPECTED_MESSAGE;
1936 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1937 goto f_err;
1939 d=p=(unsigned char *)s->init_msg;
1941 if ((sk=sk_X509_new_null()) == NULL)
1943 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1944 goto err;
1947 n2l3(p,llen);
1948 if (llen+3 != n)
1950 al=SSL_AD_DECODE_ERROR;
1951 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1952 goto f_err;
1954 for (nc=0; nc<llen; )
1956 n2l3(p,l);
1957 if ((l+nc+3) > llen)
1959 al=SSL_AD_DECODE_ERROR;
1960 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1961 goto f_err;
1964 q=p;
1965 x=d2i_X509(NULL,&p,l);
1966 if (x == NULL)
1968 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1969 goto err;
1971 if (p != (q+l))
1973 al=SSL_AD_DECODE_ERROR;
1974 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1975 goto f_err;
1977 if (!sk_X509_push(sk,x))
1979 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1980 goto err;
1982 x=NULL;
1983 nc+=l+3;
1986 if (sk_X509_num(sk) <= 0)
1988 /* TLS does not mind 0 certs returned */
1989 if (s->version == SSL3_VERSION)
1991 al=SSL_AD_HANDSHAKE_FAILURE;
1992 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1993 goto f_err;
1995 /* Fail for TLS only if we required a certificate */
1996 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1997 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1999 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
2000 al=SSL_AD_HANDSHAKE_FAILURE;
2001 goto f_err;
2004 else
2006 i=ssl_verify_cert_chain(s,sk);
2007 if (!i)
2009 al=ssl_verify_alarm_type(s->verify_result);
2010 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
2011 goto f_err;
2015 if (s->session->peer != NULL) /* This should not be needed */
2016 X509_free(s->session->peer);
2017 s->session->peer=sk_X509_shift(sk);
2018 s->session->verify_result = s->verify_result;
2020 /* With the current implementation, sess_cert will always be NULL
2021 * when we arrive here. */
2022 if (s->session->sess_cert == NULL)
2024 s->session->sess_cert = ssl_sess_cert_new();
2025 if (s->session->sess_cert == NULL)
2027 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
2028 goto err;
2031 if (s->session->sess_cert->cert_chain != NULL)
2032 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
2033 s->session->sess_cert->cert_chain=sk;
2034 /* Inconsistency alert: cert_chain does *not* include the
2035 * peer's own certificate, while we do include it in s3_clnt.c */
2037 sk=NULL;
2039 ret=1;
2040 if (0)
2042 f_err:
2043 ssl3_send_alert(s,SSL3_AL_FATAL,al);
2045 err:
2046 if (x != NULL) X509_free(x);
2047 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
2048 return(ret);
2051 int ssl3_send_server_certificate(SSL *s)
2053 unsigned long l;
2054 X509 *x;
2056 if (s->state == SSL3_ST_SW_CERT_A)
2058 x=ssl_get_server_send_cert(s);
2059 if (x == NULL &&
2060 /* VRS: allow null cert if auth == KRB5 */
2061 (s->s3->tmp.new_cipher->algorithms
2062 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
2063 != (SSL_aKRB5|SSL_kKRB5))
2065 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,ERR_R_INTERNAL_ERROR);
2066 return(0);
2069 l=ssl3_output_cert_chain(s,x);
2070 s->state=SSL3_ST_SW_CERT_B;
2071 s->init_num=(int)l;
2072 s->init_off=0;
2075 /* SSL3_ST_SW_CERT_B */
2076 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));