[BACK]Return to krl.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/krl.c, Revision 1.1

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