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
|
/* $OpenBSD: keynote-keygen.c,v 1.23 2024/02/07 17:22:01 tb Exp $ */
/*
* The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
*
* This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
* in April-May 1998
*
* Copyright (C) 1998, 1999 by Angelos D. Keromytis.
*
* Permission to use, copy, and modify this software with or without fee
* is hereby granted, provided that this entire notice is included in
* all copies of any software which is or includes a copy or
* modification of this software.
*
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
* MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
* PURPOSE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/dsa.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/rsa.h>
#include "header.h"
#include "keynote.h"
#include "assertion.h"
#include "signature.h"
void keygenusage(void);
void
keygenusage(void)
{
fprintf(stderr, "Arguments:\n");
fprintf(stderr, "\t<AlgorithmName> <keysize> "
"<PublicKeyFile> <PrivateKeyFile> [<print-offset>] "
"[<print-length>]\n");
}
/*
* Print the specified number of spaces.
*/
void
print_space(FILE *fp, int n)
{
while (n--)
fprintf(fp, " ");
}
/*
* Output a key, properly formatted.
*/
void
print_key(FILE *fp, char *algname, char *key, int start, int length)
{
int i, k;
print_space(fp, start);
fprintf(fp, "\"%s", algname);
for (i = 0, k = strlen(algname) + 2; i < strlen(key); i++, k++)
{
if (k == length)
{
if (i == strlen(key))
{
fprintf(fp, "\"\n");
return;
}
fprintf(fp, "\\\n");
print_space(fp, start);
i--;
k = 0;
}
else
fprintf(fp, "%c", key[i]);
}
fprintf(fp, "\"\n");
}
void
keynote_keygen(int argc, char *argv[])
{
int begin = KEY_PRINT_OFFSET, prlen = KEY_PRINT_LENGTH;
char *foo, *privalgname, seed[SEED_LEN];
int alg, enc, ienc, len = 0, counter;
struct keynote_deckey dc;
unsigned long h;
DSA *dsa;
RSA *rsa;
FILE *fp;
char *algname;
if ((argc != 5) && (argc != 6) && (argc != 7))
{
keygenusage();
exit(0);
}
/* Fix algorithm name */
if (argv[1][strlen(argv[1]) - 1] != ':')
{
int len = strlen(argv[1]) + 2;
fprintf(stderr, "Algorithm name [%s] should be terminated with a "
"colon, fixing.\n", argv[1]);
algname = calloc(len, sizeof(char));
if (algname == NULL)
{
perror("calloc()");
exit(1);
}
strlcpy(algname, argv[1], len);
algname[strlen(algname)] = ':';
}
else
algname = argv[1];
if (argc > 5)
{
begin = atoi(argv[5]);
if (begin <= -1)
{
fprintf(stderr, "Erroneous value for print-offset parameter.\n");
exit(1);
}
}
if (argc > 6)
{
prlen = atoi(argv[6]);
if (prlen <= 0)
{
fprintf(stderr, "Erroneous value for print-length parameter.\n");
exit(1);
}
}
if (strlen(algname) + 2 > prlen)
{
fprintf(stderr, "Parameter ``print-length'' should be larger "
"than the length of AlgorithmName (%lu)\n",
(unsigned long) strlen(algname));
exit(1);
}
alg = keynote_get_key_algorithm(algname, &enc, &ienc);
len = atoi(argv[2]);
if (len <= 0)
{
fprintf(stderr, "Invalid specified keysize %d\n", len);
exit(1);
}
if ((alg == KEYNOTE_ALGORITHM_DSA) &&
(ienc == INTERNAL_ENC_ASN1) &&
((enc == ENCODING_HEX) || (enc == ENCODING_BASE64)))
{
RAND_bytes(seed, SEED_LEN);
dsa = DSA_new();
if (dsa == NULL)
{
ERR_print_errors_fp(stderr);
exit(1);
}
if (DSA_generate_parameters_ex(dsa, len, seed, SEED_LEN,
&counter, &h, NULL) != 1)
{
ERR_print_errors_fp(stderr);
exit(1);
}
if (DSA_generate_key(dsa) != 1)
{
ERR_print_errors_fp(stderr);
exit(1);
}
dc.dec_algorithm = KEYNOTE_ALGORITHM_DSA;
dc.dec_key = (void *) dsa;
foo = kn_encode_key(&dc, ienc, enc, KEYNOTE_PUBLIC_KEY);
if (foo == NULL)
{
fprintf(stderr, "Error encoding key (errno %d)\n", keynote_errno);
exit(1);
}
if (!strcmp(argv[3], "-"))
fp = stdout;
else
{
fp = fopen(argv[3], "w");
if (fp == NULL)
{
perror(argv[3]);
exit(1);
}
}
print_key(fp, algname, foo, begin, prlen);
free(foo);
if (strcmp(argv[3], "-"))
fclose(fp);
foo = kn_encode_key(&dc, ienc, enc, KEYNOTE_PRIVATE_KEY);
if (foo == NULL)
{
fprintf(stderr, "Error encoding key (errno %d)\n", keynote_errno);
exit(1);
}
if (!strcmp(argv[4], "-"))
{
fp = stdout;
if (!strcmp(argv[3], "-"))
printf("===========================\n");
}
else
{
fp = fopen(argv[4], "w");
if (fp == NULL)
{
perror(argv[4]);
exit(1);
}
}
len = strlen(KEYNOTE_PRIVATE_KEY_PREFIX) + strlen(foo) + 1;
privalgname = calloc(len, sizeof(char));
if (privalgname == NULL)
{
perror("calloc()");
exit(1);
}
snprintf(privalgname, len, "%s%s", KEYNOTE_PRIVATE_KEY_PREFIX, algname);
print_key(fp, privalgname, foo, begin, prlen);
free(privalgname);
free(foo);
if (strcmp(argv[4], "-"))
fclose(fp);
exit(0);
}
if ((alg == KEYNOTE_ALGORITHM_RSA) &&
(ienc == INTERNAL_ENC_PKCS1) &&
((enc == ENCODING_HEX) || (enc == ENCODING_BASE64)))
{
rsa = RSA_generate_key(len, DEFAULT_PUBLIC, NULL, NULL);
if (rsa == NULL)
{
ERR_print_errors_fp(stderr);
exit(1);
}
dc.dec_algorithm = KEYNOTE_ALGORITHM_RSA;
dc.dec_key = (void *) rsa;
foo = kn_encode_key(&dc, ienc, enc, KEYNOTE_PUBLIC_KEY);
if (foo == NULL)
{
fprintf(stderr, "Error encoding key (errno %d)\n", keynote_errno);
exit(1);
}
if (!strcmp(argv[3], "-"))
fp = stdout;
else
{
fp = fopen(argv[3], "w");
if (fp == NULL)
{
perror(argv[3]);
exit(1);
}
}
print_key(fp, algname, foo, begin, prlen);
free(foo);
if (strcmp(argv[3], "-"))
fclose(fp);
foo = kn_encode_key(&dc, ienc, enc, KEYNOTE_PRIVATE_KEY);
if (foo == NULL)
{
fprintf(stderr, "Error encoding key (errno %d)\n", keynote_errno);
exit(1);
}
if (!strcmp(argv[4], "-"))
{
fp = stdout;
if (!strcmp(argv[3], "-"))
printf("===========================\n");
}
else
{
fp = fopen(argv[4], "w");
if (fp == NULL)
{
perror(argv[4]);
exit(1);
}
}
len = strlen(KEYNOTE_PRIVATE_KEY_PREFIX) + strlen(foo) + 1;
privalgname = calloc(len, sizeof(char));
if (privalgname == NULL)
{
perror("calloc()");
exit(1);
}
snprintf(privalgname, len, "%s%s", KEYNOTE_PRIVATE_KEY_PREFIX, algname);
print_key(fp, privalgname, foo, begin, prlen);
free(privalgname);
free(foo);
if (strcmp(argv[4], "-"))
fclose(fp);
exit(0);
}
/* More algorithms here */
fprintf(stderr, "Unknown/unsupported algorithm [%s]\n", algname);
exit(1);
}
|