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