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