blob: 570a4f7474848e83e8094a502162d887ed10314c [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
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +000017/* $OpenBSD: krl.c,v 1.34 2015/09/02 07:51:12 jsg Exp $ */
Damien Millerf3747bf2013-01-18 11:44:04 +110018
19#include "includes.h"
20
deraadt@openbsd.org087266e2015-01-20 23:14:00 +000021#include <sys/param.h> /* MIN */
Damien Millerf3747bf2013-01-18 11:44:04 +110022#include <sys/types.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 }
djm@openbsd.org669aee92015-01-30 01:10:33 +0000159 sshkey_free(rc->ca_key);
Damien Millerf3747bf2013-01-18 11:44:04 +1100160}
161
162void
163ssh_krl_free(struct ssh_krl *krl)
164{
165 struct revoked_blob *rb, *trb;
166 struct revoked_certs *rc, *trc;
167
168 if (krl == NULL)
169 return;
170
171 free(krl->comment);
172 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
173 RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
174 free(rb->blob);
175 free(rb);
176 }
177 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
178 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
179 free(rb->blob);
180 free(rb);
181 }
182 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
183 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
184 revoked_certs_free(rc);
185 }
186}
187
188void
189ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
190{
191 krl->krl_version = version;
192}
193
djm@openbsd.org74de2542014-12-04 01:49:59 +0000194int
Damien Millerf3747bf2013-01-18 11:44:04 +1100195ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
196{
197 free(krl->comment);
198 if ((krl->comment = strdup(comment)) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000199 return SSH_ERR_ALLOC_FAIL;
200 return 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100201}
202
203/*
204 * Find the revoked_certs struct for a CA key. If allow_create is set then
205 * create a new one in the tree if one did not exist already.
206 */
207static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000208revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100209 struct revoked_certs **rcp, int allow_create)
210{
211 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000212 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100213
214 *rcp = NULL;
215 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org669aee92015-01-30 01:10:33 +0000216 if ((ca_key == NULL && rc->ca_key == NULL) ||
217 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;
djm@openbsd.org669aee92015-01-30 01:10:33 +0000227 if (ca_key == NULL)
228 rc->ca_key = NULL;
229 else if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100230 free(rc);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000231 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100232 }
233 RB_INIT(&rc->revoked_serials);
234 RB_INIT(&rc->revoked_key_ids);
235 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
djm@openbsd.org669aee92015-01-30 01:10:33 +0000236 KRL_DBG(("%s: new CA %s", __func__,
237 ca_key == NULL ? "*" : sshkey_type(ca_key)));
Damien Millerf3747bf2013-01-18 11:44:04 +1100238 *rcp = rc;
239 return 0;
240}
241
242static int
243insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
244{
245 struct revoked_serial rs, *ers, *crs, *irs;
246
247 KRL_DBG(("%s: insert %llu:%llu", __func__, lo, hi));
Damien Miller1d2c4562014-02-04 11:18:20 +1100248 memset(&rs, 0, sizeof(rs));
Damien Millerf3747bf2013-01-18 11:44:04 +1100249 rs.lo = lo;
250 rs.hi = hi;
251 ers = RB_NFIND(revoked_serial_tree, rt, &rs);
252 if (ers == NULL || serial_cmp(ers, &rs) != 0) {
253 /* No entry matches. Just insert */
254 if ((irs = malloc(sizeof(rs))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000255 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100256 memcpy(irs, &rs, sizeof(*irs));
257 ers = RB_INSERT(revoked_serial_tree, rt, irs);
258 if (ers != NULL) {
259 KRL_DBG(("%s: bad: ers != NULL", __func__));
260 /* Shouldn't happen */
Damien Millera7522d92013-01-20 22:35:31 +1100261 free(irs);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000262 return SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100263 }
264 ers = irs;
265 } else {
266 KRL_DBG(("%s: overlap found %llu:%llu", __func__,
267 ers->lo, ers->hi));
268 /*
269 * The inserted entry overlaps an existing one. Grow the
270 * existing entry.
271 */
272 if (ers->lo > lo)
273 ers->lo = lo;
274 if (ers->hi < hi)
275 ers->hi = hi;
276 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000277
Damien Millerf3747bf2013-01-18 11:44:04 +1100278 /*
279 * The inserted or revised range might overlap or abut adjacent ones;
280 * coalesce as necessary.
281 */
282
283 /* Check predecessors */
284 while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
285 KRL_DBG(("%s: pred %llu:%llu", __func__, crs->lo, crs->hi));
286 if (ers->lo != 0 && crs->hi < ers->lo - 1)
287 break;
288 /* This entry overlaps. */
289 if (crs->lo < ers->lo) {
290 ers->lo = crs->lo;
291 KRL_DBG(("%s: pred extend %llu:%llu", __func__,
292 ers->lo, ers->hi));
293 }
294 RB_REMOVE(revoked_serial_tree, rt, crs);
295 free(crs);
296 }
297 /* Check successors */
298 while ((crs = RB_NEXT(revoked_serial_tree, rt, ers)) != NULL) {
299 KRL_DBG(("%s: succ %llu:%llu", __func__, crs->lo, crs->hi));
300 if (ers->hi != (u_int64_t)-1 && crs->lo > ers->hi + 1)
301 break;
302 /* This entry overlaps. */
303 if (crs->hi > ers->hi) {
304 ers->hi = crs->hi;
305 KRL_DBG(("%s: succ extend %llu:%llu", __func__,
306 ers->lo, ers->hi));
307 }
308 RB_REMOVE(revoked_serial_tree, rt, crs);
309 free(crs);
310 }
311 KRL_DBG(("%s: done, final %llu:%llu", __func__, ers->lo, ers->hi));
312 return 0;
313}
314
315int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000316ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100317 u_int64_t serial)
318{
319 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
320}
321
322int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000323ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl,
324 const struct sshkey *ca_key, u_int64_t lo, u_int64_t hi)
Damien Millerf3747bf2013-01-18 11:44:04 +1100325{
326 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000327 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100328
329 if (lo > hi || lo == 0)
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000330 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000331 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
332 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100333 return insert_serial_range(&rc->revoked_serials, lo, hi);
334}
335
336int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000337ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100338 const char *key_id)
339{
340 struct revoked_key_id *rki, *erki;
341 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000342 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100343
djm@openbsd.org74de2542014-12-04 01:49:59 +0000344 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
345 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100346
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000347 KRL_DBG(("%s: revoke %s", __func__, key_id));
Damien Millerf3747bf2013-01-18 11:44:04 +1100348 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
349 (rki->key_id = strdup(key_id)) == NULL) {
350 free(rki);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000351 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100352 }
353 erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
354 if (erki != NULL) {
355 free(rki->key_id);
356 free(rki);
357 }
358 return 0;
359}
360
361/* Convert "key" to a public key blob without any certificate information */
362static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000363plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
Damien Millerf3747bf2013-01-18 11:44:04 +1100364{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000365 struct sshkey *kcopy;
Damien Millerf3747bf2013-01-18 11:44:04 +1100366 int r;
367
djm@openbsd.org74de2542014-12-04 01:49:59 +0000368 if ((r = sshkey_from_private(key, &kcopy)) != 0)
369 return r;
370 if (sshkey_is_cert(kcopy)) {
371 if ((r = sshkey_drop_cert(kcopy)) != 0) {
372 sshkey_free(kcopy);
373 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100374 }
375 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000376 r = sshkey_to_blob(kcopy, blob, blen);
markus@openbsd.org905fe302015-01-12 14:05:19 +0000377 sshkey_free(kcopy);
Damien Miller86687062014-07-02 15:28:02 +1000378 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100379}
380
381/* Revoke a key blob. Ownership of blob is transferred to the tree */
382static int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000383revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
Damien Millerf3747bf2013-01-18 11:44:04 +1100384{
385 struct revoked_blob *rb, *erb;
386
387 if ((rb = calloc(1, sizeof(*rb))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000388 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100389 rb->blob = blob;
390 rb->len = len;
391 erb = RB_INSERT(revoked_blob_tree, rbt, rb);
392 if (erb != NULL) {
393 free(rb->blob);
394 free(rb);
395 }
396 return 0;
397}
398
399int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000400ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100401{
402 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000403 size_t len;
404 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100405
djm@openbsd.org74de2542014-12-04 01:49:59 +0000406 debug3("%s: revoke type %s", __func__, sshkey_type(key));
407 if ((r = plain_key_blob(key, &blob, &len)) != 0)
408 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100409 return revoke_blob(&krl->revoked_keys, blob, len);
410}
411
412int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000413ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100414{
415 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000416 size_t len;
417 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100418
djm@openbsd.org74de2542014-12-04 01:49:59 +0000419 debug3("%s: revoke type %s by sha1", __func__, sshkey_type(key));
djm@openbsd.org56d1c832014-12-21 22:27:55 +0000420 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
421 &blob, &len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000422 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100423 return revoke_blob(&krl->revoked_sha1s, blob, len);
424}
425
426int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000427ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100428{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000429 if (!sshkey_is_cert(key))
Damien Millerf3747bf2013-01-18 11:44:04 +1100430 return ssh_krl_revoke_key_sha1(krl, key);
431
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000432 if (key->cert->serial == 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100433 return ssh_krl_revoke_cert_by_key_id(krl,
434 key->cert->signature_key,
435 key->cert->key_id);
436 } else {
437 return ssh_krl_revoke_cert_by_serial(krl,
438 key->cert->signature_key,
439 key->cert->serial);
440 }
441}
442
443/*
djm@openbsd.org74de2542014-12-04 01:49:59 +0000444 * Select the most compact section type to emit next in a KRL based on
445 * the current section type, the run length of contiguous revoked serial
Damien Millerf3747bf2013-01-18 11:44:04 +1100446 * numbers and the gaps from the last and to the next revoked serial.
447 * Applies a mostly-accurate bit cost model to select the section type
448 * that will minimise the size of the resultant KRL.
449 */
450static int
451choose_next_state(int current_state, u_int64_t contig, int final,
452 u_int64_t last_gap, u_int64_t next_gap, int *force_new_section)
453{
454 int new_state;
455 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
456
457 /*
458 * Avoid unsigned overflows.
459 * The limits are high enough to avoid confusing the calculations.
460 */
461 contig = MIN(contig, 1ULL<<31);
462 last_gap = MIN(last_gap, 1ULL<<31);
463 next_gap = MIN(next_gap, 1ULL<<31);
464
465 /*
466 * Calculate the cost to switch from the current state to candidates.
467 * NB. range sections only ever contain a single range, so their
468 * switching cost is independent of the current_state.
469 */
470 cost_list = cost_bitmap = cost_bitmap_restart = 0;
471 cost_range = 8;
472 switch (current_state) {
473 case KRL_SECTION_CERT_SERIAL_LIST:
474 cost_bitmap_restart = cost_bitmap = 8 + 64;
475 break;
476 case KRL_SECTION_CERT_SERIAL_BITMAP:
477 cost_list = 8;
478 cost_bitmap_restart = 8 + 64;
479 break;
480 case KRL_SECTION_CERT_SERIAL_RANGE:
481 case 0:
482 cost_bitmap_restart = cost_bitmap = 8 + 64;
483 cost_list = 8;
484 }
485
486 /* Estimate base cost in bits of each section type */
487 cost_list += 64 * contig + (final ? 0 : 8+64);
488 cost_range += (2 * 64) + (final ? 0 : 8+64);
489 cost_bitmap += last_gap + contig + (final ? 0 : MIN(next_gap, 8+64));
490 cost_bitmap_restart += contig + (final ? 0 : MIN(next_gap, 8+64));
491
492 /* Convert to byte costs for actual comparison */
493 cost_list = (cost_list + 7) / 8;
494 cost_bitmap = (cost_bitmap + 7) / 8;
495 cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
496 cost_range = (cost_range + 7) / 8;
497
498 /* Now pick the best choice */
499 *force_new_section = 0;
500 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
501 cost = cost_bitmap;
502 if (cost_range < cost) {
503 new_state = KRL_SECTION_CERT_SERIAL_RANGE;
504 cost = cost_range;
505 }
506 if (cost_list < cost) {
507 new_state = KRL_SECTION_CERT_SERIAL_LIST;
508 cost = cost_list;
509 }
510 if (cost_bitmap_restart < cost) {
511 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
512 *force_new_section = 1;
513 cost = cost_bitmap_restart;
514 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000515 KRL_DBG(("%s: contig %llu last_gap %llu next_gap %llu final %d, costs:"
Damien Millerf3747bf2013-01-18 11:44:04 +1100516 "list %llu range %llu bitmap %llu new bitmap %llu, "
Damien Millerd677ad12013-04-23 15:18:51 +1000517 "selected 0x%02x%s", __func__, (long long unsigned)contig,
518 (long long unsigned)last_gap, (long long unsigned)next_gap, final,
519 (long long unsigned)cost_list, (long long unsigned)cost_range,
520 (long long unsigned)cost_bitmap,
521 (long long unsigned)cost_bitmap_restart, new_state,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000522 *force_new_section ? " restart" : ""));
Damien Millerf3747bf2013-01-18 11:44:04 +1100523 return new_state;
524}
525
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000526static int
527put_bitmap(struct sshbuf *buf, struct bitmap *bitmap)
528{
529 size_t len;
530 u_char *blob;
531 int r;
532
533 len = bitmap_nbytes(bitmap);
534 if ((blob = malloc(len)) == NULL)
535 return SSH_ERR_ALLOC_FAIL;
536 if (bitmap_to_string(bitmap, blob, len) != 0) {
537 free(blob);
538 return SSH_ERR_INTERNAL_ERROR;
539 }
540 r = sshbuf_put_bignum2_bytes(buf, blob, len);
541 free(blob);
542 return r;
543}
544
Damien Millerf3747bf2013-01-18 11:44:04 +1100545/* Generate a KRL_SECTION_CERTIFICATES KRL section */
546static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000547revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
Damien Millerf3747bf2013-01-18 11:44:04 +1100548{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000549 int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100550 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
551 struct revoked_serial *rs, *nrs;
552 struct revoked_key_id *rki;
553 int next_state, state = 0;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000554 struct sshbuf *sect;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000555 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100556
djm@openbsd.org74de2542014-12-04 01:49:59 +0000557 if ((sect = sshbuf_new()) == NULL)
558 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100559
djm@openbsd.org669aee92015-01-30 01:10:33 +0000560 /* Store the header: optional CA scope key, reserved */
561 if (rc->ca_key == NULL) {
562 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
563 goto out;
564 } else {
565 if ((r = sshkey_puts(rc->ca_key, buf)) != 0)
566 goto out;
567 }
568 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000569 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100570
Damien Millerf3747bf2013-01-18 11:44:04 +1100571 /* Store the revoked serials. */
572 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
573 rs != NULL;
574 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000575 KRL_DBG(("%s: serial %llu:%llu state 0x%02x", __func__,
Damien Millerd677ad12013-04-23 15:18:51 +1000576 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000577 state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100578
579 /* Check contiguous length and gap to next section (if any) */
580 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
581 final = nrs == NULL;
582 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
583 contig = 1 + (rs->hi - rs->lo);
584
585 /* Choose next state based on these */
586 next_state = choose_next_state(state, contig, final,
587 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
588
589 /*
590 * If the current section is a range section or has a different
591 * type to the next section, then finish it off now.
592 */
593 if (state != 0 && (force_new_sect || next_state != state ||
594 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000595 KRL_DBG(("%s: finish state 0x%02x", __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100596 switch (state) {
597 case KRL_SECTION_CERT_SERIAL_LIST:
598 case KRL_SECTION_CERT_SERIAL_RANGE:
599 break;
600 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000601 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000602 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000603 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100604 bitmap = NULL;
605 break;
606 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000607 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
608 (r = sshbuf_put_stringb(buf, sect)) != 0)
609 goto out;
610 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100611 }
612
613 /* If we are starting a new section then prepare it now */
614 if (next_state != state || force_new_sect) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000615 KRL_DBG(("%s: start state 0x%02x", __func__,
616 next_state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100617 state = next_state;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000618 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100619 switch (state) {
620 case KRL_SECTION_CERT_SERIAL_LIST:
621 case KRL_SECTION_CERT_SERIAL_RANGE:
622 break;
623 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000624 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000625 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100626 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000627 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100628 bitmap_start = rs->lo;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000629 if ((r = sshbuf_put_u64(sect,
630 bitmap_start)) != 0)
631 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100632 break;
633 }
634 }
635
636 /* Perform section-specific processing */
637 switch (state) {
638 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000639 for (i = 0; i < contig; i++) {
640 if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
641 goto out;
642 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100643 break;
644 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000645 if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
646 (r = sshbuf_put_u64(sect, rs->hi)) != 0)
647 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100648 break;
649 case KRL_SECTION_CERT_SERIAL_BITMAP:
650 if (rs->lo - bitmap_start > INT_MAX) {
651 error("%s: insane bitmap gap", __func__);
652 goto out;
653 }
654 for (i = 0; i < contig; i++) {
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000655 if (bitmap_set_bit(bitmap,
656 rs->lo + i - bitmap_start) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000657 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100658 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000659 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100660 }
661 break;
662 }
663 last = rs->hi;
664 }
665 /* Flush the remaining section, if any */
666 if (state != 0) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000667 KRL_DBG(("%s: serial final flush for state 0x%02x",
668 __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100669 switch (state) {
670 case KRL_SECTION_CERT_SERIAL_LIST:
671 case KRL_SECTION_CERT_SERIAL_RANGE:
672 break;
673 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000674 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000675 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000676 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100677 bitmap = NULL;
678 break;
679 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000680 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
681 (r = sshbuf_put_stringb(buf, sect)) != 0)
682 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100683 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000684 KRL_DBG(("%s: serial done ", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +1100685
686 /* Now output a section for any revocations by key ID */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000687 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100688 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000689 KRL_DBG(("%s: key ID %s", __func__, rki->key_id));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000690 if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
691 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100692 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000693 if (sshbuf_len(sect) != 0) {
694 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
695 (r = sshbuf_put_stringb(buf, sect)) != 0)
696 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100697 }
698 r = 0;
699 out:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000700 bitmap_free(bitmap);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000701 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100702 return r;
703}
704
705int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000706ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
707 const struct sshkey **sign_keys, u_int nsign_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100708{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000709 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100710 struct revoked_certs *rc;
711 struct revoked_blob *rb;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000712 struct sshbuf *sect;
713 u_char *sblob = NULL;
714 size_t slen, i;
Damien Millerf3747bf2013-01-18 11:44:04 +1100715
716 if (krl->generated_date == 0)
717 krl->generated_date = time(NULL);
718
djm@openbsd.org74de2542014-12-04 01:49:59 +0000719 if ((sect = sshbuf_new()) == NULL)
720 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100721
722 /* Store the header */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000723 if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
724 (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
725 (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000726 (r = sshbuf_put_u64(buf, krl->generated_date)) != 0 ||
djm@openbsd.org74de2542014-12-04 01:49:59 +0000727 (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
728 (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
729 (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
730 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100731
732 /* Store sections for revoked certificates */
733 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000734 sshbuf_reset(sect);
735 if ((r = revoked_certs_generate(rc, sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100736 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000737 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
738 (r = sshbuf_put_stringb(buf, sect)) != 0)
739 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100740 }
741
742 /* Finally, output sections for revocations by public key/hash */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000743 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100744 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000745 KRL_DBG(("%s: key len %zu ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000746 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000747 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100748 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000749 if (sshbuf_len(sect) != 0) {
750 if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
751 (r = sshbuf_put_stringb(buf, sect)) != 0)
752 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100753 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000754 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100755 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000756 KRL_DBG(("%s: hash len %zu ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000757 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000758 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100759 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000760 if (sshbuf_len(sect) != 0) {
761 if ((r = sshbuf_put_u8(buf,
762 KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
763 (r = sshbuf_put_stringb(buf, sect)) != 0)
764 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100765 }
766
767 for (i = 0; i < nsign_keys; i++) {
djm@openbsd.org60b18252015-01-26 02:59:11 +0000768 KRL_DBG(("%s: signature key %s", __func__,
769 sshkey_ssh_name(sign_keys[i])));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000770 if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
djm@openbsd.org60b18252015-01-26 02:59:11 +0000771 (r = sshkey_puts(sign_keys[i], buf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100772 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000773
774 if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
djm@openbsd.org9d27fb72015-06-24 23:47:23 +0000775 sshbuf_ptr(buf), sshbuf_len(buf), 0)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000776 goto out;
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000777 KRL_DBG(("%s: signature sig len %zu", __func__, slen));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000778 if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
779 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100780 }
781
782 r = 0;
783 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100784 free(sblob);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000785 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100786 return r;
787}
788
789static void
790format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
791{
792 time_t t;
793 struct tm *tm;
794
795 t = timestamp;
796 tm = localtime(&t);
djm@openbsd.orgb6de5ac2014-11-21 01:00:38 +0000797 if (tm == NULL)
deraadt@openbsd.orgf101d822015-01-18 14:01:00 +0000798 strlcpy(ts, "<INVALID>", nts);
djm@openbsd.orgb6de5ac2014-11-21 01:00:38 +0000799 else {
800 *ts = '\0';
801 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
802 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100803}
804
805static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000806parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +1100807{
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000808 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller633de332014-05-15 13:48:26 +1000809 u_char type;
810 const u_char *blob;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000811 size_t blen, nbits;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000812 struct sshbuf *subsect = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100813 u_int64_t serial, serial_lo, serial_hi;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000814 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100815 char *key_id = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000816 struct sshkey *ca_key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100817
djm@openbsd.org74de2542014-12-04 01:49:59 +0000818 if ((subsect = sshbuf_new()) == NULL)
819 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100820
djm@openbsd.org74de2542014-12-04 01:49:59 +0000821 /* Header: key, reserved */
822 if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
823 (r = sshbuf_skip_string(buf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100824 goto out;
djm@openbsd.org669aee92015-01-30 01:10:33 +0000825 if (blen != 0 && (r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100826 goto out;
827
djm@openbsd.org74de2542014-12-04 01:49:59 +0000828 while (sshbuf_len(buf) > 0) {
829 if (subsect != NULL) {
830 sshbuf_free(subsect);
831 subsect = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100832 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000833 if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
834 (r = sshbuf_froms(buf, &subsect)) != 0)
835 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000836 KRL_DBG(("%s: subsection type 0x%02x", __func__, type));
837 /* sshbuf_dump(subsect, stderr); */
Damien Millerf3747bf2013-01-18 11:44:04 +1100838
839 switch (type) {
840 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000841 while (sshbuf_len(subsect) > 0) {
842 if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100843 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000844 if ((r = ssh_krl_revoke_cert_by_serial(krl,
845 ca_key, serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100846 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100847 }
848 break;
849 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000850 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
851 (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100852 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000853 if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
854 ca_key, serial_lo, serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100855 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100856 break;
857 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000858 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000859 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100860 goto out;
861 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000862 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000863 (r = sshbuf_get_bignum2_bytes_direct(subsect,
864 &blob, &blen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100865 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000866 if (bitmap_from_string(bitmap, blob, blen) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000867 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100868 goto out;
869 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000870 nbits = bitmap_nbits(bitmap);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000871 for (serial = 0; serial < (u_int64_t)nbits; serial++) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100872 if (serial > 0 && serial_lo + serial == 0) {
873 error("%s: bitmap wraps u64", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000874 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100875 goto out;
876 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000877 if (!bitmap_test_bit(bitmap, serial))
Damien Millerf3747bf2013-01-18 11:44:04 +1100878 continue;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000879 if ((r = ssh_krl_revoke_cert_by_serial(krl,
880 ca_key, serial_lo + serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100881 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100882 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000883 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100884 bitmap = NULL;
885 break;
886 case KRL_SECTION_CERT_KEY_ID:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000887 while (sshbuf_len(subsect) > 0) {
888 if ((r = sshbuf_get_cstring(subsect,
889 &key_id, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100890 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000891 if ((r = ssh_krl_revoke_cert_by_key_id(krl,
892 ca_key, key_id)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100893 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100894 free(key_id);
895 key_id = NULL;
896 }
897 break;
898 default:
899 error("Unsupported KRL certificate section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000900 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100901 goto out;
902 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000903 if (sshbuf_len(subsect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100904 error("KRL certificate section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +0000905 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100906 goto out;
907 }
908 }
909
djm@openbsd.org74de2542014-12-04 01:49:59 +0000910 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100911 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100912 if (bitmap != NULL)
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000913 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100914 free(key_id);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000915 sshkey_free(ca_key);
916 sshbuf_free(subsect);
917 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100918}
919
920
921/* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
922int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000923ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000924 const struct sshkey **sign_ca_keys, size_t nsign_ca_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100925{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000926 struct sshbuf *copy = NULL, *sect = NULL;
927 struct ssh_krl *krl = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100928 char timestamp[64];
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000929 int r = SSH_ERR_INTERNAL_ERROR, sig_seen;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000930 struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
Damien Miller633de332014-05-15 13:48:26 +1000931 u_char type, *rdata = NULL;
932 const u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000933 size_t i, j, sig_off, sects_off, rlen, blen, nca_used;
934 u_int format_version;
Damien Millerf3747bf2013-01-18 11:44:04 +1100935
Damien Miller30710702013-07-18 16:09:44 +1000936 nca_used = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100937 *krlp = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000938 if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
939 memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100940 debug3("%s: not a KRL", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000941 return SSH_ERR_KRL_BAD_MAGIC;
Damien Millerf3747bf2013-01-18 11:44:04 +1100942 }
943
944 /* Take a copy of the KRL buffer so we can verify its signature later */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000945 if ((copy = sshbuf_fromb(buf)) == NULL) {
946 r = SSH_ERR_ALLOC_FAIL;
947 goto out;
948 }
949 if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0)
950 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100951
952 if ((krl = ssh_krl_init()) == NULL) {
953 error("%s: alloc failed", __func__);
954 goto out;
955 }
956
djm@openbsd.org74de2542014-12-04 01:49:59 +0000957 if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100958 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100959 if (format_version != KRL_FORMAT_VERSION) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000960 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100961 goto out;
962 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000963 if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
964 (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
965 (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
966 (r = sshbuf_skip_string(copy)) != 0 ||
967 (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100968 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100969
970 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
Damien Millerd677ad12013-04-23 15:18:51 +1000971 debug("KRL version %llu generated at %s%s%s",
972 (long long unsigned)krl->krl_version, timestamp,
973 *krl->comment ? ": " : "", krl->comment);
Damien Millerf3747bf2013-01-18 11:44:04 +1100974
975 /*
976 * 1st pass: verify signatures, if any. This is done to avoid
977 * detailed parsing of data whose provenance is unverified.
978 */
979 sig_seen = 0;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000980 if (sshbuf_len(buf) < sshbuf_len(copy)) {
981 /* Shouldn't happen */
982 r = SSH_ERR_INTERNAL_ERROR;
983 goto out;
984 }
985 sects_off = sshbuf_len(buf) - sshbuf_len(copy);
986 while (sshbuf_len(copy) > 0) {
987 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
988 (r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100989 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000990 KRL_DBG(("%s: first pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +1100991 if (type != KRL_SECTION_SIGNATURE) {
992 if (sig_seen) {
993 error("KRL contains non-signature section "
994 "after signature");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000995 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100996 goto out;
997 }
998 /* Not interested for now. */
999 continue;
1000 }
1001 sig_seen = 1;
1002 /* First string component is the signing key */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001003 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1004 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001005 goto out;
1006 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001007 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1008 /* Shouldn't happen */
1009 r = SSH_ERR_INTERNAL_ERROR;
1010 goto out;
1011 }
1012 sig_off = sshbuf_len(buf) - sshbuf_len(copy);
Damien Millerf3747bf2013-01-18 11:44:04 +11001013 /* Second string component is the signature itself */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001014 if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
1015 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001016 goto out;
1017 }
1018 /* Check signature over entire KRL up to this point */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001019 if ((r = sshkey_verify(key, blob, blen,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001020 sshbuf_ptr(buf), sshbuf_len(buf) - sig_off, 0)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001021 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001022 /* Check if this key has already signed this KRL */
1023 for (i = 0; i < nca_used; i++) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001024 if (sshkey_equal(ca_used[i], key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001025 error("KRL signed more than once with "
1026 "the same key");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001027 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001028 goto out;
1029 }
1030 }
1031 /* Record keys used to sign the KRL */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001032 tmp_ca_used = reallocarray(ca_used, nca_used + 1,
1033 sizeof(*ca_used));
1034 if (tmp_ca_used == NULL) {
1035 r = SSH_ERR_ALLOC_FAIL;
1036 goto out;
1037 }
1038 ca_used = tmp_ca_used;
Damien Millerf3747bf2013-01-18 11:44:04 +11001039 ca_used[nca_used++] = key;
1040 key = NULL;
1041 break;
1042 }
1043
djm@openbsd.org74de2542014-12-04 01:49:59 +00001044 if (sshbuf_len(copy) != 0) {
1045 /* Shouldn't happen */
1046 r = SSH_ERR_INTERNAL_ERROR;
1047 goto out;
1048 }
1049
Damien Millerf3747bf2013-01-18 11:44:04 +11001050 /*
1051 * 2nd pass: parse and load the KRL, skipping the header to the point
1052 * where the section start.
1053 */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001054 sshbuf_free(copy);
1055 if ((copy = sshbuf_fromb(buf)) == NULL) {
1056 r = SSH_ERR_ALLOC_FAIL;
1057 goto out;
1058 }
1059 if ((r = sshbuf_consume(copy, sects_off)) != 0)
1060 goto out;
1061 while (sshbuf_len(copy) > 0) {
1062 if (sect != NULL) {
1063 sshbuf_free(sect);
1064 sect = NULL;
1065 }
1066 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001067 (r = sshbuf_froms(copy, &sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001068 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001069 KRL_DBG(("%s: second pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +11001070
1071 switch (type) {
1072 case KRL_SECTION_CERTIFICATES:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001073 if ((r = parse_revoked_certs(sect, krl)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001074 goto out;
1075 break;
1076 case KRL_SECTION_EXPLICIT_KEY:
1077 case KRL_SECTION_FINGERPRINT_SHA1:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001078 while (sshbuf_len(sect) > 0) {
1079 if ((r = sshbuf_get_string(sect,
1080 &rdata, &rlen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001081 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001082 if (type == KRL_SECTION_FINGERPRINT_SHA1 &&
Damien Miller30710702013-07-18 16:09:44 +10001083 rlen != 20) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001084 error("%s: bad SHA1 length", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001085 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001086 goto out;
1087 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001088 if ((r = revoke_blob(
Damien Millerf3747bf2013-01-18 11:44:04 +11001089 type == KRL_SECTION_EXPLICIT_KEY ?
1090 &krl->revoked_keys : &krl->revoked_sha1s,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001091 rdata, rlen)) != 0)
Damien Miller30710702013-07-18 16:09:44 +10001092 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001093 rdata = NULL; /* revoke_blob frees rdata */
Damien Millerf3747bf2013-01-18 11:44:04 +11001094 }
1095 break;
1096 case KRL_SECTION_SIGNATURE:
1097 /* Handled above, but still need to stay in synch */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001098 sshbuf_reset(sect);
1099 sect = NULL;
1100 if ((r = sshbuf_skip_string(copy)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001101 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001102 break;
1103 default:
1104 error("Unsupported KRL section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001105 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001106 goto out;
1107 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001108 if (sshbuf_len(sect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001109 error("KRL section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +00001110 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001111 goto out;
1112 }
1113 }
1114
1115 /* Check that the key(s) used to sign the KRL weren't revoked */
1116 sig_seen = 0;
1117 for (i = 0; i < nca_used; i++) {
1118 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1119 sig_seen = 1;
1120 else {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001121 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001122 ca_used[i] = NULL;
1123 }
1124 }
1125 if (nca_used && !sig_seen) {
1126 error("All keys used to sign KRL were revoked");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001127 r = SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001128 goto out;
1129 }
1130
1131 /* If we have CA keys, then verify that one was used to sign the KRL */
1132 if (sig_seen && nsign_ca_keys != 0) {
1133 sig_seen = 0;
1134 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1135 for (j = 0; j < nca_used; j++) {
1136 if (ca_used[j] == NULL)
1137 continue;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001138 if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001139 sig_seen = 1;
1140 break;
1141 }
1142 }
1143 }
1144 if (!sig_seen) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001145 r = SSH_ERR_SIGNATURE_INVALID;
Damien Millerf3747bf2013-01-18 11:44:04 +11001146 error("KRL not signed with any trusted key");
1147 goto out;
1148 }
1149 }
1150
1151 *krlp = krl;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001152 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11001153 out:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001154 if (r != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001155 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001156 for (i = 0; i < nca_used; i++)
1157 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001158 free(ca_used);
Damien Miller30710702013-07-18 16:09:44 +10001159 free(rdata);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001160 sshkey_free(key);
1161 sshbuf_free(copy);
1162 sshbuf_free(sect);
1163 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001164}
1165
djm@openbsd.org669aee92015-01-30 01:10:33 +00001166/* Checks certificate serial number and key ID revocation */
1167static int
1168is_cert_revoked(const struct sshkey *key, struct revoked_certs *rc)
1169{
1170 struct revoked_serial rs, *ers;
1171 struct revoked_key_id rki, *erki;
1172
1173 /* Check revocation by cert key ID */
1174 memset(&rki, 0, sizeof(rki));
1175 rki.key_id = key->cert->key_id;
1176 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1177 if (erki != NULL) {
1178 KRL_DBG(("%s: revoked by key ID", __func__));
1179 return SSH_ERR_KEY_REVOKED;
1180 }
1181
1182 /*
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001183 * Zero serials numbers are ignored (it's the default when the
1184 * CA doesn't specify one).
djm@openbsd.org669aee92015-01-30 01:10:33 +00001185 */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001186 if (key->cert->serial == 0)
djm@openbsd.org669aee92015-01-30 01:10:33 +00001187 return 0;
1188
1189 memset(&rs, 0, sizeof(rs));
1190 rs.lo = rs.hi = key->cert->serial;
1191 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1192 if (ers != NULL) {
1193 KRL_DBG(("%s: revoked serial %llu matched %llu:%llu", __func__,
1194 key->cert->serial, ers->lo, ers->hi));
1195 return SSH_ERR_KEY_REVOKED;
1196 }
1197 return 0;
1198}
1199
Damien Millerf3747bf2013-01-18 11:44:04 +11001200/* Checks whether a given key/cert is revoked. Does not check its CA */
1201static int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001202is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001203{
1204 struct revoked_blob rb, *erb;
Damien Millerf3747bf2013-01-18 11:44:04 +11001205 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001206 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001207
1208 /* Check explicitly revoked hashes first */
Damien Miller1d2c4562014-02-04 11:18:20 +11001209 memset(&rb, 0, sizeof(rb));
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001210 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001211 &rb.blob, &rb.len)) != 0)
1212 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001213 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1214 free(rb.blob);
1215 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001216 KRL_DBG(("%s: revoked by key SHA1", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001217 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001218 }
1219
1220 /* Next, explicit keys */
Damien Miller1d2c4562014-02-04 11:18:20 +11001221 memset(&rb, 0, sizeof(rb));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001222 if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1223 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001224 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1225 free(rb.blob);
1226 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001227 KRL_DBG(("%s: revoked by explicit key", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001228 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001229 }
1230
djm@openbsd.org74de2542014-12-04 01:49:59 +00001231 if (!sshkey_is_cert(key))
Damien Millerf3747bf2013-01-18 11:44:04 +11001232 return 0;
1233
djm@openbsd.org669aee92015-01-30 01:10:33 +00001234 /* Check cert revocation for the specified CA */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001235 if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1236 &rc, 0)) != 0)
1237 return r;
djm@openbsd.org669aee92015-01-30 01:10:33 +00001238 if (rc != NULL) {
1239 if ((r = is_cert_revoked(key, rc)) != 0)
1240 return r;
1241 }
1242 /* Check cert revocation for the wildcard CA */
1243 if ((r = revoked_certs_for_ca_key(krl, NULL, &rc, 0)) != 0)
1244 return r;
1245 if (rc != NULL) {
1246 if ((r = is_cert_revoked(key, rc)) != 0)
1247 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001248 }
1249
Damien Millerf3747bf2013-01-18 11:44:04 +11001250 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
Damien Millerf3747bf2013-01-18 11:44:04 +11001251 return 0;
1252}
1253
1254int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001255ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001256{
1257 int r;
1258
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001259 KRL_DBG(("%s: checking key", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001260 if ((r = is_key_revoked(krl, key)) != 0)
1261 return r;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001262 if (sshkey_is_cert(key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001263 debug2("%s: checking CA key", __func__);
1264 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1265 return r;
1266 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001267 KRL_DBG(("%s: key okay", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001268 return 0;
1269}
1270
Damien Millerf3747bf2013-01-18 11:44:04 +11001271int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001272ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001273{
djm@openbsd.org74de2542014-12-04 01:49:59 +00001274 struct sshbuf *krlbuf = NULL;
1275 struct ssh_krl *krl = NULL;
1276 int oerrno = 0, r, fd;
Damien Millerf3747bf2013-01-18 11:44:04 +11001277
1278 if (path == NULL)
1279 return 0;
1280
djm@openbsd.org74de2542014-12-04 01:49:59 +00001281 if ((krlbuf = sshbuf_new()) == NULL)
1282 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +11001283 if ((fd = open(path, O_RDONLY)) == -1) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001284 r = SSH_ERR_SYSTEM_ERROR;
1285 oerrno = errno;
1286 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001287 }
djm@openbsd.org1195f4c2015-01-08 10:14:08 +00001288 if ((r = sshkey_load_file(fd, krlbuf)) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001289 oerrno = errno;
1290 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001291 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001292 if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
1293 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001294 debug2("%s: checking KRL %s", __func__, path);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001295 r = ssh_krl_check_key(krl, key);
1296 out:
1297 close(fd);
1298 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11001299 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001300 if (r != 0)
1301 errno = oerrno;
1302 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001303}