Modifications of Poorna to fix SetMasterPassword() defect.

This commit is contained in:
Cameron (Kamran) Mashayekhi 2005-10-25 16:06:04 +00:00
parent dc2d35ba49
commit a9e5a67876

View File

@ -34,6 +34,8 @@ namespace sscs.crypto
byte[] IV, byte[] baMasterPasscode, string fileName) byte[] IV, byte[] baMasterPasscode, string fileName)
{ {
bool bRet = false; bool bRet = false;
FileStream fsEncrypt = null;
CryptoStream csEncrypt = null;
try try
{ {
@ -43,7 +45,7 @@ namespace sscs.crypto
encryptor = myRijndael.CreateEncryptor(baMasterPasscode, baMasterPasscode); encryptor = myRijndael.CreateEncryptor(baMasterPasscode, baMasterPasscode);
//Encrypt the data to a file //Encrypt the data to a file
FileStream fsEncrypt = new FileStream(fileName, FileMode.Create); fsEncrypt = new FileStream(fileName, FileMode.Create);
#if LINUX #if LINUX
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR); Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
#endif #endif
@ -54,12 +56,11 @@ namespace sscs.crypto
fsEncrypt.Write(hash,0,hash.Length); fsEncrypt.Write(hash,0,hash.Length);
fsEncrypt.Flush(); fsEncrypt.Flush();
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write); csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
//Write all data to the crypto stream and flush it. //Write all data to the crypto stream and flush it.
csEncrypt.Write(key, 0, key.Length); csEncrypt.Write(key, 0, key.Length);
csEncrypt.FlushFinalBlock(); csEncrypt.FlushFinalBlock();
fsEncrypt.Close();
bRet = true; bRet = true;
} }
catch(Exception e) catch(Exception e)
@ -68,6 +69,10 @@ namespace sscs.crypto
CSSSLogger.DbgLog("Unable to store the generated key"); CSSSLogger.DbgLog("Unable to store the generated key");
bRet = false; bRet = false;
} }
if( fsEncrypt != null )
fsEncrypt.Close();
if( csEncrypt != null )
csEncrypt.Close();
return bRet; return bRet;
} }
@ -75,6 +80,8 @@ namespace sscs.crypto
string fileName ) string fileName )
{ {
byte[] baSavedKey = null; byte[] baSavedKey = null;
FileStream fsDecrypt = null;
CryptoStream csDecrypt = null;
try try
{ {
if(!File.Exists(fileName)) if(!File.Exists(fileName))
@ -89,17 +96,16 @@ namespace sscs.crypto
RijndaelManaged myRijndael = new RijndaelManaged(); RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(baMasterPasscode, baMasterPasscode); ICryptoTransform decryptor = myRijndael.CreateDecryptor(baMasterPasscode, baMasterPasscode);
//Now decrypt //Now decrypt
FileStream fsDecrypt = new FileStream(fileName, FileMode.Open); fsDecrypt = new FileStream(fileName, FileMode.Open);
byte[] storedHash = new byte[32]; byte[] storedHash = new byte[32];
fsDecrypt.Read(storedHash,0,storedHash.Length); fsDecrypt.Read(storedHash,0,storedHash.Length);
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read); csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
baSavedKey = new byte[32]; baSavedKey = new byte[32];
//Read the data out of the crypto stream. //Read the data out of the crypto stream.
csDecrypt.Read(baSavedKey, 0, baSavedKey.Length); csDecrypt.Read(baSavedKey, 0, baSavedKey.Length);
fsDecrypt.Close();
SHA256 sha = new SHA256Managed(); SHA256 sha = new SHA256Managed();
byte[] newHash = sha.ComputeHash(baSavedKey); byte[] newHash = sha.ComputeHash(baSavedKey);
@ -108,11 +114,11 @@ namespace sscs.crypto
if(storedHash[i] != newHash[i]) if(storedHash[i] != newHash[i])
{ {
CSSSLogger.DbgLog("Hash doesnot match"); CSSSLogger.DbgLog("Hash doesnot match");
fsDecrypt.Close();
csDecrypt.Close();
return null; return null;
} }
} }
return baSavedKey;
} }
catch(Exception e) catch(Exception e)
{ {
@ -120,12 +126,19 @@ namespace sscs.crypto
CSSSLogger.DbgLog("Unable to get the stored key"); CSSSLogger.DbgLog("Unable to get the stored key");
baSavedKey = null; baSavedKey = null;
} }
if ( fsDecrypt != null )
fsDecrypt.Close();
if( csDecrypt != null )
csDecrypt.Close();
return baSavedKey; return baSavedKey;
} }
internal static void EncryptDataAndWriteToFile(byte[] xmlData, internal static void EncryptDataAndWriteToFile(byte[] xmlData,
byte[] key, string fileName) byte[] key, string fileName)
{ {
FileStream fsEncrypt = null;
CryptoStream csEncrypt = null;
try try
{ {
byte[] IV = new byte[16]; byte[] IV = new byte[16];
@ -137,7 +150,7 @@ namespace sscs.crypto
ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV); ICryptoTransform encryptor = myRijndael.CreateEncryptor(key, IV);
//Encrypt the data to a file //Encrypt the data to a file
FileStream fsEncrypt = new FileStream(fileName, FileMode.Create); fsEncrypt = new FileStream(fileName, FileMode.Create);
#if LINUX #if LINUX
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR); Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
#endif #endif
@ -148,24 +161,28 @@ namespace sscs.crypto
fsEncrypt.Write(hash,0,hash.Length); fsEncrypt.Write(hash,0,hash.Length);
fsEncrypt.Flush(); fsEncrypt.Flush();
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write); csEncrypt = new CryptoStream(fsEncrypt, encryptor, CryptoStreamMode.Write);
//Write all data to the crypto stream and flush it. //Write all data to the crypto stream and flush it.
csEncrypt.Write(xmlData, 0, xmlData.Length); csEncrypt.Write(xmlData, 0, xmlData.Length);
csEncrypt.FlushFinalBlock(); csEncrypt.FlushFinalBlock();
fsEncrypt.Close();
} }
catch(Exception e) catch(Exception e)
{ {
CSSSLogger.ExpLog(e.ToString()); CSSSLogger.ExpLog(e.ToString());
CSSSLogger.DbgLog("Encrypting and storing to file failed."); CSSSLogger.DbgLog("Encrypting and storing to file failed.");
} }
if( fsEncrypt != null )
fsEncrypt.Close();
if( csEncrypt != null )
csEncrypt.Close();
} }
internal static byte[] ReadFileAndDecryptData(byte[] key, internal static byte[] ReadFileAndDecryptData(byte[] key,
string fileName) string fileName)
{ {
FileStream fsDecrypt = null; FileStream fsDecrypt = null;
CryptoStream csDecrypt = null;
try try
{ {
byte[] IV = new byte[16]; byte[] IV = new byte[16];
@ -175,7 +192,6 @@ namespace sscs.crypto
//Get a decryptor that uses the same key and IV as the encryptor. //Get a decryptor that uses the same key and IV as the encryptor.
RijndaelManaged myRijndael = new RijndaelManaged(); RijndaelManaged myRijndael = new RijndaelManaged();
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV); ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);
if(!File.Exists(fileName)) if(!File.Exists(fileName))
{ {
return null; return null;
@ -186,7 +202,7 @@ namespace sscs.crypto
byte[] storedHash = new byte[32]; byte[] storedHash = new byte[32];
fsDecrypt.Read(storedHash,0,storedHash.Length); fsDecrypt.Read(storedHash,0,storedHash.Length);
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read); csDecrypt = new CryptoStream(fsDecrypt, decryptor, CryptoStreamMode.Read);
long fileLen = fsDecrypt.Length - 32; long fileLen = fsDecrypt.Length - 32;
byte[] fromEncrypt = new byte[fileLen]; byte[] fromEncrypt = new byte[fileLen];
@ -204,23 +220,29 @@ namespace sscs.crypto
if(storedHash[i] != newHash[i]) if(storedHash[i] != newHash[i])
{ {
CSSSLogger.DbgLog("Hash doesnot match"); CSSSLogger.DbgLog("Hash doesnot match");
fsDecrypt.Close();
csDecrypt.Close();
return null; return null;
} }
} }
fsDecrypt.Close(); fsDecrypt.Close();
csDecrypt.Close();
return tmpEncrypt; return tmpEncrypt;
} }
catch(Exception e) catch(Exception e)
{ {
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
} }
if(fsDecrypt != null) if( fsDecrypt != null )
{ {
fsDecrypt.Close(); fsDecrypt.Close();
} }
if( csDecrypt != null )
{
csDecrypt.Close();
}
return null; return null;
} }
/* The methods EncryptData() and DecryptData() would be /* The methods EncryptData() and DecryptData() would be
@ -317,6 +339,8 @@ namespace sscs.crypto
string passwd, string passwd,
string fileName) string fileName)
{ {
FileStream fsEncrypt = null;
CryptoStream csEncrypt = null;
try try
{ {
if(File.Exists(fileName)) if(File.Exists(fileName))
@ -330,11 +354,11 @@ namespace sscs.crypto
encryptor = myRijndael.CreateEncryptor(baKey, baKey); encryptor = myRijndael.CreateEncryptor(baKey, baKey);
//Encrypt the data to a file //Encrypt the data to a file
FileStream fsEncrypt = new FileStream(fileName,FileMode.Create); fsEncrypt = new FileStream(fileName,FileMode.Create);
#if LINUX #if LINUX
Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR); Mono.Unix.Syscall.chmod(fileName,Mono.Unix.FilePermissions.S_IRUSR | Mono.Unix.FilePermissions.S_IWUSR);
#endif #endif
CryptoStream csEncrypt = new CryptoStream(fsEncrypt, encryptor, csEncrypt = new CryptoStream(fsEncrypt, encryptor,
CryptoStreamMode.Write); CryptoStreamMode.Write);
//Write all data to the crypto stream and flush it. //Write all data to the crypto stream and flush it.
@ -342,15 +366,28 @@ namespace sscs.crypto
csEncrypt.Write(baMasterPasscode, 0, baMasterPasscode.Length); csEncrypt.Write(baMasterPasscode, 0, baMasterPasscode.Length);
csEncrypt.FlushFinalBlock(); csEncrypt.FlushFinalBlock();
fsEncrypt.Close(); fsEncrypt.Close();
csEncrypt.Close();
} }
catch(Exception e) catch(Exception e)
{ {
CSSSLogger.ExpLog(e.ToString()); CSSSLogger.ExpLog(e.ToString());
} }
if( fsEncrypt != null )
{
fsEncrypt.Close();
} }
if( csEncrypt != null )
{
csEncrypt.Close();
}
}
public static byte[] DecryptMasterPasscodeUsingString(string passwd, public static byte[] DecryptMasterPasscodeUsingString(string passwd,
string fileName) string fileName)
{ {
FileStream fsDecrypt = null;
CryptoStream csDecrypt = null;
byte[] baSavedMasterPasscode = null;
try try
{ {
byte[] baKey = Generate16ByteKeyFromString(passwd); byte[] baKey = Generate16ByteKeyFromString(passwd);
@ -362,23 +399,25 @@ namespace sscs.crypto
ICryptoTransform decryptor = myRijndael.CreateDecryptor(baKey, ICryptoTransform decryptor = myRijndael.CreateDecryptor(baKey,
baKey); baKey);
//Now decrypt //Now decrypt
FileStream fsDecrypt = new FileStream(fileName, FileMode.Open); fsDecrypt = new FileStream(fileName, FileMode.Open);
CryptoStream csDecrypt = new CryptoStream(fsDecrypt, decryptor, csDecrypt = new CryptoStream(fsDecrypt, decryptor,
CryptoStreamMode.Read); CryptoStreamMode.Read);
byte[] baSavedMasterPasscode = new byte[16]; baSavedMasterPasscode = new byte[16];
//Read the data out of the crypto stream. //Read the data out of the crypto stream.
csDecrypt.Read(baSavedMasterPasscode, 0, 16); csDecrypt.Read(baSavedMasterPasscode, 0, 16);
fsDecrypt.Close();
return baSavedMasterPasscode;
} }
catch(Exception e) catch(Exception e)
{ {
CSSSLogger.ExpLog(e.ToString()); CSSSLogger.ExpLog(e.ToString());
CSSSLogger.DbgLog("Unable to decrypt master passode"); CSSSLogger.DbgLog("Unable to decrypt master passode");
baSavedMasterPasscode = null;
} }
return null; if( fsDecrypt != null )
fsDecrypt.Close();
if( csDecrypt != null )
csDecrypt.Close();
return baSavedMasterPasscode;
} }
internal static byte[] GetMasterPasscodeUsingMasterPasswd( internal static byte[] GetMasterPasscodeUsingMasterPasswd(