1*f374ba41SEd Maste /* $OpenBSD: ssh-sk-helper.c,v 1.14 2022/12/04 11:03:11 dtucker Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Copyright (c) 2019 Google LLC
419261079SEd Maste *
519261079SEd Maste * Permission to use, copy, modify, and distribute this software for any
619261079SEd Maste * purpose with or without fee is hereby granted, provided that the above
719261079SEd Maste * copyright notice and this permission notice appear in all copies.
819261079SEd Maste *
919261079SEd Maste * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1019261079SEd Maste * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1119261079SEd Maste * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1219261079SEd Maste * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1319261079SEd Maste * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1419261079SEd Maste * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1519261079SEd Maste * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1619261079SEd Maste */
1719261079SEd Maste
1819261079SEd Maste /*
1919261079SEd Maste * This is a tiny program used to isolate the address space used for
2019261079SEd Maste * security key middleware signing operations from ssh-agent. It is similar
2119261079SEd Maste * to ssh-pkcs11-helper.c but considerably simpler as the operations for
2219261079SEd Maste * security keys are stateless.
2319261079SEd Maste *
2419261079SEd Maste * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
2519261079SEd Maste * protocol changes.
2619261079SEd Maste */
2719261079SEd Maste
2819261079SEd Maste #include "includes.h"
2919261079SEd Maste
3019261079SEd Maste #include <limits.h>
3119261079SEd Maste #include <stdarg.h>
3219261079SEd Maste #include <stdio.h>
3319261079SEd Maste #include <stdlib.h>
3419261079SEd Maste #include <string.h>
3519261079SEd Maste #include <unistd.h>
3619261079SEd Maste #include <errno.h>
3719261079SEd Maste
3819261079SEd Maste #include "xmalloc.h"
3919261079SEd Maste #include "log.h"
4019261079SEd Maste #include "sshkey.h"
4119261079SEd Maste #include "authfd.h"
4219261079SEd Maste #include "misc.h"
4319261079SEd Maste #include "sshbuf.h"
4419261079SEd Maste #include "msg.h"
4519261079SEd Maste #include "uidswap.h"
4619261079SEd Maste #include "ssherr.h"
4719261079SEd Maste #include "ssh-sk.h"
4819261079SEd Maste
4919261079SEd Maste #ifdef ENABLE_SK
5019261079SEd Maste extern char *__progname;
5119261079SEd Maste
5219261079SEd Maste static struct sshbuf *reply_error(int r, char *fmt, ...)
5319261079SEd Maste __attribute__((__format__ (printf, 2, 3)));
5419261079SEd Maste
5519261079SEd Maste static struct sshbuf *
reply_error(int r,char * fmt,...)5619261079SEd Maste reply_error(int r, char *fmt, ...)
5719261079SEd Maste {
5819261079SEd Maste char *msg;
5919261079SEd Maste va_list ap;
6019261079SEd Maste struct sshbuf *resp;
6119261079SEd Maste
6219261079SEd Maste va_start(ap, fmt);
6319261079SEd Maste xvasprintf(&msg, fmt, ap);
6419261079SEd Maste va_end(ap);
6519261079SEd Maste debug("%s: %s", __progname, msg);
6619261079SEd Maste free(msg);
6719261079SEd Maste
6819261079SEd Maste if (r >= 0)
6919261079SEd Maste fatal_f("invalid error code %d", r);
7019261079SEd Maste
7119261079SEd Maste if ((resp = sshbuf_new()) == NULL)
7219261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
7319261079SEd Maste if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
7419261079SEd Maste sshbuf_put_u32(resp, (u_int)-r) != 0)
7519261079SEd Maste fatal("%s: buffer error", __progname);
7619261079SEd Maste return resp;
7719261079SEd Maste }
7819261079SEd Maste
7919261079SEd Maste /* If the specified string is zero length, then free it and replace with NULL */
8019261079SEd Maste static void
null_empty(char ** s)8119261079SEd Maste null_empty(char **s)
8219261079SEd Maste {
8319261079SEd Maste if (s == NULL || *s == NULL || **s != '\0')
8419261079SEd Maste return;
8519261079SEd Maste
8619261079SEd Maste free(*s);
8719261079SEd Maste *s = NULL;
8819261079SEd Maste }
8919261079SEd Maste
9019261079SEd Maste static struct sshbuf *
process_sign(struct sshbuf * req)9119261079SEd Maste process_sign(struct sshbuf *req)
9219261079SEd Maste {
9319261079SEd Maste int r = SSH_ERR_INTERNAL_ERROR;
9419261079SEd Maste struct sshbuf *resp, *kbuf;
9519261079SEd Maste struct sshkey *key = NULL;
9619261079SEd Maste uint32_t compat;
9719261079SEd Maste const u_char *message;
9819261079SEd Maste u_char *sig = NULL;
9919261079SEd Maste size_t msglen, siglen = 0;
10019261079SEd Maste char *provider = NULL, *pin = NULL;
10119261079SEd Maste
10219261079SEd Maste if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
10319261079SEd Maste (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
10419261079SEd Maste (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
10519261079SEd Maste (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
10619261079SEd Maste (r = sshbuf_get_u32(req, &compat)) != 0 ||
10719261079SEd Maste (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
10819261079SEd Maste fatal_r(r, "%s: parse", __progname);
10919261079SEd Maste if (sshbuf_len(req) != 0)
11019261079SEd Maste fatal("%s: trailing data in request", __progname);
11119261079SEd Maste
11219261079SEd Maste if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
11319261079SEd Maste fatal_r(r, "%s: Unable to parse private key", __progname);
11419261079SEd Maste if (!sshkey_is_sk(key)) {
11519261079SEd Maste fatal("%s: Unsupported key type %s",
11619261079SEd Maste __progname, sshkey_ssh_name(key));
11719261079SEd Maste }
11819261079SEd Maste
11919261079SEd Maste debug_f("ready to sign with key %s, provider %s: "
12019261079SEd Maste "msg len %zu, compat 0x%lx", sshkey_type(key),
12119261079SEd Maste provider, msglen, (u_long)compat);
12219261079SEd Maste
12319261079SEd Maste null_empty(&pin);
12419261079SEd Maste
12519261079SEd Maste if ((r = sshsk_sign(provider, key, &sig, &siglen,
12619261079SEd Maste message, msglen, compat, pin)) != 0) {
12719261079SEd Maste resp = reply_error(r, "Signing failed: %s", ssh_err(r));
12819261079SEd Maste goto out;
12919261079SEd Maste }
13019261079SEd Maste
13119261079SEd Maste if ((resp = sshbuf_new()) == NULL)
13219261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
13319261079SEd Maste
13419261079SEd Maste if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
13519261079SEd Maste (r = sshbuf_put_string(resp, sig, siglen)) != 0)
13619261079SEd Maste fatal_r(r, "%s: compose", __progname);
13719261079SEd Maste out:
13819261079SEd Maste sshkey_free(key);
13919261079SEd Maste sshbuf_free(kbuf);
14019261079SEd Maste free(provider);
14119261079SEd Maste if (sig != NULL)
14219261079SEd Maste freezero(sig, siglen);
14319261079SEd Maste if (pin != NULL)
14419261079SEd Maste freezero(pin, strlen(pin));
14519261079SEd Maste return resp;
14619261079SEd Maste }
14719261079SEd Maste
14819261079SEd Maste static struct sshbuf *
process_enroll(struct sshbuf * req)14919261079SEd Maste process_enroll(struct sshbuf *req)
15019261079SEd Maste {
15119261079SEd Maste int r;
15219261079SEd Maste u_int type;
15319261079SEd Maste char *provider, *application, *pin, *device, *userid;
15419261079SEd Maste uint8_t flags;
15519261079SEd Maste struct sshbuf *challenge, *attest, *kbuf, *resp;
15619261079SEd Maste struct sshkey *key;
15719261079SEd Maste
15819261079SEd Maste if ((attest = sshbuf_new()) == NULL ||
15919261079SEd Maste (kbuf = sshbuf_new()) == NULL)
16019261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
16119261079SEd Maste
16219261079SEd Maste if ((r = sshbuf_get_u32(req, &type)) != 0 ||
16319261079SEd Maste (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
16419261079SEd Maste (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
16519261079SEd Maste (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
16619261079SEd Maste (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
16719261079SEd Maste (r = sshbuf_get_u8(req, &flags)) != 0 ||
16819261079SEd Maste (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
16919261079SEd Maste (r = sshbuf_froms(req, &challenge)) != 0)
17019261079SEd Maste fatal_r(r, "%s: parse", __progname);
17119261079SEd Maste if (sshbuf_len(req) != 0)
17219261079SEd Maste fatal("%s: trailing data in request", __progname);
17319261079SEd Maste
17419261079SEd Maste if (type > INT_MAX)
17519261079SEd Maste fatal("%s: bad type %u", __progname, type);
17619261079SEd Maste if (sshbuf_len(challenge) == 0) {
17719261079SEd Maste sshbuf_free(challenge);
17819261079SEd Maste challenge = NULL;
17919261079SEd Maste }
18019261079SEd Maste null_empty(&device);
18119261079SEd Maste null_empty(&userid);
18219261079SEd Maste null_empty(&pin);
18319261079SEd Maste
18419261079SEd Maste if ((r = sshsk_enroll((int)type, provider, device, application, userid,
18519261079SEd Maste flags, pin, challenge, &key, attest)) != 0) {
18619261079SEd Maste resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
18719261079SEd Maste goto out;
18819261079SEd Maste }
18919261079SEd Maste
19019261079SEd Maste if ((resp = sshbuf_new()) == NULL)
19119261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
19219261079SEd Maste if ((r = sshkey_private_serialize(key, kbuf)) != 0)
19319261079SEd Maste fatal_r(r, "%s: encode key", __progname);
19419261079SEd Maste if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
19519261079SEd Maste (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
19619261079SEd Maste (r = sshbuf_put_stringb(resp, attest)) != 0)
19719261079SEd Maste fatal_r(r, "%s: compose", __progname);
19819261079SEd Maste
19919261079SEd Maste out:
20019261079SEd Maste sshkey_free(key);
20119261079SEd Maste sshbuf_free(kbuf);
20219261079SEd Maste sshbuf_free(attest);
20319261079SEd Maste sshbuf_free(challenge);
20419261079SEd Maste free(provider);
20519261079SEd Maste free(application);
20619261079SEd Maste if (pin != NULL)
20719261079SEd Maste freezero(pin, strlen(pin));
20819261079SEd Maste
20919261079SEd Maste return resp;
21019261079SEd Maste }
21119261079SEd Maste
21219261079SEd Maste static struct sshbuf *
process_load_resident(struct sshbuf * req)21319261079SEd Maste process_load_resident(struct sshbuf *req)
21419261079SEd Maste {
21519261079SEd Maste int r;
21619261079SEd Maste char *provider, *pin, *device;
21719261079SEd Maste struct sshbuf *kbuf, *resp;
2181323ec57SEd Maste struct sshsk_resident_key **srks = NULL;
2191323ec57SEd Maste size_t nsrks = 0, i;
2201323ec57SEd Maste u_int flags;
22119261079SEd Maste
22219261079SEd Maste if ((kbuf = sshbuf_new()) == NULL)
22319261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
22419261079SEd Maste
22519261079SEd Maste if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
22619261079SEd Maste (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
2271323ec57SEd Maste (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
2281323ec57SEd Maste (r = sshbuf_get_u32(req, &flags)) != 0)
22919261079SEd Maste fatal_r(r, "%s: parse", __progname);
23019261079SEd Maste if (sshbuf_len(req) != 0)
23119261079SEd Maste fatal("%s: trailing data in request", __progname);
23219261079SEd Maste
23319261079SEd Maste null_empty(&device);
23419261079SEd Maste null_empty(&pin);
23519261079SEd Maste
2361323ec57SEd Maste if ((r = sshsk_load_resident(provider, device, pin, flags,
2371323ec57SEd Maste &srks, &nsrks)) != 0) {
23819261079SEd Maste resp = reply_error(r, "sshsk_load_resident failed: %s",
23919261079SEd Maste ssh_err(r));
24019261079SEd Maste goto out;
24119261079SEd Maste }
24219261079SEd Maste
24319261079SEd Maste if ((resp = sshbuf_new()) == NULL)
24419261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
24519261079SEd Maste
24619261079SEd Maste if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
24719261079SEd Maste fatal_r(r, "%s: compose", __progname);
24819261079SEd Maste
2491323ec57SEd Maste for (i = 0; i < nsrks; i++) {
2501323ec57SEd Maste debug_f("key %zu %s %s uidlen %zu", i,
2511323ec57SEd Maste sshkey_type(srks[i]->key), srks[i]->key->sk_application,
2521323ec57SEd Maste srks[i]->user_id_len);
25319261079SEd Maste sshbuf_reset(kbuf);
2541323ec57SEd Maste if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
25519261079SEd Maste fatal_r(r, "%s: encode key", __progname);
25619261079SEd Maste if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
2571323ec57SEd Maste (r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
2581323ec57SEd Maste (r = sshbuf_put_string(resp, srks[i]->user_id,
2591323ec57SEd Maste srks[i]->user_id_len)) != 0)
26019261079SEd Maste fatal_r(r, "%s: compose key", __progname);
26119261079SEd Maste }
26219261079SEd Maste
26319261079SEd Maste out:
2641323ec57SEd Maste sshsk_free_resident_keys(srks, nsrks);
26519261079SEd Maste sshbuf_free(kbuf);
26619261079SEd Maste free(provider);
26738a52bd3SEd Maste free(device);
26819261079SEd Maste if (pin != NULL)
26919261079SEd Maste freezero(pin, strlen(pin));
27019261079SEd Maste return resp;
27119261079SEd Maste }
27219261079SEd Maste
27319261079SEd Maste int
main(int argc,char ** argv)27419261079SEd Maste main(int argc, char **argv)
27519261079SEd Maste {
27619261079SEd Maste SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
27719261079SEd Maste LogLevel log_level = SYSLOG_LEVEL_ERROR;
27819261079SEd Maste struct sshbuf *req, *resp;
27919261079SEd Maste int in, out, ch, r, vflag = 0;
28019261079SEd Maste u_int rtype, ll = 0;
28119261079SEd Maste uint8_t version, log_stderr = 0;
28219261079SEd Maste
28319261079SEd Maste sanitise_stdfd();
28419261079SEd Maste log_init(__progname, log_level, log_facility, log_stderr);
28519261079SEd Maste
28619261079SEd Maste while ((ch = getopt(argc, argv, "v")) != -1) {
28719261079SEd Maste switch (ch) {
28819261079SEd Maste case 'v':
28919261079SEd Maste vflag = 1;
29019261079SEd Maste if (log_level == SYSLOG_LEVEL_ERROR)
29119261079SEd Maste log_level = SYSLOG_LEVEL_DEBUG1;
29219261079SEd Maste else if (log_level < SYSLOG_LEVEL_DEBUG3)
29319261079SEd Maste log_level++;
29419261079SEd Maste break;
29519261079SEd Maste default:
29619261079SEd Maste fprintf(stderr, "usage: %s [-v]\n", __progname);
29719261079SEd Maste exit(1);
29819261079SEd Maste }
29919261079SEd Maste }
30019261079SEd Maste log_init(__progname, log_level, log_facility, vflag);
30119261079SEd Maste
30219261079SEd Maste /*
30319261079SEd Maste * Rearrange our file descriptors a little; we don't trust the
30419261079SEd Maste * providers not to fiddle with stdin/out.
30519261079SEd Maste */
30619261079SEd Maste closefrom(STDERR_FILENO + 1);
30719261079SEd Maste if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
30819261079SEd Maste fatal("%s: dup: %s", __progname, strerror(errno));
30919261079SEd Maste close(STDIN_FILENO);
31019261079SEd Maste close(STDOUT_FILENO);
31119261079SEd Maste sanitise_stdfd(); /* resets to /dev/null */
31219261079SEd Maste
31319261079SEd Maste if ((req = sshbuf_new()) == NULL)
31419261079SEd Maste fatal("%s: sshbuf_new failed", __progname);
31519261079SEd Maste if (ssh_msg_recv(in, req) < 0)
31619261079SEd Maste fatal("ssh_msg_recv failed");
31719261079SEd Maste close(in);
31819261079SEd Maste debug_f("received message len %zu", sshbuf_len(req));
31919261079SEd Maste
32019261079SEd Maste if ((r = sshbuf_get_u8(req, &version)) != 0)
32119261079SEd Maste fatal_r(r, "%s: parse version", __progname);
32219261079SEd Maste if (version != SSH_SK_HELPER_VERSION) {
32319261079SEd Maste fatal("unsupported version: received %d, expected %d",
32419261079SEd Maste version, SSH_SK_HELPER_VERSION);
32519261079SEd Maste }
32619261079SEd Maste
32719261079SEd Maste if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
32819261079SEd Maste (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
32919261079SEd Maste (r = sshbuf_get_u32(req, &ll)) != 0)
33019261079SEd Maste fatal_r(r, "%s: parse", __progname);
33119261079SEd Maste
33219261079SEd Maste if (!vflag && log_level_name((LogLevel)ll) != NULL)
33319261079SEd Maste log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
33419261079SEd Maste
33519261079SEd Maste switch (rtype) {
33619261079SEd Maste case SSH_SK_HELPER_SIGN:
33719261079SEd Maste resp = process_sign(req);
33819261079SEd Maste break;
33919261079SEd Maste case SSH_SK_HELPER_ENROLL:
34019261079SEd Maste resp = process_enroll(req);
34119261079SEd Maste break;
34219261079SEd Maste case SSH_SK_HELPER_LOAD_RESIDENT:
34319261079SEd Maste resp = process_load_resident(req);
34419261079SEd Maste break;
34519261079SEd Maste default:
34619261079SEd Maste fatal("%s: unsupported request type %u", __progname, rtype);
34719261079SEd Maste }
34819261079SEd Maste sshbuf_free(req);
34919261079SEd Maste debug_f("reply len %zu", sshbuf_len(resp));
35019261079SEd Maste
35119261079SEd Maste if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
35219261079SEd Maste fatal("ssh_msg_send failed");
35319261079SEd Maste sshbuf_free(resp);
35419261079SEd Maste close(out);
35519261079SEd Maste
35619261079SEd Maste return (0);
35719261079SEd Maste }
35819261079SEd Maste #else /* ENABLE_SK */
35919261079SEd Maste #include <stdio.h>
36019261079SEd Maste
36119261079SEd Maste int
main(int argc,char ** argv)36219261079SEd Maste main(int argc, char **argv)
36319261079SEd Maste {
36419261079SEd Maste fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
36519261079SEd Maste return -1;
36619261079SEd Maste }
36719261079SEd Maste #endif /* ENABLE_SK */
368