2021-08-30 19:14:33 +00:00
|
|
|
/* $OpenBSD: ssh-pkcs11-helper.c,v 1.25 2021/08/11 05:20:17 djm Exp $ */
|
2010-03-08 11:19:52 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#ifdef HAVE_SYS_TIME_H
|
|
|
|
# include <sys/time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "openbsd-compat/sys-queue.h"
|
|
|
|
|
2021-02-14 21:00:25 +00:00
|
|
|
#include <stdlib.h>
|
2020-02-14 19:47:15 +00:00
|
|
|
#include <errno.h>
|
2021-02-14 21:04:52 +00:00
|
|
|
#ifdef HAVE_POLL_H
|
2020-02-14 19:47:15 +00:00
|
|
|
#include <poll.h>
|
2021-02-14 21:04:52 +00:00
|
|
|
#endif
|
2010-03-08 11:19:52 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "xmalloc.h"
|
2018-05-06 12:27:04 +00:00
|
|
|
#include "sshbuf.h"
|
2010-03-08 11:19:52 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "misc.h"
|
2018-05-06 12:27:04 +00:00
|
|
|
#include "sshkey.h"
|
2010-03-08 11:19:52 +00:00
|
|
|
#include "authfd.h"
|
|
|
|
#include "ssh-pkcs11.h"
|
2018-05-06 12:27:04 +00:00
|
|
|
#include "ssherr.h"
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2010-04-28 08:37:00 +00:00
|
|
|
#ifdef ENABLE_PKCS11
|
|
|
|
|
2021-02-14 21:00:25 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
|
2010-03-08 11:19:52 +00:00
|
|
|
/* borrows code from sftp-server and ssh-agent */
|
|
|
|
|
|
|
|
struct pkcs11_keyinfo {
|
2018-05-06 12:24:45 +00:00
|
|
|
struct sshkey *key;
|
2021-02-14 21:04:52 +00:00
|
|
|
char *providername, *label;
|
2010-03-08 11:19:52 +00:00
|
|
|
TAILQ_ENTRY(pkcs11_keyinfo) next;
|
|
|
|
};
|
|
|
|
|
|
|
|
TAILQ_HEAD(, pkcs11_keyinfo) pkcs11_keylist;
|
|
|
|
|
|
|
|
#define MAX_MSG_LENGTH 10240 /*XXX*/
|
|
|
|
|
|
|
|
/* input and output queue */
|
2018-05-06 12:27:04 +00:00
|
|
|
struct sshbuf *iqueue;
|
|
|
|
struct sshbuf *oqueue;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
|
|
|
static void
|
2021-02-14 21:04:52 +00:00
|
|
|
add_key(struct sshkey *k, char *name, char *label)
|
2010-03-08 11:19:52 +00:00
|
|
|
{
|
|
|
|
struct pkcs11_keyinfo *ki;
|
|
|
|
|
|
|
|
ki = xcalloc(1, sizeof(*ki));
|
|
|
|
ki->providername = xstrdup(name);
|
|
|
|
ki->key = k;
|
2021-02-14 21:04:52 +00:00
|
|
|
ki->label = xstrdup(label);
|
2010-03-08 11:19:52 +00:00
|
|
|
TAILQ_INSERT_TAIL(&pkcs11_keylist, ki, next);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
del_keys_by_name(char *name)
|
|
|
|
{
|
|
|
|
struct pkcs11_keyinfo *ki, *nxt;
|
|
|
|
|
|
|
|
for (ki = TAILQ_FIRST(&pkcs11_keylist); ki; ki = nxt) {
|
|
|
|
nxt = TAILQ_NEXT(ki, next);
|
|
|
|
if (!strcmp(ki->providername, name)) {
|
|
|
|
TAILQ_REMOVE(&pkcs11_keylist, ki, next);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(ki->providername);
|
2021-02-14 21:04:52 +00:00
|
|
|
free(ki->label);
|
2018-05-06 12:27:04 +00:00
|
|
|
sshkey_free(ki->key);
|
2010-03-08 11:19:52 +00:00
|
|
|
free(ki);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lookup matching 'private' key */
|
2018-05-06 12:24:45 +00:00
|
|
|
static struct sshkey *
|
|
|
|
lookup_key(struct sshkey *k)
|
2010-03-08 11:19:52 +00:00
|
|
|
{
|
|
|
|
struct pkcs11_keyinfo *ki;
|
|
|
|
|
|
|
|
TAILQ_FOREACH(ki, &pkcs11_keylist, next) {
|
2021-08-30 19:14:33 +00:00
|
|
|
debug("check %s %s %s", sshkey_type(ki->key),
|
|
|
|
ki->providername, ki->label);
|
2018-05-06 12:27:04 +00:00
|
|
|
if (sshkey_equal(k, ki->key))
|
2010-03-08 11:19:52 +00:00
|
|
|
return (ki->key);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-05-06 12:27:04 +00:00
|
|
|
send_msg(struct sshbuf *m)
|
2010-03-08 11:19:52 +00:00
|
|
|
{
|
2018-05-06 12:27:04 +00:00
|
|
|
int r;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "enqueue");
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_add(void)
|
|
|
|
{
|
|
|
|
char *name, *pin;
|
2020-02-14 19:47:15 +00:00
|
|
|
struct sshkey **keys = NULL;
|
2018-05-06 12:27:04 +00:00
|
|
|
int r, i, nkeys;
|
2010-03-08 11:19:52 +00:00
|
|
|
u_char *blob;
|
2018-05-06 12:27:04 +00:00
|
|
|
size_t blen;
|
|
|
|
struct sshbuf *msg;
|
2021-02-14 21:04:52 +00:00
|
|
|
char **labels = NULL;
|
2018-05-06 12:27:04 +00:00
|
|
|
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "parse");
|
2021-02-14 21:04:52 +00:00
|
|
|
if ((nkeys = pkcs11_add_provider(name, pin, &keys, &labels)) > 0) {
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg,
|
|
|
|
SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, nkeys)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "compose");
|
2010-03-08 11:19:52 +00:00
|
|
|
for (i = 0; i < nkeys; i++) {
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshkey_to_blob(keys[i], &blob, &blen)) != 0) {
|
2021-04-23 19:10:38 +00:00
|
|
|
debug_fr(r, "encode key");
|
2014-01-30 10:56:49 +00:00
|
|
|
continue;
|
2018-05-06 12:27:04 +00:00
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_string(msg, blob, blen)) != 0 ||
|
2021-02-14 21:04:52 +00:00
|
|
|
(r = sshbuf_put_cstring(msg, labels[i])) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "compose key");
|
2013-09-18 17:27:38 +00:00
|
|
|
free(blob);
|
2021-02-14 21:04:52 +00:00
|
|
|
add_key(keys[i], name, labels[i]);
|
|
|
|
free(labels[i]);
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
2021-04-23 19:10:38 +00:00
|
|
|
} else if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, -nkeys)) != 0)
|
|
|
|
fatal_fr(r, "compose");
|
2021-02-14 21:04:52 +00:00
|
|
|
free(labels);
|
|
|
|
free(keys); /* keys themselves are transferred to pkcs11_keylist */
|
2013-09-18 17:27:38 +00:00
|
|
|
free(pin);
|
|
|
|
free(name);
|
2018-05-06 12:27:04 +00:00
|
|
|
send_msg(msg);
|
|
|
|
sshbuf_free(msg);
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_del(void)
|
|
|
|
{
|
|
|
|
char *name, *pin;
|
2018-05-06 12:27:04 +00:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "parse");
|
2010-03-08 11:19:52 +00:00
|
|
|
del_keys_by_name(name);
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, pkcs11_del_provider(name) == 0 ?
|
|
|
|
SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "compose");
|
2013-09-18 17:27:38 +00:00
|
|
|
free(pin);
|
|
|
|
free(name);
|
2018-05-06 12:27:04 +00:00
|
|
|
send_msg(msg);
|
|
|
|
sshbuf_free(msg);
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_sign(void)
|
|
|
|
{
|
|
|
|
u_char *blob, *data, *signature = NULL;
|
2018-05-06 12:27:04 +00:00
|
|
|
size_t blen, dlen, slen = 0;
|
|
|
|
int r, ok = -1;
|
2018-05-06 12:24:45 +00:00
|
|
|
struct sshkey *key, *found;
|
2018-05-06 12:27:04 +00:00
|
|
|
struct sshbuf *msg;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2018-05-06 12:27:04 +00:00
|
|
|
/* XXX support SHA2 signature flags */
|
|
|
|
if ((r = sshbuf_get_string(iqueue, &blob, &blen)) != 0 ||
|
|
|
|
(r = sshbuf_get_string(iqueue, &data, &dlen)) != 0 ||
|
|
|
|
(r = sshbuf_get_u32(iqueue, NULL)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "parse");
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "decode key");
|
2018-05-06 12:27:04 +00:00
|
|
|
else {
|
2010-03-08 11:19:52 +00:00
|
|
|
if ((found = lookup_key(key)) != NULL) {
|
2015-01-05 16:09:55 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
|
|
|
int ret;
|
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
if (key->type == KEY_RSA) {
|
|
|
|
slen = RSA_size(key->rsa);
|
|
|
|
signature = xmalloc(slen);
|
|
|
|
ret = RSA_private_encrypt(dlen, data, signature,
|
|
|
|
found->rsa, RSA_PKCS1_PADDING);
|
|
|
|
if (ret != -1) {
|
|
|
|
slen = ret;
|
|
|
|
ok = 0;
|
|
|
|
}
|
2021-02-14 21:00:25 +00:00
|
|
|
#ifdef OPENSSL_HAS_ECC
|
2020-02-14 19:47:15 +00:00
|
|
|
} else if (key->type == KEY_ECDSA) {
|
2021-02-14 21:00:25 +00:00
|
|
|
u_int xslen = ECDSA_size(key->ecdsa);
|
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
signature = xmalloc(xslen);
|
|
|
|
/* "The parameter type is ignored." */
|
|
|
|
ret = ECDSA_sign(-1, data, dlen, signature,
|
|
|
|
&xslen, found->ecdsa);
|
|
|
|
if (ret != 0)
|
|
|
|
ok = 0;
|
|
|
|
else
|
2021-04-23 19:10:38 +00:00
|
|
|
error_f("ECDSA_sign returned %d", ret);
|
2020-02-14 19:47:15 +00:00
|
|
|
slen = xslen;
|
2021-02-14 21:00:25 +00:00
|
|
|
#endif /* OPENSSL_HAS_ECC */
|
2020-02-14 19:47:15 +00:00
|
|
|
} else
|
2021-04-23 19:10:38 +00:00
|
|
|
error_f("don't know how to sign with key "
|
|
|
|
"type %d", (int)key->type);
|
2015-01-05 16:09:55 +00:00
|
|
|
#endif /* WITH_OPENSSL */
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
2018-05-06 12:27:04 +00:00
|
|
|
sshkey_free(key);
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2010-03-08 11:19:52 +00:00
|
|
|
if (ok == 0) {
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(msg, signature, slen)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "compose response");
|
2010-03-08 11:19:52 +00:00
|
|
|
} else {
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENT_FAILURE)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "compose failure response");
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
2013-09-18 17:27:38 +00:00
|
|
|
free(data);
|
|
|
|
free(blob);
|
|
|
|
free(signature);
|
2018-05-06 12:27:04 +00:00
|
|
|
send_msg(msg);
|
|
|
|
sshbuf_free(msg);
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
process(void)
|
|
|
|
{
|
|
|
|
u_int msg_len;
|
|
|
|
u_int buf_len;
|
|
|
|
u_int consumed;
|
2018-05-06 12:27:04 +00:00
|
|
|
u_char type;
|
|
|
|
const u_char *cp;
|
|
|
|
int r;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2018-05-06 12:27:04 +00:00
|
|
|
buf_len = sshbuf_len(iqueue);
|
2010-03-08 11:19:52 +00:00
|
|
|
if (buf_len < 5)
|
|
|
|
return; /* Incomplete message. */
|
2018-05-06 12:27:04 +00:00
|
|
|
cp = sshbuf_ptr(iqueue);
|
2010-03-08 11:19:52 +00:00
|
|
|
msg_len = get_u32(cp);
|
|
|
|
if (msg_len > MAX_MSG_LENGTH) {
|
|
|
|
error("bad message len %d", msg_len);
|
|
|
|
cleanup_exit(11);
|
|
|
|
}
|
|
|
|
if (buf_len < msg_len + 4)
|
|
|
|
return;
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_consume(iqueue, 4)) != 0 ||
|
|
|
|
(r = sshbuf_get_u8(iqueue, &type)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "parse type/len");
|
2010-03-08 11:19:52 +00:00
|
|
|
buf_len -= 4;
|
|
|
|
switch (type) {
|
|
|
|
case SSH_AGENTC_ADD_SMARTCARD_KEY:
|
|
|
|
debug("process_add");
|
|
|
|
process_add();
|
|
|
|
break;
|
|
|
|
case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
|
|
|
|
debug("process_del");
|
|
|
|
process_del();
|
|
|
|
break;
|
|
|
|
case SSH2_AGENTC_SIGN_REQUEST:
|
|
|
|
debug("process_sign");
|
|
|
|
process_sign();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error("Unknown message %d", type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* discard the remaining bytes from the current packet */
|
2018-05-06 12:27:04 +00:00
|
|
|
if (buf_len < sshbuf_len(iqueue)) {
|
2010-03-08 11:19:52 +00:00
|
|
|
error("iqueue grew unexpectedly");
|
|
|
|
cleanup_exit(255);
|
|
|
|
}
|
2018-05-06 12:27:04 +00:00
|
|
|
consumed = buf_len - sshbuf_len(iqueue);
|
2010-03-08 11:19:52 +00:00
|
|
|
if (msg_len < consumed) {
|
|
|
|
error("msg_len %d < consumed %d", msg_len, consumed);
|
|
|
|
cleanup_exit(255);
|
|
|
|
}
|
2018-05-06 12:27:04 +00:00
|
|
|
if (msg_len > consumed) {
|
|
|
|
if ((r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "consume");
|
2018-05-06 12:27:04 +00:00
|
|
|
}
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cleanup_exit(int i)
|
|
|
|
{
|
|
|
|
/* XXX */
|
|
|
|
_exit(i);
|
|
|
|
}
|
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
|
2010-03-08 11:19:52 +00:00
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
2021-02-14 21:07:21 +00:00
|
|
|
int r, ch, in, out, log_stderr = 0;
|
2020-02-14 19:47:15 +00:00
|
|
|
ssize_t len;
|
2010-03-08 11:19:52 +00:00
|
|
|
SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
|
|
|
|
LogLevel log_level = SYSLOG_LEVEL_ERROR;
|
|
|
|
char buf[4*4096];
|
|
|
|
extern char *__progname;
|
2020-02-14 19:47:15 +00:00
|
|
|
struct pollfd pfd[2];
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
__progname = ssh_get_progname(argv[0]);
|
|
|
|
seed_rng();
|
2010-03-08 11:19:52 +00:00
|
|
|
TAILQ_INIT(&pkcs11_keylist);
|
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
log_init(__progname, log_level, log_facility, log_stderr);
|
|
|
|
|
|
|
|
while ((ch = getopt(argc, argv, "v")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'v':
|
|
|
|
log_stderr = 1;
|
|
|
|
if (log_level == SYSLOG_LEVEL_ERROR)
|
|
|
|
log_level = SYSLOG_LEVEL_DEBUG1;
|
|
|
|
else if (log_level < SYSLOG_LEVEL_DEBUG3)
|
|
|
|
log_level++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "usage: %s [-v]\n", __progname);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2010-03-08 11:19:52 +00:00
|
|
|
|
|
|
|
log_init(__progname, log_level, log_facility, log_stderr);
|
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
pkcs11_init(0);
|
2010-03-08 11:19:52 +00:00
|
|
|
in = STDIN_FILENO;
|
|
|
|
out = STDOUT_FILENO;
|
|
|
|
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((iqueue = sshbuf_new()) == NULL)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((oqueue = sshbuf_new()) == NULL)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_f("sshbuf_new failed");
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
while (1) {
|
|
|
|
memset(pfd, 0, sizeof(pfd));
|
|
|
|
pfd[0].fd = in;
|
|
|
|
pfd[1].fd = out;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Ensure that we can read a full buffer and handle
|
|
|
|
* the worst-case length packet it can generate,
|
|
|
|
* otherwise apply backpressure by stopping reads.
|
|
|
|
*/
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
|
|
|
|
(r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
|
2020-02-14 19:47:15 +00:00
|
|
|
pfd[0].events = POLLIN;
|
2018-05-06 12:27:04 +00:00
|
|
|
else if (r != SSH_ERR_NO_BUFFER_SPACE)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "reserve");
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
if (sshbuf_len(oqueue) > 0)
|
|
|
|
pfd[1].events = POLLOUT;
|
2010-03-08 11:19:52 +00:00
|
|
|
|
2020-02-14 19:47:15 +00:00
|
|
|
if ((r = poll(pfd, 2, -1 /* INFTIM */)) <= 0) {
|
|
|
|
if (r == 0 || errno == EINTR)
|
2010-03-08 11:19:52 +00:00
|
|
|
continue;
|
2020-02-14 19:47:15 +00:00
|
|
|
fatal("poll: %s", strerror(errno));
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy stdin to iqueue */
|
2020-02-14 19:47:15 +00:00
|
|
|
if ((pfd[0].revents & (POLLIN|POLLERR)) != 0) {
|
2010-03-08 11:19:52 +00:00
|
|
|
len = read(in, buf, sizeof buf);
|
|
|
|
if (len == 0) {
|
|
|
|
debug("read eof");
|
|
|
|
cleanup_exit(0);
|
|
|
|
} else if (len < 0) {
|
|
|
|
error("read: %s", strerror(errno));
|
|
|
|
cleanup_exit(1);
|
2021-04-23 19:10:38 +00:00
|
|
|
} else if ((r = sshbuf_put(iqueue, buf, len)) != 0)
|
|
|
|
fatal_fr(r, "sshbuf_put");
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
/* send oqueue to stdout */
|
2020-02-14 19:47:15 +00:00
|
|
|
if ((pfd[1].revents & (POLLOUT|POLLHUP)) != 0) {
|
|
|
|
len = write(out, sshbuf_ptr(oqueue),
|
|
|
|
sshbuf_len(oqueue));
|
2010-03-08 11:19:52 +00:00
|
|
|
if (len < 0) {
|
|
|
|
error("write: %s", strerror(errno));
|
|
|
|
cleanup_exit(1);
|
2021-04-23 19:10:38 +00:00
|
|
|
} else if ((r = sshbuf_consume(oqueue, len)) != 0)
|
|
|
|
fatal_fr(r, "consume");
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process requests from client if we can fit the results
|
|
|
|
* into the output buffer, otherwise stop processing input
|
|
|
|
* and let the output queue drain.
|
|
|
|
*/
|
2018-05-06 12:27:04 +00:00
|
|
|
if ((r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
|
2010-03-08 11:19:52 +00:00
|
|
|
process();
|
2018-05-06 12:27:04 +00:00
|
|
|
else if (r != SSH_ERR_NO_BUFFER_SPACE)
|
2021-04-23 19:10:38 +00:00
|
|
|
fatal_fr(r, "reserve");
|
2010-03-08 11:19:52 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-14 21:00:25 +00:00
|
|
|
|
|
|
|
#else /* WITH_OPENSSL */
|
|
|
|
void
|
|
|
|
cleanup_exit(int i)
|
|
|
|
{
|
|
|
|
_exit(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "PKCS#11 code is not enabled\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif /* WITH_OPENSSL */
|
2010-03-08 11:19:52 +00:00
|
|
|
#else /* ENABLE_PKCS11 */
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
extern char *__progname;
|
|
|
|
|
|
|
|
__progname = ssh_get_progname(argv[0]);
|
|
|
|
log_init(__progname, SYSLOG_LEVEL_ERROR, SYSLOG_FACILITY_AUTH, 0);
|
|
|
|
fatal("PKCS#11 support disabled at compile time");
|
|
|
|
}
|
|
|
|
#endif /* ENABLE_PKCS11 */
|