blob: 0acb8d1728af1323ae1637e10de9eea426933665 [file] [log] [blame]
markus@openbsd.org2c557442019-11-12 19:33:08 +00001/* $OpenBSD: ssh-sk-helper.c,v 1.3 2019/11/12 19:33:08 markus Exp $ */
djm@openbsd.org07da39f2019-10-31 21:22:01 +00002/*
3 * Copyright (c) 2019 Google LLC
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18/*
19 * This is a tiny program used to isolate the address space used for
20 * security key middleware signing operations from ssh-agent. It is similar
21 * to ssh-pkcs11-helper.c but considerably simpler as the signing operation
22 * for this case are stateless.
23 *
24 * It receives a signing request (key, provider, message, flags) from
25 * stdin, attempts to perform a signature using the security key provider
26 * and returns the resultant signature via stdout.
27 *
28 * In the future, this program might gain additional functions to support
29 * FIDO2 tokens such as enumerating resident keys. When this happens it will
30 * be necessary to crank SSH_SK_HELPER_VERSION below.
31 */
32
33#include "includes.h"
34
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40#include <errno.h>
41
42#include "xmalloc.h"
43#include "log.h"
44#include "sshkey.h"
45#include "authfd.h"
46#include "misc.h"
47#include "sshbuf.h"
48#include "msg.h"
49#include "uidswap.h"
50#include "sshkey.h"
51#include "ssherr.h"
52#include "ssh-sk.h"
53
Damien Miller764d51e2019-11-01 13:34:49 +110054#ifdef ENABLE_SK
djm@openbsd.org07da39f2019-10-31 21:22:01 +000055extern char *__progname;
56
57int
58main(int argc, char **argv)
59{
60 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
61 LogLevel log_level = SYSLOG_LEVEL_ERROR;
62 struct sshbuf *req, *resp, *kbuf;
63 struct sshkey *key;
64 uint32_t compat;
65 const u_char *message;
66 u_char version, *sig;
67 size_t msglen, siglen;
68 char *provider;
69 int in, out, ch, r, log_stderr = 0;
70
71 sanitise_stdfd();
72 log_init(__progname, log_level, log_facility, log_stderr);
73
74 while ((ch = getopt(argc, argv, "v")) != -1) {
75 switch (ch) {
76 case 'v':
77 log_stderr = 1;
78 if (log_level == SYSLOG_LEVEL_ERROR)
79 log_level = SYSLOG_LEVEL_DEBUG1;
80 else if (log_level < SYSLOG_LEVEL_DEBUG3)
81 log_level++;
82 break;
83 default:
84 fprintf(stderr, "usage: %s [-v]\n", __progname);
85 exit(1);
86 }
87 }
88 log_init(__progname, log_level, log_facility, log_stderr);
89
90 /*
91 * Rearrange our file descriptors a little; we don't trust the
92 * providers not to fiddle with stdin/out.
93 */
94 closefrom(STDERR_FILENO + 1);
95 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
96 fatal("%s: dup: %s", __progname, strerror(errno));
97 close(STDIN_FILENO);
98 close(STDOUT_FILENO);
99 sanitise_stdfd(); /* resets to /dev/null */
100
101 if ((req = sshbuf_new()) == NULL || (resp = sshbuf_new()) == NULL)
102 fatal("%s: sshbuf_new failed", __progname);
103 if (ssh_msg_recv(in, req) < 0)
104 fatal("ssh_msg_recv failed");
105 close(in);
106 debug("%s: received message len %zu", __progname, sshbuf_len(req));
107
108 if ((r = sshbuf_get_u8(req, &version)) != 0)
109 fatal("%s: buffer error: %s", __progname, ssh_err(r));
110 if (version != SSH_SK_HELPER_VERSION) {
111 fatal("unsupported version: received %d, expected %d",
112 version, SSH_SK_HELPER_VERSION);
113 }
114 if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
115 (r = sshkey_private_deserialize(kbuf, &key)) != 0)
116 fatal("Unable to parse key: %s", ssh_err(r));
markus@openbsd.org2c557442019-11-12 19:33:08 +0000117 if (!sshkey_is_sk(key))
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000118 fatal("Unsupported key type %s", sshkey_ssh_name(key));
119
120 if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
121 (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
122 (r = sshbuf_get_u32(req, &compat)) != 0)
123 fatal("%s: buffer error: %s", __progname, ssh_err(r));
124 if (sshbuf_len(req) != 0)
125 fatal("%s: trailing data in request", __progname);
126
127 debug("%s: ready to sign with key %s, provider %s: "
128 "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
129 provider, msglen, (u_long)compat);
130
markus@openbsd.orge03a29e2019-11-12 19:30:50 +0000131 if ((r = sshsk_sign(provider, key, &sig, &siglen,
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000132 message, msglen, compat)) != 0)
133 fatal("Signing failed: %s", ssh_err(r));
134
135 /* send reply */
136 if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
137 fatal("%s: buffer error: %s", __progname, ssh_err(r));
138 debug("%s: reply len %zu", __progname, sshbuf_len(resp));
139 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
140 fatal("ssh_msg_send failed");
141 close(out);
142
143 return (0);
144}
Damien Miller764d51e2019-11-01 13:34:49 +1100145#else /* ENABLE_SK */
146#include <stdio.h>
147
148int
149main(int argc, char **argv)
150{
151 fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
152 return -1;
153}
154#endif /* ENABLE_SK */