xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-pkcs11-helper.c (revision 9469f4f13c84743995b7d51c506f9c9849ba30de)
1*9469f4f1Schristos /*	$NetBSD: ssh-pkcs11-helper.c,v 1.23 2024/09/24 21:32:19 christos Exp $	*/
2*9469f4f1Schristos /* $OpenBSD: ssh-pkcs11-helper.c,v 1.27 2024/08/15 00:51:51 djm Exp $ */
3*9469f4f1Schristos 
4264ec8a8Sadam /*
5264ec8a8Sadam  * Copyright (c) 2010 Markus Friedl.  All rights reserved.
6264ec8a8Sadam  *
7264ec8a8Sadam  * Permission to use, copy, modify, and distribute this software for any
8264ec8a8Sadam  * purpose with or without fee is hereby granted, provided that the above
9264ec8a8Sadam  * copyright notice and this permission notice appear in all copies.
10264ec8a8Sadam  *
11264ec8a8Sadam  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12264ec8a8Sadam  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13264ec8a8Sadam  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14264ec8a8Sadam  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15264ec8a8Sadam  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16264ec8a8Sadam  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17264ec8a8Sadam  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18264ec8a8Sadam  */
19aef795aaSadam #include "includes.h"
20*9469f4f1Schristos __RCSID("$NetBSD: ssh-pkcs11-helper.c,v 1.23 2024/09/24 21:32:19 christos Exp $");
21264ec8a8Sadam 
22264ec8a8Sadam #include <sys/types.h>
23e4d43b82Schristos #include <sys/queue.h>
24264ec8a8Sadam #include <sys/time.h>
25aef795aaSadam #include <sys/param.h>
26264ec8a8Sadam 
27cd4ada6aSchristos #include <stdlib.h>
28aa36fcacSchristos #include <errno.h>
29aa36fcacSchristos #include <poll.h>
30264ec8a8Sadam #include <stdarg.h>
31264ec8a8Sadam #include <string.h>
32264ec8a8Sadam #include <unistd.h>
33264ec8a8Sadam 
34264ec8a8Sadam #include "xmalloc.h"
35ffae97bbSchristos #include "sshbuf.h"
36264ec8a8Sadam #include "log.h"
37264ec8a8Sadam #include "misc.h"
38ffae97bbSchristos #include "sshkey.h"
39264ec8a8Sadam #include "authfd.h"
40264ec8a8Sadam #include "ssh-pkcs11.h"
41ffae97bbSchristos #include "ssherr.h"
42264ec8a8Sadam 
43cd4ada6aSchristos #ifdef WITH_OPENSSL
44*9469f4f1Schristos #include <openssl/ec.h>
45*9469f4f1Schristos #include <openssl/rsa.h>
46cd4ada6aSchristos 
47264ec8a8Sadam /* borrows code from sftp-server and ssh-agent */
48264ec8a8Sadam 
49264ec8a8Sadam struct pkcs11_keyinfo {
507a183406Schristos 	struct sshkey	*key;
51ed75d7a8Schristos 	char		*providername, *label;
52264ec8a8Sadam 	TAILQ_ENTRY(pkcs11_keyinfo) next;
53264ec8a8Sadam };
54264ec8a8Sadam 
55264ec8a8Sadam TAILQ_HEAD(, pkcs11_keyinfo) pkcs11_keylist;
56264ec8a8Sadam 
57264ec8a8Sadam #define MAX_MSG_LENGTH		10240 /*XXX*/
58264ec8a8Sadam 
59264ec8a8Sadam /* input and output queue */
60ffae97bbSchristos struct sshbuf *iqueue;
61ffae97bbSchristos struct sshbuf *oqueue;
62264ec8a8Sadam 
63264ec8a8Sadam static void
64ed75d7a8Schristos add_key(struct sshkey *k, char *name, char *label)
65264ec8a8Sadam {
66264ec8a8Sadam 	struct pkcs11_keyinfo *ki;
67264ec8a8Sadam 
68264ec8a8Sadam 	ki = xcalloc(1, sizeof(*ki));
69264ec8a8Sadam 	ki->providername = xstrdup(name);
70264ec8a8Sadam 	ki->key = k;
71ed75d7a8Schristos 	ki->label = xstrdup(label);
72264ec8a8Sadam 	TAILQ_INSERT_TAIL(&pkcs11_keylist, ki, next);
73264ec8a8Sadam }
74264ec8a8Sadam 
75264ec8a8Sadam static void
76264ec8a8Sadam del_keys_by_name(char *name)
77264ec8a8Sadam {
78264ec8a8Sadam 	struct pkcs11_keyinfo *ki, *nxt;
79264ec8a8Sadam 
80264ec8a8Sadam 	for (ki = TAILQ_FIRST(&pkcs11_keylist); ki; ki = nxt) {
81264ec8a8Sadam 		nxt = TAILQ_NEXT(ki, next);
82264ec8a8Sadam 		if (!strcmp(ki->providername, name)) {
83264ec8a8Sadam 			TAILQ_REMOVE(&pkcs11_keylist, ki, next);
8400a838c4Schristos 			free(ki->providername);
85ed75d7a8Schristos 			free(ki->label);
86ffae97bbSchristos 			sshkey_free(ki->key);
87264ec8a8Sadam 			free(ki);
88264ec8a8Sadam 		}
89264ec8a8Sadam 	}
90264ec8a8Sadam }
91264ec8a8Sadam 
92264ec8a8Sadam /* lookup matching 'private' key */
937a183406Schristos static struct sshkey *
947a183406Schristos lookup_key(struct sshkey *k)
95264ec8a8Sadam {
96264ec8a8Sadam 	struct pkcs11_keyinfo *ki;
97264ec8a8Sadam 
98264ec8a8Sadam 	TAILQ_FOREACH(ki, &pkcs11_keylist, next) {
99b592f463Schristos 		debug("check %s %s %s", sshkey_type(ki->key),
100b592f463Schristos 		    ki->providername, ki->label);
101ffae97bbSchristos 		if (sshkey_equal(k, ki->key))
102264ec8a8Sadam 			return (ki->key);
103264ec8a8Sadam 	}
104264ec8a8Sadam 	return (NULL);
105264ec8a8Sadam }
106264ec8a8Sadam 
107264ec8a8Sadam static void
108ffae97bbSchristos send_msg(struct sshbuf *m)
109264ec8a8Sadam {
110ffae97bbSchristos 	int r;
111264ec8a8Sadam 
112ffae97bbSchristos 	if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
11317418e98Schristos 		fatal_fr(r, "enqueue");
114264ec8a8Sadam }
115264ec8a8Sadam 
116264ec8a8Sadam static void
117264ec8a8Sadam process_add(void)
118264ec8a8Sadam {
119264ec8a8Sadam 	char *name, *pin;
120aa36fcacSchristos 	struct sshkey **keys = NULL;
121ffae97bbSchristos 	int r, i, nkeys;
122264ec8a8Sadam 	u_char *blob;
123ffae97bbSchristos 	size_t blen;
124ffae97bbSchristos 	struct sshbuf *msg;
125ed75d7a8Schristos 	char **labels = NULL;
126264ec8a8Sadam 
127ffae97bbSchristos 	if ((msg = sshbuf_new()) == NULL)
12817418e98Schristos 		fatal_f("sshbuf_new failed");
129ffae97bbSchristos 	if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
130ffae97bbSchristos 	    (r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
13117418e98Schristos 		fatal_fr(r, "parse");
132ed75d7a8Schristos 	if ((nkeys = pkcs11_add_provider(name, pin, &keys, &labels)) > 0) {
133ffae97bbSchristos 		if ((r = sshbuf_put_u8(msg,
134ffae97bbSchristos 		    SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
135ffae97bbSchristos 		    (r = sshbuf_put_u32(msg, nkeys)) != 0)
13617418e98Schristos 			fatal_fr(r, "compose");
137264ec8a8Sadam 		for (i = 0; i < nkeys; i++) {
138ffae97bbSchristos 			if ((r = sshkey_to_blob(keys[i], &blob, &blen)) != 0) {
13917418e98Schristos 				debug_fr(r, "encode key");
14093118e33Schristos 				continue;
141ffae97bbSchristos 			}
142ffae97bbSchristos 			if ((r = sshbuf_put_string(msg, blob, blen)) != 0 ||
143ed75d7a8Schristos 			    (r = sshbuf_put_cstring(msg, labels[i])) != 0)
14417418e98Schristos 				fatal_fr(r, "compose key");
14500a838c4Schristos 			free(blob);
146ed75d7a8Schristos 			add_key(keys[i], name, labels[i]);
147ed75d7a8Schristos 			free(labels[i]);
148264ec8a8Sadam 		}
14917418e98Schristos 	} else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0 ||
15017418e98Schristos 	    (r = sshbuf_put_u32(msg, -nkeys)) != 0)
15117418e98Schristos 		fatal_fr(r, "compose");
152ed75d7a8Schristos 	free(labels);
153ed75d7a8Schristos 	free(keys); /* keys themselves are transferred to pkcs11_keylist */
15400a838c4Schristos 	free(pin);
15500a838c4Schristos 	free(name);
156ffae97bbSchristos 	send_msg(msg);
157ffae97bbSchristos 	sshbuf_free(msg);
158264ec8a8Sadam }
159264ec8a8Sadam 
160264ec8a8Sadam static void
161264ec8a8Sadam process_del(void)
162264ec8a8Sadam {
163264ec8a8Sadam 	char *name, *pin;
164ffae97bbSchristos 	struct sshbuf *msg;
165ffae97bbSchristos 	int r;
166264ec8a8Sadam 
167ffae97bbSchristos 	if ((msg = sshbuf_new()) == NULL)
16817418e98Schristos 		fatal_f("sshbuf_new failed");
169ffae97bbSchristos 	if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
170ffae97bbSchristos 	    (r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
17117418e98Schristos 		fatal_fr(r, "parse");
172264ec8a8Sadam 	del_keys_by_name(name);
173ffae97bbSchristos 	if ((r = sshbuf_put_u8(msg, pkcs11_del_provider(name) == 0 ?
174ffae97bbSchristos 	    SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
17517418e98Schristos 		fatal_fr(r, "compose");
17600a838c4Schristos 	free(pin);
17700a838c4Schristos 	free(name);
178ffae97bbSchristos 	send_msg(msg);
179ffae97bbSchristos 	sshbuf_free(msg);
180264ec8a8Sadam }
181264ec8a8Sadam 
182264ec8a8Sadam static void
183264ec8a8Sadam process_sign(void)
184264ec8a8Sadam {
185264ec8a8Sadam 	u_char *blob, *data, *signature = NULL;
186*9469f4f1Schristos 	size_t blen, dlen;
187*9469f4f1Schristos 	u_int slen = 0;
188*9469f4f1Schristos 	int len, r, ok = -1;
189*9469f4f1Schristos 	struct sshkey *key = NULL, *found;
190ffae97bbSchristos 	struct sshbuf *msg;
191*9469f4f1Schristos 	RSA *rsa = NULL;
192*9469f4f1Schristos 	EC_KEY *ecdsa = NULL;
193264ec8a8Sadam 
194ffae97bbSchristos 	/* XXX support SHA2 signature flags */
195ffae97bbSchristos 	if ((r = sshbuf_get_string(iqueue, &blob, &blen)) != 0 ||
196ffae97bbSchristos 	    (r = sshbuf_get_string(iqueue, &data, &dlen)) != 0 ||
197ffae97bbSchristos 	    (r = sshbuf_get_u32(iqueue, NULL)) != 0)
19817418e98Schristos 		fatal_fr(r, "parse");
199264ec8a8Sadam 
200ffae97bbSchristos 	if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
20117418e98Schristos 		fatal_fr(r, "decode key");
202*9469f4f1Schristos 	if ((found = lookup_key(key)) == NULL)
203*9469f4f1Schristos 		goto reply;
2048a4530f9Schristos 
205*9469f4f1Schristos 	/* XXX use pkey API properly for signing */
206*9469f4f1Schristos 	switch (key->type) {
207*9469f4f1Schristos 	case KEY_RSA:
208*9469f4f1Schristos 		if ((rsa = EVP_PKEY_get1_RSA(found->pkey)) == NULL)
209*9469f4f1Schristos 			fatal_f("no RSA in pkey");
210*9469f4f1Schristos 		if ((len = RSA_size(rsa)) < 0)
211*9469f4f1Schristos 			fatal_f("bad RSA length");
212*9469f4f1Schristos 		signature = xmalloc(len);
213*9469f4f1Schristos 		if ((len = RSA_private_encrypt(dlen, data, signature,
214*9469f4f1Schristos 		    rsa, RSA_PKCS1_PADDING)) < 0) {
215*9469f4f1Schristos 			error_f("RSA_private_encrypt failed");
216*9469f4f1Schristos 			goto reply;
217*9469f4f1Schristos 		}
218*9469f4f1Schristos 		slen = (u_int)len;
219*9469f4f1Schristos 		break;
220*9469f4f1Schristos 	case KEY_ECDSA:
221*9469f4f1Schristos 		if ((ecdsa = EVP_PKEY_get1_EC_KEY(found->pkey)) == NULL)
222*9469f4f1Schristos 			fatal_f("no ECDSA in pkey");
223*9469f4f1Schristos 		if ((len = ECDSA_size(ecdsa)) < 0)
224*9469f4f1Schristos 			fatal_f("bad ECDSA length");
225*9469f4f1Schristos 		slen = (u_int)len;
226264ec8a8Sadam 		signature = xmalloc(slen);
227aa36fcacSchristos 		/* "The parameter type is ignored." */
228*9469f4f1Schristos 		if (!ECDSA_sign(-1, data, dlen, signature, &slen, ecdsa)) {
229*9469f4f1Schristos 			error_f("ECDSA_sign failed");
230*9469f4f1Schristos 			goto reply;
231*9469f4f1Schristos 		}
232*9469f4f1Schristos 		break;
233*9469f4f1Schristos 	default:
234*9469f4f1Schristos 		fatal_f("unsupported key type %d", key->type);
235*9469f4f1Schristos 	}
236*9469f4f1Schristos 	/* success */
237aa36fcacSchristos 	ok = 0;
238*9469f4f1Schristos  reply:
239ffae97bbSchristos 	if ((msg = sshbuf_new()) == NULL)
24017418e98Schristos 		fatal_f("sshbuf_new failed");
241264ec8a8Sadam 	if (ok == 0) {
242ffae97bbSchristos 		if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
243ffae97bbSchristos 		    (r = sshbuf_put_string(msg, signature, slen)) != 0)
24417418e98Schristos 			fatal_fr(r, "compose response");
245264ec8a8Sadam 	} else {
246ffae97bbSchristos 		if ((r = sshbuf_put_u8(msg, SSH2_AGENT_FAILURE)) != 0)
24717418e98Schristos 			fatal_fr(r, "compose failure response");
248264ec8a8Sadam 	}
249*9469f4f1Schristos 	sshkey_free(key);
250*9469f4f1Schristos 	RSA_free(rsa);
251*9469f4f1Schristos 	EC_KEY_free(ecdsa);
25200a838c4Schristos 	free(data);
25300a838c4Schristos 	free(blob);
25400a838c4Schristos 	free(signature);
255ffae97bbSchristos 	send_msg(msg);
256ffae97bbSchristos 	sshbuf_free(msg);
257264ec8a8Sadam }
258264ec8a8Sadam 
259264ec8a8Sadam static void
260264ec8a8Sadam process(void)
261264ec8a8Sadam {
262264ec8a8Sadam 	u_int msg_len;
263264ec8a8Sadam 	u_int buf_len;
264264ec8a8Sadam 	u_int consumed;
265ffae97bbSchristos 	u_char type;
266ffae97bbSchristos 	const u_char *cp;
267ffae97bbSchristos 	int r;
268264ec8a8Sadam 
269ffae97bbSchristos 	buf_len = sshbuf_len(iqueue);
270264ec8a8Sadam 	if (buf_len < 5)
271264ec8a8Sadam 		return;		/* Incomplete message. */
272ffae97bbSchristos 	cp = sshbuf_ptr(iqueue);
273264ec8a8Sadam 	msg_len = get_u32(cp);
274264ec8a8Sadam 	if (msg_len > MAX_MSG_LENGTH) {
275264ec8a8Sadam 		error("bad message len %d", msg_len);
276264ec8a8Sadam 		cleanup_exit(11);
277264ec8a8Sadam 	}
278264ec8a8Sadam 	if (buf_len < msg_len + 4)
279264ec8a8Sadam 		return;
280ffae97bbSchristos 	if ((r = sshbuf_consume(iqueue, 4)) != 0 ||
281ffae97bbSchristos 	    (r = sshbuf_get_u8(iqueue, &type)) != 0)
28217418e98Schristos 		fatal_fr(r, "parse type/len");
283264ec8a8Sadam 	buf_len -= 4;
284264ec8a8Sadam 	switch (type) {
285264ec8a8Sadam 	case SSH_AGENTC_ADD_SMARTCARD_KEY:
286264ec8a8Sadam 		debug("process_add");
287264ec8a8Sadam 		process_add();
288264ec8a8Sadam 		break;
289264ec8a8Sadam 	case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
290264ec8a8Sadam 		debug("process_del");
291264ec8a8Sadam 		process_del();
292264ec8a8Sadam 		break;
293264ec8a8Sadam 	case SSH2_AGENTC_SIGN_REQUEST:
294264ec8a8Sadam 		debug("process_sign");
295264ec8a8Sadam 		process_sign();
296264ec8a8Sadam 		break;
297264ec8a8Sadam 	default:
298264ec8a8Sadam 		error("Unknown message %d", type);
299264ec8a8Sadam 		break;
300264ec8a8Sadam 	}
301264ec8a8Sadam 	/* discard the remaining bytes from the current packet */
302ffae97bbSchristos 	if (buf_len < sshbuf_len(iqueue)) {
303264ec8a8Sadam 		error("iqueue grew unexpectedly");
304264ec8a8Sadam 		cleanup_exit(255);
305264ec8a8Sadam 	}
306ffae97bbSchristos 	consumed = buf_len - sshbuf_len(iqueue);
307264ec8a8Sadam 	if (msg_len < consumed) {
308264ec8a8Sadam 		error("msg_len %d < consumed %d", msg_len, consumed);
309264ec8a8Sadam 		cleanup_exit(255);
310264ec8a8Sadam 	}
311ffae97bbSchristos 	if (msg_len > consumed) {
312ffae97bbSchristos 		if ((r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
31317418e98Schristos 			fatal_fr(r, "consume");
314ffae97bbSchristos 	}
315264ec8a8Sadam }
316264ec8a8Sadam 
317264ec8a8Sadam void
318264ec8a8Sadam cleanup_exit(int i)
319264ec8a8Sadam {
320264ec8a8Sadam 	/* XXX */
321264ec8a8Sadam 	_exit(i);
322264ec8a8Sadam }
323264ec8a8Sadam 
324aa36fcacSchristos 
325264ec8a8Sadam int
326264ec8a8Sadam main(int argc, char **argv)
327264ec8a8Sadam {
3288db691beSchristos 	int r, ch, in, out, log_stderr = 0;
329aa36fcacSchristos 	ssize_t len;
330264ec8a8Sadam 	SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
331264ec8a8Sadam 	LogLevel log_level = SYSLOG_LEVEL_ERROR;
332264ec8a8Sadam 	char buf[4*4096];
333264ec8a8Sadam 	extern char *__progname;
334aa36fcacSchristos 	struct pollfd pfd[2];
335264ec8a8Sadam 
336264ec8a8Sadam 	TAILQ_INIT(&pkcs11_keylist);
337264ec8a8Sadam 
338264ec8a8Sadam 	log_init(__progname, log_level, log_facility, log_stderr);
339264ec8a8Sadam 
340aa36fcacSchristos 	while ((ch = getopt(argc, argv, "v")) != -1) {
341aa36fcacSchristos 		switch (ch) {
342aa36fcacSchristos 		case 'v':
343aa36fcacSchristos 			log_stderr = 1;
344aa36fcacSchristos 			if (log_level == SYSLOG_LEVEL_ERROR)
345aa36fcacSchristos 				log_level = SYSLOG_LEVEL_DEBUG1;
346aa36fcacSchristos 			else if (log_level < SYSLOG_LEVEL_DEBUG3)
347aa36fcacSchristos 				log_level++;
348aa36fcacSchristos 			break;
349aa36fcacSchristos 		default:
350aa36fcacSchristos 			fprintf(stderr, "usage: %s [-v]\n", __progname);
351aa36fcacSchristos 			exit(1);
352aa36fcacSchristos 		}
353aa36fcacSchristos 	}
354aa36fcacSchristos 
355aa36fcacSchristos 	log_init(__progname, log_level, log_facility, log_stderr);
356aa36fcacSchristos 
357aa36fcacSchristos 	pkcs11_init(0);
358264ec8a8Sadam 	in = STDIN_FILENO;
359264ec8a8Sadam 	out = STDOUT_FILENO;
360264ec8a8Sadam 
361ffae97bbSchristos 	if ((iqueue = sshbuf_new()) == NULL)
36217418e98Schristos 		fatal_f("sshbuf_new failed");
363ffae97bbSchristos 	if ((oqueue = sshbuf_new()) == NULL)
36417418e98Schristos 		fatal_f("sshbuf_new failed");
365264ec8a8Sadam 
366aa36fcacSchristos 	while (1) {
367aa36fcacSchristos 		memset(pfd, 0, sizeof(pfd));
368aa36fcacSchristos 		pfd[0].fd = in;
369aa36fcacSchristos 		pfd[1].fd = out;
370264ec8a8Sadam 
371264ec8a8Sadam 		/*
372264ec8a8Sadam 		 * Ensure that we can read a full buffer and handle
373264ec8a8Sadam 		 * the worst-case length packet it can generate,
374264ec8a8Sadam 		 * otherwise apply backpressure by stopping reads.
375264ec8a8Sadam 		 */
376ffae97bbSchristos 		if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
377ffae97bbSchristos 		    (r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
378aa36fcacSchristos 			pfd[0].events = POLLIN;
379ffae97bbSchristos 		else if (r != SSH_ERR_NO_BUFFER_SPACE)
38017418e98Schristos 			fatal_fr(r, "reserve");
381264ec8a8Sadam 
382aa36fcacSchristos 		if (sshbuf_len(oqueue) > 0)
383aa36fcacSchristos 			pfd[1].events = POLLOUT;
384264ec8a8Sadam 
385aa36fcacSchristos 		if ((r = poll(pfd, 2, -1 /* INFTIM */)) <= 0) {
386aa36fcacSchristos 			if (r == 0 || errno == EINTR)
387264ec8a8Sadam 				continue;
388aa36fcacSchristos 			fatal("poll: %s", strerror(errno));
389264ec8a8Sadam 		}
390264ec8a8Sadam 
391264ec8a8Sadam 		/* copy stdin to iqueue */
392a03ec00cSchristos 		if ((pfd[0].revents & (POLLIN|POLLHUP|POLLERR)) != 0) {
393264ec8a8Sadam 			len = read(in, buf, sizeof buf);
394264ec8a8Sadam 			if (len == 0) {
395264ec8a8Sadam 				debug("read eof");
396264ec8a8Sadam 				cleanup_exit(0);
397264ec8a8Sadam 			} else if (len < 0) {
398264ec8a8Sadam 				error("read: %s", strerror(errno));
399264ec8a8Sadam 				cleanup_exit(1);
40017418e98Schristos 			} else if ((r = sshbuf_put(iqueue, buf, len)) != 0)
40117418e98Schristos 				fatal_fr(r, "sshbuf_put");
402264ec8a8Sadam 		}
403264ec8a8Sadam 		/* send oqueue to stdout */
404aa36fcacSchristos 		if ((pfd[1].revents & (POLLOUT|POLLHUP)) != 0) {
405aa36fcacSchristos 			len = write(out, sshbuf_ptr(oqueue),
406aa36fcacSchristos 			    sshbuf_len(oqueue));
407264ec8a8Sadam 			if (len < 0) {
408264ec8a8Sadam 				error("write: %s", strerror(errno));
409264ec8a8Sadam 				cleanup_exit(1);
41017418e98Schristos 			} else if ((r = sshbuf_consume(oqueue, len)) != 0)
41117418e98Schristos 				fatal_fr(r, "consume");
412264ec8a8Sadam 		}
413264ec8a8Sadam 
414264ec8a8Sadam 		/*
415264ec8a8Sadam 		 * Process requests from client if we can fit the results
416264ec8a8Sadam 		 * into the output buffer, otherwise stop processing input
417264ec8a8Sadam 		 * and let the output queue drain.
418264ec8a8Sadam 		 */
419ffae97bbSchristos 		if ((r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
420264ec8a8Sadam 			process();
421ffae97bbSchristos 		else if (r != SSH_ERR_NO_BUFFER_SPACE)
42217418e98Schristos 			fatal_fr(r, "reserve");
423264ec8a8Sadam 	}
424264ec8a8Sadam }
425cd4ada6aSchristos 
426cd4ada6aSchristos #else /* WITH_OPENSSL */
427cd4ada6aSchristos void
428cd4ada6aSchristos cleanup_exit(int i)
429cd4ada6aSchristos {
430cd4ada6aSchristos 	_exit(i);
431cd4ada6aSchristos }
432cd4ada6aSchristos 
433cd4ada6aSchristos int
434cd4ada6aSchristos main(int argc, char **argv)
435cd4ada6aSchristos {
436cd4ada6aSchristos 	fprintf(stderr, "PKCS#11 code is not enabled\n");
437cd4ada6aSchristos 	return 1;
438cd4ada6aSchristos }
439cd4ada6aSchristos #endif /* WITH_OPENSSL */
440