*** empty log message ***
[libtasn1.git] / src / asn1Coding.c
blob5b004c61ebb3711aa83ecf4963a04cf33898d712
1 /*
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
20 * 02110-1301, USA
24 /*****************************************************/
25 /* File: asn1Coding.c */
26 /* Description: program to generate a DER coding */
27 /* of an ASN1 definition. */
28 /*****************************************************/
30 #include <config.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <libtasn1.h>
35 #include <stdlib.h>
36 #include <unistd.h>
38 #ifdef HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
42 char version_man[] = "asn1Coding (GNU libtasn1) " VERSION "\n"
43 "Written by Fabio Fiorina."
44 "\n"
45 "Copyright (C) 2002, 2006 Free Software Foundation, Inc.\n";
47 char help_man[] =
48 "Usage: asn1Coding [options] <file1> <file2>\n"
49 "\n"
50 "asn1Coding generates a DER encoding from FILE1\n"
51 "with ASN1 definitions and FILE2 with assignments.\n"
52 "\n"
53 #ifdef HAVE_GETOPT_LONG
54 "Operation modes:\n"
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"
58 "\n"
59 "Output:\n"
60 " -o <file>, --output <file> output file.\n"
61 #else
62 "Operation modes:\n"
63 " -h shows this message and exit.\n"
64 " -v shows version information and exit.\n"
65 " -c checks the syntax only.\n"
66 "\n"
67 "Output:\n"
68 " -o <file> output file.\n"
69 #endif
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){
78 int ret;
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 */
97 char_p=inputFileName;
98 slash_p=inputFileName;
99 while((char_p=strchr(char_p,'/'))){
100 char_p++;
101 slash_p=char_p;
104 char_p=slash_p;
105 dot_p=inputFileName+strlen(inputFileName);
107 while((char_p=strchr(char_p,'.'))){
108 dot_p=char_p;
109 char_p++;
112 /* outputFileName= inputFileName + .out */
113 *outputFileName=(char *)malloc(dot_p-inputFileName+1+
114 strlen(".out"));
115 memcpy(*outputFileName,inputFileName,
116 dot_p-inputFileName);
117 (*outputFileName)[dot_p-inputFileName]=0;
118 strcat(*outputFileName,".out");
119 return;
123 /********************************************************/
124 /* Function : main */
125 /* Description: */
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'},
138 {0, 0, 0, 0}
140 int option_index=0;
141 #endif
143 int option_result;
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;
152 FILE *outputFile;
153 FILE *inputFile;
154 char varName[1024];
155 char value[1024];
156 unsigned char der[1024];
157 int der_len;
158 int k;
160 opterr=0; /* disable error messages from getopt */
162 printf("\n");
164 while(1){
166 #ifdef HAVE_GETOPT_LONG
167 option_result=getopt_long(argc,argv,"hvco:",long_options,&option_index);
168 #else
169 option_result=getopt(argc,argv,"hvco:");
170 #endif
172 if(option_result == -1) break;
174 switch(option_result){
175 case 'h': /* HELP */
176 printf("%s\n",help_man);
178 if(outputFileName) free(outputFileName);
179 exit(0);
180 break;
181 case 'v': /* VERSION */
182 printf("%s\n",version_man);
184 if(outputFileName) free(outputFileName);
185 exit(0);
186 break;
187 case 'c': /* CHECK SYNTAX */
188 checkSyntaxOnly = 1;
189 break;
190 case 'o': /* OUTPUT */
191 outputFileName=(char *)malloc(strlen(optarg)+1);
192 strcpy(outputFileName,optarg);
193 break;
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);
199 exit(1);
200 break;
201 default:
202 fprintf(stderr,"asn1Coding: ?? getopt returned character code Ox%x ??\n",option_result);
206 if(optind == argc){
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);
212 exit(1);
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);
220 exit(1);
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);
231 switch(asn1_result){
232 case ASN1_SUCCESS:
233 printf("Parse: done.\n");
234 break;
235 case ASN1_FILE_NOT_FOUND:
236 printf("asn1Coding: FILE %s NOT FOUND\n",inputFileAsnName);
237 break;
238 case ASN1_SYNTAX_ERROR:
239 case ASN1_IDENTIFIER_NOT_FOUND:
240 case ASN1_NAME_TOO_LONG:
241 printf("asn1Coding: %s\n",errorDescription);
242 break;
243 default:
244 printf("libtasn1 ERROR: %s\n",libtasn1_strerror(asn1_result));
247 if(asn1_result != ASN1_SUCCESS){
248 free(inputFileAsnName);
249 free(inputFileAssignmentName);
250 exit(1);
254 inputFile=fopen(inputFileAssignmentName,"r");
256 if(inputFile==NULL){
257 printf("asn1Coding: file '%s' not found\n",inputFileAssignmentName);
258 free(inputFileAsnName);
259 free(inputFileAssignmentName);
260 exit(1);
264 printf("\n");
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);
271 else
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);
283 fclose(inputFile);
284 exit(1);
287 fclose(inputFile);
289 printf("\n");
290 asn1_print_structure(stdout,structure,"",ASN1_PRINT_NAME_TYPE_VALUE);
292 der_len=1024;
293 asn1_result=asn1_der_coding(structure,"",der,&der_len,
294 errorDescription);
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);
305 exit(1);
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);
329 exit(1);
332 for(k=0;k<der_len;k++)
333 fprintf(outputFile,"%c",der[k]);
334 fclose(outputFile);
335 printf("\nWriting: done.\n");
338 free(inputFileAsnName);
339 free(inputFileAssignmentName);
340 free(outputFileName);
342 exit(0);