]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/s2_srvr.c
Import of old SSLeay release: SSLeay 0.9.0b
[thirdparty/openssl.git] / ssl / s2_srvr.c
1 /* ssl/s2_srvr.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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>
60 #include "bio.h"
61 #include "rand.h"
62 #include "objects.h"
63 #include "ssl_locl.h"
64 #include "evp.h"
65
66 #ifndef NOPROTO
67 static int get_client_master_key(SSL *s);
68 static int get_client_hello(SSL *s);
69 static int server_hello(SSL *s);
70 static int get_client_finished(SSL *s);
71 static int server_verify(SSL *s);
72 static int server_finish(SSL *s);
73 static int request_certificate(SSL *s);
74 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
75 unsigned char *to,int padding);
76 #else
77 static int get_client_master_key();
78 static int get_client_hello();
79 static int server_hello();
80 static int get_client_finished();
81 static int server_verify();
82 static int server_finish();
83 static int request_certificate();
84 static int ssl_rsa_private_decrypt();
85 #endif
86
87 #define BREAK break
88
89 static SSL_METHOD *ssl2_get_server_method(ver)
90 int ver;
91 {
92 if (ver == SSL2_VERSION)
93 return(SSLv2_server_method());
94 else
95 return(NULL);
96 }
97
98 SSL_METHOD *SSLv2_server_method()
99 {
100 static int init=1;
101 static SSL_METHOD SSLv2_server_data;
102
103 if (init)
104 {
105 init=0;
106 memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
107 sizeof(SSL_METHOD));
108 SSLv2_server_data.ssl_accept=ssl2_accept;
109 SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
110 }
111 return(&SSLv2_server_data);
112 }
113
114 int ssl2_accept(s)
115 SSL *s;
116 {
117 unsigned long l=time(NULL);
118 BUF_MEM *buf=NULL;
119 int ret= -1;
120 long num1;
121 void (*cb)()=NULL;
122 int new_state,state;
123
124 RAND_seed((unsigned char *)&l,sizeof(l));
125 ERR_clear_error();
126 clear_sys_error();
127
128 if (s->info_callback != NULL)
129 cb=s->info_callback;
130 else if (s->ctx->info_callback != NULL)
131 cb=s->ctx->info_callback;
132
133 /* init things to blank */
134 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
135 s->in_handshake++;
136
137 if (((s->session == NULL) || (s->session->cert == NULL)) &&
138 (s->cert == NULL))
139 {
140 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
141 return(-1);
142 }
143
144 clear_sys_error();
145 for (;;)
146 {
147 state=s->state;
148
149 switch (s->state)
150 {
151 case SSL_ST_BEFORE:
152 case SSL_ST_ACCEPT:
153 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
154 case SSL_ST_OK|SSL_ST_ACCEPT:
155
156 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
157
158 s->version=SSL2_VERSION;
159 s->type=SSL_ST_ACCEPT;
160
161 buf=s->init_buf;
162 if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
163 { ret= -1; goto end; }
164 if (!BUF_MEM_grow(buf,(int)
165 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
166 { ret= -1; goto end; }
167 s->init_buf=buf;
168 s->init_num=0;
169 s->ctx->sess_accept++;
170 s->handshake_func=ssl2_accept;
171 s->state=SSL2_ST_GET_CLIENT_HELLO_A;
172 BREAK;
173
174 case SSL2_ST_GET_CLIENT_HELLO_A:
175 case SSL2_ST_GET_CLIENT_HELLO_B:
176 case SSL2_ST_GET_CLIENT_HELLO_C:
177 s->shutdown=0;
178 ret=get_client_hello(s);
179 if (ret <= 0) goto end;
180 s->init_num=0;
181 s->state=SSL2_ST_SEND_SERVER_HELLO_A;
182 BREAK;
183
184 case SSL2_ST_SEND_SERVER_HELLO_A:
185 case SSL2_ST_SEND_SERVER_HELLO_B:
186 ret=server_hello(s);
187 if (ret <= 0) goto end;
188 s->init_num=0;
189 if (!s->hit)
190 {
191 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
192 BREAK;
193 }
194 else
195 {
196 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
197 BREAK;
198 }
199 case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
200 case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
201 ret=get_client_master_key(s);
202 if (ret <= 0) goto end;
203 s->init_num=0;
204 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
205 BREAK;
206
207 case SSL2_ST_SERVER_START_ENCRYPTION:
208 /* Ok we how have sent all the stuff needed to
209 * start encrypting, the next packet back will
210 * be encrypted. */
211 if (!ssl2_enc_init(s,0))
212 { ret= -1; goto end; }
213 s->s2->clear_text=0;
214 s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
215 BREAK;
216
217 case SSL2_ST_SEND_SERVER_VERIFY_A:
218 case SSL2_ST_SEND_SERVER_VERIFY_B:
219 ret=server_verify(s);
220 if (ret <= 0) goto end;
221 s->init_num=0;
222 if (s->hit)
223 {
224 /* If we are in here, we have been
225 * buffering the output, so we need to
226 * flush it and remove buffering from
227 * future traffic */
228 s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
229 BREAK;
230 }
231 else
232 {
233 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
234 break;
235 }
236
237 case SSL2_ST_SEND_SERVER_VERIFY_C:
238 /* get the number of bytes to write */
239 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
240 if (num1 != 0)
241 {
242 s->rwstate=SSL_WRITING;
243 num1=BIO_flush(s->wbio);
244 if (num1 <= 0) { ret= -1; goto end; }
245 s->rwstate=SSL_NOTHING;
246 }
247
248 /* flushed and now remove buffering */
249 s->wbio=BIO_pop(s->wbio);
250
251 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
252 BREAK;
253
254 case SSL2_ST_GET_CLIENT_FINISHED_A:
255 case SSL2_ST_GET_CLIENT_FINISHED_B:
256 ret=get_client_finished(s);
257 if (ret <= 0)
258 goto end;
259 s->init_num=0;
260 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
261 BREAK;
262
263 case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
264 case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
265 case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
266 case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
267 /* don't do a 'request certificate' if we
268 * don't want to, or we already have one, and
269 * we only want to do it once. */
270 if (!(s->verify_mode & SSL_VERIFY_PEER) ||
271 ((s->session->peer != NULL) &&
272 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
273 {
274 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
275 break;
276 }
277 else
278 {
279 ret=request_certificate(s);
280 if (ret <= 0) goto end;
281 s->init_num=0;
282 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
283 }
284 BREAK;
285
286 case SSL2_ST_SEND_SERVER_FINISHED_A:
287 case SSL2_ST_SEND_SERVER_FINISHED_B:
288 ret=server_finish(s);
289 if (ret <= 0) goto end;
290 s->init_num=0;
291 s->state=SSL_ST_OK;
292 break;
293
294 case SSL_ST_OK:
295 BUF_MEM_free(s->init_buf);
296 s->init_buf=NULL;
297 s->init_num=0;
298 /* ERR_clear_error();*/
299
300 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
301
302 s->ctx->sess_accept_good++;
303 /* s->server=1; */
304 ret=1;
305
306 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
307
308 goto end;
309 /* BREAK; */
310
311 default:
312 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
313 ret= -1;
314 goto end;
315 /* BREAK; */
316 }
317
318 if ((cb != NULL) && (s->state != state))
319 {
320 new_state=s->state;
321 s->state=state;
322 cb(s,SSL_CB_ACCEPT_LOOP,1);
323 s->state=new_state;
324 }
325 }
326 end:
327 s->in_handshake--;
328 if (cb != NULL)
329 cb(s,SSL_CB_ACCEPT_EXIT,ret);
330 return(ret);
331 }
332
333 static int get_client_master_key(s)
334 SSL *s;
335 {
336 int export,i,n,keya,error=0,ek;
337 unsigned char *p;
338 SSL_CIPHER *cp;
339 EVP_CIPHER *c;
340 EVP_MD *md;
341
342 p=(unsigned char *)s->init_buf->data;
343 if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
344 {
345 i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
346
347 if (i < (10-s->init_num))
348 return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
349 if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
350 {
351 if (p[-1] != SSL2_MT_ERROR)
352 {
353 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
354 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
355 }
356 else
357 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
358 SSL_R_PEER_ERROR);
359 return(-1);
360 }
361
362 cp=ssl2_get_cipher_by_char(p);
363 if (cp == NULL)
364 {
365 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
366 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
367 SSL_R_NO_CIPHER_MATCH);
368 return(-1);
369 }
370 s->session->cipher= cp;
371
372 p+=3;
373 n2s(p,i); s->s2->tmp.clear=i;
374 n2s(p,i); s->s2->tmp.enc=i;
375 n2s(p,i); s->session->key_arg_length=i;
376 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
377 s->init_num=0;
378 }
379
380 /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
381 p=(unsigned char *)s->init_buf->data;
382 keya=s->session->key_arg_length;
383 n=s->s2->tmp.clear+s->s2->tmp.enc+keya - s->init_num;
384 i=ssl2_read(s,(char *)&(p[s->init_num]),n);
385 if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
386
387 memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
388 (unsigned int)keya);
389
390 if (s->session->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
391 {
392 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
393 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
394 return(-1);
395 }
396 i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
397 &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
398 (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
399
400 export=(s->session->cipher->algorithms & SSL_EXP)?1:0;
401
402 if (!ssl_cipher_get_evp(s->session->cipher,&c,&md))
403 {
404 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
405 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
406 return(0);
407 }
408
409 if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
410 {
411 export=1;
412 ek=8;
413 }
414 else
415 ek=5;
416
417 /* bad decrypt */
418 #if 1
419 /* If a bad decrypt, continue with protocol but with a
420 * dud master secret */
421 if ((i < 0) ||
422 ((!export && (i != EVP_CIPHER_key_length(c)))
423 || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
424 EVP_CIPHER_key_length(c))))))
425 {
426 if (export)
427 i=ek;
428 else
429 i=EVP_CIPHER_key_length(c);
430 RAND_bytes(p,i);
431 }
432 #else
433 if (i < 0)
434 {
435 error=1;
436 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
437 }
438 /* incorrect number of key bytes for non export cipher */
439 else if ((!export && (i != EVP_CIPHER_key_length(c)))
440 || ( export && ((i != ek) || (s->s2->tmp.clear+i !=
441 EVP_CIPHER_key_length(c)))))
442 {
443 error=1;
444 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
445 }
446 if (error)
447 {
448 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
449 return(-1);
450 }
451 #endif
452
453 if (export) i+=s->s2->tmp.clear;
454 s->session->master_key_length=i;
455 memcpy(s->session->master_key,p,(unsigned int)i);
456 return(1);
457 }
458
459 static int get_client_hello(s)
460 SSL *s;
461 {
462 int i,n;
463 unsigned char *p;
464 STACK *cs; /* a stack of SSL_CIPHERS */
465 STACK *cl; /* the ones we want to use */
466 int z;
467
468 /* This is a bit of a hack to check for the correct packet
469 * type the first time round. */
470 if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
471 {
472 s->first_packet=1;
473 s->state=SSL2_ST_GET_CLIENT_HELLO_B;
474 }
475
476 p=(unsigned char *)s->init_buf->data;
477 if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
478 {
479 i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
480 if (i < (9-s->init_num))
481 return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
482
483 if (*(p++) != SSL2_MT_CLIENT_HELLO)
484 {
485 if (p[-1] != SSL2_MT_ERROR)
486 {
487 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
488 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
489 }
490 else
491 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
492 return(-1);
493 }
494 n2s(p,i);
495 if (i < s->version) s->version=i;
496 n2s(p,i); s->s2->tmp.cipher_spec_length=i;
497 n2s(p,i); s->s2->tmp.session_id_length=i;
498 n2s(p,i); s->s2->challenge_length=i;
499 if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
500 (i > SSL2_MAX_CHALLENGE_LENGTH))
501 {
502 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
503 return(-1);
504 }
505 s->state=SSL2_ST_GET_CLIENT_HELLO_C;
506 s->init_num=0;
507 }
508
509 /* SSL2_ST_GET_CLIENT_HELLO_C */
510 p=(unsigned char *)s->init_buf->data;
511 n=s->s2->tmp.cipher_spec_length+s->s2->challenge_length+
512 s->s2->tmp.session_id_length-s->init_num;
513 i=ssl2_read(s,(char *)&(p[s->init_num]),n);
514 if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
515
516 /* get session-id before cipher stuff so we can get out session
517 * structure if it is cached */
518 /* session-id */
519 if ((s->s2->tmp.session_id_length != 0) &&
520 (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
521 {
522 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
523 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
524 return(-1);
525 }
526
527 if (s->s2->tmp.session_id_length == 0)
528 {
529 if (!ssl_get_new_session(s,1))
530 {
531 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
532 return(-1);
533 }
534 }
535 else
536 {
537 i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
538 s->s2->tmp.session_id_length);
539 if (i == 1)
540 { /* previous session */
541 s->hit=1;
542 }
543 else if (i == -1)
544 {
545 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
546 return(-1);
547 }
548 else
549 {
550 if (s->cert == NULL)
551 {
552 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
553 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
554 return(-1);
555 }
556
557 if (!ssl_get_new_session(s,1))
558 {
559 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
560 return(-1);
561 }
562 }
563 }
564
565 if (!s->hit)
566 {
567 cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
568 &s->session->ciphers);
569 if (cs == NULL) goto mem_err;
570
571 cl=ssl_get_ciphers_by_id(s);
572
573 for (z=0; z<sk_num(cs); z++)
574 {
575 if (sk_find(cl,sk_value(cs,z)) < 0)
576 {
577 sk_delete(cs,z);
578 z--;
579 }
580 }
581
582 /* s->session->ciphers should now have a list of
583 * ciphers that are on both the client and server.
584 * This list is ordered by the order the client sent
585 * the ciphers.
586 */
587 }
588 p+=s->s2->tmp.cipher_spec_length;
589 /* done cipher selection */
590
591 /* session id extracted already */
592 p+=s->s2->tmp.session_id_length;
593
594 /* challenge */
595 memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
596 return(1);
597 mem_err:
598 SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
599 return(0);
600 }
601
602 static int server_hello(s)
603 SSL *s;
604 {
605 unsigned char *p,*d;
606 int n,hit;
607 STACK *sk;
608
609 p=(unsigned char *)s->init_buf->data;
610 if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
611 {
612 d=p+11;
613 *(p++)=SSL2_MT_SERVER_HELLO; /* type */
614 hit=s->hit;
615 *(p++)=(unsigned char)hit;
616 if (!hit)
617 { /* else add cert to session */
618 CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
619 if (s->session->cert != NULL)
620 ssl_cert_free(s->session->cert);
621 s->session->cert=s->cert;
622 }
623 else /* We have a session id-cache hit, if the
624 * session-id has no certificate listed against
625 * the 'cert' structure, grab the 'old' one
626 * listed against the SSL connection */
627 {
628 if (s->session->cert == NULL)
629 {
630 CRYPTO_add(&s->cert->references,1,
631 CRYPTO_LOCK_SSL_CERT);
632 s->session->cert=s->cert;
633 }
634 }
635
636 if (s->session->cert == NULL)
637 {
638 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
639 SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
640 return(-1);
641 }
642
643 if (hit)
644 {
645 *(p++)=0; /* no certificate type */
646 s2n(s->version,p); /* version */
647 s2n(0,p); /* cert len */
648 s2n(0,p); /* ciphers len */
649 }
650 else
651 {
652 /* EAY EAY */
653 /* put certificate type */
654 *(p++)=SSL2_CT_X509_CERTIFICATE;
655 s2n(s->version,p); /* version */
656 n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
657 s2n(n,p); /* certificate length */
658 i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
659 n=0;
660
661 /* lets send out the ciphers we like in the
662 * prefered order */
663 sk= s->session->ciphers;
664 n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
665 d+=n;
666 s2n(n,p); /* add cipher length */
667 }
668
669 /* make and send conn_id */
670 s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
671 s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
672 RAND_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
673 memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
674 d+=SSL2_CONNECTION_ID_LENGTH;
675
676 s->state=SSL2_ST_SEND_SERVER_HELLO_B;
677 s->init_num=d-(unsigned char *)s->init_buf->data;
678 s->init_off=0;
679 }
680 /* SSL2_ST_SEND_SERVER_HELLO_B */
681 /* If we are using TCP/IP, the performace is bad if we do 2
682 * writes without a read between them. This occurs when
683 * Session-id reuse is used, so I will put in a buffering module
684 */
685 if (s->hit)
686 {
687 if (!ssl_init_wbio_buffer(s,1)) return(-1);
688 }
689
690 return(ssl2_do_write(s));
691 }
692
693 static int get_client_finished(s)
694 SSL *s;
695 {
696 unsigned char *p;
697 int i;
698
699 p=(unsigned char *)s->init_buf->data;
700 if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
701 {
702 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
703 if (i < 1-s->init_num)
704 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
705
706 if (*p != SSL2_MT_CLIENT_FINISHED)
707 {
708 if (*p != SSL2_MT_ERROR)
709 {
710 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
711 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
712 }
713 else
714 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
715 return(-1);
716 }
717 s->init_num=0;
718 s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
719 }
720
721 /* SSL2_ST_GET_CLIENT_FINISHED_B */
722 i=ssl2_read(s,(char *)&(p[s->init_num]),s->s2->conn_id_length-s->init_num);
723 if (i < (int)s->s2->conn_id_length-s->init_num)
724 {
725 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
726 }
727 if (memcmp(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length) != 0)
728 {
729 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
730 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
731 return(-1);
732 }
733 return(1);
734 }
735
736 static int server_verify(s)
737 SSL *s;
738 {
739 unsigned char *p;
740
741 if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
742 {
743 p=(unsigned char *)s->init_buf->data;
744 *(p++)=SSL2_MT_SERVER_VERIFY;
745 memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
746 /* p+=s->s2->challenge_length; */
747
748 s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
749 s->init_num=s->s2->challenge_length+1;
750 s->init_off=0;
751 }
752 return(ssl2_do_write(s));
753 }
754
755 static int server_finish(s)
756 SSL *s;
757 {
758 unsigned char *p;
759
760 if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
761 {
762 p=(unsigned char *)s->init_buf->data;
763 *(p++)=SSL2_MT_SERVER_FINISHED;
764
765 memcpy(p,s->session->session_id,
766 (unsigned int)s->session->session_id_length);
767 /* p+=s->session->session_id_length; */
768
769 s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
770 s->init_num=s->session->session_id_length+1;
771 s->init_off=0;
772 }
773
774 /* SSL2_ST_SEND_SERVER_FINISHED_B */
775 return(ssl2_do_write(s));
776 }
777
778 /* send the request and check the response */
779 static int request_certificate(s)
780 SSL *s;
781 {
782 unsigned char *p,*p2,*buf2;
783 unsigned char *ccd;
784 int i,j,ctype,ret= -1;
785 X509 *x509=NULL;
786 STACK *sk=NULL;
787
788 ccd=s->s2->tmp.ccl;
789 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
790 {
791 p=(unsigned char *)s->init_buf->data;
792 *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
793 *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
794 RAND_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
795 memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
796
797 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
798 s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
799 s->init_off=0;
800 }
801
802 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
803 {
804 i=ssl2_do_write(s);
805 if (i <= 0)
806 {
807 ret=i;
808 goto end;
809 }
810
811 s->init_num=0;
812 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
813 }
814
815 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
816 {
817 p=(unsigned char *)s->init_buf->data;
818 i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);
819 if (i < 3)
820 {
821 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
822 goto end;
823 }
824
825 if ((*p == SSL2_MT_ERROR) && (i >= 3))
826 {
827 n2s(p,i);
828 if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
829 {
830 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
831 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
832 goto end;
833 }
834 ret=1;
835 goto end;
836 }
837 if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))
838 {
839 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
840 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
841 goto end;
842 }
843 /* ok we have a response */
844 /* certificate type, there is only one right now. */
845 ctype= *(p++);
846 if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
847 {
848 ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
849 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
850 goto end;
851 }
852 n2s(p,i); s->s2->tmp.clen=i;
853 n2s(p,i); s->s2->tmp.rlen=i;
854 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
855 s->init_num=0;
856 }
857
858 /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
859 p=(unsigned char *)s->init_buf->data;
860 j=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;
861 i=ssl2_read(s,(char *)&(p[s->init_num]),j);
862 if (i < j)
863 {
864 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
865 goto end;
866 }
867
868 x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
869 if (x509 == NULL)
870 {
871 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
872 goto msg_end;
873 }
874
875 if (((sk=sk_new_null()) == NULL) || (!sk_push(sk,(char *)x509)))
876 {
877 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
878 goto msg_end;
879 }
880
881 i=ssl_verify_cert_chain(s,sk);
882
883 if (i) /* we like the packet, now check the chksum */
884 {
885 EVP_MD_CTX ctx;
886 EVP_PKEY *pkey=NULL;
887
888 EVP_VerifyInit(&ctx,s->ctx->rsa_md5);
889 EVP_VerifyUpdate(&ctx,s->s2->key_material,
890 (unsigned int)s->s2->key_material_length);
891 EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
892
893 i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
894 buf2=(unsigned char *)Malloc((unsigned int)i);
895 if (buf2 == NULL)
896 {
897 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
898 goto msg_end;
899 }
900 p2=buf2;
901 i=i2d_X509(s->session->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
902 EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
903 Free(buf2);
904
905 pkey=X509_get_pubkey(x509);
906 if (pkey == NULL) goto end;
907 i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
908 memset(&ctx,0,sizeof(ctx));
909
910 if (i)
911 {
912 if (s->session->peer != NULL)
913 X509_free(s->session->peer);
914 s->session->peer=x509;
915 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
916 ret=1;
917 goto end;
918 }
919 else
920 {
921 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
922 goto msg_end;
923 }
924 }
925 else
926 {
927 msg_end:
928 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
929 }
930 end:
931 if (sk != NULL) sk_free(sk);
932 if (x509 != NULL) X509_free(x509);
933 return(ret);
934 }
935
936 static int ssl_rsa_private_decrypt(c, len, from, to,padding)
937 CERT *c;
938 int len;
939 unsigned char *from;
940 unsigned char *to;
941 int padding;
942 {
943 RSA *rsa;
944 int i;
945
946 if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
947 {
948 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
949 return(-1);
950 }
951 if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
952 {
953 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
954 return(-1);
955 }
956 rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
957
958 /* we have the public key */
959 i=RSA_private_decrypt(len,from,to,rsa,padding);
960 if (i < 0)
961 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
962 return(i);
963 }
964