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