2009-02-04 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / rand.c
index e4bfab72f5b930dc145c10aed48f80f2ceaf3d57..1060e6519f0a23861db4e5f9d151ddb21835358e 100644 (file)
@@ -6,8 +6,8 @@
  *      Patrik Torstensson (p@rxc.se)
  *     Sebastien Pouliot (sebastien@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
- * (C) 2004 Novell (http://www.novell.com)
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
 #include <mono/metadata/object.h>
 #include <mono/metadata/rand.h>
@@ -94,7 +99,22 @@ get_entropy_from_server (const char *path, guchar *buf, int len)
 
 #if defined (PLATFORM_WIN32)
 
-#include <WinCrypt.h>
+#include <windows.h>
+#include <wincrypt.h>
+
+#ifndef PROV_INTEL_SEC
+#define PROV_INTEL_SEC         22
+#endif
+#ifndef CRYPT_VERIFY_CONTEXT
+#define CRYPT_VERIFY_CONTEXT   0xF0000000
+#endif
+
+MonoBoolean
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngOpen (void)
+{
+       /* FALSE == Local (instance) handle for randomness */
+       return FALSE;
+}
 
 gpointer
 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
@@ -106,11 +126,11 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (M
           http://blogs.msdn.com/dangriff/archive/2003/11/19/51709.aspx */
 
        /* We first try to use the Intel PIII RNG if drivers are present */
-       if (!CryptAcquireContext (&provider, NULL, NULL, INTEL_DEF_PROV, CRYPT_VERIFY_CONTEXT)) {
-               /* not a PIII or no drivers available, use default RSA CSP */\r
+       if (!CryptAcquireContext (&provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFY_CONTEXT)) {
+               /* not a PIII or no drivers available, use default RSA CSP */
                if (!CryptAcquireContext (&provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFY_CONTEXT)) {
                        provider = 0;
-                       /* exception will be thrown in managed code */\r
+                       /* exception will be thrown in managed code */
                }
        }
 
@@ -131,9 +151,9 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (M
                }
        }
 
-       return (gpointer) provider;     \r
+       return (gpointer) provider;     
 }
-\r
+
 gpointer
 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry)
 {
@@ -142,21 +162,22 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpo
        guchar *buf = mono_array_addr (arry, guchar, 0);
 
        if (!CryptGenRandom (provider, len, buf)) {
-               CryptReleaseContext (provider);
+               CryptReleaseContext (provider, 0);
                /* we may have lost our context with CryptoAPI, but all hope isn't lost yet! */
-               provider = ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL);
+               provider = (HCRYPTPROV) ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (NULL);
                if (!CryptGenRandom (provider, len, buf)) {
-                       CryptReleaseContext (provider);
+                       CryptReleaseContext (provider, 0);
                        provider = 0;
-                       /* exception will be thrown in managed code */\r
+                       /* exception will be thrown in managed code */
                }
-       } 
+       }
+       return (gpointer) provider;
 }
 
 void
 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpointer handle) 
 {
-       CryptReleaseContext ((HCRYPTPROV) handle);
+       CryptReleaseContext ((HCRYPTPROV) handle, 0);
 }
 
 #else
@@ -166,14 +187,13 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpoint
 #endif
 
 static gboolean egd = FALSE;
+static gint file = -1;
 
-gpointer
-ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
+MonoBoolean
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngOpen (void)
 {
-       gint file = -1;
-
-       if (egd)
-               return -1;
+       if (egd || (file >= 0))
+               return TRUE;
 
 #if defined (NAME_DEV_URANDOM)
        file = open (NAME_DEV_URANDOM, O_RDONLY);
@@ -187,41 +207,51 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (M
        if (file < 0) {
                const char *socket_path = g_getenv("MONO_EGD_SOCKET");
                egd = (socket_path != NULL);
-               return -1;
        }
 
-       /* if required exception will be thrown in managed code */\r
-       return ((file < 0) ? NULL : (gpointer) file);
+       /* TRUE == Global handle for randomness */
+       return TRUE;
+}
+
+gpointer
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
+{
+       /* if required exception will be thrown in managed code */
+       return ((!egd && (file < 0)) ? NULL : GINT_TO_POINTER (file));
 }
 
 gpointer 
 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry)
 {
-       gint file = (gint) handle;
+       gint file = GPOINTER_TO_INT (handle);
        guint32 len = mono_array_length (arry);
        guchar *buf = mono_array_addr (arry, guchar, 0);
 
        if (egd) {
-               const char *socket_path = getenv ("MONO_EGD_SOCKET");
+               const char *socket_path = g_getenv ("MONO_EGD_SOCKET");
                /* exception will be thrown in managed code */
                if (socket_path == NULL)
                        return NULL; 
                get_entropy_from_server (socket_path, mono_array_addr (arry, guchar, 0), mono_array_length (arry));
-               return -1;
-       }
-       else {
+               return (gpointer) -1;
+       } else {
                /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
                gint count = 0;
                gint err;
 
                do {
                        err = read (file, buf + count, len - count);
+                       if (err < 0) {
+                               if (errno == EINTR)
+                                       continue;
+                               break;
+                       }
                        count += err;
-               } while (err >= 0 && count < len);
+               } while (count < len);
 
                if (err < 0) {
                        g_warning("Entropy error! Error in read (%s).", strerror (errno));
-                       /* exception will be thrown in managed code */\r
+                       /* exception will be thrown in managed code */
                        return NULL;
                }
        }
@@ -234,8 +264,6 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpo
 void
 ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpointer handle) 
 {
-       if (!egd)
-               close ((gint) handle);
 }
 
 #endif /* OS definition */