summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKen Raeburn <raeburn@mit.edu>2001-03-03 22:02:00 +0000
committerKen Raeburn <raeburn@mit.edu>2001-03-03 22:02:00 +0000
commit4531ba725b82df5c1232d763eed82930b09ec234 (patch)
treeb1d67f651d5b3a35e1c0f012cf75ea6fd7cf563b /src
parent1d04f82647095502e00afca300c5db8c8e0a788d (diff)
downloadkrb5-4531ba725b82df5c1232d763eed82930b09ec234.tar.gz
krb5-4531ba725b82df5c1232d763eed82930b09ec234.tar.xz
krb5-4531ba725b82df5c1232d763eed82930b09ec234.zip
init_os_ctx.c: On UNIX, seed PRNG with data from /dev/[u]random if available
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@13050 dc483132-0cff-0310-8789-dd5450dbe970
Diffstat (limited to 'src')
-rw-r--r--src/lib/krb5/os/ChangeLog7
-rw-r--r--src/lib/krb5/os/init_os_ctx.c41
2 files changed, 48 insertions, 0 deletions
diff --git a/src/lib/krb5/os/ChangeLog b/src/lib/krb5/os/ChangeLog
index 92b42a6b2e..9fcc89b739 100644
--- a/src/lib/krb5/os/ChangeLog
+++ b/src/lib/krb5/os/ChangeLog
@@ -1,3 +1,10 @@
+2001-03-03 Ken Raeburn <raeburn@mit.edu>
+
+ * init_os_ctx.c: If not Mac or Windows, define USE_RANDOM_DEVICE
+ and include sys/ioctl.h.
+ (krb5_os_init_context) [USE_RANDOM_DEVICE]: Read some bytes from
+ /dev/urandom or /dev/random and use them to re-seed the PRNG.
+
2001-02-05 Tom Yu <tlyu@mit.edu>
* prompter.c (krb5_prompter_posix): Fix up terminal modes if we're
diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c
index 4168dfe463..f72d643f10 100644
--- a/src/lib/krb5/os/init_os_ctx.c
+++ b/src/lib/krb5/os/init_os_ctx.c
@@ -33,6 +33,11 @@
#include <PreferencesLib.h>
#endif /* macintosh */
+#if !defined(macintosh) && !defined(_MSDOS) && !defined(_WIN32)
+#define USE_RANDOM_DEVICE
+#include <sys/ioctl.h> /* for FIONBIO */
+#endif
+
#if defined(_MSDOS) || defined(_WIN32)
static krb5_error_code
@@ -443,6 +448,42 @@ krb5_os_init_context(ctx)
os_ctx->default_ccname = 0;
os_ctx->default_ccprincipal = 0;
+#ifdef USE_RANDOM_DEVICE
+ /* If the OS provides us with random or even pseudorandom
+ data, use it. It'll be far better than what we've got
+ above, but not all systems provide it (yet).
+
+ Typical setup seems to be /dev/urandom is pseudo-random, as
+ good as the system can provide, but never blocking;
+ /dev/random is supposedly truly random (the metrics are
+ sometimes suspect), and may block. */
+ {
+ char rndbytes[128];
+ krb5_data seed;
+ int tmp;
+
+ tmp = open ("/dev/urandom", O_RDONLY);
+ if (tmp == -1) {
+ int dontblock = 1;
+ tmp = open ("/dev/random", O_RDONLY);
+ (void) ioctl (tmp, FIONBIO, (char *) &dontblock);
+ }
+ if (tmp != -1) {
+ /* If this doesn't work, should we continue or
+ report an error that'll cause all Kerberos
+ programs to fail? */
+ (void) read (tmp, &rndbytes, sizeof (rndbytes));
+ close (tmp);
+ }
+ /* Okay. Take whatever we've got, and seed the
+ PRNG. */
+ seed.length = sizeof(rndbytes);
+ seed.data = (char *) &rndbytes;
+ if ((retval = krb5_c_random_seed(ctx, &seed)))
+ return retval;
+ }
+#endif
+
krb5_cc_set_default_name(ctx, NULL);
retval = os_init_paths(ctx);