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
= {
81 /* TODO: Convert to new style write function */
84 /* TODO: Convert to new style read function */
92 asn1_bio_callback_ctrl
,
95 const BIO_METHOD
*BIO_f_asn1(void)
97 return (&methods_asn1
);
100 static int asn1_bio_new(BIO
*b
)
102 BIO_ASN1_BUF_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
106 if (!asn1_bio_init(ctx
, DEFAULT_ASN1_BUF_SIZE
)) {
110 BIO_set_data(b
, ctx
);
116 static int asn1_bio_init(BIO_ASN1_BUF_CTX
*ctx
, int size
)
118 ctx
->buf
= OPENSSL_malloc(size
);
119 if (ctx
->buf
== NULL
)
122 ctx
->asn1_class
= V_ASN1_UNIVERSAL
;
123 ctx
->asn1_tag
= V_ASN1_OCTET_STRING
;
124 ctx
->state
= ASN1_STATE_START
;
128 static int asn1_bio_free(BIO
*b
)
130 BIO_ASN1_BUF_CTX
*ctx
;
135 ctx
= BIO_get_data(b
);
139 OPENSSL_free(ctx
->buf
);
141 BIO_set_data(b
, NULL
);
147 static int asn1_bio_write(BIO
*b
, const char *in
, int inl
)
149 BIO_ASN1_BUF_CTX
*ctx
;
150 int wrmax
, wrlen
, ret
;
154 ctx
= BIO_get_data(b
);
156 if (in
== NULL
|| inl
< 0 || ctx
== NULL
|| next
== NULL
)
163 switch (ctx
->state
) {
164 /* Setup prefix data, call it */
165 case ASN1_STATE_START
:
166 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->prefix
,
167 ASN1_STATE_PRE_COPY
, ASN1_STATE_HEADER
))
171 /* Copy any pre data first */
172 case ASN1_STATE_PRE_COPY
:
174 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->prefix_free
,
182 case ASN1_STATE_HEADER
:
183 ctx
->buflen
= ASN1_object_size(0, inl
, ctx
->asn1_tag
) - inl
;
184 OPENSSL_assert(ctx
->buflen
<= ctx
->bufsize
);
186 ASN1_put_object(&p
, 0, inl
, ctx
->asn1_tag
, ctx
->asn1_class
);
188 ctx
->state
= ASN1_STATE_HEADER_COPY
;
192 case ASN1_STATE_HEADER_COPY
:
193 ret
= BIO_write(next
, ctx
->buf
+ ctx
->bufpos
, ctx
->buflen
);
202 ctx
->state
= ASN1_STATE_DATA_COPY
;
207 case ASN1_STATE_DATA_COPY
:
209 if (inl
> ctx
->copylen
)
210 wrmax
= ctx
->copylen
;
213 ret
= BIO_write(next
, in
, wrmax
);
221 if (ctx
->copylen
== 0)
222 ctx
->state
= ASN1_STATE_HEADER
;
229 case ASN1_STATE_POST_COPY
:
230 case ASN1_STATE_DONE
:
231 BIO_clear_retry_flags(b
);
239 BIO_clear_retry_flags(b
);
240 BIO_copy_next_retry(b
);
242 return (wrlen
> 0) ? wrlen
: ret
;
246 static int asn1_bio_flush_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
247 asn1_ps_func
*cleanup
, asn1_bio_state_t next
)
251 if (ctx
->ex_len
<= 0)
254 ret
= BIO_write(BIO_next(b
), ctx
->ex_buf
+ ctx
->ex_pos
, ctx
->ex_len
);
262 cleanup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
);
271 static int asn1_bio_setup_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
273 asn1_bio_state_t ex_state
,
274 asn1_bio_state_t other_state
)
276 if (setup
&& !setup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
)) {
277 BIO_clear_retry_flags(b
);
281 ctx
->state
= ex_state
;
283 ctx
->state
= other_state
;
287 static int asn1_bio_read(BIO
*b
, char *in
, int inl
)
289 BIO
*next
= BIO_next(b
);
292 return BIO_read(next
, in
, inl
);
295 static int asn1_bio_puts(BIO
*b
, const char *str
)
297 return asn1_bio_write(b
, str
, strlen(str
));
300 static int asn1_bio_gets(BIO
*b
, char *str
, int size
)
302 BIO
*next
= BIO_next(b
);
305 return BIO_gets(next
, str
, size
);
308 static long asn1_bio_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
310 BIO
*next
= BIO_next(b
);
313 return BIO_callback_ctrl(next
, cmd
, fp
);
316 static long asn1_bio_ctrl(BIO
*b
, int cmd
, long arg1
, void *arg2
)
318 BIO_ASN1_BUF_CTX
*ctx
;
319 BIO_ASN1_EX_FUNCS
*ex_func
;
323 ctx
= BIO_get_data(b
);
329 case BIO_C_SET_PREFIX
:
331 ctx
->prefix
= ex_func
->ex_func
;
332 ctx
->prefix_free
= ex_func
->ex_free_func
;
335 case BIO_C_GET_PREFIX
:
337 ex_func
->ex_func
= ctx
->prefix
;
338 ex_func
->ex_free_func
= ctx
->prefix_free
;
341 case BIO_C_SET_SUFFIX
:
343 ctx
->suffix
= ex_func
->ex_func
;
344 ctx
->suffix_free
= ex_func
->ex_free_func
;
347 case BIO_C_GET_SUFFIX
:
349 ex_func
->ex_func
= ctx
->suffix
;
350 ex_func
->ex_free_func
= ctx
->suffix_free
;
353 case BIO_C_SET_EX_ARG
:
357 case BIO_C_GET_EX_ARG
:
358 *(void **)arg2
= ctx
->ex_arg
;
365 /* Call post function if possible */
366 if (ctx
->state
== ASN1_STATE_HEADER
) {
367 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->suffix
,
368 ASN1_STATE_POST_COPY
, ASN1_STATE_DONE
))
372 if (ctx
->state
== ASN1_STATE_POST_COPY
) {
373 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->suffix_free
,
379 if (ctx
->state
== ASN1_STATE_DONE
)
380 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
382 BIO_clear_retry_flags(b
);
389 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
396 static int asn1_bio_set_ex(BIO
*b
, int cmd
,
397 asn1_ps_func
*ex_func
, asn1_ps_func
*ex_free_func
)
399 BIO_ASN1_EX_FUNCS extmp
;
400 extmp
.ex_func
= ex_func
;
401 extmp
.ex_free_func
= ex_free_func
;
402 return BIO_ctrl(b
, cmd
, 0, &extmp
);
405 static int asn1_bio_get_ex(BIO
*b
, int cmd
,
406 asn1_ps_func
**ex_func
,
407 asn1_ps_func
**ex_free_func
)
409 BIO_ASN1_EX_FUNCS extmp
;
411 ret
= BIO_ctrl(b
, cmd
, 0, &extmp
);
413 *ex_func
= extmp
.ex_func
;
414 *ex_free_func
= extmp
.ex_free_func
;
419 int BIO_asn1_set_prefix(BIO
*b
, asn1_ps_func
*prefix
,
420 asn1_ps_func
*prefix_free
)
422 return asn1_bio_set_ex(b
, BIO_C_SET_PREFIX
, prefix
, prefix_free
);
425 int BIO_asn1_get_prefix(BIO
*b
, asn1_ps_func
**pprefix
,
426 asn1_ps_func
**pprefix_free
)
428 return asn1_bio_get_ex(b
, BIO_C_GET_PREFIX
, pprefix
, pprefix_free
);
431 int BIO_asn1_set_suffix(BIO
*b
, asn1_ps_func
*suffix
,
432 asn1_ps_func
*suffix_free
)
434 return asn1_bio_set_ex(b
, BIO_C_SET_SUFFIX
, suffix
, suffix_free
);
437 int BIO_asn1_get_suffix(BIO
*b
, asn1_ps_func
**psuffix
,
438 asn1_ps_func
**psuffix_free
)
440 return asn1_bio_get_ex(b
, BIO_C_GET_SUFFIX
, psuffix
, psuffix_free
);