2020-11-21 23:58:00 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2021-11-17 20:38:21 +01:00
|
|
|
Copyright (C) 2020, 2021 Taler Systems SA
|
2020-11-21 23:58:00 +01:00
|
|
|
|
|
|
|
TALER is free software; you can redistribute it and/or modify it under the
|
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
|
|
|
TALER is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
|
|
|
/**
|
2020-11-22 22:25:49 +01:00
|
|
|
* @file util/crypto_helper_denom.c
|
2020-11-21 23:58:00 +01:00
|
|
|
* @brief utility functions for running out-of-process private key operations
|
|
|
|
* @author Christian Grothoff
|
|
|
|
*/
|
|
|
|
#include "platform.h"
|
|
|
|
#include "taler_util.h"
|
2020-11-23 16:36:07 +01:00
|
|
|
#include "taler_signatures.h"
|
2021-01-17 20:07:55 +01:00
|
|
|
#include "taler-exchange-secmod-rsa.h"
|
2020-12-12 11:34:57 +01:00
|
|
|
#include <poll.h>
|
2021-11-17 20:38:21 +01:00
|
|
|
#include "crypto_helper_common.h"
|
2020-11-21 23:58:00 +01:00
|
|
|
|
|
|
|
|
2021-11-17 23:02:05 +01:00
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Function to call with updates to available key material.
|
|
|
|
*/
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_RsaDenominationKeyStatusCallback dkc;
|
2020-11-21 23:58:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Closure for @e dkc
|
|
|
|
*/
|
|
|
|
void *dkc_cls;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket address of the denomination helper process.
|
|
|
|
* Used to reconnect if the connection breaks.
|
|
|
|
*/
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The UNIX domain socket, -1 if we are currently not connected.
|
|
|
|
*/
|
|
|
|
int sock;
|
2020-12-12 22:40:40 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Have we ever been sync'ed?
|
|
|
|
*/
|
|
|
|
bool synced;
|
2020-11-21 23:58:00 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnect from the helper process. Updates
|
|
|
|
* @e sock field in @a dh.
|
|
|
|
*
|
|
|
|
* @param[in,out] dh handle to tear down connection of
|
|
|
|
*/
|
|
|
|
static void
|
2021-11-17 23:02:05 +01:00
|
|
|
do_disconnect (struct TALER_CRYPTO_RsaDenominationHelper *dh)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
GNUNET_break (0 == close (dh->sock));
|
|
|
|
dh->sock = -1;
|
2021-11-17 11:26:19 +01:00
|
|
|
dh->synced = false;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Try to connect to the helper process. Updates
|
|
|
|
* @e sock field in @a dh.
|
|
|
|
*
|
|
|
|
* @param[in,out] dh handle to establish connection for
|
2021-11-17 20:38:21 +01:00
|
|
|
* @return #GNUNET_OK on success
|
2020-11-21 23:58:00 +01:00
|
|
|
*/
|
2021-11-17 20:38:21 +01:00
|
|
|
static enum GNUNET_GenericReturnValue
|
2021-11-17 23:02:05 +01:00
|
|
|
try_connect (struct TALER_CRYPTO_RsaDenominationHelper *dh)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
if (-1 != dh->sock)
|
2021-11-17 20:38:21 +01:00
|
|
|
return GNUNET_OK;
|
2020-11-21 23:58:00 +01:00
|
|
|
dh->sock = socket (AF_UNIX,
|
2021-11-17 20:38:21 +01:00
|
|
|
SOCK_STREAM,
|
2020-11-21 23:58:00 +01:00
|
|
|
0);
|
|
|
|
if (-1 == dh->sock)
|
|
|
|
{
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"socket");
|
2021-11-17 20:38:21 +01:00
|
|
|
return GNUNET_SYSERR;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
if (0 !=
|
|
|
|
connect (dh->sock,
|
|
|
|
(const struct sockaddr *) &dh->sa,
|
|
|
|
sizeof (dh->sa)))
|
2020-11-22 18:31:33 +01:00
|
|
|
{
|
2021-01-06 10:22:49 +01:00
|
|
|
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
|
2021-11-17 20:38:21 +01:00
|
|
|
"connect",
|
|
|
|
dh->sa.sun_path);
|
2020-11-22 18:31:33 +01:00
|
|
|
do_disconnect (dh);
|
2021-11-17 20:38:21 +01:00
|
|
|
return GNUNET_SYSERR;
|
2020-11-22 18:31:33 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
return GNUNET_OK;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-11-17 23:02:05 +01:00
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper *
|
|
|
|
TALER_CRYPTO_helper_rsa_connect (
|
2020-11-21 23:58:00 +01:00
|
|
|
const struct GNUNET_CONFIGURATION_Handle *cfg,
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_RsaDenominationKeyStatusCallback dkc,
|
2020-11-21 23:58:00 +01:00
|
|
|
void *dkc_cls)
|
|
|
|
{
|
2021-11-17 23:02:05 +01:00
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper *dh;
|
2020-11-21 23:58:00 +01:00
|
|
|
char *unixpath;
|
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
GNUNET_CONFIGURATION_get_value_filename (cfg,
|
2021-01-17 20:07:55 +01:00
|
|
|
"taler-exchange-secmod-rsa",
|
2020-11-21 23:58:00 +01:00
|
|
|
"UNIXPATH",
|
|
|
|
&unixpath))
|
|
|
|
{
|
|
|
|
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
|
2021-01-17 20:07:55 +01:00
|
|
|
"taler-exchange-secmod-rsa",
|
2020-11-21 23:58:00 +01:00
|
|
|
"UNIXPATH");
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-11-22 18:31:33 +01:00
|
|
|
/* we use >= here because we want the sun_path to always
|
|
|
|
be 0-terminated */
|
|
|
|
if (strlen (unixpath) >= sizeof (dh->sa.sun_path))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
|
2021-01-17 20:07:55 +01:00
|
|
|
"taler-exchange-secmod-rsa",
|
2020-11-21 23:58:00 +01:00
|
|
|
"UNIXPATH",
|
|
|
|
"path too long");
|
|
|
|
GNUNET_free (unixpath);
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-11-17 23:02:05 +01:00
|
|
|
dh = GNUNET_new (struct TALER_CRYPTO_RsaDenominationHelper);
|
2020-11-21 23:58:00 +01:00
|
|
|
dh->dkc = dkc;
|
|
|
|
dh->dkc_cls = dkc_cls;
|
2020-11-22 18:31:33 +01:00
|
|
|
dh->sa.sun_family = AF_UNIX;
|
2020-11-21 23:58:00 +01:00
|
|
|
strncpy (dh->sa.sun_path,
|
|
|
|
unixpath,
|
2021-01-15 12:18:27 +01:00
|
|
|
sizeof (dh->sa.sun_path) - 1);
|
2021-02-13 16:24:38 +01:00
|
|
|
GNUNET_free (unixpath);
|
2020-11-21 23:58:00 +01:00
|
|
|
dh->sock = -1;
|
2021-11-17 20:31:08 +01:00
|
|
|
if (GNUNET_OK !=
|
2021-11-17 20:38:21 +01:00
|
|
|
try_connect (dh))
|
2020-11-22 18:31:33 +01:00
|
|
|
{
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_disconnect (dh);
|
2021-11-17 20:31:08 +01:00
|
|
|
return NULL;
|
2020-11-22 18:31:33 +01:00
|
|
|
}
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_poll (dh);
|
2020-11-21 23:58:00 +01:00
|
|
|
return dh;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-22 22:25:49 +01:00
|
|
|
/**
|
|
|
|
* Handle a #TALER_HELPER_RSA_MT_AVAIL message from the helper.
|
|
|
|
*
|
|
|
|
* @param dh helper context
|
|
|
|
* @param hdr message that we received
|
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
2021-10-22 23:15:04 +02:00
|
|
|
static enum GNUNET_GenericReturnValue
|
2021-11-17 23:02:05 +01:00
|
|
|
handle_mt_avail (struct TALER_CRYPTO_RsaDenominationHelper *dh,
|
2020-11-22 22:25:49 +01:00
|
|
|
const struct GNUNET_MessageHeader *hdr)
|
|
|
|
{
|
|
|
|
const struct TALER_CRYPTO_RsaKeyAvailableNotification *kan
|
|
|
|
= (const struct TALER_CRYPTO_RsaKeyAvailableNotification *) hdr;
|
|
|
|
const char *buf = (const char *) &kan[1];
|
|
|
|
const char *section_name;
|
2021-11-19 11:47:52 +01:00
|
|
|
uint16_t ps;
|
|
|
|
uint16_t snl;
|
2020-11-22 22:25:49 +01:00
|
|
|
|
|
|
|
if (sizeof (*kan) > ntohs (hdr->size))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2021-11-19 11:47:52 +01:00
|
|
|
ps = ntohs (kan->pub_size);
|
|
|
|
snl = ntohs (kan->section_name_len);
|
|
|
|
if (ntohs (hdr->size) != sizeof (*kan) + ps + snl)
|
2020-11-22 22:25:49 +01:00
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2021-11-19 11:47:52 +01:00
|
|
|
if (0 == snl)
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
section_name = &buf[ps];
|
|
|
|
if ('\0' != section_name[snl - 1])
|
2020-11-22 22:25:49 +01:00
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
struct TALER_DenominationPublicKey denom_pub;
|
2021-11-17 23:02:05 +01:00
|
|
|
struct TALER_RsaPubHashP h_rsa;
|
2020-11-22 22:25:49 +01:00
|
|
|
|
2021-10-22 23:15:04 +02:00
|
|
|
denom_pub.cipher = TALER_DENOMINATION_RSA;
|
|
|
|
denom_pub.details.rsa_public_key
|
2020-11-22 22:25:49 +01:00
|
|
|
= GNUNET_CRYPTO_rsa_public_key_decode (buf,
|
|
|
|
ntohs (kan->pub_size));
|
2021-10-22 23:15:04 +02:00
|
|
|
if (NULL == denom_pub.details.rsa_public_key)
|
2020-11-22 22:25:49 +01:00
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
GNUNET_CRYPTO_rsa_public_key_hash (denom_pub.details.rsa_public_key,
|
2021-11-17 23:02:05 +01:00
|
|
|
&h_rsa.hash);
|
2020-12-31 19:18:31 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Received RSA key %s (%s)\n",
|
2021-11-17 23:02:05 +01:00
|
|
|
GNUNET_h2s (&h_rsa.hash),
|
2020-12-31 19:18:31 +01:00
|
|
|
section_name);
|
2020-11-23 16:36:07 +01:00
|
|
|
if (GNUNET_OK !=
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_exchange_secmod_rsa_verify (
|
|
|
|
&h_rsa,
|
2020-12-05 19:47:54 +01:00
|
|
|
section_name,
|
|
|
|
GNUNET_TIME_absolute_ntoh (kan->anchor_time),
|
|
|
|
GNUNET_TIME_relative_ntoh (kan->duration_withdraw),
|
|
|
|
&kan->secm_pub,
|
|
|
|
&kan->secm_sig))
|
2020-11-23 16:36:07 +01:00
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
2021-11-17 20:38:21 +01:00
|
|
|
TALER_denom_pub_free (&denom_pub);
|
2020-11-23 16:36:07 +01:00
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2020-11-22 22:25:49 +01:00
|
|
|
dh->dkc (dh->dkc_cls,
|
|
|
|
section_name,
|
|
|
|
GNUNET_TIME_absolute_ntoh (kan->anchor_time),
|
|
|
|
GNUNET_TIME_relative_ntoh (kan->duration_withdraw),
|
2021-11-17 23:02:05 +01:00
|
|
|
&h_rsa,
|
2020-11-23 16:36:07 +01:00
|
|
|
&denom_pub,
|
|
|
|
&kan->secm_pub,
|
|
|
|
&kan->secm_sig);
|
2021-11-17 20:38:21 +01:00
|
|
|
TALER_denom_pub_free (&denom_pub);
|
2020-11-22 22:25:49 +01:00
|
|
|
}
|
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle a #TALER_HELPER_RSA_MT_PURGE message from the helper.
|
|
|
|
*
|
|
|
|
* @param dh helper context
|
|
|
|
* @param hdr message that we received
|
|
|
|
* @return #GNUNET_OK on success
|
|
|
|
*/
|
2021-10-22 23:15:04 +02:00
|
|
|
static enum GNUNET_GenericReturnValue
|
2021-11-17 23:02:05 +01:00
|
|
|
handle_mt_purge (struct TALER_CRYPTO_RsaDenominationHelper *dh,
|
2020-11-22 22:25:49 +01:00
|
|
|
const struct GNUNET_MessageHeader *hdr)
|
|
|
|
{
|
|
|
|
const struct TALER_CRYPTO_RsaKeyPurgeNotification *pn
|
|
|
|
= (const struct TALER_CRYPTO_RsaKeyPurgeNotification *) hdr;
|
|
|
|
|
|
|
|
if (sizeof (*pn) != ntohs (hdr->size))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
return GNUNET_SYSERR;
|
|
|
|
}
|
2021-01-15 15:38:47 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Received revocation of denomination key %s\n",
|
2021-11-17 23:02:05 +01:00
|
|
|
GNUNET_h2s (&pn->h_rsa.hash));
|
2020-11-22 22:25:49 +01:00
|
|
|
dh->dkc (dh->dkc_cls,
|
|
|
|
NULL,
|
|
|
|
GNUNET_TIME_UNIT_ZERO_ABS,
|
|
|
|
GNUNET_TIME_UNIT_ZERO,
|
2021-11-17 23:02:05 +01:00
|
|
|
&pn->h_rsa,
|
2020-11-23 16:36:07 +01:00
|
|
|
NULL,
|
|
|
|
NULL,
|
2020-11-22 22:25:49 +01:00
|
|
|
NULL);
|
|
|
|
return GNUNET_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-21 23:58:00 +01:00
|
|
|
void
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_poll (struct TALER_CRYPTO_RsaDenominationHelper *dh)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
char buf[UINT16_MAX];
|
2021-11-17 20:38:21 +01:00
|
|
|
size_t off = 0;
|
2021-08-07 19:02:54 +02:00
|
|
|
unsigned int retry_limit = 3;
|
2020-11-21 23:58:00 +01:00
|
|
|
const struct GNUNET_MessageHeader *hdr
|
|
|
|
= (const struct GNUNET_MessageHeader *) buf;
|
|
|
|
|
2021-11-17 20:38:21 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
try_connect (dh))
|
2020-11-21 23:58:00 +01:00
|
|
|
return; /* give up */
|
|
|
|
while (1)
|
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
uint16_t msize;
|
|
|
|
ssize_t ret;
|
|
|
|
|
2020-11-21 23:58:00 +01:00
|
|
|
ret = recv (dh->sock,
|
|
|
|
buf,
|
|
|
|
sizeof (buf),
|
2021-11-17 20:38:21 +01:00
|
|
|
(dh->synced && (0 == off))
|
|
|
|
? MSG_DONTWAIT
|
|
|
|
: 0);
|
2020-11-21 23:58:00 +01:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
if (EINTR == errno)
|
|
|
|
continue;
|
2020-11-22 18:31:33 +01:00
|
|
|
if (EAGAIN == errno)
|
2020-12-12 22:40:40 +01:00
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
GNUNET_assert (dh->synced);
|
|
|
|
GNUNET_assert (0 == off);
|
|
|
|
break;
|
2020-12-12 22:40:40 +01:00
|
|
|
}
|
2020-11-21 23:58:00 +01:00
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"recv");
|
|
|
|
do_disconnect (dh);
|
2021-11-17 20:38:21 +01:00
|
|
|
if (0 == retry_limit)
|
|
|
|
return; /* give up */
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
try_connect (dh))
|
|
|
|
return; /* give up */
|
|
|
|
retry_limit--;
|
|
|
|
continue;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
if (0 == ret)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
GNUNET_break (0 == off);
|
2020-11-21 23:58:00 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
off += ret;
|
|
|
|
more:
|
|
|
|
if (off < sizeof (struct GNUNET_MessageHeader))
|
|
|
|
continue;
|
|
|
|
msize = ntohs (hdr->size);
|
|
|
|
if (off < msize)
|
|
|
|
continue;
|
2021-11-18 13:52:58 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Received message of type %u and length %u\n",
|
|
|
|
(unsigned int) ntohs (hdr->type),
|
|
|
|
(unsigned int) msize);
|
2020-11-21 23:58:00 +01:00
|
|
|
switch (ntohs (hdr->type))
|
|
|
|
{
|
|
|
|
case TALER_HELPER_RSA_MT_AVAIL:
|
2020-11-22 22:25:49 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
handle_mt_avail (dh,
|
|
|
|
hdr))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2020-11-22 22:25:49 +01:00
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
return;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case TALER_HELPER_RSA_MT_PURGE:
|
2020-11-22 22:25:49 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
handle_mt_purge (dh,
|
|
|
|
hdr))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2020-11-22 22:25:49 +01:00
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
return;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
break;
|
2020-12-12 22:40:40 +01:00
|
|
|
case TALER_HELPER_RSA_SYNCED:
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Now synchronized with RSA helper\n");
|
|
|
|
dh->synced = true;
|
|
|
|
break;
|
2020-11-21 23:58:00 +01:00
|
|
|
default:
|
2021-11-17 20:38:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Received unexpected message of type %d (len: %u)\n",
|
|
|
|
(unsigned int) ntohs (hdr->type),
|
|
|
|
(unsigned int) msize);
|
2020-11-21 23:58:00 +01:00
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
return;
|
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
memmove (buf,
|
|
|
|
&buf[msize],
|
|
|
|
off - msize);
|
|
|
|
off -= msize;
|
|
|
|
goto more;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-31 17:56:56 +01:00
|
|
|
struct TALER_BlindedDenominationSignature
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_sign (
|
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper *dh,
|
|
|
|
const struct TALER_RsaPubHashP *h_rsa,
|
2020-11-21 23:58:00 +01:00
|
|
|
const void *msg,
|
|
|
|
size_t msg_size,
|
|
|
|
enum TALER_ErrorCode *ec)
|
|
|
|
{
|
2021-10-31 17:56:56 +01:00
|
|
|
struct TALER_BlindedDenominationSignature ds = {
|
2021-11-17 20:38:21 +01:00
|
|
|
.cipher = TALER_DENOMINATION_INVALID
|
2021-10-22 23:15:04 +02:00
|
|
|
};
|
2021-11-17 20:38:21 +01:00
|
|
|
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
try_connect (dh))
|
|
|
|
{
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"Failed to connect to helper\n");
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE;
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
char buf[sizeof (struct TALER_CRYPTO_SignRequest) + msg_size];
|
|
|
|
struct TALER_CRYPTO_SignRequest *sr
|
|
|
|
= (struct TALER_CRYPTO_SignRequest *) buf;
|
|
|
|
|
|
|
|
sr->header.size = htons (sizeof (buf));
|
|
|
|
sr->header.type = htons (TALER_HELPER_RSA_MT_REQ_SIGN);
|
|
|
|
sr->reserved = htonl (0);
|
2021-11-17 23:02:05 +01:00
|
|
|
sr->h_rsa = *h_rsa;
|
2020-11-21 23:58:00 +01:00
|
|
|
memcpy (&sr[1],
|
|
|
|
msg,
|
|
|
|
msg_size);
|
2021-11-17 20:38:21 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
TALER_crypto_helper_send_all (dh->sock,
|
|
|
|
buf,
|
|
|
|
sizeof (buf)))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
|
2021-11-17 20:38:21 +01:00
|
|
|
"send");
|
2020-11-21 23:58:00 +01:00
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE;
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
char buf[UINT16_MAX];
|
2021-11-17 20:38:21 +01:00
|
|
|
size_t off = 0;
|
2020-11-21 23:58:00 +01:00
|
|
|
const struct GNUNET_MessageHeader *hdr
|
|
|
|
= (const struct GNUNET_MessageHeader *) buf;
|
2021-11-17 20:38:21 +01:00
|
|
|
bool finished = false;
|
2020-11-21 23:58:00 +01:00
|
|
|
|
2021-11-17 20:38:21 +01:00
|
|
|
*ec = TALER_EC_INVALID;
|
|
|
|
while (1)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
uint16_t msize;
|
|
|
|
ssize_t ret;
|
|
|
|
|
|
|
|
ret = recv (dh->sock,
|
|
|
|
buf,
|
|
|
|
sizeof (buf),
|
|
|
|
(finished && (0 == off))
|
|
|
|
? MSG_DONTWAIT
|
|
|
|
: 0);
|
|
|
|
if (ret < 0)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
if (EINTR == errno)
|
|
|
|
continue;
|
|
|
|
if (EAGAIN == errno)
|
|
|
|
{
|
|
|
|
GNUNET_assert (finished);
|
|
|
|
GNUNET_assert (0 == off);
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"recv");
|
2020-11-21 23:58:00 +01:00
|
|
|
do_disconnect (dh);
|
2021-11-17 20:38:21 +01:00
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (0 == ret)
|
|
|
|
{
|
|
|
|
GNUNET_break (0 == off);
|
|
|
|
if (! finished)
|
|
|
|
*ec = TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
|
2020-11-22 22:25:49 +01:00
|
|
|
return ds;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
off += ret;
|
|
|
|
more:
|
|
|
|
if (off < sizeof (struct GNUNET_MessageHeader))
|
|
|
|
continue;
|
|
|
|
msize = ntohs (hdr->size);
|
|
|
|
if (off < msize)
|
|
|
|
continue;
|
|
|
|
switch (ntohs (hdr->type))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2021-11-17 20:38:21 +01:00
|
|
|
case TALER_HELPER_RSA_MT_RES_SIGNATURE:
|
|
|
|
if ( (msize < sizeof (struct TALER_CRYPTO_SignResponse)) ||
|
|
|
|
(finished) )
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
2021-11-17 20:38:21 +01:00
|
|
|
goto end;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
{
|
|
|
|
const struct TALER_CRYPTO_SignResponse *sr =
|
|
|
|
(const struct TALER_CRYPTO_SignResponse *) buf;
|
|
|
|
struct GNUNET_CRYPTO_RsaSignature *rsa_signature;
|
|
|
|
|
|
|
|
rsa_signature = GNUNET_CRYPTO_rsa_signature_decode (
|
|
|
|
&sr[1],
|
|
|
|
msize - sizeof (*sr));
|
|
|
|
if (NULL == rsa_signature)
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
*ec = TALER_EC_NONE;
|
|
|
|
finished = true;
|
2021-11-17 20:52:39 +01:00
|
|
|
ds.cipher = TALER_DENOMINATION_RSA;
|
2021-11-17 20:38:21 +01:00
|
|
|
ds.details.blinded_rsa_signature = rsa_signature;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TALER_HELPER_RSA_MT_RES_SIGN_FAILURE:
|
|
|
|
if (msize != sizeof (struct TALER_CRYPTO_SignFailure))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const struct TALER_CRYPTO_SignFailure *sf =
|
|
|
|
(const struct TALER_CRYPTO_SignFailure *) buf;
|
2020-11-21 23:58:00 +01:00
|
|
|
|
2021-11-17 20:38:21 +01:00
|
|
|
*ec = (enum TALER_ErrorCode) ntohl (sf->ec);
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
case TALER_HELPER_RSA_MT_AVAIL:
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
handle_mt_avail (dh,
|
|
|
|
hdr))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break; /* while(1) loop ensures we recvfrom() again */
|
|
|
|
case TALER_HELPER_RSA_MT_PURGE:
|
|
|
|
if (GNUNET_OK !=
|
|
|
|
handle_mt_purge (dh,
|
|
|
|
hdr))
|
|
|
|
{
|
|
|
|
GNUNET_break_op (0);
|
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
break; /* while(1) loop ensures we recvfrom() again */
|
|
|
|
case TALER_HELPER_RSA_SYNCED:
|
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
|
|
|
|
"Synchronized add odd time with RSA helper!\n");
|
|
|
|
dh->synced = true;
|
|
|
|
break;
|
|
|
|
default:
|
2020-11-22 22:25:49 +01:00
|
|
|
GNUNET_break_op (0);
|
2021-11-17 20:38:21 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
|
|
|
|
"Received unexpected message of type %u\n",
|
|
|
|
ntohs (hdr->type));
|
2020-11-22 22:25:49 +01:00
|
|
|
do_disconnect (dh);
|
|
|
|
*ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG;
|
2021-11-17 20:38:21 +01:00
|
|
|
goto end;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
2021-11-17 20:38:21 +01:00
|
|
|
memmove (buf,
|
|
|
|
&buf[msize],
|
|
|
|
off - msize);
|
|
|
|
off -= msize;
|
|
|
|
goto more;
|
|
|
|
} /* while(1) */
|
|
|
|
end:
|
|
|
|
if (finished)
|
|
|
|
TALER_blinded_denom_sig_free (&ds);
|
|
|
|
return ds;
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_revoke (
|
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper *dh,
|
|
|
|
const struct TALER_RsaPubHashP *h_rsa)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
struct TALER_CRYPTO_RevokeRequest rr = {
|
|
|
|
.header.size = htons (sizeof (rr)),
|
|
|
|
.header.type = htons (TALER_HELPER_RSA_MT_REQ_REVOKE),
|
2021-11-17 23:02:05 +01:00
|
|
|
.h_rsa = *h_rsa
|
2020-11-21 23:58:00 +01:00
|
|
|
};
|
|
|
|
|
2021-11-17 20:38:21 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
try_connect (dh))
|
2020-11-21 23:58:00 +01:00
|
|
|
return; /* give up */
|
2021-11-17 20:38:21 +01:00
|
|
|
if (GNUNET_OK !=
|
|
|
|
TALER_crypto_helper_send_all (dh->sock,
|
|
|
|
&rr,
|
|
|
|
sizeof (rr)))
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
|
|
|
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
|
2021-11-17 20:38:21 +01:00
|
|
|
"send");
|
2020-11-21 23:58:00 +01:00
|
|
|
do_disconnect (dh);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-15 15:38:47 +01:00
|
|
|
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
|
|
|
|
"Requested revocation of denomination key %s\n",
|
2021-11-17 23:02:05 +01:00
|
|
|
GNUNET_h2s (&h_rsa->hash));
|
2020-11-21 23:58:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2021-11-17 23:02:05 +01:00
|
|
|
TALER_CRYPTO_helper_rsa_disconnect (
|
|
|
|
struct TALER_CRYPTO_RsaDenominationHelper *dh)
|
2020-11-21 23:58:00 +01:00
|
|
|
{
|
2020-12-20 17:10:09 +01:00
|
|
|
if (-1 != dh->sock)
|
|
|
|
do_disconnect (dh);
|
2020-11-21 23:58:00 +01:00
|
|
|
GNUNET_free (dh);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-11-22 22:25:49 +01:00
|
|
|
/* end of crypto_helper_denom.c */
|