Move ssl3_do_write from s3_pkt.c to s3_both.c.
[openssl.git] / ssl / s3_lib.c
index 8bcbe50f8022f21afa481bfc214a0ee8318a239b..87525faab496aca9496499e4296f484f4b8e8838 100644 (file)
@@ -608,18 +608,9 @@ static SSL_METHOD SSLv3_data= {
        ssl_bad_method,
        ssl3_default_timeout,
        &SSLv3_enc_data,
-       };
-
-union rsa_fn_to_char_u
-       {
-       char *char_p;
-       RSA *(*fn_p)(SSL *, int, int);
-       };
-
-union dh_fn_to_char_u
-       {
-       char *char_p;
-       DH *(*fn_p)(SSL *, int, int);
+       ssl_undefined_function,
+       ssl3_callback_ctrl,
+       ssl3_ctx_callback_ctrl,
        };
 
 static long ssl3_default_timeout(void)
@@ -655,19 +646,12 @@ int ssl3_pending(SSL *s)
 
 int ssl3_new(SSL *s)
        {
-       SSL3_CTX *s3;
+       SSL3_STATE *s3;
 
-       if ((s3=(SSL3_CTX *)Malloc(sizeof(SSL3_CTX))) == NULL) goto err;
-       memset(s3,0,sizeof(SSL3_CTX));
+       if ((s3=Malloc(sizeof *s3)) == NULL) goto err;
+       memset(s3,0,sizeof *s3);
 
        s->s3=s3;
-       /*
-       s->s3->tmp.ca_names=NULL;
-       s->s3->tmp.key_block=NULL;
-       s->s3->tmp.key_block_length=0;
-       s->s3->rbuf.buf=NULL;
-       s->s3->wbuf.buf=NULL;
-       */
 
        s->method->ssl_clear(s);
        return(1);
@@ -693,7 +677,7 @@ void ssl3_free(SSL *s)
 #endif
        if (s->s3->tmp.ca_names != NULL)
                sk_X509_NAME_pop_free(s->s3->tmp.ca_names,X509_NAME_free);
-       memset(s->s3,0,sizeof(SSL3_CTX));
+       memset(s->s3,0,sizeof *s->s3);
        Free(s->s3);
        s->s3=NULL;
        }
@@ -715,7 +699,7 @@ void ssl3_clear(SSL *s)
        rp=s->s3->rbuf.buf;
        wp=s->s3->wbuf.buf;
 
-       memset(s->s3,0,sizeof(SSL3_CTX));
+       memset(s->s3,0,sizeof *s->s3);
        if (rp != NULL) s->s3->rbuf.buf=rp;
        if (wp != NULL) s->s3->wbuf.buf=wp;
 
@@ -799,10 +783,8 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
                break;
        case SSL_CTRL_SET_TMP_RSA_CB:
                {
-               union rsa_fn_to_char_u rsa_tmp_cb;
-
-               rsa_tmp_cb.char_p = parg;
-               s->cert->rsa_tmp_cb = rsa_tmp_cb.fn_p;
+               SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return(ret);
                }
                break;
 #endif
@@ -831,10 +813,52 @@ long ssl3_ctrl(SSL *s, int cmd, long larg, char *parg)
                break;
        case SSL_CTRL_SET_TMP_DH_CB:
                {
-               union dh_fn_to_char_u dh_tmp_cb;
+               SSLerr(SSL_F_SSL3_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return(ret);
+               }
+               break;
+#endif
+       default:
+               break;
+               }
+       return(ret);
+       }
+
+long ssl3_callback_ctrl(SSL *s, int cmd, void (*fp)())
+       {
+       int ret=0;
+
+#if !defined(NO_DSA) || !defined(NO_RSA)
+       if (
+#ifndef NO_RSA
+           cmd == SSL_CTRL_SET_TMP_RSA_CB ||
+#endif
+#ifndef NO_DSA
+           cmd == SSL_CTRL_SET_TMP_DH_CB ||
+#endif
+               0)
+               {
+               if (!ssl_cert_inst(&s->cert))
+                       {
+                       SSLerr(SSL_F_SSL3_CTRL, ERR_R_MALLOC_FAILURE);
+                       return(0);
+                       }
+               }
+#endif
 
-               dh_tmp_cb.char_p = parg;
-               s->cert->dh_tmp_cb = dh_tmp_cb.fn_p;
+       switch (cmd)
+               {
+#ifndef NO_RSA
+       case SSL_CTRL_SET_TMP_RSA_CB:
+               {
+               s->cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
+               }
+               break;
+#endif
+#ifndef NO_DH
+       case SSL_CTRL_SET_TMP_DH_CB:
+               {
+               s->cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
                }
                break;
 #endif
@@ -892,10 +916,8 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
                /* break; */
        case SSL_CTRL_SET_TMP_RSA_CB:
                {
-               union rsa_fn_to_char_u rsa_tmp_cb;
-
-               rsa_tmp_cb.char_p = parg;
-               cert->rsa_tmp_cb = rsa_tmp_cb.fn_p;
+               SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return(0);
                }
                break;
 #endif
@@ -924,10 +946,8 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
                /*break; */
        case SSL_CTRL_SET_TMP_DH_CB:
                {
-               union dh_fn_to_char_u dh_tmp_cb;
-
-               dh_tmp_cb.char_p = parg;
-               cert->dh_tmp_cb = dh_tmp_cb.fn_p;
+               SSLerr(SSL_F_SSL3_CTX_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
+               return(0);
                }
                break;
 #endif
@@ -947,6 +967,34 @@ long ssl3_ctx_ctrl(SSL_CTX *ctx, int cmd, long larg, char *parg)
        return(1);
        }
 
+long ssl3_ctx_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)())
+       {
+       CERT *cert;
+
+       cert=ctx->cert;
+
+       switch (cmd)
+               {
+#ifndef NO_RSA
+       case SSL_CTRL_SET_TMP_RSA_CB:
+               {
+               cert->rsa_tmp_cb = (RSA *(*)(SSL *, int, int))fp;
+               }
+               break;
+#endif
+#ifndef NO_DH
+       case SSL_CTRL_SET_TMP_DH_CB:
+               {
+               cert->dh_tmp_cb = (DH *(*)(SSL *, int, int))fp;
+               }
+               break;
+#endif
+       default:
+               return(0);
+               }
+       return(1);
+       }
+
 /* This function needs to check if the ciphers required are actually
  * available */
 SSL_CIPHER *ssl3_get_cipher_by_char(const unsigned char *p)
@@ -999,21 +1047,6 @@ int ssl3_put_cipher_by_char(const SSL_CIPHER *c, unsigned char *p)
        return(2);
        }
 
-int ssl3_part_read(SSL *s, int i)
-       {
-       s->rwstate=SSL_READING;
-
-       if (i < 0)
-               {
-               return(i);
-               }
-       else
-               {
-               s->init_num+=i;
-               return(0);
-               }
-       }
-
 SSL_CIPHER *ssl3_choose_cipher(SSL *s, STACK_OF(SSL_CIPHER) *have,
             STACK_OF(SSL_CIPHER) *pref)
        {
@@ -1214,8 +1247,12 @@ int ssl3_read(SSL *s, void *buf, int len)
        ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
        if ((ret == -1) && (s->s3->in_read_app_data == 0))
                {
-               ERR_get_error(); /* clear the error */
-               s->s3->in_read_app_data=0;
+               /* ssl3_read_bytes decided to call s->handshake_func, which
+                * called ssl3_read_bytes to read handshake data.
+                * However, ssl3_read_bytes actually found application data
+                * and thinks that application data makes sense here (signalled
+                * by resetting 'in_read_app_data', strangely); so disable
+                * handshake processing and try to read application data again. */
                s->in_handshake++;
                ret=ssl3_read_bytes(s,SSL3_RT_APPLICATION_DATA,buf,len);
                s->in_handshake--;