2 * Copyright (C) 2006, 2007 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 modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU 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, see <http://www.gnu.org/licenses/>.
23 /*****************************************************/
24 /* File: asn1Coding.c */
25 /* Description: program to generate a DER coding */
26 /* of an ASN1 definition. */
27 /*****************************************************/
40 #include <version-etc.h>
42 static const char help_man
[] =
43 "Usage: asn1Coding [OPTION] DEFINITIONS ASSIGNMENTS\n"
44 "asn1Coding generates a DER encoding of ASN.1 DEFINITIONS file\n"
45 "and ASSIGNMENTS file with value assignments.\n"
47 "Mandatory arguments to long options are mandatory for short options too.\n"
48 " -c, --check checks the syntax only\n"
49 " -o, --output FILE output file\n"
50 " -h, --help display this help and exit\n"
51 " -v, --version output version information and exit.\n"
53 "Report bugs to <" PACKAGE_BUGREPORT
">.";
55 #define ASSIGNMENT_SUCCESS 1
56 #define ASSIGNMENT_ERROR 2
57 #define ASSIGNMENT_EOF 3
59 int readAssignment(FILE *file
,char *varName
, char *value
){
63 ret
=fscanf(file
,"%s",varName
);
64 if(ret
==EOF
) return ASSIGNMENT_EOF
;
65 if(!strcmp(varName
,"''")) varName
[0]=0;
67 ret
=fscanf(file
,"%s",value
);
68 if(ret
==EOF
) return ASSIGNMENT_ERROR
;
70 return ASSIGNMENT_SUCCESS
;
75 void createFileName(char *inputFileName
, char **outputFileName
)
77 char *char_p
,*slash_p
,*dot_p
;
79 /* searching the last '/' and '.' in inputFileAssignmentName */
81 slash_p
=inputFileName
;
82 while((char_p
=strchr(char_p
,'/'))){
88 dot_p
=inputFileName
+strlen(inputFileName
);
90 while((char_p
=strchr(char_p
,'.'))){
95 /* outputFileName= inputFileName + .out */
96 *outputFileName
=(char *)malloc(dot_p
-inputFileName
+1+
98 memcpy(*outputFileName
,inputFileName
,
100 (*outputFileName
)[dot_p
-inputFileName
]=0;
101 strcat(*outputFileName
,".out");
106 /********************************************************/
107 /* Function : main */
109 /********************************************************/
111 main(int argc
,char *argv
[])
113 static struct option long_options
[] =
115 {"help", no_argument
, 0, 'h'},
116 {"version", no_argument
, 0, 'v'},
117 {"check", no_argument
, 0, 'c'},
118 {"output", required_argument
, 0, 'o'},
123 char *outputFileName
=NULL
;
124 char *inputFileAsnName
=NULL
;
125 char *inputFileAssignmentName
=NULL
;
126 int checkSyntaxOnly
=0;
127 ASN1_TYPE definitions
=ASN1_TYPE_EMPTY
;
128 ASN1_TYPE structure
=ASN1_TYPE_EMPTY
;
129 char errorDescription
[MAX_ERROR_DESCRIPTION_SIZE
];
130 int asn1_result
=ASN1_SUCCESS
;
135 unsigned char *der
= NULL
;
139 set_program_name (argv
[0]);
141 opterr
=0; /* disable error messages from getopt */
145 option_result
=getopt_long(argc
,argv
,"hvco:",long_options
,&option_index
);
147 if(option_result
== -1) break;
149 switch(option_result
){
151 printf("%s\n",help_man
);
153 if(outputFileName
) free(outputFileName
);
156 case 'v': /* VERSION */
157 version_etc (stdout
, program_name
, PACKAGE
, VERSION
,
158 "Fabio Fiorina", NULL
);
159 if(outputFileName
) free(outputFileName
);
162 case 'c': /* CHECK SYNTAX */
165 case 'o': /* OUTPUT */
166 outputFileName
=(char *)malloc(strlen(optarg
)+1);
167 strcpy(outputFileName
,optarg
);
169 case '?': /* UNKNOW OPTION */
170 fprintf(stderr
,"asn1Coding: option '%s' not recognized or without argument.\n\n",argv
[optind
-1]);
171 printf("%s\n",help_man
);
173 if(outputFileName
) free(outputFileName
);
177 fprintf(stderr
,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result
);
182 fprintf(stderr
,"asn1Coding: input file with ASN1 definitions missing.\n");
183 fprintf(stderr
," input file with assignments missing.\n\n");
184 printf("%s\n",help_man
);
186 if(outputFileName
) free(outputFileName
);
190 if(optind
== argc
-1){
191 fprintf(stderr
,"asn1Coding: input file with assignments missing.\n\n");
192 printf("%s\n",help_man
);
194 if(outputFileName
) free(outputFileName
);
198 inputFileAsnName
=(char *)malloc(strlen(argv
[optind
])+1);
199 strcpy(inputFileAsnName
,argv
[optind
]);
201 inputFileAssignmentName
=(char *)malloc(strlen(argv
[optind
+1])+1);
202 strcpy(inputFileAssignmentName
,argv
[optind
+1]);
204 asn1_result
=asn1_parser2tree(inputFileAsnName
,&definitions
,errorDescription
);
208 printf("Parse: done.\n");
210 case ASN1_FILE_NOT_FOUND
:
211 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName
);
213 case ASN1_SYNTAX_ERROR
:
214 case ASN1_IDENTIFIER_NOT_FOUND
:
215 case ASN1_NAME_TOO_LONG
:
216 printf("asn1Coding: %s\n",errorDescription
);
219 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
222 if(asn1_result
!= ASN1_SUCCESS
){
223 free(inputFileAsnName
);
224 free(inputFileAssignmentName
);
229 inputFile
=fopen(inputFileAssignmentName
,"r");
232 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName
);
233 free(inputFileAsnName
);
234 free(inputFileAssignmentName
);
241 while(readAssignment(inputFile
,varName
,value
) == ASSIGNMENT_SUCCESS
){
242 printf("var=%s, value=%s\n",varName
,value
);
243 if(structure
==ASN1_TYPE_EMPTY
){
244 asn1_result
=asn1_create_element(definitions
,value
,&structure
);
247 asn1_result
=asn1_write_value(structure
,varName
,value
,0);
249 if(asn1_result
!= ASN1_SUCCESS
){
250 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result
));
252 asn1_delete_structure(&definitions
);
253 asn1_delete_structure(&structure
);
255 free(inputFileAsnName
);
256 free(inputFileAssignmentName
);
265 asn1_print_structure(stdout
,structure
,"",ASN1_PRINT_NAME_TYPE_VALUE
);
268 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
270 if (asn1_result
==ASN1_MEM_ERROR
)
272 der
= malloc (der_len
);
273 asn1_result
=asn1_der_coding(structure
,"",der
,&der_len
,
276 printf("\nCoding: %s\n\n",libtasn1_strerror(asn1_result
));
277 if(asn1_result
!=ASN1_SUCCESS
){
278 printf("asn1Coding: %s\n",errorDescription
);
283 asn1_delete_structure(&definitions
);
284 asn1_delete_structure(&structure
);
286 free(inputFileAsnName
);
287 free(inputFileAssignmentName
);
292 /* Print the 'Certificate1' DER encoding */
293 printf("-----------------\nNumber of bytes=%i\n",der_len
);
294 for(k
=0;k
<der_len
;k
++) printf("%02x ",der
[k
]);
295 printf("\n-----------------\n");
297 asn1_delete_structure(&definitions
);
298 asn1_delete_structure(&structure
);
301 if(outputFileName
==NULL
)
302 createFileName(inputFileAssignmentName
,&outputFileName
);
304 printf("\nOutputFile=%s\n",outputFileName
);
306 outputFile
=fopen(outputFileName
,"w");
308 if(outputFile
==NULL
){
309 printf("asn1Coding: output file '%s' not available\n",outputFileName
);
312 free(inputFileAsnName
);
313 free(inputFileAssignmentName
);
314 free(outputFileName
);
318 for(k
=0;k
<der_len
;k
++)
319 fprintf(outputFile
,"%c",der
[k
]);
321 printf("\nWriting: done.\n");
326 free(inputFileAsnName
);
327 free(inputFileAssignmentName
);
328 free(outputFileName
);