[StructLayout(LayoutKind.Sequential)] public struct MSG { public IntPtr hwnd; public uint message; public IntPtr wParam; public IntPtr lParam; public uint time; public System.Drawing.Point p; } public enum WaitReturn { Complete, Timeout, } class Utils { [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool PeekMessage(out MSG lpMsg, IntPtr hWnd, uint wMsgFilterMin, uint wMsgFilterMax, uint wRemoveMsg); [DllImport("user32.dll")] static extern bool TranslateMessage([In] ref MSG lpMsg); [DllImport("user32.dll")] static extern IntPtr DispatchMessage([In] ref MSG lpmsg); const uint PM_REMOVE = 1; public static WaitReturn WaitForComplete(double mill, WaitHandle wh) { TimeSpan goal = new TimeSpan(DateTime.Now.AddMilliseconds(mill).Ticks); do { MSG msg = new MSG(); if (PeekMessage(out msg, IntPtr.Zero, 0, 0, PM_REMOVE)) { TranslateMessage(ref msg); DispatchMessage(ref msg); } if (wh.WaitOne(new TimeSpan(0, 0, 0), false)) { return WaitReturn.Complete; } if (goal.CompareTo(new TimeSpan(DateTime.Now.Ticks)) < 0) { return WaitReturn.Timeout; } } while (true); } } // // Secure client (TLS) // class Client : DicomNet { AutoResetEvent waitEvent = new AutoResetEvent(false); string clientPEM = LeadtoolsExamples.Common.ImagesPath.Path + "client.pem"; public Client() : base(null, DicomNetSecurityeMode.Tls) { SetTlsCipherSuiteByIndex(0, DicomTlsCipherSuiteType.DheRsaWithDesCbcSha); SetTlsClientCertificate(clientPEM,DicomTlsCertificateType.Pem, null); //Over here we can get detailed information about the Cipher Suite DicomTlsCipherSuiteType cipherSuite = GetTlsCipherSuite();// Can also call GetTlsCipherSuiteByIndex //Returns DicomTlsEncryptionMethodType Console.WriteLine("Encryption Algorithm is : {0}", GetTlsEncryptionAlgorithm(cipherSuite)); //Returns DicomTlsAuthenticationMethodType Console.WriteLine("Authentication Algorithm is : {0}", GetTlsAuthenticationAlgorithm(cipherSuite)); //Returns DicomTlsMacMethodType Console.WriteLine("Integrity Algorithm is : {0}", GetTlsIntegrityAlgorithm(cipherSuite)); //Returns DicomTlsExchangeMethodType Console.WriteLine("Key Exchange Algorithm is : {0}", GetTlsKeyExchangeAlgorithm(cipherSuite)); Console.WriteLine("Encryption Key Length is : {0}", GetTlsEncryptionKeyLength(cipherSuite)); Console.WriteLine("Mutual Authentication Key Length is : {0}", GetTlsMutualAuthenticationKeyLength(cipherSuite)); } public bool Wait() { WaitReturn ret; ret = Utils.WaitForComplete((5 * 60) * 1000, waitEvent); return (ret == WaitReturn.Complete); } protected override void OnConnect(DicomExceptionCode error) { waitEvent.Set(); } protected override string OnPrivateKeyPassword(bool encryption) { return "test"; } protected override void OnSecureLinkReady(DicomExceptionCode error) { waitEvent.Set(); } } // // Secure server (TLS) // class ServerConnection : DicomNet { string serverPEM = LeadtoolsExamples.Common.ImagesPath.Path + "server.pem"; public ServerConnection() : base(null, DicomNetSecurityeMode.Tls) { SetTlsCipherSuiteByIndex(0, DicomTlsCipherSuiteType.DheRsaWith3DesEdeCbcSha); SetTlsClientCertificate(serverPEM, DicomTlsCertificateType.Pem, null); } protected override string OnPrivateKeyPassword(bool encryption) { return "test"; } } class Server : DicomNet { ServerConnection client; public Server() : base(null, DicomNetSecurityeMode.None) { } protected override void OnAccept(DicomExceptionCode error) { client = new ServerConnection(); Accept(client); } protected override void Dispose(bool __p1) { client.Dispose(); base.Dispose(__p1); } } public void TLSSecuritySample() { DicomEngine.Startup(); DicomNet.Startup(); using (Server server = new Server()) { using (Client client = new Client()) { server.Listen("127.0.0.1", 104, 1); // start server client.Connect(null, 1000, "127.0.0.1", 104); // connect to server if (!client.Wait()) // wait for connection to finish { Debug.Fail("Connection timed out"); } Debug.Assert(client.IsConnected(), "Client not connected"); // // Wait for authenication // if (!client.Wait()) { Debug.Fail("Connection timed out waiting for authenication"); } // Once two computers have negotiated the ciphersuite, and have // authenticated each other, they can begin transferring // messages and data between them. // Continue with normal dicom communication client.CloseForced(true); } server.CloseForced(true); } DicomEngine.Shutdown(); DicomNet.Shutdown(); } |