Moving micasa 1.5 trunk to Novell forge.
This commit is contained in:
15
c_micasad/cache/IKeychain.cs
vendored
Normal file
15
c_micasad/cache/IKeychain.cs
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
namespace sscs.cache
|
||||
{
|
||||
|
||||
interface IKeychain
|
||||
{
|
||||
|
||||
void AddSecret(Secret mySecret);
|
||||
void RemoveSecret(String secretID);
|
||||
Secret GetSecret(string secretID);
|
||||
// IEnumerator getAllSecrets();
|
||||
}
|
||||
|
||||
}
|
||||
14
c_micasad/cache/ISecret.cs
vendored
Normal file
14
c_micasad/cache/ISecret.cs
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
namespace sscs.cache
|
||||
{
|
||||
interface ISecret
|
||||
{
|
||||
//Setter methods
|
||||
void SetValue(byte[] key);
|
||||
void SetKey(String newkey);
|
||||
|
||||
//Get methods
|
||||
byte[] GetValue(String key);
|
||||
string GetKey();
|
||||
}
|
||||
}
|
||||
136
c_micasad/cache/KeyChain.cs
vendored
Normal file
136
c_micasad/cache/KeyChain.cs
vendored
Normal file
@@ -0,0 +1,136 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using sscs.common;
|
||||
|
||||
namespace sscs.cache
|
||||
{
|
||||
|
||||
class KeyChain : IKeychain
|
||||
{
|
||||
//We can have a Arraylist in case we dont need to look up via SecretID
|
||||
// Hashtable has [SecretID(string), Secret(class)]
|
||||
private Hashtable tSecretList = new Hashtable();
|
||||
private Hashtable SecretList; // = Hashtable.Synchronized(tSecretList);
|
||||
|
||||
private string keyChainId;
|
||||
|
||||
private DateTime createdTime;
|
||||
internal DateTime CreatedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return createdTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
createdTime = value;
|
||||
}
|
||||
}
|
||||
private DateTime accessedTime;
|
||||
internal DateTime AccessedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return accessedTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
accessedTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
private DateTime modifiedTime;
|
||||
internal DateTime ModifiedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return modifiedTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
modifiedTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal KeyChain()
|
||||
{
|
||||
SecretList = Hashtable.Synchronized(tSecretList);
|
||||
}
|
||||
|
||||
internal KeyChain(string keyChainId)
|
||||
{
|
||||
SecretList = Hashtable.Synchronized(tSecretList);
|
||||
this.keyChainId = keyChainId;
|
||||
}
|
||||
~KeyChain()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetKey(string value)
|
||||
{
|
||||
keyChainId = value;
|
||||
}
|
||||
|
||||
public void AddSecret(Secret mySecret)
|
||||
{
|
||||
try
|
||||
{
|
||||
mySecret.CreatedTime = DateTime.Now;
|
||||
SecretList.Add((mySecret.GetKey()), mySecret);
|
||||
this.ModifiedTime = DateTime.Now;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public void RemoveSecret(String secretID)
|
||||
{
|
||||
SecretList.Remove(secretID);
|
||||
this.ModifiedTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public void RemoveAllSecrets()
|
||||
{
|
||||
SecretList.Clear();
|
||||
}
|
||||
|
||||
public Secret GetSecret(string secretID)
|
||||
{
|
||||
if(SecretList.ContainsKey(secretID))
|
||||
{
|
||||
Secret secret = (Secret)SecretList[secretID];
|
||||
secret.AccessedTime = DateTime.Now;
|
||||
return secret;
|
||||
}
|
||||
else
|
||||
throw new SecretNotFoundException(secretID);
|
||||
}
|
||||
|
||||
internal string GetKey()
|
||||
{
|
||||
return keyChainId;
|
||||
}
|
||||
|
||||
|
||||
internal IEnumerator GetAllSecrets()
|
||||
{
|
||||
return SecretList.GetEnumerator();
|
||||
}
|
||||
internal int GetNumSecrets()
|
||||
{
|
||||
return SecretList.Count;
|
||||
}
|
||||
|
||||
internal bool CheckIfSecretExists(string id)
|
||||
{
|
||||
if( SecretList.ContainsKey(id) )
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
170
c_micasad/cache/KeyValue.cs
vendored
Normal file
170
c_micasad/cache/KeyValue.cs
vendored
Normal file
@@ -0,0 +1,170 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
using Novell.CASA.MiCasa.Common;
|
||||
|
||||
namespace sscs.cache
|
||||
{
|
||||
public class KeyValue
|
||||
{
|
||||
private string m_key;
|
||||
public string Key
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_key;
|
||||
}
|
||||
set
|
||||
{
|
||||
m_key = value;
|
||||
}
|
||||
}
|
||||
private byte[] m_value;
|
||||
|
||||
public string GetValue()
|
||||
{
|
||||
return (DecryptValue());
|
||||
}
|
||||
|
||||
public void SetValue(string sValue)
|
||||
{
|
||||
|
||||
string oldValue = DecryptValue();
|
||||
|
||||
if (oldValue.Equals(sValue))
|
||||
return;
|
||||
else
|
||||
{
|
||||
byte[] newEncryptedValue = EncryptValue(sValue);
|
||||
m_value = newEncryptedValue;
|
||||
m_modified = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
public string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
// decrypt
|
||||
return (DecryptValue());
|
||||
}
|
||||
set
|
||||
{
|
||||
byte[] newEncrypteValue = EncryptValue(value);
|
||||
|
||||
if ((newEncrypteValue != null) && (newEncrypteValue.Equals(m_value)))
|
||||
return;
|
||||
else
|
||||
{
|
||||
m_value = newEncrypteValue;
|
||||
m_modified = DateTime.Now;
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
private Hashtable mLinkedKeys = null; // = new Hashtable();
|
||||
|
||||
public void AddLink(LinkedKeyInfo linkedInfo)
|
||||
{
|
||||
if (mLinkedKeys == null)
|
||||
mLinkedKeys = new Hashtable();
|
||||
|
||||
if(!mLinkedKeys.ContainsKey(linkedInfo.GetLinkID()))
|
||||
mLinkedKeys.Add(linkedInfo.GetLinkID(), linkedInfo);
|
||||
}
|
||||
|
||||
public void RemoveLink(string sLinkID)
|
||||
{
|
||||
// remove link
|
||||
if (mLinkedKeys != null)
|
||||
{
|
||||
mLinkedKeys.Remove(sLinkID);
|
||||
}
|
||||
}
|
||||
|
||||
public Hashtable GetLinkedKeys()
|
||||
{
|
||||
return mLinkedKeys;
|
||||
}
|
||||
|
||||
|
||||
private DateTime m_created;
|
||||
public DateTime CreatedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_created;
|
||||
}
|
||||
}
|
||||
|
||||
private DateTime m_modified;
|
||||
public DateTime ModifiedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return m_modified;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public KeyValue(string sKey, string sValue)
|
||||
{
|
||||
// NOTE: Order is important, do not change
|
||||
m_created = m_modified = DateTime.Now;
|
||||
m_key = sKey;
|
||||
m_value = EncryptValue(sValue);
|
||||
}
|
||||
|
||||
private byte[] EncryptValue(string sValue)
|
||||
{
|
||||
byte[] baValueClear = Encoding.Default.GetBytes(sValue);
|
||||
return (XORValue(baValueClear));
|
||||
}
|
||||
|
||||
private string DecryptValue()
|
||||
{
|
||||
byte[] baValueClear = XORValue(m_value);
|
||||
return Encoding.Default.GetString(baValueClear);
|
||||
}
|
||||
|
||||
private byte[] XORValue(byte[] baInput)
|
||||
{
|
||||
|
||||
byte[] baOutput = new byte[baInput.Length];
|
||||
Random rand = new Random((int)m_created.Ticks);
|
||||
|
||||
for (int i=0; i<baInput.Length; i++)
|
||||
{
|
||||
baOutput[i] = (byte)((int)baInput[i] ^ rand.Next());
|
||||
}
|
||||
|
||||
return baOutput;
|
||||
}
|
||||
|
||||
public string ToXML()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append("<ENTRY>");
|
||||
|
||||
sb.Append("<KEY>");
|
||||
sb.Append(m_key);
|
||||
sb.Append("</KEY>");
|
||||
|
||||
sb.Append("<VALUE>");
|
||||
sb.Append(m_value);
|
||||
sb.Append("</VALUE>");
|
||||
|
||||
sb.Append("<MODIFIED>");
|
||||
sb.Append(m_modified.Ticks);
|
||||
sb.Append("</MODIFIED>");
|
||||
|
||||
sb.Append("</ENTRY>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
318
c_micasad/cache/Secret.cs
vendored
Normal file
318
c_micasad/cache/Secret.cs
vendored
Normal file
@@ -0,0 +1,318 @@
|
||||
using System;
|
||||
using System.Text;
|
||||
using System.Collections;
|
||||
|
||||
namespace sscs.cache
|
||||
{
|
||||
class Secret : ISecret
|
||||
{
|
||||
private string secretID;
|
||||
//private byte[] data;
|
||||
string ePasswd ; // TBD
|
||||
|
||||
DateTime createdTime;
|
||||
public DateTime CreatedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return createdTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
createdTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
DateTime accessedTime;
|
||||
public DateTime AccessedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return accessedTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
accessedTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
DateTime modifiedTime;
|
||||
public DateTime ModifiedTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return modifiedTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
modifiedTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Hashtable htKeyValues = new Hashtable();
|
||||
internal Secret()
|
||||
{
|
||||
}
|
||||
|
||||
internal Secret(string secretID)
|
||||
{
|
||||
this.secretID = secretID;
|
||||
}
|
||||
|
||||
internal Secret(string secretID, byte[] data)
|
||||
{
|
||||
this.secretID = secretID;
|
||||
//this.data = data;
|
||||
|
||||
// parse the data
|
||||
ParseDataBuffer(data);
|
||||
|
||||
}
|
||||
|
||||
internal Secret(string secretID, byte[] data, string ePasswd)
|
||||
{
|
||||
this.secretID = secretID;
|
||||
this.ePasswd = ePasswd;
|
||||
ParseDataBuffer(data);
|
||||
}
|
||||
|
||||
internal void ParseDataBuffer(byte[] data)
|
||||
{
|
||||
|
||||
if (data != null)
|
||||
{
|
||||
UTF8Encoding dec = new UTF8Encoding();
|
||||
string theData = dec.GetString(data);
|
||||
|
||||
char[] theChars = new char[2];
|
||||
theChars[0] = '\n';
|
||||
theChars[1] = '\0';
|
||||
String[] keyValues = theData.Split(theChars);
|
||||
|
||||
char delimiter = '=';
|
||||
foreach (string keyValue in keyValues)
|
||||
{
|
||||
int iLocation = keyValue.IndexOf(delimiter);
|
||||
if (iLocation > 0)
|
||||
{
|
||||
String key = keyValue.Substring(0, iLocation);
|
||||
String value = keyValue.Substring(iLocation + 1);
|
||||
|
||||
if (key != null && value != null)
|
||||
this.SetKeyValue(key, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal void SetEpasswd(string value)
|
||||
{
|
||||
ePasswd = value;
|
||||
}
|
||||
|
||||
internal string GetEpasswd()
|
||||
{
|
||||
return ePasswd;
|
||||
}
|
||||
|
||||
public void SetValue(byte[] data)
|
||||
{
|
||||
this.ModifiedTime = DateTime.Now;
|
||||
ParseDataBuffer(data);
|
||||
}
|
||||
|
||||
public void SetKey(String newkey)
|
||||
{
|
||||
//Validation TBD
|
||||
this.secretID = newkey;
|
||||
}
|
||||
|
||||
//Get methods
|
||||
public byte[] GetValue(String key)
|
||||
{
|
||||
//validation TBD
|
||||
//if (data == null)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
IDictionaryEnumerator etor = (IDictionaryEnumerator)this.htKeyValues.GetEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
string sKey = (string)etor.Key;
|
||||
KeyValue kv = (KeyValue)this.htKeyValues[sKey];
|
||||
|
||||
sb.Append(kv.Key);
|
||||
sb.Append("=");
|
||||
sb.Append(kv.GetValue());
|
||||
sb.Append('\n');
|
||||
}
|
||||
//sb.Append('\0');
|
||||
|
||||
UTF8Encoding enc = new UTF8Encoding();
|
||||
this.AccessedTime = DateTime.Now;
|
||||
return (enc.GetBytes(sb.ToString()));
|
||||
}
|
||||
//else
|
||||
// return data;
|
||||
}
|
||||
public byte[] GetValue()
|
||||
{
|
||||
//validation TBD
|
||||
this.AccessedTime = DateTime.Now;
|
||||
return GetValue(null);
|
||||
}
|
||||
|
||||
public string GetKey()
|
||||
{
|
||||
return secretID;
|
||||
}
|
||||
public void SetKeyValue(string key, string value)
|
||||
{
|
||||
KeyValue kv;
|
||||
if (htKeyValues.Contains(key))
|
||||
{
|
||||
kv = (KeyValue)htKeyValues[key];
|
||||
kv.SetValue(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
kv = new KeyValue(key, value);
|
||||
htKeyValues.Add(key, kv);
|
||||
}
|
||||
this.ModifiedTime = DateTime.Now;
|
||||
}
|
||||
|
||||
public KeyValue GetKeyValue(string key)
|
||||
{
|
||||
this.AccessedTime = DateTime.Now;
|
||||
if (htKeyValues.Contains(key))
|
||||
{
|
||||
KeyValue kv = (KeyValue)htKeyValues[key];
|
||||
return kv;
|
||||
}
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public void RemoveKeyValue(string key)
|
||||
{
|
||||
if (htKeyValues.Contains(key))
|
||||
{
|
||||
htKeyValues.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime GetKeyValueCreatedTime(string key)
|
||||
{
|
||||
if (htKeyValues.Contains(key))
|
||||
{
|
||||
KeyValue kv = (KeyValue)htKeyValues[key];
|
||||
return kv.CreatedTime;
|
||||
}
|
||||
else
|
||||
return (new DateTime(0));
|
||||
}
|
||||
|
||||
public Hashtable GetLinkedKeys(string keyId)
|
||||
{
|
||||
if (htKeyValues.Contains(keyId))
|
||||
{
|
||||
KeyValue kv = (KeyValue)htKeyValues[keyId];
|
||||
return kv.GetLinkedKeys();
|
||||
}
|
||||
else
|
||||
return null;
|
||||
|
||||
}
|
||||
|
||||
public DateTime GetKeyValueModifiedTime(string key)
|
||||
{
|
||||
if (htKeyValues.Contains(key))
|
||||
{
|
||||
KeyValue kv = (KeyValue)htKeyValues[key];
|
||||
return kv.ModifiedTime;
|
||||
}
|
||||
else
|
||||
return (new DateTime(0));
|
||||
}
|
||||
|
||||
|
||||
public void MergeSecret(Secret newSecret)
|
||||
{
|
||||
IDictionaryEnumerator etor = (IDictionaryEnumerator)newSecret.htKeyValues.GetEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
string sKey = (string)etor.Key;
|
||||
|
||||
// TODO: When we sync, we should consider modified time as well
|
||||
KeyValue newKV = (KeyValue)newSecret.htKeyValues[sKey];
|
||||
this.SetKeyValue(newKV.Key, newKV.GetValue());
|
||||
}
|
||||
etor = (IDictionaryEnumerator)newSecret.htKeyValues.GetEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
string sKey = (string)etor.Key;
|
||||
if(!htKeyValues.Contains(sKey))
|
||||
this.RemoveKeyValue(sKey);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList GetKeyList()
|
||||
{
|
||||
IDictionaryEnumerator etor = (IDictionaryEnumerator)this.htKeyValues.GetEnumerator();
|
||||
ArrayList list = new ArrayList();
|
||||
if( null == etor )
|
||||
{
|
||||
return null;
|
||||
}
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
string key = (string)etor.Key;
|
||||
list.Add(key);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
internal IDictionaryEnumerator GetKeyValueEnumerator()
|
||||
{
|
||||
if( htKeyValues != null)
|
||||
return htKeyValues.GetEnumerator();
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public string ToXML()
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
sb.Append("<SECRET>");
|
||||
|
||||
sb.Append("<ID>");
|
||||
sb.Append(secretID);
|
||||
sb.Append("</ID>");
|
||||
|
||||
sb.Append("<DATA>");
|
||||
// enum the htKeyValues list
|
||||
//IDictionaryEnumerator ienum = htKeyValues.GetEnumerator();
|
||||
ICollection coll = htKeyValues.Values;
|
||||
|
||||
IDictionaryEnumerator ienum = (IDictionaryEnumerator)coll.GetEnumerator();
|
||||
|
||||
|
||||
KeyValue kv = (KeyValue)ienum.Current;
|
||||
while (kv != null)
|
||||
{
|
||||
sb.Append(kv.ToXML());
|
||||
if (ienum.MoveNext())
|
||||
kv = (KeyValue)ienum.Value;
|
||||
else
|
||||
kv = null;
|
||||
}
|
||||
sb.Append("</DATA>");
|
||||
sb.Append("</SECRET>");
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
603
c_micasad/cache/SecretStore.cs
vendored
Normal file
603
c_micasad/cache/SecretStore.cs
vendored
Normal file
@@ -0,0 +1,603 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Threading;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using System.Xml.Serialization;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using sscs.cache;
|
||||
using sscs.common;
|
||||
using sscs.constants;
|
||||
using sscs.lss;
|
||||
using sscs.crypto;
|
||||
|
||||
namespace sscs.cache
|
||||
{
|
||||
class SecretStore
|
||||
{
|
||||
internal string secretStoreName; // User name ?
|
||||
internal int refCount;
|
||||
private uint version;
|
||||
private Hashtable tKeyChainList = new Hashtable();
|
||||
private Hashtable keyChainList; //= Hashtable.Synchronized(tKeyChainList);
|
||||
internal User user;
|
||||
private Mutex ssMutex ; //reqd only for refCount
|
||||
private int state; // Maintains the state of SS ( keychain
|
||||
// type availability). TODO: Convert to a class.
|
||||
|
||||
private static int STATE_NOT_DEFINED = 0;
|
||||
private static int STATE_OK = 1;
|
||||
private static int STATE_LOCKED = 2;
|
||||
|
||||
private LocalStorage lss = null;
|
||||
bool bIsStorePersistent = false;
|
||||
|
||||
private DateTime createTime;
|
||||
public DateTime CreateTime
|
||||
{
|
||||
get
|
||||
{
|
||||
return createTime;
|
||||
}
|
||||
set
|
||||
{
|
||||
createTime = value;
|
||||
}
|
||||
}
|
||||
|
||||
~SecretStore()
|
||||
{
|
||||
ssMutex.Close();
|
||||
}
|
||||
|
||||
internal SecretStore(User ssUser)
|
||||
{
|
||||
secretStoreName = ssUser.GetUserName();
|
||||
version = 1;
|
||||
state = STATE_NOT_DEFINED;
|
||||
user = ssUser;
|
||||
refCount = 0;
|
||||
keyChainList = Hashtable.Synchronized(tKeyChainList);
|
||||
|
||||
ssMutex = new Mutex();
|
||||
}
|
||||
|
||||
internal bool IsStorePersistent()
|
||||
{
|
||||
return bIsStorePersistent;
|
||||
}
|
||||
|
||||
public bool StopPersistence()
|
||||
{
|
||||
if(lss != null && bIsStorePersistent == true)
|
||||
{
|
||||
lss.StopPersistence();
|
||||
lss = null;
|
||||
bIsStorePersistent = false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool IsStoreLocked()
|
||||
{
|
||||
if (state == STATE_LOCKED)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
public void LockStore()
|
||||
{
|
||||
state = STATE_LOCKED;
|
||||
}
|
||||
|
||||
public bool UnlockStore(string sDesktopPassword, string sMasterPassword)
|
||||
{
|
||||
if (sDesktopPassword != null)
|
||||
{
|
||||
// verify Desktop password
|
||||
state = STATE_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
if (sMasterPassword != null)
|
||||
{
|
||||
// verify MasterPassword
|
||||
state = STATE_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
internal bool StartPersistenceByDesktopPasswd(string desktopPasswd)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] baPasscode;
|
||||
/* Persistence could have started because the user
|
||||
* could have set master password.
|
||||
*/
|
||||
if(lss != null && bIsStorePersistent == true)
|
||||
{
|
||||
/* Verify passcode and if validation fails, rewrite
|
||||
* desktop file.
|
||||
*/
|
||||
if(File.Exists(GetPasscodeByDesktopFilePath()))
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Write the desktop passwd file.
|
||||
*/
|
||||
}
|
||||
CSSSLogger.DbgLog(CSSSLogger.GetExecutionPath(this) + " Store is already persistent");
|
||||
return true;
|
||||
}
|
||||
|
||||
if(!File.Exists(GetPasscodeByDesktopFilePath()))
|
||||
{
|
||||
//Else passcode needs to be generated.
|
||||
baPasscode = CASACrypto.GenerateMasterPasscodeUsingString(
|
||||
desktopPasswd,
|
||||
GetPasscodeByDesktopFilePath(),
|
||||
GetValidationFilePath(),
|
||||
user.UserIdentifier);
|
||||
|
||||
if( null == baPasscode )
|
||||
return false;
|
||||
|
||||
if(!File.Exists(GetKeyFilePath()))
|
||||
{
|
||||
RijndaelManaged myRijndael = new RijndaelManaged();
|
||||
byte[] key;
|
||||
byte[] IV = new byte[16];
|
||||
//Create a new key and initialization vector.
|
||||
myRijndael.GenerateKey();
|
||||
key = myRijndael.Key;
|
||||
CASACrypto.StoreKeySetUsingMasterPasscode(key,IV,
|
||||
baPasscode,
|
||||
GetKeyFilePath());
|
||||
}
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
return true;
|
||||
}
|
||||
baPasscode = CASACrypto.GetMasterPasscodeUsingDesktopPasswd(desktopPasswd, GetPasscodeByDesktopFilePath());
|
||||
if(baPasscode != null)
|
||||
{
|
||||
if(CASACrypto.ValidatePasscode(baPasscode,GetValidationFilePath()))
|
||||
{
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
lss = null;
|
||||
bIsStorePersistent = false; //till masterPasswd is verified
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
CSSSLogger.DbgLog(CSSSLogger.GetExecutionPath(this) + " May be desktop passwd has changed");
|
||||
lss = null;
|
||||
bIsStorePersistent = false;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
internal bool SetMasterPassword(string mPasswdFromIDK)
|
||||
{
|
||||
try
|
||||
{
|
||||
char[] trimChars = {'\0'};
|
||||
string mPasswd = mPasswdFromIDK.TrimEnd(trimChars);
|
||||
bool isVerifyOperation = false;
|
||||
string mPasswdFileName = GetPasscodeByMasterPasswdFilePath();
|
||||
byte[] baPasscode;
|
||||
if(File.Exists(mPasswdFileName))
|
||||
isVerifyOperation = true; //else it is a set operation.
|
||||
|
||||
string desktopPasswd = GetDesktopPasswd();
|
||||
|
||||
if(isVerifyOperation == false)
|
||||
{
|
||||
/* Here the master password file needs to be generated.
|
||||
*/
|
||||
if(desktopPasswd != null)
|
||||
{
|
||||
baPasscode = CASACrypto.GetMasterPasscodeUsingDesktopPasswd(desktopPasswd, GetPasscodeByDesktopFilePath());
|
||||
if(CASACrypto.ValidatePasscode(baPasscode,GetValidationFilePath()))
|
||||
{
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(
|
||||
baPasscode,
|
||||
mPasswd,
|
||||
GetPasscodeByMasterPasswdFilePath());
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Probably desktop passwd has changed.
|
||||
//But as even master passwd is being set only now,
|
||||
//the persistent store is lost.
|
||||
|
||||
baPasscode = CASACrypto.GenerateMasterPasscodeUsingString(mPasswd,GetPasscodeByMasterPasswdFilePath(),GetValidationFilePath(), user.UserIdentifier);
|
||||
if(baPasscode != null)
|
||||
{
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(baPasscode,mPasswd,GetPasscodeByMasterPasswdFilePath());
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(baPasscode,desktopPasswd,GetPasscodeByDesktopFilePath());
|
||||
if(File.Exists(GetPersistenceFilePath()))
|
||||
{
|
||||
File.Delete(GetPersistenceFilePath());
|
||||
CSSSLogger.DbgLog("Removing the persistent storeas its meaningless now.");
|
||||
}
|
||||
if( bIsStorePersistent == false )
|
||||
{
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//return true;
|
||||
}//if a valid desktop Passwd is present - if ends here
|
||||
else
|
||||
{
|
||||
/* If desktop passwd is not there and user sets
|
||||
* master password.
|
||||
*/
|
||||
if(File.Exists(GetPersistenceFilePath()))
|
||||
{
|
||||
File.Delete(GetPersistenceFilePath());
|
||||
CSSSLogger.DbgLog("Removing the persistent storeas its meaningless now. - Desktop passwd is not there and Master password is being set");
|
||||
}
|
||||
if(File.Exists((GetPasscodeByDesktopFilePath())))
|
||||
{
|
||||
File.Delete((GetPasscodeByDesktopFilePath()));
|
||||
CSSSLogger.DbgLog("Removing the persistent storeas its meaningless now. - Desktop passwd is not there and Master password is being set");
|
||||
}
|
||||
|
||||
|
||||
baPasscode = CASACrypto.GenerateMasterPasscodeUsingString(mPasswd,GetPasscodeByMasterPasswdFilePath(),GetValidationFilePath(), user.UserIdentifier);
|
||||
if(baPasscode != null)
|
||||
{
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(baPasscode,mPasswd,GetPasscodeByMasterPasswdFilePath());
|
||||
if( bIsStorePersistent == false )
|
||||
{
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}//end of isVerifyOperation == false
|
||||
else
|
||||
{
|
||||
/* Verify the master password. If verified, and if
|
||||
* persistence has not started, start it.
|
||||
*/
|
||||
|
||||
//Get the passcode from master passwd file and validate.
|
||||
//If validation succeeds,start persistence.
|
||||
if(desktopPasswd == null)
|
||||
{
|
||||
baPasscode = CASACrypto.DecryptMasterPasscodeUsingString(mPasswd, GetPasscodeByMasterPasswdFilePath());
|
||||
if(CASACrypto.ValidatePasscode(baPasscode,GetValidationFilePath()))
|
||||
{
|
||||
if(bIsStorePersistent == false)
|
||||
{
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ //There are 2 cases - either desktop passwd has changed
|
||||
//or it hasnt.
|
||||
baPasscode = CASACrypto.GetMasterPasscodeUsingMasterPasswd(mPasswd, GetPasscodeByMasterPasswdFilePath());
|
||||
if(CASACrypto.ValidatePasscode(baPasscode,GetValidationFilePath()))
|
||||
{
|
||||
RewriteDesktopPasswdFile(baPasscode,desktopPasswd);
|
||||
if(bIsStorePersistent == false)
|
||||
{
|
||||
lss = new LocalStorage(this,baPasscode);
|
||||
bIsStorePersistent = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return false;
|
||||
}//End of SetMasterPassword
|
||||
|
||||
internal bool RewriteDesktopPasswdFile(byte[] baPasscode, string desktopPasswd)
|
||||
{
|
||||
try
|
||||
{
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(baPasscode, desktopPasswd, GetPasscodeByDesktopFilePath());
|
||||
CSSSLogger.DbgLog("Re-encryted passcode with desktop passwd");
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
internal byte[] GetPasscodeFromOldDesktopPasswd(string oldDesktopPasswd)
|
||||
{
|
||||
try
|
||||
{
|
||||
byte[] baPasscode = CASACrypto.GetMasterPasscodeUsingDesktopPasswd(oldDesktopPasswd, GetPasscodeByDesktopFilePath());
|
||||
if(CASACrypto.ValidatePasscode(baPasscode,GetValidationFilePath()))
|
||||
{
|
||||
return baPasscode;
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/* This method would be called, when the user is setting his
|
||||
* master passcode for the first time.
|
||||
*/
|
||||
|
||||
internal bool SetMasterPasscode(string sMasterPasscode)
|
||||
{
|
||||
return true;
|
||||
#if false
|
||||
bool bRet = false;
|
||||
try
|
||||
{
|
||||
if(!CASACrypto.CheckIfMasterPasscodeIsAvailable(desktopPasswd, GetPasswdFilePath()))
|
||||
{
|
||||
RijndaelManaged myRijndael = new RijndaelManaged();
|
||||
byte[] key;
|
||||
byte[] IV = new byte[16];
|
||||
//Create a new key and initialization vector.
|
||||
myRijndael.GenerateKey();
|
||||
key = myRijndael.Key;
|
||||
CASACrypto.StoreKeySetUsingMasterPasscode(key,IV,sMasterPasscode,GetKeyFilePath());
|
||||
//Store the master passcode encrypted with the desktopPasswd
|
||||
CASACrypto.EncryptAndStoreMasterPasscodeUsingString(sMasterPasscode, desktopPasswd, GetPasswdFilePath());
|
||||
lss = new LocalStorage(this,sMasterPasscode);
|
||||
bIsStorePersistent = true;
|
||||
bRet = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
//Console.WriteLine("Master passcode is already set");
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return bRet;
|
||||
#endif
|
||||
}
|
||||
|
||||
internal void IncrRefCount()
|
||||
{
|
||||
try
|
||||
{
|
||||
ssMutex.WaitOne();
|
||||
refCount++;
|
||||
ssMutex.ReleaseMutex();
|
||||
CSSSLogger.DbgLog(CSSSLogger.GetExecutionPath(this) + " : RefCount = " + refCount);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
internal void DecrRefCount()
|
||||
{
|
||||
try
|
||||
{
|
||||
ssMutex.WaitOne();
|
||||
refCount--;
|
||||
ssMutex.ReleaseMutex();
|
||||
CSSSLogger.DbgLog(CSSSLogger.GetExecutionPath(this) + " : RefCount = " + refCount);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
throw e;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal bool AddKeyChain(KeyChain keychain)
|
||||
{
|
||||
try
|
||||
{
|
||||
keychain.CreatedTime = DateTime.Now;
|
||||
keyChainList.Add(keychain.GetKey(),keychain);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.DbgLog(e.ToString());
|
||||
throw e;
|
||||
}
|
||||
|
||||
CSSSLogger.DbgLog(CSSSLogger.GetExecutionPath(this) + " - Succefully added Keychain = "+ keychain.GetKey() + " length = "+ (keychain.GetKey()).Length);
|
||||
|
||||
return true;
|
||||
}
|
||||
internal bool RemoveKeyChain(string id)
|
||||
{
|
||||
keyChainList.Remove(id);
|
||||
return true;
|
||||
}
|
||||
internal KeyChain GetKeyChain(string id)
|
||||
{
|
||||
if(keyChainList.ContainsKey(id))
|
||||
{
|
||||
CSSSLogger.DbgLog("In " + CSSSLogger.GetExecutionPath(this) + " Keychain already exists.");
|
||||
KeyChain kc = (KeyChain)(keyChainList[id]);
|
||||
kc.AccessedTime = DateTime.Now;
|
||||
return kc;
|
||||
}
|
||||
else
|
||||
{
|
||||
CSSSLogger.DbgLog("In " + CSSSLogger.GetExecutionPath(this) + " Keychain doesnot exist.Returning null.");
|
||||
throw new KeyChainDoesNotExistException(id);
|
||||
}
|
||||
|
||||
}
|
||||
internal bool CheckIfKeyChainExists(string id)
|
||||
{
|
||||
if(keyChainList.ContainsKey(id))
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
/* This function would need to do any storage/cleanup required
|
||||
* before removing a user session.
|
||||
*/
|
||||
internal bool CommitStore()
|
||||
{
|
||||
if(lss != null)
|
||||
lss.PersistStore();
|
||||
return true;
|
||||
}
|
||||
|
||||
internal IEnumerator GetKeyChainEnumerator()
|
||||
{
|
||||
//TBD
|
||||
// Return an Enumerator class which has all secrets in this keychain
|
||||
return keyChainList.GetEnumerator();
|
||||
}
|
||||
internal void DumpSecretstore()
|
||||
{
|
||||
lock(keyChainList.SyncRoot)
|
||||
{
|
||||
IDictionaryEnumerator iter = (IDictionaryEnumerator)GetKeyChainEnumerator();
|
||||
while( iter.MoveNext() )
|
||||
{
|
||||
int i = 0;
|
||||
KeyChain kc = (KeyChain)iter.Value;
|
||||
CSSSLogger.DbgLog("\nKeychain id = " + kc.GetKey());
|
||||
CSSSLogger.DbgLog("Secret List is ");
|
||||
IDictionaryEnumerator secIter = (IDictionaryEnumerator)(kc.GetAllSecrets());
|
||||
while(secIter.MoveNext())
|
||||
{
|
||||
Secret secret = (Secret)secIter.Value;
|
||||
CSSSLogger.DbgLog("Secret " + i.ToString() + " id = " + secret.GetKey() + " value = " + secret.GetValue() );
|
||||
IDictionaryEnumerator etor = (IDictionaryEnumerator) secret.GetKeyValueEnumerator();
|
||||
while(etor.MoveNext())
|
||||
{
|
||||
KeyValue kv = (KeyValue)etor.Value;
|
||||
CSSSLogger.DbgLog("Key = " + kv.Key +" Value = " + kv.GetValue());
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
internal int GetSecretStoreState()
|
||||
{
|
||||
return state;
|
||||
}
|
||||
internal int GetNumKeyChains()
|
||||
{
|
||||
return keyChainList.Count;
|
||||
}
|
||||
|
||||
internal bool SetSecretStoreState(int stateToSet)
|
||||
{
|
||||
//BrainShare Special Only - Only Session keychains state 1
|
||||
|
||||
state = STATE_OK;
|
||||
return true;
|
||||
}
|
||||
|
||||
internal string GetDesktopPasswd()
|
||||
{
|
||||
try
|
||||
{
|
||||
string keyChainId = ConstStrings.SSCS_SESSION_KEY_CHAIN_ID + "\0";
|
||||
KeyChain keyChain = GetKeyChain(keyChainId);
|
||||
Secret secret = keyChain.GetSecret(ConstStrings.MICASA_DESKTOP_PASSWD);
|
||||
string passwd = secret.GetKeyValue(ConstStrings.MICASA_DESKTOP_PASSWD_KEYNAME).GetValue();
|
||||
return passwd;
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
CSSSLogger.ExpLog(e.ToString());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
internal string GetUserHomeDirectory()
|
||||
{
|
||||
return user.GetUserHomeDir();
|
||||
}
|
||||
|
||||
internal string GetKeyFilePath()
|
||||
{
|
||||
string homeDir = GetUserHomeDirectory();
|
||||
return homeDir + ConstStrings.MICASA_KEY_FILE;
|
||||
}
|
||||
internal string GetPasscodeByDesktopFilePath()
|
||||
{
|
||||
string homeDir = GetUserHomeDirectory();
|
||||
return homeDir + ConstStrings.MICASA_PASSCODE_BY_DESKTOP_FILE;
|
||||
}
|
||||
|
||||
internal string GetPasscodeByMasterPasswdFilePath()
|
||||
{
|
||||
string homeDir = GetUserHomeDirectory();
|
||||
return homeDir + ConstStrings.MICASA_PASSCODE_BY_MASTERPASSWD_FILE;
|
||||
}
|
||||
|
||||
internal string GetPersistenceFilePath()
|
||||
{
|
||||
string homeDir = GetUserHomeDirectory();
|
||||
return homeDir + ConstStrings.MICASA_PERSISTENCE_FILE;
|
||||
}
|
||||
internal string GetValidationFilePath()
|
||||
{
|
||||
string homeDir = GetUserHomeDirectory();
|
||||
return homeDir + ConstStrings.MICASA_VALIDATION_FILE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user