3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
61 * Experimental ASN1 BIO. When written through the data is converted to an
62 * ASN1 string type: default is OCTET STRING. Additional functions can be
63 * provided to add prefix and suffix data.
67 #include <openssl/bio.h>
68 #include <openssl/asn1.h>
70 /* Must be large enough for biggest tag+length */
71 #define DEFAULT_ASN1_BUF_SIZE 20
77 ASN1_STATE_HEADER_COPY,
83 typedef struct BIO_ASN1_EX_FUNCS_st {
84 asn1_ps_func *ex_func;
85 asn1_ps_func *ex_free_func;
88 typedef struct BIO_ASN1_BUF_CTX_t {
90 asn1_bio_state_t state;
95 /* Current position in buffer */
97 /* Current buffer length */
99 /* Amount of data to copy */
101 /* Class and tag to use */
102 int asn1_class, asn1_tag;
103 asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
104 /* Extra buffer for prefix and suffix data */
105 unsigned char *ex_buf;
111 static int asn1_bio_write(BIO *h, const char *buf, int num);
112 static int asn1_bio_read(BIO *h, char *buf, int size);
113 static int asn1_bio_puts(BIO *h, const char *str);
114 static int asn1_bio_gets(BIO *h, char *str, int size);
115 static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
116 static int asn1_bio_new(BIO *h);
117 static int asn1_bio_free(BIO *data);
118 static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
120 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
121 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
122 asn1_ps_func *cleanup, asn1_bio_state_t next);
123 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
125 asn1_bio_state_t ex_state,
126 asn1_bio_state_t other_state);
128 static BIO_METHOD methods_asn1 = {
138 asn1_bio_callback_ctrl,
141 BIO_METHOD *BIO_f_asn1(void)
143 return (&methods_asn1);
146 static int asn1_bio_new(BIO *b)
148 BIO_ASN1_BUF_CTX *ctx;
149 ctx = OPENSSL_malloc(sizeof(*ctx));
152 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
157 b->ptr = (char *)ctx;
162 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
164 ctx->buf = OPENSSL_malloc(size);
171 ctx->asn1_class = V_ASN1_UNIVERSAL;
172 ctx->asn1_tag = V_ASN1_OCTET_STRING;
176 ctx->state = ASN1_STATE_START;
180 static int asn1_bio_free(BIO *b)
182 BIO_ASN1_BUF_CTX *ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
186 OPENSSL_free(ctx->buf);
194 static int asn1_bio_write(BIO *b, const char *in, int inl)
196 BIO_ASN1_BUF_CTX *ctx;
197 int wrmax, wrlen, ret;
199 if (!in || (inl < 0) || (b->next_bio == NULL))
201 ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
209 switch (ctx->state) {
211 /* Setup prefix data, call it */
212 case ASN1_STATE_START:
213 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
214 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
218 /* Copy any pre data first */
219 case ASN1_STATE_PRE_COPY:
221 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
229 case ASN1_STATE_HEADER:
230 ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
231 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
233 ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
235 ctx->state = ASN1_STATE_HEADER_COPY;
239 case ASN1_STATE_HEADER_COPY:
240 ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen);
249 ctx->state = ASN1_STATE_DATA_COPY;
254 case ASN1_STATE_DATA_COPY:
256 if (inl > ctx->copylen)
257 wrmax = ctx->copylen;
260 ret = BIO_write(b->next_bio, in, wrmax);
268 if (ctx->copylen == 0)
269 ctx->state = ASN1_STATE_HEADER;
277 BIO_clear_retry_flags(b);
285 BIO_clear_retry_flags(b);
286 BIO_copy_next_retry(b);
288 return (wrlen > 0) ? wrlen : ret;
292 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
293 asn1_ps_func *cleanup, asn1_bio_state_t next)
296 if (ctx->ex_len <= 0)
299 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
307 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
316 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
318 asn1_bio_state_t ex_state,
319 asn1_bio_state_t other_state)
321 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
322 BIO_clear_retry_flags(b);
326 ctx->state = ex_state;
328 ctx->state = other_state;
332 static int asn1_bio_read(BIO *b, char *in, int inl)
336 return BIO_read(b->next_bio, in, inl);
339 static int asn1_bio_puts(BIO *b, const char *str)
341 return asn1_bio_write(b, str, strlen(str));
344 static int asn1_bio_gets(BIO *b, char *str, int size)
348 return BIO_gets(b->next_bio, str, size);
351 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
353 if (b->next_bio == NULL)
355 return BIO_callback_ctrl(b->next_bio, cmd, fp);
358 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
360 BIO_ASN1_BUF_CTX *ctx;
361 BIO_ASN1_EX_FUNCS *ex_func;
363 ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
368 case BIO_C_SET_PREFIX:
370 ctx->prefix = ex_func->ex_func;
371 ctx->prefix_free = ex_func->ex_free_func;
374 case BIO_C_GET_PREFIX:
376 ex_func->ex_func = ctx->prefix;
377 ex_func->ex_free_func = ctx->prefix_free;
380 case BIO_C_SET_SUFFIX:
382 ctx->suffix = ex_func->ex_func;
383 ctx->suffix_free = ex_func->ex_free_func;
386 case BIO_C_GET_SUFFIX:
388 ex_func->ex_func = ctx->suffix;
389 ex_func->ex_free_func = ctx->suffix_free;
392 case BIO_C_SET_EX_ARG:
396 case BIO_C_GET_EX_ARG:
397 *(void **)arg2 = ctx->ex_arg;
404 /* Call post function if possible */
405 if (ctx->state == ASN1_STATE_HEADER) {
406 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
407 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
411 if (ctx->state == ASN1_STATE_POST_COPY) {
412 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
418 if (ctx->state == ASN1_STATE_DONE)
419 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
421 BIO_clear_retry_flags(b);
428 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
435 static int asn1_bio_set_ex(BIO *b, int cmd,
436 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
438 BIO_ASN1_EX_FUNCS extmp;
439 extmp.ex_func = ex_func;
440 extmp.ex_free_func = ex_free_func;
441 return BIO_ctrl(b, cmd, 0, &extmp);
444 static int asn1_bio_get_ex(BIO *b, int cmd,
445 asn1_ps_func **ex_func,
446 asn1_ps_func **ex_free_func)
448 BIO_ASN1_EX_FUNCS extmp;
450 ret = BIO_ctrl(b, cmd, 0, &extmp);
452 *ex_func = extmp.ex_func;
453 *ex_free_func = extmp.ex_free_func;
458 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
459 asn1_ps_func *prefix_free)
461 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
464 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
465 asn1_ps_func **pprefix_free)
467 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
470 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
471 asn1_ps_func *suffix_free)
473 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
476 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
477 asn1_ps_func **psuffix_free)
479 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);