| From dc8386dbb390f4b867019873cd072a5fe01ba4e9 Mon Sep 17 00:00:00 2001 |
| From: Adam Langley <agl@chromium.org> |
| Date: Thu, 25 Jul 2013 17:35:23 -0400 |
| Subject: [PATCH 41/50] aead_ssl_support. |
| |
| This change allows AEADs to be used in ssl/ to implement SSL/TLS |
| ciphersuites. |
| --- |
| ssl/s2_clnt.c | 2 +- |
| ssl/s2_enc.c | 2 +- |
| ssl/s2_srvr.c | 2 +- |
| ssl/s3_enc.c | 8 +- |
| ssl/s3_pkt.c | 4 +- |
| ssl/ssl.h | 15 +++- |
| ssl/ssl3.h | 1 + |
| ssl/ssl_ciph.c | 70 +++++++++++---- |
| ssl/ssl_err.c | 3 + |
| ssl/ssl_lib.c | 12 +++ |
| ssl/ssl_locl.h | 23 ++++- |
| ssl/ssl_txt.c | 2 +- |
| ssl/t1_enc.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++------ |
| 13 files changed, 356 insertions(+), 50 deletions(-) |
| |
| diff --git a/ssl/s2_clnt.c b/ssl/s2_clnt.c |
| index 03b6cf9..32adaf5 100644 |
| --- a/ssl/s2_clnt.c |
| +++ b/ssl/s2_clnt.c |
| @@ -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); |
| diff --git a/ssl/s2_enc.c b/ssl/s2_enc.c |
| index ff3395f..087c4a2 100644 |
| --- a/ssl/s2_enc.c |
| +++ b/ssl/s2_enc.c |
| @@ -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); |
| diff --git a/ssl/s2_srvr.c b/ssl/s2_srvr.c |
| index 9b1a6ac..9392921 100644 |
| --- a/ssl/s2_srvr.c |
| +++ b/ssl/s2_srvr.c |
| @@ -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); |
| diff --git a/ssl/s3_enc.c b/ssl/s3_enc.c |
| index e3cd4f0..191b86b 100644 |
| --- a/ssl/s3_enc.c |
| +++ b/ssl/s3_enc.c |
| @@ -397,7 +397,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); |
| diff --git a/ssl/s3_pkt.c b/ssl/s3_pkt.c |
| index 33bb78a..5038f6c 100644 |
| --- a/ssl/s3_pkt.c |
| +++ b/ssl/s3_pkt.c |
| @@ -790,7 +790,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. */ |
| diff --git a/ssl/ssl.h b/ssl/ssl.h |
| index 672f3eb..0644cbf 100644 |
| --- a/ssl/ssl.h |
| +++ b/ssl/ssl.h |
| @@ -406,7 +406,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 */ |
| }; |
| @@ -748,6 +750,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 |
| @@ -1294,6 +1299,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 |
| @@ -1302,6 +1310,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 |
| @@ -2437,8 +2448,10 @@ void ERR_load_SSL_strings(void); |
| #define SSL_F_SSL_USE_RSAPRIVATEKEY_FILE 206 |
| #define SSL_F_SSL_VERIFY_CERT_CHAIN 207 |
| #define SSL_F_SSL_WRITE 208 |
| +#define SSL_F_TLS1_AEAD_CTX_INIT 339 |
| #define SSL_F_TLS1_CERT_VERIFY_MAC 286 |
| #define SSL_F_TLS1_CHANGE_CIPHER_STATE 209 |
| +#define SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD 340 |
| #define SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER 338 |
| #define SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT 274 |
| #define SSL_F_TLS1_ENC 210 |
| diff --git a/ssl/ssl3.h b/ssl/ssl3.h |
| index a4f6d4a..6a5cdbe 100644 |
| --- a/ssl/ssl3.h |
| +++ b/ssl/ssl3.h |
| @@ -517,6 +517,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; |
| diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c |
| index 2966ddf..7e780cd 100644 |
| --- a/ssl/ssl_ciph.c |
| +++ b/ssl/ssl_ciph.c |
| @@ -484,32 +484,66 @@ 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; |
| + |
| +#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,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); |
| |
| diff --git a/ssl/ssl_err.c b/ssl/ssl_err.c |
| index 97b2a0d..ad3a7b9 100644 |
| --- a/ssl/ssl_err.c |
| +++ b/ssl/ssl_err.c |
| @@ -280,6 +280,9 @@ 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_TLS1_CERT_VERIFY_MAC), "tls1_cert_verify_mac"}, |
| +{ERR_FUNC(SSL_F_TLS1_AEAD_CTX_INIT), "TLS1_AEAD_CTX_INIT"}, |
| +{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"}, |
| diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c |
| index 3b264b6..8a0150c 100644 |
| --- a/ssl/ssl_lib.c |
| +++ b/ssl/ssl_lib.c |
| @@ -2881,6 +2881,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) |
| { |
| diff --git a/ssl/ssl_locl.h b/ssl/ssl_locl.h |
| index 3d800af..63bc28b 100644 |
| --- a/ssl/ssl_locl.h |
| +++ b/ssl/ssl_locl.h |
| @@ -380,6 +380,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 |
| @@ -588,6 +596,17 @@ typedef struct ssl3_enc_method |
| int use_context); |
| } SSL3_ENC_METHOD; |
| |
| +/* 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 |
| @@ -834,8 +853,10 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METHOD *meth, |
| STACK_OF(SSL_CIPHER) **sorted, |
| const char *rule_str); |
| 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_verify_cert_chain(SSL *s,STACK_OF(X509) *sk); |
| int ssl_undefined_function(SSL *s); |
| diff --git a/ssl/ssl_txt.c b/ssl/ssl_txt.c |
| index 6479d52..07826d5 100644 |
| --- a/ssl/ssl_txt.c |
| +++ b/ssl/ssl_txt.c |
| @@ -216,7 +216,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; |
| diff --git a/ssl/t1_enc.c b/ssl/t1_enc.c |
| index e1f91ba..7af1a32 100644 |
| --- a/ssl/t1_enc.c |
| +++ b/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; |
| + } |
| |
| return 1; |
| err: |
| @@ -603,13 +680,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"); |
| @@ -618,22 +696,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); |
| } |
| - |
| - 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 (!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); |
| + } |
| + |
| + 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); |
| @@ -696,6 +788,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. |
| @@ -714,6 +810,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) |
| { |
| -- |
| 1.8.4.1 |
| |