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 backenddb/pg_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_error_codes.h>
25 : #include <taler/taler_dbevents.h>
26 : #include <taler/taler_pq_lib.h>
27 : #include "pg_insert_token_family_key.h"
28 : #include "pg_helper.h"
29 :
30 :
31 : enum GNUNET_DB_QueryStatus
32 4 : TMH_PG_insert_token_family_key (
33 : void *cls,
34 : const char *merchant_id,
35 : const char *token_family_slug,
36 : const struct TALER_TokenIssuePublicKey *pub,
37 : const struct TALER_TokenIssuePrivateKey *priv,
38 : struct GNUNET_TIME_Timestamp key_expires,
39 : struct GNUNET_TIME_Timestamp valid_after,
40 : struct GNUNET_TIME_Timestamp valid_before)
41 : {
42 4 : struct PostgresClosure *pg = cls;
43 : struct GNUNET_TIME_Timestamp now
44 4 : = GNUNET_TIME_timestamp_get ();
45 4 : const char *cipher = NULL;
46 :
47 : #if DEBUG
48 : struct GNUNET_HashCode pub_hash;
49 :
50 : switch (pub->public_key->cipher)
51 : {
52 : case GNUNET_CRYPTO_BSA_RSA:
53 : cipher = "rsa";
54 : GNUNET_CRYPTO_rsa_public_key_hash (
55 : pub->public_key->details.rsa_public_key,
56 : &pub_hash);
57 : break;
58 : case GNUNET_CRYPTO_BSA_CS:
59 : cipher = "cs";
60 : GNUNET_CRYPTO_hash (
61 : &pub->public_key->details.cs_public_key,
62 : sizeof (pub->public_key->details.cs_public_key),
63 : &pub_hash);
64 : break;
65 : case GNUNET_CRYPTO_BSA_INVALID:
66 : GNUNET_break (0);
67 : return GNUNET_DB_STATUS_HARD_ERROR;
68 : }
69 : GNUNET_assert (0 ==
70 : GNUNET_memcmp (&pub_hash,
71 : &pub->public_key->pub_key_hash));
72 : #endif
73 4 : switch (pub->public_key->cipher)
74 : {
75 4 : case GNUNET_CRYPTO_BSA_RSA:
76 4 : cipher = "rsa";
77 4 : break;
78 0 : case GNUNET_CRYPTO_BSA_CS:
79 0 : cipher = "cs";
80 0 : break;
81 0 : case GNUNET_CRYPTO_BSA_INVALID:
82 0 : GNUNET_break (0);
83 0 : return GNUNET_DB_STATUS_HARD_ERROR;
84 : }
85 4 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
86 : "Storing token public key with hash %s\n",
87 : GNUNET_h2s (&pub->public_key->pub_key_hash));
88 4 : GNUNET_assert (pub->public_key->cipher ==
89 : priv->private_key->cipher);
90 4 : GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
91 : valid_after.abs_time));
92 4 : GNUNET_assert (! GNUNET_TIME_absolute_is_zero (
93 : valid_before.abs_time));
94 4 : PREPARE (pg,
95 : "token_family_key_insert",
96 : "INSERT INTO merchant_token_family_keys "
97 : "(token_family_serial"
98 : ",pub"
99 : ",h_pub"
100 : ",priv"
101 : ",private_key_created_at"
102 : ",private_key_deleted_at"
103 : ",signature_validity_start"
104 : ",signature_validity_end"
105 : ",cipher)"
106 : " SELECT token_family_serial, $2, $3, $4, $5, $6, $7, $8, $9"
107 : " FROM merchant_token_families"
108 : " WHERE (slug = $1)"
109 : " AND merchant_serial="
110 : " (SELECT merchant_serial"
111 : " FROM merchant_instances"
112 : " WHERE merchant_id=$10)");
113 : {
114 4 : struct GNUNET_PQ_QueryParam params[] = {
115 4 : GNUNET_PQ_query_param_string (token_family_slug),
116 4 : GNUNET_PQ_query_param_blind_sign_pub (pub->public_key),
117 4 : GNUNET_PQ_query_param_auto_from_type (&pub->public_key->pub_key_hash),
118 4 : GNUNET_PQ_query_param_blind_sign_priv (priv->private_key),
119 4 : GNUNET_PQ_query_param_timestamp (&now),
120 4 : GNUNET_PQ_query_param_timestamp (&key_expires),
121 4 : GNUNET_PQ_query_param_timestamp (&valid_after),
122 4 : GNUNET_PQ_query_param_timestamp (&valid_before),
123 4 : GNUNET_PQ_query_param_string (cipher),
124 4 : GNUNET_PQ_query_param_string (merchant_id),
125 : GNUNET_PQ_query_param_end
126 : };
127 : enum GNUNET_DB_QueryStatus qs;
128 :
129 4 : qs = GNUNET_PQ_eval_prepared_non_select (pg->conn,
130 : "token_family_key_insert",
131 : params);
132 4 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
133 : "Insert into MTFK %s with valid [%llu,%llu] got %d\n",
134 : token_family_slug,
135 : (unsigned long long) valid_after.abs_time.abs_value_us,
136 : (unsigned long long) valid_before.abs_time.abs_value_us,
137 : (int) qs);
138 4 : return qs;
139 : }
140 : }
|