Path: blob/main/crypto/heimdal/lib/asn1/asn1_print.c
34878 views
/*1* Copyright (c) 1997 - 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "der_locl.h"36#include <com_err.h>37#include <sys/types.h>38#include <sys/stat.h>39#include <getarg.h>40#include <err.h>41#include <der.h>4243static int indent_flag = 1;44static int inner_flag = 0;4546static unsigned long indefinite_form_loop;47static unsigned long indefinite_form_loop_max = 10000;4849static size_t50loop (unsigned char *buf, size_t len, int indent)51{52unsigned char *start_buf = buf;5354while (len > 0) {55int ret;56Der_class class;57Der_type type;58unsigned int tag;59size_t sz;60size_t length;61size_t loop_length = 0;62int end_tag = 0;63const char *tagname;6465ret = der_get_tag (buf, len, &class, &type, &tag, &sz);66if (ret)67errx (1, "der_get_tag: %s", error_message (ret));68if (sz > len)69errx (1, "unreasonable length (%u) > %u",70(unsigned)sz, (unsigned)len);71buf += sz;72len -= sz;73if (indent_flag) {74int i;75for (i = 0; i < indent; ++i)76printf (" ");77}78printf ("%s %s ", der_get_class_name(class), der_get_type_name(type));79tagname = der_get_tag_name(tag);80if (class == ASN1_C_UNIV && tagname != NULL)81printf ("%s = ", tagname);82else83printf ("tag %d = ", tag);84ret = der_get_length (buf, len, &length, &sz);85if (ret)86errx (1, "der_get_tag: %s", error_message (ret));87if (sz > len)88errx (1, "unreasonable tag length (%u) > %u",89(unsigned)sz, (unsigned)len);90buf += sz;91len -= sz;92if (length == ASN1_INDEFINITE) {93if ((class == ASN1_C_UNIV && type == PRIM && tag == UT_OctetString) ||94(class == ASN1_C_CONTEXT && type == CONS) ||95(class == ASN1_C_UNIV && type == CONS && tag == UT_Sequence) ||96(class == ASN1_C_UNIV && type == CONS && tag == UT_Set)) {97printf("*INDEFINITE FORM*");98} else {99fflush(stdout);100errx(1, "indef form used on unsupported object");101}102end_tag = 1;103if (indefinite_form_loop > indefinite_form_loop_max)104errx(1, "indefinite form used recursively more then %lu "105"times, aborting", indefinite_form_loop_max);106indefinite_form_loop++;107length = len;108} else if (length > len) {109printf("\n");110fflush(stdout);111errx (1, "unreasonable inner length (%u) > %u",112(unsigned)length, (unsigned)len);113}114if (class == ASN1_C_CONTEXT || class == ASN1_C_APPL) {115printf ("%lu bytes [%u]", (unsigned long)length, tag);116if (type == CONS) {117printf("\n");118loop_length = loop (buf, length, indent + 2);119} else {120printf(" IMPLICIT content\n");121}122} else if (class == ASN1_C_UNIV) {123switch (tag) {124case UT_EndOfContent:125printf (" INDEFINITE length was %lu\n",126(unsigned long)(buf - start_buf));127break;128case UT_Set :129case UT_Sequence :130printf ("%lu bytes {\n", (unsigned long)length);131loop_length = loop (buf, length, indent + 2);132if (indent_flag) {133int i;134for (i = 0; i < indent; ++i)135printf (" ");136printf ("}\n");137} else138printf ("} indent = %d\n", indent / 2);139break;140case UT_Integer : {141int val;142143if (length <= sizeof(val)) {144ret = der_get_integer (buf, length, &val, NULL);145if (ret)146errx (1, "der_get_integer: %s", error_message (ret));147printf ("integer %d\n", val);148} else {149heim_integer vali;150char *p;151152ret = der_get_heim_integer(buf, length, &vali, NULL);153if (ret)154errx (1, "der_get_heim_integer: %s",155error_message (ret));156ret = der_print_hex_heim_integer(&vali, &p);157if (ret)158errx (1, "der_print_hex_heim_integer: %s",159error_message (ret));160printf ("BIG NUM integer: length %lu %s\n",161(unsigned long)length, p);162free(p);163}164break;165}166case UT_OctetString : {167heim_octet_string str;168size_t i;169170ret = der_get_octet_string (buf, length, &str, NULL);171if (ret)172errx (1, "der_get_octet_string: %s", error_message (ret));173printf ("(length %lu), ", (unsigned long)length);174175if (inner_flag) {176Der_class class;177Der_type type;178unsigned int tag;179180ret = der_get_tag(str.data, str.length,181&class, &type, &tag, &sz);182if (ret || sz > str.length ||183type != CONS || tag != UT_Sequence)184goto just_an_octet_string;185186printf("{\n");187loop (str.data, str.length, indent + 2);188for (i = 0; i < indent; ++i)189printf (" ");190printf ("}\n");191192} else {193unsigned char *uc;194195just_an_octet_string:196uc = (unsigned char *)str.data;197for (i = 0; i < min(16,length); ++i)198printf ("%02x", uc[i]);199printf ("\n");200}201free (str.data);202break;203}204case UT_IA5String :205case UT_PrintableString : {206heim_printable_string str;207unsigned char *s;208size_t n;209210memset(&str, 0, sizeof(str));211212ret = der_get_printable_string (buf, length, &str, NULL);213if (ret)214errx (1, "der_get_general_string: %s",215error_message (ret));216s = str.data;217printf("\"");218for (n = 0; n < str.length; n++) {219if (isprint((int)s[n]))220printf ("%c", s[n]);221else222printf ("#%02x", s[n]);223}224printf("\"\n");225der_free_printable_string(&str);226break;227}228case UT_GeneralizedTime :229case UT_GeneralString :230case UT_VisibleString :231case UT_UTF8String : {232heim_general_string str;233234ret = der_get_general_string (buf, length, &str, NULL);235if (ret)236errx (1, "der_get_general_string: %s",237error_message (ret));238printf ("\"%s\"\n", str);239free (str);240break;241}242case UT_OID: {243heim_oid o;244char *p;245246ret = der_get_oid(buf, length, &o, NULL);247if (ret)248errx (1, "der_get_oid: %s", error_message (ret));249ret = der_print_heim_oid(&o, '.', &p);250der_free_oid(&o);251if (ret)252errx (1, "der_print_heim_oid: %s", error_message (ret));253printf("%s\n", p);254free(p);255256break;257}258case UT_Enumerated: {259int num;260261ret = der_get_integer (buf, length, &num, NULL);262if (ret)263errx (1, "der_get_enum: %s", error_message (ret));264265printf("%u\n", num);266break;267}268default :269printf ("%lu bytes\n", (unsigned long)length);270break;271}272}273if (end_tag) {274if (loop_length == 0)275errx(1, "zero length INDEFINITE data ? indent = %d\n",276indent / 2);277if (loop_length < length)278length = loop_length;279if (indefinite_form_loop == 0)280errx(1, "internal error in indefinite form loop detection");281indefinite_form_loop--;282} else if (loop_length)283errx(1, "internal error for INDEFINITE form");284buf += length;285len -= length;286}287return 0;288}289290static int291doit (const char *filename)292{293int fd = open (filename, O_RDONLY);294struct stat sb;295unsigned char *buf;296size_t len;297int ret;298299if(fd < 0)300err (1, "opening %s for read", filename);301if (fstat (fd, &sb) < 0)302err (1, "stat %s", filename);303len = sb.st_size;304buf = emalloc (len);305if (read (fd, buf, len) != len)306errx (1, "read failed");307close (fd);308ret = loop (buf, len, 0);309free (buf);310return ret;311}312313314static int version_flag;315static int help_flag;316struct getargs args[] = {317{ "indent", 0, arg_negative_flag, &indent_flag },318{ "inner", 0, arg_flag, &inner_flag, "try to parse inner structures of OCTET STRING" },319{ "version", 0, arg_flag, &version_flag },320{ "help", 0, arg_flag, &help_flag }321};322int num_args = sizeof(args) / sizeof(args[0]);323324static void325usage(int code)326{327arg_printusage(args, num_args, NULL, "dump-file");328exit(code);329}330331int332main(int argc, char **argv)333{334int optidx = 0;335336setprogname (argv[0]);337initialize_asn1_error_table ();338if(getarg(args, num_args, argc, argv, &optidx))339usage(1);340if(help_flag)341usage(0);342if(version_flag) {343print_version(NULL);344exit(0);345}346argv += optidx;347argc -= optidx;348if (argc != 1)349usage (1);350return doit (argv[0]);351}352353354