xref: /openbsd-src/lib/libcrypto/asn1/bio_asn1.c (revision 859caa332e6ddb5c421d0566d9cfa5e301f2801a)
1*859caa33Stb /* $OpenBSD: bio_asn1.c,v 1.23 2023/07/28 09:58:30 tb Exp $ */
2e2ee43e0Sdjm /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3e2ee43e0Sdjm  * project.
4e2ee43e0Sdjm  */
5e2ee43e0Sdjm /* ====================================================================
6e2ee43e0Sdjm  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
7e2ee43e0Sdjm  *
8e2ee43e0Sdjm  * Redistribution and use in source and binary forms, with or without
9e2ee43e0Sdjm  * modification, are permitted provided that the following conditions
10e2ee43e0Sdjm  * are met:
11e2ee43e0Sdjm  *
12e2ee43e0Sdjm  * 1. Redistributions of source code must retain the above copyright
13e2ee43e0Sdjm  *    notice, this list of conditions and the following disclaimer.
14e2ee43e0Sdjm  *
15e2ee43e0Sdjm  * 2. Redistributions in binary form must reproduce the above copyright
16e2ee43e0Sdjm  *    notice, this list of conditions and the following disclaimer in
17e2ee43e0Sdjm  *    the documentation and/or other materials provided with the
18e2ee43e0Sdjm  *    distribution.
19e2ee43e0Sdjm  *
20e2ee43e0Sdjm  * 3. All advertising materials mentioning features or use of this
21e2ee43e0Sdjm  *    software must display the following acknowledgment:
22e2ee43e0Sdjm  *    "This product includes software developed by the OpenSSL Project
23e2ee43e0Sdjm  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24e2ee43e0Sdjm  *
25e2ee43e0Sdjm  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26e2ee43e0Sdjm  *    endorse or promote products derived from this software without
27e2ee43e0Sdjm  *    prior written permission. For written permission, please contact
28e2ee43e0Sdjm  *    licensing@OpenSSL.org.
29e2ee43e0Sdjm  *
30e2ee43e0Sdjm  * 5. Products derived from this software may not be called "OpenSSL"
31e2ee43e0Sdjm  *    nor may "OpenSSL" appear in their names without prior written
32e2ee43e0Sdjm  *    permission of the OpenSSL Project.
33e2ee43e0Sdjm  *
34e2ee43e0Sdjm  * 6. Redistributions of any form whatsoever must retain the following
35e2ee43e0Sdjm  *    acknowledgment:
36e2ee43e0Sdjm  *    "This product includes software developed by the OpenSSL Project
37e2ee43e0Sdjm  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38e2ee43e0Sdjm  *
39e2ee43e0Sdjm  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40e2ee43e0Sdjm  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41e2ee43e0Sdjm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42e2ee43e0Sdjm  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43e2ee43e0Sdjm  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44e2ee43e0Sdjm  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45e2ee43e0Sdjm  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46e2ee43e0Sdjm  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47e2ee43e0Sdjm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48e2ee43e0Sdjm  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49e2ee43e0Sdjm  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50e2ee43e0Sdjm  * OF THE POSSIBILITY OF SUCH DAMAGE.
51e2ee43e0Sdjm  * ====================================================================
52e2ee43e0Sdjm  *
53e2ee43e0Sdjm  * This product includes cryptographic software written by Eric Young
54e2ee43e0Sdjm  * (eay@cryptsoft.com).  This product includes software written by Tim
55e2ee43e0Sdjm  * Hudson (tjh@cryptsoft.com).
56e2ee43e0Sdjm  *
57e2ee43e0Sdjm  */
58e2ee43e0Sdjm 
59e2ee43e0Sdjm /* Experimental ASN1 BIO. When written through the data is converted
60e2ee43e0Sdjm  * to an ASN1 string type: default is OCTET STRING. Additional functions
61e2ee43e0Sdjm  * can be provided to add prefix and suffix data.
62e2ee43e0Sdjm  */
63e2ee43e0Sdjm 
64a8913c44Sjsing #include <stdlib.h>
65e2ee43e0Sdjm #include <string.h>
66a8913c44Sjsing 
67e2ee43e0Sdjm #include <openssl/bio.h>
68e2ee43e0Sdjm #include <openssl/asn1.h>
69e2ee43e0Sdjm 
7094b1984eStb #include "bio_local.h"
7194b1984eStb 
72*859caa33Stb #define BIO_C_SET_PREFIX			149
73*859caa33Stb #define BIO_C_SET_SUFFIX			151
74*859caa33Stb 
75e2ee43e0Sdjm /* Must be large enough for biggest tag+length */
76e2ee43e0Sdjm #define DEFAULT_ASN1_BUF_SIZE 20
77e2ee43e0Sdjm 
78f309b230Sjsing typedef enum {
79e2ee43e0Sdjm 	ASN1_STATE_START,
80e2ee43e0Sdjm 	ASN1_STATE_PRE_COPY,
81e2ee43e0Sdjm 	ASN1_STATE_HEADER,
82e2ee43e0Sdjm 	ASN1_STATE_HEADER_COPY,
83e2ee43e0Sdjm 	ASN1_STATE_DATA_COPY,
84e2ee43e0Sdjm 	ASN1_STATE_POST_COPY,
85e2ee43e0Sdjm 	ASN1_STATE_DONE
86e2ee43e0Sdjm } asn1_bio_state_t;
87e2ee43e0Sdjm 
88f309b230Sjsing typedef struct BIO_ASN1_EX_FUNCS_st {
89e2ee43e0Sdjm 	asn1_ps_func	*ex_func;
90e2ee43e0Sdjm 	asn1_ps_func	*ex_free_func;
91e2ee43e0Sdjm } BIO_ASN1_EX_FUNCS;
92e2ee43e0Sdjm 
93f309b230Sjsing typedef struct BIO_ASN1_BUF_CTX_t {
94e2ee43e0Sdjm 	/* Internal state */
95e2ee43e0Sdjm 	asn1_bio_state_t state;
96e2ee43e0Sdjm 	/* Internal buffer */
97e2ee43e0Sdjm 	unsigned char *buf;
98e2ee43e0Sdjm 	/* Size of buffer */
99e2ee43e0Sdjm 	int bufsize;
100e2ee43e0Sdjm 	/* Current position in buffer */
101e2ee43e0Sdjm 	int bufpos;
102e2ee43e0Sdjm 	/* Current buffer length */
103e2ee43e0Sdjm 	int buflen;
104e2ee43e0Sdjm 	/* Amount of data to copy */
105e2ee43e0Sdjm 	int copylen;
106e2ee43e0Sdjm 	/* Class and tag to use */
107e2ee43e0Sdjm 	int asn1_class, asn1_tag;
108e2ee43e0Sdjm 	asn1_ps_func *prefix, *prefix_free, *suffix, *suffix_free;
109e2ee43e0Sdjm 	/* Extra buffer for prefix and suffix data */
110e2ee43e0Sdjm 	unsigned char *ex_buf;
111e2ee43e0Sdjm 	int ex_len;
112e2ee43e0Sdjm 	int ex_pos;
113e2ee43e0Sdjm 	void *ex_arg;
114e2ee43e0Sdjm } BIO_ASN1_BUF_CTX;
115e2ee43e0Sdjm 
116e2ee43e0Sdjm 
117e2ee43e0Sdjm static int asn1_bio_write(BIO *h, const char *buf, int num);
118e2ee43e0Sdjm static int asn1_bio_read(BIO *h, char *buf, int size);
119e2ee43e0Sdjm static int asn1_bio_puts(BIO *h, const char *str);
120e2ee43e0Sdjm static int asn1_bio_gets(BIO *h, char *str, int size);
121e2ee43e0Sdjm static long asn1_bio_ctrl(BIO *h, int cmd, long arg1, void *arg2);
122e2ee43e0Sdjm static int asn1_bio_new(BIO *h);
123e2ee43e0Sdjm static int asn1_bio_free(BIO *data);
124818427c5Stb static long asn1_bio_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
125e2ee43e0Sdjm 
126e2ee43e0Sdjm static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
127e2ee43e0Sdjm     asn1_ps_func *cleanup, asn1_bio_state_t next);
128e2ee43e0Sdjm static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
129f309b230Sjsing     asn1_ps_func *setup, asn1_bio_state_t ex_state,
130e2ee43e0Sdjm     asn1_bio_state_t other_state);
131e2ee43e0Sdjm 
1326dc76777Stb static const BIO_METHOD methods_asn1 = {
133e402ce74Smiod 	.type = BIO_TYPE_ASN1,
134e402ce74Smiod 	.name = "asn1",
135e402ce74Smiod 	.bwrite = asn1_bio_write,
136e402ce74Smiod 	.bread = asn1_bio_read,
137e402ce74Smiod 	.bputs = asn1_bio_puts,
138e402ce74Smiod 	.bgets = asn1_bio_gets,
139e402ce74Smiod 	.ctrl = asn1_bio_ctrl,
140e402ce74Smiod 	.create = asn1_bio_new,
141e402ce74Smiod 	.destroy = asn1_bio_free,
142e402ce74Smiod 	.callback_ctrl = asn1_bio_callback_ctrl
143e2ee43e0Sdjm };
144e2ee43e0Sdjm 
1456dc76777Stb const BIO_METHOD *
BIO_f_asn1(void)146f309b230Sjsing BIO_f_asn1(void)
147e2ee43e0Sdjm {
148e2ee43e0Sdjm 	return (&methods_asn1);
149e2ee43e0Sdjm }
150e2ee43e0Sdjm 
151f309b230Sjsing static int
asn1_bio_new(BIO * b)152f309b230Sjsing asn1_bio_new(BIO *b)
153e2ee43e0Sdjm {
154e2ee43e0Sdjm 	BIO_ASN1_BUF_CTX *ctx;
155a925b4f7Sschwarze 
156a925b4f7Sschwarze 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
157e2ee43e0Sdjm 		return 0;
158a925b4f7Sschwarze 
159a925b4f7Sschwarze 	if ((ctx->buf = malloc(DEFAULT_ASN1_BUF_SIZE)) == NULL) {
160603b910fSjsg 		free(ctx);
161e2ee43e0Sdjm 		return 0;
162603b910fSjsg 	}
163a925b4f7Sschwarze 	ctx->bufsize = DEFAULT_ASN1_BUF_SIZE;
164a925b4f7Sschwarze 	ctx->asn1_class = V_ASN1_UNIVERSAL;
165a925b4f7Sschwarze 	ctx->asn1_tag = V_ASN1_OCTET_STRING;
166a925b4f7Sschwarze 	ctx->state = ASN1_STATE_START;
167a925b4f7Sschwarze 
168e2ee43e0Sdjm 	b->init = 1;
169cc21081eStb 	b->ptr = ctx;
170e2ee43e0Sdjm 	b->flags = 0;
171e2ee43e0Sdjm 
172e2ee43e0Sdjm 	return 1;
173e2ee43e0Sdjm }
174e2ee43e0Sdjm 
175f309b230Sjsing static int
asn1_bio_free(BIO * b)176f309b230Sjsing asn1_bio_free(BIO *b)
177e2ee43e0Sdjm {
178cc21081eStb 	BIO_ASN1_BUF_CTX *ctx = b->ptr;
179f309b230Sjsing 
180e2ee43e0Sdjm 	if (ctx == NULL)
181e2ee43e0Sdjm 		return 0;
1823319543dStb 
1833319543dStb 	if (ctx->prefix_free != NULL)
1843319543dStb 		ctx->prefix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
1853319543dStb 	if (ctx->suffix_free != NULL)
1863319543dStb 		ctx->suffix_free(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg);
1873319543dStb 
1886f3a6cb1Sbeck 	free(ctx->buf);
1896f3a6cb1Sbeck 	free(ctx);
190e2ee43e0Sdjm 	b->init = 0;
191e2ee43e0Sdjm 	b->ptr = NULL;
192e2ee43e0Sdjm 	b->flags = 0;
193e2ee43e0Sdjm 	return 1;
194e2ee43e0Sdjm }
195e2ee43e0Sdjm 
196f309b230Sjsing static int
asn1_bio_write(BIO * b,const char * in,int inl)197f309b230Sjsing asn1_bio_write(BIO *b, const char *in , int inl)
198e2ee43e0Sdjm {
199e2ee43e0Sdjm 	BIO_ASN1_BUF_CTX *ctx;
2008ad85bf2Smiod 	int wrmax, wrlen, ret, buflen;
201e2ee43e0Sdjm 	unsigned char *p;
202f309b230Sjsing 
203e2ee43e0Sdjm 	if (!in || (inl < 0) || (b->next_bio == NULL))
204e2ee43e0Sdjm 		return 0;
205cc21081eStb 
206cc21081eStb 	if ((ctx = b->ptr) == NULL)
207e2ee43e0Sdjm 		return 0;
208e2ee43e0Sdjm 
209e2ee43e0Sdjm 	wrlen = 0;
210e2ee43e0Sdjm 	ret = -1;
211e2ee43e0Sdjm 
212cc777fd4Stedu 	for (;;) {
213cc777fd4Stedu 		switch (ctx->state) {
214e2ee43e0Sdjm 
215e2ee43e0Sdjm 			/* Setup prefix data, call it */
216e2ee43e0Sdjm 		case ASN1_STATE_START:
217e2ee43e0Sdjm 			if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
218e2ee43e0Sdjm 				    ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
219e2ee43e0Sdjm 				return 0;
220e2ee43e0Sdjm 			break;
221e2ee43e0Sdjm 
222e2ee43e0Sdjm 			/* Copy any pre data first */
223e2ee43e0Sdjm 		case ASN1_STATE_PRE_COPY:
224e2ee43e0Sdjm 			ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
225e2ee43e0Sdjm 			    ASN1_STATE_HEADER);
226e2ee43e0Sdjm 			if (ret <= 0)
227e2ee43e0Sdjm 				goto done;
228e2ee43e0Sdjm 			break;
229e2ee43e0Sdjm 
230e2ee43e0Sdjm 		case ASN1_STATE_HEADER:
2318ad85bf2Smiod 			buflen = ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
2328ad85bf2Smiod 			if (buflen <= 0 || buflen > ctx->bufsize)
2338ad85bf2Smiod 				return -1;
2348ad85bf2Smiod 			ctx->buflen = buflen;
235e2ee43e0Sdjm 			p = ctx->buf;
236e2ee43e0Sdjm 			ASN1_put_object(&p, 0, inl,
237e2ee43e0Sdjm 			    ctx->asn1_tag, ctx->asn1_class);
238e2ee43e0Sdjm 			ctx->copylen = inl;
239e2ee43e0Sdjm 			ctx->state = ASN1_STATE_HEADER_COPY;
240e2ee43e0Sdjm 			break;
241e2ee43e0Sdjm 
242e2ee43e0Sdjm 		case ASN1_STATE_HEADER_COPY:
243e2ee43e0Sdjm 			ret = BIO_write(b->next_bio,
244e2ee43e0Sdjm 			    ctx->buf + ctx->bufpos, ctx->buflen);
245e2ee43e0Sdjm 			if (ret <= 0)
246e2ee43e0Sdjm 				goto done;
247e2ee43e0Sdjm 
248e2ee43e0Sdjm 			ctx->buflen -= ret;
249e2ee43e0Sdjm 			if (ctx->buflen)
250e2ee43e0Sdjm 				ctx->bufpos += ret;
251cc777fd4Stedu 			else {
252e2ee43e0Sdjm 				ctx->bufpos = 0;
253e2ee43e0Sdjm 				ctx->state = ASN1_STATE_DATA_COPY;
254e2ee43e0Sdjm 			}
255e2ee43e0Sdjm 			break;
256e2ee43e0Sdjm 
257e2ee43e0Sdjm 		case ASN1_STATE_DATA_COPY:
258e2ee43e0Sdjm 
259e2ee43e0Sdjm 			if (inl > ctx->copylen)
260e2ee43e0Sdjm 				wrmax = ctx->copylen;
261e2ee43e0Sdjm 			else
262e2ee43e0Sdjm 				wrmax = inl;
263e2ee43e0Sdjm 			ret = BIO_write(b->next_bio, in, wrmax);
264e2ee43e0Sdjm 			if (ret <= 0)
265e31c4e69Stb 				goto done;
266e2ee43e0Sdjm 			wrlen += ret;
267e2ee43e0Sdjm 			ctx->copylen -= ret;
268e2ee43e0Sdjm 			in += ret;
269e2ee43e0Sdjm 			inl -= ret;
270e2ee43e0Sdjm 
271e2ee43e0Sdjm 			if (ctx->copylen == 0)
272e2ee43e0Sdjm 				ctx->state = ASN1_STATE_HEADER;
273e2ee43e0Sdjm 			if (inl == 0)
274e2ee43e0Sdjm 				goto done;
275e2ee43e0Sdjm 			break;
276e2ee43e0Sdjm 
277e2ee43e0Sdjm 		default:
278e2ee43e0Sdjm 			BIO_clear_retry_flags(b);
279e2ee43e0Sdjm 			return 0;
280e2ee43e0Sdjm 		}
281e2ee43e0Sdjm 
282e2ee43e0Sdjm 	}
283e2ee43e0Sdjm 
284e2ee43e0Sdjm  done:
285e2ee43e0Sdjm 	BIO_clear_retry_flags(b);
286e2ee43e0Sdjm 	BIO_copy_next_retry(b);
287e2ee43e0Sdjm 
288e2ee43e0Sdjm 	return (wrlen > 0) ? wrlen : ret;
289e2ee43e0Sdjm }
290e2ee43e0Sdjm 
291f309b230Sjsing static int
asn1_bio_flush_ex(BIO * b,BIO_ASN1_BUF_CTX * ctx,asn1_ps_func * cleanup,asn1_bio_state_t next)292f309b230Sjsing asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *cleanup,
293f309b230Sjsing     asn1_bio_state_t next)
294e2ee43e0Sdjm {
295e2ee43e0Sdjm 	int ret;
296f309b230Sjsing 
297e2ee43e0Sdjm 	if (ctx->ex_len <= 0)
298e2ee43e0Sdjm 		return 1;
299cc777fd4Stedu 	for (;;) {
300e2ee43e0Sdjm 		ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
301e2ee43e0Sdjm 		    ctx->ex_len);
302e2ee43e0Sdjm 		if (ret <= 0)
303e2ee43e0Sdjm 			break;
304e2ee43e0Sdjm 		ctx->ex_len -= ret;
305e2ee43e0Sdjm 		if (ctx->ex_len > 0)
306e2ee43e0Sdjm 			ctx->ex_pos += ret;
307cc777fd4Stedu 		else {
308e2ee43e0Sdjm 			if (cleanup)
309e2ee43e0Sdjm 				cleanup(b, &ctx->ex_buf, &ctx->ex_len,
310e2ee43e0Sdjm 				    &ctx->ex_arg);
311e2ee43e0Sdjm 			ctx->state = next;
312e2ee43e0Sdjm 			ctx->ex_pos = 0;
313e2ee43e0Sdjm 			break;
314e2ee43e0Sdjm 		}
315e2ee43e0Sdjm 	}
316e2ee43e0Sdjm 	return ret;
317e2ee43e0Sdjm }
318e2ee43e0Sdjm 
319f309b230Sjsing static int
asn1_bio_setup_ex(BIO * b,BIO_ASN1_BUF_CTX * ctx,asn1_ps_func * setup,asn1_bio_state_t ex_state,asn1_bio_state_t other_state)320f309b230Sjsing asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *setup,
321f309b230Sjsing     asn1_bio_state_t ex_state, asn1_bio_state_t other_state)
322e2ee43e0Sdjm {
323cc777fd4Stedu 	if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
324e2ee43e0Sdjm 		BIO_clear_retry_flags(b);
325e2ee43e0Sdjm 		return 0;
326e2ee43e0Sdjm 	}
327e2ee43e0Sdjm 	if (ctx->ex_len > 0)
328e2ee43e0Sdjm 		ctx->state = ex_state;
329e2ee43e0Sdjm 	else
330e2ee43e0Sdjm 		ctx->state = other_state;
331e2ee43e0Sdjm 	return 1;
332e2ee43e0Sdjm }
333e2ee43e0Sdjm 
334f309b230Sjsing static int
asn1_bio_read(BIO * b,char * in,int inl)335f309b230Sjsing asn1_bio_read(BIO *b, char *in , int inl)
336e2ee43e0Sdjm {
337e2ee43e0Sdjm 	if (!b->next_bio)
338e2ee43e0Sdjm 		return 0;
339e2ee43e0Sdjm 	return BIO_read(b->next_bio, in , inl);
340e2ee43e0Sdjm }
341e2ee43e0Sdjm 
342f309b230Sjsing static int
asn1_bio_puts(BIO * b,const char * str)343f309b230Sjsing asn1_bio_puts(BIO *b, const char *str)
344e2ee43e0Sdjm {
345e2ee43e0Sdjm 	return asn1_bio_write(b, str, strlen(str));
346e2ee43e0Sdjm }
347e2ee43e0Sdjm 
348f309b230Sjsing static int
asn1_bio_gets(BIO * b,char * str,int size)349f309b230Sjsing asn1_bio_gets(BIO *b, char *str, int size)
350e2ee43e0Sdjm {
351e2ee43e0Sdjm 	if (!b->next_bio)
352e2ee43e0Sdjm 		return 0;
353e2ee43e0Sdjm 	return BIO_gets(b->next_bio, str , size);
354e2ee43e0Sdjm }
355e2ee43e0Sdjm 
356f309b230Sjsing static long
asn1_bio_callback_ctrl(BIO * b,int cmd,BIO_info_cb * fp)357818427c5Stb asn1_bio_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
358e2ee43e0Sdjm {
359f309b230Sjsing 	if (b->next_bio == NULL)
360f309b230Sjsing 		return (0);
361e2ee43e0Sdjm 	return BIO_callback_ctrl(b->next_bio, cmd, fp);
362e2ee43e0Sdjm }
363e2ee43e0Sdjm 
364f309b230Sjsing static long
asn1_bio_ctrl(BIO * b,int cmd,long arg1,void * arg2)365f309b230Sjsing asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
366e2ee43e0Sdjm {
367e2ee43e0Sdjm 	BIO_ASN1_BUF_CTX *ctx;
368e2ee43e0Sdjm 	BIO_ASN1_EX_FUNCS *ex_func;
369e2ee43e0Sdjm 	long ret = 1;
370f309b230Sjsing 
371cc21081eStb 	if ((ctx = b->ptr) == NULL)
372e2ee43e0Sdjm 		return 0;
373cc777fd4Stedu 	switch (cmd) {
374e2ee43e0Sdjm 
375e2ee43e0Sdjm 	case BIO_C_SET_PREFIX:
376e2ee43e0Sdjm 		ex_func = arg2;
377e2ee43e0Sdjm 		ctx->prefix = ex_func->ex_func;
378e2ee43e0Sdjm 		ctx->prefix_free = ex_func->ex_free_func;
379e2ee43e0Sdjm 		break;
380e2ee43e0Sdjm 
381e2ee43e0Sdjm 	case BIO_C_SET_SUFFIX:
382e2ee43e0Sdjm 		ex_func = arg2;
383e2ee43e0Sdjm 		ctx->suffix = ex_func->ex_func;
384e2ee43e0Sdjm 		ctx->suffix_free = ex_func->ex_free_func;
385e2ee43e0Sdjm 		break;
386e2ee43e0Sdjm 
387e2ee43e0Sdjm 	case BIO_C_SET_EX_ARG:
388e2ee43e0Sdjm 		ctx->ex_arg = arg2;
389e2ee43e0Sdjm 		break;
390e2ee43e0Sdjm 
391e2ee43e0Sdjm 	case BIO_C_GET_EX_ARG:
392e2ee43e0Sdjm 		*(void **)arg2 = ctx->ex_arg;
393e2ee43e0Sdjm 		break;
394e2ee43e0Sdjm 
395e2ee43e0Sdjm 	case BIO_CTRL_FLUSH:
396e2ee43e0Sdjm 		if (!b->next_bio)
397e2ee43e0Sdjm 			return 0;
398e2ee43e0Sdjm 
399e2ee43e0Sdjm 		/* Call post function if possible */
400cc777fd4Stedu 		if (ctx->state == ASN1_STATE_HEADER) {
401e2ee43e0Sdjm 			if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
402e2ee43e0Sdjm 			    ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
403e2ee43e0Sdjm 				return 0;
404e2ee43e0Sdjm 		}
405e2ee43e0Sdjm 
406cc777fd4Stedu 		if (ctx->state == ASN1_STATE_POST_COPY) {
407e2ee43e0Sdjm 			ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
408e2ee43e0Sdjm 			    ASN1_STATE_DONE);
409e2ee43e0Sdjm 			if (ret <= 0)
410e2ee43e0Sdjm 				return ret;
411e2ee43e0Sdjm 		}
412e2ee43e0Sdjm 
413e2ee43e0Sdjm 		if (ctx->state == ASN1_STATE_DONE)
414e2ee43e0Sdjm 			return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
415cc777fd4Stedu 		else {
416e2ee43e0Sdjm 			BIO_clear_retry_flags(b);
417e2ee43e0Sdjm 			return 0;
418e2ee43e0Sdjm 		}
419e2ee43e0Sdjm 		break;
420e2ee43e0Sdjm 
421e2ee43e0Sdjm 
422e2ee43e0Sdjm 	default:
423e2ee43e0Sdjm 		if (!b->next_bio)
424e2ee43e0Sdjm 			return 0;
425e2ee43e0Sdjm 		return BIO_ctrl(b->next_bio, cmd, arg1, arg2);
426e2ee43e0Sdjm 
427e2ee43e0Sdjm 	}
428e2ee43e0Sdjm 
429e2ee43e0Sdjm 	return ret;
430e2ee43e0Sdjm }
431e2ee43e0Sdjm 
432f309b230Sjsing static int
asn1_bio_set_ex(BIO * b,int cmd,asn1_ps_func * ex_func,asn1_ps_func * ex_free_func)433f309b230Sjsing asn1_bio_set_ex(BIO *b, int cmd, asn1_ps_func *ex_func, asn1_ps_func
434f309b230Sjsing     *ex_free_func)
435e2ee43e0Sdjm {
436e2ee43e0Sdjm 	BIO_ASN1_EX_FUNCS extmp;
437f309b230Sjsing 
438e2ee43e0Sdjm 	extmp.ex_func = ex_func;
439e2ee43e0Sdjm 	extmp.ex_free_func = ex_free_func;
440e2ee43e0Sdjm 	return BIO_ctrl(b, cmd, 0, &extmp);
441e2ee43e0Sdjm }
442e2ee43e0Sdjm 
443f309b230Sjsing int
BIO_asn1_set_prefix(BIO * b,asn1_ps_func * prefix,asn1_ps_func * prefix_free)444f309b230Sjsing BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
445e2ee43e0Sdjm {
446e2ee43e0Sdjm 	return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
447e2ee43e0Sdjm }
448e2ee43e0Sdjm 
449f309b230Sjsing int
BIO_asn1_set_suffix(BIO * b,asn1_ps_func * suffix,asn1_ps_func * suffix_free)450f309b230Sjsing BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
451e2ee43e0Sdjm {
452e2ee43e0Sdjm 	return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
453e2ee43e0Sdjm }
454