Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2024 Taler Systems SA
4 :
5 : TALER is free software; you can redistribute it and/or modify it under the
6 : terms of the GNU General Public License as published by the Free Software
7 : Foundation; either version 3, or (at your option) any later version.
8 :
9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY
10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12 :
13 : You should have received a copy of the GNU General Public License along with
14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
15 : */
16 : /**
17 : * @file src/backenddb/insert_token_family_key.c
18 : * @brief Implementation of the insert_token_family_key function for Postgres
19 : * @author Christian Blättler
20 : */
21 : #include "platform.h"
22 : #include <gnunet/gnunet_common.h>
23 : #include <gnunet/gnunet_pq_lib.h>
24 : #include <taler/taler_pq_lib.h>
25 : #include "merchant-database/insert_token_family_key.h"
26 : #include "helper.h"
27 :
28 :
29 : enum GNUNET_DB_QueryStatus
30 5 : TALER_MERCHANTDB_insert_token_family_key (
31 : struct TALER_MERCHANTDB_PostgresContext *pg,
32 : const char *merchant_id,
33 : const char *token_family_slug,
34 : const struct TALER_TokenIssuePublicKey *pub,
35 : const struct TALER_TokenIssuePrivateKey *priv,
36 : struct GNUNET_TIME_Timestamp key_expires,
37 : struct GNUNET_TIME_Timestamp valid_after,
38 : struct GNUNET_TIME_Timestamp valid_before)
39 : {
40 : struct GNUNET_TIME_Timestamp now
41 5 : = GNUNET_TIME_timestamp_get ();
42 5 : const char *cipher = NULL;
43 :
44 : #if DEBUG
45 : struct GNUNET_HashCode pub_hash;
46 :
47 : switch (pub->public_key->cipher)
48 : {
49 : case GNUNET_CRYPTO_BSA_RSA:
50 : cipher = "rsa";
51 : GNUNET_CRYPTO_rsa_public_key_hash (
52 : pub->public_key->details.rsa_public_key,
53 : &pub_hash);
54 : break;
55 : case GNUNET_CRYPTO_BSA_CS:
56 : cipher = "cs";
57 : GNUNET_CRYPTO_hash (
58 : &pub->public_key->details.cs_public_key,
59 : sizeof (pub->public_key->details.cs_public_key),
60 : &pub_hash);
61 : break;
62 : case GNUNET_CRYPTO_BSA_INVALID:
63 : GNUNET_break (0);
64 : return GNUNET_DB_STATUS_HARD_ERROR;
65 : }
66 : GNUNET_assert (0 ==
67 : GNUNET_memcmp (&pub_hash,
68 : &pub->public_key->pub_key_hash));
69 : #endif
70 5 : switch (pub->public_key->cipher)
71 : {
72 4 : case GNUNET_CRYPTO_BSA_RSA:
73 4 : cipher = "rsa";
74 4 : break;
75 1 : case GNUNET_CRYPTO_BSA_CS:
76 1 : cipher = "cs";
77 1 : break;
78 0 : case GNUNET_CRYPTO_BSA_INVALID:
79 0 : GNUNET_break (0);
80 0 : return GNUNET_DB_STATUS_HARD_ERROR;
81 : }
82 5 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
83 : "Storing token public key with hash %s\n",
84 : GNUNET_h2s (&pub->public_key->pub_key_hash));
85 5 : GNUNET_assert (pub->public_key->cipher ==
86 : priv->private_key->cipher);
87 5 : GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
88 : valid_after.abs_time));
89 5 : GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
90 : valid_before.abs_time));
91 5 : GNUNET_assert (NULL != pg->current_merchant_id);
92 5 : GNUNET_assert (0 == strcmp (merchant_id,
93 : pg->current_merchant_id));
94 5 : TMH_PQ_prepare_anon (pg,
95 : "INSERT INTO merchant_token_family_keys "
96 : "(token_family_serial"
97 : ",pub"
98 : ",h_pub"
99 : ",priv"
100 : ",private_key_created_at"
101 : ",private_key_deleted_at"
102 : ",signature_validity_start"
103 : ",signature_validity_end"
104 : ",cipher)"
105 : " SELECT token_family_serial, $2, $3, $4, $5, $6, $7, $8, $9"
106 : " FROM merchant_token_families"
107 : " WHERE (slug = $1)");
108 : {
109 5 : struct GNUNET_PQ_QueryParam params[] = {
110 5 : GNUNET_PQ_query_param_string (token_family_slug),
111 5 : GNUNET_PQ_query_param_blind_sign_pub (pub->public_key),
112 5 : GNUNET_PQ_query_param_auto_from_type (&pub->public_key->pub_key_hash),
113 5 : GNUNET_PQ_query_param_blind_sign_priv (priv->private_key),
114 5 : GNUNET_PQ_query_param_timestamp (&now),
115 5 : GNUNET_PQ_query_param_timestamp (&key_expires),
116 5 : GNUNET_PQ_query_param_timestamp (&valid_after),
117 5 : GNUNET_PQ_query_param_timestamp (&valid_before),
118 5 : GNUNET_PQ_query_param_string (cipher),
119 : GNUNET_PQ_query_param_end
120 : };
121 : enum GNUNET_DB_QueryStatus qs;
122 :
123 5 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
124 : "",
125 : params);
126 5 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
127 : "Insert into MTFK %s with valid [%llu,%llu] got %d\n",
128 : token_family_slug,
129 : (unsigned long long) valid_after.abs_time.abs_value_us,
130 : (unsigned long long) valid_before.abs_time.abs_value_us,
131 : (int) qs);
132 5 : return qs;
133 : }
134 : }
|