2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBASN1.
6 * LIBASN1 is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * LIBASN1 is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
22 /*****************************************************/
23 /* File: asn1Deoding.c */
24 /* Description: program to generate an ASN1 type from*/
26 /*****************************************************/
42 char version_man
[] = "asn1Decoding (GNU libasn1) " VERSION
;
44 char help_man
[] = "asn1Decoding generates an ASN1 type from a file\n"
45 "with ASN1 definitions and another one with a DER encoding.\n"
47 "Usage: asn1Decoding [options] <file1> <file2> <type>\n"
48 " <file1> file with ASN1 definitions.\n"
49 " <file2> file with a DER coding.\n"
50 " <type> ASN1 type name\n"
52 #ifdef HAVE_GETOPT_LONG
54 " -h, --help shows this message and exit.\n"
55 " -v, --version shows version information and exit.\n"
56 " -c, --check checks the syntax only.\n";
59 " -h shows this message and exit.\n"
60 " -v shows version information and exit.\n"
61 " -c checks the syntax only.\n";
66 /********************************************************/
69 /********************************************************/
71 main(int argc
,char *argv
[])
74 #ifdef HAVE_GETOPT_LONG
75 static struct option long_options
[] =
77 {"help", no_argument
, 0, 'h'},
78 {"version", no_argument
, 0, 'v'},
79 {"check", no_argument
, 0, 'c'},
86 char *inputFileAsnName
=NULL
;
87 char *inputFileDerName
=NULL
;
89 int checkSyntaxOnly
=0;
90 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
91 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
92 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
93 int asn1_result
=ASN1_SUCCESS
;
95 unsigned char der
[100*1024];
97 /* FILE *outputFile; */
99 opterr
=0; /* disable error messages from getopt */
105 #ifdef HAVE_GETOPT_LONG
106 option_result
=getopt_long(argc
,argv
,"hvc",long_options
,&option_index
);
108 option_result
=getopt(argc
,argv
,"hvc");
111 if(option_result
== -1) break;
113 switch(option_result
){
115 printf("%s\n",help_man
);
118 case 'v': /* VERSION */
119 printf("%s\n",version_man
);
122 case 'c': /* CHECK SYNTAX */
125 case '?': /* UNKNOW OPTION */
126 fprintf(stderr
,"asn1Decoding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
127 printf("%s\n",help_man
);
132 fprintf(stderr
,"asn1Decoding: ?? getopt returned character code Ox%x ??\n",option_result
);
137 fprintf(stderr
,"asn1Decoding: input file with ASN1 definitions missing.\n");
138 fprintf(stderr
," input file with DER coding missing.\n");
139 fprintf(stderr
," ASN1 type name missing.\n\n");
140 printf("%s\n",help_man
);
145 if(optind
== argc
-1){
146 fprintf(stderr
,"asn1Decoding: input file with DER coding missing.\n\n");
147 fprintf(stderr
," ASN1 type name missing.\n\n");
148 printf("%s\n",help_man
);
153 if(optind
== argc
-2){
154 fprintf(stderr
,"asn1Decoding: ASN1 type name missing.\n\n");
155 printf("%s\n",help_man
);
160 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
161 strcpy(inputFileAsnName
,argv
[optind
]);
163 inputFileDerName
=(char *)malloc(strlen(argv
[optind
+1])+1);
164 strcpy(inputFileDerName
,argv
[optind
+1]);
166 typeName
=(char *)malloc(strlen(argv
[optind
+2])+1);
167 strcpy(typeName
,argv
[optind
+2]);
169 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
173 printf("Parse: done.\n");
175 case ASN1_FILE_NOT_FOUND
:
176 printf("asn1Decoding: FILE %s NOT FOUND\n",inputFileAsnName
);
178 case ASN1_SYNTAX_ERROR
:
179 case ASN1_IDENTIFIER_NOT_FOUND
:
180 case ASN1_NAME_TOO_LONG
:
181 printf("asn1Decoding: %s\n",errorDescription
);
184 printf("libasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
187 if(asn1_result
!= ASN1_SUCCESS
){
188 free(inputFileAsnName
);
189 free(inputFileDerName
);
195 inputFile
=fopen(inputFileDerName
,"r");
198 printf("asn1Decoding: file '%s' not found\n",inputFileDerName
);
199 asn1_delete_structure(&definitions
);
201 free(inputFileAsnName
);
202 free(inputFileDerName
);
208 /*****************************************/
210 /*****************************************/
213 outputFile=fopen("data.p12","w");
214 while(fscanf(inputFile,"%c",der+der_len) != EOF){
215 if((der_len>=0x11) && (der_len<=(0xe70)))
216 fprintf(outputFile,"%c",der[der_len]);
223 while(fscanf(inputFile
,"%c",der
+der_len
) != EOF
){
229 asn1_result
=asn1_create_element(definitions
,typeName
,&structure
);
231 /* asn1_print_structure(stdout,structure,"",ASN1_PRINT_ALL); */
234 if(asn1_result
!= ASN1_SUCCESS
){
235 printf("Structure creation: %s\n",libtasn1_strerror(asn1_result
));
236 asn1_delete_structure(&definitions
);
238 free(inputFileAsnName
);
239 free(inputFileDerName
);
244 asn1_result
=asn1_der_decoding(&structure
,der
,der_len
,errorDescription
);
245 printf("\nDecoding: %s\n",libtasn1_strerror(asn1_result
));
246 if(asn1_result
!= ASN1_SUCCESS
)
247 printf("asn1Decoding: %s\n",errorDescription
);
249 printf("\nDECODING RESULT:\n");
250 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
252 /*****************************************/
254 /*****************************************/
258 asn1_result=asn1_read_value(structure,"?2.content",der,&der_len);
259 outputFile=fopen("encryptedData.p12","w");
261 fprintf(outputFile,"%c",der[option_index]);
268 asn1_delete_structure(&definitions
);
269 asn1_delete_structure(&structure
);
271 free(inputFileAsnName
);
272 free(inputFileDerName
);
275 if(asn1_result
!= ASN1_SUCCESS
)