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 #include "../internal.h"
29 
30 
31 typedef struct {
32   EVP_CIPHER_CTX cipher_ctx;
33   EVP_MD_CTX md_ctx;
34 } AEAD_SSL3_CTX;
35 
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)36 static int ssl3_mac(AEAD_SSL3_CTX *ssl3_ctx, uint8_t *out, unsigned *out_len,
37                     const uint8_t *ad, size_t ad_len, const uint8_t *in,
38                     size_t in_len) {
39   size_t md_size = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
40   size_t pad_len = (md_size == 20) ? 40 : 48;
41 
42   /* To allow for CBC mode which changes cipher length, |ad| doesn't include the
43    * length for legacy ciphers. */
44   uint8_t ad_extra[2];
45   ad_extra[0] = (uint8_t)(in_len >> 8);
46   ad_extra[1] = (uint8_t)(in_len & 0xff);
47 
48   EVP_MD_CTX md_ctx;
49   EVP_MD_CTX_init(&md_ctx);
50 
51   uint8_t pad[48];
52   uint8_t tmp[EVP_MAX_MD_SIZE];
53   OPENSSL_memset(pad, 0x36, pad_len);
54   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
55       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
56       !EVP_DigestUpdate(&md_ctx, ad, ad_len) ||
57       !EVP_DigestUpdate(&md_ctx, ad_extra, sizeof(ad_extra)) ||
58       !EVP_DigestUpdate(&md_ctx, in, in_len) ||
59       !EVP_DigestFinal_ex(&md_ctx, tmp, NULL)) {
60     EVP_MD_CTX_cleanup(&md_ctx);
61     return 0;
62   }
63 
64   OPENSSL_memset(pad, 0x5c, pad_len);
65   if (!EVP_MD_CTX_copy_ex(&md_ctx, &ssl3_ctx->md_ctx) ||
66       !EVP_DigestUpdate(&md_ctx, pad, pad_len) ||
67       !EVP_DigestUpdate(&md_ctx, tmp, md_size) ||
68       !EVP_DigestFinal_ex(&md_ctx, out, out_len)) {
69     EVP_MD_CTX_cleanup(&md_ctx);
70     return 0;
71   }
72   EVP_MD_CTX_cleanup(&md_ctx);
73   return 1;
74 }
75 
aead_ssl3_cleanup(EVP_AEAD_CTX * ctx)76 static void aead_ssl3_cleanup(EVP_AEAD_CTX *ctx) {
77   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
78   EVP_CIPHER_CTX_cleanup(&ssl3_ctx->cipher_ctx);
79   EVP_MD_CTX_cleanup(&ssl3_ctx->md_ctx);
80   OPENSSL_free(ssl3_ctx);
81   ctx->aead_state = NULL;
82 }
83 
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)84 static int aead_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key, size_t key_len,
85                           size_t tag_len, enum evp_aead_direction_t dir,
86                           const EVP_CIPHER *cipher, const EVP_MD *md) {
87   if (tag_len != EVP_AEAD_DEFAULT_TAG_LENGTH &&
88       tag_len != EVP_MD_size(md)) {
89     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_UNSUPPORTED_TAG_SIZE);
90     return 0;
91   }
92 
93   if (key_len != EVP_AEAD_key_length(ctx->aead)) {
94     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_KEY_LENGTH);
95     return 0;
96   }
97 
98   size_t mac_key_len = EVP_MD_size(md);
99   size_t enc_key_len = EVP_CIPHER_key_length(cipher);
100   assert(mac_key_len + enc_key_len + EVP_CIPHER_iv_length(cipher) == key_len);
101 
102   AEAD_SSL3_CTX *ssl3_ctx = OPENSSL_malloc(sizeof(AEAD_SSL3_CTX));
103   if (ssl3_ctx == NULL) {
104     OPENSSL_PUT_ERROR(CIPHER, ERR_R_MALLOC_FAILURE);
105     return 0;
106   }
107   EVP_CIPHER_CTX_init(&ssl3_ctx->cipher_ctx);
108   EVP_MD_CTX_init(&ssl3_ctx->md_ctx);
109 
110   ctx->aead_state = ssl3_ctx;
111   if (!EVP_CipherInit_ex(&ssl3_ctx->cipher_ctx, cipher, NULL, &key[mac_key_len],
112                          &key[mac_key_len + enc_key_len],
113                          dir == evp_aead_seal) ||
114       !EVP_DigestInit_ex(&ssl3_ctx->md_ctx, md, NULL) ||
115       !EVP_DigestUpdate(&ssl3_ctx->md_ctx, key, mac_key_len)) {
116     aead_ssl3_cleanup(ctx);
117     ctx->aead_state = NULL;
118     return 0;
119   }
120   EVP_CIPHER_CTX_set_padding(&ssl3_ctx->cipher_ctx, 0);
121 
122   return 1;
123 }
124 
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)125 static int aead_ssl3_seal(const EVP_AEAD_CTX *ctx, uint8_t *out,
126                          size_t *out_len, size_t max_out_len,
127                          const uint8_t *nonce, size_t nonce_len,
128                          const uint8_t *in, size_t in_len,
129                          const uint8_t *ad, size_t ad_len) {
130   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
131   size_t total = 0;
132 
133   if (!ssl3_ctx->cipher_ctx.encrypt) {
134     /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */
135     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
136     return 0;
137   }
138 
139   if (in_len + EVP_AEAD_max_overhead(ctx->aead) < in_len ||
140       in_len > INT_MAX) {
141     /* EVP_CIPHER takes int as input. */
142     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
143     return 0;
144   }
145 
146   if (max_out_len < in_len + EVP_AEAD_max_overhead(ctx->aead)) {
147     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
148     return 0;
149   }
150 
151   if (nonce_len != 0) {
152     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_IV_TOO_LARGE);
153     return 0;
154   }
155 
156   if (ad_len != 11 - 2 /* length bytes */) {
157     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
158     return 0;
159   }
160 
161   /* Compute the MAC. This must be first in case the operation is being done
162    * in-place. */
163   uint8_t mac[EVP_MAX_MD_SIZE];
164   unsigned mac_len;
165   if (!ssl3_mac(ssl3_ctx, mac, &mac_len, ad, ad_len, in, in_len)) {
166     return 0;
167   }
168 
169   /* Encrypt the input. */
170   int len;
171   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in,
172                          (int)in_len)) {
173     return 0;
174   }
175   total = len;
176 
177   /* Feed the MAC into the cipher. */
178   if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out + total, &len, mac,
179                          (int)mac_len)) {
180     return 0;
181   }
182   total += len;
183 
184   unsigned block_size = EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx);
185   if (block_size > 1) {
186     assert(block_size <= 256);
187     assert(EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE);
188 
189     /* Compute padding and feed that into the cipher. */
190     uint8_t padding[256];
191     unsigned padding_len = block_size - ((in_len + mac_len) % block_size);
192     OPENSSL_memset(padding, 0, padding_len - 1);
193     padding[padding_len - 1] = padding_len - 1;
194     if (!EVP_EncryptUpdate(&ssl3_ctx->cipher_ctx, out + total, &len, padding,
195                            (int)padding_len)) {
196       return 0;
197     }
198     total += len;
199   }
200 
201   if (!EVP_EncryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) {
202     return 0;
203   }
204   total += len;
205 
206   *out_len = total;
207   return 1;
208 }
209 
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)210 static int aead_ssl3_open(const EVP_AEAD_CTX *ctx, uint8_t *out,
211                          size_t *out_len, size_t max_out_len,
212                          const uint8_t *nonce, size_t nonce_len,
213                          const uint8_t *in, size_t in_len,
214                          const uint8_t *ad, size_t ad_len) {
215   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
216 
217   if (ssl3_ctx->cipher_ctx.encrypt) {
218     /* Unlike a normal AEAD, an SSL3 AEAD may only be used in one direction. */
219     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_OPERATION);
220     return 0;
221   }
222 
223   size_t mac_len = EVP_MD_CTX_size(&ssl3_ctx->md_ctx);
224   if (in_len < mac_len) {
225     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
226     return 0;
227   }
228 
229   if (max_out_len < in_len) {
230     /* This requires that the caller provide space for the MAC, even though it
231      * will always be removed on return. */
232     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BUFFER_TOO_SMALL);
233     return 0;
234   }
235 
236   if (nonce_len != 0) {
237     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
238     return 0;
239   }
240 
241   if (ad_len != 11 - 2 /* length bytes */) {
242     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_INVALID_AD_SIZE);
243     return 0;
244   }
245 
246   if (in_len > INT_MAX) {
247     /* EVP_CIPHER takes int as input. */
248     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_TOO_LARGE);
249     return 0;
250   }
251 
252   /* Decrypt to get the plaintext + MAC + padding. */
253   size_t total = 0;
254   int len;
255   if (!EVP_DecryptUpdate(&ssl3_ctx->cipher_ctx, out, &len, in, (int)in_len)) {
256     return 0;
257   }
258   total += len;
259   if (!EVP_DecryptFinal_ex(&ssl3_ctx->cipher_ctx, out + total, &len)) {
260     return 0;
261   }
262   total += len;
263   assert(total == in_len);
264 
265   /* Remove CBC padding and MAC. This would normally be timing-sensitive, but
266    * SSLv3 CBC ciphers are already broken. Support will be removed eventually.
267    * https://www.openssl.org/~bodo/ssl-poodle.pdf */
268   size_t data_len;
269   if (EVP_CIPHER_CTX_mode(&ssl3_ctx->cipher_ctx) == EVP_CIPH_CBC_MODE) {
270     unsigned padding_length = out[total - 1];
271     if (total < padding_length + 1 + mac_len) {
272       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
273       return 0;
274     }
275     /* The padding must be minimal. */
276     if (padding_length + 1 > EVP_CIPHER_CTX_block_size(&ssl3_ctx->cipher_ctx)) {
277       OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
278       return 0;
279     }
280     data_len = total - padding_length - 1 - mac_len;
281   } else {
282     data_len = total - mac_len;
283   }
284 
285   /* Compute the MAC and compare against the one in the record. */
286   uint8_t mac[EVP_MAX_MD_SIZE];
287   if (!ssl3_mac(ssl3_ctx, mac, NULL, ad, ad_len, out, data_len)) {
288     return 0;
289   }
290   if (CRYPTO_memcmp(&out[data_len], mac, mac_len) != 0) {
291     OPENSSL_PUT_ERROR(CIPHER, CIPHER_R_BAD_DECRYPT);
292     return 0;
293   }
294 
295   *out_len = data_len;
296   return 1;
297 }
298 
aead_ssl3_get_iv(const EVP_AEAD_CTX * ctx,const uint8_t ** out_iv,size_t * out_iv_len)299 static int aead_ssl3_get_iv(const EVP_AEAD_CTX *ctx, const uint8_t **out_iv,
300                             size_t *out_iv_len) {
301   AEAD_SSL3_CTX *ssl3_ctx = (AEAD_SSL3_CTX *)ctx->aead_state;
302   const size_t iv_len = EVP_CIPHER_CTX_iv_length(&ssl3_ctx->cipher_ctx);
303   if (iv_len <= 1) {
304     return 0;
305   }
306 
307   *out_iv = ssl3_ctx->cipher_ctx.iv;
308   *out_iv_len = iv_len;
309   return 1;
310 }
311 
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)312 static int aead_aes_128_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
313                                            size_t key_len, size_t tag_len,
314                                            enum evp_aead_direction_t dir) {
315   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_128_cbc(),
316                         EVP_sha1());
317 }
318 
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)319 static int aead_aes_256_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
320                                            size_t key_len, size_t tag_len,
321                                            enum evp_aead_direction_t dir) {
322   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_aes_256_cbc(),
323                         EVP_sha1());
324 }
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)325 static int aead_des_ede3_cbc_sha1_ssl3_init(EVP_AEAD_CTX *ctx,
326                                             const uint8_t *key, size_t key_len,
327                                             size_t tag_len,
328                                             enum evp_aead_direction_t dir) {
329   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_des_ede3_cbc(),
330                         EVP_sha1());
331 }
332 
aead_null_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)333 static int aead_null_sha1_ssl3_init(EVP_AEAD_CTX *ctx, const uint8_t *key,
334                                     size_t key_len, size_t tag_len,
335                                     enum evp_aead_direction_t dir) {
336   return aead_ssl3_init(ctx, key, key_len, tag_len, dir, EVP_enc_null(),
337                         EVP_sha1());
338 }
339 
340 static const EVP_AEAD aead_aes_128_cbc_sha1_ssl3 = {
341     SHA_DIGEST_LENGTH + 16 + 16, /* key len (SHA1 + AES128 + IV) */
342     0,                           /* nonce len */
343     16 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
344     SHA_DIGEST_LENGTH,           /* max tag length */
345     NULL, /* init */
346     aead_aes_128_cbc_sha1_ssl3_init,
347     aead_ssl3_cleanup,
348     aead_ssl3_seal,
349     aead_ssl3_open,
350     aead_ssl3_get_iv,
351 };
352 
353 static const EVP_AEAD aead_aes_256_cbc_sha1_ssl3 = {
354     SHA_DIGEST_LENGTH + 32 + 16, /* key len (SHA1 + AES256 + IV) */
355     0,                           /* nonce len */
356     16 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
357     SHA_DIGEST_LENGTH,           /* max tag length */
358     NULL, /* init */
359     aead_aes_256_cbc_sha1_ssl3_init,
360     aead_ssl3_cleanup,
361     aead_ssl3_seal,
362     aead_ssl3_open,
363     aead_ssl3_get_iv,
364 };
365 
366 static const EVP_AEAD aead_des_ede3_cbc_sha1_ssl3 = {
367     SHA_DIGEST_LENGTH + 24 + 8, /* key len (SHA1 + 3DES + IV) */
368     0,                          /* nonce len */
369     8 + SHA_DIGEST_LENGTH,      /* overhead (padding + SHA1) */
370     SHA_DIGEST_LENGTH,          /* max tag length */
371     NULL, /* init */
372     aead_des_ede3_cbc_sha1_ssl3_init,
373     aead_ssl3_cleanup,
374     aead_ssl3_seal,
375     aead_ssl3_open,
376     aead_ssl3_get_iv,
377 };
378 
379 static const EVP_AEAD aead_null_sha1_ssl3 = {
380     SHA_DIGEST_LENGTH,          /* key len */
381     0,                          /* nonce len */
382     SHA_DIGEST_LENGTH,          /* overhead (SHA1) */
383     SHA_DIGEST_LENGTH,          /* max tag length */
384     NULL,                       /* init */
385     aead_null_sha1_ssl3_init,
386     aead_ssl3_cleanup,
387     aead_ssl3_seal,
388     aead_ssl3_open,
389     NULL,                       /* get_iv */
390 };
391 
EVP_aead_aes_128_cbc_sha1_ssl3(void)392 const EVP_AEAD *EVP_aead_aes_128_cbc_sha1_ssl3(void) {
393   return &aead_aes_128_cbc_sha1_ssl3;
394 }
395 
EVP_aead_aes_256_cbc_sha1_ssl3(void)396 const EVP_AEAD *EVP_aead_aes_256_cbc_sha1_ssl3(void) {
397   return &aead_aes_256_cbc_sha1_ssl3;
398 }
399 
EVP_aead_des_ede3_cbc_sha1_ssl3(void)400 const EVP_AEAD *EVP_aead_des_ede3_cbc_sha1_ssl3(void) {
401   return &aead_des_ede3_cbc_sha1_ssl3;
402 }
403 
EVP_aead_null_sha1_ssl3(void)404 const EVP_AEAD *EVP_aead_null_sha1_ssl3(void) { return &aead_null_sha1_ssl3; }
405