]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/s3_clnt.c
Add missing DEPFLAG.
[thirdparty/openssl.git] / ssl / s3_clnt.c
CommitLineData
d02b48c6 1/* ssl/s3_clnt.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#include <stdio.h>
ec577822
BM
60#include <openssl/buffer.h>
61#include <openssl/rand.h>
62#include <openssl/objects.h>
63#include <openssl/evp.h>
d02b48c6
RE
64#include "ssl_locl.h"
65
9b3086fe 66static SSL_METHOD *ssl3_get_client_method(int ver);
d02b48c6
RE
67static int ssl3_client_hello(SSL *s);
68static int ssl3_get_server_hello(SSL *s);
69static int ssl3_get_certificate_request(SSL *s);
70static int ca_dn_cmp(X509_NAME **a,X509_NAME **b);
71static int ssl3_get_server_done(SSL *s);
72static int ssl3_send_client_verify(SSL *s);
73static int ssl3_send_client_certificate(SSL *s);
74static int ssl3_send_client_key_exchange(SSL *s);
75static int ssl3_get_key_exchange(SSL *s);
76static int ssl3_get_server_certificate(SSL *s);
77static int ssl3_check_cert_and_algorithm(SSL *s);
6b691a5c 78static SSL_METHOD *ssl3_get_client_method(int ver)
d02b48c6 79 {
58964a49 80 if (ver == SSL3_VERSION)
d02b48c6
RE
81 return(SSLv3_client_method());
82 else
83 return(NULL);
84 }
85
6b691a5c 86SSL_METHOD *SSLv3_client_method(void)
d02b48c6
RE
87 {
88 static int init=1;
89 static SSL_METHOD SSLv3_client_data;
90
91 if (init)
92 {
93 init=0;
94 memcpy((char *)&SSLv3_client_data,(char *)sslv3_base_method(),
95 sizeof(SSL_METHOD));
96 SSLv3_client_data.ssl_connect=ssl3_connect;
97 SSLv3_client_data.get_ssl_method=ssl3_get_client_method;
98 }
99 return(&SSLv3_client_data);
100 }
101
6b691a5c 102int ssl3_connect(SSL *s)
d02b48c6
RE
103 {
104 BUF_MEM *buf;
105 unsigned long Time=time(NULL),l;
106 long num1;
107 void (*cb)()=NULL;
108 int ret= -1;
d02b48c6
RE
109 int new_state,state,skip=0;;
110
bf5dcd13 111 RAND_seed(&Time,sizeof(Time));
d02b48c6 112 ERR_clear_error();
58964a49 113 clear_sys_error();
d02b48c6
RE
114
115 if (s->info_callback != NULL)
116 cb=s->info_callback;
117 else if (s->ctx->info_callback != NULL)
118 cb=s->ctx->info_callback;
119
120 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
121 s->in_handshake++;
122
123 for (;;)
124 {
125 state=s->state;
126
127 switch(s->state)
128 {
129 case SSL_ST_RENEGOTIATE:
130 s->new_session=1;
131 s->state=SSL_ST_CONNECT;
413c4f45 132 s->ctx->stats.sess_connect_renegotiate++;
d02b48c6
RE
133 /* break */
134 case SSL_ST_BEFORE:
135 case SSL_ST_CONNECT:
136 case SSL_ST_BEFORE|SSL_ST_CONNECT:
137 case SSL_ST_OK|SSL_ST_CONNECT:
138
413c4f45 139 s->server=0;
d02b48c6
RE
140 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
141
58964a49
RE
142 if ((s->version & 0xff00 ) != 0x0300)
143 abort();
144 /* s->version=SSL3_VERSION; */
d02b48c6
RE
145 s->type=SSL_ST_CONNECT;
146
147 if (s->init_buf == NULL)
148 {
149 if ((buf=BUF_MEM_new()) == NULL)
150 {
151 ret= -1;
152 goto end;
153 }
154 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
155 {
156 ret= -1;
157 goto end;
158 }
159 s->init_buf=buf;
160 }
161
162 if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
163
164 /* setup buffing BIO */
58964a49 165 if (!ssl_init_wbio_buffer(s,0)) { ret= -1; goto end; }
d02b48c6
RE
166
167 /* don't push the buffering BIO quite yet */
168
169 ssl3_init_finished_mac(s);
170
171 s->state=SSL3_ST_CW_CLNT_HELLO_A;
413c4f45 172 s->ctx->stats.sess_connect++;
d02b48c6
RE
173 s->init_num=0;
174 break;
175
176 case SSL3_ST_CW_CLNT_HELLO_A:
177 case SSL3_ST_CW_CLNT_HELLO_B:
178
179 s->shutdown=0;
180 ret=ssl3_client_hello(s);
181 if (ret <= 0) goto end;
182 s->state=SSL3_ST_CR_SRVR_HELLO_A;
183 s->init_num=0;
184
185 /* turn on buffering for the next lot of output */
58964a49
RE
186 if (s->bbio != s->wbio)
187 s->wbio=BIO_push(s->bbio,s->wbio);
d02b48c6
RE
188
189 break;
190
191 case SSL3_ST_CR_SRVR_HELLO_A:
192 case SSL3_ST_CR_SRVR_HELLO_B:
193 ret=ssl3_get_server_hello(s);
194 if (ret <= 0) goto end;
195 if (s->hit)
196 s->state=SSL3_ST_CR_FINISHED_A;
197 else
198 s->state=SSL3_ST_CR_CERT_A;
199 s->init_num=0;
200 break;
201
202 case SSL3_ST_CR_CERT_A:
203 case SSL3_ST_CR_CERT_B:
204 /* Check if it is anon DH */
205 if (!(s->s3->tmp.new_cipher->algorithms & SSL_aNULL))
206 {
207 ret=ssl3_get_server_certificate(s);
208 if (ret <= 0) goto end;
209 }
210 else
211 skip=1;
212 s->state=SSL3_ST_CR_KEY_EXCH_A;
213 s->init_num=0;
214 break;
215
216 case SSL3_ST_CR_KEY_EXCH_A:
217 case SSL3_ST_CR_KEY_EXCH_B:
218 ret=ssl3_get_key_exchange(s);
219 if (ret <= 0) goto end;
220 s->state=SSL3_ST_CR_CERT_REQ_A;
221 s->init_num=0;
222
223 /* at this point we check that we have the
224 * required stuff from the server */
225 if (!ssl3_check_cert_and_algorithm(s))
226 {
227 ret= -1;
228 goto end;
229 }
230 break;
231
232 case SSL3_ST_CR_CERT_REQ_A:
233 case SSL3_ST_CR_CERT_REQ_B:
234 ret=ssl3_get_certificate_request(s);
235 if (ret <= 0) goto end;
236 s->state=SSL3_ST_CR_SRVR_DONE_A;
237 s->init_num=0;
238 break;
239
240 case SSL3_ST_CR_SRVR_DONE_A:
241 case SSL3_ST_CR_SRVR_DONE_B:
242 ret=ssl3_get_server_done(s);
243 if (ret <= 0) goto end;
244 if (s->s3->tmp.cert_req)
245 s->state=SSL3_ST_CW_CERT_A;
246 else
247 s->state=SSL3_ST_CW_KEY_EXCH_A;
248 s->init_num=0;
249
250 break;
251
252 case SSL3_ST_CW_CERT_A:
253 case SSL3_ST_CW_CERT_B:
254 case SSL3_ST_CW_CERT_C:
255 ret=ssl3_send_client_certificate(s);
256 if (ret <= 0) goto end;
257 s->state=SSL3_ST_CW_KEY_EXCH_A;
258 s->init_num=0;
259 break;
260
261 case SSL3_ST_CW_KEY_EXCH_A:
262 case SSL3_ST_CW_KEY_EXCH_B:
263 ret=ssl3_send_client_key_exchange(s);
264 if (ret <= 0) goto end;
265 l=s->s3->tmp.new_cipher->algorithms;
266 /* EAY EAY EAY need to check for DH fix cert
267 * sent back */
58964a49
RE
268 /* For TLS, cert_req is set to 2, so a cert chain
269 * of nothing is sent, but no verify packet is sent */
270 if (s->s3->tmp.cert_req == 1)
d02b48c6
RE
271 {
272 s->state=SSL3_ST_CW_CERT_VRFY_A;
273 }
274 else
275 {
276 s->state=SSL3_ST_CW_CHANGE_A;
277 s->s3->change_cipher_spec=0;
278 }
279
280 s->init_num=0;
281 break;
282
283 case SSL3_ST_CW_CERT_VRFY_A:
284 case SSL3_ST_CW_CERT_VRFY_B:
285 ret=ssl3_send_client_verify(s);
286 if (ret <= 0) goto end;
287 s->state=SSL3_ST_CW_CHANGE_A;
288 s->init_num=0;
289 s->s3->change_cipher_spec=0;
290 break;
291
292 case SSL3_ST_CW_CHANGE_A:
293 case SSL3_ST_CW_CHANGE_B:
294 ret=ssl3_send_change_cipher_spec(s,
295 SSL3_ST_CW_CHANGE_A,SSL3_ST_CW_CHANGE_B);
296 if (ret <= 0) goto end;
297 s->state=SSL3_ST_CW_FINISHED_A;
298 s->init_num=0;
299
300 s->session->cipher=s->s3->tmp.new_cipher;
413c4f45
MC
301 if (s->s3->tmp.new_compression == NULL)
302 s->session->compress_meth=0;
303 else
304 s->session->compress_meth=
305 s->s3->tmp.new_compression->id;
58964a49 306 if (!s->method->ssl3_enc->setup_key_block(s))
d02b48c6
RE
307 {
308 ret= -1;
309 goto end;
310 }
311
58964a49 312 if (!s->method->ssl3_enc->change_cipher_state(s,
d02b48c6
RE
313 SSL3_CHANGE_CIPHER_CLIENT_WRITE))
314 {
315 ret= -1;
316 goto end;
317 }
318
319 break;
320
321 case SSL3_ST_CW_FINISHED_A:
322 case SSL3_ST_CW_FINISHED_B:
323 ret=ssl3_send_finished(s,
324 SSL3_ST_CW_FINISHED_A,SSL3_ST_CW_FINISHED_B,
58964a49
RE
325 s->method->ssl3_enc->client_finished,
326 s->method->ssl3_enc->client_finished_len);
d02b48c6
RE
327 if (ret <= 0) goto end;
328 s->state=SSL3_ST_CW_FLUSH;
329
330 /* clear flags */
331 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
332 if (s->hit)
333 {
334 s->s3->tmp.next_state=SSL_ST_OK;
335 if (s->s3->flags & SSL3_FLAGS_DELAY_CLIENT_FINISHED)
336 {
337 s->state=SSL_ST_OK;
338 s->s3->flags|=SSL3_FLAGS_POP_BUFFER;
339 s->s3->delay_buf_pop_ret=0;
340 }
341 }
342 else
343 {
344 s->s3->tmp.next_state=SSL3_ST_CR_FINISHED_A;
345 }
346 s->init_num=0;
347 break;
348
349 case SSL3_ST_CR_FINISHED_A:
350 case SSL3_ST_CR_FINISHED_B:
351
352 ret=ssl3_get_finished(s,SSL3_ST_CR_FINISHED_A,
58964a49 353 SSL3_ST_CR_FINISHED_B);
d02b48c6
RE
354 if (ret <= 0) goto end;
355
356 if (s->hit)
357 s->state=SSL3_ST_CW_CHANGE_A;
358 else
359 s->state=SSL_ST_OK;
360 s->init_num=0;
361 break;
362
363 case SSL3_ST_CW_FLUSH:
364 /* number of bytes to be flushed */
365 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
366 if (num1 > 0)
367 {
368 s->rwstate=SSL_WRITING;
369 num1=BIO_flush(s->wbio);
370 if (num1 <= 0) { ret= -1; goto end; }
371 s->rwstate=SSL_NOTHING;
372 }
373
374 s->state=s->s3->tmp.next_state;
375 break;
376
377 case SSL_ST_OK:
378 /* clean a few things up */
379 ssl3_cleanup_key_block(s);
380
413c4f45 381 if (s->init_buf != NULL)
d02b48c6 382 {
413c4f45
MC
383 BUF_MEM_free(s->init_buf);
384 s->init_buf=NULL;
d02b48c6 385 }
413c4f45
MC
386
387 /* If we are not 'joining' the last two packets,
388 * remove the buffering now */
389 if (!(s->s3->flags & SSL3_FLAGS_POP_BUFFER))
390 ssl_free_wbio_buffer(s);
391 /* else do it later in ssl3_write */
d02b48c6
RE
392
393 s->init_num=0;
394 s->new_session=0;
395
396 ssl_update_cache(s,SSL_SESS_CACHE_CLIENT);
413c4f45 397 if (s->hit) s->ctx->stats.sess_hit++;
d02b48c6
RE
398
399 ret=1;
400 /* s->server=0; */
401 s->handshake_func=ssl3_connect;
413c4f45 402 s->ctx->stats.sess_connect_good++;
d02b48c6
RE
403
404 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
405
406 goto end;
dfeab068 407 /* break; */
d02b48c6
RE
408
409 default:
410 SSLerr(SSL_F_SSL3_CONNECT,SSL_R_UNKNOWN_STATE);
411 ret= -1;
412 goto end;
413 /* break; */
414 }
415
416 /* did we do anything */
417 if (!s->s3->tmp.reuse_message && !skip)
418 {
58964a49
RE
419 if (s->debug)
420 {
421 if ((ret=BIO_flush(s->wbio)) <= 0)
422 goto end;
423 }
d02b48c6
RE
424
425 if ((cb != NULL) && (s->state != state))
426 {
427 new_state=s->state;
428 s->state=state;
429 cb(s,SSL_CB_CONNECT_LOOP,1);
430 s->state=new_state;
431 }
432 }
433 skip=0;
434 }
435end:
436 if (cb != NULL)
437 cb(s,SSL_CB_CONNECT_EXIT,ret);
438 s->in_handshake--;
439 return(ret);
440 }
441
442
6b691a5c 443static int ssl3_client_hello(SSL *s)
d02b48c6
RE
444 {
445 unsigned char *buf;
446 unsigned char *p,*d;
413c4f45 447 int i,j;
d02b48c6 448 unsigned long Time,l;
413c4f45 449 SSL_COMP *comp;
d02b48c6
RE
450
451 buf=(unsigned char *)s->init_buf->data;
452 if (s->state == SSL3_ST_CW_CLNT_HELLO_A)
453 {
454 if ((s->session == NULL) ||
dfeab068
RE
455 (s->session->ssl_version != s->version) ||
456 (s->session->not_resumable))
d02b48c6
RE
457 {
458 if (!ssl_get_new_session(s,0))
459 goto err;
460 }
461 /* else use the pre-loaded session */
462
463 p=s->s3->client_random;
464 Time=time(NULL); /* Time */
465 l2n(Time,p);
dfeab068 466 RAND_bytes(p,SSL3_RANDOM_SIZE-sizeof(Time));
d02b48c6
RE
467
468 /* Do the message type and length last */
469 d=p= &(buf[4]);
470
58964a49
RE
471 *(p++)=s->version>>8;
472 *(p++)=s->version&0xff;
413c4f45 473 s->client_version=s->version;
d02b48c6
RE
474
475 /* Random stuff */
476 memcpy(p,s->s3->client_random,SSL3_RANDOM_SIZE);
477 p+=SSL3_RANDOM_SIZE;
478
479 /* Session ID */
480 if (s->new_session)
481 i=0;
482 else
483 i=s->session->session_id_length;
484 *(p++)=i;
485 if (i != 0)
486 {
487 memcpy(p,s->session->session_id,i);
488 p+=i;
489 }
490
491 /* Ciphers supported */
492 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]));
493 if (i == 0)
494 {
495 SSLerr(SSL_F_SSL3_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
496 goto err;
497 }
498 s2n(i,p);
499 p+=i;
500
dfeab068 501 /* COMPRESSION */
413c4f45
MC
502 if (s->ctx->comp_methods == NULL)
503 j=0;
504 else
f73e07cf 505 j=sk_SSL_COMP_num(s->ctx->comp_methods);
413c4f45
MC
506 *(p++)=1+j;
507 for (i=0; i<j; i++)
508 {
f73e07cf 509 comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
413c4f45
MC
510 *(p++)=comp->id;
511 }
512 *(p++)=0; /* Add the NULL method */
d02b48c6
RE
513
514 l=(p-d);
515 d=buf;
516 *(d++)=SSL3_MT_CLIENT_HELLO;
517 l2n3(l,d);
518
519 s->state=SSL3_ST_CW_CLNT_HELLO_B;
520 /* number of bytes to write */
521 s->init_num=p-buf;
522 s->init_off=0;
523 }
524
525 /* SSL3_ST_CW_CLNT_HELLO_B */
526 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
527err:
528 return(-1);
529 }
530
6b691a5c 531static int ssl3_get_server_hello(SSL *s)
d02b48c6 532 {
f73e07cf 533 STACK_OF(SSL_CIPHER) *sk;
d02b48c6
RE
534 SSL_CIPHER *c;
535 unsigned char *p,*d;
536 int i,al,ok;
537 unsigned int j;
538 long n;
413c4f45 539 SSL_COMP *comp;
d02b48c6
RE
540
541 n=ssl3_get_message(s,
542 SSL3_ST_CR_SRVR_HELLO_A,
543 SSL3_ST_CR_SRVR_HELLO_B,
544 SSL3_MT_SERVER_HELLO,
545 300, /* ?? */
546 &ok);
547
548 if (!ok) return((int)n);
549 d=p=(unsigned char *)s->init_buf->data;
550
58964a49 551 if ((p[0] != (s->version>>8)) || (p[1] != (s->version&0xff)))
d02b48c6
RE
552 {
553 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_SSL_VERSION);
58964a49
RE
554 s->version=(s->version&0xff00)|p[1];
555 al=SSL_AD_PROTOCOL_VERSION;
556 goto f_err;
d02b48c6
RE
557 }
558 p+=2;
559
560 /* load the server hello data */
561 /* load the server random */
562 memcpy(s->s3->server_random,p,SSL3_RANDOM_SIZE);
563 p+=SSL3_RANDOM_SIZE;
564
565 /* get the session-id */
566 j= *(p++);
567
568 if ((j != 0) && (j != SSL3_SESSION_ID_SIZE))
569 {
570 /* SSLref returns 16 :-( */
571 if (j < SSL2_SSL_SESSION_ID_LENGTH)
572 {
58964a49 573 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
574 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_SSL3_SESSION_ID_TOO_SHORT);
575 goto f_err;
576 }
577 }
b4cadc6e
BL
578 if (j != 0 && j == s->session->session_id_length
579 && memcmp(p,s->session->session_id,j) == 0)
580 {
581 if(s->sid_ctx_length != s->session->sid_ctx_length
582 || memcmp(s->session->sid_ctx,s->sid_ctx,s->sid_ctx_length))
583 {
584 al=SSL_AD_ILLEGAL_PARAMETER;
585 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT);
586 goto f_err;
587 }
588 s->hit=1;
589 }
58964a49 590 else /* a miss or crap from the other end */
d02b48c6 591 {
58964a49
RE
592 /* If we were trying for session-id reuse, make a new
593 * SSL_SESSION so we don't stuff up other people */
d02b48c6 594 s->hit=0;
58964a49
RE
595 if (s->session->session_id_length > 0)
596 {
597 if (!ssl_get_new_session(s,0))
598 {
599 al=SSL_AD_INTERNAL_ERROR;
600 goto f_err;
601 }
602 }
603 s->session->session_id_length=j;
604 memcpy(s->session->session_id,p,j); /* j could be 0 */
d02b48c6
RE
605 }
606 p+=j;
607 c=ssl_get_cipher_by_char(s,p);
608 if (c == NULL)
609 {
610 /* unknown cipher */
58964a49 611 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
612 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNKNOWN_CIPHER_RETURNED);
613 goto f_err;
614 }
615 p+=ssl_put_cipher_by_char(s,NULL,NULL);
616
617 sk=ssl_get_ciphers_by_id(s);
f73e07cf 618 i=sk_SSL_CIPHER_find(sk,c);
d02b48c6
RE
619 if (i < 0)
620 {
621 /* we did not say we would use this cipher */
58964a49 622 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
623 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_WRONG_CIPHER_RETURNED);
624 goto f_err;
625 }
626
627 if (s->hit && (s->session->cipher != c))
628 {
58964a49 629 if (!(s->options &
d02b48c6
RE
630 SSL_OP_NETSCAPE_REUSE_CIPHER_CHANGE_BUG))
631 {
58964a49 632 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
633 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_OLD_SESSION_CIPHER_NOT_RETURNED);
634 goto f_err;
635 }
636 }
637 s->s3->tmp.new_cipher=c;
638
639 /* lets get the compression algorithm */
dfeab068 640 /* COMPRESSION */
d02b48c6 641 j= *(p++);
413c4f45
MC
642 if (j == 0)
643 comp=NULL;
644 else
645 comp=ssl3_comp_find(s->ctx->comp_methods,j);
646
647 if ((j != 0) && (comp == NULL))
d02b48c6 648 {
58964a49 649 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
650 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_COMPRESSION_ALGORITHM);
651 goto f_err;
652 }
413c4f45
MC
653 else
654 {
655 s->s3->tmp.new_compression=comp;
656 }
d02b48c6
RE
657
658 if (p != (d+n))
659 {
660 /* wrong packet length */
58964a49 661 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
662 SSLerr(SSL_F_SSL3_GET_SERVER_HELLO,SSL_R_BAD_PACKET_LENGTH);
663 goto err;
664 }
665
666 return(1);
667f_err:
668 ssl3_send_alert(s,SSL3_AL_FATAL,al);
669err:
670 return(-1);
671 }
672
6b691a5c 673static int ssl3_get_server_certificate(SSL *s)
d02b48c6
RE
674 {
675 int al,i,ok,ret= -1;
676 unsigned long n,nc,llen,l;
677 X509 *x=NULL;
678 unsigned char *p,*d,*q;
f73e07cf 679 STACK_OF(X509) *sk=NULL;
d02b48c6
RE
680 CERT *c;
681 EVP_PKEY *pkey=NULL;
682
683 n=ssl3_get_message(s,
684 SSL3_ST_CR_CERT_A,
685 SSL3_ST_CR_CERT_B,
686 -1,
687#if defined(MSDOS) && !defined(WIN32)
688 1024*30, /* 30k max cert list :-) */
689#else
690 1024*100, /* 100k max cert list :-) */
691#endif
692 &ok);
693
694 if (!ok) return((int)n);
695
696 if (s->s3->tmp.message_type == SSL3_MT_SERVER_KEY_EXCHANGE)
697 {
698 s->s3->tmp.reuse_message=1;
699 return(1);
700 }
701
702 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE)
703 {
58964a49 704 al=SSL_AD_UNEXPECTED_MESSAGE;
d02b48c6
RE
705 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_BAD_MESSAGE_TYPE);
706 goto f_err;
707 }
708 d=p=(unsigned char *)s->init_buf->data;
709
f73e07cf 710 if ((sk=sk_X509_new_null()) == NULL)
d02b48c6
RE
711 {
712 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
713 goto err;
714 }
715
716 n2l3(p,llen);
717 if (llen+3 != n)
718 {
58964a49 719 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
720 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_LENGTH_MISMATCH);
721 goto f_err;
722 }
723 for (nc=0; nc<llen; )
724 {
725 n2l3(p,l);
726 if ((l+nc+3) > llen)
727 {
58964a49 728 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
729 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
730 goto f_err;
731 }
732
733 q=p;
734 x=d2i_X509(NULL,&q,l);
735 if (x == NULL)
736 {
58964a49 737 al=SSL_AD_BAD_CERTIFICATE;
d02b48c6
RE
738 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_ASN1_LIB);
739 goto f_err;
740 }
741 if (q != (p+l))
742 {
58964a49 743 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
744 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERT_LENGTH_MISMATCH);
745 goto f_err;
746 }
f73e07cf 747 if (!sk_X509_push(sk,x))
d02b48c6
RE
748 {
749 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,ERR_R_MALLOC_FAILURE);
750 goto err;
751 }
752 x=NULL;
753 nc+=l+3;
754 p=q;
755 }
756
757 i=ssl_verify_cert_chain(s,sk);
758 if ((s->verify_mode != SSL_VERIFY_NONE) && (!i))
759 {
760 al=ssl_verify_alarm_type(s->verify_result);
761 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_CERTIFICATE_VERIFY_FAILED);
762 goto f_err;
763 }
764
765 c=ssl_cert_new();
766 if (c == NULL) goto err;
767
768 if (s->session->cert) ssl_cert_free(s->session->cert);
769 s->session->cert=c;
770
771 c->cert_chain=sk;
f73e07cf 772 x=sk_X509_value(sk,0);
d02b48c6
RE
773 sk=NULL;
774
775 pkey=X509_get_pubkey(x);
776
dfeab068 777 if ((pkey == NULL) || EVP_PKEY_missing_parameters(pkey))
d02b48c6
RE
778 {
779 x=NULL;
780 al=SSL3_AL_FATAL;
781 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNABLE_TO_FIND_PUBLIC_KEY_PARAMETERS);
782 goto f_err;
783 }
784
785 i=ssl_cert_type(x,pkey);
786 if (i < 0)
787 {
788 x=NULL;
789 al=SSL3_AL_FATAL;
790 SSLerr(SSL_F_SSL3_GET_SERVER_CERTIFICATE,SSL_R_UNKNOWN_CERTIFICATE_TYPE);
791 goto f_err;
792 }
793
794 c->cert_type=i;
58964a49 795 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
d02b48c6
RE
796 if (c->pkeys[i].x509 != NULL)
797 X509_free(c->pkeys[i].x509);
798 c->pkeys[i].x509=x;
799 c->key= &(c->pkeys[i]);
800
801 if ((s->session != NULL) && (s->session->peer != NULL))
802 X509_free(s->session->peer);
58964a49 803 CRYPTO_add(&x->references,1,CRYPTO_LOCK_X509);
d02b48c6
RE
804 s->session->peer=x;
805
806 x=NULL;
807 ret=1;
808
809 if (0)
810 {
811f_err:
812 ssl3_send_alert(s,SSL3_AL_FATAL,al);
813 }
814err:
a8236c8c
DSH
815 EVP_PKEY_free(pkey);
816 X509_free(x);
f73e07cf 817 sk_X509_pop_free(sk,X509_free);
d02b48c6
RE
818 return(ret);
819 }
820
6b691a5c 821static int ssl3_get_key_exchange(SSL *s)
d02b48c6
RE
822 {
823#ifndef NO_RSA
824 unsigned char *q,md_buf[EVP_MAX_MD_SIZE*2];
825#endif
826 EVP_MD_CTX md_ctx;
827 unsigned char *param,*p;
828 int al,i,j,param_len,ok;
829 long n,alg;
830 EVP_PKEY *pkey=NULL;
831 RSA *rsa=NULL;
58964a49 832#ifndef NO_DH
d02b48c6 833 DH *dh=NULL;
58964a49 834#endif
d02b48c6
RE
835
836 n=ssl3_get_message(s,
837 SSL3_ST_CR_KEY_EXCH_A,
838 SSL3_ST_CR_KEY_EXCH_B,
839 -1,
840 1024*8, /* ?? */
841 &ok);
842
843 if (!ok) return((int)n);
844
845 if (s->s3->tmp.message_type != SSL3_MT_SERVER_KEY_EXCHANGE)
846 {
847 s->s3->tmp.reuse_message=1;
848 return(1);
849 }
850
851 param=p=(unsigned char *)s->init_buf->data;
852
853 if (s->session->cert != NULL)
854 {
855#ifndef NO_RSA
856 if (s->session->cert->rsa_tmp != NULL)
857 {
858 RSA_free(s->session->cert->rsa_tmp);
859 s->session->cert->rsa_tmp=NULL;
860 }
861#endif
862#ifndef NO_DH
863 if (s->session->cert->dh_tmp)
864 {
865 DH_free(s->session->cert->dh_tmp);
866 s->session->cert->dh_tmp=NULL;
867 }
868#endif
869 }
870 else
871 {
872 s->session->cert=ssl_cert_new();
873 }
874
875 param_len=0;
876 alg=s->s3->tmp.new_cipher->algorithms;
877
878#ifndef NO_RSA
879 if (alg & SSL_kRSA)
880 {
881 if ((rsa=RSA_new()) == NULL)
882 {
883 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_MALLOC_FAILURE);
884 goto err;
885 }
886 n2s(p,i);
887 param_len=i+2;
888 if (param_len > n)
889 {
58964a49 890 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
891 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_MODULUS_LENGTH);
892 goto f_err;
893 }
894 if (!(rsa->n=BN_bin2bn(p,i,rsa->n)))
895 {
896 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
897 goto err;
898 }
899 p+=i;
900
901 n2s(p,i);
902 param_len+=i+2;
903 if (param_len > n)
904 {
58964a49 905 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
906 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_E_LENGTH);
907 goto f_err;
908 }
909 if (!(rsa->e=BN_bin2bn(p,i,rsa->e)))
910 {
911 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
912 goto err;
913 }
914 p+=i;
915 n-=param_len;
916
917/* s->session->cert->rsa_tmp=rsa;*/
918 /* this should be because we are using an export cipher */
919 if (alg & SSL_aRSA)
920 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
921 else
922 {
923 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
924 goto err;
925 }
926 s->session->cert->rsa_tmp=rsa;
927 }
928 else
929#endif
930#ifndef NO_DH
931 if (alg & SSL_kEDH)
932 {
933 if ((dh=DH_new()) == NULL)
934 {
935 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_DH_LIB);
936 goto err;
937 }
938 n2s(p,i);
939 param_len=i+2;
940 if (param_len > n)
941 {
58964a49 942 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
943 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_P_LENGTH);
944 goto f_err;
945 }
946 if (!(dh->p=BN_bin2bn(p,i,NULL)))
947 {
948 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
949 goto err;
950 }
951 p+=i;
952
953 n2s(p,i);
954 param_len+=i+2;
955 if (param_len > n)
956 {
58964a49 957 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
958 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_G_LENGTH);
959 goto f_err;
960 }
961 if (!(dh->g=BN_bin2bn(p,i,NULL)))
962 {
963 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
964 goto err;
965 }
966 p+=i;
967
968 n2s(p,i);
969 param_len+=i+2;
970 if (param_len > n)
971 {
58964a49 972 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
973 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_DH_PUB_KEY_LENGTH);
974 goto f_err;
975 }
976 if (!(dh->pub_key=BN_bin2bn(p,i,NULL)))
977 {
978 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,ERR_R_BN_LIB);
979 goto err;
980 }
981 p+=i;
982 n-=param_len;
983
984#ifndef NO_RSA
985 if (alg & SSL_aRSA)
986 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
987 else
988#endif
989#ifndef NO_DSA
990 if (alg & SSL_aDSS)
991 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_DSA_SIGN].x509);
992#endif
993 /* else anonymous DH, so no certificate or pkey. */
994
995 s->session->cert->dh_tmp=dh;
413c4f45 996 dh=NULL;
d02b48c6
RE
997 }
998 else if ((alg & SSL_kDHr) || (alg & SSL_kDHd))
999 {
58964a49 1000 al=SSL_AD_ILLEGAL_PARAMETER;
d02b48c6
RE
1001 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1002 goto f_err;
1003 }
1004#endif
dfeab068
RE
1005 if (alg & SSL_aFZA)
1006 {
1007 al=SSL_AD_HANDSHAKE_FAILURE;
1008 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_TRIED_TO_USE_UNSUPPORTED_CIPHER);
1009 goto f_err;
1010 }
1011
d02b48c6
RE
1012
1013 /* p points to the next byte, there are 'n' bytes left */
1014
1015
1016 /* if it was signed, check the signature */
1017 if (pkey != NULL)
1018 {
1019 n2s(p,i);
1020 n-=2;
1021 j=EVP_PKEY_size(pkey);
1022
1023 if ((i != n) || (n > j) || (n <= 0))
1024 {
1025 /* wrong packet length */
58964a49 1026 al=SSL_AD_DECODE_ERROR;
d02b48c6 1027 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_WRONG_SIGNATURE_LENGTH);
dfeab068 1028 goto f_err;
d02b48c6
RE
1029 }
1030
1031#ifndef NO_RSA
1032 if (pkey->type == EVP_PKEY_RSA)
1033 {
1034 int num;
1035
1036 j=0;
1037 q=md_buf;
1038 for (num=2; num > 0; num--)
1039 {
58964a49
RE
1040 EVP_DigestInit(&md_ctx,(num == 2)
1041 ?s->ctx->md5:s->ctx->sha1);
d02b48c6
RE
1042 EVP_DigestUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1043 EVP_DigestUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1044 EVP_DigestUpdate(&md_ctx,param,param_len);
1045 EVP_DigestFinal(&md_ctx,q,(unsigned int *)&i);
1046 q+=i;
1047 j+=i;
1048 }
1049 i=RSA_public_decrypt((int)n,p,p,pkey->pkey.rsa,
1050 RSA_PKCS1_PADDING);
1051 if (i <= 0)
1052 {
58964a49 1053 al=SSL_AD_DECRYPT_ERROR;
d02b48c6
RE
1054 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_RSA_DECRYPT);
1055 goto f_err;
1056 }
1057 if ((j != i) || (memcmp(p,md_buf,i) != 0))
1058 {
1059 /* bad signature */
58964a49 1060 al=SSL_AD_DECRYPT_ERROR;
d02b48c6
RE
1061 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1062 goto f_err;
1063 }
1064 }
1065 else
1066#endif
1067#ifndef NO_DSA
1068 if (pkey->type == EVP_PKEY_DSA)
1069 {
1070 /* lets do DSS */
1071 EVP_VerifyInit(&md_ctx,EVP_dss1());
1072 EVP_VerifyUpdate(&md_ctx,&(s->s3->client_random[0]),SSL3_RANDOM_SIZE);
1073 EVP_VerifyUpdate(&md_ctx,&(s->s3->server_random[0]),SSL3_RANDOM_SIZE);
1074 EVP_VerifyUpdate(&md_ctx,param,param_len);
1075 if (!EVP_VerifyFinal(&md_ctx,p,(int)n,pkey))
1076 {
1077 /* bad signature */
58964a49 1078 al=SSL_AD_DECRYPT_ERROR;
d02b48c6
RE
1079 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_BAD_SIGNATURE);
1080 goto f_err;
1081 }
1082 }
1083 else
1084#endif
1085 {
1086 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1087 goto err;
1088 }
1089 }
1090 else
1091 {
1092 /* still data left over */
1093 if (!(alg & SSL_aNULL))
1094 {
1095 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1096 goto err;
1097 }
1098 if (n != 0)
1099 {
58964a49 1100 al=SSL_AD_DECODE_ERROR;
d02b48c6
RE
1101 SSLerr(SSL_F_SSL3_GET_KEY_EXCHANGE,SSL_R_EXTRA_DATA_IN_MESSAGE);
1102 goto f_err;
1103 }
1104 }
a8236c8c 1105 EVP_PKEY_free(pkey);
d02b48c6
RE
1106 return(1);
1107f_err:
1108 ssl3_send_alert(s,SSL3_AL_FATAL,al);
1109err:
a8236c8c 1110 EVP_PKEY_free(pkey);
d02b48c6
RE
1111 return(-1);
1112 }
1113
6b691a5c 1114static int ssl3_get_certificate_request(SSL *s)
d02b48c6
RE
1115 {
1116 int ok,ret=0;
58964a49
RE
1117 unsigned long n,nc,l;
1118 unsigned int llen,ctype_num,i;
d02b48c6
RE
1119 X509_NAME *xn=NULL;
1120 unsigned char *p,*d,*q;
f73e07cf 1121 STACK_OF(X509_NAME) *ca_sk=NULL;
d02b48c6
RE
1122
1123 n=ssl3_get_message(s,
1124 SSL3_ST_CR_CERT_REQ_A,
1125 SSL3_ST_CR_CERT_REQ_B,
1126 -1,
1127#if defined(MSDOS) && !defined(WIN32)
1128 1024*30, /* 30k max cert list :-) */
1129#else
1130 1024*100, /* 100k max cert list :-) */
1131#endif
1132 &ok);
1133
1134 if (!ok) return((int)n);
1135
1136 s->s3->tmp.cert_req=0;
1137
1138 if (s->s3->tmp.message_type == SSL3_MT_SERVER_DONE)
1139 {
1140 s->s3->tmp.reuse_message=1;
1141 return(1);
1142 }
1143
1144 if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE_REQUEST)
1145 {
58964a49 1146 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
d02b48c6
RE
1147 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_WRONG_MESSAGE_TYPE);
1148 goto err;
1149 }
1150
58964a49
RE
1151 /* TLS does not like anon-DH with client cert */
1152 if (s->version > SSL3_VERSION)
1153 {
1154 l=s->s3->tmp.new_cipher->algorithms;
1155 if (l & SSL_aNULL)
1156 {
1157 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_UNEXPECTED_MESSAGE);
1158 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_TLS_CLIENT_CERT_REQ_WITH_ANON_CIPHER);
1159 goto err;
1160 }
1161 }
1162
d02b48c6
RE
1163 d=p=(unsigned char *)s->init_buf->data;
1164
f73e07cf 1165 if ((ca_sk=sk_X509_NAME_new(ca_dn_cmp)) == NULL)
d02b48c6
RE
1166 {
1167 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1168 goto err;
1169 }
1170
1171 /* get the certificate types */
1172 ctype_num= *(p++);
1173 if (ctype_num > SSL3_CT_NUMBER)
1174 ctype_num=SSL3_CT_NUMBER;
1175 for (i=0; i<ctype_num; i++)
1176 s->s3->tmp.ctype[i]= p[i];
1177 p+=ctype_num;
1178
1179 /* get the CA RDNs */
1180 n2s(p,llen);
dfeab068
RE
1181#if 0
1182{
1183FILE *out;
1184out=fopen("/tmp/vsign.der","w");
1185fwrite(p,1,llen,out);
1186fclose(out);
1187}
1188#endif
1189
d02b48c6
RE
1190 if ((llen+ctype_num+2+1) != n)
1191 {
58964a49 1192 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
d02b48c6
RE
1193 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_LENGTH_MISMATCH);
1194 goto err;
1195 }
1196
1197 for (nc=0; nc<llen; )
1198 {
1199 n2s(p,l);
1200 if ((l+nc+2) > llen)
1201 {
58964a49 1202 if ((s->options & SSL_OP_NETSCAPE_CA_DN_BUG))
d02b48c6 1203 goto cont; /* netscape bugs */
58964a49 1204 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
d02b48c6
RE
1205 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_TOO_LONG);
1206 goto err;
1207 }
1208
1209 q=p;
1210
1211 if ((xn=d2i_X509_NAME(NULL,&q,l)) == NULL)
1212 {
1213 /* If netscape tollerance is on, ignore errors */
58964a49 1214 if (s->options & SSL_OP_NETSCAPE_CA_DN_BUG)
d02b48c6
RE
1215 goto cont;
1216 else
1217 {
58964a49 1218 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
d02b48c6
RE
1219 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_ASN1_LIB);
1220 goto err;
1221 }
1222 }
1223
1224 if (q != (p+l))
1225 {
58964a49 1226 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
d02b48c6
RE
1227 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,SSL_R_CA_DN_LENGTH_MISMATCH);
1228 goto err;
1229 }
f73e07cf 1230 if (!sk_X509_NAME_push(ca_sk,xn))
d02b48c6
RE
1231 {
1232 SSLerr(SSL_F_SSL3_GET_CERTIFICATE_REQUEST,ERR_R_MALLOC_FAILURE);
1233 goto err;
1234 }
1235
1236 p+=l;
1237 nc+=l+2;
1238 }
1239
1240 if (0)
1241 {
1242cont:
1243 ERR_clear_error();
1244 }
1245
1246 /* we should setup a certficate to return.... */
1247 s->s3->tmp.cert_req=1;
1248 s->s3->tmp.ctype_num=ctype_num;
1249 if (s->s3->tmp.ca_names != NULL)
f73e07cf 1250 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
d02b48c6
RE
1251 s->s3->tmp.ca_names=ca_sk;
1252 ca_sk=NULL;
1253
1254 ret=1;
1255err:
f73e07cf 1256 if (ca_sk != NULL) sk_X509_NAME_pop_free(ca_sk,X509_NAME_free);
d02b48c6
RE
1257 return(ret);
1258 }
1259
6b691a5c 1260static int ca_dn_cmp(X509_NAME **a, X509_NAME **b)
d02b48c6
RE
1261 {
1262 return(X509_NAME_cmp(*a,*b));
1263 }
1264
6b691a5c 1265static int ssl3_get_server_done(SSL *s)
d02b48c6
RE
1266 {
1267 int ok,ret=0;
1268 long n;
1269
1270 n=ssl3_get_message(s,
1271 SSL3_ST_CR_SRVR_DONE_A,
1272 SSL3_ST_CR_SRVR_DONE_B,
1273 SSL3_MT_SERVER_DONE,
1274 30, /* should be very small, like 0 :-) */
1275 &ok);
1276
1277 if (!ok) return((int)n);
1278 if (n > 0)
1279 {
1280 /* should contain no data */
58964a49 1281 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_DECODE_ERROR);
d02b48c6
RE
1282 SSLerr(SSL_F_SSL3_GET_SERVER_DONE,SSL_R_LENGTH_MISMATCH);
1283 }
1284 ret=1;
1285 return(ret);
1286 }
1287
6b691a5c 1288static int ssl3_send_client_key_exchange(SSL *s)
d02b48c6 1289 {
58964a49 1290 unsigned char *p,*q,*d;
d02b48c6
RE
1291 int n;
1292 unsigned long l;
1293 EVP_PKEY *pkey=NULL;
1294
1295 if (s->state == SSL3_ST_CW_KEY_EXCH_A)
1296 {
1297 d=(unsigned char *)s->init_buf->data;
1298 p= &(d[4]);
1299
1300 l=s->s3->tmp.new_cipher->algorithms;
1301
1302#ifndef NO_RSA
1303 if (l & SSL_kRSA)
1304 {
1305 RSA *rsa;
dfeab068 1306 unsigned char tmp_buf[SSL_MAX_MASTER_KEY_LENGTH];
d02b48c6
RE
1307
1308 if (s->session->cert->rsa_tmp != NULL)
1309 rsa=s->session->cert->rsa_tmp;
1310 else
1311 {
1312 pkey=X509_get_pubkey(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509);
1313 if ((pkey == NULL) ||
1314 (pkey->type != EVP_PKEY_RSA) ||
1315 (pkey->pkey.rsa == NULL))
1316 {
1317 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1318 goto err;
1319 }
1320 rsa=pkey->pkey.rsa;
1321 }
1322
413c4f45
MC
1323 tmp_buf[0]=s->client_version>>8;
1324 tmp_buf[1]=s->client_version&0xff;
d02b48c6
RE
1325 RAND_bytes(&(tmp_buf[2]),SSL_MAX_MASTER_KEY_LENGTH-2);
1326
1327 s->session->master_key_length=SSL_MAX_MASTER_KEY_LENGTH;
1328
58964a49
RE
1329 q=p;
1330 /* Fix buf for TLS and beyond */
1331 if (s->version > SSL3_VERSION)
1332 p+=2;
1333 n=RSA_public_encrypt(SSL_MAX_MASTER_KEY_LENGTH,
1334 tmp_buf,p,rsa,RSA_PKCS1_PADDING);
dfeab068
RE
1335#ifdef PKCS1_CHECK
1336 if (s->options & SSL_OP_PKCS1_CHECK_1) p[1]++;
1337 if (s->options & SSL_OP_PKCS1_CHECK_2) tmp_buf[0]=0x70;
1338#endif
d02b48c6
RE
1339 if (n <= 0)
1340 {
1341 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_BAD_RSA_ENCRYPT);
1342 goto err;
1343 }
1344
58964a49
RE
1345 /* Fix buf for TLS and beyond */
1346 if (s->version > SSL3_VERSION)
1347 {
1348 s2n(n,q);
1349 n+=2;
1350 }
1351
d02b48c6 1352 s->session->master_key_length=
58964a49 1353 s->method->ssl3_enc->generate_master_secret(s,
d02b48c6 1354 s->session->master_key,
dfeab068
RE
1355 tmp_buf,SSL_MAX_MASTER_KEY_LENGTH);
1356 memset(tmp_buf,0,SSL_MAX_MASTER_KEY_LENGTH);
d02b48c6
RE
1357 }
1358 else
1359#endif
1360#ifndef NO_DH
1361 if (l & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
1362 {
1363 DH *dh_srvr,*dh_clnt;
1364
1365 if (s->session->cert->dh_tmp != NULL)
1366 dh_srvr=s->session->cert->dh_tmp;
1367 else
1368 {
1369 /* we get them from the cert */
58964a49 1370 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
d02b48c6
RE
1371 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_UNABLE_TO_FIND_DH_PARAMETERS);
1372 goto err;
1373 }
1374
1375 /* generate a new random key */
1376 if ((dh_clnt=DHparams_dup(dh_srvr)) == NULL)
1377 {
1378 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1379 goto err;
1380 }
1381 if (!DH_generate_key(dh_clnt))
1382 {
1383 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1384 goto err;
1385 }
1386
1387 /* use the 'p' output buffer for the DH key, but
1388 * make sure to clear it out afterwards */
58964a49 1389
d02b48c6 1390 n=DH_compute_key(p,dh_srvr->pub_key,dh_clnt);
58964a49 1391
d02b48c6
RE
1392 if (n <= 0)
1393 {
1394 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,ERR_R_DH_LIB);
1395 goto err;
1396 }
1397
1398 /* generate master key from the result */
1399 s->session->master_key_length=
58964a49 1400 s->method->ssl3_enc->generate_master_secret(s,
d02b48c6
RE
1401 s->session->master_key,p,n);
1402 /* clean up */
1403 memset(p,0,n);
1404
1405 /* send off the data */
1406 n=BN_num_bytes(dh_clnt->pub_key);
1407 s2n(n,p);
1408 BN_bn2bin(dh_clnt->pub_key,p);
1409 n+=2;
1410
1411 DH_free(dh_clnt);
1412
1413 /* perhaps clean things up a bit EAY EAY EAY EAY*/
1414 }
1415 else
1416#endif
1417 {
58964a49 1418 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
d02b48c6
RE
1419 SSLerr(SSL_F_SSL3_SEND_CLIENT_KEY_EXCHANGE,SSL_R_INTERNAL_ERROR);
1420 goto err;
1421 }
1422
1423 *(d++)=SSL3_MT_CLIENT_KEY_EXCHANGE;
1424 l2n3(n,d);
1425
1426 s->state=SSL3_ST_CW_KEY_EXCH_B;
1427 /* number of bytes to write */
1428 s->init_num=n+4;
1429 s->init_off=0;
1430 }
1431
1432 /* SSL3_ST_CW_KEY_EXCH_B */
1433 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1434err:
1435 return(-1);
1436 }
1437
6b691a5c 1438static int ssl3_send_client_verify(SSL *s)
d02b48c6
RE
1439 {
1440 unsigned char *p,*d;
1441 unsigned char data[MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH];
1442 EVP_PKEY *pkey;
58964a49 1443 int i=0;
d02b48c6 1444 unsigned long n;
58964a49
RE
1445#ifndef NO_DSA
1446 int j;
1447#endif
d02b48c6
RE
1448
1449 if (s->state == SSL3_ST_CW_CERT_VRFY_A)
1450 {
1451 d=(unsigned char *)s->init_buf->data;
1452 p= &(d[4]);
1453 pkey=s->cert->key->privatekey;
1454
58964a49
RE
1455 s->method->ssl3_enc->cert_verify_mac(s,&(s->s3->finish_dgst2),
1456 &(data[MD5_DIGEST_LENGTH]));
d02b48c6
RE
1457
1458#ifndef NO_RSA
1459 if (pkey->type == EVP_PKEY_RSA)
1460 {
58964a49
RE
1461 s->method->ssl3_enc->cert_verify_mac(s,
1462 &(s->s3->finish_dgst1),&(data[0]));
d02b48c6
RE
1463 i=RSA_private_encrypt(
1464 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
1465 data,&(p[2]),pkey->pkey.rsa,
1466 RSA_PKCS1_PADDING);
1467 if (i <= 0)
1468 {
1469 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_RSA_LIB);
1470 goto err;
1471 }
1472 s2n(i,p);
1473 n=i+2;
1474 }
1475 else
1476#endif
1477#ifndef NO_DSA
1478 if (pkey->type == EVP_PKEY_DSA)
1479 {
1480 if (!DSA_sign(pkey->save_type,
1481 &(data[MD5_DIGEST_LENGTH]),
1482 SHA_DIGEST_LENGTH,&(p[2]),
1483 (unsigned int *)&j,pkey->pkey.dsa))
1484 {
1485 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,ERR_R_DSA_LIB);
1486 goto err;
1487 }
1488 s2n(j,p);
1489 n=j+2;
1490 }
1491 else
1492#endif
1493 {
1494 SSLerr(SSL_F_SSL3_SEND_CLIENT_VERIFY,SSL_R_INTERNAL_ERROR);
1495 goto err;
1496 }
1497 *(d++)=SSL3_MT_CERTIFICATE_VERIFY;
1498 l2n3(n,d);
1499
1500 s->init_num=(int)n+4;
1501 s->init_off=0;
1502 }
1503 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1504err:
1505 return(-1);
1506 }
1507
6b691a5c 1508static int ssl3_send_client_certificate(SSL *s)
d02b48c6
RE
1509 {
1510 X509 *x509=NULL;
1511 EVP_PKEY *pkey=NULL;
1512 int i;
1513 unsigned long l;
1514
1515 if (s->state == SSL3_ST_CW_CERT_A)
1516 {
1517 if ((s->cert == NULL) ||
1518 (s->cert->key->x509 == NULL) ||
1519 (s->cert->key->privatekey == NULL))
1520 s->state=SSL3_ST_CW_CERT_B;
1521 else
1522 s->state=SSL3_ST_CW_CERT_C;
1523 }
1524
1525 /* We need to get a client cert */
1526 if (s->state == SSL3_ST_CW_CERT_B)
1527 {
1528 /* If we get an error, we need to
1529 * ssl->rwstate=SSL_X509_LOOKUP; return(-1);
1530 * We then get retied later */
1531 i=0;
1532 if (s->ctx->client_cert_cb != NULL)
1533 i=s->ctx->client_cert_cb(s,&(x509),&(pkey));
1534 if (i < 0)
1535 {
1536 s->rwstate=SSL_X509_LOOKUP;
1537 return(-1);
1538 }
1539 s->rwstate=SSL_NOTHING;
1540 if ((i == 1) && (pkey != NULL) && (x509 != NULL))
1541 {
1542 s->state=SSL3_ST_CW_CERT_B;
1543 if ( !SSL_use_certificate(s,x509) ||
1544 !SSL_use_PrivateKey(s,pkey))
1545 i=0;
1546 }
1547 else if (i == 1)
1548 {
1549 i=0;
1550 SSLerr(SSL_F_SSL3_SEND_CLIENT_CERTIFICATE,SSL_R_BAD_DATA_RETURNED_BY_CALLBACK);
1551 }
1552
1553 if (x509 != NULL) X509_free(x509);
1554 if (pkey != NULL) EVP_PKEY_free(pkey);
1555 if (i == 0)
1556 {
58964a49
RE
1557 if (s->version == SSL3_VERSION)
1558 {
1559 s->s3->tmp.cert_req=0;
1560 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_NO_CERTIFICATE);
1561 return(1);
1562 }
1563 else
1564 {
1565 s->s3->tmp.cert_req=2;
1566 }
d02b48c6
RE
1567 }
1568
1569 /* Ok, we have a cert */
1570 s->state=SSL3_ST_CW_CERT_C;
1571 }
1572
1573 if (s->state == SSL3_ST_CW_CERT_C)
1574 {
1575 s->state=SSL3_ST_CW_CERT_D;
58964a49
RE
1576 l=ssl3_output_cert_chain(s,
1577 (s->s3->tmp.cert_req == 2)?NULL:s->cert->key->x509);
d02b48c6
RE
1578 s->init_num=(int)l;
1579 s->init_off=0;
1580 }
1581 /* SSL3_ST_CW_CERT_D */
1582 return(ssl3_do_write(s,SSL3_RT_HANDSHAKE));
1583 }
1584
1585#define has_bits(i,m) (((i)&(m)) == (m))
1586
6b691a5c 1587static int ssl3_check_cert_and_algorithm(SSL *s)
d02b48c6
RE
1588 {
1589 int i,idx;
1590 long algs;
1591 EVP_PKEY *pkey=NULL;
1592 CERT *c;
1593 RSA *rsa;
1594 DH *dh;
1595
1596 c=s->session->cert;
1597
1598 if (c == NULL)
1599 {
1600 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_INTERNAL_ERROR);
1601 goto err;
1602 }
1603
1604 algs=s->s3->tmp.new_cipher->algorithms;
1605
1606 /* we don't have a certificate */
1607 if (algs & (SSL_aDH|SSL_aNULL))
1608 return(1);
1609
1610 rsa=s->session->cert->rsa_tmp;
1611 dh=s->session->cert->dh_tmp;
1612
1613 /* This is the passed certificate */
1614
1615 idx=c->cert_type;
1616 pkey=X509_get_pubkey(c->pkeys[idx].x509);
1617 i=X509_certificate_type(c->pkeys[idx].x509,pkey);
a8236c8c 1618 EVP_PKEY_free(pkey);
d02b48c6
RE
1619
1620
1621 /* Check that we have a certificate if we require one */
1622 if ((algs & SSL_aRSA) && !has_bits(i,EVP_PK_RSA|EVP_PKT_SIGN))
1623 {
1624 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_SIGNING_CERT);
1625 goto f_err;
1626 }
1627#ifndef NO_DSA
1628 else if ((algs & SSL_aDSS) && !has_bits(i,EVP_PK_DSA|EVP_PKT_SIGN))
1629 {
1630 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DSA_SIGNING_CERT);
1631 goto f_err;
1632 }
1633#endif
1634
1635 if ((algs & SSL_kRSA) &&
1636 !(has_bits(i,EVP_PK_RSA|EVP_PKT_ENC) || (rsa != NULL)))
1637 {
1638 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_RSA_ENCRYPTING_CERT);
1639 goto f_err;
1640 }
1641#ifndef NO_DH
1642 else if ((algs & SSL_kEDH) &&
1643 !(has_bits(i,EVP_PK_DH|EVP_PKT_EXCH) || (dh != NULL)))
1644 {
1645 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_KEY);
1646 goto f_err;
1647 }
1648 else if ((algs & SSL_kDHr) && !has_bits(i,EVP_PK_DH|EVP_PKS_RSA))
1649 {
1650 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_RSA_CERT);
1651 goto f_err;
1652 }
1653#ifndef NO_DSA
1654 else if ((algs & SSL_kDHd) && !has_bits(i,EVP_PK_DH|EVP_PKS_DSA))
1655 {
1656 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_DH_DSA_CERT);
1657 goto f_err;
1658 }
1659#endif
1660#endif
1661
06ab81f9 1662 if (SSL_IS_EXPORT(algs) && !has_bits(i,EVP_PKT_EXP))
d02b48c6
RE
1663 {
1664#ifndef NO_RSA
1665 if (algs & SSL_kRSA)
1666 {
06ab81f9
BL
1667 if (rsa == NULL
1668 || RSA_size(rsa) > SSL_EXPORT_PKEYLENGTH(algs))
d02b48c6
RE
1669 {
1670 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_RSA_KEY);
1671 goto f_err;
1672 }
1673 }
1674 else
1675#endif
1676#ifndef NO_DH
1677 if (algs & (SSL_kEDH|SSL_kDHr|SSL_kDHd))
06ab81f9
BL
1678 {
1679 if (dh == NULL
1680 || DH_size(dh) > SSL_EXPORT_PKEYLENGTH(algs))
d02b48c6
RE
1681 {
1682 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_MISSING_EXPORT_TMP_DH_KEY);
1683 goto f_err;
1684 }
1685 }
1686 else
1687#endif
1688 {
1689 SSLerr(SSL_F_SSL3_CHECK_CERT_AND_ALGORITHM,SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE);
1690 goto f_err;
1691 }
1692 }
1693 return(1);
1694f_err:
58964a49 1695 ssl3_send_alert(s,SSL3_AL_FATAL,SSL_AD_HANDSHAKE_FAILURE);
d02b48c6
RE
1696err:
1697 return(0);
1698 }
1699