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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
/* $OpenBSD: keynote-verify.c,v 1.20 2021/10/24 21:24:20 deraadt 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 <getopt.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "header.h"
#include "keynote.h"
int sessid;
void verifyusage(void);
void
verifyusage(void)
{
fprintf(stderr, "Arguments:\n");
fprintf(stderr, "\t-h: This message\n");
fprintf(stderr,
"\t-r <valuelist>: Comma separated, ordered return-value list\n");
fprintf(stderr, "\t-e <filename>: Environment settings\n");
fprintf(stderr, "\t-l <filename>: Trusted (local) assertion\n");
fprintf(stderr, "\t-k <filename>: File containing key\n");
fprintf(stderr, "Followed by a list of:\n");
fprintf(stderr, "\t<filename>: Non-local assertion\n");
}
void
keynote_verify(int argc, char *argv[])
{
int fd, i, ch, se = 0, cl = 8192, sk = 0, sl = 0, p, ac = argc;
char *buf, **av = argv, **retv, **foov, *ptr;
int numretv = 16, numret = 0, sn;
struct stat sb;
if (argc == 1)
{
verifyusage();
exit(1);
}
if ((buf = calloc(cl, sizeof(char))) == NULL)
{
perror("calloc()");
exit(1);
}
if ((retv = calloc(numretv, sizeof(char *))) == NULL)
{
perror("calloc()");
exit(1);
}
/* "ac" and "av" are used for stress-testing, ignore otherwise */
argv = av;
argc = ac;
sn = 0;
opterr = 0;
sessid = kn_init();
if (sessid == -1)
{
fprintf(stderr, "kn_init() failed (errno %d).\n", keynote_errno);
exit(keynote_errno);
}
while ((ch = getopt(argc, argv, "hqistl:e:k:r:")) != -1)
{
switch (ch)
{
case 'e':
if (read_environment(optarg) == -1)
exit(1);
se = 1;
break;
case 'k':
sk = 1;
if ((fd = open(optarg, O_RDONLY)) == -1)
{
perror(optarg);
exit(1);
}
if (fstat(fd, &sb) == -1)
{
perror("fstat()");
exit(1);
}
if (sb.st_size > cl - 1)
{
free(buf);
cl = sb.st_size + 1;
buf = calloc(cl, sizeof(char));
if (buf == NULL)
{
perror("calloc()");
exit(1);
}
}
i = read(fd, buf, sb.st_size);
if (i == -1)
{
perror("read()");
exit(1);
}
close(fd);
parse_key(buf);
switch (keynote_errno)
{
case 0: /* No errors */
break;
case ERROR_SYNTAX:
fprintf(stderr, "Syntax error adding authorizer "
"%s\n", optarg);
exit(1);
case ERROR_MEMORY:
perror("Out of memory.\n");
exit(1);
default:
fprintf(stderr, "Unknown error (%d).\n",
keynote_errno);
}
break;
case 'h':
verifyusage();
exit(0);
case 'r':
if (sn != 0)
{
fprintf(stderr,
"Do not define two sets of return values.\n");
exit(1);
}
sn = 1;
for (numret = 0;
(ptr = strchr(optarg, ',')) != NULL;
numret++)
{
/* Running out of memory */
if (numret > numretv - 3)
{
numretv *= 2;
foov = calloc(numretv, sizeof(char **));
if (foov == NULL)
{
/*
* If this were a real program, we 'd be freeing
* retv here. Since we're exiting, we can be a
* little sloppy.
*/
perror("calloc()");
exit(1);
}
memcpy(foov, retv, numretv * sizeof(char **));
free(retv);
retv = foov;
}
retv[numret] = calloc((ptr - optarg) + 1,
sizeof(char));
if (retv[numret] == NULL)
{
/* Comment from above applies here as well */
perror("calloc()");
exit(1);
}
/* Copy */
memcpy(retv[numret], optarg, ptr - optarg);
optarg = ptr + 1;
}
/* Last component */
retv[numret] = strdup(optarg);
if (retv[numret] == NULL)
{
perror("calloc()");
exit(1);
}
numret++;
break;
case 'l':
if ((fd = open(optarg, O_RDONLY)) == -1)
{
perror(optarg);
exit(1);
}
if (fstat(fd, &sb) == -1)
{
perror("fstat()");
exit(1);
}
if (sb.st_size > cl - 1)
{
free(buf);
cl = sb.st_size + 1;
buf = calloc(cl, sizeof(char));
if (buf == NULL)
{
perror("calloc()");
exit(1);
}
}
i = read(fd, buf, sb.st_size);
if (i == -1)
{
perror("read()");
exit(1);
}
close(fd);
p = kn_add_assertion(sessid, buf, i, ASSERT_FLAG_LOCAL);
if (p == -1)
{
fprintf(stderr,
"Error for assertion in file <%s>, errno %d.\n",
optarg, keynote_errno);
keynote_errno = 0;
}
memset(buf, 0, sb.st_size);
sl = 1;
break;
case '?':
default:
verifyusage();
exit(1);
}
}
argc -= optind;
argv += optind;
optind = 1;
if (sn == 0)
{
fprintf(stderr,
"Should set return values before evaluations begin.\n");
exit(1);
}
if (se == 0)
{
fprintf(stderr, "Should set environment before evaluations begin.\n");
exit(1);
}
if (sk == 0)
{
fprintf(stderr, "Should specify at least one action authorizer.\n");
exit(1);
}
if (sl == 0)
{
fprintf(stderr,
"Should specify at least one trusted assertion (POLICY).\n");
exit(1);
}
while (argc--)
{
if ((fd = open(argv[argc], O_RDONLY)) == -1)
{
perror(argv[argc]);
exit(1);
}
if (fstat(fd, &sb) == -1)
{
perror("fstat()");
exit(1);
}
if (sb.st_size > cl - 1)
{
free(buf);
cl = sb.st_size + 1;
buf = calloc(cl, sizeof(char));
if (buf == NULL)
{
perror("calloc()");
exit(1);
}
}
i = read(fd, buf, sb.st_size);
if (i == -1)
{
perror("read()");
exit(1);
}
close(fd);
p = kn_add_assertion(sessid, buf, i, 0);
if (p == -1)
{
fprintf(stderr, "Error for assertion in file <%s>, errno %d.\n",
argv[argc], keynote_errno);
keynote_errno = 0;
}
memset(buf, 0, sb.st_size);
}
p = kn_do_query(sessid, retv, numret); /* Evaluation time */
printf("Query result = ");
switch (keynote_errno)
{
case ERROR_MEMORY:
printf("<out of memory>\n");
exit(1);
case ERROR_SYNTAX:
printf("<uninitialized authorizers or all POLICY "
"assertions are malformed!>\n");
exit(1);
case ERROR_NOTFOUND:
printf("<session or other information not found!>\n");
exit(1);
case 0: /* No errors */
break;
default:
printf("<should never happen (%d)!>\n", keynote_errno);
exit(1);
}
printf("%s\n", retv[p]);
keynote_errno = 0;
while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_MEMORY, 0)) != -1)
{
printf("Failed assertion %d due to memory error.\n", i);
kn_remove_assertion(sessid, i);
}
while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_SYNTAX, 0)) != -1)
{
printf("Failed assertion %d due to syntax or semantic error.\n", i);
kn_remove_assertion(sessid, i);
}
while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_SIGNATURE, 0)) != -1)
{
printf("Failed assertion %d due to signature verification failure.\n",
i);
kn_remove_assertion(sessid, i);
}
while ((i = kn_get_failed(sessid, KEYNOTE_ERROR_ANY, 0)) != -1)
{
printf("Failed assertion %d due to unspecified error.\n", i);
kn_remove_assertion(sessid, i);
}
kn_close(sessid);
/* This is a reminder that return values are not free'ed by KeyNote */
for (sn = 0; sn < numret; sn++)
free(retv[sn]);
free(retv);
retv = NULL;
exit(0);
}
|