Merge from main trunk: lets see if this works ;-)
[OpenSSL.git] / ssl / s3_srvr.c
blob4704dfb59867a918d817e04d5cb51b8ca479912c
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.]
59 #define REUSE_CIPHER_BUG
60 #define NETSCAPE_HANG_BUG
63 #include <stdio.h>
64 #include <openssl/buffer.h>
65 #include <openssl/rand.h>
66 #include <openssl/objects.h>
67 #include <openssl/md5.h>
68 #include <openssl/sha.h>
69 #include <openssl/evp.h>
70 #include <openssl/x509.h>
71 #include "ssl_locl.h"
73 #ifndef NO_KRB5
74 #include "kssl.h"
75 #endif /* NO_KRB5 */
77 static SSL_METHOD *ssl3_get_server_method(int ver);
78 static int ssl3_get_client_hello(SSL *s);
79 static int ssl3_check_client_hello(SSL *s);
80 static int ssl3_send_server_hello(SSL *s);
81 static int ssl3_send_server_key_exchange(SSL *s);
82 static int ssl3_send_certificate_request(SSL *s);
83 static int ssl3_send_server_done(SSL *s);
84 static int ssl3_get_client_key_exchange(SSL *s);
85 static int ssl3_get_client_certificate(SSL *s);
86 static int ssl3_get_cert_verify(SSL *s);
87 static int ssl3_send_hello_request(SSL *s);
89 static SSL_METHOD *ssl3_get_server_method(int ver)
91 if (ver == SSL3_VERSION)
92 return(SSLv3_server_method());
93 else
94 return(NULL);
97 SSL_METHOD *SSLv3_server_method(void)
99 static int init=1;
100 static SSL_METHOD SSLv3_server_data;
102 if (init)
104 memcpy((char *)&SSLv3_server_data,(char *)sslv3_base_method(),
105 sizeof(SSL_METHOD));
106 SSLv3_server_data.ssl_accept=ssl3_accept;
107 SSLv3_server_data.get_ssl_method=ssl3_get_server_method;
108 init=0;
110 return(&SSLv3_server_data);
113 int ssl3_accept(SSL *s)
115 BUF_MEM *buf;
116 unsigned long l,Time=time(NULL);
117 void (*cb)()=NULL;
118 long num1;
119 int ret= -1;
120 int new_state,state,skip=0;
122 RAND_add(&Time,sizeof(Time),0);
123 ERR_clear_error();
124 clear_sys_error();
126 if (s->info_callback != NULL)
127 cb=s->info_callback;
128 else if (s->ctx->info_callback != NULL)
129 cb=s->ctx->info_callback;
131 /* init things to blank */
132 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
133 s->in_handshake++;
135 if (s->cert == NULL)
137 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
138 return(-1);
141 for (;;)
143 state=s->state;
145 switch (s->state)
147 case SSL_ST_RENEGOTIATE:
148 s->new_session=1;
149 /* s->state=SSL_ST_ACCEPT; */
151 case SSL_ST_BEFORE:
152 case SSL_ST_ACCEPT:
153 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
154 case SSL_ST_OK|SSL_ST_ACCEPT:
156 s->server=1;
157 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
159 if ((s->version>>8) != 3)
161 SSLerr(SSL_F_SSL3_ACCEPT, SSL_R_INTERNAL_ERROR);
162 return -1;
164 s->type=SSL_ST_ACCEPT;
166 if (s->init_buf == NULL)
168 if ((buf=BUF_MEM_new()) == NULL)
170 ret= -1;
171 goto end;
173 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
175 ret= -1;
176 goto end;
178 s->init_buf=buf;
181 if (!ssl3_setup_buffers(s))
183 ret= -1;
184 goto end;
187 /* Ok, we now need to push on a buffering BIO so that
188 * the output is sent in a way that TCP likes :-)
190 if (!ssl_init_wbio_buffer(s,1)) { ret= -1; goto end; }
192 s->init_num=0;
194 if (s->state != SSL_ST_RENEGOTIATE)
196 ssl3_init_finished_mac(s);
197 s->state=SSL3_ST_SR_CLNT_HELLO_A;
198 s->ctx->stats.sess_accept++;
200 else
202 s->ctx->stats.sess_accept_renegotiate++;
203 s->state=SSL3_ST_SW_HELLO_REQ_A;
205 break;
207 case SSL3_ST_SW_HELLO_REQ_A:
208 case SSL3_ST_SW_HELLO_REQ_B:
210 s->shutdown=0;
211 ret=ssl3_send_hello_request(s);
212 if (ret <= 0) goto end;
213 s->s3->tmp.next_state=SSL3_ST_SW_HELLO_REQ_C;
214 s->state=SSL3_ST_SW_FLUSH;
215 s->init_num=0;
217 ssl3_init_finished_mac(s);
218 break;
220 case SSL3_ST_SW_HELLO_REQ_C:
221 s->state=SSL_ST_OK;
222 ret=1;
223 goto end;
224 /* break; */
226 case SSL3_ST_SR_CLNT_HELLO_A:
227 case SSL3_ST_SR_CLNT_HELLO_B:
228 case SSL3_ST_SR_CLNT_HELLO_C:
230 s->shutdown=0;
231 ret=ssl3_get_client_hello(s);
232 if (ret <= 0) goto end;
233 s->state=SSL3_ST_SW_SRVR_HELLO_A;
234 s->init_num=0;
235 break;
237 case SSL3_ST_SW_SRVR_HELLO_A:
238 case SSL3_ST_SW_SRVR_HELLO_B:
239 ret=ssl3_send_server_hello(s);
240 if (ret <= 0) goto end;
242 if (s->hit)
243 s->state=SSL3_ST_SW_CHANGE_A;
244 else
245 s->state=SSL3_ST_SW_CERT_A;
246 s->init_num=0;
247 break;
249 case SSL3_ST_SW_CERT_A:
250 case SSL3_ST_SW_CERT_B:
251 /* Check if it is anon DH */
252 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
254 ret=ssl3_send_server_certificate(s);
255 if (ret <= 0) goto end;
257 else
258 skip=1;
259 s->state=SSL3_ST_SW_KEY_EXCH_A;
260 s->init_num=0;
261 break;
263 case SSL3_ST_SW_KEY_EXCH_A:
264 case SSL3_ST_SW_KEY_EXCH_B:
265 l=s->s3->tmp.new_cipher->algorithms;
267 /* clear this, it may get reset by
268 * send_server_key_exchange */
269 if ((s->options & SSL_OP_EPHEMERAL_RSA)
270 #ifndef NO_KRB5
271 && !(l & SSL_KRB5)
272 #endif /* NO_KRB5 */
274 s->s3->tmp.use_rsa_tmp=1;
275 else
276 s->s3->tmp.use_rsa_tmp=0;
278 /* only send if a DH key exchange, fortezza or
279 * RSA but we have a sign only certificate */
280 if (s->s3->tmp.use_rsa_tmp
281 || (l & (SSL_DH|SSL_kFZA))
282 || ((l & SSL_kRSA)
283 && (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL
284 || (SSL_C_IS_EXPORT(s->s3->tmp.new_cipher)
285 && EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey)*8 > SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher)
291 ret=ssl3_send_server_key_exchange(s);
292 if (ret <= 0) goto end;
294 else
295 skip=1;
297 s->state=SSL3_ST_SW_CERT_REQ_A;
298 s->init_num=0;
299 break;
301 case SSL3_ST_SW_CERT_REQ_A:
302 case SSL3_ST_SW_CERT_REQ_B:
303 if (/* don't request cert unless asked for it: */
304 !(s->verify_mode & SSL_VERIFY_PEER) ||
305 /* if SSL_VERIFY_CLIENT_ONCE is set,
306 * don't request cert during re-negotiation: */
307 ((s->session->peer != NULL) &&
308 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) ||
309 /* never request cert in anonymous ciphersuites
310 * (see section "Certificate request" in SSL 3 drafts
311 * and in RFC 2246): */
312 ((s->s3->tmp.new_cipher->algorithms & SSL_aNULL) &&
313 /* ... except when the application insists on verification
314 * (against the specs, but s3_clnt.c accepts this for SSL 3) */
315 !(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)))
317 /* no cert request */
318 skip=1;
319 s->s3->tmp.cert_request=0;
320 s->state=SSL3_ST_SW_SRVR_DONE_A;
322 else
324 s->s3->tmp.cert_request=1;
325 ret=ssl3_send_certificate_request(s);
326 if (ret <= 0) goto end;
327 #ifndef NETSCAPE_HANG_BUG
328 s->state=SSL3_ST_SW_SRVR_DONE_A;
329 #else
330 s->state=SSL3_ST_SW_FLUSH;
331 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
332 #endif
333 s->init_num=0;
335 break;
337 case SSL3_ST_SW_SRVR_DONE_A:
338 case SSL3_ST_SW_SRVR_DONE_B:
339 ret=ssl3_send_server_done(s);
340 if (ret <= 0) goto end;
341 s->s3->tmp.next_state=SSL3_ST_SR_CERT_A;
342 s->state=SSL3_ST_SW_FLUSH;
343 s->init_num=0;
344 break;
346 case SSL3_ST_SW_FLUSH:
347 /* number of bytes to be flushed */
348 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
349 if (num1 > 0)
351 s->rwstate=SSL_WRITING;
352 num1=BIO_flush(s->wbio);
353 if (num1 <= 0) { ret= -1; goto end; }
354 s->rwstate=SSL_NOTHING;
357 s->state=s->s3->tmp.next_state;
358 break;
360 case SSL3_ST_SR_CERT_A:
361 case SSL3_ST_SR_CERT_B:
362 /* Check for second client hello (MS SGC) */
363 ret = ssl3_check_client_hello(s);
364 if (ret <= 0)
365 goto end;
366 if (ret == 2)
367 s->state = SSL3_ST_SR_CLNT_HELLO_C;
368 else {
369 /* could be sent for a DH cert, even if we
370 * have not asked for it :-) */
371 ret=ssl3_get_client_certificate(s);
372 if (ret <= 0) goto end;
373 s->init_num=0;
374 s->state=SSL3_ST_SR_KEY_EXCH_A;
376 break;
378 case SSL3_ST_SR_KEY_EXCH_A:
379 case SSL3_ST_SR_KEY_EXCH_B:
380 ret=ssl3_get_client_key_exchange(s);
381 if (ret <= 0) goto end;
382 s->state=SSL3_ST_SR_CERT_VRFY_A;
383 s->init_num=0;
385 /* We need to get hashes here so if there is
386 * a client cert, it can be verified */
387 s->method->ssl3_enc->cert_verify_mac(s,
388 &(s->s3->finish_dgst1),
389 &(s->s3->tmp.cert_verify_md[0]));
390 s->method->ssl3_enc->cert_verify_mac(s,
391 &(s->s3->finish_dgst2),
392 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]));
394 break;
396 case SSL3_ST_SR_CERT_VRFY_A:
397 case SSL3_ST_SR_CERT_VRFY_B:
399 /* we should decide if we expected this one */
400 ret=ssl3_get_cert_verify(s);
401 if (ret <= 0) goto end;
403 s->state=SSL3_ST_SR_FINISHED_A;
404 s->init_num=0;
405 break;
407 case SSL3_ST_SR_FINISHED_A:
408 case SSL3_ST_SR_FINISHED_B:
409 ret=ssl3_get_finished(s,SSL3_ST_SR_FINISHED_A,
410 SSL3_ST_SR_FINISHED_B);
411 if (ret <= 0) goto end;
412 if (s->hit)
413 s->state=SSL_ST_OK;
414 else
415 s->state=SSL3_ST_SW_CHANGE_A;
416 s->init_num=0;
417 break;
419 case SSL3_ST_SW_CHANGE_A:
420 case SSL3_ST_SW_CHANGE_B:
422 s->session->cipher=s->s3->tmp.new_cipher;
423 if (!s->method->ssl3_enc->setup_key_block(s))
424 { ret= -1; goto end; }
426 ret=ssl3_send_change_cipher_spec(s,
427 SSL3_ST_SW_CHANGE_A,SSL3_ST_SW_CHANGE_B);
429 if (ret <= 0) goto end;
430 s->state=SSL3_ST_SW_FINISHED_A;
431 s->init_num=0;
433 if (!s->method->ssl3_enc->change_cipher_state(s,
434 SSL3_CHANGE_CIPHER_SERVER_WRITE))
436 ret= -1;
437 goto end;
440 break;
442 case SSL3_ST_SW_FINISHED_A:
443 case SSL3_ST_SW_FINISHED_B:
444 ret=ssl3_send_finished(s,
445 SSL3_ST_SW_FINISHED_A,SSL3_ST_SW_FINISHED_B,
446 s->method->ssl3_enc->server_finished_label,
447 s->method->ssl3_enc->server_finished_label_len);
448 if (ret <= 0) goto end;
449 s->state=SSL3_ST_SW_FLUSH;
450 if (s->hit)
451 s->s3->tmp.next_state=SSL3_ST_SR_FINISHED_A;
452 else
453 s->s3->tmp.next_state=SSL_ST_OK;
454 s->init_num=0;
455 break;
457 case SSL_ST_OK:
458 /* clean a few things up */
459 ssl3_cleanup_key_block(s);
461 BUF_MEM_free(s->init_buf);
462 s->init_buf=NULL;
464 /* remove buffering on output */
465 ssl_free_wbio_buffer(s);
467 s->new_session=0;
468 s->init_num=0;
470 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
472 s->ctx->stats.sess_accept_good++;
473 /* s->server=1; */
474 s->handshake_func=ssl3_accept;
475 ret=1;
477 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
479 goto end;
480 /* break; */
482 default:
483 SSLerr(SSL_F_SSL3_ACCEPT,SSL_R_UNKNOWN_STATE);
484 ret= -1;
485 goto end;
486 /* break; */
489 if (!s->s3->tmp.reuse_message && !skip)
491 if (s->debug)
493 if ((ret=BIO_flush(s->wbio)) <= 0)
494 goto end;
498 if ((cb != NULL) && (s->state != state))
500 new_state=s->state;
501 s->state=state;
502 cb(s,SSL_CB_ACCEPT_LOOP,1);
503 s->state=new_state;
506 skip=0;
508 end:
509 /* BIO_flush(s->wbio); */
511 if (cb != NULL)
512 cb(s,SSL_CB_ACCEPT_EXIT,ret);
513 s->in_handshake--;
514 return(ret);
517 static int ssl3_send_hello_request(SSL *s)
519 unsigned char *p;
521 if (s->state == SSL3_ST_SW_HELLO_REQ_A)
523 p=(unsigned char *)s->init_buf->data;
524 *(p++)=SSL3_MT_HELLO_REQUEST;
525 *(p++)=0;
526 *(p++)=0;
527 *(p++)=0;
529 s->state=SSL3_ST_SW_HELLO_REQ_B;
530 /* number of bytes to write */
531 s->init_num=4;
532 s->init_off=0;
535 /* SSL3_ST_SW_HELLO_REQ_B */
536 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
539 static int ssl3_check_client_hello(SSL *s)
541 int ok;
542 long n;
544 n=ssl3_get_message(s,
545 SSL3_ST_SR_CERT_A,
546 SSL3_ST_SR_CERT_B,
548 SSL3_RT_MAX_PLAIN_LENGTH,
549 &ok);
550 if (!ok) return((int)n);
551 s->s3->tmp.reuse_message = 1;
552 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_HELLO)
554 /* Throw away what we have done so far in the current handshake,
555 * which will now be aborted. (A full SSL_clear would be too much.)
556 * I hope that tmp.dh is the only thing that may need to be cleared
557 * when a handshake is not completed ... */
558 #ifndef NO_DH
559 if (s->s3->tmp.dh != NULL)
561 DH_free(s->s3->tmp.dh);
562 s->s3->tmp.dh = NULL;
564 #endif
565 return 2;
567 return 1;
570 static int ssl3_get_client_hello(SSL *s)
572 int i,j,ok,al,ret= -1;
573 long n;
574 unsigned long id;
575 unsigned char *p,*d,*q;
576 SSL_CIPHER *c;
577 SSL_COMP *comp=NULL;
578 STACK_OF(SSL_CIPHER) *ciphers=NULL;
580 /* We do this so that we will respond with our native type.
581 * If we are TLSv1 and we get SSLv3, we will respond with TLSv1,
582 * This down switching should be handled by a different method.
583 * If we are SSLv3, we will respond with SSLv3, even if prompted with
584 * TLSv1.
586 if (s->state == SSL3_ST_SR_CLNT_HELLO_A)
588 s->first_packet=1;
589 s->state=SSL3_ST_SR_CLNT_HELLO_B;
591 n=ssl3_get_message(s,
592 SSL3_ST_SR_CLNT_HELLO_B,
593 SSL3_ST_SR_CLNT_HELLO_C,
594 SSL3_MT_CLIENT_HELLO,
595 SSL3_RT_MAX_PLAIN_LENGTH,
596 &ok);
598 if (!ok) return((int)n);
599 d=p=(unsigned char *)s->init_buf->data;
601 /* use version from inside client hello, not from record header
602 * (may differ: see RFC 2246, Appendix E, second paragraph) */
603 s->client_version=(((int)p[0])<<8)|(int)p[1];
604 p+=2;
606 /* load the client random */
607 memcpy(s->s3->client_random,p,SSL3_RANDOM_SIZE);
608 p+=SSL3_RANDOM_SIZE;
610 /* get the session-id */
611 j= *(p++);
613 s->hit=0;
614 if (j == 0)
616 if (!ssl_get_new_session(s,1))
617 goto err;
619 else
621 i=ssl_get_prev_session(s,p,j);
622 if (i == 1)
623 { /* previous session */
624 s->hit=1;
626 else if (i == -1)
627 goto err;
628 else /* i == 0 */
630 if (!ssl_get_new_session(s,1))
631 goto err;
635 p+=j;
636 n2s(p,i);
637 if ((i == 0) && (j != 0))
639 /* we need a cipher if we are not resuming a session */
640 al=SSL_AD_ILLEGAL_PARAMETER;
641 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_SPECIFIED);
642 goto f_err;
644 if ((i+p) > (d+n))
646 /* not enough data */
647 al=SSL_AD_DECODE_ERROR;
648 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
649 goto f_err;
651 if ((i > 0) && (ssl_bytes_to_cipher_list(s,p,i,&(ciphers))
652 == NULL))
654 goto err;
656 p+=i;
658 /* If it is a hit, check that the cipher is in the list */
659 if ((s->hit) && (i > 0))
661 j=0;
662 id=s->session->cipher->id;
664 #ifdef CIPHER_DEBUG
665 printf("client sent %d ciphers\n",sk_num(ciphers));
666 #endif
667 for (i=0; i<sk_SSL_CIPHER_num(ciphers); i++)
669 c=sk_SSL_CIPHER_value(ciphers,i);
670 #ifdef CIPHER_DEBUG
671 printf("client [%2d of %2d]:%s\n",
672 i,sk_num(ciphers),SSL_CIPHER_get_name(c));
673 #endif
674 if (c->id == id)
676 j=1;
677 break;
680 if (j == 0)
682 if ((s->options & SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG) && (sk_SSL_CIPHER_num(ciphers) == 1))
684 /* Very bad for multi-threading.... */
685 s->session->cipher=sk_SSL_CIPHER_value(ciphers,
688 else
690 /* we need to have the cipher in the cipher
691 * list if we are asked to reuse it */
692 al=SSL_AD_ILLEGAL_PARAMETER;
693 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_REQUIRED_CIPHER_MISSING);
694 goto f_err;
699 /* compression */
700 i= *(p++);
701 q=p;
702 for (j=0; j<i; j++)
704 if (p[j] == 0) break;
707 p+=i;
708 if (j >= i)
710 /* no compress */
711 al=SSL_AD_DECODE_ERROR;
712 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_COMPRESSION_SPECIFIED);
713 goto f_err;
716 /* Worst case, we will use the NULL compression, but if we have other
717 * options, we will now look for them. We have i-1 compression
718 * algorithms from the client, starting at q. */
719 s->s3->tmp.new_compression=NULL;
720 if (s->ctx->comp_methods != NULL)
721 { /* See if we have a match */
722 int m,nn,o,v,done=0;
724 nn=sk_SSL_COMP_num(s->ctx->comp_methods);
725 for (m=0; m<nn; m++)
727 comp=sk_SSL_COMP_value(s->ctx->comp_methods,m);
728 v=comp->id;
729 for (o=0; o<i; o++)
731 if (v == q[o])
733 done=1;
734 break;
737 if (done) break;
739 if (done)
740 s->s3->tmp.new_compression=comp;
741 else
742 comp=NULL;
745 /* TLS does not mind if there is extra stuff */
746 if (s->version == SSL3_VERSION)
748 if (p > (d+n))
750 /* wrong number of bytes,
751 * there could be more to follow */
752 al=SSL_AD_DECODE_ERROR;
753 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_LENGTH_MISMATCH);
754 goto f_err;
758 /* Given s->session->ciphers and ssl_get_ciphers_by_id(s), we must
759 * pick a cipher */
761 if (!s->hit)
763 s->session->compress_meth=(comp == NULL)?0:comp->id;
764 if (s->session->ciphers != NULL)
765 sk_SSL_CIPHER_free(s->session->ciphers);
766 s->session->ciphers=ciphers;
767 if (ciphers == NULL)
769 al=SSL_AD_ILLEGAL_PARAMETER;
770 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_CIPHERS_PASSED);
771 goto f_err;
773 ciphers=NULL;
774 c=ssl3_choose_cipher(s,s->session->ciphers,
775 ssl_get_ciphers_by_id(s));
777 if (c == NULL)
779 al=SSL_AD_HANDSHAKE_FAILURE;
780 SSLerr(SSL_F_SSL3_GET_CLIENT_HELLO,SSL_R_NO_SHARED_CIPHER);
781 goto f_err;
783 s->s3->tmp.new_cipher=c;
785 else
787 /* Session-id reuse */
788 #ifdef REUSE_CIPHER_BUG
789 STACK_OF(SSL_CIPHER) *sk;
790 SSL_CIPHER *nc=NULL;
791 SSL_CIPHER *ec=NULL;
793 if (s->options & SSL_OP_NETSCAPE_DEMO_CIPHER_CHANGE_BUG)
795 sk=s->session->ciphers;
796 for (i=0; i<sk_SSL_CIPHER_num(sk); i++)
798 c=sk_SSL_CIPHER_value(sk,i);
799 if (c->algorithms & SSL_eNULL)
800 nc=c;
801 if (SSL_C_IS_EXPORT(c))
802 ec=c;
804 if (nc != NULL)
805 s->s3->tmp.new_cipher=nc;
806 else if (ec != NULL)
807 s->s3->tmp.new_cipher=ec;
808 else
809 s->s3->tmp.new_cipher=s->session->cipher;
811 else
812 #endif
813 s->s3->tmp.new_cipher=s->session->cipher;
816 /* we now have the following setup.
817 * client_random
818 * cipher_list - our prefered list of ciphers
819 * ciphers - the clients prefered list of ciphers
820 * compression - basically ignored right now
821 * ssl version is set - sslv3
822 * s->session - The ssl session has been setup.
823 * s->hit - session reuse flag
824 * s->tmp.new_cipher - the new cipher to use.
827 ret=1;
828 if (0)
830 f_err:
831 ssl3_send_alert(s,SSL3_AL_FATAL,al);
833 err:
834 if (ciphers != NULL) sk_SSL_CIPHER_free(ciphers);
835 return(ret);
838 static int ssl3_send_server_hello(SSL *s)
840 unsigned char *buf;
841 unsigned char *p,*d;
842 int i,sl;
843 unsigned long l,Time;
845 if (s->state == SSL3_ST_SW_SRVR_HELLO_A)
847 buf=(unsigned char *)s->init_buf->data;
848 p=s->s3->server_random;
849 Time=time(NULL); /* Time */
850 l2n(Time,p);
851 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
852 /* Do the message type and length last */
853 d=p= &(buf[4]);
855 *(p++)=s->version>>8;
856 *(p++)=s->version&0xff;
858 /* Random stuff */
859 memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
860 p+=SSL3_RANDOM_SIZE;
862 /* now in theory we have 3 options to sending back the
863 * session id. If it is a re-use, we send back the
864 * old session-id, if it is a new session, we send
865 * back the new session-id or we send back a 0 length
866 * session-id if we want it to be single use.
867 * Currently I will not implement the '0' length session-id
868 * 12-Jan-98 - I'll now support the '0' length stuff.
870 if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER))
871 s->session->session_id_length=0;
873 sl=s->session->session_id_length;
874 *(p++)=sl;
875 memcpy(p,s->session->session_id,sl);
876 p+=sl;
878 /* put the cipher */
879 i=ssl3_put_cipher_by_char(s->s3->tmp.new_cipher,p);
880 p+=i;
882 /* put the compression method */
883 if (s->s3->tmp.new_compression == NULL)
884 *(p++)=0;
885 else
886 *(p++)=s->s3->tmp.new_compression->id;
888 /* do the header */
889 l=(p-d);
890 d=buf;
891 *(d++)=SSL3_MT_SERVER_HELLO;
892 l2n3(l,d);
894 s->state=SSL3_ST_CW_CLNT_HELLO_B;
895 /* number of bytes to write */
896 s->init_num=p-buf;
897 s->init_off=0;
900 /* SSL3_ST_CW_CLNT_HELLO_B */
901 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
904 static int ssl3_send_server_done(SSL *s)
906 unsigned char *p;
908 if (s->state == SSL3_ST_SW_SRVR_DONE_A)
910 p=(unsigned char *)s->init_buf->data;
912 /* do the header */
913 *(p++)=SSL3_MT_SERVER_DONE;
914 *(p++)=0;
915 *(p++)=0;
916 *(p++)=0;
918 s->state=SSL3_ST_SW_SRVR_DONE_B;
919 /* number of bytes to write */
920 s->init_num=4;
921 s->init_off=0;
924 /* SSL3_ST_CW_CLNT_HELLO_B */
925 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
928 static int ssl3_send_server_key_exchange(SSL *s)
930 #ifndef NO_RSA
931 unsigned char *q;
932 int j,num;
933 RSA *rsa;
934 unsigned char md_buf[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
935 unsigned int u;
936 #endif
937 #ifndef NO_DH
938 DH *dh=NULL,*dhp;
939 #endif
940 EVP_PKEY *pkey;
941 unsigned char *p,*d;
942 int al,i;
943 unsigned long type;
944 int n;
945 CERT *cert;
946 BIGNUM *r[4];
947 int nr[4],kn;
948 BUF_MEM *buf;
949 EVP_MD_CTX md_ctx;
951 if (s->state == SSL3_ST_SW_KEY_EXCH_A)
953 type=s->s3->tmp.new_cipher->algorithms & SSL_MKEY_MASK;
954 cert=s->cert;
956 buf=s->init_buf;
958 r[0]=r[1]=r[2]=r[3]=NULL;
959 n=0;
960 #ifndef NO_RSA
961 if (type & SSL_kRSA)
963 rsa=cert->rsa_tmp;
964 if ((rsa == NULL) && (s->cert->rsa_tmp_cb != NULL))
966 rsa=s->cert->rsa_tmp_cb(s,
967 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
968 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
969 if(rsa == NULL)
971 al=SSL_AD_HANDSHAKE_FAILURE;
972 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_ERROR_GENERATING_TMP_RSA_KEY);
973 goto f_err;
975 CRYPTO_add(&rsa->references,1,CRYPTO_LOCK_RSA);
976 cert->rsa_tmp=rsa;
978 if (rsa == NULL)
980 al=SSL_AD_HANDSHAKE_FAILURE;
981 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_KEY);
982 goto f_err;
984 r[0]=rsa->n;
985 r[1]=rsa->e;
986 s->s3->tmp.use_rsa_tmp=1;
988 else
989 #endif
990 #ifndef NO_DH
991 if (type & SSL_kEDH)
993 dhp=cert->dh_tmp;
994 if ((dhp == NULL) && (s->cert->dh_tmp_cb != NULL))
995 dhp=s->cert->dh_tmp_cb(s,
996 SSL_C_IS_EXPORT(s->s3->tmp.new_cipher),
997 SSL_C_EXPORT_PKEYLENGTH(s->s3->tmp.new_cipher));
998 if (dhp == NULL)
1000 al=SSL_AD_HANDSHAKE_FAILURE;
1001 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1002 goto f_err;
1005 if (s->s3->tmp.dh != NULL)
1007 DH_free(dh);
1008 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE, SSL_R_INTERNAL_ERROR);
1009 goto err;
1012 if ((dh=DHparams_dup(dhp)) == NULL)
1014 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1015 goto err;
1018 s->s3->tmp.dh=dh;
1019 if ((dhp->pub_key == NULL ||
1020 dhp->priv_key == NULL ||
1021 (s->options & SSL_OP_SINGLE_DH_USE)))
1023 if(!DH_generate_key(dh))
1025 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,
1026 ERR_R_DH_LIB);
1027 goto err;
1030 else
1032 dh->pub_key=BN_dup(dhp->pub_key);
1033 dh->priv_key=BN_dup(dhp->priv_key);
1034 if ((dh->pub_key == NULL) ||
1035 (dh->priv_key == NULL))
1037 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_R_DH_LIB);
1038 goto err;
1041 r[0]=dh->p;
1042 r[1]=dh->g;
1043 r[2]=dh->pub_key;
1045 else
1046 #endif
1048 al=SSL_AD_HANDSHAKE_FAILURE;
1049 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1050 goto f_err;
1052 for (i=0; r[i] != NULL; i++)
1054 nr[i]=BN_num_bytes(r[i]);
1055 n+=2+nr[i];
1058 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
1060 if ((pkey=ssl_get_sign_pkey(s,s->s3->tmp.new_cipher))
1061 == NULL)
1063 al=SSL_AD_DECODE_ERROR;
1064 goto f_err;
1066 kn=EVP_PKEY_size(pkey);
1068 else
1070 pkey=NULL;
1071 kn=0;
1074 if (!BUF_MEM_grow(buf,n+4+kn))
1076 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_BUF);
1077 goto err;
1079 d=(unsigned char *)s->init_buf->data;
1080 p= &(d[4]);
1082 for (i=0; r[i] != NULL; i++)
1084 s2n(nr[i],p);
1085 BN_bn2bin(r[i],p);
1086 p+=nr[i];
1089 /* not anonymous */
1090 if (pkey != NULL)
1092 /* n is the length of the params, they start at &(d[4])
1093 * and p points to the space at the end. */
1094 #ifndef NO_RSA
1095 if (pkey->type == EVP_PKEY_RSA)
1097 q=md_buf;
1098 j=0;
1099 for (num=2; num > 0; num--)
1101 EVP_DigestInit(&md_ctx,(num == 2)
1102 ?s->ctx->md5:s->ctx->sha1);
1103 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1104 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1105 EVP_DigestUpdate(&md_ctx,&(d[4]),n);
1106 EVP_DigestFinal(&md_ctx,q,
1107 (unsigned int *)&i);
1108 q+=i;
1109 j+=i;
1111 if (RSA_sign(NID_md5_sha1, md_buf, j,
1112 &(p[2]), &u, pkey->pkey.rsa) <= 0)
1114 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_RSA);
1115 goto err;
1117 s2n(u,p);
1118 n+=u+2;
1120 else
1121 #endif
1122 #if !defined(NO_DSA)
1123 if (pkey->type == EVP_PKEY_DSA)
1125 /* lets do DSS */
1126 EVP_SignInit(&md_ctx,EVP_dss1());
1127 EVP_SignUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1128 EVP_SignUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1129 EVP_SignUpdate(&md_ctx,&(d[4]),n);
1130 if (!EVP_SignFinal(&md_ctx,&(p[2]),
1131 (unsigned int *)&i,pkey))
1133 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,ERR_LIB_DSA);
1134 goto err;
1136 s2n(i,p);
1137 n+=i+2;
1139 else
1140 #endif
1142 /* Is this error check actually needed? */
1143 al=SSL_AD_HANDSHAKE_FAILURE;
1144 SSLerr(SSL_F_SSL3_SEND_SERVER_KEY_EXCHANGE,SSL_R_UNKNOWN_PKEY_TYPE);
1145 goto f_err;
1149 *(d++)=SSL3_MT_SERVER_KEY_EXCHANGE;
1150 l2n3(n,d);
1152 /* we should now have things packed up, so lets send
1153 * it off */
1154 s->init_num=n+4;
1155 s->init_off=0;
1158 s->state = SSL3_ST_SW_KEY_EXCH_B;
1159 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1160 f_err:
1161 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1162 err:
1163 return(-1);
1166 static int ssl3_send_certificate_request(SSL *s)
1168 unsigned char *p,*d;
1169 int i,j,nl,off,n;
1170 STACK_OF(X509_NAME) *sk=NULL;
1171 X509_NAME *name;
1172 BUF_MEM *buf;
1174 if (s->state == SSL3_ST_SW_CERT_REQ_A)
1176 buf=s->init_buf;
1178 d=p=(unsigned char *)&(buf->data[4]);
1180 /* get the list of acceptable cert types */
1181 p++;
1182 n=ssl3_get_req_cert_type(s,p);
1183 d[0]=n;
1184 p+=n;
1185 n++;
1187 off=n;
1188 p+=2;
1189 n+=2;
1191 sk=SSL_get_client_CA_list(s);
1192 nl=0;
1193 if (sk != NULL)
1195 for (i=0; i<sk_X509_NAME_num(sk); i++)
1197 name=sk_X509_NAME_value(sk,i);
1198 j=i2d_X509_NAME(name,NULL);
1199 if (!BUF_MEM_grow(buf,4+n+j+2))
1201 SSLerr(SSL_F_SSL3_SEND_CERTIFICATE_REQUEST,ERR_R_BUF_LIB);
1202 goto err;
1204 p=(unsigned char *)&(buf->data[4+n]);
1205 if (!(s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
1207 s2n(j,p);
1208 i2d_X509_NAME(name,&p);
1209 n+=2+j;
1210 nl+=2+j;
1212 else
1214 d=p;
1215 i2d_X509_NAME(name,&p);
1216 j-=2; s2n(j,d); j+=2;
1217 n+=j;
1218 nl+=j;
1222 /* else no CA names */
1223 p=(unsigned char *)&(buf->data[4+off]);
1224 s2n(nl,p);
1226 d=(unsigned char *)buf->data;
1227 *(d++)=SSL3_MT_CERTIFICATE_REQUEST;
1228 l2n3(n,d);
1230 /* we should now have things packed up, so lets send
1231 * it off */
1233 s->init_num=n+4;
1234 s->init_off=0;
1235 #ifdef NETSCAPE_HANG_BUG
1236 p=(unsigned char *)s->init_buf->data + s->init_num;
1238 /* do the header */
1239 *(p++)=SSL3_MT_SERVER_DONE;
1240 *(p++)=0;
1241 *(p++)=0;
1242 *(p++)=0;
1243 s->init_num += 4;
1244 #endif
1248 /* SSL3_ST_SW_CERT_REQ_B */
1249 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1250 err:
1251 return(-1);
1254 static int ssl3_get_client_key_exchange(SSL *s)
1256 int i,al,ok;
1257 long n;
1258 unsigned long l;
1259 unsigned char *p;
1260 #ifndef NO_RSA
1261 RSA *rsa=NULL;
1262 EVP_PKEY *pkey=NULL;
1263 #endif
1264 #ifndef NO_DH
1265 BIGNUM *pub=NULL;
1266 DH *dh_srvr;
1267 #endif
1268 #ifndef NO_KRB5
1269 KSSL_ERR kssl_err;
1270 #endif /* NO_KRB5 */
1272 n=ssl3_get_message(s,
1273 SSL3_ST_SR_KEY_EXCH_A,
1274 SSL3_ST_SR_KEY_EXCH_B,
1275 SSL3_MT_CLIENT_KEY_EXCHANGE,
1276 2048, /* ??? */
1277 &ok);
1279 if (!ok) return((int)n);
1280 p=(unsigned char *)s->init_buf->data;
1282 l=s->s3->tmp.new_cipher->algorithms;
1284 #ifndef NO_RSA
1285 if (l & SSL_kRSA)
1287 /* FIX THIS UP EAY EAY EAY EAY */
1288 if (s->s3->tmp.use_rsa_tmp)
1290 if ((s->cert != NULL) && (s->cert->rsa_tmp != NULL))
1291 rsa=s->cert->rsa_tmp;
1292 /* Don't do a callback because rsa_tmp should
1293 * be sent already */
1294 if (rsa == NULL)
1296 al=SSL_AD_HANDSHAKE_FAILURE;
1297 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_RSA_PKEY);
1298 goto f_err;
1302 else
1304 pkey=s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey;
1305 if ( (pkey == NULL) ||
1306 (pkey->type != EVP_PKEY_RSA) ||
1307 (pkey->pkey.rsa == NULL))
1309 al=SSL_AD_HANDSHAKE_FAILURE;
1310 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_RSA_CERTIFICATE);
1311 goto f_err;
1313 rsa=pkey->pkey.rsa;
1316 /* TLS */
1317 if (s->version > SSL3_VERSION)
1319 n2s(p,i);
1320 if (n != i+2)
1322 if (!(s->options & SSL_OP_TLS_D5_BUG))
1324 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG);
1325 goto err;
1327 else
1328 p-=2;
1330 else
1331 n=i;
1334 i=RSA_private_decrypt((int)n,p,p,rsa,RSA_PKCS1_PADDING);
1336 if (i != SSL_MAX_MASTER_KEY_LENGTH)
1338 al=SSL_AD_DECODE_ERROR;
1339 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1340 goto f_err;
1343 if (!((p[0] == (s->client_version>>8)) && (p[1] == (s->client_version & 0xff))))
1345 /* The premaster secret must contain the same version number as the
1346 * ClientHello to detect version rollback attacks (strangely, the
1347 * protocol does not offer such protection for DH ciphersuites).
1348 * However, buggy clients exist that send the negotiated protocol
1349 * version instead if the server does not support the requested
1350 * protocol version.
1351 * If SSL_OP_TLS_ROLLBACK_BUG is set, tolerate such clients. */
1352 if (!((s->options & SSL_OP_TLS_ROLLBACK_BUG) &&
1353 (p[0] == (s->version>>8)) && (p[1] == (s->version & 0xff))))
1355 al=SSL_AD_DECODE_ERROR;
1356 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BAD_PROTOCOL_VERSION_NUMBER);
1357 goto f_err;
1361 s->session->master_key_length=
1362 s->method->ssl3_enc->generate_master_secret(s,
1363 s->session->master_key,
1364 p,i);
1365 memset(p,0,i);
1367 else
1368 #endif
1369 #ifndef NO_DH
1370 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1372 n2s(p,i);
1373 if (n != i+2)
1375 if (!(s->options & SSL_OP_SSLEAY_080_CLIENT_DH_BUG))
1377 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG);
1378 goto err;
1380 else
1382 p-=2;
1383 i=(int)n;
1387 if (n == 0L) /* the parameters are in the cert */
1389 al=SSL_AD_HANDSHAKE_FAILURE;
1390 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_DECODE_DH_CERTS);
1391 goto f_err;
1393 else
1395 if (s->s3->tmp.dh == NULL)
1397 al=SSL_AD_HANDSHAKE_FAILURE;
1398 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_MISSING_TMP_DH_KEY);
1399 goto f_err;
1401 else
1402 dh_srvr=s->s3->tmp.dh;
1405 pub=BN_bin2bn(p,i,NULL);
1406 if (pub == NULL)
1408 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_BN_LIB);
1409 goto err;
1412 i=DH_compute_key(p,pub,dh_srvr);
1414 if (i <= 0)
1416 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1417 goto err;
1420 DH_free(s->s3->tmp.dh);
1421 s->s3->tmp.dh=NULL;
1423 BN_clear_free(pub);
1424 pub=NULL;
1425 s->session->master_key_length=
1426 s->method->ssl3_enc->generate_master_secret(s,
1427 s->session->master_key,p,i);
1429 else
1430 #endif
1431 #ifndef NO_KRB5
1432 if (l & SSL_kKRB5)
1434 krb5_error_code krb5rc;
1435 KSSL_CTX *kssl_ctx = s->kssl_ctx;
1437 if (!kssl_ctx) kssl_ctx = kssl_ctx_new();
1438 if ((krb5rc = kssl_sget_tkt(kssl_ctx,
1439 s->init_buf->data, s->init_buf->length,
1440 &kssl_err)) != 0)
1442 #ifdef KSSL_DEBUG
1443 printf("kssl_sget_tkt rtn %d [%d]\n",
1444 krb5rc, kssl_err.reason);
1445 if (kssl_err.text)
1446 printf("kssl_err text= %s\n", kssl_err.text);
1447 #endif /* KSSL_DEBUG */
1448 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,
1449 kssl_err.reason);
1450 goto err;
1453 #ifdef KSSL_DEBUG
1454 kssl_ctx_show(kssl_ctx);
1455 #endif /* KSSL_DEBUG */
1457 /* 19991013 VRS - 3DES is kind of bogus here,
1458 ** at least until Kerberos supports 3DES. The only
1459 ** real secret is the 8-byte Kerberos session key;
1460 ** the other key material (client_random, server_random)
1461 ** could be sniffed. Nonces may help against replays though.
1463 ** Alternate code for Kerberos Purists:
1465 ** memcpy(s->session->master_key, kssl_ctx->key, kssl_ctx->length);
1466 ** s->session->master_key_length = kssl_ctx->length;
1468 s->session->master_key_length=
1469 s->method->ssl3_enc->generate_master_secret(s,
1470 s->session->master_key, kssl_ctx->key, kssl_ctx->length);
1471 /* Was doing kssl_ctx_free() here, but it caused problems for apache.
1472 ** kssl_ctx = kssl_ctx_free(kssl_ctx);
1473 ** if (s->kssl_ctx) s->kssl_ctx = NULL;
1476 else
1477 #endif /* NO_KRB5 */
1479 al=SSL_AD_HANDSHAKE_FAILURE;
1480 SSLerr(SSL_F_SSL3_GET_CLIENT_KEY_EXCHANGE,SSL_R_UNKNOWN_CIPHER_TYPE);
1481 goto f_err;
1484 return(1);
1485 f_err:
1486 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1487 #if !defined(NO_DH) || !defined(NO_RSA)
1488 err:
1489 #endif
1490 return(-1);
1493 static int ssl3_get_cert_verify(SSL *s)
1495 EVP_PKEY *pkey=NULL;
1496 unsigned char *p;
1497 int al,ok,ret=0;
1498 long n;
1499 int type=0,i,j;
1500 X509 *peer;
1502 n=ssl3_get_message(s,
1503 SSL3_ST_SR_CERT_VRFY_A,
1504 SSL3_ST_SR_CERT_VRFY_B,
1506 512, /* 512? */
1507 &ok);
1509 if (!ok) return((int)n);
1511 if (s->session->peer != NULL)
1513 peer=s->session->peer;
1514 pkey=X509_get_pubkey(peer);
1515 type=X509_certificate_type(peer,pkey);
1517 else
1519 peer=NULL;
1520 pkey=NULL;
1523 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_VERIFY)
1525 s->s3->tmp.reuse_message=1;
1526 if ((peer != NULL) && (type | EVP_PKT_SIGN))
1528 al=SSL_AD_UNEXPECTED_MESSAGE;
1529 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_MISSING_VERIFY_MESSAGE);
1530 goto f_err;
1532 ret=1;
1533 goto end;
1536 if (peer == NULL)
1538 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_NO_CLIENT_CERT_RECEIVED);
1539 al=SSL_AD_UNEXPECTED_MESSAGE;
1540 goto f_err;
1543 if (!(type & EVP_PKT_SIGN))
1545 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_SIGNATURE_FOR_NON_SIGNING_CERTIFICATE);
1546 al=SSL_AD_ILLEGAL_PARAMETER;
1547 goto f_err;
1550 if (s->s3->change_cipher_spec)
1552 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_CCS_RECEIVED_EARLY);
1553 al=SSL_AD_UNEXPECTED_MESSAGE;
1554 goto f_err;
1557 /* we now have a signature that we need to verify */
1558 p=(unsigned char *)s->init_buf->data;
1559 n2s(p,i);
1560 n-=2;
1561 if (i > n)
1563 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_LENGTH_MISMATCH);
1564 al=SSL_AD_DECODE_ERROR;
1565 goto f_err;
1568 j=EVP_PKEY_size(pkey);
1569 if ((i > j) || (n > j) || (n <= 0))
1571 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_WRONG_SIGNATURE_SIZE);
1572 al=SSL_AD_DECODE_ERROR;
1573 goto f_err;
1576 #ifndef NO_RSA
1577 if (pkey->type == EVP_PKEY_RSA)
1579 i=RSA_verify(NID_md5_sha1, s->s3->tmp.cert_verify_md,
1580 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH, p, i,
1581 pkey->pkey.rsa);
1582 if (i < 0)
1584 al=SSL_AD_DECRYPT_ERROR;
1585 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_DECRYPT);
1586 goto f_err;
1588 if (i == 0)
1590 al=SSL_AD_DECRYPT_ERROR;
1591 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_RSA_SIGNATURE);
1592 goto f_err;
1595 else
1596 #endif
1597 #ifndef NO_DSA
1598 if (pkey->type == EVP_PKEY_DSA)
1600 j=DSA_verify(pkey->save_type,
1601 &(s->s3->tmp.cert_verify_md[MD5_DIGEST_LENGTH]),
1602 SHA_DIGEST_LENGTH,p,i,pkey->pkey.dsa);
1603 if (j <= 0)
1605 /* bad signature */
1606 al=SSL_AD_DECRYPT_ERROR;
1607 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_BAD_DSA_SIGNATURE);
1608 goto f_err;
1611 else
1612 #endif
1614 SSLerr(SSL_F_SSL3_GET_CERT_VERIFY,SSL_R_INTERNAL_ERROR);
1615 al=SSL_AD_UNSUPPORTED_CERTIFICATE;
1616 goto f_err;
1620 ret=1;
1621 if (0)
1623 f_err:
1624 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1626 end:
1627 EVP_PKEY_free(pkey);
1628 return(ret);
1631 static int ssl3_get_client_certificate(SSL *s)
1633 int i,ok,al,ret= -1;
1634 X509 *x=NULL;
1635 unsigned long l,nc,llen,n;
1636 unsigned char *p,*d,*q;
1637 STACK_OF(X509) *sk=NULL;
1639 n=ssl3_get_message(s,
1640 SSL3_ST_SR_CERT_A,
1641 SSL3_ST_SR_CERT_B,
1643 #if defined(MSDOS) && !defined(WIN32)
1644 1024*30, /* 30k max cert list :-) */
1645 #else
1646 1024*100, /* 100k max cert list :-) */
1647 #endif
1648 &ok);
1650 if (!ok) return((int)n);
1652 if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE)
1654 if ( (s->verify_mode & SSL_VERIFY_PEER) &&
1655 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1657 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1658 al=SSL_AD_HANDSHAKE_FAILURE;
1659 goto f_err;
1661 /* If tls asked for a client cert, the client must return a 0 list */
1662 if ((s->version > SSL3_VERSION) && s->s3->tmp.cert_request)
1664 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST);
1665 al=SSL_AD_UNEXPECTED_MESSAGE;
1666 goto f_err;
1668 s->s3->tmp.reuse_message=1;
1669 return(1);
1672 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
1674 al=SSL_AD_UNEXPECTED_MESSAGE;
1675 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_WRONG_MESSAGE_TYPE);
1676 goto f_err;
1678 d=p=(unsigned char *)s->init_buf->data;
1680 if ((sk=sk_X509_new_null()) == NULL)
1682 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1683 goto err;
1686 n2l3(p,llen);
1687 if (llen+3 != n)
1689 al=SSL_AD_DECODE_ERROR;
1690 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
1691 goto f_err;
1693 for (nc=0; nc<llen; )
1695 n2l3(p,l);
1696 if ((l+nc+3) > llen)
1698 al=SSL_AD_DECODE_ERROR;
1699 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1700 goto f_err;
1703 q=p;
1704 x=d2i_X509(NULL,&p,l);
1705 if (x == NULL)
1707 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_ASN1_LIB);
1708 goto err;
1710 if (p != (q+l))
1712 al=SSL_AD_DECODE_ERROR;
1713 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
1714 goto f_err;
1716 if (!sk_X509_push(sk,x))
1718 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,ERR_R_MALLOC_FAILURE);
1719 goto err;
1721 x=NULL;
1722 nc+=l+3;
1725 if (sk_X509_num(sk) <= 0)
1727 /* TLS does not mind 0 certs returned */
1728 if (s->version == SSL3_VERSION)
1730 al=SSL_AD_HANDSHAKE_FAILURE;
1731 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATES_RETURNED);
1732 goto f_err;
1734 /* Fail for TLS only if we required a certificate */
1735 else if ((s->verify_mode & SSL_VERIFY_PEER) &&
1736 (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT))
1738 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
1739 al=SSL_AD_HANDSHAKE_FAILURE;
1740 goto f_err;
1743 else
1745 i=ssl_verify_cert_chain(s,sk);
1746 if (!i)
1748 al=ssl_verify_alarm_type(s->verify_result);
1749 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE,SSL_R_NO_CERTIFICATE_RETURNED);
1750 goto f_err;
1754 if (s->session->peer != NULL) /* This should not be needed */
1755 X509_free(s->session->peer);
1756 s->session->peer=sk_X509_shift(sk);
1757 s->session->verify_result = s->verify_result;
1759 /* With the current implementation, sess_cert will always be NULL
1760 * when we arrive here. */
1761 if (s->session->sess_cert == NULL)
1763 s->session->sess_cert = ssl_sess_cert_new();
1764 if (s->session->sess_cert == NULL)
1766 SSLerr(SSL_F_SSL3_GET_CLIENT_CERTIFICATE, ERR_R_MALLOC_FAILURE);
1767 goto err;
1770 if (s->session->sess_cert->cert_chain != NULL)
1771 sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free);
1772 s->session->sess_cert->cert_chain=sk;
1773 /* Inconsistency alert: cert_chain does *not* include the
1774 * peer's own certificate, while we do include it in s3_clnt.c */
1776 sk=NULL;
1778 ret=1;
1779 if (0)
1781 f_err:
1782 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1784 err:
1785 if (x != NULL) X509_free(x);
1786 if (sk != NULL) sk_X509_pop_free(sk,X509_free);
1787 return(ret);
1790 int ssl3_send_server_certificate(SSL *s)
1792 unsigned long l;
1793 X509 *x;
1795 if (s->state == SSL3_ST_SW_CERT_A)
1797 x=ssl_get_server_send_cert(s);
1798 if (x == NULL &&
1799 /* VRS: allow null cert if auth == KRB5 */
1800 (s->s3->tmp.new_cipher->algorithms
1801 & (SSL_MKEY_MASK|SSL_AUTH_MASK))
1802 != (SSL_aKRB5|SSL_kKRB5))
1804 SSLerr(SSL_F_SSL3_SEND_SERVER_CERTIFICATE,SSL_R_INTERNAL_ERROR);
1805 return(0);
1808 l=ssl3_output_cert_chain(s,x);
1809 s->state=SSL3_ST_SW_CERT_B;
1810 s->init_num=(int)l;
1811 s->init_off=0;
1814 /* SSL3_ST_SW_CERT_B */
1815 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));