1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci/*
11e1051a39Sopenharmony_ci * Experimental ASN1 BIO. When written through the data is converted to an
12e1051a39Sopenharmony_ci * ASN1 string type: default is OCTET STRING. Additional functions can be
13e1051a39Sopenharmony_ci * provided to add prefix and suffix data.
14e1051a39Sopenharmony_ci */
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_ci#include <string.h>
17e1051a39Sopenharmony_ci#include "internal/bio.h"
18e1051a39Sopenharmony_ci#include <openssl/asn1.h>
19e1051a39Sopenharmony_ci#include "internal/cryptlib.h"
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci/* Must be large enough for biggest tag+length */
22e1051a39Sopenharmony_ci#define DEFAULT_ASN1_BUF_SIZE 20
23e1051a39Sopenharmony_ci
24e1051a39Sopenharmony_citypedef enum {
25e1051a39Sopenharmony_ci    ASN1_STATE_START,
26e1051a39Sopenharmony_ci    ASN1_STATE_PRE_COPY,
27e1051a39Sopenharmony_ci    ASN1_STATE_HEADER,
28e1051a39Sopenharmony_ci    ASN1_STATE_HEADER_COPY,
29e1051a39Sopenharmony_ci    ASN1_STATE_DATA_COPY,
30e1051a39Sopenharmony_ci    ASN1_STATE_POST_COPY,
31e1051a39Sopenharmony_ci    ASN1_STATE_DONE
32e1051a39Sopenharmony_ci} asn1_bio_state_t;
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_citypedef struct BIO_ASN1_EX_FUNCS_st {
35e1051a39Sopenharmony_ci    asn1_ps_func *ex_func;
36e1051a39Sopenharmony_ci    asn1_ps_func *ex_free_func;
37e1051a39Sopenharmony_ci} BIO_ASN1_EX_FUNCS;
38e1051a39Sopenharmony_ci
39e1051a39Sopenharmony_citypedef struct BIO_ASN1_BUF_CTX_t {
40e1051a39Sopenharmony_ci    /* Internal state */
41e1051a39Sopenharmony_ci    asn1_bio_state_t state;
42e1051a39Sopenharmony_ci    /* Internal buffer */
43e1051a39Sopenharmony_ci    unsigned char *buf;
44e1051a39Sopenharmony_ci    /* Size of buffer */
45e1051a39Sopenharmony_ci    int bufsize;
46e1051a39Sopenharmony_ci    /* Current position in buffer */
47e1051a39Sopenharmony_ci    int bufpos;
48e1051a39Sopenharmony_ci    /* Current buffer length */
49e1051a39Sopenharmony_ci    int buflen;
50e1051a39Sopenharmony_ci    /* Amount of data to copy */
51e1051a39Sopenharmony_ci    int copylen;
52e1051a39Sopenharmony_ci    /* Class and tag to use */
53e1051a39Sopenharmony_ci    int asn1_class, asn1_tag;
54e1051a39Sopenharmony_ci    asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
55e1051a39Sopenharmony_ci    /* Extra buffer for prefix and suffix data */
56e1051a39Sopenharmony_ci    unsigned char *ex_buf;
57e1051a39Sopenharmony_ci    int ex_len;
58e1051a39Sopenharmony_ci    int ex_pos;
59e1051a39Sopenharmony_ci    void *ex_arg;
60e1051a39Sopenharmony_ci} BIO_ASN1_BUF_CTX;
61e1051a39Sopenharmony_ci
62e1051a39Sopenharmony_cistatic int asn1_bio_write(BIO *h, const char *buf, int num);
63e1051a39Sopenharmony_cistatic int asn1_bio_read(BIO *h, char *buf, int size);
64e1051a39Sopenharmony_cistatic int asn1_bio_puts(BIO *h, const char *str);
65e1051a39Sopenharmony_cistatic int asn1_bio_gets(BIO *h, char *str, int size);
66e1051a39Sopenharmony_cistatic long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
67e1051a39Sopenharmony_cistatic int asn1_bio_new(BIO *h);
68e1051a39Sopenharmony_cistatic int asn1_bio_free(BIO *data);
69e1051a39Sopenharmony_cistatic long asn1_bio_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
70e1051a39Sopenharmony_ci
71e1051a39Sopenharmony_cistatic int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
72e1051a39Sopenharmony_cistatic int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
73e1051a39Sopenharmony_ci                             asn1_ps_func *cleanup, asn1_bio_state_t next);
74e1051a39Sopenharmony_cistatic int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
75e1051a39Sopenharmony_ci                             asn1_ps_func *setup,
76e1051a39Sopenharmony_ci                             asn1_bio_state_t ex_state,
77e1051a39Sopenharmony_ci                             asn1_bio_state_t other_state);
78e1051a39Sopenharmony_ci
79e1051a39Sopenharmony_cistatic const BIO_METHOD methods_asn1 = {
80e1051a39Sopenharmony_ci    BIO_TYPE_ASN1,
81e1051a39Sopenharmony_ci    "asn1",
82e1051a39Sopenharmony_ci    bwrite_conv,
83e1051a39Sopenharmony_ci    asn1_bio_write,
84e1051a39Sopenharmony_ci    bread_conv,
85e1051a39Sopenharmony_ci    asn1_bio_read,
86e1051a39Sopenharmony_ci    asn1_bio_puts,
87e1051a39Sopenharmony_ci    asn1_bio_gets,
88e1051a39Sopenharmony_ci    asn1_bio_ctrl,
89e1051a39Sopenharmony_ci    asn1_bio_new,
90e1051a39Sopenharmony_ci    asn1_bio_free,
91e1051a39Sopenharmony_ci    asn1_bio_callback_ctrl,
92e1051a39Sopenharmony_ci};
93e1051a39Sopenharmony_ci
94e1051a39Sopenharmony_ciconst BIO_METHOD *BIO_f_asn1(void)
95e1051a39Sopenharmony_ci{
96e1051a39Sopenharmony_ci    return &methods_asn1;
97e1051a39Sopenharmony_ci}
98e1051a39Sopenharmony_ci
99e1051a39Sopenharmony_cistatic int asn1_bio_new(BIO *b)
100e1051a39Sopenharmony_ci{
101e1051a39Sopenharmony_ci    BIO_ASN1_BUF_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
102e1051a39Sopenharmony_ci
103e1051a39Sopenharmony_ci    if (ctx == NULL) {
104e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
105e1051a39Sopenharmony_ci        return 0;
106e1051a39Sopenharmony_ci    }
107e1051a39Sopenharmony_ci    if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
108e1051a39Sopenharmony_ci        OPENSSL_free(ctx);
109e1051a39Sopenharmony_ci        return 0;
110e1051a39Sopenharmony_ci    }
111e1051a39Sopenharmony_ci    BIO_set_data(b, ctx);
112e1051a39Sopenharmony_ci    BIO_set_init(b, 1);
113e1051a39Sopenharmony_ci
114e1051a39Sopenharmony_ci    return 1;
115e1051a39Sopenharmony_ci}
116e1051a39Sopenharmony_ci
117e1051a39Sopenharmony_cistatic int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
118e1051a39Sopenharmony_ci{
119e1051a39Sopenharmony_ci    if (size <= 0 || (ctx->buf = OPENSSL_malloc(size)) == NULL) {
120e1051a39Sopenharmony_ci        ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE);
121e1051a39Sopenharmony_ci        return 0;
122e1051a39Sopenharmony_ci    }
123e1051a39Sopenharmony_ci    ctx->bufsize = size;
124e1051a39Sopenharmony_ci    ctx->asn1_class = V_ASN1_UNIVERSAL;
125e1051a39Sopenharmony_ci    ctx->asn1_tag = V_ASN1_OCTET_STRING;
126e1051a39Sopenharmony_ci    ctx->state = ASN1_STATE_START;
127e1051a39Sopenharmony_ci    return 1;
128e1051a39Sopenharmony_ci}
129e1051a39Sopenharmony_ci
130e1051a39Sopenharmony_cistatic int asn1_bio_free(BIO *b)
131e1051a39Sopenharmony_ci{
132e1051a39Sopenharmony_ci    BIO_ASN1_BUF_CTX *ctx;
133e1051a39Sopenharmony_ci
134e1051a39Sopenharmony_ci    if (b == NULL)
135e1051a39Sopenharmony_ci        return 0;
136e1051a39Sopenharmony_ci
137e1051a39Sopenharmony_ci    ctx = BIO_get_data(b);
138e1051a39Sopenharmony_ci    if (ctx == NULL)
139e1051a39Sopenharmony_ci        return 0;
140e1051a39Sopenharmony_ci
141e1051a39Sopenharmony_ci    if (ctx->prefix_free != NULL)
142e1051a39Sopenharmony_ci        ctx->prefix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
143e1051a39Sopenharmony_ci    if (ctx->suffix_free != NULL)
144e1051a39Sopenharmony_ci        ctx->suffix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
145e1051a39Sopenharmony_ci
146e1051a39Sopenharmony_ci    OPENSSL_free(ctx->buf);
147e1051a39Sopenharmony_ci    OPENSSL_free(ctx);
148e1051a39Sopenharmony_ci    BIO_set_data(b, NULL);
149e1051a39Sopenharmony_ci    BIO_set_init(b, 0);
150e1051a39Sopenharmony_ci
151e1051a39Sopenharmony_ci    return 1;
152e1051a39Sopenharmony_ci}
153e1051a39Sopenharmony_ci
154e1051a39Sopenharmony_cistatic int asn1_bio_write(BIO *b, const char *in, int inl)
155e1051a39Sopenharmony_ci{
156e1051a39Sopenharmony_ci    BIO_ASN1_BUF_CTX *ctx;
157e1051a39Sopenharmony_ci    int wrmax, wrlen, ret;
158e1051a39Sopenharmony_ci    unsigned char *p;
159e1051a39Sopenharmony_ci    BIO *next;
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci    ctx = BIO_get_data(b);
162e1051a39Sopenharmony_ci    next = BIO_next(b);
163e1051a39Sopenharmony_ci    if (in == NULL || inl < 0 || ctx == NULL || next == NULL)
164e1051a39Sopenharmony_ci        return 0;
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci    wrlen = 0;
167e1051a39Sopenharmony_ci    ret = -1;
168e1051a39Sopenharmony_ci
169e1051a39Sopenharmony_ci    for (;;) {
170e1051a39Sopenharmony_ci        switch (ctx->state) {
171e1051a39Sopenharmony_ci            /* Setup prefix data, call it */
172e1051a39Sopenharmony_ci        case ASN1_STATE_START:
173e1051a39Sopenharmony_ci            if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
174e1051a39Sopenharmony_ci                                   ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
175e1051a39Sopenharmony_ci                return -1;
176e1051a39Sopenharmony_ci            break;
177e1051a39Sopenharmony_ci
178e1051a39Sopenharmony_ci            /* Copy any pre data first */
179e1051a39Sopenharmony_ci        case ASN1_STATE_PRE_COPY:
180e1051a39Sopenharmony_ci
181e1051a39Sopenharmony_ci            ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
182e1051a39Sopenharmony_ci                                    ASN1_STATE_HEADER);
183e1051a39Sopenharmony_ci
184e1051a39Sopenharmony_ci            if (ret <= 0)
185e1051a39Sopenharmony_ci                goto done;
186e1051a39Sopenharmony_ci
187e1051a39Sopenharmony_ci            break;
188e1051a39Sopenharmony_ci
189e1051a39Sopenharmony_ci        case ASN1_STATE_HEADER:
190e1051a39Sopenharmony_ci            ctx->buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
191e1051a39Sopenharmony_ci            if (!ossl_assert(ctx->buflen <= ctx->bufsize))
192e1051a39Sopenharmony_ci                return -1;
193e1051a39Sopenharmony_ci            p = ctx->buf;
194e1051a39Sopenharmony_ci            ASN1_put_object(&p, 0, inl, ctx->asn1_tag, ctx->asn1_class);
195e1051a39Sopenharmony_ci            ctx->copylen = inl;
196e1051a39Sopenharmony_ci            ctx->state = ASN1_STATE_HEADER_COPY;
197e1051a39Sopenharmony_ci
198e1051a39Sopenharmony_ci            break;
199e1051a39Sopenharmony_ci
200e1051a39Sopenharmony_ci        case ASN1_STATE_HEADER_COPY:
201e1051a39Sopenharmony_ci            ret = BIO_write(next, ctx->buf + ctx->bufpos, ctx->buflen);
202e1051a39Sopenharmony_ci            if (ret <= 0)
203e1051a39Sopenharmony_ci                goto done;
204e1051a39Sopenharmony_ci
205e1051a39Sopenharmony_ci            ctx->buflen -= ret;
206e1051a39Sopenharmony_ci            if (ctx->buflen)
207e1051a39Sopenharmony_ci                ctx->bufpos += ret;
208e1051a39Sopenharmony_ci            else {
209e1051a39Sopenharmony_ci                ctx->bufpos = 0;
210e1051a39Sopenharmony_ci                ctx->state = ASN1_STATE_DATA_COPY;
211e1051a39Sopenharmony_ci            }
212e1051a39Sopenharmony_ci
213e1051a39Sopenharmony_ci            break;
214e1051a39Sopenharmony_ci
215e1051a39Sopenharmony_ci        case ASN1_STATE_DATA_COPY:
216e1051a39Sopenharmony_ci
217e1051a39Sopenharmony_ci            if (inl > ctx->copylen)
218e1051a39Sopenharmony_ci                wrmax = ctx->copylen;
219e1051a39Sopenharmony_ci            else
220e1051a39Sopenharmony_ci                wrmax = inl;
221e1051a39Sopenharmony_ci            ret = BIO_write(next, in, wrmax);
222e1051a39Sopenharmony_ci            if (ret <= 0)
223e1051a39Sopenharmony_ci                goto done;
224e1051a39Sopenharmony_ci            wrlen += ret;
225e1051a39Sopenharmony_ci            ctx->copylen -= ret;
226e1051a39Sopenharmony_ci            in += ret;
227e1051a39Sopenharmony_ci            inl -= ret;
228e1051a39Sopenharmony_ci
229e1051a39Sopenharmony_ci            if (ctx->copylen == 0)
230e1051a39Sopenharmony_ci                ctx->state = ASN1_STATE_HEADER;
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci            if (inl == 0)
233e1051a39Sopenharmony_ci                goto done;
234e1051a39Sopenharmony_ci
235e1051a39Sopenharmony_ci            break;
236e1051a39Sopenharmony_ci
237e1051a39Sopenharmony_ci        case ASN1_STATE_POST_COPY:
238e1051a39Sopenharmony_ci        case ASN1_STATE_DONE:
239e1051a39Sopenharmony_ci            BIO_clear_retry_flags(b);
240e1051a39Sopenharmony_ci            return 0;
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_ci        }
243e1051a39Sopenharmony_ci
244e1051a39Sopenharmony_ci    }
245e1051a39Sopenharmony_ci
246e1051a39Sopenharmony_ci done:
247e1051a39Sopenharmony_ci    BIO_clear_retry_flags(b);
248e1051a39Sopenharmony_ci    BIO_copy_next_retry(b);
249e1051a39Sopenharmony_ci
250e1051a39Sopenharmony_ci    return (wrlen > 0) ? wrlen : ret;
251e1051a39Sopenharmony_ci
252e1051a39Sopenharmony_ci}
253e1051a39Sopenharmony_ci
254e1051a39Sopenharmony_cistatic int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
255e1051a39Sopenharmony_ci                             asn1_ps_func *cleanup, asn1_bio_state_t next)
256e1051a39Sopenharmony_ci{
257e1051a39Sopenharmony_ci    int ret;
258e1051a39Sopenharmony_ci
259e1051a39Sopenharmony_ci    if (ctx->ex_len <= 0)
260e1051a39Sopenharmony_ci        return 1;
261e1051a39Sopenharmony_ci    for (;;) {
262e1051a39Sopenharmony_ci        ret = BIO_write(BIO_next(b), ctx->ex_buf + ctx->ex_pos, ctx->ex_len);
263e1051a39Sopenharmony_ci        if (ret <= 0)
264e1051a39Sopenharmony_ci            break;
265e1051a39Sopenharmony_ci        ctx->ex_len -= ret;
266e1051a39Sopenharmony_ci        if (ctx->ex_len > 0)
267e1051a39Sopenharmony_ci            ctx->ex_pos += ret;
268e1051a39Sopenharmony_ci        else {
269e1051a39Sopenharmony_ci            if (cleanup)
270e1051a39Sopenharmony_ci                cleanup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
271e1051a39Sopenharmony_ci            ctx->state = next;
272e1051a39Sopenharmony_ci            ctx->ex_pos = 0;
273e1051a39Sopenharmony_ci            break;
274e1051a39Sopenharmony_ci        }
275e1051a39Sopenharmony_ci    }
276e1051a39Sopenharmony_ci    return ret;
277e1051a39Sopenharmony_ci}
278e1051a39Sopenharmony_ci
279e1051a39Sopenharmony_cistatic int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
280e1051a39Sopenharmony_ci                             asn1_ps_func *setup,
281e1051a39Sopenharmony_ci                             asn1_bio_state_t ex_state,
282e1051a39Sopenharmony_ci                             asn1_bio_state_t other_state)
283e1051a39Sopenharmony_ci{
284e1051a39Sopenharmony_ci    if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
285e1051a39Sopenharmony_ci        BIO_clear_retry_flags(b);
286e1051a39Sopenharmony_ci        return 0;
287e1051a39Sopenharmony_ci    }
288e1051a39Sopenharmony_ci    if (ctx->ex_len > 0)
289e1051a39Sopenharmony_ci        ctx->state = ex_state;
290e1051a39Sopenharmony_ci    else
291e1051a39Sopenharmony_ci        ctx->state = other_state;
292e1051a39Sopenharmony_ci    return 1;
293e1051a39Sopenharmony_ci}
294e1051a39Sopenharmony_ci
295e1051a39Sopenharmony_cistatic int asn1_bio_read(BIO *b, char *in, int inl)
296e1051a39Sopenharmony_ci{
297e1051a39Sopenharmony_ci    BIO *next = BIO_next(b);
298e1051a39Sopenharmony_ci    if (next == NULL)
299e1051a39Sopenharmony_ci        return 0;
300e1051a39Sopenharmony_ci    return BIO_read(next, in, inl);
301e1051a39Sopenharmony_ci}
302e1051a39Sopenharmony_ci
303e1051a39Sopenharmony_cistatic int asn1_bio_puts(BIO *b, const char *str)
304e1051a39Sopenharmony_ci{
305e1051a39Sopenharmony_ci    return asn1_bio_write(b, str, strlen(str));
306e1051a39Sopenharmony_ci}
307e1051a39Sopenharmony_ci
308e1051a39Sopenharmony_cistatic int asn1_bio_gets(BIO *b, char *str, int size)
309e1051a39Sopenharmony_ci{
310e1051a39Sopenharmony_ci    BIO *next = BIO_next(b);
311e1051a39Sopenharmony_ci    if (next == NULL)
312e1051a39Sopenharmony_ci        return 0;
313e1051a39Sopenharmony_ci    return BIO_gets(next, str, size);
314e1051a39Sopenharmony_ci}
315e1051a39Sopenharmony_ci
316e1051a39Sopenharmony_cistatic long asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
317e1051a39Sopenharmony_ci{
318e1051a39Sopenharmony_ci    BIO *next = BIO_next(b);
319e1051a39Sopenharmony_ci    if (next == NULL)
320e1051a39Sopenharmony_ci        return 0;
321e1051a39Sopenharmony_ci    return BIO_callback_ctrl(next, cmd, fp);
322e1051a39Sopenharmony_ci}
323e1051a39Sopenharmony_ci
324e1051a39Sopenharmony_cistatic long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
325e1051a39Sopenharmony_ci{
326e1051a39Sopenharmony_ci    BIO_ASN1_BUF_CTX *ctx;
327e1051a39Sopenharmony_ci    BIO_ASN1_EX_FUNCS *ex_func;
328e1051a39Sopenharmony_ci    long ret = 1;
329e1051a39Sopenharmony_ci    BIO *next;
330e1051a39Sopenharmony_ci
331e1051a39Sopenharmony_ci    ctx = BIO_get_data(b);
332e1051a39Sopenharmony_ci    if (ctx == NULL)
333e1051a39Sopenharmony_ci        return 0;
334e1051a39Sopenharmony_ci    next = BIO_next(b);
335e1051a39Sopenharmony_ci    switch (cmd) {
336e1051a39Sopenharmony_ci
337e1051a39Sopenharmony_ci    case BIO_C_SET_PREFIX:
338e1051a39Sopenharmony_ci        ex_func = arg2;
339e1051a39Sopenharmony_ci        ctx->prefix = ex_func->ex_func;
340e1051a39Sopenharmony_ci        ctx->prefix_free = ex_func->ex_free_func;
341e1051a39Sopenharmony_ci        break;
342e1051a39Sopenharmony_ci
343e1051a39Sopenharmony_ci    case BIO_C_GET_PREFIX:
344e1051a39Sopenharmony_ci        ex_func = arg2;
345e1051a39Sopenharmony_ci        ex_func->ex_func = ctx->prefix;
346e1051a39Sopenharmony_ci        ex_func->ex_free_func = ctx->prefix_free;
347e1051a39Sopenharmony_ci        break;
348e1051a39Sopenharmony_ci
349e1051a39Sopenharmony_ci    case BIO_C_SET_SUFFIX:
350e1051a39Sopenharmony_ci        ex_func = arg2;
351e1051a39Sopenharmony_ci        ctx->suffix = ex_func->ex_func;
352e1051a39Sopenharmony_ci        ctx->suffix_free = ex_func->ex_free_func;
353e1051a39Sopenharmony_ci        break;
354e1051a39Sopenharmony_ci
355e1051a39Sopenharmony_ci    case BIO_C_GET_SUFFIX:
356e1051a39Sopenharmony_ci        ex_func = arg2;
357e1051a39Sopenharmony_ci        ex_func->ex_func = ctx->suffix;
358e1051a39Sopenharmony_ci        ex_func->ex_free_func = ctx->suffix_free;
359e1051a39Sopenharmony_ci        break;
360e1051a39Sopenharmony_ci
361e1051a39Sopenharmony_ci    case BIO_C_SET_EX_ARG:
362e1051a39Sopenharmony_ci        ctx->ex_arg = arg2;
363e1051a39Sopenharmony_ci        break;
364e1051a39Sopenharmony_ci
365e1051a39Sopenharmony_ci    case BIO_C_GET_EX_ARG:
366e1051a39Sopenharmony_ci        *(void **)arg2 = ctx->ex_arg;
367e1051a39Sopenharmony_ci        break;
368e1051a39Sopenharmony_ci
369e1051a39Sopenharmony_ci    case BIO_CTRL_FLUSH:
370e1051a39Sopenharmony_ci        if (next == NULL)
371e1051a39Sopenharmony_ci            return 0;
372e1051a39Sopenharmony_ci
373e1051a39Sopenharmony_ci        /* Call post function if possible */
374e1051a39Sopenharmony_ci        if (ctx->state == ASN1_STATE_HEADER) {
375e1051a39Sopenharmony_ci            if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
376e1051a39Sopenharmony_ci                                   ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
377e1051a39Sopenharmony_ci                return 0;
378e1051a39Sopenharmony_ci        }
379e1051a39Sopenharmony_ci
380e1051a39Sopenharmony_ci        if (ctx->state == ASN1_STATE_POST_COPY) {
381e1051a39Sopenharmony_ci            ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
382e1051a39Sopenharmony_ci                                    ASN1_STATE_DONE);
383e1051a39Sopenharmony_ci            if (ret <= 0)
384e1051a39Sopenharmony_ci                return ret;
385e1051a39Sopenharmony_ci        }
386e1051a39Sopenharmony_ci
387e1051a39Sopenharmony_ci        if (ctx->state == ASN1_STATE_DONE)
388e1051a39Sopenharmony_ci            return BIO_ctrl(next, cmd, arg1, arg2);
389e1051a39Sopenharmony_ci        else {
390e1051a39Sopenharmony_ci            BIO_clear_retry_flags(b);
391e1051a39Sopenharmony_ci            return 0;
392e1051a39Sopenharmony_ci        }
393e1051a39Sopenharmony_ci
394e1051a39Sopenharmony_ci    default:
395e1051a39Sopenharmony_ci        if (next == NULL)
396e1051a39Sopenharmony_ci            return 0;
397e1051a39Sopenharmony_ci        return BIO_ctrl(next, cmd, arg1, arg2);
398e1051a39Sopenharmony_ci
399e1051a39Sopenharmony_ci    }
400e1051a39Sopenharmony_ci
401e1051a39Sopenharmony_ci    return ret;
402e1051a39Sopenharmony_ci}
403e1051a39Sopenharmony_ci
404e1051a39Sopenharmony_cistatic int asn1_bio_set_ex(BIO *b, int cmd,
405e1051a39Sopenharmony_ci                           asn1_ps_func *ex_func, asn1_ps_func *ex_free_func)
406e1051a39Sopenharmony_ci{
407e1051a39Sopenharmony_ci    BIO_ASN1_EX_FUNCS extmp;
408e1051a39Sopenharmony_ci    extmp.ex_func = ex_func;
409e1051a39Sopenharmony_ci    extmp.ex_free_func = ex_free_func;
410e1051a39Sopenharmony_ci    return BIO_ctrl(b, cmd, 0, &extmp);
411e1051a39Sopenharmony_ci}
412e1051a39Sopenharmony_ci
413e1051a39Sopenharmony_cistatic int asn1_bio_get_ex(BIO *b, int cmd,
414e1051a39Sopenharmony_ci                           asn1_ps_func **ex_func,
415e1051a39Sopenharmony_ci                           asn1_ps_func **ex_free_func)
416e1051a39Sopenharmony_ci{
417e1051a39Sopenharmony_ci    BIO_ASN1_EX_FUNCS extmp;
418e1051a39Sopenharmony_ci    int ret;
419e1051a39Sopenharmony_ci    ret = BIO_ctrl(b, cmd, 0, &extmp);
420e1051a39Sopenharmony_ci    if (ret > 0) {
421e1051a39Sopenharmony_ci        *ex_func = extmp.ex_func;
422e1051a39Sopenharmony_ci        *ex_free_func = extmp.ex_free_func;
423e1051a39Sopenharmony_ci    }
424e1051a39Sopenharmony_ci    return ret;
425e1051a39Sopenharmony_ci}
426e1051a39Sopenharmony_ci
427e1051a39Sopenharmony_ciint BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix,
428e1051a39Sopenharmony_ci                        asn1_ps_func *prefix_free)
429e1051a39Sopenharmony_ci{
430e1051a39Sopenharmony_ci    return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
431e1051a39Sopenharmony_ci}
432e1051a39Sopenharmony_ci
433e1051a39Sopenharmony_ciint BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix,
434e1051a39Sopenharmony_ci                        asn1_ps_func **pprefix_free)
435e1051a39Sopenharmony_ci{
436e1051a39Sopenharmony_ci    return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
437e1051a39Sopenharmony_ci}
438e1051a39Sopenharmony_ci
439e1051a39Sopenharmony_ciint BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix,
440e1051a39Sopenharmony_ci                        asn1_ps_func *suffix_free)
441e1051a39Sopenharmony_ci{
442e1051a39Sopenharmony_ci    return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
443e1051a39Sopenharmony_ci}
444e1051a39Sopenharmony_ci
445e1051a39Sopenharmony_ciint BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix,
446e1051a39Sopenharmony_ci                        asn1_ps_func **psuffix_free)
447e1051a39Sopenharmony_ci{
448e1051a39Sopenharmony_ci    return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
449e1051a39Sopenharmony_ci}
450