summaryrefslogtreecommitdiff
path: root/krl.c
diff options
context:
space:
mode:
authorColin Watson <cjwatson@debian.org>2013-05-07 10:06:42 +0100
committerColin Watson <cjwatson@debian.org>2013-05-07 10:06:42 +0100
commitecebda56da46a03dafff923d91c382f31faa9eec (patch)
tree449614b6c06a2622c74a609b31fcc46c60037c56 /krl.c
parentc6a2c0334e45419875687d250aed9bea78480f2e (diff)
parentffc06452028ba78cd693d4ed43df8b60a10d6163 (diff)
merge 6.2p1; reorder additions to monitor.h for easier merging in future
Diffstat (limited to 'krl.c')
-rw-r--r--krl.c1229
1 files changed, 1229 insertions, 0 deletions
diff --git a/krl.c b/krl.c
new file mode 100644
index 000000000..5a6bd14aa
--- /dev/null
+++ b/krl.c
@@ -0,0 +1,1229 @@
1/*
2 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $OpenBSD: krl.c,v 1.9 2013/01/27 10:06:12 djm Exp $ */
18
19#include "includes.h"
20
21#include <sys/types.h>
22#include <sys/param.h>
23#include <openbsd-compat/sys-tree.h>
24#include <openbsd-compat/sys-queue.h>
25
26#include <errno.h>
27#include <fcntl.h>
28#include <limits.h>
29#include <string.h>
30#include <time.h>
31#include <unistd.h>
32
33#include "buffer.h"
34#include "key.h"
35#include "authfile.h"
36#include "misc.h"
37#include "log.h"
38#include "xmalloc.h"
39
40#include "krl.h"
41
42/* #define DEBUG_KRL */
43#ifdef DEBUG_KRL
44# define KRL_DBG(x) debug3 x
45#else
46# define KRL_DBG(x)
47#endif
48
49/*
50 * Trees of revoked serial numbers, key IDs and keys. This allows
51 * quick searching, querying and producing lists in canonical order.
52 */
53
54/* Tree of serial numbers. XXX make smarter: really need a real sparse bitmap */
55struct revoked_serial {
56 u_int64_t lo, hi;
57 RB_ENTRY(revoked_serial) tree_entry;
58};
59static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
60RB_HEAD(revoked_serial_tree, revoked_serial);
61RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp);
62
63/* Tree of key IDs */
64struct revoked_key_id {
65 char *key_id;
66 RB_ENTRY(revoked_key_id) tree_entry;
67};
68static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
69RB_HEAD(revoked_key_id_tree, revoked_key_id);
70RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp);
71
72/* Tree of blobs (used for keys and fingerprints) */
73struct revoked_blob {
74 u_char *blob;
75 u_int len;
76 RB_ENTRY(revoked_blob) tree_entry;
77};
78static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
79RB_HEAD(revoked_blob_tree, revoked_blob);
80RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp);
81
82/* Tracks revoked certs for a single CA */
83struct revoked_certs {
84 Key *ca_key;
85 struct revoked_serial_tree revoked_serials;
86 struct revoked_key_id_tree revoked_key_ids;
87 TAILQ_ENTRY(revoked_certs) entry;
88};
89TAILQ_HEAD(revoked_certs_list, revoked_certs);
90
91struct ssh_krl {
92 u_int64_t krl_version;
93 u_int64_t generated_date;
94 u_int64_t flags;
95 char *comment;
96 struct revoked_blob_tree revoked_keys;
97 struct revoked_blob_tree revoked_sha1s;
98 struct revoked_certs_list revoked_certs;
99};
100
101/* Return equal if a and b overlap */
102static int
103serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
104{
105 if (a->hi >= b->lo && a->lo <= b->hi)
106 return 0;
107 return a->lo < b->lo ? -1 : 1;
108}
109
110static int
111key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
112{
113 return strcmp(a->key_id, b->key_id);
114}
115
116static int
117blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
118{
119 int r;
120
121 if (a->len != b->len) {
122 if ((r = memcmp(a->blob, b->blob, MIN(a->len, b->len))) != 0)
123 return r;
124 return a->len > b->len ? 1 : -1;
125 } else
126 return memcmp(a->blob, b->blob, a->len);
127}
128
129struct ssh_krl *
130ssh_krl_init(void)
131{
132 struct ssh_krl *krl;
133
134 if ((krl = calloc(1, sizeof(*krl))) == NULL)
135 return NULL;
136 RB_INIT(&krl->revoked_keys);
137 RB_INIT(&krl->revoked_sha1s);
138 TAILQ_INIT(&krl->revoked_certs);
139 return krl;
140}
141
142static void
143revoked_certs_free(struct revoked_certs *rc)
144{
145 struct revoked_serial *rs, *trs;
146 struct revoked_key_id *rki, *trki;
147
148 RB_FOREACH_SAFE(rs, revoked_serial_tree, &rc->revoked_serials, trs) {
149 RB_REMOVE(revoked_serial_tree, &rc->revoked_serials, rs);
150 free(rs);
151 }
152 RB_FOREACH_SAFE(rki, revoked_key_id_tree, &rc->revoked_key_ids, trki) {
153 RB_REMOVE(revoked_key_id_tree, &rc->revoked_key_ids, rki);
154 free(rki->key_id);
155 free(rki);
156 }
157 if (rc->ca_key != NULL)
158 key_free(rc->ca_key);
159}
160
161void
162ssh_krl_free(struct ssh_krl *krl)
163{
164 struct revoked_blob *rb, *trb;
165 struct revoked_certs *rc, *trc;
166
167 if (krl == NULL)
168 return;
169
170 free(krl->comment);
171 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
172 RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
173 free(rb->blob);
174 free(rb);
175 }
176 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
177 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
178 free(rb->blob);
179 free(rb);
180 }
181 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
182 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
183 revoked_certs_free(rc);
184 }
185}
186
187void
188ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
189{
190 krl->krl_version = version;
191}
192
193void
194ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
195{
196 free(krl->comment);
197 if ((krl->comment = strdup(comment)) == NULL)
198 fatal("%s: strdup", __func__);
199}
200
201/*
202 * Find the revoked_certs struct for a CA key. If allow_create is set then
203 * create a new one in the tree if one did not exist already.
204 */
205static int
206revoked_certs_for_ca_key(struct ssh_krl *krl, const Key *ca_key,
207 struct revoked_certs **rcp, int allow_create)
208{
209 struct revoked_certs *rc;
210
211 *rcp = NULL;
212 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
213 if (key_equal(rc->ca_key, ca_key)) {
214 *rcp = rc;
215 return 0;
216 }
217 }
218 if (!allow_create)
219 return 0;
220 /* If this CA doesn't exist in the list then add it now */
221 if ((rc = calloc(1, sizeof(*rc))) == NULL)
222 return -1;
223 if ((rc->ca_key = key_from_private(ca_key)) == NULL) {
224 free(rc);
225 return -1;
226 }
227 RB_INIT(&rc->revoked_serials);
228 RB_INIT(&rc->revoked_key_ids);
229 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
230 debug3("%s: new CA %s", __func__, key_type(ca_key));
231 *rcp = rc;
232 return 0;
233}
234
235static int
236insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
237{
238 struct revoked_serial rs, *ers, *crs, *irs;
239
240 KRL_DBG(("%s: insert %llu:%llu", __func__, lo, hi));
241 bzero(&rs, sizeof(rs));
242 rs.lo = lo;
243 rs.hi = hi;
244 ers = RB_NFIND(revoked_serial_tree, rt, &rs);
245 if (ers == NULL || serial_cmp(ers, &rs) != 0) {
246 /* No entry matches. Just insert */
247 if ((irs = malloc(sizeof(rs))) == NULL)
248 return -1;
249 memcpy(irs, &rs, sizeof(*irs));
250 ers = RB_INSERT(revoked_serial_tree, rt, irs);
251 if (ers != NULL) {
252 KRL_DBG(("%s: bad: ers != NULL", __func__));
253 /* Shouldn't happen */
254 free(irs);
255 return -1;
256 }
257 ers = irs;
258 } else {
259 KRL_DBG(("%s: overlap found %llu:%llu", __func__,
260 ers->lo, ers->hi));
261 /*
262 * The inserted entry overlaps an existing one. Grow the
263 * existing entry.
264 */
265 if (ers->lo > lo)
266 ers->lo = lo;
267 if (ers->hi < hi)
268 ers->hi = hi;
269 }
270 /*
271 * The inserted or revised range might overlap or abut adjacent ones;
272 * coalesce as necessary.
273 */
274
275 /* Check predecessors */
276 while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
277 KRL_DBG(("%s: pred %llu:%llu", __func__, crs->lo, crs->hi));
278 if (ers->lo != 0 && crs->hi < ers->lo - 1)
279 break;
280 /* This entry overlaps. */
281 if (crs->lo < ers->lo) {
282 ers->lo = crs->lo;
283 KRL_DBG(("%s: pred extend %llu:%llu", __func__,
284 ers->lo, ers->hi));
285 }
286 RB_REMOVE(revoked_serial_tree, rt, crs);
287 free(crs);
288 }
289 /* Check successors */
290 while ((crs = RB_NEXT(revoked_serial_tree, rt, ers)) != NULL) {
291 KRL_DBG(("%s: succ %llu:%llu", __func__, crs->lo, crs->hi));
292 if (ers->hi != (u_int64_t)-1 && crs->lo > ers->hi + 1)
293 break;
294 /* This entry overlaps. */
295 if (crs->hi > ers->hi) {
296 ers->hi = crs->hi;
297 KRL_DBG(("%s: succ extend %llu:%llu", __func__,
298 ers->lo, ers->hi));
299 }
300 RB_REMOVE(revoked_serial_tree, rt, crs);
301 free(crs);
302 }
303 KRL_DBG(("%s: done, final %llu:%llu", __func__, ers->lo, ers->hi));
304 return 0;
305}
306
307int
308ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const Key *ca_key,
309 u_int64_t serial)
310{
311 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
312}
313
314int
315ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl, const Key *ca_key,
316 u_int64_t lo, u_int64_t hi)
317{
318 struct revoked_certs *rc;
319
320 if (lo > hi || lo == 0)
321 return -1;
322 if (revoked_certs_for_ca_key(krl, ca_key, &rc, 1) != 0)
323 return -1;
324 return insert_serial_range(&rc->revoked_serials, lo, hi);
325}
326
327int
328ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const Key *ca_key,
329 const char *key_id)
330{
331 struct revoked_key_id *rki, *erki;
332 struct revoked_certs *rc;
333
334 if (revoked_certs_for_ca_key(krl, ca_key, &rc, 1) != 0)
335 return -1;
336
337 debug3("%s: revoke %s", __func__, key_id);
338 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
339 (rki->key_id = strdup(key_id)) == NULL) {
340 free(rki);
341 fatal("%s: strdup", __func__);
342 }
343 erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
344 if (erki != NULL) {
345 free(rki->key_id);
346 free(rki);
347 }
348 return 0;
349}
350
351/* Convert "key" to a public key blob without any certificate information */
352static int
353plain_key_blob(const Key *key, u_char **blob, u_int *blen)
354{
355 Key *kcopy;
356 int r;
357
358 if ((kcopy = key_from_private(key)) == NULL)
359 return -1;
360 if (key_is_cert(kcopy)) {
361 if (key_drop_cert(kcopy) != 0) {
362 error("%s: key_drop_cert", __func__);
363 key_free(kcopy);
364 return -1;
365 }
366 }
367 r = key_to_blob(kcopy, blob, blen);
368 free(kcopy);
369 return r == 0 ? -1 : 0;
370}
371
372/* Revoke a key blob. Ownership of blob is transferred to the tree */
373static int
374revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, u_int len)
375{
376 struct revoked_blob *rb, *erb;
377
378 if ((rb = calloc(1, sizeof(*rb))) == NULL)
379 return -1;
380 rb->blob = blob;
381 rb->len = len;
382 erb = RB_INSERT(revoked_blob_tree, rbt, rb);
383 if (erb != NULL) {
384 free(rb->blob);
385 free(rb);
386 }
387 return 0;
388}
389
390int
391ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const Key *key)
392{
393 u_char *blob;
394 u_int len;
395
396 debug3("%s: revoke type %s", __func__, key_type(key));
397 if (plain_key_blob(key, &blob, &len) != 0)
398 return -1;
399 return revoke_blob(&krl->revoked_keys, blob, len);
400}
401
402int
403ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const Key *key)
404{
405 u_char *blob;
406 u_int len;
407
408 debug3("%s: revoke type %s by sha1", __func__, key_type(key));
409 if ((blob = key_fingerprint_raw(key, SSH_FP_SHA1, &len)) == NULL)
410 return -1;
411 return revoke_blob(&krl->revoked_sha1s, blob, len);
412}
413
414int
415ssh_krl_revoke_key(struct ssh_krl *krl, const Key *key)
416{
417 if (!key_is_cert(key))
418 return ssh_krl_revoke_key_sha1(krl, key);
419
420 if (key_cert_is_legacy(key) || key->cert->serial == 0) {
421 return ssh_krl_revoke_cert_by_key_id(krl,
422 key->cert->signature_key,
423 key->cert->key_id);
424 } else {
425 return ssh_krl_revoke_cert_by_serial(krl,
426 key->cert->signature_key,
427 key->cert->serial);
428 }
429}
430
431/*
432 * Select a copact next section type to emit in a KRL based on the
433 * current section type, the run length of contiguous revoked serial
434 * numbers and the gaps from the last and to the next revoked serial.
435 * Applies a mostly-accurate bit cost model to select the section type
436 * that will minimise the size of the resultant KRL.
437 */
438static int
439choose_next_state(int current_state, u_int64_t contig, int final,
440 u_int64_t last_gap, u_int64_t next_gap, int *force_new_section)
441{
442 int new_state;
443 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
444
445 /*
446 * Avoid unsigned overflows.
447 * The limits are high enough to avoid confusing the calculations.
448 */
449 contig = MIN(contig, 1ULL<<31);
450 last_gap = MIN(last_gap, 1ULL<<31);
451 next_gap = MIN(next_gap, 1ULL<<31);
452
453 /*
454 * Calculate the cost to switch from the current state to candidates.
455 * NB. range sections only ever contain a single range, so their
456 * switching cost is independent of the current_state.
457 */
458 cost_list = cost_bitmap = cost_bitmap_restart = 0;
459 cost_range = 8;
460 switch (current_state) {
461 case KRL_SECTION_CERT_SERIAL_LIST:
462 cost_bitmap_restart = cost_bitmap = 8 + 64;
463 break;
464 case KRL_SECTION_CERT_SERIAL_BITMAP:
465 cost_list = 8;
466 cost_bitmap_restart = 8 + 64;
467 break;
468 case KRL_SECTION_CERT_SERIAL_RANGE:
469 case 0:
470 cost_bitmap_restart = cost_bitmap = 8 + 64;
471 cost_list = 8;
472 }
473
474 /* Estimate base cost in bits of each section type */
475 cost_list += 64 * contig + (final ? 0 : 8+64);
476 cost_range += (2 * 64) + (final ? 0 : 8+64);
477 cost_bitmap += last_gap + contig + (final ? 0 : MIN(next_gap, 8+64));
478 cost_bitmap_restart += contig + (final ? 0 : MIN(next_gap, 8+64));
479
480 /* Convert to byte costs for actual comparison */
481 cost_list = (cost_list + 7) / 8;
482 cost_bitmap = (cost_bitmap + 7) / 8;
483 cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
484 cost_range = (cost_range + 7) / 8;
485
486 /* Now pick the best choice */
487 *force_new_section = 0;
488 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
489 cost = cost_bitmap;
490 if (cost_range < cost) {
491 new_state = KRL_SECTION_CERT_SERIAL_RANGE;
492 cost = cost_range;
493 }
494 if (cost_list < cost) {
495 new_state = KRL_SECTION_CERT_SERIAL_LIST;
496 cost = cost_list;
497 }
498 if (cost_bitmap_restart < cost) {
499 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
500 *force_new_section = 1;
501 cost = cost_bitmap_restart;
502 }
503 debug3("%s: contig %llu last_gap %llu next_gap %llu final %d, costs:"
504 "list %llu range %llu bitmap %llu new bitmap %llu, "
505 "selected 0x%02x%s", __func__, contig, last_gap, next_gap, final,
506 cost_list, cost_range, cost_bitmap, cost_bitmap_restart, new_state,
507 *force_new_section ? " restart" : "");
508 return new_state;
509}
510
511/* Generate a KRL_SECTION_CERTIFICATES KRL section */
512static int
513revoked_certs_generate(struct revoked_certs *rc, Buffer *buf)
514{
515 int final, force_new_sect, r = -1;
516 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
517 struct revoked_serial *rs, *nrs;
518 struct revoked_key_id *rki;
519 int next_state, state = 0;
520 Buffer sect;
521 u_char *kblob = NULL;
522 u_int klen;
523 BIGNUM *bitmap = NULL;
524
525 /* Prepare CA scope key blob if we have one supplied */
526 if (key_to_blob(rc->ca_key, &kblob, &klen) == 0)
527 return -1;
528
529 buffer_init(&sect);
530
531 /* Store the header */
532 buffer_put_string(buf, kblob, klen);
533 buffer_put_string(buf, NULL, 0); /* Reserved */
534
535 free(kblob);
536
537 /* Store the revoked serials. */
538 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
539 rs != NULL;
540 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
541 debug3("%s: serial %llu:%llu state 0x%02x", __func__,
542 rs->lo, rs->hi, state);
543
544 /* Check contiguous length and gap to next section (if any) */
545 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
546 final = nrs == NULL;
547 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
548 contig = 1 + (rs->hi - rs->lo);
549
550 /* Choose next state based on these */
551 next_state = choose_next_state(state, contig, final,
552 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
553
554 /*
555 * If the current section is a range section or has a different
556 * type to the next section, then finish it off now.
557 */
558 if (state != 0 && (force_new_sect || next_state != state ||
559 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
560 debug3("%s: finish state 0x%02x", __func__, state);
561 switch (state) {
562 case KRL_SECTION_CERT_SERIAL_LIST:
563 case KRL_SECTION_CERT_SERIAL_RANGE:
564 break;
565 case KRL_SECTION_CERT_SERIAL_BITMAP:
566 buffer_put_bignum2(&sect, bitmap);
567 BN_free(bitmap);
568 bitmap = NULL;
569 break;
570 }
571 buffer_put_char(buf, state);
572 buffer_put_string(buf,
573 buffer_ptr(&sect), buffer_len(&sect));
574 }
575
576 /* If we are starting a new section then prepare it now */
577 if (next_state != state || force_new_sect) {
578 debug3("%s: start state 0x%02x", __func__, next_state);
579 state = next_state;
580 buffer_clear(&sect);
581 switch (state) {
582 case KRL_SECTION_CERT_SERIAL_LIST:
583 case KRL_SECTION_CERT_SERIAL_RANGE:
584 break;
585 case KRL_SECTION_CERT_SERIAL_BITMAP:
586 if ((bitmap = BN_new()) == NULL)
587 goto out;
588 bitmap_start = rs->lo;
589 buffer_put_int64(&sect, bitmap_start);
590 break;
591 }
592 }
593
594 /* Perform section-specific processing */
595 switch (state) {
596 case KRL_SECTION_CERT_SERIAL_LIST:
597 for (i = 0; i < contig; i++)
598 buffer_put_int64(&sect, rs->lo + i);
599 break;
600 case KRL_SECTION_CERT_SERIAL_RANGE:
601 buffer_put_int64(&sect, rs->lo);
602 buffer_put_int64(&sect, rs->hi);
603 break;
604 case KRL_SECTION_CERT_SERIAL_BITMAP:
605 if (rs->lo - bitmap_start > INT_MAX) {
606 error("%s: insane bitmap gap", __func__);
607 goto out;
608 }
609 for (i = 0; i < contig; i++) {
610 if (BN_set_bit(bitmap,
611 rs->lo + i - bitmap_start) != 1)
612 goto out;
613 }
614 break;
615 }
616 last = rs->hi;
617 }
618 /* Flush the remaining section, if any */
619 if (state != 0) {
620 debug3("%s: serial final flush for state 0x%02x",
621 __func__, state);
622 switch (state) {
623 case KRL_SECTION_CERT_SERIAL_LIST:
624 case KRL_SECTION_CERT_SERIAL_RANGE:
625 break;
626 case KRL_SECTION_CERT_SERIAL_BITMAP:
627 buffer_put_bignum2(&sect, bitmap);
628 BN_free(bitmap);
629 bitmap = NULL;
630 break;
631 }
632 buffer_put_char(buf, state);
633 buffer_put_string(buf,
634 buffer_ptr(&sect), buffer_len(&sect));
635 }
636 debug3("%s: serial done ", __func__);
637
638 /* Now output a section for any revocations by key ID */
639 buffer_clear(&sect);
640 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
641 debug3("%s: key ID %s", __func__, rki->key_id);
642 buffer_put_cstring(&sect, rki->key_id);
643 }
644 if (buffer_len(&sect) != 0) {
645 buffer_put_char(buf, KRL_SECTION_CERT_KEY_ID);
646 buffer_put_string(buf, buffer_ptr(&sect),
647 buffer_len(&sect));
648 }
649 r = 0;
650 out:
651 if (bitmap != NULL)
652 BN_free(bitmap);
653 buffer_free(&sect);
654 return r;
655}
656
657int
658ssh_krl_to_blob(struct ssh_krl *krl, Buffer *buf, const Key **sign_keys,
659 u_int nsign_keys)
660{
661 int r = -1;
662 struct revoked_certs *rc;
663 struct revoked_blob *rb;
664 Buffer sect;
665 u_char *kblob = NULL, *sblob = NULL;
666 u_int klen, slen, i;
667
668 if (krl->generated_date == 0)
669 krl->generated_date = time(NULL);
670
671 buffer_init(&sect);
672
673 /* Store the header */
674 buffer_append(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1);
675 buffer_put_int(buf, KRL_FORMAT_VERSION);
676 buffer_put_int64(buf, krl->krl_version);
677 buffer_put_int64(buf, krl->generated_date);
678 buffer_put_int64(buf, krl->flags);
679 buffer_put_string(buf, NULL, 0);
680 buffer_put_cstring(buf, krl->comment ? krl->comment : "");
681
682 /* Store sections for revoked certificates */
683 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
684 if (revoked_certs_generate(rc, &sect) != 0)
685 goto out;
686 buffer_put_char(buf, KRL_SECTION_CERTIFICATES);
687 buffer_put_string(buf, buffer_ptr(&sect),
688 buffer_len(&sect));
689 }
690
691 /* Finally, output sections for revocations by public key/hash */
692 buffer_clear(&sect);
693 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
694 debug3("%s: key len %u ", __func__, rb->len);
695 buffer_put_string(&sect, rb->blob, rb->len);
696 }
697 if (buffer_len(&sect) != 0) {
698 buffer_put_char(buf, KRL_SECTION_EXPLICIT_KEY);
699 buffer_put_string(buf, buffer_ptr(&sect),
700 buffer_len(&sect));
701 }
702 buffer_clear(&sect);
703 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
704 debug3("%s: hash len %u ", __func__, rb->len);
705 buffer_put_string(&sect, rb->blob, rb->len);
706 }
707 if (buffer_len(&sect) != 0) {
708 buffer_put_char(buf, KRL_SECTION_FINGERPRINT_SHA1);
709 buffer_put_string(buf, buffer_ptr(&sect),
710 buffer_len(&sect));
711 }
712
713 for (i = 0; i < nsign_keys; i++) {
714 if (key_to_blob(sign_keys[i], &kblob, &klen) == 0)
715 goto out;
716
717 debug3("%s: signature key len %u", __func__, klen);
718 buffer_put_char(buf, KRL_SECTION_SIGNATURE);
719 buffer_put_string(buf, kblob, klen);
720
721 if (key_sign(sign_keys[i], &sblob, &slen,
722 buffer_ptr(buf), buffer_len(buf)) == -1)
723 goto out;
724 debug3("%s: signature sig len %u", __func__, slen);
725 buffer_put_string(buf, sblob, slen);
726 }
727
728 r = 0;
729 out:
730 free(kblob);
731 free(sblob);
732 buffer_free(&sect);
733 return r;
734}
735
736static void
737format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
738{
739 time_t t;
740 struct tm *tm;
741
742 t = timestamp;
743 tm = localtime(&t);
744 *ts = '\0';
745 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
746}
747
748static int
749parse_revoked_certs(Buffer *buf, struct ssh_krl *krl)
750{
751 int ret = -1, nbits;
752 u_char type, *blob;
753 u_int blen;
754 Buffer subsect;
755 u_int64_t serial, serial_lo, serial_hi;
756 BIGNUM *bitmap = NULL;
757 char *key_id = NULL;
758 Key *ca_key = NULL;
759
760 buffer_init(&subsect);
761
762 if ((blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL ||
763 buffer_get_string_ptr_ret(buf, NULL) == NULL) { /* reserved */
764 error("%s: buffer error", __func__);
765 goto out;
766 }
767 if ((ca_key = key_from_blob(blob, blen)) == NULL)
768 goto out;
769
770 while (buffer_len(buf) > 0) {
771 if (buffer_get_char_ret(&type, buf) != 0 ||
772 (blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL) {
773 error("%s: buffer error", __func__);
774 goto out;
775 }
776 buffer_clear(&subsect);
777 buffer_append(&subsect, blob, blen);
778 debug3("%s: subsection type 0x%02x", __func__, type);
779 /* buffer_dump(&subsect); */
780
781 switch (type) {
782 case KRL_SECTION_CERT_SERIAL_LIST:
783 while (buffer_len(&subsect) > 0) {
784 if (buffer_get_int64_ret(&serial,
785 &subsect) != 0) {
786 error("%s: buffer error", __func__);
787 goto out;
788 }
789 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,
790 serial) != 0) {
791 error("%s: update failed", __func__);
792 goto out;
793 }
794 }
795 break;
796 case KRL_SECTION_CERT_SERIAL_RANGE:
797 if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||
798 buffer_get_int64_ret(&serial_hi, &subsect) != 0) {
799 error("%s: buffer error", __func__);
800 goto out;
801 }
802 if (ssh_krl_revoke_cert_by_serial_range(krl, ca_key,
803 serial_lo, serial_hi) != 0) {
804 error("%s: update failed", __func__);
805 goto out;
806 }
807 break;
808 case KRL_SECTION_CERT_SERIAL_BITMAP:
809 if ((bitmap = BN_new()) == NULL) {
810 error("%s: BN_new", __func__);
811 goto out;
812 }
813 if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||
814 buffer_get_bignum2_ret(&subsect, bitmap) != 0) {
815 error("%s: buffer error", __func__);
816 goto out;
817 }
818 if ((nbits = BN_num_bits(bitmap)) < 0) {
819 error("%s: bitmap bits < 0", __func__);
820 goto out;
821 }
822 for (serial = 0; serial < (u_int)nbits; serial++) {
823 if (serial > 0 && serial_lo + serial == 0) {
824 error("%s: bitmap wraps u64", __func__);
825 goto out;
826 }
827 if (!BN_is_bit_set(bitmap, serial))
828 continue;
829 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,
830 serial_lo + serial) != 0) {
831 error("%s: update failed", __func__);
832 goto out;
833 }
834 }
835 BN_free(bitmap);
836 bitmap = NULL;
837 break;
838 case KRL_SECTION_CERT_KEY_ID:
839 while (buffer_len(&subsect) > 0) {
840 if ((key_id = buffer_get_cstring_ret(&subsect,
841 NULL)) == NULL) {
842 error("%s: buffer error", __func__);
843 goto out;
844 }
845 if (ssh_krl_revoke_cert_by_key_id(krl, ca_key,
846 key_id) != 0) {
847 error("%s: update failed", __func__);
848 goto out;
849 }
850 free(key_id);
851 key_id = NULL;
852 }
853 break;
854 default:
855 error("Unsupported KRL certificate section %u", type);
856 goto out;
857 }
858 if (buffer_len(&subsect) > 0) {
859 error("KRL certificate section contains unparsed data");
860 goto out;
861 }
862 }
863
864 ret = 0;
865 out:
866 if (ca_key != NULL)
867 key_free(ca_key);
868 if (bitmap != NULL)
869 BN_free(bitmap);
870 free(key_id);
871 buffer_free(&subsect);
872 return ret;
873}
874
875
876/* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
877int
878ssh_krl_from_blob(Buffer *buf, struct ssh_krl **krlp,
879 const Key **sign_ca_keys, u_int nsign_ca_keys)
880{
881 Buffer copy, sect;
882 struct ssh_krl *krl;
883 char timestamp[64];
884 int ret = -1, r, sig_seen;
885 Key *key = NULL, **ca_used = NULL;
886 u_char type, *blob;
887 u_int i, j, sig_off, sects_off, blen, format_version, nca_used = 0;
888
889 *krlp = NULL;
890 if (buffer_len(buf) < sizeof(KRL_MAGIC) - 1 ||
891 memcmp(buffer_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
892 debug3("%s: not a KRL", __func__);
893 /*
894 * Return success but a NULL *krlp here to signal that the
895 * file might be a simple list of keys.
896 */
897 return 0;
898 }
899
900 /* Take a copy of the KRL buffer so we can verify its signature later */
901 buffer_init(&copy);
902 buffer_append(&copy, buffer_ptr(buf), buffer_len(buf));
903
904 buffer_init(&sect);
905 buffer_consume(&copy, sizeof(KRL_MAGIC) - 1);
906
907 if ((krl = ssh_krl_init()) == NULL) {
908 error("%s: alloc failed", __func__);
909 goto out;
910 }
911
912 if (buffer_get_int_ret(&format_version, &copy) != 0) {
913 error("%s: KRL truncated", __func__);
914 goto out;
915 }
916 if (format_version != KRL_FORMAT_VERSION) {
917 error("%s: KRL unsupported format version %u",
918 __func__, format_version);
919 goto out;
920 }
921 if (buffer_get_int64_ret(&krl->krl_version, &copy) != 0 ||
922 buffer_get_int64_ret(&krl->generated_date, &copy) != 0 ||
923 buffer_get_int64_ret(&krl->flags, &copy) != 0 ||
924 buffer_get_string_ptr_ret(&copy, NULL) == NULL || /* reserved */
925 (krl->comment = buffer_get_cstring_ret(&copy, NULL)) == NULL) {
926 error("%s: buffer error", __func__);
927 goto out;
928 }
929
930 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
931 debug("KRL version %llu generated at %s%s%s", krl->krl_version,
932 timestamp, *krl->comment ? ": " : "", krl->comment);
933
934 /*
935 * 1st pass: verify signatures, if any. This is done to avoid
936 * detailed parsing of data whose provenance is unverified.
937 */
938 sig_seen = 0;
939 sects_off = buffer_len(buf) - buffer_len(&copy);
940 while (buffer_len(&copy) > 0) {
941 if (buffer_get_char_ret(&type, &copy) != 0 ||
942 (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
943 error("%s: buffer error", __func__);
944 goto out;
945 }
946 debug3("%s: first pass, section 0x%02x", __func__, type);
947 if (type != KRL_SECTION_SIGNATURE) {
948 if (sig_seen) {
949 error("KRL contains non-signature section "
950 "after signature");
951 goto out;
952 }
953 /* Not interested for now. */
954 continue;
955 }
956 sig_seen = 1;
957 /* First string component is the signing key */
958 if ((key = key_from_blob(blob, blen)) == NULL) {
959 error("%s: invalid signature key", __func__);
960 goto out;
961 }
962 sig_off = buffer_len(buf) - buffer_len(&copy);
963 /* Second string component is the signature itself */
964 if ((blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
965 error("%s: buffer error", __func__);
966 goto out;
967 }
968 /* Check signature over entire KRL up to this point */
969 if (key_verify(key, blob, blen,
970 buffer_ptr(buf), buffer_len(buf) - sig_off) == -1) {
971 error("bad signaure on KRL");
972 goto out;
973 }
974 /* Check if this key has already signed this KRL */
975 for (i = 0; i < nca_used; i++) {
976 if (key_equal(ca_used[i], key)) {
977 error("KRL signed more than once with "
978 "the same key");
979 goto out;
980 }
981 }
982 /* Record keys used to sign the KRL */
983 ca_used = xrealloc(ca_used, nca_used + 1, sizeof(*ca_used));
984 ca_used[nca_used++] = key;
985 key = NULL;
986 break;
987 }
988
989 /*
990 * 2nd pass: parse and load the KRL, skipping the header to the point
991 * where the section start.
992 */
993 buffer_append(&copy, (u_char*)buffer_ptr(buf) + sects_off,
994 buffer_len(buf) - sects_off);
995 while (buffer_len(&copy) > 0) {
996 if (buffer_get_char_ret(&type, &copy) != 0 ||
997 (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
998 error("%s: buffer error", __func__);
999 goto out;
1000 }
1001 debug3("%s: second pass, section 0x%02x", __func__, type);
1002 buffer_clear(&sect);
1003 buffer_append(&sect, blob, blen);
1004
1005 switch (type) {
1006 case KRL_SECTION_CERTIFICATES:
1007 if ((r = parse_revoked_certs(&sect, krl)) != 0)
1008 goto out;
1009 break;
1010 case KRL_SECTION_EXPLICIT_KEY:
1011 case KRL_SECTION_FINGERPRINT_SHA1:
1012 while (buffer_len(&sect) > 0) {
1013 if ((blob = buffer_get_string_ret(&sect,
1014 &blen)) == NULL) {
1015 error("%s: buffer error", __func__);
1016 goto out;
1017 }
1018 if (type == KRL_SECTION_FINGERPRINT_SHA1 &&
1019 blen != 20) {
1020 error("%s: bad SHA1 length", __func__);
1021 goto out;
1022 }
1023 if (revoke_blob(
1024 type == KRL_SECTION_EXPLICIT_KEY ?
1025 &krl->revoked_keys : &krl->revoked_sha1s,
1026 blob, blen) != 0)
1027 goto out; /* revoke_blob frees blob */
1028 }
1029 break;
1030 case KRL_SECTION_SIGNATURE:
1031 /* Handled above, but still need to stay in synch */
1032 buffer_clear(&sect);
1033 if ((blob = buffer_get_string_ptr_ret(&copy,
1034 &blen)) == NULL) {
1035 error("%s: buffer error", __func__);
1036 goto out;
1037 }
1038 break;
1039 default:
1040 error("Unsupported KRL section %u", type);
1041 goto out;
1042 }
1043 if (buffer_len(&sect) > 0) {
1044 error("KRL section contains unparsed data");
1045 goto out;
1046 }
1047 }
1048
1049 /* Check that the key(s) used to sign the KRL weren't revoked */
1050 sig_seen = 0;
1051 for (i = 0; i < nca_used; i++) {
1052 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1053 sig_seen = 1;
1054 else {
1055 key_free(ca_used[i]);
1056 ca_used[i] = NULL;
1057 }
1058 }
1059 if (nca_used && !sig_seen) {
1060 error("All keys used to sign KRL were revoked");
1061 goto out;
1062 }
1063
1064 /* If we have CA keys, then verify that one was used to sign the KRL */
1065 if (sig_seen && nsign_ca_keys != 0) {
1066 sig_seen = 0;
1067 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1068 for (j = 0; j < nca_used; j++) {
1069 if (ca_used[j] == NULL)
1070 continue;
1071 if (key_equal(ca_used[j], sign_ca_keys[i])) {
1072 sig_seen = 1;
1073 break;
1074 }
1075 }
1076 }
1077 if (!sig_seen) {
1078 error("KRL not signed with any trusted key");
1079 goto out;
1080 }
1081 }
1082
1083 *krlp = krl;
1084 ret = 0;
1085 out:
1086 if (ret != 0)
1087 ssh_krl_free(krl);
1088 for (i = 0; i < nca_used; i++) {
1089 if (ca_used[i] != NULL)
1090 key_free(ca_used[i]);
1091 }
1092 free(ca_used);
1093 if (key != NULL)
1094 key_free(key);
1095 buffer_free(&copy);
1096 buffer_free(&sect);
1097 return ret;
1098}
1099
1100/* Checks whether a given key/cert is revoked. Does not check its CA */
1101static int
1102is_key_revoked(struct ssh_krl *krl, const Key *key)
1103{
1104 struct revoked_blob rb, *erb;
1105 struct revoked_serial rs, *ers;
1106 struct revoked_key_id rki, *erki;
1107 struct revoked_certs *rc;
1108
1109 /* Check explicitly revoked hashes first */
1110 bzero(&rb, sizeof(rb));
1111 if ((rb.blob = key_fingerprint_raw(key, SSH_FP_SHA1, &rb.len)) == NULL)
1112 return -1;
1113 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1114 free(rb.blob);
1115 if (erb != NULL) {
1116 debug("%s: revoked by key SHA1", __func__);
1117 return -1;
1118 }
1119
1120 /* Next, explicit keys */
1121 bzero(&rb, sizeof(rb));
1122 if (plain_key_blob(key, &rb.blob, &rb.len) != 0)
1123 return -1;
1124 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1125 free(rb.blob);
1126 if (erb != NULL) {
1127 debug("%s: revoked by explicit key", __func__);
1128 return -1;
1129 }
1130
1131 if (!key_is_cert(key))
1132 return 0;
1133
1134 /* Check cert revocation */
1135 if (revoked_certs_for_ca_key(krl, key->cert->signature_key,
1136 &rc, 0) != 0)
1137 return -1;
1138 if (rc == NULL)
1139 return 0; /* No entry for this CA */
1140
1141 /* Check revocation by cert key ID */
1142 bzero(&rki, sizeof(rki));
1143 rki.key_id = key->cert->key_id;
1144 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1145 if (erki != NULL) {
1146 debug("%s: revoked by key ID", __func__);
1147 return -1;
1148 }
1149
1150 /*
1151 * Legacy cert formats lack serial numbers. Zero serials numbers
1152 * are ignored (it's the default when the CA doesn't specify one).
1153 */
1154 if (key_cert_is_legacy(key) || key->cert->serial == 0)
1155 return 0;
1156
1157 bzero(&rs, sizeof(rs));
1158 rs.lo = rs.hi = key->cert->serial;
1159 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1160 if (ers != NULL) {
1161 KRL_DBG(("%s: %llu matched %llu:%llu", __func__,
1162 key->cert->serial, ers->lo, ers->hi));
1163 debug("%s: revoked by serial", __func__);
1164 return -1;
1165 }
1166 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
1167
1168 return 0;
1169}
1170
1171int
1172ssh_krl_check_key(struct ssh_krl *krl, const Key *key)
1173{
1174 int r;
1175
1176 debug2("%s: checking key", __func__);
1177 if ((r = is_key_revoked(krl, key)) != 0)
1178 return r;
1179 if (key_is_cert(key)) {
1180 debug2("%s: checking CA key", __func__);
1181 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1182 return r;
1183 }
1184 debug3("%s: key okay", __func__);
1185 return 0;
1186}
1187
1188/* Returns 0 on success, -1 on error or key revoked, -2 if path is not a KRL */
1189int
1190ssh_krl_file_contains_key(const char *path, const Key *key)
1191{
1192 Buffer krlbuf;
1193 struct ssh_krl *krl;
1194 int revoked, fd;
1195
1196 if (path == NULL)
1197 return 0;
1198
1199 if ((fd = open(path, O_RDONLY)) == -1) {
1200 error("open %s: %s", path, strerror(errno));
1201 error("Revoked keys file not accessible - refusing public key "
1202 "authentication");
1203 return -1;
1204 }
1205 buffer_init(&krlbuf);
1206 if (!key_load_file(fd, path, &krlbuf)) {
1207 close(fd);
1208 buffer_free(&krlbuf);
1209 error("Revoked keys file not readable - refusing public key "
1210 "authentication");
1211 return -1;
1212 }
1213 close(fd);
1214 if (ssh_krl_from_blob(&krlbuf, &krl, NULL, 0) != 0) {
1215 buffer_free(&krlbuf);
1216 error("Invalid KRL, refusing public key "
1217 "authentication");
1218 return -1;
1219 }
1220 buffer_free(&krlbuf);
1221 if (krl == NULL) {
1222 debug3("%s: %s is not a KRL file", __func__, path);
1223 return -2;
1224 }
1225 debug2("%s: checking KRL %s", __func__, path);
1226 revoked = ssh_krl_check_key(krl, key) != 0;
1227 ssh_krl_free(krl);
1228 return revoked ? -1 : 0;
1229}