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
|
/*
This file is part of TALER
Copyright (C) 2020, 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 util/crypto_helper_esign.c
* @brief utility functions for running out-of-process private key operations
* @author Christian Grothoff
*/
#include "platform.h"
#include "taler_util.h"
#include "taler_signatures.h"
#include "secmod_eddsa.h"
#include <poll.h>
#include "crypto_helper_common.h"
struct TALER_CRYPTO_ExchangeSignHelper
{
/**
* Function to call with updates to available key material.
*/
TALER_CRYPTO_ExchangeKeyStatusCallback ekc;
/**
* Closure for @e ekc
*/
void *ekc_cls;
/**
* Socket address of the denomination helper process.
* Used to reconnect if the connection breaks.
*/
struct sockaddr_un sa;
/**
* The UNIX domain socket, -1 if we are currently not connected.
*/
int sock;
/**
* Have we reached the sync'ed state?
*/
bool synced;
};
/**
* Disconnect from the helper process. Updates
* @e sock field in @a esh.
*
* @param[in,out] esh handle to tear down connection of
*/
static void
do_disconnect (struct TALER_CRYPTO_ExchangeSignHelper *esh)
{
GNUNET_break (0 == close (esh->sock));
esh->sock = -1;
esh->synced = false;
}
/**
* Try to connect to the helper process. Updates
* @e sock field in @a esh.
*
* @param[in,out] esh handle to establish connection for
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
try_connect (struct TALER_CRYPTO_ExchangeSignHelper *esh)
{
if (-1 != esh->sock)
return GNUNET_OK;
esh->sock = socket (AF_UNIX,
SOCK_STREAM,
0);
if (-1 == esh->sock)
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"socket");
return GNUNET_SYSERR;
}
if (0 !=
connect (esh->sock,
(const struct sockaddr *) &esh->sa,
sizeof (esh->sa)))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
"connect",
esh->sa.sun_path);
do_disconnect (esh);
return GNUNET_SYSERR;
}
return GNUNET_OK;
}
struct TALER_CRYPTO_ExchangeSignHelper *
TALER_CRYPTO_helper_esign_connect (
const struct GNUNET_CONFIGURATION_Handle *cfg,
const char *section,
TALER_CRYPTO_ExchangeKeyStatusCallback ekc,
void *ekc_cls)
{
struct TALER_CRYPTO_ExchangeSignHelper *esh;
char *unixpath;
char *secname;
GNUNET_asprintf (&secname,
"%s-secmod-eddsa",
section);
if (GNUNET_OK !=
GNUNET_CONFIGURATION_get_value_filename (cfg,
secname,
"UNIXPATH",
&unixpath))
{
GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
secname,
"UNIXPATH");
GNUNET_free (secname);
return NULL;
}
/* we use >= here because we want the sun_path to always
be 0-terminated */
if (strlen (unixpath) >= sizeof (esh->sa.sun_path))
{
GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
secname,
"UNIXPATH",
"path too long");
GNUNET_free (unixpath);
GNUNET_free (secname);
return NULL;
}
GNUNET_free (secname);
esh = GNUNET_new (struct TALER_CRYPTO_ExchangeSignHelper);
esh->ekc = ekc;
esh->ekc_cls = ekc_cls;
esh->sa.sun_family = AF_UNIX;
strncpy (esh->sa.sun_path,
unixpath,
sizeof (esh->sa.sun_path) - 1);
GNUNET_free (unixpath);
esh->sock = -1;
if (GNUNET_OK !=
try_connect (esh))
{
TALER_CRYPTO_helper_esign_disconnect (esh);
return NULL;
}
TALER_CRYPTO_helper_esign_poll (esh);
return esh;
}
/**
* Handle a #TALER_HELPER_EDDSA_MT_AVAIL message from the helper.
*
* @param esh helper context
* @param hdr message that we received
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
handle_mt_avail (struct TALER_CRYPTO_ExchangeSignHelper *esh,
const struct GNUNET_MessageHeader *hdr)
{
const struct TALER_CRYPTO_EddsaKeyAvailableNotification *kan
= (const struct TALER_CRYPTO_EddsaKeyAvailableNotification *) hdr;
if (sizeof (*kan) != ntohs (hdr->size))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
if (GNUNET_OK !=
TALER_exchange_secmod_eddsa_verify (
&kan->exchange_pub,
GNUNET_TIME_timestamp_ntoh (kan->anchor_time),
GNUNET_TIME_relative_ntoh (kan->duration),
&kan->secm_pub,
&kan->secm_sig))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
esh->ekc (esh->ekc_cls,
GNUNET_TIME_timestamp_ntoh (kan->anchor_time),
GNUNET_TIME_relative_ntoh (kan->duration),
&kan->exchange_pub,
&kan->secm_pub,
&kan->secm_sig);
return GNUNET_OK;
}
/**
* Handle a #TALER_HELPER_EDDSA_MT_PURGE message from the helper.
*
* @param esh helper context
* @param hdr message that we received
* @return #GNUNET_OK on success
*/
static enum GNUNET_GenericReturnValue
handle_mt_purge (struct TALER_CRYPTO_ExchangeSignHelper *esh,
const struct GNUNET_MessageHeader *hdr)
{
const struct TALER_CRYPTO_EddsaKeyPurgeNotification *pn
= (const struct TALER_CRYPTO_EddsaKeyPurgeNotification *) hdr;
if (sizeof (*pn) != ntohs (hdr->size))
{
GNUNET_break_op (0);
return GNUNET_SYSERR;
}
esh->ekc (esh->ekc_cls,
GNUNET_TIME_UNIT_ZERO_TS,
GNUNET_TIME_UNIT_ZERO,
&pn->exchange_pub,
NULL,
NULL);
return GNUNET_OK;
}
void
TALER_CRYPTO_helper_esign_poll (struct TALER_CRYPTO_ExchangeSignHelper *esh)
{
char buf[UINT16_MAX];
size_t off = 0;
unsigned int retry_limit = 3;
const struct GNUNET_MessageHeader *hdr
= (const struct GNUNET_MessageHeader *) buf;
if (GNUNET_OK !=
try_connect (esh))
return; /* give up */
while (1)
{
uint16_t msize;
ssize_t ret;
ret = recv (esh->sock,
buf + off,
sizeof (buf) - off,
(esh->synced && (0 == off))
? MSG_DONTWAIT
: 0);
if (ret < 0)
{
if (EINTR == errno)
continue;
if (EAGAIN == errno)
{
GNUNET_assert (esh->synced);
GNUNET_assert (0 == off);
break;
}
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"recv");
do_disconnect (esh);
if (0 == retry_limit)
return; /* give up */
if (GNUNET_OK !=
try_connect (esh))
return; /* give up */
retry_limit--;
continue;
}
if (0 == ret)
{
GNUNET_break (0 == off);
return;
}
off += ret;
more:
if (off < sizeof (struct GNUNET_MessageHeader))
continue;
msize = ntohs (hdr->size);
if (off < msize)
continue;
switch (ntohs (hdr->type))
{
case TALER_HELPER_EDDSA_MT_AVAIL:
if (GNUNET_OK !=
handle_mt_avail (esh,
hdr))
{
GNUNET_break_op (0);
do_disconnect (esh);
return;
}
break;
case TALER_HELPER_EDDSA_MT_PURGE:
if (GNUNET_OK !=
handle_mt_purge (esh,
hdr))
{
GNUNET_break_op (0);
do_disconnect (esh);
return;
}
break;
case TALER_HELPER_EDDSA_SYNCED:
GNUNET_log (GNUNET_ERROR_TYPE_INFO,
"Now synchronized with EdDSA helper\n");
esh->synced = true;
break;
default:
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Received unexpected message of type %d (len: %u)\n",
(unsigned int) ntohs (hdr->type),
(unsigned int) msize);
GNUNET_break_op (0);
do_disconnect (esh);
return;
}
memmove (buf,
&buf[msize],
off - msize);
off -= msize;
goto more;
}
}
enum TALER_ErrorCode
TALER_CRYPTO_helper_esign_sign_ (
struct TALER_CRYPTO_ExchangeSignHelper *esh,
const struct GNUNET_CRYPTO_EccSignaturePurpose *purpose,
struct TALER_ExchangePublicKeyP *exchange_pub,
struct TALER_ExchangeSignatureP *exchange_sig)
{
uint32_t purpose_size = ntohl (purpose->size);
if (GNUNET_OK !=
try_connect (esh))
{
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Failed to connect to helper\n");
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_UNAVAILABLE;
}
GNUNET_assert (purpose_size <
UINT16_MAX - sizeof (struct TALER_CRYPTO_EddsaSignRequest));
{
char buf[sizeof (struct TALER_CRYPTO_EddsaSignRequest) + purpose_size
- sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose)];
struct TALER_CRYPTO_EddsaSignRequest *sr
= (struct TALER_CRYPTO_EddsaSignRequest *) buf;
sr->header.size = htons (sizeof (buf));
sr->header.type = htons (TALER_HELPER_EDDSA_MT_REQ_SIGN);
sr->reserved = htonl (0);
GNUNET_memcpy (&sr->purpose,
purpose,
purpose_size);
if (GNUNET_OK !=
TALER_crypto_helper_send_all (esh->sock,
buf,
sizeof (buf)))
{
GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
"send",
esh->sa.sun_path);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_UNAVAILABLE;
}
}
{
char buf[UINT16_MAX];
size_t off = 0;
const struct GNUNET_MessageHeader *hdr
= (const struct GNUNET_MessageHeader *) buf;
bool finished = false;
enum TALER_ErrorCode ec = TALER_EC_INVALID;
while (1)
{
ssize_t ret;
uint16_t msize;
ret = recv (esh->sock,
&buf[off],
sizeof (buf) - off,
(finished && (0 == off))
? MSG_DONTWAIT
: 0);
if (ret < 0)
{
if (EINTR == errno)
continue;
if (EAGAIN == errno)
{
GNUNET_assert (finished);
GNUNET_assert (0 == off);
break;
}
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"recv");
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_UNAVAILABLE;
}
if (0 == ret)
{
GNUNET_break (0 == off);
if (finished)
return TALER_EC_NONE;
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
off += ret;
more:
if (off < sizeof (struct GNUNET_MessageHeader))
continue;
msize = ntohs (hdr->size);
if (off < msize)
continue;
switch (ntohs (hdr->type))
{
case TALER_HELPER_EDDSA_MT_RES_SIGNATURE:
if (msize != sizeof (struct TALER_CRYPTO_EddsaSignResponse))
{
GNUNET_break_op (0);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
if (finished)
{
GNUNET_break_op (0);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
{
const struct TALER_CRYPTO_EddsaSignResponse *sr =
(const struct TALER_CRYPTO_EddsaSignResponse *) buf;
*exchange_sig = sr->exchange_sig;
*exchange_pub = sr->exchange_pub;
finished = true;
ec = TALER_EC_NONE;
break;
}
case TALER_HELPER_EDDSA_MT_RES_SIGN_FAILURE:
if (msize != sizeof (struct TALER_CRYPTO_EddsaSignFailure))
{
GNUNET_break_op (0);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
{
const struct TALER_CRYPTO_EddsaSignFailure *sf =
(const struct TALER_CRYPTO_EddsaSignFailure *) buf;
finished = true;
ec = (enum TALER_ErrorCode) (int) ntohl (sf->ec);
break;
}
case TALER_HELPER_EDDSA_MT_AVAIL:
if (GNUNET_OK !=
handle_mt_avail (esh,
hdr))
{
GNUNET_break_op (0);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
break; /* while(1) loop ensures we recv() again */
case TALER_HELPER_EDDSA_MT_PURGE:
if (GNUNET_OK !=
handle_mt_purge (esh,
hdr))
{
GNUNET_break_op (0);
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
break; /* while(1) loop ensures we recv() again */
case TALER_HELPER_EDDSA_SYNCED:
GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
"Synchronized add odd time with EdDSA helper!\n");
esh->synced = true;
break;
default:
GNUNET_break_op (0);
GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
"Received unexpected message of type %u\n",
ntohs (hdr->type));
do_disconnect (esh);
return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG;
}
memmove (buf,
&buf[msize],
off - msize);
off -= msize;
goto more;
} /* while(1) */
return ec;
}
}
void
TALER_CRYPTO_helper_esign_revoke (
struct TALER_CRYPTO_ExchangeSignHelper *esh,
const struct TALER_ExchangePublicKeyP *exchange_pub)
{
if (GNUNET_OK !=
try_connect (esh))
return; /* give up */
{
struct TALER_CRYPTO_EddsaRevokeRequest rr = {
.header.size = htons (sizeof (rr)),
.header.type = htons (TALER_HELPER_EDDSA_MT_REQ_REVOKE),
.exchange_pub = *exchange_pub
};
if (GNUNET_OK !=
TALER_crypto_helper_send_all (esh->sock,
&rr,
sizeof (rr)))
{
GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
"send");
do_disconnect (esh);
return;
}
}
}
void
TALER_CRYPTO_helper_esign_disconnect (
struct TALER_CRYPTO_ExchangeSignHelper *esh)
{
if (-1 != esh->sock)
do_disconnect (esh);
GNUNET_free (esh);
}
/* end of crypto_helper_esign.c */
|