summaryrefslogtreecommitdiff
path: root/src/exchange-tools/taler-crypto-worker.c
blob: a1db1e8125caeb75cc062fb32c37ecad2cf8eab4 (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
/*
  This file is part of TALER
  Copyright (C) 2014-2021 Taler Systems SA

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

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

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
*/
/**
 * @file exchange-tools/taler-crypto-worker.c
 * @brief Standalone process to perform various cryptographic operations.
 * @author Florian Dold
 */
#include "platform.h"
#include "taler_util.h"
#include <gnunet/gnunet_json_lib.h>
#include <gnunet/gnunet_crypto_lib.h>
#include "taler_error_codes.h"
#include "taler_json_lib.h"
#include "taler_signatures.h"


/**
 * Return value from main().
 */
static int global_ret;


/**
 * Main function that will be run under the GNUnet scheduler.
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param cfg configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile,
     const struct GNUNET_CONFIGURATION_Handle *cfg)
{
  (void) cls;
  (void) args;
  (void) cfgfile;

  json_t *req;
  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
              "started crypto worker\n");

  for (;;)
  {
    const char *op;
    const json_t *args;
    req = json_loadf (stdin, JSON_DISABLE_EOF_CHECK, NULL);
    if (NULL == req)
    {
      if (feof (stdin))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                    "end of input\n");
        global_ret = 0;
        return;
      }
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "invalid JSON\n");
      global_ret = 1;
      return;
    }
    op = json_string_value (json_object_get (req,
                                             "op"));
    if (! op)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "no op specified\n");
      global_ret = 1;
      return;
    }
    args = json_object_get (req, "args");
    if (! args)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                  "no args specified\n");
      global_ret = 1;
      return;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                "got request\n");
    if (0 == strcmp ("eddsa_get_public",
                     op))
    {
      struct GNUNET_CRYPTO_EddsaPublicKey eddsa_pub;
      struct GNUNET_CRYPTO_EddsaPrivateKey eddsa_priv;
      json_t *resp;
      struct GNUNET_JSON_Specification spec[] = {
        GNUNET_JSON_spec_fixed_auto ("eddsa_priv",
                                     &eddsa_priv),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK != GNUNET_JSON_parse (args,
                                          spec,
                                          NULL,
                                          NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      GNUNET_CRYPTO_eddsa_key_get_public (&eddsa_priv,
                                          &eddsa_pub);
      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto ("eddsa_pub",
                                    &eddsa_pub)
        );
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (spec);
      continue;
    }
    if (0 == strcmp ("ecdhe_get_public",
                     op))
    {
      struct GNUNET_CRYPTO_EcdhePublicKey ecdhe_pub;
      struct GNUNET_CRYPTO_EcdhePrivateKey ecdhe_priv;
      json_t *resp;
      struct GNUNET_JSON_Specification spec[] = {
        GNUNET_JSON_spec_fixed_auto ("ecdhe_priv",
                                     &ecdhe_priv),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK != GNUNET_JSON_parse (args,
                                          spec,
                                          NULL,
                                          NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      GNUNET_CRYPTO_ecdhe_key_get_public (&ecdhe_priv,
                                          &ecdhe_pub);
      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto ("ecdhe_pub",
                                    &ecdhe_pub)
        );
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (spec);
      continue;
    }
    if (0 == strcmp ("eddsa_verify",
                     op))
    {
      struct GNUNET_CRYPTO_EddsaPublicKey pub;
      struct GNUNET_CRYPTO_EddsaSignature sig;
      struct GNUNET_CRYPTO_EccSignaturePurpose *msg;
      size_t msg_size;
      enum GNUNET_GenericReturnValue verify_ret;
      json_t *resp;
      struct GNUNET_JSON_Specification eddsa_verify_spec[] = {
        GNUNET_JSON_spec_fixed_auto ("pub",
                                     &pub),
        GNUNET_JSON_spec_fixed_auto ("sig",
                                     &sig),
        GNUNET_JSON_spec_varsize ("msg",
                                  (void **) &msg,
                                  &msg_size),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK != GNUNET_JSON_parse (args,
                                          eddsa_verify_spec,
                                          NULL,
                                          NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      verify_ret = GNUNET_CRYPTO_eddsa_verify_ (
        ntohl (msg->purpose),
        msg,
        &sig,
        &pub);
      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_bool ("valid",
                               GNUNET_OK == verify_ret));
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (eddsa_verify_spec);
      continue;
    }
    if (0 == strcmp ("kx_ecdhe_eddsa",
                     op))
    {
      struct GNUNET_CRYPTO_EcdhePrivateKey priv;
      struct GNUNET_CRYPTO_EddsaPublicKey pub;
      struct GNUNET_HashCode key_material;
      json_t *resp;
      struct GNUNET_JSON_Specification kx_spec[] = {
        GNUNET_JSON_spec_fixed_auto ("eddsa_pub",
                                     &pub),
        GNUNET_JSON_spec_fixed_auto ("ecdhe_priv",
                                     &priv),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK != GNUNET_JSON_parse (args,
                                          kx_spec,
                                          NULL,
                                          NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      if (GNUNET_OK != GNUNET_CRYPTO_ecdh_eddsa (&priv,
                                                 &pub,
                                                 &key_material))
      {
        // FIXME: Return as result?
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "kx failed\n");
        global_ret = 1;
        return;
      }
      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto ("h",
                                    &key_material)
        );
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (kx_spec);
      continue;
    }
    if (0 == strcmp ("eddsa_sign",
                     op))
    {
      struct GNUNET_CRYPTO_EddsaSignature sig;
      struct GNUNET_CRYPTO_EccSignaturePurpose *msg;
      struct GNUNET_CRYPTO_EddsaPrivateKey priv;
      size_t msg_size;
      json_t *resp;
      struct GNUNET_JSON_Specification eddsa_sign_spec[] = {
        GNUNET_JSON_spec_fixed_auto ("priv",
                                     &priv),
        GNUNET_JSON_spec_varsize ("msg",
                                  (void **) &msg,
                                  &msg_size),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK !=
          GNUNET_JSON_parse (args,
                             eddsa_sign_spec,
                             NULL,
                             NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      GNUNET_assert (GNUNET_OK ==
                     GNUNET_CRYPTO_eddsa_sign_ (
                       &priv,
                       msg,
                       &sig));
      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto ("sig", &sig)
        );
      json_dumpf (resp, stdout,
                  JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (eddsa_sign_spec);
      continue;
    }
    if (0 == strcmp ("setup_refresh_planchet", op))
    {
      struct TALER_TransferSecretP transfer_secret;
      uint32_t coin_index;
      json_t *resp;
      struct GNUNET_JSON_Specification setup_refresh_planchet_spec[] = {
        GNUNET_JSON_spec_uint32 ("coin_index",
                                 &coin_index),
        GNUNET_JSON_spec_fixed_auto ("transfer_secret",
                                     &transfer_secret),
        GNUNET_JSON_spec_end ()
      };
      struct TALER_CoinSpendPublicKeyP coin_pub;
      struct TALER_CoinSpendPrivateKeyP coin_priv;
      struct TALER_PlanchetMasterSecretP ps;
      struct TALER_ExchangeWithdrawValues alg_values = {
        // FIXME: also allow CS
        .cipher = TALER_DENOMINATION_RSA,
      };
      union TALER_DenominationBlindingKeyP dbk;

      if (GNUNET_OK !=
          GNUNET_JSON_parse (args,
                             setup_refresh_planchet_spec,
                             NULL,
                             NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      TALER_transfer_secret_to_planchet_secret (&transfer_secret,
                                                coin_index,
                                                &ps);
      TALER_planchet_setup_coin_priv (&ps,
                                      &alg_values,
                                      &coin_priv);
      GNUNET_CRYPTO_eddsa_key_get_public (&coin_priv.eddsa_priv,
                                          &coin_pub.eddsa_pub);
      TALER_planchet_blinding_secret_create (&ps,
                                             &alg_values,
                                             &dbk);

      resp = GNUNET_JSON_PACK (
        GNUNET_JSON_pack_data_auto ("coin_priv", &coin_priv),
        GNUNET_JSON_pack_data_auto ("coin_pub", &coin_pub),
        GNUNET_JSON_pack_data_auto ("blinding_key", &dbk.rsa_bks)
        );
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (setup_refresh_planchet_spec);
      continue;
    }
    if (0 == strcmp ("rsa_blind", op))
    {
      struct GNUNET_HashCode hm;
      struct GNUNET_CRYPTO_RsaBlindingKeySecret bks;
      void *pub_enc;
      size_t pub_enc_size;
      int success;
      struct GNUNET_CRYPTO_RsaPublicKey *pub;
      void *blinded_buf;
      size_t blinded_size;
      json_t *resp;
      struct GNUNET_JSON_Specification rsa_blind_spec[] = {
        GNUNET_JSON_spec_fixed_auto ("hm",
                                     &hm),
        GNUNET_JSON_spec_fixed_auto ("bks",
                                     &bks),
        GNUNET_JSON_spec_varsize ("pub",
                                  &pub_enc,
                                  &pub_enc_size),
        GNUNET_JSON_spec_end ()
      };
      if (GNUNET_OK !=
          GNUNET_JSON_parse (args,
                             rsa_blind_spec,
                             NULL,
                             NULL))
      {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "malformed op args\n");
        global_ret = 1;
        return;
      }
      pub = GNUNET_CRYPTO_rsa_public_key_decode (pub_enc,
                                                 pub_enc_size);
      success = GNUNET_CRYPTO_rsa_blind (&hm,
                                         &bks,
                                         pub,
                                         &blinded_buf,
                                         &blinded_size);

      if (GNUNET_YES == success)
      {
        resp = GNUNET_JSON_PACK (
          GNUNET_JSON_pack_data_varsize ("blinded", blinded_buf, blinded_size),
          GNUNET_JSON_pack_bool ("success", true)
          );
      }
      else
      {
        resp = GNUNET_JSON_PACK (
          GNUNET_JSON_pack_bool ("success", false)
          );
      }
      json_dumpf (resp, stdout, JSON_COMPACT);
      printf ("\n");
      fflush (stdout);
      GNUNET_log (GNUNET_ERROR_TYPE_INFO,
                  "sent response\n");
      GNUNET_JSON_parse_free (rsa_blind_spec);
      GNUNET_free (blinded_buf);
      continue;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "unsupported operation '%s'\n",
                op);
    global_ret = 1;
    return;
  }

}


/**
 * The entry point.
 *
 * @param argc number of arguments in @a argv
 * @param argv command-line arguments
 * @return 0 on normal termination
 */
int
main (int argc,
      char **argv)
{
  struct GNUNET_GETOPT_CommandLineOption options[] = {
    GNUNET_GETOPT_OPTION_END
  };
  int ret;

  /* force linker to link against libtalerutil; if we do
   not do this, the linker may "optimize" libtalerutil
   away and skip #TALER_OS_init(), which we do need */
  TALER_OS_init ();
  ret = GNUNET_PROGRAM_run (argc, argv,
                            "taler-crypto-worker",
                            "Execute cryptographic operations read from stdin",
                            options,
                            &run,
                            NULL);
  if (GNUNET_NO == ret)
    return 0;
  if (GNUNET_SYSERR == ret)
    return 1;
  return global_ret;
}