Line data Source code
1 : /*
2 : This file is part of TALER
3 : Copyright (C) 2014-2026 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 util/secmod_rsa.c
18 : * @brief Standalone process to perform private key RSA operations
19 : * @author Christian Grothoff
20 : *
21 : * Key design points:
22 : * - EVERY thread of the exchange will have its own pair of connections to the
23 : * crypto helpers. This way, every thread will also have its own /keys state
24 : * and avoid the need to synchronize on those.
25 : * - auditor signatures and master signatures are to be kept in the exchange DB,
26 : * and merged with the public keys of the helper by the exchange HTTPD!
27 : * - the main loop of the helper is SINGLE-THREADED, but there are
28 : * threads for crypto-workers which do the signing in parallel, one per client.
29 : * - thread-safety: signing happens in parallel, thus when REMOVING private keys,
30 : * we must ensure that all signers are done before we fully free() the
31 : * private key. This is done by reference counting (as work is always
32 : * assigned and collected by the main thread).
33 : */
34 : #include "platform.h"
35 : #include "taler/taler_util.h"
36 : #include "secmod_rsa.h"
37 : #include <gcrypt.h>
38 : #include <pthread.h>
39 : #include "taler/taler_error_codes.h"
40 : #include "taler/taler_signatures.h"
41 : #include "secmod_common.h"
42 : #include <poll.h>
43 :
44 :
45 : /**
46 : * Information we keep per denomination.
47 : */
48 : struct Denomination;
49 :
50 :
51 : /**
52 : * One particular denomination key.
53 : */
54 : struct DenominationKey
55 : {
56 :
57 : /**
58 : * Kept in a DLL of the respective denomination. Sorted by anchor time.
59 : */
60 : struct DenominationKey *next;
61 :
62 : /**
63 : * Kept in a DLL of the respective denomination. Sorted by anchor time.
64 : */
65 : struct DenominationKey *prev;
66 :
67 : /**
68 : * Denomination this key belongs to.
69 : */
70 : struct Denomination *denom;
71 :
72 : /**
73 : * Name of the file this key is stored under.
74 : */
75 : char *filename;
76 :
77 : /**
78 : * The private key of the denomination.
79 : */
80 : struct GNUNET_CRYPTO_RsaPrivateKey *denom_priv;
81 :
82 : /**
83 : * The public key of the denomination.
84 : */
85 : struct GNUNET_CRYPTO_RsaPublicKey *denom_pub;
86 :
87 : /**
88 : * Message to transmit to clients to introduce this public key.
89 : */
90 : struct TALER_CRYPTO_RsaKeyAvailableNotification *an;
91 :
92 : /**
93 : * Hash of this denomination's public key.
94 : */
95 : struct TALER_RsaPubHashP h_rsa;
96 :
97 : /**
98 : * Time at which this key is supposed to become valid.
99 : */
100 : struct GNUNET_TIME_Timestamp anchor_start;
101 :
102 : /**
103 : * Time at which this key is supposed to expire (exclusive).
104 : */
105 : struct GNUNET_TIME_Timestamp anchor_end;
106 :
107 : /**
108 : * Generation when this key was created or revoked.
109 : */
110 : uint64_t key_gen;
111 :
112 : /**
113 : * Reference counter. Counts the number of threads that are
114 : * using this key at this time.
115 : */
116 : unsigned int rc;
117 :
118 : /**
119 : * Flag set to true if this key has been purged and the memory
120 : * must be freed as soon as @e rc hits zero.
121 : */
122 : bool purge;
123 :
124 : };
125 :
126 :
127 : struct Denomination
128 : {
129 :
130 : /**
131 : * Kept in a DLL.
132 : */
133 : struct Denomination *next;
134 :
135 : /**
136 : * Kept in a DLL.
137 : */
138 : struct Denomination *prev;
139 :
140 : /**
141 : * Head of DLL of actual keys of this denomination.
142 : */
143 : struct DenominationKey *keys_head;
144 :
145 : /**
146 : * Tail of DLL of actual keys of this denomination.
147 : */
148 : struct DenominationKey *keys_tail;
149 :
150 : /**
151 : * How long can coins be withdrawn (generated)? Should be small
152 : * enough to limit how many coins will be signed into existence with
153 : * the same key, but large enough to still provide a reasonable
154 : * anonymity set.
155 : */
156 : struct GNUNET_TIME_Relative duration_withdraw;
157 :
158 : /**
159 : * Calendar interval the start of the validity period of our keys is
160 : * rounded down to (and the end of the validity period rounded up to).
161 : * #GNUNET_TIME_RI_NONE (the default) disables the rounding. Donau sets
162 : * this to #GNUNET_TIME_RI_YEAR so that its keys are valid for exactly one
163 : * calendar year (starting January 1st UTC), even if the key was generated
164 : * in the middle of the year.
165 : */
166 : enum GNUNET_TIME_RounderInterval anchor_round;
167 :
168 : /**
169 : * What is the configuration section of this denomination type? Also used
170 : * for the directory name where the denomination keys are stored.
171 : */
172 : char *section;
173 :
174 : /**
175 : * Length of (new) RSA keys (in bits).
176 : */
177 : uint32_t rsa_keysize;
178 : };
179 :
180 :
181 : /**
182 : * A semaphore.
183 : */
184 : struct Semaphore
185 : {
186 : /**
187 : * Mutex for the semaphore.
188 : */
189 : pthread_mutex_t mutex;
190 :
191 : /**
192 : * Condition variable for the semaphore.
193 : */
194 : pthread_cond_t cv;
195 :
196 : /**
197 : * Counter of the semaphore.
198 : */
199 : unsigned int ctr;
200 : };
201 :
202 :
203 : /**
204 : * Job in a batch sign request.
205 : */
206 : struct BatchJob;
207 :
208 : /**
209 : * Handle for a thread that does work in batch signing.
210 : */
211 : struct Worker
212 : {
213 : /**
214 : * Kept in a DLL.
215 : */
216 : struct Worker *prev;
217 :
218 : /**
219 : * Kept in a DLL.
220 : */
221 : struct Worker *next;
222 :
223 : /**
224 : * Job this worker should do next.
225 : */
226 : struct BatchJob *job;
227 :
228 : /**
229 : * Semaphore to signal the worker that a job is available.
230 : */
231 : struct Semaphore sem;
232 :
233 : /**
234 : * Handle for this thread.
235 : */
236 : pthread_t pt;
237 :
238 : /**
239 : * Set to true if the worker should terminate.
240 : */
241 : bool do_shutdown;
242 : };
243 :
244 :
245 : /**
246 : * Job in a batch sign request.
247 : */
248 : struct BatchJob
249 : {
250 : /**
251 : * Request we are working on.
252 : */
253 : const struct TALER_CRYPTO_SignRequest *sr;
254 :
255 : /**
256 : * Thread doing the work.
257 : */
258 : struct Worker *worker;
259 :
260 : /**
261 : * Result with the signature.
262 : */
263 : struct GNUNET_CRYPTO_RsaSignature *rsa_signature;
264 :
265 : /**
266 : * Semaphore to signal that the job is finished.
267 : */
268 : struct Semaphore sem;
269 :
270 : /**
271 : * Computation status.
272 : */
273 : enum TALER_ErrorCode ec;
274 :
275 : };
276 :
277 :
278 : /**
279 : * Head of DLL of workers ready for more work.
280 : */
281 : static struct Worker *worker_head;
282 :
283 : /**
284 : * Tail of DLL of workers ready for more work.
285 : */
286 : static struct Worker *worker_tail;
287 :
288 : /**
289 : * Lock for manipulating the worker DLL.
290 : */
291 : static pthread_mutex_t worker_lock = PTHREAD_MUTEX_INITIALIZER;
292 :
293 : /**
294 : * Total number of workers that were started.
295 : */
296 : static unsigned int workers;
297 :
298 : /**
299 : * Semaphore used to grab a worker.
300 : */
301 : static struct Semaphore worker_sem;
302 :
303 : /**
304 : * Command-line options for various TALER_SECMOD_XXX_run() functions.
305 : */
306 : static struct TALER_SECMOD_Options *globals;
307 :
308 : /**
309 : * Where do we store the keys?
310 : */
311 : static char *keydir;
312 :
313 : /**
314 : * How much should coin creation (@e duration_withdraw) duration overlap
315 : * with the next denomination? Basically, the starting time of two
316 : * denominations is always @e duration_withdraw - #overlap_duration apart.
317 : */
318 : static struct GNUNET_TIME_Relative overlap_duration;
319 :
320 : /**
321 : * How long into the future do we pre-generate keys?
322 : */
323 : static struct GNUNET_TIME_Relative lookahead_sign;
324 :
325 : /**
326 : * All of our denominations, in a DLL. Sorted?
327 : */
328 : static struct Denomination *denom_head;
329 :
330 : /**
331 : * All of our denominations, in a DLL. Sorted?
332 : */
333 : static struct Denomination *denom_tail;
334 :
335 : /**
336 : * Map of hashes of public (RSA) keys to `struct DenominationKey *`
337 : * with the respective private keys.
338 : */
339 : static struct GNUNET_CONTAINER_MultiHashMap *keys;
340 :
341 : /**
342 : * Task run to generate new keys.
343 : */
344 : static struct GNUNET_SCHEDULER_Task *keygen_task;
345 :
346 : /**
347 : * Lock for the keys queue.
348 : */
349 : static pthread_mutex_t keys_lock = PTHREAD_MUTEX_INITIALIZER;
350 :
351 : /**
352 : * Current key generation.
353 : */
354 : static uint64_t key_gen;
355 :
356 :
357 : /**
358 : * Generate the announcement message for @a dk.
359 : *
360 : * @param[in,out] dk denomination key to generate the announcement for
361 : */
362 : static void
363 330 : generate_response (struct DenominationKey *dk)
364 : {
365 330 : struct Denomination *denom = dk->denom;
366 330 : size_t nlen = strlen (denom->section) + 1;
367 : struct TALER_CRYPTO_RsaKeyAvailableNotification *an;
368 : size_t buf_len;
369 : void *buf;
370 : void *p;
371 : size_t tlen;
372 : struct GNUNET_TIME_Relative effective_duration;
373 :
374 330 : buf_len = GNUNET_CRYPTO_rsa_public_key_encode (dk->denom_pub,
375 : &buf);
376 330 : GNUNET_assert (buf_len < UINT16_MAX);
377 330 : GNUNET_assert (nlen < UINT16_MAX);
378 330 : tlen = buf_len + nlen + sizeof (*an);
379 330 : GNUNET_assert (tlen < UINT16_MAX);
380 330 : an = GNUNET_malloc (tlen);
381 330 : an->header.size = htons ((uint16_t) tlen);
382 330 : an->header.type = htons (TALER_HELPER_RSA_MT_AVAIL);
383 330 : an->pub_size = htons ((uint16_t) buf_len);
384 330 : an->section_name_len = htons ((uint16_t) nlen);
385 330 : an->anchor_time = GNUNET_TIME_timestamp_hton (dk->anchor_start);
386 : /* Effective duration is based on denum->duration_withdraw + overlap,
387 : but we may have shifted the 'anchor_end' to align them, thus the
388 : only correct way to determine it is: */
389 330 : effective_duration = GNUNET_TIME_absolute_get_difference (
390 : dk->anchor_start.abs_time,
391 : dk->anchor_end.abs_time);
392 330 : an->duration_withdraw = GNUNET_TIME_relative_hton (effective_duration);
393 :
394 330 : TALER_exchange_secmod_rsa_sign (&dk->h_rsa,
395 330 : denom->section,
396 : dk->anchor_start,
397 : effective_duration,
398 : &TES_smpriv,
399 : &an->secm_sig);
400 330 : an->secm_pub = TES_smpub;
401 330 : p = (void *) &an[1];
402 330 : GNUNET_memcpy (p,
403 : buf,
404 : buf_len);
405 330 : GNUNET_free (buf);
406 330 : GNUNET_memcpy (p + buf_len,
407 : denom->section,
408 : nlen);
409 330 : dk->an = an;
410 330 : }
411 :
412 :
413 : /**
414 : * Do the actual signing work.
415 : *
416 : * @param h_rsa key to sign with
417 : * @param bm blinded message to sign
418 : * @param[out] rsa_signaturep set to the RSA signature
419 : * @return #TALER_EC_NONE on success
420 : */
421 : static enum TALER_ErrorCode
422 1514 : do_sign (const struct TALER_RsaPubHashP *h_rsa,
423 : const struct GNUNET_CRYPTO_RsaBlindedMessage *bm,
424 : struct GNUNET_CRYPTO_RsaSignature **rsa_signaturep)
425 : {
426 : struct DenominationKey *dk;
427 : struct GNUNET_CRYPTO_RsaSignature *rsa_signature;
428 1514 : struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
429 :
430 1513 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
431 1539 : dk = GNUNET_CONTAINER_multihashmap_get (keys,
432 : &h_rsa->hash);
433 1539 : if (NULL == dk)
434 : {
435 3 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
436 3 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
437 : "Signing request failed, denomination key %s unknown\n",
438 : GNUNET_h2s (&h_rsa->hash));
439 3 : return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_KEY_UNKNOWN;
440 : }
441 1536 : if (dk->purge)
442 : {
443 : /* key was revoked, it must not be used for signing anymore */
444 8 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
445 8 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
446 : "Signing request failed, denomination key %s was revoked\n",
447 : GNUNET_h2s (&h_rsa->hash));
448 8 : return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_REVOKED;
449 : }
450 1528 : if (GNUNET_TIME_absolute_is_future (dk->anchor_start.abs_time))
451 : {
452 : /* it is too early */
453 332 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
454 332 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
455 : "Signing request failed, denomination key %s is not yet valid (%llu)\n",
456 : GNUNET_h2s (&h_rsa->hash),
457 : (unsigned long long) dk->anchor_start.abs_time.abs_value_us);
458 331 : return TALER_EC_EXCHANGE_DENOMINATION_HELPER_TOO_EARLY;
459 : }
460 1196 : if (GNUNET_TIME_absolute_is_past (dk->anchor_end.abs_time))
461 : {
462 : /* it is too late; now, usually we should never get here
463 : as we delete upon expiration, so this is just conservative */
464 0 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
465 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
466 : "Signing request failed, denomination key %s is expired (%llu)\n",
467 : GNUNET_h2s (&h_rsa->hash),
468 : (unsigned long long) dk->anchor_end.abs_time.abs_value_us);
469 : /* usually we delete upon expiratoin, hence same EC */
470 0 : return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_KEY_UNKNOWN;
471 : }
472 :
473 1196 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
474 : "Received request to sign over %u bytes with key %s\n",
475 : (unsigned int) bm->blinded_msg_size,
476 : GNUNET_h2s (&h_rsa->hash));
477 1196 : GNUNET_assert (dk->rc < UINT_MAX);
478 1196 : dk->rc++;
479 1196 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
480 : rsa_signature
481 1196 : = GNUNET_CRYPTO_rsa_sign_blinded (dk->denom_priv,
482 : bm);
483 1196 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
484 1196 : GNUNET_assert (dk->rc > 0);
485 1196 : dk->rc--;
486 1196 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
487 1196 : if (NULL == rsa_signature)
488 : {
489 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
490 : "Signing request failed, worker failed to produce signature\n");
491 0 : return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE;
492 : }
493 :
494 1196 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
495 : "Sending RSA signature after %s\n",
496 : GNUNET_TIME_relative2s (
497 : GNUNET_TIME_absolute_get_duration (now),
498 : GNUNET_YES));
499 1196 : *rsa_signaturep = rsa_signature;
500 1196 : return TALER_EC_NONE;
501 : }
502 :
503 :
504 : /**
505 : * Generate error response that signing failed.
506 : *
507 : * @param client client to send response to
508 : * @param ec error code to include
509 : * @return #GNUNET_OK on success
510 : */
511 : static enum GNUNET_GenericReturnValue
512 343 : fail_sign (struct TES_Client *client,
513 : enum TALER_ErrorCode ec)
514 : {
515 343 : struct TALER_CRYPTO_SignFailure sf = {
516 343 : .header.size = htons (sizeof (sf)),
517 343 : .header.type = htons (TALER_HELPER_RSA_MT_RES_SIGN_FAILURE),
518 343 : .ec = htonl (ec)
519 : };
520 :
521 343 : return TES_transmit (client->csock,
522 : &sf.header);
523 : }
524 :
525 :
526 : /**
527 : * Generate signature response.
528 : *
529 : * @param client client to send response to
530 : * @param[in] rsa_signature signature to send, freed by this function
531 : * @return #GNUNET_OK on success
532 : */
533 : static enum GNUNET_GenericReturnValue
534 1196 : send_signature (struct TES_Client *client,
535 : struct GNUNET_CRYPTO_RsaSignature *rsa_signature)
536 : {
537 : struct TALER_CRYPTO_SignResponse *sr;
538 : void *buf;
539 : size_t buf_size;
540 : size_t tsize;
541 : enum GNUNET_GenericReturnValue ret;
542 :
543 1196 : buf_size = GNUNET_CRYPTO_rsa_signature_encode (rsa_signature,
544 : &buf);
545 1196 : GNUNET_CRYPTO_rsa_signature_free (rsa_signature);
546 1196 : tsize = sizeof (*sr) + buf_size;
547 1196 : GNUNET_assert (tsize < UINT16_MAX);
548 1196 : sr = GNUNET_malloc (tsize);
549 1196 : sr->header.size = htons (tsize);
550 1196 : sr->header.type = htons (TALER_HELPER_RSA_MT_RES_SIGNATURE);
551 1196 : GNUNET_memcpy (&sr[1],
552 : buf,
553 : buf_size);
554 1196 : GNUNET_free (buf);
555 1196 : ret = TES_transmit (client->csock,
556 1196 : &sr->header);
557 1196 : GNUNET_free (sr);
558 1196 : return ret;
559 : }
560 :
561 :
562 : /**
563 : * Handle @a client request @a sr to create signature. Create the
564 : * signature using the respective key and return the result to
565 : * the client.
566 : *
567 : * @param client the client making the request
568 : * @param sr the request details
569 : * @return #GNUNET_OK on success
570 : */
571 : static enum GNUNET_GenericReturnValue
572 0 : handle_sign_request (struct TES_Client *client,
573 : const struct TALER_CRYPTO_SignRequest *sr)
574 : {
575 0 : struct GNUNET_CRYPTO_RsaBlindedMessage bm = {
576 0 : .blinded_msg = (void *) &sr[1],
577 0 : .blinded_msg_size = ntohs (sr->header.size) - sizeof (*sr)
578 : };
579 : struct GNUNET_CRYPTO_RsaSignature *rsa_signature;
580 : enum TALER_ErrorCode ec;
581 :
582 0 : ec = do_sign (&sr->h_rsa,
583 : &bm,
584 : &rsa_signature);
585 0 : if (TALER_EC_NONE != ec)
586 : {
587 0 : return fail_sign (client,
588 : ec);
589 : }
590 0 : return send_signature (client,
591 : rsa_signature);
592 : }
593 :
594 :
595 : /**
596 : * Initialize a semaphore @a sem with a value of @a val.
597 : *
598 : * @param[out] sem semaphore to initialize
599 : * @param val initial value of the semaphore
600 : */
601 : static void
602 1887 : sem_init (struct Semaphore *sem,
603 : unsigned int val)
604 : {
605 1887 : GNUNET_assert (0 ==
606 : pthread_mutex_init (&sem->mutex,
607 : NULL));
608 1887 : GNUNET_assert (0 ==
609 : pthread_cond_init (&sem->cv,
610 : NULL));
611 1887 : sem->ctr = val;
612 1887 : }
613 :
614 :
615 : /**
616 : * Decrement semaphore, blocks until this is possible.
617 : *
618 : * @param[in,out] sem semaphore to decrement
619 : */
620 : static void
621 5254 : sem_down (struct Semaphore *sem)
622 : {
623 5254 : GNUNET_assert (0 == pthread_mutex_lock (&sem->mutex));
624 8189 : while (0 == sem->ctr)
625 : {
626 2949 : pthread_cond_wait (&sem->cv,
627 : &sem->mutex);
628 : }
629 5240 : sem->ctr--;
630 5240 : GNUNET_assert (0 == pthread_mutex_unlock (&sem->mutex));
631 5222 : }
632 :
633 :
634 : /**
635 : * Increment semaphore, blocks until this is possible.
636 : *
637 : * @param[in,out] sem semaphore to decrement
638 : */
639 : static void
640 5231 : sem_up (struct Semaphore *sem)
641 : {
642 5231 : GNUNET_assert (0 == pthread_mutex_lock (&sem->mutex));
643 5242 : sem->ctr++;
644 5242 : pthread_cond_signal (&sem->cv);
645 5226 : GNUNET_assert (0 == pthread_mutex_unlock (&sem->mutex));
646 5253 : }
647 :
648 :
649 : /**
650 : * Release resources used by @a sem.
651 : *
652 : * @param[in] sem semaphore to release (except the memory itself)
653 : */
654 : static void
655 1887 : sem_done (struct Semaphore *sem)
656 : {
657 1887 : GNUNET_break (0 == pthread_cond_destroy (&sem->cv));
658 1887 : GNUNET_break (0 == pthread_mutex_destroy (&sem->mutex));
659 1887 : }
660 :
661 :
662 : /**
663 : * Main logic of a worker thread. Grabs work, does it,
664 : * grabs more work.
665 : *
666 : * @param cls a `struct Worker *`
667 : * @returns cls
668 : */
669 : static void *
670 324 : worker (void *cls)
671 : {
672 324 : struct Worker *w = cls;
673 :
674 : while (true)
675 : {
676 1859 : GNUNET_assert (0 == pthread_mutex_lock (&worker_lock));
677 1863 : GNUNET_CONTAINER_DLL_insert (worker_head,
678 : worker_tail,
679 : w);
680 1863 : GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock));
681 1862 : sem_up (&worker_sem);
682 1863 : sem_down (&w->sem);
683 1843 : if (w->do_shutdown)
684 324 : break;
685 : {
686 1519 : struct BatchJob *bj = w->job;
687 1519 : const struct TALER_CRYPTO_SignRequest *sr = bj->sr;
688 1519 : struct GNUNET_CRYPTO_RsaBlindedMessage bm = {
689 1519 : .blinded_msg = (void *) &sr[1],
690 1519 : .blinded_msg_size = ntohs (sr->header.size) - sizeof (*sr)
691 : };
692 :
693 1519 : bj->ec = do_sign (&sr->h_rsa,
694 : &bm,
695 : &bj->rsa_signature);
696 1536 : sem_up (&bj->sem);
697 1535 : w->job = NULL;
698 : }
699 : }
700 324 : return w;
701 : }
702 :
703 :
704 : /**
705 : * Start batch job @a bj to sign @a sr.
706 : *
707 : * @param sr signature request to answer
708 : * @param[out] bj job data structure
709 : */
710 : static void
711 1539 : start_job (const struct TALER_CRYPTO_SignRequest *sr,
712 : struct BatchJob *bj)
713 : {
714 1539 : sem_init (&bj->sem,
715 : 0);
716 1539 : bj->sr = sr;
717 1539 : sem_down (&worker_sem);
718 1539 : GNUNET_assert (0 == pthread_mutex_lock (&worker_lock));
719 1539 : bj->worker = worker_head;
720 1539 : GNUNET_CONTAINER_DLL_remove (worker_head,
721 : worker_tail,
722 : bj->worker);
723 1539 : GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock));
724 1539 : bj->worker->job = bj;
725 1539 : sem_up (&bj->worker->sem);
726 1539 : }
727 :
728 :
729 : /**
730 : * Finish a job @a bj for a @a client.
731 : *
732 : * @param client who made the request
733 : * @param[in,out] bj job to finish
734 : * @return #GNUNET_OK on success, #GNUNET_SYSERR if we
735 : * failed to transmit the reply to @a client
736 : */
737 : static enum GNUNET_GenericReturnValue
738 1539 : finish_job (struct TES_Client *client,
739 : struct BatchJob *bj)
740 : {
741 : enum GNUNET_GenericReturnValue ret;
742 :
743 1539 : sem_down (&bj->sem);
744 1539 : sem_done (&bj->sem);
745 1539 : if (TALER_EC_NONE != bj->ec)
746 343 : return fail_sign (client,
747 : bj->ec);
748 1196 : GNUNET_assert (NULL != bj->rsa_signature);
749 1196 : ret = send_signature (client,
750 : bj->rsa_signature);
751 1196 : bj->rsa_signature = NULL; /* freed in send_signature */
752 1196 : return ret;
753 : }
754 :
755 :
756 : /**
757 : * Handle @a client request @a sr to create a batch of signature. Creates the
758 : * signatures using the respective key and return the results to the client.
759 : *
760 : * @param client the client making the request
761 : * @param bsr the request details
762 : * @return #GNUNET_OK on success
763 : */
764 : static enum GNUNET_GenericReturnValue
765 1016 : handle_batch_sign_request (struct TES_Client *client,
766 : const struct TALER_CRYPTO_BatchSignRequest *bsr)
767 : {
768 1016 : uint32_t bs = ntohl (bsr->batch_size);
769 1016 : uint16_t size = ntohs (bsr->header.size) - sizeof (*bsr);
770 1016 : const void *off = (const void *) &bsr[1];
771 1016 : unsigned int idx = 0;
772 1016 : bool failure = false;
773 1016 : enum GNUNET_GenericReturnValue tret = GNUNET_OK;
774 :
775 : /* an empty batch would be answered with no message at all,
776 : leaving the client waiting for a reply forever */
777 1016 : if ( (0 == bs) ||
778 : (bs > TALER_MAX_COINS) )
779 : {
780 0 : GNUNET_break_op (0);
781 0 : return GNUNET_SYSERR;
782 : }
783 1016 : {
784 1016 : struct BatchJob jobs[bs];
785 :
786 2555 : while ( (idx < bs) &&
787 : (size >= sizeof (struct TALER_CRYPTO_SignRequest)) )
788 : {
789 1539 : const struct TALER_CRYPTO_SignRequest *sr = off;
790 1539 : uint16_t s = ntohs (sr->header.size);
791 :
792 1539 : if ( (s > size) ||
793 : (s < sizeof (*sr)) )
794 : {
795 0 : failure = true;
796 0 : bs = idx;
797 0 : break;
798 : }
799 1539 : start_job (sr,
800 1539 : &jobs[idx++]);
801 1539 : off += s;
802 1539 : size -= s;
803 : }
804 1016 : GNUNET_break_op (0 == size);
805 1016 : if (idx < bs)
806 : {
807 : /* fewer requests than announced: the client waits for a reply
808 : to each of them, so it must learn that the batch failed */
809 0 : GNUNET_break_op (0);
810 0 : failure = true;
811 : }
812 1016 : bs = GNUNET_MIN (bs,
813 : idx);
814 : /* Note: we must finish (and thus reap) *every* job we started,
815 : even once transmission failed, or we would leak workers and
816 : the signatures they produced. */
817 2555 : for (unsigned int i = 0; i<bs; i++)
818 1539 : if (GNUNET_OK !=
819 1539 : finish_job (client,
820 : &jobs[i]))
821 0 : tret = GNUNET_SYSERR;
822 : }
823 1016 : if (GNUNET_OK != tret)
824 0 : return tret; /* client is gone, do not bother with more replies */
825 1016 : if (failure)
826 : {
827 0 : struct TALER_CRYPTO_SignFailure sf = {
828 0 : .header.size = htons (sizeof (sf)),
829 0 : .header.type = htons (TALER_HELPER_RSA_MT_RES_BATCH_FAILURE),
830 0 : .ec = htonl (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE)
831 : };
832 :
833 0 : GNUNET_break (0);
834 0 : return TES_transmit (client->csock,
835 : &sf.header);
836 : }
837 1016 : return GNUNET_OK;
838 : }
839 :
840 :
841 : /**
842 : * Start worker thread for batch processing.
843 : *
844 : * @return #GNUNET_OK on success
845 : */
846 : static enum GNUNET_GenericReturnValue
847 324 : start_worker (void)
848 : {
849 : struct Worker *w;
850 :
851 324 : w = GNUNET_new (struct Worker);
852 324 : sem_init (&w->sem,
853 : 0);
854 324 : if (0 != pthread_create (&w->pt,
855 : NULL,
856 : &worker,
857 : w))
858 : {
859 0 : GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
860 : "pthread_create");
861 0 : GNUNET_free (w);
862 0 : return GNUNET_SYSERR;
863 : }
864 324 : workers++;
865 324 : return GNUNET_OK;
866 : }
867 :
868 :
869 : /**
870 : * Stop all worker threads.
871 : */
872 : static void
873 24 : stop_workers (void)
874 : {
875 348 : while (workers > 0)
876 : {
877 : struct Worker *w;
878 : void *result;
879 :
880 324 : sem_down (&worker_sem);
881 324 : GNUNET_assert (0 == pthread_mutex_lock (&worker_lock));
882 324 : w = worker_head;
883 324 : GNUNET_CONTAINER_DLL_remove (worker_head,
884 : worker_tail,
885 : w);
886 324 : GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock));
887 324 : w->do_shutdown = true;
888 324 : sem_up (&w->sem);
889 324 : pthread_join (w->pt,
890 : &result);
891 324 : GNUNET_assert (result == w);
892 324 : sem_done (&w->sem);
893 324 : GNUNET_free (w);
894 324 : workers--;
895 : }
896 24 : }
897 :
898 :
899 : /**
900 : * Initialize key material for denomination key @a dk (also on disk).
901 : *
902 : * @param[in,out] dk denomination key to compute key material for
903 : * @param position where in the DLL will the @a dk go
904 : * @return #GNUNET_OK on success
905 : */
906 : static enum GNUNET_GenericReturnValue
907 321 : setup_key (struct DenominationKey *dk,
908 : struct DenominationKey *position)
909 : {
910 321 : struct Denomination *denom = dk->denom;
911 : struct GNUNET_CRYPTO_RsaPrivateKey *priv;
912 : struct GNUNET_CRYPTO_RsaPublicKey *pub;
913 : size_t buf_size;
914 : void *buf;
915 :
916 321 : priv = GNUNET_CRYPTO_rsa_private_key_create (denom->rsa_keysize);
917 321 : if (NULL == priv)
918 : {
919 0 : GNUNET_break (0);
920 0 : GNUNET_SCHEDULER_shutdown ();
921 0 : globals->global_ret = EXIT_FAILURE;
922 0 : return GNUNET_SYSERR;
923 : }
924 321 : pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
925 321 : if (NULL == pub)
926 : {
927 0 : GNUNET_break (0);
928 0 : GNUNET_CRYPTO_rsa_private_key_free (priv);
929 0 : return GNUNET_SYSERR;
930 : }
931 321 : buf_size = GNUNET_CRYPTO_rsa_private_key_encode (priv,
932 : &buf);
933 321 : GNUNET_CRYPTO_rsa_public_key_hash (pub,
934 : &dk->h_rsa.hash);
935 321 : GNUNET_asprintf (
936 : &dk->filename,
937 : "%s/%s/%llu-%llu",
938 : keydir,
939 : denom->section,
940 321 : (unsigned long long) (dk->anchor_start.abs_time.abs_value_us
941 321 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us),
942 321 : (unsigned long long) (dk->anchor_end.abs_time.abs_value_us
943 321 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us));
944 321 : if (GNUNET_OK !=
945 321 : GNUNET_DISK_fn_write (dk->filename,
946 : buf,
947 : buf_size,
948 : GNUNET_DISK_PERM_USER_READ))
949 : {
950 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
951 : "write",
952 : dk->filename);
953 0 : GNUNET_free (dk->filename);
954 0 : GNUNET_free (buf);
955 0 : GNUNET_CRYPTO_rsa_private_key_free (priv);
956 0 : GNUNET_CRYPTO_rsa_public_key_free (pub);
957 0 : return GNUNET_SYSERR;
958 : }
959 321 : GNUNET_free (buf);
960 321 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
961 : "Setup fresh private key %s at %s in `%s' (generation #%llu)\n",
962 : GNUNET_h2s (&dk->h_rsa.hash),
963 : GNUNET_TIME_timestamp2s (dk->anchor_start),
964 : dk->filename,
965 : (unsigned long long) key_gen);
966 321 : dk->denom_priv = priv;
967 321 : dk->denom_pub = pub;
968 321 : dk->key_gen = key_gen;
969 321 : generate_response (dk);
970 321 : if (GNUNET_OK !=
971 321 : GNUNET_CONTAINER_multihashmap_put (
972 : keys,
973 321 : &dk->h_rsa.hash,
974 : dk,
975 : GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
976 : {
977 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
978 : "Duplicate private key created! Terminating.\n");
979 0 : GNUNET_CRYPTO_rsa_private_key_free (dk->denom_priv);
980 0 : GNUNET_CRYPTO_rsa_public_key_free (dk->denom_pub);
981 0 : GNUNET_free (dk->filename);
982 0 : GNUNET_free (dk->an);
983 0 : GNUNET_free (dk);
984 0 : return GNUNET_SYSERR;
985 : }
986 321 : GNUNET_CONTAINER_DLL_insert_after (denom->keys_head,
987 : denom->keys_tail,
988 : position,
989 : dk);
990 321 : return GNUNET_OK;
991 : }
992 :
993 :
994 : /**
995 : * The withdraw period of a key @a dk has expired. Purge it.
996 : *
997 : * @param[in] dk expired denomination key to purge
998 : */
999 : static void
1000 12 : purge_key (struct DenominationKey *dk)
1001 : {
1002 12 : if (dk->purge)
1003 0 : return;
1004 12 : if (0 != unlink (dk->filename))
1005 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1006 : "unlink",
1007 : dk->filename);
1008 12 : GNUNET_free (dk->filename);
1009 12 : dk->purge = true;
1010 12 : dk->key_gen = key_gen;
1011 : }
1012 :
1013 :
1014 : /**
1015 : * A @a client informs us that a key has been revoked.
1016 : * Check if the key is still in use, and if so replace (!)
1017 : * it with a fresh key.
1018 : *
1019 : * @param client the client making the request
1020 : * @param rr the revocation request
1021 : */
1022 : static enum GNUNET_GenericReturnValue
1023 13 : handle_revoke_request (struct TES_Client *client,
1024 : const struct TALER_CRYPTO_RevokeRequest *rr)
1025 : {
1026 : struct DenominationKey *dk;
1027 : struct DenominationKey *ndk;
1028 : struct Denomination *denom;
1029 :
1030 : (void) client;
1031 13 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
1032 13 : dk = GNUNET_CONTAINER_multihashmap_get (keys,
1033 : &rr->h_rsa.hash);
1034 13 : if (NULL == dk)
1035 : {
1036 0 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1037 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1038 : "Revocation request ignored, denomination key %s unknown\n",
1039 : GNUNET_h2s (&rr->h_rsa.hash));
1040 0 : return GNUNET_OK;
1041 : }
1042 13 : if (dk->purge)
1043 : {
1044 1 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1045 1 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1046 : "Revocation request ignored, denomination key %s already revoked\n",
1047 : GNUNET_h2s (&rr->h_rsa.hash));
1048 1 : return GNUNET_OK;
1049 : }
1050 :
1051 12 : key_gen++;
1052 12 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1053 : "Revoking key %s, bumping generation to %llu\n",
1054 : GNUNET_h2s (&rr->h_rsa.hash),
1055 : (unsigned long long) key_gen);
1056 12 : purge_key (dk);
1057 :
1058 : /* Setup replacement key */
1059 12 : denom = dk->denom;
1060 12 : ndk = GNUNET_new (struct DenominationKey);
1061 12 : ndk->denom = denom;
1062 12 : ndk->anchor_start = dk->anchor_start;
1063 12 : ndk->anchor_end = dk->anchor_end;
1064 12 : if (GNUNET_OK !=
1065 12 : setup_key (ndk,
1066 : dk))
1067 : {
1068 0 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1069 0 : GNUNET_break (0);
1070 0 : GNUNET_SCHEDULER_shutdown ();
1071 0 : globals->global_ret = EXIT_FAILURE;
1072 0 : return GNUNET_SYSERR;
1073 : }
1074 12 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1075 12 : TES_wake_clients ();
1076 12 : return GNUNET_OK;
1077 : }
1078 :
1079 :
1080 : /**
1081 : * Handle @a hdr message received from @a client.
1082 : *
1083 : * @param client the client that received the message
1084 : * @param hdr message that was received
1085 : * @return #GNUNET_OK on success
1086 : */
1087 : static enum GNUNET_GenericReturnValue
1088 1029 : rsa_work_dispatch (struct TES_Client *client,
1089 : const struct GNUNET_MessageHeader *hdr)
1090 : {
1091 1029 : uint16_t msize = ntohs (hdr->size);
1092 :
1093 1029 : switch (ntohs (hdr->type))
1094 : {
1095 0 : case TALER_HELPER_RSA_MT_REQ_SIGN:
1096 0 : if (msize <= sizeof (struct TALER_CRYPTO_SignRequest))
1097 : {
1098 0 : GNUNET_break_op (0);
1099 0 : return GNUNET_SYSERR;
1100 : }
1101 0 : return handle_sign_request (
1102 : client,
1103 : (const struct TALER_CRYPTO_SignRequest *) hdr);
1104 13 : case TALER_HELPER_RSA_MT_REQ_REVOKE:
1105 13 : if (msize != sizeof (struct TALER_CRYPTO_RevokeRequest))
1106 : {
1107 0 : GNUNET_break_op (0);
1108 0 : return GNUNET_SYSERR;
1109 : }
1110 13 : return handle_revoke_request (
1111 : client,
1112 : (const struct TALER_CRYPTO_RevokeRequest *) hdr);
1113 1016 : case TALER_HELPER_RSA_MT_REQ_BATCH_SIGN:
1114 1016 : if (msize <= sizeof (struct TALER_CRYPTO_BatchSignRequest))
1115 : {
1116 0 : GNUNET_break_op (0);
1117 0 : return GNUNET_SYSERR;
1118 : }
1119 1016 : return handle_batch_sign_request (
1120 : client,
1121 : (const struct TALER_CRYPTO_BatchSignRequest *) hdr);
1122 0 : default:
1123 0 : GNUNET_break_op (0);
1124 0 : return GNUNET_SYSERR;
1125 : }
1126 : }
1127 :
1128 :
1129 : /**
1130 : * Send our initial key set to @a client together with the
1131 : * "sync" terminator.
1132 : *
1133 : * @param client the client to inform
1134 : * @return #GNUNET_OK on success
1135 : */
1136 : static enum GNUNET_GenericReturnValue
1137 32 : rsa_client_init (struct TES_Client *client)
1138 : {
1139 32 : size_t obs = 0;
1140 : char *buf;
1141 :
1142 32 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1143 : "Initializing new client %p\n",
1144 : client);
1145 32 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
1146 32 : for (struct Denomination *denom = denom_head;
1147 119 : NULL != denom;
1148 87 : denom = denom->next)
1149 : {
1150 87 : for (struct DenominationKey *dk = denom->keys_head;
1151 473 : NULL != dk;
1152 386 : dk = dk->next)
1153 : {
1154 386 : GNUNET_assert (obs + ntohs (dk->an->header.size)
1155 : > obs);
1156 386 : obs += ntohs (dk->an->header.size);
1157 : }
1158 : }
1159 32 : buf = GNUNET_malloc (obs);
1160 32 : obs = 0;
1161 32 : for (struct Denomination *denom = denom_head;
1162 119 : NULL != denom;
1163 87 : denom = denom->next)
1164 : {
1165 87 : for (struct DenominationKey *dk = denom->keys_head;
1166 473 : NULL != dk;
1167 386 : dk = dk->next)
1168 : {
1169 386 : GNUNET_memcpy (&buf[obs],
1170 : dk->an,
1171 : ntohs (dk->an->header.size));
1172 386 : GNUNET_assert (obs + ntohs (dk->an->header.size)
1173 : > obs);
1174 386 : obs += ntohs (dk->an->header.size);
1175 : }
1176 : }
1177 32 : client->key_gen = key_gen;
1178 32 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1179 32 : if (GNUNET_OK !=
1180 32 : TES_transmit_raw (client->csock,
1181 : obs,
1182 : buf))
1183 : {
1184 0 : GNUNET_free (buf);
1185 0 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1186 : "Client %p must have disconnected\n",
1187 : client);
1188 0 : return GNUNET_SYSERR;
1189 : }
1190 32 : GNUNET_free (buf);
1191 : {
1192 32 : struct GNUNET_MessageHeader synced = {
1193 32 : .type = htons (TALER_HELPER_RSA_SYNCED),
1194 32 : .size = htons (sizeof (synced))
1195 : };
1196 :
1197 32 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1198 : "Sending RSA SYNCED message to %p\n",
1199 : client);
1200 32 : if (GNUNET_OK !=
1201 32 : TES_transmit (client->csock,
1202 : &synced))
1203 : {
1204 0 : GNUNET_break (0);
1205 0 : return GNUNET_SYSERR;
1206 : }
1207 : }
1208 32 : return GNUNET_OK;
1209 : }
1210 :
1211 :
1212 : /**
1213 : * Notify @a client about all changes to the keys since
1214 : * the last generation known to the @a client.
1215 : *
1216 : * @param client the client to notify
1217 : * @return #GNUNET_OK on success
1218 : */
1219 : static enum GNUNET_GenericReturnValue
1220 35 : rsa_update_client_keys (struct TES_Client *client)
1221 : {
1222 35 : size_t obs = 0;
1223 : char *buf;
1224 : enum GNUNET_GenericReturnValue ret;
1225 :
1226 35 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
1227 35 : for (struct Denomination *denom = denom_head;
1228 206 : NULL != denom;
1229 171 : denom = denom->next)
1230 : {
1231 171 : for (struct DenominationKey *key = denom->keys_head;
1232 900 : NULL != key;
1233 729 : key = key->next)
1234 : {
1235 729 : if (key->key_gen <= client->key_gen)
1236 701 : continue;
1237 28 : if (key->purge)
1238 12 : obs += sizeof (struct TALER_CRYPTO_RsaKeyPurgeNotification);
1239 : else
1240 16 : obs += ntohs (key->an->header.size);
1241 : }
1242 : }
1243 35 : if (0 == obs)
1244 : {
1245 : /* nothing to do */
1246 19 : client->key_gen = key_gen;
1247 19 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1248 19 : return GNUNET_OK;
1249 : }
1250 16 : buf = GNUNET_malloc (obs);
1251 16 : obs = 0;
1252 16 : for (struct Denomination *denom = denom_head;
1253 113 : NULL != denom;
1254 97 : denom = denom->next)
1255 : {
1256 97 : for (struct DenominationKey *key = denom->keys_head;
1257 519 : NULL != key;
1258 422 : key = key->next)
1259 : {
1260 422 : if (key->key_gen <= client->key_gen)
1261 394 : continue;
1262 28 : if (key->purge)
1263 : {
1264 12 : struct TALER_CRYPTO_RsaKeyPurgeNotification pn = {
1265 12 : .header.type = htons (TALER_HELPER_RSA_MT_PURGE),
1266 12 : .header.size = htons (sizeof (pn)),
1267 : .h_rsa = key->h_rsa
1268 : };
1269 :
1270 12 : GNUNET_memcpy (&buf[obs],
1271 : &pn,
1272 : sizeof (pn));
1273 12 : GNUNET_assert (obs + sizeof (pn)
1274 : > obs);
1275 12 : obs += sizeof (pn);
1276 : }
1277 : else
1278 : {
1279 16 : GNUNET_memcpy (&buf[obs],
1280 : key->an,
1281 : ntohs (key->an->header.size));
1282 16 : GNUNET_assert (obs + ntohs (key->an->header.size)
1283 : > obs);
1284 16 : obs += ntohs (key->an->header.size);
1285 : }
1286 : }
1287 : }
1288 16 : client->key_gen = key_gen;
1289 16 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1290 16 : ret = TES_transmit_raw (client->csock,
1291 : obs,
1292 : buf);
1293 16 : GNUNET_free (buf);
1294 16 : return ret;
1295 : }
1296 :
1297 :
1298 : /**
1299 : * Create a new denomination key (we do not have enough).
1300 : *
1301 : * @param[in,out] denom denomination key to create
1302 : * @param anchor_start when to start key signing validity
1303 : * @param anchor_end when to end key signing validity
1304 : * @return #GNUNET_OK on success
1305 : */
1306 : static enum GNUNET_GenericReturnValue
1307 309 : create_key (struct Denomination *denom,
1308 : struct GNUNET_TIME_Timestamp anchor_start,
1309 : struct GNUNET_TIME_Timestamp anchor_end)
1310 : {
1311 : struct DenominationKey *dk;
1312 :
1313 309 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 : "Creating new key for `%s' with start date %s\n",
1315 : denom->section,
1316 : GNUNET_TIME_timestamp2s (anchor_start));
1317 309 : dk = GNUNET_new (struct DenominationKey);
1318 309 : dk->denom = denom;
1319 309 : dk->anchor_start = anchor_start;
1320 309 : dk->anchor_end = anchor_end;
1321 309 : if (GNUNET_OK !=
1322 309 : setup_key (dk,
1323 : denom->keys_tail))
1324 : {
1325 0 : GNUNET_break (0);
1326 0 : GNUNET_free (dk);
1327 0 : GNUNET_SCHEDULER_shutdown ();
1328 0 : globals->global_ret = EXIT_FAILURE;
1329 0 : return GNUNET_SYSERR;
1330 : }
1331 309 : return GNUNET_OK;
1332 : }
1333 :
1334 :
1335 : /**
1336 : * Obtain the maximum withdraw duration of all denominations.
1337 : *
1338 : * Must only be called while the #keys_lock is held.
1339 : *
1340 : * @return maximum withdraw duration, zero if there are no denominations
1341 : */
1342 : static struct GNUNET_TIME_Relative
1343 64 : get_maximum_duration (void)
1344 : {
1345 64 : struct GNUNET_TIME_Relative ret
1346 : = GNUNET_TIME_UNIT_ZERO;
1347 :
1348 64 : for (struct Denomination *denom = denom_head;
1349 309 : NULL != denom;
1350 245 : denom = denom->next)
1351 : {
1352 245 : ret = GNUNET_TIME_relative_max (ret,
1353 : denom->duration_withdraw);
1354 : }
1355 64 : return ret;
1356 : }
1357 :
1358 :
1359 : /**
1360 : * At what time do we need to next create keys if we just did?
1361 : *
1362 : * @return time when to next create keys if we just finished key generation
1363 : */
1364 : static struct GNUNET_TIME_Absolute
1365 20 : action_time (void)
1366 : {
1367 20 : struct GNUNET_TIME_Relative md = get_maximum_duration ();
1368 20 : struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1369 : uint64_t mod;
1370 :
1371 20 : if (GNUNET_TIME_relative_is_zero (md))
1372 0 : return GNUNET_TIME_UNIT_FOREVER_ABS;
1373 20 : mod = now.abs_value_us % md.rel_value_us;
1374 20 : now.abs_value_us -= mod;
1375 20 : return GNUNET_TIME_absolute_add (now,
1376 : md);
1377 : }
1378 :
1379 :
1380 : /**
1381 : * Remove all denomination keys of @a denom that have expired.
1382 : *
1383 : * @param[in,out] denom denomination family to remove keys for
1384 : */
1385 : static void
1386 324 : remove_expired_denomination_keys (struct Denomination *denom)
1387 : {
1388 576 : while ( (NULL != denom->keys_head) &&
1389 250 : GNUNET_TIME_absolute_is_past (
1390 250 : denom->keys_head->anchor_end.abs_time))
1391 : {
1392 2 : struct DenominationKey *key = denom->keys_head;
1393 2 : struct DenominationKey *nxt = key->next;
1394 :
1395 2 : if (0 != key->rc)
1396 0 : break; /* later */
1397 2 : GNUNET_CONTAINER_DLL_remove (denom->keys_head,
1398 : denom->keys_tail,
1399 : key);
1400 2 : GNUNET_assert (GNUNET_OK ==
1401 : GNUNET_CONTAINER_multihashmap_remove (
1402 : keys,
1403 : &key->h_rsa.hash,
1404 : key));
1405 4 : if ( (! key->purge) &&
1406 2 : (0 != unlink (key->filename)) )
1407 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR,
1408 : "unlink",
1409 : key->filename);
1410 2 : GNUNET_free (key->filename);
1411 2 : GNUNET_CRYPTO_rsa_private_key_free (key->denom_priv);
1412 2 : GNUNET_CRYPTO_rsa_public_key_free (key->denom_pub);
1413 2 : GNUNET_free (key->an);
1414 2 : GNUNET_free (key);
1415 2 : key = nxt;
1416 : }
1417 324 : }
1418 :
1419 :
1420 : /**
1421 : * Obtain the end anchor to use at this point. Uses the
1422 : * #lookahead_sign and then rounds it up by the maximum
1423 : * duration of any denomination to arrive at a globally
1424 : * valid end-date.
1425 : *
1426 : * Must only be called while the #keys_lock is held.
1427 : *
1428 : * @return end anchor
1429 : */
1430 : static struct GNUNET_TIME_Timestamp
1431 44 : get_anchor_end (void)
1432 : {
1433 44 : struct GNUNET_TIME_Relative md = get_maximum_duration ();
1434 : struct GNUNET_TIME_Absolute end
1435 44 : = GNUNET_TIME_relative_to_absolute (lookahead_sign);
1436 : uint64_t mod;
1437 :
1438 44 : if (GNUNET_TIME_relative_is_zero (md))
1439 8 : return GNUNET_TIME_UNIT_ZERO_TS;
1440 : /* Round up 'end' to a multiple of 'md' */
1441 36 : mod = end.abs_value_us % md.rel_value_us;
1442 36 : end.abs_value_us -= mod;
1443 36 : return GNUNET_TIME_absolute_to_timestamp (
1444 : GNUNET_TIME_absolute_add (end,
1445 : md));
1446 : }
1447 :
1448 :
1449 : /**
1450 : * Create all denomination keys that are required for our
1451 : * desired lookahead and that we do not yet have.
1452 : *
1453 : * @param[in,out] opt our options
1454 : * @param[in,out] wake set to true if we should wake the clients
1455 : */
1456 : static void
1457 44 : create_missing_keys (struct TALER_SECMOD_Options *opt,
1458 : bool *wake)
1459 : {
1460 : struct GNUNET_TIME_Timestamp start;
1461 : struct GNUNET_TIME_Timestamp end;
1462 :
1463 44 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 : "Updating denominations ...\n");
1465 44 : start = opt->global_now;
1466 44 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
1467 44 : end = get_anchor_end ();
1468 44 : for (struct Denomination *denom = denom_head;
1469 206 : NULL != denom;
1470 162 : denom = denom->next)
1471 : {
1472 : struct GNUNET_TIME_Timestamp anchor_start;
1473 : struct GNUNET_TIME_Timestamp anchor_end;
1474 : struct GNUNET_TIME_Timestamp next_end;
1475 162 : bool finished = false;
1476 :
1477 162 : remove_expired_denomination_keys (denom);
1478 162 : if (NULL != denom->keys_tail)
1479 : {
1480 86 : anchor_start = denom->keys_tail->anchor_end;
1481 86 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1482 : "Expanding keys of denomination `%s', last key %s valid for another %s\n",
1483 : denom->section,
1484 : GNUNET_h2s (&denom->keys_tail->h_rsa.hash),
1485 : GNUNET_TIME_relative2s (
1486 : GNUNET_TIME_absolute_get_remaining (
1487 : anchor_start.abs_time),
1488 : true));
1489 : }
1490 : else
1491 : {
1492 76 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1493 : "Starting keys of denomination `%s'\n",
1494 : denom->section);
1495 : /* Round the very first anchor down to the configured calendar
1496 : interval; subsequent anchors inherit the alignment from the
1497 : (rounded up) end of the preceding key. The UTC variants are used
1498 : so that the result does not depend on the time zone the secmod
1499 : happens to run in. */
1500 76 : anchor_start = GNUNET_TIME_absolute_to_timestamp (
1501 : GNUNET_TIME_round_down_utc (start.abs_time,
1502 : denom->anchor_round));
1503 : }
1504 162 : finished = GNUNET_TIME_timestamp_cmp (anchor_start,
1505 : >=,
1506 : end);
1507 471 : while (! finished)
1508 : {
1509 : /* Round the end of the validity period up to the configured calendar
1510 : interval. As #GNUNET_TIME_UNIT_YEARS is 365 days, this is also what
1511 : keeps the anchors from drifting off the calendar boundary across leap
1512 : years. Without ANCHOR_ROUND, all of this is a no-op. */
1513 309 : anchor_end = GNUNET_TIME_absolute_to_timestamp (
1514 : GNUNET_TIME_round_up_utc (
1515 : GNUNET_TIME_absolute_add (anchor_start.abs_time,
1516 : denom->duration_withdraw),
1517 : denom->anchor_round));
1518 309 : next_end = GNUNET_TIME_absolute_to_timestamp (
1519 : GNUNET_TIME_round_up_utc (
1520 : GNUNET_TIME_absolute_add (anchor_end.abs_time,
1521 : denom->duration_withdraw),
1522 : denom->anchor_round));
1523 309 : if (GNUNET_TIME_timestamp_cmp (next_end,
1524 : >,
1525 : end))
1526 : {
1527 : /* With ANCHOR_ROUND set the calendar interval already provides the
1528 : alignment, and stretching the last key would make it cover more
1529 : than the one interval it is supposed to cover. */
1530 80 : if (GNUNET_TIME_RI_NONE == denom->anchor_round)
1531 79 : anchor_end = end; /* extend period to align end periods */
1532 80 : finished = true;
1533 : }
1534 : /* adjust start time down to ensure overlap */
1535 309 : anchor_start = GNUNET_TIME_absolute_to_timestamp (
1536 : GNUNET_TIME_absolute_subtract (anchor_start.abs_time,
1537 : overlap_duration));
1538 309 : if (! *wake)
1539 : {
1540 4 : key_gen++;
1541 4 : *wake = true;
1542 : }
1543 309 : if (GNUNET_OK !=
1544 309 : create_key (denom,
1545 : anchor_start,
1546 : anchor_end))
1547 : {
1548 0 : GNUNET_break (0);
1549 0 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1550 0 : globals->global_ret = EXIT_FAILURE;
1551 0 : GNUNET_SCHEDULER_shutdown ();
1552 0 : return;
1553 : }
1554 309 : anchor_start = anchor_end;
1555 : }
1556 162 : remove_expired_denomination_keys (denom);
1557 : }
1558 44 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
1559 44 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 : "Updating denominations finished ...\n");
1561 : }
1562 :
1563 :
1564 : /**
1565 : * Task run periodically to expire keys and/or generate fresh ones.
1566 : *
1567 : * @param cls the `struct TALER_SECMOD_Options *`
1568 : */
1569 : static void
1570 20 : update_denominations (void *cls)
1571 : {
1572 20 : struct TALER_SECMOD_Options *opt = cls;
1573 : struct GNUNET_TIME_Absolute at;
1574 20 : bool wake = false;
1575 :
1576 : (void) cls;
1577 20 : keygen_task = NULL;
1578 : /* update current time, global override no longer applies */
1579 20 : opt->global_now = GNUNET_TIME_timestamp_get ();
1580 20 : create_missing_keys (opt,
1581 : &wake);
1582 20 : if (wake)
1583 4 : TES_wake_clients ();
1584 20 : at = action_time ();
1585 20 : GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 : "Next key generation due at %s\n",
1587 : GNUNET_TIME_absolute2s (at));
1588 20 : keygen_task = GNUNET_SCHEDULER_add_at (at,
1589 : &update_denominations,
1590 : opt);
1591 20 : }
1592 :
1593 :
1594 : /**
1595 : * Parse private key of denomination @a denom in @a buf.
1596 : *
1597 : * @param[out] denom denomination of the key
1598 : * @param filename name of the file we are parsing, for logging
1599 : * @param buf key material
1600 : * @param buf_size number of bytes in @a buf
1601 : */
1602 : static void
1603 9 : parse_key (struct Denomination *denom,
1604 : const char *filename,
1605 : const void *buf,
1606 : size_t buf_size)
1607 : {
1608 : struct GNUNET_CRYPTO_RsaPrivateKey *priv;
1609 : const char *anchor_s;
1610 : char dummy;
1611 : unsigned long long anchor_start_ll;
1612 : unsigned long long anchor_end_ll;
1613 : struct GNUNET_TIME_Timestamp anchor_start;
1614 : struct GNUNET_TIME_Timestamp anchor_end;
1615 9 : char *nf = NULL;
1616 :
1617 9 : anchor_s = strrchr (filename,
1618 : '/');
1619 9 : if (NULL == anchor_s)
1620 : {
1621 : /* File in a directory without '/' in the name, this makes no sense. */
1622 0 : GNUNET_break (0);
1623 0 : return;
1624 : }
1625 9 : anchor_s++;
1626 9 : if (2 != sscanf (anchor_s,
1627 : "%llu-%llu%c",
1628 : &anchor_start_ll,
1629 : &anchor_end_ll,
1630 : &dummy))
1631 : {
1632 : /* try legacy mode */
1633 3 : if (1 != sscanf (anchor_s,
1634 : "%llu%c",
1635 : &anchor_start_ll,
1636 : &dummy))
1637 : {
1638 : /* Filenames in KEYDIR must ONLY be the anchor time in seconds! */
1639 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1640 : "Filename `%s' invalid for key file, skipping\n",
1641 : anchor_s);
1642 0 : return;
1643 : }
1644 : anchor_start.abs_time.abs_value_us
1645 3 : = anchor_start_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1646 6 : if (anchor_start_ll != anchor_start.abs_time.abs_value_us
1647 3 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us)
1648 : {
1649 : /* Integer overflow. Bad, invalid filename. */
1650 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1651 : "Integer overflow. Filename `%s' invalid for key file, skipping\n",
1652 : anchor_s);
1653 0 : return;
1654 : }
1655 : anchor_end
1656 3 : = GNUNET_TIME_absolute_to_timestamp (
1657 : GNUNET_TIME_absolute_add (anchor_start.abs_time,
1658 : denom->duration_withdraw));
1659 3 : GNUNET_asprintf (
1660 : &nf,
1661 : "%s/%s/%llu-%llu",
1662 : keydir,
1663 : denom->section,
1664 : anchor_start_ll,
1665 3 : (unsigned long long) (anchor_end.abs_time.abs_value_us
1666 3 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us));
1667 : /* Try to fix the legacy filename */
1668 3 : if (0 !=
1669 3 : rename (filename,
1670 : nf))
1671 : {
1672 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1673 : "rename",
1674 : filename);
1675 0 : GNUNET_free (nf);
1676 : }
1677 : }
1678 : else
1679 : {
1680 : anchor_start.abs_time.abs_value_us
1681 6 : = anchor_start_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1682 : anchor_end.abs_time.abs_value_us
1683 6 : = anchor_end_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us;
1684 12 : if ( (anchor_start_ll != anchor_start.abs_time.abs_value_us
1685 6 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us) ||
1686 12 : (anchor_end_ll != anchor_end.abs_time.abs_value_us
1687 6 : / GNUNET_TIME_UNIT_SECONDS.rel_value_us) )
1688 : {
1689 : /* Integer overflow. Bad, invalid filename. */
1690 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1691 : "Integer overflow. Filename `%s' invalid for key file, skipping\n",
1692 : anchor_s);
1693 0 : return;
1694 : }
1695 : }
1696 9 : priv = GNUNET_CRYPTO_rsa_private_key_decode (buf,
1697 : buf_size);
1698 9 : if (NULL == priv)
1699 : {
1700 : /* Parser failure. */
1701 0 : GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1702 : "File `%s' is malformed, skipping\n",
1703 : (NULL == nf) ? filename : nf);
1704 0 : GNUNET_free (nf);
1705 0 : return;
1706 : }
1707 :
1708 : {
1709 : struct GNUNET_CRYPTO_RsaPublicKey *pub;
1710 : struct DenominationKey *dk;
1711 : struct DenominationKey *before;
1712 :
1713 9 : pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv);
1714 9 : if (NULL == pub)
1715 : {
1716 0 : GNUNET_break (0);
1717 0 : GNUNET_CRYPTO_rsa_private_key_free (priv);
1718 0 : GNUNET_free (nf);
1719 0 : return;
1720 : }
1721 : /* refuse keys whose modulus does not have the configured size */
1722 9 : if (denom->rsa_keysize !=
1723 9 : GNUNET_CRYPTO_rsa_public_key_len (pub))
1724 : {
1725 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1726 : "File `%s' holds a %u-bit RSA key, but RSA_KEYSIZE of section `%s' is %u; refusing to use it\n",
1727 : (NULL == nf) ? filename : nf,
1728 : GNUNET_CRYPTO_rsa_public_key_len (pub),
1729 : denom->section,
1730 : (unsigned int) denom->rsa_keysize);
1731 0 : GNUNET_CRYPTO_rsa_public_key_free (pub);
1732 0 : GNUNET_CRYPTO_rsa_private_key_free (priv);
1733 0 : GNUNET_free (nf);
1734 0 : return;
1735 : }
1736 9 : dk = GNUNET_new (struct DenominationKey);
1737 9 : dk->denom_priv = priv;
1738 9 : dk->denom = denom;
1739 9 : dk->anchor_start = anchor_start;
1740 9 : dk->anchor_end = anchor_end;
1741 9 : dk->filename = (NULL == nf) ? GNUNET_strdup (filename) : nf;
1742 9 : GNUNET_CRYPTO_rsa_public_key_hash (pub,
1743 : &dk->h_rsa.hash);
1744 9 : dk->denom_pub = pub;
1745 9 : generate_response (dk);
1746 9 : if (GNUNET_OK !=
1747 9 : GNUNET_CONTAINER_multihashmap_put (
1748 : keys,
1749 9 : &dk->h_rsa.hash,
1750 : dk,
1751 : GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
1752 : {
1753 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1754 : "Duplicate private key %s detected in file `%s'. Skipping.\n",
1755 : GNUNET_h2s (&dk->h_rsa.hash),
1756 : filename);
1757 0 : GNUNET_CRYPTO_rsa_private_key_free (priv);
1758 0 : GNUNET_CRYPTO_rsa_public_key_free (pub);
1759 0 : GNUNET_free (dk->an);
1760 0 : GNUNET_free (dk);
1761 0 : return;
1762 : }
1763 9 : before = NULL;
1764 9 : for (struct DenominationKey *pos = denom->keys_head;
1765 10 : NULL != pos;
1766 1 : pos = pos->next)
1767 : {
1768 6 : if (GNUNET_TIME_timestamp_cmp (pos->anchor_start,
1769 : >,
1770 : anchor_start))
1771 5 : break;
1772 1 : before = pos;
1773 : }
1774 9 : GNUNET_CONTAINER_DLL_insert_after (denom->keys_head,
1775 : denom->keys_tail,
1776 : before,
1777 : dk);
1778 9 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1779 : "Imported key %s from `%s'\n",
1780 : GNUNET_h2s (&dk->h_rsa.hash),
1781 : filename);
1782 : }
1783 : }
1784 :
1785 :
1786 : /**
1787 : * Import a private key from @a filename for the denomination
1788 : * given in @a cls.
1789 : *
1790 : * @param[in,out] cls a `struct Denomiantion`
1791 : * @param filename name of a file in the directory
1792 : * @return #GNUNET_OK (always, continue to iterate)
1793 : */
1794 : static enum GNUNET_GenericReturnValue
1795 9 : import_key (void *cls,
1796 : const char *filename)
1797 : {
1798 9 : struct Denomination *denom = cls;
1799 : struct GNUNET_DISK_FileHandle *fh;
1800 : struct GNUNET_DISK_MapHandle *map;
1801 : void *ptr;
1802 : int fd;
1803 : struct stat sbuf;
1804 :
1805 9 : fd = TES_open_key_file (filename,
1806 : &sbuf);
1807 9 : if (-1 == fd)
1808 0 : return GNUNET_OK;
1809 9 : fh = GNUNET_DISK_get_handle_from_int_fd (fd);
1810 9 : if (NULL == fh)
1811 : {
1812 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1813 : "open",
1814 : filename);
1815 0 : GNUNET_break (0 == close (fd));
1816 0 : return GNUNET_OK;
1817 : }
1818 9 : if (sbuf.st_size > 16 * 1024)
1819 : {
1820 0 : GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1821 : "File `%s' too big to be a private key\n",
1822 : filename);
1823 0 : GNUNET_DISK_file_close (fh);
1824 0 : return GNUNET_OK;
1825 : }
1826 9 : ptr = GNUNET_DISK_file_map (fh,
1827 : &map,
1828 : GNUNET_DISK_MAP_TYPE_READ,
1829 9 : (size_t) sbuf.st_size);
1830 9 : if (NULL == ptr)
1831 : {
1832 0 : GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1833 : "mmap",
1834 : filename);
1835 0 : GNUNET_DISK_file_close (fh);
1836 0 : return GNUNET_OK;
1837 : }
1838 9 : parse_key (denom,
1839 : filename,
1840 : ptr,
1841 9 : (size_t) sbuf.st_size);
1842 9 : GNUNET_DISK_file_unmap (map);
1843 9 : GNUNET_DISK_file_close (fh);
1844 9 : return GNUNET_OK;
1845 : }
1846 :
1847 :
1848 : /**
1849 : * Parse configuration for denomination type parameters. Also determines
1850 : * our anchor by looking at the existing denominations of the same type.
1851 : *
1852 : * @param cfg configuration to use
1853 : * @param ct section in the configuration file giving the denomination type parameters
1854 : * @param[out] denom set to the denomination parameters from the configuration
1855 : * @return #GNUNET_OK on success, #GNUNET_SYSERR if the configuration is invalid
1856 : */
1857 : static enum GNUNET_GenericReturnValue
1858 79 : parse_denomination_cfg (const struct GNUNET_CONFIGURATION_Handle *cfg,
1859 : const char *ct,
1860 : struct Denomination *denom)
1861 : {
1862 : unsigned long long rsa_keysize;
1863 : char *secname;
1864 :
1865 79 : GNUNET_asprintf (&secname,
1866 : "%s-secmod-rsa",
1867 79 : globals->section);
1868 79 : if (GNUNET_OK !=
1869 79 : GNUNET_CONFIGURATION_get_value_time (cfg,
1870 : ct,
1871 : "DURATION_WITHDRAW",
1872 : &denom->duration_withdraw))
1873 : {
1874 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1875 : ct,
1876 : "DURATION_WITHDRAW");
1877 0 : GNUNET_free (secname);
1878 0 : return GNUNET_SYSERR;
1879 : }
1880 79 : if (GNUNET_TIME_relative_cmp (denom->duration_withdraw,
1881 : <,
1882 : GNUNET_TIME_UNIT_SECONDS))
1883 : {
1884 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1885 : ct,
1886 : "DURATION_WITHDRAW",
1887 : "less than one second is not supported");
1888 0 : GNUNET_free (secname);
1889 0 : return GNUNET_SYSERR;
1890 : }
1891 79 : if (GNUNET_TIME_relative_cmp (overlap_duration,
1892 : >=,
1893 : denom->duration_withdraw))
1894 : {
1895 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1896 : secname,
1897 : "OVERLAP_DURATION",
1898 : "Value given must be smaller than value for DURATION_WITHDRAW!");
1899 0 : GNUNET_free (secname);
1900 0 : return GNUNET_SYSERR;
1901 : }
1902 : {
1903 : struct GNUNET_TIME_Relative ar;
1904 :
1905 : /* The denomination section takes precedence, the secmod section
1906 : provides the default for all denominations. */
1907 79 : if ( (GNUNET_OK !=
1908 79 : GNUNET_CONFIGURATION_get_value_time (cfg,
1909 : ct,
1910 : "ANCHOR_ROUND",
1911 75 : &ar)) &&
1912 : (GNUNET_OK !=
1913 75 : GNUNET_CONFIGURATION_get_value_time (cfg,
1914 : secname,
1915 : "ANCHOR_ROUND",
1916 : &ar)) )
1917 0 : ar = GNUNET_TIME_UNIT_ZERO; /* not configured: do not round */
1918 : denom->anchor_round
1919 79 : = GNUNET_TIME_relative_to_round_interval (ar);
1920 79 : if ( (GNUNET_TIME_RI_NONE == denom->anchor_round) &&
1921 75 : (! GNUNET_TIME_relative_is_zero (ar)) )
1922 : {
1923 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1924 : ct,
1925 : "ANCHOR_ROUND",
1926 : "Value given must be zero or exactly one second, minute, hour, day, week, month, quarter or year");
1927 0 : GNUNET_free (secname);
1928 0 : return GNUNET_SYSERR;
1929 : }
1930 : }
1931 79 : if (GNUNET_OK !=
1932 79 : GNUNET_CONFIGURATION_get_value_number (cfg,
1933 : ct,
1934 : "RSA_KEYSIZE",
1935 : &rsa_keysize))
1936 : {
1937 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
1938 : ct,
1939 : "RSA_KEYSIZE");
1940 0 : GNUNET_free (secname);
1941 0 : return GNUNET_SYSERR;
1942 : }
1943 79 : if ( (rsa_keysize > 4 * 2048) ||
1944 79 : (rsa_keysize < 1024) )
1945 : {
1946 0 : GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR,
1947 : ct,
1948 : "RSA_KEYSIZE",
1949 : "Given RSA keysize outside of permitted range [1024,8192]\n");
1950 0 : GNUNET_free (secname);
1951 0 : return GNUNET_SYSERR;
1952 : }
1953 79 : GNUNET_free (secname);
1954 79 : denom->rsa_keysize = (unsigned int) rsa_keysize;
1955 79 : denom->section = TES_normalize_section (ct);
1956 79 : return GNUNET_OK;
1957 : }
1958 :
1959 :
1960 : /**
1961 : * Closure for #load_denominations.
1962 : */
1963 : struct LoadContext
1964 : {
1965 :
1966 : /**
1967 : * Configuration to use.
1968 : */
1969 : const struct GNUNET_CONFIGURATION_Handle *cfg;
1970 :
1971 : /**
1972 : * Configuration section prefix to use for denomination settings.
1973 : * "coin_" for the exchange, "doco_" for Donau.
1974 : */
1975 : const char *cprefix;
1976 :
1977 : /**
1978 : * Status, to be set to #GNUNET_SYSERR on failure
1979 : */
1980 : enum GNUNET_GenericReturnValue ret;
1981 : };
1982 :
1983 :
1984 : /**
1985 : * Generate new denomination signing keys for the denomination type of the given @a
1986 : * denomination_alias.
1987 : *
1988 : * @param cls a `struct LoadContext`, with 'ret' to be set to #GNUNET_SYSERR on failure
1989 : * @param denomination_alias name of the denomination's section in the configuration
1990 : */
1991 : static void
1992 1027 : load_denominations (void *cls,
1993 : const char *denomination_alias)
1994 : {
1995 1027 : struct LoadContext *ctx = cls;
1996 : struct Denomination *denom;
1997 : char *cipher;
1998 :
1999 1027 : if (0 != strncasecmp (denomination_alias,
2000 : ctx->cprefix,
2001 : strlen (ctx->cprefix)))
2002 948 : return; /* not a denomination type definition */
2003 141 : if (GNUNET_OK !=
2004 141 : GNUNET_CONFIGURATION_get_value_string (ctx->cfg,
2005 : denomination_alias,
2006 : "CIPHER",
2007 : &cipher))
2008 : {
2009 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2010 : denomination_alias,
2011 : "CIPHER");
2012 0 : return;
2013 : }
2014 141 : if (0 != strcmp (cipher,
2015 : "RSA"))
2016 : {
2017 62 : GNUNET_free (cipher);
2018 62 : return; /* Ignore denominations of other types than CS */
2019 : }
2020 79 : GNUNET_free (cipher);
2021 79 : denom = GNUNET_new (struct Denomination);
2022 79 : if (GNUNET_OK !=
2023 79 : parse_denomination_cfg (ctx->cfg,
2024 : denomination_alias,
2025 : denom))
2026 : {
2027 0 : ctx->ret = GNUNET_SYSERR;
2028 0 : GNUNET_free (denom);
2029 0 : return;
2030 : }
2031 79 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2032 : "Loading keys for denomination %s\n",
2033 : denom->section);
2034 : {
2035 : char *dname;
2036 :
2037 79 : GNUNET_asprintf (&dname,
2038 : "%s/%s",
2039 : keydir,
2040 : denom->section);
2041 79 : GNUNET_break (GNUNET_OK ==
2042 : GNUNET_DISK_directory_create (dname));
2043 79 : GNUNET_DISK_directory_scan (dname,
2044 : &import_key,
2045 : denom);
2046 79 : GNUNET_free (dname);
2047 : }
2048 79 : GNUNET_CONTAINER_DLL_insert (denom_head,
2049 : denom_tail,
2050 : denom);
2051 : }
2052 :
2053 :
2054 : /**
2055 : * Load the various duration values from @a cfg
2056 : *
2057 : * @param cfg configuration to use
2058 : * @return #GNUNET_OK on success
2059 : */
2060 : static enum GNUNET_GenericReturnValue
2061 37 : load_durations (const struct GNUNET_CONFIGURATION_Handle *cfg)
2062 : {
2063 : char *secname;
2064 :
2065 37 : GNUNET_asprintf (&secname,
2066 : "%s-secmod-rsa",
2067 37 : globals->section);
2068 37 : if (GNUNET_OK !=
2069 37 : GNUNET_CONFIGURATION_get_value_time (cfg,
2070 : secname,
2071 : "OVERLAP_DURATION",
2072 : &overlap_duration))
2073 : {
2074 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2075 : secname,
2076 : "OVERLAP_DURATION");
2077 0 : GNUNET_free (secname);
2078 0 : return GNUNET_SYSERR;
2079 : }
2080 37 : if (GNUNET_OK !=
2081 37 : GNUNET_CONFIGURATION_get_value_time (cfg,
2082 : secname,
2083 : "LOOKAHEAD_SIGN",
2084 : &lookahead_sign))
2085 : {
2086 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2087 : secname,
2088 : "LOOKAHEAD_SIGN");
2089 0 : GNUNET_free (secname);
2090 0 : return GNUNET_SYSERR;
2091 : }
2092 37 : GNUNET_free (secname);
2093 37 : return GNUNET_OK;
2094 : }
2095 :
2096 :
2097 : /**
2098 : * Function run on shutdown. Stops the various jobs (nicely).
2099 : *
2100 : * @param cls NULL
2101 : */
2102 : static void
2103 24 : do_shutdown (void *cls)
2104 : {
2105 : (void) cls;
2106 24 : TES_listen_stop ();
2107 24 : if (NULL != keygen_task)
2108 : {
2109 16 : GNUNET_SCHEDULER_cancel (keygen_task);
2110 16 : keygen_task = NULL;
2111 : }
2112 24 : stop_workers ();
2113 24 : sem_done (&worker_sem);
2114 24 : }
2115 :
2116 :
2117 : void
2118 37 : TALER_SECMOD_rsa_run (void *cls,
2119 : char *const *args,
2120 : const char *cfgfile,
2121 : const struct GNUNET_CONFIGURATION_Handle *cfg)
2122 : {
2123 : static struct TES_Callbacks cb = {
2124 : .dispatch = rsa_work_dispatch,
2125 : .updater = rsa_update_client_keys,
2126 : .init = rsa_client_init
2127 : };
2128 37 : struct TALER_SECMOD_Options *opt = cls;
2129 : char *secname;
2130 :
2131 : (void) args;
2132 : (void) cfgfile;
2133 37 : globals = opt;
2134 37 : if (GNUNET_TIME_timestamp_cmp (opt->global_now,
2135 : !=,
2136 : opt->global_now_tmp))
2137 : {
2138 : /* The user gave "--now", use it! */
2139 0 : opt->global_now = opt->global_now_tmp;
2140 : }
2141 : else
2142 : {
2143 : /* get current time again, we may be timetraveling! */
2144 37 : opt->global_now = GNUNET_TIME_timestamp_get ();
2145 : }
2146 37 : GNUNET_asprintf (&secname,
2147 : "%s-secmod-rsa",
2148 : opt->section);
2149 37 : if (GNUNET_OK !=
2150 37 : GNUNET_CONFIGURATION_get_value_filename (cfg,
2151 : secname,
2152 : "KEY_DIR",
2153 : &keydir))
2154 : {
2155 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
2156 : secname,
2157 : "KEY_DIR");
2158 0 : GNUNET_free (secname);
2159 0 : opt->global_ret = EXIT_NOTCONFIGURED;
2160 21 : return;
2161 : }
2162 37 : if (GNUNET_OK !=
2163 37 : load_durations (cfg))
2164 : {
2165 0 : opt->global_ret = EXIT_NOTCONFIGURED;
2166 0 : GNUNET_free (secname);
2167 0 : return;
2168 : }
2169 37 : if (GNUNET_OK !=
2170 37 : TES_normalize_key_directory (cfg,
2171 : keydir,
2172 : opt->cprefix))
2173 : {
2174 13 : opt->global_ret = EXIT_FAILURE;
2175 13 : GNUNET_free (secname);
2176 13 : return;
2177 : }
2178 24 : opt->global_ret = TES_listen_start (cfg,
2179 : secname,
2180 : &cb);
2181 24 : GNUNET_free (secname);
2182 24 : if (0 != opt->global_ret)
2183 0 : return;
2184 24 : sem_init (&worker_sem,
2185 : 0);
2186 24 : GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
2187 : NULL);
2188 24 : if (0 == opt->max_workers)
2189 : {
2190 : long lret;
2191 :
2192 0 : lret = sysconf (_SC_NPROCESSORS_CONF);
2193 0 : if (lret <= 0)
2194 0 : lret = 1;
2195 0 : opt->max_workers = (unsigned int) lret;
2196 : }
2197 :
2198 348 : for (unsigned int i = 0; i<opt->max_workers; i++)
2199 324 : if (GNUNET_OK !=
2200 324 : start_worker ())
2201 : {
2202 0 : GNUNET_SCHEDULER_shutdown ();
2203 0 : return;
2204 : }
2205 : /* Load denominations */
2206 24 : keys = GNUNET_CONTAINER_multihashmap_create (65536,
2207 : true);
2208 : {
2209 24 : struct LoadContext lc = {
2210 : .cfg = cfg,
2211 : .ret = GNUNET_OK,
2212 24 : .cprefix = opt->cprefix
2213 : };
2214 24 : bool wake = true;
2215 :
2216 24 : GNUNET_assert (0 == pthread_mutex_lock (&keys_lock));
2217 24 : GNUNET_CONFIGURATION_iterate_sections (cfg,
2218 : &load_denominations,
2219 : &lc);
2220 24 : GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock));
2221 24 : if (GNUNET_OK != lc.ret)
2222 : {
2223 0 : opt->global_ret = EXIT_FAILURE;
2224 0 : GNUNET_SCHEDULER_shutdown ();
2225 0 : return;
2226 : }
2227 24 : create_missing_keys (opt,
2228 : &wake);
2229 : }
2230 24 : if (NULL == denom_head)
2231 : {
2232 8 : GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2233 : "No RSA denominations configured. Make sure section names start with `%s' if you are using RSA!\n",
2234 : opt->cprefix);
2235 8 : TES_wake_clients ();
2236 8 : return;
2237 : }
2238 : /* start job to keep keys up-to-date; MUST be run before the #listen_task,
2239 : hence with priority. */
2240 16 : keygen_task = GNUNET_SCHEDULER_add_with_priority (
2241 : GNUNET_SCHEDULER_PRIORITY_URGENT,
2242 : &update_denominations,
2243 : opt);
2244 : }
|