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