2 * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 if ((ctx
->buf
= OPENSSL_malloc(size
)) == NULL
) {
120 ERR_raise(ERR_LIB_ASN1
, ERR_R_MALLOC_FAILURE
);
124 ctx
->asn1_class
= V_ASN1_UNIVERSAL
;
125 ctx
->asn1_tag
= V_ASN1_OCTET_STRING
;
126 ctx
->state
= ASN1_STATE_START
;
130 static int asn1_bio_free(BIO
*b
)
132 BIO_ASN1_BUF_CTX
*ctx
;
137 ctx
= BIO_get_data(b
);
141 OPENSSL_free(ctx
->buf
);
143 BIO_set_data(b
, NULL
);
149 static int asn1_bio_write(BIO
*b
, const char *in
, int inl
)
151 BIO_ASN1_BUF_CTX
*ctx
;
152 int wrmax
, wrlen
, ret
;
156 ctx
= BIO_get_data(b
);
158 if (in
== NULL
|| inl
< 0 || ctx
== NULL
|| next
== NULL
)
165 switch (ctx
->state
) {
166 /* Setup prefix data, call it */
167 case ASN1_STATE_START
:
168 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->prefix
,
169 ASN1_STATE_PRE_COPY
, ASN1_STATE_HEADER
))
173 /* Copy any pre data first */
174 case ASN1_STATE_PRE_COPY
:
176 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->prefix_free
,
184 case ASN1_STATE_HEADER
:
185 ctx
->buflen
= ASN1_object_size(0, inl
, ctx
->asn1_tag
) - inl
;
186 if (!ossl_assert(ctx
->buflen
<= ctx
->bufsize
))
189 ASN1_put_object(&p
, 0, inl
, ctx
->asn1_tag
, ctx
->asn1_class
);
191 ctx
->state
= ASN1_STATE_HEADER_COPY
;
195 case ASN1_STATE_HEADER_COPY
:
196 ret
= BIO_write(next
, ctx
->buf
+ ctx
->bufpos
, ctx
->buflen
);
205 ctx
->state
= ASN1_STATE_DATA_COPY
;
210 case ASN1_STATE_DATA_COPY
:
212 if (inl
> ctx
->copylen
)
213 wrmax
= ctx
->copylen
;
216 ret
= BIO_write(next
, in
, wrmax
);
224 if (ctx
->copylen
== 0)
225 ctx
->state
= ASN1_STATE_HEADER
;
232 case ASN1_STATE_POST_COPY
:
233 case ASN1_STATE_DONE
:
234 BIO_clear_retry_flags(b
);
242 BIO_clear_retry_flags(b
);
243 BIO_copy_next_retry(b
);
245 return (wrlen
> 0) ? wrlen
: ret
;
249 static int asn1_bio_flush_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
250 asn1_ps_func
*cleanup
, asn1_bio_state_t next
)
254 if (ctx
->ex_len
<= 0)
257 ret
= BIO_write(BIO_next(b
), ctx
->ex_buf
+ ctx
->ex_pos
, ctx
->ex_len
);
265 cleanup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
);
274 static int asn1_bio_setup_ex(BIO
*b
, BIO_ASN1_BUF_CTX
*ctx
,
276 asn1_bio_state_t ex_state
,
277 asn1_bio_state_t other_state
)
279 if (setup
&& !setup(b
, &ctx
->ex_buf
, &ctx
->ex_len
, &ctx
->ex_arg
)) {
280 BIO_clear_retry_flags(b
);
284 ctx
->state
= ex_state
;
286 ctx
->state
= other_state
;
290 static int asn1_bio_read(BIO
*b
, char *in
, int inl
)
292 BIO
*next
= BIO_next(b
);
295 return BIO_read(next
, in
, inl
);
298 static int asn1_bio_puts(BIO
*b
, const char *str
)
300 return asn1_bio_write(b
, str
, strlen(str
));
303 static int asn1_bio_gets(BIO
*b
, char *str
, int size
)
305 BIO
*next
= BIO_next(b
);
308 return BIO_gets(next
, str
, size
);
311 static long asn1_bio_callback_ctrl(BIO
*b
, int cmd
, BIO_info_cb
*fp
)
313 BIO
*next
= BIO_next(b
);
316 return BIO_callback_ctrl(next
, cmd
, fp
);
319 static long asn1_bio_ctrl(BIO
*b
, int cmd
, long arg1
, void *arg2
)
321 BIO_ASN1_BUF_CTX
*ctx
;
322 BIO_ASN1_EX_FUNCS
*ex_func
;
326 ctx
= BIO_get_data(b
);
332 case BIO_C_SET_PREFIX
:
334 ctx
->prefix
= ex_func
->ex_func
;
335 ctx
->prefix_free
= ex_func
->ex_free_func
;
338 case BIO_C_GET_PREFIX
:
340 ex_func
->ex_func
= ctx
->prefix
;
341 ex_func
->ex_free_func
= ctx
->prefix_free
;
344 case BIO_C_SET_SUFFIX
:
346 ctx
->suffix
= ex_func
->ex_func
;
347 ctx
->suffix_free
= ex_func
->ex_free_func
;
350 case BIO_C_GET_SUFFIX
:
352 ex_func
->ex_func
= ctx
->suffix
;
353 ex_func
->ex_free_func
= ctx
->suffix_free
;
356 case BIO_C_SET_EX_ARG
:
360 case BIO_C_GET_EX_ARG
:
361 *(void **)arg2
= ctx
->ex_arg
;
368 /* Call post function if possible */
369 if (ctx
->state
== ASN1_STATE_HEADER
) {
370 if (!asn1_bio_setup_ex(b
, ctx
, ctx
->suffix
,
371 ASN1_STATE_POST_COPY
, ASN1_STATE_DONE
))
375 if (ctx
->state
== ASN1_STATE_POST_COPY
) {
376 ret
= asn1_bio_flush_ex(b
, ctx
, ctx
->suffix_free
,
382 if (ctx
->state
== ASN1_STATE_DONE
)
383 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
385 BIO_clear_retry_flags(b
);
392 return BIO_ctrl(next
, cmd
, arg1
, arg2
);
399 static int asn1_bio_set_ex(BIO
*b
, int cmd
,
400 asn1_ps_func
*ex_func
, asn1_ps_func
*ex_free_func
)
402 BIO_ASN1_EX_FUNCS extmp
;
403 extmp
.ex_func
= ex_func
;
404 extmp
.ex_free_func
= ex_free_func
;
405 return BIO_ctrl(b
, cmd
, 0, &extmp
);
408 static int asn1_bio_get_ex(BIO
*b
, int cmd
,
409 asn1_ps_func
**ex_func
,
410 asn1_ps_func
**ex_free_func
)
412 BIO_ASN1_EX_FUNCS extmp
;
414 ret
= BIO_ctrl(b
, cmd
, 0, &extmp
);
416 *ex_func
= extmp
.ex_func
;
417 *ex_free_func
= extmp
.ex_free_func
;
422 int BIO_asn1_set_prefix(BIO
*b
, asn1_ps_func
*prefix
,
423 asn1_ps_func
*prefix_free
)
425 return asn1_bio_set_ex(b
, BIO_C_SET_PREFIX
, prefix
, prefix_free
);
428 int BIO_asn1_get_prefix(BIO
*b
, asn1_ps_func
**pprefix
,
429 asn1_ps_func
**pprefix_free
)
431 return asn1_bio_get_ex(b
, BIO_C_GET_PREFIX
, pprefix
, pprefix_free
);
434 int BIO_asn1_set_suffix(BIO
*b
, asn1_ps_func
*suffix
,
435 asn1_ps_func
*suffix_free
)
437 return asn1_bio_set_ex(b
, BIO_C_SET_SUFFIX
, suffix
, suffix_free
);
440 int BIO_asn1_get_suffix(BIO
*b
, asn1_ps_func
**psuffix
,
441 asn1_ps_func
**psuffix_free
)
443 return asn1_bio_get_ex(b
, BIO_C_GET_SUFFIX
, psuffix
, psuffix_free
);