xref: /openbsd-src/usr.bin/ssh/ssh-sk-helper.c (revision 5a38ef86d0b61900239c7913d24a05e7b88a58f0)
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 <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 sshsk_resident_key **srks = NULL;
217 	size_t nsrks = 0, i;
218 	u_int flags;
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 	    (r = sshbuf_get_u32(req, &flags)) != 0)
227 		fatal_r(r, "%s: parse", __progname);
228 	if (sshbuf_len(req) != 0)
229 		fatal("%s: trailing data in request", __progname);
230 
231 	null_empty(&device);
232 	null_empty(&pin);
233 
234 	if ((r = sshsk_load_resident(provider, device, pin, flags,
235 	    &srks, &nsrks)) != 0) {
236 		resp = reply_error(r, "sshsk_load_resident failed: %s",
237 		    ssh_err(r));
238 		goto out;
239 	}
240 
241 	if ((resp = sshbuf_new()) == NULL)
242 		fatal("%s: sshbuf_new failed", __progname);
243 
244 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
245 		fatal_r(r, "%s: compose", __progname);
246 
247 	for (i = 0; i < nsrks; i++) {
248 		debug_f("key %zu %s %s uidlen %zu", i,
249 		    sshkey_type(srks[i]->key), srks[i]->key->sk_application,
250 		    srks[i]->user_id_len);
251 		sshbuf_reset(kbuf);
252 		if ((r = sshkey_private_serialize(srks[i]->key, 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 		    (r = sshbuf_put_string(resp, srks[i]->user_id,
257 		    srks[i]->user_id_len)) != 0)
258 			fatal_r(r, "%s: compose key", __progname);
259 	}
260 
261  out:
262 	sshsk_free_resident_keys(srks, nsrks);
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