xref: /freebsd/crypto/openssh/ssh-sk-helper.c (revision 8b959dd6a3921c35395bef4a6d7ad2426a3bd88e)
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 "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_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
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_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 *
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 *
214 process_load_resident(struct sshbuf *req)
215 {
216 	int r;
217 	char *provider, *pin, *device;
218 	struct sshbuf *kbuf, *resp;
219 	struct sshkey **keys = NULL;
220 	size_t nkeys = 0, i;
221 
222 	if ((kbuf = sshbuf_new()) == NULL)
223 		fatal("%s: sshbuf_new failed", __progname);
224 
225 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
226 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
227 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
228 		fatal_r(r, "%s: parse", __progname);
229 	if (sshbuf_len(req) != 0)
230 		fatal("%s: trailing data in request", __progname);
231 
232 	null_empty(&device);
233 	null_empty(&pin);
234 
235 	if ((r = sshsk_load_resident(provider, device, pin,
236 	    &keys, &nkeys)) != 0) {
237 		resp = reply_error(r, " sshsk_load_resident failed: %s",
238 		    ssh_err(r));
239 		goto out;
240 	}
241 
242 	if ((resp = sshbuf_new()) == NULL)
243 		fatal("%s: sshbuf_new failed", __progname);
244 
245 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
246 		fatal_r(r, "%s: compose", __progname);
247 
248 	for (i = 0; i < nkeys; i++) {
249 		debug_f("key %zu %s %s", i, sshkey_type(keys[i]),
250 		    keys[i]->sk_application);
251 		sshbuf_reset(kbuf);
252 		if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0)
253 			fatal_r(r, "%s: encode key", __progname);
254 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
255 		    (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */
256 			fatal_r(r, "%s: compose key", __progname);
257 	}
258 
259  out:
260 	for (i = 0; i < nkeys; i++)
261 		sshkey_free(keys[i]);
262 	free(keys);
263 	sshbuf_free(kbuf);
264 	free(provider);
265 	if (pin != NULL)
266 		freezero(pin, strlen(pin));
267 	return resp;
268 }
269 
270 int
271 main(int argc, char **argv)
272 {
273 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
274 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
275 	struct sshbuf *req, *resp;
276 	int in, out, ch, r, vflag = 0;
277 	u_int rtype, ll = 0;
278 	uint8_t version, log_stderr = 0;
279 
280 	sanitise_stdfd();
281 	log_init(__progname, log_level, log_facility, log_stderr);
282 
283 	while ((ch = getopt(argc, argv, "v")) != -1) {
284 		switch (ch) {
285 		case 'v':
286 			vflag = 1;
287 			if (log_level == SYSLOG_LEVEL_ERROR)
288 				log_level = SYSLOG_LEVEL_DEBUG1;
289 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
290 				log_level++;
291 			break;
292 		default:
293 			fprintf(stderr, "usage: %s [-v]\n", __progname);
294 			exit(1);
295 		}
296 	}
297 	log_init(__progname, log_level, log_facility, vflag);
298 
299 	/*
300 	 * Rearrange our file descriptors a little; we don't trust the
301 	 * providers not to fiddle with stdin/out.
302 	 */
303 	closefrom(STDERR_FILENO + 1);
304 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
305 		fatal("%s: dup: %s", __progname, strerror(errno));
306 	close(STDIN_FILENO);
307 	close(STDOUT_FILENO);
308 	sanitise_stdfd(); /* resets to /dev/null */
309 
310 	if ((req = sshbuf_new()) == NULL)
311 		fatal("%s: sshbuf_new failed", __progname);
312 	if (ssh_msg_recv(in, req) < 0)
313 		fatal("ssh_msg_recv failed");
314 	close(in);
315 	debug_f("received message len %zu", sshbuf_len(req));
316 
317 	if ((r = sshbuf_get_u8(req, &version)) != 0)
318 		fatal_r(r, "%s: parse version", __progname);
319 	if (version != SSH_SK_HELPER_VERSION) {
320 		fatal("unsupported version: received %d, expected %d",
321 		    version, SSH_SK_HELPER_VERSION);
322 	}
323 
324 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
325 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
326 	    (r = sshbuf_get_u32(req, &ll)) != 0)
327 		fatal_r(r, "%s: parse", __progname);
328 
329 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
330 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
331 
332 	switch (rtype) {
333 	case SSH_SK_HELPER_SIGN:
334 		resp = process_sign(req);
335 		break;
336 	case SSH_SK_HELPER_ENROLL:
337 		resp = process_enroll(req);
338 		break;
339 	case SSH_SK_HELPER_LOAD_RESIDENT:
340 		resp = process_load_resident(req);
341 		break;
342 	default:
343 		fatal("%s: unsupported request type %u", __progname, rtype);
344 	}
345 	sshbuf_free(req);
346 	debug_f("reply len %zu", sshbuf_len(resp));
347 
348 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
349 		fatal("ssh_msg_send failed");
350 	sshbuf_free(resp);
351 	close(out);
352 
353 	return (0);
354 }
355 #else /* ENABLE_SK */
356 #include <stdio.h>
357 
358 int
359 main(int argc, char **argv)
360 {
361 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
362 	return -1;
363 }
364 #endif /* ENABLE_SK */
365