Coverage Report

Created: 2026-12-01 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/hpn-ssh/krl.c
Line
Count
Source
1
/* $OpenBSD: krl.c,v 1.67 2026/10/30 05:33:18 djm Exp $ */
2
/*
3
 * Copyright (c) 2012 Damien Miller <djm@mindrot.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include "includes.h"
19
20
#include <sys/types.h>
21
#include <sys/tree.h>
22
#include <sys/queue.h>
23
24
#include <errno.h>
25
#include <limits.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <time.h>
29
30
#include "sshbuf.h"
31
#include "ssherr.h"
32
#include "sshkey.h"
33
#include "misc.h"
34
#include "log.h"
35
#include "digest.h"
36
#include "bitmap.h"
37
#include "utf8.h"
38
39
#include "krl.h"
40
41
/* #define DEBUG_KRL */
42
#ifdef DEBUG_KRL
43
# define KRL_DBG(x) debug3_f x
44
#else
45
# define KRL_DBG(x)
46
#endif
47
48
/*
49
 * Trees of revoked serial numbers, key IDs and keys. This allows
50
 * quick searching, querying and producing lists in canonical order.
51
 */
52
53
/* Tree of serial numbers. XXX make smarter: really need a real sparse bitmap */
54
struct revoked_serial {
55
  uint64_t lo, hi;
56
  RB_ENTRY(revoked_serial) tree_entry;
57
};
58
static int serial_cmp(struct revoked_serial *a, struct revoked_serial *b);
59
RB_HEAD(revoked_serial_tree, revoked_serial);
60
0
RB_GENERATE_STATIC(revoked_serial_tree, revoked_serial, tree_entry, serial_cmp)
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_REMOVE
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_REMOVE_COLOR
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_NFIND
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_INSERT
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_FIND
Unexecuted instantiation: krl.c:revoked_serial_tree_RB_MINMAX
61
0
62
0
/* Tree of key IDs */
63
0
struct revoked_key_id {
64
0
  char *key_id;
65
0
  RB_ENTRY(revoked_key_id) tree_entry;
66
0
};
67
0
static int key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b);
68
0
RB_HEAD(revoked_key_id_tree, revoked_key_id);
69
0
RB_GENERATE_STATIC(revoked_key_id_tree, revoked_key_id, tree_entry, key_id_cmp)
Unexecuted instantiation: krl.c:revoked_key_id_tree_RB_REMOVE
Unexecuted instantiation: krl.c:revoked_key_id_tree_RB_REMOVE_COLOR
Unexecuted instantiation: krl.c:revoked_key_id_tree_RB_INSERT
Unexecuted instantiation: krl.c:revoked_key_id_tree_RB_FIND
Unexecuted instantiation: krl.c:revoked_key_id_tree_RB_MINMAX
70
0
71
0
/* Tree of blobs (used for keys and fingerprints) */
72
0
struct revoked_blob {
73
0
  u_char *blob;
74
0
  size_t len;
75
0
  RB_ENTRY(revoked_blob) tree_entry;
76
0
};
77
0
static int blob_cmp(struct revoked_blob *a, struct revoked_blob *b);
78
0
RB_HEAD(revoked_blob_tree, revoked_blob);
79
0
RB_GENERATE_STATIC(revoked_blob_tree, revoked_blob, tree_entry, blob_cmp)
Unexecuted instantiation: krl.c:revoked_blob_tree_RB_MINMAX
Unexecuted instantiation: krl.c:revoked_blob_tree_RB_REMOVE
Unexecuted instantiation: krl.c:revoked_blob_tree_RB_REMOVE_COLOR
Unexecuted instantiation: krl.c:revoked_blob_tree_RB_INSERT
Unexecuted instantiation: krl.c:revoked_blob_tree_RB_FIND
80
0
81
0
/*
82
0
 * Tracks revoked certs for a single CA. These are held in a list, to preserve
83
0
 * their order, and in a tree indexed by the serialised CA key for searching.
84
0
 */
85
0
struct revoked_certs {
86
0
  struct sshkey *ca_key;
87
0
  u_char *ca_blob;
88
0
  size_t ca_blob_len;
89
0
  struct revoked_serial_tree revoked_serials;
90
0
  struct revoked_key_id_tree revoked_key_ids;
91
0
  TAILQ_ENTRY(revoked_certs) entry;
92
0
  RB_ENTRY(revoked_certs) tree_entry;
93
0
};
94
0
TAILQ_HEAD(revoked_certs_list, revoked_certs);
95
0
static int ca_cmp(struct revoked_certs *a, struct revoked_certs *b);
96
0
RB_HEAD(revoked_certs_tree, revoked_certs);
97
0
RB_GENERATE_STATIC(revoked_certs_tree, revoked_certs, tree_entry, ca_cmp)
Unexecuted instantiation: krl.c:revoked_certs_tree_RB_REMOVE
Unexecuted instantiation: krl.c:revoked_certs_tree_RB_REMOVE_COLOR
Unexecuted instantiation: krl.c:revoked_certs_tree_RB_FIND
Unexecuted instantiation: krl.c:revoked_certs_tree_RB_INSERT
98
0
99
0
struct ssh_krl {
100
0
  uint64_t krl_version;
101
0
  uint64_t generated_date;
102
0
  uint64_t flags;
103
0
  char *comment;
104
0
  struct revoked_blob_tree revoked_keys;
105
0
  struct revoked_blob_tree revoked_sha1s;
106
0
  struct revoked_blob_tree revoked_sha256s;
107
0
  struct revoked_certs_list revoked_certs;
108
0
  struct revoked_certs_tree revoked_certs_by_ca;
109
0
};
110
0
111
0
/* Return equal if a and b overlap */
112
0
static int
113
0
serial_cmp(struct revoked_serial *a, struct revoked_serial *b)
114
0
{
115
0
  if (a->hi >= b->lo && a->lo <= b->hi)
116
0
    return 0;
117
0
  return a->lo < b->lo ? -1 : 1;
118
0
}
119
120
static int
121
key_id_cmp(struct revoked_key_id *a, struct revoked_key_id *b)
122
0
{
123
0
  return strcmp(a->key_id, b->key_id);
124
0
}
125
126
static int
127
blob_cmp(struct revoked_blob *a, struct revoked_blob *b)
128
0
{
129
0
  int r;
130
131
0
  if (a->len != b->len) {
132
0
    if ((r = memcmp(a->blob, b->blob, MINIMUM(a->len, b->len))) != 0)
133
0
      return r;
134
0
    return a->len > b->len ? 1 : -1;
135
0
  } else
136
0
    return memcmp(a->blob, b->blob, a->len);
137
0
}
138
139
/* NB. the wildcard CA has a NULL, zero-length blob */
140
static int
141
ca_cmp(struct revoked_certs *a, struct revoked_certs *b)
142
0
{
143
0
  size_t len = MINIMUM(a->ca_blob_len, b->ca_blob_len);
144
0
  int r;
145
146
0
  if (len != 0 && (r = memcmp(a->ca_blob, b->ca_blob, len)) != 0)
147
0
    return r;
148
0
  if (a->ca_blob_len == b->ca_blob_len)
149
0
    return 0;
150
0
  return a->ca_blob_len > b->ca_blob_len ? 1 : -1;
151
0
}
152
153
struct ssh_krl *
154
ssh_krl_init(void)
155
0
{
156
0
  struct ssh_krl *krl;
157
158
0
  if ((krl = calloc(1, sizeof(*krl))) == NULL)
159
0
    return NULL;
160
0
  RB_INIT(&krl->revoked_keys);
161
0
  RB_INIT(&krl->revoked_sha1s);
162
0
  RB_INIT(&krl->revoked_sha256s);
163
0
  TAILQ_INIT(&krl->revoked_certs);
164
0
  RB_INIT(&krl->revoked_certs_by_ca);
165
0
  return krl;
166
0
}
167
168
static void
169
revoked_certs_free(struct revoked_certs *rc)
170
0
{
171
0
  struct revoked_serial *rs, *trs;
172
0
  struct revoked_key_id *rki, *trki;
173
174
0
  if (rc == NULL)
175
0
    return;
176
0
  RB_FOREACH_SAFE(rs, revoked_serial_tree, &rc->revoked_serials, trs) {
177
0
    RB_REMOVE(revoked_serial_tree, &rc->revoked_serials, rs);
178
0
    free(rs);
179
0
  }
180
0
  RB_FOREACH_SAFE(rki, revoked_key_id_tree, &rc->revoked_key_ids, trki) {
181
0
    RB_REMOVE(revoked_key_id_tree, &rc->revoked_key_ids, rki);
182
0
    free(rki->key_id);
183
0
    free(rki);
184
0
  }
185
0
  sshkey_free(rc->ca_key);
186
0
  free(rc->ca_blob);
187
0
  freezero(rc, sizeof(*rc));
188
0
}
189
190
void
191
ssh_krl_free(struct ssh_krl *krl)
192
0
{
193
0
  struct revoked_blob *rb, *trb;
194
0
  struct revoked_certs *rc, *trc;
195
196
0
  if (krl == NULL)
197
0
    return;
198
199
0
  free(krl->comment);
200
0
  RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_keys, trb) {
201
0
    RB_REMOVE(revoked_blob_tree, &krl->revoked_keys, rb);
202
0
    free(rb->blob);
203
0
    free(rb);
204
0
  }
205
0
  RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha1s, trb) {
206
0
    RB_REMOVE(revoked_blob_tree, &krl->revoked_sha1s, rb);
207
0
    free(rb->blob);
208
0
    free(rb);
209
0
  }
210
0
  RB_FOREACH_SAFE(rb, revoked_blob_tree, &krl->revoked_sha256s, trb) {
211
0
    RB_REMOVE(revoked_blob_tree, &krl->revoked_sha256s, rb);
212
0
    free(rb->blob);
213
0
    free(rb);
214
0
  }
215
0
  TAILQ_FOREACH_SAFE(rc, &krl->revoked_certs, entry, trc) {
216
0
    TAILQ_REMOVE(&krl->revoked_certs, rc, entry);
217
0
    RB_REMOVE(revoked_certs_tree, &krl->revoked_certs_by_ca, rc);
218
0
    revoked_certs_free(rc);
219
0
  }
220
0
  free(krl);
221
0
}
222
223
void
224
ssh_krl_set_version(struct ssh_krl *krl, uint64_t version)
225
0
{
226
0
  krl->krl_version = version;
227
0
}
228
229
int
230
ssh_krl_set_comment(struct ssh_krl *krl, const char *comment)
231
0
{
232
0
  free(krl->comment);
233
0
  if ((krl->comment = strdup(comment)) == NULL)
234
0
    return SSH_ERR_ALLOC_FAIL;
235
0
  return 0;
236
0
}
237
238
/*
239
 * Find the revoked_certs struct for a CA key. If allow_create is set then
240
 * create a new one in the tree if one did not exist already.
241
 */
242
static int
243
revoked_certs_for_ca_key(struct ssh_krl *krl, const struct sshkey *ca_key,
244
    struct revoked_certs **rcp, int allow_create)
245
0
{
246
0
  struct revoked_certs find, *rc = NULL;
247
0
  int r;
248
249
0
  *rcp = NULL;
250
0
  memset(&find, 0, sizeof(find));
251
0
  if (ca_key != NULL && (r = sshkey_to_blob(ca_key,
252
0
      &find.ca_blob, &find.ca_blob_len)) != 0)
253
0
    return r;
254
0
  if ((rc = RB_FIND(revoked_certs_tree, &krl->revoked_certs_by_ca,
255
0
      &find)) != NULL || !allow_create) {
256
0
    r = 0;
257
0
    goto out;
258
0
  }
259
  /* If this CA doesn't exist in the tree then add it now */
260
0
  if ((rc = calloc(1, sizeof(*rc))) == NULL) {
261
0
    r = SSH_ERR_ALLOC_FAIL;
262
0
    goto out;
263
0
  }
264
0
  if (ca_key != NULL &&
265
0
      (r = sshkey_from_private(ca_key, &rc->ca_key)) != 0) {
266
0
    free(rc);
267
0
    rc = NULL;
268
0
    goto out;
269
0
  }
270
0
  rc->ca_blob = find.ca_blob;
271
0
  rc->ca_blob_len = find.ca_blob_len;
272
0
  find.ca_blob = NULL;
273
0
  RB_INIT(&rc->revoked_serials);
274
0
  RB_INIT(&rc->revoked_key_ids);
275
0
  TAILQ_INSERT_TAIL(&krl->revoked_certs, rc, entry);
276
0
  RB_INSERT(revoked_certs_tree, &krl->revoked_certs_by_ca, rc);
277
0
  KRL_DBG(("new CA %s", ca_key == NULL ? "*" : sshkey_type(ca_key)));
278
0
  r = 0;
279
0
 out:
280
0
  free(find.ca_blob);
281
0
  *rcp = rc;
282
0
  return r;
283
0
}
284
285
static int
286
insert_serial_range(struct revoked_serial_tree *rt, uint64_t lo, uint64_t hi)
287
0
{
288
0
  struct revoked_serial rs, *ers, *crs, *irs;
289
290
0
  KRL_DBG(("insert %llu:%llu",
291
0
      (unsigned long long)lo, (unsigned long long)hi));
292
0
  memset(&rs, 0, sizeof(rs));
293
0
  rs.lo = lo;
294
0
  rs.hi = hi;
295
0
  ers = RB_NFIND(revoked_serial_tree, rt, &rs);
296
0
  if (ers == NULL || serial_cmp(ers, &rs) != 0) {
297
    /* No entry matches. Just insert */
298
0
    if ((irs = malloc(sizeof(rs))) == NULL)
299
0
      return SSH_ERR_ALLOC_FAIL;
300
0
    memcpy(irs, &rs, sizeof(*irs));
301
0
    ers = RB_INSERT(revoked_serial_tree, rt, irs);
302
0
    if (ers != NULL) {
303
0
      KRL_DBG(("bad: ers != NULL"));
304
      /* Shouldn't happen */
305
0
      free(irs);
306
0
      return SSH_ERR_INTERNAL_ERROR;
307
0
    }
308
0
    ers = irs;
309
0
  } else {
310
0
    KRL_DBG(("overlap found %llu:%llu",
311
0
        (unsigned long long)ers->lo, (unsigned long long)ers->hi));
312
    /*
313
     * The inserted entry overlaps an existing one. Grow the
314
     * existing entry.
315
     */
316
0
    if (ers->lo > lo)
317
0
      ers->lo = lo;
318
0
    if (ers->hi < hi)
319
0
      ers->hi = hi;
320
0
  }
321
322
  /*
323
   * The inserted or revised range might overlap or abut adjacent ones;
324
   * coalesce as necessary.
325
   */
326
327
  /* Check predecessors */
328
0
  while ((crs = RB_PREV(revoked_serial_tree, rt, ers)) != NULL) {
329
0
    KRL_DBG(("pred %llu:%llu",
330
0
        (unsigned long long)crs->lo, (unsigned long long)crs->hi));
331
0
    if (ers->lo != 0 && crs->hi < ers->lo - 1)
332
0
      break;
333
    /* This entry overlaps. */
334
0
    if (crs->lo < ers->lo) {
335
0
      ers->lo = crs->lo;
336
0
      KRL_DBG(("pred extend %llu:%llu",
337
0
          (unsigned long long)ers->lo,
338
0
          (unsigned long long)ers->hi));
339
0
    }
340
0
    RB_REMOVE(revoked_serial_tree, rt, crs);
341
0
    free(crs);
342
0
  }
343
  /* Check successors */
344
0
  while ((crs = RB_NEXT(revoked_serial_tree, rt, ers)) != NULL) {
345
0
    KRL_DBG(("succ %llu:%llu",
346
0
        (unsigned long long)crs->lo, (unsigned long long)crs->hi));
347
0
    if (ers->hi != (uint64_t)-1 && crs->lo > ers->hi + 1)
348
0
      break;
349
    /* This entry overlaps. */
350
0
    if (crs->hi > ers->hi) {
351
0
      ers->hi = crs->hi;
352
0
      KRL_DBG(("succ extend %llu:%llu",
353
0
          (unsigned long long)ers->lo,
354
0
          (unsigned long long)ers->hi));
355
0
    }
356
0
    RB_REMOVE(revoked_serial_tree, rt, crs);
357
0
    free(crs);
358
0
  }
359
0
  KRL_DBG(("done, final %llu:%llu",
360
0
      (unsigned long long)ers->lo, (unsigned long long)ers->hi));
361
0
  return 0;
362
0
}
363
364
/*
365
 * Revoke a range of serials. If *rcp is NULL, then it will be set to the
366
 * revoked_certs for ca_key, so callers making multiple revocations for the
367
 * same CA need only search for it once.
368
 */
369
static int
370
revoke_serial_range(struct ssh_krl *krl, const struct sshkey *ca_key,
371
    struct revoked_certs **rcp, uint64_t lo, uint64_t hi)
372
0
{
373
0
  int r;
374
375
0
  if (lo > hi || lo == 0)
376
0
    return SSH_ERR_INVALID_ARGUMENT;
377
0
  if (*rcp == NULL &&
378
0
      (r = revoked_certs_for_ca_key(krl, ca_key, rcp, 1)) != 0)
379
0
    return r;
380
0
  return insert_serial_range(&(*rcp)->revoked_serials, lo, hi);
381
0
}
382
383
int
384
ssh_krl_revoke_cert_by_serial(struct ssh_krl *krl, const struct sshkey *ca_key,
385
    uint64_t serial)
386
0
{
387
0
  return ssh_krl_revoke_cert_by_serial_range(krl, ca_key, serial, serial);
388
0
}
389
390
int
391
ssh_krl_revoke_cert_by_serial_range(struct ssh_krl *krl,
392
    const struct sshkey *ca_key, uint64_t lo, uint64_t hi)
393
0
{
394
0
  struct revoked_certs *rc = NULL;
395
396
0
  return revoke_serial_range(krl, ca_key, &rc, lo, hi);
397
0
}
398
399
/* Revoke a key ID. *rcp is treated as per revoke_serial_range() */
400
static int
401
revoke_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
402
    struct revoked_certs **rcp, const char *key_id)
403
0
{
404
0
  struct revoked_key_id *rki, *erki;
405
0
  int r;
406
407
0
  if (*rcp == NULL &&
408
0
      (r = revoked_certs_for_ca_key(krl, ca_key, rcp, 1)) != 0)
409
0
    return r;
410
411
0
  KRL_DBG(("revoke %s", key_id));
412
0
  if ((rki = calloc(1, sizeof(*rki))) == NULL ||
413
0
      (rki->key_id = strdup(key_id)) == NULL) {
414
0
    free(rki);
415
0
    return SSH_ERR_ALLOC_FAIL;
416
0
  }
417
0
  erki = RB_INSERT(revoked_key_id_tree, &(*rcp)->revoked_key_ids, rki);
418
0
  if (erki != NULL) {
419
0
    free(rki->key_id);
420
0
    free(rki);
421
0
  }
422
0
  return 0;
423
0
}
424
425
int
426
ssh_krl_revoke_cert_by_key_id(struct ssh_krl *krl, const struct sshkey *ca_key,
427
    const char *key_id)
428
0
{
429
0
  struct revoked_certs *rc = NULL;
430
431
0
  return revoke_key_id(krl, ca_key, &rc, key_id);
432
0
}
433
434
/* Convert "key" to a public key blob without any certificate information */
435
static int
436
plain_key_blob(const struct sshkey *key, u_char **blob, size_t *blen)
437
0
{
438
0
  struct sshkey *kcopy;
439
0
  int r;
440
441
0
  if ((r = sshkey_from_private(key, &kcopy)) != 0)
442
0
    return r;
443
0
  if (sshkey_is_cert(kcopy)) {
444
0
    if ((r = sshkey_drop_cert(kcopy)) != 0) {
445
0
      sshkey_free(kcopy);
446
0
      return r;
447
0
    }
448
0
  }
449
0
  r = sshkey_to_blob(kcopy, blob, blen);
450
0
  sshkey_free(kcopy);
451
0
  return r;
452
0
}
453
454
/* Revoke a key blob. Ownership of blob is transferred to the tree */
455
static int
456
revoke_blob(struct revoked_blob_tree *rbt, u_char *blob, size_t len)
457
0
{
458
0
  struct revoked_blob *rb, *erb;
459
460
0
  if ((rb = calloc(1, sizeof(*rb))) == NULL)
461
0
    return SSH_ERR_ALLOC_FAIL;
462
0
  rb->blob = blob;
463
0
  rb->len = len;
464
0
  erb = RB_INSERT(revoked_blob_tree, rbt, rb);
465
0
  if (erb != NULL) {
466
0
    free(rb->blob);
467
0
    free(rb);
468
0
  }
469
0
  return 0;
470
0
}
471
472
int
473
ssh_krl_revoke_key_explicit(struct ssh_krl *krl, const struct sshkey *key)
474
0
{
475
0
  u_char *blob;
476
0
  size_t len;
477
0
  int r;
478
479
0
  debug3_f("revoke type %s", sshkey_type(key));
480
0
  if ((r = plain_key_blob(key, &blob, &len)) != 0)
481
0
    return r;
482
0
  return revoke_blob(&krl->revoked_keys, blob, len);
483
0
}
484
485
static int
486
revoke_by_hash(struct revoked_blob_tree *target, const u_char *p, size_t len)
487
0
{
488
0
  u_char *blob;
489
0
  int r;
490
491
  /* need to copy hash, as revoke_blob steals ownership */
492
0
  if ((blob = malloc(len)) == NULL)
493
0
    return SSH_ERR_SYSTEM_ERROR;
494
0
  memcpy(blob, p, len);
495
0
  if ((r = revoke_blob(target, blob, len)) != 0) {
496
0
    free(blob);
497
0
    return r;
498
0
  }
499
0
  return 0;
500
0
}
501
502
int
503
ssh_krl_revoke_key_sha1(struct ssh_krl *krl, const u_char *p, size_t len)
504
0
{
505
0
  debug3_f("revoke by sha1");
506
0
  if (len != 20)
507
0
    return SSH_ERR_INVALID_FORMAT;
508
0
  return revoke_by_hash(&krl->revoked_sha1s, p, len);
509
0
}
510
511
int
512
ssh_krl_revoke_key_sha256(struct ssh_krl *krl, const u_char *p, size_t len)
513
0
{
514
0
  debug3_f("revoke by sha256");
515
0
  if (len != 32)
516
0
    return SSH_ERR_INVALID_FORMAT;
517
0
  return revoke_by_hash(&krl->revoked_sha256s, p, len);
518
0
}
519
520
int
521
ssh_krl_revoke_key(struct ssh_krl *krl, const struct sshkey *key)
522
0
{
523
  /* XXX replace with SHA256? */
524
0
  if (!sshkey_is_cert(key))
525
0
    return ssh_krl_revoke_key_explicit(krl, key);
526
527
0
  if (key->cert->serial == 0) {
528
0
    return ssh_krl_revoke_cert_by_key_id(krl,
529
0
        key->cert->signature_key,
530
0
        key->cert->key_id);
531
0
  } else {
532
0
    return ssh_krl_revoke_cert_by_serial(krl,
533
0
        key->cert->signature_key,
534
0
        key->cert->serial);
535
0
  }
536
0
}
537
538
/*
539
 * Select the most compact section type to emit next in a KRL based on
540
 * the current section type, the run length of contiguous revoked serial
541
 * numbers and the gaps from the last and to the next revoked serial.
542
 * Applies a mostly-accurate bit cost model to select the section type
543
 * that will minimise the size of the resultant KRL.
544
 */
545
static int
546
choose_next_state(int current_state, uint64_t contig, int final,
547
    uint64_t last_gap, uint64_t next_gap, size_t bitmap_len,
548
    int *force_new_section)
549
0
{
550
0
  int new_state;
551
0
  uint64_t cost, cost_list, cost_range, cost_bitmap, cost_bitmap_restart;
552
553
  /*
554
   * Avoid unsigned overflows.
555
   * The limits are high enough to avoid confusing the calculations.
556
   */
557
0
  contig = MINIMUM(contig, 1ULL<<31);
558
0
  last_gap = MINIMUM(last_gap, 1ULL<<31);
559
0
  next_gap = MINIMUM(next_gap, 1ULL<<31);
560
561
  /*
562
   * Calculate the cost to switch from the current state to candidates.
563
   * NB. range sections only ever contain a single range, so their
564
   * switching cost is independent of the current_state.
565
   */
566
0
  cost_list = cost_bitmap = cost_bitmap_restart = 0;
567
0
  cost_range = 8;
568
0
  switch (current_state) {
569
0
  case KRL_SECTION_CERT_SERIAL_LIST:
570
0
    cost_bitmap_restart = cost_bitmap = 8 + 64;
571
0
    break;
572
0
  case KRL_SECTION_CERT_SERIAL_BITMAP:
573
0
    cost_list = 8;
574
0
    cost_bitmap_restart = 8 + 64;
575
0
    break;
576
0
  case KRL_SECTION_CERT_SERIAL_RANGE:
577
0
  case 0:
578
0
    cost_bitmap_restart = cost_bitmap = 8 + 64;
579
0
    cost_list = 8;
580
0
  }
581
582
  /* Estimate base cost in bits of each section type */
583
0
  cost_list += 64 * contig + (final ? 0 : 8+64);
584
0
  cost_range += (2 * 64) + (final ? 0 : 8+64);
585
0
  cost_bitmap += last_gap + contig + (final ? 0 : MINIMUM(next_gap, 8+64));
586
0
  cost_bitmap_restart += contig + (final ? 0 : MINIMUM(next_gap, 8+64));
587
588
  /* Convert to byte costs for actual comparison */
589
0
  cost_list = (cost_list + 7) / 8;
590
0
  cost_bitmap = (cost_bitmap + 7) / 8;
591
0
  cost_bitmap_restart = (cost_bitmap_restart + 7) / 8;
592
0
  cost_range = (cost_range + 7) / 8;
593
594
  /* Ensure that we never emit a bitmap larger than sshbuf can handle */
595
0
  if (bitmap_len + cost_bitmap >= SSHBUF_MAX_BIGNUM) {
596
0
    KRL_DBG(("bitmap overfull len=%zu cost=%llu max=%u",
597
0
        bitmap_len, (unsigned long long)cost_bitmap,
598
0
        SSHBUF_MAX_BIGNUM));
599
0
    cost_bitmap = (uint64_t)-1;
600
0
  }
601
602
  /* Now pick the best choice */
603
0
  *force_new_section = 0;
604
0
  new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
605
0
  cost = cost_bitmap;
606
0
  if (cost_range < cost) {
607
0
    new_state = KRL_SECTION_CERT_SERIAL_RANGE;
608
0
    cost = cost_range;
609
0
  }
610
0
  if (cost_list < cost) {
611
0
    new_state = KRL_SECTION_CERT_SERIAL_LIST;
612
0
    cost = cost_list;
613
0
  }
614
0
  if (cost_bitmap_restart < cost) {
615
0
    new_state = KRL_SECTION_CERT_SERIAL_BITMAP;
616
0
    *force_new_section = 1;
617
0
    cost = cost_bitmap_restart;
618
0
  }
619
0
  KRL_DBG(("contig %llu last_gap %llu next_gap %llu final %d, costs:"
620
0
      "list %llu range %llu bitmap %llu new bitmap %llu, "
621
0
      "selected 0x%02x%s", (long long unsigned)contig,
622
0
      (long long unsigned)last_gap, (long long unsigned)next_gap, final,
623
0
      (long long unsigned)cost_list, (long long unsigned)cost_range,
624
0
      (long long unsigned)cost_bitmap,
625
0
      (long long unsigned)cost_bitmap_restart, new_state,
626
0
      *force_new_section ? " restart" : ""));
627
0
  return new_state;
628
0
}
629
630
static int
631
put_bitmap(struct sshbuf *buf, struct bitmap *bitmap)
632
0
{
633
0
  size_t len;
634
0
  u_char *blob;
635
0
  int r;
636
637
0
  len = bitmap_nbytes(bitmap);
638
0
  if ((blob = malloc(len)) == NULL)
639
0
    return SSH_ERR_ALLOC_FAIL;
640
0
  if (bitmap_to_string(bitmap, blob, len) != 0) {
641
0
    free(blob);
642
0
    return SSH_ERR_INTERNAL_ERROR;
643
0
  }
644
0
  r = sshbuf_put_bignum2_bytes(buf, blob, len);
645
0
  free(blob);
646
0
  return r;
647
0
}
648
649
/* Generate a KRL_SECTION_CERTIFICATES KRL section */
650
static int
651
revoked_certs_generate(struct revoked_certs *rc, struct sshbuf *buf)
652
0
{
653
0
  int final, force_new_sect, r = SSH_ERR_INTERNAL_ERROR;
654
0
  uint64_t i, contig, gap, last = 0, bitmap_start = 0;
655
0
  struct revoked_serial *rs, *nrs;
656
0
  struct revoked_key_id *rki;
657
0
  int next_state, state = 0;
658
0
  size_t bitmap_len;
659
0
  struct sshbuf *sect;
660
0
  struct bitmap *bitmap = NULL;
661
662
0
  if ((sect = sshbuf_new()) == NULL)
663
0
    return SSH_ERR_ALLOC_FAIL;
664
665
  /* Store the header: optional CA scope key, reserved */
666
0
  if (rc->ca_key == NULL) {
667
0
    if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
668
0
      goto out;
669
0
  } else {
670
0
    if ((r = sshkey_puts(rc->ca_key, buf)) != 0)
671
0
      goto out;
672
0
  }
673
0
  if ((r = sshbuf_put_string(buf, NULL, 0)) != 0)
674
0
    goto out;
675
676
  /* Store the revoked serials.  */
677
0
  for (rs = RB_MIN(revoked_serial_tree, &rc->revoked_serials);
678
0
       rs != NULL;
679
0
       rs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs)) {
680
0
    KRL_DBG(("serial %llu:%llu state 0x%02x",
681
0
        (long long unsigned)rs->lo, (long long unsigned)rs->hi,
682
0
        state));
683
684
    /* Check contiguous length and gap to next section (if any) */
685
0
    nrs = RB_NEXT(revoked_serial_tree, &rc->revoked_serials, rs);
686
0
    final = nrs == NULL;
687
0
    gap = nrs == NULL ? 0 : nrs->lo - rs->hi;
688
0
    contig = 1 + (rs->hi - rs->lo);
689
690
0
    bitmap_len = state == KRL_SECTION_CERT_SERIAL_BITMAP ?
691
0
        bitmap_nbytes(bitmap) : 0;
692
693
    /* Choose next state based on these */
694
0
    next_state = choose_next_state(state, contig, final,
695
0
        state == 0 ? 0 : rs->lo - last, gap, bitmap_len,
696
0
        &force_new_sect);
697
698
    /*
699
     * If the current section is a range section or has a different
700
     * type to the next section, then finish it off now.
701
     */
702
0
    if (state != 0 && (force_new_sect || next_state != state ||
703
0
        state == KRL_SECTION_CERT_SERIAL_RANGE)) {
704
0
      KRL_DBG(("finish state 0x%02x", state));
705
0
      switch (state) {
706
0
      case KRL_SECTION_CERT_SERIAL_LIST:
707
0
      case KRL_SECTION_CERT_SERIAL_RANGE:
708
0
        break;
709
0
      case KRL_SECTION_CERT_SERIAL_BITMAP:
710
0
        if ((r = put_bitmap(sect, bitmap)) != 0)
711
0
          goto out;
712
0
        bitmap_free(bitmap);
713
0
        bitmap = NULL;
714
0
        break;
715
0
      }
716
0
      if ((r = sshbuf_put_u8(buf, state)) != 0 ||
717
0
          (r = sshbuf_put_stringb(buf, sect)) != 0)
718
0
        goto out;
719
0
      sshbuf_reset(sect);
720
0
    }
721
722
    /* If we are starting a new section then prepare it now */
723
0
    if (next_state != state || force_new_sect) {
724
0
      KRL_DBG(("start state 0x%02x",
725
0
          next_state));
726
0
      state = next_state;
727
0
      sshbuf_reset(sect);
728
0
      switch (state) {
729
0
      case KRL_SECTION_CERT_SERIAL_LIST:
730
0
      case KRL_SECTION_CERT_SERIAL_RANGE:
731
0
        break;
732
0
      case KRL_SECTION_CERT_SERIAL_BITMAP:
733
0
        if ((bitmap = bitmap_new()) == NULL) {
734
0
          r = SSH_ERR_ALLOC_FAIL;
735
0
          goto out;
736
0
        }
737
0
        bitmap_start = rs->lo;
738
0
        if ((r = sshbuf_put_u64(sect,
739
0
            bitmap_start)) != 0)
740
0
          goto out;
741
0
        break;
742
0
      }
743
0
    }
744
745
    /* Perform section-specific processing */
746
0
    switch (state) {
747
0
    case KRL_SECTION_CERT_SERIAL_LIST:
748
0
      for (i = 0; i < contig; i++) {
749
0
        if ((r = sshbuf_put_u64(sect, rs->lo + i)) != 0)
750
0
          goto out;
751
0
      }
752
0
      break;
753
0
    case KRL_SECTION_CERT_SERIAL_RANGE:
754
0
      if ((r = sshbuf_put_u64(sect, rs->lo)) != 0 ||
755
0
          (r = sshbuf_put_u64(sect, rs->hi)) != 0)
756
0
        goto out;
757
0
      break;
758
0
    case KRL_SECTION_CERT_SERIAL_BITMAP:
759
0
      if (rs->lo - bitmap_start > INT_MAX) {
760
0
        r = SSH_ERR_INVALID_FORMAT;
761
0
        error_f("insane bitmap gap");
762
0
        goto out;
763
0
      }
764
0
      for (i = 0; i < contig; i++) {
765
0
        if (bitmap_set_bit(bitmap,
766
0
            rs->lo + i - bitmap_start) != 0) {
767
0
          r = SSH_ERR_ALLOC_FAIL;
768
0
          goto out;
769
0
        }
770
0
      }
771
0
      break;
772
0
    }
773
0
    last = rs->hi;
774
0
  }
775
  /* Flush the remaining section, if any */
776
0
  if (state != 0) {
777
0
    KRL_DBG(("serial final flush for state 0x%02x", state));
778
0
    switch (state) {
779
0
    case KRL_SECTION_CERT_SERIAL_LIST:
780
0
    case KRL_SECTION_CERT_SERIAL_RANGE:
781
0
      break;
782
0
    case KRL_SECTION_CERT_SERIAL_BITMAP:
783
0
      if ((r = put_bitmap(sect, bitmap)) != 0)
784
0
        goto out;
785
0
      bitmap_free(bitmap);
786
0
      bitmap = NULL;
787
0
      break;
788
0
    }
789
0
    if ((r = sshbuf_put_u8(buf, state)) != 0 ||
790
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
791
0
      goto out;
792
0
  }
793
0
  KRL_DBG(("serial done "));
794
795
  /* Now output a section for any revocations by key ID */
796
0
  sshbuf_reset(sect);
797
0
  RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
798
0
    KRL_DBG(("key ID %s", rki->key_id));
799
0
    if ((r = sshbuf_put_cstring(sect, rki->key_id)) != 0)
800
0
      goto out;
801
0
  }
802
0
  if (sshbuf_len(sect) != 0) {
803
0
    if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERT_KEY_ID)) != 0 ||
804
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
805
0
      goto out;
806
0
  }
807
0
  r = 0;
808
0
 out:
809
0
  bitmap_free(bitmap);
810
0
  sshbuf_free(sect);
811
0
  return r;
812
0
}
813
814
int
815
ssh_krl_to_blob(struct ssh_krl *krl, struct sshbuf *buf)
816
0
{
817
0
  int r = SSH_ERR_INTERNAL_ERROR;
818
0
  struct revoked_certs *rc;
819
0
  struct revoked_blob *rb;
820
0
  struct sshbuf *sect;
821
0
  u_char *sblob = NULL;
822
823
0
  if (krl->generated_date == 0)
824
0
    krl->generated_date = time(NULL);
825
826
0
  if ((sect = sshbuf_new()) == NULL)
827
0
    return SSH_ERR_ALLOC_FAIL;
828
829
  /* Store the header */
830
0
  if ((r = sshbuf_put(buf, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0 ||
831
0
      (r = sshbuf_put_u32(buf, KRL_FORMAT_VERSION)) != 0 ||
832
0
      (r = sshbuf_put_u64(buf, krl->krl_version)) != 0 ||
833
0
      (r = sshbuf_put_u64(buf, krl->generated_date)) != 0 ||
834
0
      (r = sshbuf_put_u64(buf, krl->flags)) != 0 ||
835
0
      (r = sshbuf_put_string(buf, NULL, 0)) != 0 ||
836
0
      (r = sshbuf_put_cstring(buf, krl->comment)) != 0)
837
0
    goto out;
838
839
  /* Store sections for revoked certificates */
840
0
  TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
841
0
    sshbuf_reset(sect);
842
0
    if ((r = revoked_certs_generate(rc, sect)) != 0)
843
0
      goto out;
844
0
    if ((r = sshbuf_put_u8(buf, KRL_SECTION_CERTIFICATES)) != 0 ||
845
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
846
0
      goto out;
847
0
  }
848
849
  /* Finally, output sections for revocations by public key/hash */
850
0
  sshbuf_reset(sect);
851
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
852
0
    KRL_DBG(("key len %zu ", rb->len));
853
0
    if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
854
0
      goto out;
855
0
  }
856
0
  if (sshbuf_len(sect) != 0) {
857
0
    if ((r = sshbuf_put_u8(buf, KRL_SECTION_EXPLICIT_KEY)) != 0 ||
858
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
859
0
      goto out;
860
0
  }
861
0
  sshbuf_reset(sect);
862
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
863
0
    KRL_DBG(("hash len %zu ", rb->len));
864
0
    if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
865
0
      goto out;
866
0
  }
867
0
  if (sshbuf_len(sect) != 0) {
868
0
    if ((r = sshbuf_put_u8(buf,
869
0
        KRL_SECTION_FINGERPRINT_SHA1)) != 0 ||
870
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
871
0
      goto out;
872
0
  }
873
0
  sshbuf_reset(sect);
874
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
875
0
    KRL_DBG(("hash len %zu ", rb->len));
876
0
    if ((r = sshbuf_put_string(sect, rb->blob, rb->len)) != 0)
877
0
      goto out;
878
0
  }
879
0
  if (sshbuf_len(sect) != 0) {
880
0
    if ((r = sshbuf_put_u8(buf,
881
0
        KRL_SECTION_FINGERPRINT_SHA256)) != 0 ||
882
0
        (r = sshbuf_put_stringb(buf, sect)) != 0)
883
0
      goto out;
884
0
  }
885
  /* success */
886
0
  r = 0;
887
0
 out:
888
0
  free(sblob);
889
0
  sshbuf_free(sect);
890
0
  return r;
891
0
}
892
893
static void
894
format_timestamp(uint64_t timestamp, char *ts, size_t nts)
895
0
{
896
0
  time_t t;
897
0
  struct tm *tm;
898
899
0
  t = timestamp;
900
0
  tm = localtime(&t);
901
0
  if (tm == NULL)
902
0
    strlcpy(ts, "<INVALID>", nts);
903
0
  else {
904
0
    *ts = '\0';
905
0
    strftime(ts, nts, "%Y%m%dT%H%M%S", tm);
906
0
  }
907
0
}
908
909
static int
910
cert_extension_subsection(struct sshbuf *subsect, struct ssh_krl *krl)
911
0
{
912
0
  int r = SSH_ERR_INTERNAL_ERROR;
913
0
  u_char critical = 1;
914
0
  struct sshbuf *value = NULL;
915
0
  char *name = NULL;
916
917
0
  if ((r = sshbuf_get_cstring(subsect, &name, NULL)) != 0 ||
918
0
      (r = sshbuf_get_u8(subsect, &critical)) != 0 ||
919
0
      (r = sshbuf_froms(subsect, &value)) != 0) {
920
0
    debug_fr(r, "parse");
921
0
    error("KRL has invalid certificate extension subsection");
922
0
    r = SSH_ERR_INVALID_FORMAT;
923
0
    goto out;
924
0
  }
925
0
  if (sshbuf_len(subsect) != 0) {
926
0
    error("KRL has invalid certificate extension subsection: "
927
0
        "trailing data");
928
0
    r = SSH_ERR_INVALID_FORMAT;
929
0
    goto out;
930
0
  }
931
0
  debug_f("cert extension %s critical %u len %zu",
932
0
      name, critical, sshbuf_len(value));
933
  /* no extensions are currently supported */
934
0
  if (critical) {
935
0
    error("KRL contains unsupported critical certificate "
936
0
        "subsection \"%s\"", name);
937
0
    r = SSH_ERR_FEATURE_UNSUPPORTED;
938
0
    goto out;
939
0
  }
940
  /* success */
941
0
  r = 0;
942
0
 out:
943
0
  free(name);
944
0
  sshbuf_free(value);
945
0
  return r;
946
0
}
947
948
static int
949
parse_revoked_certs(struct sshbuf *buf, struct ssh_krl *krl)
950
0
{
951
0
  int r = SSH_ERR_INTERNAL_ERROR;
952
0
  u_char type;
953
0
  const u_char *blob;
954
0
  size_t blen, nbits;
955
0
  struct sshbuf *subsect = NULL;
956
0
  uint64_t serial, serial_lo, serial_hi;
957
0
  struct bitmap *bitmap = NULL;
958
0
  char *key_id = NULL;
959
0
  struct sshkey *ca_key = NULL;
960
0
  struct revoked_certs *rc = NULL;
961
962
0
  if ((subsect = sshbuf_new()) == NULL)
963
0
    return SSH_ERR_ALLOC_FAIL;
964
965
  /* Header: key, reserved */
966
0
  if ((r = sshbuf_get_string_direct(buf, &blob, &blen)) != 0 ||
967
0
      (r = sshbuf_skip_string(buf)) != 0)
968
0
    goto out;
969
0
  if (blen != 0 && (r = sshkey_from_blob(blob, blen, &ca_key)) != 0)
970
0
    goto out;
971
972
0
  while (sshbuf_len(buf) > 0) {
973
0
    sshbuf_free(subsect);
974
0
    subsect = NULL;
975
0
    if ((r = sshbuf_get_u8(buf, &type)) != 0 ||
976
0
        (r = sshbuf_froms(buf, &subsect)) != 0)
977
0
      goto out;
978
0
    KRL_DBG(("subsection type 0x%02x", type));
979
    /* sshbuf_dump(subsect, stderr); */
980
981
0
    switch (type) {
982
0
    case KRL_SECTION_CERT_SERIAL_LIST:
983
0
      while (sshbuf_len(subsect) > 0) {
984
0
        if ((r = sshbuf_get_u64(subsect, &serial)) != 0)
985
0
          goto out;
986
0
        if ((r = revoke_serial_range(krl, ca_key, &rc,
987
0
            serial, serial)) != 0)
988
0
          goto out;
989
0
      }
990
0
      break;
991
0
    case KRL_SECTION_CERT_SERIAL_RANGE:
992
0
      if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
993
0
          (r = sshbuf_get_u64(subsect, &serial_hi)) != 0)
994
0
        goto out;
995
0
      if ((r = revoke_serial_range(krl, ca_key, &rc,
996
0
          serial_lo, serial_hi)) != 0)
997
0
        goto out;
998
0
      break;
999
0
    case KRL_SECTION_CERT_SERIAL_BITMAP:
1000
0
      if ((bitmap = bitmap_new()) == NULL) {
1001
0
        r = SSH_ERR_ALLOC_FAIL;
1002
0
        goto out;
1003
0
      }
1004
0
      if ((r = sshbuf_get_u64(subsect, &serial_lo)) != 0 ||
1005
0
          (r = sshbuf_get_bignum2_bytes_direct(subsect,
1006
0
          &blob, &blen)) != 0)
1007
0
        goto out;
1008
0
      if (bitmap_from_string(bitmap, blob, blen) != 0) {
1009
0
        r = SSH_ERR_INVALID_FORMAT;
1010
0
        goto out;
1011
0
      }
1012
0
      nbits = bitmap_nbits(bitmap);
1013
0
      for (serial = 0; serial < (uint64_t)nbits; serial++) {
1014
0
        if (serial > 0 && serial_lo + serial == 0) {
1015
0
          error_f("bitmap wraps u64");
1016
0
          r = SSH_ERR_INVALID_FORMAT;
1017
0
          goto out;
1018
0
        }
1019
0
        if (!bitmap_test_bit(bitmap, serial))
1020
0
          continue;
1021
0
        if ((r = revoke_serial_range(krl, ca_key, &rc,
1022
0
            serial_lo + serial,
1023
0
            serial_lo + serial)) != 0)
1024
0
          goto out;
1025
0
      }
1026
0
      bitmap_free(bitmap);
1027
0
      bitmap = NULL;
1028
0
      break;
1029
0
    case KRL_SECTION_CERT_KEY_ID:
1030
0
      while (sshbuf_len(subsect) > 0) {
1031
0
        if ((r = sshbuf_get_cstring(subsect,
1032
0
            &key_id, NULL)) != 0)
1033
0
          goto out;
1034
0
        if ((r = revoke_key_id(krl, ca_key, &rc,
1035
0
            key_id)) != 0)
1036
0
          goto out;
1037
0
        free(key_id);
1038
0
        key_id = NULL;
1039
0
      }
1040
0
      break;
1041
0
    case KRL_SECTION_CERT_EXTENSION:
1042
0
      if ((r = cert_extension_subsection(subsect, krl)) != 0)
1043
0
        goto out;
1044
0
      break;
1045
0
    default:
1046
0
      error("Unsupported KRL certificate section %u", type);
1047
0
      r = SSH_ERR_INVALID_FORMAT;
1048
0
      goto out;
1049
0
    }
1050
0
    if (sshbuf_len(subsect) > 0) {
1051
0
      error("KRL certificate section contains unparsed data");
1052
0
      r = SSH_ERR_INVALID_FORMAT;
1053
0
      goto out;
1054
0
    }
1055
0
  }
1056
1057
0
  r = 0;
1058
0
 out:
1059
0
  if (bitmap != NULL)
1060
0
    bitmap_free(bitmap);
1061
0
  free(key_id);
1062
0
  sshkey_free(ca_key);
1063
0
  sshbuf_free(subsect);
1064
0
  return r;
1065
0
}
1066
1067
static int
1068
blob_section(struct sshbuf *sect, struct revoked_blob_tree *target_tree,
1069
    size_t expected_len)
1070
0
{
1071
0
  u_char *rdata = NULL;
1072
0
  size_t rlen = 0;
1073
0
  int r;
1074
1075
0
  while (sshbuf_len(sect) > 0) {
1076
0
    if ((r = sshbuf_get_string(sect, &rdata, &rlen)) != 0)
1077
0
      return r;
1078
0
    if (expected_len != 0 && rlen != expected_len) {
1079
0
      error_f("bad length");
1080
0
      free(rdata);
1081
0
      return SSH_ERR_INVALID_FORMAT;
1082
0
    }
1083
0
    if ((r = revoke_blob(target_tree, rdata, rlen)) != 0) {
1084
0
      free(rdata);
1085
0
      return r;
1086
0
    }
1087
0
  }
1088
0
  return 0;
1089
0
}
1090
1091
static int
1092
extension_section(struct sshbuf *sect, struct ssh_krl *krl)
1093
0
{
1094
0
  int r = SSH_ERR_INTERNAL_ERROR;
1095
0
  u_char critical = 1;
1096
0
  struct sshbuf *value = NULL;
1097
0
  char *name = NULL;
1098
1099
0
  if ((r = sshbuf_get_cstring(sect, &name, NULL)) != 0 ||
1100
0
      (r = sshbuf_get_u8(sect, &critical)) != 0 ||
1101
0
      (r = sshbuf_froms(sect, &value)) != 0) {
1102
0
    debug_fr(r, "parse");
1103
0
    error("KRL has invalid extension section");
1104
0
    r = SSH_ERR_INVALID_FORMAT;
1105
0
    goto out;
1106
0
  }
1107
0
  if (sshbuf_len(sect) != 0) {
1108
0
    error("KRL has invalid extension section: trailing data");
1109
0
    r = SSH_ERR_INVALID_FORMAT;
1110
0
    goto out;
1111
0
  }
1112
0
  debug_f("extension %s critical %u len %zu",
1113
0
      name, critical, sshbuf_len(value));
1114
  /* no extensions are currently supported */
1115
0
  if (critical) {
1116
0
    error("KRL contains unsupported critical section \"%s\"", name);
1117
0
    r = SSH_ERR_FEATURE_UNSUPPORTED;
1118
0
    goto out;
1119
0
  }
1120
  /* success */
1121
0
  r = 0;
1122
0
 out:
1123
0
  free(name);
1124
0
  sshbuf_free(value);
1125
0
  return r;
1126
0
}
1127
1128
/* Attempt to parse a KRL */
1129
int
1130
ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp)
1131
0
{
1132
0
  struct sshbuf *copy = NULL, *sect = NULL;
1133
0
  struct ssh_krl *krl = NULL;
1134
0
  char timestamp[64];
1135
0
  int r = SSH_ERR_INTERNAL_ERROR;
1136
0
  u_char type;
1137
0
  u_int format_version;
1138
1139
0
  *krlp = NULL;
1140
1141
  /* KRL must begin with magic string */
1142
0
  if ((r = sshbuf_cmp(buf, 0, KRL_MAGIC, sizeof(KRL_MAGIC) - 1)) != 0) {
1143
0
    debug2_f("bad KRL magic header");
1144
0
    return SSH_ERR_KRL_BAD_MAGIC;
1145
0
  }
1146
1147
0
  if ((krl = ssh_krl_init()) == NULL) {
1148
0
    r = SSH_ERR_ALLOC_FAIL;
1149
0
    error_f("alloc failed");
1150
0
    goto out;
1151
0
  }
1152
  /* Don't modify buffer */
1153
0
  if ((copy = sshbuf_fromb(buf)) == NULL) {
1154
0
    r = SSH_ERR_ALLOC_FAIL;
1155
0
    goto out;
1156
0
  }
1157
0
  if ((r = sshbuf_consume(copy, sizeof(KRL_MAGIC) - 1)) != 0 ||
1158
0
      (r = sshbuf_get_u32(copy, &format_version)) != 0)
1159
0
    goto out;
1160
0
  if (format_version != KRL_FORMAT_VERSION) {
1161
0
    error_f("unsupported KRL format version %u", format_version);
1162
0
    r = SSH_ERR_INVALID_FORMAT;
1163
0
    goto out;
1164
0
  }
1165
0
  if ((r = sshbuf_get_u64(copy, &krl->krl_version)) != 0 ||
1166
0
      (r = sshbuf_get_u64(copy, &krl->generated_date)) != 0 ||
1167
0
      (r = sshbuf_get_u64(copy, &krl->flags)) != 0 ||
1168
0
      (r = sshbuf_skip_string(copy)) != 0 ||
1169
0
      (r = sshbuf_get_cstring(copy, &krl->comment, NULL)) != 0) {
1170
0
    error_fr(r, "parse KRL header");
1171
0
    goto out;
1172
0
  }
1173
0
  format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1174
0
  debug("KRL version %llu generated at %s%s%s",
1175
0
      (long long unsigned)krl->krl_version, timestamp,
1176
0
      *krl->comment ? ": " : "", krl->comment);
1177
1178
  /* Parse and load the KRL sections. */
1179
0
  while (sshbuf_len(copy) > 0) {
1180
0
    sshbuf_free(sect);
1181
0
    sect = NULL;
1182
0
    if ((r = sshbuf_get_u8(copy, &type)) != 0 ||
1183
0
        (r = sshbuf_froms(copy, &sect)) != 0)
1184
0
      goto out;
1185
0
    KRL_DBG(("section 0x%02x", type));
1186
1187
0
    switch (type) {
1188
0
    case KRL_SECTION_CERTIFICATES:
1189
0
      if ((r = parse_revoked_certs(sect, krl)) != 0)
1190
0
        goto out;
1191
0
      break;
1192
0
    case KRL_SECTION_EXPLICIT_KEY:
1193
0
      if ((r = blob_section(sect,
1194
0
          &krl->revoked_keys, 0)) != 0)
1195
0
        goto out;
1196
0
      break;
1197
0
    case KRL_SECTION_FINGERPRINT_SHA1:
1198
0
      if ((r = blob_section(sect,
1199
0
          &krl->revoked_sha1s, 20)) != 0)
1200
0
        goto out;
1201
0
      break;
1202
0
    case KRL_SECTION_FINGERPRINT_SHA256:
1203
0
      if ((r = blob_section(sect,
1204
0
          &krl->revoked_sha256s, 32)) != 0)
1205
0
        goto out;
1206
0
      break;
1207
0
    case KRL_SECTION_EXTENSION:
1208
0
      if ((r = extension_section(sect, krl)) != 0)
1209
0
        goto out;
1210
0
      break;
1211
0
    case KRL_SECTION_SIGNATURE:
1212
      /* Handled above, but still need to stay in synch */
1213
0
      sshbuf_free(sect);
1214
0
      sect = NULL;
1215
0
      if ((r = sshbuf_skip_string(copy)) != 0)
1216
0
        goto out;
1217
0
      break;
1218
0
    default:
1219
0
      error("Unsupported KRL section %u", type);
1220
0
      r = SSH_ERR_INVALID_FORMAT;
1221
0
      goto out;
1222
0
    }
1223
0
    if (sect != NULL && sshbuf_len(sect) > 0) {
1224
0
      error("KRL section contains unparsed data");
1225
0
      r = SSH_ERR_INVALID_FORMAT;
1226
0
      goto out;
1227
0
    }
1228
0
  }
1229
1230
  /* Success */
1231
0
  *krlp = krl;
1232
0
  r = 0;
1233
0
 out:
1234
0
  if (r != 0)
1235
0
    ssh_krl_free(krl);
1236
0
  sshbuf_free(copy);
1237
0
  sshbuf_free(sect);
1238
0
  return r;
1239
0
}
1240
1241
/* Checks certificate serial number and key ID revocation */
1242
static int
1243
is_cert_revoked(const struct sshkey *key, struct revoked_certs *rc)
1244
0
{
1245
0
  struct revoked_serial rs, *ers;
1246
0
  struct revoked_key_id rki, *erki;
1247
1248
  /* Check revocation by cert key ID */
1249
0
  memset(&rki, 0, sizeof(rki));
1250
0
  rki.key_id = key->cert->key_id;
1251
0
  erki = RB_FIND(revoked_key_id_tree, &rc->revoked_key_ids, &rki);
1252
0
  if (erki != NULL) {
1253
0
    KRL_DBG(("revoked by key ID"));
1254
0
    return SSH_ERR_KEY_REVOKED;
1255
0
  }
1256
1257
  /*
1258
   * Zero serials numbers are ignored (it's the default when the
1259
   * CA doesn't specify one).
1260
   */
1261
0
  if (key->cert->serial == 0)
1262
0
    return 0;
1263
1264
0
  memset(&rs, 0, sizeof(rs));
1265
0
  rs.lo = rs.hi = key->cert->serial;
1266
0
  ers = RB_FIND(revoked_serial_tree, &rc->revoked_serials, &rs);
1267
0
  if (ers != NULL) {
1268
0
    KRL_DBG(("revoked serial %llu matched %llu:%llu",
1269
0
        (unsigned long long)key->cert->serial,
1270
0
        (unsigned long long)ers->lo,
1271
0
        (unsigned long long)ers->hi));
1272
0
    return SSH_ERR_KEY_REVOKED;
1273
0
  }
1274
0
  return 0;
1275
0
}
1276
1277
/* Checks whether a given key/cert is revoked. Does not check its CA */
1278
static int
1279
is_key_revoked(struct ssh_krl *krl, const struct sshkey *key)
1280
0
{
1281
0
  struct revoked_blob rb, *erb;
1282
0
  struct revoked_certs *rc;
1283
0
  int r;
1284
1285
  /* Check explicitly revoked hashes first */
1286
0
  memset(&rb, 0, sizeof(rb));
1287
0
  if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA1,
1288
0
      &rb.blob, &rb.len)) != 0)
1289
0
    return r;
1290
0
  erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha1s, &rb);
1291
0
  free(rb.blob);
1292
0
  if (erb != NULL) {
1293
0
    KRL_DBG(("revoked by key SHA1"));
1294
0
    return SSH_ERR_KEY_REVOKED;
1295
0
  }
1296
0
  memset(&rb, 0, sizeof(rb));
1297
0
  if ((r = sshkey_fingerprint_raw(key, SSH_DIGEST_SHA256,
1298
0
      &rb.blob, &rb.len)) != 0)
1299
0
    return r;
1300
0
  erb = RB_FIND(revoked_blob_tree, &krl->revoked_sha256s, &rb);
1301
0
  free(rb.blob);
1302
0
  if (erb != NULL) {
1303
0
    KRL_DBG(("revoked by key SHA256"));
1304
0
    return SSH_ERR_KEY_REVOKED;
1305
0
  }
1306
1307
  /* Next, explicit keys */
1308
0
  memset(&rb, 0, sizeof(rb));
1309
0
  if ((r = plain_key_blob(key, &rb.blob, &rb.len)) != 0)
1310
0
    return r;
1311
0
  erb = RB_FIND(revoked_blob_tree, &krl->revoked_keys, &rb);
1312
0
  free(rb.blob);
1313
0
  if (erb != NULL) {
1314
0
    KRL_DBG(("revoked by explicit key"));
1315
0
    return SSH_ERR_KEY_REVOKED;
1316
0
  }
1317
1318
0
  if (!sshkey_is_cert(key))
1319
0
    return 0;
1320
1321
  /* Check cert revocation for the specified CA */
1322
0
  if ((r = revoked_certs_for_ca_key(krl, key->cert->signature_key,
1323
0
      &rc, 0)) != 0)
1324
0
    return r;
1325
0
  if (rc != NULL) {
1326
0
    if ((r = is_cert_revoked(key, rc)) != 0)
1327
0
      return r;
1328
0
  }
1329
  /* Check cert revocation for the wildcard CA */
1330
0
  if ((r = revoked_certs_for_ca_key(krl, NULL, &rc, 0)) != 0)
1331
0
    return r;
1332
0
  if (rc != NULL) {
1333
0
    if ((r = is_cert_revoked(key, rc)) != 0)
1334
0
      return r;
1335
0
  }
1336
1337
0
  KRL_DBG(("%llu no match", (unsigned long long)key->cert->serial));
1338
0
  return 0;
1339
0
}
1340
1341
int
1342
ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key)
1343
0
{
1344
0
  int r;
1345
1346
0
  KRL_DBG(("checking key"));
1347
0
  if ((r = is_key_revoked(krl, key)) != 0)
1348
0
    return r;
1349
0
  if (sshkey_is_cert(key)) {
1350
0
    debug2_f("checking CA key");
1351
0
    if ((r = is_key_revoked(krl, key->cert->signature_key)) != 0)
1352
0
      return r;
1353
0
  }
1354
0
  KRL_DBG(("key okay"));
1355
0
  return 0;
1356
0
}
1357
1358
int
1359
ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
1360
0
{
1361
0
  struct sshbuf *krlbuf = NULL;
1362
0
  struct ssh_krl *krl = NULL;
1363
0
  int oerrno = 0, r;
1364
1365
0
  if (path == NULL)
1366
0
    return 0;
1367
0
  if ((r = sshbuf_load_file(path, &krlbuf)) != 0) {
1368
0
    oerrno = errno;
1369
0
    goto out;
1370
0
  }
1371
0
  if ((r = ssh_krl_from_blob(krlbuf, &krl)) != 0)
1372
0
    goto out;
1373
0
  debug2_f("checking KRL %s", path);
1374
0
  r = ssh_krl_check_key(krl, key);
1375
0
 out:
1376
0
  sshbuf_free(krlbuf);
1377
0
  ssh_krl_free(krl);
1378
0
  if (r != 0)
1379
0
    errno = oerrno;
1380
0
  return r;
1381
0
}
1382
1383
int
1384
krl_dump(struct ssh_krl *krl, FILE *f)
1385
0
{
1386
0
  struct sshkey *key = NULL;
1387
0
  struct revoked_blob *rb;
1388
0
  struct revoked_certs *rc;
1389
0
  struct revoked_serial *rs;
1390
0
  struct revoked_key_id *rki;
1391
0
  int r, ret = 0;
1392
0
  char *fp, timestamp[64];
1393
1394
  /* Try to print in a KRL spec-compatible format */
1395
0
  format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1396
0
  fprintf(f, "# KRL version %llu\n", (unsigned long long)krl->krl_version);
1397
0
  fprintf(f, "# Generated at %s\n", timestamp);
1398
0
  if (krl->comment != NULL && *krl->comment != '\0') {
1399
0
    r = INT_MAX;
1400
0
    asmprintf(&fp, INT_MAX, &r, "%s", krl->comment);
1401
0
    fprintf(f, "# Comment: %s\n", fp);
1402
0
    free(fp);
1403
0
  }
1404
0
  fputc('\n', f);
1405
1406
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
1407
0
    if ((r = sshkey_from_blob(rb->blob, rb->len, &key)) != 0) {
1408
0
      ret = SSH_ERR_INVALID_FORMAT;
1409
0
      error_r(r, "parse KRL key");
1410
0
      continue;
1411
0
    }
1412
0
    if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
1413
0
        SSH_FP_DEFAULT)) == NULL) {
1414
0
      ret = SSH_ERR_INVALID_FORMAT;
1415
0
      error("sshkey_fingerprint failed");
1416
0
      continue;
1417
0
    }
1418
0
    fprintf(f, "hash: %s # %s\n", fp, sshkey_ssh_name(key));
1419
0
    free(fp);
1420
0
    free(key);
1421
0
  }
1422
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
1423
0
    fp = tohex(rb->blob, rb->len);
1424
0
    fprintf(f, "hash: SHA256:%s\n", fp);
1425
0
    free(fp);
1426
0
  }
1427
0
  RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
1428
    /*
1429
     * There is not KRL spec keyword for raw SHA1 hashes, so
1430
     * print them as comments.
1431
     */
1432
0
    fp = tohex(rb->blob, rb->len);
1433
0
    fprintf(f, "# hash SHA1:%s\n", fp);
1434
0
    free(fp);
1435
0
  }
1436
1437
0
  TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
1438
0
    fputc('\n', f);
1439
0
    if (rc->ca_key == NULL)
1440
0
      fprintf(f, "# Wildcard CA\n");
1441
0
    else {
1442
0
      if ((fp = sshkey_fingerprint(rc->ca_key,
1443
0
          SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL) {
1444
0
        ret = SSH_ERR_INVALID_FORMAT;
1445
0
        error("sshkey_fingerprint failed");
1446
0
        continue;
1447
0
      }
1448
0
      fprintf(f, "# CA key %s %s\n",
1449
0
          sshkey_ssh_name(rc->ca_key), fp);
1450
0
      free(fp);
1451
0
    }
1452
0
    RB_FOREACH(rs, revoked_serial_tree, &rc->revoked_serials) {
1453
0
      if (rs->lo == rs->hi) {
1454
0
        fprintf(f, "serial: %llu\n",
1455
0
            (unsigned long long)rs->lo);
1456
0
      } else {
1457
0
        fprintf(f, "serial: %llu-%llu\n",
1458
0
            (unsigned long long)rs->lo,
1459
0
            (unsigned long long)rs->hi);
1460
0
      }
1461
0
    }
1462
0
    RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
1463
      /*
1464
       * We don't want key IDs with embedded newlines to
1465
       * mess up the display.
1466
       */
1467
0
      r = INT_MAX;
1468
      asmprintf(&fp, INT_MAX, &r, "%s", rki->key_id);
1469
0
      fprintf(f, "id: %s\n", fp);
1470
0
      free(fp);
1471
0
    }
1472
0
  }
1473
0
  return ret;
1474
0
}