Fix typos.
[libtasn1.git] / src / asn1Decoding.c
blob69b2bdbe0f7e41981a14685e8f884b5c6490493c
1 /*
2 * Copyright (C) 2006 Free Software Foundation
3 * Copyright (C) 2002 Fabio Fiorina
5 * This file is part of LIBTASN1.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * 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, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: asn1Deoding.c */
26 /* Description: program to generate an ASN1 type from*/
27 /* a DER coding. */
28 /*****************************************************/
30 #include <stdio.h>
31 #include <string.h>
32 #include <libtasn1.h>
33 #include <stdlib.h>
34 #include <config.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
40 #ifdef HAVE_GETOPT_H
41 #include <getopt.h>
42 #endif
44 char version_man[] = "asn1Decoding (GNU libtasn1) " VERSION;
46 char help_man[] = "asn1Decoding generates an ASN1 type from a file\n"
47 "with ASN1 definitions and another one with a DER encoding.\n"
48 "\n"
49 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
50 " <file1> file with ASN1 definitions.\n"
51 " <file2> file with a DER coding.\n"
52 " <type> ASN1 type name\n"
53 "\n"
54 #ifdef HAVE_GETOPT_LONG
55 "Operation modes:\n"
56 " -h, --help shows this message and exit.\n"
57 " -v, --version shows version information and exit.\n"
58 " -c, --check checks the syntax only.\n";
59 #else
60 "Operation modes:\n"
61 " -h shows this message and exit.\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n";
64 #endif
68 /********************************************************/
69 /* Function : main */
70 /* Description: */
71 /********************************************************/
72 int
73 main(int argc,char *argv[])
76 #ifdef HAVE_GETOPT_LONG
77 static struct option long_options[] =
79 {"help", no_argument, 0, 'h'},
80 {"version", no_argument, 0, 'v'},
81 {"check", no_argument, 0, 'c'},
82 {0, 0, 0, 0}
84 int option_index = 0;
85 #endif
87 int option_result;
88 char *inputFileAsnName=NULL;
89 char *inputFileDerName=NULL;
90 char *typeName=NULL;
91 int checkSyntaxOnly=0;
92 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
93 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
94 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
95 int asn1_result=ASN1_SUCCESS;
96 FILE *inputFile;
97 unsigned char der[100*1024];
98 int der_len=0;
99 /* FILE *outputFile; */
101 opterr=0; /* disable error messages from getopt */
103 printf("\n");
105 while(1){
107 #ifdef HAVE_GETOPT_LONG
108 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
109 #else
110 option_result=getopt(argc,argv,"hvc");
111 #endif
113 if(option_result == -1) break;
115 switch(option_result){
116 case 'h': /* HELP */
117 printf("%s\n",help_man);
118 exit(0);
119 break;
120 case 'v': /* VERSION */
121 printf("%s\n",version_man);
122 exit(0);
123 break;
124 case 'c': /* CHECK SYNTAX */
125 checkSyntaxOnly = 1;
126 break;
127 case '?': /* UNKNOW OPTION */
128 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
129 printf("%s\n",help_man);
131 exit(1);
132 break;
133 default:
134 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
138 if(optind == argc){
139 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
140 fprintf(stderr," input file with DER coding missing.\n");
141 fprintf(stderr," ASN1 type name missing.\n\n");
142 printf("%s\n",help_man);
144 exit(1);
147 if(optind == argc-1){
148 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
149 fprintf(stderr," ASN1 type name missing.\n\n");
150 printf("%s\n",help_man);
152 exit(1);
155 if(optind == argc-2){
156 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
157 printf("%s\n",help_man);
159 exit(1);
162 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
163 strcpy(inputFileAsnName,argv[optind]);
165 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
166 strcpy(inputFileDerName,argv[optind+1]);
168 typeName=(char *)malloc(strlen(argv[optind+2])+1);
169 strcpy(typeName,argv[optind+2]);
171 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
173 switch(asn1_result){
174 case ASN1_SUCCESS:
175 printf("Parse: done.\n");
176 break;
177 case ASN1_FILE_NOT_FOUND:
178 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
179 break;
180 case ASN1_SYNTAX_ERROR:
181 case ASN1_IDENTIFIER_NOT_FOUND:
182 case ASN1_NAME_TOO_LONG:
183 printf("asn1Decoding: %s\n",errorDescription);
184 break;
185 default:
186 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
189 if(asn1_result != ASN1_SUCCESS){
190 free(inputFileAsnName);
191 free(inputFileDerName);
192 free(typeName);
193 exit(1);
197 inputFile=fopen(inputFileDerName,"r");
199 if(inputFile==NULL){
200 printf("asn1Decoding: file '%s' not found\n",inputFileDerName);
201 asn1_delete_structure(&definitions);
203 free(inputFileAsnName);
204 free(inputFileDerName);
205 free(typeName);
206 exit(1);
210 /*****************************************/
211 /* ONLY FOR TEST */
212 /*****************************************/
214 der_len=0;
215 outputFile=fopen("data.p12","w");
216 while(fscanf(inputFile,"%c",der+der_len) != EOF){
217 if((der_len>=0x11) && (der_len<=(0xe70)))
218 fprintf(outputFile,"%c",der[der_len]);
219 der_len++;
221 fclose(outputFile);
222 fclose(inputFile);
225 while(fscanf(inputFile,"%c",der+der_len) != EOF){
226 der_len++;
228 fclose(inputFile);
231 asn1_result=asn1_create_element(definitions,typeName,&structure);
233 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
236 if(asn1_result != ASN1_SUCCESS){
237 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
238 asn1_delete_structure(&definitions);
240 free(inputFileAsnName);
241 free(inputFileDerName);
242 free(typeName);
243 exit(1);
246 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
247 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
248 if(asn1_result != ASN1_SUCCESS)
249 printf("asn1Decoding: %s\n",errorDescription);
251 printf("\nDECODING RESULT:\n");
252 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
254 /*****************************************/
255 /* ONLY FOR TEST */
256 /*****************************************/
258 der_len=10000;
259 option_index=0;
260 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
261 outputFile=fopen("encryptedData.p12","w");
262 while(der_len>0){
263 fprintf(outputFile,"%c",der[option_index]);
264 der_len--;
265 option_index++;
267 fclose(outputFile);
270 asn1_delete_structure(&definitions);
271 asn1_delete_structure(&structure);
273 free(inputFileAsnName);
274 free(inputFileDerName);
275 free(typeName);
277 if(asn1_result != ASN1_SUCCESS)
278 exit(1);
280 exit(0);