1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/* asn1Coding.c --- program to generate a DER coding of an ASN1 definition.
* Copyright (C) 2002-2014 Free Software Foundation, Inc.
*
* This file is part of LIBTASN1.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <libtasn1.h>
#include <progname.h>
#include <version-etc.h>
/* This feature is available in gcc versions 2.5 and later. */
#if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5)
#define ATTR_NO_RETRUN
#else
#define ATTR_NO_RETRUN __attribute__ ((__noreturn__))
#endif
ATTR_NO_RETRUN static void
usage (int status)
{
if (status != EXIT_SUCCESS)
fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
else
{
printf ("\
Usage: %s [OPTION] DEFINITIONS ASSIGNMENTS\n", program_name);
printf ("\
Generates a DER encoding of ASN.1 DEFINITIONS file\n\
and ASSIGNMENTS file with value assignments.\n\
\n");
printf ("\
Mandatory arguments to long options are mandatory for short options too.\n\
-c, --check checks the syntax only\n\
-o, --output=FILE output file\n\
-h, --help display this help and exit\n\
-v, --version output version information and exit\n");
emit_bug_reporting_address ();
}
exit (status);
}
#define ASSIGNMENT_SUCCESS 1
#define ASSIGNMENT_ERROR 2
#define ASSIGNMENT_EOF 3
static int
readAssignment (FILE * file, char *varName, char *value)
{
int ret;
ret = fscanf (file, "%s", varName);
if (ret == EOF)
return ASSIGNMENT_EOF;
if (!strcmp (varName, "''"))
varName[0] = 0;
ret = fscanf (file, "%s", value);
if (ret == EOF)
return ASSIGNMENT_ERROR;
return ASSIGNMENT_SUCCESS;
}
static void
createFileName (char *inputFileName, char **outputFileName)
{
char *char_p, *slash_p, *dot_p;
/* searching the last '/' and '.' in inputFileAssignmentName */
char_p = inputFileName;
slash_p = inputFileName;
while ((char_p = strchr (char_p, '/')))
{
char_p++;
slash_p = char_p;
}
char_p = slash_p;
dot_p = inputFileName + strlen (inputFileName);
while ((char_p = strchr (char_p, '.')))
{
dot_p = char_p;
char_p++;
}
/* outputFileName= inputFileName + .out */
*outputFileName = (char *) malloc (dot_p - inputFileName + 1 +
strlen (".out"));
memcpy (*outputFileName, inputFileName, dot_p - inputFileName);
(*outputFileName)[dot_p - inputFileName] = 0;
strcat (*outputFileName, ".out");
return;
}
int
main (int argc, char *argv[])
{
static const struct option long_options[] = {
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{"check", no_argument, 0, 'c'},
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}
};
int option_index = 0;
int option_result;
char *outputFileName = NULL;
char *inputFileAsnName = NULL;
char *inputFileAssignmentName = NULL;
int checkSyntaxOnly = 0;
asn1_node definitions = NULL;
asn1_node structure = NULL;
char errorDescription[ASN1_MAX_ERROR_DESCRIPTION_SIZE];
int asn1_result = ASN1_SUCCESS;
FILE *outputFile;
FILE *inputFile;
char varName[1024];
char value[1024];
unsigned char *der = NULL;
int der_len;
int k;
int last_ra;
set_program_name (argv[0]);
opterr = 0; /* disable error messages from getopt */
while (1)
{
option_result =
getopt_long (argc, argv, "hvco:", long_options, &option_index);
if (option_result == -1)
break;
switch (option_result)
{
case 'h': /* HELP */
free (outputFileName);
usage (EXIT_SUCCESS);
break;
case 'v': /* VERSION */
version_etc (stdout, program_name, PACKAGE, VERSION,
"Fabio Fiorina", NULL);
free (outputFileName);
exit (0);
break;
case 'c': /* CHECK SYNTAX */
checkSyntaxOnly = 1;
break;
case 'o': /* OUTPUT */
outputFileName = (char *) malloc (strlen (optarg) + 1);
strcpy (outputFileName, optarg);
break;
case '?': /* UNKNOW OPTION */
free (outputFileName);
fprintf (stderr,
"asn1Coding: option '%s' not recognized or without argument.\n\n",
argv[optind - 1]);
usage (EXIT_FAILURE);
break;
default:
fprintf (stderr,
"asn1Coding: ?? getopt returned character code Ox%x ??\n",
(unsigned)option_result);
}
}
if (optind == argc || optind == argc - 1)
{
free (outputFileName);
fputs ("asn1Coding: input files missing\n", stderr);
usage (EXIT_FAILURE);
}
inputFileAsnName = (char *) malloc (strlen (argv[optind]) + 1);
strcpy (inputFileAsnName, argv[optind]);
inputFileAssignmentName = (char *) malloc (strlen (argv[optind + 1]) + 1);
strcpy (inputFileAssignmentName, argv[optind + 1]);
asn1_result =
asn1_parser2tree (inputFileAsnName, &definitions, errorDescription);
switch (asn1_result)
{
case ASN1_SUCCESS:
fputs ("Parse: done.\n", stderr);
break;
case ASN1_FILE_NOT_FOUND:
fprintf (stderr, "asn1Coding: FILE %s NOT FOUND\n", inputFileAsnName);
break;
case ASN1_SYNTAX_ERROR:
case ASN1_IDENTIFIER_NOT_FOUND:
case ASN1_NAME_TOO_LONG:
fprintf (stderr, "asn1Coding: %s\n", errorDescription);
break;
default:
fprintf (stderr, "libtasn1 ERROR: %s\n", asn1_strerror (asn1_result));
}
if (asn1_result != ASN1_SUCCESS)
{
free (inputFileAsnName);
free (inputFileAssignmentName);
exit (1);
}
inputFile = fopen (inputFileAssignmentName, "r");
if (inputFile == NULL)
{
fprintf (stderr, "asn1Coding: file '%s' not found\n",
inputFileAssignmentName);
free (inputFileAsnName);
free (inputFileAssignmentName);
exit (1);
}
putc ('\n', stderr);
while ((last_ra = readAssignment (inputFile, varName, value))
== ASSIGNMENT_SUCCESS)
{
fprintf (stderr, "var=%s, value=%s\n", varName, value);
if (structure == NULL)
{
asn1_result = asn1_create_element (definitions, value, &structure);
}
else
{
if (strcmp(value, "(NULL)") == 0)
asn1_result = asn1_write_value (structure, varName, NULL, 0);
else
asn1_result = asn1_write_value (structure, varName, value, 0);
}
if (asn1_result != ASN1_SUCCESS)
{
fprintf (stderr, "libtasn1 ERROR: %s\n",
asn1_strerror (asn1_result));
asn1_delete_structure (&definitions);
asn1_delete_structure (&structure);
free (inputFileAsnName);
free (inputFileAssignmentName);
fclose (inputFile);
exit (1);
}
}
if (last_ra != ASSIGNMENT_EOF)
{
fprintf (stderr, "asn1Coding: error reading assignment file\n");
exit (1);
}
fclose (inputFile);
putc ('\n', stderr);
asn1_print_structure (stderr, structure, "", ASN1_PRINT_NAME_TYPE_VALUE);
der_len = 0;
asn1_result = asn1_der_coding (structure, "", der, &der_len,
errorDescription);
if (asn1_result == ASN1_MEM_ERROR)
{
der = malloc (der_len);
asn1_result = asn1_der_coding (structure, "", der, &der_len,
errorDescription);
}
fprintf (stderr, "\nCoding: %s\n\n", asn1_strerror (asn1_result));
if (asn1_result != ASN1_SUCCESS)
{
fprintf (stderr, "asn1Coding: %s\n", errorDescription);
free (der);
asn1_delete_structure (&definitions);
asn1_delete_structure (&structure);
free (inputFileAsnName);
free (inputFileAssignmentName);
exit (1);
}
/* Print the 'Certificate1' DER encoding */
fprintf (stderr, "-----------------\nNumber of bytes=%i\n", der_len);
for (k = 0; k < der_len; k++)
fprintf (stderr, "%02x ", der[k]);
fputs ("\n-----------------\n", stderr);
asn1_delete_structure (&definitions);
asn1_delete_structure (&structure);
if (!checkSyntaxOnly)
{
if (outputFileName == NULL)
createFileName (inputFileAssignmentName, &outputFileName);
fprintf (stderr, "\nOutputFile=%s\n", outputFileName);
outputFile = fopen (outputFileName, "w");
if (outputFile == NULL)
{
fprintf (stderr,
"asn1Coding: output file '%s' not available\n",
outputFileName);
free (der);
free (inputFileAsnName);
free (inputFileAssignmentName);
free (outputFileName);
exit (1);
}
for (k = 0; k < der_len; k++)
fprintf (outputFile, "%c", der[k]);
fclose (outputFile);
fputs ("\nWriting: done.\n", stderr);
}
free (der);
free (inputFileAsnName);
free (inputFileAssignmentName);
free (outputFileName);
exit (0);
}
|