=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/ssh-keygen.c,v retrieving revision 1.384 retrieving revision 1.387 diff -u -r1.384 -r1.387 --- src/usr.bin/ssh/ssh-keygen.c 2020/01/21 11:06:09 1.384 +++ src/usr.bin/ssh/ssh-keygen.c 2020/01/23 07:54:04 1.387 @@ -1,4 +1,4 @@ -/* $OpenBSD: ssh-keygen.c,v 1.384 2020/01/21 11:06:09 djm Exp $ */ +/* $OpenBSD: ssh-keygen.c,v 1.387 2020/01/23 07:54:04 djm Exp $ */ /* * Author: Tatu Ylonen * Copyright (c) 1994 Tatu Ylonen , Espoo, Finland @@ -1241,8 +1241,10 @@ if (fp == NULL || ra == NULL) fatal("%s: sshkey_fingerprint failed", __func__); - mprintf("%s %s %s %s\n", ctx->host, - sshkey_type(l->key), fp, l->comment); + mprintf("%s %s %s%s%s\n", ctx->host, + sshkey_type(l->key), fp, + l->comment[0] ? " " : "", + l->comment); if (log_level_get() >= SYSLOG_LEVEL_VERBOSE) printf("%s\n", ra); free(ra); @@ -2577,7 +2579,7 @@ } static int -sign(const char *keypath, const char *sig_namespace, int argc, char **argv) +sig_sign(const char *keypath, const char *sig_namespace, int argc, char **argv) { int i, fd = -1, r, ret = -1; int agent_fd = -1; @@ -2648,8 +2650,8 @@ } static int -verify(const char *signature, const char *sig_namespace, const char *principal, - const char *allowed_keys, const char *revoked_keys) +sig_verify(const char *signature, const char *sig_namespace, + const char *principal, const char *allowed_keys, const char *revoked_keys) { int r, ret = -1, sigfd = -1; struct sshbuf *sigbuf = NULL, *abuf = NULL; @@ -2672,7 +2674,7 @@ } if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) { error("%s: sshsig_armor: %s", __func__, ssh_err(r)); - return r; + goto done; } if ((r = sshsig_verify_fd(sigbuf, STDIN_FILENO, sig_namespace, &sign_key, &sig_details)) != 0) @@ -2735,6 +2737,57 @@ return ret; } +static int +sig_find_principal(const char *signature, const char *allowed_keys) { + int r, ret = -1, sigfd = -1; + struct sshbuf *sigbuf = NULL, *abuf = NULL; + struct sshkey *sign_key = NULL; + char *principal = NULL; + + if ((abuf = sshbuf_new()) == NULL) + fatal("%s: sshbuf_new() failed", __func__); + + if ((sigfd = open(signature, O_RDONLY)) < 0) { + error("Couldn't open signature file %s", signature); + goto done; + } + + if ((r = sshkey_load_file(sigfd, abuf)) != 0) { + error("Couldn't read signature file: %s", ssh_err(r)); + goto done; + } + if ((r = sshsig_dearmor(abuf, &sigbuf)) != 0) { + error("%s: sshsig_armor: %s", __func__, ssh_err(r)); + goto done; + } + if ((r = sshsig_get_pubkey(sigbuf, &sign_key)) != 0) { + error("%s: sshsig_get_pubkey: %s", + __func__, ssh_err(r)); + goto done; + } + + if ((r = sshsig_find_principal(allowed_keys, sign_key, + &principal)) != 0) { + error("%s: sshsig_get_principal: %s", + __func__, ssh_err(r)); + goto done; + } + ret = 0; +done: + if (ret == 0 ) { + printf("Found matching principal: %s\n", principal); + } else { + printf("Could not find matching principal.\n"); + } + if (sigfd != -1) + close(sigfd); + sshbuf_free(sigbuf); + sshbuf_free(abuf); + sshkey_free(sign_key); + free(principal); + return ret; +} + static void do_moduli_gen(const char *out_file, char **opts, size_t nopts) { @@ -3020,6 +3073,7 @@ " ssh-keygen -k -f krl_file [-u] [-s ca_public] [-z version_number]\n" " file ...\n" " ssh-keygen -Q -f krl_file file ...\n" + " ssh-keygen -Y find-principal -s signature_file -f allowed_signers_file\n" " ssh-keygen -Y check-novalidate -n namespace -s signature_file\n" " ssh-keygen -Y sign -f key_file -n namespace file ...\n" " ssh-keygen -Y verify -f allowed_signers_file -I signer_identity\n" @@ -3280,6 +3334,19 @@ argc -= optind; if (sign_op != NULL) { + if (strncmp(sign_op, "find-principal", 14) == 0) { + if (ca_key_path == NULL) { + error("Too few arguments for find-principal:" + "missing signature file"); + exit(1); + } + if (!have_identity) { + error("Too few arguments for find-principal:" + "missing allowed keys file"); + exit(1); + } + return sig_find_principal(ca_key_path, identity_file); + } if (cert_principals == NULL || *cert_principals == '\0') { error("Too few arguments for sign/verify: " "missing namespace"); @@ -3291,15 +3358,16 @@ "missing key"); exit(1); } - return sign(identity_file, cert_principals, argc, argv); + return sig_sign(identity_file, cert_principals, + argc, argv); } else if (strncmp(sign_op, "check-novalidate", 16) == 0) { if (ca_key_path == NULL) { error("Too few arguments for check-novalidate: " "missing signature file"); exit(1); } - return verify(ca_key_path, cert_principals, - NULL, NULL, NULL); + return sig_verify(ca_key_path, cert_principals, + NULL, NULL, NULL); } else if (strncmp(sign_op, "verify", 6) == 0) { if (ca_key_path == NULL) { error("Too few arguments for verify: " @@ -3316,7 +3384,7 @@ "missing principal ID"); exit(1); } - return verify(ca_key_path, cert_principals, + return sig_verify(ca_key_path, cert_principals, cert_key_id, identity_file, rr_hostname); } usage(); @@ -3551,7 +3619,7 @@ sshkey_free(private); if (!quiet) { - printf("Your identification has been saved in %s.\n", + printf("Your identification has been saved in %s\n", identity_file); } @@ -3568,7 +3636,7 @@ SSH_FP_RANDOMART); if (fp == NULL || ra == NULL) fatal("sshkey_fingerprint failed"); - printf("Your public key has been saved in %s.\n", + printf("Your public key has been saved in %s\n", identity_file); printf("The key fingerprint is:\n"); printf("%s %s\n", fp, comment);