diff options
Diffstat (limited to 'src/util/crypto_rsa.c')
-rw-r--r-- | src/util/crypto_rsa.c | 64 |
1 files changed, 43 insertions, 21 deletions
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c index cbd9f8f37..c3e0a7833 100644 --- a/src/util/crypto_rsa.c +++ b/src/util/crypto_rsa.c | |||
@@ -571,9 +571,13 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
571 | return NULL; | 571 | return NULL; |
572 | while (GNUNET_YES != GNUNET_DISK_file_test (filename)) | 572 | while (GNUNET_YES != GNUNET_DISK_file_test (filename)) |
573 | { | 573 | { |
574 | fd = GNUNET_DISK_file_open (filename, | 574 | fd = GNUNET_DISK_file_open (filename, |
575 | GNUNET_DISK_OPEN_WRITE | GNUNET_DISK_OPEN_CREATE | GNUNET_DISK_OPEN_FAILIFEXISTS, | 575 | GNUNET_DISK_OPEN_WRITE | |
576 | GNUNET_DISK_PERM_USER_READ| GNUNET_DISK_PERM_USER_WRITE | GNUNET_DISK_PERM_GROUP_READ); | 576 | GNUNET_DISK_OPEN_CREATE | |
577 | GNUNET_DISK_OPEN_FAILIFEXISTS, | ||
578 | GNUNET_DISK_PERM_USER_READ | | ||
579 | GNUNET_DISK_PERM_USER_WRITE | | ||
580 | GNUNET_DISK_PERM_GROUP_READ); | ||
577 | if (NULL == fd) | 581 | if (NULL == fd) |
578 | { | 582 | { |
579 | if (errno == EEXIST) | 583 | if (errno == EEXIST) |
@@ -584,7 +588,11 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
584 | } | 588 | } |
585 | cnt = 0; | 589 | cnt = 0; |
586 | 590 | ||
587 | while (GNUNET_YES != GNUNET_DISK_file_lock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded), GNUNET_YES)) | 591 | while (GNUNET_YES != |
592 | GNUNET_DISK_file_lock (fd, 0, | ||
593 | sizeof (struct | ||
594 | RsaPrivateKeyBinaryEncoded), | ||
595 | GNUNET_YES)) | ||
588 | { | 596 | { |
589 | sleep (1); | 597 | sleep (1); |
590 | if (0 == ++cnt % 10) | 598 | if (0 == ++cnt % 10) |
@@ -602,13 +610,17 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
602 | GNUNET_assert (ret != NULL); | 610 | GNUNET_assert (ret != NULL); |
603 | enc = rsa_encode_key (ret); | 611 | enc = rsa_encode_key (ret); |
604 | GNUNET_assert (enc != NULL); | 612 | GNUNET_assert (enc != NULL); |
605 | GNUNET_assert (ntohs (enc->len) == GNUNET_DISK_file_write (fd, enc, ntohs (enc->len))); | 613 | GNUNET_assert (ntohs (enc->len) == |
614 | GNUNET_DISK_file_write (fd, enc, ntohs (enc->len))); | ||
606 | GNUNET_free (enc); | 615 | GNUNET_free (enc); |
607 | 616 | ||
608 | GNUNET_DISK_file_sync (fd); | 617 | GNUNET_DISK_file_sync (fd); |
609 | if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded))) | 618 | if (GNUNET_YES != |
610 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 619 | GNUNET_DISK_file_unlock (fd, 0, |
611 | "fcntl", filename); | 620 | sizeof (struct |
621 | RsaPrivateKeyBinaryEncoded))) | ||
622 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", | ||
623 | filename); | ||
612 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); | 624 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); |
613 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | 625 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, |
614 | _("Stored new private key in `%s'.\n"), filename); | 626 | _("Stored new private key in `%s'.\n"), filename); |
@@ -616,7 +628,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
616 | } | 628 | } |
617 | /* hostkey file exists already, read it! */ | 629 | /* hostkey file exists already, read it! */ |
618 | fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, | 630 | fd = GNUNET_DISK_file_open (filename, GNUNET_DISK_OPEN_READ, |
619 | GNUNET_DISK_PERM_NONE); | 631 | GNUNET_DISK_PERM_NONE); |
620 | if (NULL == fd) | 632 | if (NULL == fd) |
621 | { | 633 | { |
622 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); | 634 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, "open", filename); |
@@ -625,7 +637,10 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
625 | cnt = 0; | 637 | cnt = 0; |
626 | while (1) | 638 | while (1) |
627 | { | 639 | { |
628 | if (GNUNET_YES != GNUNET_DISK_file_lock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded), GNUNET_NO)) | 640 | if (GNUNET_YES != |
641 | GNUNET_DISK_file_lock (fd, 0, | ||
642 | sizeof (struct RsaPrivateKeyBinaryEncoded), | ||
643 | GNUNET_NO)) | ||
629 | { | 644 | { |
630 | if (0 == ++cnt % 10) | 645 | if (0 == ++cnt % 10) |
631 | { | 646 | { |
@@ -645,9 +660,12 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
645 | /* eh, what!? File we opened is now gone!? */ | 660 | /* eh, what!? File we opened is now gone!? */ |
646 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 661 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, |
647 | "stat", filename); | 662 | "stat", filename); |
648 | if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded))) | 663 | if (GNUNET_YES != |
649 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 664 | GNUNET_DISK_file_unlock (fd, 0, |
650 | "fcntl", filename); | 665 | sizeof (struct |
666 | RsaPrivateKeyBinaryEncoded))) | ||
667 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", | ||
668 | filename); | ||
651 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); | 669 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fd)); |
652 | 670 | ||
653 | return NULL; | 671 | return NULL; |
@@ -658,9 +676,12 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
658 | { | 676 | { |
659 | /* maybe we got the read lock before the hostkey generating | 677 | /* maybe we got the read lock before the hostkey generating |
660 | process had a chance to get the write lock; give it up! */ | 678 | process had a chance to get the write lock; give it up! */ |
661 | if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded))) | 679 | if (GNUNET_YES != |
662 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, | 680 | GNUNET_DISK_file_unlock (fd, 0, |
663 | "fcntl", filename); | 681 | sizeof (struct |
682 | RsaPrivateKeyBinaryEncoded))) | ||
683 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", | ||
684 | filename); | ||
664 | if (0 == ++cnt % 10) | 685 | if (0 == ++cnt % 10) |
665 | { | 686 | { |
666 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | 687 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, |
@@ -690,7 +711,9 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename) | |||
690 | filename); | 711 | filename); |
691 | } | 712 | } |
692 | GNUNET_free (enc); | 713 | GNUNET_free (enc); |
693 | if (GNUNET_YES != GNUNET_DISK_file_unlock (fd, 0, sizeof (struct RsaPrivateKeyBinaryEncoded))) | 714 | if (GNUNET_YES != |
715 | GNUNET_DISK_file_unlock (fd, 0, | ||
716 | sizeof (struct RsaPrivateKeyBinaryEncoded))) | ||
694 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); | 717 | GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "fcntl", filename); |
695 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); | 718 | GNUNET_assert (GNUNET_YES == GNUNET_DISK_file_close (fd)); |
696 | return ret; | 719 | return ret; |
@@ -758,10 +781,9 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, | |||
758 | * @return the size of the decrypted block, -1 on error | 781 | * @return the size of the decrypted block, -1 on error |
759 | */ | 782 | */ |
760 | ssize_t | 783 | ssize_t |
761 | GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey *key, | 784 | GNUNET_CRYPTO_rsa_decrypt (const struct GNUNET_CRYPTO_RsaPrivateKey * key, |
762 | const struct GNUNET_CRYPTO_RsaEncryptedData *block, | 785 | const struct GNUNET_CRYPTO_RsaEncryptedData * |
763 | void *result, | 786 | block, void *result, size_t max) |
764 | size_t max) | ||
765 | { | 787 | { |
766 | gcry_sexp_t resultsexp; | 788 | gcry_sexp_t resultsexp; |
767 | gcry_sexp_t data; | 789 | gcry_sexp_t data; |