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