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 /*****************************************************/
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"
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"
53 "Report bugs to <" PACKAGE_BUGREPORT
">.";
55 /********************************************************/
58 /********************************************************/
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'},
71 char *inputFileAsnName
=NULL
;
72 char *inputFileDerName
=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
;
81 /* FILE *outputFile; */
83 set_program_name (argv
[0]);
85 opterr
=0; /* disable error messages from getopt */
89 option_result
=getopt_long(argc
,argv
,"hvc",long_options
,&option_index
);
91 if(option_result
== -1) break;
93 switch(option_result
){
95 printf("%s\n",help_man
);
98 case 'v': /* VERSION */
99 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
100 "Fabio Fiorina", NULL
);
103 case 'c': /* CHECK SYNTAX */
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
);
113 fprintf(stderr
,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result
);
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
);
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
);
134 if(optind
== argc
-2){
135 fprintf(stderr
,"asn1Decoding: ASN1 type name missing.\n\n");
136 printf("%s\n",help_man
);
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
);
154 printf("Parse: done.\n");
156 case ASN1_FILE_NOT_FOUND
:
157 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName
);
159 case ASN1_SYNTAX_ERROR
:
160 case ASN1_IDENTIFIER_NOT_FOUND
:
161 case ASN1_NAME_TOO_LONG
:
162 printf("asn1Decoding: %s\n",errorDescription
);
165 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
168 if(asn1_result
!= ASN1_SUCCESS
){
169 free(inputFileAsnName
);
170 free(inputFileDerName
);
178 der
= read_binary_file (inputFileDerName
, &tmplen
);
183 printf("asn1Decoding: could not read '%s'\n",inputFileDerName
);
184 asn1_delete_structure(&definitions
);
186 free(inputFileAsnName
);
187 free(inputFileDerName
);
193 /*****************************************/
195 /*****************************************/
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]);
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
);
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 /*****************************************/
234 /*****************************************/
238 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
239 outputFile=fopen("encryptedData.p12","w");
241 fprintf(outputFile,"%c",der[option_index]);
248 asn1_delete_structure(&definitions
);
249 asn1_delete_structure(&structure
);
253 free(inputFileAsnName
);
254 free(inputFileDerName
);
257 if(asn1_result
!= ASN1_SUCCESS
)