blob: 3dc149b950cad72a3c3f5bc78033316fcd612811 [file] [log] [blame]
djm@openbsd.org611073f2019-12-13 19:11:14 +00001/* $OpenBSD: ssh-sk-helper.c,v 1.4 2019/12/13 19:11:14 djm 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
djm@openbsd.org611073f2019-12-13 19:11:14 +000021 * to ssh-pkcs11-helper.c but considerably simpler as the operations for
22 * security keys are stateless.
djm@openbsd.org07da39f2019-10-31 21:22:01 +000023 *
djm@openbsd.org611073f2019-12-13 19:11:14 +000024 * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
25 * protocol changes.
djm@openbsd.org07da39f2019-10-31 21:22:01 +000026 */
djm@openbsd.org611073f2019-12-13 19:11:14 +000027
djm@openbsd.org07da39f2019-10-31 21:22:01 +000028#include "includes.h"
29
djm@openbsd.org611073f2019-12-13 19:11:14 +000030#include <limits.h>
djm@openbsd.org07da39f2019-10-31 21:22:01 +000031#include <stdarg.h>
32#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#include <unistd.h>
36#include <errno.h>
37
38#include "xmalloc.h"
39#include "log.h"
40#include "sshkey.h"
41#include "authfd.h"
42#include "misc.h"
43#include "sshbuf.h"
44#include "msg.h"
45#include "uidswap.h"
46#include "sshkey.h"
47#include "ssherr.h"
48#include "ssh-sk.h"
49
Damien Miller764d51e2019-11-01 13:34:49 +110050#ifdef ENABLE_SK
djm@openbsd.org07da39f2019-10-31 21:22:01 +000051extern char *__progname;
52
djm@openbsd.org611073f2019-12-13 19:11:14 +000053static struct sshbuf *
54process_sign(struct sshbuf *req)
55{
56 int r = SSH_ERR_INTERNAL_ERROR;
57 struct sshbuf *resp, *kbuf;
58 struct sshkey *key;
59 uint32_t compat;
60 const u_char *message;
61 u_char *sig;
62 size_t msglen, siglen;
63 char *provider;
64
65 if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
66 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
67 (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
68 (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
69 (r = sshbuf_get_u32(req, &compat)) != 0)
70 fatal("%s: buffer error: %s", __progname, ssh_err(r));
71 if (sshbuf_len(req) != 0)
72 fatal("%s: trailing data in request", __progname);
73
74 if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
75 fatal("Unable to parse private key: %s", ssh_err(r));
76 if (!sshkey_is_sk(key))
77 fatal("Unsupported key type %s", sshkey_ssh_name(key));
78
79 debug("%s: ready to sign with key %s, provider %s: "
80 "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
81 provider, msglen, (u_long)compat);
82
83 if ((r = sshsk_sign(provider, key, &sig, &siglen,
84 message, msglen, compat)) != 0)
85 fatal("Signing failed: %s", ssh_err(r));
86
87 if ((resp = sshbuf_new()) == NULL)
88 fatal("%s: sshbuf_new failed", __progname);
89
90 if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
91 fatal("%s: buffer error: %s", __progname, ssh_err(r));
92
93 sshbuf_free(kbuf);
94 free(provider);
95
96 return resp;
97}
98
99static struct sshbuf *
100process_enroll(struct sshbuf *req)
101{
102 int r;
103 u_int type;
104 char *provider;
105 char *application;
106 uint8_t flags;
107 struct sshbuf *challenge, *attest, *kbuf, *resp;
108 struct sshkey *key;
109
110 if ((resp = sshbuf_new()) == NULL ||
111 (attest = sshbuf_new()) == NULL ||
112 (kbuf = sshbuf_new()) == NULL)
113 fatal("%s: sshbuf_new failed", __progname);
114
115 if ((r = sshbuf_get_u32(req, &type)) != 0 ||
116 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
117 (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
118 (r = sshbuf_get_u8(req, &flags)) != 0 ||
119 (r = sshbuf_froms(req, &challenge)) != 0)
120 fatal("%s: buffer error: %s", __progname, ssh_err(r));
121 if (sshbuf_len(req) != 0)
122 fatal("%s: trailing data in request", __progname);
123
124 if (type > INT_MAX)
125 fatal("%s: bad type %u", __progname, type);
126 if (sshbuf_len(challenge) == 0) {
127 sshbuf_free(challenge);
128 challenge = NULL;
129 }
130
131 if ((r = sshsk_enroll((int)type, provider, application, flags,
132 challenge, &key, attest)) != 0)
133 fatal("%s: sshsk_enroll failed: %s", __progname, ssh_err(r));
134
135 if ((r = sshkey_private_serialize(key, kbuf)) != 0)
136 fatal("%s: serialize private key: %s", __progname, ssh_err(r));
137 if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
138 (r = sshbuf_put_stringb(resp, attest)) != 0)
139 fatal("%s: buffer error: %s", __progname, ssh_err(r));
140
141 sshkey_free(key);
142 sshbuf_free(kbuf);
143 sshbuf_free(attest);
144 sshbuf_free(challenge);
145 free(provider);
146 free(application);
147
148 return resp;
149}
150
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000151int
152main(int argc, char **argv)
153{
154 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
155 LogLevel log_level = SYSLOG_LEVEL_ERROR;
Damien Millerf45f3a82019-12-14 07:53:11 +1100156 struct sshbuf *req, *resp;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000157 int in, out, ch, r, log_stderr = 0;
djm@openbsd.org611073f2019-12-13 19:11:14 +0000158 u_int rtype;
159 uint8_t version;
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000160
161 sanitise_stdfd();
162 log_init(__progname, log_level, log_facility, log_stderr);
163
164 while ((ch = getopt(argc, argv, "v")) != -1) {
165 switch (ch) {
166 case 'v':
167 log_stderr = 1;
168 if (log_level == SYSLOG_LEVEL_ERROR)
169 log_level = SYSLOG_LEVEL_DEBUG1;
170 else if (log_level < SYSLOG_LEVEL_DEBUG3)
171 log_level++;
172 break;
173 default:
174 fprintf(stderr, "usage: %s [-v]\n", __progname);
175 exit(1);
176 }
177 }
178 log_init(__progname, log_level, log_facility, log_stderr);
179
180 /*
181 * Rearrange our file descriptors a little; we don't trust the
182 * providers not to fiddle with stdin/out.
183 */
184 closefrom(STDERR_FILENO + 1);
185 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
186 fatal("%s: dup: %s", __progname, strerror(errno));
187 close(STDIN_FILENO);
188 close(STDOUT_FILENO);
189 sanitise_stdfd(); /* resets to /dev/null */
190
djm@openbsd.org611073f2019-12-13 19:11:14 +0000191 if ((req = sshbuf_new()) == NULL)
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000192 fatal("%s: sshbuf_new failed", __progname);
193 if (ssh_msg_recv(in, req) < 0)
194 fatal("ssh_msg_recv failed");
195 close(in);
196 debug("%s: received message len %zu", __progname, sshbuf_len(req));
197
198 if ((r = sshbuf_get_u8(req, &version)) != 0)
199 fatal("%s: buffer error: %s", __progname, ssh_err(r));
200 if (version != SSH_SK_HELPER_VERSION) {
201 fatal("unsupported version: received %d, expected %d",
202 version, SSH_SK_HELPER_VERSION);
203 }
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000204
djm@openbsd.org611073f2019-12-13 19:11:14 +0000205 if ((r = sshbuf_get_u32(req, &rtype)) != 0)
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000206 fatal("%s: buffer error: %s", __progname, ssh_err(r));
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000207
djm@openbsd.org611073f2019-12-13 19:11:14 +0000208 switch (rtype) {
209 case SSH_SK_HELPER_SIGN:
210 resp = process_sign(req);
211 break;
212 case SSH_SK_HELPER_ENROLL:
213 resp = process_enroll(req);
214 break;
215 default:
216 fatal("%s: unsupported request type %u", __progname, rtype);
217 }
218 sshbuf_free(req);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000219 debug("%s: reply len %zu", __progname, sshbuf_len(resp));
djm@openbsd.org611073f2019-12-13 19:11:14 +0000220
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000221 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
222 fatal("ssh_msg_send failed");
djm@openbsd.org611073f2019-12-13 19:11:14 +0000223 sshbuf_free(resp);
djm@openbsd.org07da39f2019-10-31 21:22:01 +0000224 close(out);
225
226 return (0);
227}
Damien Miller764d51e2019-11-01 13:34:49 +1100228#else /* ENABLE_SK */
229#include <stdio.h>
230
231int
232main(int argc, char **argv)
233{
234 fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
235 return -1;
236}
237#endif /* ENABLE_SK */