Add.
[libtasn1.git] / src / asn1Decoding.c
blob8a0a2e850c38e79d552c6a8c17132155555e7b7d
1 /*
2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * LIBASN1 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 2 of the License, or
9 * (at your option) any later version.
11 * LIBASN1 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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /*****************************************************/
23 /* File: asn1Deoding.c */
24 /* Description: program to generate an ASN1 type from*/
25 /* a DER coding. */
26 /*****************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <libtasn1.h>
31 #include <stdlib.h>
32 #include <config.h>
34 #ifdef HAVE_UNISTD_H
35 #include <unistd.h>
36 #endif
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 char version_man[] = "asn1Decoding (GNU libtasn1) " VERSION;
44 char help_man[] = "asn1Decoding generates an ASN1 type from a file\n"
45 "with ASN1 definitions and another one with a DER encoding.\n"
46 "\n"
47 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
48 " <file1> file with ASN1 definitions.\n"
49 " <file2> file with a DER coding.\n"
50 " <type> ASN1 type name\n"
51 "\n"
52 #ifdef HAVE_GETOPT_LONG
53 "Operation modes:\n"
54 " -h, --help shows this message and exit.\n"
55 " -v, --version shows version information and exit.\n"
56 " -c, --check checks the syntax only.\n";
57 #else
58 "Operation modes:\n"
59 " -h shows this message and exit.\n"
60 " -v shows version information and exit.\n"
61 " -c checks the syntax only.\n";
62 #endif
66 /********************************************************/
67 /* Function : main */
68 /* Description: */
69 /********************************************************/
70 int
71 main(int argc,char *argv[])
74 #ifdef HAVE_GETOPT_LONG
75 static struct option long_options[] =
77 {"help", no_argument, 0, 'h'},
78 {"version", no_argument, 0, 'v'},
79 {"check", no_argument, 0, 'c'},
80 {0, 0, 0, 0}
82 int option_index = 0;
83 #endif
85 int option_result;
86 char *inputFileAsnName=NULL;
87 char *inputFileDerName=NULL;
88 char *typeName=NULL;
89 int checkSyntaxOnly=0;
90 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
91 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
92 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
93 int asn1_result=ASN1_SUCCESS;
94 FILE *inputFile;
95 unsigned char der[100*1024];
96 int der_len=0;
97 /* FILE *outputFile; */
99 opterr=0; /* disable error messages from getopt */
101 printf("\n");
103 while(1){
105 #ifdef HAVE_GETOPT_LONG
106 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
107 #else
108 option_result=getopt(argc,argv,"hvc");
109 #endif
111 if(option_result == -1) break;
113 switch(option_result){
114 case 'h': /* HELP */
115 printf("%s\n",help_man);
116 exit(0);
117 break;
118 case 'v': /* VERSION */
119 printf("%s\n",version_man);
120 exit(0);
121 break;
122 case 'c': /* CHECK SYNTAX */
123 checkSyntaxOnly = 1;
124 break;
125 case '?': /* UNKNOW OPTION */
126 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
127 printf("%s\n",help_man);
129 exit(1);
130 break;
131 default:
132 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
136 if(optind == argc){
137 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
138 fprintf(stderr," input file with DER coding missing.\n");
139 fprintf(stderr," ASN1 type name missing.\n\n");
140 printf("%s\n",help_man);
142 exit(1);
145 if(optind == argc-1){
146 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
147 fprintf(stderr," ASN1 type name missing.\n\n");
148 printf("%s\n",help_man);
150 exit(1);
153 if(optind == argc-2){
154 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
155 printf("%s\n",help_man);
157 exit(1);
160 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
161 strcpy(inputFileAsnName,argv[optind]);
163 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
164 strcpy(inputFileDerName,argv[optind+1]);
166 typeName=(char *)malloc(strlen(argv[optind+2])+1);
167 strcpy(typeName,argv[optind+2]);
169 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
171 switch(asn1_result){
172 case ASN1_SUCCESS:
173 printf("Parse: done.\n");
174 break;
175 case ASN1_FILE_NOT_FOUND:
176 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
177 break;
178 case ASN1_SYNTAX_ERROR:
179 case ASN1_IDENTIFIER_NOT_FOUND:
180 case ASN1_NAME_TOO_LONG:
181 printf("asn1Decoding: %s\n",errorDescription);
182 break;
183 default:
184 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
187 if(asn1_result != ASN1_SUCCESS){
188 free(inputFileAsnName);
189 free(inputFileDerName);
190 free(typeName);
191 exit(1);
195 inputFile=fopen(inputFileDerName,"r");
197 if(inputFile==NULL){
198 printf("asn1Decoding: file '%s' not found\n",inputFileDerName);
199 asn1_delete_structure(&definitions);
201 free(inputFileAsnName);
202 free(inputFileDerName);
203 free(typeName);
204 exit(1);
208 /*****************************************/
209 /* ONLY FOR TEST */
210 /*****************************************/
212 der_len=0;
213 outputFile=fopen("data.p12","w");
214 while(fscanf(inputFile,"%c",der+der_len) != EOF){
215 if((der_len>=0x11) && (der_len<=(0xe70)))
216 fprintf(outputFile,"%c",der[der_len]);
217 der_len++;
219 fclose(outputFile);
220 fclose(inputFile);
223 while(fscanf(inputFile,"%c",der+der_len) != EOF){
224 der_len++;
226 fclose(inputFile);
229 asn1_result=asn1_create_element(definitions,typeName,&structure);
231 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
234 if(asn1_result != ASN1_SUCCESS){
235 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
236 asn1_delete_structure(&definitions);
238 free(inputFileAsnName);
239 free(inputFileDerName);
240 free(typeName);
241 exit(1);
244 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
245 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
246 if(asn1_result != ASN1_SUCCESS)
247 printf("asn1Decoding: %s\n",errorDescription);
249 printf("\nDECODING RESULT:\n");
250 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
252 /*****************************************/
253 /* ONLY FOR TEST */
254 /*****************************************/
256 der_len=10000;
257 option_index=0;
258 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
259 outputFile=fopen("encryptedData.p12","w");
260 while(der_len>0){
261 fprintf(outputFile,"%c",der[option_index]);
262 der_len--;
263 option_index++;
265 fclose(outputFile);
268 asn1_delete_structure(&definitions);
269 asn1_delete_structure(&structure);
271 free(inputFileAsnName);
272 free(inputFileDerName);
273 free(typeName);
275 if(asn1_result != ASN1_SUCCESS)
276 exit(1);
278 exit(0);