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