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: asn1Coding.c */
24 /* Description: program to generate a DER coding */
25 /* of an ASN1 definition. */
26 /*****************************************************/
42 char version_man
[] = "asn1Coding (GNU libtasn1) " VERSION
;
44 char help_man
[] = "asn1Coding generates a DER encoding from a file\n"
45 "with ASN1 definitions and another one with assignments.\n"
47 "Usage: asn1Coding [options] <file1> <file2>\n"
48 " <file1> file with ASN1 definitions.\n"
49 " <file2> file with assignments.\n"
51 #ifdef HAVE_GETOPT_LONG
53 " -h, --help shows this message and exit.\n"
54 " -v, --version shows version information and exit.\n"
55 " -c, --check checks the syntax only.\n"
58 " -o <file>, --output <file> output file.\n";
61 " -h shows this message and exit.\n"
62 " -v shows version information and exit.\n"
63 " -c checks the syntax only.\n"
66 " -o <file> output file.\n";
70 #define ASSIGNMENT_SUCCESS 1
71 #define ASSIGNMENT_ERROR 2
72 #define ASSIGNMENT_EOF 3
74 int readAssignment(FILE *file
,char *varName
, char *value
){
78 ret
=fscanf(file
,"%s",varName
);
79 if(ret
==EOF
) return ASSIGNMENT_EOF
;
80 if(!strcmp(varName
,"''")) varName
[0]=0;
82 ret
=fscanf(file
,"%s",value
);
83 if(ret
==EOF
) return ASSIGNMENT_ERROR
;
85 return ASSIGNMENT_SUCCESS
;
90 void createFileName(char *inputFileName
, char **outputFileName
)
92 char *char_p
,*slash_p
,*dot_p
;
94 /* searching the last '/' and '.' in inputFileAssignmentName */
96 slash_p
=inputFileName
;
97 while((char_p
=strchr(char_p
,'/'))){
103 dot_p
=inputFileName
+strlen(inputFileName
);
105 while((char_p
=strchr(char_p
,'.'))){
110 /* outputFileName= inputFileName + .out */
111 *outputFileName
=(char *)malloc(dot_p
-inputFileName
+1+
113 memcpy(*outputFileName
,inputFileName
,
114 dot_p
-inputFileName
);
115 (*outputFileName
)[dot_p
-inputFileName
]=0;
116 strcat(*outputFileName
,".out");
121 /********************************************************/
122 /* Function : main */
124 /********************************************************/
126 main(int argc
,char *argv
[])
129 #ifdef HAVE_GETOPT_LONG
130 static struct option long_options
[] =
132 {"help", no_argument
, 0, 'h'},
133 {"version", no_argument
, 0, 'v'},
134 {"check", no_argument
, 0, 'c'},
135 {"output", required_argument
, 0, 'o'},
142 char *outputFileName
=NULL
;
143 char *inputFileAsnName
=NULL
;
144 char *inputFileAssignmentName
=NULL
;
145 int checkSyntaxOnly
=0;
146 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
147 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
148 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
149 int asn1_result
=ASN1_SUCCESS
;
154 unsigned char der
[1024];
158 opterr
=0; /* disable error messages from getopt */
164 #ifdef HAVE_GETOPT_LONG
165 option_result
=getopt_long(argc
,argv
,"hvco:",long_options
,&option_index
);
167 option_result
=getopt(argc
,argv
,"hvco:");
170 if(option_result
== -1) break;
172 switch(option_result
){
174 printf("%s\n",help_man
);
176 if(outputFileName
) free(outputFileName
);
179 case 'v': /* VERSION */
180 printf("%s\n",version_man
);
182 if(outputFileName
) free(outputFileName
);
185 case 'c': /* CHECK SYNTAX */
188 case 'o': /* OUTPUT */
189 outputFileName
=(char *)malloc(strlen(optarg
)+1);
190 strcpy(outputFileName
,optarg
);
192 case '?': /* UNKNOW OPTION */
193 fprintf(stderr
,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
194 printf("%s\n",help_man
);
196 if(outputFileName
) free(outputFileName
);
200 fprintf(stderr
,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result
);
205 fprintf(stderr
,"asn1Coding: input file with ASN1 definitions missing.\n");
206 fprintf(stderr
," input file with assignments missing.\n\n");
207 printf("%s\n",help_man
);
209 if(outputFileName
) free(outputFileName
);
213 if(optind
== argc
-1){
214 fprintf(stderr
,"asn1Coding: input file with assignments missing.\n\n");
215 printf("%s\n",help_man
);
217 if(outputFileName
) free(outputFileName
);
221 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
222 strcpy(inputFileAsnName
,argv
[optind
]);
224 inputFileAssignmentName
=(char *)malloc(strlen(argv
[optind
+1])+1);
225 strcpy(inputFileAssignmentName
,argv
[optind
+1]);
227 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
231 printf("Parse: done.\n");
233 case ASN1_FILE_NOT_FOUND
:
234 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName
);
236 case ASN1_SYNTAX_ERROR
:
237 case ASN1_IDENTIFIER_NOT_FOUND
:
238 case ASN1_NAME_TOO_LONG
:
239 printf("asn1Coding: %s\n",errorDescription
);
242 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
245 if(asn1_result
!= ASN1_SUCCESS
){
246 free(inputFileAsnName
);
247 free(inputFileAssignmentName
);
252 inputFile
=fopen(inputFileAssignmentName
,"r");
255 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName
);
256 free(inputFileAsnName
);
257 free(inputFileAssignmentName
);
264 while(readAssignment(inputFile
,varName
,value
) == ASSIGNMENT_SUCCESS
){
265 printf("var=%s, value=%s\n",varName
,value
);
266 if(structure
==ASN1_TYPE_EMPTY
){
267 asn1_result
=asn1_create_element(definitions
,value
,&structure
);
270 asn1_result
=asn1_write_value(structure
,varName
,value
,0);
272 if(asn1_result
!= ASN1_SUCCESS
){
273 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
275 asn1_delete_structure(&definitions
);
276 asn1_delete_structure(&structure
);
278 free(inputFileAsnName
);
279 free(inputFileAssignmentName
);
288 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
291 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
293 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result
));
294 if(asn1_result
!=ASN1_SUCCESS
){
295 printf("asn1Coding: %s\n",errorDescription
);
297 asn1_delete_structure(&definitions
);
298 asn1_delete_structure(&structure
);
300 free(inputFileAsnName
);
301 free(inputFileAssignmentName
);
306 /* Print the 'Certificate1' DER encoding */
307 printf("-----------------\nNumber of bytes=%i\n",der_len
);
308 for(k
=0;k
<der_len
;k
++) printf("%02x ",der
[k
]);
309 printf("\n-----------------\n");
311 asn1_delete_structure(&definitions
);
312 asn1_delete_structure(&structure
);
315 if(outputFileName
==NULL
)
316 createFileName(inputFileAssignmentName
,&outputFileName
);
318 printf("\nOutputFile=%s\n",outputFileName
);
320 outputFile
=fopen(outputFileName
,"w");
322 if(outputFile
==NULL
){
323 printf("asn1Coding: output file '%s' not available\n",outputFileName
);
324 free(inputFileAsnName
);
325 free(inputFileAssignmentName
);
326 free(outputFileName
);
330 for(k
=0;k
<der_len
;k
++)
331 fprintf(outputFile
,"%c",der
[k
]);
333 printf("\nWriting: done.\n");
336 free(inputFileAsnName
);
337 free(inputFileAssignmentName
);
338 free(outputFileName
);