New tests.
[mono.git] / mcs / class / corlib / Mono.Security.Cryptography / KeyPairPersistence.cs
old mode 100755 (executable)
new mode 100644 (file)
index c383ec7..ac694bb
@@ -3,10 +3,6 @@
 //
 // Author:
 //     Sebastien Pouliot <sebastien@ximian.com>
-//
-// (C) 2004 Novell (http://www.novell.com)
-//
-
 //
 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
 //
@@ -30,6 +26,8 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#if !MOONLIGHT
+
 using System;
 using System.Globalization;
 using System.IO;
@@ -166,7 +164,7 @@ namespace Mono.Security.Cryptography {
                public bool Load () 
                {
                        // see NOTES
-                       new FileIOPermission (FileIOPermissionAccess.Read, this.Filename).Assert ();
+// FIXME               new FileIOPermission (FileIOPermissionAccess.Read, this.Filename).Assert ();
 
                        bool result = File.Exists (this.Filename);
                        if (result) {
@@ -180,7 +178,7 @@ namespace Mono.Security.Cryptography {
                public void Save () 
                {
                        // see NOTES
-                       new FileIOPermission (FileIOPermissionAccess.Write, this.Filename).Assert ();
+// FIXME               new FileIOPermission (FileIOPermissionAccess.Write, this.Filename).Assert ();
 
                        using (FileStream fs = File.Open (this.Filename, FileMode.Create)) {
                                StreamWriter sw = new StreamWriter (fs, Encoding.UTF8);
@@ -197,7 +195,7 @@ namespace Mono.Security.Cryptography {
                public void Remove () 
                {
                        // see NOTES
-                       new FileIOPermission (FileIOPermissionAccess.Write, this.Filename).Assert ();
+// FIXME               new FileIOPermission (FileIOPermissionAccess.Write, this.Filename).Assert ();
 
                        File.Delete (this.Filename);
                        // it's now possible to change the keypair un the container
@@ -205,10 +203,12 @@ namespace Mono.Security.Cryptography {
 
                // private static stuff
 
+               static object lockobj = new object ();
+               
                private static string UserPath {
                        get {
-                               if ((_userPath == null) || (!_userPathExists)) {
-                                       lock (typeof (KeyPairPersistence)) {
+                               lock (lockobj) {
+                                       if ((_userPath == null) || (!_userPathExists)) {
                                                _userPath = Path.Combine (
                                                        Environment.GetFolderPath (Environment.SpecialFolder.ApplicationData),
                                                        ".mono");
@@ -222,14 +222,16 @@ namespace Mono.Security.Cryptography {
                                                                _userPathExists = true;
                                                        }
                                                        catch (Exception e) {
-                                                               throw new CryptographicException ("Could not create key store.", e);
+                                                               string msg = Locale.GetText ("Could not create user key store '{0}'.");
+                                                               throw new CryptographicException (String.Format (msg, _userPath), e);
                                                        }
                                                }
                                        }
                                }
                                // is it properly protected ?
                                if (!IsUserProtected (_userPath)) {
-                                       throw new CryptographicException ("Improperly protected key pairs.");
+                                       string msg = Locale.GetText ("Improperly protected user's key pairs in '{0}'.");
+                                       throw new CryptographicException (String.Format (msg, _userPath));
                                }
                                return _userPath;
                        }
@@ -237,8 +239,8 @@ namespace Mono.Security.Cryptography {
 
                private static string MachinePath {
                        get {
-                               if ((_machinePath == null) || (!_machinePathExists)) {
-                                       lock (typeof (KeyPairPersistence)) {
+                               lock (lockobj) {
+                                       if ((_machinePath == null) || (!_machinePathExists)) {
                                                _machinePath = Path.Combine (
                                                        Environment.GetFolderPath (Environment.SpecialFolder.CommonApplicationData),
                                                        ".mono");
@@ -252,14 +254,16 @@ namespace Mono.Security.Cryptography {
                                                                _machinePathExists = true;
                                                        }
                                                        catch (Exception e) {
-                                                               throw new CryptographicException ("Could not create key store.", e);
+                                                               string msg = Locale.GetText ("Could not create machine key store '{0}'.");
+                                                               throw new CryptographicException (String.Format (msg, _machinePath), e);
                                                        }
                                                }
                                        }
                                }
                                // is it properly protected ?
                                if (!IsMachineProtected (_machinePath)) {
-                                       throw new CryptographicException ("Improperly protected key pairs.");
+                                       string msg = Locale.GetText ("Improperly protected machine's key pairs in '{0}'.");
+                                       throw new CryptographicException (String.Format (msg, _machinePath));
                                }
                                return _machinePath;
                        }
@@ -317,8 +321,13 @@ namespace Mono.Security.Cryptography {
                private static bool CanSecure (string path) 
                {
                        // we assume POSIX filesystems can always be secured
-                       if ((int) Environment.OSVersion.Platform == 128)
+
+                       // check for Unix platforms - see FAQ for more details
+                       // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
+                       int platform = (int) Environment.OSVersion.Platform;
+                       if ((platform == 4) || (platform == 128) || (platform == 6))
                                return true;
+
                        // while we ask the runtime for Windows OS
                        return _CanSecure (Path.GetPathRoot (path));
                }
@@ -389,7 +398,10 @@ namespace Mono.Security.Cryptography {
                                                // we don't want to trust the key container name as we don't control it
                                                // anyway some characters may not be compatible with the file system
                                                byte[] data = Encoding.UTF8.GetBytes (_params.KeyContainerName);
-                                               MD5 hash = MD5.Create ();       // faster than SHA1, same length as GUID
+                                               // Note: We use MD5 as it is faster than SHA1 and has the same length 
+                                               // as a GUID. Recent problems found in MD5 (like collisions) aren't a
+                                               // problem in this case.
+                                               MD5 hash = MD5.Create ();
                                                byte[] result = hash.ComputeHash (data);
                                                _container = new Guid (result).ToString ();
                                        }
@@ -414,7 +426,7 @@ namespace Mono.Security.Cryptography {
 
                        SecurityElement root = sp.ToXml ();
                        if (root.Tag == "KeyPair") {
-                               SecurityElement prop = root.SearchForChildByTag ("Properties");
+                               //SecurityElement prop = root.SearchForChildByTag ("Properties");
                                SecurityElement keyv = root.SearchForChildByTag ("KeyValue");
                                if (keyv.Children.Count > 0)
                                        _keyvalue = keyv.Children [0].ToString ();
@@ -442,3 +454,6 @@ namespace Mono.Security.Cryptography {
                }
        }
 }
+
+#endif
+