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