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