Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: openssl/patches/aead_ssl_support.patch

Issue 59083010: third_party/openssl: add ChaCha20+Poly1305 support. Base URL: https://chromium.googlesource.com/chromium/deps/openssl.git@master
Patch Set: Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « openssl/openssl.config ('k') | openssl/patches/aead_support.patch » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 From dc8386dbb390f4b867019873cd072a5fe01ba4e9 Mon Sep 17 00:00:00 2001
2 From: Adam Langley <agl@chromium.org>
3 Date: Thu, 25 Jul 2013 17:35:23 -0400
4 Subject: [PATCH 41/50] aead_ssl_support.
5
6 This change allows AEADs to be used in ssl/ to implement SSL/TLS
7 ciphersuites.
8 ---
9 ssl/s2_clnt.c | 2 +-
10 ssl/s2_enc.c | 2 +-
11 ssl/s2_srvr.c | 2 +-
12 ssl/s3_enc.c | 8 +-
13 ssl/s3_pkt.c | 4 +-
14 ssl/ssl.h | 15 +++-
15 ssl/ssl3.h | 1 +
16 ssl/ssl_ciph.c | 70 +++++++++++----
17 ssl/ssl_err.c | 3 +
18 ssl/ssl_lib.c | 12 +++
19 ssl/ssl_locl.h | 23 ++++-
20 ssl/ssl_txt.c | 2 +-
21 ssl/t1_enc.c | 262 +++++++++++++++++++++++++++++++++++++++++++++++++++------
22 13 files changed, 356 insertions(+), 50 deletions(-)
23
24 diff --git a/ssl/s2_clnt.c b/ssl/s2_clnt.c
25 index 03b6cf9..32adaf5 100644
26 --- a/ssl/s2_clnt.c
27 +++ b/ssl/s2_clnt.c
28 @@ -623,7 +623,7 @@ static int client_master_key(SSL *s)
29 if (s->state == SSL2_ST_SEND_CLIENT_MASTER_KEY_A)
30 {
31
32 - if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
33 + if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
34 {
35 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
36 SSLerr(SSL_F_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CI PHER_FUNCTIONS);
37 diff --git a/ssl/s2_enc.c b/ssl/s2_enc.c
38 index ff3395f..087c4a2 100644
39 --- a/ssl/s2_enc.c
40 +++ b/ssl/s2_enc.c
41 @@ -68,7 +68,7 @@ int ssl2_enc_init(SSL *s, int client)
42 const EVP_MD *md;
43 int num;
44
45 - if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
46 + if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
47 {
48 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
49 SSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIO NS);
50 diff --git a/ssl/s2_srvr.c b/ssl/s2_srvr.c
51 index 9b1a6ac..9392921 100644
52 --- a/ssl/s2_srvr.c
53 +++ b/ssl/s2_srvr.c
54 @@ -452,7 +452,7 @@ static int get_client_master_key(SSL *s)
55
56 is_export=SSL_C_IS_EXPORT(s->session->cipher);
57
58 - if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL,NULL))
59 + if (!ssl_cipher_get_evp(s->session,&c,&md,NULL,NULL))
60 {
61 ssl2_return_error(s,SSL2_PE_NO_CIPHER);
62 SSLerr(SSL_F_GET_CLIENT_MASTER_KEY,SSL_R_PROBLEMS_MAPPING_CIPHER _FUNCTIONS);
63 diff --git a/ssl/s3_enc.c b/ssl/s3_enc.c
64 index e3cd4f0..191b86b 100644
65 --- a/ssl/s3_enc.c
66 +++ b/ssl/s3_enc.c
67 @@ -397,7 +397,13 @@ int ssl3_setup_key_block(SSL *s)
68 if (s->s3->tmp.key_block_length != 0)
69 return(1);
70
71 - if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL,&comp))
72 + if (!ssl_cipher_get_comp(s->session, &comp))
73 + {
74 + SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILAB LE);
75 + return(0);
76 + }
77 +
78 + if (!ssl_cipher_get_evp(s->session,&c,&hash,NULL,NULL))
79 {
80 SSLerr(SSL_F_SSL3_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILAB LE);
81 return(0);
82 diff --git a/ssl/s3_pkt.c b/ssl/s3_pkt.c
83 index 33bb78a..5038f6c 100644
84 --- a/ssl/s3_pkt.c
85 +++ b/ssl/s3_pkt.c
86 @@ -790,7 +790,9 @@ static int do_ssl3_write(SSL *s, int type, const unsigned ch ar *buf,
87 else
88 eivlen = 0;
89 }
90 - else
91 + else if (s->aead_write_ctx != NULL)
92 + eivlen = s->aead_write_ctx->variable_nonce_len;
93 + else
94 eivlen = 0;
95
96 /* lets setup the record stuff. */
97 diff --git a/ssl/ssl.h b/ssl/ssl.h
98 index 672f3eb..0644cbf 100644
99 --- a/ssl/ssl.h
100 +++ b/ssl/ssl.h
101 @@ -406,7 +406,9 @@ struct ssl_cipher_st
102 unsigned long algorithm_ssl; /* (major) protocol version */
103
104 unsigned long algo_strength; /* strength and export flags */
105 - unsigned long algorithm2; /* Extra flags */
106 + unsigned long algorithm2; /* Extra flags. See SSL2_CF_* in ssl2.h
107 + and algorithm2 section in
108 + ssl_locl.h */
109 int strength_bits; /* Number of bits really used */
110 int alg_bits; /* Number of bits for algorithm */
111 };
112 @@ -748,6 +750,9 @@ int SRP_generate_client_master_secret(SSL *s,unsigned char * master_key);
113
114 #endif
115
116 +struct ssl_aead_ctx_st;
117 +typedef struct ssl_aead_ctx_st SSL_AEAD_CTX;
118 +
119 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN32)
120 #define SSL_MAX_CERT_LIST_DEFAULT 1024*30 /* 30k max cert list :-) */
121 #else
122 @@ -1294,6 +1299,9 @@ struct ssl_st
123 /* These are the ones being used, the ones in SSL_SESSION are
124 * the ones to be 'copied' into these ones */
125 int mac_flags;
126 + SSL_AEAD_CTX *aead_read_ctx; /* AEAD context. If non-NULL, then
127 + |enc_read_ctx| and |read_hash| are
128 + ignored. */
129 EVP_CIPHER_CTX *enc_read_ctx; /* cryptographic state */
130 EVP_MD_CTX *read_hash; /* used for mac generation */
131 #ifndef OPENSSL_NO_COMP
132 @@ -1302,6 +1310,9 @@ struct ssl_st
133 char *expand;
134 #endif
135
136 + SSL_AEAD_CTX *aead_write_ctx; /* AEAD context. If non-NULL, then
137 + |enc_write_ctx| and |write_hash| are
138 + ignored. */
139 EVP_CIPHER_CTX *enc_write_ctx; /* cryptographic state */
140 EVP_MD_CTX *write_hash; /* used for mac generation */
141 #ifndef OPENSSL_NO_COMP
142 @@ -2437,8 +2448,10 @@ void ERR_load_SSL_strings(void);
143 #define SSL_F_SSL_USE_RSAPRIVATEKEY_FILE 206
144 #define SSL_F_SSL_VERIFY_CERT_CHAIN 207
145 #define SSL_F_SSL_WRITE 208
146 +#define SSL_F_TLS1_AEAD_CTX_INIT 339
147 #define SSL_F_TLS1_CERT_VERIFY_MAC 286
148 #define SSL_F_TLS1_CHANGE_CIPHER_STATE 209
149 +#define SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD 340
150 #define SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER 338
151 #define SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT 274
152 #define SSL_F_TLS1_ENC 210
153 diff --git a/ssl/ssl3.h b/ssl/ssl3.h
154 index a4f6d4a..6a5cdbe 100644
155 --- a/ssl/ssl3.h
156 +++ b/ssl/ssl3.h
157 @@ -517,6 +517,7 @@ typedef struct ssl3_state_st
158 unsigned char *key_block;
159
160 const EVP_CIPHER *new_sym_enc;
161 + const EVP_AEAD *new_aead;
162 const EVP_MD *new_hash;
163 int new_mac_pkey_type;
164 int new_mac_secret_size;
165 diff --git a/ssl/ssl_ciph.c b/ssl/ssl_ciph.c
166 index 2966ddf..7e780cd 100644
167 --- a/ssl/ssl_ciph.c
168 +++ b/ssl/ssl_ciph.c
169 @@ -484,32 +484,66 @@ static void load_builtin_compressions(void)
170 }
171 #endif
172
173 +/* ssl_cipher_get_comp sets |comp| to the correct SSL_COMP for the given
174 + * session and returns 1. On error it returns 0. */
175 +int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp)
176 + {
177 + int i;
178 +
179 + SSL_COMP ctmp;
180 +#ifndef OPENSSL_NO_COMP
181 + load_builtin_compressions();
182 +#endif
183 +
184 + *comp=NULL;
185 + ctmp.id=s->compress_meth;
186 + if (ssl_comp_methods != NULL)
187 + {
188 + i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
189 + if (i >= 0)
190 + *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
191 + else
192 + *comp=NULL;
193 + }
194 +
195 + return 1;
196 + }
197 +
198 +/* ssl_cipher_get_evp_aead sets |*aead| to point to the correct EVP_AEAD object
199 + * for |s->cipher|. It returns 1 on success and 0 on error. */
200 +int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead)
201 + {
202 + const SSL_CIPHER *c = s->cipher;
203 +
204 + *aead = NULL;
205 +
206 + if (c == NULL)
207 + return 0;
208 + if ((c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD) == 0)
209 + return 0;
210 +
211 +#ifndef OPENSSL_NO_AES
212 + /* There is only one AEAD for now. */
213 + *aead = EVP_aead_aes_128_gcm();
214 + return 1;
215 +#endif
216 +
217 + return 0;
218 + }
219 +
220 int ssl_cipher_get_evp(const SSL_SESSION *s, const EVP_CIPHER **enc,
221 - const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size,SSL_COM P **comp)
222 + const EVP_MD **md, int *mac_pkey_type, int *mac_secret_size)
223 {
224 int i;
225 const SSL_CIPHER *c;
226
227 c=s->cipher;
228 if (c == NULL) return(0);
229 - if (comp != NULL)
230 - {
231 - SSL_COMP ctmp;
232 -#ifndef OPENSSL_NO_COMP
233 - load_builtin_compressions();
234 -#endif
235
236 - *comp=NULL;
237 - ctmp.id=s->compress_meth;
238 - if (ssl_comp_methods != NULL)
239 - {
240 - i=sk_SSL_COMP_find(ssl_comp_methods,&ctmp);
241 - if (i >= 0)
242 - *comp=sk_SSL_COMP_value(ssl_comp_methods,i);
243 - else
244 - *comp=NULL;
245 - }
246 - }
247 + /* This function doesn't deal with EVP_AEAD. See
248 + * |ssl_cipher_get_aead_evp|. */
249 + if (c->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD)
250 + return(0);
251
252 if ((enc == NULL) || (md == NULL)) return(0);
253
254 diff --git a/ssl/ssl_err.c b/ssl/ssl_err.c
255 index 97b2a0d..ad3a7b9 100644
256 --- a/ssl/ssl_err.c
257 +++ b/ssl/ssl_err.c
258 @@ -280,6 +280,9 @@ static ERR_STRING_DATA SSL_str_functs[]=
259 {ERR_FUNC(SSL_F_SSL_VERIFY_CERT_CHAIN), "SSL_VERIFY_CERT_CHAIN"},
260 {ERR_FUNC(SSL_F_SSL_WRITE), "SSL_write"},
261 {ERR_FUNC(SSL_F_TLS1_CERT_VERIFY_MAC), "tls1_cert_verify_mac"},
262 +{ERR_FUNC(SSL_F_TLS1_AEAD_CTX_INIT), "TLS1_AEAD_CTX_INIT"},
263 +{ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE), "tls1_change_cipher_state"},
264 +{ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD), "TLS1_CHANGE_CIPHER_STAT E_AEAD"},
265 {ERR_FUNC(SSL_F_TLS1_CHANGE_CIPHER_STATE_CIPHER), "TLS1_CHANGE_CIPHER_STAT E_CIPHER"},
266 {ERR_FUNC(SSL_F_TLS1_CHECK_SERVERHELLO_TLSEXT), "TLS1_CHECK_SERVERHELLO_ TLSEXT"},
267 {ERR_FUNC(SSL_F_TLS1_ENC), "TLS1_ENC"},
268 diff --git a/ssl/ssl_lib.c b/ssl/ssl_lib.c
269 index 3b264b6..8a0150c 100644
270 --- a/ssl/ssl_lib.c
271 +++ b/ssl/ssl_lib.c
272 @@ -2881,6 +2881,18 @@ void ssl_clear_cipher_ctx(SSL *s)
273 OPENSSL_free(s->enc_write_ctx);
274 s->enc_write_ctx=NULL;
275 }
276 + if (s->aead_read_ctx != NULL)
277 + {
278 + EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
279 + OPENSSL_free(s->aead_read_ctx);
280 + s->aead_read_ctx = NULL;
281 + }
282 + if (s->aead_write_ctx != NULL)
283 + {
284 + EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
285 + OPENSSL_free(s->aead_write_ctx);
286 + s->aead_write_ctx = NULL;
287 + }
288 #ifndef OPENSSL_NO_COMP
289 if (s->expand != NULL)
290 {
291 diff --git a/ssl/ssl_locl.h b/ssl/ssl_locl.h
292 index 3d800af..63bc28b 100644
293 --- a/ssl/ssl_locl.h
294 +++ b/ssl/ssl_locl.h
295 @@ -380,6 +380,14 @@
296
297 #define TLSEXT_CHANNEL_ID_SIZE 128
298
299 +/* SSL_CIPHER_ALGORITHM2_AEAD is a flag in SSL_CIPHER.algorithm2 which
300 + * indicates that the cipher is implemented via an EVP_AEAD. */
301 +#define SSL_CIPHER_ALGORITHM2_AEAD (1<<23)
302 +
303 +/* SSL_CIPHER_AEAD_FIXED_NONCE_LEN returns the number of bytes of fixed nonce
304 + * for an SSL_CIPHER* with the SSL_CIPHER_ALGORITHM2_AEAD flag. */
305 +#define SSL_CIPHER_AEAD_FIXED_NONCE_LEN(ssl_cipher) \
306 + (((ssl_cipher->algorithm2 >> 24) & 0xf)*2)
307
308 /*
309 * Export and cipher strength information. For each cipher we have to decide
310 @@ -588,6 +596,17 @@ typedef struct ssl3_enc_method
311 int use_context);
312 } SSL3_ENC_METHOD;
313
314 +/* ssl_aead_ctx_st contains information about an AEAD that is being used to
315 + * encrypt an SSL connection. */
316 +struct ssl_aead_ctx_st
317 + {
318 + EVP_AEAD_CTX ctx;
319 + /* fixed_nonce contains any bytes of the nonce that are fixed for all
320 + * records. */
321 + unsigned char fixed_nonce[8];
322 + unsigned char fixed_nonce_len, variable_nonce_len, tag_len;
323 + };
324 +
325 #ifndef OPENSSL_NO_COMP
326 /* Used for holding the relevant compression methods loaded into SSL_CTX */
327 typedef struct ssl3_comp_st
328 @@ -834,8 +853,10 @@ STACK_OF(SSL_CIPHER) *ssl_create_cipher_list(const SSL_METH OD *meth,
329 STACK_OF(SSL_CIPHER) **sorted,
330 const char *rule_str);
331 void ssl_update_cache(SSL *s, int mode);
332 +int ssl_cipher_get_comp(const SSL_SESSION *s, SSL_COMP **comp);
333 +int ssl_cipher_get_evp_aead(const SSL_SESSION *s, const EVP_AEAD **aead);
334 int ssl_cipher_get_evp(const SSL_SESSION *s,const EVP_CIPHER **enc,
335 - const EVP_MD **md,int *mac_pkey_type,int *mac_secret_size , SSL_COMP **comp);
336 + const EVP_MD **md,int *mac_pkey_type,int *mac_secret_size );
337 int ssl_get_handshake_digest(int i,long *mask,const EVP_MD **md);
338 int ssl_verify_cert_chain(SSL *s,STACK_OF(X509) *sk);
339 int ssl_undefined_function(SSL *s);
340 diff --git a/ssl/ssl_txt.c b/ssl/ssl_txt.c
341 index 6479d52..07826d5 100644
342 --- a/ssl/ssl_txt.c
343 +++ b/ssl/ssl_txt.c
344 @@ -216,7 +216,7 @@ int SSL_SESSION_print(BIO *bp, const SSL_SESSION *x)
345 {
346 SSL_COMP *comp = NULL;
347
348 - ssl_cipher_get_evp(x,NULL,NULL,NULL,NULL,&comp);
349 + ssl_cipher_get_comp(x, &comp);
350 if (comp == NULL)
351 {
352 if (BIO_printf(bp,"\n Compression: %d",x->compress_me th) <= 0) goto err;
353 diff --git a/ssl/t1_enc.c b/ssl/t1_enc.c
354 index e1f91ba..7af1a32 100644
355 --- a/ssl/t1_enc.c
356 +++ b/ssl/t1_enc.c
357 @@ -316,6 +316,66 @@ static int tls1_generate_key_block(SSL *s, unsigned char *k m,
358 return ret;
359 }
360
361 +/* tls1_aead_ctx_init allocates |*aead_ctx|, if needed and returns 1. It
362 + * returns 0 on malloc error. */
363 +static int tls1_aead_ctx_init(SSL_AEAD_CTX **aead_ctx)
364 + {
365 + if (*aead_ctx != NULL)
366 + EVP_AEAD_CTX_cleanup(&(*aead_ctx)->ctx);
367 + else
368 + {
369 + *aead_ctx = (SSL_AEAD_CTX*) OPENSSL_malloc(sizeof(SSL_AEAD_CTX)) ;
370 + if (*aead_ctx == NULL)
371 + {
372 + SSLerr(SSL_F_TLS1_AEAD_CTX_INIT, ERR_R_MALLOC_FAILURE);
373 + return 0;
374 + }
375 + }
376 +
377 + return 1;
378 + }
379 +
380 +static int tls1_change_cipher_state_aead(SSL *s, char is_read,
381 + const unsigned char *key, unsigned key_len,
382 + const unsigned char *iv, unsigned iv_len)
383 + {
384 + const EVP_AEAD *aead = s->s3->tmp.new_aead;
385 + SSL_AEAD_CTX *aead_ctx;
386 +
387 + if (is_read)
388 + {
389 + if (!tls1_aead_ctx_init(&s->aead_read_ctx))
390 + return 0;
391 + aead_ctx = s->aead_read_ctx;
392 + }
393 + else
394 + {
395 + if (!tls1_aead_ctx_init(&s->aead_write_ctx))
396 + return 0;
397 + aead_ctx = s->aead_write_ctx;
398 + }
399 +
400 + if (!EVP_AEAD_CTX_init(&aead_ctx->ctx, aead, key, key_len,
401 + EVP_AEAD_DEFAULT_TAG_LENGTH, NULL /* engine */))
402 + return 0;
403 + if (iv_len > sizeof(aead_ctx->fixed_nonce))
404 + {
405 + SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD, ERR_R_INTERNAL_ERROR );
406 + return 0;
407 + }
408 + memcpy(aead_ctx->fixed_nonce, iv, iv_len);
409 + aead_ctx->fixed_nonce_len = iv_len;
410 + aead_ctx->variable_nonce_len = 8; /* always the case, currently. */
411 + if (aead_ctx->variable_nonce_len + aead_ctx->fixed_nonce_len != EVP_AEAD _nonce_length(aead))
412 + {
413 + SSLerr(SSL_F_TLS1_CHANGE_CIPHER_STATE_AEAD, ERR_R_INTERNAL_ERROR );
414 + return 0;
415 + }
416 + aead_ctx->tag_len = EVP_AEAD_max_overhead(aead);
417 +
418 + return 1;
419 + }
420 +
421 /* tls1_change_cipher_state_cipher performs the work needed to switch cipher
422 * states when using EVP_CIPHER. The argument |is_read| is true iff this
423 * function is being called due to reading, as opposed to writing, a
424 @@ -494,6 +554,7 @@ int tls1_change_cipher_state(SSL *s, int which)
425 const unsigned char *client_write_key, *server_write_key, *key;
426 const unsigned char *client_write_iv, *server_write_iv, *iv;
427 const EVP_CIPHER *cipher = s->s3->tmp.new_sym_enc;
428 + const EVP_AEAD *aead = s->s3->tmp.new_aead;
429 unsigned key_len, iv_len, mac_secret_len;
430 const unsigned char *key_data;
431 const char is_export = SSL_C_IS_EXPORT(s->s3->tmp.new_cipher) != 0;
432 @@ -551,14 +612,22 @@ int tls1_change_cipher_state(SSL *s, int which)
433
434 mac_secret_len = s->s3->tmp.new_mac_secret_size;
435
436 - key_len = EVP_CIPHER_key_length(cipher);
437 - if (is_export && key_len > SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher) )
438 - key_len = SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher);
439 -
440 - if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
441 - iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
442 + if (aead != NULL)
443 + {
444 + key_len = EVP_AEAD_key_length(aead);
445 + iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->s3->tmp.new_cipher);
446 + }
447 else
448 - iv_len = EVP_CIPHER_iv_length(cipher);
449 + {
450 + key_len = EVP_CIPHER_key_length(cipher);
451 + if (is_export && key_len > SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new _cipher))
452 + key_len = SSL_C_EXPORT_KEYLENGTH(s->s3->tmp.new_cipher);
453 +
454 + if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE)
455 + iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
456 + else
457 + iv_len = EVP_CIPHER_iv_length(cipher);
458 + }
459
460 key_data = s->s3->tmp.key_block;
461 client_write_mac_secret = key_data; key_data += mac_secret_len;
462 @@ -587,12 +656,20 @@ int tls1_change_cipher_state(SSL *s, int which)
463 return 0;
464 }
465
466 - if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys,
467 - mac_secret, mac_secret_len,
468 - key, key_len,
469 - iv, iv_len)) {
470 - return 0;
471 - }
472 + if (aead != NULL)
473 + {
474 + if (!tls1_change_cipher_state_aead(s, is_read,
475 + key, key_len, iv, iv_len))
476 + return 0;
477 + }
478 + else
479 + {
480 + if (!tls1_change_cipher_state_cipher(s, is_read, use_client_keys ,
481 + mac_secret, mac_secret_len,
482 + key, key_len,
483 + iv, iv_len))
484 + return 0;
485 + }
486
487 return 1;
488 err:
489 @@ -603,13 +680,14 @@ err:
490 int tls1_setup_key_block(SSL *s)
491 {
492 unsigned char *p1,*p2=NULL;
493 - const EVP_CIPHER *c;
494 - const EVP_MD *hash;
495 + const EVP_CIPHER *c = NULL;
496 + const EVP_MD *hash = NULL;
497 + const EVP_AEAD *aead = NULL;
498 int num;
499 SSL_COMP *comp;
500 int mac_type= NID_undef,mac_secret_size=0;
501 int ret=0;
502 - int iv_len;
503 + unsigned key_len, iv_len;
504
505 #ifdef KSSL_DEBUG
506 printf ("tls1_setup_key_block()\n");
507 @@ -618,22 +696,36 @@ int tls1_setup_key_block(SSL *s)
508 if (s->s3->tmp.key_block_length != 0)
509 return(1);
510
511 - if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secret_size,& comp))
512 + if (!ssl_cipher_get_comp(s->session, &comp))
513 + goto cipher_unavailable_err;
514 +
515 + if (s->session->cipher &&
516 + (s->session->cipher->algorithm2 & SSL_CIPHER_ALGORITHM2_AEAD))
517 {
518 - SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILAB LE);
519 - return(0);
520 + if (!ssl_cipher_get_evp_aead(s->session, &aead))
521 + goto cipher_unavailable_err;
522 + key_len = EVP_AEAD_key_length(aead);
523 + iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher);
524 }
525 -
526 - if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
527 - iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
528 else
529 - iv_len = EVP_CIPHER_iv_length(c);
530 + {
531 + if (!ssl_cipher_get_evp(s->session,&c,&hash,&mac_type,&mac_secre t_size))
532 + goto cipher_unavailable_err;
533 + key_len = EVP_CIPHER_key_length(c);
534
535 + if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
536 + iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
537 + else
538 + iv_len = EVP_CIPHER_iv_length(c);
539 + }
540 +
541 + s->s3->tmp.new_aead=aead;
542 s->s3->tmp.new_sym_enc=c;
543 s->s3->tmp.new_hash=hash;
544 s->s3->tmp.new_mac_pkey_type = mac_type;
545 s->s3->tmp.new_mac_secret_size = mac_secret_size;
546 - num=EVP_CIPHER_key_length(c)+mac_secret_size+iv_len;
547 +
548 + num=key_len+mac_secret_size+iv_len;
549 num*=2;
550
551 ssl3_cleanup_key_block(s);
552 @@ -696,6 +788,10 @@ err:
553 OPENSSL_free(p2);
554 }
555 return(ret);
556 +
557 +cipher_unavailable_err:
558 + SSLerr(SSL_F_TLS1_SETUP_KEY_BLOCK,SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
559 + return 0;
560 }
561
562 /* tls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively .
563 @@ -714,6 +810,124 @@ int tls1_enc(SSL *s, int send)
564 unsigned long l;
565 int bs,i,j,k,pad=0,ret,mac_size=0;
566 const EVP_CIPHER *enc;
567 + const SSL_AEAD_CTX *aead;
568 +
569 + if (send)
570 + rec = &s->s3->wrec;
571 + else
572 + rec = &s->s3->rrec;
573 +
574 + if (send)
575 + aead = s->aead_write_ctx;
576 + else
577 + aead = s->aead_read_ctx;
578 +
579 + if (aead)
580 + {
581 + unsigned char ad[13], *seq, *in, *out, nonce[16];
582 + unsigned nonce_used;
583 + ssize_t n;
584 +
585 + seq = send ? s->s3->write_sequence : s->s3->read_sequence;
586 +
587 + if (s->version == DTLS1_VERSION || s->version == DTLS1_BAD_VER)
588 + {
589 + unsigned char dtlsseq[9], *p = dtlsseq;
590 +
591 + s2n(send ? s->d1->w_epoch : s->d1->r_epoch, p);
592 + memcpy(p, &seq[2], 6);
593 + memcpy(ad, dtlsseq, 8);
594 + }
595 + else
596 + {
597 + memcpy(ad, seq, 8);
598 + for (i=7; i>=0; i--) /* increment */
599 + {
600 + ++seq[i];
601 + if (seq[i] != 0)
602 + break;
603 + }
604 + }
605 +
606 + ad[8] = rec->type;
607 + ad[9] = (unsigned char)(s->version>>8);
608 + ad[10] = (unsigned char)(s->version);
609 +
610 + if (aead->fixed_nonce_len + aead->variable_nonce_len > sizeof(no nce) ||
611 + aead->variable_nonce_len > 8)
612 + return -1; /* internal error - should never happen. */
613 +
614 + memcpy(nonce, aead->fixed_nonce, aead->fixed_nonce_len);
615 + nonce_used = aead->fixed_nonce_len;
616 +
617 + if (send)
618 + {
619 + size_t len = rec->length;
620 + in = rec->input;
621 + out = rec->data;
622 +
623 + /* When sending we use the sequence number as the
624 + * variable part of the nonce. */
625 + if (aead->variable_nonce_len > 8)
626 + return -1;
627 + memcpy(nonce + nonce_used, ad, aead->variable_nonce_len) ;
628 + nonce_used += aead->variable_nonce_len;
629 +
630 + /* in do_ssl3_write, rec->input is moved forward by
631 + * variable_nonce_len in order to leave space for the
632 + * variable nonce. Thus we can copy the sequence number
633 + * bytes into place without overwriting any of the
634 + * plaintext. */
635 + memcpy(out, ad, aead->variable_nonce_len);
636 + len -= aead->variable_nonce_len;
637 +
638 + ad[11] = len >> 8;
639 + ad[12] = len & 0xff;
640 +
641 + n = EVP_AEAD_CTX_seal(&aead->ctx,
642 + out + aead->variable_nonce_len, le n + aead->tag_len,
643 + nonce, nonce_used,
644 + in + aead->variable_nonce_len, len ,
645 + ad, sizeof(ad));
646 + if (n >= 0)
647 + n += aead->variable_nonce_len;
648 + }
649 + else
650 + {
651 + /* receive */
652 + size_t len = rec->length;
653 +
654 + if (rec->data != rec->input)
655 + return -1; /* internal error - should never hap pen. */
656 + out = in = rec->input;
657 +
658 + if (len < aead->variable_nonce_len)
659 + return 0;
660 + memcpy(nonce + nonce_used, in, aead->variable_nonce_len) ;
661 + nonce_used += aead->variable_nonce_len;
662 +
663 + in += aead->variable_nonce_len;
664 + len -= aead->variable_nonce_len;
665 + out += aead->variable_nonce_len;
666 +
667 + if (len < aead->tag_len)
668 + return 0;
669 + len -= aead->tag_len;
670 +
671 + ad[11] = len >> 8;
672 + ad[12] = len & 0xff;
673 +
674 + n = EVP_AEAD_CTX_open(&aead->ctx, out, len, nonce, nonce _used,
675 + in, len + aead->tag_len, ad, sizeo f(ad));
676 +
677 + rec->data = rec->input = out;
678 + }
679 +
680 + if (n == -1)
681 + return -1;
682 + rec->length = n;
683 + return 1;
684 + }
685
686 if (send)
687 {
688 --
689 1.8.4.1
690
OLDNEW
« no previous file with comments | « openssl/openssl.config ('k') | openssl/patches/aead_support.patch » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698