ssh-sk-client.c revision 1.2 1 1.2 christos /* $NetBSD: ssh-sk-client.c,v 1.2 2020/02/27 00:24:40 christos Exp $ */
2 1.1 christos /* $OpenBSD: ssh-sk-client.c,v 1.7 2020/01/23 07:10:22 dtucker Exp $ */
3 1.1 christos /*
4 1.1 christos * Copyright (c) 2019 Google LLC
5 1.1 christos *
6 1.1 christos * Permission to use, copy, modify, and distribute this software for any
7 1.1 christos * purpose with or without fee is hereby granted, provided that the above
8 1.1 christos * copyright notice and this permission notice appear in all copies.
9 1.1 christos *
10 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 1.1 christos */
18 1.2 christos #include "includes.h"
19 1.2 christos __RCSID("$NetBSD: ssh-sk-client.c,v 1.2 2020/02/27 00:24:40 christos Exp $");
20 1.1 christos
21 1.1 christos #include <sys/types.h>
22 1.1 christos #include <sys/socket.h>
23 1.1 christos #include <sys/wait.h>
24 1.1 christos
25 1.1 christos #include <fcntl.h>
26 1.1 christos #include <limits.h>
27 1.1 christos #include <errno.h>
28 1.1 christos #include <signal.h>
29 1.1 christos #include <stdarg.h>
30 1.1 christos #include <stdio.h>
31 1.1 christos #include <stdlib.h>
32 1.1 christos #include <string.h>
33 1.1 christos #include <unistd.h>
34 1.1 christos
35 1.1 christos #include "log.h"
36 1.1 christos #include "ssherr.h"
37 1.1 christos #include "sshbuf.h"
38 1.1 christos #include "sshkey.h"
39 1.1 christos #include "msg.h"
40 1.1 christos #include "digest.h"
41 1.1 christos #include "pathnames.h"
42 1.1 christos #include "ssh-sk.h"
43 1.1 christos #include "misc.h"
44 1.1 christos
45 1.1 christos /* #define DEBUG_SK 1 */
46 1.1 christos
47 1.1 christos static int
48 1.1 christos start_helper(int *fdp, pid_t *pidp, void (**osigchldp)(int))
49 1.1 christos {
50 1.1 christos void (*osigchld)(int);
51 1.1 christos int oerrno, pair[2], r = SSH_ERR_INTERNAL_ERROR;
52 1.1 christos pid_t pid;
53 1.2 christos const char *helper, *verbosity = NULL;
54 1.1 christos
55 1.1 christos *fdp = -1;
56 1.1 christos *pidp = 0;
57 1.1 christos *osigchldp = SIG_DFL;
58 1.1 christos
59 1.1 christos helper = getenv("SSH_SK_HELPER");
60 1.1 christos if (helper == NULL || strlen(helper) == 0)
61 1.1 christos helper = _PATH_SSH_SK_HELPER;
62 1.1 christos if (access(helper, X_OK) != 0) {
63 1.1 christos oerrno = errno;
64 1.1 christos error("%s: helper \"%s\" unusable: %s", __func__, helper,
65 1.1 christos strerror(errno));
66 1.1 christos errno = oerrno;
67 1.1 christos return SSH_ERR_SYSTEM_ERROR;
68 1.1 christos }
69 1.1 christos #ifdef DEBUG_SK
70 1.1 christos verbosity = "-vvv";
71 1.1 christos #endif
72 1.1 christos
73 1.1 christos /* Start helper */
74 1.1 christos if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
75 1.1 christos error("socketpair: %s", strerror(errno));
76 1.1 christos return SSH_ERR_SYSTEM_ERROR;
77 1.1 christos }
78 1.1 christos osigchld = ssh_signal(SIGCHLD, SIG_DFL);
79 1.1 christos if ((pid = fork()) == -1) {
80 1.1 christos oerrno = errno;
81 1.1 christos error("fork: %s", strerror(errno));
82 1.1 christos close(pair[0]);
83 1.1 christos close(pair[1]);
84 1.1 christos ssh_signal(SIGCHLD, osigchld);
85 1.1 christos errno = oerrno;
86 1.1 christos return SSH_ERR_SYSTEM_ERROR;
87 1.1 christos }
88 1.1 christos if (pid == 0) {
89 1.1 christos if ((dup2(pair[1], STDIN_FILENO) == -1) ||
90 1.1 christos (dup2(pair[1], STDOUT_FILENO) == -1)) {
91 1.1 christos error("%s: dup2: %s", __func__, ssh_err(r));
92 1.1 christos _exit(1);
93 1.1 christos }
94 1.1 christos close(pair[0]);
95 1.1 christos close(pair[1]);
96 1.1 christos closefrom(STDERR_FILENO + 1);
97 1.1 christos debug("%s: starting %s %s", __func__, helper,
98 1.1 christos verbosity == NULL ? "" : verbosity);
99 1.1 christos execlp(helper, helper, verbosity, (char *)NULL);
100 1.1 christos error("%s: execlp: %s", __func__, strerror(errno));
101 1.1 christos _exit(1);
102 1.1 christos }
103 1.1 christos close(pair[1]);
104 1.1 christos
105 1.1 christos /* success */
106 1.1 christos debug3("%s: started pid=%ld", __func__, (long)pid);
107 1.1 christos *fdp = pair[0];
108 1.1 christos *pidp = pid;
109 1.1 christos *osigchldp = osigchld;
110 1.1 christos return 0;
111 1.1 christos }
112 1.1 christos
113 1.1 christos static int
114 1.1 christos reap_helper(pid_t pid)
115 1.1 christos {
116 1.1 christos int status, oerrno;
117 1.1 christos
118 1.1 christos debug3("%s: pid=%ld", __func__, (long)pid);
119 1.1 christos
120 1.1 christos errno = 0;
121 1.1 christos while (waitpid(pid, &status, 0) == -1) {
122 1.1 christos if (errno == EINTR) {
123 1.1 christos errno = 0;
124 1.1 christos continue;
125 1.1 christos }
126 1.1 christos oerrno = errno;
127 1.1 christos error("%s: waitpid: %s", __func__, strerror(errno));
128 1.1 christos errno = oerrno;
129 1.1 christos return SSH_ERR_SYSTEM_ERROR;
130 1.1 christos }
131 1.1 christos if (!WIFEXITED(status)) {
132 1.1 christos error("%s: helper exited abnormally", __func__);
133 1.1 christos return SSH_ERR_AGENT_FAILURE;
134 1.1 christos } else if (WEXITSTATUS(status) != 0) {
135 1.1 christos error("%s: helper exited with non-zero exit status", __func__);
136 1.1 christos return SSH_ERR_AGENT_FAILURE;
137 1.1 christos }
138 1.1 christos return 0;
139 1.1 christos }
140 1.1 christos
141 1.1 christos static int
142 1.1 christos client_converse(struct sshbuf *msg, struct sshbuf **respp, u_int type)
143 1.1 christos {
144 1.1 christos int oerrno, fd, r2, ll, r = SSH_ERR_INTERNAL_ERROR;
145 1.1 christos u_int rtype, rerr;
146 1.1 christos pid_t pid;
147 1.1 christos u_char version;
148 1.1 christos void (*osigchld)(int);
149 1.1 christos struct sshbuf *req = NULL, *resp = NULL;
150 1.1 christos *respp = NULL;
151 1.1 christos
152 1.1 christos if ((r = start_helper(&fd, &pid, &osigchld)) != 0)
153 1.1 christos return r;
154 1.1 christos
155 1.1 christos if ((req = sshbuf_new()) == NULL || (resp = sshbuf_new()) == NULL) {
156 1.1 christos r = SSH_ERR_ALLOC_FAIL;
157 1.1 christos goto out;
158 1.1 christos }
159 1.1 christos /* Request preamble: type, log_on_stderr, log_level */
160 1.1 christos ll = log_level_get();
161 1.1 christos if ((r = sshbuf_put_u32(req, type)) != 0 ||
162 1.1 christos (r = sshbuf_put_u8(req, log_is_on_stderr() != 0)) != 0 ||
163 1.1 christos (r = sshbuf_put_u32(req, ll < 0 ? 0 : ll)) != 0 ||
164 1.1 christos (r = sshbuf_putb(req, msg)) != 0) {
165 1.1 christos error("%s: build: %s", __func__, ssh_err(r));
166 1.1 christos goto out;
167 1.1 christos }
168 1.1 christos if ((r = ssh_msg_send(fd, SSH_SK_HELPER_VERSION, req)) != 0) {
169 1.1 christos error("%s: send: %s", __func__, ssh_err(r));
170 1.1 christos goto out;
171 1.1 christos }
172 1.1 christos if ((r = ssh_msg_recv(fd, resp)) != 0) {
173 1.1 christos error("%s: receive: %s", __func__, ssh_err(r));
174 1.1 christos goto out;
175 1.1 christos }
176 1.1 christos if ((r = sshbuf_get_u8(resp, &version)) != 0) {
177 1.1 christos error("%s: parse version: %s", __func__, ssh_err(r));
178 1.1 christos goto out;
179 1.1 christos }
180 1.1 christos if (version != SSH_SK_HELPER_VERSION) {
181 1.1 christos error("%s: unsupported version: got %u, expected %u",
182 1.1 christos __func__, version, SSH_SK_HELPER_VERSION);
183 1.1 christos r = SSH_ERR_INVALID_FORMAT;
184 1.1 christos goto out;
185 1.1 christos }
186 1.1 christos if ((r = sshbuf_get_u32(resp, &rtype)) != 0) {
187 1.1 christos error("%s: parse message type: %s", __func__, ssh_err(r));
188 1.1 christos goto out;
189 1.1 christos }
190 1.1 christos if (rtype == SSH_SK_HELPER_ERROR) {
191 1.1 christos if ((r = sshbuf_get_u32(resp, &rerr)) != 0) {
192 1.1 christos error("%s: parse error: %s", __func__, ssh_err(r));
193 1.1 christos goto out;
194 1.1 christos }
195 1.1 christos debug("%s: helper returned error -%u", __func__, rerr);
196 1.1 christos /* OpenSSH error values are negative; encoded as -err on wire */
197 1.1 christos if (rerr == 0 || rerr >= INT_MAX)
198 1.1 christos r = SSH_ERR_INTERNAL_ERROR;
199 1.1 christos else
200 1.1 christos r = -(int)rerr;
201 1.1 christos goto out;
202 1.1 christos } else if (rtype != type) {
203 1.1 christos error("%s: helper returned incorrect message type %u, "
204 1.1 christos "expecting %u", __func__, rtype, type);
205 1.1 christos r = SSH_ERR_INTERNAL_ERROR;
206 1.1 christos goto out;
207 1.1 christos }
208 1.1 christos /* success */
209 1.1 christos r = 0;
210 1.1 christos out:
211 1.1 christos oerrno = errno;
212 1.1 christos close(fd);
213 1.1 christos if ((r2 = reap_helper(pid)) != 0) {
214 1.1 christos if (r == 0) {
215 1.1 christos r = r2;
216 1.1 christos oerrno = errno;
217 1.1 christos }
218 1.1 christos }
219 1.1 christos if (r == 0) {
220 1.1 christos *respp = resp;
221 1.1 christos resp = NULL;
222 1.1 christos }
223 1.1 christos sshbuf_free(req);
224 1.1 christos sshbuf_free(resp);
225 1.1 christos ssh_signal(SIGCHLD, osigchld);
226 1.1 christos errno = oerrno;
227 1.1 christos return r;
228 1.1 christos
229 1.1 christos }
230 1.1 christos
231 1.1 christos int
232 1.1 christos sshsk_sign(const char *provider, struct sshkey *key,
233 1.1 christos u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
234 1.1 christos u_int compat, const char *pin)
235 1.1 christos {
236 1.1 christos int oerrno, r = SSH_ERR_INTERNAL_ERROR;
237 1.1 christos char *fp = NULL;
238 1.1 christos struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
239 1.1 christos
240 1.1 christos *sigp = NULL;
241 1.1 christos *lenp = 0;
242 1.1 christos
243 1.1 christos if ((kbuf = sshbuf_new()) == NULL ||
244 1.1 christos (req = sshbuf_new()) == NULL) {
245 1.1 christos r = SSH_ERR_ALLOC_FAIL;
246 1.1 christos goto out;
247 1.1 christos }
248 1.1 christos
249 1.1 christos if ((r = sshkey_private_serialize(key, kbuf)) != 0) {
250 1.1 christos error("%s: serialize private key: %s", __func__, ssh_err(r));
251 1.1 christos goto out;
252 1.1 christos }
253 1.1 christos if ((r = sshbuf_put_stringb(req, kbuf)) != 0 ||
254 1.1 christos (r = sshbuf_put_cstring(req, provider)) != 0 ||
255 1.1 christos (r = sshbuf_put_string(req, data, datalen)) != 0 ||
256 1.1 christos (r = sshbuf_put_cstring(req, NULL)) != 0 || /* alg */
257 1.1 christos (r = sshbuf_put_u32(req, compat)) != 0 ||
258 1.1 christos (r = sshbuf_put_cstring(req, pin)) != 0) {
259 1.1 christos error("%s: compose: %s", __func__, ssh_err(r));
260 1.1 christos goto out;
261 1.1 christos }
262 1.1 christos
263 1.1 christos if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
264 1.1 christos SSH_FP_DEFAULT)) == NULL) {
265 1.1 christos error("%s: sshkey_fingerprint failed", __func__);
266 1.1 christos r = SSH_ERR_ALLOC_FAIL;
267 1.1 christos goto out;
268 1.1 christos }
269 1.1 christos if ((r = client_converse(req, &resp, SSH_SK_HELPER_SIGN)) != 0)
270 1.1 christos goto out;
271 1.1 christos
272 1.1 christos if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) {
273 1.1 christos error("%s: parse signature: %s", __func__, ssh_err(r));
274 1.1 christos r = SSH_ERR_INVALID_FORMAT;
275 1.1 christos goto out;
276 1.1 christos }
277 1.1 christos if (sshbuf_len(resp) != 0) {
278 1.1 christos error("%s: trailing data in response", __func__);
279 1.1 christos r = SSH_ERR_INVALID_FORMAT;
280 1.1 christos goto out;
281 1.1 christos }
282 1.1 christos /* success */
283 1.1 christos r = 0;
284 1.1 christos out:
285 1.1 christos oerrno = errno;
286 1.1 christos if (r != 0) {
287 1.1 christos freezero(*sigp, *lenp);
288 1.1 christos *sigp = NULL;
289 1.1 christos *lenp = 0;
290 1.1 christos }
291 1.1 christos sshbuf_free(kbuf);
292 1.1 christos sshbuf_free(req);
293 1.1 christos sshbuf_free(resp);
294 1.1 christos errno = oerrno;
295 1.1 christos return r;
296 1.1 christos }
297 1.1 christos
298 1.1 christos int
299 1.1 christos sshsk_enroll(int type, const char *provider_path, const char *device,
300 1.1 christos const char *application, const char *userid, uint8_t flags,
301 1.1 christos const char *pin, struct sshbuf *challenge_buf,
302 1.1 christos struct sshkey **keyp, struct sshbuf *attest)
303 1.1 christos {
304 1.1 christos int oerrno, r = SSH_ERR_INTERNAL_ERROR;
305 1.1 christos struct sshbuf *kbuf = NULL, *abuf = NULL, *req = NULL, *resp = NULL;
306 1.1 christos struct sshkey *key = NULL;
307 1.1 christos
308 1.1 christos *keyp = NULL;
309 1.1 christos if (attest != NULL)
310 1.1 christos sshbuf_reset(attest);
311 1.1 christos
312 1.1 christos if (type < 0)
313 1.1 christos return SSH_ERR_INVALID_ARGUMENT;
314 1.1 christos
315 1.1 christos if ((abuf = sshbuf_new()) == NULL ||
316 1.1 christos (kbuf = sshbuf_new()) == NULL ||
317 1.1 christos (req = sshbuf_new()) == NULL) {
318 1.1 christos r = SSH_ERR_ALLOC_FAIL;
319 1.1 christos goto out;
320 1.1 christos }
321 1.1 christos
322 1.1 christos if ((r = sshbuf_put_u32(req, (u_int)type)) != 0 ||
323 1.1 christos (r = sshbuf_put_cstring(req, provider_path)) != 0 ||
324 1.1 christos (r = sshbuf_put_cstring(req, device)) != 0 ||
325 1.1 christos (r = sshbuf_put_cstring(req, application)) != 0 ||
326 1.1 christos (r = sshbuf_put_cstring(req, userid)) != 0 ||
327 1.1 christos (r = sshbuf_put_u8(req, flags)) != 0 ||
328 1.1 christos (r = sshbuf_put_cstring(req, pin)) != 0 ||
329 1.1 christos (r = sshbuf_put_stringb(req, challenge_buf)) != 0) {
330 1.1 christos error("%s: compose: %s", __func__, ssh_err(r));
331 1.1 christos goto out;
332 1.1 christos }
333 1.1 christos
334 1.1 christos if ((r = client_converse(req, &resp, SSH_SK_HELPER_ENROLL)) != 0)
335 1.1 christos goto out;
336 1.1 christos
337 1.1 christos if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
338 1.1 christos (r = sshbuf_get_stringb(resp, abuf)) != 0) {
339 1.1 christos error("%s: parse signature: %s", __func__, ssh_err(r));
340 1.1 christos r = SSH_ERR_INVALID_FORMAT;
341 1.1 christos goto out;
342 1.1 christos }
343 1.1 christos if (sshbuf_len(resp) != 0) {
344 1.1 christos error("%s: trailing data in response", __func__);
345 1.1 christos r = SSH_ERR_INVALID_FORMAT;
346 1.1 christos goto out;
347 1.1 christos }
348 1.1 christos if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
349 1.1 christos error("Unable to parse private key: %s", ssh_err(r));
350 1.1 christos goto out;
351 1.1 christos }
352 1.1 christos if (attest != NULL && (r = sshbuf_putb(attest, abuf)) != 0) {
353 1.1 christos error("%s: buffer error: %s", __func__, ssh_err(r));
354 1.1 christos goto out;
355 1.1 christos }
356 1.1 christos
357 1.1 christos /* success */
358 1.1 christos r = 0;
359 1.1 christos *keyp = key;
360 1.1 christos key = NULL;
361 1.1 christos out:
362 1.1 christos oerrno = errno;
363 1.1 christos sshkey_free(key);
364 1.1 christos sshbuf_free(kbuf);
365 1.1 christos sshbuf_free(abuf);
366 1.1 christos sshbuf_free(req);
367 1.1 christos sshbuf_free(resp);
368 1.1 christos errno = oerrno;
369 1.1 christos return r;
370 1.1 christos }
371 1.1 christos
372 1.1 christos int
373 1.1 christos sshsk_load_resident(const char *provider_path, const char *device,
374 1.1 christos const char *pin, struct sshkey ***keysp, size_t *nkeysp)
375 1.1 christos {
376 1.1 christos int oerrno, r = SSH_ERR_INTERNAL_ERROR;
377 1.1 christos struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
378 1.1 christos struct sshkey *key = NULL, **keys = NULL, **tmp;
379 1.1 christos size_t i, nkeys = 0;
380 1.1 christos
381 1.1 christos *keysp = NULL;
382 1.1 christos *nkeysp = 0;
383 1.1 christos
384 1.1 christos if ((resp = sshbuf_new()) == NULL ||
385 1.1 christos (kbuf = sshbuf_new()) == NULL ||
386 1.1 christos (req = sshbuf_new()) == NULL) {
387 1.1 christos r = SSH_ERR_ALLOC_FAIL;
388 1.1 christos goto out;
389 1.1 christos }
390 1.1 christos
391 1.1 christos if ((r = sshbuf_put_cstring(req, provider_path)) != 0 ||
392 1.1 christos (r = sshbuf_put_cstring(req, device)) != 0 ||
393 1.1 christos (r = sshbuf_put_cstring(req, pin)) != 0) {
394 1.1 christos error("%s: compose: %s", __func__, ssh_err(r));
395 1.1 christos goto out;
396 1.1 christos }
397 1.1 christos
398 1.1 christos if ((r = client_converse(req, &resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
399 1.1 christos goto out;
400 1.1 christos
401 1.1 christos while (sshbuf_len(resp) != 0) {
402 1.1 christos /* key, comment */
403 1.1 christos if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
404 1.1 christos (r = sshbuf_get_cstring(resp, NULL, NULL)) != 0) {
405 1.1 christos error("%s: parse signature: %s", __func__, ssh_err(r));
406 1.1 christos r = SSH_ERR_INVALID_FORMAT;
407 1.1 christos goto out;
408 1.1 christos }
409 1.1 christos if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
410 1.1 christos error("Unable to parse private key: %s", ssh_err(r));
411 1.1 christos goto out;
412 1.1 christos }
413 1.1 christos if ((tmp = recallocarray(keys, nkeys, nkeys + 1,
414 1.1 christos sizeof(*keys))) == NULL) {
415 1.1 christos error("%s: recallocarray keys failed", __func__);
416 1.1 christos goto out;
417 1.1 christos }
418 1.1 christos debug("%s: keys[%zu]: %s %s", __func__,
419 1.1 christos nkeys, sshkey_type(key), key->sk_application);
420 1.1 christos keys = tmp;
421 1.1 christos keys[nkeys++] = key;
422 1.1 christos key = NULL;
423 1.1 christos }
424 1.1 christos
425 1.1 christos /* success */
426 1.1 christos r = 0;
427 1.1 christos *keysp = keys;
428 1.1 christos *nkeysp = nkeys;
429 1.1 christos keys = NULL;
430 1.1 christos nkeys = 0;
431 1.1 christos out:
432 1.1 christos oerrno = errno;
433 1.1 christos for (i = 0; i < nkeys; i++)
434 1.1 christos sshkey_free(keys[i]);
435 1.1 christos free(keys);
436 1.1 christos sshkey_free(key);
437 1.1 christos sshbuf_free(kbuf);
438 1.1 christos sshbuf_free(req);
439 1.1 christos sshbuf_free(resp);
440 1.1 christos errno = oerrno;
441 1.1 christos return r;
442 1.1 christos }
443