summaryrefslogtreecommitdiff
path: root/src/include/sync_service.h
blob: 8b47f33f9eb3b05384257bd0a56fe70ad7b680aa (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
/*
  This file is part of TALER
  Copyright (C) 2019 Taler Systems SA

  Anastasis is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero 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 Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with
  Anastasis; see the file COPYING.LIB.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file include/sync_service.h
 * @brief C interface of libsync, a C library to use sync's HTTP API
 * @author Christian Grothoff
 */
#ifndef SYNC_SERVICE_H
#define SYNC_SERVICE_H

#include <gnunet/gnunet_util_lib.h>
#include <taler/taler_error_codes.h>
#include <gnunet/gnunet_curl_lib.h>
#include <jansson.h>


GNUNET_NETWORK_STRUCT_BEGIN


/**
 * Private key identifying an account.
 */
struct SYNC_AccountPrivateKeyP
{
  /**
   * We use EdDSA.
   */
  struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_priv;
};


/**
 * Public key identifying an account.
 */
struct SYNC_AccountPublicKeyP
{
  /**
   * We use EdDSA.
   */
  struct GNUNET_CRYPTO_EddsaPublicKey eddsa_pub;
};


/**
 * Data signed by the account public key of a sync client to
 * authorize the upload of the backup.
 */
struct SYNC_UploadSignaturePS
{
  /**
   * Set to #TALER_SIGNATURE_SYNC_BACKUP_UPLOAD.
   */
  struct GNUNET_CRYPTO_EccSignaturePurpose purpose;

  /**
   * Hash of the previous backup, all zeros for none.
   */
  struct GNUNET_HashCode old_backup_hash GNUNET_PACKED;

  /**
   * Hash of the new backup.
   */
  struct GNUNET_HashCode new_backup_hash GNUNET_PACKED;

};


/**
 * Signature made with an account's public key.
 */
struct SYNC_AccountSignatureP
{
  /**
   * We use EdDSA.
   */
  struct GNUNET_CRYPTO_EddsaSignature eddsa_sig;
};

GNUNET_NETWORK_STRUCT_END


/**
 * High-level ways how an upload may conclude.
 */
enum SYNC_UploadStatus
{
  /**
   * Backup was successfully made.
   */
  SYNC_US_SUCCESS = 0,

  /**
   * Account expired or payment was explicitly requested
   * by the client.
   */
  SYNC_US_PAYMENT_REQUIRED = 1,

  /**
   * Conflicting backup existed on server. Client should
   * reconcile and try again with (using the provided
   * recovered backup as the previous backup).
   */
  SYNC_US_CONFLICTING_BACKUP = 2,

  /**
   * HTTP interaction failed, see HTTP status.
   */
  SYNC_US_HTTP_ERROR = 3,

  /**
   * We had an internal error (not sure this can happen,
   * but reserved for HTTP 400 status codes).
   */
  SYNC_US_CLIENT_ERROR = 4,

  /**
   * Server had an internal error.
   */
  SYNC_US_SERVER_ERROR = 5
};


/**
 * Result of an upload.
 */
struct SYNC_UploadDetails
{
  /**
   * High level status of the upload operation.
   */
  enum SYNC_UploadStatus us;

  union
  {

    /**
     * Hash of the synchronized backup, returned if
     * @e us is #SYNC_US_SUCCESS.
     */
    const struct GNUNET_HashCode *curr_backup_hash;

    /**
     * Previous backup. Returned if @e us is
     * #SYNC_US_CONFLICTING_BACKUP
     */
    struct
    {
      /**
       * Hash over @e existing_backup.
       */
      struct GNUNET_HashCode existing_backup_hash;

      /**
       * Number of bytes in @e existing_backup.
       */
      size_t existing_backup_size;

      /**
       * The backup on the server, which does not match the
       * "previous" backup expected by the client and thus
       * needs to be decrypted, reconciled and re-uploaded.
       */
      const void *existing_backup;

    } recovered_backup;

    /**
     * A taler://pay/-URI with a request to pay the annual fee for
     * the service.  Returned if @e us is #SYNC_US_PAYMENT_REQUIRED.
     */
    const char *payment_request;

  } details;

};


/**
 * Function called with the results of a #SYNC_upload().
 *
 * @param cls closure
 * @param ec Taler error code
 * @param http_status HTTP status of the request
 * @param ud details about the upload operation
 */
typedef void
(*SYNC_UploadCallback)(void *cls,
                       enum TALER_ErrorCode ec,
                       unsigned int http_status,
                       const struct SYNC_UploadDetails *ud);


/**
 * Handle for an upload operatoin.
 */
struct SYNC_UploadOperation;

/**
 * Options for payment.
 */
enum SYNC_PaymentOptions
{
  /**
   * No special options.
   */
  SYNC_PO_NONE = 0,

  /**
   * Trigger payment even if sync does not require it
   * yet (forced payment).
   */
  SYNC_PO_FORCE_PAYMENT = 1,

  /**
   * Request a fresh order to be created, say because the
   * existing one was claimed (but not paid) by another wallet.
   */
  SYNC_PO_FRESH_ORDER = 2
};

/**
 * Upload a @a backup to a Sync server. Note that @a backup must
 * have already been compressed, padded and encrypted by the
 * client.
 *
 * While @a pub is theoretically protected by the HTTPS protocol and
 * required to access the backup, it should be assumed that an
 * adversary might be able to download the backups from the Sync
 * server -- or even run the Sync server. Thus, strong encryption
 * is essential and NOT implemented by this function.
 *
 * The use of Anastasis to safely store the Sync encryption keys and
 * @a pub is recommended.  Storing @a priv in Anastasis depends on
 * your priorities: without @a priv, further updates to the backup are
 * not possible, and the user would have to pay for another
 * account. OTOH, without @a priv an adversary that compromised
 * Anastasis can only read the backups, but not alter or destroy them.
 *
 * @param ctx for HTTP client request processing
 * @param base_url base URL of the Sync server
 * @param priv private key of an account with the server
 * @param prev_backup_hash hash of the previous backup, NULL for the first upload ever
 * @param backup_size number of bytes in @a backup
 * @param backup the encrypted backup, must remain in
 *         memory until we are done with the operation!
 * @param payment_requested #GNUNET_YES if the client wants to pay more for the account now
 * @param po payment options
 * @param cb function to call with the result
 * @param cb_cls closure for @a cb
 * @return handle for the operation
 */
struct SYNC_UploadOperation *
SYNC_upload (struct GNUNET_CURL_Context *ctx,
             const char *base_url,
             struct SYNC_AccountPrivateKeyP *priv,
             const struct GNUNET_HashCode *prev_backup_hash,
             size_t backup_size,
             const void *backup,
             enum SYNC_PaymentOptions po,
             const char *paid_order_id,
             SYNC_UploadCallback cb,
             void *cb_cls);


/**
 * Cancel the upload.  Note that aborting an upload does NOT guarantee
 * that it did not complete, it is possible that the server did
 * receive the full request before the upload is aborted.
 *
 * @param uo operation to cancel.
 */
void
SYNC_upload_cancel (struct SYNC_UploadOperation *uo);


/**
 * Detailed results from the successful download.
 */
struct SYNC_DownloadDetails
{
  /**
   * Signature (already verified).
   */
  struct SYNC_AccountSignatureP sig;

  /**
   * Hash of the previous version.
   */
  struct GNUNET_HashCode prev_backup_hash;

  /**
   * Hash over @e backup and @e backup_size.
   */
  struct GNUNET_HashCode curr_backup_hash;

  /**
   * The backup we downloaded.
   */
  const void *backup;

  /**
   * Number of bytes in @e backup.
   */
  size_t backup_size;

};

/**
 * Function called with the results of a #SYNC_download().
 *
 * @param cls closure
 * @param sig signature of the account owner, affirming the
 *            integrity of the backup (already verified)
 * @param prev_backup_hash hash of the previous backup (used
 *            to verify the signature, could be used by clients
 *            to verify backup chains)
 * @param curr_backup_hash hash over @a backup (verified)
 * @param backup_size number of bytes in @a backup
 * @param backup the latest backup as downloaded from the
 *        server and affirmed by @a sig
 */
typedef void
(*SYNC_DownloadCallback)(void *cls,
                         unsigned int http_status,
                         const struct SYNC_DownloadDetails *dd);


/**
 * Handle for a download operation.
 */
struct SYNC_DownloadOperation;


/**
 * Download the latest version of a backup for account @a pub.
 *
 * @param ctx for HTTP client request processing
 * @param base_url base URL of the Sync server
 * @param pub account public key
 * @param cb function to call with the backup
 * @param cb_cls closure for @a cb
 * @return handle for the operation
 */
struct SYNC_DownloadOperation *
SYNC_download (struct GNUNET_CURL_Context *ctx,
               const char *base_url,
               const struct SYNC_AccountPublicKeyP *pub,
               SYNC_DownloadCallback cb,
               void *cb_cls);


/**
 * Cancel the download.
 *
 * @param do operation to cancel.
 */
void
SYNC_download_cancel (struct SYNC_DownloadOperation *download);


#endif  /* SYNC_SERVICE_H */