Fix release.
[libtasn1.git] / src / asn1Decoding.c
blobebc26ce67b78ba00451fd934e3e6358bd2f237c9
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 <stdlib.h>
35 #include <unistd.h>
36 #include <getopt.h>
38 #include <libtasn1.h>
40 #include <progname.h>
41 #include <version-etc.h>
42 #include <read-file.h>
44 static const char help_man[] =
45 "Usage: asn1Decoding [OPTION] DEFINITIONS ENCODED ASN1TYPE\n"
46 "asn1Decoding decodes DER data in ENCODED file, for the ASN1TYPE element\n"
47 "described in ASN.1 DEFINITIONS file, and print decoded structures.\n"
48 "\n"
49 " -c, --check checks the syntax only\n"
50 " -h, --help display this help and exit\n"
51 " -v, --version output version information and exit.\n"
52 "\n"
53 "Report bugs to <" PACKAGE_BUGREPORT ">.";
55 /********************************************************/
56 /* Function : main */
57 /* Description: */
58 /********************************************************/
59 int
60 main(int argc,char *argv[])
62 static struct option long_options[] =
64 {"help", no_argument, 0, 'h'},
65 {"version", no_argument, 0, 'v'},
66 {"check", no_argument, 0, 'c'},
67 {0, 0, 0, 0}
69 int option_index = 0;
70 int option_result;
71 char *inputFileAsnName=NULL;
72 char *inputFileDerName=NULL;
73 char *typeName=NULL;
74 int checkSyntaxOnly=0;
75 ASN1_TYPE definitions=ASN1_TYPE_EMPTY;
76 ASN1_TYPE structure=ASN1_TYPE_EMPTY;
77 char errorDescription[MAX_ERROR_DESCRIPTION_SIZE];
78 int asn1_result=ASN1_SUCCESS;
79 unsigned char *der;
80 int der_len=0;
81 /* FILE *outputFile; */
83 set_program_name (argv[0]);
85 opterr=0; /* disable error messages from getopt */
87 while(1){
89 option_result=getopt_long(argc,argv,"hvc",long_options,&option_index);
91 if(option_result == -1) break;
93 switch(option_result){
94 case 'h': /* HELP */
95 printf("%s\n",help_man);
96 exit(0);
97 break;
98 case 'v': /* VERSION */
99 version_etc (stdout, program_name, PACKAGE, VERSION,
100 "Fabio Fiorina", NULL);
101 exit(0);
102 break;
103 case 'c': /* CHECK SYNTAX */
104 checkSyntaxOnly = 1;
105 break;
106 case '?': /* UNKNOW OPTION */
107 fprintf(stderr,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv[optind-1]);
108 printf("%s\n",help_man);
110 exit(1);
111 break;
112 default:
113 fprintf(stderr,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result);
117 if(optind == argc){
118 fprintf(stderr,"asn1Decoding: input file with ASN1 definitions missing.\n");
119 fprintf(stderr," input file with DER coding missing.\n");
120 fprintf(stderr," ASN1 type name missing.\n\n");
121 printf("%s\n",help_man);
123 exit(1);
126 if(optind == argc-1){
127 fprintf(stderr,"asn1Decoding: input file with DER coding missing.\n\n");
128 fprintf(stderr," ASN1 type name missing.\n\n");
129 printf("%s\n",help_man);
131 exit(1);
134 if(optind == argc-2){
135 fprintf(stderr,"asn1Decoding: ASN1 type name missing.\n\n");
136 printf("%s\n",help_man);
138 exit(1);
141 inputFileAsnName=(char *)malloc(strlen(argv[optind])+1);
142 strcpy(inputFileAsnName,argv[optind]);
144 inputFileDerName=(char *)malloc(strlen(argv[optind+1])+1);
145 strcpy(inputFileDerName,argv[optind+1]);
147 typeName=(char *)malloc(strlen(argv[optind+2])+1);
148 strcpy(typeName,argv[optind+2]);
150 asn1_result=asn1_parser2tree(inputFileAsnName,&definitions,errorDescription);
152 switch(asn1_result){
153 case ASN1_SUCCESS:
154 printf("Parse: done.\n");
155 break;
156 case ASN1_FILE_NOT_FOUND:
157 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName);
158 break;
159 case ASN1_SYNTAX_ERROR:
160 case ASN1_IDENTIFIER_NOT_FOUND:
161 case ASN1_NAME_TOO_LONG:
162 printf("asn1Decoding: %s\n",errorDescription);
163 break;
164 default:
165 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
168 if(asn1_result != ASN1_SUCCESS){
169 free(inputFileAsnName);
170 free(inputFileDerName);
171 free(typeName);
172 exit(1);
177 size_t tmplen;
178 der = read_binary_file (inputFileDerName, &tmplen);
179 der_len = tmplen;
182 if(der==NULL){
183 printf("asn1Decoding: could not read '%s'\n",inputFileDerName);
184 asn1_delete_structure(&definitions);
186 free(inputFileAsnName);
187 free(inputFileDerName);
188 free(typeName);
189 exit(1);
193 /*****************************************/
194 /* ONLY FOR TEST */
195 /*****************************************/
197 der_len=0;
198 outputFile=fopen("data.p12","w");
199 while(fscanf(inputFile,"%c",der+der_len) != EOF){
200 if((der_len>=0x11) && (der_len<=(0xe70)))
201 fprintf(outputFile,"%c",der[der_len]);
202 der_len++;
204 fclose(outputFile);
205 fclose(inputFile);
208 asn1_result=asn1_create_element(definitions,typeName,&structure);
210 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
213 if(asn1_result != ASN1_SUCCESS){
214 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result));
215 asn1_delete_structure(&definitions);
217 free(inputFileAsnName);
218 free(inputFileDerName);
219 free(typeName);
220 free(der);
221 exit(1);
224 asn1_result=asn1_der_decoding(&structure,der,der_len,errorDescription);
225 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result));
226 if(asn1_result != ASN1_SUCCESS)
227 printf("asn1Decoding: %s\n",errorDescription);
229 printf("\nDECODING RESULT:\n");
230 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
232 /*****************************************/
233 /* ONLY FOR TEST */
234 /*****************************************/
236 der_len=10000;
237 option_index=0;
238 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
239 outputFile=fopen("encryptedData.p12","w");
240 while(der_len>0){
241 fprintf(outputFile,"%c",der[option_index]);
242 der_len--;
243 option_index++;
245 fclose(outputFile);
248 asn1_delete_structure(&definitions);
249 asn1_delete_structure(&structure);
251 free(der);
253 free(inputFileAsnName);
254 free(inputFileDerName);
255 free(typeName);
257 if(asn1_result != ASN1_SUCCESS)
258 exit(1);
260 exit(0);