2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
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
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.
17 #include <internal/bio.h>
18 #include <openssl/asn1.h>
20 /* Must be large enough for biggest tag+length */
21 #define DEFAULT_ASN1_BUF_SIZE 20
27 ASN1_STATE_HEADER_COPY,
33 typedef struct BIO_ASN1_EX_FUNCS_st {
34 asn1_ps_func *ex_func;
35 asn1_ps_func *ex_free_func;
38 typedef struct BIO_ASN1_BUF_CTX_t {
40 asn1_bio_state_t state;
45 /* Current position in buffer */
47 /* Current buffer length */
49 /* Amount of data to copy */
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;
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);
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,
75 asn1_bio_state_t ex_state,
76 asn1_bio_state_t other_state);
78 static const BIO_METHOD methods_asn1 = {
82 /* TODO: Convert to new style read function */
90 asn1_bio_callback_ctrl,
93 const BIO_METHOD *BIO_f_asn1(void)
95 return (&methods_asn1);
98 static int asn1_bio_new(BIO *b)
100 BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
104 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
108 BIO_set_data(b, ctx);
114 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
116 ctx->buf = OPENSSL_malloc(size);
117 if (ctx->buf == NULL)
120 ctx->asn1_class = V_ASN1_UNIVERSAL;
121 ctx->asn1_tag = V_ASN1_OCTET_STRING;
122 ctx->state = ASN1_STATE_START;
126 static int asn1_bio_free(BIO *b)
128 BIO_ASN1_BUF_CTX *ctx;
133 ctx = BIO_get_data(b);
137 OPENSSL_free(ctx->buf);
139 BIO_set_data(b, NULL);
145 static int asn1_bio_write(BIO *b, const char *in, int inl)
147 BIO_ASN1_BUF_CTX *ctx;
148 int wrmax, wrlen, ret;
152 ctx = BIO_get_data(b);
154 if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
161 switch (ctx->state) {
162 /* Setup prefix data, call it */
163 case ASN1_STATE_START:
164 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
165 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
169 /* Copy any pre data first */
170 case ASN1_STATE_PRE_COPY:
172 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
180 case ASN1_STATE_HEADER:
181 ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
182 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
184 ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
186 ctx->state = ASN1_STATE_HEADER_COPY;
190 case ASN1_STATE_HEADER_COPY:
191 ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
200 ctx->state = ASN1_STATE_DATA_COPY;
205 case ASN1_STATE_DATA_COPY:
207 if (inl > ctx->copylen)
208 wrmax = ctx->copylen;
211 ret = BIO_write(next, in, wrmax);
219 if (ctx->copylen == 0)
220 ctx->state = ASN1_STATE_HEADER;
227 case ASN1_STATE_POST_COPY:
228 case ASN1_STATE_DONE:
229 BIO_clear_retry_flags(b);
237 BIO_clear_retry_flags(b);
238 BIO_copy_next_retry(b);
240 return (wrlen > 0) ? wrlen : ret;
244 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
245 asn1_ps_func *cleanup, asn1_bio_state_t next)
249 if (ctx->ex_len <= 0)
252 ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
260 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
269 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
271 asn1_bio_state_t ex_state,
272 asn1_bio_state_t other_state)
274 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
275 BIO_clear_retry_flags(b);
279 ctx->state = ex_state;
281 ctx->state = other_state;
285 static int asn1_bio_read(BIO *b, char *in, int inl)
287 BIO *next = BIO_next(b);
290 return BIO_read(next, in, inl);
293 static int asn1_bio_puts(BIO *b, const char *str)
295 return asn1_bio_write(b, str, strlen(str));
298 static int asn1_bio_gets(BIO *b, char *str, int size)
300 BIO *next = BIO_next(b);
303 return BIO_gets(next, str, size);
306 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
308 BIO *next = BIO_next(b);
311 return BIO_callback_ctrl(next, cmd, fp);
314 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
316 BIO_ASN1_BUF_CTX *ctx;
317 BIO_ASN1_EX_FUNCS *ex_func;
321 ctx = BIO_get_data(b);
327 case BIO_C_SET_PREFIX:
329 ctx->prefix = ex_func->ex_func;
330 ctx->prefix_free = ex_func->ex_free_func;
333 case BIO_C_GET_PREFIX:
335 ex_func->ex_func = ctx->prefix;
336 ex_func->ex_free_func = ctx->prefix_free;
339 case BIO_C_SET_SUFFIX:
341 ctx->suffix = ex_func->ex_func;
342 ctx->suffix_free = ex_func->ex_free_func;
345 case BIO_C_GET_SUFFIX:
347 ex_func->ex_func = ctx->suffix;
348 ex_func->ex_free_func = ctx->suffix_free;
351 case BIO_C_SET_EX_ARG:
355 case BIO_C_GET_EX_ARG:
356 *(void **)arg2 = ctx->ex_arg;
363 /* Call post function if possible */
364 if (ctx->state == ASN1_STATE_HEADER) {
365 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
366 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
370 if (ctx->state == ASN1_STATE_POST_COPY) {
371 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
377 if (ctx->state == ASN1_STATE_DONE)
378 return BIO_ctrl(next, cmd, arg1, arg2);
380 BIO_clear_retry_flags(b);
387 return BIO_ctrl(next, cmd, arg1, arg2);
394 static int asn1_bio_set_ex(BIO *b, int cmd,
395 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
397 BIO_ASN1_EX_FUNCS extmp;
398 extmp.ex_func = ex_func;
399 extmp.ex_free_func = ex_free_func;
400 return BIO_ctrl(b, cmd, 0, &extmp);
403 static int asn1_bio_get_ex(BIO *b, int cmd,
404 asn1_ps_func **ex_func,
405 asn1_ps_func **ex_free_func)
407 BIO_ASN1_EX_FUNCS extmp;
409 ret = BIO_ctrl(b, cmd, 0, &extmp);
411 *ex_func = extmp.ex_func;
412 *ex_free_func = extmp.ex_free_func;
417 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
418 asn1_ps_func *prefix_free)
420 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
423 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
424 asn1_ps_func **pprefix_free)
426 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
429 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
430 asn1_ps_func *suffix_free)
432 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
435 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
436 asn1_ps_func **psuffix_free)
438 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);