1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <assert.h>
16 #include <limits.h>
17 #include <string.h>
18 
19 #include <openssl/aead.h>
20 #include <openssl/cipher.h>
21 #include <openssl/err.h>
22 #include <openssl/hmac.h>
23 #include <openssl/md5.h>
24 #include <openssl/mem.h>
25 #include <openssl/sha.h>
26 
27 #include "internal.h"
28 
29 
30 typedef struct {
31   EVP_CIPHER_CTX cipher_ctx;
32   EVP_MD_CTX md_ctx;
33 } AEAD_SSL3_CTX;
34 
ssl3_mac(AEAD_SSL3_CTX * ssl3_ctx,uint8_t * out,unsigned * out_len,const uint8_t * ad,size_t ad_len,const uint8_t * in,size_t in_len)35 static int ssl3_mac(AEAD_SSL3_CTX *ssl3_ctx, uint8_t *out, unsigned *out_len,
36                     const uint8_t *ad, size_t ad_len, const uint8_t *in,
37                     size_t in_len) {
38   size_t md_size = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
39   size_t pad_len = (md_size == 20) ? 40 : 48;
40 
41   /* To allow for CBC mode which changes cipher length, |ad| doesn't include the
42    * length for legacy ciphers. */
43   uint8_t ad_extra[2];
44   ad_extra[0] = (uint8_t)(in_len >> 8);
45   ad_extra[1] = (uint8_t)(in_len & 0xff);
46 
47   EVP_MD_CTX md_ctx;
48   EVP_MD_CTX_init(&md_ctx);
49 
50   uint8_t pad[48];
51   uint8_t tmp[EVP_MAX_MD_SIZE];
52   memset(pad, 0x36, pad_len);
53   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
54       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
55       !EVP_DigestUpdate(&md_ctx, ad, ad_len) ||
56       !EVP_DigestUpdate(&md_ctx, ad_extra, sizeof(ad_extra)) ||
57       !EVP_DigestUpdate(&md_ctx, in, in_len) ||
58       !EVP_DigestFinal_ex(&md_ctx, tmp, NULL)) {
59     EVP_MD_CTX_cleanup(&md_ctx);
60     return 0;
61   }
62 
63   memset(pad, 0x5c, pad_len);
64   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
65       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
66       !EVP_DigestUpdate(&md_ctx, tmp, md_size) ||
67       !EVP_DigestFinal_ex(&md_ctx, out, out_len)) {
68     EVP_MD_CTX_cleanup(&md_ctx);
69     return 0;
70   }
71   EVP_MD_CTX_cleanup(&md_ctx);
72   return 1;
73 }
74 
aead_ssl3_cleanup(EVP_AEAD_CTX * ctx)75 static void aead_ssl3_cleanup(EVP_AEAD_CTX *ctx) {
76   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
77   EVP_CIPHER_CTX_cleanup(&ssl3_ctx->cipher_ctx);
78   EVP_MD_CTX_cleanup(&ssl3_ctx->md_ctx);
79   OPENSSL_free(ssl3_ctx);
80   ctx->aead_state = NULL;
81 }
82 
aead_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir,const EVP_CIPHER * cipher,const EVP_MD * md)83 static int aead_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
84                           size_t tag_len, enum evp_aead_direction_t dir,
85                           const EVP_CIPHER *cipher, const EVP_MD *md) {
86   if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
87       tag_len != EVP_MD_size(md)) {
88     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_init, CIPHER_R_UNSUPPORTED_TAG_SIZE);
89     return 0;
90   }
91 
92   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
93     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_init, CIPHER_R_BAD_KEY_LENGTH);
94     return 0;
95   }
96 
97   size_t mac_key_len = EVP_MD_size(md);
98   size_t enc_key_len = EVP_CIPHER_key_length(cipher);
99   assert(mac_key_len + enc_key_len + EVP_CIPHER_iv_length(cipher) == key_len);
100   /* Although EVP_rc4() is a variable-length cipher, the default key size is
101    * correct for SSL3. */
102 
103   AEAD_SSL3_CTX *ssl3_ctx = OPENSSL_malloc(sizeof(AEAD_SSL3_CTX));
104   if (ssl3_ctx == NULL) {
105     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_init, ERR_R_MALLOC_FAILURE);
106     return 0;
107   }
108   EVP_CIPHER_CTX_init(&ssl3_ctx->cipher_ctx);
109   EVP_MD_CTX_init(&ssl3_ctx->md_ctx);
110 
111   ctx->aead_state = ssl3_ctx;
112   if (!EVP_CipherInit_ex(&ssl3_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
113                          &key[mac_key_len + enc_key_len],
114                          dir == evp_aead_seal) ||
115       !EVP_DigestInit_ex(&ssl3_ctx->md_ctx, md, NULL) ||
116       !EVP_DigestUpdate(&ssl3_ctx->md_ctx, key, mac_key_len)) {
117     aead_ssl3_cleanup(ctx);
118     ctx->aead_state = NULL;
119     return 0;
120   }
121   EVP_CIPHER_CTX_set_padding(&ssl3_ctx->cipher_ctx, 0);
122 
123   return 1;
124 }
125 
aead_ssl3_seal(const EVP_AEAD_CTX * ctx,uint8_t * out,size_t * out_len,size_t max_out_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len)126 static int aead_ssl3_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
127                          size_t *out_len, size_t max_out_len,
128                          const uint8_t *nonce, size_t nonce_len,
129                          const uint8_t *in, size_t in_len,
130                          const uint8_t *ad, size_t ad_len) {
131   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
132   size_t total = 0;
133 
134   if (!ssl3_ctx->cipher_ctx.encrypt) {
135     /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */
136     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_seal, CIPHER_R_INVALID_OPERATION);
137     return 0;
138   }
139 
140   if (in_len + EVP_AEAD_max_overhead(ctx->aead) < in_len ||
141       in_len > INT_MAX) {
142     /* EVP_CIPHER takes int as input. */
143     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_seal, CIPHER_R_TOO_LARGE);
144     return 0;
145   }
146 
147   if (max_out_len < in_len + EVP_AEAD_max_overhead(ctx->aead)) {
148     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_seal, CIPHER_R_BUFFER_TOO_SMALL);
149     return 0;
150   }
151 
152   if (nonce_len != 0) {
153     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_seal, CIPHER_R_IV_TOO_LARGE);
154     return 0;
155   }
156 
157   if (ad_len != 11 - 2 /* length bytes */) {
158     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_seal, CIPHER_R_INVALID_AD_SIZE);
159     return 0;
160   }
161 
162   /* Compute the MAC. This must be first in case the operation is being done
163    * in-place. */
164   uint8_t mac[EVP_MAX_MD_SIZE];
165   unsigned mac_len;
166   if (!ssl3_mac(ssl3_ctx, mac, &mac_len, ad, ad_len, in, in_len)) {
167     return 0;
168   }
169 
170   /* Encrypt the input. */
171   int len;
172   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in,
173                          (int)in_len)) {
174     return 0;
175   }
176   total = len;
177 
178   /* Feed the MAC into the cipher. */
179   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out + total, &len, mac,
180                          (int)mac_len)) {
181     return 0;
182   }
183   total += len;
184 
185   unsigned block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx);
186   if (block_size > 1) {
187     assert(block_size <= 256);
188     assert(EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
189 
190     /* Compute padding and feed that into the cipher. */
191     uint8_t padding[256];
192     unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
193     memset(padding, 0, padding_len - 1);
194     padding[padding_len - 1] = padding_len - 1;
195     if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out + total, &len, padding,
196                            (int)padding_len)) {
197       return 0;
198     }
199     total += len;
200   }
201 
202   if (!EVP_EncryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) {
203     return 0;
204   }
205   total += len;
206 
207   *out_len = total;
208   return 1;
209 }
210 
aead_ssl3_open(const EVP_AEAD_CTX * ctx,uint8_t * out,size_t * out_len,size_t max_out_len,const uint8_t * nonce,size_t nonce_len,const uint8_t * in,size_t in_len,const uint8_t * ad,size_t ad_len)211 static int aead_ssl3_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
212                          size_t *out_len, size_t max_out_len,
213                          const uint8_t *nonce, size_t nonce_len,
214                          const uint8_t *in, size_t in_len,
215                          const uint8_t *ad, size_t ad_len) {
216   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
217 
218   if (ssl3_ctx->cipher_ctx.encrypt) {
219     /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */
220     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_INVALID_OPERATION);
221     return 0;
222   }
223 
224   size_t mac_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
225   if (in_len < mac_len) {
226     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_BAD_DECRYPT);
227     return 0;
228   }
229 
230   if (max_out_len < in_len) {
231     /* This requires that the caller provide space for the MAC, even though it
232      * will always be removed on return. */
233     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_BUFFER_TOO_SMALL);
234     return 0;
235   }
236 
237   if (nonce_len != 0) {
238     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_TOO_LARGE);
239     return 0;
240   }
241 
242   if (ad_len != 11 - 2 /* length bytes */) {
243     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_INVALID_AD_SIZE);
244     return 0;
245   }
246 
247   if (in_len > INT_MAX) {
248     /* EVP_CIPHER takes int as input. */
249     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_TOO_LARGE);
250     return 0;
251   }
252 
253   /* Decrypt to get the plaintext + MAC + padding. */
254   size_t total = 0;
255   int len;
256   if (!EVP_DecryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
257     return 0;
258   }
259   total += len;
260   if (!EVP_DecryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) {
261     return 0;
262   }
263   total += len;
264   assert(total == in_len);
265 
266   /* Remove CBC padding and MAC. This would normally be timing-sensitive, but SSLv3 CBC
267    * ciphers are already broken. Support will be removed eventually.
268    * https://www.openssl.org/~bodo/ssl-poodle.pdf */
269   unsigned data_len;
270   if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
271     unsigned padding_length = out[total - 1];
272     if (total < padding_length + 1 + mac_len) {
273       OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_BAD_DECRYPT);
274       return 0;
275     }
276     /* The padding must be minimal. */
277     if (padding_length + 1 > EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx)) {
278       OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_BAD_DECRYPT);
279       return 0;
280     }
281     data_len = total - padding_length - 1 - mac_len;
282   } else {
283     data_len = total - mac_len;
284   }
285 
286   /* Compute the MAC and compare against the one in the record. */
287   uint8_t mac[EVP_MAX_MD_SIZE];
288   if (!ssl3_mac(ssl3_ctx, mac, NULL, ad, ad_len, out, data_len)) {
289     return 0;
290   }
291   if (CRYPTO_memcmp(&out[data_len], mac, mac_len) != 0) {
292     OPENSSL_PUT_ERROR(CIPHER, aead_ssl3_open, CIPHER_R_BAD_DECRYPT);
293     return 0;
294   }
295 
296   *out_len = data_len;
297   return 1;
298 }
299 
aead_ssl3_get_rc4_state(const EVP_AEAD_CTX * ctx,const RC4_KEY ** out_key)300 static int aead_ssl3_get_rc4_state(const EVP_AEAD_CTX *ctx, const RC4_KEY **out_key) {
301   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
302   if (EVP_CIPHER_CTX_cipher(&ssl3_ctx->cipher_ctx) != EVP_rc4()) {
303     return 0;
304   }
305 
306   *out_key = (RC4_KEY*) ssl3_ctx->cipher_ctx.cipher_data;
307   return 1;
308 }
309 
aead_rc4_md5_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)310 static int aead_rc4_md5_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
311                                   size_t key_len, size_t tag_len,
312                                   enum evp_aead_direction_t dir) {
313   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_rc4(), EVP_md5());
314 }
315 
aead_rc4_sha1_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)316 static int aead_rc4_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
317                                    size_t key_len, size_t tag_len,
318                                    enum evp_aead_direction_t dir) {
319   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_rc4(), EVP_sha1());
320 }
321 
aead_aes_128_cbc_sha1_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)322 static int aead_aes_128_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
323                                            size_t key_len, size_t tag_len,
324                                            enum evp_aead_direction_t dir) {
325   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
326                         EVP_sha1());
327 }
328 
aead_aes_256_cbc_sha1_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)329 static int aead_aes_256_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
330                                            size_t key_len, size_t tag_len,
331                                            enum evp_aead_direction_t dir) {
332   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
333                         EVP_sha1());
334 }
aead_des_ede3_cbc_sha1_ssl3_init(EVP_AEAD_CTX * ctx,const uint8_t * key,size_t key_len,size_t tag_len,enum evp_aead_direction_t dir)335 static int aead_des_ede3_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx,
336                                             const uint8_t *key, size_t key_len,
337                                             size_t tag_len,
338                                             enum evp_aead_direction_t dir) {
339   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
340                         EVP_sha1());
341 }
342 
343 static const EVP_AEAD aead_rc4_md5_ssl3 = {
344     MD5_DIGEST_LENGTH + 16, /* key len (MD5 + RC4) */
345     0,                      /* nonce len */
346     MD5_DIGEST_LENGTH,      /* overhead */
347     MD5_DIGEST_LENGTH,      /* max tag length */
348     NULL, /* init */
349     aead_rc4_md5_ssl3_init,
350     aead_ssl3_cleanup,
351     aead_ssl3_seal,
352     aead_ssl3_open,
353     aead_ssl3_get_rc4_state,
354 };
355 
356 static const EVP_AEAD aead_rc4_sha1_ssl3 = {
357     SHA_DIGEST_LENGTH + 16, /* key len (SHA1 + RC4) */
358     0,                      /* nonce len */
359     SHA_DIGEST_LENGTH,      /* overhead */
360     SHA_DIGEST_LENGTH,      /* max tag length */
361     NULL, /* init */
362     aead_rc4_sha1_ssl3_init,
363     aead_ssl3_cleanup,
364     aead_ssl3_seal,
365     aead_ssl3_open,
366     aead_ssl3_get_rc4_state,
367 };
368 
369 static const EVP_AEAD aead_aes_128_cbc_sha1_ssl3 = {
370     SHA_DIGEST_LENGTH + 16 + 16, /* key len (SHA1 + AES128 + IV) */
371     0,                           /* nonce len */
372     16 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
373     SHA_DIGEST_LENGTH,           /* max tag length */
374     NULL, /* init */
375     aead_aes_128_cbc_sha1_ssl3_init,
376     aead_ssl3_cleanup,
377     aead_ssl3_seal,
378     aead_ssl3_open,
379     NULL,                        /* get_rc4_state */
380 };
381 
382 static const EVP_AEAD aead_aes_256_cbc_sha1_ssl3 = {
383     SHA_DIGEST_LENGTH + 32 + 16, /* key len (SHA1 + AES256 + IV) */
384     0,                           /* nonce len */
385     16 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
386     SHA_DIGEST_LENGTH,           /* max tag length */
387     NULL, /* init */
388     aead_aes_256_cbc_sha1_ssl3_init,
389     aead_ssl3_cleanup,
390     aead_ssl3_seal,
391     aead_ssl3_open,
392     NULL,                        /* get_rc4_state */
393 };
394 
395 static const EVP_AEAD aead_des_ede3_cbc_sha1_ssl3 = {
396     SHA_DIGEST_LENGTH + 24 + 8, /* key len (SHA1 + 3DES + IV) */
397     0,                          /* nonce len */
398     8 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
399     SHA_DIGEST_LENGTH,          /* max tag length */
400     NULL, /* init */
401     aead_des_ede3_cbc_sha1_ssl3_init,
402     aead_ssl3_cleanup,
403     aead_ssl3_seal,
404     aead_ssl3_open,
405     NULL,                        /* get_rc4_state */
406 };
407 
EVP_aead_rc4_md5_ssl3(void)408 const EVP_AEAD *EVP_aead_rc4_md5_ssl3(void) { return &aead_rc4_md5_ssl3; }
409 
EVP_aead_rc4_sha1_ssl3(void)410 const EVP_AEAD *EVP_aead_rc4_sha1_ssl3(void) { return &aead_rc4_sha1_ssl3; }
411 
EVP_aead_aes_128_cbc_sha1_ssl3(void)412 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void) {
413   return &aead_aes_128_cbc_sha1_ssl3;
414 }
415 
EVP_aead_aes_256_cbc_sha1_ssl3(void)416 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void) {
417   return &aead_aes_256_cbc_sha1_ssl3;
418 }
419 
EVP_aead_des_ede3_cbc_sha1_ssl3(void)420 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void) {
421   return &aead_des_ede3_cbc_sha1_ssl3;
422 }
423