2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
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)"
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
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.]
60 #include <openssl/bio.h>
61 #include <openssl/rand.h>
62 #include <openssl/objects.h>
64 #include <openssl/evp.h>
67 static SSL_METHOD
*ssl2_get_server_method(int ver
);
68 static int get_client_master_key(SSL
*s
);
69 static int get_client_hello(SSL
*s
);
70 static int server_hello(SSL
*s
);
71 static int get_client_finished(SSL
*s
);
72 static int server_verify(SSL
*s
);
73 static int server_finish(SSL
*s
);
74 static int request_certificate(SSL
*s
);
75 static int ssl_rsa_private_decrypt(CERT
*c
, int len
, unsigned char *from
,
76 unsigned char *to
,int padding
);
78 static SSL_METHOD
*ssl2_get_server_method();
79 static int get_client_master_key();
80 static int get_client_hello();
81 static int server_hello();
82 static int get_client_finished();
83 static int server_verify();
84 static int server_finish();
85 static int request_certificate();
86 static int ssl_rsa_private_decrypt();
91 static SSL_METHOD
*ssl2_get_server_method(int ver
)
93 if (ver
== SSL2_VERSION
)
94 return(SSLv2_server_method());
99 SSL_METHOD
*SSLv2_server_method(void)
102 static SSL_METHOD SSLv2_server_data
;
106 memcpy((char *)&SSLv2_server_data
,(char *)sslv2_base_method(),
108 SSLv2_server_data
.ssl_accept
=ssl2_accept
;
109 SSLv2_server_data
.get_ssl_method
=ssl2_get_server_method
;
112 return(&SSLv2_server_data
);
115 int ssl2_accept(SSL
*s
)
117 unsigned long l
=time(NULL
);
124 RAND_seed(&l
,sizeof(l
));
128 if (s
->info_callback
!= NULL
)
130 else if (s
->ctx
->info_callback
!= NULL
)
131 cb
=s
->ctx
->info_callback
;
133 /* init things to blank */
134 if (!SSL_in_init(s
) || SSL_in_before(s
)) SSL_clear(s
);
137 if (((s
->session
== NULL
) || (s
->session
->cert
== NULL
)) &&
140 SSLerr(SSL_F_SSL2_ACCEPT
,SSL_R_NO_CERTIFICATE_SET
);
153 case SSL_ST_BEFORE
|SSL_ST_ACCEPT
:
154 case SSL_ST_OK
|SSL_ST_ACCEPT
:
157 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_START
,1);
159 s
->version
=SSL2_VERSION
;
160 s
->type
=SSL_ST_ACCEPT
;
163 if ((buf
== NULL
) && ((buf
=BUF_MEM_new()) == NULL
))
164 { ret
= -1; goto end
; }
165 if (!BUF_MEM_grow(buf
,(int)
166 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER
))
167 { ret
= -1; goto end
; }
170 s
->ctx
->stats
.sess_accept
++;
171 s
->handshake_func
=ssl2_accept
;
172 s
->state
=SSL2_ST_GET_CLIENT_HELLO_A
;
175 case SSL2_ST_GET_CLIENT_HELLO_A
:
176 case SSL2_ST_GET_CLIENT_HELLO_B
:
177 case SSL2_ST_GET_CLIENT_HELLO_C
:
179 ret
=get_client_hello(s
);
180 if (ret
<= 0) goto end
;
182 s
->state
=SSL2_ST_SEND_SERVER_HELLO_A
;
185 case SSL2_ST_SEND_SERVER_HELLO_A
:
186 case SSL2_ST_SEND_SERVER_HELLO_B
:
188 if (ret
<= 0) goto end
;
192 s
->state
=SSL2_ST_GET_CLIENT_MASTER_KEY_A
;
197 s
->state
=SSL2_ST_SERVER_START_ENCRYPTION
;
200 case SSL2_ST_GET_CLIENT_MASTER_KEY_A
:
201 case SSL2_ST_GET_CLIENT_MASTER_KEY_B
:
202 ret
=get_client_master_key(s
);
203 if (ret
<= 0) goto end
;
205 s
->state
=SSL2_ST_SERVER_START_ENCRYPTION
;
208 case SSL2_ST_SERVER_START_ENCRYPTION
:
209 /* Ok we how have sent all the stuff needed to
210 * start encrypting, the next packet back will
212 if (!ssl2_enc_init(s
,0))
213 { ret
= -1; goto end
; }
215 s
->state
=SSL2_ST_SEND_SERVER_VERIFY_A
;
218 case SSL2_ST_SEND_SERVER_VERIFY_A
:
219 case SSL2_ST_SEND_SERVER_VERIFY_B
:
220 ret
=server_verify(s
);
221 if (ret
<= 0) goto end
;
225 /* If we are in here, we have been
226 * buffering the output, so we need to
227 * flush it and remove buffering from
229 s
->state
=SSL2_ST_SEND_SERVER_VERIFY_C
;
234 s
->state
=SSL2_ST_GET_CLIENT_FINISHED_A
;
238 case SSL2_ST_SEND_SERVER_VERIFY_C
:
239 /* get the number of bytes to write */
240 num1
=BIO_ctrl(s
->wbio
,BIO_CTRL_INFO
,0,NULL
);
243 s
->rwstate
=SSL_WRITING
;
244 num1
=BIO_flush(s
->wbio
);
245 if (num1
<= 0) { ret
= -1; goto end
; }
246 s
->rwstate
=SSL_NOTHING
;
249 /* flushed and now remove buffering */
250 s
->wbio
=BIO_pop(s
->wbio
);
252 s
->state
=SSL2_ST_GET_CLIENT_FINISHED_A
;
255 case SSL2_ST_GET_CLIENT_FINISHED_A
:
256 case SSL2_ST_GET_CLIENT_FINISHED_B
:
257 ret
=get_client_finished(s
);
261 s
->state
=SSL2_ST_SEND_REQUEST_CERTIFICATE_A
;
264 case SSL2_ST_SEND_REQUEST_CERTIFICATE_A
:
265 case SSL2_ST_SEND_REQUEST_CERTIFICATE_B
:
266 case SSL2_ST_SEND_REQUEST_CERTIFICATE_C
:
267 case SSL2_ST_SEND_REQUEST_CERTIFICATE_D
:
268 /* don't do a 'request certificate' if we
269 * don't want to, or we already have one, and
270 * we only want to do it once. */
271 if (!(s
->verify_mode
& SSL_VERIFY_PEER
) ||
272 ((s
->session
->peer
!= NULL
) &&
273 (s
->verify_mode
& SSL_VERIFY_CLIENT_ONCE
)))
275 s
->state
=SSL2_ST_SEND_SERVER_FINISHED_A
;
280 ret
=request_certificate(s
);
281 if (ret
<= 0) goto end
;
283 s
->state
=SSL2_ST_SEND_SERVER_FINISHED_A
;
287 case SSL2_ST_SEND_SERVER_FINISHED_A
:
288 case SSL2_ST_SEND_SERVER_FINISHED_B
:
289 ret
=server_finish(s
);
290 if (ret
<= 0) goto end
;
296 BUF_MEM_free(s
->init_buf
);
297 ssl_free_wbio_buffer(s
);
300 /* ERR_clear_error();*/
302 ssl_update_cache(s
,SSL_SESS_CACHE_SERVER
);
304 s
->ctx
->stats
.sess_accept_good
++;
308 if (cb
!= NULL
) cb(s
,SSL_CB_HANDSHAKE_DONE
,1);
314 SSLerr(SSL_F_SSL2_ACCEPT
,SSL_R_UNKNOWN_STATE
);
320 if ((cb
!= NULL
) && (s
->state
!= state
))
324 cb(s
,SSL_CB_ACCEPT_LOOP
,1);
331 cb(s
,SSL_CB_ACCEPT_EXIT
,ret
);
335 static int get_client_master_key(SSL
*s
)
337 int export
,i
,n
,keya
,ek
;
343 p
=(unsigned char *)s
->init_buf
->data
;
344 if (s
->state
== SSL2_ST_GET_CLIENT_MASTER_KEY_A
)
346 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),10-s
->init_num
);
348 if (i
< (10-s
->init_num
))
349 return(ssl2_part_read(s
,SSL_F_GET_CLIENT_MASTER_KEY
,i
));
350 if (*(p
++) != SSL2_MT_CLIENT_MASTER_KEY
)
352 if (p
[-1] != SSL2_MT_ERROR
)
354 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
355 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,SSL_R_READ_WRONG_PACKET_TYPE
);
358 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,
363 cp
=ssl2_get_cipher_by_char(p
);
366 ssl2_return_error(s
,SSL2_PE_NO_CIPHER
);
367 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,
368 SSL_R_NO_CIPHER_MATCH
);
371 s
->session
->cipher
= cp
;
374 n2s(p
,i
); s
->s2
->tmp
.clear
=i
;
375 n2s(p
,i
); s
->s2
->tmp
.enc
=i
;
376 n2s(p
,i
); s
->session
->key_arg_length
=i
;
377 s
->state
=SSL2_ST_GET_CLIENT_MASTER_KEY_B
;
381 /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
382 p
=(unsigned char *)s
->init_buf
->data
;
383 keya
=s
->session
->key_arg_length
;
384 n
=s
->s2
->tmp
.clear
+s
->s2
->tmp
.enc
+keya
- s
->init_num
;
385 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),n
);
386 if (i
!= n
) return(ssl2_part_read(s
,SSL_F_GET_CLIENT_MASTER_KEY
,i
));
388 memcpy(s
->session
->key_arg
,&(p
[s
->s2
->tmp
.clear
+s
->s2
->tmp
.enc
]),
391 if (s
->session
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
)
393 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
394 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,SSL_R_NO_PRIVATEKEY
);
397 i
=ssl_rsa_private_decrypt(s
->cert
,s
->s2
->tmp
.enc
,
398 &(p
[s
->s2
->tmp
.clear
]),&(p
[s
->s2
->tmp
.clear
]),
399 (s
->s2
->ssl2_rollback
)?RSA_SSLV23_PADDING
:RSA_PKCS1_PADDING
);
401 export
=SSL_C_IS_EXPORT(s
->session
->cipher
);
403 if (!ssl_cipher_get_evp(s
->session
,&c
,&md
,NULL
))
405 ssl2_return_error(s
,SSL2_PE_NO_CIPHER
);
406 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS
);
410 if (s
->session
->cipher
->algorithm2
& SSL2_CF_8_BYTE_ENC
)
420 /* If a bad decrypt, continue with protocol but with a
421 * dud master secret */
423 ((!export
&& (i
!= EVP_CIPHER_key_length(c
)))
424 || ( export
&& ((i
!= ek
) || (s
->s2
->tmp
.clear
+i
!=
425 EVP_CIPHER_key_length(c
))))))
430 i
=EVP_CIPHER_key_length(c
);
437 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,SSL_R_BAD_RSA_DECRYPT
);
439 /* incorrect number of key bytes for non export cipher */
440 else if ((!export
&& (i
!= EVP_CIPHER_key_length(c
)))
441 || ( export
&& ((i
!= ek
) || (s
->s2
->tmp
.clear
+i
!=
442 EVP_CIPHER_key_length(c
)))))
445 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY
,SSL_R_WRONG_NUMBER_OF_KEY_BITS
);
449 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
454 if (export
) i
+=s
->s2
->tmp
.clear
;
455 s
->session
->master_key_length
=i
;
456 memcpy(s
->session
->master_key
,p
,(unsigned int)i
);
460 static int get_client_hello(SSL
*s
)
464 STACK_OF(SSL_CIPHER
) *cs
; /* a stack of SSL_CIPHERS */
465 STACK_OF(SSL_CIPHER
) *cl
; /* the ones we want to use */
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
)
473 s
->state
=SSL2_ST_GET_CLIENT_HELLO_B
;
476 p
=(unsigned char *)s
->init_buf
->data
;
477 if (s
->state
== SSL2_ST_GET_CLIENT_HELLO_B
)
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
));
483 if (*(p
++) != SSL2_MT_CLIENT_HELLO
)
485 if (p
[-1] != SSL2_MT_ERROR
)
487 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
488 SSLerr(SSL_F_GET_CLIENT_HELLO
,SSL_R_READ_WRONG_PACKET_TYPE
);
491 SSLerr(SSL_F_GET_CLIENT_HELLO
,SSL_R_PEER_ERROR
);
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
))
502 SSLerr(SSL_F_GET_CLIENT_HELLO
,SSL_R_INVALID_CHALLENGE_LENGTH
);
505 s
->state
=SSL2_ST_GET_CLIENT_HELLO_C
;
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
));
516 /* get session-id before cipher stuff so we can get out session
517 * structure if it is cached */
519 if ((s
->s2
->tmp
.session_id_length
!= 0) &&
520 (s
->s2
->tmp
.session_id_length
!= SSL2_SSL_SESSION_ID_LENGTH
))
522 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
523 SSLerr(SSL_F_GET_CLIENT_HELLO
,SSL_R_BAD_SSL_SESSION_ID_LENGTH
);
527 if (s
->s2
->tmp
.session_id_length
== 0)
529 if (!ssl_get_new_session(s
,1))
531 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
537 i
=ssl_get_prev_session(s
,&(p
[s
->s2
->tmp
.cipher_spec_length
]),
538 s
->s2
->tmp
.session_id_length
);
540 { /* previous session */
545 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
552 ssl2_return_error(s
,SSL2_PE_NO_CERTIFICATE
);
553 SSLerr(SSL_F_GET_CLIENT_HELLO
,SSL_R_NO_CERTIFICATE_SET
);
557 if (!ssl_get_new_session(s
,1))
559 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
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
;
571 cl
=ssl_get_ciphers_by_id(s
);
573 for (z
=0; z
<sk_SSL_CIPHER_num(cs
); z
++)
575 if (sk_SSL_CIPHER_find(cl
,sk_SSL_CIPHER_value(cs
,z
)) < 0)
577 sk_SSL_CIPHER_delete(cs
,z
);
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
588 p
+=s
->s2
->tmp
.cipher_spec_length
;
589 /* done cipher selection */
591 /* session id extracted already */
592 p
+=s
->s2
->tmp
.session_id_length
;
595 memcpy(s
->s2
->challenge
,p
,(unsigned int)s
->s2
->challenge_length
);
598 SSLerr(SSL_F_GET_CLIENT_HELLO
,ERR_R_MALLOC_FAILURE
);
602 static int server_hello(SSL
*s
)
606 STACK_OF(SSL_CIPHER
) *sk
;
608 p
=(unsigned char *)s
->init_buf
->data
;
609 if (s
->state
== SSL2_ST_SEND_SERVER_HELLO_A
)
612 *(p
++)=SSL2_MT_SERVER_HELLO
; /* type */
614 *(p
++)=(unsigned char)hit
;
616 { /* else add cert to session */
617 CRYPTO_add(&s
->cert
->references
,1,CRYPTO_LOCK_SSL_CERT
);
618 if (s
->session
->cert
!= NULL
)
619 ssl_cert_free(s
->session
->cert
);
620 s
->session
->cert
=s
->cert
;
622 else /* We have a session id-cache hit, if the
623 * session-id has no certificate listed against
624 * the 'cert' structure, grab the 'old' one
625 * listed against the SSL connection */
627 if (s
->session
->cert
== NULL
)
629 CRYPTO_add(&s
->cert
->references
,1,
630 CRYPTO_LOCK_SSL_CERT
);
631 s
->session
->cert
=s
->cert
;
635 if (s
->session
->cert
== NULL
)
637 ssl2_return_error(s
,SSL2_PE_NO_CERTIFICATE
);
638 SSLerr(SSL_F_SERVER_HELLO
,SSL_R_NO_CERTIFICATE_SPECIFIED
);
644 *(p
++)=0; /* no certificate type */
645 s2n(s
->version
,p
); /* version */
646 s2n(0,p
); /* cert len */
647 s2n(0,p
); /* ciphers len */
652 /* put certificate type */
653 *(p
++)=SSL2_CT_X509_CERTIFICATE
;
654 s2n(s
->version
,p
); /* version */
655 n
=i2d_X509(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].x509
,NULL
);
656 s2n(n
,p
); /* certificate length */
657 i2d_X509(s
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].x509
,&d
);
660 /* lets send out the ciphers we like in the
662 sk
= s
->session
->ciphers
;
663 n
=ssl_cipher_list_to_bytes(s
,s
->session
->ciphers
,d
);
665 s2n(n
,p
); /* add cipher length */
668 /* make and send conn_id */
669 s2n(SSL2_CONNECTION_ID_LENGTH
,p
); /* add conn_id length */
670 s
->s2
->conn_id_length
=SSL2_CONNECTION_ID_LENGTH
;
671 RAND_bytes(s
->s2
->conn_id
,(int)s
->s2
->conn_id_length
);
672 memcpy(d
,s
->s2
->conn_id
,SSL2_CONNECTION_ID_LENGTH
);
673 d
+=SSL2_CONNECTION_ID_LENGTH
;
675 s
->state
=SSL2_ST_SEND_SERVER_HELLO_B
;
676 s
->init_num
=d
-(unsigned char *)s
->init_buf
->data
;
679 /* SSL2_ST_SEND_SERVER_HELLO_B */
680 /* If we are using TCP/IP, the performace is bad if we do 2
681 * writes without a read between them. This occurs when
682 * Session-id reuse is used, so I will put in a buffering module
686 if (!ssl_init_wbio_buffer(s
,1)) return(-1);
689 return(ssl2_do_write(s
));
692 static int get_client_finished(SSL
*s
)
697 p
=(unsigned char *)s
->init_buf
->data
;
698 if (s
->state
== SSL2_ST_GET_CLIENT_FINISHED_A
)
700 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),1-s
->init_num
);
701 if (i
< 1-s
->init_num
)
702 return(ssl2_part_read(s
,SSL_F_GET_CLIENT_FINISHED
,i
));
704 if (*p
!= SSL2_MT_CLIENT_FINISHED
)
706 if (*p
!= SSL2_MT_ERROR
)
708 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
709 SSLerr(SSL_F_GET_CLIENT_FINISHED
,SSL_R_READ_WRONG_PACKET_TYPE
);
712 SSLerr(SSL_F_GET_CLIENT_FINISHED
,SSL_R_PEER_ERROR
);
716 s
->state
=SSL2_ST_GET_CLIENT_FINISHED_B
;
719 /* SSL2_ST_GET_CLIENT_FINISHED_B */
720 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),s
->s2
->conn_id_length
-s
->init_num
);
721 if (i
< (int)s
->s2
->conn_id_length
-s
->init_num
)
723 return(ssl2_part_read(s
,SSL_F_GET_CLIENT_FINISHED
,i
));
725 if (memcmp(p
,s
->s2
->conn_id
,(unsigned int)s
->s2
->conn_id_length
) != 0)
727 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
728 SSLerr(SSL_F_GET_CLIENT_FINISHED
,SSL_R_CONNECTION_ID_IS_DIFFERENT
);
734 static int server_verify(SSL
*s
)
738 if (s
->state
== SSL2_ST_SEND_SERVER_VERIFY_A
)
740 p
=(unsigned char *)s
->init_buf
->data
;
741 *(p
++)=SSL2_MT_SERVER_VERIFY
;
742 memcpy(p
,s
->s2
->challenge
,(unsigned int)s
->s2
->challenge_length
);
743 /* p+=s->s2->challenge_length; */
745 s
->state
=SSL2_ST_SEND_SERVER_VERIFY_B
;
746 s
->init_num
=s
->s2
->challenge_length
+1;
749 return(ssl2_do_write(s
));
752 static int server_finish(SSL
*s
)
756 if (s
->state
== SSL2_ST_SEND_SERVER_FINISHED_A
)
758 p
=(unsigned char *)s
->init_buf
->data
;
759 *(p
++)=SSL2_MT_SERVER_FINISHED
;
761 memcpy(p
,s
->session
->session_id
,
762 (unsigned int)s
->session
->session_id_length
);
763 /* p+=s->session->session_id_length; */
765 s
->state
=SSL2_ST_SEND_SERVER_FINISHED_B
;
766 s
->init_num
=s
->session
->session_id_length
+1;
770 /* SSL2_ST_SEND_SERVER_FINISHED_B */
771 return(ssl2_do_write(s
));
774 /* send the request and check the response */
775 static int request_certificate(SSL
*s
)
777 unsigned char *p
,*p2
,*buf2
;
779 int i
,j
,ctype
,ret
= -1;
781 STACK_OF(X509
) *sk
=NULL
;
784 if (s
->state
== SSL2_ST_SEND_REQUEST_CERTIFICATE_A
)
786 p
=(unsigned char *)s
->init_buf
->data
;
787 *(p
++)=SSL2_MT_REQUEST_CERTIFICATE
;
788 *(p
++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION
;
789 RAND_bytes(ccd
,SSL2_MIN_CERT_CHALLENGE_LENGTH
);
790 memcpy(p
,ccd
,SSL2_MIN_CERT_CHALLENGE_LENGTH
);
792 s
->state
=SSL2_ST_SEND_REQUEST_CERTIFICATE_B
;
793 s
->init_num
=SSL2_MIN_CERT_CHALLENGE_LENGTH
+2;
797 if (s
->state
== SSL2_ST_SEND_REQUEST_CERTIFICATE_B
)
807 s
->state
=SSL2_ST_SEND_REQUEST_CERTIFICATE_C
;
810 if (s
->state
== SSL2_ST_SEND_REQUEST_CERTIFICATE_C
)
812 p
=(unsigned char *)s
->init_buf
->data
;
813 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),6-s
->init_num
);
816 ret
=ssl2_part_read(s
,SSL_F_REQUEST_CERTIFICATE
,i
);
820 if ((*p
== SSL2_MT_ERROR
) && (i
>= 3))
823 if (s
->verify_mode
& SSL_VERIFY_FAIL_IF_NO_PEER_CERT
)
825 ssl2_return_error(s
,SSL2_PE_BAD_CERTIFICATE
);
826 SSLerr(SSL_F_REQUEST_CERTIFICATE
,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE
);
832 if ((*(p
++) != SSL2_MT_CLIENT_CERTIFICATE
) || (i
< 6))
834 ssl2_return_error(s
,SSL2_PE_UNDEFINED_ERROR
);
835 SSLerr(SSL_F_REQUEST_CERTIFICATE
,SSL_R_SHORT_READ
);
838 /* ok we have a response */
839 /* certificate type, there is only one right now. */
841 if (ctype
!= SSL2_AT_MD5_WITH_RSA_ENCRYPTION
)
843 ssl2_return_error(s
,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE
);
844 SSLerr(SSL_F_REQUEST_CERTIFICATE
,SSL_R_BAD_RESPONSE_ARGUMENT
);
847 n2s(p
,i
); s
->s2
->tmp
.clen
=i
;
848 n2s(p
,i
); s
->s2
->tmp
.rlen
=i
;
849 s
->state
=SSL2_ST_SEND_REQUEST_CERTIFICATE_D
;
853 /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
854 p
=(unsigned char *)s
->init_buf
->data
;
855 j
=s
->s2
->tmp
.clen
+s
->s2
->tmp
.rlen
-s
->init_num
;
856 i
=ssl2_read(s
,(char *)&(p
[s
->init_num
]),j
);
859 ret
=ssl2_part_read(s
,SSL_F_REQUEST_CERTIFICATE
,i
);
863 x509
=(X509
*)d2i_X509(NULL
,&p
,(long)s
->s2
->tmp
.clen
);
866 SSLerr(SSL_F_REQUEST_CERTIFICATE
,ERR_R_X509_LIB
);
870 if (((sk
=sk_X509_new_null()) == NULL
) || (!sk_X509_push(sk
,x509
)))
872 SSLerr(SSL_F_REQUEST_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
876 i
=ssl_verify_cert_chain(s
,sk
);
878 if (i
) /* we like the packet, now check the chksum */
883 EVP_VerifyInit(&ctx
,s
->ctx
->rsa_md5
);
884 EVP_VerifyUpdate(&ctx
,s
->s2
->key_material
,
885 (unsigned int)s
->s2
->key_material_length
);
886 EVP_VerifyUpdate(&ctx
,ccd
,SSL2_MIN_CERT_CHALLENGE_LENGTH
);
888 i
=i2d_X509(s
->session
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].x509
,NULL
);
889 buf2
=(unsigned char *)Malloc((unsigned int)i
);
892 SSLerr(SSL_F_REQUEST_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
896 i
=i2d_X509(s
->session
->cert
->pkeys
[SSL_PKEY_RSA_ENC
].x509
,&p2
);
897 EVP_VerifyUpdate(&ctx
,buf2
,(unsigned int)i
);
900 pkey
=X509_get_pubkey(x509
);
901 if (pkey
== NULL
) goto end
;
902 i
=EVP_VerifyFinal(&ctx
,p
,s
->s2
->tmp
.rlen
,pkey
);
904 memset(&ctx
,0,sizeof(ctx
));
908 if (s
->session
->peer
!= NULL
)
909 X509_free(s
->session
->peer
);
910 s
->session
->peer
=x509
;
911 CRYPTO_add(&x509
->references
,1,CRYPTO_LOCK_X509
);
917 SSLerr(SSL_F_REQUEST_CERTIFICATE
,SSL_R_BAD_CHECKSUM
);
924 ssl2_return_error(s
,SSL2_PE_BAD_CERTIFICATE
);
932 static int ssl_rsa_private_decrypt(CERT
*c
, int len
, unsigned char *from
,
933 unsigned char *to
, int padding
)
938 if ((c
== NULL
) || (c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
== NULL
))
940 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT
,SSL_R_NO_PRIVATEKEY
);
943 if (c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
->type
!= EVP_PKEY_RSA
)
945 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT
,SSL_R_PUBLIC_KEY_IS_NOT_RSA
);
948 rsa
=c
->pkeys
[SSL_PKEY_RSA_ENC
].privatekey
->pkey
.rsa
;
950 /* we have the public key */
951 i
=RSA_private_decrypt(len
,from
,to
,rsa
,padding
);
953 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT
,ERR_R_RSA_LIB
);