blob: 721cabdd81504b47a016d3fcb3131d750dd36eb8 [file] [log] [blame]
Damien Millerf3747bf2013-01-18 11:44:04 +11001/*
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.orgf101d822015-01-18 14:01:00 +000017/* $OpenBSD: krl.c,v 1.27 2015/01/18 14:01:00 deraadt Exp $ */
Damien Millerf3747bf2013-01-18 11:44:04 +110018
19#include "includes.h"
20
21#include <sys/types.h>
22#include <sys/param.h>
Damien Millerd60b2102013-01-20 22:49:58 +110023#include <openbsd-compat/sys-tree.h>
24#include <openbsd-compat/sys-queue.h>
Damien Millerf3747bf2013-01-18 11:44:04 +110025
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.org74de2542014-12-04 01:49:59 +000033#include "sshbuf.h"
djm@openbsd.orge7fd9522015-01-13 19:04:35 +000034#include "ssherr.h"
djm@openbsd.org74de2542014-12-04 01:49:59 +000035#include "sshkey.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110036#include "authfile.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110037#include "misc.h"
38#include "log.h"
djm@openbsd.org56d1c832014-12-21 22:27:55 +000039#include "digest.h"
djm@openbsd.orga165bab2015-01-14 15:02:39 +000040#include "bitmap.h"
Damien Millerf3747bf2013-01-18 11:44:04 +110041
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 */
57struct revoked_serial {
58 u_int64_t lo, hi;
59 RB_ENTRY(revoked_serial) tree_entry;
60};
61static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
62RB_HEAD(revoked_serial_tree, revoked_serial);
63RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp);
64
65/* Tree of key IDs */
66struct revoked_key_id {
67 char *key_id;
68 RB_ENTRY(revoked_key_id) tree_entry;
69};
70static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
71RB_HEAD(revoked_key_id_tree, revoked_key_id);
72RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp);
73
74/* Tree of blobs (used for keys and fingerprints) */
75struct revoked_blob {
76 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +000077 size_t len;
Damien Millerf3747bf2013-01-18 11:44:04 +110078 RB_ENTRY(revoked_blob) tree_entry;
79};
80static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
81RB_HEAD(revoked_blob_tree, revoked_blob);
82RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp);
83
84/* Tracks revoked certs for a single CA */
85struct revoked_certs {
djm@openbsd.org74de2542014-12-04 01:49:59 +000086 struct sshkey *ca_key;
Damien Millerf3747bf2013-01-18 11:44:04 +110087 struct revoked_serial_tree revoked_serials;
88 struct revoked_key_id_tree revoked_key_ids;
89 TAILQ_ENTRY(revoked_certs) entry;
90};
91TAILQ_HEAD(revoked_certs_list, revoked_certs);
92
93struct 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 */
104static int
105serial_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
112static int
113key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
114{
115 return strcmp(a->key_id, b->key_id);
116}
117
118static int
119blob_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
131struct ssh_krl *
132ssh_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
144static void
145revoked_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.org74de2542014-12-04 01:49:59 +0000160 sshkey_free(rc->ca_key);
Damien Millerf3747bf2013-01-18 11:44:04 +1100161}
162
163void
164ssh_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
189void
190ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
191{
192 krl->krl_version = version;
193}
194
djm@openbsd.org74de2542014-12-04 01:49:59 +0000195int
Damien Millerf3747bf2013-01-18 11:44:04 +1100196ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
197{
198 free(krl->comment);
199 if ((krl->comment = strdup(comment)) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000200 return SSH_ERR_ALLOC_FAIL;
201 return 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100202}
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 */
208static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000209revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100210 struct revoked_certs **rcp, int allow_create)
211{
212 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000213 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100214
215 *rcp = NULL;
216 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000217 if (sshkey_equal(rc->ca_key, ca_key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100218 *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.org74de2542014-12-04 01:49:59 +0000226 return SSH_ERR_ALLOC_FAIL;
227 if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100228 free(rc);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000229 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100230 }
231 RB_INIT(&rc->revoked_serials);
232 RB_INIT(&rc->revoked_key_ids);
233 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000234 KRL_DBG(("%s: new CA %s", __func__, sshkey_type(ca_key)));
Damien Millerf3747bf2013-01-18 11:44:04 +1100235 *rcp = rc;
236 return 0;
237}
238
239static int
240insert_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 Miller1d2c4562014-02-04 11:18:20 +1100245 memset(&rs, 0, sizeof(rs));
Damien Millerf3747bf2013-01-18 11:44:04 +1100246 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.org74de2542014-12-04 01:49:59 +0000252 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100253 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 Millera7522d92013-01-20 22:35:31 +1100258 free(irs);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000259 return SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100260 }
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.orge7fd9522015-01-13 19:04:35 +0000274
Damien Millerf3747bf2013-01-18 11:44:04 +1100275 /*
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
312int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000313ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100314 u_int64_t serial)
315{
316 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
317}
318
319int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000320ssh_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 Millerf3747bf2013-01-18 11:44:04 +1100322{
323 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000324 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100325
326 if (lo > hi || lo == 0)
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000327 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000328 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
329 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100330 return insert_serial_range(&rc->revoked_serials, lo, hi);
331}
332
333int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000334ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100335 const char *key_id)
336{
337 struct revoked_key_id *rki, *erki;
338 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000339 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100340
djm@openbsd.org74de2542014-12-04 01:49:59 +0000341 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
342 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100343
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000344 KRL_DBG(("%s: revoke %s", __func__, key_id));
Damien Millerf3747bf2013-01-18 11:44:04 +1100345 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
346 (rki->key_id = strdup(key_id)) == NULL) {
347 free(rki);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000348 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100349 }
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 */
359static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000360plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
Damien Millerf3747bf2013-01-18 11:44:04 +1100361{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000362 struct sshkey *kcopy;
Damien Millerf3747bf2013-01-18 11:44:04 +1100363 int r;
364
djm@openbsd.org74de2542014-12-04 01:49:59 +0000365 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 Millerf3747bf2013-01-18 11:44:04 +1100371 }
372 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000373 r = sshkey_to_blob(kcopy, blob, blen);
markus@openbsd.org905fe302015-01-12 14:05:19 +0000374 sshkey_free(kcopy);
Damien Miller86687062014-07-02 15:28:02 +1000375 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100376}
377
378/* Revoke a key blob. Ownership of blob is transferred to the tree */
379static int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000380revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
Damien Millerf3747bf2013-01-18 11:44:04 +1100381{
382 struct revoked_blob *rb, *erb;
383
384 if ((rb = calloc(1, sizeof(*rb))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000385 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100386 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
396int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000397ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100398{
399 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000400 size_t len;
401 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100402
djm@openbsd.org74de2542014-12-04 01:49:59 +0000403 debug3("%s: revoke type %s", __func__, sshkey_type(key));
404 if ((r = plain_key_blob(key, &blob, &len)) != 0)
405 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100406 return revoke_blob(&krl->revoked_keys, blob, len);
407}
408
409int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000410ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100411{
412 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000413 size_t len;
414 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100415
djm@openbsd.org74de2542014-12-04 01:49:59 +0000416 debug3("%s: revoke type %s by sha1", __func__, sshkey_type(key));
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000417 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
418 &blob, &len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000419 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100420 return revoke_blob(&krl->revoked_sha1s, blob, len);
421}
422
423int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000424ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100425{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000426 if (!sshkey_is_cert(key))
Damien Millerf3747bf2013-01-18 11:44:04 +1100427 return ssh_krl_revoke_key_sha1(krl, key);
428
djm@openbsd.org74de2542014-12-04 01:49:59 +0000429 if (sshkey_cert_is_legacy(key) || key->cert->serial == 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100430 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.org74de2542014-12-04 01:49:59 +0000441 * 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 Millerf3747bf2013-01-18 11:44:04 +1100443 * 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 */
447static int
448choose_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.orge7fd9522015-01-13 19:04:35 +0000512 KRL_DBG(("%s: contig %llu last_gap %llu next_gap %llu final %d, costs:"
Damien Millerf3747bf2013-01-18 11:44:04 +1100513 "list %llu range %llu bitmap %llu new bitmap %llu, "
Damien Millerd677ad12013-04-23 15:18:51 +1000514 "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.orge7fd9522015-01-13 19:04:35 +0000519 *force_new_section ? " restart" : ""));
Damien Millerf3747bf2013-01-18 11:44:04 +1100520 return new_state;
521}
522
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000523static int
524put_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 Millerf3747bf2013-01-18 11:44:04 +1100542/* Generate a KRL_SECTION_CERTIFICATES KRL section */
543static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000544revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
Damien Millerf3747bf2013-01-18 11:44:04 +1100545{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000546 int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100547 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.org74de2542014-12-04 01:49:59 +0000551 struct sshbuf *sect;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000552 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100553
djm@openbsd.org74de2542014-12-04 01:49:59 +0000554 if ((sect = sshbuf_new()) == NULL)
555 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100556
djm@openbsd.org74de2542014-12-04 01:49:59 +0000557 /* 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 Millerf3747bf2013-01-18 11:44:04 +1100562
djm@openbsd.org74de2542014-12-04 01:49:59 +0000563 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100564
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.orge7fd9522015-01-13 19:04:35 +0000569 KRL_DBG(("%s: serial %llu:%llu state 0x%02x", __func__,
Damien Millerd677ad12013-04-23 15:18:51 +1000570 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000571 state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100572
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.orge7fd9522015-01-13 19:04:35 +0000589 KRL_DBG(("%s: finish state 0x%02x", __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100590 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.orga165bab2015-01-14 15:02:39 +0000595 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000596 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000597 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100598 bitmap = NULL;
599 break;
600 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000601 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
602 (r = sshbuf_put_stringb(buf, sect)) != 0)
603 goto out;
604 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100605 }
606
607 /* If we are starting a new section then prepare it now */
608 if (next_state != state || force_new_sect) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000609 KRL_DBG(("%s: start state 0x%02x", __func__,
610 next_state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100611 state = next_state;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000612 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100613 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.orga165bab2015-01-14 15:02:39 +0000618 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000619 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100620 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000621 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100622 bitmap_start = rs->lo;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000623 if ((r = sshbuf_put_u64(sect,
624 bitmap_start)) != 0)
625 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100626 break;
627 }
628 }
629
630 /* Perform section-specific processing */
631 switch (state) {
632 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000633 for (i = 0; i < contig; i++) {
634 if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
635 goto out;
636 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100637 break;
638 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000639 if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
640 (r = sshbuf_put_u64(sect, rs->hi)) != 0)
641 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100642 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.orga165bab2015-01-14 15:02:39 +0000649 if (bitmap_set_bit(bitmap,
650 rs->lo + i - bitmap_start) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000651 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100652 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000653 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100654 }
655 break;
656 }
657 last = rs->hi;
658 }
659 /* Flush the remaining section, if any */
660 if (state != 0) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000661 KRL_DBG(("%s: serial final flush for state 0x%02x",
662 __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100663 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.orga165bab2015-01-14 15:02:39 +0000668 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000669 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000670 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100671 bitmap = NULL;
672 break;
673 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000674 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
675 (r = sshbuf_put_stringb(buf, sect)) != 0)
676 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100677 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000678 KRL_DBG(("%s: serial done ", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +1100679
680 /* Now output a section for any revocations by key ID */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000681 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100682 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000683 KRL_DBG(("%s: key ID %s", __func__, rki->key_id));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000684 if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
685 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100686 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000687 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 Millerf3747bf2013-01-18 11:44:04 +1100691 }
692 r = 0;
693 out:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000694 bitmap_free(bitmap);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000695 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100696 return r;
697}
698
699int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000700ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
701 const struct sshkey **sign_keys, u_int nsign_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100702{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000703 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100704 struct revoked_certs *rc;
705 struct revoked_blob *rb;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000706 struct sshbuf *sect;
707 u_char *sblob = NULL;
708 size_t slen, i;
Damien Millerf3747bf2013-01-18 11:44:04 +1100709
710 if (krl->generated_date == 0)
711 krl->generated_date = time(NULL);
712
djm@openbsd.org74de2542014-12-04 01:49:59 +0000713 if ((sect = sshbuf_new()) == NULL)
714 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100715
716 /* Store the header */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000717 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 Millerf3747bf2013-01-18 11:44:04 +1100725
726 /* Store sections for revoked certificates */
727 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000728 sshbuf_reset(sect);
729 if ((r = revoked_certs_generate(rc, sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100730 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000731 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
732 (r = sshbuf_put_stringb(buf, sect)) != 0)
733 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100734 }
735
736 /* Finally, output sections for revocations by public key/hash */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000737 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100738 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000739 KRL_DBG(("%s: key len %u ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000740 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000741 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100742 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000743 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 Millerf3747bf2013-01-18 11:44:04 +1100747 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000748 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100749 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000750 KRL_DBG(("%s: hash len %u ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000751 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000752 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100753 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000754 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 Millerf3747bf2013-01-18 11:44:04 +1100759 }
760
761 for (i = 0; i < nsign_keys; i++) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000762 sshbuf_reset(sect);
763 if ((r = sshkey_to_blob_buf(sign_keys[i], sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100764 goto out;
765
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000766 KRL_DBG(("%s: signature key len %zu", __func__,
767 sshbuf_len(sect)));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000768 if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
769 (r = sshbuf_put_stringb(buf, sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100770 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000771
772 if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
773 sshbuf_ptr(buf), sshbuf_len(buf), 0)) == -1)
774 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000775 KRL_DBG(("%s: signature sig len %u", __func__, slen));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000776 if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
777 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100778 }
779
780 r = 0;
781 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100782 free(sblob);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000783 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100784 return r;
785}
786
787static void
788format_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.orgb6de5ac2014-11-21 01:00:38 +0000795 if (tm == NULL)
deraadt@openbsd.orgf101d822015-01-18 14:01:00 +0000796 strlcpy(ts, "<INVALID>", nts);
djm@openbsd.orgb6de5ac2014-11-21 01:00:38 +0000797 else {
798 *ts = '\0';
799 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
800 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100801}
802
803static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000804parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +1100805{
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000806 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller633de332014-05-15 13:48:26 +1000807 u_char type;
808 const u_char *blob;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000809 size_t blen, nbits;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000810 struct sshbuf *subsect = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100811 u_int64_t serial, serial_lo, serial_hi;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000812 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100813 char *key_id = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000814 struct sshkey *ca_key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100815
djm@openbsd.org74de2542014-12-04 01:49:59 +0000816 if ((subsect = sshbuf_new()) == NULL)
817 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100818
djm@openbsd.org74de2542014-12-04 01:49:59 +0000819 /* Header: key, reserved */
820 if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
821 (r = sshbuf_skip_string(buf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100822 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000823 if ((r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100824 goto out;
825
djm@openbsd.org74de2542014-12-04 01:49:59 +0000826 while (sshbuf_len(buf) > 0) {
827 if (subsect != NULL) {
828 sshbuf_free(subsect);
829 subsect = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100830 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000831 if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
832 (r = sshbuf_froms(buf, &subsect)) != 0)
833 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000834 KRL_DBG(("%s: subsection type 0x%02x", __func__, type));
835 /* sshbuf_dump(subsect, stderr); */
Damien Millerf3747bf2013-01-18 11:44:04 +1100836
837 switch (type) {
838 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000839 while (sshbuf_len(subsect) > 0) {
840 if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100841 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000842 if ((r = ssh_krl_revoke_cert_by_serial(krl,
843 ca_key, serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100844 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100845 }
846 break;
847 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000848 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
849 (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100850 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000851 if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
852 ca_key, serial_lo, serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100853 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100854 break;
855 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000856 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000857 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100858 goto out;
859 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000860 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000861 (r = sshbuf_get_bignum2_bytes_direct(subsect,
862 &blob, &blen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100863 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000864 if (bitmap_from_string(bitmap, blob, blen) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000865 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100866 goto out;
867 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000868 nbits = bitmap_nbits(bitmap);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000869 for (serial = 0; serial < (u_int64_t)nbits; serial++) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100870 if (serial > 0 && serial_lo + serial == 0) {
871 error("%s: bitmap wraps u64", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000872 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100873 goto out;
874 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000875 if (!bitmap_test_bit(bitmap, serial))
Damien Millerf3747bf2013-01-18 11:44:04 +1100876 continue;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000877 if ((r = ssh_krl_revoke_cert_by_serial(krl,
878 ca_key, serial_lo + serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100879 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100880 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000881 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100882 bitmap = NULL;
883 break;
884 case KRL_SECTION_CERT_KEY_ID:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000885 while (sshbuf_len(subsect) > 0) {
886 if ((r = sshbuf_get_cstring(subsect,
887 &key_id, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100888 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000889 if ((r = ssh_krl_revoke_cert_by_key_id(krl,
890 ca_key, key_id)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100891 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100892 free(key_id);
893 key_id = NULL;
894 }
895 break;
896 default:
897 error("Unsupported KRL certificate section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000898 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100899 goto out;
900 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000901 if (sshbuf_len(subsect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100902 error("KRL certificate section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +0000903 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100904 goto out;
905 }
906 }
907
djm@openbsd.org74de2542014-12-04 01:49:59 +0000908 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100909 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100910 if (bitmap != NULL)
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000911 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100912 free(key_id);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000913 sshkey_free(ca_key);
914 sshbuf_free(subsect);
915 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100916}
917
918
919/* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
920int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000921ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000922 const struct sshkey **sign_ca_keys, size_t nsign_ca_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100923{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000924 struct sshbuf *copy = NULL, *sect = NULL;
925 struct ssh_krl *krl = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100926 char timestamp[64];
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000927 int r = SSH_ERR_INTERNAL_ERROR, sig_seen;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000928 struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
Damien Miller633de332014-05-15 13:48:26 +1000929 u_char type, *rdata = NULL;
930 const u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000931 size_t i, j, sig_off, sects_off, rlen, blen, nca_used;
932 u_int format_version;
Damien Millerf3747bf2013-01-18 11:44:04 +1100933
Damien Miller30710702013-07-18 16:09:44 +1000934 nca_used = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100935 *krlp = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000936 if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
937 memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100938 debug3("%s: not a KRL", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000939 return SSH_ERR_KRL_BAD_MAGIC;
Damien Millerf3747bf2013-01-18 11:44:04 +1100940 }
941
942 /* Take a copy of the KRL buffer so we can verify its signature later */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000943 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 Millerf3747bf2013-01-18 11:44:04 +1100949
950 if ((krl = ssh_krl_init()) == NULL) {
951 error("%s: alloc failed", __func__);
952 goto out;
953 }
954
djm@openbsd.org74de2542014-12-04 01:49:59 +0000955 if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100956 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100957 if (format_version != KRL_FORMAT_VERSION) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000958 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100959 goto out;
960 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000961 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 Millerf3747bf2013-01-18 11:44:04 +1100966 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100967
968 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
Damien Millerd677ad12013-04-23 15:18:51 +1000969 debug("KRL version %llu generated at %s%s%s",
970 (long long unsigned)krl->krl_version, timestamp,
971 *krl->comment ? ": " : "", krl->comment);
Damien Millerf3747bf2013-01-18 11:44:04 +1100972
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.org74de2542014-12-04 01:49:59 +0000978 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 Millerf3747bf2013-01-18 11:44:04 +1100987 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000988 KRL_DBG(("%s: first pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +1100989 if (type != KRL_SECTION_SIGNATURE) {
990 if (sig_seen) {
991 error("KRL contains non-signature section "
992 "after signature");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000993 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100994 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.org74de2542014-12-04 01:49:59 +00001001 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1002 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001003 goto out;
1004 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001005 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 Millerf3747bf2013-01-18 11:44:04 +11001011 /* Second string component is the signature itself */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001012 if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
1013 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001014 goto out;
1015 }
1016 /* Check signature over entire KRL up to this point */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001017 if ((r = sshkey_verify(key, blob, blen,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001018 sshbuf_ptr(buf), sshbuf_len(buf) - sig_off, 0)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001019 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001020 /* Check if this key has already signed this KRL */
1021 for (i = 0; i < nca_used; i++) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001022 if (sshkey_equal(ca_used[i], key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001023 error("KRL signed more than once with "
1024 "the same key");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001025 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001026 goto out;
1027 }
1028 }
1029 /* Record keys used to sign the KRL */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001030 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 Millerf3747bf2013-01-18 11:44:04 +11001037 ca_used[nca_used++] = key;
1038 key = NULL;
1039 break;
1040 }
1041
djm@openbsd.org74de2542014-12-04 01:49:59 +00001042 if (sshbuf_len(copy) != 0) {
1043 /* Shouldn't happen */
1044 r = SSH_ERR_INTERNAL_ERROR;
1045 goto out;
1046 }
1047
Damien Millerf3747bf2013-01-18 11:44:04 +11001048 /*
1049 * 2nd pass: parse and load the KRL, skipping the header to the point
1050 * where the section start.
1051 */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001052 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.orge7fd9522015-01-13 19:04:35 +00001065 (r = sshbuf_froms(copy, &sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001066 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001067 KRL_DBG(("%s: second pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +11001068
1069 switch (type) {
1070 case KRL_SECTION_CERTIFICATES:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001071 if ((r = parse_revoked_certs(sect, krl)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001072 goto out;
1073 break;
1074 case KRL_SECTION_EXPLICIT_KEY:
1075 case KRL_SECTION_FINGERPRINT_SHA1:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001076 while (sshbuf_len(sect) > 0) {
1077 if ((r = sshbuf_get_string(sect,
1078 &rdata, &rlen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001079 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001080 if (type == KRL_SECTION_FINGERPRINT_SHA1 &&
Damien Miller30710702013-07-18 16:09:44 +10001081 rlen != 20) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001082 error("%s: bad SHA1 length", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001083 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001084 goto out;
1085 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001086 if ((r = revoke_blob(
Damien Millerf3747bf2013-01-18 11:44:04 +11001087 type == KRL_SECTION_EXPLICIT_KEY ?
1088 &krl->revoked_keys : &krl->revoked_sha1s,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001089 rdata, rlen)) != 0)
Damien Miller30710702013-07-18 16:09:44 +10001090 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001091 rdata = NULL; /* revoke_blob frees rdata */
Damien Millerf3747bf2013-01-18 11:44:04 +11001092 }
1093 break;
1094 case KRL_SECTION_SIGNATURE:
1095 /* Handled above, but still need to stay in synch */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001096 sshbuf_reset(sect);
1097 sect = NULL;
1098 if ((r = sshbuf_skip_string(copy)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001099 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001100 break;
1101 default:
1102 error("Unsupported KRL section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001103 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001104 goto out;
1105 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001106 if (sshbuf_len(sect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001107 error("KRL section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +00001108 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001109 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.org74de2542014-12-04 01:49:59 +00001119 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001120 ca_used[i] = NULL;
1121 }
1122 }
1123 if (nca_used && !sig_seen) {
1124 error("All keys used to sign KRL were revoked");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001125 r = SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001126 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.org74de2542014-12-04 01:49:59 +00001136 if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001137 sig_seen = 1;
1138 break;
1139 }
1140 }
1141 }
1142 if (!sig_seen) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001143 r = SSH_ERR_SIGNATURE_INVALID;
Damien Millerf3747bf2013-01-18 11:44:04 +11001144 error("KRL not signed with any trusted key");
1145 goto out;
1146 }
1147 }
1148
1149 *krlp = krl;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001150 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11001151 out:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001152 if (r != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001153 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001154 for (i = 0; i < nca_used; i++)
1155 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001156 free(ca_used);
Damien Miller30710702013-07-18 16:09:44 +10001157 free(rdata);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001158 sshkey_free(key);
1159 sshbuf_free(copy);
1160 sshbuf_free(sect);
1161 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001162}
1163
1164/* Checks whether a given key/cert is revoked. Does not check its CA */
1165static int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001166is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001167{
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.org74de2542014-12-04 01:49:59 +00001172 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001173
1174 /* Check explicitly revoked hashes first */
Damien Miller1d2c4562014-02-04 11:18:20 +11001175 memset(&rb, 0, sizeof(rb));
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001176 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001177 &rb.blob, &rb.len)) != 0)
1178 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001179 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1180 free(rb.blob);
1181 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001182 KRL_DBG(("%s: revoked by key SHA1", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001183 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001184 }
1185
1186 /* Next, explicit keys */
Damien Miller1d2c4562014-02-04 11:18:20 +11001187 memset(&rb, 0, sizeof(rb));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001188 if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1189 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001190 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1191 free(rb.blob);
1192 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001193 KRL_DBG(("%s: revoked by explicit key", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001194 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001195 }
1196
djm@openbsd.org74de2542014-12-04 01:49:59 +00001197 if (!sshkey_is_cert(key))
Damien Millerf3747bf2013-01-18 11:44:04 +11001198 return 0;
1199
1200 /* Check cert revocation */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001201 if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1202 &rc, 0)) != 0)
1203 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001204 if (rc == NULL)
1205 return 0; /* No entry for this CA */
1206
1207 /* Check revocation by cert key ID */
Damien Miller1d2c4562014-02-04 11:18:20 +11001208 memset(&rki, 0, sizeof(rki));
Damien Millerf3747bf2013-01-18 11:44:04 +11001209 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.orge7fd9522015-01-13 19:04:35 +00001212 KRL_DBG(("%s: revoked by key ID", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001213 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001214 }
1215
Damien Miller60565bc2013-02-12 10:56:42 +11001216 /*
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.org74de2542014-12-04 01:49:59 +00001220 if (sshkey_cert_is_legacy(key) || key->cert->serial == 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001221 return 0;
1222
Damien Miller1d2c4562014-02-04 11:18:20 +11001223 memset(&rs, 0, sizeof(rs));
Damien Millerf3747bf2013-01-18 11:44:04 +11001224 rs.lo = rs.hi = key->cert->serial;
1225 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1226 if (ers != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001227 KRL_DBG(("%s: revoked serial %llu matched %llu:%llu", __func__,
Damien Millerf3747bf2013-01-18 11:44:04 +11001228 key->cert->serial, ers->lo, ers->hi));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001229 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001230 }
1231 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
Damien Millerf3747bf2013-01-18 11:44:04 +11001232 return 0;
1233}
1234
1235int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001236ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001237{
1238 int r;
1239
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001240 KRL_DBG(("%s: checking key", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001241 if ((r = is_key_revoked(krl, key)) != 0)
1242 return r;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001243 if (sshkey_is_cert(key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001244 debug2("%s: checking CA key", __func__);
1245 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1246 return r;
1247 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001248 KRL_DBG(("%s: key okay", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001249 return 0;
1250}
1251
Damien Millerf3747bf2013-01-18 11:44:04 +11001252int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001253ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001254{
djm@openbsd.org74de2542014-12-04 01:49:59 +00001255 struct sshbuf *krlbuf = NULL;
1256 struct ssh_krl *krl = NULL;
1257 int oerrno = 0, r, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11001258
1259 if (path == NULL)
1260 return 0;
1261
djm@openbsd.org74de2542014-12-04 01:49:59 +00001262 if ((krlbuf = sshbuf_new()) == NULL)
1263 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +11001264 if ((fd = open(path, O_RDONLY)) == -1) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001265 r = SSH_ERR_SYSTEM_ERROR;
1266 oerrno = errno;
1267 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001268 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001269 if ((r = sshkey_load_file(fd, krlbuf)) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001270 oerrno = errno;
1271 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001272 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001273 if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
1274 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001275 debug2("%s: checking KRL %s", __func__, path);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001276 r = ssh_krl_check_key(krl, key);
1277 out:
1278 close(fd);
1279 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11001280 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001281 if (r != 0)
1282 errno = oerrno;
1283 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001284}