Significant crypto hardening - will break old cookies, requiring relogin

This commit is contained in:
Rick Falkvinge
2015-05-11 12:58:41 +02:00
parent 2bc115f84d
commit f151ba1d4c
4 changed files with 135 additions and 2 deletions
+50
View File
@@ -1,5 +1,9 @@
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Swarmops.Basic.Types;
using Swarmops.Basic.Types.Swarm;
using Swarmops.Common.Enums;
@@ -567,5 +571,51 @@ namespace Swarmops.Logic.Security
{
}
}
public static byte[] InitializeSymmetricDatabaseKey()
{
if (!String.IsNullOrEmpty (Persistence.Key["SymmetricEncryptionKey"]))
{
throw new InvalidOperationException("Cannot overwrite encryption key! BAD BAD BAD code!");
}
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.KeySize = 256;
aes.GenerateKey();
byte[] key = aes.Key;
string keyString = Convert.ToBase64String (key);
Persistence.Key["SymmetricEncryptionKey"] = keyString;
return key;
}
}
public static byte[] InitializeSymmetricFileSystemKey()
{
if (Debugger.IsAttached && Path.DirectorySeparatorChar == '/') // check if we're debugging - double check to overdo security
{
return new byte[32]; // can't write to /etc/swarmops in the debugger; return an all-zero key while debugging
}
if (File.Exists ("/etc/swarmops/symmetricKey.config"))
{
throw new InvalidOperationException("Can't overwrite encryption key - BAD BAD BAD code!");
}
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
{
aes.KeySize = 256;
aes.GenerateKey();
byte[] key = aes.Key;
string keyString = Convert.ToBase64String(key);
File.WriteAllText("/etc/swarmops/symmetricKey.config", keyString, Encoding.ASCII);
return key;
}
}
}
}
+2 -2
View File
@@ -27,7 +27,7 @@ namespace Swarmops.Logic.Security
public static Authority FromEncryptedXml (string cryptXml)
{
byte[] keyBytes = Encoding.ASCII.GetBytes(SystemSettings.InstallationId.Replace("-", "")); // unique for this install
byte[] keyBytes = SystemSettings.SymmetricEncryptionKeyDatabase;
byte[] cryptoBytes = Convert.FromBase64String(cryptXml);
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
@@ -51,7 +51,7 @@ namespace Swarmops.Logic.Security
public string ToEncryptedXml()
{
byte[] keyBytes = Encoding.ASCII.GetBytes(SystemSettings.InstallationId.Replace ("-","")); // unique for this install
byte[] keyBytes = SystemSettings.SymmetricEncryptionKeyDatabase;
byte[] dataBytes = Encoding.UTF8.GetBytes (ToXml());
using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider())
+78
View File
@@ -1,11 +1,15 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// This class is just for readability of other code. Doesn't add any logic at all, really - just readability, like SystemSettings.SmtpHost instead of Persistence.blahblah.
using NBitcoin;
using Swarmops.Logic.Security;
namespace Swarmops.Logic.Support
{
@@ -116,6 +120,80 @@ namespace Swarmops.Logic.Support
set { Persistence.Key["InstallationName"] = value; }
}
public static byte[] SymmetricEncryptionKeyCombined
{
get
{
byte[] dbKey = SymmetricEncryptionKeyDatabase;
byte[] fsKey = SymmetricEncryptionKeyFileSystem;
byte[] result = new byte[dbKey.Length];
for (int index = 0; index < dbKey.Length; index++)
{
result[index] = (byte) (dbKey[index] ^ fsKey[index]); // combine the two keys through xor
}
return result;
}
}
static public byte[] SymmetricEncryptionKeyDatabase
{
get
{
if (_cacheDatabaseSymmetricKey != null)
{
return _cacheDatabaseSymmetricKey;
}
string keyString = Persistence.Key["SymmetricEncryptionKey"];
if (string.IsNullOrEmpty (keyString))
{
_cacheDatabaseSymmetricKey = Authentication.InitializeSymmetricDatabaseKey(); // Creates a new key - remove this by Alpha-12; should be in Init // TODO
}
else
{
_cacheDatabaseSymmetricKey = Convert.FromBase64String (keyString);
}
return _cacheDatabaseSymmetricKey;
}
}
private static byte[] _cacheDatabaseSymmetricKey = null;
private static byte[] _cacheFileSystemSymmetricKey = null;
static public byte[] SymmetricEncryptionKeyFileSystem
{
get {
if (_cacheFileSystemSymmetricKey != null)
{
return _cacheFileSystemSymmetricKey;
}
if (Debugger.IsAttached && Path.DirectorySeparatorChar != '/')
{
// If we're debugging, return a zero key - can't read from /etc/swarmops in debug code
_cacheFileSystemSymmetricKey = new byte[32]; // return 256 bits all zero
}
else if (!File.Exists ("/etc/swarmops/symmetricKey.config"))
{
_cacheFileSystemSymmetricKey = Authentication.InitializeSymmetricFileSystemKey();
}
else
{
string keyString = File.ReadAllText ("/etc/swarmops/symmetricKey.config", Encoding.ASCII);
_cacheFileSystemSymmetricKey = Convert.FromBase64String (keyString);
}
return _cacheFileSystemSymmetricKey;
}
}
static private string GetDefaultedPersistedKey (string key, string defaultValue)
{
string result = Persistence.Key[key];
+5
View File
@@ -372,6 +372,11 @@ namespace Swarmops.Frontend.Pages.v5.Public
File.WriteAllText ("/etc/swarmops/machineKey.config", machineKeyXml, Encoding.GetEncoding (1252));
}
// SECURITY: Initialize encryption keys
Authentication.InitializeSymmetricDatabaseKey();
Authentication.InitializeSymmetricFileSystemKey();
// Start an async thread that does all the initialization work, then return
Thread initThread = new Thread (InitDatabaseThread);