From 3b8276bd7eb958902bdcb141f9faee158320fa17 Mon Sep 17 00:00:00 2001 From: Pekka Riikonen Date: Tue, 17 Jul 2007 17:26:45 +0000 Subject: [PATCH] Added apps/asn1utils, asn1dump. --- apps/Makefile.ad | 14 ++- apps/asn1utils/Makefile.am | 25 ++++ apps/asn1utils/asn1dump.c | 234 ++++++++++++++++++++++++++++++++++++ apps/asn1utils/configure.ad | 24 ++++ distdir/default | 3 + 5 files changed, 295 insertions(+), 5 deletions(-) create mode 100644 apps/asn1utils/Makefile.am create mode 100644 apps/asn1utils/asn1dump.c create mode 100644 apps/asn1utils/configure.ad diff --git a/apps/Makefile.ad b/apps/Makefile.ad index 2be826d5..4c8d26d6 100644 --- a/apps/Makefile.ad +++ b/apps/Makefile.ad @@ -17,17 +17,21 @@ AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign -SUBDIRS = \ +SUBDIRS = \ #ifdef SILC_DIST_AUTODIST - autodist \ + autodist \ #endif SILC_DIST_AUTODIST #ifdef SILC_DIST_SERVER - @SILCD_SUBDIR@ \ + @SILCD_SUBDIR@ \ #endif SILC_DIST_SERVER #ifdef SILC_DIST_CLIENT - @IRSSI_SUBDIR@ \ + @IRSSI_SUBDIR@ \ #endif SILC_DIST_CLIENT #ifdef SILC_DIST_INPLACE -# silcstress +# silcstress \ #endif SILC_DIST_INPLACE +#ifdef SILC_DIST_ASN1UTILS + asn1utils \ +#endif SILC_DIST_ASN1UTILS + diff --git a/apps/asn1utils/Makefile.am b/apps/asn1utils/Makefile.am new file mode 100644 index 00000000..b097dd12 --- /dev/null +++ b/apps/asn1utils/Makefile.am @@ -0,0 +1,25 @@ +# +# Makefile.am +# +# Author: Pekka Riikonen +# +# Copyright (C) 2007 Pekka Riikonen +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +AUTOMAKE_OPTIONS = 1.0 no-dependencies foreign + +bin_PROGRAMS = asn1dump +asn1dump_SOURCES = asn1dump.c + +LIBS = $(SILC_COMMON_LIBS) + +include $(top_srcdir)/Makefile.defines.in diff --git a/apps/asn1utils/asn1dump.c b/apps/asn1utils/asn1dump.c new file mode 100644 index 00000000..a5e59906 --- /dev/null +++ b/apps/asn1utils/asn1dump.c @@ -0,0 +1,234 @@ +/* + + asn1dump.c + + Author: Pekka Riikonen + + Copyright (C) 2007 Pekka Riikonen + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; version 2 of the License. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + +*/ + +#include "silc.h" + +SilcBool hexdump = FALSE; +SilcBool dec_base64 = FALSE; + +const char *asn1_tag_name(SilcAsn1Tag tag) +{ + switch (tag) { + case SILC_ASN1_END: + return "END"; + case SILC_ASN1_TAG_OPTS: + return ""; + case SILC_ASN1_TAG_CHOICE: + return "choice"; + case SILC_ASN1_TAG_ANY: + return "any"; + case SILC_ASN1_TAG_ANY_PRIMITIVE: + return "any primitive"; + case SILC_ASN1_TAG_SEQUENCE_OF: + return "sequence of"; + case SILC_ASN1_TAG_SEQUENCE: + return "sequence"; + case SILC_ASN1_TAG_SET: + return "set"; + case SILC_ASN1_TAG_INTEGER: + return "integer"; + case SILC_ASN1_TAG_SHORT_INTEGER: + return "short integer"; + case SILC_ASN1_TAG_OID: + return "oid"; + case SILC_ASN1_TAG_BOOLEAN: + return "boolean"; + case SILC_ASN1_TAG_OCTET_STRING: + return "octet-string"; + case SILC_ASN1_TAG_BIT_STRING: + return "bit-string"; + case SILC_ASN1_TAG_NULL: + return "null"; + case SILC_ASN1_TAG_ENUM: + return "enum"; + case SILC_ASN1_TAG_UTC_TIME: + return "utc-time"; + case SILC_ASN1_TAG_GENERALIZED_TIME: + return "generalized-time"; + case SILC_ASN1_TAG_UTF8_STRING: + return "utf8-string"; + case SILC_ASN1_TAG_NUMERIC_STRING: + return "numeric-string"; + case SILC_ASN1_TAG_PRINTABLE_STRING: + return "printable-string"; + case SILC_ASN1_TAG_IA5_STRING: + return "ia5-string"; + case SILC_ASN1_TAG_VISIBLE_STRING: + return "visible-string"; + case SILC_ASN1_TAG_UNIVERSAL_STRING: + return "universal-string"; + case SILC_ASN1_TAG_UNRESTRICTED_STRING: + return "unrestricted-string"; + case SILC_ASN1_TAG_BMP_STRING: + return "bmp-string"; + case SILC_ASN1_TAG_ODE: + return "ode"; + case SILC_ASN1_TAG_ETI: + return "eti"; + case SILC_ASN1_TAG_REAL: + return "real"; + case SILC_ASN1_TAG_EMBEDDED: + return "embedded"; + case SILC_ASN1_TAG_ROI: + return "roi"; + case SILC_ASN1_TAG_TELETEX_STRING: + return "teletex-string"; + case SILC_ASN1_TAG_VIDEOTEX_STRING: + return "videotex-string"; + case SILC_ASN1_TAG_GRAPHIC_STRING: + return "graphic-string"; + case SILC_ASN1_TAG_GENERAL_STRING: + return "general-string"; + default: + break; + } + return "unknown"; +} + +int asn1_dump(SilcAsn1 asn1, SilcBuffer src) +{ + SilcBool ret = FALSE; + SilcBerEncoding renc; + SilcUInt32 rtag; + const unsigned char *rdata; + SilcUInt32 rdata_len, len = 0; + SilcBool rindef; + char indent[64]; + int depth = 0; + + SILC_LOG_DEBUG(("Dumping ASN.1")); + memset(indent, 0, sizeof(indent)); + + while (silc_buffer_len(src)) { + /* Decode the BER block */ + ret = silc_ber_decode(src, NULL, &renc, &rtag, &rdata, + &rdata_len, &rindef, &len); + if (!ret) { + fprintf(stderr, "Error: Cannot parse BER block, malformed ASN.1 data"); + return -1; + } + + memset(indent, 32, depth); + + fprintf(stdout, "%04d: %s[%s] [%d]", depth, indent, + asn1_tag_name(rtag), (int)rtag); + + if (rtag != SILC_ASN1_TAG_SEQUENCE) { + if (hexdump) { + fprintf(stdout, " [length %lu]\n", rdata_len); + silc_hexdump(rdata, rdata_len, stdout); + } else { + fprintf(stdout, "\n"); + } + } else { + fprintf(stdout, "\n"); + } + + if (rtag == SILC_ASN1_TAG_SEQUENCE && depth < sizeof(indent)) + depth++; + + if (renc == SILC_BER_ENC_PRIMITIVE) + len = len + rdata_len; + else + len = len; + + if (len) + silc_buffer_pull(src, len); + } + + return 0; +} + +void usage(void) +{ + fprintf(stdout, "" +"Usage: asn1dump [OPTIONS] FILE\n" +"\n" +"Operation modes:\n" +" -h Print this help, then exit\n" +" -x HEX dump ASN.1 data\n" +" -b Decode Base64 encoding\n" +"\n" + ); +} + +int main(int argc, char **argv) +{ + int opt, ret, i; + SilcAsn1 asn1; + SilcBufferStruct buf; + unsigned char *data, *tmp; + SilcUInt32 data_len; + + if (argc < 2) { + usage(); + return 1; + } + + i = 1; + while ((opt = getopt(argc, argv, "hxb")) != EOF) { + switch (opt) { + case 'h': + usage(); + return 1; + break; + + case 'x': + hexdump = TRUE; + i++; + break; + + case 'b': + dec_base64 = TRUE; + i++; + break; + + default: + usage(); + return 1; + } + } + + data = tmp = silc_file_readfile(argv[i], &data_len, NULL); + if (!data) { + fprintf(stderr, "Error: Cannot read file '%s': %s", argv[i], + strerror(errno)); + return 1; + } + + if (dec_base64) { + data = silc_base64_decode(NULL, data, data_len, &data_len); + if (!data) { + fprintf(stderr, "Error: Cannot decode Base64 encoding\n"); + return 1; + } + silc_free(tmp); + } + + silc_buffer_set(&buf, data, data_len); + + asn1 = silc_asn1_alloc(NULL); + + ret = asn1_dump(asn1, &buf); + + silc_asn1_free(asn1); + silc_free(data); + + return ret; +} diff --git a/apps/asn1utils/configure.ad b/apps/asn1utils/configure.ad new file mode 100644 index 00000000..5458a0aa --- /dev/null +++ b/apps/asn1utils/configure.ad @@ -0,0 +1,24 @@ +#ifdef SILC_DIST_ASN1UTILS +# +# apps/asn1utils/configure.ad +# +# Author: Pekka Riikonen +# +# Copyright (C) 2007 Pekka Riikonen +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# + +AC_CONFIG_FILES( +apps/asn1utils/Makefile +) + +AC_SUBST(SILCD_SUBDIR) +#endif SILC_DIST_ASN1UTILS diff --git a/distdir/default b/distdir/default index 53ecb918..b6167faa 100644 --- a/distdir/default +++ b/distdir/default @@ -19,4 +19,7 @@ define SILC_DIST_SIM # SFTP is undefined in server, so force it here define SILC_DIST_SFTP +# Apps +define SILC_DIST_ASN1UTILS + pre-hook distdir/pre-run -- 2.24.0