xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-sk-helper.c (revision 7d62b00eb9ad855ffcd7da46b41e23feb5476fac)
1 /*	$NetBSD: ssh-sk-helper.c,v 1.6 2022/10/05 22:39:36 christos Exp $	*/
2 /* $OpenBSD: ssh-sk-helper.c,v 1.13 2022/04/29 03:16:48 dtucker Exp $ */
3 
4 /*
5  * Copyright (c) 2019 Google LLC
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * This is a tiny program used to isolate the address space used for
22  * security key middleware signing operations from ssh-agent. It is similar
23  * to ssh-pkcs11-helper.c but considerably simpler as the operations for
24  * security keys are stateless.
25  *
26  * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
27  * protocol changes.
28  */
29 #include "includes.h"
30 __RCSID("$NetBSD: ssh-sk-helper.c,v 1.6 2022/10/05 22:39:36 christos Exp $");
31 
32 #include <limits.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39 
40 #include "xmalloc.h"
41 #include "log.h"
42 #include "sshkey.h"
43 #include "authfd.h"
44 #include "misc.h"
45 #include "sshbuf.h"
46 #include "msg.h"
47 #include "uidswap.h"
48 #include "sshkey.h"
49 #include "ssherr.h"
50 #include "ssh-sk.h"
51 
52 extern char *__progname;
53 
54 static struct sshbuf *reply_error(int r, const char *fmt, ...)
55     __attribute__((__format__ (__printf__, 2, 3)));
56 
57 static struct sshbuf *
58 reply_error(int r, const char *fmt, ...)
59 {
60 	char *msg;
61 	va_list ap;
62 	struct sshbuf *resp;
63 
64 	va_start(ap, fmt);
65 	xvasprintf(&msg, fmt, ap);
66 	va_end(ap);
67 	debug("%s: %s", __progname, msg);
68 	free(msg);
69 
70 	if (r >= 0)
71 		fatal_f("invalid error code %d", r);
72 
73 	if ((resp = sshbuf_new()) == NULL)
74 		fatal("%s: sshbuf_new failed", __progname);
75 	if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
76 	    sshbuf_put_u32(resp, (u_int)-r) != 0)
77 		fatal("%s: buffer error", __progname);
78 	return resp;
79 }
80 
81 /* If the specified string is zero length, then free it and replace with NULL */
82 static void
83 null_empty(char **s)
84 {
85 	if (s == NULL || *s == NULL || **s != '\0')
86 		return;
87 
88 	free(*s);
89 	*s = NULL;
90 }
91 
92 static struct sshbuf *
93 process_sign(struct sshbuf *req)
94 {
95 	int r = SSH_ERR_INTERNAL_ERROR;
96 	struct sshbuf *resp, *kbuf;
97 	struct sshkey *key = NULL;
98 	uint32_t compat;
99 	const u_char *message;
100 	u_char *sig = NULL;
101 	size_t msglen, siglen = 0;
102 	char *provider = NULL, *pin = NULL;
103 
104 	if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
105 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
106 	    (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
107 	    (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
108 	    (r = sshbuf_get_u32(req, &compat)) != 0 ||
109 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
110 		fatal_r(r, "%s: parse", __progname);
111 	if (sshbuf_len(req) != 0)
112 		fatal("%s: trailing data in request", __progname);
113 
114 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
115 		fatal_r(r, "%s: Unable to parse private key", __progname);
116 	if (!sshkey_is_sk(key)) {
117 		fatal("%s: Unsupported key type %s",
118 		    __progname, sshkey_ssh_name(key));
119 	}
120 
121 	debug_f("ready to sign with key %s, provider %s: "
122 	    "msg len %zu, compat 0x%lx", sshkey_type(key),
123 	    provider, msglen, (u_long)compat);
124 
125 	null_empty(&pin);
126 
127 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
128 	    message, msglen, compat, pin)) != 0) {
129 		resp = reply_error(r, "Signing failed: %s", ssh_err(r));
130 		goto out;
131 	}
132 
133 	if ((resp = sshbuf_new()) == NULL)
134 		fatal("%s: sshbuf_new failed", __progname);
135 
136 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
137 	    (r = sshbuf_put_string(resp, sig, siglen)) != 0)
138 		fatal_r(r, "%s: compose", __progname);
139  out:
140 	sshkey_free(key);
141 	sshbuf_free(kbuf);
142 	free(provider);
143 	if (sig != NULL)
144 		freezero(sig, siglen);
145 	if (pin != NULL)
146 		freezero(pin, strlen(pin));
147 	return resp;
148 }
149 
150 static struct sshbuf *
151 process_enroll(struct sshbuf *req)
152 {
153 	int r;
154 	u_int type;
155 	char *provider, *application, *pin, *device, *userid;
156 	uint8_t flags;
157 	struct sshbuf *challenge, *attest, *kbuf, *resp;
158 	struct sshkey *key;
159 
160 	if ((attest = sshbuf_new()) == NULL ||
161 	    (kbuf = sshbuf_new()) == NULL)
162 		fatal("%s: sshbuf_new failed", __progname);
163 
164 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
165 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
166 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
167 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
168 	    (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
169 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
170 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
171 	    (r = sshbuf_froms(req, &challenge)) != 0)
172 		fatal_r(r, "%s: parse", __progname);
173 	if (sshbuf_len(req) != 0)
174 		fatal("%s: trailing data in request", __progname);
175 
176 	if (type > INT_MAX)
177 		fatal("%s: bad type %u", __progname, type);
178 	if (sshbuf_len(challenge) == 0) {
179 		sshbuf_free(challenge);
180 		challenge = NULL;
181 	}
182 	null_empty(&device);
183 	null_empty(&userid);
184 	null_empty(&pin);
185 
186 	if ((r = sshsk_enroll((int)type, provider, device, application, userid,
187 	    flags, pin, challenge, &key, attest)) != 0) {
188 		resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
189 		goto out;
190 	}
191 
192 	if ((resp = sshbuf_new()) == NULL)
193 		fatal("%s: sshbuf_new failed", __progname);
194 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
195 		fatal_r(r, "%s: encode key", __progname);
196 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
197 	    (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
198 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
199 		fatal_r(r, "%s: compose", __progname);
200 
201  out:
202 	sshkey_free(key);
203 	sshbuf_free(kbuf);
204 	sshbuf_free(attest);
205 	sshbuf_free(challenge);
206 	free(provider);
207 	free(application);
208 	if (pin != NULL)
209 		freezero(pin, strlen(pin));
210 
211 	return resp;
212 }
213 
214 static struct sshbuf *
215 process_load_resident(struct sshbuf *req)
216 {
217 	int r;
218 	char *provider, *pin, *device;
219 	struct sshbuf *kbuf, *resp;
220 	struct sshsk_resident_key **srks = NULL;
221 	size_t nsrks = 0, i;
222 	u_int flags;
223 
224 	if ((kbuf = sshbuf_new()) == NULL)
225 		fatal("%s: sshbuf_new failed", __progname);
226 
227 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
228 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
229 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
230 	    (r = sshbuf_get_u32(req, &flags)) != 0)
231 		fatal_r(r, "%s: parse", __progname);
232 	if (sshbuf_len(req) != 0)
233 		fatal("%s: trailing data in request", __progname);
234 
235 	null_empty(&device);
236 	null_empty(&pin);
237 
238 	if ((r = sshsk_load_resident(provider, device, pin, flags,
239 	    &srks, &nsrks)) != 0) {
240 		resp = reply_error(r, "sshsk_load_resident failed: %s",
241 		    ssh_err(r));
242 		goto out;
243 	}
244 
245 	if ((resp = sshbuf_new()) == NULL)
246 		fatal("%s: sshbuf_new failed", __progname);
247 
248 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
249 		fatal_r(r, "%s: compose", __progname);
250 
251 	for (i = 0; i < nsrks; i++) {
252 		debug_f("key %zu %s %s uidlen %zu", i,
253 		    sshkey_type(srks[i]->key), srks[i]->key->sk_application,
254 		    srks[i]->user_id_len);
255 		sshbuf_reset(kbuf);
256 		if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
257 			fatal_r(r, "%s: encode key", __progname);
258 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
259 		    (r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
260 		    (r = sshbuf_put_string(resp, srks[i]->user_id,
261 		    srks[i]->user_id_len)) != 0)
262 			fatal_r(r, "%s: compose key", __progname);
263 	}
264 
265  out:
266 	sshsk_free_resident_keys(srks, nsrks);
267 	sshbuf_free(kbuf);
268 	free(provider);
269 	free(device);
270 	if (pin != NULL)
271 		freezero(pin, strlen(pin));
272 	return resp;
273 }
274 
275 int
276 main(int argc, char **argv)
277 {
278 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
279 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
280 	struct sshbuf *req, *resp;
281 	int in, out, ch, r, vflag = 0;
282 	u_int rtype, ll = 0;
283 	uint8_t version, log_stderr = 0;
284 
285 	sanitise_stdfd();
286 	log_init(__progname, log_level, log_facility, log_stderr);
287 
288 	while ((ch = getopt(argc, argv, "v")) != -1) {
289 		switch (ch) {
290 		case 'v':
291 			vflag = 1;
292 			if (log_level == SYSLOG_LEVEL_ERROR)
293 				log_level = SYSLOG_LEVEL_DEBUG1;
294 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
295 				log_level++;
296 			break;
297 		default:
298 			fprintf(stderr, "usage: %s [-v]\n", __progname);
299 			exit(1);
300 		}
301 	}
302 	log_init(__progname, log_level, log_facility, vflag);
303 
304 	/*
305 	 * Rearrange our file descriptors a little; we don't trust the
306 	 * providers not to fiddle with stdin/out.
307 	 */
308 	closefrom(STDERR_FILENO + 1);
309 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
310 		fatal("%s: dup: %s", __progname, strerror(errno));
311 	close(STDIN_FILENO);
312 	close(STDOUT_FILENO);
313 	sanitise_stdfd(); /* resets to /dev/null */
314 
315 	if ((req = sshbuf_new()) == NULL)
316 		fatal("%s: sshbuf_new failed", __progname);
317 	if (ssh_msg_recv(in, req) < 0)
318 		fatal("ssh_msg_recv failed");
319 	close(in);
320 	debug_f("received message len %zu", sshbuf_len(req));
321 
322 	if ((r = sshbuf_get_u8(req, &version)) != 0)
323 		fatal_r(r, "%s: parse version", __progname);
324 	if (version != SSH_SK_HELPER_VERSION) {
325 		fatal("unsupported version: received %d, expected %d",
326 		    version, SSH_SK_HELPER_VERSION);
327 	}
328 
329 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
330 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
331 	    (r = sshbuf_get_u32(req, &ll)) != 0)
332 		fatal_r(r, "%s: parse", __progname);
333 
334 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
335 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
336 
337 	switch (rtype) {
338 	case SSH_SK_HELPER_SIGN:
339 		resp = process_sign(req);
340 		break;
341 	case SSH_SK_HELPER_ENROLL:
342 		resp = process_enroll(req);
343 		break;
344 	case SSH_SK_HELPER_LOAD_RESIDENT:
345 		resp = process_load_resident(req);
346 		break;
347 	default:
348 		fatal("%s: unsupported request type %u", __progname, rtype);
349 	}
350 	sshbuf_free(req);
351 	debug_f("reply len %zu", sshbuf_len(resp));
352 
353 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
354 		fatal("ssh_msg_send failed");
355 	sshbuf_free(resp);
356 	close(out);
357 
358 	return (0);
359 }
360