crypto_helper_cs.c (28612B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2020, 2021, 2022 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/crypto_helper_cs.c 18 * @brief utility functions for running out-of-process private key operations 19 * @author Christian Grothoff 20 */ 21 #include "platform.h" 22 #include "taler/taler_util.h" 23 #include "taler/taler_signatures.h" 24 #include "secmod_cs.h" 25 #include <poll.h> 26 #include "crypto_helper_common.h" 27 28 29 struct TALER_CRYPTO_CsDenominationHelper 30 { 31 /** 32 * Function to call with updates to available key material. 33 */ 34 TALER_CRYPTO_CsDenominationKeyStatusCallback dkc; 35 36 /** 37 * Closure for @e dkc 38 */ 39 void *dkc_cls; 40 41 /** 42 * Socket address of the denomination helper process. 43 * Used to reconnect if the connection breaks. 44 */ 45 struct sockaddr_un sa; 46 47 /** 48 * The UNIX domain socket, -1 if we are currently not connected. 49 */ 50 int sock; 51 52 /** 53 * Have we ever been sync'ed? 54 */ 55 bool synced; 56 }; 57 58 59 /** 60 * Disconnect from the helper process. Updates 61 * @e sock field in @a dh. 62 * 63 * @param[in,out] dh handle to tear down connection of 64 */ 65 static void 66 do_disconnect (struct TALER_CRYPTO_CsDenominationHelper *dh) 67 { 68 GNUNET_break (0 == close (dh->sock)); 69 dh->sock = -1; 70 dh->synced = false; 71 } 72 73 74 /** 75 * Try to connect to the helper process. Updates 76 * @e sock field in @a dh. 77 * 78 * @param[in,out] dh handle to establish connection for 79 * @return #GNUNET_OK on success 80 */ 81 static enum GNUNET_GenericReturnValue 82 try_connect (struct TALER_CRYPTO_CsDenominationHelper *dh) 83 { 84 if (-1 != dh->sock) 85 return GNUNET_OK; 86 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 87 "Establishing connection!\n"); 88 dh->sock = socket (AF_UNIX, 89 SOCK_STREAM, 90 0); 91 if (-1 == dh->sock) 92 { 93 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 94 "socket"); 95 return GNUNET_SYSERR; 96 } 97 if (0 != 98 connect (dh->sock, 99 (const struct sockaddr *) &dh->sa, 100 sizeof (dh->sa))) 101 { 102 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 103 "connect", 104 dh->sa.sun_path); 105 do_disconnect (dh); 106 return GNUNET_SYSERR; 107 } 108 TALER_CRYPTO_helper_cs_poll (dh); 109 return GNUNET_OK; 110 } 111 112 113 struct TALER_CRYPTO_CsDenominationHelper * 114 TALER_CRYPTO_helper_cs_connect ( 115 const struct GNUNET_CONFIGURATION_Handle *cfg, 116 const char *section, 117 TALER_CRYPTO_CsDenominationKeyStatusCallback dkc, 118 void *dkc_cls) 119 { 120 struct TALER_CRYPTO_CsDenominationHelper *dh; 121 char *unixpath; 122 char *secname; 123 124 GNUNET_asprintf (&secname, 125 "%s-secmod-cs", 126 section); 127 if (GNUNET_OK != 128 GNUNET_CONFIGURATION_get_value_filename (cfg, 129 secname, 130 "UNIXPATH", 131 &unixpath)) 132 { 133 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 134 secname, 135 "UNIXPATH"); 136 GNUNET_free (secname); 137 return NULL; 138 } 139 /* we use >= here because we want the sun_path to always 140 be 0-terminated */ 141 if (strlen (unixpath) >= sizeof (dh->sa.sun_path)) 142 { 143 GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_ERROR, 144 secname, 145 "UNIXPATH", 146 "path too long"); 147 GNUNET_free (unixpath); 148 GNUNET_free (secname); 149 return NULL; 150 } 151 GNUNET_free (secname); 152 dh = GNUNET_new (struct TALER_CRYPTO_CsDenominationHelper); 153 dh->dkc = dkc; 154 dh->dkc_cls = dkc_cls; 155 dh->sa.sun_family = AF_UNIX; 156 strncpy (dh->sa.sun_path, 157 unixpath, 158 sizeof (dh->sa.sun_path) - 1); 159 GNUNET_free (unixpath); 160 dh->sock = -1; 161 if (GNUNET_OK != 162 try_connect (dh)) 163 { 164 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 165 "Could not connect to %s. Will keep trying\n", 166 "taler-exchange-helper-secmod-cs"); 167 } 168 return dh; 169 } 170 171 172 /** 173 * Handle a #TALER_HELPER_CS_MT_AVAIL message from the helper. 174 * 175 * @param dh helper context 176 * @param hdr message that we received 177 * @return #GNUNET_OK on success 178 */ 179 static enum GNUNET_GenericReturnValue 180 handle_mt_avail (struct TALER_CRYPTO_CsDenominationHelper *dh, 181 const struct GNUNET_MessageHeader *hdr) 182 { 183 const struct TALER_CRYPTO_CsKeyAvailableNotification *kan 184 = (const struct TALER_CRYPTO_CsKeyAvailableNotification *) hdr; 185 const char *buf = (const char *) &kan[1]; 186 const char *section_name; 187 uint32_t snl; 188 189 if (sizeof (*kan) > ntohs (hdr->size)) 190 { 191 GNUNET_break_op (0); 192 return GNUNET_SYSERR; 193 } 194 snl = ntohl (kan->section_name_len); 195 if (ntohs (hdr->size) != sizeof (*kan) + snl) 196 { 197 GNUNET_break_op (0); 198 return GNUNET_SYSERR; 199 } 200 if (0 == snl) 201 { 202 GNUNET_break_op (0); 203 return GNUNET_SYSERR; 204 } 205 section_name = buf; 206 if ('\0' != section_name[snl - 1]) 207 { 208 GNUNET_break_op (0); 209 return GNUNET_SYSERR; 210 } 211 212 { 213 struct GNUNET_CRYPTO_BlindSignPublicKey *bsign_pub; 214 struct TALER_CsPubHashP h_cs; 215 216 bsign_pub = GNUNET_new (struct GNUNET_CRYPTO_BlindSignPublicKey); 217 bsign_pub->cipher = GNUNET_CRYPTO_BSA_CS; 218 bsign_pub->rc = 1; 219 bsign_pub->details.cs_public_key = kan->denom_pub; 220 221 GNUNET_CRYPTO_hash (&bsign_pub->details.cs_public_key, 222 sizeof (bsign_pub->details.cs_public_key), 223 &bsign_pub->pub_key_hash); 224 h_cs.hash = bsign_pub->pub_key_hash; 225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 226 "Received CS key %s (%s)\n", 227 GNUNET_h2s (&h_cs.hash), 228 section_name); 229 if (GNUNET_OK != 230 TALER_exchange_secmod_cs_verify ( 231 &h_cs, 232 section_name, 233 GNUNET_TIME_timestamp_ntoh (kan->anchor_time), 234 GNUNET_TIME_relative_ntoh (kan->duration_withdraw), 235 &kan->secm_pub, 236 &kan->secm_sig)) 237 { 238 GNUNET_break_op (0); 239 GNUNET_CRYPTO_blind_sign_pub_decref (bsign_pub); 240 return GNUNET_SYSERR; 241 } 242 dh->dkc (dh->dkc_cls, 243 section_name, 244 GNUNET_TIME_timestamp_ntoh (kan->anchor_time), 245 GNUNET_TIME_relative_ntoh (kan->duration_withdraw), 246 &h_cs, 247 bsign_pub, 248 &kan->secm_pub, 249 &kan->secm_sig); 250 GNUNET_CRYPTO_blind_sign_pub_decref (bsign_pub); 251 } 252 return GNUNET_OK; 253 } 254 255 256 /** 257 * Handle a #TALER_HELPER_CS_MT_PURGE message from the helper. 258 * 259 * @param dh helper context 260 * @param hdr message that we received 261 * @return #GNUNET_OK on success 262 */ 263 static enum GNUNET_GenericReturnValue 264 handle_mt_purge (struct TALER_CRYPTO_CsDenominationHelper *dh, 265 const struct GNUNET_MessageHeader *hdr) 266 { 267 const struct TALER_CRYPTO_CsKeyPurgeNotification *pn 268 = (const struct TALER_CRYPTO_CsKeyPurgeNotification *) hdr; 269 270 if (sizeof (*pn) != ntohs (hdr->size)) 271 { 272 GNUNET_break_op (0); 273 return GNUNET_SYSERR; 274 } 275 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 276 "Received revocation of denomination key %s\n", 277 GNUNET_h2s (&pn->h_cs.hash)); 278 dh->dkc (dh->dkc_cls, 279 NULL, 280 GNUNET_TIME_UNIT_ZERO_TS, 281 GNUNET_TIME_UNIT_ZERO, 282 &pn->h_cs, 283 NULL, 284 NULL, 285 NULL); 286 return GNUNET_OK; 287 } 288 289 290 void 291 TALER_CRYPTO_helper_cs_poll (struct TALER_CRYPTO_CsDenominationHelper *dh) 292 { 293 char buf[UINT16_MAX]; 294 size_t off = 0; 295 unsigned int retry_limit = 3; 296 const struct GNUNET_MessageHeader *hdr 297 = (const struct GNUNET_MessageHeader *) buf; 298 299 if (GNUNET_OK != 300 try_connect (dh)) 301 return; /* give up */ 302 while (1) 303 { 304 uint16_t msize; 305 ssize_t ret; 306 307 ret = recv (dh->sock, 308 buf + off, 309 sizeof (buf) - off, 310 (dh->synced && (0 == off)) 311 ? MSG_DONTWAIT 312 : 0); 313 if (ret < 0) 314 { 315 if (EINTR == errno) 316 continue; 317 if (EAGAIN == errno) 318 { 319 GNUNET_assert (dh->synced); 320 GNUNET_assert (0 == off); 321 break; 322 } 323 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 324 "recv"); 325 do_disconnect (dh); 326 if (0 == retry_limit) 327 return; /* give up */ 328 if (GNUNET_OK != 329 try_connect (dh)) 330 return; /* give up */ 331 retry_limit--; 332 continue; 333 } 334 if (0 == ret) 335 { 336 GNUNET_break (0 == off); 337 return; 338 } 339 off += ret; 340 more: 341 if (off < sizeof (struct GNUNET_MessageHeader)) 342 continue; 343 msize = ntohs (hdr->size); 344 if (msize < sizeof (struct GNUNET_MessageHeader)) 345 { 346 GNUNET_break_op (0); 347 do_disconnect (dh); 348 return; 349 } 350 if (off < msize) 351 continue; 352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 353 "Received message of type %u and length %u\n", 354 (unsigned int) ntohs (hdr->type), 355 (unsigned int) msize); 356 switch (ntohs (hdr->type)) 357 { 358 case TALER_HELPER_CS_MT_AVAIL: 359 if (GNUNET_OK != 360 handle_mt_avail (dh, 361 hdr)) 362 { 363 GNUNET_break_op (0); 364 do_disconnect (dh); 365 return; 366 } 367 break; 368 case TALER_HELPER_CS_MT_PURGE: 369 if (GNUNET_OK != 370 handle_mt_purge (dh, 371 hdr)) 372 { 373 GNUNET_break_op (0); 374 do_disconnect (dh); 375 return; 376 } 377 break; 378 case TALER_HELPER_CS_SYNCED: 379 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 380 "Now synchronized with CS helper\n"); 381 dh->synced = true; 382 break; 383 default: 384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 385 "Received unexpected message of type %d (len: %u)\n", 386 (unsigned int) ntohs (hdr->type), 387 (unsigned int) msize); 388 GNUNET_break_op (0); 389 do_disconnect (dh); 390 return; 391 } 392 memmove (buf, 393 &buf[msize], 394 off - msize); 395 off -= msize; 396 goto more; 397 } 398 } 399 400 401 void 402 TALER_CRYPTO_helper_cs_revoke ( 403 struct TALER_CRYPTO_CsDenominationHelper *dh, 404 const struct TALER_CsPubHashP *h_cs) 405 { 406 struct TALER_CRYPTO_CsRevokeRequest rr = { 407 .header.size = htons (sizeof (rr)), 408 .header.type = htons (TALER_HELPER_CS_MT_REQ_REVOKE), 409 .h_cs = *h_cs 410 }; 411 412 if (GNUNET_OK != 413 try_connect (dh)) 414 return; /* give up */ 415 if (GNUNET_OK != 416 TALER_crypto_helper_send_all (dh->sock, 417 &rr, 418 sizeof (rr))) 419 { 420 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 421 "send"); 422 do_disconnect (dh); 423 return; 424 } 425 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 426 "Requested revocation of denomination key %s\n", 427 GNUNET_h2s (&h_cs->hash)); 428 } 429 430 431 enum TALER_ErrorCode 432 TALER_CRYPTO_helper_cs_batch_sign ( 433 struct TALER_CRYPTO_CsDenominationHelper *dh, 434 unsigned int reqs_length, 435 const struct TALER_CRYPTO_CsSignRequest reqs[static reqs_length], 436 bool for_melt, 437 struct TALER_BlindedDenominationSignature bss[static reqs_length]) 438 { 439 enum TALER_ErrorCode ec = TALER_EC_INVALID; 440 unsigned int rpos; 441 unsigned int rend; 442 unsigned int wpos; 443 444 memset (bss, 445 0, 446 sizeof (*bss) * reqs_length); 447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 448 "Starting signature process\n"); 449 if (GNUNET_OK != 450 try_connect (dh)) 451 { 452 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 453 "Failed to connect to helper\n"); 454 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 455 } 456 457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 458 "Requesting %u signatures\n", 459 reqs_length); 460 rpos = 0; 461 rend = 0; 462 wpos = 0; 463 while (rpos < reqs_length) 464 { 465 unsigned int mlen = sizeof (struct TALER_CRYPTO_BatchSignRequest); 466 467 while ( (rend < reqs_length) && 468 (mlen + sizeof (struct TALER_CRYPTO_CsSignRequestMessage) 469 < UINT16_MAX) ) 470 { 471 mlen += sizeof (struct TALER_CRYPTO_CsSignRequestMessage); 472 rend++; 473 } 474 { 475 char obuf[mlen] GNUNET_ALIGN; 476 struct TALER_CRYPTO_BatchSignRequest *bsr 477 = (struct TALER_CRYPTO_BatchSignRequest *) obuf; 478 void *wbuf; 479 480 bsr->header.type = htons (TALER_HELPER_CS_MT_REQ_BATCH_SIGN); 481 bsr->header.size = htons (mlen); 482 bsr->batch_size = htonl (rend - rpos); 483 wbuf = &bsr[1]; 484 for (unsigned int i = rpos; i<rend; i++) 485 { 486 struct TALER_CRYPTO_CsSignRequestMessage *csm = wbuf; 487 const struct TALER_CRYPTO_CsSignRequest *csr = &reqs[i]; 488 489 csm->header.size = htons (sizeof (*csm)); 490 csm->header.type = htons (TALER_HELPER_CS_MT_REQ_SIGN); 491 csm->for_melt = htonl (for_melt ? 1 : 0); 492 csm->h_cs = *csr->h_cs; 493 csm->message = *csr->blinded_planchet; 494 wbuf += sizeof (*csm); 495 } 496 GNUNET_assert (wbuf == &obuf[mlen]); 497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 498 "Sending batch request [%u-%u)\n", 499 rpos, 500 rend); 501 if (GNUNET_OK != 502 TALER_crypto_helper_send_all (dh->sock, 503 obuf, 504 sizeof (obuf))) 505 { 506 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 507 "send"); 508 do_disconnect (dh); 509 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 510 } 511 } /* end of obuf scope */ 512 rpos = rend; 513 { 514 char buf[UINT16_MAX]; 515 size_t off = 0; 516 const struct GNUNET_MessageHeader *hdr 517 = (const struct GNUNET_MessageHeader *) buf; 518 bool finished = false; 519 520 while (1) 521 { 522 uint16_t msize; 523 ssize_t ret; 524 525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 526 "Awaiting reply at %u (up to %u)\n", 527 wpos, 528 rend); 529 ret = recv (dh->sock, 530 &buf[off], 531 sizeof (buf) - off, 532 (finished && (0 == off)) 533 ? MSG_DONTWAIT 534 : 0); 535 if (ret < 0) 536 { 537 if (EINTR == errno) 538 continue; 539 if (EAGAIN == errno) 540 { 541 GNUNET_assert (finished); 542 GNUNET_assert (0 == off); 543 break; 544 } 545 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 546 "recv"); 547 do_disconnect (dh); 548 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 549 } 550 if (0 == ret) 551 { 552 GNUNET_break (0 == off); 553 if (! finished) 554 return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG; 555 if (TALER_EC_NONE == ec) 556 break; 557 return ec; 558 } 559 off += ret; 560 more: 561 if (off < sizeof (struct GNUNET_MessageHeader)) 562 continue; 563 msize = ntohs (hdr->size); 564 if (msize < sizeof (struct GNUNET_MessageHeader)) 565 { 566 GNUNET_break_op (0); 567 do_disconnect (dh); 568 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 569 } 570 if (off < msize) 571 continue; 572 switch (ntohs (hdr->type)) 573 { 574 case TALER_HELPER_CS_MT_RES_SIGNATURE: 575 if (msize != sizeof (struct TALER_CRYPTO_SignResponse)) 576 { 577 GNUNET_break_op (0); 578 do_disconnect (dh); 579 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 580 } 581 if (finished) 582 { 583 GNUNET_break_op (0); 584 do_disconnect (dh); 585 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 586 } 587 { 588 const struct TALER_CRYPTO_SignResponse *sr = 589 (const struct TALER_CRYPTO_SignResponse *) buf; 590 struct GNUNET_CRYPTO_BlindedSignature *blinded_sig; 591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 592 "Received %u signature\n", 593 wpos); 594 blinded_sig = GNUNET_new (struct GNUNET_CRYPTO_BlindedSignature); 595 blinded_sig->cipher = GNUNET_CRYPTO_BSA_CS; 596 blinded_sig->rc = 1; 597 blinded_sig->details.blinded_cs_answer.b = ntohl (sr->b); 598 blinded_sig->details.blinded_cs_answer.s_scalar = sr->cs_answer; 599 600 bss[wpos].blinded_sig = blinded_sig; 601 wpos++; 602 if (wpos == rend) 603 { 604 if (TALER_EC_INVALID == ec) 605 ec = TALER_EC_NONE; 606 finished = true; 607 } 608 break; 609 } 610 611 case TALER_HELPER_CS_MT_RES_SIGN_FAILURE: 612 if (msize != sizeof (struct TALER_CRYPTO_SignFailure)) 613 { 614 GNUNET_break_op (0); 615 do_disconnect (dh); 616 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 617 } 618 if (finished) 619 { 620 /* more replies than requests in this batch */ 621 GNUNET_break_op (0); 622 do_disconnect (dh); 623 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 624 } 625 { 626 const struct TALER_CRYPTO_SignFailure *sf = 627 (const struct TALER_CRYPTO_SignFailure *) buf; 628 629 ec = (enum TALER_ErrorCode) (int) ntohl (sf->ec); 630 if (TALER_EC_NONE == ec) 631 { 632 /* a failure must not pass for success */ 633 GNUNET_break_op (0); 634 ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 635 } 636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 637 "Signing %u failed with status %d!\n", 638 wpos, 639 ec); 640 wpos++; 641 if (wpos == rend) 642 { 643 finished = true; 644 } 645 break; 646 } 647 case TALER_HELPER_CS_MT_AVAIL: 648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 649 "Received new key!\n"); 650 if (GNUNET_OK != 651 handle_mt_avail (dh, 652 hdr)) 653 { 654 GNUNET_break_op (0); 655 do_disconnect (dh); 656 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 657 } 658 break; /* while(1) loop ensures we recvfrom() again */ 659 case TALER_HELPER_CS_MT_PURGE: 660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 661 "Received revocation!\n"); 662 if (GNUNET_OK != 663 handle_mt_purge (dh, 664 hdr)) 665 { 666 GNUNET_break_op (0); 667 do_disconnect (dh); 668 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 669 } 670 break; /* while(1) loop ensures we recvfrom() again */ 671 case TALER_HELPER_CS_SYNCED: 672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 673 "Synchronized add odd time with CS helper!\n"); 674 dh->synced = true; 675 break; 676 default: 677 GNUNET_break_op (0); 678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 679 "Received unexpected message of type %u\n", 680 ntohs (hdr->type)); 681 do_disconnect (dh); 682 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 683 } 684 memmove (buf, 685 &buf[msize], 686 off - msize); 687 off -= msize; 688 goto more; 689 } /* while(1) */ 690 } /* scope */ 691 } /* while (rpos < cdrs_length) */ 692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 693 "Existing with %u signatures and status %d\n", 694 wpos, 695 ec); 696 return ec; 697 } 698 699 700 enum TALER_ErrorCode 701 TALER_CRYPTO_helper_cs_r_batch_derive ( 702 struct TALER_CRYPTO_CsDenominationHelper *dh, 703 unsigned int cdrs_length, 704 const struct TALER_CRYPTO_CsDeriveRequest cdrs[static cdrs_length], 705 bool for_melt, 706 struct GNUNET_CRYPTO_CSPublicRPairP crps[static cdrs_length]) 707 { 708 enum TALER_ErrorCode ec = TALER_EC_INVALID; 709 unsigned int rpos; 710 unsigned int rend; 711 unsigned int wpos; 712 713 memset (crps, 714 0, 715 sizeof (*crps) * cdrs_length); 716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 717 "Starting R derivation process\n"); 718 if (GNUNET_OK != 719 try_connect (dh)) 720 { 721 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 722 "Failed to connect to helper\n"); 723 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 724 } 725 726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 727 "Requesting %u R pairs\n", 728 cdrs_length); 729 rpos = 0; 730 rend = 0; 731 wpos = 0; 732 while (rpos < cdrs_length) 733 { 734 unsigned int mlen = sizeof (struct TALER_CRYPTO_BatchDeriveRequest); 735 736 while ( (rend < cdrs_length) && 737 (mlen + sizeof (struct TALER_CRYPTO_CsRDeriveRequest) 738 < UINT16_MAX) ) 739 { 740 mlen += sizeof (struct TALER_CRYPTO_CsRDeriveRequest); 741 rend++; 742 } 743 { 744 char obuf[mlen] GNUNET_ALIGN; 745 struct TALER_CRYPTO_BatchDeriveRequest *bdr 746 = (struct TALER_CRYPTO_BatchDeriveRequest *) obuf; 747 void *wbuf; 748 749 bdr->header.type = htons (TALER_HELPER_CS_MT_REQ_BATCH_RDERIVE); 750 bdr->header.size = htons (mlen); 751 bdr->batch_size = htonl (rend - rpos); 752 wbuf = &bdr[1]; 753 for (unsigned int i = rpos; i<rend; i++) 754 { 755 struct TALER_CRYPTO_CsRDeriveRequest *rdr = wbuf; 756 const struct TALER_CRYPTO_CsDeriveRequest *cdr = &cdrs[i]; 757 758 rdr->header.size = htons (sizeof (*rdr)); 759 rdr->header.type = htons (TALER_HELPER_CS_MT_REQ_RDERIVE); 760 rdr->for_melt = htonl (for_melt ? 1 : 0); 761 rdr->h_cs = *cdr->h_cs; 762 rdr->nonce = *cdr->nonce; 763 wbuf += sizeof (*rdr); 764 } 765 GNUNET_assert (wbuf == &obuf[mlen]); 766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 767 "Sending batch request [%u-%u)\n", 768 rpos, 769 rend); 770 if (GNUNET_OK != 771 TALER_crypto_helper_send_all (dh->sock, 772 obuf, 773 sizeof (obuf))) 774 { 775 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 776 "send"); 777 do_disconnect (dh); 778 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 779 } 780 } /* end of obuf scope */ 781 rpos = rend; 782 { 783 char buf[UINT16_MAX]; 784 size_t off = 0; 785 const struct GNUNET_MessageHeader *hdr 786 = (const struct GNUNET_MessageHeader *) buf; 787 bool finished = false; 788 789 while (1) 790 { 791 uint16_t msize; 792 ssize_t ret; 793 794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 795 "Awaiting reply at %u (up to %u)\n", 796 wpos, 797 rend); 798 ret = recv (dh->sock, 799 &buf[off], 800 sizeof (buf) - off, 801 (finished && (0 == off)) 802 ? MSG_DONTWAIT 803 : 0); 804 if (ret < 0) 805 { 806 if (EINTR == errno) 807 continue; 808 if (EAGAIN == errno) 809 { 810 GNUNET_assert (finished); 811 GNUNET_assert (0 == off); 812 break; 813 } 814 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, 815 "recv"); 816 do_disconnect (dh); 817 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_UNAVAILABLE; 818 } 819 if (0 == ret) 820 { 821 GNUNET_break (0 == off); 822 if (! finished) 823 return TALER_EC_EXCHANGE_SIGNKEY_HELPER_BUG; 824 if (TALER_EC_NONE == ec) 825 break; 826 return ec; 827 } 828 off += ret; 829 more: 830 if (off < sizeof (struct GNUNET_MessageHeader)) 831 continue; 832 msize = ntohs (hdr->size); 833 if (msize < sizeof (struct GNUNET_MessageHeader)) 834 { 835 GNUNET_break_op (0); 836 do_disconnect (dh); 837 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 838 } 839 if (off < msize) 840 continue; 841 switch (ntohs (hdr->type)) 842 { 843 case TALER_HELPER_CS_MT_RES_RDERIVE: 844 if (msize != sizeof (struct TALER_CRYPTO_RDeriveResponse)) 845 { 846 GNUNET_break_op (0); 847 do_disconnect (dh); 848 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 849 } 850 if (finished) 851 { 852 GNUNET_break_op (0); 853 do_disconnect (dh); 854 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 855 } 856 { 857 const struct TALER_CRYPTO_RDeriveResponse *rdr = 858 (const struct TALER_CRYPTO_RDeriveResponse *) buf; 859 860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 861 "Received %u R pair\n", 862 wpos); 863 crps[wpos] = rdr->r_pub; 864 wpos++; 865 if (wpos == rend) 866 { 867 if (TALER_EC_INVALID == ec) 868 ec = TALER_EC_NONE; 869 finished = true; 870 } 871 break; 872 } 873 case TALER_HELPER_CS_MT_RES_RDERIVE_FAILURE: 874 if (msize != sizeof (struct TALER_CRYPTO_RDeriveFailure)) 875 { 876 GNUNET_break_op (0); 877 do_disconnect (dh); 878 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 879 } 880 if (finished) 881 { 882 /* more replies than requests in this batch */ 883 GNUNET_break_op (0); 884 do_disconnect (dh); 885 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 886 } 887 { 888 const struct TALER_CRYPTO_RDeriveFailure *rdf = 889 (const struct TALER_CRYPTO_RDeriveFailure *) buf; 890 891 ec = (enum TALER_ErrorCode) (int) ntohl (rdf->ec); 892 if (TALER_EC_NONE == ec) 893 { 894 /* a failure must not pass for success */ 895 GNUNET_break_op (0); 896 ec = TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 897 } 898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 899 "R derivation %u failed with status %d!\n", 900 wpos, 901 ec); 902 wpos++; 903 if (wpos == rend) 904 { 905 finished = true; 906 } 907 break; 908 } 909 case TALER_HELPER_CS_MT_AVAIL: 910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 911 "Received new key!\n"); 912 if (GNUNET_OK != 913 handle_mt_avail (dh, 914 hdr)) 915 { 916 GNUNET_break_op (0); 917 do_disconnect (dh); 918 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 919 } 920 break; /* while(1) loop ensures we recvfrom() again */ 921 case TALER_HELPER_CS_MT_PURGE: 922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 923 "Received revocation!\n"); 924 if (GNUNET_OK != 925 handle_mt_purge (dh, 926 hdr)) 927 { 928 GNUNET_break_op (0); 929 do_disconnect (dh); 930 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 931 } 932 break; /* while(1) loop ensures we recvfrom() again */ 933 case TALER_HELPER_CS_SYNCED: 934 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 935 "Synchronized add odd time with CS helper!\n"); 936 dh->synced = true; 937 break; 938 default: 939 GNUNET_break_op (0); 940 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 941 "Received unexpected message of type %u\n", 942 ntohs (hdr->type)); 943 do_disconnect (dh); 944 return TALER_EC_EXCHANGE_DENOMINATION_HELPER_BUG; 945 } 946 memmove (buf, 947 &buf[msize], 948 off - msize); 949 off -= msize; 950 goto more; 951 } /* while(1) */ 952 } /* scope */ 953 } /* while (rpos < cdrs_length) */ 954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 955 "Existing with %u signatures and status %d\n", 956 wpos, 957 ec); 958 return ec; 959 } 960 961 962 void 963 TALER_CRYPTO_helper_cs_disconnect ( 964 struct TALER_CRYPTO_CsDenominationHelper *dh) 965 { 966 if (-1 != dh->sock) 967 do_disconnect (dh); 968 GNUNET_free (dh); 969 } 970 971 972 /* end of crypto_helper_cs.c */