Import OpenSSH-6.7p1.
[dragonfly.git] / crypto / openssh / krl.c
blobeb31df90fdc10702bd25d000449c7cb0837aa1b7
1 /*
2 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
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.
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.
17 /* $OpenBSD: krl.c,v 1.17 2014/06/24 01:13:21 djm Exp $ */
19 #include "includes.h"
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <openbsd-compat/sys-tree.h>
24 #include <openbsd-compat/sys-queue.h>
26 #include <errno.h>
27 #include <fcntl.h>
28 #include <limits.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
33 #include "buffer.h"
34 #include "key.h"
35 #include "authfile.h"
36 #include "misc.h"
37 #include "log.h"
38 #include "xmalloc.h"
40 #include "krl.h"
42 /* #define DEBUG_KRL */
43 #ifdef DEBUG_KRL
44 # define KRL_DBG(x) debug3 x
45 #else
46 # define KRL_DBG(x)
47 #endif
50 * Trees of revoked serial numbers, key IDs and keys. This allows
51 * quick searching, querying and producing lists in canonical order.
54 /* Tree of serial numbers. XXX make smarter: really need a real sparse bitmap */
55 struct revoked_serial {
56 u_int64_t lo, hi;
57 RB_ENTRY(revoked_serial) tree_entry;
59 static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
60 RB_HEAD(revoked_serial_tree, revoked_serial);
61 RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp);
63 /* Tree of key IDs */
64 struct revoked_key_id {
65 char *key_id;
66 RB_ENTRY(revoked_key_id) tree_entry;
68 static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
69 RB_HEAD(revoked_key_id_tree, revoked_key_id);
70 RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp);
72 /* Tree of blobs (used for keys and fingerprints) */
73 struct revoked_blob {
74 u_char *blob;
75 u_int len;
76 RB_ENTRY(revoked_blob) tree_entry;
78 static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
79 RB_HEAD(revoked_blob_tree, revoked_blob);
80 RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp);
82 /* Tracks revoked certs for a single CA */
83 struct 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;
89 TAILQ_HEAD(revoked_certs_list, revoked_certs);
91 struct 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;
101 /* Return equal if a and b overlap */
102 static int
103 serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
105 if (a->hi >= b->lo && a->lo <= b->hi)
106 return 0;
107 return a->lo < b->lo ? -1 : 1;
110 static int
111 key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
113 return strcmp(a->key_id, b->key_id);
116 static int
117 blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
119 int r;
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);
129 struct ssh_krl *
130 ssh_krl_init(void)
132 struct ssh_krl *krl;
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;
142 static void
143 revoked_certs_free(struct revoked_certs *rc)
145 struct revoked_serial *rs, *trs;
146 struct revoked_key_id *rki, *trki;
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);
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);
157 if (rc->ca_key != NULL)
158 key_free(rc->ca_key);
161 void
162 ssh_krl_free(struct ssh_krl *krl)
164 struct revoked_blob *rb, *trb;
165 struct revoked_certs *rc, *trc;
167 if (krl == NULL)
168 return;
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);
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);
181 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
182 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
183 revoked_certs_free(rc);
187 void
188 ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
190 krl->krl_version = version;
193 void
194 ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
196 free(krl->comment);
197 if ((krl->comment = strdup(comment)) == NULL)
198 fatal("%s: strdup", __func__);
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.
205 static int
206 revoked_certs_for_ca_key(struct ssh_krl *krl, const Key *ca_key,
207 struct revoked_certs **rcp, int allow_create)
209 struct revoked_certs *rc;
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;
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;
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;
235 static int
236 insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
238 struct revoked_serial rs, *ers, *crs, *irs;
240 KRL_DBG(("%s: insert %llu:%llu", __func__, lo, hi));
241 memset(&rs, 0, 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;
257 ers = irs;
258 } else {
259 KRL_DBG(("%s: overlap found %llu:%llu", __func__,
260 ers->lo, ers->hi));
262 * The inserted entry overlaps an existing one. Grow the
263 * existing entry.
265 if (ers->lo > lo)
266 ers->lo = lo;
267 if (ers->hi < hi)
268 ers->hi = hi;
271 * The inserted or revised range might overlap or abut adjacent ones;
272 * coalesce as necessary.
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));
286 RB_REMOVE(revoked_serial_tree, rt, crs);
287 free(crs);
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));
300 RB_REMOVE(revoked_serial_tree, rt, crs);
301 free(crs);
303 KRL_DBG(("%s: done, final %llu:%llu", __func__, ers->lo, ers->hi));
304 return 0;
308 ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const Key *ca_key,
309 u_int64_t serial)
311 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
315 ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl, const Key *ca_key,
316 u_int64_t lo, u_int64_t hi)
318 struct revoked_certs *rc;
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);
328 ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const Key *ca_key,
329 const char *key_id)
331 struct revoked_key_id *rki, *erki;
332 struct revoked_certs *rc;
334 if (revoked_certs_for_ca_key(krl, ca_key, &rc, 1) != 0)
335 return -1;
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__);
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);
348 return 0;
351 /* Convert "key" to a public key blob without any certificate information */
352 static int
353 plain_key_blob(const Key *key, u_char **blob, u_int *blen)
355 Key *kcopy;
356 int r;
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;
367 r = key_to_blob(kcopy, blob, blen);
368 free(kcopy);
369 return r;
372 /* Revoke a key blob. Ownership of blob is transferred to the tree */
373 static int
374 revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, u_int len)
376 struct revoked_blob *rb, *erb;
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);
387 return 0;
391 ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const Key *key)
393 u_char *blob;
394 u_int len;
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);
403 ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const Key *key)
405 u_char *blob;
406 u_int len;
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);
415 ssh_krl_revoke_key(struct ssh_krl *krl, const Key *key)
417 if (!key_is_cert(key))
418 return ssh_krl_revoke_key_sha1(krl, key);
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);
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.
438 static int
439 choose_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)
442 int new_state;
443 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
446 * Avoid unsigned overflows.
447 * The limits are high enough to avoid confusing the calculations.
449 contig = MIN(contig, 1ULL<<31);
450 last_gap = MIN(last_gap, 1ULL<<31);
451 next_gap = MIN(next_gap, 1ULL<<31);
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.
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;
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));
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;
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;
494 if (cost_list < cost) {
495 new_state = KRL_SECTION_CERT_SERIAL_LIST;
496 cost = cost_list;
498 if (cost_bitmap_restart < cost) {
499 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
500 *force_new_section = 1;
501 cost = cost_bitmap_restart;
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__, (long long unsigned)contig,
506 (long long unsigned)last_gap, (long long unsigned)next_gap, final,
507 (long long unsigned)cost_list, (long long unsigned)cost_range,
508 (long long unsigned)cost_bitmap,
509 (long long unsigned)cost_bitmap_restart, new_state,
510 *force_new_section ? " restart" : "");
511 return new_state;
514 /* Generate a KRL_SECTION_CERTIFICATES KRL section */
515 static int
516 revoked_certs_generate(struct revoked_certs *rc, Buffer *buf)
518 int final, force_new_sect, r = -1;
519 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
520 struct revoked_serial *rs, *nrs;
521 struct revoked_key_id *rki;
522 int next_state, state = 0;
523 Buffer sect;
524 u_char *kblob = NULL;
525 u_int klen;
526 BIGNUM *bitmap = NULL;
528 /* Prepare CA scope key blob if we have one supplied */
529 if (key_to_blob(rc->ca_key, &kblob, &klen) == 0)
530 return -1;
532 buffer_init(&sect);
534 /* Store the header */
535 buffer_put_string(buf, kblob, klen);
536 buffer_put_string(buf, NULL, 0); /* Reserved */
538 free(kblob);
540 /* Store the revoked serials. */
541 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
542 rs != NULL;
543 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
544 debug3("%s: serial %llu:%llu state 0x%02x", __func__,
545 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
546 state);
548 /* Check contiguous length and gap to next section (if any) */
549 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
550 final = nrs == NULL;
551 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
552 contig = 1 + (rs->hi - rs->lo);
554 /* Choose next state based on these */
555 next_state = choose_next_state(state, contig, final,
556 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
559 * If the current section is a range section or has a different
560 * type to the next section, then finish it off now.
562 if (state != 0 && (force_new_sect || next_state != state ||
563 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
564 debug3("%s: finish state 0x%02x", __func__, state);
565 switch (state) {
566 case KRL_SECTION_CERT_SERIAL_LIST:
567 case KRL_SECTION_CERT_SERIAL_RANGE:
568 break;
569 case KRL_SECTION_CERT_SERIAL_BITMAP:
570 buffer_put_bignum2(&sect, bitmap);
571 BN_free(bitmap);
572 bitmap = NULL;
573 break;
575 buffer_put_char(buf, state);
576 buffer_put_string(buf,
577 buffer_ptr(&sect), buffer_len(&sect));
578 buffer_clear(&sect);
581 /* If we are starting a new section then prepare it now */
582 if (next_state != state || force_new_sect) {
583 debug3("%s: start state 0x%02x", __func__, next_state);
584 state = next_state;
585 buffer_clear(&sect);
586 switch (state) {
587 case KRL_SECTION_CERT_SERIAL_LIST:
588 case KRL_SECTION_CERT_SERIAL_RANGE:
589 break;
590 case KRL_SECTION_CERT_SERIAL_BITMAP:
591 if ((bitmap = BN_new()) == NULL)
592 goto out;
593 bitmap_start = rs->lo;
594 buffer_put_int64(&sect, bitmap_start);
595 break;
599 /* Perform section-specific processing */
600 switch (state) {
601 case KRL_SECTION_CERT_SERIAL_LIST:
602 for (i = 0; i < contig; i++)
603 buffer_put_int64(&sect, rs->lo + i);
604 break;
605 case KRL_SECTION_CERT_SERIAL_RANGE:
606 buffer_put_int64(&sect, rs->lo);
607 buffer_put_int64(&sect, rs->hi);
608 break;
609 case KRL_SECTION_CERT_SERIAL_BITMAP:
610 if (rs->lo - bitmap_start > INT_MAX) {
611 error("%s: insane bitmap gap", __func__);
612 goto out;
614 for (i = 0; i < contig; i++) {
615 if (BN_set_bit(bitmap,
616 rs->lo + i - bitmap_start) != 1)
617 goto out;
619 break;
621 last = rs->hi;
623 /* Flush the remaining section, if any */
624 if (state != 0) {
625 debug3("%s: serial final flush for state 0x%02x",
626 __func__, state);
627 switch (state) {
628 case KRL_SECTION_CERT_SERIAL_LIST:
629 case KRL_SECTION_CERT_SERIAL_RANGE:
630 break;
631 case KRL_SECTION_CERT_SERIAL_BITMAP:
632 buffer_put_bignum2(&sect, bitmap);
633 BN_free(bitmap);
634 bitmap = NULL;
635 break;
637 buffer_put_char(buf, state);
638 buffer_put_string(buf,
639 buffer_ptr(&sect), buffer_len(&sect));
641 debug3("%s: serial done ", __func__);
643 /* Now output a section for any revocations by key ID */
644 buffer_clear(&sect);
645 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
646 debug3("%s: key ID %s", __func__, rki->key_id);
647 buffer_put_cstring(&sect, rki->key_id);
649 if (buffer_len(&sect) != 0) {
650 buffer_put_char(buf, KRL_SECTION_CERT_KEY_ID);
651 buffer_put_string(buf, buffer_ptr(&sect),
652 buffer_len(&sect));
654 r = 0;
655 out:
656 if (bitmap != NULL)
657 BN_free(bitmap);
658 buffer_free(&sect);
659 return r;
663 ssh_krl_to_blob(struct ssh_krl *krl, Buffer *buf, const Key **sign_keys,
664 u_int nsign_keys)
666 int r = -1;
667 struct revoked_certs *rc;
668 struct revoked_blob *rb;
669 Buffer sect;
670 u_char *kblob = NULL, *sblob = NULL;
671 u_int klen, slen, i;
673 if (krl->generated_date == 0)
674 krl->generated_date = time(NULL);
676 buffer_init(&sect);
678 /* Store the header */
679 buffer_append(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1);
680 buffer_put_int(buf, KRL_FORMAT_VERSION);
681 buffer_put_int64(buf, krl->krl_version);
682 buffer_put_int64(buf, krl->generated_date);
683 buffer_put_int64(buf, krl->flags);
684 buffer_put_string(buf, NULL, 0);
685 buffer_put_cstring(buf, krl->comment ? krl->comment : "");
687 /* Store sections for revoked certificates */
688 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
689 if (revoked_certs_generate(rc, &sect) != 0)
690 goto out;
691 buffer_put_char(buf, KRL_SECTION_CERTIFICATES);
692 buffer_put_string(buf, buffer_ptr(&sect),
693 buffer_len(&sect));
696 /* Finally, output sections for revocations by public key/hash */
697 buffer_clear(&sect);
698 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
699 debug3("%s: key len %u ", __func__, rb->len);
700 buffer_put_string(&sect, rb->blob, rb->len);
702 if (buffer_len(&sect) != 0) {
703 buffer_put_char(buf, KRL_SECTION_EXPLICIT_KEY);
704 buffer_put_string(buf, buffer_ptr(&sect),
705 buffer_len(&sect));
707 buffer_clear(&sect);
708 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
709 debug3("%s: hash len %u ", __func__, rb->len);
710 buffer_put_string(&sect, rb->blob, rb->len);
712 if (buffer_len(&sect) != 0) {
713 buffer_put_char(buf, KRL_SECTION_FINGERPRINT_SHA1);
714 buffer_put_string(buf, buffer_ptr(&sect),
715 buffer_len(&sect));
718 for (i = 0; i < nsign_keys; i++) {
719 if (key_to_blob(sign_keys[i], &kblob, &klen) == 0)
720 goto out;
722 debug3("%s: signature key len %u", __func__, klen);
723 buffer_put_char(buf, KRL_SECTION_SIGNATURE);
724 buffer_put_string(buf, kblob, klen);
726 if (key_sign(sign_keys[i], &sblob, &slen,
727 buffer_ptr(buf), buffer_len(buf)) == -1)
728 goto out;
729 debug3("%s: signature sig len %u", __func__, slen);
730 buffer_put_string(buf, sblob, slen);
733 r = 0;
734 out:
735 free(kblob);
736 free(sblob);
737 buffer_free(&sect);
738 return r;
741 static void
742 format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
744 time_t t;
745 struct tm *tm;
747 t = timestamp;
748 tm = localtime(&t);
749 *ts = '\0';
750 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
753 static int
754 parse_revoked_certs(Buffer *buf, struct ssh_krl *krl)
756 int ret = -1, nbits;
757 u_char type;
758 const u_char *blob;
759 u_int blen;
760 Buffer subsect;
761 u_int64_t serial, serial_lo, serial_hi;
762 BIGNUM *bitmap = NULL;
763 char *key_id = NULL;
764 Key *ca_key = NULL;
766 buffer_init(&subsect);
768 if ((blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL ||
769 buffer_get_string_ptr_ret(buf, NULL) == NULL) { /* reserved */
770 error("%s: buffer error", __func__);
771 goto out;
773 if ((ca_key = key_from_blob(blob, blen)) == NULL)
774 goto out;
776 while (buffer_len(buf) > 0) {
777 if (buffer_get_char_ret(&type, buf) != 0 ||
778 (blob = buffer_get_string_ptr_ret(buf, &blen)) == NULL) {
779 error("%s: buffer error", __func__);
780 goto out;
782 buffer_clear(&subsect);
783 buffer_append(&subsect, blob, blen);
784 debug3("%s: subsection type 0x%02x", __func__, type);
785 /* buffer_dump(&subsect); */
787 switch (type) {
788 case KRL_SECTION_CERT_SERIAL_LIST:
789 while (buffer_len(&subsect) > 0) {
790 if (buffer_get_int64_ret(&serial,
791 &subsect) != 0) {
792 error("%s: buffer error", __func__);
793 goto out;
795 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,
796 serial) != 0) {
797 error("%s: update failed", __func__);
798 goto out;
801 break;
802 case KRL_SECTION_CERT_SERIAL_RANGE:
803 if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||
804 buffer_get_int64_ret(&serial_hi, &subsect) != 0) {
805 error("%s: buffer error", __func__);
806 goto out;
808 if (ssh_krl_revoke_cert_by_serial_range(krl, ca_key,
809 serial_lo, serial_hi) != 0) {
810 error("%s: update failed", __func__);
811 goto out;
813 break;
814 case KRL_SECTION_CERT_SERIAL_BITMAP:
815 if ((bitmap = BN_new()) == NULL) {
816 error("%s: BN_new", __func__);
817 goto out;
819 if (buffer_get_int64_ret(&serial_lo, &subsect) != 0 ||
820 buffer_get_bignum2_ret(&subsect, bitmap) != 0) {
821 error("%s: buffer error", __func__);
822 goto out;
824 if ((nbits = BN_num_bits(bitmap)) < 0) {
825 error("%s: bitmap bits < 0", __func__);
826 goto out;
828 for (serial = 0; serial < (u_int)nbits; serial++) {
829 if (serial > 0 && serial_lo + serial == 0) {
830 error("%s: bitmap wraps u64", __func__);
831 goto out;
833 if (!BN_is_bit_set(bitmap, serial))
834 continue;
835 if (ssh_krl_revoke_cert_by_serial(krl, ca_key,
836 serial_lo + serial) != 0) {
837 error("%s: update failed", __func__);
838 goto out;
841 BN_free(bitmap);
842 bitmap = NULL;
843 break;
844 case KRL_SECTION_CERT_KEY_ID:
845 while (buffer_len(&subsect) > 0) {
846 if ((key_id = buffer_get_cstring_ret(&subsect,
847 NULL)) == NULL) {
848 error("%s: buffer error", __func__);
849 goto out;
851 if (ssh_krl_revoke_cert_by_key_id(krl, ca_key,
852 key_id) != 0) {
853 error("%s: update failed", __func__);
854 goto out;
856 free(key_id);
857 key_id = NULL;
859 break;
860 default:
861 error("Unsupported KRL certificate section %u", type);
862 goto out;
864 if (buffer_len(&subsect) > 0) {
865 error("KRL certificate section contains unparsed data");
866 goto out;
870 ret = 0;
871 out:
872 if (ca_key != NULL)
873 key_free(ca_key);
874 if (bitmap != NULL)
875 BN_free(bitmap);
876 free(key_id);
877 buffer_free(&subsect);
878 return ret;
882 /* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
884 ssh_krl_from_blob(Buffer *buf, struct ssh_krl **krlp,
885 const Key **sign_ca_keys, u_int nsign_ca_keys)
887 Buffer copy, sect;
888 struct ssh_krl *krl;
889 char timestamp[64];
890 int ret = -1, r, sig_seen;
891 Key *key = NULL, **ca_used = NULL;
892 u_char type, *rdata = NULL;
893 const u_char *blob;
894 u_int i, j, sig_off, sects_off, rlen, blen, format_version, nca_used;
896 nca_used = 0;
897 *krlp = NULL;
898 if (buffer_len(buf) < sizeof(KRL_MAGIC) - 1 ||
899 memcmp(buffer_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
900 debug3("%s: not a KRL", __func__);
902 * Return success but a NULL *krlp here to signal that the
903 * file might be a simple list of keys.
905 return 0;
908 /* Take a copy of the KRL buffer so we can verify its signature later */
909 buffer_init(&copy);
910 buffer_append(&copy, buffer_ptr(buf), buffer_len(buf));
912 buffer_init(&sect);
913 buffer_consume(&copy, sizeof(KRL_MAGIC) - 1);
915 if ((krl = ssh_krl_init()) == NULL) {
916 error("%s: alloc failed", __func__);
917 goto out;
920 if (buffer_get_int_ret(&format_version, &copy) != 0) {
921 error("%s: KRL truncated", __func__);
922 goto out;
924 if (format_version != KRL_FORMAT_VERSION) {
925 error("%s: KRL unsupported format version %u",
926 __func__, format_version);
927 goto out;
929 if (buffer_get_int64_ret(&krl->krl_version, &copy) != 0 ||
930 buffer_get_int64_ret(&krl->generated_date, &copy) != 0 ||
931 buffer_get_int64_ret(&krl->flags, &copy) != 0 ||
932 buffer_get_string_ptr_ret(&copy, NULL) == NULL || /* reserved */
933 (krl->comment = buffer_get_cstring_ret(&copy, NULL)) == NULL) {
934 error("%s: buffer error", __func__);
935 goto out;
938 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
939 debug("KRL version %llu generated at %s%s%s",
940 (long long unsigned)krl->krl_version, timestamp,
941 *krl->comment ? ": " : "", krl->comment);
944 * 1st pass: verify signatures, if any. This is done to avoid
945 * detailed parsing of data whose provenance is unverified.
947 sig_seen = 0;
948 sects_off = buffer_len(buf) - buffer_len(&copy);
949 while (buffer_len(&copy) > 0) {
950 if (buffer_get_char_ret(&type, &copy) != 0 ||
951 (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
952 error("%s: buffer error", __func__);
953 goto out;
955 debug3("%s: first pass, section 0x%02x", __func__, type);
956 if (type != KRL_SECTION_SIGNATURE) {
957 if (sig_seen) {
958 error("KRL contains non-signature section "
959 "after signature");
960 goto out;
962 /* Not interested for now. */
963 continue;
965 sig_seen = 1;
966 /* First string component is the signing key */
967 if ((key = key_from_blob(blob, blen)) == NULL) {
968 error("%s: invalid signature key", __func__);
969 goto out;
971 sig_off = buffer_len(buf) - buffer_len(&copy);
972 /* Second string component is the signature itself */
973 if ((blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
974 error("%s: buffer error", __func__);
975 goto out;
977 /* Check signature over entire KRL up to this point */
978 if (key_verify(key, blob, blen,
979 buffer_ptr(buf), buffer_len(buf) - sig_off) != 1) {
980 error("bad signaure on KRL");
981 goto out;
983 /* Check if this key has already signed this KRL */
984 for (i = 0; i < nca_used; i++) {
985 if (key_equal(ca_used[i], key)) {
986 error("KRL signed more than once with "
987 "the same key");
988 goto out;
991 /* Record keys used to sign the KRL */
992 ca_used = xrealloc(ca_used, nca_used + 1, sizeof(*ca_used));
993 ca_used[nca_used++] = key;
994 key = NULL;
995 break;
999 * 2nd pass: parse and load the KRL, skipping the header to the point
1000 * where the section start.
1002 buffer_append(&copy, (u_char*)buffer_ptr(buf) + sects_off,
1003 buffer_len(buf) - sects_off);
1004 while (buffer_len(&copy) > 0) {
1005 if (buffer_get_char_ret(&type, &copy) != 0 ||
1006 (blob = buffer_get_string_ptr_ret(&copy, &blen)) == NULL) {
1007 error("%s: buffer error", __func__);
1008 goto out;
1010 debug3("%s: second pass, section 0x%02x", __func__, type);
1011 buffer_clear(&sect);
1012 buffer_append(&sect, blob, blen);
1014 switch (type) {
1015 case KRL_SECTION_CERTIFICATES:
1016 if ((r = parse_revoked_certs(&sect, krl)) != 0)
1017 goto out;
1018 break;
1019 case KRL_SECTION_EXPLICIT_KEY:
1020 case KRL_SECTION_FINGERPRINT_SHA1:
1021 while (buffer_len(&sect) > 0) {
1022 if ((rdata = buffer_get_string_ret(&sect,
1023 &rlen)) == NULL) {
1024 error("%s: buffer error", __func__);
1025 goto out;
1027 if (type == KRL_SECTION_FINGERPRINT_SHA1 &&
1028 rlen != 20) {
1029 error("%s: bad SHA1 length", __func__);
1030 goto out;
1032 if (revoke_blob(
1033 type == KRL_SECTION_EXPLICIT_KEY ?
1034 &krl->revoked_keys : &krl->revoked_sha1s,
1035 rdata, rlen) != 0)
1036 goto out;
1037 rdata = NULL; /* revoke_blob frees blob */
1039 break;
1040 case KRL_SECTION_SIGNATURE:
1041 /* Handled above, but still need to stay in synch */
1042 buffer_clear(&sect);
1043 if ((blob = buffer_get_string_ptr_ret(&copy,
1044 &blen)) == NULL) {
1045 error("%s: buffer error", __func__);
1046 goto out;
1048 break;
1049 default:
1050 error("Unsupported KRL section %u", type);
1051 goto out;
1053 if (buffer_len(&sect) > 0) {
1054 error("KRL section contains unparsed data");
1055 goto out;
1059 /* Check that the key(s) used to sign the KRL weren't revoked */
1060 sig_seen = 0;
1061 for (i = 0; i < nca_used; i++) {
1062 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1063 sig_seen = 1;
1064 else {
1065 key_free(ca_used[i]);
1066 ca_used[i] = NULL;
1069 if (nca_used && !sig_seen) {
1070 error("All keys used to sign KRL were revoked");
1071 goto out;
1074 /* If we have CA keys, then verify that one was used to sign the KRL */
1075 if (sig_seen && nsign_ca_keys != 0) {
1076 sig_seen = 0;
1077 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1078 for (j = 0; j < nca_used; j++) {
1079 if (ca_used[j] == NULL)
1080 continue;
1081 if (key_equal(ca_used[j], sign_ca_keys[i])) {
1082 sig_seen = 1;
1083 break;
1087 if (!sig_seen) {
1088 error("KRL not signed with any trusted key");
1089 goto out;
1093 *krlp = krl;
1094 ret = 0;
1095 out:
1096 if (ret != 0)
1097 ssh_krl_free(krl);
1098 for (i = 0; i < nca_used; i++) {
1099 if (ca_used[i] != NULL)
1100 key_free(ca_used[i]);
1102 free(ca_used);
1103 free(rdata);
1104 if (key != NULL)
1105 key_free(key);
1106 buffer_free(&copy);
1107 buffer_free(&sect);
1108 return ret;
1111 /* Checks whether a given key/cert is revoked. Does not check its CA */
1112 static int
1113 is_key_revoked(struct ssh_krl *krl, const Key *key)
1115 struct revoked_blob rb, *erb;
1116 struct revoked_serial rs, *ers;
1117 struct revoked_key_id rki, *erki;
1118 struct revoked_certs *rc;
1120 /* Check explicitly revoked hashes first */
1121 memset(&rb, 0, sizeof(rb));
1122 if ((rb.blob = key_fingerprint_raw(key, SSH_FP_SHA1, &rb.len)) == NULL)
1123 return -1;
1124 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1125 free(rb.blob);
1126 if (erb != NULL) {
1127 debug("%s: revoked by key SHA1", __func__);
1128 return -1;
1131 /* Next, explicit keys */
1132 memset(&rb, 0, sizeof(rb));
1133 if (plain_key_blob(key, &rb.blob, &rb.len) < 0)
1134 return -1;
1135 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1136 free(rb.blob);
1137 if (erb != NULL) {
1138 debug("%s: revoked by explicit key", __func__);
1139 return -1;
1142 if (!key_is_cert(key))
1143 return 0;
1145 /* Check cert revocation */
1146 if (revoked_certs_for_ca_key(krl, key->cert->signature_key,
1147 &rc, 0) != 0)
1148 return -1;
1149 if (rc == NULL)
1150 return 0; /* No entry for this CA */
1152 /* Check revocation by cert key ID */
1153 memset(&rki, 0, sizeof(rki));
1154 rki.key_id = key->cert->key_id;
1155 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1156 if (erki != NULL) {
1157 debug("%s: revoked by key ID", __func__);
1158 return -1;
1162 * Legacy cert formats lack serial numbers. Zero serials numbers
1163 * are ignored (it's the default when the CA doesn't specify one).
1165 if (key_cert_is_legacy(key) || key->cert->serial == 0)
1166 return 0;
1168 memset(&rs, 0, sizeof(rs));
1169 rs.lo = rs.hi = key->cert->serial;
1170 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1171 if (ers != NULL) {
1172 KRL_DBG(("%s: %llu matched %llu:%llu", __func__,
1173 key->cert->serial, ers->lo, ers->hi));
1174 debug("%s: revoked by serial", __func__);
1175 return -1;
1177 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
1179 return 0;
1183 ssh_krl_check_key(struct ssh_krl *krl, const Key *key)
1185 int r;
1187 debug2("%s: checking key", __func__);
1188 if ((r = is_key_revoked(krl, key)) != 0)
1189 return r;
1190 if (key_is_cert(key)) {
1191 debug2("%s: checking CA key", __func__);
1192 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1193 return r;
1195 debug3("%s: key okay", __func__);
1196 return 0;
1199 /* Returns 0 on success, -1 on error or key revoked, -2 if path is not a KRL */
1201 ssh_krl_file_contains_key(const char *path, const Key *key)
1203 Buffer krlbuf;
1204 struct ssh_krl *krl;
1205 int revoked, fd;
1207 if (path == NULL)
1208 return 0;
1210 if ((fd = open(path, O_RDONLY)) == -1) {
1211 error("open %s: %s", path, strerror(errno));
1212 error("Revoked keys file not accessible - refusing public key "
1213 "authentication");
1214 return -1;
1216 buffer_init(&krlbuf);
1217 if (!key_load_file(fd, path, &krlbuf)) {
1218 close(fd);
1219 buffer_free(&krlbuf);
1220 error("Revoked keys file not readable - refusing public key "
1221 "authentication");
1222 return -1;
1224 close(fd);
1225 if (ssh_krl_from_blob(&krlbuf, &krl, NULL, 0) != 0) {
1226 buffer_free(&krlbuf);
1227 error("Invalid KRL, refusing public key "
1228 "authentication");
1229 return -1;
1231 buffer_free(&krlbuf);
1232 if (krl == NULL) {
1233 debug3("%s: %s is not a KRL file", __func__, path);
1234 return -2;
1236 debug2("%s: checking KRL %s", __func__, path);
1237 revoked = ssh_krl_check_key(krl, key) != 0;
1238 ssh_krl_free(krl);
1239 return revoked ? -1 : 0;