Skip to content

Commit 6ec7457

Browse files
committed
upstream: give ssh-keygen the ability to dump the contents of a
binary key revocation list: ssh-keygen -lQf /path bz#3132; ok dtucker OpenBSD-Commit-ID: b76afc4e3b74ab735dbde4e5f0cfa1f02356033b
1 parent af628b8 commit 6ec7457

File tree

4 files changed

+107
-8
lines changed

4 files changed

+107
-8
lines changed

krl.c

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*/
1616

17-
/* $OpenBSD: krl.c,v 1.47 2020/01/25 23:02:13 djm Exp $ */
17+
/* $OpenBSD: krl.c,v 1.48 2020/04/03 02:26:56 djm Exp $ */
1818

1919
#include "includes.h"
2020

@@ -38,6 +38,7 @@
3838
#include "log.h"
3939
#include "digest.h"
4040
#include "bitmap.h"
41+
#include "utf8.h"
4142

4243
#include "krl.h"
4344

@@ -1355,3 +1356,94 @@ ssh_krl_file_contains_key(const char *path, const struct sshkey *key)
13551356
errno = oerrno;
13561357
return r;
13571358
}
1359+
1360+
int
1361+
krl_dump(struct ssh_krl *krl, FILE *f)
1362+
{
1363+
struct sshkey *key = NULL;
1364+
struct revoked_blob *rb;
1365+
struct revoked_certs *rc;
1366+
struct revoked_serial *rs;
1367+
struct revoked_key_id *rki;
1368+
int r, ret = 0;
1369+
char *fp, timestamp[64];
1370+
1371+
/* Try to print in a KRL spec-compatible format */
1372+
format_timestamp(krl->generated_date, timestamp, sizeof(timestamp));
1373+
fprintf(f, "# KRL version %lld\n", krl->krl_version);
1374+
fprintf(f, "# Generated at %s\n", timestamp);
1375+
if (krl->comment != NULL && *krl->comment != '\0') {
1376+
r = INT_MAX;
1377+
asmprintf(&fp, INT_MAX, &r, "%s", krl->comment);
1378+
fprintf(f, "# Comment: %s\n", fp);
1379+
free(fp);
1380+
}
1381+
fputc('\n', f);
1382+
1383+
RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_keys) {
1384+
if ((r = sshkey_from_blob(rb->blob, rb->len, &key)) != 0) {
1385+
ret = SSH_ERR_INVALID_FORMAT;
1386+
error("Parse key in KRL: %s", ssh_err(r));
1387+
continue;
1388+
}
1389+
if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
1390+
SSH_FP_DEFAULT)) == NULL) {
1391+
ret = SSH_ERR_INVALID_FORMAT;
1392+
error("sshkey_fingerprint failed");
1393+
continue;
1394+
}
1395+
fprintf(f, "hash: SHA256:%s # %s\n", fp, sshkey_ssh_name(key));
1396+
free(fp);
1397+
free(key);
1398+
}
1399+
RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha256s) {
1400+
fp = tohex(rb->blob, rb->len);
1401+
fprintf(f, "hash: SHA256:%s\n", fp);
1402+
free(fp);
1403+
}
1404+
RB_FOREACH(rb, revoked_blob_tree, &krl->revoked_sha1s) {
1405+
/*
1406+
* There is not KRL spec keyword for raw SHA1 hashes, so
1407+
* print them as comments.
1408+
*/
1409+
fp = tohex(rb->blob, rb->len);
1410+
fprintf(f, "# hash SHA1:%s\n", fp);
1411+
free(fp);
1412+
}
1413+
1414+
TAILQ_FOREACH(rc, &krl->revoked_certs, entry) {
1415+
fputc('\n', f);
1416+
if (rc->ca_key == NULL)
1417+
fprintf(f, "# Wildcard CA\n");
1418+
else {
1419+
if ((fp = sshkey_fingerprint(rc->ca_key,
1420+
SSH_FP_HASH_DEFAULT, SSH_FP_DEFAULT)) == NULL) {
1421+
ret = SSH_ERR_INVALID_FORMAT;
1422+
error("sshkey_fingerprint failed");
1423+
continue;
1424+
}
1425+
fprintf(f, "# CA key %s %s\n",
1426+
sshkey_ssh_name(rc->ca_key), fp);
1427+
free(fp);
1428+
}
1429+
RB_FOREACH(rs, revoked_serial_tree, &rc->revoked_serials) {
1430+
if (rs->lo == rs->hi)
1431+
fprintf(f, "serial: %lld\n", rs->lo);
1432+
else {
1433+
fprintf(f, "serial: %lld-%lld\n",
1434+
rs->lo, rs->hi);
1435+
}
1436+
}
1437+
RB_FOREACH(rki, revoked_key_id_tree, &rc->revoked_key_ids) {
1438+
/*
1439+
* We don't want key IDs with embedded newlines to
1440+
* mess up the display.
1441+
*/
1442+
r = INT_MAX;
1443+
asmprintf(&fp, INT_MAX, &r, "%s", rki->key_id);
1444+
fprintf(f, "id: %s\n", fp);
1445+
free(fp);
1446+
}
1447+
}
1448+
return ret;
1449+
}

krl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1515
*/
1616

17-
/* $OpenBSD: krl.h,v 1.7 2019/06/21 04:21:04 djm Exp $ */
17+
/* $OpenBSD: krl.h,v 1.8 2020/04/03 02:26:56 djm Exp $ */
1818

1919
#ifndef _KRL_H
2020
#define _KRL_H
@@ -61,6 +61,7 @@ int ssh_krl_from_blob(struct sshbuf *buf, struct ssh_krl **krlp,
6161
const struct sshkey **sign_ca_keys, size_t nsign_ca_keys);
6262
int ssh_krl_check_key(struct ssh_krl *krl, const struct sshkey *key);
6363
int ssh_krl_file_contains_key(const char *path, const struct sshkey *key);
64+
int krl_dump(struct ssh_krl *krl, FILE *f);
6465

6566
#endif /* _KRL_H */
6667

ssh-keygen.1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.\" $OpenBSD: ssh-keygen.1,v 1.202 2020/02/24 04:27:58 dtucker Exp $
1+
.\" $OpenBSD: ssh-keygen.1,v 1.203 2020/04/03 02:26:56 djm Exp $
22
.\"
33
.\" Author: Tatu Ylonen <[email protected]>
44
.\" Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -35,7 +35,7 @@
3535
.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3636
.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3737
.\"
38-
.Dd $Mdocdate: February 24 2020 $
38+
.Dd $Mdocdate: April 3 2020 $
3939
.Dt SSH-KEYGEN 1
4040
.Os
4141
.Sh NAME
@@ -135,6 +135,7 @@
135135
.Ar
136136
.Nm ssh-keygen
137137
.Fl Q
138+
.Op Fl l
138139
.Fl f Ar krl_file
139140
.Ar
140141
.Nm ssh-keygen
@@ -521,6 +522,9 @@ containing the private key, for the old passphrase, and twice for the
521522
new passphrase.
522523
.It Fl Q
523524
Test whether keys have been revoked in a KRL.
525+
If the
526+
.Fl l
527+
option is also specified then the contents of the KRL will be printed.
524528
.It Fl q
525529
Silence
526530
.Nm ssh-keygen .

ssh-keygen.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $OpenBSD: ssh-keygen.c,v 1.404 2020/03/13 03:17:07 djm Exp $ */
1+
/* $OpenBSD: ssh-keygen.c,v 1.405 2020/04/03 02:26:56 djm Exp $ */
22
/*
33
* Author: Tatu Ylonen <[email protected]>
44
* Copyright (c) 1994 Tatu Ylonen <[email protected]>, Espoo, Finland
@@ -2439,7 +2439,7 @@ do_gen_krl(struct passwd *pw, int updating, const char *ca_key_path,
24392439
}
24402440

24412441
static void
2442-
do_check_krl(struct passwd *pw, int argc, char **argv)
2442+
do_check_krl(struct passwd *pw, int print_krl, int argc, char **argv)
24432443
{
24442444
int i, r, ret = 0;
24452445
char *comment;
@@ -2449,6 +2449,8 @@ do_check_krl(struct passwd *pw, int argc, char **argv)
24492449
if (*identity_file == '\0')
24502450
fatal("KRL checking requires an input file");
24512451
load_krl(identity_file, &krl);
2452+
if (print_krl)
2453+
krl_dump(krl, stdout);
24522454
for (i = 0; i < argc; i++) {
24532455
if ((r = sshkey_load_public(argv[i], &k, &comment)) != 0)
24542456
fatal("Cannot load public key %s: %s",
@@ -3086,7 +3088,7 @@ usage(void)
30863088
" ssh-keygen -A [-f prefix_path]\n"
30873089
" ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n"
30883090
" file ...\n"
3089-
" ssh-keygen -Q -f krl_file file ...\n"
3091+
" ssh-keygen -Q [-l] -f krl_file [file ...]\n"
30903092
" ssh-keygen -Y find-principals -s signature_file -f allowed_signers_file\n"
30913093
" ssh-keygen -Y check-novalidate -n namespace -s signature_file\n"
30923094
" ssh-keygen -Y sign -f key_file -n namespace file ...\n"
@@ -3441,7 +3443,7 @@ main(int argc, char **argv)
34413443
return (0);
34423444
}
34433445
if (check_krl) {
3444-
do_check_krl(pw, argc, argv);
3446+
do_check_krl(pw, print_fingerprint, argc, argv);
34453447
return (0);
34463448
}
34473449
if (ca_key_path != NULL) {

0 commit comments

Comments
 (0)