static internal void DoDemo(int numDevices) { Program.IODir += "MultiDevice/"; CertMaker m = new CertMaker(Program.IODir); IoTDevice[] deviceList = new IoTDevice[numDevices]; HubController = new HubControl(); // make the devices and enroll them in the hub for (int j = 0; j < numDevices; j++) { string devId = GetDeviceID(j); Program.SetDeviceNumber(j); int fwidSeed = 0; m.MakeNew(5, false, fwidSeed); HubController.Connect(); HubController.RemoveDevice(devId); var devInfo = ExtensionDecoder.Decode(Program.ToPath(Program.AliasCert)); HubController.EnrollDevice(devId, fwidSeed, Helpers.Hexify(devInfo.FirmwareID), devInfo.Cert.Thumbprint); IoTDevice device = new IoTDevice(devId, 0, j); deviceList[j] = device; } // run through messaging and update bool[] primaryOrSEcondary = new bool[numDevices]; int epoch = 0; while (true) { for (int j = 0; j < numDevices; j++) { Program.SetDeviceNumber(j); var device = deviceList[j]; string devId = GetDeviceID(j); // send messages using current firmware device.RefreshCert(); device.SendMessages(1, 30); if (device.FirmwareUpdateNeeded) { // update the firmware on the device int fwidSeed = device.DesiredFwVersionNumber; m.MakeAliasCert(true, fwidSeed); var devInfo = ExtensionDecoder.Decode(Program.ToPath(Program.AliasCert)); // and tell the hub HubController.RefreshDevice(devId, fwidSeed, Helpers.Hexify(devInfo.FirmwareID), devInfo.Cert.Thumbprint, primaryOrSEcondary[j]); primaryOrSEcondary[j] = !primaryOrSEcondary[j]; device.CurrentFwVersionNumber = fwidSeed; } } Debug.WriteLine($"Epoch == {epoch++}"); } }
void RunDemo() { while (true) { // tell the server what version number to look for int versionNumber = HubController.GetTargetVersionNumber(); // register or re-register foreach (var d in ClientDevices) { Program.SetDeviceNumber(d.DeviceNumber); if (d.HubRefreshNeeded) { d.RegisterWithFakeDRSServer(); } } // try to send some messages foreach (var d in ClientDevices) { Program.SetDeviceNumber(d.DeviceNumber); d.SendMessages(1, 10); } // see if anyone needs to be updated foreach (var d in ClientDevices) { Program.SetDeviceNumber(d.DeviceNumber); if (d.FirmwareUpdateNeeded) { int targetFwid = d.DesiredFwVersionNumber; d.CurrentFwVersionNumber = targetFwid; if (!d.P0wned) { CertMaker m = new CertMaker(Program.IODir); m.MakeNew(5, true, targetFwid); d.FirmwareUpdateNeeded = false; d.HubRefreshNeeded = true; d.RefreshCert(); } else { Debug.WriteLine($"I'm powned: {d.DeviceName}"); } } } Thread.Sleep(1000); } }
/// <summary> /// Make a bunch of IOTDevice objects and give them a name, keys and certs, but do not enroll them /// in the hub /// </summary> /// <param name="numDevices"></param> void MakeClientDevicesAndCerts(int numDevices) { Program.IODir += "MultiDevice/"; CertMaker m = new CertMaker(Program.IODir); ClientDevices = new IoTDevice[numDevices]; // make the devices and enroll them in the hub for (int j = 0; j < numDevices; j++) { string devId = GetDeviceID(j); Program.SetDeviceNumber(j); // todo - have the devices chain to the same vendor root. int fwidSeed = 0; m.MakeNew(5, false, fwidSeed); IoTDevice device = new IoTDevice(devId, 0, j); ClientDevices[j] = device; } }
static void Main(string[] args) { // This invokes testing using WeClient, etc. Not yet working. //HttpsListener.StartListener(IODir + ServerCert, IODir + ServerKey, IODir + ServerCA, IODir+AliasCert, IODir+AliasKey); InitParms(); bool ok = ParseParms(args); if (!ok) { return; } foreach (var action in ActiveParms) { if (action.Flag == "dir") { IODir = action.Parameter; if (!IODir.EndsWith("\\")) { IODir += "\\"; } continue; } if (action.Flag == "gentest") { CertMaker m = new CertMaker(IODir); m.MakeNew(5, false, 0); continue; } if (action.Flag == "bare") { ChainOrBareCert = "B"; continue; } if (action.Flag == "certify") { CertMaker m = new CertMaker(IODir); m.CertifyExisting(5); continue; } if (action.Flag == "certifyj") { CertMaker m = new CertMaker(IODir); m.CertifyExistingForJava(5); continue; } if (action.Flag == "csr") { CertMaker m = new CertMaker(IODir); m.CertifyExistingFromCsr(5); continue; } if (action.Flag == "server") { SslTcpServer.RunServer( ToPath(Program.ServerCA), ToPath(Program.ServerCert), ToPath(Program.ServerKey), ToPath(Program.DeviceCA), ToPath(Program.DeviceIDPublic) ); continue; } if (action.Flag == "testemu") { SslTcpServer.ValidateEmulatorChain(@"AliasCert.pem", @"DeviceIDCrt.pem", @"r00tcrt.pem"); continue; } if (action.Flag == "sc") { Helpers.Notify("Starting TLSClient..."); var psi = new ProcessStartInfo("TlsClient.exe"); psi.Arguments = ChainOrBareCert + " " + IODir; psi.UseShellExecute = true; var proc = Process.Start(psi);; SslTcpServer.RunServer( ToPath(Program.ServerCA), ToPath(Program.ServerCert), ToPath(Program.ServerKey), ToPath(Program.DeviceCA), ToPath(Program.DeviceIDPublic) ); proc.WaitForExit(); continue; } if (action.Flag == "nogen") { MakeCerts = false; continue; } if (action.Flag == "e2e") { if (MakeCerts) { Helpers.Notify("Making a new certificate set"); CertMaker m = new CertMaker(IODir); m.MakeNew(5, false, 0); //m.MakeNew(5, true, 1); } Helpers.Notify("Starting TLSClient..."); var psi = new ProcessStartInfo("TlsClient.exe"); psi.Arguments = ChainOrBareCert + " " + IODir; psi.UseShellExecute = true; var proc = Process.Start(psi);; SslTcpServer.RunServer( ToPath(Program.ServerCA), ToPath(Program.ServerCert), ToPath(Program.ServerKey), ToPath(Program.DeviceCA), ToPath(Program.DeviceIDPublic) ); proc.WaitForExit(); continue; } if (action.Flag == "ossl_server") { Helpers.Notify("OpenSSL s_server parameters for TLS test server (start in directory with certificates and files)"); Helpers.Notify($"openssl s_server -cert {ToPath(ServerCert)} -key {ToPath(ServerKey)} -CAfile {ToPath(DeviceCertChainAndServerCA)} -status_verbose -verify 10 -rev -accept 5556"); continue; } if (action.Flag == "ossl_client") { Helpers.Notify("OpenSSL s_client parameters for TLS test client (start in directory with certificates and files)"); Helpers.Notify($"openssl s_client -connect localhost:5556 -cert {ToPath(AliasCert)} -key {ToPath(AliasKey)} -CAfile {ToPath(DeviceCertChainAndServerCA)}"); continue; } if (action.Flag == "tls_client") { Helpers.Notify("Starting TLSClient..."); var psi = new ProcessStartInfo("TlsClient.exe"); psi.Arguments = ChainOrBareCert + " " + IODir; psi.CreateNoWindow = true; psi.UseShellExecute = false; psi.RedirectStandardError = true; var proc = Process.Start(psi);; string op = proc.StandardError.ReadToEnd(); proc.WaitForExit(); Helpers.Notify(op); continue; } if (action.Flag == "demo") { var demo = new UpdateDemo(); demo.FakeDRSTest(); } } if (System.Diagnostics.Debugger.IsAttached) { Thread.Sleep(3000); } return; }