Add credits.
[libtasn1.git] / src / asn1Decoding.c
blobe4879e269a46ca2b066851ca5306debad836e256
1 /*
2 * Copyright (C) 2006, 2007 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 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.
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.
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/>.
23 /*****************************************************/
24 /* File: asn1Deoding.c */
25 /* Description: program to generate an ASN1 type from*/
26 /* a DER coding. */
27 /*****************************************************/
29 #include <config.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <getopt.h>
37 #include <libtasn1.h>
39 #include <progname.h>
40 #include <version-etc.h>
41 #include <read-file.h>
43 static const char help_man[] =
44 "Usage: asn1Decoding [OPTION] DEFINITIONS ENCODED ASN1TYPE\n"
45 "asn1Decoding decodes DER data in ENCODED file, for the ASN1TYPE element\n"
46 "described in ASN.1 DEFINITIONS file, and print decoded structures.\n"
47 "\n"
48 " -c, --check checks the syntax only\n"
49 " -h, --help display this help and exit\n"
50 " -v, --version output version information and exit.\n"
51 "\n"
52 "Report bugs to <" PACKAGE_BUGREPORT ">.";
54 /********************************************************/
55 /* Function : main */
56 /* Description: */
57 /********************************************************/
58 int
59 main(int argc,char *argv[])
61 static struct option long_options[] =
63 {"help", no_argument, 0, 'h'},
64 {"version", no_argument, 0, 'v'},
65 {"check", no_argument, 0, 'c'},
66 {0, 0, 0, 0}
68 int option_index = 0;
69 int option_result;
70 char *inputFileAsnName=NULL;
71 char *inputFileDerName=NULL;
72 char *typeName=NULL;
73 int checkSyntaxOnly=0;
74 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
75 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
76 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
77 int asn1_result=ASN1_SUCCESS;
78 unsigned char *der;
79 int der_len=0;
80 /* FILE *outputFile; */
82 set_program_name (argv[0]);
84 opterr=0; /* disable error messages from getopt */
86 while(1){
88 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
90 if(option_result == -1) break;
92 switch(option_result){
93 case 'h': /* HELP */
94 printf("%s\n",help_man);
95 exit(0);
96 break;
97 case 'v': /* VERSION */
98 version_etc (stdout, program_name, PACKAGE, VERSION,
99 "Fabio Fiorina", NULL);
100 exit(0);
101 break;
102 case 'c': /* CHECK SYNTAX */
103 checkSyntaxOnly = 1;
104 break;
105 case '?': /* UNKNOW OPTION */
106 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
107 printf("%s\n",help_man);
109 exit(1);
110 break;
111 default:
112 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
116 if(optind == argc){
117 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
118 fprintf(stderr," input file with DER coding missing.\n");
119 fprintf(stderr," ASN1 type name missing.\n\n");
120 printf("%s\n",help_man);
122 exit(1);
125 if(optind == argc-1){
126 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
127 fprintf(stderr," ASN1 type name missing.\n\n");
128 printf("%s\n",help_man);
130 exit(1);
133 if(optind == argc-2){
134 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
135 printf("%s\n",help_man);
137 exit(1);
140 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
141 strcpy(inputFileAsnName,argv[optind]);
143 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
144 strcpy(inputFileDerName,argv[optind+1]);
146 typeName=(char *)malloc(strlen(argv[optind+2])+1);
147 strcpy(typeName,argv[optind+2]);
149 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
151 switch(asn1_result){
152 case ASN1_SUCCESS:
153 printf("Parse: done.\n");
154 break;
155 case ASN1_FILE_NOT_FOUND:
156 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
157 break;
158 case ASN1_SYNTAX_ERROR:
159 case ASN1_IDENTIFIER_NOT_FOUND:
160 case ASN1_NAME_TOO_LONG:
161 printf("asn1Decoding: %s\n",errorDescription);
162 break;
163 default:
164 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
167 if(asn1_result != ASN1_SUCCESS){
168 free(inputFileAsnName);
169 free(inputFileDerName);
170 free(typeName);
171 exit(1);
176 size_t tmplen;
177 der = read_binary_file (inputFileDerName, &tmplen);
178 der_len = tmplen;
181 if(der==NULL){
182 printf("asn1Decoding: could not read '%s'\n",inputFileDerName);
183 asn1_delete_structure(&definitions);
185 free(inputFileAsnName);
186 free(inputFileDerName);
187 free(typeName);
188 exit(1);
192 /*****************************************/
193 /* ONLY FOR TEST */
194 /*****************************************/
196 der_len=0;
197 outputFile=fopen("data.p12","w");
198 while(fscanf(inputFile,"%c",der+der_len) != EOF){
199 if((der_len>=0x11) && (der_len<=(0xe70)))
200 fprintf(outputFile,"%c",der[der_len]);
201 der_len++;
203 fclose(outputFile);
204 fclose(inputFile);
207 asn1_result=asn1_create_element(definitions,typeName,&structure);
209 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
212 if(asn1_result != ASN1_SUCCESS){
213 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
214 asn1_delete_structure(&definitions);
216 free(inputFileAsnName);
217 free(inputFileDerName);
218 free(typeName);
219 free(der);
220 exit(1);
223 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
224 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
225 if(asn1_result != ASN1_SUCCESS)
226 printf("asn1Decoding: %s\n",errorDescription);
228 printf("\nDECODING RESULT:\n");
229 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
231 /*****************************************/
232 /* ONLY FOR TEST */
233 /*****************************************/
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++;
244 fclose(outputFile);
247 asn1_delete_structure(&definitions);
248 asn1_delete_structure(&structure);
250 free(der);
252 free(inputFileAsnName);
253 free(inputFileDerName);
254 free(typeName);
256 if(asn1_result != ASN1_SUCCESS)
257 exit(1);
259 exit(0);