aboutsummaryrefslogtreecommitdiff
path: root/src/util/crypto_rsa.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-09-17 10:45:23 +0000
committerChristian Grothoff <christian@grothoff.org>2012-09-17 10:45:23 +0000
commit7e065c18499688141eb68513058131a49344cac1 (patch)
treee441b44c8f0db8a4f214775e4945039cc820cf2f /src/util/crypto_rsa.c
parentb3ad920b6e0107c3da946fe1f2f720955dbac151 (diff)
downloadgnunet-7e065c18499688141eb68513058131a49344cac1.tar.gz
gnunet-7e065c18499688141eb68513058131a49344cac1.zip
fixing #1551/#2503
Diffstat (limited to 'src/util/crypto_rsa.c')
-rw-r--r--src/util/crypto_rsa.c46
1 files changed, 38 insertions, 8 deletions
diff --git a/src/util/crypto_rsa.c b/src/util/crypto_rsa.c
index 4afda1f6e..8843464a2 100644
--- a/src/util/crypto_rsa.c
+++ b/src/util/crypto_rsa.c
@@ -629,11 +629,17 @@ try_read_key (const char *filename)
629 (void) GNUNET_DISK_file_close (fd); 629 (void) GNUNET_DISK_file_close (fd);
630 return NULL; 630 return NULL;
631 } 631 }
632 if (0 == fs)
633 {
634 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
635 return NULL;
636 }
632 if (fs > UINT16_MAX) 637 if (fs > UINT16_MAX)
633 { 638 {
634 LOG (GNUNET_ERROR_TYPE_ERROR, 639 LOG (GNUNET_ERROR_TYPE_ERROR,
635 _("File `%s' does not contain a valid private key. Deleting it.\n"), 640 _("File `%s' does not contain a valid private key (too long, %llu bytes). Deleting it.\n"),
636 filename); 641 filename,
642 (unsigned long long) fs);
637 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd)); 643 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
638 if (0 != UNLINK (filename)) 644 if (0 != UNLINK (filename))
639 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); 645 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
@@ -648,8 +654,9 @@ try_read_key (const char *filename)
648 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len)))) 654 (NULL == (ret = GNUNET_CRYPTO_rsa_decode_key ((char *) enc, len))))
649 { 655 {
650 LOG (GNUNET_ERROR_TYPE_ERROR, 656 LOG (GNUNET_ERROR_TYPE_ERROR,
651 _("File `%s' does not contain a valid private key. Deleting it.\n"), 657 _("File `%s' does not contain a valid private key (failed decode, %llu bytes). Deleting it.\n"),
652 filename); 658 filename,
659 (unsigned long long) fs);
653 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd)); 660 GNUNET_break (GNUNET_OK == GNUNET_DISK_file_close (fd));
654 if (0 != UNLINK (filename)) 661 if (0 != UNLINK (filename))
655 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename); 662 LOG_STRERROR_FILE (GNUNET_ERROR_TYPE_WARNING, "unlink", filename);
@@ -664,6 +671,23 @@ try_read_key (const char *filename)
664 671
665 672
666/** 673/**
674 * Wait for a short time (we're trying to lock a file or want
675 * to give another process a shot at finishing a disk write, etc.).
676 * Sleeps for 100ms (as that should be long enough for virtually all
677 * modern systems to context switch and allow another process to do
678 * some 'real' work).
679 */
680static void
681short_wait ()
682{
683 struct GNUNET_TIME_Relative timeout;
684
685 timeout = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100);
686 GNUNET_NETWORK_socket_select (NULL, NULL, NULL, timeout);
687}
688
689
690/**
667 * Create a new private key by reading it from a file. If the 691 * Create a new private key by reading it from a file. If the
668 * files does not exist, create a new key and write it to the 692 * files does not exist, create a new key and write it to the
669 * file. Caller must free return value. Note that this function 693 * file. Caller must free return value. Note that this function
@@ -723,7 +747,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
723 sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded), 747 sizeof (struct GNUNET_CRYPTO_RsaPrivateKeyBinaryEncoded),
724 GNUNET_YES)) 748 GNUNET_YES))
725 { 749 {
726 sleep (1); 750 short_wait (1);
727 if (0 == ++cnt % 10) 751 if (0 == ++cnt % 10)
728 { 752 {
729 ec = errno; 753 ec = errno;
@@ -781,7 +805,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
781 _ 805 _
782 ("This may be ok if someone is currently generating a hostkey.\n")); 806 ("This may be ok if someone is currently generating a hostkey.\n"));
783 } 807 }
784 sleep (1); 808 short_wait (1);
785 continue; 809 continue;
786 } 810 }
787 if (GNUNET_YES != GNUNET_DISK_file_test (filename)) 811 if (GNUNET_YES != GNUNET_DISK_file_test (filename))
@@ -817,7 +841,7 @@ GNUNET_CRYPTO_rsa_key_create_from_file (const char *filename)
817 _ 841 _
818 ("This may be ok if someone is currently generating a hostkey.\n")); 842 ("This may be ok if someone is currently generating a hostkey.\n"));
819 } 843 }
820 sleep (2); /* wait a bit longer! */ 844 short_wait (1); /* wait a bit longer! */
821 continue; 845 continue;
822 } 846 }
823 break; 847 break;
@@ -996,6 +1020,7 @@ GNUNET_CRYPTO_rsa_key_create_start (const char *filename,
996{ 1020{
997 struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc; 1021 struct GNUNET_CRYPTO_RsaKeyGenerationContext *gc;
998 struct GNUNET_CRYPTO_RsaPrivateKey *pk; 1022 struct GNUNET_CRYPTO_RsaPrivateKey *pk;
1023 const char *weak_random;
999 1024
1000 if (NULL != (pk = try_read_key (filename))) 1025 if (NULL != (pk = try_read_key (filename)))
1001 { 1026 {
@@ -1023,13 +1048,18 @@ GNUNET_CRYPTO_rsa_key_create_start (const char *filename,
1023 GNUNET_free (gc); 1048 GNUNET_free (gc);
1024 return NULL; 1049 return NULL;
1025 } 1050 }
1051 weak_random = NULL;
1052 if (GNUNET_YES ==
1053 GNUNET_CRYPTO_random_is_weak ())
1054 weak_random = "-w";
1026 gc->gnunet_rsa = GNUNET_OS_start_process (GNUNET_NO, 1055 gc->gnunet_rsa = GNUNET_OS_start_process (GNUNET_NO,
1027 GNUNET_OS_INHERIT_STD_ERR, 1056 GNUNET_OS_INHERIT_STD_ERR,
1028 NULL, 1057 NULL,
1029 gc->gnunet_rsa_out, 1058 gc->gnunet_rsa_out,
1030 "gnunet-rsa", 1059 "gnunet-rsa",
1031 "gnunet-rsa", 1060 "gnunet-rsa",
1032 gc->filename, 1061 gc->filename,
1062 weak_random,
1033 NULL); 1063 NULL);
1034 if (NULL == gc->gnunet_rsa) 1064 if (NULL == gc->gnunet_rsa)
1035 { 1065 {