blob: 03476dedd4e1e7835f5cc8cbb107447e86f3b7da [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
djm@openbsd.org99aa8032020-01-25 23:02:13 +000017/* $OpenBSD: krl.c,v 1.47 2020/01/25 23:02:13 djm Exp $ */
Damien Millerf3747bf2013-01-18 11:44:04 +110018
19#include "includes.h"
20
21#include <sys/types.h>
Damien Millerd60b2102013-01-20 22:49:58 +110022#include <openbsd-compat/sys-tree.h>
23#include <openbsd-compat/sys-queue.h>
Damien Millerf3747bf2013-01-18 11:44:04 +110024
25#include <errno.h>
26#include <fcntl.h>
27#include <limits.h>
Darren Tucker2ea60312019-07-23 22:11:50 +100028#include <stdlib.h>
Damien Millerf3747bf2013-01-18 11:44:04 +110029#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;
djm@openbsd.org9405c622018-09-12 01:21:34 +0000100 struct revoked_blob_tree revoked_sha256s;
Damien Millerf3747bf2013-01-18 11:44:04 +1100101 struct revoked_certs_list revoked_certs;
102};
103
104/* Return equal if a and b overlap */
105static int
106serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
107{
108 if (a->hi >= b->lo && a->lo <= b->hi)
109 return 0;
110 return a->lo < b->lo ? -1 : 1;
111}
112
113static int
114key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
115{
116 return strcmp(a->key_id, b->key_id);
117}
118
119static int
120blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
121{
122 int r;
123
124 if (a->len != b->len) {
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000125 if ((r = memcmp(a->blob, b->blob, MINIMUM(a->len, b->len))) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100126 return r;
127 return a->len > b->len ? 1 : -1;
128 } else
129 return memcmp(a->blob, b->blob, a->len);
130}
131
132struct ssh_krl *
133ssh_krl_init(void)
134{
135 struct ssh_krl *krl;
136
137 if ((krl = calloc(1, sizeof(*krl))) == NULL)
138 return NULL;
139 RB_INIT(&krl->revoked_keys);
140 RB_INIT(&krl->revoked_sha1s);
djm@openbsd.org9405c622018-09-12 01:21:34 +0000141 RB_INIT(&krl->revoked_sha256s);
Damien Millerf3747bf2013-01-18 11:44:04 +1100142 TAILQ_INIT(&krl->revoked_certs);
143 return krl;
144}
145
146static void
147revoked_certs_free(struct revoked_certs *rc)
148{
149 struct revoked_serial *rs, *trs;
150 struct revoked_key_id *rki, *trki;
151
152 RB_FOREACH_SAFE(rs, revoked_serial_tree, &rc->revoked_serials, trs) {
153 RB_REMOVE(revoked_serial_tree, &rc->revoked_serials, rs);
154 free(rs);
155 }
156 RB_FOREACH_SAFE(rki, revoked_key_id_tree, &rc->revoked_key_ids, trki) {
157 RB_REMOVE(revoked_key_id_tree, &rc->revoked_key_ids, rki);
158 free(rki->key_id);
159 free(rki);
160 }
djm@openbsd.org669aee92015-01-30 01:10:33 +0000161 sshkey_free(rc->ca_key);
Damien Millerf3747bf2013-01-18 11:44:04 +1100162}
163
164void
165ssh_krl_free(struct ssh_krl *krl)
166{
167 struct revoked_blob *rb, *trb;
168 struct revoked_certs *rc, *trc;
169
170 if (krl == NULL)
171 return;
172
173 free(krl->comment);
174 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
175 RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
176 free(rb->blob);
177 free(rb);
178 }
179 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
180 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
181 free(rb->blob);
182 free(rb);
183 }
djm@openbsd.org9405c622018-09-12 01:21:34 +0000184 RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha256s, trb) {
185 RB_REMOVE(revoked_blob_tree, &krl->revoked_sha256s, rb);
186 free(rb->blob);
187 free(rb);
188 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100189 TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
190 TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
191 revoked_certs_free(rc);
192 }
193}
194
195void
196ssh_krl_set_version(struct ssh_krl *krl, u_int64_t version)
197{
198 krl->krl_version = version;
199}
200
djm@openbsd.org74de2542014-12-04 01:49:59 +0000201int
Damien Millerf3747bf2013-01-18 11:44:04 +1100202ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
203{
204 free(krl->comment);
205 if ((krl->comment = strdup(comment)) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000206 return SSH_ERR_ALLOC_FAIL;
207 return 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100208}
209
210/*
211 * Find the revoked_certs struct for a CA key. If allow_create is set then
212 * create a new one in the tree if one did not exist already.
213 */
214static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000215revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100216 struct revoked_certs **rcp, int allow_create)
217{
218 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000219 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100220
221 *rcp = NULL;
222 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org669aee92015-01-30 01:10:33 +0000223 if ((ca_key == NULL && rc->ca_key == NULL) ||
224 sshkey_equal(rc->ca_key, ca_key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100225 *rcp = rc;
226 return 0;
227 }
228 }
229 if (!allow_create)
230 return 0;
231 /* If this CA doesn't exist in the list then add it now */
232 if ((rc = calloc(1, sizeof(*rc))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000233 return SSH_ERR_ALLOC_FAIL;
djm@openbsd.org669aee92015-01-30 01:10:33 +0000234 if (ca_key == NULL)
235 rc->ca_key = NULL;
236 else if ((r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100237 free(rc);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000238 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100239 }
240 RB_INIT(&rc->revoked_serials);
241 RB_INIT(&rc->revoked_key_ids);
242 TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
djm@openbsd.org669aee92015-01-30 01:10:33 +0000243 KRL_DBG(("%s: new CA %s", __func__,
244 ca_key == NULL ? "*" : sshkey_type(ca_key)));
Damien Millerf3747bf2013-01-18 11:44:04 +1100245 *rcp = rc;
246 return 0;
247}
248
249static int
250insert_serial_range(struct revoked_serial_tree *rt, u_int64_t lo, u_int64_t hi)
251{
252 struct revoked_serial rs, *ers, *crs, *irs;
253
254 KRL_DBG(("%s: insert %llu:%llu", __func__, lo, hi));
Damien Miller1d2c4562014-02-04 11:18:20 +1100255 memset(&rs, 0, sizeof(rs));
Damien Millerf3747bf2013-01-18 11:44:04 +1100256 rs.lo = lo;
257 rs.hi = hi;
258 ers = RB_NFIND(revoked_serial_tree, rt, &rs);
259 if (ers == NULL || serial_cmp(ers, &rs) != 0) {
260 /* No entry matches. Just insert */
261 if ((irs = malloc(sizeof(rs))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000262 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100263 memcpy(irs, &rs, sizeof(*irs));
264 ers = RB_INSERT(revoked_serial_tree, rt, irs);
265 if (ers != NULL) {
266 KRL_DBG(("%s: bad: ers != NULL", __func__));
267 /* Shouldn't happen */
Damien Millera7522d92013-01-20 22:35:31 +1100268 free(irs);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000269 return SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100270 }
271 ers = irs;
272 } else {
273 KRL_DBG(("%s: overlap found %llu:%llu", __func__,
274 ers->lo, ers->hi));
275 /*
276 * The inserted entry overlaps an existing one. Grow the
277 * existing entry.
278 */
279 if (ers->lo > lo)
280 ers->lo = lo;
281 if (ers->hi < hi)
282 ers->hi = hi;
283 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000284
Damien Millerf3747bf2013-01-18 11:44:04 +1100285 /*
286 * The inserted or revised range might overlap or abut adjacent ones;
287 * coalesce as necessary.
288 */
289
290 /* Check predecessors */
291 while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
292 KRL_DBG(("%s: pred %llu:%llu", __func__, crs->lo, crs->hi));
293 if (ers->lo != 0 && crs->hi < ers->lo - 1)
294 break;
295 /* This entry overlaps. */
296 if (crs->lo < ers->lo) {
297 ers->lo = crs->lo;
298 KRL_DBG(("%s: pred extend %llu:%llu", __func__,
299 ers->lo, ers->hi));
300 }
301 RB_REMOVE(revoked_serial_tree, rt, crs);
302 free(crs);
303 }
304 /* Check successors */
305 while ((crs = RB_NEXT(revoked_serial_tree, rt, ers)) != NULL) {
306 KRL_DBG(("%s: succ %llu:%llu", __func__, crs->lo, crs->hi));
307 if (ers->hi != (u_int64_t)-1 && crs->lo > ers->hi + 1)
308 break;
309 /* This entry overlaps. */
310 if (crs->hi > ers->hi) {
311 ers->hi = crs->hi;
312 KRL_DBG(("%s: succ extend %llu:%llu", __func__,
313 ers->lo, ers->hi));
314 }
315 RB_REMOVE(revoked_serial_tree, rt, crs);
316 free(crs);
317 }
318 KRL_DBG(("%s: done, final %llu:%llu", __func__, ers->lo, ers->hi));
319 return 0;
320}
321
322int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000323ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100324 u_int64_t serial)
325{
326 return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
327}
328
329int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000330ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl,
331 const struct sshkey *ca_key, u_int64_t lo, u_int64_t hi)
Damien Millerf3747bf2013-01-18 11:44:04 +1100332{
333 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000334 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100335
336 if (lo > hi || lo == 0)
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000337 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000338 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
339 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100340 return insert_serial_range(&rc->revoked_serials, lo, hi);
341}
342
343int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000344ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
Damien Millerf3747bf2013-01-18 11:44:04 +1100345 const char *key_id)
346{
347 struct revoked_key_id *rki, *erki;
348 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000349 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100350
djm@openbsd.org74de2542014-12-04 01:49:59 +0000351 if ((r = revoked_certs_for_ca_key(krl, ca_key, &rc, 1)) != 0)
352 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100353
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000354 KRL_DBG(("%s: revoke %s", __func__, key_id));
Damien Millerf3747bf2013-01-18 11:44:04 +1100355 if ((rki = calloc(1, sizeof(*rki))) == NULL ||
356 (rki->key_id = strdup(key_id)) == NULL) {
357 free(rki);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000358 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100359 }
360 erki = RB_INSERT(revoked_key_id_tree, &rc->revoked_key_ids, rki);
361 if (erki != NULL) {
362 free(rki->key_id);
363 free(rki);
364 }
365 return 0;
366}
367
368/* Convert "key" to a public key blob without any certificate information */
369static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000370plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
Damien Millerf3747bf2013-01-18 11:44:04 +1100371{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000372 struct sshkey *kcopy;
Damien Millerf3747bf2013-01-18 11:44:04 +1100373 int r;
374
djm@openbsd.org74de2542014-12-04 01:49:59 +0000375 if ((r = sshkey_from_private(key, &kcopy)) != 0)
376 return r;
377 if (sshkey_is_cert(kcopy)) {
378 if ((r = sshkey_drop_cert(kcopy)) != 0) {
379 sshkey_free(kcopy);
380 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100381 }
382 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000383 r = sshkey_to_blob(kcopy, blob, blen);
markus@openbsd.org905fe302015-01-12 14:05:19 +0000384 sshkey_free(kcopy);
Damien Miller86687062014-07-02 15:28:02 +1000385 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100386}
387
388/* Revoke a key blob. Ownership of blob is transferred to the tree */
389static int
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000390revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
Damien Millerf3747bf2013-01-18 11:44:04 +1100391{
392 struct revoked_blob *rb, *erb;
393
394 if ((rb = calloc(1, sizeof(*rb))) == NULL)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000395 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100396 rb->blob = blob;
397 rb->len = len;
398 erb = RB_INSERT(revoked_blob_tree, rbt, rb);
399 if (erb != NULL) {
400 free(rb->blob);
401 free(rb);
402 }
403 return 0;
404}
405
406int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000407ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100408{
409 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000410 size_t len;
411 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100412
djm@openbsd.org74de2542014-12-04 01:49:59 +0000413 debug3("%s: revoke type %s", __func__, sshkey_type(key));
414 if ((r = plain_key_blob(key, &blob, &len)) != 0)
415 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100416 return revoke_blob(&krl->revoked_keys, blob, len);
417}
418
djm@openbsd.org9405c622018-09-12 01:21:34 +0000419static int
420revoke_by_hash(struct revoked_blob_tree *target, const u_char *p, size_t len)
Damien Millerf3747bf2013-01-18 11:44:04 +1100421{
422 u_char *blob;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000423 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100424
djm@openbsd.org9405c622018-09-12 01:21:34 +0000425 /* need to copy hash, as revoke_blob steals ownership */
426 if ((blob = malloc(len)) == NULL)
427 return SSH_ERR_SYSTEM_ERROR;
428 memcpy(blob, p, len);
429 if ((r = revoke_blob(target, blob, len)) != 0) {
430 free(blob);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000431 return r;
djm@openbsd.org9405c622018-09-12 01:21:34 +0000432 }
433 return 0;
434}
435
436int
437ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const u_char *p, size_t len)
438{
439 debug3("%s: revoke by sha1", __func__);
440 if (len != 20)
441 return SSH_ERR_INVALID_FORMAT;
442 return revoke_by_hash(&krl->revoked_sha1s, p, len);
443}
444
445int
446ssh_krl_revoke_key_sha256(struct ssh_krl *krl, const u_char *p, size_t len)
447{
448 debug3("%s: revoke by sha256", __func__);
449 if (len != 32)
450 return SSH_ERR_INVALID_FORMAT;
451 return revoke_by_hash(&krl->revoked_sha256s, p, len);
Damien Millerf3747bf2013-01-18 11:44:04 +1100452}
453
454int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000455ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +1100456{
djm@openbsd.org9405c622018-09-12 01:21:34 +0000457 /* XXX replace with SHA256? */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000458 if (!sshkey_is_cert(key))
djm@openbsd.org9405c622018-09-12 01:21:34 +0000459 return ssh_krl_revoke_key_explicit(krl, key);
Damien Millerf3747bf2013-01-18 11:44:04 +1100460
djm@openbsd.orgc28fc622015-07-03 03:43:18 +0000461 if (key->cert->serial == 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100462 return ssh_krl_revoke_cert_by_key_id(krl,
463 key->cert->signature_key,
464 key->cert->key_id);
465 } else {
466 return ssh_krl_revoke_cert_by_serial(krl,
467 key->cert->signature_key,
468 key->cert->serial);
469 }
470}
471
472/*
djm@openbsd.org74de2542014-12-04 01:49:59 +0000473 * Select the most compact section type to emit next in a KRL based on
474 * the current section type, the run length of contiguous revoked serial
Damien Millerf3747bf2013-01-18 11:44:04 +1100475 * numbers and the gaps from the last and to the next revoked serial.
476 * Applies a mostly-accurate bit cost model to select the section type
477 * that will minimise the size of the resultant KRL.
478 */
479static int
480choose_next_state(int current_state, u_int64_t contig, int final,
481 u_int64_t last_gap, u_int64_t next_gap, int *force_new_section)
482{
483 int new_state;
484 u_int64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
485
486 /*
487 * Avoid unsigned overflows.
488 * The limits are high enough to avoid confusing the calculations.
489 */
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000490 contig = MINIMUM(contig, 1ULL<<31);
491 last_gap = MINIMUM(last_gap, 1ULL<<31);
492 next_gap = MINIMUM(next_gap, 1ULL<<31);
Damien Millerf3747bf2013-01-18 11:44:04 +1100493
494 /*
495 * Calculate the cost to switch from the current state to candidates.
496 * NB. range sections only ever contain a single range, so their
497 * switching cost is independent of the current_state.
498 */
499 cost_list = cost_bitmap = cost_bitmap_restart = 0;
500 cost_range = 8;
501 switch (current_state) {
502 case KRL_SECTION_CERT_SERIAL_LIST:
503 cost_bitmap_restart = cost_bitmap = 8 + 64;
504 break;
505 case KRL_SECTION_CERT_SERIAL_BITMAP:
506 cost_list = 8;
507 cost_bitmap_restart = 8 + 64;
508 break;
509 case KRL_SECTION_CERT_SERIAL_RANGE:
510 case 0:
511 cost_bitmap_restart = cost_bitmap = 8 + 64;
512 cost_list = 8;
513 }
514
515 /* Estimate base cost in bits of each section type */
516 cost_list += 64 * contig + (final ? 0 : 8+64);
517 cost_range += (2 * 64) + (final ? 0 : 8+64);
deraadt@openbsd.org9136ec12016-09-12 01:22:38 +0000518 cost_bitmap += last_gap + contig + (final ? 0 : MINIMUM(next_gap, 8+64));
519 cost_bitmap_restart += contig + (final ? 0 : MINIMUM(next_gap, 8+64));
Damien Millerf3747bf2013-01-18 11:44:04 +1100520
521 /* Convert to byte costs for actual comparison */
522 cost_list = (cost_list + 7) / 8;
523 cost_bitmap = (cost_bitmap + 7) / 8;
524 cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
525 cost_range = (cost_range + 7) / 8;
526
527 /* Now pick the best choice */
528 *force_new_section = 0;
529 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
530 cost = cost_bitmap;
531 if (cost_range < cost) {
532 new_state = KRL_SECTION_CERT_SERIAL_RANGE;
533 cost = cost_range;
534 }
535 if (cost_list < cost) {
536 new_state = KRL_SECTION_CERT_SERIAL_LIST;
537 cost = cost_list;
538 }
539 if (cost_bitmap_restart < cost) {
540 new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
541 *force_new_section = 1;
542 cost = cost_bitmap_restart;
543 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000544 KRL_DBG(("%s: contig %llu last_gap %llu next_gap %llu final %d, costs:"
Damien Millerf3747bf2013-01-18 11:44:04 +1100545 "list %llu range %llu bitmap %llu new bitmap %llu, "
Damien Millerd677ad12013-04-23 15:18:51 +1000546 "selected 0x%02x%s", __func__, (long long unsigned)contig,
547 (long long unsigned)last_gap, (long long unsigned)next_gap, final,
548 (long long unsigned)cost_list, (long long unsigned)cost_range,
549 (long long unsigned)cost_bitmap,
550 (long long unsigned)cost_bitmap_restart, new_state,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000551 *force_new_section ? " restart" : ""));
Damien Millerf3747bf2013-01-18 11:44:04 +1100552 return new_state;
553}
554
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000555static int
556put_bitmap(struct sshbuf *buf, struct bitmap *bitmap)
557{
558 size_t len;
559 u_char *blob;
560 int r;
561
562 len = bitmap_nbytes(bitmap);
563 if ((blob = malloc(len)) == NULL)
564 return SSH_ERR_ALLOC_FAIL;
565 if (bitmap_to_string(bitmap, blob, len) != 0) {
566 free(blob);
567 return SSH_ERR_INTERNAL_ERROR;
568 }
569 r = sshbuf_put_bignum2_bytes(buf, blob, len);
570 free(blob);
571 return r;
572}
573
Damien Millerf3747bf2013-01-18 11:44:04 +1100574/* Generate a KRL_SECTION_CERTIFICATES KRL section */
575static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000576revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
Damien Millerf3747bf2013-01-18 11:44:04 +1100577{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000578 int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100579 u_int64_t i, contig, gap, last = 0, bitmap_start = 0;
580 struct revoked_serial *rs, *nrs;
581 struct revoked_key_id *rki;
582 int next_state, state = 0;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000583 struct sshbuf *sect;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000584 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100585
djm@openbsd.org74de2542014-12-04 01:49:59 +0000586 if ((sect = sshbuf_new()) == NULL)
587 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100588
djm@openbsd.org669aee92015-01-30 01:10:33 +0000589 /* Store the header: optional CA scope key, reserved */
590 if (rc->ca_key == NULL) {
591 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
592 goto out;
593 } else {
594 if ((r = sshkey_puts(rc->ca_key, buf)) != 0)
595 goto out;
596 }
597 if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000598 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100599
Damien Millerf3747bf2013-01-18 11:44:04 +1100600 /* Store the revoked serials. */
601 for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
602 rs != NULL;
603 rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000604 KRL_DBG(("%s: serial %llu:%llu state 0x%02x", __func__,
Damien Millerd677ad12013-04-23 15:18:51 +1000605 (long long unsigned)rs->lo, (long long unsigned)rs->hi,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000606 state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100607
608 /* Check contiguous length and gap to next section (if any) */
609 nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
610 final = nrs == NULL;
611 gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
612 contig = 1 + (rs->hi - rs->lo);
613
614 /* Choose next state based on these */
615 next_state = choose_next_state(state, contig, final,
616 state == 0 ? 0 : rs->lo - last, gap, &force_new_sect);
617
618 /*
619 * If the current section is a range section or has a different
620 * type to the next section, then finish it off now.
621 */
622 if (state != 0 && (force_new_sect || next_state != state ||
623 state == KRL_SECTION_CERT_SERIAL_RANGE)) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000624 KRL_DBG(("%s: finish state 0x%02x", __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100625 switch (state) {
626 case KRL_SECTION_CERT_SERIAL_LIST:
627 case KRL_SECTION_CERT_SERIAL_RANGE:
628 break;
629 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000630 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000631 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000632 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100633 bitmap = NULL;
634 break;
635 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000636 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
637 (r = sshbuf_put_stringb(buf, sect)) != 0)
638 goto out;
639 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100640 }
641
642 /* If we are starting a new section then prepare it now */
643 if (next_state != state || force_new_sect) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000644 KRL_DBG(("%s: start state 0x%02x", __func__,
645 next_state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100646 state = next_state;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000647 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100648 switch (state) {
649 case KRL_SECTION_CERT_SERIAL_LIST:
650 case KRL_SECTION_CERT_SERIAL_RANGE:
651 break;
652 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000653 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000654 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100655 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000656 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100657 bitmap_start = rs->lo;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000658 if ((r = sshbuf_put_u64(sect,
659 bitmap_start)) != 0)
660 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100661 break;
662 }
663 }
664
665 /* Perform section-specific processing */
666 switch (state) {
667 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000668 for (i = 0; i < contig; i++) {
669 if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
670 goto out;
671 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100672 break;
673 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000674 if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
675 (r = sshbuf_put_u64(sect, rs->hi)) != 0)
676 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100677 break;
678 case KRL_SECTION_CERT_SERIAL_BITMAP:
679 if (rs->lo - bitmap_start > INT_MAX) {
680 error("%s: insane bitmap gap", __func__);
681 goto out;
682 }
683 for (i = 0; i < contig; i++) {
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000684 if (bitmap_set_bit(bitmap,
685 rs->lo + i - bitmap_start) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000686 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100687 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000688 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100689 }
690 break;
691 }
692 last = rs->hi;
693 }
694 /* Flush the remaining section, if any */
695 if (state != 0) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000696 KRL_DBG(("%s: serial final flush for state 0x%02x",
697 __func__, state));
Damien Millerf3747bf2013-01-18 11:44:04 +1100698 switch (state) {
699 case KRL_SECTION_CERT_SERIAL_LIST:
700 case KRL_SECTION_CERT_SERIAL_RANGE:
701 break;
702 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000703 if ((r = put_bitmap(sect, bitmap)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000704 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000705 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100706 bitmap = NULL;
707 break;
708 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000709 if ((r = sshbuf_put_u8(buf, state)) != 0 ||
710 (r = sshbuf_put_stringb(buf, sect)) != 0)
711 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100712 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000713 KRL_DBG(("%s: serial done ", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +1100714
715 /* Now output a section for any revocations by key ID */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000716 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100717 RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000718 KRL_DBG(("%s: key ID %s", __func__, rki->key_id));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000719 if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
720 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100721 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000722 if (sshbuf_len(sect) != 0) {
723 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
724 (r = sshbuf_put_stringb(buf, sect)) != 0)
725 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100726 }
727 r = 0;
728 out:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000729 bitmap_free(bitmap);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000730 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100731 return r;
732}
733
734int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000735ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf,
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +0000736 struct sshkey **sign_keys, u_int nsign_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100737{
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000738 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerf3747bf2013-01-18 11:44:04 +1100739 struct revoked_certs *rc;
740 struct revoked_blob *rb;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000741 struct sshbuf *sect;
742 u_char *sblob = NULL;
743 size_t slen, i;
Damien Millerf3747bf2013-01-18 11:44:04 +1100744
745 if (krl->generated_date == 0)
746 krl->generated_date = time(NULL);
747
djm@openbsd.org74de2542014-12-04 01:49:59 +0000748 if ((sect = sshbuf_new()) == NULL)
749 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100750
751 /* Store the header */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000752 if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
753 (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
754 (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000755 (r = sshbuf_put_u64(buf, krl->generated_date)) != 0 ||
djm@openbsd.org74de2542014-12-04 01:49:59 +0000756 (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
757 (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
758 (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
759 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100760
761 /* Store sections for revoked certificates */
762 TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000763 sshbuf_reset(sect);
764 if ((r = revoked_certs_generate(rc, sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100765 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000766 if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
767 (r = sshbuf_put_stringb(buf, sect)) != 0)
768 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100769 }
770
771 /* Finally, output sections for revocations by public key/hash */
djm@openbsd.org74de2542014-12-04 01:49:59 +0000772 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100773 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000774 KRL_DBG(("%s: key len %zu ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000775 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000776 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100777 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000778 if (sshbuf_len(sect) != 0) {
779 if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
780 (r = sshbuf_put_stringb(buf, sect)) != 0)
781 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100782 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000783 sshbuf_reset(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100784 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000785 KRL_DBG(("%s: hash len %zu ", __func__, rb->len));
markus@openbsd.org00975652015-01-12 19:22:46 +0000786 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000787 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100788 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000789 if (sshbuf_len(sect) != 0) {
790 if ((r = sshbuf_put_u8(buf,
791 KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
792 (r = sshbuf_put_stringb(buf, sect)) != 0)
793 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100794 }
djm@openbsd.org9405c622018-09-12 01:21:34 +0000795 sshbuf_reset(sect);
796 RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
797 KRL_DBG(("%s: hash len %zu ", __func__, rb->len));
798 if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
799 goto out;
800 }
801 if (sshbuf_len(sect) != 0) {
802 if ((r = sshbuf_put_u8(buf,
803 KRL_SECTION_FINGERPRINT_SHA256)) != 0 ||
804 (r = sshbuf_put_stringb(buf, sect)) != 0)
805 goto out;
806 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100807
808 for (i = 0; i < nsign_keys; i++) {
djm@openbsd.org60b18252015-01-26 02:59:11 +0000809 KRL_DBG(("%s: signature key %s", __func__,
810 sshkey_ssh_name(sign_keys[i])));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000811 if ((r = sshbuf_put_u8(buf, KRL_SECTION_SIGNATURE)) != 0 ||
djm@openbsd.org60b18252015-01-26 02:59:11 +0000812 (r = sshkey_puts(sign_keys[i], buf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100813 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000814
815 if ((r = sshkey_sign(sign_keys[i], &sblob, &slen,
djm@openbsd.org9a14c642019-10-31 21:23:19 +0000816 sshbuf_ptr(buf), sshbuf_len(buf), NULL, NULL, 0)) != 0)
djm@openbsd.org74de2542014-12-04 01:49:59 +0000817 goto out;
djm@openbsd.org4e62cc62015-01-19 17:35:48 +0000818 KRL_DBG(("%s: signature sig len %zu", __func__, slen));
djm@openbsd.org74de2542014-12-04 01:49:59 +0000819 if ((r = sshbuf_put_string(buf, sblob, slen)) != 0)
820 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100821 }
822
823 r = 0;
824 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100825 free(sblob);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000826 sshbuf_free(sect);
Damien Millerf3747bf2013-01-18 11:44:04 +1100827 return r;
828}
829
830static void
831format_timestamp(u_int64_t timestamp, char *ts, size_t nts)
832{
833 time_t t;
834 struct tm *tm;
835
836 t = timestamp;
837 tm = localtime(&t);
djm@openbsd.orgb6de5ac2014-11-21 01:00:38 +0000838 if (tm == NULL)
deraadt@openbsd.orgf101d822015-01-18 14:01:00 +0000839 strlcpy(ts, "<INVALID>", nts);
djm@openbsd.orgb6de5ac2014-11-21 01:00:38 +0000840 else {
841 *ts = '\0';
842 strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
843 }
Damien Millerf3747bf2013-01-18 11:44:04 +1100844}
845
846static int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000847parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
Damien Millerf3747bf2013-01-18 11:44:04 +1100848{
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000849 int r = SSH_ERR_INTERNAL_ERROR;
Damien Miller633de332014-05-15 13:48:26 +1000850 u_char type;
851 const u_char *blob;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000852 size_t blen, nbits;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000853 struct sshbuf *subsect = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100854 u_int64_t serial, serial_lo, serial_hi;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000855 struct bitmap *bitmap = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100856 char *key_id = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000857 struct sshkey *ca_key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100858
djm@openbsd.org74de2542014-12-04 01:49:59 +0000859 if ((subsect = sshbuf_new()) == NULL)
860 return SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100861
djm@openbsd.org74de2542014-12-04 01:49:59 +0000862 /* Header: key, reserved */
863 if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
864 (r = sshbuf_skip_string(buf)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100865 goto out;
djm@openbsd.org669aee92015-01-30 01:10:33 +0000866 if (blen != 0 && (r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100867 goto out;
868
djm@openbsd.org74de2542014-12-04 01:49:59 +0000869 while (sshbuf_len(buf) > 0) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +0000870 sshbuf_free(subsect);
871 subsect = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000872 if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
873 (r = sshbuf_froms(buf, &subsect)) != 0)
874 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000875 KRL_DBG(("%s: subsection type 0x%02x", __func__, type));
876 /* sshbuf_dump(subsect, stderr); */
Damien Millerf3747bf2013-01-18 11:44:04 +1100877
878 switch (type) {
879 case KRL_SECTION_CERT_SERIAL_LIST:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000880 while (sshbuf_len(subsect) > 0) {
881 if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100882 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000883 if ((r = ssh_krl_revoke_cert_by_serial(krl,
884 ca_key, serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100885 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100886 }
887 break;
888 case KRL_SECTION_CERT_SERIAL_RANGE:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000889 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
890 (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100891 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000892 if ((r = ssh_krl_revoke_cert_by_serial_range(krl,
893 ca_key, serial_lo, serial_hi)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100894 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100895 break;
896 case KRL_SECTION_CERT_SERIAL_BITMAP:
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000897 if ((bitmap = bitmap_new()) == NULL) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000898 r = SSH_ERR_ALLOC_FAIL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100899 goto out;
900 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000901 if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000902 (r = sshbuf_get_bignum2_bytes_direct(subsect,
903 &blob, &blen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100904 goto out;
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000905 if (bitmap_from_string(bitmap, blob, blen) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +0000906 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100907 goto out;
908 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000909 nbits = bitmap_nbits(bitmap);
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000910 for (serial = 0; serial < (u_int64_t)nbits; serial++) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100911 if (serial > 0 && serial_lo + serial == 0) {
912 error("%s: bitmap wraps u64", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000913 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100914 goto out;
915 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000916 if (!bitmap_test_bit(bitmap, serial))
Damien Millerf3747bf2013-01-18 11:44:04 +1100917 continue;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000918 if ((r = ssh_krl_revoke_cert_by_serial(krl,
919 ca_key, serial_lo + serial)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100920 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100921 }
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000922 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100923 bitmap = NULL;
924 break;
925 case KRL_SECTION_CERT_KEY_ID:
djm@openbsd.org74de2542014-12-04 01:49:59 +0000926 while (sshbuf_len(subsect) > 0) {
927 if ((r = sshbuf_get_cstring(subsect,
928 &key_id, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100929 goto out;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000930 if ((r = ssh_krl_revoke_cert_by_key_id(krl,
931 ca_key, key_id)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +1100932 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +1100933 free(key_id);
934 key_id = NULL;
935 }
936 break;
937 default:
938 error("Unsupported KRL certificate section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000939 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100940 goto out;
941 }
djm@openbsd.org74de2542014-12-04 01:49:59 +0000942 if (sshbuf_len(subsect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +1100943 error("KRL certificate section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +0000944 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +1100945 goto out;
946 }
947 }
948
djm@openbsd.org74de2542014-12-04 01:49:59 +0000949 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100950 out:
Damien Millerf3747bf2013-01-18 11:44:04 +1100951 if (bitmap != NULL)
djm@openbsd.orga165bab2015-01-14 15:02:39 +0000952 bitmap_free(bitmap);
Damien Millerf3747bf2013-01-18 11:44:04 +1100953 free(key_id);
djm@openbsd.org74de2542014-12-04 01:49:59 +0000954 sshkey_free(ca_key);
955 sshbuf_free(subsect);
956 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +1100957}
958
djm@openbsd.org9405c622018-09-12 01:21:34 +0000959static int
960blob_section(struct sshbuf *sect, struct revoked_blob_tree *target_tree,
961 size_t expected_len)
962{
963 u_char *rdata = NULL;
964 size_t rlen = 0;
965 int r;
966
967 while (sshbuf_len(sect) > 0) {
968 if ((r = sshbuf_get_string(sect, &rdata, &rlen)) != 0)
969 return r;
970 if (expected_len != 0 && rlen != expected_len) {
971 error("%s: bad length", __func__);
972 free(rdata);
973 return SSH_ERR_INVALID_FORMAT;
974 }
975 if ((r = revoke_blob(target_tree, rdata, rlen)) != 0) {
976 free(rdata);
977 return r;
978 }
979 }
980 return 0;
981}
Damien Millerf3747bf2013-01-18 11:44:04 +1100982
983/* Attempt to parse a KRL, checking its signature (if any) with sign_ca_keys. */
984int
djm@openbsd.org74de2542014-12-04 01:49:59 +0000985ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000986 const struct sshkey **sign_ca_keys, size_t nsign_ca_keys)
Damien Millerf3747bf2013-01-18 11:44:04 +1100987{
djm@openbsd.org74de2542014-12-04 01:49:59 +0000988 struct sshbuf *copy = NULL, *sect = NULL;
989 struct ssh_krl *krl = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +1100990 char timestamp[64];
djm@openbsd.orge7fd9522015-01-13 19:04:35 +0000991 int r = SSH_ERR_INTERNAL_ERROR, sig_seen;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000992 struct sshkey *key = NULL, **ca_used = NULL, **tmp_ca_used;
djm@openbsd.org9405c622018-09-12 01:21:34 +0000993 u_char type;
Damien Miller633de332014-05-15 13:48:26 +1000994 const u_char *blob;
djm@openbsd.org9405c622018-09-12 01:21:34 +0000995 size_t i, j, sig_off, sects_off, blen, nca_used;
djm@openbsd.org74de2542014-12-04 01:49:59 +0000996 u_int format_version;
Damien Millerf3747bf2013-01-18 11:44:04 +1100997
Damien Miller30710702013-07-18 16:09:44 +1000998 nca_used = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +1100999 *krlp = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001000 if (sshbuf_len(buf) < sizeof(KRL_MAGIC) - 1 ||
1001 memcmp(sshbuf_ptr(buf), KRL_MAGIC, sizeof(KRL_MAGIC) - 1) != 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001002 debug3("%s: not a KRL", __func__);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001003 return SSH_ERR_KRL_BAD_MAGIC;
Damien Millerf3747bf2013-01-18 11:44:04 +11001004 }
1005
1006 /* Take a copy of the KRL buffer so we can verify its signature later */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001007 if ((copy = sshbuf_fromb(buf)) == NULL) {
1008 r = SSH_ERR_ALLOC_FAIL;
1009 goto out;
1010 }
1011 if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0)
1012 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001013
1014 if ((krl = ssh_krl_init()) == NULL) {
1015 error("%s: alloc failed", __func__);
1016 goto out;
1017 }
1018
djm@openbsd.org74de2542014-12-04 01:49:59 +00001019 if ((r = sshbuf_get_u32(copy, &format_version)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001020 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001021 if (format_version != KRL_FORMAT_VERSION) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001022 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001023 goto out;
1024 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001025 if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
1026 (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
1027 (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
1028 (r = sshbuf_skip_string(copy)) != 0 ||
1029 (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001030 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001031
1032 format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
Damien Millerd677ad12013-04-23 15:18:51 +10001033 debug("KRL version %llu generated at %s%s%s",
1034 (long long unsigned)krl->krl_version, timestamp,
1035 *krl->comment ? ": " : "", krl->comment);
Damien Millerf3747bf2013-01-18 11:44:04 +11001036
1037 /*
1038 * 1st pass: verify signatures, if any. This is done to avoid
1039 * detailed parsing of data whose provenance is unverified.
1040 */
1041 sig_seen = 0;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001042 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1043 /* Shouldn't happen */
1044 r = SSH_ERR_INTERNAL_ERROR;
1045 goto out;
1046 }
1047 sects_off = sshbuf_len(buf) - sshbuf_len(copy);
1048 while (sshbuf_len(copy) > 0) {
1049 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1050 (r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001051 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001052 KRL_DBG(("%s: first pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +11001053 if (type != KRL_SECTION_SIGNATURE) {
1054 if (sig_seen) {
1055 error("KRL contains non-signature section "
1056 "after signature");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001057 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001058 goto out;
1059 }
1060 /* Not interested for now. */
1061 continue;
1062 }
1063 sig_seen = 1;
1064 /* First string component is the signing key */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001065 if ((r = sshkey_from_blob(blob, blen, &key)) != 0) {
1066 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001067 goto out;
1068 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001069 if (sshbuf_len(buf) < sshbuf_len(copy)) {
1070 /* Shouldn't happen */
1071 r = SSH_ERR_INTERNAL_ERROR;
1072 goto out;
1073 }
1074 sig_off = sshbuf_len(buf) - sshbuf_len(copy);
Damien Millerf3747bf2013-01-18 11:44:04 +11001075 /* Second string component is the signature itself */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001076 if ((r = sshbuf_get_string_direct(copy, &blob, &blen)) != 0) {
1077 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001078 goto out;
1079 }
1080 /* Check signature over entire KRL up to this point */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001081 if ((r = sshkey_verify(key, blob, blen,
djm@openbsd.orgb7e74ea2019-11-25 00:51:37 +00001082 sshbuf_ptr(buf), sig_off, NULL, 0, NULL)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001083 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001084 /* Check if this key has already signed this KRL */
1085 for (i = 0; i < nca_used; i++) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001086 if (sshkey_equal(ca_used[i], key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001087 error("KRL signed more than once with "
1088 "the same key");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001089 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001090 goto out;
1091 }
1092 }
1093 /* Record keys used to sign the KRL */
deraadt@openbsd.org9e509d42017-05-31 09:15:42 +00001094 tmp_ca_used = recallocarray(ca_used, nca_used, nca_used + 1,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001095 sizeof(*ca_used));
1096 if (tmp_ca_used == NULL) {
1097 r = SSH_ERR_ALLOC_FAIL;
1098 goto out;
1099 }
1100 ca_used = tmp_ca_used;
Damien Millerf3747bf2013-01-18 11:44:04 +11001101 ca_used[nca_used++] = key;
1102 key = NULL;
Damien Millerf3747bf2013-01-18 11:44:04 +11001103 }
1104
djm@openbsd.org74de2542014-12-04 01:49:59 +00001105 if (sshbuf_len(copy) != 0) {
1106 /* Shouldn't happen */
1107 r = SSH_ERR_INTERNAL_ERROR;
1108 goto out;
1109 }
1110
Damien Millerf3747bf2013-01-18 11:44:04 +11001111 /*
1112 * 2nd pass: parse and load the KRL, skipping the header to the point
1113 * where the section start.
1114 */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001115 sshbuf_free(copy);
1116 if ((copy = sshbuf_fromb(buf)) == NULL) {
1117 r = SSH_ERR_ALLOC_FAIL;
1118 goto out;
1119 }
1120 if ((r = sshbuf_consume(copy, sects_off)) != 0)
1121 goto out;
1122 while (sshbuf_len(copy) > 0) {
mmcc@openbsd.org52d70782015-12-11 04:21:11 +00001123 sshbuf_free(sect);
1124 sect = NULL;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001125 if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001126 (r = sshbuf_froms(copy, &sect)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001127 goto out;
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001128 KRL_DBG(("%s: second pass, section 0x%02x", __func__, type));
Damien Millerf3747bf2013-01-18 11:44:04 +11001129
1130 switch (type) {
1131 case KRL_SECTION_CERTIFICATES:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001132 if ((r = parse_revoked_certs(sect, krl)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001133 goto out;
1134 break;
1135 case KRL_SECTION_EXPLICIT_KEY:
djm@openbsd.org9405c622018-09-12 01:21:34 +00001136 if ((r = blob_section(sect,
1137 &krl->revoked_keys, 0)) != 0)
1138 goto out;
1139 break;
Damien Millerf3747bf2013-01-18 11:44:04 +11001140 case KRL_SECTION_FINGERPRINT_SHA1:
djm@openbsd.org9405c622018-09-12 01:21:34 +00001141 if ((r = blob_section(sect,
1142 &krl->revoked_sha1s, 20)) != 0)
1143 goto out;
1144 break;
1145 case KRL_SECTION_FINGERPRINT_SHA256:
1146 if ((r = blob_section(sect,
1147 &krl->revoked_sha256s, 32)) != 0)
1148 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001149 break;
1150 case KRL_SECTION_SIGNATURE:
1151 /* Handled above, but still need to stay in synch */
dtucker@openbsd.orgef653dd2017-03-10 07:18:32 +00001152 sshbuf_free(sect);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001153 sect = NULL;
1154 if ((r = sshbuf_skip_string(copy)) != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001155 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001156 break;
1157 default:
1158 error("Unsupported KRL section %u", type);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001159 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001160 goto out;
1161 }
djm@openbsd.org422d1b32015-12-31 00:33:52 +00001162 if (sect != NULL && sshbuf_len(sect) > 0) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001163 error("KRL section contains unparsed data");
djm@openbsd.org74de2542014-12-04 01:49:59 +00001164 r = SSH_ERR_INVALID_FORMAT;
Damien Millerf3747bf2013-01-18 11:44:04 +11001165 goto out;
1166 }
1167 }
1168
1169 /* Check that the key(s) used to sign the KRL weren't revoked */
1170 sig_seen = 0;
1171 for (i = 0; i < nca_used; i++) {
1172 if (ssh_krl_check_key(krl, ca_used[i]) == 0)
1173 sig_seen = 1;
1174 else {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001175 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001176 ca_used[i] = NULL;
1177 }
1178 }
1179 if (nca_used && !sig_seen) {
1180 error("All keys used to sign KRL were revoked");
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001181 r = SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001182 goto out;
1183 }
1184
1185 /* If we have CA keys, then verify that one was used to sign the KRL */
1186 if (sig_seen && nsign_ca_keys != 0) {
1187 sig_seen = 0;
1188 for (i = 0; !sig_seen && i < nsign_ca_keys; i++) {
1189 for (j = 0; j < nca_used; j++) {
1190 if (ca_used[j] == NULL)
1191 continue;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001192 if (sshkey_equal(ca_used[j], sign_ca_keys[i])) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001193 sig_seen = 1;
1194 break;
1195 }
1196 }
1197 }
1198 if (!sig_seen) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001199 r = SSH_ERR_SIGNATURE_INVALID;
Damien Millerf3747bf2013-01-18 11:44:04 +11001200 error("KRL not signed with any trusted key");
1201 goto out;
1202 }
1203 }
1204
1205 *krlp = krl;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001206 r = 0;
Damien Millerf3747bf2013-01-18 11:44:04 +11001207 out:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001208 if (r != 0)
Damien Millerf3747bf2013-01-18 11:44:04 +11001209 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001210 for (i = 0; i < nca_used; i++)
1211 sshkey_free(ca_used[i]);
Damien Millerf3747bf2013-01-18 11:44:04 +11001212 free(ca_used);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001213 sshkey_free(key);
1214 sshbuf_free(copy);
1215 sshbuf_free(sect);
1216 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001217}
1218
djm@openbsd.org669aee92015-01-30 01:10:33 +00001219/* Checks certificate serial number and key ID revocation */
1220static int
1221is_cert_revoked(const struct sshkey *key, struct revoked_certs *rc)
1222{
1223 struct revoked_serial rs, *ers;
1224 struct revoked_key_id rki, *erki;
1225
1226 /* Check revocation by cert key ID */
1227 memset(&rki, 0, sizeof(rki));
1228 rki.key_id = key->cert->key_id;
1229 erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1230 if (erki != NULL) {
1231 KRL_DBG(("%s: revoked by key ID", __func__));
1232 return SSH_ERR_KEY_REVOKED;
1233 }
1234
1235 /*
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001236 * Zero serials numbers are ignored (it's the default when the
1237 * CA doesn't specify one).
djm@openbsd.org669aee92015-01-30 01:10:33 +00001238 */
djm@openbsd.orgc28fc622015-07-03 03:43:18 +00001239 if (key->cert->serial == 0)
djm@openbsd.org669aee92015-01-30 01:10:33 +00001240 return 0;
1241
1242 memset(&rs, 0, sizeof(rs));
1243 rs.lo = rs.hi = key->cert->serial;
1244 ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1245 if (ers != NULL) {
1246 KRL_DBG(("%s: revoked serial %llu matched %llu:%llu", __func__,
1247 key->cert->serial, ers->lo, ers->hi));
1248 return SSH_ERR_KEY_REVOKED;
1249 }
1250 return 0;
1251}
1252
Damien Millerf3747bf2013-01-18 11:44:04 +11001253/* Checks whether a given key/cert is revoked. Does not check its CA */
1254static int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001255is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001256{
1257 struct revoked_blob rb, *erb;
Damien Millerf3747bf2013-01-18 11:44:04 +11001258 struct revoked_certs *rc;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001259 int r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001260
1261 /* Check explicitly revoked hashes first */
Damien Miller1d2c4562014-02-04 11:18:20 +11001262 memset(&rb, 0, sizeof(rb));
djm@openbsd.org56d1c832014-12-21 22:27:55 +00001263 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
djm@openbsd.org74de2542014-12-04 01:49:59 +00001264 &rb.blob, &rb.len)) != 0)
1265 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001266 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1267 free(rb.blob);
1268 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001269 KRL_DBG(("%s: revoked by key SHA1", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001270 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001271 }
djm@openbsd.org9405c622018-09-12 01:21:34 +00001272 memset(&rb, 0, sizeof(rb));
1273 if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA256,
1274 &rb.blob, &rb.len)) != 0)
1275 return r;
1276 erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha256s, &rb);
1277 free(rb.blob);
1278 if (erb != NULL) {
1279 KRL_DBG(("%s: revoked by key SHA256", __func__));
1280 return SSH_ERR_KEY_REVOKED;
1281 }
Damien Millerf3747bf2013-01-18 11:44:04 +11001282
1283 /* Next, explicit keys */
Damien Miller1d2c4562014-02-04 11:18:20 +11001284 memset(&rb, 0, sizeof(rb));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001285 if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1286 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001287 erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1288 free(rb.blob);
1289 if (erb != NULL) {
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001290 KRL_DBG(("%s: revoked by explicit key", __func__));
djm@openbsd.org74de2542014-12-04 01:49:59 +00001291 return SSH_ERR_KEY_REVOKED;
Damien Millerf3747bf2013-01-18 11:44:04 +11001292 }
1293
djm@openbsd.org74de2542014-12-04 01:49:59 +00001294 if (!sshkey_is_cert(key))
Damien Millerf3747bf2013-01-18 11:44:04 +11001295 return 0;
1296
djm@openbsd.org669aee92015-01-30 01:10:33 +00001297 /* Check cert revocation for the specified CA */
djm@openbsd.org74de2542014-12-04 01:49:59 +00001298 if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1299 &rc, 0)) != 0)
1300 return r;
djm@openbsd.org669aee92015-01-30 01:10:33 +00001301 if (rc != NULL) {
1302 if ((r = is_cert_revoked(key, rc)) != 0)
1303 return r;
1304 }
1305 /* Check cert revocation for the wildcard CA */
1306 if ((r = revoked_certs_for_ca_key(krl, NULL, &rc, 0)) != 0)
1307 return r;
1308 if (rc != NULL) {
1309 if ((r = is_cert_revoked(key, rc)) != 0)
1310 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001311 }
1312
Damien Millerf3747bf2013-01-18 11:44:04 +11001313 KRL_DBG(("%s: %llu no match", __func__, key->cert->serial));
Damien Millerf3747bf2013-01-18 11:44:04 +11001314 return 0;
1315}
1316
1317int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001318ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001319{
1320 int r;
1321
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001322 KRL_DBG(("%s: checking key", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001323 if ((r = is_key_revoked(krl, key)) != 0)
1324 return r;
djm@openbsd.org74de2542014-12-04 01:49:59 +00001325 if (sshkey_is_cert(key)) {
Damien Millerf3747bf2013-01-18 11:44:04 +11001326 debug2("%s: checking CA key", __func__);
1327 if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1328 return r;
1329 }
djm@openbsd.orge7fd9522015-01-13 19:04:35 +00001330 KRL_DBG(("%s: key okay", __func__));
Damien Millerf3747bf2013-01-18 11:44:04 +11001331 return 0;
1332}
1333
Damien Millerf3747bf2013-01-18 11:44:04 +11001334int
djm@openbsd.org74de2542014-12-04 01:49:59 +00001335ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
Damien Millerf3747bf2013-01-18 11:44:04 +11001336{
djm@openbsd.org74de2542014-12-04 01:49:59 +00001337 struct sshbuf *krlbuf = NULL;
1338 struct ssh_krl *krl = NULL;
djm@openbsd.org99aa8032020-01-25 23:02:13 +00001339 int oerrno = 0, r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001340
1341 if (path == NULL)
1342 return 0;
djm@openbsd.org99aa8032020-01-25 23:02:13 +00001343 if ((r = sshbuf_load_file(path, &krlbuf)) != 0) {
djm@openbsd.org74de2542014-12-04 01:49:59 +00001344 oerrno = errno;
1345 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001346 }
djm@openbsd.org74de2542014-12-04 01:49:59 +00001347 if ((r = ssh_krl_from_blob(krlbuf, &krl, NULL, 0)) != 0)
1348 goto out;
Damien Millerf3747bf2013-01-18 11:44:04 +11001349 debug2("%s: checking KRL %s", __func__, path);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001350 r = ssh_krl_check_key(krl, key);
1351 out:
djm@openbsd.org74de2542014-12-04 01:49:59 +00001352 sshbuf_free(krlbuf);
Damien Millerf3747bf2013-01-18 11:44:04 +11001353 ssh_krl_free(krl);
djm@openbsd.org74de2542014-12-04 01:49:59 +00001354 if (r != 0)
1355 errno = oerrno;
1356 return r;
Damien Millerf3747bf2013-01-18 11:44:04 +11001357}