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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
|
/* $OpenBSD: parse_assertion.c,v 1.17 2022/12/27 17:10:06 jmc 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 <ctype.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keynote.h"
#include "assertion.h"
#include "signature.h"
/*
* Recurse on graph discovery.
*/
static int
rec_evaluate_query(struct assertion *as)
{
struct assertion *ast;
struct keylist *kl;
int i, s;
as->as_kresult = KRESULT_IN_PROGRESS;
/*
* If we get the minimum result or an error from evaluating this
* assertion, we don't need to recurse.
*/
keynote_evaluate_assertion(as);
if (keynote_errno != 0)
{
as->as_kresult = KRESULT_DONE;
if (keynote_errno)
as->as_error = keynote_errno;
if (keynote_errno == ERROR_MEMORY)
return -1;
else
{
keynote_errno = 0; /* Ignore syntax errors for now */
return 0;
}
}
if (as->as_result == 0)
{
as->as_kresult = KRESULT_DONE;
return as->as_result;
}
for (kl = as->as_keylist;
kl != NULL;
kl = kl->key_next)
{
switch (keynote_in_action_authorizers(kl->key_key, kl->key_alg))
{
case -1:
as->as_kresult = KRESULT_DONE;
if (keynote_errno == ERROR_MEMORY)
{
as->as_error = ERROR_MEMORY;
return -1;
}
else
{
keynote_errno = 0; /* Reset */
continue;
}
case RESULT_FALSE: /* Not there, check for assertions instead */
break;
case RESULT_TRUE: /* Ok, don't bother with assertions */
keynote_current_assertion = NULL;
continue;
}
for (i = 0;; i++)
{
ast = keynote_find_assertion(kl->key_key, i, kl->key_alg);
if (ast == NULL)
break;
if (ast->as_kresult == KRESULT_IN_PROGRESS) /* Cycle detected */
continue;
if (ast->as_kresult == KRESULT_UNTOUCHED) /* Recurse if needed */
rec_evaluate_query(ast);
/* Check for errors */
if (keynote_errno == ERROR_MEMORY)
{
as->as_error = ERROR_MEMORY;
as->as_kresult = KRESULT_DONE;
return -1;
}
else
keynote_errno = 0; /* Reset */
}
}
keynote_current_assertion = as;
s = keynote_parse_keypred(as, 0);
keynote_current_assertion = NULL;
if (keynote_errno == ERROR_MEMORY)
{
as->as_error = ERROR_MEMORY;
as->as_kresult = KRESULT_DONE;
return -1;
}
else
if (keynote_errno)
{
keynote_errno = 0;
s = 0;
}
/* Keep lower of two */
as->as_result = (as->as_result < s ? as->as_result : s);
/* Check the signature now if we haven't done so already */
if (as->as_sigresult == SIGRESULT_UNTOUCHED)
{
if (!(as->as_flags & ASSERT_FLAG_LOCAL))
as->as_sigresult = keynote_sigverify_assertion(as);
else
as->as_sigresult = SIGRESULT_TRUE; /* Trusted assertion */
}
if (as->as_sigresult != SIGRESULT_TRUE)
{
as->as_result = 0;
as->as_sigresult = SIGRESULT_FALSE;
if (keynote_errno != ERROR_MEMORY)
keynote_errno = 0; /* Reset */
else
{
as->as_error = ERROR_MEMORY;
as->as_kresult = KRESULT_DONE;
return -1;
}
}
as->as_kresult = KRESULT_DONE;
return as->as_result;
}
/*
* Fix the Authorizer/Licencees/Signature fields. If the first argument is
* empty, fix all assertions. The second argument specifies whether the
* Signature field should be parsed or not.
*/
static int
keynote_fix_fields(struct assertion *ast, int sigfield)
{
struct assertion *as;
int i;
/* Signature generation/verification handling, no need to eval Licensees */
if (ast != NULL)
{
/* Authorizer */
if (keynote_evaluate_authorizer(ast, 1) != RESULT_TRUE)
return -1;
/* Signature */
if ((sigfield) && (ast->as_signature_string_s != NULL))
if (keynote_evaluate_authorizer(ast, 0) != RESULT_TRUE)
return -1;
return RESULT_TRUE;
}
for (i = 0; i < HASHTABLESIZE; i++)
for (as = keynote_current_session->ks_assertion_table[i];
as != NULL;
as = as->as_next)
{
if (!(as->as_internalflags & ASSERT_IFLAG_NEEDPROC) &&
!(as->as_internalflags & ASSERT_IFLAG_WEIRDLICS) &&
!(as->as_internalflags & ASSERT_IFLAG_WEIRDAUTH) &&
!(as->as_internalflags & ASSERT_IFLAG_WEIRDSIG))
continue;
/* Parse the Signature field */
if (((as->as_internalflags & ASSERT_IFLAG_WEIRDSIG) ||
(as->as_internalflags & ASSERT_IFLAG_NEEDPROC)) &&
(as->as_signature_string_s != NULL))
if (keynote_evaluate_authorizer(as, 0) == -1)
{
if (keynote_errno)
as->as_error = keynote_errno;
if (keynote_errno == ERROR_MEMORY)
return -1;
else
keynote_errno = 0;
}
/* Parse the Licensees field */
if ((as->as_internalflags & ASSERT_IFLAG_WEIRDLICS) ||
(as->as_internalflags & ASSERT_IFLAG_NEEDPROC))
if (keynote_parse_keypred(as, 1) == -1)
{
if (keynote_errno)
as->as_error = keynote_errno;
if (keynote_errno == ERROR_MEMORY)
return -1;
else
keynote_errno = 0;
}
/* Parse the Authorizer field */
if ((as->as_internalflags & ASSERT_IFLAG_WEIRDAUTH) ||
(as->as_internalflags & ASSERT_IFLAG_NEEDPROC))
if (keynote_evaluate_authorizer(as, 1) == -1)
{
if (keynote_errno)
as->as_error = keynote_errno;
if (keynote_errno == ERROR_MEMORY)
return -1;
else
keynote_errno = 0;
}
}
/* Reposition if necessary */
for (i = 0; i < HASHTABLESIZE; i++)
for (as = keynote_current_session->ks_assertion_table[i];
as != NULL;
as = as->as_next)
if (((as->as_internalflags & ASSERT_IFLAG_WEIRDAUTH) &&
!(as->as_internalflags & ASSERT_IFLAG_PROCESSED)) ||
(as->as_internalflags & ASSERT_IFLAG_NEEDPROC))
{
as->as_internalflags &= ~ASSERT_IFLAG_NEEDPROC;
as->as_internalflags |= ASSERT_IFLAG_PROCESSED;
keynote_sremove_assertion(keynote_current_session->ks_id,
as->as_id);
if (keynote_add_htable(as, 1) != RESULT_TRUE)
return -1;
/* Point to beginning of the previous list. */
i--;
break;
}
return RESULT_TRUE;
}
/*
* Find the trust graph. This is a depth-first search, starting at
* POLICY assertions.
*/
int
keynote_evaluate_query(void)
{
struct assertion *as;
int p, prev;
int i;
/* Fix the authorizer/licensees/signature fields */
if (keynote_fix_fields(NULL, 0) != RESULT_TRUE)
return -1;
/* Find POLICY assertions and try to evaluate the query. */
for (i = 0, prev = 0; i < HASHTABLESIZE; i++)
for (as = keynote_current_session->ks_assertion_table[i];
as != NULL;
as = as->as_next)
if ((as->as_authorizer != NULL) && /* Paranoid */
(as->as_signeralgorithm == KEYNOTE_ALGORITHM_NONE))
if ((!strcmp("POLICY", as->as_authorizer)) &&
(as->as_flags & ASSERT_FLAG_LOCAL))
{
if ((p = rec_evaluate_query(as)) == -1)
{
if (keynote_errno)
as->as_error = keynote_errno;
if (keynote_errno == ERROR_MEMORY)
return -1;
else
{
keynote_errno = 0;
continue;
}
}
if (p > prev)
prev = p;
/* If we get the highest possible return value, just return */
if (prev == (keynote_current_session->ks_values_num - 1))
return prev;
}
return prev;
}
/*
* Return keyword type.
*/
static int
whichkeyword(char *start, char *end)
{
int len = (end - start);
if (len <= 0)
{
keynote_errno = ERROR_MEMORY;
return -1;
}
if (!strncasecmp("keynote-version:", start, len))
return KEYWORD_VERSION;
if (!strncasecmp("local-constants:", start, len))
return KEYWORD_LOCALINIT;
if (!strncasecmp("authorizer:", start, len))
return KEYWORD_AUTHORIZER;
if (!strncasecmp("licensees:", start, len))
return KEYWORD_LICENSEES;
if (!strncasecmp("conditions:", start, len))
return KEYWORD_CONDITIONS;
if (!strncasecmp("signature:", start, len))
return KEYWORD_SIGNATURE;
if (!strncasecmp("comment:", start, len))
return KEYWORD_COMMENT;
keynote_errno = ERROR_SYNTAX;
return -1;
}
/*
* Parse an assertion. Set keynote_errno to ERROR_SYNTAX if parsing
* failed due to certificate badness, and ERROR_MEMORY if memory
* problem. If more than one assertions have been passed in the
* buffer, they will be linked.
*/
struct assertion *
keynote_parse_assertion(char *buf, int len, int assertion_flags)
{
int k, i, j, seen_field = 0, ver = 0, end_of_assertion = 0;
char *ks, *ke, *ts, *te = NULL;
struct assertion *as;
/* Allocate memory for assertion */
as = calloc(1, sizeof(struct assertion));
if (as == NULL)
{
keynote_errno = ERROR_MEMORY;
return NULL;
}
/* Keep a copy of the assertion around */
as->as_buf = strdup(buf);
if (as->as_buf == NULL)
{
keynote_errno = ERROR_MEMORY;
keynote_free_assertion(as);
return NULL;
}
as->as_flags = assertion_flags & ~(ASSERT_FLAG_SIGGEN |
ASSERT_FLAG_SIGVER);
/* Skip any leading whitespace */
for (i = 0, j = len; i < j && isspace((unsigned char)as->as_buf[i]); i++)
;
/* Keyword must start at beginning of buffer or line */
if ((i >= j) || ((i != 0) && (as->as_buf[i - 1] != '\n')))
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
while (i < j) /* Decomposition loop */
{
ks = as->as_buf + i;
/* Mark beginning of assertion for signature purposes */
if (as->as_startofsignature == NULL)
as->as_startofsignature = ks;
/* This catches comments at the beginning of an assertion only */
if (as->as_buf[i] == '#') /* Comment */
{
seen_field = 1;
/* Skip until the end of line */
while ((i< j) && as->as_buf[++i] != '\n')
;
i++;
continue; /* Loop */
}
/* Advance until we find a keyword separator */
for (; (as->as_buf[i] != ':') && (i < j); i++)
;
if (i + 1 > j)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
/* ks points at beginning of keyword, ke points at end */
ke = as->as_buf + i;
/* ts points at beginning of value field */
ts = as->as_buf + i + 1; /* Skip ':' */
/*
* Find the end of the field -- means end of buffer,
* a newline followed by a non-whitespace character,
* or two newlines.
*/
while (++i <= j)
{
/* If end of buffer, we're at the end of the field */
if (i == j)
{
end_of_assertion = 1;
te = as->as_buf + i;
break;
}
/* If two newlines, end of assertion */
if ((as->as_buf[i] == '\n') && (i + 1 < j) &&
(as->as_buf[i + 1] == '\n'))
{
end_of_assertion = 1;
te = as->as_buf + i;
break;
}
/* If newline followed by non-whitespace or comment character */
if ((as->as_buf[i] == '\n') &&
(!isspace((unsigned char)as->as_buf[i + 1])) &&
(as->as_buf[i + 1] != '#'))
{
te = as->as_buf + i;
break;
}
}
i++;
/*
* On each of the cases (except the first), we check that:
* - we've already seen a keynote-version field (and that
* it's the first one that appears in the assertion)
* - the signature field, if present, is the last one
* - no field appears more than once
*/
switch (whichkeyword(ks, ke))
{
case -1:
keynote_free_assertion(as);
return NULL;
case KEYWORD_VERSION:
if ((ver == 1) || (seen_field == 1))
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
/* Test for version correctness */
keynote_get_envlist(ts, te, 1);
if (keynote_errno != 0)
{
keynote_free_assertion(as);
return NULL;
}
ver = 1;
break;
case KEYWORD_LOCALINIT:
if (as->as_env != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
as->as_env = keynote_get_envlist(ts, te, 0);
if (keynote_errno != 0)
{
keynote_free_assertion(as);
return NULL;
}
break;
case KEYWORD_AUTHORIZER:
if (as->as_authorizer_string_s != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
as->as_authorizer_string_s = ts;
as->as_authorizer_string_e = te;
break;
case KEYWORD_LICENSEES:
if (as->as_keypred_s != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
as->as_keypred_s = ts;
as->as_keypred_e = te;
break;
case KEYWORD_CONDITIONS:
if (as->as_conditions_s != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
as->as_conditions_s = ts;
as->as_conditions_e = te;
break;
case KEYWORD_SIGNATURE:
if (as->as_signature_string_s != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
end_of_assertion = 1;
as->as_allbutsignature = ks;
as->as_signature_string_s = ts;
as->as_signature_string_e = te;
break;
case KEYWORD_COMMENT:
if (as->as_comment_s != NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
as->as_comment_s = ts;
as->as_comment_e = te;
break;
}
seen_field = 1;
if (end_of_assertion == 1)
{
/* End of buffer, good termination */
if ((te == as->as_buf + len) || (te + 1 == as->as_buf + len) ||
(*(te) == '\0') || (*(te + 1) == '\0'))
break;
/* Check whether there's something else following */
for (k = 1; te + k < as->as_buf + len && *(te + k) != '\n'; k++)
if (!isspace((unsigned char)*(te + k)))
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
break; /* Assertion is "properly" terminated */
}
}
/* Check that the basic fields are there */
if (as->as_authorizer_string_s == NULL)
{
keynote_free_assertion(as);
keynote_errno = ERROR_SYNTAX;
return NULL;
}
/* Signature generation/verification handling */
if (assertion_flags & ASSERT_FLAG_SIGGEN)
{
if (keynote_fix_fields(as, 0) != RESULT_TRUE)
{
keynote_free_assertion(as);
return NULL;
}
}
else
if (assertion_flags & ASSERT_FLAG_SIGVER)
if (keynote_fix_fields(as, 1) != RESULT_TRUE)
{
keynote_free_assertion(as);
return NULL;
}
return as;
}
|