void StartServer() { SslTcpServer.RunServer( Program.ToPath(Program.ServerCA), Program.ToPath(Program.ServerCert), Program.ToPath(Program.ServerKey), null, null ); }
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; }
internal bool FakeDRSServerHandshake(string devId) { string tempCertFile = "AliasCert.PFX"; string password = ""; Helpers.MakePFXFile(Program.ToPath(Program.AliasCert), Program.ToPath(Program.AliasKey), tempCertFile, password); var clientCert = new X509Certificate2(tempCertFile); var certs = new X509Certificate2Collection(new X509Certificate2[] { clientCert }); // connect to server TcpClient client = new TcpClient("127.0.0.1", 5556); // Create an SSL stream and connect. SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); try { sslStream.AuthenticateAsClient("RIoT Server CA", certs, SslProtocols.Tls, false); } catch (AuthenticationException e) { Console.WriteLine("Exception: {0}", e.Message); if (e.InnerException != null) { Helpers.Notify($"Inner exception: {e.InnerException.Message}", true); } Helpers.Notify("Authentication failed - closing the connection."); client.Close(); return(false); } sslStream.ReadTimeout = 10000; sslStream.WriteTimeout = 10000; SslTcpServer.SendMessage(sslStream, devId); string messageFromServer = SslTcpServer.ReadMessage(sslStream); /* * byte[] message = Encoding.UTF8.GetBytes(devId); * byte[] len = new byte[] { (byte) message.Length }; * sslStream.Write(len, 0, 1); * sslStream.Write(message,0, message.Length); * sslStream.Flush(); * byte[] buf = new byte[1024]; * int numRead = sslStream.Read(buf, 0, 1); * if(numRead!=1) * { * Helpers.Notify("TLSClient got a bad message from the server"); * } * int pos = 0; * int lenX = (int) buf[0]; * while (true) * { * numRead = sslStream.Read(buf, pos, lenX - pos); * pos += numRead; * if (pos == lenX) break; * } * string serverMessage = Encoding.UTF8.GetString(buf, 0, lenX); */ Helpers.Notify($"Client received: {messageFromServer}"); Thread.Sleep(30); client.Close(); Helpers.Notify("Client closed."); return(true); }