CVS: tmda/bin ChangeLog,1.293,1.294 tmda-keygen,1.20,1.21

"Jason R. Mastaler" <[email protected]> Fri, 05 Mar 2004 15:21:50 -0800
Newsgroups gmane.mail.spam.tmda.cvs
Message-ID <[email protected]>
Update of /cvsroot/tmda/tmda/bin
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv25335/bin

Modified Files:
	ChangeLog tmda-keygen 
Log Message:
A random number device such as /dev/urandom is now
required by tmda-keygen.  The fallback method of 
gathering entropy from system commands was too 
platform dependent -- i.e, on Windows this was completely
broken as of course those commands were unavailable.

This is unlikely to affect many users anyway as random
number devices are now extremely prevolent.  Alternatives
are provided in UPGRADE as well as in tmda-keygen's output.



Index: ChangeLog
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/ChangeLog,v
retrieving revision 1.293
retrieving revision 1.294
diff -u -r1.293 -r1.294
--- ChangeLog	5 Mar 2004 01:46:33 -0000	1.293
+++ ChangeLog	5 Mar 2004 23:21:47 -0000	1.294
@@ -1,3 +1,7 @@
+2004-03-05    <[email protected]>
+
+	* tmda-keygen (keygen): /dev/urandom or equivalent is now required.
+	
 2004-03-04  Jason R. Mastaler  <[email protected]>
 
 	* tmda-rfilter (autorespond_to_sender): Look for 'X-List'.

Index: tmda-keygen
===================================================================
RCS file: /cvsroot/tmda/tmda/bin/tmda-keygen,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- tmda-keygen	1 Jan 2004 23:22:19 -0000	1.20
+++ tmda-keygen	5 Mar 2004 23:21:48 -0000	1.21
@@ -43,7 +43,6 @@
 
 
 import binascii
-import commands
 import getopt
 import os
 import sys
@@ -59,7 +58,7 @@
 from TMDA import Version
 
 
-batch = None
+batch = False
 randomdev = '/dev/urandom'
 
 program = sys.argv[0]
@@ -91,46 +90,23 @@
     if opt in ('-d', '--device'):
         randomdev = arg
     elif opt in ('-b', '--batch'):
-        batch = 1
+        batch = True
 
 
 def keygen():
-    # Use the kernel's random number generator if available.
+    # Use the kernel's random number generator.
     if os.path.exists(randomdev):
         key = open(randomdev,'rb').read(20)
+        return binascii.hexlify(key)
+    #elif sys.platform == 'win32':
+        # The Windows equivalent of polling /dev/urandom is to call
+        # the Windows Crypto/API function "CryptGenRandom"
+        # (http://tinyurl.com/4z0q).  At present there is no wrapper
+        # for this in the Python standard library on Windows so we
+        # can't utilize it.
     else:
-        # Otherwise generate some pseudo-random data from the system
-        # and use the SHA of resulting key as the key.
-        import sha
-        if not batch:
-            # Warn user that use of a cryptographic random number
-            # generator is preferred.
-            warning = ("key generation on a system without a "
-                       + randomdev + " device is not recommended!")
-            print "WARNING:"
-            print '*' * len(warning)
-            print warning
-            print '*' * len(warning)
-            print
-        unpredictable = ( "date",
-                          "fstat",
-                          "iostat",
-                          "vmstat",
-                          "finger",
-                          "ps -la",
-                          "netstat",
-                          "uname -a",
-                          "cat /etc/passwd",
-                          "cat /etc/aliases",
-                          "cat /proc/interrupts" )
-        key_data = ''
-        for i in unpredictable:
-            if commands.getstatusoutput(i)[0] == 0:
-                key_data = key_data + os.popen(i).read()
-        key = sha.new(key_data + "key").digest()
-    return binascii.hexlify(key)
-
-
+        return None
+    
 def main():
     
     if not batch:
@@ -139,6 +115,20 @@
 
     key = keygen()
 
+    if key is None:
+        # Use of a cryptographic random number generator is required.
+        warning = ("key generation on a system without a "
+                   + randomdev + " device is not supported!")
+        print "WARNING:"
+        print '*' * len(warning)
+        print warning
+        print
+        print "specify a different random number device with the '-d' option,"
+        print "or use http://tmda.net/cgi-bin/tmda-keygen if your system lacks one."
+        print '*' * len(warning)
+        print "exiting!"
+        sys.exit()
+        
     if len(key) != 40:
         print "Oops, generated key is not 40-characters long, exiting!"
         sys.exit()