diff --git a/Backend/SocketServices/BackendServices.cs b/Backend/SocketServices/BackendServices.cs index 2c1a15de..c9e2867e 100644 --- a/Backend/SocketServices/BackendServices.cs +++ b/Backend/SocketServices/BackendServices.cs @@ -46,16 +46,14 @@ namespace Swarmops.Backend.SocketServices string authBase64 = Context.QueryString["Auth"]; authBase64 = Uri.UnescapeDataString(authBase64); // Defensive programming - % sign does not exist in base64 so this won't ever collapse a working encoding - _authority = Authority.FromEncryptedXml(authBase64); + _systemAuthority = Authority.IsSystemAuthorityTokenValid(authBase64); + + if (!_systemAuthority) + { + throw new UnauthorizedAccessException("System access token invalid"); + } base.OnOpen(); - - if (Context.QueryString["Notify"] != "false") - { - Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + - String.Format("\"editorCount\":\"{0}\"", Sessions.Count) + '}'); - //Send(Program.GetQueueInfoJson()); - } } protected override void OnClose(CloseEventArgs e) @@ -65,7 +63,7 @@ namespace Swarmops.Backend.SocketServices Sessions.Broadcast("{\"messageType\":\"EditorCount\"," + String.Format("\"editorCount\":\"{0}\"", Sessions.Count) + '}'); } - private Authority _authority = null; + private bool _systemAuthority = false; } } diff --git a/FrontSocket/FrontendLoop.cs b/FrontSocket/FrontendLoop.cs index d4e5f278..596e23f6 100644 --- a/FrontSocket/FrontendLoop.cs +++ b/FrontSocket/FrontendLoop.cs @@ -12,6 +12,7 @@ using Newtonsoft.Json.Linq; using Swarmops.Logic.Communications; using Swarmops.Logic.Communications.Payload; using Swarmops.Common.ExtensionMethods; +using Swarmops.Logic.Security; using Swarmops.Logic.Support; using WebSocketSharp; using WebSocketSharp.Server; @@ -79,7 +80,7 @@ namespace Swarmops.Frontend.Socket string backendSocketUri = "ws://" + backendHostname + ":" + SystemSettings.WebsocketPortBackend.ToString(CultureInfo.InvariantCulture) + - "/Backend"; + "/Backend?Auth=" + Authority.GetSystemAuthorityToken("Frontend"); bool exitFlag = false; diff --git a/Logic/Security/Authority.cs b/Logic/Security/Authority.cs index 76db54f0..b31bee48 100644 --- a/Logic/Security/Authority.cs +++ b/Logic/Security/Authority.cs @@ -12,6 +12,7 @@ using Swarmops.Basic.Types; using Swarmops.Basic.Types.Security; using Swarmops.Basic.Types.Swarm; using Swarmops.Common.Enums; +using Swarmops.Common.ExtensionMethods; using Swarmops.Common.Interfaces; using Swarmops.Logic.Structure; using Swarmops.Logic.Support; @@ -28,21 +29,7 @@ namespace Swarmops.Logic.Security public static Authority FromEncryptedXml (string cryptXml) { - byte[] keyBytes = SystemSettings.SymmetricEncryptionKeyDatabase; - byte[] cryptoBytes = Convert.FromBase64String(cryptXml); - - using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) - { - aes.Key = keyBytes; - aes.IV = new ArraySegment (cryptoBytes, 0, 16).ToArray(); - cryptoBytes = new ArraySegment(cryptoBytes, 16, cryptoBytes.Length - 16).ToArray(); - - using (ICryptoTransform crypto = aes.CreateDecryptor()) - { - byte[] clearBytes = crypto.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length); - return FromXml(Encoding.UTF8.GetString (clearBytes)); - } - } + return FromXml(DecryptString(cryptXml)); } public string ToXml() @@ -51,9 +38,14 @@ namespace Swarmops.Logic.Security } public string ToEncryptedXml() + { + return EncryptString(ToXml()); + } + + private static string EncryptString(string input) { byte[] keyBytes = SystemSettings.SymmetricEncryptionKeyDatabase; - byte[] dataBytes = Encoding.UTF8.GetBytes (ToXml()); + byte[] dataBytes = Encoding.UTF8.GetBytes(input); using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) { @@ -67,12 +59,48 @@ namespace Swarmops.Logic.Security using (ICryptoTransform crypto = aes.CreateEncryptor()) { - byte[] cryptoBytes = crypto.TransformFinalBlock (dataBytes, 0, dataBytes.Length); - return Convert.ToBase64String (aes.IV.Concat (cryptoBytes).ToArray()); // joins two byte[] arrays + byte[] cryptoBytes = crypto.TransformFinalBlock(dataBytes, 0, dataBytes.Length); + return Convert.ToBase64String(aes.IV.Concat(cryptoBytes).ToArray()); // joins two byte[] arrays } } } + private static string DecryptString(string input) + { + byte[] keyBytes = SystemSettings.SymmetricEncryptionKeyDatabase; + byte[] cryptoBytes = Convert.FromBase64String(input); + + using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider()) + { + aes.Key = keyBytes; + aes.IV = new ArraySegment(cryptoBytes, 0, 16).ToArray(); + cryptoBytes = new ArraySegment(cryptoBytes, 16, cryptoBytes.Length - 16).ToArray(); + + using (ICryptoTransform crypto = aes.CreateDecryptor()) + { + byte[] clearBytes = crypto.TransformFinalBlock(cryptoBytes, 0, cryptoBytes.Length); + return Encoding.UTF8.GetString(clearBytes); + } + } + } + + public static string GetSystemAuthorityToken(string subsystem) + { + return EncryptString("Swarmops/" + subsystem + " " + DateTime.UtcNow.ToUnix()); + } + + public static bool IsSystemAuthorityTokenValid(string token) + { + string decrypted = DecryptString(token); + if (decrypted.StartsWith("Swarmops/")) + { + // TODO: Check timestamp + return true; + } + + return false; + } + private Authority (AuthorityData data) { this._data = data; diff --git a/Logic/Security/Authorization.cs b/Logic/Security/Authorization.cs index 4cf74051..6af4dec5 100644 --- a/Logic/Security/Authorization.cs +++ b/Logic/Security/Authorization.cs @@ -803,5 +803,6 @@ namespace Swarmops.Logic.Security internal class FoundItException : Exception { }*/ + } } \ No newline at end of file