2 * Copyright (C) 2002 Fabio Fiorina
4 * This file is part of LIBTASN1.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * 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., 51 Franklin Street, Fifth Floor, Boston, MA
23 /*****************************************************/
24 /* File: asn1Coding.c */
25 /* Description: program to generate a DER coding */
26 /* of an ASN1 definition. */
27 /*****************************************************/
43 char version_man
[] = "asn1Coding (GNU libtasn1) " VERSION
;
45 char help_man
[] = "asn1Coding generates a DER encoding from a file\n"
46 "with ASN1 definitions and another one with assignments.\n"
48 "Usage: asn1Coding [options] <file1> <file2>\n"
49 " <file1> file with ASN1 definitions.\n"
50 " <file2> file with assignments.\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 " -o <file>, --output <file> output file.\n";
62 " -h shows this message and exit.\n"
63 " -v shows version information and exit.\n"
64 " -c checks the syntax only.\n"
67 " -o <file> output file.\n";
71 #define ASSIGNMENT_SUCCESS 1
72 #define ASSIGNMENT_ERROR 2
73 #define ASSIGNMENT_EOF 3
75 int readAssignment(FILE *file
,char *varName
, char *value
){
79 ret
=fscanf(file
,"%s",varName
);
80 if(ret
==EOF
) return ASSIGNMENT_EOF
;
81 if(!strcmp(varName
,"''")) varName
[0]=0;
83 ret
=fscanf(file
,"%s",value
);
84 if(ret
==EOF
) return ASSIGNMENT_ERROR
;
86 return ASSIGNMENT_SUCCESS
;
91 void createFileName(char *inputFileName
, char **outputFileName
)
93 char *char_p
,*slash_p
,*dot_p
;
95 /* searching the last '/' and '.' in inputFileAssignmentName */
97 slash_p
=inputFileName
;
98 while((char_p
=strchr(char_p
,'/'))){
104 dot_p
=inputFileName
+strlen(inputFileName
);
106 while((char_p
=strchr(char_p
,'.'))){
111 /* outputFileName= inputFileName + .out */
112 *outputFileName
=(char *)malloc(dot_p
-inputFileName
+1+
114 memcpy(*outputFileName
,inputFileName
,
115 dot_p
-inputFileName
);
116 (*outputFileName
)[dot_p
-inputFileName
]=0;
117 strcat(*outputFileName
,".out");
122 /********************************************************/
123 /* Function : main */
125 /********************************************************/
127 main(int argc
,char *argv
[])
130 #ifdef HAVE_GETOPT_LONG
131 static struct option long_options
[] =
133 {"help", no_argument
, 0, 'h'},
134 {"version", no_argument
, 0, 'v'},
135 {"check", no_argument
, 0, 'c'},
136 {"output", required_argument
, 0, 'o'},
143 char *outputFileName
=NULL
;
144 char *inputFileAsnName
=NULL
;
145 char *inputFileAssignmentName
=NULL
;
146 int checkSyntaxOnly
=0;
147 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
148 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
149 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
150 int asn1_result
=ASN1_SUCCESS
;
155 unsigned char der
[1024];
159 opterr
=0; /* disable error messages from getopt */
165 #ifdef HAVE_GETOPT_LONG
166 option_result
=getopt_long(argc
,argv
,"hvco:",long_options
,&option_index
);
168 option_result
=getopt(argc
,argv
,"hvco:");
171 if(option_result
== -1) break;
173 switch(option_result
){
175 printf("%s\n",help_man
);
177 if(outputFileName
) free(outputFileName
);
180 case 'v': /* VERSION */
181 printf("%s\n",version_man
);
183 if(outputFileName
) free(outputFileName
);
186 case 'c': /* CHECK SYNTAX */
189 case 'o': /* OUTPUT */
190 outputFileName
=(char *)malloc(strlen(optarg
)+1);
191 strcpy(outputFileName
,optarg
);
193 case '?': /* UNKNOW OPTION */
194 fprintf(stderr
,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
195 printf("%s\n",help_man
);
197 if(outputFileName
) free(outputFileName
);
201 fprintf(stderr
,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result
);
206 fprintf(stderr
,"asn1Coding: input file with ASN1 definitions missing.\n");
207 fprintf(stderr
," input file with assignments missing.\n\n");
208 printf("%s\n",help_man
);
210 if(outputFileName
) free(outputFileName
);
214 if(optind
== argc
-1){
215 fprintf(stderr
,"asn1Coding: input file with assignments missing.\n\n");
216 printf("%s\n",help_man
);
218 if(outputFileName
) free(outputFileName
);
222 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
223 strcpy(inputFileAsnName
,argv
[optind
]);
225 inputFileAssignmentName
=(char *)malloc(strlen(argv
[optind
+1])+1);
226 strcpy(inputFileAssignmentName
,argv
[optind
+1]);
228 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
232 printf("Parse: done.\n");
234 case ASN1_FILE_NOT_FOUND
:
235 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName
);
237 case ASN1_SYNTAX_ERROR
:
238 case ASN1_IDENTIFIER_NOT_FOUND
:
239 case ASN1_NAME_TOO_LONG
:
240 printf("asn1Coding: %s\n",errorDescription
);
243 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
246 if(asn1_result
!= ASN1_SUCCESS
){
247 free(inputFileAsnName
);
248 free(inputFileAssignmentName
);
253 inputFile
=fopen(inputFileAssignmentName
,"r");
256 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName
);
257 free(inputFileAsnName
);
258 free(inputFileAssignmentName
);
265 while(readAssignment(inputFile
,varName
,value
) == ASSIGNMENT_SUCCESS
){
266 printf("var=%s, value=%s\n",varName
,value
);
267 if(structure
==ASN1_TYPE_EMPTY
){
268 asn1_result
=asn1_create_element(definitions
,value
,&structure
);
271 asn1_result
=asn1_write_value(structure
,varName
,value
,0);
273 if(asn1_result
!= ASN1_SUCCESS
){
274 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
276 asn1_delete_structure(&definitions
);
277 asn1_delete_structure(&structure
);
279 free(inputFileAsnName
);
280 free(inputFileAssignmentName
);
289 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
292 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
294 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result
));
295 if(asn1_result
!=ASN1_SUCCESS
){
296 printf("asn1Coding: %s\n",errorDescription
);
298 asn1_delete_structure(&definitions
);
299 asn1_delete_structure(&structure
);
301 free(inputFileAsnName
);
302 free(inputFileAssignmentName
);
307 /* Print the 'Certificate1' DER encoding */
308 printf("-----------------\nNumber of bytes=%i\n",der_len
);
309 for(k
=0;k
<der_len
;k
++) printf("%02x ",der
[k
]);
310 printf("\n-----------------\n");
312 asn1_delete_structure(&definitions
);
313 asn1_delete_structure(&structure
);
316 if(outputFileName
==NULL
)
317 createFileName(inputFileAssignmentName
,&outputFileName
);
319 printf("\nOutputFile=%s\n",outputFileName
);
321 outputFile
=fopen(outputFileName
,"w");
323 if(outputFile
==NULL
){
324 printf("asn1Coding: output file '%s' not available\n",outputFileName
);
325 free(inputFileAsnName
);
326 free(inputFileAssignmentName
);
327 free(outputFileName
);
331 for(k
=0;k
<der_len
;k
++)
332 fprintf(outputFile
,"%c",der
[k
]);
334 printf("\nWriting: done.\n");
337 free(inputFileAsnName
);
338 free(inputFileAssignmentName
);
339 free(outputFileName
);