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