xref: /openssh-portable/ssh-sk-client.c (revision 31d8d231)
1*31d8d231Sdjm@openbsd.org /* $OpenBSD: ssh-sk-client.c,v 1.9 2021/04/03 06:18:41 djm Exp $ */
2d2143476Sdjm@openbsd.org /*
3d2143476Sdjm@openbsd.org  * Copyright (c) 2019 Google LLC
4d2143476Sdjm@openbsd.org  *
5d2143476Sdjm@openbsd.org  * Permission to use, copy, modify, and distribute this software for any
6d2143476Sdjm@openbsd.org  * purpose with or without fee is hereby granted, provided that the above
7d2143476Sdjm@openbsd.org  * copyright notice and this permission notice appear in all copies.
8d2143476Sdjm@openbsd.org  *
9d2143476Sdjm@openbsd.org  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10d2143476Sdjm@openbsd.org  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11d2143476Sdjm@openbsd.org  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12d2143476Sdjm@openbsd.org  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13d2143476Sdjm@openbsd.org  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14d2143476Sdjm@openbsd.org  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15d2143476Sdjm@openbsd.org  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16d2143476Sdjm@openbsd.org  */
17d2143476Sdjm@openbsd.org 
18a33ab168SDamien Miller #include "includes.h"
19a33ab168SDamien Miller 
20d2143476Sdjm@openbsd.org #include <sys/types.h>
21d2143476Sdjm@openbsd.org #include <sys/socket.h>
22d2143476Sdjm@openbsd.org #include <sys/wait.h>
23d2143476Sdjm@openbsd.org 
24b5fcb0acSdjm@openbsd.org #include <fcntl.h>
25c54cd189Sdjm@openbsd.org #include <limits.h>
26d2143476Sdjm@openbsd.org #include <errno.h>
27d2143476Sdjm@openbsd.org #include <signal.h>
28d2143476Sdjm@openbsd.org #include <stdarg.h>
29d2143476Sdjm@openbsd.org #include <stdio.h>
30d2143476Sdjm@openbsd.org #include <stdlib.h>
31d2143476Sdjm@openbsd.org #include <string.h>
32d2143476Sdjm@openbsd.org #include <unistd.h>
33d2143476Sdjm@openbsd.org 
34d2143476Sdjm@openbsd.org #include "log.h"
35d2143476Sdjm@openbsd.org #include "ssherr.h"
36d2143476Sdjm@openbsd.org #include "sshbuf.h"
37d2143476Sdjm@openbsd.org #include "sshkey.h"
38d2143476Sdjm@openbsd.org #include "msg.h"
39d2143476Sdjm@openbsd.org #include "digest.h"
40d2143476Sdjm@openbsd.org #include "pathnames.h"
41d2143476Sdjm@openbsd.org #include "ssh-sk.h"
423bf2a6acSdtucker@openbsd.org #include "misc.h"
43d2143476Sdjm@openbsd.org 
44d2143476Sdjm@openbsd.org /* #define DEBUG_SK 1 */
45d2143476Sdjm@openbsd.org 
46d2143476Sdjm@openbsd.org static int
start_helper(int * fdp,pid_t * pidp,void (** osigchldp)(int))47d2143476Sdjm@openbsd.org start_helper(int *fdp, pid_t *pidp, void (**osigchldp)(int))
48d2143476Sdjm@openbsd.org {
49d2143476Sdjm@openbsd.org 	void (*osigchld)(int);
50816036f1Sdjm@openbsd.org 	int oerrno, pair[2];
51d2143476Sdjm@openbsd.org 	pid_t pid;
52d2143476Sdjm@openbsd.org 	char *helper, *verbosity = NULL;
53d2143476Sdjm@openbsd.org 
54d2143476Sdjm@openbsd.org 	*fdp = -1;
55d2143476Sdjm@openbsd.org 	*pidp = 0;
56d2143476Sdjm@openbsd.org 	*osigchldp = SIG_DFL;
57d2143476Sdjm@openbsd.org 
58d2143476Sdjm@openbsd.org 	helper = getenv("SSH_SK_HELPER");
59d2143476Sdjm@openbsd.org 	if (helper == NULL || strlen(helper) == 0)
60d2143476Sdjm@openbsd.org 		helper = _PATH_SSH_SK_HELPER;
61b5fcb0acSdjm@openbsd.org 	if (access(helper, X_OK) != 0) {
62b5fcb0acSdjm@openbsd.org 		oerrno = errno;
63816036f1Sdjm@openbsd.org 		error_f("helper \"%s\" unusable: %s", helper, strerror(errno));
64b5fcb0acSdjm@openbsd.org 		errno = oerrno;
65b5fcb0acSdjm@openbsd.org 		return SSH_ERR_SYSTEM_ERROR;
66b5fcb0acSdjm@openbsd.org 	}
67d2143476Sdjm@openbsd.org #ifdef DEBUG_SK
68d2143476Sdjm@openbsd.org 	verbosity = "-vvv";
69d2143476Sdjm@openbsd.org #endif
70d2143476Sdjm@openbsd.org 
71d2143476Sdjm@openbsd.org 	/* Start helper */
72d2143476Sdjm@openbsd.org 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
73d2143476Sdjm@openbsd.org 		error("socketpair: %s", strerror(errno));
74d2143476Sdjm@openbsd.org 		return SSH_ERR_SYSTEM_ERROR;
75d2143476Sdjm@openbsd.org 	}
763bf2a6acSdtucker@openbsd.org 	osigchld = ssh_signal(SIGCHLD, SIG_DFL);
77d2143476Sdjm@openbsd.org 	if ((pid = fork()) == -1) {
78d2143476Sdjm@openbsd.org 		oerrno = errno;
79d2143476Sdjm@openbsd.org 		error("fork: %s", strerror(errno));
80d2143476Sdjm@openbsd.org 		close(pair[0]);
81d2143476Sdjm@openbsd.org 		close(pair[1]);
823bf2a6acSdtucker@openbsd.org 		ssh_signal(SIGCHLD, osigchld);
83d2143476Sdjm@openbsd.org 		errno = oerrno;
84d2143476Sdjm@openbsd.org 		return SSH_ERR_SYSTEM_ERROR;
85d2143476Sdjm@openbsd.org 	}
86d2143476Sdjm@openbsd.org 	if (pid == 0) {
87d2143476Sdjm@openbsd.org 		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
88d2143476Sdjm@openbsd.org 		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
89816036f1Sdjm@openbsd.org 			error_f("dup2: %s", strerror(errno));
90d2143476Sdjm@openbsd.org 			_exit(1);
91d2143476Sdjm@openbsd.org 		}
92d2143476Sdjm@openbsd.org 		close(pair[0]);
93d2143476Sdjm@openbsd.org 		close(pair[1]);
94d2143476Sdjm@openbsd.org 		closefrom(STDERR_FILENO + 1);
95816036f1Sdjm@openbsd.org 		debug_f("starting %s %s", helper,
96d2143476Sdjm@openbsd.org 		    verbosity == NULL ? "" : verbosity);
97d2143476Sdjm@openbsd.org 		execlp(helper, helper, verbosity, (char *)NULL);
98816036f1Sdjm@openbsd.org 		error_f("execlp: %s", strerror(errno));
99d2143476Sdjm@openbsd.org 		_exit(1);
100d2143476Sdjm@openbsd.org 	}
101d2143476Sdjm@openbsd.org 	close(pair[1]);
102d2143476Sdjm@openbsd.org 
103d2143476Sdjm@openbsd.org 	/* success */
104816036f1Sdjm@openbsd.org 	debug3_f("started pid=%ld", (long)pid);
105d2143476Sdjm@openbsd.org 	*fdp = pair[0];
106d2143476Sdjm@openbsd.org 	*pidp = pid;
107d2143476Sdjm@openbsd.org 	*osigchldp = osigchld;
108d2143476Sdjm@openbsd.org 	return 0;
109d2143476Sdjm@openbsd.org }
110d2143476Sdjm@openbsd.org 
111d2143476Sdjm@openbsd.org static int
reap_helper(pid_t pid)112d2143476Sdjm@openbsd.org reap_helper(pid_t pid)
113d2143476Sdjm@openbsd.org {
114d2143476Sdjm@openbsd.org 	int status, oerrno;
115d2143476Sdjm@openbsd.org 
116816036f1Sdjm@openbsd.org 	debug3_f("pid=%ld", (long)pid);
117d2143476Sdjm@openbsd.org 
118d2143476Sdjm@openbsd.org 	errno = 0;
119d2143476Sdjm@openbsd.org 	while (waitpid(pid, &status, 0) == -1) {
120d2143476Sdjm@openbsd.org 		if (errno == EINTR) {
121d2143476Sdjm@openbsd.org 			errno = 0;
122d2143476Sdjm@openbsd.org 			continue;
123d2143476Sdjm@openbsd.org 		}
124d2143476Sdjm@openbsd.org 		oerrno = errno;
125816036f1Sdjm@openbsd.org 		error_f("waitpid: %s", strerror(errno));
126d2143476Sdjm@openbsd.org 		errno = oerrno;
127d2143476Sdjm@openbsd.org 		return SSH_ERR_SYSTEM_ERROR;
128d2143476Sdjm@openbsd.org 	}
129d2143476Sdjm@openbsd.org 	if (!WIFEXITED(status)) {
130816036f1Sdjm@openbsd.org 		error_f("helper exited abnormally");
131d2143476Sdjm@openbsd.org 		return SSH_ERR_AGENT_FAILURE;
132d2143476Sdjm@openbsd.org 	} else if (WEXITSTATUS(status) != 0) {
133816036f1Sdjm@openbsd.org 		error_f("helper exited with non-zero exit status");
134d2143476Sdjm@openbsd.org 		return SSH_ERR_AGENT_FAILURE;
135d2143476Sdjm@openbsd.org 	}
136d2143476Sdjm@openbsd.org 	return 0;
137d2143476Sdjm@openbsd.org }
138d2143476Sdjm@openbsd.org 
139d2143476Sdjm@openbsd.org static int
client_converse(struct sshbuf * msg,struct sshbuf ** respp,u_int type)14057b181eaSdjm@openbsd.org client_converse(struct sshbuf *msg, struct sshbuf **respp, u_int type)
141d2143476Sdjm@openbsd.org {
14257b181eaSdjm@openbsd.org 	int oerrno, fd, r2, ll, r = SSH_ERR_INTERNAL_ERROR;
14357b181eaSdjm@openbsd.org 	u_int rtype, rerr;
144d2143476Sdjm@openbsd.org 	pid_t pid;
145d2143476Sdjm@openbsd.org 	u_char version;
146d2143476Sdjm@openbsd.org 	void (*osigchld)(int);
14757b181eaSdjm@openbsd.org 	struct sshbuf *req = NULL, *resp = NULL;
148d2143476Sdjm@openbsd.org 	*respp = NULL;
149d2143476Sdjm@openbsd.org 
150d2143476Sdjm@openbsd.org 	if ((r = start_helper(&fd, &pid, &osigchld)) != 0)
151d2143476Sdjm@openbsd.org 		return r;
152d2143476Sdjm@openbsd.org 
15357b181eaSdjm@openbsd.org 	if ((req = sshbuf_new()) == NULL || (resp = sshbuf_new()) == NULL) {
154d2143476Sdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
155d2143476Sdjm@openbsd.org 		goto out;
156d2143476Sdjm@openbsd.org 	}
15757b181eaSdjm@openbsd.org 	/* Request preamble: type, log_on_stderr, log_level */
15857b181eaSdjm@openbsd.org 	ll = log_level_get();
15957b181eaSdjm@openbsd.org 	if ((r = sshbuf_put_u32(req, type)) != 0 ||
16057b181eaSdjm@openbsd.org 	    (r = sshbuf_put_u8(req, log_is_on_stderr() != 0)) != 0 ||
16157b181eaSdjm@openbsd.org 	    (r = sshbuf_put_u32(req, ll < 0 ? 0 : ll)) != 0 ||
16257b181eaSdjm@openbsd.org 	    (r = sshbuf_putb(req, msg)) != 0) {
163816036f1Sdjm@openbsd.org 		error_fr(r, "compose");
16457b181eaSdjm@openbsd.org 		goto out;
16557b181eaSdjm@openbsd.org 	}
166d2143476Sdjm@openbsd.org 	if ((r = ssh_msg_send(fd, SSH_SK_HELPER_VERSION, req)) != 0) {
167816036f1Sdjm@openbsd.org 		error_fr(r, "send");
168d2143476Sdjm@openbsd.org 		goto out;
169d2143476Sdjm@openbsd.org 	}
170d2143476Sdjm@openbsd.org 	if ((r = ssh_msg_recv(fd, resp)) != 0) {
171816036f1Sdjm@openbsd.org 		error_fr(r, "receive");
172d2143476Sdjm@openbsd.org 		goto out;
173d2143476Sdjm@openbsd.org 	}
174d2143476Sdjm@openbsd.org 	if ((r = sshbuf_get_u8(resp, &version)) != 0) {
175816036f1Sdjm@openbsd.org 		error_fr(r, "parse version");
176d2143476Sdjm@openbsd.org 		goto out;
177d2143476Sdjm@openbsd.org 	}
178d2143476Sdjm@openbsd.org 	if (version != SSH_SK_HELPER_VERSION) {
179816036f1Sdjm@openbsd.org 		error_f("unsupported version: got %u, expected %u",
180816036f1Sdjm@openbsd.org 		    version, SSH_SK_HELPER_VERSION);
181d2143476Sdjm@openbsd.org 		r = SSH_ERR_INVALID_FORMAT;
182d2143476Sdjm@openbsd.org 		goto out;
183d2143476Sdjm@openbsd.org 	}
18457b181eaSdjm@openbsd.org 	if ((r = sshbuf_get_u32(resp, &rtype)) != 0) {
185816036f1Sdjm@openbsd.org 		error_fr(r, "parse message type");
186c54cd189Sdjm@openbsd.org 		goto out;
187c54cd189Sdjm@openbsd.org 	}
18857b181eaSdjm@openbsd.org 	if (rtype == SSH_SK_HELPER_ERROR) {
189c54cd189Sdjm@openbsd.org 		if ((r = sshbuf_get_u32(resp, &rerr)) != 0) {
190816036f1Sdjm@openbsd.org 			error_fr(r, "parse");
191c54cd189Sdjm@openbsd.org 			goto out;
192c54cd189Sdjm@openbsd.org 		}
193816036f1Sdjm@openbsd.org 		debug_f("helper returned error -%u", rerr);
194c54cd189Sdjm@openbsd.org 		/* OpenSSH error values are negative; encoded as -err on wire */
195c54cd189Sdjm@openbsd.org 		if (rerr == 0 || rerr >= INT_MAX)
196c54cd189Sdjm@openbsd.org 			r = SSH_ERR_INTERNAL_ERROR;
197c54cd189Sdjm@openbsd.org 		else
198c54cd189Sdjm@openbsd.org 			r = -(int)rerr;
199c54cd189Sdjm@openbsd.org 		goto out;
20057b181eaSdjm@openbsd.org 	} else if (rtype != type) {
201816036f1Sdjm@openbsd.org 		error_f("helper returned incorrect message type %u, "
202816036f1Sdjm@openbsd.org 		    "expecting %u", rtype, type);
203c54cd189Sdjm@openbsd.org 		r = SSH_ERR_INTERNAL_ERROR;
204c54cd189Sdjm@openbsd.org 		goto out;
205c54cd189Sdjm@openbsd.org 	}
206d2143476Sdjm@openbsd.org 	/* success */
207d2143476Sdjm@openbsd.org 	r = 0;
208d2143476Sdjm@openbsd.org  out:
209d2143476Sdjm@openbsd.org 	oerrno = errno;
210d2143476Sdjm@openbsd.org 	close(fd);
211d2143476Sdjm@openbsd.org 	if ((r2 = reap_helper(pid)) != 0) {
212d2143476Sdjm@openbsd.org 		if (r == 0) {
213d2143476Sdjm@openbsd.org 			r = r2;
214d2143476Sdjm@openbsd.org 			oerrno = errno;
215d2143476Sdjm@openbsd.org 		}
216d2143476Sdjm@openbsd.org 	}
217d2143476Sdjm@openbsd.org 	if (r == 0) {
218d2143476Sdjm@openbsd.org 		*respp = resp;
219d2143476Sdjm@openbsd.org 		resp = NULL;
220d2143476Sdjm@openbsd.org 	}
22157b181eaSdjm@openbsd.org 	sshbuf_free(req);
222d2143476Sdjm@openbsd.org 	sshbuf_free(resp);
2233bf2a6acSdtucker@openbsd.org 	ssh_signal(SIGCHLD, osigchld);
224d2143476Sdjm@openbsd.org 	errno = oerrno;
225d2143476Sdjm@openbsd.org 	return r;
226d2143476Sdjm@openbsd.org 
227d2143476Sdjm@openbsd.org }
228d2143476Sdjm@openbsd.org 
229d2143476Sdjm@openbsd.org int
sshsk_sign(const char * provider,struct sshkey * key,u_char ** sigp,size_t * lenp,const u_char * data,size_t datalen,u_int compat,const char * pin)230d2143476Sdjm@openbsd.org sshsk_sign(const char *provider, struct sshkey *key,
231d2143476Sdjm@openbsd.org     u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
232c54cd189Sdjm@openbsd.org     u_int compat, const char *pin)
233d2143476Sdjm@openbsd.org {
234d2143476Sdjm@openbsd.org 	int oerrno, r = SSH_ERR_INTERNAL_ERROR;
235d2143476Sdjm@openbsd.org 	char *fp = NULL;
236d2143476Sdjm@openbsd.org 	struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
237d2143476Sdjm@openbsd.org 
238d2143476Sdjm@openbsd.org 	*sigp = NULL;
239d2143476Sdjm@openbsd.org 	*lenp = 0;
240d2143476Sdjm@openbsd.org 
2419244990eSDamien Miller #ifndef ENABLE_SK
2429244990eSDamien Miller 	return SSH_ERR_KEY_TYPE_UNKNOWN;
2439244990eSDamien Miller #endif
2449244990eSDamien Miller 
245d2143476Sdjm@openbsd.org 	if ((kbuf = sshbuf_new()) == NULL ||
246d2143476Sdjm@openbsd.org 	    (req = sshbuf_new()) == NULL) {
247d2143476Sdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
248d2143476Sdjm@openbsd.org 		goto out;
249d2143476Sdjm@openbsd.org 	}
250d2143476Sdjm@openbsd.org 
251d2143476Sdjm@openbsd.org 	if ((r = sshkey_private_serialize(key, kbuf)) != 0) {
252816036f1Sdjm@openbsd.org 		error_fr(r, "encode key");
253d2143476Sdjm@openbsd.org 		goto out;
254d2143476Sdjm@openbsd.org 	}
25557b181eaSdjm@openbsd.org 	if ((r = sshbuf_put_stringb(req, kbuf)) != 0 ||
256d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, provider)) != 0 ||
257d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_string(req, data, datalen)) != 0 ||
258d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, NULL)) != 0 || /* alg */
259c54cd189Sdjm@openbsd.org 	    (r = sshbuf_put_u32(req, compat)) != 0 ||
260c54cd189Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, pin)) != 0) {
261816036f1Sdjm@openbsd.org 		error_fr(r, "compose");
262d2143476Sdjm@openbsd.org 		goto out;
263d2143476Sdjm@openbsd.org 	}
264d2143476Sdjm@openbsd.org 
265d2143476Sdjm@openbsd.org 	if ((fp = sshkey_fingerprint(key, SSH_FP_HASH_DEFAULT,
266d2143476Sdjm@openbsd.org 	    SSH_FP_DEFAULT)) == NULL) {
267816036f1Sdjm@openbsd.org 		error_f("sshkey_fingerprint failed");
268d2143476Sdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
269d2143476Sdjm@openbsd.org 		goto out;
270d2143476Sdjm@openbsd.org 	}
271c54cd189Sdjm@openbsd.org 	if ((r = client_converse(req, &resp, SSH_SK_HELPER_SIGN)) != 0)
272d2143476Sdjm@openbsd.org 		goto out;
273d2143476Sdjm@openbsd.org 
274d2143476Sdjm@openbsd.org 	if ((r = sshbuf_get_string(resp, sigp, lenp)) != 0) {
275816036f1Sdjm@openbsd.org 		error_fr(r, "parse signature");
276d2143476Sdjm@openbsd.org 		r = SSH_ERR_INVALID_FORMAT;
277d2143476Sdjm@openbsd.org 		goto out;
278d2143476Sdjm@openbsd.org 	}
279d2143476Sdjm@openbsd.org 	if (sshbuf_len(resp) != 0) {
280816036f1Sdjm@openbsd.org 		error_f("trailing data in response");
281d2143476Sdjm@openbsd.org 		r = SSH_ERR_INVALID_FORMAT;
282d2143476Sdjm@openbsd.org 		goto out;
283d2143476Sdjm@openbsd.org 	}
284d2143476Sdjm@openbsd.org 	/* success */
285d2143476Sdjm@openbsd.org 	r = 0;
286d2143476Sdjm@openbsd.org  out:
287d2143476Sdjm@openbsd.org 	oerrno = errno;
288d2143476Sdjm@openbsd.org 	if (r != 0) {
289d2143476Sdjm@openbsd.org 		freezero(*sigp, *lenp);
290d2143476Sdjm@openbsd.org 		*sigp = NULL;
291d2143476Sdjm@openbsd.org 		*lenp = 0;
292d2143476Sdjm@openbsd.org 	}
293d2143476Sdjm@openbsd.org 	sshbuf_free(kbuf);
294d2143476Sdjm@openbsd.org 	sshbuf_free(req);
295d2143476Sdjm@openbsd.org 	sshbuf_free(resp);
296d2143476Sdjm@openbsd.org 	errno = oerrno;
297d2143476Sdjm@openbsd.org 	return r;
298d2143476Sdjm@openbsd.org }
299d2143476Sdjm@openbsd.org 
300d2143476Sdjm@openbsd.org int
sshsk_enroll(int type,const char * provider_path,const char * device,const char * application,const char * userid,uint8_t flags,const char * pin,struct sshbuf * challenge_buf,struct sshkey ** keyp,struct sshbuf * attest)301c312ca07Sdjm@openbsd.org sshsk_enroll(int type, const char *provider_path, const char *device,
302c312ca07Sdjm@openbsd.org     const char *application, const char *userid, uint8_t flags,
303c312ca07Sdjm@openbsd.org     const char *pin, struct sshbuf *challenge_buf,
304c54cd189Sdjm@openbsd.org     struct sshkey **keyp, struct sshbuf *attest)
305d2143476Sdjm@openbsd.org {
306d2143476Sdjm@openbsd.org 	int oerrno, r = SSH_ERR_INTERNAL_ERROR;
307d2143476Sdjm@openbsd.org 	struct sshbuf *kbuf = NULL, *abuf = NULL, *req = NULL, *resp = NULL;
308d2143476Sdjm@openbsd.org 	struct sshkey *key = NULL;
309d2143476Sdjm@openbsd.org 
310d2143476Sdjm@openbsd.org 	*keyp = NULL;
311d2143476Sdjm@openbsd.org 	if (attest != NULL)
312d2143476Sdjm@openbsd.org 		sshbuf_reset(attest);
313d2143476Sdjm@openbsd.org 
3149244990eSDamien Miller #ifndef ENABLE_SK
3159244990eSDamien Miller 	return SSH_ERR_KEY_TYPE_UNKNOWN;
3169244990eSDamien Miller #endif
3179244990eSDamien Miller 
318d2143476Sdjm@openbsd.org 	if (type < 0)
319d2143476Sdjm@openbsd.org 		return SSH_ERR_INVALID_ARGUMENT;
320d2143476Sdjm@openbsd.org 
321d2143476Sdjm@openbsd.org 	if ((abuf = sshbuf_new()) == NULL ||
322d2143476Sdjm@openbsd.org 	    (kbuf = sshbuf_new()) == NULL ||
323d2143476Sdjm@openbsd.org 	    (req = sshbuf_new()) == NULL) {
324d2143476Sdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
325d2143476Sdjm@openbsd.org 		goto out;
326d2143476Sdjm@openbsd.org 	}
327d2143476Sdjm@openbsd.org 
32857b181eaSdjm@openbsd.org 	if ((r = sshbuf_put_u32(req, (u_int)type)) != 0 ||
329d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, provider_path)) != 0 ||
330c312ca07Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, device)) != 0 ||
331d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, application)) != 0 ||
332c312ca07Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, userid)) != 0 ||
333d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_u8(req, flags)) != 0 ||
334c54cd189Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, pin)) != 0 ||
335d2143476Sdjm@openbsd.org 	    (r = sshbuf_put_stringb(req, challenge_buf)) != 0) {
336816036f1Sdjm@openbsd.org 		error_fr(r, "compose");
337d2143476Sdjm@openbsd.org 		goto out;
338d2143476Sdjm@openbsd.org 	}
339d2143476Sdjm@openbsd.org 
340c54cd189Sdjm@openbsd.org 	if ((r = client_converse(req, &resp, SSH_SK_HELPER_ENROLL)) != 0)
341d2143476Sdjm@openbsd.org 		goto out;
342d2143476Sdjm@openbsd.org 
343d2143476Sdjm@openbsd.org 	if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
344d2143476Sdjm@openbsd.org 	    (r = sshbuf_get_stringb(resp, abuf)) != 0) {
345816036f1Sdjm@openbsd.org 		error_fr(r, "parse");
346d2143476Sdjm@openbsd.org 		r = SSH_ERR_INVALID_FORMAT;
347d2143476Sdjm@openbsd.org 		goto out;
348d2143476Sdjm@openbsd.org 	}
349d2143476Sdjm@openbsd.org 	if (sshbuf_len(resp) != 0) {
350816036f1Sdjm@openbsd.org 		error_f("trailing data in response");
351d2143476Sdjm@openbsd.org 		r = SSH_ERR_INVALID_FORMAT;
352d2143476Sdjm@openbsd.org 		goto out;
353d2143476Sdjm@openbsd.org 	}
354d2143476Sdjm@openbsd.org 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
355816036f1Sdjm@openbsd.org 		error_fr(r, "encode");
356d2143476Sdjm@openbsd.org 		goto out;
357d2143476Sdjm@openbsd.org 	}
358d2143476Sdjm@openbsd.org 	if (attest != NULL && (r = sshbuf_putb(attest, abuf)) != 0) {
359816036f1Sdjm@openbsd.org 		error_fr(r, "encode attestation information");
360d2143476Sdjm@openbsd.org 		goto out;
361d2143476Sdjm@openbsd.org 	}
362d2143476Sdjm@openbsd.org 
363d2143476Sdjm@openbsd.org 	/* success */
364d2143476Sdjm@openbsd.org 	r = 0;
365d2143476Sdjm@openbsd.org 	*keyp = key;
366d2143476Sdjm@openbsd.org 	key = NULL;
367d2143476Sdjm@openbsd.org  out:
368d2143476Sdjm@openbsd.org 	oerrno = errno;
369d2143476Sdjm@openbsd.org 	sshkey_free(key);
370d2143476Sdjm@openbsd.org 	sshbuf_free(kbuf);
371d2143476Sdjm@openbsd.org 	sshbuf_free(abuf);
372d2143476Sdjm@openbsd.org 	sshbuf_free(req);
373d2143476Sdjm@openbsd.org 	sshbuf_free(resp);
374d2143476Sdjm@openbsd.org 	errno = oerrno;
375d2143476Sdjm@openbsd.org 	return r;
376d2143476Sdjm@openbsd.org }
37727753a8eSdjm@openbsd.org 
37827753a8eSdjm@openbsd.org int
sshsk_load_resident(const char * provider_path,const char * device,const char * pin,struct sshkey *** keysp,size_t * nkeysp)379c312ca07Sdjm@openbsd.org sshsk_load_resident(const char *provider_path, const char *device,
380c312ca07Sdjm@openbsd.org     const char *pin, struct sshkey ***keysp, size_t *nkeysp)
38127753a8eSdjm@openbsd.org {
38227753a8eSdjm@openbsd.org 	int oerrno, r = SSH_ERR_INTERNAL_ERROR;
38327753a8eSdjm@openbsd.org 	struct sshbuf *kbuf = NULL, *req = NULL, *resp = NULL;
38427753a8eSdjm@openbsd.org 	struct sshkey *key = NULL, **keys = NULL, **tmp;
38527753a8eSdjm@openbsd.org 	size_t i, nkeys = 0;
38627753a8eSdjm@openbsd.org 
38727753a8eSdjm@openbsd.org 	*keysp = NULL;
38827753a8eSdjm@openbsd.org 	*nkeysp = 0;
38927753a8eSdjm@openbsd.org 
39027753a8eSdjm@openbsd.org 	if ((resp = sshbuf_new()) == NULL ||
39127753a8eSdjm@openbsd.org 	    (kbuf = sshbuf_new()) == NULL ||
39227753a8eSdjm@openbsd.org 	    (req = sshbuf_new()) == NULL) {
39327753a8eSdjm@openbsd.org 		r = SSH_ERR_ALLOC_FAIL;
39427753a8eSdjm@openbsd.org 		goto out;
39527753a8eSdjm@openbsd.org 	}
39627753a8eSdjm@openbsd.org 
39757b181eaSdjm@openbsd.org 	if ((r = sshbuf_put_cstring(req, provider_path)) != 0 ||
398c312ca07Sdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, device)) != 0 ||
39927753a8eSdjm@openbsd.org 	    (r = sshbuf_put_cstring(req, pin)) != 0) {
400816036f1Sdjm@openbsd.org 		error_fr(r, "compose");
40127753a8eSdjm@openbsd.org 		goto out;
40227753a8eSdjm@openbsd.org 	}
40327753a8eSdjm@openbsd.org 
404c54cd189Sdjm@openbsd.org 	if ((r = client_converse(req, &resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
40527753a8eSdjm@openbsd.org 		goto out;
40627753a8eSdjm@openbsd.org 
40727753a8eSdjm@openbsd.org 	while (sshbuf_len(resp) != 0) {
40827753a8eSdjm@openbsd.org 		/* key, comment */
40927753a8eSdjm@openbsd.org 		if ((r = sshbuf_get_stringb(resp, kbuf)) != 0 ||
41027753a8eSdjm@openbsd.org 		    (r = sshbuf_get_cstring(resp, NULL, NULL)) != 0) {
411816036f1Sdjm@openbsd.org 			error_fr(r, "parse signature");
41227753a8eSdjm@openbsd.org 			r = SSH_ERR_INVALID_FORMAT;
41327753a8eSdjm@openbsd.org 			goto out;
41427753a8eSdjm@openbsd.org 		}
41527753a8eSdjm@openbsd.org 		if ((r = sshkey_private_deserialize(kbuf, &key)) != 0) {
416816036f1Sdjm@openbsd.org 			error_fr(r, "decode key");
41727753a8eSdjm@openbsd.org 			goto out;
41827753a8eSdjm@openbsd.org 		}
41927753a8eSdjm@openbsd.org 		if ((tmp = recallocarray(keys, nkeys, nkeys + 1,
42027753a8eSdjm@openbsd.org 		    sizeof(*keys))) == NULL) {
421816036f1Sdjm@openbsd.org 			error_f("recallocarray keys failed");
42227753a8eSdjm@openbsd.org 			goto out;
42327753a8eSdjm@openbsd.org 		}
424816036f1Sdjm@openbsd.org 		debug_f("keys[%zu]: %s %s", nkeys, sshkey_type(key),
425816036f1Sdjm@openbsd.org 		    key->sk_application);
42627753a8eSdjm@openbsd.org 		keys = tmp;
42727753a8eSdjm@openbsd.org 		keys[nkeys++] = key;
42827753a8eSdjm@openbsd.org 		key = NULL;
42927753a8eSdjm@openbsd.org 	}
43027753a8eSdjm@openbsd.org 
43127753a8eSdjm@openbsd.org 	/* success */
43227753a8eSdjm@openbsd.org 	r = 0;
43327753a8eSdjm@openbsd.org 	*keysp = keys;
43427753a8eSdjm@openbsd.org 	*nkeysp = nkeys;
43527753a8eSdjm@openbsd.org 	keys = NULL;
43627753a8eSdjm@openbsd.org 	nkeys = 0;
43727753a8eSdjm@openbsd.org  out:
43827753a8eSdjm@openbsd.org 	oerrno = errno;
43927753a8eSdjm@openbsd.org 	for (i = 0; i < nkeys; i++)
44027753a8eSdjm@openbsd.org 		sshkey_free(keys[i]);
44127753a8eSdjm@openbsd.org 	free(keys);
44227753a8eSdjm@openbsd.org 	sshkey_free(key);
44327753a8eSdjm@openbsd.org 	sshbuf_free(kbuf);
44427753a8eSdjm@openbsd.org 	sshbuf_free(req);
44527753a8eSdjm@openbsd.org 	sshbuf_free(resp);
44627753a8eSdjm@openbsd.org 	errno = oerrno;
44727753a8eSdjm@openbsd.org 	return r;
44827753a8eSdjm@openbsd.org }
449