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*/
27 /*****************************************************/
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"
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"
52 "Report bugs to <" PACKAGE_BUGREPORT
">.";
54 /********************************************************/
57 /********************************************************/
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'},
70 char *inputFileAsnName
=NULL
;
71 char *inputFileDerName
=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
;
80 /* FILE *outputFile; */
82 set_program_name (argv
[0]);
84 opterr
=0; /* disable error messages from getopt */
88 option_result
=getopt_long(argc
,argv
,"hvc",long_options
,&option_index
);
90 if(option_result
== -1) break;
92 switch(option_result
){
94 printf("%s\n",help_man
);
97 case 'v': /* VERSION */
98 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
99 "Fabio Fiorina", NULL
);
102 case 'c': /* CHECK SYNTAX */
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
);
112 fprintf(stderr
,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result
);
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
);
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
);
133 if(optind
== argc
-2){
134 fprintf(stderr
,"asn1Decoding: ASN1 type name missing.\n\n");
135 printf("%s\n",help_man
);
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
);
153 printf("Parse: done.\n");
155 case ASN1_FILE_NOT_FOUND
:
156 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName
);
158 case ASN1_SYNTAX_ERROR
:
159 case ASN1_IDENTIFIER_NOT_FOUND
:
160 case ASN1_NAME_TOO_LONG
:
161 printf("asn1Decoding: %s\n",errorDescription
);
164 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
167 if(asn1_result
!= ASN1_SUCCESS
){
168 free(inputFileAsnName
);
169 free(inputFileDerName
);
177 der
= read_binary_file (inputFileDerName
, &tmplen
);
182 printf("asn1Decoding: could not read '%s'\n",inputFileDerName
);
183 asn1_delete_structure(&definitions
);
185 free(inputFileAsnName
);
186 free(inputFileDerName
);
192 /*****************************************/
194 /*****************************************/
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]);
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
);
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 /*****************************************/
233 /*****************************************/
237 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
238 outputFile=fopen("encryptedData.p12","w");
240 fprintf(outputFile,"%c",der[option_index]);
247 asn1_delete_structure(&definitions
);
248 asn1_delete_structure(&structure
);
252 free(inputFileAsnName
);
253 free(inputFileDerName
);
256 if(asn1_result
!= ASN1_SUCCESS
)