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