Copyright consolidation 06/10
[openssl.git] / crypto / asn1 / bio_asn1.c
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3  * project.
4  */
5 /* ====================================================================
6  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58
59 /*
60  * Experimental ASN1 BIO. When written through the data is converted to an
61  * ASN1 string type: default is OCTET STRING. Additional functions can be
62  * provided to add prefix and suffix data.
63  */
64
65 #include <string.h>
66 #include <internal/bio.h>
67 #include <openssl/asn1.h>
68
69 /* Must be large enough for biggest tag+length */
70 #define DEFAULT_ASN1_BUF_SIZE 20
71
72 typedef enum {
73     ASN1_STATE_START,
74     ASN1_STATE_PRE_COPY,
75     ASN1_STATE_HEADER,
76     ASN1_STATE_HEADER_COPY,
77     ASN1_STATE_DATA_COPY,
78     ASN1_STATE_POST_COPY,
79     ASN1_STATE_DONE
80 } asn1_bio_state_t;
81
82 typedef struct BIO_ASN1_EX_FUNCS_st {
83     asn1_ps_func *ex_func;
84     asn1_ps_func *ex_free_func;
85 } BIO_ASN1_EX_FUNCS;
86
87 typedef struct BIO_ASN1_BUF_CTX_t {
88     /* Internal state */
89     asn1_bio_state_t state;
90     /* Internal buffer */
91     unsigned char *buf;
92     /* Size of buffer */
93     int bufsize;
94     /* Current position in buffer */
95     int bufpos;
96     /* Current buffer length */
97     int buflen;
98     /* Amount of data to copy */
99     int copylen;
100     /* Class and tag to use */
101     int asn1_class, asn1_tag;
102     asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
103     /* Extra buffer for prefix and suffix data */
104     unsigned char *ex_buf;
105     int ex_len;
106     int ex_pos;
107     void *ex_arg;
108 } BIO_ASN1_BUF_CTX;
109
110 static int asn1_bio_write(BIO *h, const char *buf, int num);
111 static int asn1_bio_read(BIO *h, char *buf, int size);
112 static int asn1_bio_puts(BIO *h, const char *str);
113 static int asn1_bio_gets(BIO *h, char *str, int size);
114 static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
115 static int asn1_bio_new(BIO *h);
116 static int asn1_bio_free(BIO *data);
117 static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
118
119 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
120 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
121                              asn1_ps_func *cleanup, asn1_bio_state_t next);
122 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
123                              asn1_ps_func *setup,
124                              asn1_bio_state_t ex_state,
125                              asn1_bio_state_t other_state);
126
127 static const BIO_METHOD methods_asn1 = {
128     BIO_TYPE_ASN1,
129     "asn1",
130     asn1_bio_write,
131     asn1_bio_read,
132     asn1_bio_puts,
133     asn1_bio_gets,
134     asn1_bio_ctrl,
135     asn1_bio_new,
136     asn1_bio_free,
137     asn1_bio_callback_ctrl,
138 };
139
140 const BIO_METHOD *BIO_f_asn1(void)
141 {
142     return (&methods_asn1);
143 }
144
145 static int asn1_bio_new(BIO *b)
146 {
147     BIO_ASN1_BUF_CTX *ctx;
148     ctx = OPENSSL_malloc(sizeof(*ctx));
149     if (ctx == NULL)
150         return 0;
151     if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
152         OPENSSL_free(ctx);
153         return 0;
154     }
155     BIO_set_data(b, ctx);
156     BIO_set_init(b, 1);
157
158     return 1;
159 }
160
161 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
162 {
163     ctx->buf = OPENSSL_malloc(size);
164     if (ctx->buf == NULL)
165         return 0;
166     ctx->bufsize = size;
167     ctx->bufpos = 0;
168     ctx->buflen = 0;
169     ctx->copylen = 0;
170     ctx->asn1_class = V_ASN1_UNIVERSAL;
171     ctx->asn1_tag = V_ASN1_OCTET_STRING;
172     ctx->ex_buf = 0;
173     ctx->ex_pos = 0;
174     ctx->ex_len = 0;
175     ctx->state = ASN1_STATE_START;
176     return 1;
177 }
178
179 static int asn1_bio_free(BIO *b)
180 {
181     BIO_ASN1_BUF_CTX *ctx;
182
183     if (b == NULL)
184         return 0;
185
186     ctx = BIO_get_data(b);
187     if (ctx == NULL)
188         return 0;
189
190     OPENSSL_free(ctx->buf);
191     OPENSSL_free(ctx);
192     BIO_set_data(b, NULL);
193     BIO_set_init(b, 0);
194
195     return 1;
196 }
197
198 static int asn1_bio_write(BIO *b, const char *in, int inl)
199 {
200     BIO_ASN1_BUF_CTX *ctx;
201     int wrmax, wrlen, ret;
202     unsigned char *p;
203     BIO *next;
204
205     ctx = BIO_get_data(b);
206     next = BIO_next(b);
207     if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
208         return 0;
209
210     wrlen = 0;
211     ret = -1;
212
213     for (;;) {
214         switch (ctx->state) {
215
216             /* Setup prefix data, call it */
217         case ASN1_STATE_START:
218             if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
219                                    ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
220                 return 0;
221             break;
222
223             /* Copy any pre data first */
224         case ASN1_STATE_PRE_COPY:
225
226             ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
227                                     ASN1_STATE_HEADER);
228
229             if (ret <= 0)
230                 goto done;
231
232             break;
233
234         case ASN1_STATE_HEADER:
235             ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
236             OPENSSL_assert(ctx->buflen <= ctx->bufsize);
237             p = ctx->buf;
238             ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
239             ctx->copylen = inl;
240             ctx->state = ASN1_STATE_HEADER_COPY;
241
242             break;
243
244         case ASN1_STATE_HEADER_COPY:
245             ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
246             if (ret <= 0)
247                 goto done;
248
249             ctx->buflen -= ret;
250             if (ctx->buflen)
251                 ctx->bufpos += ret;
252             else {
253                 ctx->bufpos = 0;
254                 ctx->state = ASN1_STATE_DATA_COPY;
255             }
256
257             break;
258
259         case ASN1_STATE_DATA_COPY:
260
261             if (inl > ctx->copylen)
262                 wrmax = ctx->copylen;
263             else
264                 wrmax = inl;
265             ret = BIO_write(next, in, wrmax);
266             if (ret <= 0)
267                 break;
268             wrlen += ret;
269             ctx->copylen -= ret;
270             in += ret;
271             inl -= ret;
272
273             if (ctx->copylen == 0)
274                 ctx->state = ASN1_STATE_HEADER;
275
276             if (inl == 0)
277                 goto done;
278
279             break;
280
281         default:
282             BIO_clear_retry_flags(b);
283             return 0;
284
285         }
286
287     }
288
289  done:
290     BIO_clear_retry_flags(b);
291     BIO_copy_next_retry(b);
292
293     return (wrlen > 0) ? wrlen : ret;
294
295 }
296
297 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
298                              asn1_ps_func *cleanup, asn1_bio_state_t next)
299 {
300     int ret;
301
302     if (ctx->ex_len <= 0)
303         return 1;
304     for (;;) {
305         ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
306         if (ret <= 0)
307             break;
308         ctx->ex_len -= ret;
309         if (ctx->ex_len > 0)
310             ctx->ex_pos += ret;
311         else {
312             if (cleanup)
313                 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
314             ctx->state = next;
315             ctx->ex_pos = 0;
316             break;
317         }
318     }
319     return ret;
320 }
321
322 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
323                              asn1_ps_func *setup,
324                              asn1_bio_state_t ex_state,
325                              asn1_bio_state_t other_state)
326 {
327     if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
328         BIO_clear_retry_flags(b);
329         return 0;
330     }
331     if (ctx->ex_len > 0)
332         ctx->state = ex_state;
333     else
334         ctx->state = other_state;
335     return 1;
336 }
337
338 static int asn1_bio_read(BIO *b, char *in, int inl)
339 {
340     BIO *next = BIO_next(b);
341     if (next == NULL)
342         return 0;
343     return BIO_read(next, in, inl);
344 }
345
346 static int asn1_bio_puts(BIO *b, const char *str)
347 {
348     return asn1_bio_write(b, str, strlen(str));
349 }
350
351 static int asn1_bio_gets(BIO *b, char *str, int size)
352 {
353     BIO *next = BIO_next(b);
354     if (next == NULL)
355         return 0;
356     return BIO_gets(next, str, size);
357 }
358
359 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
360 {
361     BIO *next = BIO_next(b);
362     if (next == NULL)
363         return 0;
364     return BIO_callback_ctrl(next, cmd, fp);
365 }
366
367 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
368 {
369     BIO_ASN1_BUF_CTX *ctx;
370     BIO_ASN1_EX_FUNCS *ex_func;
371     long ret = 1;
372     BIO *next;
373
374     ctx = BIO_get_data(b);
375     if (ctx == NULL)
376         return 0;
377     next = BIO_next(b);
378     switch (cmd) {
379
380     case BIO_C_SET_PREFIX:
381         ex_func = arg2;
382         ctx->prefix = ex_func->ex_func;
383         ctx->prefix_free = ex_func->ex_free_func;
384         break;
385
386     case BIO_C_GET_PREFIX:
387         ex_func = arg2;
388         ex_func->ex_func = ctx->prefix;
389         ex_func->ex_free_func = ctx->prefix_free;
390         break;
391
392     case BIO_C_SET_SUFFIX:
393         ex_func = arg2;
394         ctx->suffix = ex_func->ex_func;
395         ctx->suffix_free = ex_func->ex_free_func;
396         break;
397
398     case BIO_C_GET_SUFFIX:
399         ex_func = arg2;
400         ex_func->ex_func = ctx->suffix;
401         ex_func->ex_free_func = ctx->suffix_free;
402         break;
403
404     case BIO_C_SET_EX_ARG:
405         ctx->ex_arg = arg2;
406         break;
407
408     case BIO_C_GET_EX_ARG:
409         *(void **)arg2 = ctx->ex_arg;
410         break;
411
412     case BIO_CTRL_FLUSH:
413         if (next == NULL)
414             return 0;
415
416         /* Call post function if possible */
417         if (ctx->state == ASN1_STATE_HEADER) {
418             if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
419                                    ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
420                 return 0;
421         }
422
423         if (ctx->state == ASN1_STATE_POST_COPY) {
424             ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
425                                     ASN1_STATE_DONE);
426             if (ret <= 0)
427                 return ret;
428         }
429
430         if (ctx->state == ASN1_STATE_DONE)
431             return BIO_ctrl(next, cmd, arg1, arg2);
432         else {
433             BIO_clear_retry_flags(b);
434             return 0;
435         }
436
437     default:
438         if (next == NULL)
439             return 0;
440         return BIO_ctrl(next, cmd, arg1, arg2);
441
442     }
443
444     return ret;
445 }
446
447 static int asn1_bio_set_ex(BIO *b, int cmd,
448                            asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
449 {
450     BIO_ASN1_EX_FUNCS extmp;
451     extmp.ex_func = ex_func;
452     extmp.ex_free_func = ex_free_func;
453     return BIO_ctrl(b, cmd, 0, &extmp);
454 }
455
456 static int asn1_bio_get_ex(BIO *b, int cmd,
457                            asn1_ps_func **ex_func,
458                            asn1_ps_func **ex_free_func)
459 {
460     BIO_ASN1_EX_FUNCS extmp;
461     int ret;
462     ret = BIO_ctrl(b, cmd, 0, &extmp);
463     if (ret > 0) {
464         *ex_func = extmp.ex_func;
465         *ex_free_func = extmp.ex_free_func;
466     }
467     return ret;
468 }
469
470 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
471                         asn1_ps_func *prefix_free)
472 {
473     return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
474 }
475
476 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
477                         asn1_ps_func **pprefix_free)
478 {
479     return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
480 }
481
482 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
483                         asn1_ps_func *suffix_free)
484 {
485     return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
486 }
487
488 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
489                         asn1_ps_func **psuffix_free)
490 {
491     return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
492 }