SSL AEAD support.

This change allows AEADs to be used in ssl/ to implement SSL/TLS
ciphersuites.
This commit is contained in:
Adam Langley 2014-06-20 12:00:00 -07:00
parent fd772a5aeb
commit c9fb37504f
10 changed files with 363 additions and 46 deletions

View File

@ -371,7 +371,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))
{
OPENSSL_PUT_ERROR(SSL, ssl3_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
return(0);
}
if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL))
{
OPENSSL_PUT_ERROR(SSL, ssl3_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
return(0);

View File

@ -765,7 +765,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. */

View File

@ -438,7 +438,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 */
};
@ -825,6 +827,9 @@ void SSL_set_msg_callback(SSL *ssl, void (*cb)(int write_p, int version, int con
#define SSL_set_msg_callback_arg(ssl, arg) SSL_ctrl((ssl), SSL_CTRL_SET_MSG_CALLBACK_ARG, 0, (arg))
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
@ -1404,10 +1409,16 @@ 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 */
char *expand;
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 */
char *compress;
@ -2658,6 +2669,8 @@ void ERR_load_SSL_strings(void);
#define SSL_F_ssl3_send_channel_id 276
#define SSL_F_SSL_CTX_set_cipher_list_tls11 277
#define SSL_F_tls1_change_cipher_state_cipher 278
#define SSL_F_tls1_change_cipher_state_aead 279
#define SSL_F_tls1_aead_ctx_init 280
#define SSL_R_UNABLE_TO_FIND_ECDH_PARAMETERS 100
#define SSL_R_DECRYPTION_FAILED_OR_BAD_RECORD_MAC 101
#define SSL_R_INVALID_NULL_CMD_NAME 102

View File

@ -529,6 +529,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;

View File

@ -350,29 +350,62 @@ void ssl_load_ciphers(void)
ssl_mac_secret_size[SSL_MD_SHA384_IDX]= EVP_MD_size(EVP_sha384());
}
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)
/* 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)
{
size_t index;
SSL_COMP ctmp;
*comp=NULL;
ctmp.id=s->compress_meth;
if (ssl_comp_methods != NULL)
{
if (sk_SSL_COMP_find(ssl_comp_methods, &index, &ctmp))
*comp=sk_SSL_COMP_value(ssl_comp_methods,index);
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;
#ifndef OPENSSL_NO_AES
/* There is only one AEAD for now. */
*aead = EVP_aead_aes_128_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)
{
size_t compression_index;
int i;
const SSL_CIPHER *c;
c=s->cipher;
if (c == NULL) return(0);
if (comp != NULL)
{
SSL_COMP ctmp;
*comp=NULL;
ctmp.id=s->compress_meth;
if (ssl_comp_methods != NULL)
{
if (sk_SSL_COMP_find(ssl_comp_methods, &compression_index, &ctmp))
*comp=sk_SSL_COMP_value(ssl_comp_methods, compression_index);
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);

View File

@ -188,8 +188,10 @@ const ERR_STRING_DATA SSL_error_string_data[] = {
{ERR_PACK(ERR_LIB_SSL, SSL_F_ssl_verify_cert_chain, 0), "ssl_verify_cert_chain"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls12_check_peer_sigalg, 0), "tls12_check_peer_sigalg"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_PRF, 0), "tls1_PRF"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_aead_ctx_init, 0), "tls1_aead_ctx_init"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_cert_verify_mac, 0), "tls1_cert_verify_mac"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_change_cipher_state, 0), "tls1_change_cipher_state"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_change_cipher_state_aead, 0), "tls1_change_cipher_state_aead"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_change_cipher_state_cipher, 0), "tls1_change_cipher_state_cipher"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_export_keying_material, 0), "tls1_export_keying_material"},
{ERR_PACK(ERR_LIB_SSL, SSL_F_tls1_get_server_supplemental_data, 0), "tls1_get_server_supplemental_data"},

View File

@ -2914,6 +2914,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;
}
}
X509 *SSL_get_certificate(const SSL *s)

View File

@ -149,6 +149,7 @@
#include <string.h>
#include <time.h>
#include <openssl/aead.h>
#include <openssl/bio.h>
#include <openssl/buf.h>
#include <openssl/dsa.h>
@ -367,6 +368,14 @@
#define TLSEXT_CHANNEL_ID_SIZE 128
/* 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
@ -729,6 +738,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
@ -978,8 +998,11 @@ 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_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);

View File

@ -133,7 +133,10 @@ int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
if (x->cipher == NULL)
{
if (((x->cipher_id) & 0xff000000) == 0x02000000)
SSL_COMP *comp = NULL;
ssl_cipher_get_comp(x, &comp);
if (comp == NULL)
{
if (BIO_printf(bp," Cipher : %06lX\n",x->cipher_id&0xffffff) <= 0)
goto err;

View File

@ -311,6 +311,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)
{
OPENSSL_PUT_ERROR(SSL, 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))
{
OPENSSL_PUT_ERROR(SSL, 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))
{
OPENSSL_PUT_ERROR(SSL, 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
@ -500,6 +560,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;
@ -513,14 +574,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;
@ -549,12 +618,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;
}
return 1;
}
@ -562,13 +639,14 @@ int tls1_change_cipher_state(SSL *s, int which)
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");
@ -577,22 +655,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))
{
OPENSSL_PUT_ERROR(SSL, 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);
@ -655,6 +747,10 @@ err:
OPENSSL_free(p2);
}
return(ret);
cipher_unavailable_err:
OPENSSL_PUT_ERROR(SSL, tls1_setup_key_block, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
return 0;
}
/* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
@ -673,6 +769,132 @@ 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;
size_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;
if (!EVP_AEAD_CTX_seal(
&aead->ctx,
out + aead->variable_nonce_len, &n, len + aead->tag_len,
nonce, nonce_used,
in + aead->variable_nonce_len, len,
ad, sizeof(ad)))
{
return -1;
}
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;
if (!EVP_AEAD_CTX_open(
&aead->ctx,
out, &n, len,
nonce, nonce_used,
in, len + aead->tag_len,
ad, sizeof(ad)))
{
return -1;
}
rec->data = rec->input = out;
}
rec->length = n;
return 1;
}
if (send)
{