1*82e9f75eSdtucker /* $OpenBSD: ssh-sk-helper.c,v 1.14 2022/12/04 11:03:11 dtucker Exp $ */
219e6a92cSdjm /*
319e6a92cSdjm * Copyright (c) 2019 Google LLC
419e6a92cSdjm *
519e6a92cSdjm * Permission to use, copy, modify, and distribute this software for any
619e6a92cSdjm * purpose with or without fee is hereby granted, provided that the above
719e6a92cSdjm * copyright notice and this permission notice appear in all copies.
819e6a92cSdjm *
919e6a92cSdjm * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1019e6a92cSdjm * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1119e6a92cSdjm * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1219e6a92cSdjm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1319e6a92cSdjm * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1419e6a92cSdjm * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1519e6a92cSdjm * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1619e6a92cSdjm */
1719e6a92cSdjm
1819e6a92cSdjm /*
1919e6a92cSdjm * This is a tiny program used to isolate the address space used for
2019e6a92cSdjm * security key middleware signing operations from ssh-agent. It is similar
216db35375Sdjm * to ssh-pkcs11-helper.c but considerably simpler as the operations for
226db35375Sdjm * security keys are stateless.
2319e6a92cSdjm *
246db35375Sdjm * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
256db35375Sdjm * protocol changes.
2619e6a92cSdjm */
2719e6a92cSdjm
286db35375Sdjm #include <limits.h>
2919e6a92cSdjm #include <stdarg.h>
3019e6a92cSdjm #include <stdio.h>
3119e6a92cSdjm #include <stdlib.h>
3219e6a92cSdjm #include <string.h>
3319e6a92cSdjm #include <unistd.h>
3419e6a92cSdjm #include <errno.h>
3519e6a92cSdjm
3619e6a92cSdjm #include "xmalloc.h"
3719e6a92cSdjm #include "log.h"
3819e6a92cSdjm #include "sshkey.h"
3919e6a92cSdjm #include "authfd.h"
4019e6a92cSdjm #include "misc.h"
4119e6a92cSdjm #include "sshbuf.h"
4219e6a92cSdjm #include "msg.h"
4319e6a92cSdjm #include "uidswap.h"
4419e6a92cSdjm #include "ssherr.h"
4519e6a92cSdjm #include "ssh-sk.h"
4619e6a92cSdjm
4719e6a92cSdjm extern char *__progname;
4819e6a92cSdjm
492db06755Sdjm static struct sshbuf *reply_error(int r, char *fmt, ...)
502db06755Sdjm __attribute__((__format__ (printf, 2, 3)));
512db06755Sdjm
522db06755Sdjm static struct sshbuf *
reply_error(int r,char * fmt,...)532db06755Sdjm reply_error(int r, char *fmt, ...)
542db06755Sdjm {
552db06755Sdjm char *msg;
562db06755Sdjm va_list ap;
572db06755Sdjm struct sshbuf *resp;
582db06755Sdjm
592db06755Sdjm va_start(ap, fmt);
602db06755Sdjm xvasprintf(&msg, fmt, ap);
612db06755Sdjm va_end(ap);
62b0297854Sdjm debug("%s: %s", __progname, msg);
632db06755Sdjm free(msg);
642db06755Sdjm
652db06755Sdjm if (r >= 0)
6648e6b99dSdjm fatal_f("invalid error code %d", r);
672db06755Sdjm
682db06755Sdjm if ((resp = sshbuf_new()) == NULL)
692db06755Sdjm fatal("%s: sshbuf_new failed", __progname);
702db06755Sdjm if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
712db06755Sdjm sshbuf_put_u32(resp, (u_int)-r) != 0)
722db06755Sdjm fatal("%s: buffer error", __progname);
732db06755Sdjm return resp;
742db06755Sdjm }
752db06755Sdjm
76a0caf565Sdjm /* If the specified string is zero length, then free it and replace with NULL */
77a0caf565Sdjm static void
null_empty(char ** s)78a0caf565Sdjm null_empty(char **s)
79a0caf565Sdjm {
80a0caf565Sdjm if (s == NULL || *s == NULL || **s != '\0')
81a0caf565Sdjm return;
82a0caf565Sdjm
83a0caf565Sdjm free(*s);
84a0caf565Sdjm *s = NULL;
85a0caf565Sdjm }
86a0caf565Sdjm
876db35375Sdjm static struct sshbuf *
process_sign(struct sshbuf * req)886db35375Sdjm process_sign(struct sshbuf *req)
896db35375Sdjm {
906db35375Sdjm int r = SSH_ERR_INTERNAL_ERROR;
916db35375Sdjm struct sshbuf *resp, *kbuf;
9243bf7622Sdjm struct sshkey *key = NULL;
936db35375Sdjm uint32_t compat;
946db35375Sdjm const u_char *message;
9543bf7622Sdjm u_char *sig = NULL;
9643bf7622Sdjm size_t msglen, siglen = 0;
9743bf7622Sdjm char *provider = NULL, *pin = NULL;
986db35375Sdjm
996db35375Sdjm if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
1006db35375Sdjm (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
1016db35375Sdjm (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
1026db35375Sdjm (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
1032db06755Sdjm (r = sshbuf_get_u32(req, &compat)) != 0 ||
1042db06755Sdjm (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
10548e6b99dSdjm fatal_r(r, "%s: parse", __progname);
1066db35375Sdjm if (sshbuf_len(req) != 0)
1076db35375Sdjm fatal("%s: trailing data in request", __progname);
1086db35375Sdjm
1096db35375Sdjm if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
11048e6b99dSdjm fatal_r(r, "%s: Unable to parse private key", __progname);
11148e6b99dSdjm if (!sshkey_is_sk(key)) {
11248e6b99dSdjm fatal("%s: Unsupported key type %s",
11348e6b99dSdjm __progname, sshkey_ssh_name(key));
11448e6b99dSdjm }
1156db35375Sdjm
11648e6b99dSdjm debug_f("ready to sign with key %s, provider %s: "
11748e6b99dSdjm "msg len %zu, compat 0x%lx", sshkey_type(key),
1186db35375Sdjm provider, msglen, (u_long)compat);
1196db35375Sdjm
120a0caf565Sdjm null_empty(&pin);
1212db06755Sdjm
1226db35375Sdjm if ((r = sshsk_sign(provider, key, &sig, &siglen,
1232db06755Sdjm message, msglen, compat, pin)) != 0) {
1242db06755Sdjm resp = reply_error(r, "Signing failed: %s", ssh_err(r));
1252db06755Sdjm goto out;
1262db06755Sdjm }
1276db35375Sdjm
1286db35375Sdjm if ((resp = sshbuf_new()) == NULL)
1296db35375Sdjm fatal("%s: sshbuf_new failed", __progname);
1306db35375Sdjm
1312db06755Sdjm if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
1322db06755Sdjm (r = sshbuf_put_string(resp, sig, siglen)) != 0)
13348e6b99dSdjm fatal_r(r, "%s: compose", __progname);
1342db06755Sdjm out:
13543bf7622Sdjm sshkey_free(key);
1366db35375Sdjm sshbuf_free(kbuf);
1376db35375Sdjm free(provider);
13843bf7622Sdjm if (sig != NULL)
13943bf7622Sdjm freezero(sig, siglen);
1402db06755Sdjm if (pin != NULL)
1412db06755Sdjm freezero(pin, strlen(pin));
1426db35375Sdjm return resp;
1436db35375Sdjm }
1446db35375Sdjm
1456db35375Sdjm static struct sshbuf *
process_enroll(struct sshbuf * req)1466db35375Sdjm process_enroll(struct sshbuf *req)
1476db35375Sdjm {
1486db35375Sdjm int r;
1496db35375Sdjm u_int type;
150a0caf565Sdjm char *provider, *application, *pin, *device, *userid;
1516db35375Sdjm uint8_t flags;
1526db35375Sdjm struct sshbuf *challenge, *attest, *kbuf, *resp;
1536db35375Sdjm struct sshkey *key;
1546db35375Sdjm
1552db06755Sdjm if ((attest = sshbuf_new()) == NULL ||
1566db35375Sdjm (kbuf = sshbuf_new()) == NULL)
1576db35375Sdjm fatal("%s: sshbuf_new failed", __progname);
1586db35375Sdjm
1596db35375Sdjm if ((r = sshbuf_get_u32(req, &type)) != 0 ||
1606db35375Sdjm (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
161a0caf565Sdjm (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
1626db35375Sdjm (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
163a0caf565Sdjm (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
1646db35375Sdjm (r = sshbuf_get_u8(req, &flags)) != 0 ||
1652db06755Sdjm (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
1666db35375Sdjm (r = sshbuf_froms(req, &challenge)) != 0)
16748e6b99dSdjm fatal_r(r, "%s: parse", __progname);
1686db35375Sdjm if (sshbuf_len(req) != 0)
1696db35375Sdjm fatal("%s: trailing data in request", __progname);
1706db35375Sdjm
1716db35375Sdjm if (type > INT_MAX)
1726db35375Sdjm fatal("%s: bad type %u", __progname, type);
1736db35375Sdjm if (sshbuf_len(challenge) == 0) {
1746db35375Sdjm sshbuf_free(challenge);
1756db35375Sdjm challenge = NULL;
1766db35375Sdjm }
177a0caf565Sdjm null_empty(&device);
178a0caf565Sdjm null_empty(&userid);
179a0caf565Sdjm null_empty(&pin);
1806db35375Sdjm
181a0caf565Sdjm if ((r = sshsk_enroll((int)type, provider, device, application, userid,
182a0caf565Sdjm flags, pin, challenge, &key, attest)) != 0) {
1832db06755Sdjm resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
1842db06755Sdjm goto out;
1852db06755Sdjm }
1866db35375Sdjm
1872db06755Sdjm if ((resp = sshbuf_new()) == NULL)
1882db06755Sdjm fatal("%s: sshbuf_new failed", __progname);
1896db35375Sdjm if ((r = sshkey_private_serialize(key, kbuf)) != 0)
19048e6b99dSdjm fatal_r(r, "%s: encode key", __progname);
1912db06755Sdjm if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
1922db06755Sdjm (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
1936db35375Sdjm (r = sshbuf_put_stringb(resp, attest)) != 0)
19448e6b99dSdjm fatal_r(r, "%s: compose", __progname);
1956db35375Sdjm
1962db06755Sdjm out:
1976db35375Sdjm sshkey_free(key);
1986db35375Sdjm sshbuf_free(kbuf);
1996db35375Sdjm sshbuf_free(attest);
2006db35375Sdjm sshbuf_free(challenge);
2016db35375Sdjm free(provider);
2026db35375Sdjm free(application);
2032db06755Sdjm if (pin != NULL)
2042db06755Sdjm freezero(pin, strlen(pin));
2056db35375Sdjm
2066db35375Sdjm return resp;
2076db35375Sdjm }
2086db35375Sdjm
2099fe3789cSdjm static struct sshbuf *
process_load_resident(struct sshbuf * req)2109fe3789cSdjm process_load_resident(struct sshbuf *req)
2119fe3789cSdjm {
2129fe3789cSdjm int r;
213a0caf565Sdjm char *provider, *pin, *device;
2149fe3789cSdjm struct sshbuf *kbuf, *resp;
215991d5a20Sdjm struct sshsk_resident_key **srks = NULL;
216991d5a20Sdjm size_t nsrks = 0, i;
217991d5a20Sdjm u_int flags;
2189fe3789cSdjm
2192db06755Sdjm if ((kbuf = sshbuf_new()) == NULL)
2209fe3789cSdjm fatal("%s: sshbuf_new failed", __progname);
2219fe3789cSdjm
2229fe3789cSdjm if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
223a0caf565Sdjm (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
224991d5a20Sdjm (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
225991d5a20Sdjm (r = sshbuf_get_u32(req, &flags)) != 0)
22648e6b99dSdjm fatal_r(r, "%s: parse", __progname);
2279fe3789cSdjm if (sshbuf_len(req) != 0)
2289fe3789cSdjm fatal("%s: trailing data in request", __progname);
2299fe3789cSdjm
230a0caf565Sdjm null_empty(&device);
231a0caf565Sdjm null_empty(&pin);
2322db06755Sdjm
233991d5a20Sdjm if ((r = sshsk_load_resident(provider, device, pin, flags,
234991d5a20Sdjm &srks, &nsrks)) != 0) {
2352db06755Sdjm resp = reply_error(r, "sshsk_load_resident failed: %s",
2362db06755Sdjm ssh_err(r));
2372db06755Sdjm goto out;
2382db06755Sdjm }
2392db06755Sdjm
2402db06755Sdjm if ((resp = sshbuf_new()) == NULL)
2412db06755Sdjm fatal("%s: sshbuf_new failed", __progname);
2422db06755Sdjm
2432db06755Sdjm if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
24448e6b99dSdjm fatal_r(r, "%s: compose", __progname);
2459fe3789cSdjm
246991d5a20Sdjm for (i = 0; i < nsrks; i++) {
247991d5a20Sdjm debug_f("key %zu %s %s uidlen %zu", i,
248991d5a20Sdjm sshkey_type(srks[i]->key), srks[i]->key->sk_application,
249991d5a20Sdjm srks[i]->user_id_len);
2509fe3789cSdjm sshbuf_reset(kbuf);
251991d5a20Sdjm if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
25248e6b99dSdjm fatal_r(r, "%s: encode key", __progname);
2539fe3789cSdjm if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
254991d5a20Sdjm (r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
255991d5a20Sdjm (r = sshbuf_put_string(resp, srks[i]->user_id,
256991d5a20Sdjm srks[i]->user_id_len)) != 0)
25748e6b99dSdjm fatal_r(r, "%s: compose key", __progname);
2589fe3789cSdjm }
2599fe3789cSdjm
2602db06755Sdjm out:
261991d5a20Sdjm sshsk_free_resident_keys(srks, nsrks);
2629fe3789cSdjm sshbuf_free(kbuf);
2639fe3789cSdjm free(provider);
2647a76b30eSdtucker free(device);
2652db06755Sdjm if (pin != NULL)
2669fe3789cSdjm freezero(pin, strlen(pin));
2679fe3789cSdjm return resp;
2689fe3789cSdjm }
2699fe3789cSdjm
27019e6a92cSdjm int
main(int argc,char ** argv)27119e6a92cSdjm main(int argc, char **argv)
27219e6a92cSdjm {
27319e6a92cSdjm SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
27419e6a92cSdjm LogLevel log_level = SYSLOG_LEVEL_ERROR;
2756db35375Sdjm struct sshbuf *req, *resp;
276a769387cSdjm int in, out, ch, r, vflag = 0;
277a769387cSdjm u_int rtype, ll = 0;
278a769387cSdjm uint8_t version, log_stderr = 0;
27919e6a92cSdjm
28019e6a92cSdjm sanitise_stdfd();
28119e6a92cSdjm log_init(__progname, log_level, log_facility, log_stderr);
28219e6a92cSdjm
28319e6a92cSdjm while ((ch = getopt(argc, argv, "v")) != -1) {
28419e6a92cSdjm switch (ch) {
28519e6a92cSdjm case 'v':
286a769387cSdjm vflag = 1;
28719e6a92cSdjm if (log_level == SYSLOG_LEVEL_ERROR)
28819e6a92cSdjm log_level = SYSLOG_LEVEL_DEBUG1;
28919e6a92cSdjm else if (log_level < SYSLOG_LEVEL_DEBUG3)
29019e6a92cSdjm log_level++;
29119e6a92cSdjm break;
29219e6a92cSdjm default:
29319e6a92cSdjm fprintf(stderr, "usage: %s [-v]\n", __progname);
29419e6a92cSdjm exit(1);
29519e6a92cSdjm }
29619e6a92cSdjm }
297a769387cSdjm log_init(__progname, log_level, log_facility, vflag);
29819e6a92cSdjm
29919e6a92cSdjm /*
30019e6a92cSdjm * Rearrange our file descriptors a little; we don't trust the
30119e6a92cSdjm * providers not to fiddle with stdin/out.
30219e6a92cSdjm */
30319e6a92cSdjm closefrom(STDERR_FILENO + 1);
30419e6a92cSdjm if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
30519e6a92cSdjm fatal("%s: dup: %s", __progname, strerror(errno));
30619e6a92cSdjm close(STDIN_FILENO);
30719e6a92cSdjm close(STDOUT_FILENO);
30819e6a92cSdjm sanitise_stdfd(); /* resets to /dev/null */
30919e6a92cSdjm
3106db35375Sdjm if ((req = sshbuf_new()) == NULL)
31119e6a92cSdjm fatal("%s: sshbuf_new failed", __progname);
31219e6a92cSdjm if (ssh_msg_recv(in, req) < 0)
31319e6a92cSdjm fatal("ssh_msg_recv failed");
31419e6a92cSdjm close(in);
31548e6b99dSdjm debug_f("received message len %zu", sshbuf_len(req));
31619e6a92cSdjm
31719e6a92cSdjm if ((r = sshbuf_get_u8(req, &version)) != 0)
31848e6b99dSdjm fatal_r(r, "%s: parse version", __progname);
31919e6a92cSdjm if (version != SSH_SK_HELPER_VERSION) {
32019e6a92cSdjm fatal("unsupported version: received %d, expected %d",
32119e6a92cSdjm version, SSH_SK_HELPER_VERSION);
32219e6a92cSdjm }
32319e6a92cSdjm
324a769387cSdjm if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
325a769387cSdjm (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
326a769387cSdjm (r = sshbuf_get_u32(req, &ll)) != 0)
32748e6b99dSdjm fatal_r(r, "%s: parse", __progname);
32819e6a92cSdjm
329a769387cSdjm if (!vflag && log_level_name((LogLevel)ll) != NULL)
330a769387cSdjm log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
331a769387cSdjm
3326db35375Sdjm switch (rtype) {
3336db35375Sdjm case SSH_SK_HELPER_SIGN:
3346db35375Sdjm resp = process_sign(req);
3356db35375Sdjm break;
3366db35375Sdjm case SSH_SK_HELPER_ENROLL:
3376db35375Sdjm resp = process_enroll(req);
3386db35375Sdjm break;
3399fe3789cSdjm case SSH_SK_HELPER_LOAD_RESIDENT:
3409fe3789cSdjm resp = process_load_resident(req);
3419fe3789cSdjm break;
3426db35375Sdjm default:
3436db35375Sdjm fatal("%s: unsupported request type %u", __progname, rtype);
3446db35375Sdjm }
3456db35375Sdjm sshbuf_free(req);
34648e6b99dSdjm debug_f("reply len %zu", sshbuf_len(resp));
3476db35375Sdjm
34819e6a92cSdjm if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
34919e6a92cSdjm fatal("ssh_msg_send failed");
3506db35375Sdjm sshbuf_free(resp);
35119e6a92cSdjm close(out);
35219e6a92cSdjm
35319e6a92cSdjm return (0);
35419e6a92cSdjm }
355