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