2021-10-28 02:54:18 +00:00
|
|
|
/* $OpenBSD: ssh-sk-client.c,v 1.10 2021/10/28 02:54:18 djm Exp $ */
|
2019-12-13 20:16:56 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-12-13 22:15:06 +00:00
|
|
|
#include "includes.h"
|
|
|
|
|
2019-12-13 20:16:56 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2020-01-21 07:07:31 +00:00
|
|
|
#include <fcntl.h>
|
2019-12-30 09:23:28 +00:00
|
|
|
#include <limits.h>
|
2019-12-13 20:16:56 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
|
|
|
#include "ssherr.h"
|
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
|
|
|
#include "msg.h"
|
|
|
|
#include "digest.h"
|
|
|
|
#include "pathnames.h"
|
|
|
|
#include "ssh-sk.h"
|
2020-01-23 07:10:22 +00:00
|
|
|
#include "misc.h"
|
2019-12-13 20:16:56 +00:00
|
|
|
|
|
|
|
/* #define DEBUG_SK 1 */
|
|
|
|
|
|
|
|
static int
|
|
|
|
start_helper(int *fdp, pid_t *pidp, void (**osigchldp)(int))
|
|
|
|
{
|
|
|
|
void (*osigchld)(int);
|
2020-10-18 11:32:01 +00:00
|
|
|
int oerrno, pair[2];
|
2019-12-13 20:16:56 +00:00
|
|
|
pid_t pid;
|
|
|
|
char *helper, *verbosity = NULL;
|
|
|
|
|
|
|
|
*fdp = -1;
|
|
|
|
*pidp = 0;
|
|
|
|
*osigchldp = SIG_DFL;
|
|
|
|
|
|
|
|
helper = getenv("SSH_SK_HELPER");
|
|
|
|
if (helper == NULL || strlen(helper) == 0)
|
|
|
|
helper = _PATH_SSH_SK_HELPER;
|
2020-01-21 07:07:31 +00:00
|
|
|
if (access(helper, X_OK) != 0) {
|
|
|
|
oerrno = errno;
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("helper \"%s\" unusable: %s", helper, strerror(errno));
|
2020-01-21 07:07:31 +00:00
|
|
|
errno = oerrno;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
}
|
2019-12-13 20:16:56 +00:00
|
|
|
#ifdef DEBUG_SK
|
|
|
|
verbosity = "-vvv";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Start helper */
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
|
|
|
|
error("socketpair: %s", strerror(errno));
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
}
|
2020-01-23 07:10:22 +00:00
|
|
|
osigchld = ssh_signal(SIGCHLD, SIG_DFL);
|
2019-12-13 20:16:56 +00:00
|
|
|
if ((pid = fork()) == -1) {
|
|
|
|
oerrno = errno;
|
|
|
|
error("fork: %s", strerror(errno));
|
|
|
|
close(pair[0]);
|
|
|
|
close(pair[1]);
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2019-12-13 20:16:56 +00:00
|
|
|
errno = oerrno;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
if ((dup2(pair[1], STDIN_FILENO) == -1) ||
|
|
|
|
(dup2(pair[1], STDOUT_FILENO) == -1)) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("dup2: %s", strerror(errno));
|
2019-12-13 20:16:56 +00:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
close(pair[0]);
|
|
|
|
close(pair[1]);
|
|
|
|
closefrom(STDERR_FILENO + 1);
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("starting %s %s", helper,
|
2019-12-13 20:16:56 +00:00
|
|
|
verbosity == NULL ? "" : verbosity);
|
|
|
|
execlp(helper, helper, verbosity, (char *)NULL);
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("execlp: %s", strerror(errno));
|
2019-12-13 20:16:56 +00:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
close(pair[1]);
|
|
|
|
|
|
|
|
/* success */
|
2020-10-18 11:32:01 +00:00
|
|
|
debug3_f("started pid=%ld", (long)pid);
|
2019-12-13 20:16:56 +00:00
|
|
|
*fdp = pair[0];
|
|
|
|
*pidp = pid;
|
|
|
|
*osigchldp = osigchld;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
reap_helper(pid_t pid)
|
|
|
|
{
|
|
|
|
int status, oerrno;
|
|
|
|
|
2020-10-18 11:32:01 +00:00
|
|
|
debug3_f("pid=%ld", (long)pid);
|
2019-12-13 20:16:56 +00:00
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
while (waitpid(pid, &status, 0) == -1) {
|
|
|
|
if (errno == EINTR) {
|
|
|
|
errno = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
oerrno = errno;
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("waitpid: %s", strerror(errno));
|
2019-12-13 20:16:56 +00:00
|
|
|
errno = oerrno;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
|
|
|
}
|
|
|
|
if (!WIFEXITED(status)) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("helper exited abnormally");
|
2019-12-13 20:16:56 +00:00
|
|
|
return SSH_ERR_AGENT_FAILURE;
|
|
|
|
} else if (WEXITSTATUS(status) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("helper exited with non-zero exit status");
|
2019-12-13 20:16:56 +00:00
|
|
|
return SSH_ERR_AGENT_FAILURE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2020-01-10 23:43:26 +00:00
|
|
|
client_converse(struct sshbuf *msg, struct sshbuf **respp, u_int type)
|
2019-12-13 20:16:56 +00:00
|
|
|
{
|
2020-01-10 23:43:26 +00:00
|
|
|
int oerrno, fd, r2, ll, r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
u_int rtype, rerr;
|
2019-12-13 20:16:56 +00:00
|
|
|
pid_t pid;
|
|
|
|
u_char version;
|
|
|
|
void (*osigchld)(int);
|
2020-01-10 23:43:26 +00:00
|
|
|
struct sshbuf *req = NULL, *resp = NULL;
|
2019-12-13 20:16:56 +00:00
|
|
|
*respp = NULL;
|
|
|
|
|
|
|
|
if ((r = start_helper(&fd, &pid, &osigchld)) != 0)
|
|
|
|
return r;
|
|
|
|
|
2020-01-10 23:43:26 +00:00
|
|
|
if ((req = sshbuf_new()) == NULL || (resp = sshbuf_new()) == NULL) {
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-01-10 23:43:26 +00:00
|
|
|
/* Request preamble: type, log_on_stderr, log_level */
|
|
|
|
ll = log_level_get();
|
|
|
|
if ((r = sshbuf_put_u32(req, type)) != 0 ||
|
2021-04-03 06:18:40 +00:00
|
|
|
(r = sshbuf_put_u8(req, log_is_on_stderr() != 0)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(req, ll < 0 ? 0 : ll)) != 0 ||
|
|
|
|
(r = sshbuf_putb(req, msg)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "compose");
|
2020-01-10 23:43:26 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2019-12-13 20:16:56 +00:00
|
|
|
if ((r = ssh_msg_send(fd, SSH_SK_HELPER_VERSION, req)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "send");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = ssh_msg_recv(fd, resp)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "receive");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_u8(resp, &version)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse version");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (version != SSH_SK_HELPER_VERSION) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("unsupported version: got %u, expected %u",
|
|
|
|
version, SSH_SK_HELPER_VERSION);
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2020-01-10 23:43:26 +00:00
|
|
|
if ((r = sshbuf_get_u32(resp, &rtype)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse message type");
|
2019-12-30 09:23:28 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2020-01-10 23:43:26 +00:00
|
|
|
if (rtype == SSH_SK_HELPER_ERROR) {
|
2019-12-30 09:23:28 +00:00
|
|
|
if ((r = sshbuf_get_u32(resp, &rerr)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse");
|
2019-12-30 09:23:28 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2020-10-18 11:32:01 +00:00
|
|
|
debug_f("helper returned error -%u", rerr);
|
2019-12-30 09:23:28 +00:00
|
|
|
/* OpenSSH error values are negative; encoded as -err on wire */
|
|
|
|
if (rerr == 0 || rerr >= INT_MAX)
|
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
else
|
|
|
|
r = -(int)rerr;
|
|
|
|
goto out;
|
2020-01-10 23:43:26 +00:00
|
|
|
} else if (rtype != type) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("helper returned incorrect message type %u, "
|
|
|
|
"expecting %u", rtype, type);
|
2019-12-30 09:23:28 +00:00
|
|
|
r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-12-13 20:16:56 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
oerrno = errno;
|
|
|
|
close(fd);
|
|
|
|
if ((r2 = reap_helper(pid)) != 0) {
|
|
|
|
if (r == 0) {
|
|
|
|
r = r2;
|
|
|
|
oerrno = errno;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r == 0) {
|
|
|
|
*respp = resp;
|
|
|
|
resp = NULL;
|
|
|
|
}
|
2020-01-10 23:43:26 +00:00
|
|
|
sshbuf_free(req);
|
2019-12-13 20:16:56 +00:00
|
|
|
sshbuf_free(resp);
|
2020-01-23 07:10:22 +00:00
|
|
|
ssh_signal(SIGCHLD, osigchld);
|
2019-12-13 20:16:56 +00:00
|
|
|
errno = oerrno;
|
|
|
|
return r;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sshsk_sign(const char *provider, struct sshkey *key,
|
|
|
|
u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
|
2019-12-30 09:23:28 +00:00
|
|
|
u_int compat, const char *pin)
|
2019-12-13 20:16:56 +00:00
|
|
|
{
|
|
|
|
int oerrno, r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
char *fp = NULL;
|
|
|
|
struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
|
|
|
|
|
|
|
|
*sigp = NULL;
|
|
|
|
*lenp = 0;
|
|
|
|
|
2019-12-13 22:21:46 +00:00
|
|
|
#ifndef ENABLE_SK
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
#endif
|
|
|
|
|
2019-12-13 20:16:56 +00:00
|
|
|
if ((kbuf = sshbuf_new()) == NULL ||
|
|
|
|
(req = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((r = sshkey_private_serialize(key, kbuf)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "encode key");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2020-01-10 23:43:26 +00:00
|
|
|
if ((r = sshbuf_put_stringb(req, kbuf)) != 0 ||
|
2019-12-13 20:16:56 +00:00
|
|
|
(r = sshbuf_put_cstring(req, provider)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(req, data, datalen)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(req, NULL)) != 0 || /* alg */
|
2019-12-30 09:23:28 +00:00
|
|
|
(r = sshbuf_put_u32(req, compat)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(req, pin)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "compose");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
|
|
|
|
SSH_FP_DEFAULT)) == NULL) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("sshkey_fingerprint failed");
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
2019-12-30 09:23:28 +00:00
|
|
|
if ((r = client_converse(req, &resp, SSH_SK_HELPER_SIGN)) != 0)
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse signature");
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(resp) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("trailing data in response");
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
oerrno = errno;
|
|
|
|
if (r != 0) {
|
|
|
|
freezero(*sigp, *lenp);
|
|
|
|
*sigp = NULL;
|
|
|
|
*lenp = 0;
|
|
|
|
}
|
|
|
|
sshbuf_free(kbuf);
|
|
|
|
sshbuf_free(req);
|
|
|
|
sshbuf_free(resp);
|
|
|
|
errno = oerrno;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2020-01-06 02:00:46 +00:00
|
|
|
sshsk_enroll(int type, const char *provider_path, const char *device,
|
|
|
|
const char *application, const char *userid, uint8_t flags,
|
|
|
|
const char *pin, struct sshbuf *challenge_buf,
|
2019-12-30 09:23:28 +00:00
|
|
|
struct sshkey **keyp, struct sshbuf *attest)
|
2019-12-13 20:16:56 +00:00
|
|
|
{
|
|
|
|
int oerrno, r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *kbuf = NULL, *abuf = NULL, *req = NULL, *resp = NULL;
|
|
|
|
struct sshkey *key = NULL;
|
|
|
|
|
|
|
|
*keyp = NULL;
|
|
|
|
if (attest != NULL)
|
|
|
|
sshbuf_reset(attest);
|
|
|
|
|
2019-12-13 22:21:46 +00:00
|
|
|
#ifndef ENABLE_SK
|
|
|
|
return SSH_ERR_KEY_TYPE_UNKNOWN;
|
|
|
|
#endif
|
|
|
|
|
2019-12-13 20:16:56 +00:00
|
|
|
if (type < 0)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
|
|
|
|
if ((abuf = sshbuf_new()) == NULL ||
|
|
|
|
(kbuf = sshbuf_new()) == NULL ||
|
|
|
|
(req = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-01-10 23:43:26 +00:00
|
|
|
if ((r = sshbuf_put_u32(req, (u_int)type)) != 0 ||
|
2019-12-13 20:16:56 +00:00
|
|
|
(r = sshbuf_put_cstring(req, provider_path)) != 0 ||
|
2020-01-06 02:00:46 +00:00
|
|
|
(r = sshbuf_put_cstring(req, device)) != 0 ||
|
2019-12-13 20:16:56 +00:00
|
|
|
(r = sshbuf_put_cstring(req, application)) != 0 ||
|
2020-01-06 02:00:46 +00:00
|
|
|
(r = sshbuf_put_cstring(req, userid)) != 0 ||
|
2019-12-13 20:16:56 +00:00
|
|
|
(r = sshbuf_put_u8(req, flags)) != 0 ||
|
2019-12-30 09:23:28 +00:00
|
|
|
(r = sshbuf_put_cstring(req, pin)) != 0 ||
|
2019-12-13 20:16:56 +00:00
|
|
|
(r = sshbuf_put_stringb(req, challenge_buf)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "compose");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-12-30 09:23:28 +00:00
|
|
|
if ((r = client_converse(req, &resp, SSH_SK_HELPER_ENROLL)) != 0)
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
|
|
|
|
(r = sshbuf_get_stringb(resp, abuf)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "parse");
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (sshbuf_len(resp) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_f("trailing data in response");
|
2019-12-13 20:16:56 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "encode");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (attest != NULL && (r = sshbuf_putb(attest, abuf)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "encode attestation information");
|
2019-12-13 20:16:56 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL;
|
|
|
|
out:
|
|
|
|
oerrno = errno;
|
|
|
|
sshkey_free(key);
|
|
|
|
sshbuf_free(kbuf);
|
|
|
|
sshbuf_free(abuf);
|
|
|
|
sshbuf_free(req);
|
|
|
|
sshbuf_free(resp);
|
|
|
|
errno = oerrno;
|
|
|
|
return r;
|
|
|
|
}
|
2019-12-30 09:21:59 +00:00
|
|
|
|
2021-10-28 02:54:18 +00:00
|
|
|
static void
|
|
|
|
sshsk_free_resident_key(struct sshsk_resident_key *srk)
|
|
|
|
{
|
|
|
|
if (srk == NULL)
|
|
|
|
return;
|
|
|
|
sshkey_free(srk->key);
|
|
|
|
freezero(srk->user_id, srk->user_id_len);
|
|
|
|
free(srk);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
sshsk_free_resident_keys(struct sshsk_resident_key **srks, size_t nsrks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (srks == NULL || nsrks == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < nsrks; i++)
|
|
|
|
sshsk_free_resident_key(srks[i]);
|
|
|
|
free(srks);
|
|
|
|
}
|
|
|
|
|
2019-12-30 09:21:59 +00:00
|
|
|
int
|
2020-01-06 02:00:46 +00:00
|
|
|
sshsk_load_resident(const char *provider_path, const char *device,
|
2021-10-28 02:54:18 +00:00
|
|
|
const char *pin, u_int flags, struct sshsk_resident_key ***srksp,
|
|
|
|
size_t *nsrksp)
|
2019-12-30 09:21:59 +00:00
|
|
|
{
|
|
|
|
int oerrno, r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
|
2021-10-28 02:54:18 +00:00
|
|
|
struct sshkey *key = NULL;
|
|
|
|
struct sshsk_resident_key *srk = NULL, **srks = NULL, **tmp;
|
|
|
|
u_char *userid = NULL;
|
|
|
|
size_t userid_len = 0, nsrks = 0;
|
2019-12-30 09:21:59 +00:00
|
|
|
|
2021-10-28 02:54:18 +00:00
|
|
|
*srksp = NULL;
|
|
|
|
*nsrksp = 0;
|
2019-12-30 09:21:59 +00:00
|
|
|
|
|
|
|
if ((resp = sshbuf_new()) == NULL ||
|
|
|
|
(kbuf = sshbuf_new()) == NULL ||
|
|
|
|
(req = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-01-10 23:43:26 +00:00
|
|
|
if ((r = sshbuf_put_cstring(req, provider_path)) != 0 ||
|
2020-01-06 02:00:46 +00:00
|
|
|
(r = sshbuf_put_cstring(req, device)) != 0 ||
|
2021-10-28 02:54:18 +00:00
|
|
|
(r = sshbuf_put_cstring(req, pin)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(req, flags)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "compose");
|
2019-12-30 09:21:59 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2019-12-30 09:23:28 +00:00
|
|
|
if ((r = client_converse(req, &resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
|
2019-12-30 09:21:59 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
while (sshbuf_len(resp) != 0) {
|
2021-10-28 02:54:18 +00:00
|
|
|
/* key, comment, user_id */
|
2019-12-30 09:21:59 +00:00
|
|
|
if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
|
2021-10-28 02:54:18 +00:00
|
|
|
(r = sshbuf_get_cstring(resp, NULL, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(resp, &userid, &userid_len)) != 0) {
|
|
|
|
error_fr(r, "parse");
|
2019-12-30 09:21:59 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
|
2020-10-18 11:32:01 +00:00
|
|
|
error_fr(r, "decode key");
|
2019-12-30 09:21:59 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2021-10-28 02:54:18 +00:00
|
|
|
if ((srk = calloc(1, sizeof(*srk))) == NULL) {
|
|
|
|
error_f("calloc failed");
|
2019-12-30 09:21:59 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2021-10-28 02:54:18 +00:00
|
|
|
srk->key = key;
|
2019-12-30 09:21:59 +00:00
|
|
|
key = NULL;
|
2021-10-28 02:54:18 +00:00
|
|
|
srk->user_id = userid;
|
|
|
|
srk->user_id_len = userid_len;
|
|
|
|
userid = NULL;
|
|
|
|
userid_len = 0;
|
|
|
|
if ((tmp = recallocarray(srks, nsrks, nsrks + 1,
|
|
|
|
sizeof(*srks))) == NULL) {
|
|
|
|
error_f("recallocarray keys failed");
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
debug_f("srks[%zu]: %s %s uidlen %zu", nsrks,
|
|
|
|
sshkey_type(srk->key), srk->key->sk_application,
|
|
|
|
srk->user_id_len);
|
|
|
|
srks = tmp;
|
|
|
|
srks[nsrks++] = srk;
|
|
|
|
srk = NULL;
|
2019-12-30 09:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
2021-10-28 02:54:18 +00:00
|
|
|
*srksp = srks;
|
|
|
|
*nsrksp = nsrks;
|
|
|
|
srks = NULL;
|
|
|
|
nsrks = 0;
|
2019-12-30 09:21:59 +00:00
|
|
|
out:
|
|
|
|
oerrno = errno;
|
2021-10-28 02:54:18 +00:00
|
|
|
sshsk_free_resident_key(srk);
|
|
|
|
sshsk_free_resident_keys(srks, nsrks);
|
|
|
|
freezero(userid, userid_len);
|
2019-12-30 09:21:59 +00:00
|
|
|
sshkey_free(key);
|
|
|
|
sshbuf_free(kbuf);
|
|
|
|
sshbuf_free(req);
|
|
|
|
sshbuf_free(resp);
|
|
|
|
errno = oerrno;
|
|
|
|
return r;
|
|
|
|
}
|