1656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* tasn_prn.c */
2e45f106cb6b47af1f21efe76e933bdea2f5dd1caNagendra Modadugu/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * project 2000.
4656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
5656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project/* ====================================================================
6221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom * Copyright (c) 2000,2005 The OpenSSL Project.  All rights reserved.
7656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
8656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Redistribution and use in source and binary forms, with or without
9656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * modification, are permitted provided that the following conditions
10656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * are met:
11656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
12656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 1. Redistributions of source code must retain the above copyright
13656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer.
14656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
15656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 2. Redistributions in binary form must reproduce the above copyright
16656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    notice, this list of conditions and the following disclaimer in
17656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    the documentation and/or other materials provided with the
18656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    distribution.
19656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
20656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 3. All advertising materials mentioning features or use of this
21656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    software must display the following acknowledgment:
22656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
23656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
25656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    endorse or promote products derived from this software without
27656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    prior written permission. For written permission, please contact
28656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    licensing@OpenSSL.org.
29656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
30656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 5. Products derived from this software may not be called "OpenSSL"
31656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    nor may "OpenSSL" appear in their names without prior written
32656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    permission of the OpenSSL Project.
33656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
34656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * 6. Redistributions of any form whatsoever must retain the following
35656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    acknowledgment:
36656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    "This product includes software developed by the OpenSSL Project
37656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
39656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * OF THE POSSIBILITY OF SUCH DAMAGE.
51656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * ====================================================================
52656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
53656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * This product includes cryptographic software written by Eric Young
54656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * (eay@cryptsoft.com).  This product includes software written by Tim
55656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project * Hudson (tjh@cryptsoft.com).
56656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project *
57656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
58656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
59656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
60656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <stddef.h>
61221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "cryptlib.h"
62656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/asn1.h>
63221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/asn1t.h>
64656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/objects.h>
65656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/buffer.h>
66656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project#include <openssl/err.h>
67221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include <openssl/x509v3.h>
68221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#include "asn1_locl.h"
69656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
70221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Print routines.
71656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project */
72656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
73221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* ASN1_PCTX routines */
74656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
75221304ee937bc0910948a8be1320cb8cc4eb6d36Brian CarlstromASN1_PCTX default_pctx =
76221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
77221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_PCTX_FLAGS_SHOW_ABSENT,	/* flags */
78221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,	/* nm_flags */
79221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,	/* cert_flags */
80221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0,	/* oid_flags */
81221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	0	/* str_flags */
82221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	};
83221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
84656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
85221304ee937bc0910948a8be1320cb8cc4eb6d36Brian CarlstromASN1_PCTX *ASN1_PCTX_new(void)
86221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
87221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_PCTX *ret;
88221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret = OPENSSL_malloc(sizeof(ASN1_PCTX));
89221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (ret == NULL)
90221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
91221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ASN1err(ASN1_F_ASN1_PCTX_NEW, ERR_R_MALLOC_FAILURE);
92221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return NULL;
93221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
94221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret->flags = 0;
95221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret->nm_flags = 0;
96221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret->cert_flags = 0;
97221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret->oid_flags = 0;
98221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ret->str_flags = 0;
99221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
100221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
101221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
102221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_free(ASN1_PCTX *p)
103221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
104221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	OPENSSL_free(p);
105221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
106221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
107221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p)
108221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
109221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return p->flags;
110221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
111221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
112221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
113221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
114221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p->flags = flags;
115221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
116221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
117221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
118221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
119221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return p->nm_flags;
120221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
121221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
122221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
123221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
124221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p->nm_flags = flags;
125221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
126221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
127221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
128221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
129221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return p->cert_flags;
130221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
131221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
132221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
133221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
134221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p->cert_flags = flags;
135221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
136221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
137221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
138221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
139221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return p->oid_flags;
140221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
141221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
142221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
143221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
144221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p->oid_flags = flags;
145221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
146221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
147221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromunsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
148221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
149221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return p->str_flags;
150221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
151221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
152221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromvoid ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
153221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
154221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	p->str_flags = flags;
155221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
156221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
157221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom/* Main print routines */
158221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
159221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
160221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_ITEM *it,
161221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const char *fname, const char *sname,
162221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				int nohdr, const ASN1_PCTX *pctx);
163221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
164221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
165221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
166221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
167221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
168221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_ITEM *it, int indent,
169221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const char *fname, const char *sname,
170221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_PCTX *pctx);
171221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
172221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_print_fsname(BIO *out, int indent,
173221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const char *fname, const char *sname,
174221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const ASN1_PCTX *pctx);
175221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
176221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent,
177221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_ITEM *it, const ASN1_PCTX *pctx)
178221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
179221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const char *sname;
180221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pctx == NULL)
181221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		pctx = &default_pctx;
182221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
183221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		sname = NULL;
184221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
185221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		sname = it->sname;
186221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return asn1_item_print_ctx(out, &ifld, indent, it,
187221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom							NULL, sname, 0, pctx);
188221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
189221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
190221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
191221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_ITEM *it,
192221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const char *fname, const char *sname,
193221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				int nohdr, const ASN1_PCTX *pctx)
194221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
195656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	const ASN1_TEMPLATE *tt;
196221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_EXTERN_FUNCS *ef;
197221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_VALUE **tmpfld;
198221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_AUX *aux = it->funcs;
199221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_aux_cb *asn1_cb;
200221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_PRINT_ARG parg;
201656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int i;
202221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (aux && aux->asn1_cb)
203221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
204221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		parg.out = out;
205221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		parg.indent = indent;
206221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		parg.pctx = pctx;
207221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		asn1_cb = aux->asn1_cb;
208221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
209221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else asn1_cb = 0;
210221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
211221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if(*fld == NULL)
212221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
213221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT)
214221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
215221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (!nohdr && !asn1_print_fsname(out, indent,
216221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom							fname, sname, pctx))
217221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
218221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_puts(out, "<ABSENT>\n") <= 0)
219221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
220221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
221656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 1;
222221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
223656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
224221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	switch(it->itype)
225221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
226656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		case ASN1_ITYPE_PRIMITIVE:
227656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		if(it->templates)
228221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
229221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (!asn1_template_print_ctx(out, fld, indent,
230221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom							it->templates, pctx))
231221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
232221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
233221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		/* fall thru */
234656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		case ASN1_ITYPE_MSTRING:
235221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!asn1_primitive_print(out, fld, it,
236221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				indent, fname, sname,pctx))
237221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
238221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
239656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
240656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		case ASN1_ITYPE_EXTERN:
241221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
242221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
243221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		/* Use new style print routine if possible */
244221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ef = it->funcs;
245221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (ef && ef->asn1_ex_print)
246221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
247221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			i = ef->asn1_ex_print(out, fld, indent, "", pctx);
248221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (!i)
249221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
250221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if ((i == 2) && (BIO_puts(out, "\n") <= 0))
251221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
252221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 1;
253221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
254221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		else if (sname &&
255221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
256221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
257221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
258656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
259656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		case ASN1_ITYPE_CHOICE:
260221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#if 0
261221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
262221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
263221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#endif
264656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* CHOICE type, get selector */
265656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		i = asn1_get_choice_selector(fld, it);
266656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* This should never happen... */
267221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if((i < 0) || (i >= it->tcount))
268221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
269221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_printf(out,
270221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				"ERROR: selector [%d] invalid\n", i) <= 0)
271221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
272656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			return 1;
273221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
274656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		tt = it->templates + i;
275221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		tmpfld = asn1_get_field_ptr(fld, tt);
276221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!asn1_template_print_ctx(out, tmpfld, indent, tt, pctx))
277221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
278221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
279656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
280656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		case ASN1_ITYPE_SEQUENCE:
281221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case ASN1_ITYPE_NDEF_SEQUENCE:
282221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx))
283221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
284221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (fname || sname)
285221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
286221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
287221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				{
288221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				if (BIO_puts(out, " {\n") <= 0)
289221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					return 0;
290221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				}
291221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			else
292221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				{
293221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				if (BIO_puts(out, "\n") <= 0)
294221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					return 0;
295221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				}
296221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
297221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
298221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (asn1_cb)
299221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
300221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			i = asn1_cb(ASN1_OP_PRINT_PRE, fld, it, &parg);
301221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (i == 0)
302221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
303221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (i == 2)
304221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 1;
305221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
306221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
307221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		/* Print each field entry */
308221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		for(i = 0, tt = it->templates; i < it->tcount; i++, tt++)
309221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
310221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const ASN1_TEMPLATE *seqtt;
311221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			seqtt = asn1_do_adb(fld, tt, 1);
312221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			tmpfld = asn1_get_field_ptr(fld, seqtt);
313221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (!asn1_template_print_ctx(out, tmpfld,
314221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom						indent + 2, seqtt, pctx))
315221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
316221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
317221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
318221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
319221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_printf(out, "%*s}\n", indent, "") < 0)
320221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
321221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
322221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
323221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (asn1_cb)
324221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
325221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			i = asn1_cb(ASN1_OP_PRINT_POST, fld, it, &parg);
326221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (i == 0)
327221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
328221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
329221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
330656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
331656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		default:
332221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		BIO_printf(out, "Unprocessed type %d\n", it->itype);
333656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 0;
334221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
335221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
336221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
337656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
338656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project
339221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromint asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
340221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
341221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
342656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	int i, flags;
343221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const char *sname, *fname;
344656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	flags = tt->flags;
345221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
346221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		sname = ASN1_ITEM_ptr(tt->item)->sname;
347221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
348221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		sname = NULL;
349221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
350221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		fname = NULL;
351221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
352221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		fname = tt->field_name;
353221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if(flags & ASN1_TFLG_SK_MASK)
354221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
355656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		char *tname;
356221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ASN1_VALUE *skitem;
357221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		STACK_OF(ASN1_VALUE) *stack;
358221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
359656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		/* SET OF, SEQUENCE OF */
360221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (fname)
361221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
362221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF)
363221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				{
364221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				if(flags & ASN1_TFLG_SET_OF)
365221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					tname = "SET";
366221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				else
367221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					tname = "SEQUENCE";
368221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				if (BIO_printf(out, "%*s%s OF %s {\n",
369221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					indent, "", tname, tt->field_name) <= 0)
370221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					return 0;
371221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				}
372221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			else if (BIO_printf(out, "%*s%s:\n", indent, "",
373221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					fname) <= 0)
374221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
375221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
376221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		stack = (STACK_OF(ASN1_VALUE) *)*fld;
377221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		for(i = 0; i < sk_ASN1_VALUE_num(stack); i++)
378221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
379221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if ((i > 0) && (BIO_puts(out, "\n") <= 0))
380221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
381221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
382221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			skitem = sk_ASN1_VALUE_value(stack, i);
383221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (!asn1_item_print_ctx(out, &skitem, indent + 2,
384221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
385221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
386221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
387221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
388221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
389221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE)
390221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
391221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
392221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
393656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project			}
394656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		return 1;
395221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
396221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
397221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom							fname, sname, 0, pctx);
398656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	}
399221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
400221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_print_fsname(BIO *out, int indent,
401221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const char *fname, const char *sname,
402221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			const ASN1_PCTX *pctx)
403221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
404221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	static char spaces[] = "                    ";
405221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const int nspaces = sizeof(spaces) - 1;
406221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
407221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#if 0
408221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!sname && !fname)
409221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
410221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom#endif
411221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
412221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	while (indent > nspaces)
413221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
414221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_write(out, spaces, nspaces) != nspaces)
415221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
416221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		indent -= nspaces;
417221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
418221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (BIO_write(out, spaces, indent) != indent)
419221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
420221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
421221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		sname = NULL;
422221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
423221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		fname = NULL;
424221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!sname && !fname)
425221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
426221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (fname)
427221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
428221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_puts(out, fname) <= 0)
429221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
430656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project		}
431221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (sname)
432221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
433221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (fname)
434221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
435221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_printf(out, " (%s)", sname) <= 0)
436221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
437221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
438221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		else
439221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
440221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			if (BIO_puts(out, sname) <= 0)
441221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
442221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
443221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
444221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (BIO_write(out, ": ", 2) != 2)
445221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
446656d9c7f52f88b3a3daccafa7655dec086c4756eThe Android Open Source Project	return 1;
447221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
448221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
449a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstromstatic int asn1_print_boolean_ctx(BIO *out, int boolval,
450221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom							const ASN1_PCTX *pctx)
451221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
452221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const char *str;
453a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstrom	switch (boolval)
454221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
455221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case -1:
456221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		str = "BOOL ABSENT";
457221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
458221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
459221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case 0:
460221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		str = "FALSE";
461221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
462221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
463221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		default:
464221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		str = "TRUE";
465221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
466221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
467221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
468221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
469221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (BIO_puts(out, str) <= 0)
470221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
471221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
472221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
473221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
474221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
475221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
476221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom						const ASN1_PCTX *pctx)
477221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
478221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	char *s;
479221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret = 1;
480221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	s = i2s_ASN1_INTEGER(NULL, str);
481221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (BIO_puts(out, s) <= 0)
482221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = 0;
483221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	OPENSSL_free(s);
484221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return ret;
485221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
486221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
487221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
488221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom						const ASN1_PCTX *pctx)
489221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
490221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	char objbuf[80];
491221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const char *ln;
492221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ln = OBJ_nid2ln(OBJ_obj2nid(oid));
493221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if(!ln)
494221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ln = "";
495221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
496221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
497221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
498221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
499221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
500221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
501221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
502221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom						const ASN1_PCTX *pctx)
503221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
504221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (str->type == V_ASN1_BIT_STRING)
505221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
506221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_printf(out, " (%ld unused bits)\n",
507221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom					str->flags & 0x7) <= 0)
508221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				return 0;
509221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
510221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else if (BIO_puts(out, "\n") <= 0)
511221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
512221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if ((str->length > 0)
513221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		&& BIO_dump_indent(out, (char *)str->data, str->length,
514221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				indent + 2) <= 0)
515221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
516221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
517221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
518221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
519221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstromstatic int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
520221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_ITEM *it, int indent,
521221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const char *fname, const char *sname,
522221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom				const ASN1_PCTX *pctx)
523221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	{
524221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	long utype;
525221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	ASN1_STRING *str;
526221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	int ret = 1, needlf = 1;
527221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const char *pname;
528221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	const ASN1_PRIMITIVE_FUNCS *pf;
529221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	pf = it->funcs;
530221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!asn1_print_fsname(out, indent, fname, sname, pctx))
531221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
532221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pf && pf->prim_print)
533221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return pf->prim_print(out, fld, it, indent, pctx);
534221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	str = (ASN1_STRING *)*fld;
535221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (it->itype == ASN1_ITYPE_MSTRING)
536221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		utype = str->type & ~V_ASN1_NEG;
537221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
538221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		utype = it->utype;
539221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (utype == V_ASN1_ANY)
540221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
541221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ASN1_TYPE *atype = (ASN1_TYPE *)*fld;
542221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		utype = atype->type;
543221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		fld = &atype->value.asn1_value;
544221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		str = (ASN1_STRING *)*fld;
545221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
546221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			pname = NULL;
547221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		else
548221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			pname = ASN1_tag2str(utype);
549221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
550221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	else
551221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
552221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
553221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			pname = ASN1_tag2str(utype);
554221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		else
555221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			pname = NULL;
556221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
557221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
558221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (utype == V_ASN1_NULL)
559221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
560221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_puts(out, "NULL\n") <= 0)
561221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
562221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 1;
563221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
564221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
565221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (pname)
566221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
567221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_puts(out, pname) <= 0)
568221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
569221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_puts(out, ":") <= 0)
570221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
571221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
572221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
573221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	switch (utype)
574221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		{
575221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_BOOLEAN:
576221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			{
577a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstrom			int boolval = *(int *)fld;
578a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstrom			if (boolval == -1)
579a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstrom				boolval = it->size;
580a1a5710c055e139ea00e785f9eb55b3af3e4dab1Brian Carlstrom			ret = asn1_print_boolean_ctx(out, boolval, pctx);
581221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			}
582221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
583221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
584221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_INTEGER:
585221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_ENUMERATED:
586221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = asn1_print_integer_ctx(out, str, pctx);
587221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
588221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
589221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_UTCTIME:
590221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = ASN1_UTCTIME_print(out, str);
591221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
592221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
593221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_GENERALIZEDTIME:
594221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = ASN1_GENERALIZEDTIME_print(out, str);
595221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
596221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
597221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_OBJECT:
598221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = asn1_print_oid_ctx(out, (const ASN1_OBJECT *)*fld, pctx);
599221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
600221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
601221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_OCTET_STRING:
602221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_BIT_STRING:
603221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = asn1_print_obstring_ctx(out, str, indent, pctx);
604221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		needlf = 0;
605221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
606221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
607221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_SEQUENCE:
608221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_SET:
609221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		case V_ASN1_OTHER:
610221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (BIO_puts(out, "\n") <= 0)
611221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			return 0;
612221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		if (ASN1_parse_dump(out, str->data, str->length,
613221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom						indent, 0) <= 0)
614221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom			ret = 0;
615221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		needlf = 0;
616221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		break;
617221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
618221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		default:
619221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
620221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom
621221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		}
622221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (!ret)
623221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
624221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	if (needlf && BIO_puts(out, "\n") <= 0)
625221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom		return 0;
626221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	return 1;
627221304ee937bc0910948a8be1320cb8cc4eb6d36Brian Carlstrom	}
628