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 /*****************************************************/
44 char version_man
[] = "asn1Decoding (GNU libtasn1) " VERSION
;
46 char help_man
[] = "asn1Decoding generates an ASN1 type from a file\n"
47 "with ASN1 definitions and another one with a DER encoding.\n"
49 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
50 " <file1> file with ASN1 definitions.\n"
51 " <file2> file with a DER coding.\n"
52 " <type> ASN1 type name\n"
54 #ifdef HAVE_GETOPT_LONG
56 " -h, --help shows this message and exit.\n"
57 " -v, --version shows version information and exit.\n"
58 " -c, --check checks the syntax only.\n";
61 " -h shows this message and exit.\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n";
68 /********************************************************/
71 /********************************************************/
73 main(int argc
,char *argv
[])
76 #ifdef HAVE_GETOPT_LONG
77 static struct option long_options
[] =
79 {"help", no_argument
, 0, 'h'},
80 {"version", no_argument
, 0, 'v'},
81 {"check", no_argument
, 0, 'c'},
88 char *inputFileAsnName
=NULL
;
89 char *inputFileDerName
=NULL
;
91 int checkSyntaxOnly
=0;
92 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
93 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
94 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
95 int asn1_result
=ASN1_SUCCESS
;
97 unsigned char der
[100*1024];
99 /* FILE *outputFile; */
101 opterr
=0; /* disable error messages from getopt */
107 #ifdef HAVE_GETOPT_LONG
108 option_result
=getopt_long(argc
,argv
,"hvc",long_options
,&option_index
);
110 option_result
=getopt(argc
,argv
,"hvc");
113 if(option_result
== -1) break;
115 switch(option_result
){
117 printf("%s\n",help_man
);
120 case 'v': /* VERSION */
121 printf("%s\n",version_man
);
124 case 'c': /* CHECK SYNTAX */
127 case '?': /* UNKNOW OPTION */
128 fprintf(stderr
,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
129 printf("%s\n",help_man
);
134 fprintf(stderr
,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result
);
139 fprintf(stderr
,"asn1Decoding: input file with ASN1 definitions missing.\n");
140 fprintf(stderr
," input file with DER coding missing.\n");
141 fprintf(stderr
," ASN1 type name missing.\n\n");
142 printf("%s\n",help_man
);
147 if(optind
== argc
-1){
148 fprintf(stderr
,"asn1Decoding: input file with DER coding missing.\n\n");
149 fprintf(stderr
," ASN1 type name missing.\n\n");
150 printf("%s\n",help_man
);
155 if(optind
== argc
-2){
156 fprintf(stderr
,"asn1Decoding: ASN1 type name missing.\n\n");
157 printf("%s\n",help_man
);
162 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
163 strcpy(inputFileAsnName
,argv
[optind
]);
165 inputFileDerName
=(char *)malloc(strlen(argv
[optind
+1])+1);
166 strcpy(inputFileDerName
,argv
[optind
+1]);
168 typeName
=(char *)malloc(strlen(argv
[optind
+2])+1);
169 strcpy(typeName
,argv
[optind
+2]);
171 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
175 printf("Parse: done.\n");
177 case ASN1_FILE_NOT_FOUND
:
178 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName
);
180 case ASN1_SYNTAX_ERROR
:
181 case ASN1_IDENTIFIER_NOT_FOUND
:
182 case ASN1_NAME_TOO_LONG
:
183 printf("asn1Decoding: %s\n",errorDescription
);
186 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
189 if(asn1_result
!= ASN1_SUCCESS
){
190 free(inputFileAsnName
);
191 free(inputFileDerName
);
197 inputFile
=fopen(inputFileDerName
,"r");
200 printf("asn1Decoding: file '%s' not found\n",inputFileDerName
);
201 asn1_delete_structure(&definitions
);
203 free(inputFileAsnName
);
204 free(inputFileDerName
);
210 /*****************************************/
212 /*****************************************/
215 outputFile=fopen("data.p12","w");
216 while(fscanf(inputFile,"%c",der+der_len) != EOF){
217 if((der_len>=0x11) && (der_len<=(0xe70)))
218 fprintf(outputFile,"%c",der[der_len]);
225 while(fscanf(inputFile
,"%c",der
+der_len
) != EOF
){
231 asn1_result
=asn1_create_element(definitions
,typeName
,&structure
);
233 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
236 if(asn1_result
!= ASN1_SUCCESS
){
237 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result
));
238 asn1_delete_structure(&definitions
);
240 free(inputFileAsnName
);
241 free(inputFileDerName
);
246 asn1_result
=asn1_der_decoding(&structure
,der
,der_len
,errorDescription
);
247 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result
));
248 if(asn1_result
!= ASN1_SUCCESS
)
249 printf("asn1Decoding: %s\n",errorDescription
);
251 printf("\nDECODING RESULT:\n");
252 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
254 /*****************************************/
256 /*****************************************/
260 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
261 outputFile=fopen("encryptedData.p12","w");
263 fprintf(outputFile,"%c",der[option_index]);
270 asn1_delete_structure(&definitions
);
271 asn1_delete_structure(&structure
);
273 free(inputFileAsnName
);
274 free(inputFileDerName
);
277 if(asn1_result
!= ASN1_SUCCESS
)