1221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* bio_ndef.c */
2221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * project.
4221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
5221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* ====================================================================
6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Copyright (c) 2008 The OpenSSL Project.  All rights reserved.
7221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
8221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Redistribution and use in source and binary forms, with or without
9221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * modification, are permitted provided that the following conditions
10221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * are met:
11221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
12221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 1. Redistributions of source code must retain the above copyright
13221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer.
14221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
15221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 2. Redistributions in binary form must reproduce the above copyright
16221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    notice, this list of conditions and the following disclaimer in
17221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    the documentation and/or other materials provided with the
18221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    distribution.
19221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
20221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 3. All advertising materials mentioning features or use of this
21221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    software must display the following acknowledgment:
22221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
23221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
25221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    endorse or promote products derived from this software without
27221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    prior written permission. For written permission, please contact
28221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    licensing@OpenSSL.org.
29221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
30221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 5. Products derived from this software may not be called "OpenSSL"
31221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    nor may "OpenSSL" appear in their names without prior written
32221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    permission of the OpenSSL Project.
33221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
34221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * 6. Redistributions of any form whatsoever must retain the following
35221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    acknowledgment:
36221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    "This product includes software developed by the OpenSSL Project
37221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
39221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * OF THE POSSIBILITY OF SUCH DAMAGE.
51221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * ====================================================================
52221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
53221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
54221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
55221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/asn1.h>
56221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/asn1t.h>
57221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/bio.h>
58221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/err.h>
59221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
60221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <stdio.h>
61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
62221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Experimental NDEF ASN1 BIO support routines */
63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
64221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* The usage is quite simple, initialize an ASN1 structure,
65221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * get a BIO from it then any data written through the BIO
66221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * will end up translated to approptiate format on the fly.
67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * The data is streamed out and does *not* need to be
68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * all held in memory at once.
69221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * When the BIO is flushed the output is finalized and any
71221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * signatures etc written out.
72221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * The BIO is a 'proper' BIO and can handle non blocking I/O
74221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * correctly.
75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom *
76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * The usage is simple. The implementation is *not*...
77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom */
78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* BIO support data stored in the ASN1 BIO ex_arg */
80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromtypedef struct ndef_aux_st
82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* ASN1 structure this BIO refers to */
84221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_VALUE *val;
85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_ITEM *it;
86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Top of the BIO chain */
87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO *ndef_bio;
88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Output BIO */
89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO *out;
90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Boundary where content is inserted */
91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	unsigned char **boundary;
92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* DER buffer start */
93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	unsigned char *derbuf;
94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	} NDEF_SUPPORT;
95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian CarlstromBIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
103221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	NDEF_SUPPORT *ndef_aux = NULL;
104221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO *asn_bio = NULL;
105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_AUX *aux = it->funcs;
106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_STREAM_ARG sarg;
107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!aux || !aux->asn1_cb)
109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return NULL;
112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux = OPENSSL_malloc(sizeof(NDEF_SUPPORT));
114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	asn_bio = BIO_new(BIO_f_asn1());
115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* ASN1 bio needs to be next to output BIO */
117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	out = BIO_push(asn_bio, out);
119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ndef_aux || !asn_bio || !out)
121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		goto err;
122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
124221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
125221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
126221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Now let callback prepend any digest, cipher etc BIOs
127221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	 * ASN1 structure needs.
128221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	 */
129221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
130221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.out = out;
131221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.ndef_bio = NULL;
132221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.boundary = NULL;
133221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
134221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
135221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		goto err;
136221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
137221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->val = val;
138221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->it = it;
139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->ndef_bio = sarg.ndef_bio;
140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->boundary = sarg.boundary;
141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->out = out;
142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
145221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return sarg.ndef_bio;
146221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	err:
148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (asn_bio)
149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		BIO_free(asn_bio);
150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ndef_aux)
151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_free(ndef_aux);
152221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return NULL;
153221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
156221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
157221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	NDEF_SUPPORT *ndef_aux;
158221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	unsigned char *p;
159221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int derlen;
160221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
161221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!parg)
162221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
163221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
164221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux = *(NDEF_SUPPORT **)parg;
165221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
166221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
167221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p = OPENSSL_malloc(derlen);
168221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->derbuf = p;
169221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*pbuf = p;
170221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
171221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
172221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!*ndef_aux->boundary)
173221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
174221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
175221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*plen = *ndef_aux->boundary - *pbuf;
176221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
177221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
178221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
179221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
180221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
181221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
182221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	NDEF_SUPPORT *ndef_aux;
183221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
184221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!parg)
185221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
186221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
187221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux = *(NDEF_SUPPORT **)parg;
188221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
189221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ndef_aux->derbuf)
190221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		OPENSSL_free(ndef_aux->derbuf);
191221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
192221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->derbuf = NULL;
193221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*pbuf = NULL;
194221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*plen = 0;
195221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
196221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
197221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
198221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
199221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
200221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
201221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ndef_prefix_free(b, pbuf, plen, parg))
202221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
203221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	OPENSSL_free(*pndef_aux);
204221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*pndef_aux = NULL;
205221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
206221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
207221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
208221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
209221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
210221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	NDEF_SUPPORT *ndef_aux;
211221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	unsigned char *p;
212221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int derlen;
213221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_AUX *aux;
214221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_STREAM_ARG sarg;
215221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
216221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!parg)
217221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
218221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
219221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux = *(NDEF_SUPPORT **)parg;
220221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
221221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	aux = ndef_aux->it->funcs;
222221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
223221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	/* Finalize structures */
224221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.ndef_bio = ndef_aux->ndef_bio;
225221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.out = ndef_aux->out;
226221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	sarg.boundary = ndef_aux->boundary;
227221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (aux->asn1_cb(ASN1_OP_STREAM_POST,
228221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				&ndef_aux->val, ndef_aux->it, &sarg) <= 0)
229221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
230221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
231221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
232221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p = OPENSSL_malloc(derlen);
233221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ndef_aux->derbuf = p;
234221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*pbuf = p;
235221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
236221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
237221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!*ndef_aux->boundary)
238221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
239221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*pbuf = *ndef_aux->boundary;
240221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	*plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
241221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
242221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
243221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
244