Rename lh_xxx,sk_xxx tp OPENSSL_{LH,SK}_xxx
[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     asn1_bio_write,
82     asn1_bio_read,
83     asn1_bio_puts,
84     asn1_bio_gets,
85     asn1_bio_ctrl,
86     asn1_bio_new,
87     asn1_bio_free,
88     asn1_bio_callback_ctrl,
89 };
90
91 const BIO_METHOD *BIO_f_asn1(void)
92 {
93     return (&methods_asn1);
94 }
95
96 static int asn1_bio_new(BIO *b)
97 {
98     BIO_ASN1_BUF_CTX *ctx;
99     ctx = OPENSSL_malloc(sizeof(*ctx));
100     if (ctx == NULL)
101         return 0;
102     if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
103         OPENSSL_free(ctx);
104         return 0;
105     }
106     BIO_set_data(b, ctx);
107     BIO_set_init(b, 1);
108
109     return 1;
110 }
111
112 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
113 {
114     ctx->buf = OPENSSL_malloc(size);
115     if (ctx->buf == NULL)
116         return 0;
117     ctx->bufsize = size;
118     ctx->bufpos = 0;
119     ctx->buflen = 0;
120     ctx->copylen = 0;
121     ctx->asn1_class = V_ASN1_UNIVERSAL;
122     ctx->asn1_tag = V_ASN1_OCTET_STRING;
123     ctx->ex_buf = 0;
124     ctx->ex_pos = 0;
125     ctx->ex_len = 0;
126     ctx->state = ASN1_STATE_START;
127     return 1;
128 }
129
130 static int asn1_bio_free(BIO *b)
131 {
132     BIO_ASN1_BUF_CTX *ctx;
133
134     if (b == NULL)
135         return 0;
136
137     ctx = BIO_get_data(b);
138     if (ctx == NULL)
139         return 0;
140
141     OPENSSL_free(ctx->buf);
142     OPENSSL_free(ctx);
143     BIO_set_data(b, NULL);
144     BIO_set_init(b, 0);
145
146     return 1;
147 }
148
149 static int asn1_bio_write(BIO *b, const char *in, int inl)
150 {
151     BIO_ASN1_BUF_CTX *ctx;
152     int wrmax, wrlen, ret;
153     unsigned char *p;
154     BIO *next;
155
156     ctx = BIO_get_data(b);
157     next = BIO_next(b);
158     if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
159         return 0;
160
161     wrlen = 0;
162     ret = -1;
163
164     for (;;) {
165         switch (ctx->state) {
166
167             /* Setup prefix data, call it */
168         case ASN1_STATE_START:
169             if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
170                                    ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
171                 return 0;
172             break;
173
174             /* Copy any pre data first */
175         case ASN1_STATE_PRE_COPY:
176
177             ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
178                                     ASN1_STATE_HEADER);
179
180             if (ret <= 0)
181                 goto done;
182
183             break;
184
185         case ASN1_STATE_HEADER:
186             ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
187             OPENSSL_assert(ctx->buflen <= ctx->bufsize);
188             p = ctx->buf;
189             ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
190             ctx->copylen = inl;
191             ctx->state = ASN1_STATE_HEADER_COPY;
192
193             break;
194
195         case ASN1_STATE_HEADER_COPY:
196             ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
197             if (ret <= 0)
198                 goto done;
199
200             ctx->buflen -= ret;
201             if (ctx->buflen)
202                 ctx->bufpos += ret;
203             else {
204                 ctx->bufpos = 0;
205                 ctx->state = ASN1_STATE_DATA_COPY;
206             }
207
208             break;
209
210         case ASN1_STATE_DATA_COPY:
211
212             if (inl > ctx->copylen)
213                 wrmax = ctx->copylen;
214             else
215                 wrmax = inl;
216             ret = BIO_write(next, in, wrmax);
217             if (ret <= 0)
218                 break;
219             wrlen += ret;
220             ctx->copylen -= ret;
221             in += ret;
222             inl -= ret;
223
224             if (ctx->copylen == 0)
225                 ctx->state = ASN1_STATE_HEADER;
226
227             if (inl == 0)
228                 goto done;
229
230             break;
231
232         default:
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 }