Reject decoding of an INT64 with a value >INT64_MAX
[openssl.git] / crypto / asn1 / bio_asn1.c
1 /*
2  * Copyright 2006-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 /*
11  * Experimental ASN1 BIO. When written through the data is converted to an
12  * ASN1 string type: default is OCTET STRING. Additional functions can be
13  * provided to add prefix and suffix data.
14  */
15
16 #include <string.h>
17 #include <internal/bio.h>
18 #include <openssl/asn1.h>
19
20 /* Must be large enough for biggest tag+length */
21 #define DEFAULT_ASN1_BUF_SIZE 20
22
23 typedef enum {
24     ASN1_STATE_START,
25     ASN1_STATE_PRE_COPY,
26     ASN1_STATE_HEADER,
27     ASN1_STATE_HEADER_COPY,
28     ASN1_STATE_DATA_COPY,
29     ASN1_STATE_POST_COPY,
30     ASN1_STATE_DONE
31 } asn1_bio_state_t;
32
33 typedef struct BIO_ASN1_EX_FUNCS_st {
34     asn1_ps_func *ex_func;
35     asn1_ps_func *ex_free_func;
36 } BIO_ASN1_EX_FUNCS;
37
38 typedef struct BIO_ASN1_BUF_CTX_t {
39     /* Internal state */
40     asn1_bio_state_t state;
41     /* Internal buffer */
42     unsigned char *buf;
43     /* Size of buffer */
44     int bufsize;
45     /* Current position in buffer */
46     int bufpos;
47     /* Current buffer length */
48     int buflen;
49     /* Amount of data to copy */
50     int copylen;
51     /* Class and tag to use */
52     int asn1_class, asn1_tag;
53     asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
54     /* Extra buffer for prefix and suffix data */
55     unsigned char *ex_buf;
56     int ex_len;
57     int ex_pos;
58     void *ex_arg;
59 } BIO_ASN1_BUF_CTX;
60
61 static int asn1_bio_write(BIO *h, const char *buf, int num);
62 static int asn1_bio_read(BIO *h, char *buf, int size);
63 static int asn1_bio_puts(BIO *h, const char *str);
64 static int asn1_bio_gets(BIO *h, char *str, int size);
65 static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
66 static int asn1_bio_new(BIO *h);
67 static int asn1_bio_free(BIO *data);
68 static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
69
70 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
71 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
72                              asn1_ps_func *cleanup, asn1_bio_state_t next);
73 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
74                              asn1_ps_func *setup,
75                              asn1_bio_state_t ex_state,
76                              asn1_bio_state_t other_state);
77
78 static const BIO_METHOD methods_asn1 = {
79     BIO_TYPE_ASN1,
80     "asn1",
81     /* TODO: Convert to new style write function */
82     bwrite_conv,
83     asn1_bio_write,
84     /* TODO: Convert to new style read function */
85     bread_conv,
86     asn1_bio_read,
87     asn1_bio_puts,
88     asn1_bio_gets,
89     asn1_bio_ctrl,
90     asn1_bio_new,
91     asn1_bio_free,
92     asn1_bio_callback_ctrl,
93 };
94
95 const BIO_METHOD *BIO_f_asn1(void)
96 {
97     return (&methods_asn1);
98 }
99
100 static int asn1_bio_new(BIO *b)
101 {
102     BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
103
104     if (ctx == NULL)
105         return 0;
106     if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
107         OPENSSL_free(ctx);
108         return 0;
109     }
110     BIO_set_data(b, ctx);
111     BIO_set_init(b, 1);
112
113     return 1;
114 }
115
116 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
117 {
118     ctx->buf = OPENSSL_malloc(size);
119     if (ctx->buf == NULL)
120         return 0;
121     ctx->bufsize = size;
122     ctx->asn1_class = V_ASN1_UNIVERSAL;
123     ctx->asn1_tag = V_ASN1_OCTET_STRING;
124     ctx->state = ASN1_STATE_START;
125     return 1;
126 }
127
128 static int asn1_bio_free(BIO *b)
129 {
130     BIO_ASN1_BUF_CTX *ctx;
131
132     if (b == NULL)
133         return 0;
134
135     ctx = BIO_get_data(b);
136     if (ctx == NULL)
137         return 0;
138
139     OPENSSL_free(ctx->buf);
140     OPENSSL_free(ctx);
141     BIO_set_data(b, NULL);
142     BIO_set_init(b, 0);
143
144     return 1;
145 }
146
147 static int asn1_bio_write(BIO *b, const char *in, int inl)
148 {
149     BIO_ASN1_BUF_CTX *ctx;
150     int wrmax, wrlen, ret;
151     unsigned char *p;
152     BIO *next;
153
154     ctx = BIO_get_data(b);
155     next = BIO_next(b);
156     if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
157         return 0;
158
159     wrlen = 0;
160     ret = -1;
161
162     for (;;) {
163         switch (ctx->state) {
164             /* Setup prefix data, call it */
165         case ASN1_STATE_START:
166             if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
167                                    ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
168                 return 0;
169             break;
170
171             /* Copy any pre data first */
172         case ASN1_STATE_PRE_COPY:
173
174             ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
175                                     ASN1_STATE_HEADER);
176
177             if (ret <= 0)
178                 goto done;
179
180             break;
181
182         case ASN1_STATE_HEADER:
183             ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
184             OPENSSL_assert(ctx->buflen <= ctx->bufsize);
185             p = ctx->buf;
186             ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
187             ctx->copylen = inl;
188             ctx->state = ASN1_STATE_HEADER_COPY;
189
190             break;
191
192         case ASN1_STATE_HEADER_COPY:
193             ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
194             if (ret <= 0)
195                 goto done;
196
197             ctx->buflen -= ret;
198             if (ctx->buflen)
199                 ctx->bufpos += ret;
200             else {
201                 ctx->bufpos = 0;
202                 ctx->state = ASN1_STATE_DATA_COPY;
203             }
204
205             break;
206
207         case ASN1_STATE_DATA_COPY:
208
209             if (inl > ctx->copylen)
210                 wrmax = ctx->copylen;
211             else
212                 wrmax = inl;
213             ret = BIO_write(next, in, wrmax);
214             if (ret <= 0)
215                 goto done;
216             wrlen += ret;
217             ctx->copylen -= ret;
218             in += ret;
219             inl -= ret;
220
221             if (ctx->copylen == 0)
222                 ctx->state = ASN1_STATE_HEADER;
223
224             if (inl == 0)
225                 goto done;
226
227             break;
228
229         case ASN1_STATE_POST_COPY:
230         case ASN1_STATE_DONE:
231             BIO_clear_retry_flags(b);
232             return 0;
233
234         }
235
236     }
237
238  done:
239     BIO_clear_retry_flags(b);
240     BIO_copy_next_retry(b);
241
242     return (wrlen > 0) ? wrlen : ret;
243
244 }
245
246 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
247                              asn1_ps_func *cleanup, asn1_bio_state_t next)
248 {
249     int ret;
250
251     if (ctx->ex_len <= 0)
252         return 1;
253     for (;;) {
254         ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
255         if (ret <= 0)
256             break;
257         ctx->ex_len -= ret;
258         if (ctx->ex_len > 0)
259             ctx->ex_pos += ret;
260         else {
261             if (cleanup)
262                 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
263             ctx->state = next;
264             ctx->ex_pos = 0;
265             break;
266         }
267     }
268     return ret;
269 }
270
271 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
272                              asn1_ps_func *setup,
273                              asn1_bio_state_t ex_state,
274                              asn1_bio_state_t other_state)
275 {
276     if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
277         BIO_clear_retry_flags(b);
278         return 0;
279     }
280     if (ctx->ex_len > 0)
281         ctx->state = ex_state;
282     else
283         ctx->state = other_state;
284     return 1;
285 }
286
287 static int asn1_bio_read(BIO *b, char *in, int inl)
288 {
289     BIO *next = BIO_next(b);
290     if (next == NULL)
291         return 0;
292     return BIO_read(next, in, inl);
293 }
294
295 static int asn1_bio_puts(BIO *b, const char *str)
296 {
297     return asn1_bio_write(b, str, strlen(str));
298 }
299
300 static int asn1_bio_gets(BIO *b, char *str, int size)
301 {
302     BIO *next = BIO_next(b);
303     if (next == NULL)
304         return 0;
305     return BIO_gets(next, str, size);
306 }
307
308 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
309 {
310     BIO *next = BIO_next(b);
311     if (next == NULL)
312         return 0;
313     return BIO_callback_ctrl(next, cmd, fp);
314 }
315
316 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
317 {
318     BIO_ASN1_BUF_CTX *ctx;
319     BIO_ASN1_EX_FUNCS *ex_func;
320     long ret = 1;
321     BIO *next;
322
323     ctx = BIO_get_data(b);
324     if (ctx == NULL)
325         return 0;
326     next = BIO_next(b);
327     switch (cmd) {
328
329     case BIO_C_SET_PREFIX:
330         ex_func = arg2;
331         ctx->prefix = ex_func->ex_func;
332         ctx->prefix_free = ex_func->ex_free_func;
333         break;
334
335     case BIO_C_GET_PREFIX:
336         ex_func = arg2;
337         ex_func->ex_func = ctx->prefix;
338         ex_func->ex_free_func = ctx->prefix_free;
339         break;
340
341     case BIO_C_SET_SUFFIX:
342         ex_func = arg2;
343         ctx->suffix = ex_func->ex_func;
344         ctx->suffix_free = ex_func->ex_free_func;
345         break;
346
347     case BIO_C_GET_SUFFIX:
348         ex_func = arg2;
349         ex_func->ex_func = ctx->suffix;
350         ex_func->ex_free_func = ctx->suffix_free;
351         break;
352
353     case BIO_C_SET_EX_ARG:
354         ctx->ex_arg = arg2;
355         break;
356
357     case BIO_C_GET_EX_ARG:
358         *(void **)arg2 = ctx->ex_arg;
359         break;
360
361     case BIO_CTRL_FLUSH:
362         if (next == NULL)
363             return 0;
364
365         /* Call post function if possible */
366         if (ctx->state == ASN1_STATE_HEADER) {
367             if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
368                                    ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
369                 return 0;
370         }
371
372         if (ctx->state == ASN1_STATE_POST_COPY) {
373             ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
374                                     ASN1_STATE_DONE);
375             if (ret <= 0)
376                 return ret;
377         }
378
379         if (ctx->state == ASN1_STATE_DONE)
380             return BIO_ctrl(next, cmd, arg1, arg2);
381         else {
382             BIO_clear_retry_flags(b);
383             return 0;
384         }
385
386     default:
387         if (next == NULL)
388             return 0;
389         return BIO_ctrl(next, cmd, arg1, arg2);
390
391     }
392
393     return ret;
394 }
395
396 static int asn1_bio_set_ex(BIO *b, int cmd,
397                            asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
398 {
399     BIO_ASN1_EX_FUNCS extmp;
400     extmp.ex_func = ex_func;
401     extmp.ex_free_func = ex_free_func;
402     return BIO_ctrl(b, cmd, 0, &extmp);
403 }
404
405 static int asn1_bio_get_ex(BIO *b, int cmd,
406                            asn1_ps_func **ex_func,
407                            asn1_ps_func **ex_free_func)
408 {
409     BIO_ASN1_EX_FUNCS extmp;
410     int ret;
411     ret = BIO_ctrl(b, cmd, 0, &extmp);
412     if (ret > 0) {
413         *ex_func = extmp.ex_func;
414         *ex_free_func = extmp.ex_free_func;
415     }
416     return ret;
417 }
418
419 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
420                         asn1_ps_func *prefix_free)
421 {
422     return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
423 }
424
425 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
426                         asn1_ps_func **pprefix_free)
427 {
428     return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
429 }
430
431 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
432                         asn1_ps_func *suffix_free)
433 {
434     return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
435 }
436
437 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
438                         asn1_ps_func **psuffix_free)
439 {
440     return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
441 }