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
24 /*****************************************************/
25 /* File: asn1Deoding.c */
26 /* Description: program to generate an ASN1 type from*/
28 /*****************************************************/
42 char version_man
[] = "asn1Decoding (GNU libtasn1) " VERSION
"\n"
43 "Written by Fabio Fiorina."
45 "Copyright (C) 2002, 2006 Free Software Foundation, Inc.\n";
48 "asn1Decoding generates an ASN1 type from FILE1\n"
49 "with ASN1 definitions and FILE2 with a DER encoding.\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"
56 #ifdef HAVE_GETOPT_LONG
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"
63 " -h shows this message and exit.\n"
64 " -v shows version information and exit.\n"
65 " -c checks the syntax only.\n"
67 "Report bugs to <" PACKAGE_BUGREPORT
">.";
69 /********************************************************/
72 /********************************************************/
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'},
89 char *inputFileAsnName
=NULL
;
90 char *inputFileDerName
=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
;
98 unsigned char der
[100*1024];
100 /* FILE *outputFile; */
102 opterr
=0; /* disable error messages from getopt */
108 #ifdef HAVE_GETOPT_LONG
109 option_result
=getopt_long(argc
,argv
,"hvc",long_options
,&option_index
);
111 option_result
=getopt(argc
,argv
,"hvc");
114 if(option_result
== -1) break;
116 switch(option_result
){
118 printf("%s\n",help_man
);
121 case 'v': /* VERSION */
122 printf("%s\n",version_man
);
125 case 'c': /* CHECK SYNTAX */
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
);
135 fprintf(stderr
,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result
);
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
);
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
);
156 if(optind
== argc
-2){
157 fprintf(stderr
,"asn1Decoding: ASN1 type name missing.\n\n");
158 printf("%s\n",help_man
);
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
);
176 printf("Parse: done.\n");
178 case ASN1_FILE_NOT_FOUND
:
179 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName
);
181 case ASN1_SYNTAX_ERROR
:
182 case ASN1_IDENTIFIER_NOT_FOUND
:
183 case ASN1_NAME_TOO_LONG
:
184 printf("asn1Decoding: %s\n",errorDescription
);
187 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
190 if(asn1_result
!= ASN1_SUCCESS
){
191 free(inputFileAsnName
);
192 free(inputFileDerName
);
198 inputFile
=fopen(inputFileDerName
,"r");
201 printf("asn1Decoding: file '%s' not found\n",inputFileDerName
);
202 asn1_delete_structure(&definitions
);
204 free(inputFileAsnName
);
205 free(inputFileDerName
);
211 /*****************************************/
213 /*****************************************/
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]);
226 while(fscanf(inputFile
,"%c",der
+der_len
) != EOF
){
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
);
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 /*****************************************/
257 /*****************************************/
261 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
262 outputFile=fopen("encryptedData.p12","w");
264 fprintf(outputFile,"%c",der[option_index]);
271 asn1_delete_structure(&definitions
);
272 asn1_delete_structure(&structure
);
274 free(inputFileAsnName
);
275 free(inputFileDerName
);
278 if(asn1_result
!= ASN1_SUCCESS
)