Workarounds to make broken programs happy (such as s_client and s_server).
[openssl.git] / ssl / t1_enc.c
index 893c0bc..279e45d 100644 (file)
  */
 
 #include <stdio.h>
  */
 
 #include <stdio.h>
-#include "evp.h"
-#include "hmac.h"
+#include <openssl/comp.h>
+#include <openssl/md5.h>
+#include <openssl/sha.h>
+#include <openssl/evp.h>
+#include <openssl/hmac.h>
 #include "ssl_locl.h"
 
 #include "ssl_locl.h"
 
-static void tls1_P_hash(md,sec,sec_len,seed,seed_len,out,olen)
-EVP_MD *md;
-unsigned char *sec;
-int sec_len;
-unsigned char *seed;
-int seed_len;
-unsigned char *out;
-int olen;
+static void tls1_P_hash(const EVP_MD *md, const unsigned char *sec,
+                       int sec_len, unsigned char *seed, int seed_len,
+                       unsigned char *out, int olen)
        {
        int chunk,n;
        unsigned int j;
        {
        int chunk,n;
        unsigned int j;
@@ -110,19 +108,13 @@ int olen;
        memset(A1,0,sizeof(A1));
        }
 
        memset(A1,0,sizeof(A1));
        }
 
-static void tls1_PRF(md5,sha1,label,label_len,sec,slen,out1,out2,olen)
-EVP_MD *md5;
-EVP_MD *sha1;
-unsigned char *label;
-int label_len;
-unsigned char *sec;
-int slen;
-unsigned char *out1;
-unsigned char *out2;
-int olen;
+static void tls1_PRF(const EVP_MD *md5, const EVP_MD *sha1,
+                    unsigned char *label, int label_len,
+                    const unsigned char *sec, int slen, unsigned char *out1,
+                    unsigned char *out2, int olen)
        {
        int len,i;
        {
        int len,i;
-       unsigned char *S1,*S2;
+       const unsigned char *S1,*S2;
 
        len=slen/2;
        S1=sec;
 
        len=slen/2;
        S1=sec;
@@ -137,10 +129,8 @@ int olen;
                out1[i]^=out2[i];
        }
 
                out1[i]^=out2[i];
        }
 
-static void tls1_generate_key_block(s,km,tmp,num)
-SSL *s;
-unsigned char *km,*tmp;
-int num;
+static void tls1_generate_key_block(SSL *s, unsigned char *km,
+            unsigned char *tmp, int num)
        {
        unsigned char *p;
        unsigned char buf[SSL3_RANDOM_SIZE*2+
        {
        unsigned char *p;
        unsigned char buf[SSL3_RANDOM_SIZE*2+
@@ -156,14 +146,13 @@ int num;
        p+=SSL3_RANDOM_SIZE;
 
        tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
        p+=SSL3_RANDOM_SIZE;
 
        tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),
-               s->session->master_key,s->session->master_key_length,
-               km,tmp,num);
+                s->session->master_key,s->session->master_key_length,
+                km,tmp,num);
        }
 
        }
 
-int tls1_change_cipher_state(s,which)
-SSL *s;
-int which;
+int tls1_change_cipher_state(SSL *s, int which)
        {
        {
+       static const unsigned char empty[]="";
        unsigned char *p,*key_block,*mac_secret;
        unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
                SSL3_RANDOM_SIZE*2];
        unsigned char *p,*key_block,*mac_secret;
        unsigned char *exp_label,buf[TLS_MD_MAX_CONST_SIZE+
                SSL3_RANDOM_SIZE*2];
@@ -174,12 +163,12 @@ int which;
        unsigned char *ms,*key,*iv,*er1,*er2;
        int client_write;
        EVP_CIPHER_CTX *dd;
        unsigned char *ms,*key,*iv,*er1,*er2;
        int client_write;
        EVP_CIPHER_CTX *dd;
-       EVP_CIPHER *c;
-       COMP_METHOD *comp;
-       EVP_MD *m;
-       int exp,n,i,j,k,exp_label_len;
+       const EVP_CIPHER *c;
+       const SSL_COMP *comp;
+       const EVP_MD *m;
+       int _exp,n,i,j,k,exp_label_len,cl;
 
 
-       exp=(s->s3->tmp.new_cipher->algorithms & SSL_EXPORT)?1:0;
+       _exp=SSL_C_IS_EXPORT(s->s3->tmp.new_cipher);
        c=s->s3->tmp.new_sym_enc;
        m=s->s3->tmp.new_hash;
        comp=s->s3->tmp.new_compression;
        c=s->s3->tmp.new_sym_enc;
        m=s->s3->tmp.new_hash;
        comp=s->s3->tmp.new_compression;
@@ -200,14 +189,15 @@ int which;
                        }
                if (comp != NULL)
                        {
                        }
                if (comp != NULL)
                        {
-                       s->expand=COMP_CTX_new(comp);
+                       s->expand=COMP_CTX_new(comp->method);
                        if (s->expand == NULL)
                                {
                                SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
                                goto err2;
                                }
                        if (s->expand == NULL)
                                {
                                SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
                                goto err2;
                                }
-                       s->s3->rrec.comp=(unsigned char *)
-                               Malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
+                       if (s->s3->rrec.comp == NULL)
+                               s->s3->rrec.comp=(unsigned char *)
+                                       Malloc(SSL3_RT_MAX_ENCRYPTED_LENGTH);
                        if (s->s3->rrec.comp == NULL)
                                goto err;
                        }
                        if (s->s3->rrec.comp == NULL)
                                goto err;
                        }
@@ -229,7 +219,7 @@ int which;
                        }
                if (comp != NULL)
                        {
                        }
                if (comp != NULL)
                        {
-                       s->compress=COMP_CTX_new(comp);
+                       s->compress=COMP_CTX_new(comp->method);
                        if (s->compress == NULL)
                                {
                                SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
                        if (s->compress == NULL)
                                {
                                SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE,SSL_R_COMPRESSION_LIBRARY_ERROR);
@@ -244,7 +234,10 @@ int which;
 
        p=s->s3->tmp.key_block;
        i=EVP_MD_size(m);
 
        p=s->s3->tmp.key_block;
        i=EVP_MD_size(m);
-       j=(exp)?5:EVP_CIPHER_key_length(c);
+       cl=EVP_CIPHER_key_length(c);
+       j=_exp ? (cl < SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) ?
+                 cl : SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher)) : cl;
+       /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
        k=EVP_CIPHER_iv_length(c);
        er1= &(s->s3->client_random[0]);
        er2= &(s->s3->server_random[0]);
        k=EVP_CIPHER_iv_length(c);
        er1= &(s->s3->client_random[0]);
        er2= &(s->s3->server_random[0]);
@@ -280,7 +273,7 @@ int which;
 printf("which = %04X\nmac key=",which);
 { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
 #endif
 printf("which = %04X\nmac key=",which);
 { int z; for (z=0; z<i; z++) printf("%02X%c",ms[z],((z+1)%16)?' ':'\n'); }
 #endif
-       if (exp)
+       if (_exp)
                {
                /* In here I set both the read and write key/iv to the
                 * same value since only the correct one will be used :-).
                {
                /* In here I set both the read and write key/iv to the
                 * same value since only the correct one will be used :-).
@@ -293,7 +286,7 @@ printf("which = %04X\nmac key=",which);
                memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
                p+=SSL3_RANDOM_SIZE;
                tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
                memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
                p+=SSL3_RANDOM_SIZE;
                tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,(int)(p-buf),key,j,
-                       tmp1,tmp2,EVP_CIPHER_key_length(c));
+                        tmp1,tmp2,EVP_CIPHER_key_length(c));
                key=tmp1;
 
                if (k > 0)
                key=tmp1;
 
                if (k > 0)
@@ -306,8 +299,8 @@ printf("which = %04X\nmac key=",which);
                        p+=SSL3_RANDOM_SIZE;
                        memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
                        p+=SSL3_RANDOM_SIZE;
                        p+=SSL3_RANDOM_SIZE;
                        memcpy(p,s->s3->server_random,SSL3_RANDOM_SIZE);
                        p+=SSL3_RANDOM_SIZE;
-                       tls1_PRF(s->ctx->md5,s->ctx->sha1,
-                               buf,(int)(p-buf),"",0,iv1,iv2,k*2);
+                       tls1_PRF(s->ctx->md5,s->ctx->sha1,buf,p-buf,empty,0,
+                                iv1,iv2,k*2);
                        if (client_write)
                                iv=iv1;
                        else
                        if (client_write)
                                iv=iv1;
                        else
@@ -337,18 +330,18 @@ err2:
        return(0);
        }
 
        return(0);
        }
 
-int tls1_setup_key_block(s)
-SSL *s;
+int tls1_setup_key_block(SSL *s)
        {
        unsigned char *p1,*p2;
        {
        unsigned char *p1,*p2;
-       EVP_CIPHER *c;
-       EVP_MD *hash;
-       int num,exp;
+       const EVP_CIPHER *c;
+       const EVP_MD *hash;
+       int num;
+       SSL_COMP *comp;
 
        if (s->s3->tmp.key_block_length != 0)
                return(1);
 
 
        if (s->s3->tmp.key_block_length != 0)
                return(1);
 
-       if (!ssl_cipher_get_evp(s->session->cipher,&c,&hash))
+       if (!ssl_cipher_get_evp(s->session,&c,&hash,&comp))
                {
                SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
                return(0);
                {
                SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
                return(0);
@@ -357,8 +350,6 @@ SSL *s;
        s->s3->tmp.new_sym_enc=c;
        s->s3->tmp.new_hash=hash;
 
        s->s3->tmp.new_sym_enc=c;
        s->s3->tmp.new_hash=hash;
 
-       exp=(s->session->cipher->algorithms & SSL_EXPORT)?1:0;
-
        num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
        num*=2;
 
        num=EVP_CIPHER_key_length(c)+EVP_MD_size(hash)+EVP_CIPHER_iv_length(c);
        num*=2;
 
@@ -395,15 +386,13 @@ err:
        return(0);
        }
 
        return(0);
        }
 
-int tls1_enc(s,send)
-SSL *s;
-int send;
+int tls1_enc(SSL *s, int send)
        {
        SSL3_RECORD *rec;
        EVP_CIPHER_CTX *ds;
        unsigned long l;
        int bs,i,ii,j,k,n=0;
        {
        SSL3_RECORD *rec;
        EVP_CIPHER_CTX *ds;
        unsigned long l;
        int bs,i,ii,j,k,n=0;
-       EVP_CIPHER *enc;
+       const EVP_CIPHER *enc;
 
        if (send)
                {
 
        if (send)
                {
@@ -494,25 +483,18 @@ int send;
        return(1);
        }
 
        return(1);
        }
 
-int tls1_cert_verify_mac(s,in_ctx,out)
-SSL *s;
-EVP_MD_CTX *in_ctx;
-unsigned char *out;
+int tls1_cert_verify_mac(SSL *s, EVP_MD_CTX *in_ctx, unsigned char *out)
        {
        unsigned int ret;
        EVP_MD_CTX ctx;
 
        {
        unsigned int ret;
        EVP_MD_CTX ctx;
 
-       memcpy(&ctx,in_ctx,sizeof(EVP_MD_CTX));
+       EVP_MD_CTX_copy(&ctx,in_ctx);
        EVP_DigestFinal(&ctx,out,&ret);
        return((int)ret);
        }
 
        EVP_DigestFinal(&ctx,out,&ret);
        return((int)ret);
        }
 
-int tls1_final_finish_mac(s,in1_ctx,in2_ctx,str,slen,out)
-SSL *s;
-EVP_MD_CTX *in1_ctx,*in2_ctx;
-unsigned char *str;
-int slen;
-unsigned char *out;
+int tls1_final_finish_mac(SSL *s, EVP_MD_CTX *in1_ctx, EVP_MD_CTX *in2_ctx,
+            const char *str, int slen, unsigned char *out)
        {
        unsigned int i;
        EVP_MD_CTX ctx;
        {
        unsigned int i;
        EVP_MD_CTX ctx;
@@ -523,10 +505,10 @@ unsigned char *out;
        memcpy(q,str,slen);
        q+=slen;
 
        memcpy(q,str,slen);
        q+=slen;
 
-       memcpy(&ctx,in1_ctx,sizeof(EVP_MD_CTX));
+       EVP_MD_CTX_copy(&ctx,in1_ctx);
        EVP_DigestFinal(&ctx,q,&i);
        q+=i;
        EVP_DigestFinal(&ctx,q,&i);
        q+=i;
-       memcpy(&ctx,in2_ctx,sizeof(EVP_MD_CTX));
+       EVP_MD_CTX_copy(&ctx,in2_ctx);
        EVP_DigestFinal(&ctx,q,&i);
        q+=i;
 
        EVP_DigestFinal(&ctx,q,&i);
        q+=i;
 
@@ -538,14 +520,11 @@ unsigned char *out;
        return((int)12);
        }
 
        return((int)12);
        }
 
-int tls1_mac(ssl,md,send)
-SSL *ssl;
-unsigned char *md;
-int send;
+int tls1_mac(SSL *ssl, unsigned char *md, int send)
        {
        SSL3_RECORD *rec;
        unsigned char *mac_sec,*seq;
        {
        SSL3_RECORD *rec;
        unsigned char *mac_sec,*seq;
-       EVP_MD *hash;
+       const EVP_MD *hash;
        unsigned int md_size;
        int i;
        HMAC_CTX hmac;
        unsigned int md_size;
        int i;
        HMAC_CTX hmac;
@@ -601,11 +580,8 @@ printf("rec=");
        return(md_size);
        }
 
        return(md_size);
        }
 
-int tls1_generate_master_secret(s,out,p,len)
-SSL *s;
-unsigned char *out;
-unsigned char *p;
-int len;
+int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
+            int len)
        {
        unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
        unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
        {
        unsigned char buf[SSL3_RANDOM_SIZE*2+TLS_MD_MASTER_SECRET_CONST_SIZE];
        unsigned char buff[SSL_MAX_MASTER_KEY_LENGTH];
@@ -623,8 +599,7 @@ int len;
        return(SSL3_MASTER_SECRET_SIZE);
        }
 
        return(SSL3_MASTER_SECRET_SIZE);
        }
 
-int tls1_alert_code(code)
-int code;
+int tls1_alert_code(int code)
        {
        switch (code)
                {
        {
        switch (code)
                {
@@ -646,11 +621,11 @@ int code;
        case SSL_AD_ACCESS_DENIED:      return(TLS1_AD_ACCESS_DENIED);
        case SSL_AD_DECODE_ERROR:       return(TLS1_AD_DECODE_ERROR);
        case SSL_AD_DECRYPT_ERROR:      return(TLS1_AD_DECRYPT_ERROR);
        case SSL_AD_ACCESS_DENIED:      return(TLS1_AD_ACCESS_DENIED);
        case SSL_AD_DECODE_ERROR:       return(TLS1_AD_DECODE_ERROR);
        case SSL_AD_DECRYPT_ERROR:      return(TLS1_AD_DECRYPT_ERROR);
-       case SSL_AD_EXPORT_RESTRICION:  return(TLS1_AD_EXPORT_RESTRICION);
+       case SSL_AD_EXPORT_RESTRICTION: return(TLS1_AD_EXPORT_RESTRICTION);
        case SSL_AD_PROTOCOL_VERSION:   return(TLS1_AD_PROTOCOL_VERSION);
        case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
        case SSL_AD_INTERNAL_ERROR:     return(TLS1_AD_INTERNAL_ERROR);
        case SSL_AD_PROTOCOL_VERSION:   return(TLS1_AD_PROTOCOL_VERSION);
        case SSL_AD_INSUFFICIENT_SECURITY:return(TLS1_AD_INSUFFICIENT_SECURITY);
        case SSL_AD_INTERNAL_ERROR:     return(TLS1_AD_INTERNAL_ERROR);
-       case SSL_AD_USER_CANCLED:       return(TLS1_AD_USER_CANCLED);
+       case SSL_AD_USER_CANCELLED:     return(TLS1_AD_USER_CANCELLED);
        case SSL_AD_NO_RENEGOTIATION:   return(TLS1_AD_NO_RENEGOTIATION);
        default:                        return(-1);
                }
        case SSL_AD_NO_RENEGOTIATION:   return(TLS1_AD_NO_RENEGOTIATION);
        default:                        return(-1);
                }