From 3eae999efc0cb923aebd2bf7214c5f4093217d4f Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 31 Oct 2021 17:56:56 +0100 Subject: distinguish between blind and non-blind denomination signatures --- src/pq/pq_query_helper.c | 94 +++++++++++++++++++++++++++++++++- src/pq/pq_result_helper.c | 127 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 217 insertions(+), 4 deletions(-) (limited to 'src/pq') diff --git a/src/pq/pq_query_helper.c b/src/pq/pq_query_helper.c index 3f51ddbe8..618877608 100644 --- a/src/pq/pq_query_helper.c +++ b/src/pq/pq_query_helper.c @@ -265,7 +265,7 @@ qconv_denom_sig (void *cls, const struct TALER_DenominationSignature *denom_sig = data; size_t tlen; size_t len; - uint32_t be; + uint32_t be[2]; char *buf; void *tbuf; @@ -273,7 +273,8 @@ qconv_denom_sig (void *cls, GNUNET_assert (1 == param_length); GNUNET_assert (scratch_length > 0); GNUNET_break (NULL == cls); - be = htonl ((uint32_t) denom_sig->cipher); + be[0] = htonl ((uint32_t) denom_sig->cipher); + be[1] = htonl (0x00); /* magic marker: unblinded */ switch (denom_sig->cipher) { case TALER_DENOMINATION_RSA: @@ -325,6 +326,95 @@ TALER_PQ_query_param_denom_sig ( } +/** + * Function called to convert input argument into SQL parameters. + * + * @param cls closure + * @param data pointer to input argument + * @param data_len number of bytes in @a data (if applicable) + * @param[out] param_values SQL data to set + * @param[out] param_lengths SQL length data to set + * @param[out] param_formats SQL format data to set + * @param param_length number of entries available in the @a param_values, @a param_lengths and @a param_formats arrays + * @param[out] scratch buffer for dynamic allocations (to be done via #GNUNET_malloc() + * @param scratch_length number of entries left in @a scratch + * @return -1 on error, number of offsets used in @a scratch otherwise + */ +static int +qconv_blinded_denom_sig (void *cls, + const void *data, + size_t data_len, + void *param_values[], + int param_lengths[], + int param_formats[], + unsigned int param_length, + void *scratch[], + unsigned int scratch_length) +{ + const struct TALER_BlindedDenominationSignature *denom_sig = data; + size_t tlen; + size_t len; + uint32_t be[2]; + char *buf; + void *tbuf; + + (void) cls; + GNUNET_assert (1 == param_length); + GNUNET_assert (scratch_length > 0); + GNUNET_break (NULL == cls); + be[0] = htonl ((uint32_t) denom_sig->cipher); + be[1] = htonl (0x01); /* magic marker: blinded */ + switch (denom_sig->cipher) + { + case TALER_DENOMINATION_RSA: + tlen = GNUNET_CRYPTO_rsa_signature_encode ( + denom_sig->details.blinded_rsa_signature, + &tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + len = tlen + sizeof (be); + buf = GNUNET_malloc (len); + memcpy (buf, + &be, + sizeof (be)); + switch (denom_sig->cipher) + { + case TALER_DENOMINATION_RSA: + memcpy (&buf[sizeof (be)], + tbuf, + tlen); + GNUNET_free (tbuf); + break; + // TODO: add case for Clause-Schnorr + default: + GNUNET_assert (0); + } + + scratch[0] = buf; + param_values[0] = (void *) buf; + param_lengths[0] = len; + param_formats[0] = 1; + return 1; +} + + +struct GNUNET_PQ_QueryParam +TALER_PQ_query_param_blinded_denom_sig ( + const struct TALER_BlindedDenominationSignature *denom_sig) +{ + struct GNUNET_PQ_QueryParam res = { + .conv = &qconv_blinded_denom_sig, + .data = denom_sig, + .num_params = 1 + }; + + return res; +} + + /** * Function called to convert input argument into SQL parameters. * diff --git a/src/pq/pq_result_helper.c b/src/pq/pq_result_helper.c index 57bcf6dd3..2f570b6bb 100644 --- a/src/pq/pq_result_helper.c +++ b/src/pq/pq_result_helper.c @@ -630,7 +630,7 @@ extract_denom_sig (void *cls, size_t len; const char *res; int fnum; - uint32_t be; + uint32_t be[2]; (void) cls; fnum = PQfnumber (result, @@ -661,9 +661,14 @@ extract_denom_sig (void *cls, memcpy (&be, res, sizeof (be)); + if (0x00 != ntohl (be[1])) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } res += sizeof (be); len -= sizeof (be); - sig->cipher = ntohl (be); + sig->cipher = ntohl (be[0]); switch (sig->cipher) { case TALER_DENOMINATION_RSA: @@ -717,4 +722,122 @@ TALER_PQ_result_spec_denom_sig (const char *name, } +/** + * Extract data from a Postgres database @a result at row @a row. + * + * @param cls closure + * @param result where to extract data from + * @param int row to extract data from + * @param fname name (or prefix) of the fields to extract from + * @param[in,out] dst_size where to store size of result, may be NULL + * @param[out] dst where to store the result + * @return + * #GNUNET_YES if all results could be extracted + * #GNUNET_SYSERR if a result was invalid (non-existing field or NULL) + */ +static enum GNUNET_GenericReturnValue +extract_blinded_denom_sig (void *cls, + PGresult *result, + int row, + const char *fname, + size_t *dst_size, + void *dst) +{ + struct TALER_BlindedDenominationSignature *sig = dst; + size_t len; + const char *res; + int fnum; + uint32_t be[2]; + + (void) cls; + fnum = PQfnumber (result, + fname); + if (fnum < 0) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + if (PQgetisnull (result, + row, + fnum)) + return GNUNET_NO; + + /* if a field is null, continue but + * remember that we now return a different result */ + len = PQgetlength (result, + row, + fnum); + res = PQgetvalue (result, + row, + fnum); + if (len < sizeof (be)) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + memcpy (&be, + res, + sizeof (be)); + if (0x01 != ntohl (be[1])) /* magic marker: blinded */ + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + res += sizeof (be); + len -= sizeof (be); + sig->cipher = ntohl (be[0]); + switch (sig->cipher) + { + case TALER_DENOMINATION_RSA: + sig->details.blinded_rsa_signature + = GNUNET_CRYPTO_rsa_signature_decode (res, + len); + if (NULL == sig->details.blinded_rsa_signature) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; + // FIXME: add CS case! + default: + GNUNET_break (0); + } + return GNUNET_SYSERR; +} + + +/** + * Function called to clean up memory allocated + * by a #GNUNET_PQ_ResultConverter. + * + * @param cls closure + * @param rd result data to clean up + */ +static void +clean_blinded_denom_sig (void *cls, + void *rd) +{ + struct TALER_BlindedDenominationSignature *denom_sig = rd; + + (void) cls; + TALER_blinded_denom_sig_free (denom_sig); +} + + +struct GNUNET_PQ_ResultSpec +TALER_PQ_result_spec_blinded_denom_sig ( + const char *name, + struct TALER_BlindedDenominationSignature *denom_sig) +{ + struct GNUNET_PQ_ResultSpec res = { + .conv = &extract_blinded_denom_sig, + .cleaner = &clean_blinded_denom_sig, + .dst = (void *) denom_sig, + .fname = name + }; + + return res; +} + + /* end of pq_result_helper.c */ -- cgit v1.2.3