*** empty log message ***
[libtasn1.git] / src / asn1Decoding.c
blob1df53fbc509fd1052c9b58bfd3e2180ce15b96ab
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 <config.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <libtasn1.h>
35 #include <stdlib.h>
36 #include <unistd.h>
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 char version_man[] = "asn1Decoding (GNU libtasn1) " VERSION "\n"
43 "Written by Fabio Fiorina."
44 "\n"
45 "Copyright (C) 2002, 2006 Free Software Foundation, Inc.\n";
47 char help_man[] =
48 "asn1Decoding generates an ASN1 type from FILE1\n"
49 "with ASN1 definitions and FILE2 with a DER encoding.\n"
50 "\n"
51 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
52 " <file1> file with ASN1 definitions.\n"
53 " <file2> file with a DER coding.\n"
54 " <type> ASN1 type name\n"
55 "\n"
56 #ifdef HAVE_GETOPT_LONG
57 "Operation modes:\n"
58 " -h, --help shows this message and exit.\n"
59 " -v, --version shows version information and exit.\n"
60 " -c, --check checks the syntax only.\n"
61 #else
62 "Operation modes:\n"
63 " -h shows this message and exit.\n"
64 " -v shows version information and exit.\n"
65 " -c checks the syntax only.\n"
66 #endif
67 "Report bugs to <" PACKAGE_BUGREPORT ">.";
69 /********************************************************/
70 /* Function : main */
71 /* Description: */
72 /********************************************************/
73 int
74 main(int argc,char *argv[])
77 #ifdef HAVE_GETOPT_LONG
78 static struct option long_options[] =
80 {"help", no_argument, 0, 'h'},
81 {"version", no_argument, 0, 'v'},
82 {"check", no_argument, 0, 'c'},
83 {0, 0, 0, 0}
85 int option_index = 0;
86 #endif
88 int option_result;
89 char *inputFileAsnName=NULL;
90 char *inputFileDerName=NULL;
91 char *typeName=NULL;
92 int checkSyntaxOnly=0;
93 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
94 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
95 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
96 int asn1_result=ASN1_SUCCESS;
97 FILE *inputFile;
98 unsigned char der[100*1024];
99 int der_len=0;
100 /* FILE *outputFile; */
102 opterr=0; /* disable error messages from getopt */
104 printf("\n");
106 while(1){
108 #ifdef HAVE_GETOPT_LONG
109 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
110 #else
111 option_result=getopt(argc,argv,"hvc");
112 #endif
114 if(option_result == -1) break;
116 switch(option_result){
117 case 'h': /* HELP */
118 printf("%s\n",help_man);
119 exit(0);
120 break;
121 case 'v': /* VERSION */
122 printf("%s\n",version_man);
123 exit(0);
124 break;
125 case 'c': /* CHECK SYNTAX */
126 checkSyntaxOnly = 1;
127 break;
128 case '?': /* UNKNOW OPTION */
129 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
130 printf("%s\n",help_man);
132 exit(1);
133 break;
134 default:
135 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
139 if(optind == argc){
140 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
141 fprintf(stderr," input file with DER coding missing.\n");
142 fprintf(stderr," ASN1 type name missing.\n\n");
143 printf("%s\n",help_man);
145 exit(1);
148 if(optind == argc-1){
149 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
150 fprintf(stderr," ASN1 type name missing.\n\n");
151 printf("%s\n",help_man);
153 exit(1);
156 if(optind == argc-2){
157 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
158 printf("%s\n",help_man);
160 exit(1);
163 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
164 strcpy(inputFileAsnName,argv[optind]);
166 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
167 strcpy(inputFileDerName,argv[optind+1]);
169 typeName=(char *)malloc(strlen(argv[optind+2])+1);
170 strcpy(typeName,argv[optind+2]);
172 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
174 switch(asn1_result){
175 case ASN1_SUCCESS:
176 printf("Parse: done.\n");
177 break;
178 case ASN1_FILE_NOT_FOUND:
179 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
180 break;
181 case ASN1_SYNTAX_ERROR:
182 case ASN1_IDENTIFIER_NOT_FOUND:
183 case ASN1_NAME_TOO_LONG:
184 printf("asn1Decoding: %s\n",errorDescription);
185 break;
186 default:
187 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
190 if(asn1_result != ASN1_SUCCESS){
191 free(inputFileAsnName);
192 free(inputFileDerName);
193 free(typeName);
194 exit(1);
198 inputFile=fopen(inputFileDerName,"r");
200 if(inputFile==NULL){
201 printf("asn1Decoding: file '%s' not found\n",inputFileDerName);
202 asn1_delete_structure(&definitions);
204 free(inputFileAsnName);
205 free(inputFileDerName);
206 free(typeName);
207 exit(1);
211 /*****************************************/
212 /* ONLY FOR TEST */
213 /*****************************************/
215 der_len=0;
216 outputFile=fopen("data.p12","w");
217 while(fscanf(inputFile,"%c",der+der_len) != EOF){
218 if((der_len>=0x11) && (der_len<=(0xe70)))
219 fprintf(outputFile,"%c",der[der_len]);
220 der_len++;
222 fclose(outputFile);
223 fclose(inputFile);
226 while(fscanf(inputFile,"%c",der+der_len) != EOF){
227 der_len++;
229 fclose(inputFile);
232 asn1_result=asn1_create_element(definitions,typeName,&structure);
234 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
237 if(asn1_result != ASN1_SUCCESS){
238 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
239 asn1_delete_structure(&definitions);
241 free(inputFileAsnName);
242 free(inputFileDerName);
243 free(typeName);
244 exit(1);
247 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
248 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
249 if(asn1_result != ASN1_SUCCESS)
250 printf("asn1Decoding: %s\n",errorDescription);
252 printf("\nDECODING RESULT:\n");
253 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
255 /*****************************************/
256 /* ONLY FOR TEST */
257 /*****************************************/
259 der_len=10000;
260 option_index=0;
261 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
262 outputFile=fopen("encryptedData.p12","w");
263 while(der_len>0){
264 fprintf(outputFile,"%c",der[option_index]);
265 der_len--;
266 option_index++;
268 fclose(outputFile);
271 asn1_delete_structure(&definitions);
272 asn1_delete_structure(&structure);
274 free(inputFileAsnName);
275 free(inputFileDerName);
276 free(typeName);
278 if(asn1_result != ASN1_SUCCESS)
279 exit(1);
281 exit(0);