1 /* asn1Decoding.c --- program to generate an ASN1 type from a DER coding.
2 * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
5 * This file is part of LIBTASN1.
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <version-etc.h>
34 #include <read-file.h>
36 /* This feature is available in gcc versions 2.5 and later. */
37 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
38 # define ATTR_NO_RETRUN
40 # define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
43 ATTR_NO_RETRUN static void
46 if (status != EXIT_SUCCESS)
47 fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
51 Usage: %s [OPTION] DEFINITIONS ENCODED ASN1TYPE\n", program_name);
53 Decodes DER data in ENCODED file, for the ASN1TYPE element\n\
54 described in ASN.1 DEFINITIONS file, and print decoded structures.\n\
57 -c, --check checks the syntax only\n\
58 -h, --help display this help and exit\n\
59 -v, --version output version information and exit\n");
60 emit_bug_reporting_address ();
66 main (int argc, char *argv[])
68 static const struct option long_options[] = {
69 {"help", no_argument, 0, 'h'},
70 {"version", no_argument, 0, 'v'},
71 {"check", no_argument, 0, 'c'},
76 char *inputFileAsnName = NULL;
77 char *inputFileDerName = NULL;
78 char *typeName = NULL;
79 int checkSyntaxOnly = 0;
80 ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
81 ASN1_TYPE structure = ASN1_TYPE_EMPTY;
82 char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
83 int asn1_result = ASN1_SUCCESS;
86 /* FILE *outputFile; */
88 set_program_name (argv[0]);
90 opterr = 0; /* disable error messages from getopt */
96 getopt_long (argc, argv, "hvc", long_options, &option_index);
98 if (option_result == -1)
101 switch (option_result)
104 usage (EXIT_SUCCESS);
106 case 'v': /* VERSION */
107 version_etc (stdout, program_name, PACKAGE, VERSION,
108 "Fabio Fiorina", NULL);
111 case 'c': /* CHECK SYNTAX */
114 case '?': /* UNKNOW OPTION */
116 "asn1Decoding: option '%s' not recognized or without argument.\n\n",
118 usage (EXIT_FAILURE);
122 "asn1Decoding: ?? getopt returned character code Ox%x ??\n",
127 if (optind == argc || optind == argc - 1 || optind == argc - 2)
129 fprintf (stderr, "asn1Decoding: input files or ASN.1 type "
131 usage (EXIT_FAILURE);
134 inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1);
135 strcpy (inputFileAsnName, argv[optind]);
137 inputFileDerName = (char *) malloc (strlen (argv[optind + 1]) + 1);
138 strcpy (inputFileDerName, argv[optind + 1]);
140 typeName = (char *) malloc (strlen (argv[optind + 2]) + 1);
141 strcpy (typeName, argv[optind + 2]);
144 asn1_parser2tree (inputFileAsnName, &definitions, errorDescription);
149 printf ("Parse: done.\n");
151 case ASN1_FILE_NOT_FOUND:
152 printf ("asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName);
154 case ASN1_SYNTAX_ERROR:
155 case ASN1_IDENTIFIER_NOT_FOUND:
156 case ASN1_NAME_TOO_LONG:
157 printf ("asn1Decoding: %s\n", errorDescription);
160 printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
163 if (asn1_result != ASN1_SUCCESS)
165 free (inputFileAsnName);
166 free (inputFileDerName);
174 der = (unsigned char *) read_binary_file (inputFileDerName, &tmplen);
180 printf ("asn1Decoding: could not read '%s'\n", inputFileDerName);
181 asn1_delete_structure (&definitions);
183 free (inputFileAsnName);
184 free (inputFileDerName);
190 /*****************************************/
192 /*****************************************/
195 outputFile=fopen("data.p12","w");
196 while(fscanf(inputFile,"%c",der+der_len) != EOF){
197 if((der_len>=0x11) && (der_len<=(0xe70)))
198 fprintf(outputFile,"%c",der[der_len]);
205 asn1_result = asn1_create_element (definitions, typeName, &structure);
207 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
210 if (asn1_result != ASN1_SUCCESS)
212 printf ("Structure creation: %s\n", asn1_strerror (asn1_result));
213 asn1_delete_structure (&definitions);
215 free (inputFileAsnName);
216 free (inputFileDerName);
223 asn1_der_decoding (&structure, der, der_len, errorDescription);
224 printf ("\nDecoding: %s\n", asn1_strerror (asn1_result));
225 if (asn1_result != ASN1_SUCCESS)
226 printf ("asn1Decoding: %s\n", errorDescription);
228 printf ("\nDECODING RESULT:\n");
229 asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
231 /*****************************************/
233 /*****************************************/
237 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
238 outputFile=fopen("encryptedData.p12","w");
240 fprintf(outputFile,"%c",der[option_index]);
247 asn1_delete_structure (&definitions);
248 asn1_delete_structure (&structure);
252 free (inputFileAsnName);
253 free (inputFileDerName);
256 if (asn1_result != ASN1_SUCCESS)