xref: /openssh-portable/ssh-sk-helper.c (revision 4be56399)
1 /* $OpenBSD: ssh-sk-helper.c,v 1.10 2020/05/26 01:59:46 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 "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 *
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("%s: invalid error code %d", __func__, 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
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 *
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("%s: buffer error: %s", __progname, ssh_err(r));
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("Unable to parse private key: %s", ssh_err(r));
115 	if (!sshkey_is_sk(key))
116 		fatal("Unsupported key type %s", sshkey_ssh_name(key));
117 
118 	debug("%s: ready to sign with key %s, provider %s: "
119 	    "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
120 	    provider, msglen, (u_long)compat);
121 
122 	null_empty(&pin);
123 
124 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
125 	    message, msglen, compat, pin)) != 0) {
126 		resp = reply_error(r, "Signing failed: %s", ssh_err(r));
127 		goto out;
128 	}
129 
130 	if ((resp = sshbuf_new()) == NULL)
131 		fatal("%s: sshbuf_new failed", __progname);
132 
133 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
134 	    (r = sshbuf_put_string(resp, sig, siglen)) != 0)
135 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
136  out:
137 	sshkey_free(key);
138 	sshbuf_free(kbuf);
139 	free(provider);
140 	if (sig != NULL)
141 		freezero(sig, siglen);
142 	if (pin != NULL)
143 		freezero(pin, strlen(pin));
144 	return resp;
145 }
146 
147 static struct sshbuf *
148 process_enroll(struct sshbuf *req)
149 {
150 	int r;
151 	u_int type;
152 	char *provider, *application, *pin, *device, *userid;
153 	uint8_t flags;
154 	struct sshbuf *challenge, *attest, *kbuf, *resp;
155 	struct sshkey *key;
156 
157 	if ((attest = sshbuf_new()) == NULL ||
158 	    (kbuf = sshbuf_new()) == NULL)
159 		fatal("%s: sshbuf_new failed", __progname);
160 
161 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
162 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
163 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
164 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
165 	    (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
166 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
167 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
168 	    (r = sshbuf_froms(req, &challenge)) != 0)
169 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
170 	if (sshbuf_len(req) != 0)
171 		fatal("%s: trailing data in request", __progname);
172 
173 	if (type > INT_MAX)
174 		fatal("%s: bad type %u", __progname, type);
175 	if (sshbuf_len(challenge) == 0) {
176 		sshbuf_free(challenge);
177 		challenge = NULL;
178 	}
179 	null_empty(&device);
180 	null_empty(&userid);
181 	null_empty(&pin);
182 
183 	if ((r = sshsk_enroll((int)type, provider, device, application, userid,
184 	    flags, pin, challenge, &key, attest)) != 0) {
185 		resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
186 		goto out;
187 	}
188 
189 	if ((resp = sshbuf_new()) == NULL)
190 		fatal("%s: sshbuf_new failed", __progname);
191 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
192 		fatal("%s: serialize private key: %s", __progname, ssh_err(r));
193 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
194 	    (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
195 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
196 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
197 
198  out:
199 	sshkey_free(key);
200 	sshbuf_free(kbuf);
201 	sshbuf_free(attest);
202 	sshbuf_free(challenge);
203 	free(provider);
204 	free(application);
205 	if (pin != NULL)
206 		freezero(pin, strlen(pin));
207 
208 	return resp;
209 }
210 
211 static struct sshbuf *
212 process_load_resident(struct sshbuf *req)
213 {
214 	int r;
215 	char *provider, *pin, *device;
216 	struct sshbuf *kbuf, *resp;
217 	struct sshkey **keys = NULL;
218 	size_t nkeys = 0, i;
219 
220 	if ((kbuf = sshbuf_new()) == NULL)
221 		fatal("%s: sshbuf_new failed", __progname);
222 
223 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
224 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
225 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
226 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
227 	if (sshbuf_len(req) != 0)
228 		fatal("%s: trailing data in request", __progname);
229 
230 	null_empty(&device);
231 	null_empty(&pin);
232 
233 	if ((r = sshsk_load_resident(provider, device, pin,
234 	    &keys, &nkeys)) != 0) {
235 		resp = reply_error(r, " sshsk_load_resident failed: %s",
236 		    ssh_err(r));
237 		goto out;
238 	}
239 
240 	if ((resp = sshbuf_new()) == NULL)
241 		fatal("%s: sshbuf_new failed", __progname);
242 
243 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
244 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
245 
246 	for (i = 0; i < nkeys; i++) {
247 		debug("%s: key %zu %s %s", __func__, i,
248 		    sshkey_type(keys[i]), keys[i]->sk_application);
249 		sshbuf_reset(kbuf);
250 		if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0)
251 			fatal("%s: serialize private key: %s",
252 			    __progname, ssh_err(r));
253 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
254 		    (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */
255 			fatal("%s: buffer error: %s", __progname, ssh_err(r));
256 	}
257 
258  out:
259 	for (i = 0; i < nkeys; i++)
260 		sshkey_free(keys[i]);
261 	free(keys);
262 	sshbuf_free(kbuf);
263 	free(provider);
264 	if (pin != NULL)
265 		freezero(pin, strlen(pin));
266 	return resp;
267 }
268 
269 int
270 main(int argc, char **argv)
271 {
272 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
273 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
274 	struct sshbuf *req, *resp;
275 	int in, out, ch, r, vflag = 0;
276 	u_int rtype, ll = 0;
277 	uint8_t version, log_stderr = 0;
278 
279 	sanitise_stdfd();
280 	log_init(__progname, log_level, log_facility, log_stderr);
281 
282 	while ((ch = getopt(argc, argv, "v")) != -1) {
283 		switch (ch) {
284 		case 'v':
285 			vflag = 1;
286 			if (log_level == SYSLOG_LEVEL_ERROR)
287 				log_level = SYSLOG_LEVEL_DEBUG1;
288 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
289 				log_level++;
290 			break;
291 		default:
292 			fprintf(stderr, "usage: %s [-v]\n", __progname);
293 			exit(1);
294 		}
295 	}
296 	log_init(__progname, log_level, log_facility, vflag);
297 
298 	/*
299 	 * Rearrange our file descriptors a little; we don't trust the
300 	 * providers not to fiddle with stdin/out.
301 	 */
302 	closefrom(STDERR_FILENO + 1);
303 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
304 		fatal("%s: dup: %s", __progname, strerror(errno));
305 	close(STDIN_FILENO);
306 	close(STDOUT_FILENO);
307 	sanitise_stdfd(); /* resets to /dev/null */
308 
309 	if ((req = sshbuf_new()) == NULL)
310 		fatal("%s: sshbuf_new failed", __progname);
311 	if (ssh_msg_recv(in, req) < 0)
312 		fatal("ssh_msg_recv failed");
313 	close(in);
314 	debug("%s: received message len %zu", __progname, sshbuf_len(req));
315 
316 	if ((r = sshbuf_get_u8(req, &version)) != 0)
317 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
318 	if (version != SSH_SK_HELPER_VERSION) {
319 		fatal("unsupported version: received %d, expected %d",
320 		    version, SSH_SK_HELPER_VERSION);
321 	}
322 
323 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
324 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
325 	    (r = sshbuf_get_u32(req, &ll)) != 0)
326 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
327 
328 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
329 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
330 
331 	switch (rtype) {
332 	case SSH_SK_HELPER_SIGN:
333 		resp = process_sign(req);
334 		break;
335 	case SSH_SK_HELPER_ENROLL:
336 		resp = process_enroll(req);
337 		break;
338 	case SSH_SK_HELPER_LOAD_RESIDENT:
339 		resp = process_load_resident(req);
340 		break;
341 	default:
342 		fatal("%s: unsupported request type %u", __progname, rtype);
343 	}
344 	sshbuf_free(req);
345 	debug("%s: reply len %zu", __progname, sshbuf_len(resp));
346 
347 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
348 		fatal("ssh_msg_send failed");
349 	sshbuf_free(resp);
350 	close(out);
351 
352 	return (0);
353 }
354 #else /* ENABLE_SK */
355 #include <stdio.h>
356 
357 int
358 main(int argc, char **argv)
359 {
360 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
361 	return -1;
362 }
363 #endif /* ENABLE_SK */
364