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.55 2023/03/14 07:28:47 dtucker Exp $ */
21 #include <sys/types.h>
22 #include <openbsd-compat/sys-tree.h>
23 #include <openbsd-compat/sys-queue.h>
45 /* #define DEBUG_KRL */
47 # define KRL_DBG(x) debug3_f x
53 * Trees of revoked serial numbers, key IDs and keys. This allows
54 * quick searching, querying and producing lists in canonical order.
57 /* Tree of serial numbers. XXX make smarter: really need a real sparse bitmap */
58 struct revoked_serial {
60 RB_ENTRY(revoked_serial) tree_entry;
62 static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
63 RB_HEAD(revoked_serial_tree, revoked_serial);
64 RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp)
67 struct revoked_key_id {
69 RB_ENTRY(revoked_key_id) tree_entry;
71 static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
72 RB_HEAD(revoked_key_id_tree, revoked_key_id);
73 RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp)
75 /* Tree of blobs (used for keys and fingerprints) */
79 RB_ENTRY(revoked_blob) tree_entry;
81 static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
82 RB_HEAD(revoked_blob_tree, revoked_blob);
83 RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp)
85 /* Tracks revoked certs for a single CA */
86 struct revoked_certs {
87 struct sshkey *ca_key;
88 struct revoked_serial_tree revoked_serials;
89 struct revoked_key_id_tree revoked_key_ids;
90 TAILQ_ENTRY(revoked_certs) entry;
92 TAILQ_HEAD(revoked_certs_list, revoked_certs);
95 u_int64_t krl_version;
96 u_int64_t generated_date;
99 struct revoked_blob_tree revoked_keys;
100 struct revoked_blob_tree revoked_sha1s;
101 struct revoked_blob_tree revoked_sha256s;
102 struct revoked_certs_list revoked_certs;
105 /* Return equal if a and b overlap */
107 serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
109 if (a->hi >= b->lo && a->lo <= b->hi)
111 return a->lo < b->lo ? -1 : 1;
115 key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
117 return strcmp(a->key_id, b->key_id);
121 blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
125 if (a->len != b->len) {
126 if ((r = memcmp(a->blob, b->blob, MINIMUM(a->len, b->len))) != 0)
128 return a->len > b->len ? 1 : -1;
130 return memcmp(a->blob, b->blob, a->len);
138 if ((krl = calloc(1, sizeof(*krl))) == NULL)
140 RB_INIT(&krl->revoked_keys);
141 RB_INIT(&krl->revoked_sha1s);
142 RB_INIT(&krl->revoked_sha256s);
143 TAILQ_INIT(&krl->revoked_certs);
148 revoked_certs_free(struct revoked_certs *rc)
150 struct revoked_serial *rs, *trs;
151 struct revoked_key_id *rki, *trki;
153 RB_FOREACH_SAFE(rs, revoked_serial_tree, &rc->revoked_serials, trs) {
154 RB_REMOVE(revoked_serial_tree, &rc->revoked_serials, rs);
157 RB_FOREACH_SAFE(rki, revoked_key_id_tree, &rc->revoked_key_ids, trki) {
158 RB_REMOVE(revoked_key_id_tree, &rc->revoked_key_ids, rki);
162 sshkey_free(rc->ca_key);
166 ssh_krl_free(struct ssh_krl *krl)
168 struct revoked_blob *rb, *trb;
169 struct revoked_certs *rc, *trc;
175 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
176 RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
180 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
181 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
185 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha256s, trb) {
186 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha256s, rb);
190 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
191 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
192 revoked_certs_free(rc);
198 ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
200 krl->krl_version = version;
204 ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
207 if ((krl->comment = strdup(comment)) == NULL)
208 return SSH_ERR_ALLOC_FAIL;
213 * Find the revoked_certs struct for a CA key. If allow_create is set then
214 * create a new one in the tree if one did not exist already.
217 revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
218 struct revoked_certs **rcp, int allow_create)
220 struct revoked_certs *rc;
224 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
225 if ((ca_key == NULL && rc->ca_key == NULL) ||
226 sshkey_equal(rc->ca_key, ca_key)) {
233 /* If this CA doesn't exist in the list then add it now */
234 if ((rc = calloc(1, sizeof(*rc))) == NULL)
235 return SSH_ERR_ALLOC_FAIL;
238 else if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
242 RB_INIT(&rc->revoked_serials);
243 RB_INIT(&rc->revoked_key_ids);
244 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
245 KRL_DBG(("new CA %s", ca_key == NULL ? "*" : sshkey_type(ca_key)));
251 insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
253 struct revoked_serial rs, *ers, *crs, *irs;
255 KRL_DBG(("insert %llu:%llu", lo, hi));
256 memset(&rs, 0, sizeof(rs));
259 ers = RB_NFIND(revoked_serial_tree, rt, &rs);
260 if (ers == NULL || serial_cmp(ers, &rs) != 0) {
261 /* No entry matches. Just insert */
262 if ((irs = malloc(sizeof(rs))) == NULL)
263 return SSH_ERR_ALLOC_FAIL;
264 memcpy(irs, &rs, sizeof(*irs));
265 ers = RB_INSERT(revoked_serial_tree, rt, irs);
267 KRL_DBG(("bad: ers != NULL"));
268 /* Shouldn't happen */
270 return SSH_ERR_INTERNAL_ERROR;
274 KRL_DBG(("overlap found %llu:%llu", ers->lo, ers->hi));
276 * The inserted entry overlaps an existing one. Grow the
286 * The inserted or revised range might overlap or abut adjacent ones;
287 * coalesce as necessary.
290 /* Check predecessors */
291 while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
292 KRL_DBG(("pred %llu:%llu", crs->lo, crs->hi));
293 if (ers->lo != 0 && crs->hi < ers->lo - 1)
295 /* This entry overlaps. */
296 if (crs->lo < ers->lo) {
298 KRL_DBG(("pred extend %llu:%llu", ers->lo, ers->hi));
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(("succ %llu:%llu", 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(("succ extend %llu:%llu", ers->lo, ers->hi));
313 RB_REMOVE(revoked_serial_tree, rt, crs);
316 KRL_DBG(("done, final %llu:%llu", ers->lo, ers->hi));
321 ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
324 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
328 ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl,
329 const struct sshkey *ca_key, u_int64_t lo, u_int64_t hi)
331 struct revoked_certs *rc;
334 if (lo > hi || lo == 0)
335 return SSH_ERR_INVALID_ARGUMENT;
336 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
338 return insert_serial_range(&rc->revoked_serials, lo, hi);
342 ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
345 struct revoked_key_id *rki, *erki;
346 struct revoked_certs *rc;
349 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
352 KRL_DBG(("revoke %s", key_id));
353 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
354 (rki->key_id = strdup(key_id)) == NULL) {
356 return SSH_ERR_ALLOC_FAIL;
358 erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
366 /* Convert "key" to a public key blob without any certificate information */
368 plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
370 struct sshkey *kcopy;
373 if ((r = sshkey_from_private(key, &kcopy)) != 0)
375 if (sshkey_is_cert(kcopy)) {
376 if ((r = sshkey_drop_cert(kcopy)) != 0) {
381 r = sshkey_to_blob(kcopy, blob, blen);
386 /* Revoke a key blob. Ownership of blob is transferred to the tree */
388 revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
390 struct revoked_blob *rb, *erb;
392 if ((rb = calloc(1, sizeof(*rb))) == NULL)
393 return SSH_ERR_ALLOC_FAIL;
396 erb = RB_INSERT(revoked_blob_tree, rbt, rb);
405 ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
411 debug3_f("revoke type %s", sshkey_type(key));
412 if ((r = plain_key_blob(key, &blob, &len)) != 0)
414 return revoke_blob(&krl->revoked_keys, blob, len);
418 revoke_by_hash(struct revoked_blob_tree *target, const u_char *p, size_t len)
423 /* need to copy hash, as revoke_blob steals ownership */
424 if ((blob = malloc(len)) == NULL)
425 return SSH_ERR_SYSTEM_ERROR;
426 memcpy(blob, p, len);
427 if ((r = revoke_blob(target, blob, len)) != 0) {
435 ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const u_char *p, size_t len)
437 debug3_f("revoke by sha1");
439 return SSH_ERR_INVALID_FORMAT;
440 return revoke_by_hash(&krl->revoked_sha1s, p, len);
444 ssh_krl_revoke_key_sha256(struct ssh_krl *krl, const u_char *p, size_t len)
446 debug3_f("revoke by sha256");
448 return SSH_ERR_INVALID_FORMAT;
449 return revoke_by_hash(&krl->revoked_sha256s, p, len);
453 ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
455 /* XXX replace with SHA256? */
456 if (!sshkey_is_cert(key))
457 return ssh_krl_revoke_key_explicit(krl, key);
459 if (key->cert->serial == 0) {
460 return ssh_krl_revoke_cert_by_key_id(krl,
461 key->cert->signature_key,
464 return ssh_krl_revoke_cert_by_serial(krl,
465 key->cert->signature_key,
471 * Select the most compact section type to emit next in a KRL based on
472 * the current section type, the run length of contiguous revoked serial
473 * numbers and the gaps from the last and to the next revoked serial.
474 * Applies a mostly-accurate bit cost model to select the section type
475 * that will minimise the size of the resultant KRL.
478 choose_next_state(int current_state, u_int64_t contig, int final,
479 u_int64_t last_gap, u_int64_t next_gap, int *force_new_section)
482 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
485 * Avoid unsigned overflows.
486 * The limits are high enough to avoid confusing the calculations.
488 contig = MINIMUM(contig, 1ULL<<31);
489 last_gap = MINIMUM(last_gap, 1ULL<<31);
490 next_gap = MINIMUM(next_gap, 1ULL<<31);
493 * Calculate the cost to switch from the current state to candidates.
494 * NB. range sections only ever contain a single range, so their
495 * switching cost is independent of the current_state.
497 cost_list = cost_bitmap = cost_bitmap_restart = 0;
499 switch (current_state) {
500 case KRL_SECTION_CERT_SERIAL_LIST:
501 cost_bitmap_restart = cost_bitmap = 8 + 64;
503 case KRL_SECTION_CERT_SERIAL_BITMAP:
505 cost_bitmap_restart = 8 + 64;
507 case KRL_SECTION_CERT_SERIAL_RANGE:
509 cost_bitmap_restart = cost_bitmap = 8 + 64;
513 /* Estimate base cost in bits of each section type */
514 cost_list += 64 * contig + (final ? 0 : 8+64);
515 cost_range += (2 * 64) + (final ? 0 : 8+64);
516 cost_bitmap += last_gap + contig + (final ? 0 : MINIMUM(next_gap, 8+64));
517 cost_bitmap_restart += contig + (final ? 0 : MINIMUM(next_gap, 8+64));
519 /* Convert to byte costs for actual comparison */
520 cost_list = (cost_list + 7) / 8;
521 cost_bitmap = (cost_bitmap + 7) / 8;
522 cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
523 cost_range = (cost_range + 7) / 8;
525 /* Now pick the best choice */
526 *force_new_section = 0;
527 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
529 if (cost_range < cost) {
530 new_state = KRL_SECTION_CERT_SERIAL_RANGE;
533 if (cost_list < cost) {
534 new_state = KRL_SECTION_CERT_SERIAL_LIST;
537 if (cost_bitmap_restart < cost) {
538 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
539 *force_new_section = 1;
540 cost = cost_bitmap_restart;
542 KRL_DBG(("contig %llu last_gap %llu next_gap %llu final %d, costs:"
543 "list %llu range %llu bitmap %llu new bitmap %llu, "
544 "selected 0x%02x%s", (long long unsigned)contig,
545 (long long unsigned)last_gap, (long long unsigned)next_gap, final,
546 (long long unsigned)cost_list, (long long unsigned)cost_range,
547 (long long unsigned)cost_bitmap,
548 (long long unsigned)cost_bitmap_restart, new_state,
549 *force_new_section ? " restart" : ""));
554 put_bitmap(struct sshbuf *buf, struct bitmap *bitmap)
560 len = bitmap_nbytes(bitmap);
561 if ((blob = malloc(len)) == NULL)
562 return SSH_ERR_ALLOC_FAIL;
563 if (bitmap_to_string(bitmap, blob, len) != 0) {
565 return SSH_ERR_INTERNAL_ERROR;
567 r = sshbuf_put_bignum2_bytes(buf, blob, len);
572 /* Generate a KRL_SECTION_CERTIFICATES KRL section */
574 revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
576 int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
577 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
578 struct revoked_serial *rs, *nrs;
579 struct revoked_key_id *rki;
580 int next_state, state = 0;
582 struct bitmap *bitmap = NULL;
584 if ((sect = sshbuf_new()) == NULL)
585 return SSH_ERR_ALLOC_FAIL;
587 /* Store the header: optional CA scope key, reserved */
588 if (rc->ca_key == NULL) {
589 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
592 if ((r = sshkey_puts(rc->ca_key, buf)) != 0)
595 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
598 /* Store the revoked serials. */
599 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
601 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
602 KRL_DBG(("serial %llu:%llu state 0x%02x",
603 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
606 /* Check contiguous length and gap to next section (if any) */
607 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
609 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
610 contig = 1 + (rs->hi - rs->lo);
612 /* Choose next state based on these */
613 next_state = choose_next_state(state, contig, final,
614 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
617 * If the current section is a range section or has a different
618 * type to the next section, then finish it off now.
620 if (state != 0 && (force_new_sect || next_state != state ||
621 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
622 KRL_DBG(("finish state 0x%02x", state));
624 case KRL_SECTION_CERT_SERIAL_LIST:
625 case KRL_SECTION_CERT_SERIAL_RANGE:
627 case KRL_SECTION_CERT_SERIAL_BITMAP:
628 if ((r = put_bitmap(sect, bitmap)) != 0)
634 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
635 (r = sshbuf_put_stringb(buf, sect)) != 0)
640 /* If we are starting a new section then prepare it now */
641 if (next_state != state || force_new_sect) {
642 KRL_DBG(("start state 0x%02x",
647 case KRL_SECTION_CERT_SERIAL_LIST:
648 case KRL_SECTION_CERT_SERIAL_RANGE:
650 case KRL_SECTION_CERT_SERIAL_BITMAP:
651 if ((bitmap = bitmap_new()) == NULL) {
652 r = SSH_ERR_ALLOC_FAIL;
655 bitmap_start = rs->lo;
656 if ((r = sshbuf_put_u64(sect,
663 /* Perform section-specific processing */
665 case KRL_SECTION_CERT_SERIAL_LIST:
666 for (i = 0; i < contig; i++) {
667 if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
671 case KRL_SECTION_CERT_SERIAL_RANGE:
672 if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
673 (r = sshbuf_put_u64(sect, rs->hi)) != 0)
676 case KRL_SECTION_CERT_SERIAL_BITMAP:
677 if (rs->lo - bitmap_start > INT_MAX) {
678 error_f("insane bitmap gap");
681 for (i = 0; i < contig; i++) {
682 if (bitmap_set_bit(bitmap,
683 rs->lo + i - bitmap_start) != 0) {
684 r = SSH_ERR_ALLOC_FAIL;
692 /* Flush the remaining section, if any */
694 KRL_DBG(("serial final flush for state 0x%02x", state));
696 case KRL_SECTION_CERT_SERIAL_LIST:
697 case KRL_SECTION_CERT_SERIAL_RANGE:
699 case KRL_SECTION_CERT_SERIAL_BITMAP:
700 if ((r = put_bitmap(sect, bitmap)) != 0)
706 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
707 (r = sshbuf_put_stringb(buf, sect)) != 0)
710 KRL_DBG(("serial done "));
712 /* Now output a section for any revocations by key ID */
714 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
715 KRL_DBG(("key ID %s", rki->key_id));
716 if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
719 if (sshbuf_len(sect) != 0) {
720 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
721 (r = sshbuf_put_stringb(buf, sect)) != 0)
732 ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
733 struct sshkey **sign_keys, u_int nsign_keys)
735 int r = SSH_ERR_INTERNAL_ERROR;
736 struct revoked_certs *rc;
737 struct revoked_blob *rb;
739 u_char *sblob = NULL;
742 if (krl->generated_date == 0)
743 krl->generated_date = time(NULL);
745 if ((sect = sshbuf_new()) == NULL)
746 return SSH_ERR_ALLOC_FAIL;
748 /* Store the header */
749 if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
750 (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
751 (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
752 (r = sshbuf_put_u64(buf, krl->generated_date)) != 0 ||
753 (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
754 (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
755 (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
758 /* Store sections for revoked certificates */
759 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
761 if ((r = revoked_certs_generate(rc, sect)) != 0)
763 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
764 (r = sshbuf_put_stringb(buf, sect)) != 0)
768 /* Finally, output sections for revocations by public key/hash */
770 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
771 KRL_DBG(("key len %zu ", rb->len));
772 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
775 if (sshbuf_len(sect) != 0) {
776 if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
777 (r = sshbuf_put_stringb(buf, sect)) != 0)
781 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
782 KRL_DBG(("hash len %zu ", rb->len));
783 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
786 if (sshbuf_len(sect) != 0) {
787 if ((r = sshbuf_put_u8(buf,
788 KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
789 (r = sshbuf_put_stringb(buf, sect)) != 0)
793 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
794 KRL_DBG(("hash len %zu ", rb->len));
795 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
798 if (sshbuf_len(sect) != 0) {
799 if ((r = sshbuf_put_u8(buf,
800 KRL_SECTION_FINGERPRINT_SHA256)) != 0 ||
801 (r = sshbuf_put_stringb(buf, sect)) != 0)
805 for (i = 0; i < nsign_keys; i++) {
806 KRL_DBG(("sig key %s", sshkey_ssh_name(sign_keys[i])));
807 if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
808 (r = sshkey_puts(sign_keys[i], buf)) != 0)
810 /* XXX support sk-* keys */
811 if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
812 sshbuf_ptr(buf), sshbuf_len(buf), NULL, NULL,
815 KRL_DBG(("signature sig len %zu", slen));
816 if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
828 format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
836 strlcpy(ts, "<INVALID>", nts);
839 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
844 parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
846 int r = SSH_ERR_INTERNAL_ERROR;
850 struct sshbuf *subsect = NULL;
851 u_int64_t serial, serial_lo, serial_hi;
852 struct bitmap *bitmap = NULL;
854 struct sshkey *ca_key = NULL;
856 if ((subsect = sshbuf_new()) == NULL)
857 return SSH_ERR_ALLOC_FAIL;
859 /* Header: key, reserved */
860 if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
861 (r = sshbuf_skip_string(buf)) != 0)
863 if (blen != 0 && (r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
866 while (sshbuf_len(buf) > 0) {
867 sshbuf_free(subsect);
869 if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
870 (r = sshbuf_froms(buf, &subsect)) != 0)
872 KRL_DBG(("subsection type 0x%02x", type));
873 /* sshbuf_dump(subsect, stderr); */
876 case KRL_SECTION_CERT_SERIAL_LIST:
877 while (sshbuf_len(subsect) > 0) {
878 if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
880 if ((r = ssh_krl_revoke_cert_by_serial(krl,
881 ca_key, serial)) != 0)
885 case KRL_SECTION_CERT_SERIAL_RANGE:
886 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
887 (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
889 if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
890 ca_key, serial_lo, serial_hi)) != 0)
893 case KRL_SECTION_CERT_SERIAL_BITMAP:
894 if ((bitmap = bitmap_new()) == NULL) {
895 r = SSH_ERR_ALLOC_FAIL;
898 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
899 (r = sshbuf_get_bignum2_bytes_direct(subsect,
902 if (bitmap_from_string(bitmap, blob, blen) != 0) {
903 r = SSH_ERR_INVALID_FORMAT;
906 nbits = bitmap_nbits(bitmap);
907 for (serial = 0; serial < (u_int64_t)nbits; serial++) {
908 if (serial > 0 && serial_lo + serial == 0) {
909 error_f("bitmap wraps u64");
910 r = SSH_ERR_INVALID_FORMAT;
913 if (!bitmap_test_bit(bitmap, serial))
915 if ((r = ssh_krl_revoke_cert_by_serial(krl,
916 ca_key, serial_lo + serial)) != 0)
922 case KRL_SECTION_CERT_KEY_ID:
923 while (sshbuf_len(subsect) > 0) {
924 if ((r = sshbuf_get_cstring(subsect,
925 &key_id, NULL)) != 0)
927 if ((r = ssh_krl_revoke_cert_by_key_id(krl,
928 ca_key, key_id)) != 0)
935 error("Unsupported KRL certificate section %u", type);
936 r = SSH_ERR_INVALID_FORMAT;
939 if (sshbuf_len(subsect) > 0) {
940 error("KRL certificate section contains unparsed data");
941 r = SSH_ERR_INVALID_FORMAT;
952 sshbuf_free(subsect);
957 blob_section(struct sshbuf *sect, struct revoked_blob_tree *target_tree,
960 u_char *rdata = NULL;
964 while (sshbuf_len(sect) > 0) {
965 if ((r = sshbuf_get_string(sect, &rdata, &rlen)) != 0)
967 if (expected_len != 0 && rlen != expected_len) {
968 error_f("bad length");
970 return SSH_ERR_INVALID_FORMAT;
972 if ((r = revoke_blob(target_tree, rdata, rlen)) != 0) {
980 /* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
982 ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
983 const struct sshkey **sign_ca_keys, size_t nsign_ca_keys)
985 struct sshbuf *copy = NULL, *sect = NULL;
986 struct ssh_krl *krl = NULL;
988 int r = SSH_ERR_INTERNAL_ERROR, sig_seen;
989 struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
992 size_t i, j, sig_off, sects_off, blen, nca_used;
993 u_int format_version;
997 if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
998 memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
999 debug3_f("not a KRL");
1000 return SSH_ERR_KRL_BAD_MAGIC;
1003 /* Take a copy of the KRL buffer so we can verify its signature later */
1004 if ((copy = sshbuf_fromb(buf)) == NULL) {
1005 r = SSH_ERR_ALLOC_FAIL;
1008 if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0)
1011 if ((krl = ssh_krl_init()) == NULL) {
1012 error_f("alloc failed");
1016 if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
1018 if (format_version != KRL_FORMAT_VERSION) {
1019 r = SSH_ERR_INVALID_FORMAT;
1022 if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
1023 (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
1024 (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
1025 (r = sshbuf_skip_string(copy)) != 0 ||
1026 (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0)
1029 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1030 debug("KRL version %llu generated at %s%s%s",
1031 (long long unsigned)krl->krl_version, timestamp,
1032 *krl->comment ? ": " : "", krl->comment);
1035 * 1st pass: verify signatures, if any. This is done to avoid
1036 * detailed parsing of data whose provenance is unverified.
1039 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1040 /* Shouldn't happen */
1041 r = SSH_ERR_INTERNAL_ERROR;
1044 sects_off = sshbuf_len(buf) - sshbuf_len(copy);
1045 while (sshbuf_len(copy) > 0) {
1046 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1047 (r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0)
1049 KRL_DBG(("first pass, section 0x%02x", type));
1050 if (type != KRL_SECTION_SIGNATURE) {
1052 error("KRL contains non-signature section "
1054 r = SSH_ERR_INVALID_FORMAT;
1057 /* Not interested for now. */
1061 /* First string component is the signing key */
1062 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1063 r = SSH_ERR_INVALID_FORMAT;
1066 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1067 /* Shouldn't happen */
1068 r = SSH_ERR_INTERNAL_ERROR;
1071 sig_off = sshbuf_len(buf) - sshbuf_len(copy);
1072 /* Second string component is the signature itself */
1073 if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
1074 r = SSH_ERR_INVALID_FORMAT;
1077 /* Check signature over entire KRL up to this point */
1078 if ((r = sshkey_verify(key, blob, blen,
1079 sshbuf_ptr(buf), sig_off, NULL, 0, NULL)) != 0)
1081 /* Check if this key has already signed this KRL */
1082 for (i = 0; i < nca_used; i++) {
1083 if (sshkey_equal(ca_used[i], key)) {
1084 error("KRL signed more than once with "
1086 r = SSH_ERR_INVALID_FORMAT;
1090 /* Record keys used to sign the KRL */
1091 tmp_ca_used = recallocarray(ca_used, nca_used, nca_used + 1,
1093 if (tmp_ca_used == NULL) {
1094 r = SSH_ERR_ALLOC_FAIL;
1097 ca_used = tmp_ca_used;
1098 ca_used[nca_used++] = key;
1102 if (sshbuf_len(copy) != 0) {
1103 /* Shouldn't happen */
1104 r = SSH_ERR_INTERNAL_ERROR;
1109 * 2nd pass: parse and load the KRL, skipping the header to the point
1110 * where the section start.
1113 if ((copy = sshbuf_fromb(buf)) == NULL) {
1114 r = SSH_ERR_ALLOC_FAIL;
1117 if ((r = sshbuf_consume(copy, sects_off)) != 0)
1119 while (sshbuf_len(copy) > 0) {
1122 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1123 (r = sshbuf_froms(copy, §)) != 0)
1125 KRL_DBG(("second pass, section 0x%02x", type));
1128 case KRL_SECTION_CERTIFICATES:
1129 if ((r = parse_revoked_certs(sect, krl)) != 0)
1132 case KRL_SECTION_EXPLICIT_KEY:
1133 if ((r = blob_section(sect,
1134 &krl->revoked_keys, 0)) != 0)
1137 case KRL_SECTION_FINGERPRINT_SHA1:
1138 if ((r = blob_section(sect,
1139 &krl->revoked_sha1s, 20)) != 0)
1142 case KRL_SECTION_FINGERPRINT_SHA256:
1143 if ((r = blob_section(sect,
1144 &krl->revoked_sha256s, 32)) != 0)
1147 case KRL_SECTION_SIGNATURE:
1148 /* Handled above, but still need to stay in synch */
1151 if ((r = sshbuf_skip_string(copy)) != 0)
1155 error("Unsupported KRL section %u", type);
1156 r = SSH_ERR_INVALID_FORMAT;
1159 if (sect != NULL && sshbuf_len(sect) > 0) {
1160 error("KRL section contains unparsed data");
1161 r = SSH_ERR_INVALID_FORMAT;
1166 /* Check that the key(s) used to sign the KRL weren't revoked */
1168 for (i = 0; i < nca_used; i++) {
1169 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1172 sshkey_free(ca_used[i]);
1176 if (nca_used && !sig_seen) {
1177 error("All keys used to sign KRL were revoked");
1178 r = SSH_ERR_KEY_REVOKED;
1182 /* If we have CA keys, then verify that one was used to sign the KRL */
1183 if (sig_seen && nsign_ca_keys != 0) {
1185 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1186 for (j = 0; j < nca_used; j++) {
1187 if (ca_used[j] == NULL)
1189 if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
1196 r = SSH_ERR_SIGNATURE_INVALID;
1197 error("KRL not signed with any trusted key");
1207 for (i = 0; i < nca_used; i++)
1208 sshkey_free(ca_used[i]);
1216 /* Checks certificate serial number and key ID revocation */
1218 is_cert_revoked(const struct sshkey *key, struct revoked_certs *rc)
1220 struct revoked_serial rs, *ers;
1221 struct revoked_key_id rki, *erki;
1223 /* Check revocation by cert key ID */
1224 memset(&rki, 0, sizeof(rki));
1225 rki.key_id = key->cert->key_id;
1226 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1228 KRL_DBG(("revoked by key ID"));
1229 return SSH_ERR_KEY_REVOKED;
1233 * Zero serials numbers are ignored (it's the default when the
1234 * CA doesn't specify one).
1236 if (key->cert->serial == 0)
1239 memset(&rs, 0, sizeof(rs));
1240 rs.lo = rs.hi = key->cert->serial;
1241 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1243 KRL_DBG(("revoked serial %llu matched %llu:%llu",
1244 key->cert->serial, ers->lo, ers->hi));
1245 return SSH_ERR_KEY_REVOKED;
1250 /* Checks whether a given key/cert is revoked. Does not check its CA */
1252 is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
1254 struct revoked_blob rb, *erb;
1255 struct revoked_certs *rc;
1258 /* Check explicitly revoked hashes first */
1259 memset(&rb, 0, sizeof(rb));
1260 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
1261 &rb.blob, &rb.len)) != 0)
1263 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1266 KRL_DBG(("revoked by key SHA1"));
1267 return SSH_ERR_KEY_REVOKED;
1269 memset(&rb, 0, sizeof(rb));
1270 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA256,
1271 &rb.blob, &rb.len)) != 0)
1273 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha256s, &rb);
1276 KRL_DBG(("revoked by key SHA256"));
1277 return SSH_ERR_KEY_REVOKED;
1280 /* Next, explicit keys */
1281 memset(&rb, 0, sizeof(rb));
1282 if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1284 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1287 KRL_DBG(("revoked by explicit key"));
1288 return SSH_ERR_KEY_REVOKED;
1291 if (!sshkey_is_cert(key))
1294 /* Check cert revocation for the specified CA */
1295 if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1299 if ((r = is_cert_revoked(key, rc)) != 0)
1302 /* Check cert revocation for the wildcard CA */
1303 if ((r = revoked_certs_for_ca_key(krl, NULL, &rc, 0)) != 0)
1306 if ((r = is_cert_revoked(key, rc)) != 0)
1310 KRL_DBG(("%llu no match", key->cert->serial));
1315 ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
1319 KRL_DBG(("checking key"));
1320 if ((r = is_key_revoked(krl, key)) != 0)
1322 if (sshkey_is_cert(key)) {
1323 debug2_f("checking CA key");
1324 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1327 KRL_DBG(("key okay"));
1332 ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
1334 struct sshbuf *krlbuf = NULL;
1335 struct ssh_krl *krl = NULL;
1340 if ((r = sshbuf_load_file(path, &krlbuf)) != 0) {
1344 if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
1346 debug2_f("checking KRL %s", path);
1347 r = ssh_krl_check_key(krl, key);
1349 sshbuf_free(krlbuf);
1357 krl_dump(struct ssh_krl *krl, FILE *f)
1359 struct sshkey *key = NULL;
1360 struct revoked_blob *rb;
1361 struct revoked_certs *rc;
1362 struct revoked_serial *rs;
1363 struct revoked_key_id *rki;
1365 char *fp, timestamp[64];
1367 /* Try to print in a KRL spec-compatible format */
1368 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1369 fprintf(f, "# KRL version %llu\n",
1370 (unsigned long long)krl->krl_version);
1371 fprintf(f, "# Generated at %s\n", timestamp);
1372 if (krl->comment != NULL && *krl->comment != '\0') {
1374 asmprintf(&fp, INT_MAX, &r, "%s", krl->comment);
1375 fprintf(f, "# Comment: %s\n", fp);
1380 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
1381 if ((r = sshkey_from_blob(rb->blob, rb->len, &key)) != 0) {
1382 ret = SSH_ERR_INVALID_FORMAT;
1383 error_r(r, "parse KRL key");
1386 if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
1387 SSH_FP_DEFAULT)) == NULL) {
1388 ret = SSH_ERR_INVALID_FORMAT;
1389 error("sshkey_fingerprint failed");
1392 fprintf(f, "hash: %s # %s\n", fp, sshkey_ssh_name(key));
1396 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
1397 fp = tohex(rb->blob, rb->len);
1398 fprintf(f, "hash: SHA256:%s\n", fp);
1401 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
1403 * There is not KRL spec keyword for raw SHA1 hashes, so
1404 * print them as comments.
1406 fp = tohex(rb->blob, rb->len);
1407 fprintf(f, "# hash SHA1:%s\n", fp);
1411 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
1413 if (rc->ca_key == NULL)
1414 fprintf(f, "# Wildcard CA\n");
1416 if ((fp = sshkey_fingerprint(rc->ca_key,
1417 SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL) {
1418 ret = SSH_ERR_INVALID_FORMAT;
1419 error("sshkey_fingerprint failed");
1422 fprintf(f, "# CA key %s %s\n",
1423 sshkey_ssh_name(rc->ca_key), fp);
1426 RB_FOREACH(rs, revoked_serial_tree, &rc->revoked_serials) {
1427 if (rs->lo == rs->hi) {
1428 fprintf(f, "serial: %llu\n",
1429 (unsigned long long)rs->lo);
1431 fprintf(f, "serial: %llu-%llu\n",
1432 (unsigned long long)rs->lo,
1433 (unsigned long long)rs->hi);
1436 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
1438 * We don't want key IDs with embedded newlines to
1439 * mess up the display.
1442 asmprintf(&fp, INT_MAX, &r, "%s", rki->key_id);
1443 fprintf(f, "id: %s\n", fp);