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.42 2018/09/12 01:21:34 djm Exp $ */
21 #include <sys/types.h>
22 #include <openbsd-compat/sys-tree.h>
23 #include <openbsd-compat/sys-queue.h>
43 /* #define DEBUG_KRL */
45 # define KRL_DBG(x) debug3 x
51 * Trees of revoked serial numbers, key IDs and keys. This allows
52 * quick searching, querying and producing lists in canonical order.
55 /* Tree of serial numbers. XXX make smarter: really need a real sparse bitmap */
56 struct revoked_serial {
58 RB_ENTRY(revoked_serial) tree_entry;
60 static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
61 RB_HEAD(revoked_serial_tree, revoked_serial);
62 RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp);
65 struct revoked_key_id {
67 RB_ENTRY(revoked_key_id) tree_entry;
69 static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
70 RB_HEAD(revoked_key_id_tree, revoked_key_id);
71 RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp);
73 /* Tree of blobs (used for keys and fingerprints) */
77 RB_ENTRY(revoked_blob) tree_entry;
79 static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
80 RB_HEAD(revoked_blob_tree, revoked_blob);
81 RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp);
83 /* Tracks revoked certs for a single CA */
84 struct revoked_certs {
85 struct sshkey *ca_key;
86 struct revoked_serial_tree revoked_serials;
87 struct revoked_key_id_tree revoked_key_ids;
88 TAILQ_ENTRY(revoked_certs) entry;
90 TAILQ_HEAD(revoked_certs_list, revoked_certs);
93 u_int64_t krl_version;
94 u_int64_t generated_date;
97 struct revoked_blob_tree revoked_keys;
98 struct revoked_blob_tree revoked_sha1s;
99 struct revoked_blob_tree revoked_sha256s;
100 struct revoked_certs_list revoked_certs;
103 /* Return equal if a and b overlap */
105 serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
107 if (a->hi >= b->lo && a->lo <= b->hi)
109 return a->lo < b->lo ? -1 : 1;
113 key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
115 return strcmp(a->key_id, b->key_id);
119 blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
123 if (a->len != b->len) {
124 if ((r = memcmp(a->blob, b->blob, MINIMUM(a->len, b->len))) != 0)
126 return a->len > b->len ? 1 : -1;
128 return memcmp(a->blob, b->blob, a->len);
136 if ((krl = calloc(1, sizeof(*krl))) == NULL)
138 RB_INIT(&krl->revoked_keys);
139 RB_INIT(&krl->revoked_sha1s);
140 RB_INIT(&krl->revoked_sha256s);
141 TAILQ_INIT(&krl->revoked_certs);
146 revoked_certs_free(struct revoked_certs *rc)
148 struct revoked_serial *rs, *trs;
149 struct revoked_key_id *rki, *trki;
151 RB_FOREACH_SAFE(rs, revoked_serial_tree, &rc->revoked_serials, trs) {
152 RB_REMOVE(revoked_serial_tree, &rc->revoked_serials, rs);
155 RB_FOREACH_SAFE(rki, revoked_key_id_tree, &rc->revoked_key_ids, trki) {
156 RB_REMOVE(revoked_key_id_tree, &rc->revoked_key_ids, rki);
160 sshkey_free(rc->ca_key);
164 ssh_krl_free(struct ssh_krl *krl)
166 struct revoked_blob *rb, *trb;
167 struct revoked_certs *rc, *trc;
173 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
174 RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
178 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
179 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
183 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha256s, trb) {
184 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha256s, rb);
188 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
189 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
190 revoked_certs_free(rc);
195 ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
197 krl->krl_version = version;
201 ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
204 if ((krl->comment = strdup(comment)) == NULL)
205 return SSH_ERR_ALLOC_FAIL;
210 * Find the revoked_certs struct for a CA key. If allow_create is set then
211 * create a new one in the tree if one did not exist already.
214 revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
215 struct revoked_certs **rcp, int allow_create)
217 struct revoked_certs *rc;
221 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
222 if ((ca_key == NULL && rc->ca_key == NULL) ||
223 sshkey_equal(rc->ca_key, ca_key)) {
230 /* If this CA doesn't exist in the list then add it now */
231 if ((rc = calloc(1, sizeof(*rc))) == NULL)
232 return SSH_ERR_ALLOC_FAIL;
235 else if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
239 RB_INIT(&rc->revoked_serials);
240 RB_INIT(&rc->revoked_key_ids);
241 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
242 KRL_DBG(("%s: new CA %s", __func__,
243 ca_key == NULL ? "*" : sshkey_type(ca_key)));
249 insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
251 struct revoked_serial rs, *ers, *crs, *irs;
253 KRL_DBG(("%s: insert %llu:%llu", __func__, lo, hi));
254 memset(&rs, 0, sizeof(rs));
257 ers = RB_NFIND(revoked_serial_tree, rt, &rs);
258 if (ers == NULL || serial_cmp(ers, &rs) != 0) {
259 /* No entry matches. Just insert */
260 if ((irs = malloc(sizeof(rs))) == NULL)
261 return SSH_ERR_ALLOC_FAIL;
262 memcpy(irs, &rs, sizeof(*irs));
263 ers = RB_INSERT(revoked_serial_tree, rt, irs);
265 KRL_DBG(("%s: bad: ers != NULL", __func__));
266 /* Shouldn't happen */
268 return SSH_ERR_INTERNAL_ERROR;
272 KRL_DBG(("%s: overlap found %llu:%llu", __func__,
275 * The inserted entry overlaps an existing one. Grow the
285 * The inserted or revised range might overlap or abut adjacent ones;
286 * coalesce as necessary.
289 /* Check predecessors */
290 while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
291 KRL_DBG(("%s: pred %llu:%llu", __func__, crs->lo, crs->hi));
292 if (ers->lo != 0 && crs->hi < ers->lo - 1)
294 /* This entry overlaps. */
295 if (crs->lo < ers->lo) {
297 KRL_DBG(("%s: pred extend %llu:%llu", __func__,
300 RB_REMOVE(revoked_serial_tree, rt, crs);
303 /* Check successors */
304 while ((crs = RB_NEXT(revoked_serial_tree, rt, ers)) != NULL) {
305 KRL_DBG(("%s: succ %llu:%llu", __func__, crs->lo, crs->hi));
306 if (ers->hi != (u_int64_t)-1 && crs->lo > ers->hi + 1)
308 /* This entry overlaps. */
309 if (crs->hi > ers->hi) {
311 KRL_DBG(("%s: succ extend %llu:%llu", __func__,
314 RB_REMOVE(revoked_serial_tree, rt, crs);
317 KRL_DBG(("%s: done, final %llu:%llu", __func__, ers->lo, ers->hi));
322 ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
325 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
329 ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl,
330 const struct sshkey *ca_key, u_int64_t lo, u_int64_t hi)
332 struct revoked_certs *rc;
335 if (lo > hi || lo == 0)
336 return SSH_ERR_INVALID_ARGUMENT;
337 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
339 return insert_serial_range(&rc->revoked_serials, lo, hi);
343 ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
346 struct revoked_key_id *rki, *erki;
347 struct revoked_certs *rc;
350 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
353 KRL_DBG(("%s: revoke %s", __func__, key_id));
354 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
355 (rki->key_id = strdup(key_id)) == NULL) {
357 return SSH_ERR_ALLOC_FAIL;
359 erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
367 /* Convert "key" to a public key blob without any certificate information */
369 plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
371 struct sshkey *kcopy;
374 if ((r = sshkey_from_private(key, &kcopy)) != 0)
376 if (sshkey_is_cert(kcopy)) {
377 if ((r = sshkey_drop_cert(kcopy)) != 0) {
382 r = sshkey_to_blob(kcopy, blob, blen);
387 /* Revoke a key blob. Ownership of blob is transferred to the tree */
389 revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
391 struct revoked_blob *rb, *erb;
393 if ((rb = calloc(1, sizeof(*rb))) == NULL)
394 return SSH_ERR_ALLOC_FAIL;
397 erb = RB_INSERT(revoked_blob_tree, rbt, rb);
406 ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
412 debug3("%s: revoke type %s", __func__, sshkey_type(key));
413 if ((r = plain_key_blob(key, &blob, &len)) != 0)
415 return revoke_blob(&krl->revoked_keys, blob, len);
419 revoke_by_hash(struct revoked_blob_tree *target, const u_char *p, size_t len)
424 /* need to copy hash, as revoke_blob steals ownership */
425 if ((blob = malloc(len)) == NULL)
426 return SSH_ERR_SYSTEM_ERROR;
427 memcpy(blob, p, len);
428 if ((r = revoke_blob(target, blob, len)) != 0) {
436 ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const u_char *p, size_t len)
438 debug3("%s: revoke by sha1", __func__);
440 return SSH_ERR_INVALID_FORMAT;
441 return revoke_by_hash(&krl->revoked_sha1s, p, len);
445 ssh_krl_revoke_key_sha256(struct ssh_krl *krl, const u_char *p, size_t len)
447 debug3("%s: revoke by sha256", __func__);
449 return SSH_ERR_INVALID_FORMAT;
450 return revoke_by_hash(&krl->revoked_sha256s, p, len);
454 ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
456 /* XXX replace with SHA256? */
457 if (!sshkey_is_cert(key))
458 return ssh_krl_revoke_key_explicit(krl, key);
460 if (key->cert->serial == 0) {
461 return ssh_krl_revoke_cert_by_key_id(krl,
462 key->cert->signature_key,
465 return ssh_krl_revoke_cert_by_serial(krl,
466 key->cert->signature_key,
472 * Select the most compact section type to emit next in a KRL based on
473 * the current section type, the run length of contiguous revoked serial
474 * numbers and the gaps from the last and to the next revoked serial.
475 * Applies a mostly-accurate bit cost model to select the section type
476 * that will minimise the size of the resultant KRL.
479 choose_next_state(int current_state, u_int64_t contig, int final,
480 u_int64_t last_gap, u_int64_t next_gap, int *force_new_section)
483 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
486 * Avoid unsigned overflows.
487 * The limits are high enough to avoid confusing the calculations.
489 contig = MINIMUM(contig, 1ULL<<31);
490 last_gap = MINIMUM(last_gap, 1ULL<<31);
491 next_gap = MINIMUM(next_gap, 1ULL<<31);
494 * Calculate the cost to switch from the current state to candidates.
495 * NB. range sections only ever contain a single range, so their
496 * switching cost is independent of the current_state.
498 cost_list = cost_bitmap = cost_bitmap_restart = 0;
500 switch (current_state) {
501 case KRL_SECTION_CERT_SERIAL_LIST:
502 cost_bitmap_restart = cost_bitmap = 8 + 64;
504 case KRL_SECTION_CERT_SERIAL_BITMAP:
506 cost_bitmap_restart = 8 + 64;
508 case KRL_SECTION_CERT_SERIAL_RANGE:
510 cost_bitmap_restart = cost_bitmap = 8 + 64;
514 /* Estimate base cost in bits of each section type */
515 cost_list += 64 * contig + (final ? 0 : 8+64);
516 cost_range += (2 * 64) + (final ? 0 : 8+64);
517 cost_bitmap += last_gap + contig + (final ? 0 : MINIMUM(next_gap, 8+64));
518 cost_bitmap_restart += contig + (final ? 0 : MINIMUM(next_gap, 8+64));
520 /* Convert to byte costs for actual comparison */
521 cost_list = (cost_list + 7) / 8;
522 cost_bitmap = (cost_bitmap + 7) / 8;
523 cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
524 cost_range = (cost_range + 7) / 8;
526 /* Now pick the best choice */
527 *force_new_section = 0;
528 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
530 if (cost_range < cost) {
531 new_state = KRL_SECTION_CERT_SERIAL_RANGE;
534 if (cost_list < cost) {
535 new_state = KRL_SECTION_CERT_SERIAL_LIST;
538 if (cost_bitmap_restart < cost) {
539 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
540 *force_new_section = 1;
541 cost = cost_bitmap_restart;
543 KRL_DBG(("%s: contig %llu last_gap %llu next_gap %llu final %d, costs:"
544 "list %llu range %llu bitmap %llu new bitmap %llu, "
545 "selected 0x%02x%s", __func__, (long long unsigned)contig,
546 (long long unsigned)last_gap, (long long unsigned)next_gap, final,
547 (long long unsigned)cost_list, (long long unsigned)cost_range,
548 (long long unsigned)cost_bitmap,
549 (long long unsigned)cost_bitmap_restart, new_state,
550 *force_new_section ? " restart" : ""));
555 put_bitmap(struct sshbuf *buf, struct bitmap *bitmap)
561 len = bitmap_nbytes(bitmap);
562 if ((blob = malloc(len)) == NULL)
563 return SSH_ERR_ALLOC_FAIL;
564 if (bitmap_to_string(bitmap, blob, len) != 0) {
566 return SSH_ERR_INTERNAL_ERROR;
568 r = sshbuf_put_bignum2_bytes(buf, blob, len);
573 /* Generate a KRL_SECTION_CERTIFICATES KRL section */
575 revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
577 int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
578 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
579 struct revoked_serial *rs, *nrs;
580 struct revoked_key_id *rki;
581 int next_state, state = 0;
583 struct bitmap *bitmap = NULL;
585 if ((sect = sshbuf_new()) == NULL)
586 return SSH_ERR_ALLOC_FAIL;
588 /* Store the header: optional CA scope key, reserved */
589 if (rc->ca_key == NULL) {
590 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
593 if ((r = sshkey_puts(rc->ca_key, buf)) != 0)
596 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
599 /* Store the revoked serials. */
600 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
602 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
603 KRL_DBG(("%s: serial %llu:%llu state 0x%02x", __func__,
604 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
607 /* Check contiguous length and gap to next section (if any) */
608 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
610 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
611 contig = 1 + (rs->hi - rs->lo);
613 /* Choose next state based on these */
614 next_state = choose_next_state(state, contig, final,
615 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
618 * If the current section is a range section or has a different
619 * type to the next section, then finish it off now.
621 if (state != 0 && (force_new_sect || next_state != state ||
622 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
623 KRL_DBG(("%s: finish state 0x%02x", __func__, state));
625 case KRL_SECTION_CERT_SERIAL_LIST:
626 case KRL_SECTION_CERT_SERIAL_RANGE:
628 case KRL_SECTION_CERT_SERIAL_BITMAP:
629 if ((r = put_bitmap(sect, bitmap)) != 0)
635 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
636 (r = sshbuf_put_stringb(buf, sect)) != 0)
641 /* If we are starting a new section then prepare it now */
642 if (next_state != state || force_new_sect) {
643 KRL_DBG(("%s: start state 0x%02x", __func__,
648 case KRL_SECTION_CERT_SERIAL_LIST:
649 case KRL_SECTION_CERT_SERIAL_RANGE:
651 case KRL_SECTION_CERT_SERIAL_BITMAP:
652 if ((bitmap = bitmap_new()) == NULL) {
653 r = SSH_ERR_ALLOC_FAIL;
656 bitmap_start = rs->lo;
657 if ((r = sshbuf_put_u64(sect,
664 /* Perform section-specific processing */
666 case KRL_SECTION_CERT_SERIAL_LIST:
667 for (i = 0; i < contig; i++) {
668 if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
672 case KRL_SECTION_CERT_SERIAL_RANGE:
673 if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
674 (r = sshbuf_put_u64(sect, rs->hi)) != 0)
677 case KRL_SECTION_CERT_SERIAL_BITMAP:
678 if (rs->lo - bitmap_start > INT_MAX) {
679 error("%s: insane bitmap gap", __func__);
682 for (i = 0; i < contig; i++) {
683 if (bitmap_set_bit(bitmap,
684 rs->lo + i - bitmap_start) != 0) {
685 r = SSH_ERR_ALLOC_FAIL;
693 /* Flush the remaining section, if any */
695 KRL_DBG(("%s: serial final flush for state 0x%02x",
698 case KRL_SECTION_CERT_SERIAL_LIST:
699 case KRL_SECTION_CERT_SERIAL_RANGE:
701 case KRL_SECTION_CERT_SERIAL_BITMAP:
702 if ((r = put_bitmap(sect, bitmap)) != 0)
708 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
709 (r = sshbuf_put_stringb(buf, sect)) != 0)
712 KRL_DBG(("%s: serial done ", __func__));
714 /* Now output a section for any revocations by key ID */
716 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
717 KRL_DBG(("%s: key ID %s", __func__, rki->key_id));
718 if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
721 if (sshbuf_len(sect) != 0) {
722 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
723 (r = sshbuf_put_stringb(buf, sect)) != 0)
734 ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
735 const struct sshkey **sign_keys, u_int nsign_keys)
737 int r = SSH_ERR_INTERNAL_ERROR;
738 struct revoked_certs *rc;
739 struct revoked_blob *rb;
741 u_char *sblob = NULL;
744 if (krl->generated_date == 0)
745 krl->generated_date = time(NULL);
747 if ((sect = sshbuf_new()) == NULL)
748 return SSH_ERR_ALLOC_FAIL;
750 /* Store the header */
751 if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
752 (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
753 (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
754 (r = sshbuf_put_u64(buf, krl->generated_date)) != 0 ||
755 (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
756 (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
757 (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
760 /* Store sections for revoked certificates */
761 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
763 if ((r = revoked_certs_generate(rc, sect)) != 0)
765 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
766 (r = sshbuf_put_stringb(buf, sect)) != 0)
770 /* Finally, output sections for revocations by public key/hash */
772 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
773 KRL_DBG(("%s: key len %zu ", __func__, rb->len));
774 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
777 if (sshbuf_len(sect) != 0) {
778 if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
779 (r = sshbuf_put_stringb(buf, sect)) != 0)
783 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
784 KRL_DBG(("%s: hash len %zu ", __func__, rb->len));
785 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
788 if (sshbuf_len(sect) != 0) {
789 if ((r = sshbuf_put_u8(buf,
790 KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
791 (r = sshbuf_put_stringb(buf, sect)) != 0)
795 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
796 KRL_DBG(("%s: hash len %zu ", __func__, rb->len));
797 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
800 if (sshbuf_len(sect) != 0) {
801 if ((r = sshbuf_put_u8(buf,
802 KRL_SECTION_FINGERPRINT_SHA256)) != 0 ||
803 (r = sshbuf_put_stringb(buf, sect)) != 0)
807 for (i = 0; i < nsign_keys; i++) {
808 KRL_DBG(("%s: signature key %s", __func__,
809 sshkey_ssh_name(sign_keys[i])));
810 if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
811 (r = sshkey_puts(sign_keys[i], buf)) != 0)
814 if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
815 sshbuf_ptr(buf), sshbuf_len(buf), NULL, 0)) != 0)
817 KRL_DBG(("%s: signature sig len %zu", __func__, slen));
818 if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
830 format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
838 strlcpy(ts, "<INVALID>", nts);
841 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
846 parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
848 int r = SSH_ERR_INTERNAL_ERROR;
852 struct sshbuf *subsect = NULL;
853 u_int64_t serial, serial_lo, serial_hi;
854 struct bitmap *bitmap = NULL;
856 struct sshkey *ca_key = NULL;
858 if ((subsect = sshbuf_new()) == NULL)
859 return SSH_ERR_ALLOC_FAIL;
861 /* Header: key, reserved */
862 if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
863 (r = sshbuf_skip_string(buf)) != 0)
865 if (blen != 0 && (r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
868 while (sshbuf_len(buf) > 0) {
869 sshbuf_free(subsect);
871 if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
872 (r = sshbuf_froms(buf, &subsect)) != 0)
874 KRL_DBG(("%s: subsection type 0x%02x", __func__, type));
875 /* sshbuf_dump(subsect, stderr); */
878 case KRL_SECTION_CERT_SERIAL_LIST:
879 while (sshbuf_len(subsect) > 0) {
880 if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
882 if ((r = ssh_krl_revoke_cert_by_serial(krl,
883 ca_key, serial)) != 0)
887 case KRL_SECTION_CERT_SERIAL_RANGE:
888 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
889 (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
891 if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
892 ca_key, serial_lo, serial_hi)) != 0)
895 case KRL_SECTION_CERT_SERIAL_BITMAP:
896 if ((bitmap = bitmap_new()) == NULL) {
897 r = SSH_ERR_ALLOC_FAIL;
900 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
901 (r = sshbuf_get_bignum2_bytes_direct(subsect,
904 if (bitmap_from_string(bitmap, blob, blen) != 0) {
905 r = SSH_ERR_INVALID_FORMAT;
908 nbits = bitmap_nbits(bitmap);
909 for (serial = 0; serial < (u_int64_t)nbits; serial++) {
910 if (serial > 0 && serial_lo + serial == 0) {
911 error("%s: bitmap wraps u64", __func__);
912 r = SSH_ERR_INVALID_FORMAT;
915 if (!bitmap_test_bit(bitmap, serial))
917 if ((r = ssh_krl_revoke_cert_by_serial(krl,
918 ca_key, serial_lo + serial)) != 0)
924 case KRL_SECTION_CERT_KEY_ID:
925 while (sshbuf_len(subsect) > 0) {
926 if ((r = sshbuf_get_cstring(subsect,
927 &key_id, NULL)) != 0)
929 if ((r = ssh_krl_revoke_cert_by_key_id(krl,
930 ca_key, key_id)) != 0)
937 error("Unsupported KRL certificate section %u", type);
938 r = SSH_ERR_INVALID_FORMAT;
941 if (sshbuf_len(subsect) > 0) {
942 error("KRL certificate section contains unparsed data");
943 r = SSH_ERR_INVALID_FORMAT;
954 sshbuf_free(subsect);
959 blob_section(struct sshbuf *sect, struct revoked_blob_tree *target_tree,
962 u_char *rdata = NULL;
966 while (sshbuf_len(sect) > 0) {
967 if ((r = sshbuf_get_string(sect, &rdata, &rlen)) != 0)
969 if (expected_len != 0 && rlen != expected_len) {
970 error("%s: bad length", __func__);
972 return SSH_ERR_INVALID_FORMAT;
974 if ((r = revoke_blob(target_tree, rdata, rlen)) != 0) {
982 /* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
984 ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
985 const struct sshkey **sign_ca_keys, size_t nsign_ca_keys)
987 struct sshbuf *copy = NULL, *sect = NULL;
988 struct ssh_krl *krl = NULL;
990 int r = SSH_ERR_INTERNAL_ERROR, sig_seen;
991 struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
994 size_t i, j, sig_off, sects_off, blen, nca_used;
995 u_int format_version;
999 if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
1000 memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
1001 debug3("%s: not a KRL", __func__);
1002 return SSH_ERR_KRL_BAD_MAGIC;
1005 /* Take a copy of the KRL buffer so we can verify its signature later */
1006 if ((copy = sshbuf_fromb(buf)) == NULL) {
1007 r = SSH_ERR_ALLOC_FAIL;
1010 if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0)
1013 if ((krl = ssh_krl_init()) == NULL) {
1014 error("%s: alloc failed", __func__);
1018 if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
1020 if (format_version != KRL_FORMAT_VERSION) {
1021 r = SSH_ERR_INVALID_FORMAT;
1024 if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
1025 (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
1026 (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
1027 (r = sshbuf_skip_string(copy)) != 0 ||
1028 (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0)
1031 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1032 debug("KRL version %llu generated at %s%s%s",
1033 (long long unsigned)krl->krl_version, timestamp,
1034 *krl->comment ? ": " : "", krl->comment);
1037 * 1st pass: verify signatures, if any. This is done to avoid
1038 * detailed parsing of data whose provenance is unverified.
1041 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1042 /* Shouldn't happen */
1043 r = SSH_ERR_INTERNAL_ERROR;
1046 sects_off = sshbuf_len(buf) - sshbuf_len(copy);
1047 while (sshbuf_len(copy) > 0) {
1048 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1049 (r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0)
1051 KRL_DBG(("%s: first pass, section 0x%02x", __func__, type));
1052 if (type != KRL_SECTION_SIGNATURE) {
1054 error("KRL contains non-signature section "
1056 r = SSH_ERR_INVALID_FORMAT;
1059 /* Not interested for now. */
1063 /* First string component is the signing key */
1064 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1065 r = SSH_ERR_INVALID_FORMAT;
1068 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1069 /* Shouldn't happen */
1070 r = SSH_ERR_INTERNAL_ERROR;
1073 sig_off = sshbuf_len(buf) - sshbuf_len(copy);
1074 /* Second string component is the signature itself */
1075 if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
1076 r = SSH_ERR_INVALID_FORMAT;
1079 /* Check signature over entire KRL up to this point */
1080 if ((r = sshkey_verify(key, blob, blen,
1081 sshbuf_ptr(buf), sig_off, NULL, 0)) != 0)
1083 /* Check if this key has already signed this KRL */
1084 for (i = 0; i < nca_used; i++) {
1085 if (sshkey_equal(ca_used[i], key)) {
1086 error("KRL signed more than once with "
1088 r = SSH_ERR_INVALID_FORMAT;
1092 /* Record keys used to sign the KRL */
1093 tmp_ca_used = recallocarray(ca_used, nca_used, nca_used + 1,
1095 if (tmp_ca_used == NULL) {
1096 r = SSH_ERR_ALLOC_FAIL;
1099 ca_used = tmp_ca_used;
1100 ca_used[nca_used++] = key;
1104 if (sshbuf_len(copy) != 0) {
1105 /* Shouldn't happen */
1106 r = SSH_ERR_INTERNAL_ERROR;
1111 * 2nd pass: parse and load the KRL, skipping the header to the point
1112 * where the section start.
1115 if ((copy = sshbuf_fromb(buf)) == NULL) {
1116 r = SSH_ERR_ALLOC_FAIL;
1119 if ((r = sshbuf_consume(copy, sects_off)) != 0)
1121 while (sshbuf_len(copy) > 0) {
1124 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1125 (r = sshbuf_froms(copy, §)) != 0)
1127 KRL_DBG(("%s: second pass, section 0x%02x", __func__, type));
1130 case KRL_SECTION_CERTIFICATES:
1131 if ((r = parse_revoked_certs(sect, krl)) != 0)
1134 case KRL_SECTION_EXPLICIT_KEY:
1135 if ((r = blob_section(sect,
1136 &krl->revoked_keys, 0)) != 0)
1139 case KRL_SECTION_FINGERPRINT_SHA1:
1140 if ((r = blob_section(sect,
1141 &krl->revoked_sha1s, 20)) != 0)
1144 case KRL_SECTION_FINGERPRINT_SHA256:
1145 if ((r = blob_section(sect,
1146 &krl->revoked_sha256s, 32)) != 0)
1149 case KRL_SECTION_SIGNATURE:
1150 /* Handled above, but still need to stay in synch */
1153 if ((r = sshbuf_skip_string(copy)) != 0)
1157 error("Unsupported KRL section %u", type);
1158 r = SSH_ERR_INVALID_FORMAT;
1161 if (sect != NULL && sshbuf_len(sect) > 0) {
1162 error("KRL section contains unparsed data");
1163 r = SSH_ERR_INVALID_FORMAT;
1168 /* Check that the key(s) used to sign the KRL weren't revoked */
1170 for (i = 0; i < nca_used; i++) {
1171 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1174 sshkey_free(ca_used[i]);
1178 if (nca_used && !sig_seen) {
1179 error("All keys used to sign KRL were revoked");
1180 r = SSH_ERR_KEY_REVOKED;
1184 /* If we have CA keys, then verify that one was used to sign the KRL */
1185 if (sig_seen && nsign_ca_keys != 0) {
1187 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1188 for (j = 0; j < nca_used; j++) {
1189 if (ca_used[j] == NULL)
1191 if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
1198 r = SSH_ERR_SIGNATURE_INVALID;
1199 error("KRL not signed with any trusted key");
1209 for (i = 0; i < nca_used; i++)
1210 sshkey_free(ca_used[i]);
1218 /* Checks certificate serial number and key ID revocation */
1220 is_cert_revoked(const struct sshkey *key, struct revoked_certs *rc)
1222 struct revoked_serial rs, *ers;
1223 struct revoked_key_id rki, *erki;
1225 /* Check revocation by cert key ID */
1226 memset(&rki, 0, sizeof(rki));
1227 rki.key_id = key->cert->key_id;
1228 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1230 KRL_DBG(("%s: revoked by key ID", __func__));
1231 return SSH_ERR_KEY_REVOKED;
1235 * Zero serials numbers are ignored (it's the default when the
1236 * CA doesn't specify one).
1238 if (key->cert->serial == 0)
1241 memset(&rs, 0, sizeof(rs));
1242 rs.lo = rs.hi = key->cert->serial;
1243 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1245 KRL_DBG(("%s: revoked serial %llu matched %llu:%llu", __func__,
1246 key->cert->serial, ers->lo, ers->hi));
1247 return SSH_ERR_KEY_REVOKED;
1252 /* Checks whether a given key/cert is revoked. Does not check its CA */
1254 is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
1256 struct revoked_blob rb, *erb;
1257 struct revoked_certs *rc;
1260 /* Check explicitly revoked hashes first */
1261 memset(&rb, 0, sizeof(rb));
1262 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
1263 &rb.blob, &rb.len)) != 0)
1265 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1268 KRL_DBG(("%s: revoked by key SHA1", __func__));
1269 return SSH_ERR_KEY_REVOKED;
1271 memset(&rb, 0, sizeof(rb));
1272 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA256,
1273 &rb.blob, &rb.len)) != 0)
1275 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha256s, &rb);
1278 KRL_DBG(("%s: revoked by key SHA256", __func__));
1279 return SSH_ERR_KEY_REVOKED;
1282 /* Next, explicit keys */
1283 memset(&rb, 0, sizeof(rb));
1284 if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1286 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1289 KRL_DBG(("%s: revoked by explicit key", __func__));
1290 return SSH_ERR_KEY_REVOKED;
1293 if (!sshkey_is_cert(key))
1296 /* Check cert revocation for the specified CA */
1297 if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1301 if ((r = is_cert_revoked(key, rc)) != 0)
1304 /* Check cert revocation for the wildcard CA */
1305 if ((r = revoked_certs_for_ca_key(krl, NULL, &rc, 0)) != 0)
1308 if ((r = is_cert_revoked(key, rc)) != 0)
1312 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
1317 ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
1321 KRL_DBG(("%s: checking key", __func__));
1322 if ((r = is_key_revoked(krl, key)) != 0)
1324 if (sshkey_is_cert(key)) {
1325 debug2("%s: checking CA key", __func__);
1326 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1329 KRL_DBG(("%s: key okay", __func__));
1334 ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
1336 struct sshbuf *krlbuf = NULL;
1337 struct ssh_krl *krl = NULL;
1338 int oerrno = 0, r, fd;
1343 if ((krlbuf = sshbuf_new()) == NULL)
1344 return SSH_ERR_ALLOC_FAIL;
1345 if ((fd = open(path, O_RDONLY)) == -1) {
1346 r = SSH_ERR_SYSTEM_ERROR;
1350 if ((r = sshkey_load_file(fd, krlbuf)) != 0) {
1354 if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
1356 debug2("%s: checking KRL %s", __func__, path);
1357 r = ssh_krl_check_key(krl, key);
1361 sshbuf_free(krlbuf);