2 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
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.
66 #include <openssl/bio.h>
67 #include <openssl/asn1.h>
69 /* Must be large enough for biggest tag+length */
70 #define DEFAULT_ASN1_BUF_SIZE 20
76 ASN1_STATE_HEADER_COPY,
82 typedef struct BIO_ASN1_EX_FUNCS_st {
83 asn1_ps_func *ex_func;
84 asn1_ps_func *ex_free_func;
87 typedef struct BIO_ASN1_BUF_CTX_t {
89 asn1_bio_state_t state;
94 /* Current position in buffer */
96 /* Current buffer length */
98 /* Amount of data to copy */
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;
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);
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,
124 asn1_bio_state_t ex_state,
125 asn1_bio_state_t other_state);
127 static BIO_METHOD methods_asn1 = {
137 asn1_bio_callback_ctrl,
140 BIO_METHOD *BIO_f_asn1(void)
142 return (&methods_asn1);
145 static int asn1_bio_new(BIO *b)
147 BIO_ASN1_BUF_CTX *ctx;
148 ctx = OPENSSL_malloc(sizeof(*ctx));
151 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
156 b->ptr = (char *)ctx;
161 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
163 ctx->buf = OPENSSL_malloc(size);
164 if (ctx->buf == NULL)
170 ctx->asn1_class = V_ASN1_UNIVERSAL;
171 ctx->asn1_tag = V_ASN1_OCTET_STRING;
175 ctx->state = ASN1_STATE_START;
179 static int asn1_bio_free(BIO *b)
181 BIO_ASN1_BUF_CTX *ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
185 OPENSSL_free(ctx->buf);
193 static int asn1_bio_write(BIO *b, const char *in, int inl)
195 BIO_ASN1_BUF_CTX *ctx;
196 int wrmax, wrlen, ret;
198 if (!in || (inl < 0) || (b->next_bio == NULL))
200 ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
208 switch (ctx->state) {
210 /* Setup prefix data, call it */
211 case ASN1_STATE_START:
212 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
213 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
217 /* Copy any pre data first */
218 case ASN1_STATE_PRE_COPY:
220 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
228 case ASN1_STATE_HEADER:
229 ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
230 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
232 ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
234 ctx->state = ASN1_STATE_HEADER_COPY;
238 case ASN1_STATE_HEADER_COPY:
239 ret = BIO_write(b->next_bio, ctx->buf + ctx->bufpos, ctx->buflen);
248 ctx->state = ASN1_STATE_DATA_COPY;
253 case ASN1_STATE_DATA_COPY:
255 if (inl > ctx->copylen)
256 wrmax = ctx->copylen;
259 ret = BIO_write(b->next_bio, in, wrmax);
267 if (ctx->copylen == 0)
268 ctx->state = ASN1_STATE_HEADER;
276 BIO_clear_retry_flags(b);
284 BIO_clear_retry_flags(b);
285 BIO_copy_next_retry(b);
287 return (wrlen > 0) ? wrlen : ret;
291 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
292 asn1_ps_func *cleanup, asn1_bio_state_t next)
295 if (ctx->ex_len <= 0)
298 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
306 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
315 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
317 asn1_bio_state_t ex_state,
318 asn1_bio_state_t other_state)
320 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
321 BIO_clear_retry_flags(b);
325 ctx->state = ex_state;
327 ctx->state = other_state;
331 static int asn1_bio_read(BIO *b, char *in, int inl)
335 return BIO_read(b->next_bio, in, inl);
338 static int asn1_bio_puts(BIO *b, const char *str)
340 return asn1_bio_write(b, str, strlen(str));
343 static int asn1_bio_gets(BIO *b, char *str, int size)
347 return BIO_gets(b->next_bio, str, size);
350 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
352 if (b->next_bio == NULL)
354 return BIO_callback_ctrl(b->next_bio, cmd, fp);
357 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
359 BIO_ASN1_BUF_CTX *ctx;
360 BIO_ASN1_EX_FUNCS *ex_func;
362 ctx = (BIO_ASN1_BUF_CTX *)b->ptr;
367 case BIO_C_SET_PREFIX:
369 ctx->prefix = ex_func->ex_func;
370 ctx->prefix_free = ex_func->ex_free_func;
373 case BIO_C_GET_PREFIX:
375 ex_func->ex_func = ctx->prefix;
376 ex_func->ex_free_func = ctx->prefix_free;
379 case BIO_C_SET_SUFFIX:
381 ctx->suffix = ex_func->ex_func;
382 ctx->suffix_free = ex_func->ex_free_func;
385 case BIO_C_GET_SUFFIX:
387 ex_func->ex_func = ctx->suffix;
388 ex_func->ex_free_func = ctx->suffix_free;
391 case BIO_C_SET_EX_ARG:
395 case BIO_C_GET_EX_ARG:
396 *(void **)arg2 = ctx->ex_arg;
403 /* Call post function if possible */
404 if (ctx->state == ASN1_STATE_HEADER) {
405 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
406 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
410 if (ctx->state == ASN1_STATE_POST_COPY) {
411 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
417 if (ctx->state == ASN1_STATE_DONE)
418 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
420 BIO_clear_retry_flags(b);
427 return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
434 static int asn1_bio_set_ex(BIO *b, int cmd,
435 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
437 BIO_ASN1_EX_FUNCS extmp;
438 extmp.ex_func = ex_func;
439 extmp.ex_free_func = ex_free_func;
440 return BIO_ctrl(b, cmd, 0, &extmp);
443 static int asn1_bio_get_ex(BIO *b, int cmd,
444 asn1_ps_func **ex_func,
445 asn1_ps_func **ex_free_func)
447 BIO_ASN1_EX_FUNCS extmp;
449 ret = BIO_ctrl(b, cmd, 0, &extmp);
451 *ex_func = extmp.ex_func;
452 *ex_free_func = extmp.ex_free_func;
457 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
458 asn1_ps_func *prefix_free)
460 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
463 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
464 asn1_ps_func **pprefix_free)
466 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
469 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
470 asn1_ps_func *suffix_free)
472 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
475 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
476 asn1_ps_func **psuffix_free)
478 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);