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/md5.h>
61 #include <openssl/sha.h>
62 #include <openssl/objects.h>
65 const char *ssl3_version_str="SSLv3" OPENSSL_VERSION_PTEXT;
67 #define SSL3_NUM_CIPHERS (sizeof(ssl3_ciphers)/sizeof(SSL_CIPHER))
69 static long ssl3_default_timeout(void );
71 OPENSSL_GLOBAL SSL_CIPHER ssl3_ciphers[]={
76 SSL3_TXT_RSA_NULL_MD5,
78 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_MD5|SSL_SSLV3,
89 SSL3_TXT_RSA_NULL_SHA,
91 SSL_kRSA|SSL_aRSA|SSL_eNULL |SSL_SHA1|SSL_SSLV3,
104 SSL3_TXT_ADH_RC4_40_MD5,
105 SSL3_CK_ADH_RC4_40_MD5,
106 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
107 SSL_EXPORT|SSL_EXP40,
117 SSL3_TXT_ADH_RC4_128_MD5,
118 SSL3_CK_ADH_RC4_128_MD5,
119 SSL_kEDH |SSL_aNULL|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
130 SSL3_TXT_ADH_DES_40_CBC_SHA,
131 SSL3_CK_ADH_DES_40_CBC_SHA,
132 SSL_kEDH |SSL_aNULL|SSL_DES|SSL_SHA1|SSL_SSLV3,
133 SSL_EXPORT|SSL_EXP40,
143 SSL3_TXT_ADH_DES_64_CBC_SHA,
144 SSL3_CK_ADH_DES_64_CBC_SHA,
145 SSL_kEDH |SSL_aNULL|SSL_DES |SSL_SHA1|SSL_SSLV3,
156 SSL3_TXT_ADH_DES_192_CBC_SHA,
157 SSL3_CK_ADH_DES_192_CBC_SHA,
158 SSL_kEDH |SSL_aNULL|SSL_3DES |SSL_SHA1|SSL_SSLV3,
171 SSL3_TXT_RSA_RC4_40_MD5,
172 SSL3_CK_RSA_RC4_40_MD5,
173 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5 |SSL_SSLV3,
174 SSL_EXPORT|SSL_EXP40,
184 SSL3_TXT_RSA_RC4_128_MD5,
185 SSL3_CK_RSA_RC4_128_MD5,
186 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_MD5|SSL_SSLV3,
187 SSL_NOT_EXP|SSL_MEDIUM,
197 SSL3_TXT_RSA_RC4_128_SHA,
198 SSL3_CK_RSA_RC4_128_SHA,
199 SSL_kRSA|SSL_aRSA|SSL_RC4 |SSL_SHA1|SSL_SSLV3,
200 SSL_NOT_EXP|SSL_MEDIUM,
210 SSL3_TXT_RSA_RC2_40_MD5,
211 SSL3_CK_RSA_RC2_40_MD5,
212 SSL_kRSA|SSL_aRSA|SSL_RC2 |SSL_MD5 |SSL_SSLV3,
213 SSL_EXPORT|SSL_EXP40,
223 SSL3_TXT_RSA_IDEA_128_SHA,
224 SSL3_CK_RSA_IDEA_128_SHA,
225 SSL_kRSA|SSL_aRSA|SSL_IDEA |SSL_SHA1|SSL_SSLV3,
226 SSL_NOT_EXP|SSL_MEDIUM,
236 SSL3_TXT_RSA_DES_40_CBC_SHA,
237 SSL3_CK_RSA_DES_40_CBC_SHA,
238 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
239 SSL_EXPORT|SSL_EXP40,
249 SSL3_TXT_RSA_DES_64_CBC_SHA,
250 SSL3_CK_RSA_DES_64_CBC_SHA,
251 SSL_kRSA|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_SSLV3,
262 SSL3_TXT_RSA_DES_192_CBC3_SHA,
263 SSL3_CK_RSA_DES_192_CBC3_SHA,
264 SSL_kRSA|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
265 SSL_NOT_EXP|SSL_HIGH,
277 SSL3_TXT_DH_DSS_DES_40_CBC_SHA,
278 SSL3_CK_DH_DSS_DES_40_CBC_SHA,
279 SSL_kDHd |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
280 SSL_EXPORT|SSL_EXP40,
290 SSL3_TXT_DH_DSS_DES_64_CBC_SHA,
291 SSL3_CK_DH_DSS_DES_64_CBC_SHA,
292 SSL_kDHd |SSL_aDH|SSL_DES |SSL_SHA1|SSL_SSLV3,
303 SSL3_TXT_DH_DSS_DES_192_CBC3_SHA,
304 SSL3_CK_DH_DSS_DES_192_CBC3_SHA,
305 SSL_kDHd |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
306 SSL_NOT_EXP|SSL_HIGH,
316 SSL3_TXT_DH_RSA_DES_40_CBC_SHA,
317 SSL3_CK_DH_RSA_DES_40_CBC_SHA,
318 SSL_kDHr |SSL_aDH|SSL_DES|SSL_SHA1|SSL_SSLV3,
319 SSL_EXPORT|SSL_EXP40,
329 SSL3_TXT_DH_RSA_DES_64_CBC_SHA,
330 SSL3_CK_DH_RSA_DES_64_CBC_SHA,
331 SSL_kDHr |SSL_aDH|SSL_DES |SSL_SHA1|SSL_SSLV3,
342 SSL3_TXT_DH_RSA_DES_192_CBC3_SHA,
343 SSL3_CK_DH_RSA_DES_192_CBC3_SHA,
344 SSL_kDHr |SSL_aDH|SSL_3DES |SSL_SHA1|SSL_SSLV3,
345 SSL_NOT_EXP|SSL_HIGH,
353 /* The Ephemeral DH ciphers */
357 SSL3_TXT_EDH_DSS_DES_40_CBC_SHA,
358 SSL3_CK_EDH_DSS_DES_40_CBC_SHA,
359 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA1|SSL_SSLV3,
360 SSL_EXPORT|SSL_EXP40,
370 SSL3_TXT_EDH_DSS_DES_64_CBC_SHA,
371 SSL3_CK_EDH_DSS_DES_64_CBC_SHA,
372 SSL_kEDH|SSL_aDSS|SSL_DES |SSL_SHA1|SSL_SSLV3,
383 SSL3_TXT_EDH_DSS_DES_192_CBC3_SHA,
384 SSL3_CK_EDH_DSS_DES_192_CBC3_SHA,
385 SSL_kEDH|SSL_aDSS|SSL_3DES |SSL_SHA1|SSL_SSLV3,
386 SSL_NOT_EXP|SSL_HIGH,
396 SSL3_TXT_EDH_RSA_DES_40_CBC_SHA,
397 SSL3_CK_EDH_RSA_DES_40_CBC_SHA,
398 SSL_kEDH|SSL_aRSA|SSL_DES|SSL_SHA1|SSL_SSLV3,
399 SSL_EXPORT|SSL_EXP40,
409 SSL3_TXT_EDH_RSA_DES_64_CBC_SHA,
410 SSL3_CK_EDH_RSA_DES_64_CBC_SHA,
411 SSL_kEDH|SSL_aRSA|SSL_DES |SSL_SHA1|SSL_SSLV3,
422 SSL3_TXT_EDH_RSA_DES_192_CBC3_SHA,
423 SSL3_CK_EDH_RSA_DES_192_CBC3_SHA,
424 SSL_kEDH|SSL_aRSA|SSL_3DES |SSL_SHA1|SSL_SSLV3,
425 SSL_NOT_EXP|SSL_HIGH,
437 SSL3_TXT_FZA_DMS_NULL_SHA,
438 SSL3_CK_FZA_DMS_NULL_SHA,
439 SSL_kFZA|SSL_aFZA |SSL_eNULL |SSL_SHA1|SSL_SSLV3,
451 SSL3_TXT_FZA_DMS_FZA_SHA,
452 SSL3_CK_FZA_DMS_FZA_SHA,
453 SSL_kFZA|SSL_aFZA |SSL_eFZA |SSL_SHA1|SSL_SSLV3,
465 SSL3_TXT_FZA_DMS_RC4_SHA,
466 SSL3_CK_FZA_DMS_RC4_SHA,
467 SSL_kFZA|SSL_aFZA |SSL_RC4 |SSL_SHA1|SSL_SSLV3,
476 #if TLS1_ALLOW_EXPERIMENTAL_CIPHERSUITES
477 /* New TLS Export CipherSuites */
481 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_MD5,
482 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_MD5,
483 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_MD5|SSL_TLSV1,
484 SSL_EXPORT|SSL_EXP56,
494 TLS1_TXT_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
495 TLS1_CK_RSA_EXPORT1024_WITH_RC2_CBC_56_MD5,
496 SSL_kRSA|SSL_aRSA|SSL_RC2|SSL_MD5|SSL_TLSV1,
497 SSL_EXPORT|SSL_EXP56,
507 TLS1_TXT_RSA_EXPORT1024_WITH_DES_CBC_SHA,
508 TLS1_CK_RSA_EXPORT1024_WITH_DES_CBC_SHA,
509 SSL_kRSA|SSL_aRSA|SSL_DES|SSL_SHA|SSL_TLSV1,
510 SSL_EXPORT|SSL_EXP56,
520 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
521 TLS1_CK_DHE_DSS_EXPORT1024_WITH_DES_CBC_SHA,
522 SSL_kEDH|SSL_aDSS|SSL_DES|SSL_SHA|SSL_TLSV1,
523 SSL_EXPORT|SSL_EXP56,
533 TLS1_TXT_RSA_EXPORT1024_WITH_RC4_56_SHA,
534 TLS1_CK_RSA_EXPORT1024_WITH_RC4_56_SHA,
535 SSL_kRSA|SSL_aRSA|SSL_RC4|SSL_SHA|SSL_TLSV1,
536 SSL_EXPORT|SSL_EXP56,
546 TLS1_TXT_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
547 TLS1_CK_DHE_DSS_EXPORT1024_WITH_RC4_56_SHA,
548 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
549 SSL_EXPORT|SSL_EXP56,
559 TLS1_TXT_DHE_DSS_WITH_RC4_128_SHA,
560 TLS1_CK_DHE_DSS_WITH_RC4_128_SHA,
561 SSL_kEDH|SSL_aDSS|SSL_RC4|SSL_SHA|SSL_TLSV1,
574 static SSL3_ENC_METHOD SSLv3_enc_data={
577 ssl3_setup_key_block,
578 ssl3_generate_master_secret,
579 ssl3_change_cipher_state,
580 ssl3_final_finish_mac,
581 MD5_DIGEST_LENGTH+SHA_DIGEST_LENGTH,
582 ssl3_cert_verify_mac,
583 SSL3_MD_CLIENT_FINISHED_CONST,4,
584 SSL3_MD_SERVER_FINISHED_CONST,4,
588 static SSL_METHOD SSLv3_data= {
593 ssl_undefined_function,
594 ssl_undefined_function,
600 ssl3_renegotiate_check,
603 ssl3_get_cipher_by_char,
604 ssl3_put_cipher_by_char,
609 ssl3_default_timeout,
611 ssl_undefined_function,
613 ssl3_ctx_callback_ctrl,
616 static long ssl3_default_timeout(void)
618 /* 2 hours, the 24 hours mentioned in the SSLv3 spec
619 * is way too long for http, the cache would over fill */
623 SSL_METHOD *sslv3_base_method(void)
628 int ssl3_num_ciphers(void)
630 return(SSL3_NUM_CIPHERS);
633 SSL_CIPHER *ssl3_get_cipher(unsigned int u)
635 if (u < SSL3_NUM_CIPHERS)
636 return(&(ssl3_ciphers[SSL3_NUM_CIPHERS-1-u]));
641 /* The problem is that it may not be the correct record type */
642 int ssl3_pending(SSL *s)
644 return(s->s3->rrec.length);
651 if ((s3=OPENSSL_malloc(sizeof *s3)) == NULL) goto err;
652 memset(s3,0,sizeof *s3);
656 s->method->ssl_clear(s);
662 void ssl3_free(SSL *s)
667 ssl3_cleanup_key_block(s);
668 if (s->s3->rbuf.buf != NULL)
669 OPENSSL_free(s->s3->rbuf.buf);
670 if (s->s3->wbuf.buf != NULL)
671 OPENSSL_free(s->s3->wbuf.buf);
672 if (s->s3->rrec.comp != NULL)
673 OPENSSL_free(s->s3->rrec.comp);
675 if (s->s3->tmp.dh != NULL)
676 DH_free(s->s3->tmp.dh);
678 if (s->s3->tmp.ca_names != NULL)
679 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
680 memset(s->s3,0,sizeof *s->s3);
685 void ssl3_clear(SSL *s)
687 unsigned char *rp,*wp;
689 ssl3_cleanup_key_block(s);
690 if (s->s3->tmp.ca_names != NULL)
691 sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
693 if (s->s3->rrec.comp != NULL)
695 OPENSSL_free(s->s3->rrec.comp);
696 s->s3->rrec.comp=NULL;
699 if (s->s3->tmp.dh != NULL)
700 DH_free(s->s3->tmp.dh);
706 memset(s->s3,0,sizeof *s->s3);
707 if (rp != NULL) s->s3->rbuf.buf=rp;
708 if (wp != NULL) s->s3->wbuf.buf=wp;
710 ssl_free_wbio_buffer(s);
713 s->s3->renegotiate=0;
714 s->s3->total_renegotiations=0;
715 s->s3->num_renegotiations=0;
716 s->s3->in_read_app_data=0;
717 s->version=SSL3_VERSION;
720 long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
724 #if !defined(NO_DSA) || !defined(NO_RSA)
727 cmd == SSL_CTRL_SET_TMP_RSA ||
728 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
731 cmd == SSL_CTRL_SET_TMP_DH ||
732 cmd == SSL_CTRL_SET_TMP_DH_CB ||
736 if (!ssl_cert_inst(&s->cert))
738 SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
746 case SSL_CTRL_GET_SESSION_REUSED:
749 case SSL_CTRL_GET_CLIENT_CERT_REQUEST:
751 case SSL_CTRL_GET_NUM_RENEGOTIATIONS:
752 ret=s->s3->num_renegotiations;
754 case SSL_CTRL_CLEAR_NUM_RENEGOTIATIONS:
755 ret=s->s3->num_renegotiations;
756 s->s3->num_renegotiations=0;
758 case SSL_CTRL_GET_TOTAL_RENEGOTIATIONS:
759 ret=s->s3->total_renegotiations;
761 case SSL_CTRL_GET_FLAGS:
762 ret=(int)(s->s3->flags);
765 case SSL_CTRL_NEED_TMP_RSA:
766 if ((s->cert != NULL) && (s->cert->rsa_tmp == NULL) &&
767 ((s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
768 (EVP_PKEY_size(s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8))))
771 case SSL_CTRL_SET_TMP_RSA:
773 RSA *rsa = (RSA *)parg;
776 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
779 if ((rsa = RSAPrivateKey_dup(rsa)) == NULL)
781 SSLerr(SSL_F_SSL3_CTRL, ERR_R_RSA_LIB);
784 if (s->cert->rsa_tmp != NULL)
785 RSA_free(s->cert->rsa_tmp);
786 s->cert->rsa_tmp = rsa;
790 case SSL_CTRL_SET_TMP_RSA_CB:
792 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
798 case SSL_CTRL_SET_TMP_DH:
803 SSLerr(SSL_F_SSL3_CTRL, ERR_R_PASSED_NULL_PARAMETER);
806 if ((dh = DHparams_dup(dh)) == NULL)
808 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
811 if (!(s->options & SSL_OP_SINGLE_DH_USE))
813 if (!DH_generate_key(dh))
816 SSLerr(SSL_F_SSL3_CTRL, ERR_R_DH_LIB);
820 if (s->cert->dh_tmp != NULL)
821 DH_free(s->cert->dh_tmp);
822 s->cert->dh_tmp = dh;
826 case SSL_CTRL_SET_TMP_DH_CB:
828 SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
839 long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)())
843 #if !defined(NO_DSA) || !defined(NO_RSA)
846 cmd == SSL_CTRL_SET_TMP_RSA_CB ||
849 cmd == SSL_CTRL_SET_TMP_DH_CB ||
853 if (!ssl_cert_inst(&s->cert))
855 SSLerr(SSL_F_SSL3_CALLBACK_CTRL, ERR_R_MALLOC_FAILURE);
864 case SSL_CTRL_SET_TMP_RSA_CB:
866 s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
871 case SSL_CTRL_SET_TMP_DH_CB:
873 s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
883 long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
892 case SSL_CTRL_NEED_TMP_RSA:
893 if ( (cert->rsa_tmp == NULL) &&
894 ((cert->pkeys[SSL_PKEY_RSA_ENC].privatekey == NULL) ||
895 (EVP_PKEY_size(cert->pkeys[SSL_PKEY_RSA_ENC].privatekey) > (512/8)))
901 case SSL_CTRL_SET_TMP_RSA:
912 if ((rsa=RSAPrivateKey_dup(rsa)) == NULL)
917 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_RSA_LIB);
922 if (cert->rsa_tmp != NULL)
923 RSA_free(cert->rsa_tmp);
929 case SSL_CTRL_SET_TMP_RSA_CB:
931 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
937 case SSL_CTRL_SET_TMP_DH:
942 if ((new=DHparams_dup(dh)) == NULL)
944 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
947 if (!(ctx->options & SSL_OP_SINGLE_DH_USE))
949 if (!DH_generate_key(new))
951 SSLerr(SSL_F_SSL3_CTX_CTRL,ERR_R_DH_LIB);
956 if (cert->dh_tmp != NULL)
957 DH_free(cert->dh_tmp);
962 case SSL_CTRL_SET_TMP_DH_CB:
964 SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
969 /* A Thawte special :-) */
970 case SSL_CTRL_EXTRA_CHAIN_CERT:
971 if (ctx->extra_certs == NULL)
973 if ((ctx->extra_certs=sk_X509_new_null()) == NULL)
976 sk_X509_push(ctx->extra_certs,(X509 *)parg);
985 long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
994 case SSL_CTRL_SET_TMP_RSA_CB:
996 cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
1001 case SSL_CTRL_SET_TMP_DH_CB:
1003 cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
1013 /* This function needs to check if the ciphers required are actually
1015 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
1018 static SSL_CIPHER *sorted[SSL3_NUM_CIPHERS];
1019 SSL_CIPHER c,*cp= &c,**cpp;
1025 CRYPTO_w_lock(CRYPTO_LOCK_SSL);
1027 for (i=0; i<SSL3_NUM_CIPHERS; i++)
1028 sorted[i]= &(ssl3_ciphers[i]);
1030 qsort( (char *)sorted,
1031 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1032 FP_ICC ssl_cipher_ptr_id_cmp);
1034 CRYPTO_w_unlock(CRYPTO_LOCK_SSL);
1039 id=0x03000000L|((unsigned long)p[0]<<8L)|(unsigned long)p[1];
1041 cpp=(SSL_CIPHER **)OBJ_bsearch((char *)&cp,
1043 SSL3_NUM_CIPHERS,sizeof(SSL_CIPHER *),
1044 FP_ICC ssl_cipher_ptr_id_cmp);
1045 if ((cpp == NULL) || !(*cpp)->valid)
1051 int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
1058 if ((l & 0xff000000) != 0x03000000) return(0);
1059 p[0]=((unsigned char)(l>> 8L))&0xFF;
1060 p[1]=((unsigned char)(l ))&0xFF;
1065 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *have,
1066 STACK_OF(SSL_CIPHER) *pref)
1068 SSL_CIPHER *c,*ret=NULL;
1071 unsigned long alg,mask,emask;
1073 /* Let's see which ciphers we can support */
1076 sk_SSL_CIPHER_set_cmp_func(pref,ssl_cipher_ptr_id_cmp);
1080 for(i=0 ; i < sk_num(pref) ; ++i)
1082 c=(SSL_CIPHER *)sk_value(pref,i);
1083 printf("%p:%s\n",c,c->name);
1087 for (i=0; i<sk_SSL_CIPHER_num(have); i++)
1089 c=sk_SSL_CIPHER_value(have,i);
1091 ssl_set_cert_masks(cert,c);
1093 emask=cert->export_mask;
1095 alg=c->algorithms&(SSL_MKEY_MASK|SSL_AUTH_MASK);
1096 if (SSL_C_IS_EXPORT(c))
1098 ok=((alg & emask) == alg)?1:0;
1100 printf("%d:[%08lX:%08lX]%p:%s (export)\n",ok,alg,emask,
1106 ok=((alg & mask) == alg)?1:0;
1108 printf("%d:[%08lX:%08lX]%p:%s\n",ok,alg,mask,c,
1115 j=sk_SSL_CIPHER_find(pref,c);
1118 ret=sk_SSL_CIPHER_value(pref,j);
1125 int ssl3_get_req_cert_type(SSL *s, unsigned char *p)
1130 alg=s->s3->tmp.new_cipher->algorithms;
1133 if (alg & (SSL_kDHr|SSL_kEDH))
1136 p[ret++]=SSL3_CT_RSA_FIXED_DH;
1139 p[ret++]=SSL3_CT_DSS_FIXED_DH;
1142 if ((s->version == SSL3_VERSION) &&
1143 (alg & (SSL_kEDH|SSL_kDHd|SSL_kDHr)))
1146 p[ret++]=SSL3_CT_RSA_EPHEMERAL_DH;
1149 p[ret++]=SSL3_CT_DSS_EPHEMERAL_DH;
1154 p[ret++]=SSL3_CT_RSA_SIGN;
1157 p[ret++]=SSL3_CT_DSS_SIGN;
1162 int ssl3_shutdown(SSL *s)
1165 /* Don't do anything much if we have not done the handshake or
1166 * we don't want to send messages :-) */
1167 if ((s->quiet_shutdown) || (s->state == SSL_ST_BEFORE))
1169 s->shutdown=(SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN);
1173 if (!(s->shutdown & SSL_SENT_SHUTDOWN))
1175 s->shutdown|=SSL_SENT_SHUTDOWN;
1177 ssl3_send_alert(s,SSL3_AL_WARNING,SSL_AD_CLOSE_NOTIFY);
1179 /* our shutdown alert has been sent now, and if it still needs
1180 * to be written, s->s3->alert_dispatch will be true */
1182 else if (s->s3->alert_dispatch)
1184 /* resend it if not sent */
1186 ssl3_dispatch_alert(s);
1189 else if (!(s->shutdown & SSL_RECEIVED_SHUTDOWN))
1191 /* If we are waiting for a close from our peer, we are closed */
1192 ssl3_read_bytes(s,0,NULL,0);
1195 if ((s->shutdown == (SSL_SENT_SHUTDOWN|SSL_RECEIVED_SHUTDOWN)) &&
1196 !s->s3->alert_dispatch)
1202 int ssl3_write(SSL *s, const void *buf, int len)
1207 if (s->shutdown & SSL_SEND_SHUTDOWN)
1209 s->rwstate=SSL_NOTHING;
1214 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1216 /* This is an experimental flag that sends the
1217 * last handshake message in the same packet as the first
1218 * use data - used to see if it helps the TCP protocol during
1219 * session-id reuse */
1220 /* The second test is because the buffer may have been removed */
1221 if ((s->s3->flags & SSL3_FLAGS_POP_BUFFER) && (s->wbio == s->bbio))
1223 /* First time through, we write into the buffer */
1224 if (s->s3->delay_buf_pop_ret == 0)
1226 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1228 if (ret <= 0) return(ret);
1230 s->s3->delay_buf_pop_ret=ret;
1233 s->rwstate=SSL_WRITING;
1234 n=BIO_flush(s->wbio);
1235 if (n <= 0) return(n);
1236 s->rwstate=SSL_NOTHING;
1238 /* We have flushed the buffer, so remove it */
1239 ssl_free_wbio_buffer(s);
1240 s->s3->flags&= ~SSL3_FLAGS_POP_BUFFER;
1242 ret=s->s3->delay_buf_pop_ret;
1243 s->s3->delay_buf_pop_ret=0;
1247 ret=ssl3_write_bytes(s,SSL3_RT_APPLICATION_DATA,
1249 if (ret <= 0) return(ret);
1255 int ssl3_read(SSL *s, void *buf, int len)
1260 if (s->s3->renegotiate) ssl3_renegotiate_check(s);
1261 s->s3->in_read_app_data=1;
1262 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1263 if ((ret == -1) && (s->s3->in_read_app_data == 0))
1265 /* ssl3_read_bytes decided to call s->handshake_func, which
1266 * called ssl3_read_bytes to read handshake data.
1267 * However, ssl3_read_bytes actually found application data
1268 * and thinks that application data makes sense here (signalled
1269 * by resetting 'in_read_app_data', strangely); so disable
1270 * handshake processing and try to read application data again. */
1272 ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
1276 s->s3->in_read_app_data=0;
1281 int ssl3_peek(SSL *s, char *buf, int len)
1287 if ((rr->length == 0) || (rr->type != SSL3_RT_APPLICATION_DATA))
1289 n=ssl3_read(s,buf,1);
1290 if (n <= 0) return(n);
1295 if ((unsigned int)len > rr->length)
1299 memcpy(buf,&(rr->data[rr->off]),(unsigned int)n);
1303 int ssl3_renegotiate(SSL *s)
1305 if (s->handshake_func == NULL)
1308 if (s->s3->flags & SSL3_FLAGS_NO_RENEGOTIATE_CIPHERS)
1311 s->s3->renegotiate=1;
1315 int ssl3_renegotiate_check(SSL *s)
1319 if (s->s3->renegotiate)
1321 if ( (s->s3->rbuf.left == 0) &&
1322 (s->s3->wbuf.left == 0) &&
1326 if we are the server, and we have sent a 'RENEGOTIATE' message, we
1327 need to go to SSL_ST_ACCEPT.
1330 s->state=SSL_ST_RENEGOTIATE;
1331 s->s3->renegotiate=0;
1332 s->s3->num_renegotiations++;
1333 s->s3->total_renegotiations++;