diff --git a/Site5/Pages/v5/Init/Default.aspx.cs b/Site5/Pages/v5/Init/Default.aspx.cs index bd093bfd..34fdf832 100644 --- a/Site5/Pages/v5/Init/Default.aspx.cs +++ b/Site5/Pages/v5/Init/Default.aspx.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using System.IO; +using System.Security.Cryptography; using System.Text; using System.Threading; using System.Web; @@ -405,13 +406,60 @@ public partial class Pages_v5_Init_Default : Page _testWriteCredentials, _testAdminCredentials)); - // Start an async thread that does all the work, then return + // SECURITY: Set machine token crypto keys to randomized values + + if (!Debugger.IsAttached) // (but not while debugging in a non-live environment) + { + string machineKeyXml = + "\n" + + "\n" + + GetMachineKey() + "\n"; + + File.WriteAllText ("/etc/swarmops/machineKey.config", machineKeyXml, Encoding.GetEncoding (1252)); + } + + // Start an async thread that does all the initialization work, then return Thread initThread = new Thread (InitDatabaseThread); initThread.Start(); } + private static string GetMachineKey() + { + StringBuilder machineKey = new StringBuilder(); + machineKey.Append("\n"); + return machineKey.ToString(); + } + + public static string GetRandomKey(int bytelength) + { + byte[] buffer = new byte[bytelength]; + RNGCryptoServiceProvider devRandom = new RNGCryptoServiceProvider(); + devRandom.GetBytes(buffer); + + StringBuilder result = new StringBuilder(bytelength * 2); + for (int loop = 0; loop < buffer.Length; loop++) + { + result.AppendFormat ("{0:X2}", buffer[loop]); + } + return result.ToString(); + } + /// /// This function copies the schemas and geography data off an existing Swarmops installation. Runs in its own thread. ///