e3aaadb11e8d22d14a2378de1f2471c79cb78978
[openssl.git] / crypto / evp / bio_enc.c
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "internal/cryptlib.h"
13 #include <openssl/buffer.h>
14 #include <openssl/evp.h>
15 #include "internal/bio.h"
16
17 static int enc_write(BIO *h, const char *buf, int num);
18 static int enc_read(BIO *h, char *buf, int size);
19 /*
20  * static int enc_puts(BIO *h, const char *str);
21  */
22 /*
23  * static int enc_gets(BIO *h, char *str, int size);
24  */
25 static long enc_ctrl(BIO *h, int cmd, long arg1, void *arg2);
26 static int enc_new(BIO *h);
27 static int enc_free(BIO *data);
28 static long enc_callback_ctrl(BIO *h, int cmd, bio_info_cb *fps);
29 #define ENC_BLOCK_SIZE  (1024*4)
30 #define ENC_MIN_CHUNK   (256)
31 #define BUF_OFFSET      (ENC_MIN_CHUNK + EVP_MAX_BLOCK_LENGTH)
32
33 typedef struct enc_struct {
34     int buf_len;
35     int buf_off;
36     int cont;                   /* <= 0 when finished */
37     int finished;
38     int ok;                     /* bad decrypt */
39     EVP_CIPHER_CTX *cipher;
40     unsigned char *read_start, *read_end;
41     /*
42      * buf is larger than ENC_BLOCK_SIZE because EVP_DecryptUpdate can return
43      * up to a block more data than is presented to it
44      */
45     unsigned char buf[BUF_OFFSET + ENC_BLOCK_SIZE];
46 } BIO_ENC_CTX;
47
48 static const BIO_METHOD methods_enc = {
49     BIO_TYPE_CIPHER, "cipher",
50     enc_write,
51     enc_read,
52     NULL,                       /* enc_puts, */
53     NULL,                       /* enc_gets, */
54     enc_ctrl,
55     enc_new,
56     enc_free,
57     enc_callback_ctrl,
58 };
59
60 const BIO_METHOD *BIO_f_cipher(void)
61 {
62     return (&methods_enc);
63 }
64
65 static int enc_new(BIO *bi)
66 {
67     BIO_ENC_CTX *ctx;
68
69     ctx = OPENSSL_zalloc(sizeof(*ctx));
70     if (ctx == NULL)
71         return 0;
72
73     ctx->cipher = EVP_CIPHER_CTX_new();
74     if (ctx->cipher == NULL) {
75         OPENSSL_free(ctx);
76         return 0;
77     }
78     ctx->cont = 1;
79     ctx->ok = 1;
80     ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
81     BIO_set_data(bi, ctx);
82     BIO_set_init(bi, 1);
83
84     return 1;
85 }
86
87 static int enc_free(BIO *a)
88 {
89     BIO_ENC_CTX *b;
90
91     if (a == NULL)
92         return 0;
93
94     b = BIO_get_data(a);
95     if (b == NULL)
96         return 0;
97
98     EVP_CIPHER_CTX_free(b->cipher);
99     OPENSSL_clear_free(b, sizeof(BIO_ENC_CTX));
100     BIO_set_data(a, NULL);
101     BIO_set_init(a, 0);
102
103     return 1;
104 }
105
106 static int enc_read(BIO *b, char *out, int outl)
107 {
108     int ret = 0, i, blocksize;
109     BIO_ENC_CTX *ctx;
110     BIO *next;
111
112     if (out == NULL)
113         return (0);
114     ctx = BIO_get_data(b);
115
116     next = BIO_next(b);
117     if ((ctx == NULL) || (next == NULL))
118         return 0;
119
120     /* First check if there are bytes decoded/encoded */
121     if (ctx->buf_len > 0) {
122         i = ctx->buf_len - ctx->buf_off;
123         if (i > outl)
124             i = outl;
125         memcpy(out, &(ctx->buf[ctx->buf_off]), i);
126         ret = i;
127         out += i;
128         outl -= i;
129         ctx->buf_off += i;
130         if (ctx->buf_len == ctx->buf_off) {
131             ctx->buf_len = 0;
132             ctx->buf_off = 0;
133         }
134     }
135
136     blocksize = EVP_CIPHER_CTX_block_size(ctx->cipher);
137     if (blocksize == 1)
138         blocksize = 0;
139
140     /*
141      * At this point, we have room of outl bytes and an empty buffer, so we
142      * should read in some more.
143      */
144
145     while (outl > 0) {
146         if (ctx->cont <= 0)
147             break;
148
149         if (ctx->read_start == ctx->read_end) { /* time to read more data */
150             ctx->read_end = ctx->read_start = &(ctx->buf[BUF_OFFSET]);
151             ctx->read_end += BIO_read(next, ctx->read_start, ENC_BLOCK_SIZE);
152         }
153         i = ctx->read_end - ctx->read_start;
154
155         if (i <= 0) {
156             /* Should be continue next time we are called? */
157             if (!BIO_should_retry(next)) {
158                 ctx->cont = i;
159                 i = EVP_CipherFinal_ex(ctx->cipher,
160                                        ctx->buf, &(ctx->buf_len));
161                 ctx->ok = i;
162                 ctx->buf_off = 0;
163             } else {
164                 ret = (ret == 0) ? i : ret;
165                 break;
166             }
167         } else {
168             if (outl > ENC_MIN_CHUNK) {
169                 /*
170                  * Depending on flags block cipher decrypt can write
171                  * one extra block and then back off, i.e. output buffer
172                  * has to accommodate extra block...
173                  */
174                 int j = outl - blocksize, buf_len;
175
176                 if (!EVP_CipherUpdate(ctx->cipher,
177                                       (unsigned char *)out, &buf_len,
178                                       ctx->read_start, i > j ? j : i)) {
179                     BIO_clear_retry_flags(b);
180                     return 0;
181                 }
182                 ret += buf_len;
183                 out += buf_len;
184                 outl -= buf_len;
185
186                 if ((i -= j) <= 0) {
187                     ctx->read_start = ctx->read_end;
188                     continue;
189                 }
190                 ctx->read_start += j;
191             }
192             if (i > ENC_MIN_CHUNK)
193                 i = ENC_MIN_CHUNK;
194             if (!EVP_CipherUpdate(ctx->cipher,
195                                   ctx->buf, &ctx->buf_len,
196                                   ctx->read_start, i)) {
197                 BIO_clear_retry_flags(b);
198                 ctx->ok = 0;
199                 return 0;
200             }
201             ctx->read_start += i;
202             ctx->cont = 1;
203             /*
204              * Note: it is possible for EVP_CipherUpdate to decrypt zero
205              * bytes because this is or looks like the final block: if this
206              * happens we should retry and either read more data or decrypt
207              * the final block
208              */
209             if (ctx->buf_len == 0)
210                 continue;
211         }
212
213         if (ctx->buf_len <= outl)
214             i = ctx->buf_len;
215         else
216             i = outl;
217         if (i <= 0)
218             break;
219         memcpy(out, ctx->buf, i);
220         ret += i;
221         ctx->buf_off = i;
222         outl -= i;
223         out += i;
224     }
225
226     BIO_clear_retry_flags(b);
227     BIO_copy_next_retry(b);
228     return ((ret == 0) ? ctx->cont : ret);
229 }
230
231 static int enc_write(BIO *b, const char *in, int inl)
232 {
233     int ret = 0, n, i;
234     BIO_ENC_CTX *ctx;
235     BIO *next;
236
237     ctx = BIO_get_data(b);
238     next = BIO_next(b);
239     if ((ctx == NULL) || (next == NULL))
240         return 0;
241
242     ret = inl;
243
244     BIO_clear_retry_flags(b);
245     n = ctx->buf_len - ctx->buf_off;
246     while (n > 0) {
247         i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
248         if (i <= 0) {
249             BIO_copy_next_retry(b);
250             return (i);
251         }
252         ctx->buf_off += i;
253         n -= i;
254     }
255     /* at this point all pending data has been written */
256
257     if ((in == NULL) || (inl <= 0))
258         return (0);
259
260     ctx->buf_off = 0;
261     while (inl > 0) {
262         n = (inl > ENC_BLOCK_SIZE) ? ENC_BLOCK_SIZE : inl;
263         if (!EVP_CipherUpdate(ctx->cipher,
264                               ctx->buf, &ctx->buf_len,
265                               (const unsigned char *)in, n)) {
266             BIO_clear_retry_flags(b);
267             ctx->ok = 0;
268             return 0;
269         }
270         inl -= n;
271         in += n;
272
273         ctx->buf_off = 0;
274         n = ctx->buf_len;
275         while (n > 0) {
276             i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
277             if (i <= 0) {
278                 BIO_copy_next_retry(b);
279                 return (ret == inl) ? i : ret - inl;
280             }
281             n -= i;
282             ctx->buf_off += i;
283         }
284         ctx->buf_len = 0;
285         ctx->buf_off = 0;
286     }
287     BIO_copy_next_retry(b);
288     return (ret);
289 }
290
291 static long enc_ctrl(BIO *b, int cmd, long num, void *ptr)
292 {
293     BIO *dbio;
294     BIO_ENC_CTX *ctx, *dctx;
295     long ret = 1;
296     int i;
297     EVP_CIPHER_CTX **c_ctx;
298     BIO *next;
299
300     ctx = BIO_get_data(b);
301     next = BIO_next(b);
302     if (ctx == NULL)
303         return 0;
304
305     switch (cmd) {
306     case BIO_CTRL_RESET:
307         ctx->ok = 1;
308         ctx->finished = 0;
309         if (!EVP_CipherInit_ex(ctx->cipher, NULL, NULL, NULL, NULL,
310                                EVP_CIPHER_CTX_encrypting(ctx->cipher)))
311             return 0;
312         ret = BIO_ctrl(next, cmd, num, ptr);
313         break;
314     case BIO_CTRL_EOF:         /* More to read */
315         if (ctx->cont <= 0)
316             ret = 1;
317         else
318             ret = BIO_ctrl(next, cmd, num, ptr);
319         break;
320     case BIO_CTRL_WPENDING:
321         ret = ctx->buf_len - ctx->buf_off;
322         if (ret <= 0)
323             ret = BIO_ctrl(next, cmd, num, ptr);
324         break;
325     case BIO_CTRL_PENDING:     /* More to read in buffer */
326         ret = ctx->buf_len - ctx->buf_off;
327         if (ret <= 0)
328             ret = BIO_ctrl(next, cmd, num, ptr);
329         break;
330     case BIO_CTRL_FLUSH:
331         /* do a final write */
332  again:
333         while (ctx->buf_len != ctx->buf_off) {
334             i = enc_write(b, NULL, 0);
335             if (i < 0)
336                 return i;
337         }
338
339         if (!ctx->finished) {
340             ctx->finished = 1;
341             ctx->buf_off = 0;
342             ret = EVP_CipherFinal_ex(ctx->cipher,
343                                      (unsigned char *)ctx->buf,
344                                      &(ctx->buf_len));
345             ctx->ok = (int)ret;
346             if (ret <= 0)
347                 break;
348
349             /* push out the bytes */
350             goto again;
351         }
352
353         /* Finally flush the underlying BIO */
354         ret = BIO_ctrl(next, cmd, num, ptr);
355         break;
356     case BIO_C_GET_CIPHER_STATUS:
357         ret = (long)ctx->ok;
358         break;
359     case BIO_C_DO_STATE_MACHINE:
360         BIO_clear_retry_flags(b);
361         ret = BIO_ctrl(next, cmd, num, ptr);
362         BIO_copy_next_retry(b);
363         break;
364     case BIO_C_GET_CIPHER_CTX:
365         c_ctx = (EVP_CIPHER_CTX **)ptr;
366         *c_ctx = ctx->cipher;
367         BIO_set_init(b, 1);
368         break;
369     case BIO_CTRL_DUP:
370         dbio = (BIO *)ptr;
371         dctx = BIO_get_data(dbio);
372         dctx->cipher = EVP_CIPHER_CTX_new();
373         if (dctx->cipher == NULL)
374             return 0;
375         ret = EVP_CIPHER_CTX_copy(dctx->cipher, ctx->cipher);
376         if (ret)
377             BIO_set_init(dbio, 1);
378         break;
379     default:
380         ret = BIO_ctrl(next, cmd, num, ptr);
381         break;
382     }
383     return (ret);
384 }
385
386 static long enc_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
387 {
388     long ret = 1;
389     BIO *next = BIO_next(b);
390
391     if (next == NULL)
392         return (0);
393     switch (cmd) {
394     default:
395         ret = BIO_callback_ctrl(next, cmd, fp);
396         break;
397     }
398     return (ret);
399 }
400
401 /*-
402 void BIO_set_cipher_ctx(b,c)
403 BIO *b;
404 EVP_CIPHER_ctx *c;
405         {
406         if (b == NULL) return;
407
408         if ((b->callback != NULL) &&
409                 (b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,0L) <= 0))
410                 return;
411
412         b->init=1;
413         ctx=(BIO_ENC_CTX *)b->ptr;
414         memcpy(ctx->cipher,c,sizeof(EVP_CIPHER_CTX));
415
416         if (b->callback != NULL)
417                 b->callback(b,BIO_CB_CTRL,(char *)c,BIO_CTRL_SET,e,1L);
418         }
419 */
420
421 int BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k,
422                    const unsigned char *i, int e)
423 {
424     BIO_ENC_CTX *ctx;
425     long (*callback) (struct bio_st *, int, const char *, int, long, long);
426
427     ctx = BIO_get_data(b);
428     if (ctx == NULL)
429         return 0;
430
431     callback = BIO_get_callback(b);
432
433     if ((callback != NULL) &&
434             (callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e,
435                       0L) <= 0))
436         return 0;
437
438     BIO_set_init(b, 1);
439
440     if (!EVP_CipherInit_ex(ctx->cipher, c, NULL, k, i, e))
441         return 0;
442
443     if (callback != NULL)
444         return callback(b, BIO_CB_CTRL, (const char *)c, BIO_CTRL_SET, e, 1L);
445     return 1;
446 }