1 /* $OpenBSD: ssh-sk-helper.c,v 1.13 2022/04/29 03:16:48 dtucker Exp $ */
2 /*
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 operations for
22 * security keys are stateless.
23 *
24 * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
25 * protocol changes.
26 */
27
28 #include "includes.h"
29
30 #include <limits.h>
31 #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
50 #ifdef ENABLE_SK
51 extern char *__progname;
52
53 static struct sshbuf *reply_error(int r, char *fmt, ...)
54 __attribute__((__format__ (printf, 2, 3)));
55
56 static struct sshbuf *
reply_error(int r,char * fmt,...)57 reply_error(int r, char *fmt, ...)
58 {
59 char *msg;
60 va_list ap;
61 struct sshbuf *resp;
62
63 va_start(ap, fmt);
64 xvasprintf(&msg, fmt, ap);
65 va_end(ap);
66 debug("%s: %s", __progname, msg);
67 free(msg);
68
69 if (r >= 0)
70 fatal_f("invalid error code %d", r);
71
72 if ((resp = sshbuf_new()) == NULL)
73 fatal("%s: sshbuf_new failed", __progname);
74 if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
75 sshbuf_put_u32(resp, (u_int)-r) != 0)
76 fatal("%s: buffer error", __progname);
77 return resp;
78 }
79
80 /* If the specified string is zero length, then free it and replace with NULL */
81 static void
null_empty(char ** s)82 null_empty(char **s)
83 {
84 if (s == NULL || *s == NULL || **s != '\0')
85 return;
86
87 free(*s);
88 *s = NULL;
89 }
90
91 static struct sshbuf *
process_sign(struct sshbuf * req)92 process_sign(struct sshbuf *req)
93 {
94 int r = SSH_ERR_INTERNAL_ERROR;
95 struct sshbuf *resp, *kbuf;
96 struct sshkey *key = NULL;
97 uint32_t compat;
98 const u_char *message;
99 u_char *sig = NULL;
100 size_t msglen, siglen = 0;
101 char *provider = NULL, *pin = NULL;
102
103 if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
104 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
105 (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
106 (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
107 (r = sshbuf_get_u32(req, &compat)) != 0 ||
108 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
109 fatal_r(r, "%s: parse", __progname);
110 if (sshbuf_len(req) != 0)
111 fatal("%s: trailing data in request", __progname);
112
113 if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
114 fatal_r(r, "%s: Unable to parse private key", __progname);
115 if (!sshkey_is_sk(key)) {
116 fatal("%s: Unsupported key type %s",
117 __progname, sshkey_ssh_name(key));
118 }
119
120 debug_f("ready to sign with key %s, provider %s: "
121 "msg len %zu, compat 0x%lx", sshkey_type(key),
122 provider, msglen, (u_long)compat);
123
124 null_empty(&pin);
125
126 if ((r = sshsk_sign(provider, key, &sig, &siglen,
127 message, msglen, compat, pin)) != 0) {
128 resp = reply_error(r, "Signing failed: %s", ssh_err(r));
129 goto out;
130 }
131
132 if ((resp = sshbuf_new()) == NULL)
133 fatal("%s: sshbuf_new failed", __progname);
134
135 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
136 (r = sshbuf_put_string(resp, sig, siglen)) != 0)
137 fatal_r(r, "%s: compose", __progname);
138 out:
139 sshkey_free(key);
140 sshbuf_free(kbuf);
141 free(provider);
142 if (sig != NULL)
143 freezero(sig, siglen);
144 if (pin != NULL)
145 freezero(pin, strlen(pin));
146 return resp;
147 }
148
149 static struct sshbuf *
process_enroll(struct sshbuf * req)150 process_enroll(struct sshbuf *req)
151 {
152 int r;
153 u_int type;
154 char *provider, *application, *pin, *device, *userid;
155 uint8_t flags;
156 struct sshbuf *challenge, *attest, *kbuf, *resp;
157 struct sshkey *key;
158
159 if ((attest = sshbuf_new()) == NULL ||
160 (kbuf = sshbuf_new()) == NULL)
161 fatal("%s: sshbuf_new failed", __progname);
162
163 if ((r = sshbuf_get_u32(req, &type)) != 0 ||
164 (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
165 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
166 (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
167 (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
168 (r = sshbuf_get_u8(req, &flags)) != 0 ||
169 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
170 (r = sshbuf_froms(req, &challenge)) != 0)
171 fatal_r(r, "%s: parse", __progname);
172 if (sshbuf_len(req) != 0)
173 fatal("%s: trailing data in request", __progname);
174
175 if (type > INT_MAX)
176 fatal("%s: bad type %u", __progname, type);
177 if (sshbuf_len(challenge) == 0) {
178 sshbuf_free(challenge);
179 challenge = NULL;
180 }
181 null_empty(&device);
182 null_empty(&userid);
183 null_empty(&pin);
184
185 if ((r = sshsk_enroll((int)type, provider, device, application, userid,
186 flags, pin, challenge, &key, attest)) != 0) {
187 resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
188 goto out;
189 }
190
191 if ((resp = sshbuf_new()) == NULL)
192 fatal("%s: sshbuf_new failed", __progname);
193 if ((r = sshkey_private_serialize(key, kbuf)) != 0)
194 fatal_r(r, "%s: encode key", __progname);
195 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
196 (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
197 (r = sshbuf_put_stringb(resp, attest)) != 0)
198 fatal_r(r, "%s: compose", __progname);
199
200 out:
201 sshkey_free(key);
202 sshbuf_free(kbuf);
203 sshbuf_free(attest);
204 sshbuf_free(challenge);
205 free(provider);
206 free(application);
207 if (pin != NULL)
208 freezero(pin, strlen(pin));
209
210 return resp;
211 }
212
213 static struct sshbuf *
process_load_resident(struct sshbuf * req)214 process_load_resident(struct sshbuf *req)
215 {
216 int r;
217 char *provider, *pin, *device;
218 struct sshbuf *kbuf, *resp;
219 struct sshsk_resident_key **srks = NULL;
220 size_t nsrks = 0, i;
221 u_int flags;
222
223 if ((kbuf = sshbuf_new()) == NULL)
224 fatal("%s: sshbuf_new failed", __progname);
225
226 if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
227 (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
228 (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
229 (r = sshbuf_get_u32(req, &flags)) != 0)
230 fatal_r(r, "%s: parse", __progname);
231 if (sshbuf_len(req) != 0)
232 fatal("%s: trailing data in request", __progname);
233
234 null_empty(&device);
235 null_empty(&pin);
236
237 if ((r = sshsk_load_resident(provider, device, pin, flags,
238 &srks, &nsrks)) != 0) {
239 resp = reply_error(r, "sshsk_load_resident failed: %s",
240 ssh_err(r));
241 goto out;
242 }
243
244 if ((resp = sshbuf_new()) == NULL)
245 fatal("%s: sshbuf_new failed", __progname);
246
247 if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
248 fatal_r(r, "%s: compose", __progname);
249
250 for (i = 0; i < nsrks; i++) {
251 debug_f("key %zu %s %s uidlen %zu", i,
252 sshkey_type(srks[i]->key), srks[i]->key->sk_application,
253 srks[i]->user_id_len);
254 sshbuf_reset(kbuf);
255 if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
256 fatal_r(r, "%s: encode key", __progname);
257 if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
258 (r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
259 (r = sshbuf_put_string(resp, srks[i]->user_id,
260 srks[i]->user_id_len)) != 0)
261 fatal_r(r, "%s: compose key", __progname);
262 }
263
264 out:
265 sshsk_free_resident_keys(srks, nsrks);
266 sshbuf_free(kbuf);
267 free(provider);
268 free(device);
269 if (pin != NULL)
270 freezero(pin, strlen(pin));
271 return resp;
272 }
273
274 int
main(int argc,char ** argv)275 main(int argc, char **argv)
276 {
277 SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
278 LogLevel log_level = SYSLOG_LEVEL_ERROR;
279 struct sshbuf *req, *resp;
280 int in, out, ch, r, vflag = 0;
281 u_int rtype, ll = 0;
282 uint8_t version, log_stderr = 0;
283
284 sanitise_stdfd();
285 log_init(__progname, log_level, log_facility, log_stderr);
286
287 while ((ch = getopt(argc, argv, "v")) != -1) {
288 switch (ch) {
289 case 'v':
290 vflag = 1;
291 if (log_level == SYSLOG_LEVEL_ERROR)
292 log_level = SYSLOG_LEVEL_DEBUG1;
293 else if (log_level < SYSLOG_LEVEL_DEBUG3)
294 log_level++;
295 break;
296 default:
297 fprintf(stderr, "usage: %s [-v]\n", __progname);
298 exit(1);
299 }
300 }
301 log_init(__progname, log_level, log_facility, vflag);
302
303 /*
304 * Rearrange our file descriptors a little; we don't trust the
305 * providers not to fiddle with stdin/out.
306 */
307 closefrom(STDERR_FILENO + 1);
308 if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
309 fatal("%s: dup: %s", __progname, strerror(errno));
310 close(STDIN_FILENO);
311 close(STDOUT_FILENO);
312 sanitise_stdfd(); /* resets to /dev/null */
313
314 if ((req = sshbuf_new()) == NULL)
315 fatal("%s: sshbuf_new failed", __progname);
316 if (ssh_msg_recv(in, req) < 0)
317 fatal("ssh_msg_recv failed");
318 close(in);
319 debug_f("received message len %zu", sshbuf_len(req));
320
321 if ((r = sshbuf_get_u8(req, &version)) != 0)
322 fatal_r(r, "%s: parse version", __progname);
323 if (version != SSH_SK_HELPER_VERSION) {
324 fatal("unsupported version: received %d, expected %d",
325 version, SSH_SK_HELPER_VERSION);
326 }
327
328 if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
329 (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
330 (r = sshbuf_get_u32(req, &ll)) != 0)
331 fatal_r(r, "%s: parse", __progname);
332
333 if (!vflag && log_level_name((LogLevel)ll) != NULL)
334 log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
335
336 switch (rtype) {
337 case SSH_SK_HELPER_SIGN:
338 resp = process_sign(req);
339 break;
340 case SSH_SK_HELPER_ENROLL:
341 resp = process_enroll(req);
342 break;
343 case SSH_SK_HELPER_LOAD_RESIDENT:
344 resp = process_load_resident(req);
345 break;
346 default:
347 fatal("%s: unsupported request type %u", __progname, rtype);
348 }
349 sshbuf_free(req);
350 debug_f("reply len %zu", sshbuf_len(resp));
351
352 if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
353 fatal("ssh_msg_send failed");
354 sshbuf_free(resp);
355 close(out);
356
357 return (0);
358 }
359 #else /* ENABLE_SK */
360 #include <stdio.h>
361
362 int
main(int argc,char ** argv)363 main(int argc, char **argv)
364 {
365 fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
366 return -1;
367 }
368 #endif /* ENABLE_SK */
369