Branch data Line data Source code
1 : : /* asn1Decoding.c --- program to generate an ASN1 type from a DER coding.
2 : : * Copyright (C) 2002-2012 Free Software Foundation, Inc.
3 : : *
4 : : * This file is part of LIBTASN1.
5 : : *
6 : : * This program is free software: you can redistribute it and/or modify
7 : : * it under the terms of the GNU General Public License as published by
8 : : * the Free Software Foundation, either version 3 of the License, or
9 : : * (at your option) any later version.
10 : : *
11 : : * This program is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : : * GNU General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU General Public License
17 : : * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : */
20 : :
21 : : #include <config.h>
22 : :
23 : : #include <stdio.h>
24 : : #include <string.h>
25 : : #include <stdlib.h>
26 : : #include <unistd.h>
27 : : #include <getopt.h>
28 : :
29 : : #include <libtasn1.h>
30 : :
31 : : #include <progname.h>
32 : : #include <version-etc.h>
33 : : #include <read-file.h>
34 : :
35 : : /* This feature is available in gcc versions 2.5 and later. */
36 : : #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
37 : : #define ATTR_NO_RETRUN
38 : : #else
39 : : #define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
40 : : #endif
41 : :
42 : : ATTR_NO_RETRUN static void
43 : 0 : usage (int status)
44 : : {
45 [ # # ]: 0 : if (status != EXIT_SUCCESS)
46 : 0 : fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
47 : : else
48 : : {
49 : 0 : printf ("\
50 : : Usage: %s [OPTION] DEFINITIONS ENCODED ASN1TYPE\n", program_name);
51 : 0 : printf ("\
52 : : Decodes DER data in ENCODED file, for the ASN1TYPE element\n\
53 : : described in ASN.1 DEFINITIONS file, and print decoded structures.\n\
54 : : \n");
55 : 0 : printf ("\
56 : : -h, --help display this help and exit\n\
57 : : -v, --version output version information and exit\n");
58 : 0 : emit_bug_reporting_address ();
59 : : }
60 : 0 : exit (status);
61 : : }
62 : :
63 : : int
64 : 1 : main (int argc, char *argv[])
65 : : {
66 : : static const struct option long_options[] = {
67 : : {"help", no_argument, 0, 'h'},
68 : : {"version", no_argument, 0, 'v'},
69 : : {0, 0, 0, 0}
70 : : };
71 : 1 : int option_index = 0;
72 : : int option_result;
73 : 1 : char *inputFileAsnName = NULL;
74 : 1 : char *inputFileDerName = NULL;
75 : 1 : char *typeName = NULL;
76 : 1 : ASN1_TYPE definitions = ASN1_TYPE_EMPTY;
77 : 1 : ASN1_TYPE structure = ASN1_TYPE_EMPTY;
78 : : char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
79 : 1 : int asn1_result = ASN1_SUCCESS;
80 : : unsigned char *der;
81 : 1 : int der_len = 0;
82 : : /* FILE *outputFile; */
83 : :
84 : 1 : set_program_name (argv[0]);
85 : :
86 : 1 : opterr = 0; /* disable error messages from getopt */
87 : :
88 : : while (1)
89 : : {
90 : :
91 : 1 : option_result =
92 : : getopt_long (argc, argv, "hvc", long_options, &option_index);
93 : :
94 [ + - ]: 1 : if (option_result == -1)
95 : 1 : break;
96 : :
97 [ # # # # ]: 0 : switch (option_result)
98 : : {
99 : : case 'h': /* HELP */
100 : 0 : usage (EXIT_SUCCESS);
101 : : break;
102 : : case 'v': /* VERSION */
103 : 0 : version_etc (stdout, program_name, PACKAGE, VERSION,
104 : : "Fabio Fiorina", NULL);
105 : 0 : exit (0);
106 : : break;
107 : : case '?': /* UNKNOW OPTION */
108 : 0 : fprintf (stderr,
109 : : "asn1Decoding: option '%s' not recognized or without argument.\n\n",
110 : 0 : argv[optind - 1]);
111 : 0 : usage (EXIT_FAILURE);
112 : : break;
113 : : default:
114 : 0 : fprintf (stderr,
115 : : "asn1Decoding: ?? getopt returned character code Ox%x ??\n",
116 : : option_result);
117 : : }
118 : 0 : }
119 : :
120 [ + - ][ + - ]: 1 : if (optind == argc || optind == argc - 1 || optind == argc - 2)
[ - + ]
121 : : {
122 : 0 : fprintf (stderr, "asn1Decoding: input files or ASN.1 type "
123 : : "name missing\n");
124 : 0 : usage (EXIT_FAILURE);
125 : : }
126 : :
127 : 1 : inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1);
128 : 1 : strcpy (inputFileAsnName, argv[optind]);
129 : :
130 : 1 : inputFileDerName = (char *) malloc (strlen (argv[optind + 1]) + 1);
131 : 1 : strcpy (inputFileDerName, argv[optind + 1]);
132 : :
133 : 1 : typeName = (char *) malloc (strlen (argv[optind + 2]) + 1);
134 : 1 : strcpy (typeName, argv[optind + 2]);
135 : :
136 : 1 : asn1_result =
137 : : asn1_parser2tree (inputFileAsnName, &definitions, errorDescription);
138 : :
139 [ + - - - ]: 1 : switch (asn1_result)
140 : : {
141 : : case ASN1_SUCCESS:
142 : 1 : printf ("Parse: done.\n");
143 : 1 : break;
144 : : case ASN1_FILE_NOT_FOUND:
145 : 0 : printf ("asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName);
146 : 0 : break;
147 : : case ASN1_SYNTAX_ERROR:
148 : : case ASN1_IDENTIFIER_NOT_FOUND:
149 : : case ASN1_NAME_TOO_LONG:
150 : 0 : printf ("asn1Decoding: %s\n", errorDescription);
151 : 0 : break;
152 : : default:
153 : 0 : printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
154 : : }
155 : :
156 [ - + ]: 1 : if (asn1_result != ASN1_SUCCESS)
157 : : {
158 : 0 : free (inputFileAsnName);
159 : 0 : free (inputFileDerName);
160 : 0 : free (typeName);
161 : 0 : exit (1);
162 : : }
163 : :
164 : :
165 : : {
166 : : size_t tmplen;
167 : 1 : der = (unsigned char *) read_binary_file (inputFileDerName, &tmplen);
168 : 1 : der_len = tmplen;
169 : : }
170 : :
171 [ - + ]: 1 : if (der == NULL)
172 : : {
173 : 0 : printf ("asn1Decoding: could not read '%s'\n", inputFileDerName);
174 : 0 : asn1_delete_structure (&definitions);
175 : :
176 : 0 : free (inputFileAsnName);
177 : 0 : free (inputFileDerName);
178 : 0 : free (typeName);
179 : 0 : exit (1);
180 : : }
181 : :
182 : :
183 : : /*****************************************/
184 : : /* ONLY FOR TEST */
185 : : /*****************************************/
186 : : /*
187 : : der_len=0;
188 : : outputFile=fopen("data.p12","w");
189 : : while(fscanf(inputFile,"%c",der+der_len) != EOF){
190 : : if((der_len>=0x11) && (der_len<=(0xe70)))
191 : : fprintf(outputFile,"%c",der[der_len]);
192 : : der_len++;
193 : : }
194 : : fclose(outputFile);
195 : : fclose(inputFile);
196 : : */
197 : :
198 : 1 : asn1_result = asn1_create_element (definitions, typeName, &structure);
199 : :
200 : : /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
201 : :
202 : :
203 [ - + ]: 1 : if (asn1_result != ASN1_SUCCESS)
204 : : {
205 : 0 : printf ("Structure creation: %s\n", asn1_strerror (asn1_result));
206 : 0 : asn1_delete_structure (&definitions);
207 : :
208 : 0 : free (inputFileAsnName);
209 : 0 : free (inputFileDerName);
210 : 0 : free (typeName);
211 : 0 : free (der);
212 : 0 : exit (1);
213 : : }
214 : :
215 : 1 : asn1_result =
216 : : asn1_der_decoding (&structure, der, der_len, errorDescription);
217 : 1 : printf ("\nDecoding: %s\n", asn1_strerror (asn1_result));
218 [ - + ]: 1 : if (asn1_result != ASN1_SUCCESS)
219 : 0 : printf ("asn1Decoding: %s\n", errorDescription);
220 : :
221 : 1 : printf ("\nDECODING RESULT:\n");
222 : 1 : asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
223 : :
224 : : /*****************************************/
225 : : /* ONLY FOR TEST */
226 : : /*****************************************/
227 : : /*
228 : : der_len=10000;
229 : : option_index=0;
230 : : asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
231 : : outputFile=fopen("encryptedData.p12","w");
232 : : while(der_len>0){
233 : : fprintf(outputFile,"%c",der[option_index]);
234 : : der_len--;
235 : : option_index++;
236 : : }
237 : : fclose(outputFile);
238 : : */
239 : :
240 : 1 : asn1_delete_structure (&definitions);
241 : 1 : asn1_delete_structure (&structure);
242 : :
243 : 1 : free (der);
244 : :
245 : 1 : free (inputFileAsnName);
246 : 1 : free (inputFileDerName);
247 : 1 : free (typeName);
248 : :
249 [ - + ]: 1 : if (asn1_result != ASN1_SUCCESS)
250 : 0 : exit (1);
251 : :
252 : 1 : exit (0);
253 : : }
|