xref: /freebsd/crypto/openssh/ssh-sk-helper.c (revision e87ec409fa9b21abf79895837fe375ab3d7e408a)
1 /* $OpenBSD: ssh-sk-helper.c,v 1.12 2021/10/28 02:54:18 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 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 	if (pin != NULL)
269 		freezero(pin, strlen(pin));
270 	return resp;
271 }
272 
273 int
274 main(int argc, char **argv)
275 {
276 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
277 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
278 	struct sshbuf *req, *resp;
279 	int in, out, ch, r, vflag = 0;
280 	u_int rtype, ll = 0;
281 	uint8_t version, log_stderr = 0;
282 
283 	sanitise_stdfd();
284 	log_init(__progname, log_level, log_facility, log_stderr);
285 
286 	while ((ch = getopt(argc, argv, "v")) != -1) {
287 		switch (ch) {
288 		case 'v':
289 			vflag = 1;
290 			if (log_level == SYSLOG_LEVEL_ERROR)
291 				log_level = SYSLOG_LEVEL_DEBUG1;
292 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
293 				log_level++;
294 			break;
295 		default:
296 			fprintf(stderr, "usage: %s [-v]\n", __progname);
297 			exit(1);
298 		}
299 	}
300 	log_init(__progname, log_level, log_facility, vflag);
301 
302 	/*
303 	 * Rearrange our file descriptors a little; we don't trust the
304 	 * providers not to fiddle with stdin/out.
305 	 */
306 	closefrom(STDERR_FILENO + 1);
307 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
308 		fatal("%s: dup: %s", __progname, strerror(errno));
309 	close(STDIN_FILENO);
310 	close(STDOUT_FILENO);
311 	sanitise_stdfd(); /* resets to /dev/null */
312 
313 	if ((req = sshbuf_new()) == NULL)
314 		fatal("%s: sshbuf_new failed", __progname);
315 	if (ssh_msg_recv(in, req) < 0)
316 		fatal("ssh_msg_recv failed");
317 	close(in);
318 	debug_f("received message len %zu", sshbuf_len(req));
319 
320 	if ((r = sshbuf_get_u8(req, &version)) != 0)
321 		fatal_r(r, "%s: parse version", __progname);
322 	if (version != SSH_SK_HELPER_VERSION) {
323 		fatal("unsupported version: received %d, expected %d",
324 		    version, SSH_SK_HELPER_VERSION);
325 	}
326 
327 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
328 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
329 	    (r = sshbuf_get_u32(req, &ll)) != 0)
330 		fatal_r(r, "%s: parse", __progname);
331 
332 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
333 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
334 
335 	switch (rtype) {
336 	case SSH_SK_HELPER_SIGN:
337 		resp = process_sign(req);
338 		break;
339 	case SSH_SK_HELPER_ENROLL:
340 		resp = process_enroll(req);
341 		break;
342 	case SSH_SK_HELPER_LOAD_RESIDENT:
343 		resp = process_load_resident(req);
344 		break;
345 	default:
346 		fatal("%s: unsupported request type %u", __progname, rtype);
347 	}
348 	sshbuf_free(req);
349 	debug_f("reply len %zu", sshbuf_len(resp));
350 
351 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
352 		fatal("ssh_msg_send failed");
353 	sshbuf_free(resp);
354 	close(out);
355 
356 	return (0);
357 }
358 #else /* ENABLE_SK */
359 #include <stdio.h>
360 
361 int
362 main(int argc, char **argv)
363 {
364 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
365 	return -1;
366 }
367 #endif /* ENABLE_SK */
368