xref: /third_party/openssl/apps/asn1parse.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <stdio.h>
11e1051a39Sopenharmony_ci#include <stdlib.h>
12e1051a39Sopenharmony_ci#include <string.h>
13e1051a39Sopenharmony_ci#include "apps.h"
14e1051a39Sopenharmony_ci#include "progs.h"
15e1051a39Sopenharmony_ci#include <openssl/err.h>
16e1051a39Sopenharmony_ci#include <openssl/evp.h>
17e1051a39Sopenharmony_ci#include <openssl/x509.h>
18e1051a39Sopenharmony_ci#include <openssl/pem.h>
19e1051a39Sopenharmony_ci#include <openssl/asn1t.h>
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_citypedef enum OPTION_choice {
22e1051a39Sopenharmony_ci    OPT_COMMON,
23e1051a39Sopenharmony_ci    OPT_INFORM, OPT_IN, OPT_OUT, OPT_INDENT, OPT_NOOUT,
24e1051a39Sopenharmony_ci    OPT_OID, OPT_OFFSET, OPT_LENGTH, OPT_DUMP, OPT_DLIMIT,
25e1051a39Sopenharmony_ci    OPT_STRPARSE, OPT_GENSTR, OPT_GENCONF, OPT_STRICTPEM,
26e1051a39Sopenharmony_ci    OPT_ITEM
27e1051a39Sopenharmony_ci} OPTION_CHOICE;
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_ciconst OPTIONS asn1parse_options[] = {
30e1051a39Sopenharmony_ci    OPT_SECTION("General"),
31e1051a39Sopenharmony_ci    {"help", OPT_HELP, '-', "Display this summary"},
32e1051a39Sopenharmony_ci    {"oid", OPT_OID, '<', "file of extra oid definitions"},
33e1051a39Sopenharmony_ci
34e1051a39Sopenharmony_ci    OPT_SECTION("I/O"),
35e1051a39Sopenharmony_ci    {"inform", OPT_INFORM, 'F', "input format - one of DER PEM"},
36e1051a39Sopenharmony_ci    {"in", OPT_IN, '<', "input file"},
37e1051a39Sopenharmony_ci    {"out", OPT_OUT, '>', "output file (output format is always DER)"},
38e1051a39Sopenharmony_ci    {"noout", OPT_NOOUT, 0, "do not produce any output"},
39e1051a39Sopenharmony_ci    {"offset", OPT_OFFSET, 'p', "offset into file"},
40e1051a39Sopenharmony_ci    {"length", OPT_LENGTH, 'p', "length of section in file"},
41e1051a39Sopenharmony_ci    {"strparse", OPT_STRPARSE, 'p',
42e1051a39Sopenharmony_ci     "offset; a series of these can be used to 'dig'"},
43e1051a39Sopenharmony_ci    {"genstr", OPT_GENSTR, 's', "string to generate ASN1 structure from"},
44e1051a39Sopenharmony_ci    {OPT_MORE_STR, 0, 0, "into multiple ASN1 blob wrappings"},
45e1051a39Sopenharmony_ci    {"genconf", OPT_GENCONF, 's', "file to generate ASN1 structure from"},
46e1051a39Sopenharmony_ci    {"strictpem", OPT_STRICTPEM, 0,
47e1051a39Sopenharmony_ci     "do not attempt base64 decode outside PEM markers"},
48e1051a39Sopenharmony_ci    {"item", OPT_ITEM, 's', "item to parse and print"},
49e1051a39Sopenharmony_ci    {OPT_MORE_STR, 0, 0, "(-inform  will be ignored)"},
50e1051a39Sopenharmony_ci
51e1051a39Sopenharmony_ci    OPT_SECTION("Formatting"),
52e1051a39Sopenharmony_ci    {"i", OPT_INDENT, 0, "indents the output"},
53e1051a39Sopenharmony_ci    {"dump", OPT_DUMP, 0, "unknown data in hex form"},
54e1051a39Sopenharmony_ci    {"dlimit", OPT_DLIMIT, 'p',
55e1051a39Sopenharmony_ci     "dump the first arg bytes of unknown data in hex form"},
56e1051a39Sopenharmony_ci    {NULL}
57e1051a39Sopenharmony_ci};
58e1051a39Sopenharmony_ci
59e1051a39Sopenharmony_cistatic int do_generate(char *genstr, const char *genconf, BUF_MEM *buf);
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ciint asn1parse_main(int argc, char **argv)
62e1051a39Sopenharmony_ci{
63e1051a39Sopenharmony_ci    ASN1_TYPE *at = NULL;
64e1051a39Sopenharmony_ci    BIO *in = NULL, *b64 = NULL, *derout = NULL;
65e1051a39Sopenharmony_ci    BUF_MEM *buf = NULL;
66e1051a39Sopenharmony_ci    STACK_OF(OPENSSL_STRING) *osk = NULL;
67e1051a39Sopenharmony_ci    char *genstr = NULL, *genconf = NULL;
68e1051a39Sopenharmony_ci    char *infile = NULL, *oidfile = NULL, *derfile = NULL;
69e1051a39Sopenharmony_ci    unsigned char *str = NULL;
70e1051a39Sopenharmony_ci    char *name = NULL, *header = NULL, *prog;
71e1051a39Sopenharmony_ci    const unsigned char *ctmpbuf;
72e1051a39Sopenharmony_ci    int indent = 0, noout = 0, dump = 0, strictpem = 0, informat = FORMAT_PEM;
73e1051a39Sopenharmony_ci    int offset = 0, ret = 1, i, j;
74e1051a39Sopenharmony_ci    long num, tmplen;
75e1051a39Sopenharmony_ci    unsigned char *tmpbuf;
76e1051a39Sopenharmony_ci    unsigned int length = 0;
77e1051a39Sopenharmony_ci    OPTION_CHOICE o;
78e1051a39Sopenharmony_ci    const ASN1_ITEM *it = NULL;
79e1051a39Sopenharmony_ci
80e1051a39Sopenharmony_ci    prog = opt_init(argc, argv, asn1parse_options);
81e1051a39Sopenharmony_ci
82e1051a39Sopenharmony_ci    if ((osk = sk_OPENSSL_STRING_new_null()) == NULL) {
83e1051a39Sopenharmony_ci        BIO_printf(bio_err, "%s: Memory allocation failure\n", prog);
84e1051a39Sopenharmony_ci        goto end;
85e1051a39Sopenharmony_ci    }
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ci    while ((o = opt_next()) != OPT_EOF) {
88e1051a39Sopenharmony_ci        switch (o) {
89e1051a39Sopenharmony_ci        case OPT_EOF:
90e1051a39Sopenharmony_ci        case OPT_ERR:
91e1051a39Sopenharmony_ci opthelp:
92e1051a39Sopenharmony_ci            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
93e1051a39Sopenharmony_ci            goto end;
94e1051a39Sopenharmony_ci        case OPT_HELP:
95e1051a39Sopenharmony_ci            opt_help(asn1parse_options);
96e1051a39Sopenharmony_ci            ret = 0;
97e1051a39Sopenharmony_ci            goto end;
98e1051a39Sopenharmony_ci        case OPT_INFORM:
99e1051a39Sopenharmony_ci            if (!opt_format(opt_arg(), OPT_FMT_PEMDER, &informat))
100e1051a39Sopenharmony_ci                goto opthelp;
101e1051a39Sopenharmony_ci            break;
102e1051a39Sopenharmony_ci        case OPT_IN:
103e1051a39Sopenharmony_ci            infile = opt_arg();
104e1051a39Sopenharmony_ci            break;
105e1051a39Sopenharmony_ci        case OPT_OUT:
106e1051a39Sopenharmony_ci            derfile = opt_arg();
107e1051a39Sopenharmony_ci            break;
108e1051a39Sopenharmony_ci        case OPT_INDENT:
109e1051a39Sopenharmony_ci            indent = 1;
110e1051a39Sopenharmony_ci            break;
111e1051a39Sopenharmony_ci        case OPT_NOOUT:
112e1051a39Sopenharmony_ci            noout = 1;
113e1051a39Sopenharmony_ci            break;
114e1051a39Sopenharmony_ci        case OPT_OID:
115e1051a39Sopenharmony_ci            oidfile = opt_arg();
116e1051a39Sopenharmony_ci            break;
117e1051a39Sopenharmony_ci        case OPT_OFFSET:
118e1051a39Sopenharmony_ci            offset = strtol(opt_arg(), NULL, 0);
119e1051a39Sopenharmony_ci            break;
120e1051a39Sopenharmony_ci        case OPT_LENGTH:
121e1051a39Sopenharmony_ci            length = strtol(opt_arg(), NULL, 0);
122e1051a39Sopenharmony_ci            break;
123e1051a39Sopenharmony_ci        case OPT_DUMP:
124e1051a39Sopenharmony_ci            dump = -1;
125e1051a39Sopenharmony_ci            break;
126e1051a39Sopenharmony_ci        case OPT_DLIMIT:
127e1051a39Sopenharmony_ci            dump = strtol(opt_arg(), NULL, 0);
128e1051a39Sopenharmony_ci            break;
129e1051a39Sopenharmony_ci        case OPT_STRPARSE:
130e1051a39Sopenharmony_ci            sk_OPENSSL_STRING_push(osk, opt_arg());
131e1051a39Sopenharmony_ci            break;
132e1051a39Sopenharmony_ci        case OPT_GENSTR:
133e1051a39Sopenharmony_ci            genstr = opt_arg();
134e1051a39Sopenharmony_ci            break;
135e1051a39Sopenharmony_ci        case OPT_GENCONF:
136e1051a39Sopenharmony_ci            genconf = opt_arg();
137e1051a39Sopenharmony_ci            break;
138e1051a39Sopenharmony_ci        case OPT_STRICTPEM:
139e1051a39Sopenharmony_ci            strictpem = 1;
140e1051a39Sopenharmony_ci            informat = FORMAT_PEM;
141e1051a39Sopenharmony_ci            break;
142e1051a39Sopenharmony_ci        case OPT_ITEM:
143e1051a39Sopenharmony_ci            it = ASN1_ITEM_lookup(opt_arg());
144e1051a39Sopenharmony_ci            if (it == NULL) {
145e1051a39Sopenharmony_ci                size_t tmp;
146e1051a39Sopenharmony_ci
147e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Unknown item name %s\n", opt_arg());
148e1051a39Sopenharmony_ci                BIO_puts(bio_err, "Supported types:\n");
149e1051a39Sopenharmony_ci                for (tmp = 0;; tmp++) {
150e1051a39Sopenharmony_ci                    it = ASN1_ITEM_get(tmp);
151e1051a39Sopenharmony_ci                    if (it == NULL)
152e1051a39Sopenharmony_ci                        break;
153e1051a39Sopenharmony_ci                    BIO_printf(bio_err, "    %s\n", it->sname);
154e1051a39Sopenharmony_ci                }
155e1051a39Sopenharmony_ci                goto end;
156e1051a39Sopenharmony_ci            }
157e1051a39Sopenharmony_ci            break;
158e1051a39Sopenharmony_ci        }
159e1051a39Sopenharmony_ci    }
160e1051a39Sopenharmony_ci
161e1051a39Sopenharmony_ci    /* No extra args. */
162e1051a39Sopenharmony_ci    argc = opt_num_rest();
163e1051a39Sopenharmony_ci    if (argc != 0)
164e1051a39Sopenharmony_ci        goto opthelp;
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci    if (oidfile != NULL) {
167e1051a39Sopenharmony_ci        in = bio_open_default(oidfile, 'r', FORMAT_TEXT);
168e1051a39Sopenharmony_ci        if (in == NULL)
169e1051a39Sopenharmony_ci            goto end;
170e1051a39Sopenharmony_ci        OBJ_create_objects(in);
171e1051a39Sopenharmony_ci        BIO_free(in);
172e1051a39Sopenharmony_ci    }
173e1051a39Sopenharmony_ci
174e1051a39Sopenharmony_ci    if ((in = bio_open_default(infile, 'r', informat)) == NULL)
175e1051a39Sopenharmony_ci        goto end;
176e1051a39Sopenharmony_ci
177e1051a39Sopenharmony_ci    if (derfile && (derout = bio_open_default(derfile, 'w', FORMAT_ASN1)) == NULL)
178e1051a39Sopenharmony_ci        goto end;
179e1051a39Sopenharmony_ci
180e1051a39Sopenharmony_ci    if ((buf = BUF_MEM_new()) == NULL)
181e1051a39Sopenharmony_ci        goto end;
182e1051a39Sopenharmony_ci    if (strictpem) {
183e1051a39Sopenharmony_ci        if (PEM_read_bio(in, &name, &header, &str, &num) != 1) {
184e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error reading PEM file\n");
185e1051a39Sopenharmony_ci            ERR_print_errors(bio_err);
186e1051a39Sopenharmony_ci            goto end;
187e1051a39Sopenharmony_ci        }
188e1051a39Sopenharmony_ci        buf->data = (char *)str;
189e1051a39Sopenharmony_ci        buf->length = buf->max = num;
190e1051a39Sopenharmony_ci    } else {
191e1051a39Sopenharmony_ci        if (!BUF_MEM_grow(buf, BUFSIZ * 8))
192e1051a39Sopenharmony_ci            goto end;           /* Pre-allocate :-) */
193e1051a39Sopenharmony_ci
194e1051a39Sopenharmony_ci        if (genstr || genconf) {
195e1051a39Sopenharmony_ci            num = do_generate(genstr, genconf, buf);
196e1051a39Sopenharmony_ci            if (num < 0) {
197e1051a39Sopenharmony_ci                ERR_print_errors(bio_err);
198e1051a39Sopenharmony_ci                goto end;
199e1051a39Sopenharmony_ci            }
200e1051a39Sopenharmony_ci        } else {
201e1051a39Sopenharmony_ci
202e1051a39Sopenharmony_ci            if (informat == FORMAT_PEM) {
203e1051a39Sopenharmony_ci                BIO *tmp;
204e1051a39Sopenharmony_ci
205e1051a39Sopenharmony_ci                if ((b64 = BIO_new(BIO_f_base64())) == NULL)
206e1051a39Sopenharmony_ci                    goto end;
207e1051a39Sopenharmony_ci                BIO_push(b64, in);
208e1051a39Sopenharmony_ci                tmp = in;
209e1051a39Sopenharmony_ci                in = b64;
210e1051a39Sopenharmony_ci                b64 = tmp;
211e1051a39Sopenharmony_ci            }
212e1051a39Sopenharmony_ci
213e1051a39Sopenharmony_ci            num = 0;
214e1051a39Sopenharmony_ci            for (;;) {
215e1051a39Sopenharmony_ci                if (!BUF_MEM_grow(buf, num + BUFSIZ))
216e1051a39Sopenharmony_ci                    goto end;
217e1051a39Sopenharmony_ci                i = BIO_read(in, &(buf->data[num]), BUFSIZ);
218e1051a39Sopenharmony_ci                if (i <= 0)
219e1051a39Sopenharmony_ci                    break;
220e1051a39Sopenharmony_ci                /* make sure num doesn't overflow */
221e1051a39Sopenharmony_ci                if (i > LONG_MAX - num)
222e1051a39Sopenharmony_ci                    goto end;
223e1051a39Sopenharmony_ci                num += i;
224e1051a39Sopenharmony_ci            }
225e1051a39Sopenharmony_ci        }
226e1051a39Sopenharmony_ci        str = (unsigned char *)buf->data;
227e1051a39Sopenharmony_ci
228e1051a39Sopenharmony_ci    }
229e1051a39Sopenharmony_ci
230e1051a39Sopenharmony_ci    /* If any structs to parse go through in sequence */
231e1051a39Sopenharmony_ci
232e1051a39Sopenharmony_ci    if (sk_OPENSSL_STRING_num(osk)) {
233e1051a39Sopenharmony_ci        tmpbuf = str;
234e1051a39Sopenharmony_ci        tmplen = num;
235e1051a39Sopenharmony_ci        for (i = 0; i < sk_OPENSSL_STRING_num(osk); i++) {
236e1051a39Sopenharmony_ci            ASN1_TYPE *atmp;
237e1051a39Sopenharmony_ci            int typ;
238e1051a39Sopenharmony_ci            j = strtol(sk_OPENSSL_STRING_value(osk, i), NULL, 0);
239e1051a39Sopenharmony_ci            if (j <= 0 || j >= tmplen) {
240e1051a39Sopenharmony_ci                BIO_printf(bio_err, "'%s' is out of range\n",
241e1051a39Sopenharmony_ci                           sk_OPENSSL_STRING_value(osk, i));
242e1051a39Sopenharmony_ci                continue;
243e1051a39Sopenharmony_ci            }
244e1051a39Sopenharmony_ci            tmpbuf += j;
245e1051a39Sopenharmony_ci            tmplen -= j;
246e1051a39Sopenharmony_ci            atmp = at;
247e1051a39Sopenharmony_ci            ctmpbuf = tmpbuf;
248e1051a39Sopenharmony_ci            at = d2i_ASN1_TYPE(NULL, &ctmpbuf, tmplen);
249e1051a39Sopenharmony_ci            ASN1_TYPE_free(atmp);
250e1051a39Sopenharmony_ci            if (!at) {
251e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error parsing structure\n");
252e1051a39Sopenharmony_ci                ERR_print_errors(bio_err);
253e1051a39Sopenharmony_ci                goto end;
254e1051a39Sopenharmony_ci            }
255e1051a39Sopenharmony_ci            typ = ASN1_TYPE_get(at);
256e1051a39Sopenharmony_ci            if ((typ == V_ASN1_OBJECT)
257e1051a39Sopenharmony_ci                || (typ == V_ASN1_BOOLEAN)
258e1051a39Sopenharmony_ci                || (typ == V_ASN1_NULL)) {
259e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Can't parse %s type\n", ASN1_tag2str(typ));
260e1051a39Sopenharmony_ci                ERR_print_errors(bio_err);
261e1051a39Sopenharmony_ci                goto end;
262e1051a39Sopenharmony_ci            }
263e1051a39Sopenharmony_ci            /* hmm... this is a little evil but it works */
264e1051a39Sopenharmony_ci            tmpbuf = at->value.asn1_string->data;
265e1051a39Sopenharmony_ci            tmplen = at->value.asn1_string->length;
266e1051a39Sopenharmony_ci        }
267e1051a39Sopenharmony_ci        str = tmpbuf;
268e1051a39Sopenharmony_ci        num = tmplen;
269e1051a39Sopenharmony_ci    }
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_ci    if (offset < 0 || offset >= num) {
272e1051a39Sopenharmony_ci        BIO_printf(bio_err, "Error: offset out of range\n");
273e1051a39Sopenharmony_ci        goto end;
274e1051a39Sopenharmony_ci    }
275e1051a39Sopenharmony_ci
276e1051a39Sopenharmony_ci    num -= offset;
277e1051a39Sopenharmony_ci
278e1051a39Sopenharmony_ci    if (length == 0 || length > (unsigned int)num)
279e1051a39Sopenharmony_ci        length = (unsigned int)num;
280e1051a39Sopenharmony_ci    if (derout != NULL) {
281e1051a39Sopenharmony_ci        if (BIO_write(derout, str + offset, length) != (int)length) {
282e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Error writing output\n");
283e1051a39Sopenharmony_ci            ERR_print_errors(bio_err);
284e1051a39Sopenharmony_ci            goto end;
285e1051a39Sopenharmony_ci        }
286e1051a39Sopenharmony_ci    }
287e1051a39Sopenharmony_ci    if (!noout) {
288e1051a39Sopenharmony_ci        const unsigned char *p = str + offset;
289e1051a39Sopenharmony_ci
290e1051a39Sopenharmony_ci        if (it != NULL) {
291e1051a39Sopenharmony_ci            ASN1_VALUE *value = ASN1_item_d2i(NULL, &p, length, it);
292e1051a39Sopenharmony_ci            if (value == NULL) {
293e1051a39Sopenharmony_ci                BIO_printf(bio_err, "Error parsing item %s\n", it->sname);
294e1051a39Sopenharmony_ci                ERR_print_errors(bio_err);
295e1051a39Sopenharmony_ci                goto end;
296e1051a39Sopenharmony_ci            }
297e1051a39Sopenharmony_ci            ASN1_item_print(bio_out, value, 0, it, NULL);
298e1051a39Sopenharmony_ci            ASN1_item_free(value, it);
299e1051a39Sopenharmony_ci        } else {
300e1051a39Sopenharmony_ci            if (!ASN1_parse_dump(bio_out, p, length, indent, dump)) {
301e1051a39Sopenharmony_ci                ERR_print_errors(bio_err);
302e1051a39Sopenharmony_ci                goto end;
303e1051a39Sopenharmony_ci            }
304e1051a39Sopenharmony_ci        }
305e1051a39Sopenharmony_ci    }
306e1051a39Sopenharmony_ci    ret = 0;
307e1051a39Sopenharmony_ci end:
308e1051a39Sopenharmony_ci    BIO_free(derout);
309e1051a39Sopenharmony_ci    BIO_free(in);
310e1051a39Sopenharmony_ci    BIO_free(b64);
311e1051a39Sopenharmony_ci    if (ret != 0)
312e1051a39Sopenharmony_ci        ERR_print_errors(bio_err);
313e1051a39Sopenharmony_ci    BUF_MEM_free(buf);
314e1051a39Sopenharmony_ci    OPENSSL_free(name);
315e1051a39Sopenharmony_ci    OPENSSL_free(header);
316e1051a39Sopenharmony_ci    ASN1_TYPE_free(at);
317e1051a39Sopenharmony_ci    sk_OPENSSL_STRING_free(osk);
318e1051a39Sopenharmony_ci    return ret;
319e1051a39Sopenharmony_ci}
320e1051a39Sopenharmony_ci
321e1051a39Sopenharmony_cistatic int do_generate(char *genstr, const char *genconf, BUF_MEM *buf)
322e1051a39Sopenharmony_ci{
323e1051a39Sopenharmony_ci    CONF *cnf = NULL;
324e1051a39Sopenharmony_ci    int len;
325e1051a39Sopenharmony_ci    unsigned char *p;
326e1051a39Sopenharmony_ci    ASN1_TYPE *atyp = NULL;
327e1051a39Sopenharmony_ci
328e1051a39Sopenharmony_ci    if (genconf != NULL) {
329e1051a39Sopenharmony_ci        if ((cnf = app_load_config(genconf)) == NULL)
330e1051a39Sopenharmony_ci            goto err;
331e1051a39Sopenharmony_ci        if (genstr == NULL)
332e1051a39Sopenharmony_ci            genstr = NCONF_get_string(cnf, "default", "asn1");
333e1051a39Sopenharmony_ci        if (genstr == NULL) {
334e1051a39Sopenharmony_ci            BIO_printf(bio_err, "Can't find 'asn1' in '%s'\n", genconf);
335e1051a39Sopenharmony_ci            goto err;
336e1051a39Sopenharmony_ci        }
337e1051a39Sopenharmony_ci    }
338e1051a39Sopenharmony_ci
339e1051a39Sopenharmony_ci    atyp = ASN1_generate_nconf(genstr, cnf);
340e1051a39Sopenharmony_ci    NCONF_free(cnf);
341e1051a39Sopenharmony_ci    cnf = NULL;
342e1051a39Sopenharmony_ci
343e1051a39Sopenharmony_ci    if (atyp == NULL)
344e1051a39Sopenharmony_ci        return -1;
345e1051a39Sopenharmony_ci
346e1051a39Sopenharmony_ci    len = i2d_ASN1_TYPE(atyp, NULL);
347e1051a39Sopenharmony_ci
348e1051a39Sopenharmony_ci    if (len <= 0)
349e1051a39Sopenharmony_ci        goto err;
350e1051a39Sopenharmony_ci
351e1051a39Sopenharmony_ci    if (!BUF_MEM_grow(buf, len))
352e1051a39Sopenharmony_ci        goto err;
353e1051a39Sopenharmony_ci
354e1051a39Sopenharmony_ci    p = (unsigned char *)buf->data;
355e1051a39Sopenharmony_ci
356e1051a39Sopenharmony_ci    i2d_ASN1_TYPE(atyp, &p);
357e1051a39Sopenharmony_ci
358e1051a39Sopenharmony_ci    ASN1_TYPE_free(atyp);
359e1051a39Sopenharmony_ci    return len;
360e1051a39Sopenharmony_ci
361e1051a39Sopenharmony_ci err:
362e1051a39Sopenharmony_ci    NCONF_free(cnf);
363e1051a39Sopenharmony_ci    ASN1_TYPE_free(atyp);
364e1051a39Sopenharmony_ci    return -1;
365e1051a39Sopenharmony_ci}
366