summaryrefslogtreecommitdiff
path: root/src/include/anastasis_database_plugin.h
blob: 45efb09deec4a0933bcf87d6b0748de84bdd84dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
/*
  This file is part of Anastasis
  Copyright (C) 2019 Anastasis SARL

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Lesser General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  Anastasis 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 Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  Anastasis; see the file COPYING.GPL.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file include/anastasis_database_plugin.h
 * @brief database access for Anastasis
 * @author Christian Grothoff
 */
#ifndef ANASTASIS_DATABASE_PLUGIN_H
#define ANASTASIS_DATABASE_PLUGIN_H

#include "anastasis_service.h"
#include <gnunet/gnunet_db_lib.h>

/**
 * How long is an offer for a challenge payment valid for payment?
 */
#define ANASTASIS_CHALLENGE_OFFER_LIFETIME GNUNET_TIME_UNIT_HOURS

/**
 * Return values for checking code validity.
 */
enum ANASTASIS_DB_CodeStatus
{
  /**
   * Provided authentication code does not match database content.
   */
  ANASTASIS_DB_CODE_STATUS_CHALLENGE_CODE_MISMATCH = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_CODE_STATUS_HARD_ERROR = -2,

  /**
   * Encountered serialization error talking to DB.
   */
  ANASTASIS_DB_CODE_STATUS_SOFT_ERROR = -1,

  /**
   * We have no challenge in the database.
   */
  ANASTASIS_DB_CODE_STATUS_NO_RESULTS = 0,

  /**
   * The provided challenge matches what we have in the database.
   */
  ANASTASIS_DB_CODE_STATUS_VALID_CODE_STORED = 1,
};


/**
 * Return values for checking account validity.
 */
enum ANASTASIS_DB_AccountStatus
{
  /**
   * Account is unknown, user should pay to establish it.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_PAYMENT_REQUIRED = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_HARD_ERROR = -2,

  /**
   * Account is valid, but we have no policy stored yet.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_NO_RESULTS = 0,

  /**
   * Account is valid, and we have a policy stored.
   */
  ANASTASIS_DB_ACCOUNT_STATUS_VALID_HASH_RETURNED = 1,
};


/**
 * Return values for storing data in database with payment.
 */
enum ANASTASIS_DB_StoreStatus
{
  /**
   * The client has stored too many policies, should pay to store more.
   */
  ANASTASIS_DB_STORE_STATUS_STORE_LIMIT_EXCEEDED = -4,

  /**
   * The client needs to pay to store policies.
   */
  ANASTASIS_DB_STORE_STATUS_PAYMENT_REQUIRED = -3,

  /**
   * Encountered hard error talking to DB.
   */
  ANASTASIS_DB_STORE_STATUS_HARD_ERROR = -2,

  /**
   * Despite retrying, we encountered serialization errors.
   */
  ANASTASIS_DB_STORE_STATUS_SOFT_ERROR = -1,

  /**
   * Database did not need an update (document exists).
   */
  ANASTASIS_DB_STORE_STATUS_NO_RESULTS = 0,

  /**
   * We successfully stored the document.
   */
  ANASTASIS_DB_STORE_STATUS_SUCCESS = 1,
};


/**
 * Function called on all pending payments for an account or challenge.
 *
 * @param cls closure
 * @param timestamp for how long have we been waiting
 * @param payment_secret payment secret / order id in the backend
 * @param amount how much is the order for
 */
typedef void
(*ANASTASIS_DB_PaymentPendingIterator)(
  void *cls,
  struct GNUNET_TIME_Absolute timestamp,
  const struct ANASTASIS_PaymentSecretP *payment_secret,
  const struct TALER_Amount *amount);


/**
 * Handle to interact with the database.
 *
 * Functions ending with "_TR" run their OWN transaction scope
 * and MUST NOT be called from within a transaction setup by the
 * caller.  Functions ending with "_NT" require the caller to
 * setup a transaction scope.  Functions without a suffix are
 * simple, single SQL queries that MAY be used either way.
 */
struct ANASTASIS_DatabasePlugin
{

  /**
   * Closure for all callbacks.
   */
  void *cls;

  /**
   * Name of the library which generated this plugin.  Set by the
   * plugin loader.
   */
  char *library_name;

  /**
   * Drop anastasis tables. Used for testcases.
   *
   * @param cls closure
   * @return #GNUNET_OK upon success; #GNUNET_SYSERR upon failure
   */
  int
  (*drop_tables) (void *cls);

  /**
   * Function called to perform "garbage collection" on the
   * database, expiring records we no longer require.  Deletes
   * all user records that are not paid up (and by cascade deletes
   * the associated recovery documents). Also deletes expired
   * truth and financial records older than @a fin_expire.
   *
   * @param cls closure
   * @param expire_backups backups older than the given time stamp should be garbage collected
   * @param expire_pending_payments payments still pending from since before
   *            this value should be garbage collected
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*gc)(void *cls,
        struct GNUNET_TIME_Absolute expire,
        struct GNUNET_TIME_Absolute expire_pending_payments);

  /**
  * Do a pre-flight check that we are not in an uncommitted transaction.
  * If we are, try to commit the previous transaction and output a warning.
  * Does not return anything, as we will continue regardless of the outcome.
  *
  * @param cls the `struct PostgresClosure` with the plugin-specific state
  */
  void
  (*preflight) (void *cls);

  /**
  * Check that the database connection is still up.
  *
  * @param pg connection to check
  */
  void
  (*check_connection) (void *cls);

  /**
  * Roll back the current transaction of a database connection.
  *
  * @param cls the `struct PostgresClosure` with the plugin-specific state
  * @return #GNUNET_OK on success
  */
  void
  (*rollback) (void *cls);

  /**
   * Start a transaction.
   *
   * @param cls the `struct PostgresClosure` with the plugin-specific state
   * @param name unique name identifying the transaction (for debugging),
   *             must point to a constant
   * @return #GNUNET_OK on success
   */
  int
  (*start) (void *cls,
            const char *name);

  /**
   * Commit the current transaction of a database connection.
   *
   * @param cls the `struct PostgresClosure` with the plugin-specific state
   * @return transaction status code
   */
  enum GNUNET_DB_QueryStatus
  (*commit)(void *cls);


  /**
   * Store encrypted recovery document.
   *
   * @param cls closure
   * @param account_pub public key of the user's account
   * @param account_sig signature affirming storage request
   * @param recovery_data_hash hash of @a data
   * @param recovery_data contains encrypted recovery document
   * @param recovery_data_size size of @a recovery_data blob
   * @param payment_secret identifier for the payment, used to later charge on uploads
   * @param[out] version set to the version assigned to the document by the database
   * @return transaction status, 0 if upload could not be finished because @a payment_secret
   *         did not have enough upload left; HARD error if @a payment_secret is unknown, ...
   */
  enum ANASTASIS_DB_StoreStatus
  (*store_recovery_document)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    const struct ANASTASIS_AccountSignatureP *account_sig,
    const struct GNUNET_HashCode *recovery_data_hash,
    const void *recovery_data,
    size_t recovery_data_size,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    uint32_t *version);


  /**
   * Fetch recovery document for user according given version.
   *
   * @param cls closure
   * @param account_pub public key of the user's account
   * @param version the version number of the policy the user requests
   * @param[out] account_sig signature
   * @param[out] recovery_data_hash hash of the current recovery data
   * @param[out] data_size size of data blob
   * @param[out] data blob which contains the recovery document
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*get_recovery_document)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    uint32_t version,
    struct ANASTASIS_AccountSignatureP *account_sig,
    struct GNUNET_HashCode *recovery_data_hash,
    size_t *data_size,
    void **data);


  /**
   * Fetch latest recovery document for user.
   *
   * @param cls closure
   * @param account_pub public key of the user's account
   * @param account_sig signature
   * @param recovery_data_hash hash of the current recovery data
   * @param[out] data_size set to size of @a data blob
   * @param[out] data set to blob which contains the recovery document
   * @param[out] version set to the version number of the policy being returned
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*get_latest_recovery_document)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    struct ANASTASIS_AccountSignatureP *account_sig,
    struct GNUNET_HashCode *recovery_data_hash,
    size_t *data_size,
    void **data,
    uint32_t *version);


  /**
   * Upload Truth, which contains the Truth and the KeyShare.
   *
   * @param cls closure
   * @param truth_uuid the identifier for the Truth
   * @param key_share_data contains information of an EncryptedKeyShare
   * @param mime_type presumed mime type of data in @a encrypted_truth
   * @param encrypted_truth contains the encrypted Truth which includes the ground truth i.e. H(challenge answer), phonenumber, SMS
   * @param encrypted_truth_size the size of the Truth
   * @param method name of method
   * @param truth_expiration time till the according data will be stored
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*store_truth)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    const struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share_data,
    const char *mime_type,
    const void *encrypted_truth,
    size_t encrypted_truth_size,
    const char *method,
    struct GNUNET_TIME_Relative truth_expiration);


  /**
   * Get the encrypted truth to validate the challenge response
   *
   * @param cls closure
   * @param truth_uuid the identifier for the Truth
   * @param[out] truth contains the encrypted truth
   * @param[out] truth_size size of the encrypted truth
   * @param[out] truth_mime mime type of truth
   * @param[out] method type of the challenge
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*get_escrow_challenge)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    void **truth,
    size_t *truth_size,
    char **truth_mime,
    char **method);


  /**
   * Lookup (encrypted) key share by @a truth_uuid.
   *
   * @param cls closure
   * @param truth_uuid the identifier for the Truth
   * @param[out] key_share set to the encrypted Keyshare
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*get_key_share)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    struct ANASTASIS_CRYPTO_EncryptedKeyShareP *key_share);


  /**
   * Check if an account exists, and if so, return the
   * current @a recovery_document_hash.
   *
   * @param cls closure
   * @param account_pub account identifier
   * @param[out] paid_until until when is the account paid up?
   * @param[out] recovery_data_hash set to hash of @a recovery document
   * @param[out] version set to the recovery policy version
   * @return transaction status
   */
  enum ANASTASIS_DB_AccountStatus
  (*lookup_account)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    struct GNUNET_TIME_Absolute *paid_until,
    struct GNUNET_HashCode *recovery_data_hash,
    uint32_t *version);


  /**
   * Check payment identifier. Used to check if a payment identifier given by
   * the user is valid (existing and paid).
   *
   * @param cls closure
   * @param payment_secret payment secret which the user must provide with every upload
   * @param[out] paid bool value to show if payment is paid
   * @param[out] valid_counter bool value to show if post_counter is > 0
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*check_payment_identifier)(
    void *cls,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    bool *paid,
    bool *valid_counter);


  /**
   * Check payment identifier. Used to check if a payment identifier given by
   * the user is valid (existing and paid).
   *
   * @param cls closure
   * @param payment_secret payment secret which the user must provide with every upload
   * @param truth_uuid unique identifier of the truth the user must satisfy the challenge
   * @param[out] paid bool value to show if payment is paid
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*check_challenge_payment)(
    void *cls,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    bool *paid);


  /**
   * Increment account lifetime by @a lifetime.
   *
   * @param cls closure
   * @param account_pub which account received a payment
   * @param payment_identifier proof of payment, must be unique and match pending payment
   * @param lifetime for how long is the account now paid (increment)
   * @param[out] paid_until set to the end of the lifetime after the operation
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*increment_lifetime)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    const struct ANASTASIS_PaymentSecretP *payment_identifier,
    struct GNUNET_TIME_Relative lifetime,
    struct GNUNET_TIME_Absolute *paid_until);


  /**
   * Update account lifetime to the maximum of the current
   * value and @a eol.
   *
   * @param cls closure
   * @param account_pub which account received a payment
   * @param payment_identifier proof of payment, must be unique and match pending payment
   * @param eol for how long is the account now paid (absolute)
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*update_lifetime)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    const struct ANASTASIS_PaymentSecretP *payment_identifier,
    struct GNUNET_TIME_Absolute eol);


  /**
   * Store payment. Used to begin a payment, not indicative
   * that the payment actually was made. (That is done
   * when we increment the account's lifetime.)
   *
   * @param cls closure
   * @param account_pub anastasis's public key
   * @param post_counter how many uploads does @a amount pay for
   * @param payment_secret payment secret which the user must provide with every upload
   * @param amount how much we asked for
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*record_recdoc_payment)(
    void *cls,
    const struct ANASTASIS_CRYPTO_AccountPublicKeyP *account_pub,
    uint32_t post_counter,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    const struct TALER_Amount *amount);


  /**
   * Record truth upload payment was made.
   *
   * @param cls closure
   * @param uuid the truth's UUID
   * @param amount the amount that was paid
   * @param duration how long is the truth paid for
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*record_truth_upload_payment)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid,
    const struct TALER_Amount *amount,
    struct GNUNET_TIME_Relative duration);


  /**
   * Inquire whether truth upload payment was made.
   *
   * @param cls closure
   * @param uuid the truth's UUID
   * @param[out] paid_until set for how long this truth is paid for
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*check_truth_upload_paid)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *uuid,
    struct GNUNET_TIME_Absolute *paid_until);


  /**
   * Verify the provided code with the code on the server.
   * If the code matches the function will return with success, if the code
   * does not match, the retry counter will be decreased by one.
   *
   * @param cls closure
   * @param truth_uuid identification of the challenge which the code corresponds to
   * @param hashed_code code which the user provided and wants to verify
   * @return transaction status
   */
  enum ANASTASIS_DB_CodeStatus
  (*verify_challenge_code)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    const struct GNUNET_HashCode *hashed_code);

  /**
   * Insert a new challenge code for a given challenge identified by the challenge
   * public key. The function will first check if there is already a valid code
   * for this challenge present and won't insert a new one in this case.
   *
   * @param cls closure
   * @param truth_uuid the identifier for the challenge
   * @param rotation_period for how long is the code available
   * @param validity_period for how long is the code available
   * @param retry_counter amount of retries allowed
   * @param[out] retransmission_date when to next retransmit
   * @param[out] code set to the code which will be checked for later
   * @return transaction status,
   *        #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if we are out of valid tries,
   *        #GNUNET_DB_STATUS_SUCCESS_ONE_RESULT if @a code is now in the DB
   */
  enum GNUNET_DB_QueryStatus
  (*create_challenge_code)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    struct GNUNET_TIME_Relative rotation_period,
    struct GNUNET_TIME_Relative validity_period,
    unsigned int retry_counter,
    struct GNUNET_TIME_Absolute *retransmission_date,
    uint64_t *code);


  /**
   * Remember in the database that we successfully sent a challenge.
   *
   * @param cls closure
   * @param truth_uuid the identifier for the challenge
   * @param code the challenge that was sent
   */
  enum GNUNET_DB_QueryStatus
  (*mark_challenge_sent)(
    void *cls,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    uint64_t code);


  /**
   * Store payment for challenge.
   *
   * @param cls closure
   * @param truth_key identifier of the challenge to pay
   * @param payment_secret payment secret which the user must provide with every upload
   * @param amount how much we asked for
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*record_challenge_payment)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    const struct ANASTASIS_PaymentSecretP *payment_secret,
    const struct TALER_Amount *amount);


  /**
   * Record refund for challenge.
   *
   * @param cls closure
   * @param truth_uuid identifier of the challenge to refund
   * @param payment_secret payment secret which the user must provide with every upload
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*record_challenge_refund)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    const struct ANASTASIS_PaymentSecretP *payment_secret);


  /**
   * Lookup for a pending payment for a certain challenge
   *
   * @param cls closure
   * @param truth_uuid identification of the challenge
   * @param[out] payment_secret set to the challenge payment secret
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*lookup_challenge_payment)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    struct ANASTASIS_PaymentSecretP *payment_secret);


  /**
   * Update payment status of challenge
   *
   * @param cls closure
   * @param truth_uuid which challenge received a payment
   * @param payment_identifier proof of payment, must be unique and match pending payment
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*update_challenge_payment)(
    void *cls,
    const struct ANASTASIS_CRYPTO_TruthUUIDP *truth_uuid,
    const struct ANASTASIS_PaymentSecretP *payment_identifier);


  /**
   * Function called to remove all expired codes from the database.
   * FIXME: maybe implement as part of @e gc() in the future.
   *
   * @return transaction status
   */
  enum GNUNET_DB_QueryStatus
  (*challenge_gc)(void *cls);


};
#endif