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>
19 #include "internal/cryptlib.h"
21 /* Must be large enough for biggest tag+length */
22 #define DEFAULT_ASN1_BUF_SIZE 20
28 ASN1_STATE_HEADER_COPY
,
34 typedef struct BIO_ASN1_EX_FUNCS_st
{
35 asn1_ps_func
*ex_func
;
36 asn1_ps_func
*ex_free_func
;
39 typedef struct BIO_ASN1_BUF_CTX_t
{
41 asn1_bio_state_t state
;
46 /* Current position in buffer */
48 /* Current buffer length */
50 /* Amount of data to copy */
52 /* Class and tag to use */
53 int asn1_class
, asn1_tag
;
54 asn1_ps_func
*prefix
, *prefix_free
, *suffix
, *suffix_free
;
55 /* Extra buffer for prefix and suffix data */
56 unsigned char *ex_buf
;
62 static int asn1_bio_write(BIO
*h
, const char *buf
, int num
);
63 static int asn1_bio_read(BIO
*h
, char *buf
, int size
);
64 static int asn1_bio_puts(BIO
*h
, const char *str
);
65 static int asn1_bio_gets(BIO
*h
, char *str
, int size
);
66 static long asn1_bio_ctrl(BIO
*h
, int cmd
, long arg1
, void *arg2
);
67 static int asn1_bio_new(BIO
*h
);
68 static int asn1_bio_free(BIO
*data
);
69 static long asn1_bio_callback_ctrl(BIO
*h
, int cmd
, bio_info_cb
*fp
);
71 static int asn1_bio_init(BIO_ASN1_BUF_CTX
*ctx
, int size
);
72 static int asn1_bio_flush_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
73 asn1_ps_func
*cleanup
, asn1_bio_state_t next
);
74 static int asn1_bio_setup_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
76 asn1_bio_state_t ex_state
,
77 asn1_bio_state_t other_state
);
79 static const BIO_METHOD methods_asn1
= {
82 /* TODO: Convert to new style write function */
85 /* TODO: Convert to new style read function */
93 asn1_bio_callback_ctrl
,
96 const BIO_METHOD
*BIO_f_asn1(void)
101 static int asn1_bio_new(BIO
*b
)
103 BIO_ASN1_BUF_CTX
*ctx
= OPENSSL_zalloc(sizeof(*ctx
));
107 if (!asn1_bio_init(ctx
, DEFAULT_ASN1_BUF_SIZE
)) {
111 BIO_set_data(b
, ctx
);
117 static int asn1_bio_init(BIO_ASN1_BUF_CTX
*ctx
, int size
)
119 ctx
->buf
= OPENSSL_malloc(size
);
120 if (ctx
->buf
== NULL
)
123 ctx
->asn1_class
= V_ASN1_UNIVERSAL
;
124 ctx
->asn1_tag
= V_ASN1_OCTET_STRING
;
125 ctx
->state
= ASN1_STATE_START
;
129 static int asn1_bio_free(BIO
*b
)
131 BIO_ASN1_BUF_CTX
*ctx
;
136 ctx
= BIO_get_data(b
);
140 OPENSSL_free(ctx
->buf
);
142 BIO_set_data(b
, NULL
);
148 static int asn1_bio_write(BIO
*b
, const char *in
, int inl
)
150 BIO_ASN1_BUF_CTX
*ctx
;
151 int wrmax
, wrlen
, ret
;
155 ctx
= BIO_get_data(b
);
157 if (in
== NULL
|| inl
< 0 || ctx
== NULL
|| next
== NULL
)
164 switch (ctx
->state
) {
165 /* Setup prefix data, call it */
166 case ASN1_STATE_START
:
167 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->prefix
,
168 ASN1_STATE_PRE_COPY
, ASN1_STATE_HEADER
))
172 /* Copy any pre data first */
173 case ASN1_STATE_PRE_COPY
:
175 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->prefix_free
,
183 case ASN1_STATE_HEADER
:
184 ctx
->buflen
= ASN1_object_size(0, inl
, ctx
->asn1_tag
) - inl
;
185 if (!ossl_assert(ctx
->buflen
<= ctx
->bufsize
))
188 ASN1_put_object(&p
, 0, inl
, ctx
->asn1_tag
, ctx
->asn1_class
);
190 ctx
->state
= ASN1_STATE_HEADER_COPY
;
194 case ASN1_STATE_HEADER_COPY
:
195 ret
= BIO_write(next
, ctx
->buf
+ ctx
->bufpos
, ctx
->buflen
);
204 ctx
->state
= ASN1_STATE_DATA_COPY
;
209 case ASN1_STATE_DATA_COPY
:
211 if (inl
> ctx
->copylen
)
212 wrmax
= ctx
->copylen
;
215 ret
= BIO_write(next
, in
, wrmax
);
223 if (ctx
->copylen
== 0)
224 ctx
->state
= ASN1_STATE_HEADER
;
231 case ASN1_STATE_POST_COPY
:
232 case ASN1_STATE_DONE
:
233 BIO_clear_retry_flags(b
);
241 BIO_clear_retry_flags(b
);
242 BIO_copy_next_retry(b
);
244 return (wrlen
> 0) ? wrlen
: ret
;
248 static int asn1_bio_flush_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
249 asn1_ps_func
*cleanup
, asn1_bio_state_t next
)
253 if (ctx
->ex_len
<= 0)
256 ret
= BIO_write(BIO_next(b
), ctx
->ex_buf
+ ctx
->ex_pos
, ctx
->ex_len
);
264 cleanup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
);
273 static int asn1_bio_setup_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
275 asn1_bio_state_t ex_state
,
276 asn1_bio_state_t other_state
)
278 if (setup
&& !setup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
)) {
279 BIO_clear_retry_flags(b
);
283 ctx
->state
= ex_state
;
285 ctx
->state
= other_state
;
289 static int asn1_bio_read(BIO
*b
, char *in
, int inl
)
291 BIO
*next
= BIO_next(b
);
294 return BIO_read(next
, in
, inl
);
297 static int asn1_bio_puts(BIO
*b
, const char *str
)
299 return asn1_bio_write(b
, str
, strlen(str
));
302 static int asn1_bio_gets(BIO
*b
, char *str
, int size
)
304 BIO
*next
= BIO_next(b
);
307 return BIO_gets(next
, str
, size
);
310 static long asn1_bio_callback_ctrl(BIO
*b
, int cmd
, bio_info_cb
*fp
)
312 BIO
*next
= BIO_next(b
);
315 return BIO_callback_ctrl(next
, cmd
, fp
);
318 static long asn1_bio_ctrl(BIO
*b
, int cmd
, long arg1
, void *arg2
)
320 BIO_ASN1_BUF_CTX
*ctx
;
321 BIO_ASN1_EX_FUNCS
*ex_func
;
325 ctx
= BIO_get_data(b
);
331 case BIO_C_SET_PREFIX
:
333 ctx
->prefix
= ex_func
->ex_func
;
334 ctx
->prefix_free
= ex_func
->ex_free_func
;
337 case BIO_C_GET_PREFIX
:
339 ex_func
->ex_func
= ctx
->prefix
;
340 ex_func
->ex_free_func
= ctx
->prefix_free
;
343 case BIO_C_SET_SUFFIX
:
345 ctx
->suffix
= ex_func
->ex_func
;
346 ctx
->suffix_free
= ex_func
->ex_free_func
;
349 case BIO_C_GET_SUFFIX
:
351 ex_func
->ex_func
= ctx
->suffix
;
352 ex_func
->ex_free_func
= ctx
->suffix_free
;
355 case BIO_C_SET_EX_ARG
:
359 case BIO_C_GET_EX_ARG
:
360 *(void **)arg2
= ctx
->ex_arg
;
367 /* Call post function if possible */
368 if (ctx
->state
== ASN1_STATE_HEADER
) {
369 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->suffix
,
370 ASN1_STATE_POST_COPY
, ASN1_STATE_DONE
))
374 if (ctx
->state
== ASN1_STATE_POST_COPY
) {
375 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->suffix_free
,
381 if (ctx
->state
== ASN1_STATE_DONE
)
382 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
384 BIO_clear_retry_flags(b
);
391 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
398 static int asn1_bio_set_ex(BIO
*b
, int cmd
,
399 asn1_ps_func
*ex_func
, asn1_ps_func
*ex_free_func
)
401 BIO_ASN1_EX_FUNCS extmp
;
402 extmp
.ex_func
= ex_func
;
403 extmp
.ex_free_func
= ex_free_func
;
404 return BIO_ctrl(b
, cmd
, 0, &extmp
);
407 static int asn1_bio_get_ex(BIO
*b
, int cmd
,
408 asn1_ps_func
**ex_func
,
409 asn1_ps_func
**ex_free_func
)
411 BIO_ASN1_EX_FUNCS extmp
;
413 ret
= BIO_ctrl(b
, cmd
, 0, &extmp
);
415 *ex_func
= extmp
.ex_func
;
416 *ex_free_func
= extmp
.ex_free_func
;
421 int BIO_asn1_set_prefix(BIO
*b
, asn1_ps_func
*prefix
,
422 asn1_ps_func
*prefix_free
)
424 return asn1_bio_set_ex(b
, BIO_C_SET_PREFIX
, prefix
, prefix_free
);
427 int BIO_asn1_get_prefix(BIO
*b
, asn1_ps_func
**pprefix
,
428 asn1_ps_func
**pprefix_free
)
430 return asn1_bio_get_ex(b
, BIO_C_GET_PREFIX
, pprefix
, pprefix_free
);
433 int BIO_asn1_set_suffix(BIO
*b
, asn1_ps_func
*suffix
,
434 asn1_ps_func
*suffix_free
)
436 return asn1_bio_set_ex(b
, BIO_C_SET_SUFFIX
, suffix
, suffix_free
);
439 int BIO_asn1_get_suffix(BIO
*b
, asn1_ps_func
**psuffix
,
440 asn1_ps_func
**psuffix_free
)
442 return asn1_bio_get_ex(b
, BIO_C_GET_SUFFIX
, psuffix
, psuffix_free
);