Skip to contentSkip to conversation

Move create_temp_file() out of #ifdef ENABLE_CRYPTO

PATCH In review Steffan Karger 2 participants 2 messages Jul 25, 2017 – Aug 14, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Move create_temp_file() out of #ifdef ENABLE_CRYPTO

From: Steffan Karger <[email protected]...>

By using get_random() instead of prng_bytes(), we no longer have to place create_temp_file() inside #ifdef ENABLE_CRYPTO.

The resulting filename now has 62 bits of entropy (2 * [0-INT_MAX]) instead of the previous 128 bits, but that should be plenty. Assuming an int is 32 bits, we would need about 2**31 (2147483648) files to have a (roughly) 0.5 chance of failing in one of the 6 attempts we do.

(This is preparing to move the function out of misc.c, where I'd prefer to not have to add a #include "crypto.h".)

---
 src/openvpn/misc.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
src/openvpn/misc.c +4 -8
index 4f729911..1537a2e4 100644
@@ -831,8 +831,6 @@ test_file(const char *filename)
     return ret;
 }
 
-#ifdef ENABLE_CRYPTO
-
 /* create a temporary filename in directory */
 const char *
 create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
@@ -845,15 +843,11 @@ create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
 
     do
     {
-        uint8_t rndbytes[16];
-        const char *rndstr;
-
         ++attempts;
         ++counter;
 
-        prng_bytes(rndbytes, sizeof rndbytes);
-        rndstr = format_hex_ex(rndbytes, sizeof rndbytes, 40, 0, NULL, gc);
-        buf_printf(&fname, PACKAGE "_%s_%s.tmp", prefix, rndstr);
+        buf_printf(&fname, PACKAGE "_%s_%08lx%08lx.tmp", prefix,
+                   (unsigned long) get_random(), (unsigned long) get_random());
 
         retfname = gen_path(directory, BSTR(&fname), gc);
         if (!retfname)
@@ -884,6 +878,8 @@ create_temp_file(const char *directory, const char *prefix, struct gc_arena *gc)
     return NULL;
 }
 
+#ifdef ENABLE_CRYPTO
+
 /*
  * Prepend a random string to hostname to prevent DNS caching.
  * For example, foo.bar.gov would be modified to <random-chars>.foo.bar.gov.
Signature · 1 line
2.11.0
#1 lore raw

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

ACK. This makes sense, code looks good and passes initial tests.

Your patch has been applied to the following branches

commit cd5a74d0d7c6347b31e261e98ca8984819e594df (master) commit a91c38fbabf6f949990ef8a3801d56225a47a33f (release/2.4)

Author: Steffan Karger
Date:   Tue Jul 25 23:02:34 2017 +0200
     Move create_temp_file() out of #ifdef ENABLE_CRYPTO

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: David Sommerseth <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg15146.html
     Signed-off-by: David Sommerseth <[email protected]...>

- --
kind regards,

David Sommerseth

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBCgAGBQJZkatbAAoJEIbPlEyWcf3yjUQP/1yZE+5cYLEdBYrkbv61p1wY f7EG3F29hdCYEYHS1rTvYQbPZFy48YJL5mqvs6IKemQxVIYoEFkCF5KLdYIU6ZlU 1n6Km+yUqeYpU3Tm07Yl+20qCv3+H9FAMJkDjosoh6BIuF8HJD7NsAmDFoY23X2k TXicBKu9zgIHszzFhYQkhQBIBxhpVirZ2r2TqgXOmiz/rbRtmRsHeqFEEcfWv/mT YULxqREKDPTWKHsH/s1JbwoWwrhkzeeYdxoaYs6/qN7zuNj5bmfxfvzJXmtbRlH3 mi/fCtgxq9OJvokUpfSNVoKwf90VGEVy+occziE7KdOmWNCh4QpPBVt27PdloEmc dbbDES7YSndiaHXYoU9I15SP9pGgUI8DmUc9SU9PUPSGSdVG+aRhw4DzY400EQRL jTzdiOZB33xWJivKuxWXM0kWbdu1Nga4Q/aTwJCLaa+Xcuh/mdMZMCuPKfNiRTFZ iU4ZIqSGi84h1pt9azWftI9+7fLIaAsdmwhnQBy9rGz5afzC8rf2TlOmSTYzwdqA fI6TVlu9JosMxDnUtXN5NANRVB79JVYaCVxshCajDBUe4m/j1Ywz5R/PLqMk29zt WBbNdMsVBijJ1DhMFqZDxrBCyKh9OwPHRawdonRU4MD53aXbfBNRR7kCrCAcd3Rt /qnAV7+hg5KxbpF0UrC9
=CjJG
-----END PGP SIGNATURE-----