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: asn1Coding.c */
26 /* Description: program to generate a DER coding */
27 /* of an ASN1 definition. */
28 /*****************************************************/
42 char version_man
[] = "asn1Coding (GNU libtasn1) " VERSION
"\n"
43 "Written by Fabio Fiorina."
45 "Copyright (C) 2002, 2006 Free Software Foundation, Inc.\n";
48 "Usage: asn1Coding [options] <file1> <file2>\n"
50 "asn1Coding generates a DER encoding from FILE1\n"
51 "with ASN1 definitions and FILE2 with assignments.\n"
53 #ifdef HAVE_GETOPT_LONG
55 " -h, --help shows this message and exit.\n"
56 " -v, --version shows version information and exit.\n"
57 " -c, --check checks the syntax only.\n"
60 " -o <file>, --output <file> output file.\n"
63 " -h shows this message and exit.\n"
64 " -v shows version information and exit.\n"
65 " -c checks the syntax only.\n"
68 " -o <file> output file.\n"
70 "Report bugs to <" PACKAGE_BUGREPORT
">.";
72 #define ASSIGNMENT_SUCCESS 1
73 #define ASSIGNMENT_ERROR 2
74 #define ASSIGNMENT_EOF 3
76 int readAssignment(FILE *file
,char *varName
, char *value
){
80 ret
=fscanf(file
,"%s",varName
);
81 if(ret
==EOF
) return ASSIGNMENT_EOF
;
82 if(!strcmp(varName
,"''")) varName
[0]=0;
84 ret
=fscanf(file
,"%s",value
);
85 if(ret
==EOF
) return ASSIGNMENT_ERROR
;
87 return ASSIGNMENT_SUCCESS
;
92 void createFileName(char *inputFileName
, char **outputFileName
)
94 char *char_p
,*slash_p
,*dot_p
;
96 /* searching the last '/' and '.' in inputFileAssignmentName */
98 slash_p
=inputFileName
;
99 while((char_p
=strchr(char_p
,'/'))){
105 dot_p
=inputFileName
+strlen(inputFileName
);
107 while((char_p
=strchr(char_p
,'.'))){
112 /* outputFileName= inputFileName + .out */
113 *outputFileName
=(char *)malloc(dot_p
-inputFileName
+1+
115 memcpy(*outputFileName
,inputFileName
,
116 dot_p
-inputFileName
);
117 (*outputFileName
)[dot_p
-inputFileName
]=0;
118 strcat(*outputFileName
,".out");
123 /********************************************************/
124 /* Function : main */
126 /********************************************************/
128 main(int argc
,char *argv
[])
131 #ifdef HAVE_GETOPT_LONG
132 static struct option long_options
[] =
134 {"help", no_argument
, 0, 'h'},
135 {"version", no_argument
, 0, 'v'},
136 {"check", no_argument
, 0, 'c'},
137 {"output", required_argument
, 0, 'o'},
144 char *outputFileName
=NULL
;
145 char *inputFileAsnName
=NULL
;
146 char *inputFileAssignmentName
=NULL
;
147 int checkSyntaxOnly
=0;
148 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
149 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
150 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
151 int asn1_result
=ASN1_SUCCESS
;
156 unsigned char der
[1024];
160 opterr
=0; /* disable error messages from getopt */
166 #ifdef HAVE_GETOPT_LONG
167 option_result
=getopt_long(argc
,argv
,"hvco:",long_options
,&option_index
);
169 option_result
=getopt(argc
,argv
,"hvco:");
172 if(option_result
== -1) break;
174 switch(option_result
){
176 printf("%s\n",help_man
);
178 if(outputFileName
) free(outputFileName
);
181 case 'v': /* VERSION */
182 printf("%s\n",version_man
);
184 if(outputFileName
) free(outputFileName
);
187 case 'c': /* CHECK SYNTAX */
190 case 'o': /* OUTPUT */
191 outputFileName
=(char *)malloc(strlen(optarg
)+1);
192 strcpy(outputFileName
,optarg
);
194 case '?': /* UNKNOW OPTION */
195 fprintf(stderr
,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
196 printf("%s\n",help_man
);
198 if(outputFileName
) free(outputFileName
);
202 fprintf(stderr
,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result
);
207 fprintf(stderr
,"asn1Coding: input file with ASN1 definitions missing.\n");
208 fprintf(stderr
," input file with assignments missing.\n\n");
209 printf("%s\n",help_man
);
211 if(outputFileName
) free(outputFileName
);
215 if(optind
== argc
-1){
216 fprintf(stderr
,"asn1Coding: input file with assignments missing.\n\n");
217 printf("%s\n",help_man
);
219 if(outputFileName
) free(outputFileName
);
223 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
224 strcpy(inputFileAsnName
,argv
[optind
]);
226 inputFileAssignmentName
=(char *)malloc(strlen(argv
[optind
+1])+1);
227 strcpy(inputFileAssignmentName
,argv
[optind
+1]);
229 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
233 printf("Parse: done.\n");
235 case ASN1_FILE_NOT_FOUND
:
236 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName
);
238 case ASN1_SYNTAX_ERROR
:
239 case ASN1_IDENTIFIER_NOT_FOUND
:
240 case ASN1_NAME_TOO_LONG
:
241 printf("asn1Coding: %s\n",errorDescription
);
244 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
247 if(asn1_result
!= ASN1_SUCCESS
){
248 free(inputFileAsnName
);
249 free(inputFileAssignmentName
);
254 inputFile
=fopen(inputFileAssignmentName
,"r");
257 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName
);
258 free(inputFileAsnName
);
259 free(inputFileAssignmentName
);
266 while(readAssignment(inputFile
,varName
,value
) == ASSIGNMENT_SUCCESS
){
267 printf("var=%s, value=%s\n",varName
,value
);
268 if(structure
==ASN1_TYPE_EMPTY
){
269 asn1_result
=asn1_create_element(definitions
,value
,&structure
);
272 asn1_result
=asn1_write_value(structure
,varName
,value
,0);
274 if(asn1_result
!= ASN1_SUCCESS
){
275 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
277 asn1_delete_structure(&definitions
);
278 asn1_delete_structure(&structure
);
280 free(inputFileAsnName
);
281 free(inputFileAssignmentName
);
290 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
293 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
295 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result
));
296 if(asn1_result
!=ASN1_SUCCESS
){
297 printf("asn1Coding: %s\n",errorDescription
);
299 asn1_delete_structure(&definitions
);
300 asn1_delete_structure(&structure
);
302 free(inputFileAsnName
);
303 free(inputFileAssignmentName
);
308 /* Print the 'Certificate1' DER encoding */
309 printf("-----------------\nNumber of bytes=%i\n",der_len
);
310 for(k
=0;k
<der_len
;k
++) printf("%02x ",der
[k
]);
311 printf("\n-----------------\n");
313 asn1_delete_structure(&definitions
);
314 asn1_delete_structure(&structure
);
317 if(outputFileName
==NULL
)
318 createFileName(inputFileAssignmentName
,&outputFileName
);
320 printf("\nOutputFile=%s\n",outputFileName
);
322 outputFile
=fopen(outputFileName
,"w");
324 if(outputFile
==NULL
){
325 printf("asn1Coding: output file '%s' not available\n",outputFileName
);
326 free(inputFileAsnName
);
327 free(inputFileAssignmentName
);
328 free(outputFileName
);
332 for(k
=0;k
<der_len
;k
++)
333 fprintf(outputFile
,"%c",der
[k
]);
335 printf("\nWriting: done.\n");
338 free(inputFileAsnName
);
339 free(inputFileAssignmentName
);
340 free(outputFileName
);