xref: /openssh-portable/ssh-sk-helper.c (revision 27753a8e)
1 /* $OpenBSD: ssh-sk-helper.c,v 1.5 2019/12/30 09:21:59 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 *
54 process_sign(struct sshbuf *req)
55 {
56 	int r = SSH_ERR_INTERNAL_ERROR;
57 	struct sshbuf *resp, *kbuf;
58 	struct sshkey *key;
59 	uint32_t compat;
60 	const u_char *message;
61 	u_char *sig;
62 	size_t msglen, siglen;
63 	char *provider;
64 
65 	if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
66 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
67 	    (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
68 	    (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
69 	    (r = sshbuf_get_u32(req, &compat)) != 0)
70 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
71 	if (sshbuf_len(req) != 0)
72 		fatal("%s: trailing data in request", __progname);
73 
74 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
75 		fatal("Unable to parse private key: %s", ssh_err(r));
76 	if (!sshkey_is_sk(key))
77 		fatal("Unsupported key type %s", sshkey_ssh_name(key));
78 
79 	debug("%s: ready to sign with key %s, provider %s: "
80 	    "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
81 	    provider, msglen, (u_long)compat);
82 
83 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
84 	    message, msglen, compat)) != 0)
85 		fatal("Signing failed: %s", ssh_err(r));
86 
87 	if ((resp = sshbuf_new()) == NULL)
88 		fatal("%s: sshbuf_new failed", __progname);
89 
90 	if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
91 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
92 
93 	sshbuf_free(kbuf);
94 	free(provider);
95 
96 	return resp;
97 }
98 
99 static struct sshbuf *
100 process_enroll(struct sshbuf *req)
101 {
102 	int r;
103 	u_int type;
104 	char *provider;
105 	char *application;
106 	uint8_t flags;
107 	struct sshbuf *challenge, *attest, *kbuf, *resp;
108 	struct sshkey *key;
109 
110 	if ((resp = sshbuf_new()) == NULL ||
111 	    (attest = sshbuf_new()) == NULL ||
112 	    (kbuf = sshbuf_new()) == NULL)
113 		fatal("%s: sshbuf_new failed", __progname);
114 
115 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
116 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
117 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
118 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
119 	    (r = sshbuf_froms(req, &challenge)) != 0)
120 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
121 	if (sshbuf_len(req) != 0)
122 		fatal("%s: trailing data in request", __progname);
123 
124 	if (type > INT_MAX)
125 		fatal("%s: bad type %u", __progname, type);
126 	if (sshbuf_len(challenge) == 0) {
127 		sshbuf_free(challenge);
128 		challenge = NULL;
129 	}
130 
131 	if ((r = sshsk_enroll((int)type, provider, application, flags,
132 	    challenge, &key, attest)) != 0)
133 		fatal("%s: sshsk_enroll failed: %s", __progname, ssh_err(r));
134 
135 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
136 		fatal("%s: serialize private key: %s", __progname, ssh_err(r));
137 	if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
138 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
139 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
140 
141 	sshkey_free(key);
142 	sshbuf_free(kbuf);
143 	sshbuf_free(attest);
144 	sshbuf_free(challenge);
145 	free(provider);
146 	free(application);
147 
148 	return resp;
149 }
150 
151 static struct sshbuf *
152 process_load_resident(struct sshbuf *req)
153 {
154 	int r;
155 	char *provider, *pin;
156 	struct sshbuf *kbuf, *resp;
157 	struct sshkey **keys = NULL;
158 	size_t nkeys = 0, i;
159 
160 	if ((resp = sshbuf_new()) == NULL ||
161 	    (kbuf = sshbuf_new()) == NULL)
162 		fatal("%s: sshbuf_new failed", __progname);
163 
164 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
165 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
166 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
167 	if (sshbuf_len(req) != 0)
168 		fatal("%s: trailing data in request", __progname);
169 
170 	if ((r = sshsk_load_resident(provider, pin, &keys, &nkeys)) != 0)
171 		fatal("%s: sshsk_load_resident failed: %s",
172 		    __progname, ssh_err(r));
173 
174 	for (i = 0; i < nkeys; i++) {
175 		debug("%s: key %zu %s %s", __func__, i,
176 		    sshkey_type(keys[i]), keys[i]->sk_application);
177 		sshbuf_reset(kbuf);
178 		if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0)
179 			fatal("%s: serialize private key: %s",
180 			    __progname, ssh_err(r));
181 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
182 		    (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */
183 			fatal("%s: buffer error: %s", __progname, ssh_err(r));
184 	}
185 
186 	for (i = 0; i < nkeys; i++)
187 		sshkey_free(keys[i]);
188 	free(keys);
189 	sshbuf_free(kbuf);
190 	free(provider);
191 	freezero(pin, strlen(pin));
192 	return resp;
193 }
194 
195 int
196 main(int argc, char **argv)
197 {
198 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
199 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
200 	struct sshbuf *req, *resp;
201 	int in, out, ch, r, log_stderr = 0;
202 	u_int rtype;
203 	uint8_t version;
204 
205 	sanitise_stdfd();
206 	log_init(__progname, log_level, log_facility, log_stderr);
207 
208 	while ((ch = getopt(argc, argv, "v")) != -1) {
209 		switch (ch) {
210 		case 'v':
211 			log_stderr = 1;
212 			if (log_level == SYSLOG_LEVEL_ERROR)
213 				log_level = SYSLOG_LEVEL_DEBUG1;
214 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
215 				log_level++;
216 			break;
217 		default:
218 			fprintf(stderr, "usage: %s [-v]\n", __progname);
219 			exit(1);
220 		}
221 	}
222 	log_init(__progname, log_level, log_facility, log_stderr);
223 
224 	/*
225 	 * Rearrange our file descriptors a little; we don't trust the
226 	 * providers not to fiddle with stdin/out.
227 	 */
228 	closefrom(STDERR_FILENO + 1);
229 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
230 		fatal("%s: dup: %s", __progname, strerror(errno));
231 	close(STDIN_FILENO);
232 	close(STDOUT_FILENO);
233 	sanitise_stdfd(); /* resets to /dev/null */
234 
235 	if ((req = sshbuf_new()) == NULL)
236 		fatal("%s: sshbuf_new failed", __progname);
237 	if (ssh_msg_recv(in, req) < 0)
238 		fatal("ssh_msg_recv failed");
239 	close(in);
240 	debug("%s: received message len %zu", __progname, sshbuf_len(req));
241 
242 	if ((r = sshbuf_get_u8(req, &version)) != 0)
243 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
244 	if (version != SSH_SK_HELPER_VERSION) {
245 		fatal("unsupported version: received %d, expected %d",
246 		    version, SSH_SK_HELPER_VERSION);
247 	}
248 
249 	if ((r = sshbuf_get_u32(req, &rtype)) != 0)
250 		fatal("%s: buffer error: %s", __progname, ssh_err(r));
251 
252 	switch (rtype) {
253 	case SSH_SK_HELPER_SIGN:
254 		resp = process_sign(req);
255 		break;
256 	case SSH_SK_HELPER_ENROLL:
257 		resp = process_enroll(req);
258 		break;
259 	case SSH_SK_HELPER_LOAD_RESIDENT:
260 		resp = process_load_resident(req);
261 		break;
262 	default:
263 		fatal("%s: unsupported request type %u", __progname, rtype);
264 	}
265 	sshbuf_free(req);
266 	debug("%s: reply len %zu", __progname, sshbuf_len(resp));
267 
268 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
269 		fatal("ssh_msg_send failed");
270 	sshbuf_free(resp);
271 	close(out);
272 
273 	return (0);
274 }
275 #else /* ENABLE_SK */
276 #include <stdio.h>
277 
278 int
279 main(int argc, char **argv)
280 {
281 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
282 	return -1;
283 }
284 #endif /* ENABLE_SK */
285