xref: /openssh-portable/ssh-sk-helper.c (revision 816036f1)
1*816036f1Sdjm@openbsd.org /* $OpenBSD: ssh-sk-helper.c,v 1.11 2020/10/18 11:32:02 djm Exp $ */
207da39f7Sdjm@openbsd.org /*
307da39f7Sdjm@openbsd.org  * Copyright (c) 2019 Google LLC
407da39f7Sdjm@openbsd.org  *
507da39f7Sdjm@openbsd.org  * Permission to use, copy, modify, and distribute this software for any
607da39f7Sdjm@openbsd.org  * purpose with or without fee is hereby granted, provided that the above
707da39f7Sdjm@openbsd.org  * copyright notice and this permission notice appear in all copies.
807da39f7Sdjm@openbsd.org  *
907da39f7Sdjm@openbsd.org  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1007da39f7Sdjm@openbsd.org  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1107da39f7Sdjm@openbsd.org  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1207da39f7Sdjm@openbsd.org  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1307da39f7Sdjm@openbsd.org  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1407da39f7Sdjm@openbsd.org  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1507da39f7Sdjm@openbsd.org  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1607da39f7Sdjm@openbsd.org  */
1707da39f7Sdjm@openbsd.org 
1807da39f7Sdjm@openbsd.org /*
1907da39f7Sdjm@openbsd.org  * This is a tiny program used to isolate the address space used for
2007da39f7Sdjm@openbsd.org  * security key middleware signing operations from ssh-agent. It is similar
21611073fbSdjm@openbsd.org  * to ssh-pkcs11-helper.c but considerably simpler as the operations for
22611073fbSdjm@openbsd.org  * security keys are stateless.
2307da39f7Sdjm@openbsd.org  *
24611073fbSdjm@openbsd.org  * Please crank SSH_SK_HELPER_VERSION in sshkey.h for any incompatible
25611073fbSdjm@openbsd.org  * protocol changes.
2607da39f7Sdjm@openbsd.org  */
2707da39f7Sdjm@openbsd.org 
2807da39f7Sdjm@openbsd.org #include "includes.h"
2907da39f7Sdjm@openbsd.org 
30611073fbSdjm@openbsd.org #include <limits.h>
3107da39f7Sdjm@openbsd.org #include <stdarg.h>
3207da39f7Sdjm@openbsd.org #include <stdio.h>
3307da39f7Sdjm@openbsd.org #include <stdlib.h>
3407da39f7Sdjm@openbsd.org #include <string.h>
3507da39f7Sdjm@openbsd.org #include <unistd.h>
3607da39f7Sdjm@openbsd.org #include <errno.h>
3707da39f7Sdjm@openbsd.org 
3807da39f7Sdjm@openbsd.org #include "xmalloc.h"
3907da39f7Sdjm@openbsd.org #include "log.h"
4007da39f7Sdjm@openbsd.org #include "sshkey.h"
4107da39f7Sdjm@openbsd.org #include "authfd.h"
4207da39f7Sdjm@openbsd.org #include "misc.h"
4307da39f7Sdjm@openbsd.org #include "sshbuf.h"
4407da39f7Sdjm@openbsd.org #include "msg.h"
4507da39f7Sdjm@openbsd.org #include "uidswap.h"
4607da39f7Sdjm@openbsd.org #include "sshkey.h"
4707da39f7Sdjm@openbsd.org #include "ssherr.h"
4807da39f7Sdjm@openbsd.org #include "ssh-sk.h"
4907da39f7Sdjm@openbsd.org 
50764d51e0SDamien Miller #ifdef ENABLE_SK
5107da39f7Sdjm@openbsd.org extern char *__progname;
5207da39f7Sdjm@openbsd.org 
53c54cd189Sdjm@openbsd.org static struct sshbuf *reply_error(int r, char *fmt, ...)
54c54cd189Sdjm@openbsd.org     __attribute__((__format__ (printf, 2, 3)));
55c54cd189Sdjm@openbsd.org 
56c54cd189Sdjm@openbsd.org static struct sshbuf *
reply_error(int r,char * fmt,...)57c54cd189Sdjm@openbsd.org reply_error(int r, char *fmt, ...)
58c54cd189Sdjm@openbsd.org {
59c54cd189Sdjm@openbsd.org 	char *msg;
60c54cd189Sdjm@openbsd.org 	va_list ap;
61c54cd189Sdjm@openbsd.org 	struct sshbuf *resp;
62c54cd189Sdjm@openbsd.org 
63c54cd189Sdjm@openbsd.org 	va_start(ap, fmt);
64c54cd189Sdjm@openbsd.org 	xvasprintf(&msg, fmt, ap);
65c54cd189Sdjm@openbsd.org 	va_end(ap);
6659d01f1dSdjm@openbsd.org 	debug("%s: %s", __progname, msg);
67c54cd189Sdjm@openbsd.org 	free(msg);
68c54cd189Sdjm@openbsd.org 
69c54cd189Sdjm@openbsd.org 	if (r >= 0)
70*816036f1Sdjm@openbsd.org 		fatal_f("invalid error code %d", r);
71c54cd189Sdjm@openbsd.org 
72c54cd189Sdjm@openbsd.org 	if ((resp = sshbuf_new()) == NULL)
73c54cd189Sdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
74c54cd189Sdjm@openbsd.org 	if (sshbuf_put_u32(resp, SSH_SK_HELPER_ERROR) != 0 ||
75c54cd189Sdjm@openbsd.org 	    sshbuf_put_u32(resp, (u_int)-r) != 0)
76c54cd189Sdjm@openbsd.org 		fatal("%s: buffer error", __progname);
77c54cd189Sdjm@openbsd.org 	return resp;
78c54cd189Sdjm@openbsd.org }
79c54cd189Sdjm@openbsd.org 
80c312ca07Sdjm@openbsd.org /* If the specified string is zero length, then free it and replace with NULL */
81c312ca07Sdjm@openbsd.org static void
null_empty(char ** s)82c312ca07Sdjm@openbsd.org null_empty(char **s)
83c312ca07Sdjm@openbsd.org {
84c312ca07Sdjm@openbsd.org 	if (s == NULL || *s == NULL || **s != '\0')
85c312ca07Sdjm@openbsd.org 		return;
86c312ca07Sdjm@openbsd.org 
87c312ca07Sdjm@openbsd.org 	free(*s);
88c312ca07Sdjm@openbsd.org 	*s = NULL;
89c312ca07Sdjm@openbsd.org }
90c312ca07Sdjm@openbsd.org 
91611073fbSdjm@openbsd.org static struct sshbuf *
process_sign(struct sshbuf * req)92611073fbSdjm@openbsd.org process_sign(struct sshbuf *req)
93611073fbSdjm@openbsd.org {
94611073fbSdjm@openbsd.org 	int r = SSH_ERR_INTERNAL_ERROR;
95611073fbSdjm@openbsd.org 	struct sshbuf *resp, *kbuf;
964be56399Sdjm@openbsd.org 	struct sshkey *key = NULL;
97611073fbSdjm@openbsd.org 	uint32_t compat;
98611073fbSdjm@openbsd.org 	const u_char *message;
994be56399Sdjm@openbsd.org 	u_char *sig = NULL;
1004be56399Sdjm@openbsd.org 	size_t msglen, siglen = 0;
1014be56399Sdjm@openbsd.org 	char *provider = NULL, *pin = NULL;
102611073fbSdjm@openbsd.org 
103611073fbSdjm@openbsd.org 	if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
104611073fbSdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
105611073fbSdjm@openbsd.org 	    (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
106611073fbSdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, NULL, NULL)) != 0 || /* alg */
107c54cd189Sdjm@openbsd.org 	    (r = sshbuf_get_u32(req, &compat)) != 0 ||
108c54cd189Sdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
109*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: parse", __progname);
110611073fbSdjm@openbsd.org 	if (sshbuf_len(req) != 0)
111611073fbSdjm@openbsd.org 		fatal("%s: trailing data in request", __progname);
112611073fbSdjm@openbsd.org 
113611073fbSdjm@openbsd.org 	if ((r = sshkey_private_deserialize(kbuf, &key)) != 0)
114*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: Unable to parse private key", __progname);
115*816036f1Sdjm@openbsd.org 	if (!sshkey_is_sk(key)) {
116*816036f1Sdjm@openbsd.org 		fatal("%s: Unsupported key type %s",
117*816036f1Sdjm@openbsd.org 		    __progname, sshkey_ssh_name(key));
118*816036f1Sdjm@openbsd.org 	}
119611073fbSdjm@openbsd.org 
120*816036f1Sdjm@openbsd.org 	debug_f("ready to sign with key %s, provider %s: "
121*816036f1Sdjm@openbsd.org 	    "msg len %zu, compat 0x%lx", sshkey_type(key),
122611073fbSdjm@openbsd.org 	    provider, msglen, (u_long)compat);
123611073fbSdjm@openbsd.org 
124c312ca07Sdjm@openbsd.org 	null_empty(&pin);
125c54cd189Sdjm@openbsd.org 
126611073fbSdjm@openbsd.org 	if ((r = sshsk_sign(provider, key, &sig, &siglen,
127c54cd189Sdjm@openbsd.org 	    message, msglen, compat, pin)) != 0) {
128c54cd189Sdjm@openbsd.org 		resp = reply_error(r, "Signing failed: %s", ssh_err(r));
129c54cd189Sdjm@openbsd.org 		goto out;
130c54cd189Sdjm@openbsd.org 	}
131611073fbSdjm@openbsd.org 
132611073fbSdjm@openbsd.org 	if ((resp = sshbuf_new()) == NULL)
133611073fbSdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
134611073fbSdjm@openbsd.org 
135c54cd189Sdjm@openbsd.org 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_SIGN)) != 0 ||
136c54cd189Sdjm@openbsd.org 	    (r = sshbuf_put_string(resp, sig, siglen)) != 0)
137*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: compose", __progname);
138c54cd189Sdjm@openbsd.org  out:
1394be56399Sdjm@openbsd.org 	sshkey_free(key);
140611073fbSdjm@openbsd.org 	sshbuf_free(kbuf);
141611073fbSdjm@openbsd.org 	free(provider);
1424be56399Sdjm@openbsd.org 	if (sig != NULL)
1434be56399Sdjm@openbsd.org 		freezero(sig, siglen);
144c54cd189Sdjm@openbsd.org 	if (pin != NULL)
145c54cd189Sdjm@openbsd.org 		freezero(pin, strlen(pin));
146611073fbSdjm@openbsd.org 	return resp;
147611073fbSdjm@openbsd.org }
148611073fbSdjm@openbsd.org 
149611073fbSdjm@openbsd.org static struct sshbuf *
process_enroll(struct sshbuf * req)150611073fbSdjm@openbsd.org process_enroll(struct sshbuf *req)
151611073fbSdjm@openbsd.org {
152611073fbSdjm@openbsd.org 	int r;
153611073fbSdjm@openbsd.org 	u_int type;
154c312ca07Sdjm@openbsd.org 	char *provider, *application, *pin, *device, *userid;
155611073fbSdjm@openbsd.org 	uint8_t flags;
156611073fbSdjm@openbsd.org 	struct sshbuf *challenge, *attest, *kbuf, *resp;
157611073fbSdjm@openbsd.org 	struct sshkey *key;
158611073fbSdjm@openbsd.org 
159c54cd189Sdjm@openbsd.org 	if ((attest = sshbuf_new()) == NULL ||
160611073fbSdjm@openbsd.org 	    (kbuf = sshbuf_new()) == NULL)
161611073fbSdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
162611073fbSdjm@openbsd.org 
163611073fbSdjm@openbsd.org 	if ((r = sshbuf_get_u32(req, &type)) != 0 ||
164611073fbSdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
165c312ca07Sdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
166611073fbSdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &application, NULL)) != 0 ||
167c312ca07Sdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &userid, NULL)) != 0 ||
168611073fbSdjm@openbsd.org 	    (r = sshbuf_get_u8(req, &flags)) != 0 ||
169c54cd189Sdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0 ||
170611073fbSdjm@openbsd.org 	    (r = sshbuf_froms(req, &challenge)) != 0)
171*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: parse", __progname);
172611073fbSdjm@openbsd.org 	if (sshbuf_len(req) != 0)
173611073fbSdjm@openbsd.org 		fatal("%s: trailing data in request", __progname);
174611073fbSdjm@openbsd.org 
175611073fbSdjm@openbsd.org 	if (type > INT_MAX)
176611073fbSdjm@openbsd.org 		fatal("%s: bad type %u", __progname, type);
177611073fbSdjm@openbsd.org 	if (sshbuf_len(challenge) == 0) {
178611073fbSdjm@openbsd.org 		sshbuf_free(challenge);
179611073fbSdjm@openbsd.org 		challenge = NULL;
180611073fbSdjm@openbsd.org 	}
181c312ca07Sdjm@openbsd.org 	null_empty(&device);
182c312ca07Sdjm@openbsd.org 	null_empty(&userid);
183c312ca07Sdjm@openbsd.org 	null_empty(&pin);
184611073fbSdjm@openbsd.org 
185c312ca07Sdjm@openbsd.org 	if ((r = sshsk_enroll((int)type, provider, device, application, userid,
186c312ca07Sdjm@openbsd.org 	    flags, pin, challenge, &key, attest)) != 0) {
187c54cd189Sdjm@openbsd.org 		resp = reply_error(r, "Enrollment failed: %s", ssh_err(r));
188c54cd189Sdjm@openbsd.org 		goto out;
189c54cd189Sdjm@openbsd.org 	}
190611073fbSdjm@openbsd.org 
191c54cd189Sdjm@openbsd.org 	if ((resp = sshbuf_new()) == NULL)
192c54cd189Sdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
193611073fbSdjm@openbsd.org 	if ((r = sshkey_private_serialize(key, kbuf)) != 0)
194*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: encode key", __progname);
195c54cd189Sdjm@openbsd.org 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_ENROLL)) != 0 ||
196c54cd189Sdjm@openbsd.org 	    (r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
197611073fbSdjm@openbsd.org 	    (r = sshbuf_put_stringb(resp, attest)) != 0)
198*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: compose", __progname);
199611073fbSdjm@openbsd.org 
200c54cd189Sdjm@openbsd.org  out:
201611073fbSdjm@openbsd.org 	sshkey_free(key);
202611073fbSdjm@openbsd.org 	sshbuf_free(kbuf);
203611073fbSdjm@openbsd.org 	sshbuf_free(attest);
204611073fbSdjm@openbsd.org 	sshbuf_free(challenge);
205611073fbSdjm@openbsd.org 	free(provider);
206611073fbSdjm@openbsd.org 	free(application);
207c54cd189Sdjm@openbsd.org 	if (pin != NULL)
208c54cd189Sdjm@openbsd.org 		freezero(pin, strlen(pin));
209611073fbSdjm@openbsd.org 
210611073fbSdjm@openbsd.org 	return resp;
211611073fbSdjm@openbsd.org }
212611073fbSdjm@openbsd.org 
21327753a8eSdjm@openbsd.org static struct sshbuf *
process_load_resident(struct sshbuf * req)21427753a8eSdjm@openbsd.org process_load_resident(struct sshbuf *req)
21527753a8eSdjm@openbsd.org {
21627753a8eSdjm@openbsd.org 	int r;
217c312ca07Sdjm@openbsd.org 	char *provider, *pin, *device;
21827753a8eSdjm@openbsd.org 	struct sshbuf *kbuf, *resp;
21927753a8eSdjm@openbsd.org 	struct sshkey **keys = NULL;
22027753a8eSdjm@openbsd.org 	size_t nkeys = 0, i;
22127753a8eSdjm@openbsd.org 
222c54cd189Sdjm@openbsd.org 	if ((kbuf = sshbuf_new()) == NULL)
22327753a8eSdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
22427753a8eSdjm@openbsd.org 
22527753a8eSdjm@openbsd.org 	if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
226c312ca07Sdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &device, NULL)) != 0 ||
22727753a8eSdjm@openbsd.org 	    (r = sshbuf_get_cstring(req, &pin, NULL)) != 0)
228*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: parse", __progname);
22927753a8eSdjm@openbsd.org 	if (sshbuf_len(req) != 0)
23027753a8eSdjm@openbsd.org 		fatal("%s: trailing data in request", __progname);
23127753a8eSdjm@openbsd.org 
232c312ca07Sdjm@openbsd.org 	null_empty(&device);
233c312ca07Sdjm@openbsd.org 	null_empty(&pin);
234c54cd189Sdjm@openbsd.org 
235c312ca07Sdjm@openbsd.org 	if ((r = sshsk_load_resident(provider, device, pin,
236c312ca07Sdjm@openbsd.org 	    &keys, &nkeys)) != 0) {
237c54cd189Sdjm@openbsd.org 		resp = reply_error(r, " sshsk_load_resident failed: %s",
238c54cd189Sdjm@openbsd.org 		    ssh_err(r));
239c54cd189Sdjm@openbsd.org 		goto out;
240c54cd189Sdjm@openbsd.org 	}
241c54cd189Sdjm@openbsd.org 
242c54cd189Sdjm@openbsd.org 	if ((resp = sshbuf_new()) == NULL)
243c54cd189Sdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
244c54cd189Sdjm@openbsd.org 
245c54cd189Sdjm@openbsd.org 	if ((r = sshbuf_put_u32(resp, SSH_SK_HELPER_LOAD_RESIDENT)) != 0)
246*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: compose", __progname);
24727753a8eSdjm@openbsd.org 
24827753a8eSdjm@openbsd.org 	for (i = 0; i < nkeys; i++) {
249*816036f1Sdjm@openbsd.org 		debug_f("key %zu %s %s", i, sshkey_type(keys[i]),
250*816036f1Sdjm@openbsd.org 		    keys[i]->sk_application);
25127753a8eSdjm@openbsd.org 		sshbuf_reset(kbuf);
25227753a8eSdjm@openbsd.org 		if ((r = sshkey_private_serialize(keys[i], kbuf)) != 0)
253*816036f1Sdjm@openbsd.org 			fatal_r(r, "%s: encode key", __progname);
25427753a8eSdjm@openbsd.org 		if ((r = sshbuf_put_stringb(resp, kbuf)) != 0 ||
25527753a8eSdjm@openbsd.org 		    (r = sshbuf_put_cstring(resp, "")) != 0) /* comment */
256*816036f1Sdjm@openbsd.org 			fatal_r(r, "%s: compose key", __progname);
25727753a8eSdjm@openbsd.org 	}
25827753a8eSdjm@openbsd.org 
259c54cd189Sdjm@openbsd.org  out:
26027753a8eSdjm@openbsd.org 	for (i = 0; i < nkeys; i++)
26127753a8eSdjm@openbsd.org 		sshkey_free(keys[i]);
26227753a8eSdjm@openbsd.org 	free(keys);
26327753a8eSdjm@openbsd.org 	sshbuf_free(kbuf);
26427753a8eSdjm@openbsd.org 	free(provider);
265c54cd189Sdjm@openbsd.org 	if (pin != NULL)
26627753a8eSdjm@openbsd.org 		freezero(pin, strlen(pin));
26727753a8eSdjm@openbsd.org 	return resp;
26827753a8eSdjm@openbsd.org }
26927753a8eSdjm@openbsd.org 
27007da39f7Sdjm@openbsd.org int
main(int argc,char ** argv)27107da39f7Sdjm@openbsd.org main(int argc, char **argv)
27207da39f7Sdjm@openbsd.org {
27307da39f7Sdjm@openbsd.org 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
27407da39f7Sdjm@openbsd.org 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
275f45f3a8aSDamien Miller 	struct sshbuf *req, *resp;
27657b181eaSdjm@openbsd.org 	int in, out, ch, r, vflag = 0;
27757b181eaSdjm@openbsd.org 	u_int rtype, ll = 0;
27857b181eaSdjm@openbsd.org 	uint8_t version, log_stderr = 0;
27907da39f7Sdjm@openbsd.org 
28007da39f7Sdjm@openbsd.org 	sanitise_stdfd();
28107da39f7Sdjm@openbsd.org 	log_init(__progname, log_level, log_facility, log_stderr);
28207da39f7Sdjm@openbsd.org 
28307da39f7Sdjm@openbsd.org 	while ((ch = getopt(argc, argv, "v")) != -1) {
28407da39f7Sdjm@openbsd.org 		switch (ch) {
28507da39f7Sdjm@openbsd.org 		case 'v':
28657b181eaSdjm@openbsd.org 			vflag = 1;
28707da39f7Sdjm@openbsd.org 			if (log_level == SYSLOG_LEVEL_ERROR)
28807da39f7Sdjm@openbsd.org 				log_level = SYSLOG_LEVEL_DEBUG1;
28907da39f7Sdjm@openbsd.org 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
29007da39f7Sdjm@openbsd.org 				log_level++;
29107da39f7Sdjm@openbsd.org 			break;
29207da39f7Sdjm@openbsd.org 		default:
29307da39f7Sdjm@openbsd.org 			fprintf(stderr, "usage: %s [-v]\n", __progname);
29407da39f7Sdjm@openbsd.org 			exit(1);
29507da39f7Sdjm@openbsd.org 		}
29607da39f7Sdjm@openbsd.org 	}
29757b181eaSdjm@openbsd.org 	log_init(__progname, log_level, log_facility, vflag);
29807da39f7Sdjm@openbsd.org 
29907da39f7Sdjm@openbsd.org 	/*
30007da39f7Sdjm@openbsd.org 	 * Rearrange our file descriptors a little; we don't trust the
30107da39f7Sdjm@openbsd.org 	 * providers not to fiddle with stdin/out.
30207da39f7Sdjm@openbsd.org 	 */
30307da39f7Sdjm@openbsd.org 	closefrom(STDERR_FILENO + 1);
30407da39f7Sdjm@openbsd.org 	if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
30507da39f7Sdjm@openbsd.org 		fatal("%s: dup: %s", __progname, strerror(errno));
30607da39f7Sdjm@openbsd.org 	close(STDIN_FILENO);
30707da39f7Sdjm@openbsd.org 	close(STDOUT_FILENO);
30807da39f7Sdjm@openbsd.org 	sanitise_stdfd(); /* resets to /dev/null */
30907da39f7Sdjm@openbsd.org 
310611073fbSdjm@openbsd.org 	if ((req = sshbuf_new()) == NULL)
31107da39f7Sdjm@openbsd.org 		fatal("%s: sshbuf_new failed", __progname);
31207da39f7Sdjm@openbsd.org 	if (ssh_msg_recv(in, req) < 0)
31307da39f7Sdjm@openbsd.org 		fatal("ssh_msg_recv failed");
31407da39f7Sdjm@openbsd.org 	close(in);
315*816036f1Sdjm@openbsd.org 	debug_f("received message len %zu", sshbuf_len(req));
31607da39f7Sdjm@openbsd.org 
31707da39f7Sdjm@openbsd.org 	if ((r = sshbuf_get_u8(req, &version)) != 0)
318*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: parse version", __progname);
31907da39f7Sdjm@openbsd.org 	if (version != SSH_SK_HELPER_VERSION) {
32007da39f7Sdjm@openbsd.org 		fatal("unsupported version: received %d, expected %d",
32107da39f7Sdjm@openbsd.org 		    version, SSH_SK_HELPER_VERSION);
32207da39f7Sdjm@openbsd.org 	}
32307da39f7Sdjm@openbsd.org 
32457b181eaSdjm@openbsd.org 	if ((r = sshbuf_get_u32(req, &rtype)) != 0 ||
32557b181eaSdjm@openbsd.org 	    (r = sshbuf_get_u8(req, &log_stderr)) != 0 ||
32657b181eaSdjm@openbsd.org 	    (r = sshbuf_get_u32(req, &ll)) != 0)
327*816036f1Sdjm@openbsd.org 		fatal_r(r, "%s: parse", __progname);
32807da39f7Sdjm@openbsd.org 
32957b181eaSdjm@openbsd.org 	if (!vflag && log_level_name((LogLevel)ll) != NULL)
33057b181eaSdjm@openbsd.org 		log_init(__progname, (LogLevel)ll, log_facility, log_stderr);
33157b181eaSdjm@openbsd.org 
332611073fbSdjm@openbsd.org 	switch (rtype) {
333611073fbSdjm@openbsd.org 	case SSH_SK_HELPER_SIGN:
334611073fbSdjm@openbsd.org 		resp = process_sign(req);
335611073fbSdjm@openbsd.org 		break;
336611073fbSdjm@openbsd.org 	case SSH_SK_HELPER_ENROLL:
337611073fbSdjm@openbsd.org 		resp = process_enroll(req);
338611073fbSdjm@openbsd.org 		break;
33927753a8eSdjm@openbsd.org 	case SSH_SK_HELPER_LOAD_RESIDENT:
34027753a8eSdjm@openbsd.org 		resp = process_load_resident(req);
34127753a8eSdjm@openbsd.org 		break;
342611073fbSdjm@openbsd.org 	default:
343611073fbSdjm@openbsd.org 		fatal("%s: unsupported request type %u", __progname, rtype);
344611073fbSdjm@openbsd.org 	}
345611073fbSdjm@openbsd.org 	sshbuf_free(req);
346*816036f1Sdjm@openbsd.org 	debug_f("reply len %zu", sshbuf_len(resp));
347611073fbSdjm@openbsd.org 
34807da39f7Sdjm@openbsd.org 	if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
34907da39f7Sdjm@openbsd.org 		fatal("ssh_msg_send failed");
350611073fbSdjm@openbsd.org 	sshbuf_free(resp);
35107da39f7Sdjm@openbsd.org 	close(out);
35207da39f7Sdjm@openbsd.org 
35307da39f7Sdjm@openbsd.org 	return (0);
35407da39f7Sdjm@openbsd.org }
355764d51e0SDamien Miller #else /* ENABLE_SK */
356764d51e0SDamien Miller #include <stdio.h>
357764d51e0SDamien Miller 
358764d51e0SDamien Miller int
main(int argc,char ** argv)359764d51e0SDamien Miller main(int argc, char **argv)
360764d51e0SDamien Miller {
361764d51e0SDamien Miller 	fprintf(stderr, "ssh-sk-helper: disabled at compile time\n");
362764d51e0SDamien Miller 	return -1;
363764d51e0SDamien Miller }
364764d51e0SDamien Miller #endif /* ENABLE_SK */
365