//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
-//
-// (C) 2004 Novell (http://www.novell.com)
-//
-
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
//
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
+#if !MOONLIGHT
+
using System;
using System.Globalization;
using System.IO;
#endif
class KeyPairPersistence {
- private static bool _userPathExists = false; // check at 1st use
+ private static bool _userPathExists; // check at 1st use
private static string _userPath;
- private static bool _machinePathExists = false; // check at 1st use
+ private static bool _machinePathExists; // check at 1st use
private static string _machinePath;
private CspParameters _params;
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) {
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);
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
// 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");
_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;
}
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");
_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;
}
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));
}
// 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 ();
}
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 ();
}
}
}
+
+#endif
+