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