Skip to content

Commit

Permalink
expose PKCS#11 key labels/X.509 subjects as comments
Browse files Browse the repository at this point in the history
Extract the key label or X.509 subject string when PKCS#11 keys
are retrieved from the token and plumb this through to places where
it may be used as a comment.

based on openssh/openssh-portable#138
by Danielle Church
  • Loading branch information
djmdjm committed Jan 24, 2020
1 parent b833228 commit b7aead0
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 73 deletions.
17 changes: 12 additions & 5 deletions ssh-agent.c
Expand Up @@ -619,6 +619,7 @@ static void
process_add_smartcard_key(SocketEntry *e)
{
char *provider = NULL, *pin = NULL, canonical_provider[PATH_MAX];
char **comments = NULL;
int r, i, count = 0, success = 0, confirm = 0;
u_int seconds;
time_t death = 0;
Expand Down Expand Up @@ -668,28 +669,34 @@ process_add_smartcard_key(SocketEntry *e)
if (lifetime && !death)
death = monotime() + lifetime;

count = pkcs11_add_provider(canonical_provider, pin, &keys);
count = pkcs11_add_provider(canonical_provider, pin, &keys, &comments);
for (i = 0; i < count; i++) {
k = keys[i];
if (lookup_identity(k) == NULL) {
id = xcalloc(1, sizeof(Identity));
id->key = k;
keys[i] = NULL; /* transferred */
id->provider = xstrdup(canonical_provider);
id->comment = xstrdup(canonical_provider); /* XXX */
if (*comments[i] != '\0') {
id->comment = comments[i];
comments[i] = NULL; /* transferred */
} else {
id->comment = xstrdup(canonical_provider);
}
id->death = death;
id->confirm = confirm;
TAILQ_INSERT_TAIL(&idtab->idlist, id, next);
idtab->nentries++;
success = 1;
} else {
sshkey_free(k);
}
keys[i] = NULL;
sshkey_free(keys[i]);
free(comments[i]);
}
send:
free(pin);
free(provider);
free(keys);
free(comments);
send_status(e, success);
}

Expand Down
12 changes: 8 additions & 4 deletions ssh-keygen.c
Expand Up @@ -811,13 +811,13 @@ do_download(struct passwd *pw)
int i, nkeys;
enum sshkey_fp_rep rep;
int fptype;
char *fp, *ra;
char *fp, *ra, **comments = NULL;

fptype = print_bubblebabble ? SSH_DIGEST_SHA1 : fingerprint_hash;
rep = print_bubblebabble ? SSH_FP_BUBBLEBABBLE : SSH_FP_DEFAULT;

pkcs11_init(1);
nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys);
nkeys = pkcs11_add_provider(pkcs11provider, NULL, &keys, &comments);
if (nkeys <= 0)
fatal("cannot read public key from pkcs11");
for (i = 0; i < nkeys; i++) {
Expand All @@ -835,10 +835,13 @@ do_download(struct passwd *pw)
free(fp);
} else {
(void) sshkey_write(keys[i], stdout); /* XXX check */
fprintf(stdout, "\n");
fprintf(stdout, "%s%s\n",
*(comments[i]) == '\0' ? "" : " ", comments[i]);
}
free(comments[i]);
sshkey_free(keys[i]);
}
free(comments);
free(keys);
pkcs11_terminate();
exit(0);
Expand Down Expand Up @@ -1683,7 +1686,8 @@ load_pkcs11_key(char *path)
fatal("Couldn't load CA public key \"%s\": %s",
path, ssh_err(r));

nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase, &keys);
nkeys = pkcs11_add_provider(pkcs11provider, identity_passphrase,
&keys, NULL);
debug3("%s: %d keys", __func__, nkeys);
if (nkeys <= 0)
fatal("cannot read public key from pkcs11");
Expand Down
12 changes: 10 additions & 2 deletions ssh-pkcs11-client.c
Expand Up @@ -296,11 +296,13 @@ pkcs11_start_helper(void)
}

int
pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp,
char ***labelsp)
{
struct sshkey *k;
int r, type;
u_char *blob;
char *label;
size_t blen;
u_int nkeys, i;
struct sshbuf *msg;
Expand All @@ -322,16 +324,22 @@ pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
*keysp = xcalloc(nkeys, sizeof(struct sshkey *));
if (labelsp)
*labelsp = xcalloc(nkeys, sizeof(char *));
for (i = 0; i < nkeys; i++) {
/* XXX clean up properly instead of fatal() */
if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
(r = sshbuf_skip_string(msg)) != 0)
(r = sshbuf_get_cstring(msg, &label, NULL)) != 0)
fatal("%s: buffer error: %s",
__func__, ssh_err(r));
if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
fatal("%s: bad key: %s", __func__, ssh_err(r));
wrap_key(k);
(*keysp)[i] = k;
if (labelsp)
(*labelsp)[i] = label;
else
free(label);
free(blob);
}
} else if (type == SSH2_AGENT_FAILURE) {
Expand Down
19 changes: 12 additions & 7 deletions ssh-pkcs11-helper.c
Expand Up @@ -41,7 +41,7 @@

struct pkcs11_keyinfo {
struct sshkey *key;
char *providername;
char *providername, *label;
TAILQ_ENTRY(pkcs11_keyinfo) next;
};

Expand All @@ -54,13 +54,14 @@ struct sshbuf *iqueue;
struct sshbuf *oqueue;

static void
add_key(struct sshkey *k, char *name)
add_key(struct sshkey *k, char *name, char *label)
{
struct pkcs11_keyinfo *ki;

ki = xcalloc(1, sizeof(*ki));
ki->providername = xstrdup(name);
ki->key = k;
ki->label = xstrdup(label);
TAILQ_INSERT_TAIL(&pkcs11_keylist, ki, next);
}

Expand All @@ -74,6 +75,7 @@ del_keys_by_name(char *name)
if (!strcmp(ki->providername, name)) {
TAILQ_REMOVE(&pkcs11_keylist, ki, next);
free(ki->providername);
free(ki->label);
sshkey_free(ki->key);
free(ki);
}
Expand All @@ -87,7 +89,7 @@ lookup_key(struct sshkey *k)
struct pkcs11_keyinfo *ki;

TAILQ_FOREACH(ki, &pkcs11_keylist, next) {
debug("check %p %s", ki, ki->providername);
debug("check %p %s %s", ki, ki->providername, ki->label);
if (sshkey_equal(k, ki->key))
return (ki->key);
}
Expand All @@ -112,13 +114,14 @@ process_add(void)
u_char *blob;
size_t blen;
struct sshbuf *msg;
char **labels = NULL;

if ((msg = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", __func__);
if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
(r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
if ((nkeys = pkcs11_add_provider(name, pin, &keys)) > 0) {
if ((nkeys = pkcs11_add_provider(name, pin, &keys, &labels)) > 0) {
if ((r = sshbuf_put_u8(msg,
SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
(r = sshbuf_put_u32(msg, nkeys)) != 0)
Expand All @@ -130,19 +133,21 @@ process_add(void)
continue;
}
if ((r = sshbuf_put_string(msg, blob, blen)) != 0 ||
(r = sshbuf_put_cstring(msg, name)) != 0)
(r = sshbuf_put_cstring(msg, labels[i])) != 0)
fatal("%s: buffer error: %s",
__func__, ssh_err(r));
free(blob);
add_key(keys[i], name);
add_key(keys[i], name, labels[i]);
free(labels[i]);
}
} else {
if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
if ((r = sshbuf_put_u32(msg, -nkeys)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
}
free(keys);
free(labels);
free(keys); /* keys themselves are transferred to pkcs11_keylist */
free(pin);
free(name);
send_msg(msg);
Expand Down

0 comments on commit b7aead0

Please sign in to comment.