Initialize Tizen 2.3
[external/libtasn1.git] / src / asn1Decoding.c
1 /* asn1Decoding.c ---  program to generate an ASN1 type from a DER coding.
2  * Copyright (C) 2002, 2006, 2007, 2008, 2009, 2010 Free Software
3  * Foundation, Inc.
4  *
5  * This file is part of LIBTASN1.
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <getopt.h>
29
30 #include <libtasn1.h>
31
32 #include <progname.h>
33 #include <version-etc.h>
34 #include <read-file.h>
35
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
39 #else
40 # define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
41 #endif
42
43 ATTR_NO_RETRUN static void
44 usage (int status)
45 {
46   if (status != EXIT_SUCCESS)
47     fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
48   else
49     {
50       printf ("\
51 Usage: %s [OPTION] DEFINITIONS ENCODED ASN1TYPE\n", program_name);
52       printf ("\
53 Decodes DER data in ENCODED file, for the ASN1TYPE element\n\
54 described in ASN.1 DEFINITIONS file, and print decoded structures.\n\
55 \n");
56       printf ("\
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 ();
61     }
62   exit (status);
63 }
64
65 int
66 main (int argc, char *argv[])
67 {
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'},
72     {0, 0, 0, 0}
73   };
74   int option_index = 0;
75   int option_result;
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;
84   unsigned char *der;
85   int der_len = 0;
86   /* FILE *outputFile; */
87
88   set_program_name (argv[0]);
89
90   opterr = 0;                   /* disable error messages from getopt */
91
92   while (1)
93     {
94
95       option_result =
96         getopt_long (argc, argv, "hvc", long_options, &option_index);
97
98       if (option_result == -1)
99         break;
100
101       switch (option_result)
102         {
103         case 'h':               /* HELP */
104           usage (EXIT_SUCCESS);
105           break;
106         case 'v':               /* VERSION */
107           version_etc (stdout, program_name, PACKAGE, VERSION,
108                        "Fabio Fiorina", NULL);
109           exit (0);
110           break;
111         case 'c':               /* CHECK SYNTAX */
112           checkSyntaxOnly = 1;
113           break;
114         case '?':               /* UNKNOW OPTION */
115           fprintf (stderr,
116                    "asn1Decoding: option '%s' not recognized or without argument.\n\n",
117                    argv[optind - 1]);
118           usage (EXIT_FAILURE);
119           break;
120         default:
121           fprintf (stderr,
122                    "asn1Decoding: ?? getopt returned character code Ox%x ??\n",
123                    option_result);
124         }
125     }
126
127   if (optind == argc || optind == argc - 1 || optind == argc - 2)
128     {
129       fprintf (stderr, "asn1Decoding: input files or ASN.1 type "
130                "name missing\n");
131       usage (EXIT_FAILURE);
132     }
133
134   inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1);
135   strcpy (inputFileAsnName, argv[optind]);
136
137   inputFileDerName = (char *) malloc (strlen (argv[optind + 1]) + 1);
138   strcpy (inputFileDerName, argv[optind + 1]);
139
140   typeName = (char *) malloc (strlen (argv[optind + 2]) + 1);
141   strcpy (typeName, argv[optind + 2]);
142
143   asn1_result =
144     asn1_parser2tree (inputFileAsnName, &definitions, errorDescription);
145
146   switch (asn1_result)
147     {
148     case ASN1_SUCCESS:
149       printf ("Parse: done.\n");
150       break;
151     case ASN1_FILE_NOT_FOUND:
152       printf ("asn1Decoding: FILE %s NOT FOUND\n", inputFileAsnName);
153       break;
154     case ASN1_SYNTAX_ERROR:
155     case ASN1_IDENTIFIER_NOT_FOUND:
156     case ASN1_NAME_TOO_LONG:
157       printf ("asn1Decoding: %s\n", errorDescription);
158       break;
159     default:
160       printf ("libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
161     }
162
163   if (asn1_result != ASN1_SUCCESS)
164     {
165       free (inputFileAsnName);
166       free (inputFileDerName);
167       free (typeName);
168       exit (1);
169     }
170
171
172   {
173     size_t tmplen;
174     der = (unsigned char *) read_binary_file (inputFileDerName, &tmplen);
175     der_len = tmplen;
176   }
177
178   if (der == NULL)
179     {
180       printf ("asn1Decoding: could not read '%s'\n", inputFileDerName);
181       asn1_delete_structure (&definitions);
182
183       free (inputFileAsnName);
184       free (inputFileDerName);
185       free (typeName);
186       exit (1);
187     }
188
189
190  /*****************************************/
191   /* ONLY FOR TEST                         */
192  /*****************************************/
193   /*
194      der_len=0;
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]);
199      der_len++;
200      }
201      fclose(outputFile);
202      fclose(inputFile);
203    */
204
205   asn1_result = asn1_create_element (definitions, typeName, &structure);
206
207   /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
208
209
210   if (asn1_result != ASN1_SUCCESS)
211     {
212       printf ("Structure creation: %s\n", asn1_strerror (asn1_result));
213       asn1_delete_structure (&definitions);
214
215       free (inputFileAsnName);
216       free (inputFileDerName);
217       free (typeName);
218       free (der);
219       exit (1);
220     }
221
222   asn1_result =
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);
227
228   printf ("\nDECODING RESULT:\n");
229   asn1_print_structure (stdout, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
230
231  /*****************************************/
232   /* ONLY FOR TEST                         */
233  /*****************************************/
234   /*
235      der_len=10000;
236      option_index=0;
237      asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
238      outputFile=fopen("encryptedData.p12","w");
239      while(der_len>0){
240      fprintf(outputFile,"%c",der[option_index]);
241      der_len--;
242      option_index++;
243      }
244      fclose(outputFile);
245    */
246
247   asn1_delete_structure (&definitions);
248   asn1_delete_structure (&structure);
249
250   free (der);
251
252   free (inputFileAsnName);
253   free (inputFileDerName);
254   free (typeName);
255
256   if (asn1_result != ASN1_SUCCESS)
257     exit (1);
258
259   exit (0);
260 }