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