AEAD support in ssl/
This change allows AEADs to be used in ssl/ to implement SSL/TLS ciphersuites.
This commit is contained in:
parent
444b1d416b
commit
03614034e9
@ -623,7 +623,7 @@ static int client_master_key(SSL *s)
|
||||
if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
|
||||
{
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
|
||||
{
|
||||
ssl2_return_error(s,SSL2_PE_NO_CIPHER);
|
||||
SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
|
||||
|
@ -68,7 +68,7 @@ int ssl2_enc_init(SSL *s, int client)
|
||||
const EVP_MD *md;
|
||||
int num;
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
|
||||
{
|
||||
ssl2_return_error(s,SSL2_PE_NO_CIPHER);
|
||||
SSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
|
||||
|
@ -452,7 +452,7 @@ static int get_client_master_key(SSL *s)
|
||||
|
||||
is_export=SSL_C_IS_EXPORT(s->session->cipher);
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
|
||||
{
|
||||
ssl2_return_error(s,SSL2_PE_NO_CIPHER);
|
||||
SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
|
||||
|
@ -418,7 +418,13 @@ int ssl3_setup_key_block(SSL *s)
|
||||
if (s->s3->tmp.key_block_length != 0)
|
||||
return(1);
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL,&comp))
|
||||
if (!ssl_cipher_get_comp(s->session, &comp))
|
||||
{
|
||||
SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
|
||||
return(0);
|
||||
}
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL))
|
||||
{
|
||||
SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
|
||||
return(0);
|
||||
|
@ -775,7 +775,9 @@ static int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
|
||||
else
|
||||
eivlen = 0;
|
||||
}
|
||||
else
|
||||
else if (s->aead_write_ctx != NULL)
|
||||
eivlen = s->aead_write_ctx->variable_nonce_len;
|
||||
else
|
||||
eivlen = 0;
|
||||
|
||||
/* lets setup the record stuff. */
|
||||
|
15
ssl/ssl.h
15
ssl/ssl.h
@ -452,7 +452,9 @@ struct ssl_cipher_st
|
||||
unsigned long algorithm_ssl; /* (major) protocol version */
|
||||
|
||||
unsigned long algo_strength; /* strength and export flags */
|
||||
unsigned long algorithm2; /* Extra flags */
|
||||
unsigned long algorithm2; /* Extra flags. See SSL2_CF_* in ssl2.h
|
||||
and algorithm2 section in
|
||||
ssl_locl.h */
|
||||
int strength_bits; /* Number of bits really used */
|
||||
int alg_bits; /* Number of bits for algorithm */
|
||||
};
|
||||
@ -849,6 +851,9 @@ int SRP_generate_client_master_secret(SSL *s,unsigned char *master_key);
|
||||
|
||||
#endif
|
||||
|
||||
struct ssl_aead_ctx_st;
|
||||
typedef struct ssl_aead_ctx_st SSL_AEAD_CTX;
|
||||
|
||||
#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
|
||||
#define SSL_MAX_CERT_LIST_DEFAULT 1024*30 /* 30k max cert list :-) */
|
||||
#else
|
||||
@ -1395,6 +1400,9 @@ struct ssl_st
|
||||
/* These are the ones being used, the ones in SSL_SESSION are
|
||||
* the ones to be 'copied' into these ones */
|
||||
int mac_flags;
|
||||
SSL_AEAD_CTX *aead_read_ctx; /* AEAD context. If non-NULL, then
|
||||
|enc_read_ctx| and |read_hash| are
|
||||
ignored. */
|
||||
EVP_CIPHER_CTX *enc_read_ctx; /* cryptographic state */
|
||||
EVP_MD_CTX *read_hash; /* used for mac generation */
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
@ -1403,6 +1411,9 @@ struct ssl_st
|
||||
char *expand;
|
||||
#endif
|
||||
|
||||
SSL_AEAD_CTX *aead_write_ctx; /* AEAD context. If non-NULL, then
|
||||
|enc_write_ctx| and |write_hash| are
|
||||
ignored. */
|
||||
EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */
|
||||
EVP_MD_CTX *write_hash; /* used for mac generation */
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
@ -2677,8 +2688,10 @@ void ERR_load_SSL_strings(void);
|
||||
#define SSL_F_SSL_VERIFY_CERT_CHAIN 207
|
||||
#define SSL_F_SSL_WRITE 208
|
||||
#define SSL_F_TLS12_CHECK_PEER_SIGALG 333
|
||||
#define SSL_F_TLS1_AEAD_CTX_INIT 338
|
||||
#define SSL_F_TLS1_CERT_VERIFY_MAC 286
|
||||
#define SSL_F_TLS1_CHANGE_CIPHER_STATE 209
|
||||
#define SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD 339
|
||||
#define SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER 335
|
||||
#define SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT 274
|
||||
#define SSL_F_TLS1_ENC 210
|
||||
|
@ -536,6 +536,7 @@ typedef struct ssl3_state_st
|
||||
unsigned char *key_block;
|
||||
|
||||
const EVP_CIPHER *new_sym_enc;
|
||||
const EVP_AEAD *new_aead;
|
||||
const EVP_MD *new_hash;
|
||||
int new_mac_pkey_type;
|
||||
int new_mac_secret_size;
|
||||
|
@ -484,32 +484,72 @@ static void load_builtin_compressions(void)
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ssl_cipher_get_comp sets |comp| to the correct SSL_COMP for the given
|
||||
* session and returns 1. On error it returns 0. */
|
||||
int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp)
|
||||
{
|
||||
int i;
|
||||
|
||||
SSL_COMP ctmp;
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
load_builtin_compressions();
|
||||
#endif
|
||||
|
||||
*comp=NULL;
|
||||
ctmp.id=s->compress_meth;
|
||||
if (ssl_comp_methods != NULL)
|
||||
{
|
||||
i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
|
||||
if (i >= 0)
|
||||
*comp=sk_SSL_COMP_value(ssl_comp_methods,i);
|
||||
else
|
||||
*comp=NULL;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
|
||||
* for |s->cipher|. It returns 1 on success and 0 on error. */
|
||||
int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
|
||||
{
|
||||
const SSL_CIPHER *c = s->cipher;
|
||||
|
||||
*aead = NULL;
|
||||
|
||||
if (c == NULL)
|
||||
return 0;
|
||||
if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
|
||||
return 0;
|
||||
|
||||
switch (c->algorithm_enc)
|
||||
{
|
||||
#ifndef OPENSSL_NO_AES
|
||||
case SSL_AES128GCM:
|
||||
*aead = EVP_aead_aes_128_gcm();
|
||||
return 1;
|
||||
case SSL_AES256GCM:
|
||||
*aead = EVP_aead_aes_256_gcm();
|
||||
return 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
|
||||
const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size,SSL_COMP **comp)
|
||||
const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
|
||||
{
|
||||
int i;
|
||||
const SSL_CIPHER *c;
|
||||
|
||||
c=s->cipher;
|
||||
if (c == NULL) return(0);
|
||||
if (comp != NULL)
|
||||
{
|
||||
SSL_COMP ctmp;
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
load_builtin_compressions();
|
||||
#endif
|
||||
|
||||
*comp=NULL;
|
||||
ctmp.id=s->compress_meth;
|
||||
if (ssl_comp_methods != NULL)
|
||||
{
|
||||
i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
|
||||
if (i >= 0)
|
||||
*comp=sk_SSL_COMP_value(ssl_comp_methods,i);
|
||||
else
|
||||
*comp=NULL;
|
||||
}
|
||||
}
|
||||
/* This function doesn't deal with EVP_AEAD. See
|
||||
* |ssl_cipher_get_aead_evp|. */
|
||||
if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
|
||||
return(0);
|
||||
|
||||
if ((enc == NULL) || (md == NULL)) return(0);
|
||||
|
||||
|
@ -294,8 +294,10 @@ static ERR_STRING_DATA SSL_str_functs[]=
|
||||
{ERR_FUNC(SSL_F_SSL_VERIFY_CERT_CHAIN), "ssl_verify_cert_chain"},
|
||||
{ERR_FUNC(SSL_F_SSL_WRITE), "SSL_write"},
|
||||
{ERR_FUNC(SSL_F_TLS12_CHECK_PEER_SIGALG), "tls12_check_peer_sigalg"},
|
||||
{ERR_FUNC(SSL_F_TLS1_AEAD_CTX_INIT), "TLS1_AEAD_CTX_INIT"},
|
||||
{ERR_FUNC(SSL_F_TLS1_CERT_VERIFY_MAC), "tls1_cert_verify_mac"},
|
||||
{ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE), "tls1_change_cipher_state"},
|
||||
{ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD), "TLS1_CHANGE_CIPHER_STATE_AEAD"},
|
||||
{ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER), "TLS1_CHANGE_CIPHER_STATE_CIPHER"},
|
||||
{ERR_FUNC(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT), "TLS1_CHECK_SERVERHELLO_TLSEXT"},
|
||||
{ERR_FUNC(SSL_F_TLS1_ENC), "tls1_enc"},
|
||||
|
@ -3054,6 +3054,18 @@ void ssl_clear_cipher_ctx(SSL *s)
|
||||
OPENSSL_free(s->enc_write_ctx);
|
||||
s->enc_write_ctx=NULL;
|
||||
}
|
||||
if (s->aead_read_ctx != NULL)
|
||||
{
|
||||
EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
|
||||
OPENSSL_free(s->aead_read_ctx);
|
||||
s->aead_read_ctx = NULL;
|
||||
}
|
||||
if (s->aead_write_ctx != NULL)
|
||||
{
|
||||
EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
|
||||
OPENSSL_free(s->aead_write_ctx);
|
||||
s->aead_write_ctx = NULL;
|
||||
}
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
if (s->expand != NULL)
|
||||
{
|
||||
|
@ -379,6 +379,14 @@
|
||||
#define TLS1_STREAM_MAC 0x04
|
||||
|
||||
|
||||
/* SSL_CIPHER_ALGORITHM2_AEAD is a flag in SSL_CIPHER.algorithm2 which
|
||||
* indicates that the cipher is implemented via an EVP_AEAD. */
|
||||
#define SSL_CIPHER_ALGORITHM2_AEAD (1<<23)
|
||||
|
||||
/* SSL_CIPHER_AEAD_FIXED_NONCE_LEN returns the number of bytes of fixed nonce
|
||||
* for an SSL_CIPHER* with the SSL_CIPHER_ALGORITHM2_AEAD flag. */
|
||||
#define SSL_CIPHER_AEAD_FIXED_NONCE_LEN(ssl_cipher) \
|
||||
(((ssl_cipher->algorithm2 >> 24) & 0xf)*2)
|
||||
|
||||
/*
|
||||
* Export and cipher strength information. For each cipher we have to decide
|
||||
@ -739,6 +747,17 @@ typedef struct ssl3_enc_method
|
||||
*/
|
||||
#define SSL_ENC_FLAG_TLS1_2_CIPHERS 0x10
|
||||
|
||||
/* ssl_aead_ctx_st contains information about an AEAD that is being used to
|
||||
* encrypt an SSL connection. */
|
||||
struct ssl_aead_ctx_st
|
||||
{
|
||||
EVP_AEAD_CTX ctx;
|
||||
/* fixed_nonce contains any bytes of the nonce that are fixed for all
|
||||
* records. */
|
||||
unsigned char fixed_nonce[8];
|
||||
unsigned char fixed_nonce_len, variable_nonce_len, tag_len;
|
||||
};
|
||||
|
||||
#ifndef OPENSSL_NO_COMP
|
||||
/* Used for holding the relevant compression methods loaded into SSL_CTX */
|
||||
typedef struct ssl3_comp_st
|
||||
@ -991,8 +1010,10 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth,
|
||||
STACK_OF(SSL_CIPHER) **sorted,
|
||||
const char *rule_str, CERT *c);
|
||||
void ssl_update_cache(SSL *s, int mode);
|
||||
int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp);
|
||||
int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead);
|
||||
int ssl_cipher_get_evp(const SSL_SESSION *s,const EVP_CIPHER **enc,
|
||||
const EVP_MD **md,int *mac_pkey_type,int *mac_secret_size, SSL_COMP **comp);
|
||||
const EVP_MD **md,int *mac_pkey_type,int *mac_secret_size);
|
||||
int ssl_get_handshake_digest(int i,long *mask,const EVP_MD **md);
|
||||
int ssl_cipher_get_cert_index(const SSL_CIPHER *c);
|
||||
const SSL_CIPHER *ssl_get_cipher_by_char(SSL *ssl, const unsigned char *ptr);
|
||||
|
@ -218,7 +218,7 @@ int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
|
||||
{
|
||||
SSL_COMP *comp = NULL;
|
||||
|
||||
ssl_cipher_get_evp(x,NULL,NULL,NULL,NULL,&comp);
|
||||
ssl_cipher_get_comp(x, &comp);
|
||||
if (comp == NULL)
|
||||
{
|
||||
if (BIO_printf(bp,"\n Compression: %d",x->compress_meth) <= 0) goto err;
|
||||
|
264
ssl/t1_enc.c
264
ssl/t1_enc.c
@ -316,6 +316,66 @@ static int tls1_generate_key_block(SSL *s, unsigned char *km,
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It
|
||||
* returns 0 on malloc error. */
|
||||
static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx)
|
||||
{
|
||||
if (*aead_ctx != NULL)
|
||||
EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
|
||||
else
|
||||
{
|
||||
*aead_ctx = (SSL_AEAD_CTX*) OPENSSL_malloc(sizeof(SSL_AEAD_CTX));
|
||||
if (*aead_ctx == NULL)
|
||||
{
|
||||
SSLerr(SSL_F_TLS1_AEAD_CTX_INIT, ERR_R_MALLOC_FAILURE);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int tls1_change_cipher_state_aead(SSL *s, char is_read,
|
||||
const unsigned char *key, unsigned key_len,
|
||||
const unsigned char *iv, unsigned iv_len)
|
||||
{
|
||||
const EVP_AEAD *aead = s->s3->tmp.new_aead;
|
||||
SSL_AEAD_CTX *aead_ctx;
|
||||
|
||||
if (is_read)
|
||||
{
|
||||
if (!tls1_aead_ctx_init(&s->aead_read_ctx))
|
||||
return 0;
|
||||
aead_ctx = s->aead_read_ctx;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!tls1_aead_ctx_init(&s->aead_write_ctx))
|
||||
return 0;
|
||||
aead_ctx = s->aead_write_ctx;
|
||||
}
|
||||
|
||||
if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
|
||||
EVP_AEAD_DEFAULT_TAG_LENGTH, NULL /* engine */))
|
||||
return 0;
|
||||
if (iv_len > sizeof(aead_ctx->fixed_nonce))
|
||||
{
|
||||
SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
memcpy(aead_ctx->fixed_nonce, iv, iv_len);
|
||||
aead_ctx->fixed_nonce_len = iv_len;
|
||||
aead_ctx->variable_nonce_len = 8; /* always the case, currently. */
|
||||
if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len != EVP_AEAD_nonce_length(aead))
|
||||
{
|
||||
SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD, ERR_R_INTERNAL_ERROR);
|
||||
return 0;
|
||||
}
|
||||
aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* tls1_change_cipher_state_cipher performs the work needed to switch cipher
|
||||
* states when using EVP_CIPHER. The argument |is_read| is true iff this
|
||||
* function is being called due to reading, as opposed to writing, a
|
||||
@ -494,6 +554,7 @@ int tls1_change_cipher_state(SSL *s, int which)
|
||||
const unsigned char *client_write_key, *server_write_key, *key;
|
||||
const unsigned char *client_write_iv, *server_write_iv, *iv;
|
||||
const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc;
|
||||
const EVP_AEAD *aead = s->s3->tmp.new_aead;
|
||||
unsigned key_len, iv_len, mac_secret_len;
|
||||
const unsigned char *key_data;
|
||||
const char is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) != 0;
|
||||
@ -551,14 +612,22 @@ int tls1_change_cipher_state(SSL *s, int which)
|
||||
|
||||
mac_secret_len = s->s3->tmp.new_mac_secret_size;
|
||||
|
||||
key_len = EVP_CIPHER_key_length(cipher);
|
||||
if (is_export && key_len > SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher))
|
||||
key_len = SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher);
|
||||
|
||||
if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
|
||||
iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
|
||||
if (aead != NULL)
|
||||
{
|
||||
key_len = EVP_AEAD_key_length(aead);
|
||||
iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->s3->tmp.new_cipher);
|
||||
}
|
||||
else
|
||||
iv_len = EVP_CIPHER_iv_length(cipher);
|
||||
{
|
||||
key_len = EVP_CIPHER_key_length(cipher);
|
||||
if (is_export && key_len > SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher))
|
||||
key_len = SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher);
|
||||
|
||||
if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
|
||||
iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
|
||||
else
|
||||
iv_len = EVP_CIPHER_iv_length(cipher);
|
||||
}
|
||||
|
||||
key_data = s->s3->tmp.key_block;
|
||||
client_write_mac_secret = key_data; key_data += mac_secret_len;
|
||||
@ -587,12 +656,20 @@ int tls1_change_cipher_state(SSL *s, int which)
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys,
|
||||
mac_secret, mac_secret_len,
|
||||
key, key_len,
|
||||
iv, iv_len)) {
|
||||
return 0;
|
||||
}
|
||||
if (aead != NULL)
|
||||
{
|
||||
if (!tls1_change_cipher_state_aead(s, is_read,
|
||||
key, key_len, iv, iv_len))
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys,
|
||||
mac_secret, mac_secret_len,
|
||||
key, key_len,
|
||||
iv, iv_len))
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifdef OPENSSL_SSL_TRACE_CRYPTO
|
||||
if (s->msg_callback)
|
||||
@ -627,13 +704,14 @@ err:
|
||||
int tls1_setup_key_block(SSL *s)
|
||||
{
|
||||
unsigned char *p1,*p2=NULL;
|
||||
const EVP_CIPHER *c;
|
||||
const EVP_MD *hash;
|
||||
const EVP_CIPHER *c = NULL;
|
||||
const EVP_MD *hash = NULL;
|
||||
const EVP_AEAD *aead = NULL;
|
||||
int num;
|
||||
SSL_COMP *comp;
|
||||
int mac_type= NID_undef,mac_secret_size=0;
|
||||
int ret=0;
|
||||
int iv_len;
|
||||
unsigned key_len, iv_len;
|
||||
|
||||
#ifdef KSSL_DEBUG
|
||||
printf ("tls1_setup_key_block()\n");
|
||||
@ -642,22 +720,36 @@ int tls1_setup_key_block(SSL *s)
|
||||
if (s->s3->tmp.key_block_length != 0)
|
||||
return(1);
|
||||
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size,&comp))
|
||||
if (!ssl_cipher_get_comp(s->session, &comp))
|
||||
goto cipher_unavailable_err;
|
||||
|
||||
if (s->session->cipher &&
|
||||
(s->session->cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD))
|
||||
{
|
||||
SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
|
||||
return(0);
|
||||
if (!ssl_cipher_get_evp_aead(s->session, &aead))
|
||||
goto cipher_unavailable_err;
|
||||
key_len = EVP_AEAD_key_length(aead);
|
||||
iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size))
|
||||
goto cipher_unavailable_err;
|
||||
key_len = EVP_CIPHER_key_length(c);
|
||||
|
||||
if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
|
||||
iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
|
||||
else
|
||||
iv_len = EVP_CIPHER_iv_length(c);
|
||||
}
|
||||
|
||||
if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
|
||||
iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
|
||||
else
|
||||
iv_len = EVP_CIPHER_iv_length(c);
|
||||
|
||||
s->s3->tmp.new_aead=aead;
|
||||
s->s3->tmp.new_sym_enc=c;
|
||||
s->s3->tmp.new_hash=hash;
|
||||
s->s3->tmp.new_mac_pkey_type = mac_type;
|
||||
s->s3->tmp.new_mac_secret_size = mac_secret_size;
|
||||
num=EVP_CIPHER_key_length(c)+mac_secret_size+iv_len;
|
||||
|
||||
num=key_len+mac_secret_size+iv_len;
|
||||
num*=2;
|
||||
|
||||
ssl3_cleanup_key_block(s);
|
||||
@ -720,6 +812,10 @@ err:
|
||||
OPENSSL_free(p2);
|
||||
}
|
||||
return(ret);
|
||||
|
||||
cipher_unavailable_err:
|
||||
SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
|
||||
@ -738,6 +834,124 @@ int tls1_enc(SSL *s, int send)
|
||||
unsigned long l;
|
||||
int bs,i,j,k,pad=0,ret,mac_size=0;
|
||||
const EVP_CIPHER *enc;
|
||||
const SSL_AEAD_CTX *aead;
|
||||
|
||||
if (send)
|
||||
rec = &s->s3->wrec;
|
||||
else
|
||||
rec = &s->s3->rrec;
|
||||
|
||||
if (send)
|
||||
aead = s->aead_write_ctx;
|
||||
else
|
||||
aead = s->aead_read_ctx;
|
||||
|
||||
if (aead)
|
||||
{
|
||||
unsigned char ad[13], *seq, *in, *out, nonce[16];
|
||||
unsigned nonce_used;
|
||||
ssize_t n;
|
||||
|
||||
seq = send ? s->s3->write_sequence : s->s3->read_sequence;
|
||||
|
||||
if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
|
||||
{
|
||||
unsigned char dtlsseq[9], *p = dtlsseq;
|
||||
|
||||
s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
|
||||
memcpy(p, &seq[2], 6);
|
||||
memcpy(ad, dtlsseq, 8);
|
||||
}
|
||||
else
|
||||
{
|
||||
memcpy(ad, seq, 8);
|
||||
for (i=7; i>=0; i--) /* increment */
|
||||
{
|
||||
++seq[i];
|
||||
if (seq[i] != 0)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
ad[8] = rec->type;
|
||||
ad[9] = (unsigned char)(s->version>>8);
|
||||
ad[10] = (unsigned char)(s->version);
|
||||
|
||||
if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(nonce) ||
|
||||
aead->variable_nonce_len > 8)
|
||||
return -1; /* internal error - should never happen. */
|
||||
|
||||
memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
|
||||
nonce_used = aead->fixed_nonce_len;
|
||||
|
||||
if (send)
|
||||
{
|
||||
size_t len = rec->length;
|
||||
in = rec->input;
|
||||
out = rec->data;
|
||||
|
||||
/* When sending we use the sequence number as the
|
||||
* variable part of the nonce. */
|
||||
if (aead->variable_nonce_len > 8)
|
||||
return -1;
|
||||
memcpy(nonce + nonce_used, ad, aead->variable_nonce_len);
|
||||
nonce_used += aead->variable_nonce_len;
|
||||
|
||||
/* in do_ssl3_write, rec->input is moved forward by
|
||||
* variable_nonce_len in order to leave space for the
|
||||
* variable nonce. Thus we can copy the sequence number
|
||||
* bytes into place without overwriting any of the
|
||||
* plaintext. */
|
||||
memcpy(out, ad, aead->variable_nonce_len);
|
||||
len -= aead->variable_nonce_len;
|
||||
|
||||
ad[11] = len >> 8;
|
||||
ad[12] = len & 0xff;
|
||||
|
||||
n = EVP_AEAD_CTX_seal(&aead->ctx,
|
||||
out + aead->variable_nonce_len, len + aead->tag_len,
|
||||
nonce, nonce_used,
|
||||
in + aead->variable_nonce_len, len,
|
||||
ad, sizeof(ad));
|
||||
if (n >= 0)
|
||||
n += aead->variable_nonce_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* receive */
|
||||
size_t len = rec->length;
|
||||
|
||||
if (rec->data != rec->input)
|
||||
return -1; /* internal error - should never happen. */
|
||||
out = in = rec->input;
|
||||
|
||||
if (len < aead->variable_nonce_len)
|
||||
return 0;
|
||||
memcpy(nonce + nonce_used, in, aead->variable_nonce_len);
|
||||
nonce_used += aead->variable_nonce_len;
|
||||
|
||||
in += aead->variable_nonce_len;
|
||||
len -= aead->variable_nonce_len;
|
||||
out += aead->variable_nonce_len;
|
||||
|
||||
if (len < aead->tag_len)
|
||||
return 0;
|
||||
len -= aead->tag_len;
|
||||
|
||||
ad[11] = len >> 8;
|
||||
ad[12] = len & 0xff;
|
||||
|
||||
n = EVP_AEAD_CTX_open(&aead->ctx, out, len, nonce, nonce_used,
|
||||
in, len + aead->tag_len, ad, sizeof(ad));
|
||||
|
||||
rec->data = rec->input = out;
|
||||
}
|
||||
|
||||
if (n == -1)
|
||||
return -1;
|
||||
rec->length = n;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (send)
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user