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.]
62 #include <openssl/bio.h>
63 #include <openssl/rand.h>
64 #include <openssl/objects.h>
65 #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);
79 static SSL_METHOD *ssl2_get_server_method(int ver)
81 if (ver == SSL2_VERSION)
82 return(SSLv2_server_method());
87 SSL_METHOD *SSLv2_server_method(void)
90 static SSL_METHOD SSLv2_server_data;
94 memcpy((char *)&SSLv2_server_data,(char *)sslv2_base_method(),
96 SSLv2_server_data.ssl_accept=ssl2_accept;
97 SSLv2_server_data.get_ssl_method=ssl2_get_server_method;
100 return(&SSLv2_server_data);
103 int ssl2_accept(SSL *s)
105 unsigned long l=time(NULL);
112 RAND_add(&l,sizeof(l),0);
116 if (s->info_callback != NULL)
118 else if (s->ctx->info_callback != NULL)
119 cb=s->ctx->info_callback;
121 /* init things to blank */
122 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
127 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_NO_CERTIFICATE_SET);
140 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
141 case SSL_ST_OK|SSL_ST_ACCEPT:
144 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
146 s->version=SSL2_VERSION;
147 s->type=SSL_ST_ACCEPT;
150 if ((buf == NULL) && ((buf=BUF_MEM_new()) == NULL))
151 { ret= -1; goto end; }
152 if (!BUF_MEM_grow(buf,(int)
153 SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
154 { ret= -1; goto end; }
157 s->ctx->stats.sess_accept++;
158 s->handshake_func=ssl2_accept;
159 s->state=SSL2_ST_GET_CLIENT_HELLO_A;
162 case SSL2_ST_GET_CLIENT_HELLO_A:
163 case SSL2_ST_GET_CLIENT_HELLO_B:
164 case SSL2_ST_GET_CLIENT_HELLO_C:
166 ret=get_client_hello(s);
167 if (ret <= 0) goto end;
169 s->state=SSL2_ST_SEND_SERVER_HELLO_A;
172 case SSL2_ST_SEND_SERVER_HELLO_A:
173 case SSL2_ST_SEND_SERVER_HELLO_B:
175 if (ret <= 0) goto end;
179 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_A;
184 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
187 case SSL2_ST_GET_CLIENT_MASTER_KEY_A:
188 case SSL2_ST_GET_CLIENT_MASTER_KEY_B:
189 ret=get_client_master_key(s);
190 if (ret <= 0) goto end;
192 s->state=SSL2_ST_SERVER_START_ENCRYPTION;
195 case SSL2_ST_SERVER_START_ENCRYPTION:
196 /* Ok we how have sent all the stuff needed to
197 * start encrypting, the next packet back will
199 if (!ssl2_enc_init(s,0))
200 { ret= -1; goto end; }
202 s->state=SSL2_ST_SEND_SERVER_VERIFY_A;
205 case SSL2_ST_SEND_SERVER_VERIFY_A:
206 case SSL2_ST_SEND_SERVER_VERIFY_B:
207 ret=server_verify(s);
208 if (ret <= 0) goto end;
212 /* If we are in here, we have been
213 * buffering the output, so we need to
214 * flush it and remove buffering from
216 s->state=SSL2_ST_SEND_SERVER_VERIFY_C;
221 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
225 case SSL2_ST_SEND_SERVER_VERIFY_C:
226 /* get the number of bytes to write */
227 num1=BIO_ctrl(s->wbio,BIO_CTRL_INFO,0,NULL);
230 s->rwstate=SSL_WRITING;
231 num1=BIO_flush(s->wbio);
232 if (num1 <= 0) { ret= -1; goto end; }
233 s->rwstate=SSL_NOTHING;
236 /* flushed and now remove buffering */
237 s->wbio=BIO_pop(s->wbio);
239 s->state=SSL2_ST_GET_CLIENT_FINISHED_A;
242 case SSL2_ST_GET_CLIENT_FINISHED_A:
243 case SSL2_ST_GET_CLIENT_FINISHED_B:
244 ret=get_client_finished(s);
248 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_A;
251 case SSL2_ST_SEND_REQUEST_CERTIFICATE_A:
252 case SSL2_ST_SEND_REQUEST_CERTIFICATE_B:
253 case SSL2_ST_SEND_REQUEST_CERTIFICATE_C:
254 case SSL2_ST_SEND_REQUEST_CERTIFICATE_D:
255 /* don't do a 'request certificate' if we
256 * don't want to, or we already have one, and
257 * we only want to do it once. */
258 if (!(s->verify_mode & SSL_VERIFY_PEER) ||
259 ((s->session->peer != NULL) &&
260 (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)))
262 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
267 ret=request_certificate(s);
268 if (ret <= 0) goto end;
270 s->state=SSL2_ST_SEND_SERVER_FINISHED_A;
274 case SSL2_ST_SEND_SERVER_FINISHED_A:
275 case SSL2_ST_SEND_SERVER_FINISHED_B:
276 ret=server_finish(s);
277 if (ret <= 0) goto end;
283 BUF_MEM_free(s->init_buf);
284 ssl_free_wbio_buffer(s);
287 /* ERR_clear_error();*/
289 ssl_update_cache(s,SSL_SESS_CACHE_SERVER);
291 s->ctx->stats.sess_accept_good++;
295 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_DONE,1);
301 SSLerr(SSL_F_SSL2_ACCEPT,SSL_R_UNKNOWN_STATE);
307 if ((cb != NULL) && (s->state != state))
311 cb(s,SSL_CB_ACCEPT_LOOP,1);
318 cb(s,SSL_CB_ACCEPT_EXIT,ret);
322 static int get_client_master_key(SSL *s)
324 int is_export,i,n,keya,ek;
330 p=(unsigned char *)s->init_buf->data;
331 if (s->state == SSL2_ST_GET_CLIENT_MASTER_KEY_A)
333 i=ssl2_read(s,(char *)&(p[s->init_num]),10-s->init_num);
335 if (i < (10-s->init_num))
336 return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
337 if (*(p++) != SSL2_MT_CLIENT_MASTER_KEY)
339 if (p[-1] != SSL2_MT_ERROR)
341 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
342 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_READ_WRONG_PACKET_TYPE);
345 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
350 cp=ssl2_get_cipher_by_char(p);
353 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
354 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,
355 SSL_R_NO_CIPHER_MATCH);
358 s->session->cipher= cp;
361 n2s(p,i); s->s2->tmp.clear=i;
362 n2s(p,i); s->s2->tmp.enc=i;
363 n2s(p,i); s->session->key_arg_length=i;
364 s->state=SSL2_ST_GET_CLIENT_MASTER_KEY_B;
368 /* SSL2_ST_GET_CLIENT_MASTER_KEY_B */
369 p=(unsigned char *)s->init_buf->data;
370 keya=s->session->key_arg_length;
371 n=s->s2->tmp.clear+s->s2->tmp.enc+keya - s->init_num;
372 i=ssl2_read(s,(char *)&(p[s->init_num]),n);
373 if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_MASTER_KEY,i));
375 memcpy(s->session->key_arg,&(p[s->s2->tmp.clear+s->s2->tmp.enc]),
378 if (s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL)
380 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
381 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_NO_PRIVATEKEY);
384 i=ssl_rsa_private_decrypt(s->cert,s->s2->tmp.enc,
385 &(p[s->s2->tmp.clear]),&(p[s->s2->tmp.clear]),
386 (s->s2->ssl2_rollback)?RSA_SSLV23_PADDING:RSA_PKCS1_PADDING);
388 is_export=SSL_C_IS_EXPORT(s->session->cipher);
390 if (!ssl_cipher_get_evp(s->session,&c,&md,NULL))
392 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
393 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
397 if (s->session->cipher->algorithm2 & SSL2_CF_8_BYTE_ENC)
407 /* If a bad decrypt, continue with protocol but with a
408 * random master secret (Bleichenbacher attack) */
410 ((!is_export && (i != EVP_CIPHER_key_length(c)))
411 || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
412 EVP_CIPHER_key_length(c))))))
418 i=EVP_CIPHER_key_length(c);
419 RAND_pseudo_bytes(p,i);
425 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_BAD_RSA_DECRYPT);
427 /* incorrect number of key bytes for non export cipher */
428 else if ((!is_export && (i != EVP_CIPHER_key_length(c)))
429 || (is_export && ((i != ek) || (s->s2->tmp.clear+i !=
430 EVP_CIPHER_key_length(c)))))
433 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_WRONG_NUMBER_OF_KEY_BITS);
437 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
442 if (is_export) i+=s->s2->tmp.clear;
443 s->session->master_key_length=i;
444 memcpy(s->session->master_key,p,(unsigned int)i);
448 static int get_client_hello(SSL *s)
452 STACK_OF(SSL_CIPHER) *cs; /* a stack of SSL_CIPHERS */
453 STACK_OF(SSL_CIPHER) *cl; /* the ones we want to use */
456 /* This is a bit of a hack to check for the correct packet
457 * type the first time round. */
458 if (s->state == SSL2_ST_GET_CLIENT_HELLO_A)
461 s->state=SSL2_ST_GET_CLIENT_HELLO_B;
464 p=(unsigned char *)s->init_buf->data;
465 if (s->state == SSL2_ST_GET_CLIENT_HELLO_B)
467 i=ssl2_read(s,(char *)&(p[s->init_num]),9-s->init_num);
468 if (i < (9-s->init_num))
469 return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
471 if (*(p++) != SSL2_MT_CLIENT_HELLO)
473 if (p[-1] != SSL2_MT_ERROR)
475 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
476 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_READ_WRONG_PACKET_TYPE);
479 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_PEER_ERROR);
483 if (i < s->version) s->version=i;
484 n2s(p,i); s->s2->tmp.cipher_spec_length=i;
485 n2s(p,i); s->s2->tmp.session_id_length=i;
486 n2s(p,i); s->s2->challenge_length=i;
487 if ( (i < SSL2_MIN_CHALLENGE_LENGTH) ||
488 (i > SSL2_MAX_CHALLENGE_LENGTH))
490 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_INVALID_CHALLENGE_LENGTH);
493 s->state=SSL2_ST_GET_CLIENT_HELLO_C;
497 /* SSL2_ST_GET_CLIENT_HELLO_C */
498 p=(unsigned char *)s->init_buf->data;
499 n=s->s2->tmp.cipher_spec_length+s->s2->challenge_length+
500 s->s2->tmp.session_id_length-s->init_num;
501 i=ssl2_read(s,(char *)&(p[s->init_num]),n);
502 if (i != n) return(ssl2_part_read(s,SSL_F_GET_CLIENT_HELLO,i));
504 /* get session-id before cipher stuff so we can get out session
505 * structure if it is cached */
507 if ((s->s2->tmp.session_id_length != 0) &&
508 (s->s2->tmp.session_id_length != SSL2_SSL_SESSION_ID_LENGTH))
510 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
511 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_BAD_SSL_SESSION_ID_LENGTH);
515 if (s->s2->tmp.session_id_length == 0)
517 if (!ssl_get_new_session(s,1))
519 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
525 i=ssl_get_prev_session(s,&(p[s->s2->tmp.cipher_spec_length]),
526 s->s2->tmp.session_id_length);
528 { /* previous session */
533 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
540 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
541 SSLerr(SSL_F_GET_CLIENT_HELLO,SSL_R_NO_CERTIFICATE_SET);
545 if (!ssl_get_new_session(s,1))
547 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
555 cs=ssl_bytes_to_cipher_list(s,p,s->s2->tmp.cipher_spec_length,
556 &s->session->ciphers);
557 if (cs == NULL) goto mem_err;
559 cl=ssl_get_ciphers_by_id(s);
561 for (z=0; z<sk_SSL_CIPHER_num(cs); z++)
563 if (sk_SSL_CIPHER_find(cl,sk_SSL_CIPHER_value(cs,z)) < 0)
565 sk_SSL_CIPHER_delete(cs,z);
570 /* s->session->ciphers should now have a list of
571 * ciphers that are on both the client and server.
572 * This list is ordered by the order the client sent
576 p+=s->s2->tmp.cipher_spec_length;
577 /* done cipher selection */
579 /* session id extracted already */
580 p+=s->s2->tmp.session_id_length;
583 memcpy(s->s2->challenge,p,(unsigned int)s->s2->challenge_length);
586 SSLerr(SSL_F_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
590 static int server_hello(SSL *s)
594 STACK_OF(SSL_CIPHER) *sk;
596 p=(unsigned char *)s->init_buf->data;
597 if (s->state == SSL2_ST_SEND_SERVER_HELLO_A)
600 *(p++)=SSL2_MT_SERVER_HELLO; /* type */
602 *(p++)=(unsigned char)hit;
606 if (s->session->sess_cert != NULL)
607 /* This can't really happen because get_client_hello
608 * has called ssl_get_new_session, which does not set
610 ssl_sess_cert_free(s->session->sess_cert);
611 s->session->sess_cert = ssl_sess_cert_new();
612 if (s->session->sess_cert == NULL)
614 SSLerr(SSL_F_SERVER_HELLO, ERR_R_MALLOC_FAILURE);
618 /* If 'hit' is set, then s->sess_cert may be non-NULL or NULL,
619 * depending on whether it survived in the internal cache
620 * or was retrieved from an external cache.
621 * If it is NULL, we cannot put any useful data in it anyway,
622 * so we don't touch it.
625 #else /* That's what used to be done when cert_st and sess_cert_st were
628 { /* else add cert to session */
629 CRYPTO_add(&s->cert->references,1,CRYPTO_LOCK_SSL_CERT);
630 if (s->session->sess_cert != NULL)
631 ssl_cert_free(s->session->sess_cert);
632 s->session->sess_cert=s->cert;
634 else /* We have a session id-cache hit, if the
635 * session-id has no certificate listed against
636 * the 'cert' structure, grab the 'old' one
637 * listed against the SSL connection */
639 if (s->session->sess_cert == NULL)
641 CRYPTO_add(&s->cert->references,1,
642 CRYPTO_LOCK_SSL_CERT);
643 s->session->sess_cert=s->cert;
650 ssl2_return_error(s,SSL2_PE_NO_CERTIFICATE);
651 SSLerr(SSL_F_SERVER_HELLO,SSL_R_NO_CERTIFICATE_SPECIFIED);
657 *(p++)=0; /* no certificate type */
658 s2n(s->version,p); /* version */
659 s2n(0,p); /* cert len */
660 s2n(0,p); /* ciphers len */
665 /* put certificate type */
666 *(p++)=SSL2_CT_X509_CERTIFICATE;
667 s2n(s->version,p); /* version */
668 n=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
669 s2n(n,p); /* certificate length */
670 i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&d);
673 /* lets send out the ciphers we like in the
675 sk= s->session->ciphers;
676 n=ssl_cipher_list_to_bytes(s,s->session->ciphers,d);
678 s2n(n,p); /* add cipher length */
681 /* make and send conn_id */
682 s2n(SSL2_CONNECTION_ID_LENGTH,p); /* add conn_id length */
683 s->s2->conn_id_length=SSL2_CONNECTION_ID_LENGTH;
684 RAND_pseudo_bytes(s->s2->conn_id,(int)s->s2->conn_id_length);
685 memcpy(d,s->s2->conn_id,SSL2_CONNECTION_ID_LENGTH);
686 d+=SSL2_CONNECTION_ID_LENGTH;
688 s->state=SSL2_ST_SEND_SERVER_HELLO_B;
689 s->init_num=d-(unsigned char *)s->init_buf->data;
692 /* SSL2_ST_SEND_SERVER_HELLO_B */
693 /* If we are using TCP/IP, the performance is bad if we do 2
694 * writes without a read between them. This occurs when
695 * Session-id reuse is used, so I will put in a buffering module
699 if (!ssl_init_wbio_buffer(s,1)) return(-1);
702 return(ssl2_do_write(s));
705 static int get_client_finished(SSL *s)
710 p=(unsigned char *)s->init_buf->data;
711 if (s->state == SSL2_ST_GET_CLIENT_FINISHED_A)
713 i=ssl2_read(s,(char *)&(p[s->init_num]),1-s->init_num);
714 if (i < 1-s->init_num)
715 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
717 if (*p != SSL2_MT_CLIENT_FINISHED)
719 if (*p != SSL2_MT_ERROR)
721 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
722 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_READ_WRONG_PACKET_TYPE);
725 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_PEER_ERROR);
729 s->state=SSL2_ST_GET_CLIENT_FINISHED_B;
732 /* SSL2_ST_GET_CLIENT_FINISHED_B */
733 i=ssl2_read(s,(char *)&(p[s->init_num]),s->s2->conn_id_length-s->init_num);
734 if (i < (int)s->s2->conn_id_length-s->init_num)
736 return(ssl2_part_read(s,SSL_F_GET_CLIENT_FINISHED,i));
738 if (memcmp(p,s->s2->conn_id,(unsigned int)s->s2->conn_id_length) != 0)
740 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
741 SSLerr(SSL_F_GET_CLIENT_FINISHED,SSL_R_CONNECTION_ID_IS_DIFFERENT);
747 static int server_verify(SSL *s)
751 if (s->state == SSL2_ST_SEND_SERVER_VERIFY_A)
753 p=(unsigned char *)s->init_buf->data;
754 *(p++)=SSL2_MT_SERVER_VERIFY;
755 memcpy(p,s->s2->challenge,(unsigned int)s->s2->challenge_length);
756 /* p+=s->s2->challenge_length; */
758 s->state=SSL2_ST_SEND_SERVER_VERIFY_B;
759 s->init_num=s->s2->challenge_length+1;
762 return(ssl2_do_write(s));
765 static int server_finish(SSL *s)
769 if (s->state == SSL2_ST_SEND_SERVER_FINISHED_A)
771 p=(unsigned char *)s->init_buf->data;
772 *(p++)=SSL2_MT_SERVER_FINISHED;
774 memcpy(p,s->session->session_id,
775 (unsigned int)s->session->session_id_length);
776 /* p+=s->session->session_id_length; */
778 s->state=SSL2_ST_SEND_SERVER_FINISHED_B;
779 s->init_num=s->session->session_id_length+1;
783 /* SSL2_ST_SEND_SERVER_FINISHED_B */
784 return(ssl2_do_write(s));
787 /* send the request and check the response */
788 static int request_certificate(SSL *s)
790 unsigned char *p,*p2,*buf2;
792 int i,j,ctype,ret= -1;
794 STACK_OF(X509) *sk=NULL;
797 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_A)
799 p=(unsigned char *)s->init_buf->data;
800 *(p++)=SSL2_MT_REQUEST_CERTIFICATE;
801 *(p++)=SSL2_AT_MD5_WITH_RSA_ENCRYPTION;
802 RAND_pseudo_bytes(ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
803 memcpy(p,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
805 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_B;
806 s->init_num=SSL2_MIN_CERT_CHALLENGE_LENGTH+2;
810 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_B)
820 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_C;
823 if (s->state == SSL2_ST_SEND_REQUEST_CERTIFICATE_C)
825 p=(unsigned char *)s->init_buf->data;
826 i=ssl2_read(s,(char *)&(p[s->init_num]),6-s->init_num);
829 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
833 if ((*p == SSL2_MT_ERROR) && (i >= 3))
836 if (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
838 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
839 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
845 if ((*(p++) != SSL2_MT_CLIENT_CERTIFICATE) || (i < 6))
847 ssl2_return_error(s,SSL2_PE_UNDEFINED_ERROR);
848 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_SHORT_READ);
851 /* ok we have a response */
852 /* certificate type, there is only one right now. */
854 if (ctype != SSL2_AT_MD5_WITH_RSA_ENCRYPTION)
856 ssl2_return_error(s,SSL2_PE_UNSUPPORTED_CERTIFICATE_TYPE);
857 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_RESPONSE_ARGUMENT);
860 n2s(p,i); s->s2->tmp.clen=i;
861 n2s(p,i); s->s2->tmp.rlen=i;
862 s->state=SSL2_ST_SEND_REQUEST_CERTIFICATE_D;
866 /* SSL2_ST_SEND_REQUEST_CERTIFICATE_D */
867 p=(unsigned char *)s->init_buf->data;
868 j=s->s2->tmp.clen+s->s2->tmp.rlen-s->init_num;
869 i=ssl2_read(s,(char *)&(p[s->init_num]),j);
872 ret=ssl2_part_read(s,SSL_F_REQUEST_CERTIFICATE,i);
876 x509=(X509 *)d2i_X509(NULL,&p,(long)s->s2->tmp.clen);
879 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_X509_LIB);
883 if (((sk=sk_X509_new_null()) == NULL) || (!sk_X509_push(sk,x509)))
885 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
889 i=ssl_verify_cert_chain(s,sk);
891 if (i) /* we like the packet, now check the chksum */
896 EVP_VerifyInit(&ctx,s->ctx->rsa_md5);
897 EVP_VerifyUpdate(&ctx,s->s2->key_material,
898 (unsigned int)s->s2->key_material_length);
899 EVP_VerifyUpdate(&ctx,ccd,SSL2_MIN_CERT_CHALLENGE_LENGTH);
901 i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,NULL);
902 buf2=OPENSSL_malloc((unsigned int)i);
905 SSLerr(SSL_F_REQUEST_CERTIFICATE,ERR_R_MALLOC_FAILURE);
909 i=i2d_X509(s->cert->pkeys[SSL_PKEY_RSA_ENC].x509,&p2);
910 EVP_VerifyUpdate(&ctx,buf2,(unsigned int)i);
913 pkey=X509_get_pubkey(x509);
914 if (pkey == NULL) goto end;
915 i=EVP_VerifyFinal(&ctx,p,s->s2->tmp.rlen,pkey);
917 memset(&ctx,0,sizeof(ctx));
921 if (s->session->peer != NULL)
922 X509_free(s->session->peer);
923 s->session->peer=x509;
924 CRYPTO_add(&x509->references,1,CRYPTO_LOCK_X509);
925 s->session->verify_result = s->verify_result;
931 SSLerr(SSL_F_REQUEST_CERTIFICATE,SSL_R_BAD_CHECKSUM);
938 ssl2_return_error(s,SSL2_PE_BAD_CERTIFICATE);
946 static int ssl_rsa_private_decrypt(CERT *c, int len, unsigned char *from,
947 unsigned char *to, int padding)
952 if ((c == NULL) || (c->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL))
954 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_NO_PRIVATEKEY);
957 if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey->type != EVP_PKEY_RSA)
959 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,SSL_R_PUBLIC_KEY_IS_NOT_RSA);
962 rsa=c->pkeys[SSL_PKEY_RSA_ENC].privatekey->pkey.rsa;
964 /* we have the public key */
965 i=RSA_private_decrypt(len,from,to,rsa,padding);
967 SSLerr(SSL_F_SSL_RSA_PRIVATE_DECRYPT,ERR_R_RSA_LIB);
973 static void *dummy=&dummy;