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 = {
88 asn1_bio_callback_ctrl,
91 const BIO_METHOD *BIO_f_asn1(void)
93 return (&methods_asn1);
96 static int asn1_bio_new(BIO *b)
98 BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
102 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
106 BIO_set_data(b, ctx);
112 static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
114 ctx->buf = OPENSSL_malloc(size);
115 if (ctx->buf == NULL)
118 ctx->asn1_class = V_ASN1_UNIVERSAL;
119 ctx->asn1_tag = V_ASN1_OCTET_STRING;
120 ctx->state = ASN1_STATE_START;
124 static int asn1_bio_free(BIO *b)
126 BIO_ASN1_BUF_CTX *ctx;
131 ctx = BIO_get_data(b);
135 OPENSSL_free(ctx->buf);
137 BIO_set_data(b, NULL);
143 static int asn1_bio_write(BIO *b, const char *in, int inl)
145 BIO_ASN1_BUF_CTX *ctx;
146 int wrmax, wrlen, ret;
150 ctx = BIO_get_data(b);
152 if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
159 switch (ctx->state) {
161 /* Setup prefix data, call it */
162 case ASN1_STATE_START:
163 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
164 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
168 /* Copy any pre data first */
169 case ASN1_STATE_PRE_COPY:
171 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
179 case ASN1_STATE_HEADER:
180 ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
181 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
183 ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
185 ctx->state = ASN1_STATE_HEADER_COPY;
189 case ASN1_STATE_HEADER_COPY:
190 ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
199 ctx->state = ASN1_STATE_DATA_COPY;
204 case ASN1_STATE_DATA_COPY:
206 if (inl > ctx->copylen)
207 wrmax = ctx->copylen;
210 ret = BIO_write(next, in, wrmax);
218 if (ctx->copylen == 0)
219 ctx->state = ASN1_STATE_HEADER;
227 BIO_clear_retry_flags(b);
235 BIO_clear_retry_flags(b);
236 BIO_copy_next_retry(b);
238 return (wrlen > 0) ? wrlen : ret;
242 static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
243 asn1_ps_func *cleanup, asn1_bio_state_t next)
247 if (ctx->ex_len <= 0)
250 ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
258 cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
267 static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
269 asn1_bio_state_t ex_state,
270 asn1_bio_state_t other_state)
272 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
273 BIO_clear_retry_flags(b);
277 ctx->state = ex_state;
279 ctx->state = other_state;
283 static int asn1_bio_read(BIO *b, char *in, int inl)
285 BIO *next = BIO_next(b);
288 return BIO_read(next, in, inl);
291 static int asn1_bio_puts(BIO *b, const char *str)
293 return asn1_bio_write(b, str, strlen(str));
296 static int asn1_bio_gets(BIO *b, char *str, int size)
298 BIO *next = BIO_next(b);
301 return BIO_gets(next, str, size);
304 static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
306 BIO *next = BIO_next(b);
309 return BIO_callback_ctrl(next, cmd, fp);
312 static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
314 BIO_ASN1_BUF_CTX *ctx;
315 BIO_ASN1_EX_FUNCS *ex_func;
319 ctx = BIO_get_data(b);
325 case BIO_C_SET_PREFIX:
327 ctx->prefix = ex_func->ex_func;
328 ctx->prefix_free = ex_func->ex_free_func;
331 case BIO_C_GET_PREFIX:
333 ex_func->ex_func = ctx->prefix;
334 ex_func->ex_free_func = ctx->prefix_free;
337 case BIO_C_SET_SUFFIX:
339 ctx->suffix = ex_func->ex_func;
340 ctx->suffix_free = ex_func->ex_free_func;
343 case BIO_C_GET_SUFFIX:
345 ex_func->ex_func = ctx->suffix;
346 ex_func->ex_free_func = ctx->suffix_free;
349 case BIO_C_SET_EX_ARG:
353 case BIO_C_GET_EX_ARG:
354 *(void **)arg2 = ctx->ex_arg;
361 /* Call post function if possible */
362 if (ctx->state == ASN1_STATE_HEADER) {
363 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
364 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
368 if (ctx->state == ASN1_STATE_POST_COPY) {
369 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
375 if (ctx->state == ASN1_STATE_DONE)
376 return BIO_ctrl(next, cmd, arg1, arg2);
378 BIO_clear_retry_flags(b);
385 return BIO_ctrl(next, cmd, arg1, arg2);
392 static int asn1_bio_set_ex(BIO *b, int cmd,
393 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
395 BIO_ASN1_EX_FUNCS extmp;
396 extmp.ex_func = ex_func;
397 extmp.ex_free_func = ex_free_func;
398 return BIO_ctrl(b, cmd, 0, &extmp);
401 static int asn1_bio_get_ex(BIO *b, int cmd,
402 asn1_ps_func **ex_func,
403 asn1_ps_func **ex_free_func)
405 BIO_ASN1_EX_FUNCS extmp;
407 ret = BIO_ctrl(b, cmd, 0, &extmp);
409 *ex_func = extmp.ex_func;
410 *ex_free_func = extmp.ex_free_func;
415 int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
416 asn1_ps_func *prefix_free)
418 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
421 int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
422 asn1_ps_func **pprefix_free)
424 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
427 int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
428 asn1_ps_func *suffix_free)
430 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
433 int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
434 asn1_ps_func **psuffix_free)
436 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);