secmod_rsa.c (64114B)
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 generate_response (struct DenominationKey *dk) 364 { 365 struct Denomination *denom = dk->denom; 366 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 buf_len = GNUNET_CRYPTO_rsa_public_key_encode (dk->denom_pub, 375 &buf); 376 GNUNET_assert (buf_len < UINT16_MAX); 377 GNUNET_assert (nlen < UINT16_MAX); 378 tlen = buf_len + nlen + sizeof (*an); 379 GNUNET_assert (tlen < UINT16_MAX); 380 an = GNUNET_malloc (tlen); 381 an->header.size = htons ((uint16_t) tlen); 382 an->header.type = htons (TALER_HELPER_RSA_MT_AVAIL); 383 an->pub_size = htons ((uint16_t) buf_len); 384 an->section_name_len = htons ((uint16_t) nlen); 385 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 effective_duration = GNUNET_TIME_absolute_get_difference ( 390 dk->anchor_start.abs_time, 391 dk->anchor_end.abs_time); 392 an->duration_withdraw = GNUNET_TIME_relative_hton (effective_duration); 393 394 TALER_exchange_secmod_rsa_sign (&dk->h_rsa, 395 denom->section, 396 dk->anchor_start, 397 effective_duration, 398 &TES_smpriv, 399 &an->secm_sig); 400 an->secm_pub = TES_smpub; 401 p = (void *) &an[1]; 402 GNUNET_memcpy (p, 403 buf, 404 buf_len); 405 GNUNET_free (buf); 406 GNUNET_memcpy (p + buf_len, 407 denom->section, 408 nlen); 409 dk->an = an; 410 } 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 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 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 429 430 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 431 dk = GNUNET_CONTAINER_multihashmap_get (keys, 432 &h_rsa->hash); 433 if (NULL == dk) 434 { 435 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 436 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 437 "Signing request failed, denomination key %s unknown\n", 438 GNUNET_h2s (&h_rsa->hash)); 439 return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_KEY_UNKNOWN; 440 } 441 if (dk->purge) 442 { 443 /* key was revoked, it must not be used for signing anymore */ 444 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 445 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 446 "Signing request failed, denomination key %s was revoked\n", 447 GNUNET_h2s (&h_rsa->hash)); 448 return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_REVOKED; 449 } 450 if (GNUNET_TIME_absolute_is_future (dk->anchor_start.abs_time)) 451 { 452 /* it is too early */ 453 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 454 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 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_TOO_EARLY; 459 } 460 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 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 465 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 return TALER_EC_EXCHANGE_GENERIC_DENOMINATION_KEY_UNKNOWN; 471 } 472 473 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 GNUNET_assert (dk->rc < UINT_MAX); 478 dk->rc++; 479 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 480 rsa_signature 481 = GNUNET_CRYPTO_rsa_sign_blinded (dk->denom_priv, 482 bm); 483 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 484 GNUNET_assert (dk->rc > 0); 485 dk->rc--; 486 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 487 if (NULL == rsa_signature) 488 { 489 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 490 "Signing request failed, worker failed to produce signature\n"); 491 return TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE; 492 } 493 494 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 *rsa_signaturep = rsa_signature; 500 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 fail_sign (struct TES_Client *client, 513 enum TALER_ErrorCode ec) 514 { 515 struct TALER_CRYPTO_SignFailure sf = { 516 .header.size = htons (sizeof (sf)), 517 .header.type = htons (TALER_HELPER_RSA_MT_RES_SIGN_FAILURE), 518 .ec = htonl (ec) 519 }; 520 521 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 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 buf_size = GNUNET_CRYPTO_rsa_signature_encode (rsa_signature, 544 &buf); 545 GNUNET_CRYPTO_rsa_signature_free (rsa_signature); 546 tsize = sizeof (*sr) + buf_size; 547 GNUNET_assert (tsize < UINT16_MAX); 548 sr = GNUNET_malloc (tsize); 549 sr->header.size = htons (tsize); 550 sr->header.type = htons (TALER_HELPER_RSA_MT_RES_SIGNATURE); 551 GNUNET_memcpy (&sr[1], 552 buf, 553 buf_size); 554 GNUNET_free (buf); 555 ret = TES_transmit (client->csock, 556 &sr->header); 557 GNUNET_free (sr); 558 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 handle_sign_request (struct TES_Client *client, 573 const struct TALER_CRYPTO_SignRequest *sr) 574 { 575 struct GNUNET_CRYPTO_RsaBlindedMessage bm = { 576 .blinded_msg = (void *) &sr[1], 577 .blinded_msg_size = ntohs (sr->header.size) - sizeof (*sr) 578 }; 579 struct GNUNET_CRYPTO_RsaSignature *rsa_signature; 580 enum TALER_ErrorCode ec; 581 582 ec = do_sign (&sr->h_rsa, 583 &bm, 584 &rsa_signature); 585 if (TALER_EC_NONE != ec) 586 { 587 return fail_sign (client, 588 ec); 589 } 590 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 sem_init (struct Semaphore *sem, 603 unsigned int val) 604 { 605 GNUNET_assert (0 == 606 pthread_mutex_init (&sem->mutex, 607 NULL)); 608 GNUNET_assert (0 == 609 pthread_cond_init (&sem->cv, 610 NULL)); 611 sem->ctr = val; 612 } 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 sem_down (struct Semaphore *sem) 622 { 623 GNUNET_assert (0 == pthread_mutex_lock (&sem->mutex)); 624 while (0 == sem->ctr) 625 { 626 pthread_cond_wait (&sem->cv, 627 &sem->mutex); 628 } 629 sem->ctr--; 630 GNUNET_assert (0 == pthread_mutex_unlock (&sem->mutex)); 631 } 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 sem_up (struct Semaphore *sem) 641 { 642 GNUNET_assert (0 == pthread_mutex_lock (&sem->mutex)); 643 sem->ctr++; 644 pthread_cond_signal (&sem->cv); 645 GNUNET_assert (0 == pthread_mutex_unlock (&sem->mutex)); 646 } 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 sem_done (struct Semaphore *sem) 656 { 657 GNUNET_break (0 == pthread_cond_destroy (&sem->cv)); 658 GNUNET_break (0 == pthread_mutex_destroy (&sem->mutex)); 659 } 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 worker (void *cls) 671 { 672 struct Worker *w = cls; 673 674 while (true) 675 { 676 GNUNET_assert (0 == pthread_mutex_lock (&worker_lock)); 677 GNUNET_CONTAINER_DLL_insert (worker_head, 678 worker_tail, 679 w); 680 GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock)); 681 sem_up (&worker_sem); 682 sem_down (&w->sem); 683 if (w->do_shutdown) 684 break; 685 { 686 struct BatchJob *bj = w->job; 687 const struct TALER_CRYPTO_SignRequest *sr = bj->sr; 688 struct GNUNET_CRYPTO_RsaBlindedMessage bm = { 689 .blinded_msg = (void *) &sr[1], 690 .blinded_msg_size = ntohs (sr->header.size) - sizeof (*sr) 691 }; 692 693 bj->ec = do_sign (&sr->h_rsa, 694 &bm, 695 &bj->rsa_signature); 696 sem_up (&bj->sem); 697 w->job = NULL; 698 } 699 } 700 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 start_job (const struct TALER_CRYPTO_SignRequest *sr, 712 struct BatchJob *bj) 713 { 714 sem_init (&bj->sem, 715 0); 716 bj->sr = sr; 717 sem_down (&worker_sem); 718 GNUNET_assert (0 == pthread_mutex_lock (&worker_lock)); 719 bj->worker = worker_head; 720 GNUNET_CONTAINER_DLL_remove (worker_head, 721 worker_tail, 722 bj->worker); 723 GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock)); 724 bj->worker->job = bj; 725 sem_up (&bj->worker->sem); 726 } 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 finish_job (struct TES_Client *client, 739 struct BatchJob *bj) 740 { 741 enum GNUNET_GenericReturnValue ret; 742 743 sem_down (&bj->sem); 744 sem_done (&bj->sem); 745 if (TALER_EC_NONE != bj->ec) 746 return fail_sign (client, 747 bj->ec); 748 GNUNET_assert (NULL != bj->rsa_signature); 749 ret = send_signature (client, 750 bj->rsa_signature); 751 bj->rsa_signature = NULL; /* freed in send_signature */ 752 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 handle_batch_sign_request (struct TES_Client *client, 766 const struct TALER_CRYPTO_BatchSignRequest *bsr) 767 { 768 uint32_t bs = ntohl (bsr->batch_size); 769 uint16_t size = ntohs (bsr->header.size) - sizeof (*bsr); 770 const void *off = (const void *) &bsr[1]; 771 unsigned int idx = 0; 772 bool failure = false; 773 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 if ( (0 == bs) || 778 (bs > TALER_MAX_COINS) ) 779 { 780 GNUNET_break_op (0); 781 return GNUNET_SYSERR; 782 } 783 { 784 struct BatchJob jobs[bs]; 785 786 while ( (idx < bs) && 787 (size >= sizeof (struct TALER_CRYPTO_SignRequest)) ) 788 { 789 const struct TALER_CRYPTO_SignRequest *sr = off; 790 uint16_t s = ntohs (sr->header.size); 791 792 if ( (s > size) || 793 (s < sizeof (*sr)) ) 794 { 795 failure = true; 796 bs = idx; 797 break; 798 } 799 start_job (sr, 800 &jobs[idx++]); 801 off += s; 802 size -= s; 803 } 804 GNUNET_break_op (0 == size); 805 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 GNUNET_break_op (0); 810 failure = true; 811 } 812 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 for (unsigned int i = 0; i<bs; i++) 818 if (GNUNET_OK != 819 finish_job (client, 820 &jobs[i])) 821 tret = GNUNET_SYSERR; 822 } 823 if (GNUNET_OK != tret) 824 return tret; /* client is gone, do not bother with more replies */ 825 if (failure) 826 { 827 struct TALER_CRYPTO_SignFailure sf = { 828 .header.size = htons (sizeof (sf)), 829 .header.type = htons (TALER_HELPER_RSA_MT_RES_BATCH_FAILURE), 830 .ec = htonl (TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE) 831 }; 832 833 GNUNET_break (0); 834 return TES_transmit (client->csock, 835 &sf.header); 836 } 837 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 start_worker (void) 848 { 849 struct Worker *w; 850 851 w = GNUNET_new (struct Worker); 852 sem_init (&w->sem, 853 0); 854 if (0 != pthread_create (&w->pt, 855 NULL, 856 &worker, 857 w)) 858 { 859 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, 860 "pthread_create"); 861 GNUNET_free (w); 862 return GNUNET_SYSERR; 863 } 864 workers++; 865 return GNUNET_OK; 866 } 867 868 869 /** 870 * Stop all worker threads. 871 */ 872 static void 873 stop_workers (void) 874 { 875 while (workers > 0) 876 { 877 struct Worker *w; 878 void *result; 879 880 sem_down (&worker_sem); 881 GNUNET_assert (0 == pthread_mutex_lock (&worker_lock)); 882 w = worker_head; 883 GNUNET_CONTAINER_DLL_remove (worker_head, 884 worker_tail, 885 w); 886 GNUNET_assert (0 == pthread_mutex_unlock (&worker_lock)); 887 w->do_shutdown = true; 888 sem_up (&w->sem); 889 pthread_join (w->pt, 890 &result); 891 GNUNET_assert (result == w); 892 sem_done (&w->sem); 893 GNUNET_free (w); 894 workers--; 895 } 896 } 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 setup_key (struct DenominationKey *dk, 908 struct DenominationKey *position) 909 { 910 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 priv = GNUNET_CRYPTO_rsa_private_key_create (denom->rsa_keysize); 917 if (NULL == priv) 918 { 919 GNUNET_break (0); 920 GNUNET_SCHEDULER_shutdown (); 921 globals->global_ret = EXIT_FAILURE; 922 return GNUNET_SYSERR; 923 } 924 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 925 if (NULL == pub) 926 { 927 GNUNET_break (0); 928 GNUNET_CRYPTO_rsa_private_key_free (priv); 929 return GNUNET_SYSERR; 930 } 931 buf_size = GNUNET_CRYPTO_rsa_private_key_encode (priv, 932 &buf); 933 GNUNET_CRYPTO_rsa_public_key_hash (pub, 934 &dk->h_rsa.hash); 935 GNUNET_asprintf ( 936 &dk->filename, 937 "%s/%s/%llu-%llu", 938 keydir, 939 denom->section, 940 (unsigned long long) (dk->anchor_start.abs_time.abs_value_us 941 / GNUNET_TIME_UNIT_SECONDS.rel_value_us), 942 (unsigned long long) (dk->anchor_end.abs_time.abs_value_us 943 / GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 944 if (GNUNET_OK != 945 GNUNET_DISK_fn_write (dk->filename, 946 buf, 947 buf_size, 948 GNUNET_DISK_PERM_USER_READ)) 949 { 950 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 951 "write", 952 dk->filename); 953 GNUNET_free (dk->filename); 954 GNUNET_free (buf); 955 GNUNET_CRYPTO_rsa_private_key_free (priv); 956 GNUNET_CRYPTO_rsa_public_key_free (pub); 957 return GNUNET_SYSERR; 958 } 959 GNUNET_free (buf); 960 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 dk->denom_priv = priv; 967 dk->denom_pub = pub; 968 dk->key_gen = key_gen; 969 generate_response (dk); 970 if (GNUNET_OK != 971 GNUNET_CONTAINER_multihashmap_put ( 972 keys, 973 &dk->h_rsa.hash, 974 dk, 975 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 976 { 977 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 978 "Duplicate private key created! Terminating.\n"); 979 GNUNET_CRYPTO_rsa_private_key_free (dk->denom_priv); 980 GNUNET_CRYPTO_rsa_public_key_free (dk->denom_pub); 981 GNUNET_free (dk->filename); 982 GNUNET_free (dk->an); 983 GNUNET_free (dk); 984 return GNUNET_SYSERR; 985 } 986 GNUNET_CONTAINER_DLL_insert_after (denom->keys_head, 987 denom->keys_tail, 988 position, 989 dk); 990 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 purge_key (struct DenominationKey *dk) 1001 { 1002 if (dk->purge) 1003 return; 1004 if (0 != unlink (dk->filename)) 1005 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 1006 "unlink", 1007 dk->filename); 1008 GNUNET_free (dk->filename); 1009 dk->purge = true; 1010 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 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 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 1032 dk = GNUNET_CONTAINER_multihashmap_get (keys, 1033 &rr->h_rsa.hash); 1034 if (NULL == dk) 1035 { 1036 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1037 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1038 "Revocation request ignored, denomination key %s unknown\n", 1039 GNUNET_h2s (&rr->h_rsa.hash)); 1040 return GNUNET_OK; 1041 } 1042 if (dk->purge) 1043 { 1044 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1045 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1046 "Revocation request ignored, denomination key %s already revoked\n", 1047 GNUNET_h2s (&rr->h_rsa.hash)); 1048 return GNUNET_OK; 1049 } 1050 1051 key_gen++; 1052 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 purge_key (dk); 1057 1058 /* Setup replacement key */ 1059 denom = dk->denom; 1060 ndk = GNUNET_new (struct DenominationKey); 1061 ndk->denom = denom; 1062 ndk->anchor_start = dk->anchor_start; 1063 ndk->anchor_end = dk->anchor_end; 1064 if (GNUNET_OK != 1065 setup_key (ndk, 1066 dk)) 1067 { 1068 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1069 GNUNET_break (0); 1070 GNUNET_SCHEDULER_shutdown (); 1071 globals->global_ret = EXIT_FAILURE; 1072 return GNUNET_SYSERR; 1073 } 1074 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1075 TES_wake_clients (); 1076 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 rsa_work_dispatch (struct TES_Client *client, 1089 const struct GNUNET_MessageHeader *hdr) 1090 { 1091 uint16_t msize = ntohs (hdr->size); 1092 1093 switch (ntohs (hdr->type)) 1094 { 1095 case TALER_HELPER_RSA_MT_REQ_SIGN: 1096 if (msize <= sizeof (struct TALER_CRYPTO_SignRequest)) 1097 { 1098 GNUNET_break_op (0); 1099 return GNUNET_SYSERR; 1100 } 1101 return handle_sign_request ( 1102 client, 1103 (const struct TALER_CRYPTO_SignRequest *) hdr); 1104 case TALER_HELPER_RSA_MT_REQ_REVOKE: 1105 if (msize != sizeof (struct TALER_CRYPTO_RevokeRequest)) 1106 { 1107 GNUNET_break_op (0); 1108 return GNUNET_SYSERR; 1109 } 1110 return handle_revoke_request ( 1111 client, 1112 (const struct TALER_CRYPTO_RevokeRequest *) hdr); 1113 case TALER_HELPER_RSA_MT_REQ_BATCH_SIGN: 1114 if (msize <= sizeof (struct TALER_CRYPTO_BatchSignRequest)) 1115 { 1116 GNUNET_break_op (0); 1117 return GNUNET_SYSERR; 1118 } 1119 return handle_batch_sign_request ( 1120 client, 1121 (const struct TALER_CRYPTO_BatchSignRequest *) hdr); 1122 default: 1123 GNUNET_break_op (0); 1124 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 rsa_client_init (struct TES_Client *client) 1138 { 1139 size_t obs = 0; 1140 char *buf; 1141 1142 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1143 "Initializing new client %p\n", 1144 client); 1145 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 1146 for (struct Denomination *denom = denom_head; 1147 NULL != denom; 1148 denom = denom->next) 1149 { 1150 for (struct DenominationKey *dk = denom->keys_head; 1151 NULL != dk; 1152 dk = dk->next) 1153 { 1154 GNUNET_assert (obs + ntohs (dk->an->header.size) 1155 > obs); 1156 obs += ntohs (dk->an->header.size); 1157 } 1158 } 1159 buf = GNUNET_malloc (obs); 1160 obs = 0; 1161 for (struct Denomination *denom = denom_head; 1162 NULL != denom; 1163 denom = denom->next) 1164 { 1165 for (struct DenominationKey *dk = denom->keys_head; 1166 NULL != dk; 1167 dk = dk->next) 1168 { 1169 GNUNET_memcpy (&buf[obs], 1170 dk->an, 1171 ntohs (dk->an->header.size)); 1172 GNUNET_assert (obs + ntohs (dk->an->header.size) 1173 > obs); 1174 obs += ntohs (dk->an->header.size); 1175 } 1176 } 1177 client->key_gen = key_gen; 1178 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1179 if (GNUNET_OK != 1180 TES_transmit_raw (client->csock, 1181 obs, 1182 buf)) 1183 { 1184 GNUNET_free (buf); 1185 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1186 "Client %p must have disconnected\n", 1187 client); 1188 return GNUNET_SYSERR; 1189 } 1190 GNUNET_free (buf); 1191 { 1192 struct GNUNET_MessageHeader synced = { 1193 .type = htons (TALER_HELPER_RSA_SYNCED), 1194 .size = htons (sizeof (synced)) 1195 }; 1196 1197 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 1198 "Sending RSA SYNCED message to %p\n", 1199 client); 1200 if (GNUNET_OK != 1201 TES_transmit (client->csock, 1202 &synced)) 1203 { 1204 GNUNET_break (0); 1205 return GNUNET_SYSERR; 1206 } 1207 } 1208 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 rsa_update_client_keys (struct TES_Client *client) 1221 { 1222 size_t obs = 0; 1223 char *buf; 1224 enum GNUNET_GenericReturnValue ret; 1225 1226 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 1227 for (struct Denomination *denom = denom_head; 1228 NULL != denom; 1229 denom = denom->next) 1230 { 1231 for (struct DenominationKey *key = denom->keys_head; 1232 NULL != key; 1233 key = key->next) 1234 { 1235 if (key->key_gen <= client->key_gen) 1236 continue; 1237 if (key->purge) 1238 obs += sizeof (struct TALER_CRYPTO_RsaKeyPurgeNotification); 1239 else 1240 obs += ntohs (key->an->header.size); 1241 } 1242 } 1243 if (0 == obs) 1244 { 1245 /* nothing to do */ 1246 client->key_gen = key_gen; 1247 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1248 return GNUNET_OK; 1249 } 1250 buf = GNUNET_malloc (obs); 1251 obs = 0; 1252 for (struct Denomination *denom = denom_head; 1253 NULL != denom; 1254 denom = denom->next) 1255 { 1256 for (struct DenominationKey *key = denom->keys_head; 1257 NULL != key; 1258 key = key->next) 1259 { 1260 if (key->key_gen <= client->key_gen) 1261 continue; 1262 if (key->purge) 1263 { 1264 struct TALER_CRYPTO_RsaKeyPurgeNotification pn = { 1265 .header.type = htons (TALER_HELPER_RSA_MT_PURGE), 1266 .header.size = htons (sizeof (pn)), 1267 .h_rsa = key->h_rsa 1268 }; 1269 1270 GNUNET_memcpy (&buf[obs], 1271 &pn, 1272 sizeof (pn)); 1273 GNUNET_assert (obs + sizeof (pn) 1274 > obs); 1275 obs += sizeof (pn); 1276 } 1277 else 1278 { 1279 GNUNET_memcpy (&buf[obs], 1280 key->an, 1281 ntohs (key->an->header.size)); 1282 GNUNET_assert (obs + ntohs (key->an->header.size) 1283 > obs); 1284 obs += ntohs (key->an->header.size); 1285 } 1286 } 1287 } 1288 client->key_gen = key_gen; 1289 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1290 ret = TES_transmit_raw (client->csock, 1291 obs, 1292 buf); 1293 GNUNET_free (buf); 1294 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 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 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 dk = GNUNET_new (struct DenominationKey); 1318 dk->denom = denom; 1319 dk->anchor_start = anchor_start; 1320 dk->anchor_end = anchor_end; 1321 if (GNUNET_OK != 1322 setup_key (dk, 1323 denom->keys_tail)) 1324 { 1325 GNUNET_break (0); 1326 GNUNET_free (dk); 1327 GNUNET_SCHEDULER_shutdown (); 1328 globals->global_ret = EXIT_FAILURE; 1329 return GNUNET_SYSERR; 1330 } 1331 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 get_maximum_duration (void) 1344 { 1345 struct GNUNET_TIME_Relative ret 1346 = GNUNET_TIME_UNIT_ZERO; 1347 1348 for (struct Denomination *denom = denom_head; 1349 NULL != denom; 1350 denom = denom->next) 1351 { 1352 ret = GNUNET_TIME_relative_max (ret, 1353 denom->duration_withdraw); 1354 } 1355 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 action_time (void) 1366 { 1367 struct GNUNET_TIME_Relative md = get_maximum_duration (); 1368 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get (); 1369 uint64_t mod; 1370 1371 if (GNUNET_TIME_relative_is_zero (md)) 1372 return GNUNET_TIME_UNIT_FOREVER_ABS; 1373 mod = now.abs_value_us % md.rel_value_us; 1374 now.abs_value_us -= mod; 1375 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 remove_expired_denomination_keys (struct Denomination *denom) 1387 { 1388 while ( (NULL != denom->keys_head) && 1389 GNUNET_TIME_absolute_is_past ( 1390 denom->keys_head->anchor_end.abs_time)) 1391 { 1392 struct DenominationKey *key = denom->keys_head; 1393 struct DenominationKey *nxt = key->next; 1394 1395 if (0 != key->rc) 1396 break; /* later */ 1397 GNUNET_CONTAINER_DLL_remove (denom->keys_head, 1398 denom->keys_tail, 1399 key); 1400 GNUNET_assert (GNUNET_OK == 1401 GNUNET_CONTAINER_multihashmap_remove ( 1402 keys, 1403 &key->h_rsa.hash, 1404 key)); 1405 if ( (! key->purge) && 1406 (0 != unlink (key->filename)) ) 1407 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 1408 "unlink", 1409 key->filename); 1410 GNUNET_free (key->filename); 1411 GNUNET_CRYPTO_rsa_private_key_free (key->denom_priv); 1412 GNUNET_CRYPTO_rsa_public_key_free (key->denom_pub); 1413 GNUNET_free (key->an); 1414 GNUNET_free (key); 1415 key = nxt; 1416 } 1417 } 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 get_anchor_end (void) 1432 { 1433 struct GNUNET_TIME_Relative md = get_maximum_duration (); 1434 struct GNUNET_TIME_Absolute end 1435 = GNUNET_TIME_relative_to_absolute (lookahead_sign); 1436 uint64_t mod; 1437 1438 if (GNUNET_TIME_relative_is_zero (md)) 1439 return GNUNET_TIME_UNIT_ZERO_TS; 1440 /* Round up 'end' to a multiple of 'md' */ 1441 mod = end.abs_value_us % md.rel_value_us; 1442 end.abs_value_us -= mod; 1443 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 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 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1464 "Updating denominations ...\n"); 1465 start = opt->global_now; 1466 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 1467 end = get_anchor_end (); 1468 for (struct Denomination *denom = denom_head; 1469 NULL != denom; 1470 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 bool finished = false; 1476 1477 remove_expired_denomination_keys (denom); 1478 if (NULL != denom->keys_tail) 1479 { 1480 anchor_start = denom->keys_tail->anchor_end; 1481 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 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 anchor_start = GNUNET_TIME_absolute_to_timestamp ( 1501 GNUNET_TIME_round_down_utc (start.abs_time, 1502 denom->anchor_round)); 1503 } 1504 finished = GNUNET_TIME_timestamp_cmp (anchor_start, 1505 >=, 1506 end); 1507 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 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 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 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 if (GNUNET_TIME_RI_NONE == denom->anchor_round) 1531 anchor_end = end; /* extend period to align end periods */ 1532 finished = true; 1533 } 1534 /* adjust start time down to ensure overlap */ 1535 anchor_start = GNUNET_TIME_absolute_to_timestamp ( 1536 GNUNET_TIME_absolute_subtract (anchor_start.abs_time, 1537 overlap_duration)); 1538 if (! *wake) 1539 { 1540 key_gen++; 1541 *wake = true; 1542 } 1543 if (GNUNET_OK != 1544 create_key (denom, 1545 anchor_start, 1546 anchor_end)) 1547 { 1548 GNUNET_break (0); 1549 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1550 globals->global_ret = EXIT_FAILURE; 1551 GNUNET_SCHEDULER_shutdown (); 1552 return; 1553 } 1554 anchor_start = anchor_end; 1555 } 1556 remove_expired_denomination_keys (denom); 1557 } 1558 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 1559 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 update_denominations (void *cls) 1571 { 1572 struct TALER_SECMOD_Options *opt = cls; 1573 struct GNUNET_TIME_Absolute at; 1574 bool wake = false; 1575 1576 (void) cls; 1577 keygen_task = NULL; 1578 /* update current time, global override no longer applies */ 1579 opt->global_now = GNUNET_TIME_timestamp_get (); 1580 create_missing_keys (opt, 1581 &wake); 1582 if (wake) 1583 TES_wake_clients (); 1584 at = action_time (); 1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1586 "Next key generation due at %s\n", 1587 GNUNET_TIME_absolute2s (at)); 1588 keygen_task = GNUNET_SCHEDULER_add_at (at, 1589 &update_denominations, 1590 opt); 1591 } 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 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 char *nf = NULL; 1616 1617 anchor_s = strrchr (filename, 1618 '/'); 1619 if (NULL == anchor_s) 1620 { 1621 /* File in a directory without '/' in the name, this makes no sense. */ 1622 GNUNET_break (0); 1623 return; 1624 } 1625 anchor_s++; 1626 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 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 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1640 "Filename `%s' invalid for key file, skipping\n", 1641 anchor_s); 1642 return; 1643 } 1644 anchor_start.abs_time.abs_value_us 1645 = anchor_start_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us; 1646 if (anchor_start_ll != anchor_start.abs_time.abs_value_us 1647 / GNUNET_TIME_UNIT_SECONDS.rel_value_us) 1648 { 1649 /* Integer overflow. Bad, invalid filename. */ 1650 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1651 "Integer overflow. Filename `%s' invalid for key file, skipping\n", 1652 anchor_s); 1653 return; 1654 } 1655 anchor_end 1656 = GNUNET_TIME_absolute_to_timestamp ( 1657 GNUNET_TIME_absolute_add (anchor_start.abs_time, 1658 denom->duration_withdraw)); 1659 GNUNET_asprintf ( 1660 &nf, 1661 "%s/%s/%llu-%llu", 1662 keydir, 1663 denom->section, 1664 anchor_start_ll, 1665 (unsigned long long) (anchor_end.abs_time.abs_value_us 1666 / GNUNET_TIME_UNIT_SECONDS.rel_value_us)); 1667 /* Try to fix the legacy filename */ 1668 if (0 != 1669 rename (filename, 1670 nf)) 1671 { 1672 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1673 "rename", 1674 filename); 1675 GNUNET_free (nf); 1676 } 1677 } 1678 else 1679 { 1680 anchor_start.abs_time.abs_value_us 1681 = anchor_start_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us; 1682 anchor_end.abs_time.abs_value_us 1683 = anchor_end_ll * GNUNET_TIME_UNIT_SECONDS.rel_value_us; 1684 if ( (anchor_start_ll != anchor_start.abs_time.abs_value_us 1685 / GNUNET_TIME_UNIT_SECONDS.rel_value_us) || 1686 (anchor_end_ll != anchor_end.abs_time.abs_value_us 1687 / GNUNET_TIME_UNIT_SECONDS.rel_value_us) ) 1688 { 1689 /* Integer overflow. Bad, invalid filename. */ 1690 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1691 "Integer overflow. Filename `%s' invalid for key file, skipping\n", 1692 anchor_s); 1693 return; 1694 } 1695 } 1696 priv = GNUNET_CRYPTO_rsa_private_key_decode (buf, 1697 buf_size); 1698 if (NULL == priv) 1699 { 1700 /* Parser failure. */ 1701 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 1702 "File `%s' is malformed, skipping\n", 1703 (NULL == nf) ? filename : nf); 1704 GNUNET_free (nf); 1705 return; 1706 } 1707 1708 { 1709 struct GNUNET_CRYPTO_RsaPublicKey *pub; 1710 struct DenominationKey *dk; 1711 struct DenominationKey *before; 1712 1713 pub = GNUNET_CRYPTO_rsa_private_key_get_public (priv); 1714 if (NULL == pub) 1715 { 1716 GNUNET_break (0); 1717 GNUNET_CRYPTO_rsa_private_key_free (priv); 1718 GNUNET_free (nf); 1719 return; 1720 } 1721 /* refuse keys whose modulus does not have the configured size */ 1722 if (denom->rsa_keysize != 1723 GNUNET_CRYPTO_rsa_public_key_len (pub)) 1724 { 1725 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 GNUNET_CRYPTO_rsa_public_key_free (pub); 1732 GNUNET_CRYPTO_rsa_private_key_free (priv); 1733 GNUNET_free (nf); 1734 return; 1735 } 1736 dk = GNUNET_new (struct DenominationKey); 1737 dk->denom_priv = priv; 1738 dk->denom = denom; 1739 dk->anchor_start = anchor_start; 1740 dk->anchor_end = anchor_end; 1741 dk->filename = (NULL == nf) ? GNUNET_strdup (filename) : nf; 1742 GNUNET_CRYPTO_rsa_public_key_hash (pub, 1743 &dk->h_rsa.hash); 1744 dk->denom_pub = pub; 1745 generate_response (dk); 1746 if (GNUNET_OK != 1747 GNUNET_CONTAINER_multihashmap_put ( 1748 keys, 1749 &dk->h_rsa.hash, 1750 dk, 1751 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 1752 { 1753 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 GNUNET_CRYPTO_rsa_private_key_free (priv); 1758 GNUNET_CRYPTO_rsa_public_key_free (pub); 1759 GNUNET_free (dk->an); 1760 GNUNET_free (dk); 1761 return; 1762 } 1763 before = NULL; 1764 for (struct DenominationKey *pos = denom->keys_head; 1765 NULL != pos; 1766 pos = pos->next) 1767 { 1768 if (GNUNET_TIME_timestamp_cmp (pos->anchor_start, 1769 >, 1770 anchor_start)) 1771 break; 1772 before = pos; 1773 } 1774 GNUNET_CONTAINER_DLL_insert_after (denom->keys_head, 1775 denom->keys_tail, 1776 before, 1777 dk); 1778 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 import_key (void *cls, 1796 const char *filename) 1797 { 1798 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 fd = TES_open_key_file (filename, 1806 &sbuf); 1807 if (-1 == fd) 1808 return GNUNET_OK; 1809 fh = GNUNET_DISK_get_handle_from_int_fd (fd); 1810 if (NULL == fh) 1811 { 1812 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1813 "open", 1814 filename); 1815 GNUNET_break (0 == close (fd)); 1816 return GNUNET_OK; 1817 } 1818 if (sbuf.st_size > 16 * 1024) 1819 { 1820 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1821 "File `%s' too big to be a private key\n", 1822 filename); 1823 GNUNET_DISK_file_close (fh); 1824 return GNUNET_OK; 1825 } 1826 ptr = GNUNET_DISK_file_map (fh, 1827 &map, 1828 GNUNET_DISK_MAP_TYPE_READ, 1829 (size_t) sbuf.st_size); 1830 if (NULL == ptr) 1831 { 1832 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 1833 "mmap", 1834 filename); 1835 GNUNET_DISK_file_close (fh); 1836 return GNUNET_OK; 1837 } 1838 parse_key (denom, 1839 filename, 1840 ptr, 1841 (size_t) sbuf.st_size); 1842 GNUNET_DISK_file_unmap (map); 1843 GNUNET_DISK_file_close (fh); 1844 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 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 GNUNET_asprintf (&secname, 1866 "%s-secmod-rsa", 1867 globals->section); 1868 if (GNUNET_OK != 1869 GNUNET_CONFIGURATION_get_value_time (cfg, 1870 ct, 1871 "DURATION_WITHDRAW", 1872 &denom->duration_withdraw)) 1873 { 1874 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 1875 ct, 1876 "DURATION_WITHDRAW"); 1877 GNUNET_free (secname); 1878 return GNUNET_SYSERR; 1879 } 1880 if (GNUNET_TIME_relative_cmp (denom->duration_withdraw, 1881 <, 1882 GNUNET_TIME_UNIT_SECONDS)) 1883 { 1884 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 1885 ct, 1886 "DURATION_WITHDRAW", 1887 "less than one second is not supported"); 1888 GNUNET_free (secname); 1889 return GNUNET_SYSERR; 1890 } 1891 if (GNUNET_TIME_relative_cmp (overlap_duration, 1892 >=, 1893 denom->duration_withdraw)) 1894 { 1895 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 GNUNET_free (secname); 1900 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 if ( (GNUNET_OK != 1908 GNUNET_CONFIGURATION_get_value_time (cfg, 1909 ct, 1910 "ANCHOR_ROUND", 1911 &ar)) && 1912 (GNUNET_OK != 1913 GNUNET_CONFIGURATION_get_value_time (cfg, 1914 secname, 1915 "ANCHOR_ROUND", 1916 &ar)) ) 1917 ar = GNUNET_TIME_UNIT_ZERO; /* not configured: do not round */ 1918 denom->anchor_round 1919 = GNUNET_TIME_relative_to_round_interval (ar); 1920 if ( (GNUNET_TIME_RI_NONE == denom->anchor_round) && 1921 (! GNUNET_TIME_relative_is_zero (ar)) ) 1922 { 1923 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 GNUNET_free (secname); 1928 return GNUNET_SYSERR; 1929 } 1930 } 1931 if (GNUNET_OK != 1932 GNUNET_CONFIGURATION_get_value_number (cfg, 1933 ct, 1934 "RSA_KEYSIZE", 1935 &rsa_keysize)) 1936 { 1937 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 1938 ct, 1939 "RSA_KEYSIZE"); 1940 GNUNET_free (secname); 1941 return GNUNET_SYSERR; 1942 } 1943 if ( (rsa_keysize > 4 * 2048) || 1944 (rsa_keysize < 1024) ) 1945 { 1946 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 GNUNET_free (secname); 1951 return GNUNET_SYSERR; 1952 } 1953 GNUNET_free (secname); 1954 denom->rsa_keysize = (unsigned int) rsa_keysize; 1955 denom->section = TES_normalize_section (ct); 1956 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 load_denominations (void *cls, 1993 const char *denomination_alias) 1994 { 1995 struct LoadContext *ctx = cls; 1996 struct Denomination *denom; 1997 char *cipher; 1998 1999 if (0 != strncasecmp (denomination_alias, 2000 ctx->cprefix, 2001 strlen (ctx->cprefix))) 2002 return; /* not a denomination type definition */ 2003 if (GNUNET_OK != 2004 GNUNET_CONFIGURATION_get_value_string (ctx->cfg, 2005 denomination_alias, 2006 "CIPHER", 2007 &cipher)) 2008 { 2009 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2010 denomination_alias, 2011 "CIPHER"); 2012 return; 2013 } 2014 if (0 != strcmp (cipher, 2015 "RSA")) 2016 { 2017 GNUNET_free (cipher); 2018 return; /* Ignore denominations of other types than CS */ 2019 } 2020 GNUNET_free (cipher); 2021 denom = GNUNET_new (struct Denomination); 2022 if (GNUNET_OK != 2023 parse_denomination_cfg (ctx->cfg, 2024 denomination_alias, 2025 denom)) 2026 { 2027 ctx->ret = GNUNET_SYSERR; 2028 GNUNET_free (denom); 2029 return; 2030 } 2031 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 2032 "Loading keys for denomination %s\n", 2033 denom->section); 2034 { 2035 char *dname; 2036 2037 GNUNET_asprintf (&dname, 2038 "%s/%s", 2039 keydir, 2040 denom->section); 2041 GNUNET_break (GNUNET_OK == 2042 GNUNET_DISK_directory_create (dname)); 2043 GNUNET_DISK_directory_scan (dname, 2044 &import_key, 2045 denom); 2046 GNUNET_free (dname); 2047 } 2048 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 load_durations (const struct GNUNET_CONFIGURATION_Handle *cfg) 2062 { 2063 char *secname; 2064 2065 GNUNET_asprintf (&secname, 2066 "%s-secmod-rsa", 2067 globals->section); 2068 if (GNUNET_OK != 2069 GNUNET_CONFIGURATION_get_value_time (cfg, 2070 secname, 2071 "OVERLAP_DURATION", 2072 &overlap_duration)) 2073 { 2074 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2075 secname, 2076 "OVERLAP_DURATION"); 2077 GNUNET_free (secname); 2078 return GNUNET_SYSERR; 2079 } 2080 if (GNUNET_OK != 2081 GNUNET_CONFIGURATION_get_value_time (cfg, 2082 secname, 2083 "LOOKAHEAD_SIGN", 2084 &lookahead_sign)) 2085 { 2086 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2087 secname, 2088 "LOOKAHEAD_SIGN"); 2089 GNUNET_free (secname); 2090 return GNUNET_SYSERR; 2091 } 2092 GNUNET_free (secname); 2093 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 do_shutdown (void *cls) 2104 { 2105 (void) cls; 2106 TES_listen_stop (); 2107 if (NULL != keygen_task) 2108 { 2109 GNUNET_SCHEDULER_cancel (keygen_task); 2110 keygen_task = NULL; 2111 } 2112 stop_workers (); 2113 sem_done (&worker_sem); 2114 } 2115 2116 2117 void 2118 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 struct TALER_SECMOD_Options *opt = cls; 2129 char *secname; 2130 2131 (void) args; 2132 (void) cfgfile; 2133 globals = opt; 2134 if (GNUNET_TIME_timestamp_cmp (opt->global_now, 2135 !=, 2136 opt->global_now_tmp)) 2137 { 2138 /* The user gave "--now", use it! */ 2139 opt->global_now = opt->global_now_tmp; 2140 } 2141 else 2142 { 2143 /* get current time again, we may be timetraveling! */ 2144 opt->global_now = GNUNET_TIME_timestamp_get (); 2145 } 2146 GNUNET_asprintf (&secname, 2147 "%s-secmod-rsa", 2148 opt->section); 2149 if (GNUNET_OK != 2150 GNUNET_CONFIGURATION_get_value_filename (cfg, 2151 secname, 2152 "KEY_DIR", 2153 &keydir)) 2154 { 2155 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 2156 secname, 2157 "KEY_DIR"); 2158 GNUNET_free (secname); 2159 opt->global_ret = EXIT_NOTCONFIGURED; 2160 return; 2161 } 2162 if (GNUNET_OK != 2163 load_durations (cfg)) 2164 { 2165 opt->global_ret = EXIT_NOTCONFIGURED; 2166 GNUNET_free (secname); 2167 return; 2168 } 2169 if (GNUNET_OK != 2170 TES_normalize_key_directory (cfg, 2171 keydir, 2172 opt->cprefix)) 2173 { 2174 opt->global_ret = EXIT_FAILURE; 2175 GNUNET_free (secname); 2176 return; 2177 } 2178 opt->global_ret = TES_listen_start (cfg, 2179 secname, 2180 &cb); 2181 GNUNET_free (secname); 2182 if (0 != opt->global_ret) 2183 return; 2184 sem_init (&worker_sem, 2185 0); 2186 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 2187 NULL); 2188 if (0 == opt->max_workers) 2189 { 2190 long lret; 2191 2192 lret = sysconf (_SC_NPROCESSORS_CONF); 2193 if (lret <= 0) 2194 lret = 1; 2195 opt->max_workers = (unsigned int) lret; 2196 } 2197 2198 for (unsigned int i = 0; i<opt->max_workers; i++) 2199 if (GNUNET_OK != 2200 start_worker ()) 2201 { 2202 GNUNET_SCHEDULER_shutdown (); 2203 return; 2204 } 2205 /* Load denominations */ 2206 keys = GNUNET_CONTAINER_multihashmap_create (65536, 2207 true); 2208 { 2209 struct LoadContext lc = { 2210 .cfg = cfg, 2211 .ret = GNUNET_OK, 2212 .cprefix = opt->cprefix 2213 }; 2214 bool wake = true; 2215 2216 GNUNET_assert (0 == pthread_mutex_lock (&keys_lock)); 2217 GNUNET_CONFIGURATION_iterate_sections (cfg, 2218 &load_denominations, 2219 &lc); 2220 GNUNET_assert (0 == pthread_mutex_unlock (&keys_lock)); 2221 if (GNUNET_OK != lc.ret) 2222 { 2223 opt->global_ret = EXIT_FAILURE; 2224 GNUNET_SCHEDULER_shutdown (); 2225 return; 2226 } 2227 create_missing_keys (opt, 2228 &wake); 2229 } 2230 if (NULL == denom_head) 2231 { 2232 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 TES_wake_clients (); 2236 return; 2237 } 2238 /* start job to keep keys up-to-date; MUST be run before the #listen_task, 2239 hence with priority. */ 2240 keygen_task = GNUNET_SCHEDULER_add_with_priority ( 2241 GNUNET_SCHEDULER_PRIORITY_URGENT, 2242 &update_denominations, 2243 opt); 2244 }