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 /*****************************************************/
41 #include <version-etc.h>
43 static const char help_man
[] =
44 "Usage: asn1Coding [OPTION] DEFINITIONS ASSIGNMENTS\n"
45 "asn1Coding generates a DER encoding of ASN.1 DEFINITIONS file\n"
46 "and ASSIGNMENTS file with value assignments.\n"
48 "Mandatory arguments to long options are mandatory for short options too.\n"
49 " -c, --check checks the syntax only\n"
50 " -o, --output FILE output file\n"
51 " -h, --help display this help and exit\n"
52 " -v, --version output version information and exit.\n"
54 "Report bugs to <" PACKAGE_BUGREPORT
">.";
56 #define ASSIGNMENT_SUCCESS 1
57 #define ASSIGNMENT_ERROR 2
58 #define ASSIGNMENT_EOF 3
60 int readAssignment(FILE *file
,char *varName
, char *value
){
64 ret
=fscanf(file
,"%s",varName
);
65 if(ret
==EOF
) return ASSIGNMENT_EOF
;
66 if(!strcmp(varName
,"''")) varName
[0]=0;
68 ret
=fscanf(file
,"%s",value
);
69 if(ret
==EOF
) return ASSIGNMENT_ERROR
;
71 return ASSIGNMENT_SUCCESS
;
76 void createFileName(char *inputFileName
, char **outputFileName
)
78 char *char_p
,*slash_p
,*dot_p
;
80 /* searching the last '/' and '.' in inputFileAssignmentName */
82 slash_p
=inputFileName
;
83 while((char_p
=strchr(char_p
,'/'))){
89 dot_p
=inputFileName
+strlen(inputFileName
);
91 while((char_p
=strchr(char_p
,'.'))){
96 /* outputFileName= inputFileName + .out */
97 *outputFileName
=(char *)malloc(dot_p
-inputFileName
+1+
99 memcpy(*outputFileName
,inputFileName
,
100 dot_p
-inputFileName
);
101 (*outputFileName
)[dot_p
-inputFileName
]=0;
102 strcat(*outputFileName
,".out");
107 /********************************************************/
108 /* Function : main */
110 /********************************************************/
112 main(int argc
,char *argv
[])
114 static struct option long_options
[] =
116 {"help", no_argument
, 0, 'h'},
117 {"version", no_argument
, 0, 'v'},
118 {"check", no_argument
, 0, 'c'},
119 {"output", required_argument
, 0, 'o'},
124 char *outputFileName
=NULL
;
125 char *inputFileAsnName
=NULL
;
126 char *inputFileAssignmentName
=NULL
;
127 int checkSyntaxOnly
=0;
128 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
129 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
130 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
131 int asn1_result
=ASN1_SUCCESS
;
136 unsigned char *der
= NULL
;
140 set_program_name (argv
[0]);
142 opterr
=0; /* disable error messages from getopt */
146 option_result
=getopt_long(argc
,argv
,"hvco:",long_options
,&option_index
);
148 if(option_result
== -1) break;
150 switch(option_result
){
152 printf("%s\n",help_man
);
154 if(outputFileName
) free(outputFileName
);
157 case 'v': /* VERSION */
158 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
159 "Fabio Fiorina", NULL
);
160 if(outputFileName
) free(outputFileName
);
163 case 'c': /* CHECK SYNTAX */
166 case 'o': /* OUTPUT */
167 outputFileName
=(char *)malloc(strlen(optarg
)+1);
168 strcpy(outputFileName
,optarg
);
170 case '?': /* UNKNOW OPTION */
171 fprintf(stderr
,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
172 printf("%s\n",help_man
);
174 if(outputFileName
) free(outputFileName
);
178 fprintf(stderr
,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result
);
183 fprintf(stderr
,"asn1Coding: input file with ASN1 definitions missing.\n");
184 fprintf(stderr
," input file with assignments missing.\n\n");
185 printf("%s\n",help_man
);
187 if(outputFileName
) free(outputFileName
);
191 if(optind
== argc
-1){
192 fprintf(stderr
,"asn1Coding: input file with assignments missing.\n\n");
193 printf("%s\n",help_man
);
195 if(outputFileName
) free(outputFileName
);
199 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
200 strcpy(inputFileAsnName
,argv
[optind
]);
202 inputFileAssignmentName
=(char *)malloc(strlen(argv
[optind
+1])+1);
203 strcpy(inputFileAssignmentName
,argv
[optind
+1]);
205 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
209 printf("Parse: done.\n");
211 case ASN1_FILE_NOT_FOUND
:
212 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName
);
214 case ASN1_SYNTAX_ERROR
:
215 case ASN1_IDENTIFIER_NOT_FOUND
:
216 case ASN1_NAME_TOO_LONG
:
217 printf("asn1Coding: %s\n",errorDescription
);
220 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
223 if(asn1_result
!= ASN1_SUCCESS
){
224 free(inputFileAsnName
);
225 free(inputFileAssignmentName
);
230 inputFile
=fopen(inputFileAssignmentName
,"r");
233 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName
);
234 free(inputFileAsnName
);
235 free(inputFileAssignmentName
);
242 while(readAssignment(inputFile
,varName
,value
) == ASSIGNMENT_SUCCESS
){
243 printf("var=%s, value=%s\n",varName
,value
);
244 if(structure
==ASN1_TYPE_EMPTY
){
245 asn1_result
=asn1_create_element(definitions
,value
,&structure
);
248 asn1_result
=asn1_write_value(structure
,varName
,value
,0);
250 if(asn1_result
!= ASN1_SUCCESS
){
251 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
253 asn1_delete_structure(&definitions
);
254 asn1_delete_structure(&structure
);
256 free(inputFileAsnName
);
257 free(inputFileAssignmentName
);
266 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
269 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
271 if (asn1_result
==ASN1_MEM_ERROR
)
273 der
= malloc (der_len
);
274 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
277 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result
));
278 if(asn1_result
!=ASN1_SUCCESS
){
279 printf("asn1Coding: %s\n",errorDescription
);
284 asn1_delete_structure(&definitions
);
285 asn1_delete_structure(&structure
);
287 free(inputFileAsnName
);
288 free(inputFileAssignmentName
);
293 /* Print the 'Certificate1' DER encoding */
294 printf("-----------------\nNumber of bytes=%i\n",der_len
);
295 for(k
=0;k
<der_len
;k
++) printf("%02x ",der
[k
]);
296 printf("\n-----------------\n");
298 asn1_delete_structure(&definitions
);
299 asn1_delete_structure(&structure
);
302 if(outputFileName
==NULL
)
303 createFileName(inputFileAssignmentName
,&outputFileName
);
305 printf("\nOutputFile=%s\n",outputFileName
);
307 outputFile
=fopen(outputFileName
,"w");
309 if(outputFile
==NULL
){
310 printf("asn1Coding: output file '%s' not available\n",outputFileName
);
313 free(inputFileAsnName
);
314 free(inputFileAssignmentName
);
315 free(outputFileName
);
319 for(k
=0;k
<der_len
;k
++)
320 fprintf(outputFile
,"%c",der
[k
]);
322 printf("\nWriting: done.\n");
327 free(inputFileAsnName
);
328 free(inputFileAssignmentName
);
329 free(outputFileName
);