xref: /freebsd/crypto/openssh/ssh-sk-helper.c (revision 2574974648c68c738aec3ff96644d888d7913a37)
1 /* $OpenBSD: ssh-sk-helper.c,v 1.15 2025/07/24 05:44:55 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 "ssherr.h"
47 #include "ssh-sk.h"
48 #include "ssh-pkcs11.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 *
reply_error(int r,char * fmt,...)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
null_empty(char ** s)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 /* stubs */
92 int
pkcs11_sign(struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,const char * alg,const char * sk_provider,const char * sk_pin,u_int compat)93 pkcs11_sign(struct sshkey *key,
94     u_char **sigp, size_t *lenp,
95     const u_char *data, size_t datalen,
96     const char *alg, const char *sk_provider,
97     const char *sk_pin, u_int compat)
98 {
99 	return SSH_ERR_INTERNAL_ERROR;
100 }
101 
102 void
pkcs11_key_free(struct sshkey * key)103 pkcs11_key_free(struct sshkey *key)
104 {
105 }
106 
107 static struct sshbuf *
process_sign(struct sshbuf * req)108 process_sign(struct sshbuf *req)
109 {
110 	int r = SSH_ERR_INTERNAL_ERROR;
111 	struct sshbuf *resp, *kbuf;
112 	struct sshkey *key = NULL;
113 	uint32_t compat;
114 	const u_char *message;
115 	u_char *sig = NULL;
116 	size_t msglen, siglen = 0;
117 	char *provider = NULL, *pin = NULL;
118 
119 	if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
120 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
121 	    (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
122 	    (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
123 	    (r = sshbuf_get_u32(req, &compat)) != 0 ||
124 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
125 		fatal_r(r, "%s: parse", __progname);
126 	if (sshbuf_len(req) != 0)
127 		fatal("%s: trailing data in request", __progname);
128 
129 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
130 		fatal_r(r, "%s: Unable to parse private key", __progname);
131 	if (!sshkey_is_sk(key)) {
132 		fatal("%s: Unsupported key type %s",
133 		    __progname, sshkey_ssh_name(key));
134 	}
135 
136 	debug_f("ready to sign with key %s, provider %s: "
137 	    "msg len %zu, compat 0x%lx", sshkey_type(key),
138 	    provider, msglen, (u_long)compat);
139 
140 	null_empty(&pin);
141 
142 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
143 	    message, msglen, compat, pin)) != 0) {
144 		resp = reply_error(r, "Signing failed: %s", ssh_err(r));
145 		goto out;
146 	}
147 
148 	if ((resp = sshbuf_new()) == NULL)
149 		fatal("%s: sshbuf_new failed", __progname);
150 
151 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
152 	    (r = sshbuf_put_string(resp, sig, siglen)) != 0)
153 		fatal_r(r, "%s: compose", __progname);
154  out:
155 	sshkey_free(key);
156 	sshbuf_free(kbuf);
157 	free(provider);
158 	if (sig != NULL)
159 		freezero(sig, siglen);
160 	if (pin != NULL)
161 		freezero(pin, strlen(pin));
162 	return resp;
163 }
164 
165 static struct sshbuf *
process_enroll(struct sshbuf * req)166 process_enroll(struct sshbuf *req)
167 {
168 	int r;
169 	u_int type;
170 	char *provider, *application, *pin, *device, *userid;
171 	uint8_t flags;
172 	struct sshbuf *challenge, *attest, *kbuf, *resp;
173 	struct sshkey *key;
174 
175 	if ((attest = sshbuf_new()) == NULL ||
176 	    (kbuf = sshbuf_new()) == NULL)
177 		fatal("%s: sshbuf_new failed", __progname);
178 
179 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
180 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
181 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
182 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
183 	    (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
184 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
185 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
186 	    (r = sshbuf_froms(req, &challenge)) != 0)
187 		fatal_r(r, "%s: parse", __progname);
188 	if (sshbuf_len(req) != 0)
189 		fatal("%s: trailing data in request", __progname);
190 
191 	if (type > INT_MAX)
192 		fatal("%s: bad type %u", __progname, type);
193 	if (sshbuf_len(challenge) == 0) {
194 		sshbuf_free(challenge);
195 		challenge = NULL;
196 	}
197 	null_empty(&device);
198 	null_empty(&userid);
199 	null_empty(&pin);
200 
201 	if ((r = sshsk_enroll((int)type, provider, device, application, userid,
202 	    flags, pin, challenge, &key, attest)) != 0) {
203 		resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
204 		goto out;
205 	}
206 
207 	if ((resp = sshbuf_new()) == NULL)
208 		fatal("%s: sshbuf_new failed", __progname);
209 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
210 		fatal_r(r, "%s: encode key", __progname);
211 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
212 	    (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
213 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
214 		fatal_r(r, "%s: compose", __progname);
215 
216  out:
217 	sshkey_free(key);
218 	sshbuf_free(kbuf);
219 	sshbuf_free(attest);
220 	sshbuf_free(challenge);
221 	free(provider);
222 	free(application);
223 	if (pin != NULL)
224 		freezero(pin, strlen(pin));
225 
226 	return resp;
227 }
228 
229 static struct sshbuf *
process_load_resident(struct sshbuf * req)230 process_load_resident(struct sshbuf *req)
231 {
232 	int r;
233 	char *provider, *pin, *device;
234 	struct sshbuf *kbuf, *resp;
235 	struct sshsk_resident_key **srks = NULL;
236 	size_t nsrks = 0, i;
237 	u_int flags;
238 
239 	if ((kbuf = sshbuf_new()) == NULL)
240 		fatal("%s: sshbuf_new failed", __progname);
241 
242 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
243 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
244 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
245 	    (r = sshbuf_get_u32(req, &flags)) != 0)
246 		fatal_r(r, "%s: parse", __progname);
247 	if (sshbuf_len(req) != 0)
248 		fatal("%s: trailing data in request", __progname);
249 
250 	null_empty(&device);
251 	null_empty(&pin);
252 
253 	if ((r = sshsk_load_resident(provider, device, pin, flags,
254 	    &srks, &nsrks)) != 0) {
255 		resp = reply_error(r, "sshsk_load_resident failed: %s",
256 		    ssh_err(r));
257 		goto out;
258 	}
259 
260 	if ((resp = sshbuf_new()) == NULL)
261 		fatal("%s: sshbuf_new failed", __progname);
262 
263 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
264 		fatal_r(r, "%s: compose", __progname);
265 
266 	for (i = 0; i < nsrks; i++) {
267 		debug_f("key %zu %s %s uidlen %zu", i,
268 		    sshkey_type(srks[i]->key), srks[i]->key->sk_application,
269 		    srks[i]->user_id_len);
270 		sshbuf_reset(kbuf);
271 		if ((r = sshkey_private_serialize(srks[i]->key, kbuf)) != 0)
272 			fatal_r(r, "%s: encode key", __progname);
273 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
274 		    (r = sshbuf_put_cstring(resp, "")) != 0 || /* comment */
275 		    (r = sshbuf_put_string(resp, srks[i]->user_id,
276 		    srks[i]->user_id_len)) != 0)
277 			fatal_r(r, "%s: compose key", __progname);
278 	}
279 
280  out:
281 	sshsk_free_resident_keys(srks, nsrks);
282 	sshbuf_free(kbuf);
283 	free(provider);
284 	free(device);
285 	if (pin != NULL)
286 		freezero(pin, strlen(pin));
287 	return resp;
288 }
289 
290 int
main(int argc,char ** argv)291 main(int argc, char **argv)
292 {
293 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
294 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
295 	struct sshbuf *req, *resp;
296 	int in, out, ch, r, vflag = 0;
297 	u_int rtype, ll = 0;
298 	uint8_t version, log_stderr = 0;
299 
300 	sanitise_stdfd();
301 	log_init(__progname, log_level, log_facility, log_stderr);
302 
303 	while ((ch = getopt(argc, argv, "v")) != -1) {
304 		switch (ch) {
305 		case 'v':
306 			vflag = 1;
307 			if (log_level == SYSLOG_LEVEL_ERROR)
308 				log_level = SYSLOG_LEVEL_DEBUG1;
309 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
310 				log_level++;
311 			break;
312 		default:
313 			fprintf(stderr, "usage: %s [-v]\n", __progname);
314 			exit(1);
315 		}
316 	}
317 	log_init(__progname, log_level, log_facility, vflag);
318 
319 	/*
320 	 * Rearrange our file descriptors a little; we don't trust the
321 	 * providers not to fiddle with stdin/out.
322 	 */
323 	closefrom(STDERR_FILENO + 1);
324 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
325 		fatal("%s: dup: %s", __progname, strerror(errno));
326 	close(STDIN_FILENO);
327 	close(STDOUT_FILENO);
328 	sanitise_stdfd(); /* resets to /dev/null */
329 
330 	if ((req = sshbuf_new()) == NULL)
331 		fatal("%s: sshbuf_new failed", __progname);
332 	if (ssh_msg_recv(in, req) < 0)
333 		fatal("ssh_msg_recv failed");
334 	close(in);
335 	debug_f("received message len %zu", sshbuf_len(req));
336 
337 	if ((r = sshbuf_get_u8(req, &version)) != 0)
338 		fatal_r(r, "%s: parse version", __progname);
339 	if (version != SSH_SK_HELPER_VERSION) {
340 		fatal("unsupported version: received %d, expected %d",
341 		    version, SSH_SK_HELPER_VERSION);
342 	}
343 
344 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
345 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
346 	    (r = sshbuf_get_u32(req, &ll)) != 0)
347 		fatal_r(r, "%s: parse", __progname);
348 
349 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
350 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
351 
352 	switch (rtype) {
353 	case SSH_SK_HELPER_SIGN:
354 		resp = process_sign(req);
355 		break;
356 	case SSH_SK_HELPER_ENROLL:
357 		resp = process_enroll(req);
358 		break;
359 	case SSH_SK_HELPER_LOAD_RESIDENT:
360 		resp = process_load_resident(req);
361 		break;
362 	default:
363 		fatal("%s: unsupported request type %u", __progname, rtype);
364 	}
365 	sshbuf_free(req);
366 	debug_f("reply len %zu", sshbuf_len(resp));
367 
368 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
369 		fatal("ssh_msg_send failed");
370 	sshbuf_free(resp);
371 	close(out);
372 
373 	return (0);
374 }
375 #else /* ENABLE_SK */
376 
377 int
main(int argc,char ** argv)378 main(int argc, char **argv)
379 {
380 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
381 	return -1;
382 }
383 #endif /* ENABLE_SK */
384