From 6714e6a9cc90ddef8b8498f8a62a6c11d998c65c Mon Sep 17 00:00:00 2001 From: Sree Harsha Totakura Date: Sat, 7 Mar 2015 14:03:01 +0100 Subject: [PATCH] db: Implement get_collectable_blindcoin --- src/mint/mint_db.c | 49 +++++++++++++++++++++------------- src/mint/taler-mint-httpd_db.c | 1 + src/mint/test_mint_db.c | 20 +++++++++++--- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/mint/mint_db.c b/src/mint/mint_db.c index 0a531b202..5c9f48285 100644 --- a/src/mint/mint_db.c +++ b/src/mint/mint_db.c @@ -336,7 +336,7 @@ TALER_MINT_DB_prepare (PGconn *db_conn) 4, NULL); PREPARE ("get_collectable_blindcoins", "SELECT " - "blind_ev_sig, denom_pub, reserve_sig, reserve_pub " + "denom_pub, reserve_sig, reserve_pub " "FROM collectable_blindcoins " "WHERE blind_ev = $1", 1, NULL); @@ -926,37 +926,35 @@ TALER_MINT_DB_get_collectable_blindcoin (PGconn *db_conn, const struct GNUNET_HashCode *h_blind, struct CollectableBlindcoin *collectable) { - // FIXME: check logic! PGresult *result; struct TALER_DB_QueryParam params[] = { TALER_DB_QUERY_PARAM_PTR (h_blind), TALER_DB_QUERY_PARAM_END }; - char *sig_buf; - size_t sig_buf_size; + struct GNUNET_CRYPTO_rsa_PublicKey *denom_pub; + char *denom_pub_enc; + size_t denom_pub_enc_size; + int ret; + ret = GNUNET_SYSERR; + denom_pub = NULL; + denom_pub_enc = NULL; result = TALER_DB_exec_prepared (db_conn, "get_collectable_blindcoins", params); - if (PGRES_TUPLES_OK != - PQresultStatus (result)) + if (PGRES_TUPLES_OK != PQresultStatus (result)) { - GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Query failed: %s\n", - PQresultErrorMessage (result)); - PQclear (result); - return GNUNET_SYSERR; + QUERY_ERR (result); + goto cleanup; } if (0 == PQntuples (result)) { - PQclear (result); - return GNUNET_NO; + ret = GNUNET_NO; + goto cleanup; } - struct TALER_DB_ResultSpec rs[] = { - TALER_DB_RESULT_SPEC_VAR("blind_sig", &sig_buf, &sig_buf_size), - TALER_DB_RESULT_SPEC("denom_pub", &collectable->denom_pub), + TALER_DB_RESULT_SPEC_VAR("denom_pub", &denom_pub_enc, &denom_pub_enc_size), TALER_DB_RESULT_SPEC("reserve_sig", &collectable->reserve_sig), TALER_DB_RESULT_SPEC("reserve_pub", &collectable->reserve_pub), TALER_DB_RESULT_SPEC_END @@ -965,11 +963,24 @@ TALER_MINT_DB_get_collectable_blindcoin (PGconn *db_conn, if (GNUNET_OK != TALER_DB_extract_result (result, rs, 0)) { GNUNET_break (0); - PQclear (result); - return GNUNET_SYSERR; + goto cleanup; } + denom_pub = GNUNET_CRYPTO_rsa_public_key_decode (denom_pub_enc, + denom_pub_enc_size); + if (NULL == denom_pub) + { + GNUNET_break (0); + goto cleanup; + } + collectable->denom_pub = denom_pub; + ret = GNUNET_YES; + + cleanup: PQclear (result); - return GNUNET_OK; + GNUNET_free_non_null (denom_pub_enc); + if ((GNUNET_YES != ret) && (NULL != denom_pub)) + GNUNET_CRYPTO_rsa_public_key_free (denom_pub); + return ret; } diff --git a/src/mint/taler-mint-httpd_db.c b/src/mint/taler-mint-httpd_db.c index d5613c0dc..e3f7b754c 100644 --- a/src/mint/taler-mint-httpd_db.c +++ b/src/mint/taler-mint-httpd_db.c @@ -284,6 +284,7 @@ TALER_MINT_db_execute_withdraw_sign (struct MHD_Connection *connection, res = TALER_MINT_reply_withdraw_sign_success (connection, &collectable); GNUNET_CRYPTO_rsa_signature_free (collectable.sig); + GNUNET_CRYPTO_rsa_public_key_free (collectable.denom_pub); return res; } GNUNET_assert (GNUNET_NO == res); diff --git a/src/mint/test_mint_db.c b/src/mint/test_mint_db.c index 320005833..88fb53d53 100644 --- a/src/mint/test_mint_db.c +++ b/src/mint/test_mint_db.c @@ -36,6 +36,9 @@ static int result; #define RND_BLK(ptr) \ GNUNET_CRYPTO_random_block (GNUNET_CRYPTO_QUALITY_WEAK, ptr, sizeof (*ptr)) +#define ZR_BLK(ptr) \ + memset (ptr, 0, sizeof (*ptr)) + /** * Checks if the given reserve has the given amount of balance and expiry @@ -115,9 +118,12 @@ run (void *cls, char *const *args, const char *cfgfile, struct DenomKeyPair *dkp; struct GNUNET_HashCode *h_blind; struct CollectableBlindcoin cbc; + struct CollectableBlindcoin cbc2; + db = NULL; dkp = NULL; + ZR_BLK (&cbc2); if (GNUNET_OK != TALER_MINT_DB_init ("postgres:///taler")) { result = 1; @@ -167,9 +173,15 @@ run (void *cls, char *const *args, const char *cfgfile, cbc.denom_pub = dkp->pub; cbc.sig = NULL; memcpy (&cbc.reserve_pub, &reserve_pub, sizeof (reserve_pub)); - TALER_MINT_DB_insert_collectable_blindcoin (db, - &h_blind, - &cbc); + FAILIF (GNUNET_OK!= TALER_MINT_DB_insert_collectable_blindcoin (db, + &h_blind, + &cbc)); + FAILIF (GNUNET_YES != TALER_MINT_DB_get_collectable_blindcoin (db, + &h_blind, + &cbc2)); + FAILIF (NULL == cbc2.denom_pub); + FAILIF (0 != memcmp (&cbc2.reserve_sig, &cbc.reserve_sig, sizeof (cbc2.reserve_sig))); + FAILIF (0 != memcmp (&cbc2.reserve_pub, &cbc.reserve_pub, sizeof (cbc2.reserve_pub))); result = 0; drop: @@ -177,6 +189,8 @@ run (void *cls, char *const *args, const char *cfgfile, GNUNET_break (GNUNET_OK == TALER_MINT_DB_drop_temporary (db)); if (NULL != dkp) destroy_denon_key_pair (dkp); + if (NULL != cbc2.denom_pub) + GNUNET_CRYPTO_rsa_public_key_free (cbc2.denom_pub); dkp = NULL; }