Esempio n. 1
0
        public void ProcessMessageTest()
        {
            var clientCert = ThaliClientToDeviceHubUtilities.GetLocalClientCertificate(tempDirectory);

            // Provision client to local Thali Hub
            var provisionXmlHttpRequest = GenerateXmlHttprequestForClientProvisioning();
            var xmlHttpResponse         =
                (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(provisionXmlHttpRequest, clientCert);

            Assert.AreEqual(xmlHttpResponse.status, 200);
            var hubUrl = xmlHttpResponse.responseText;

            // Provision local hub to 'remote' hub
            var localToRemoteHubXmlHttpRequest = this.GenerateXmlHttpRequestForLocalToRemoteHubProvisioning(hubUrl);

            xmlHttpResponse =
                (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(localToRemoteHubXmlHttpRequest, clientCert);
            Assert.AreEqual(200, xmlHttpResponse.status);
            Assert.AreEqual(hubUrl, xmlHttpResponse.responseText);

            var xmlHttpRequest = GenerateXmlHttpRequestForNonExistentDatabase(hubUrl);

            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
            Assert.AreEqual(xmlHttpResponse.status, 404);
        }
Esempio n. 2
0
 public void ProvisionThaliClient()
 {
     var clientCert          = ThaliClientToDeviceHubUtilities.ProvisionThaliClient(Host, Port, tempDirectory);
     var serverKey           = ThaliClientToDeviceHubUtilities.GetServersRootPublicKey(Host, Port, clientCert);
     var couchClient         = ThaliClientToDeviceHubUtilities.GetCouchClient(serverKey, Host, Port, clientCert);
     var myPrincipalDatabase = couchClient.GetDatabase(ThaliCryptoUtilities.KeyDatabaseName);
     var keyId        = BogusAuthorizeCouchDocument.GenerateRsaKeyId(new BigIntegerRSAPublicKey(clientCert));
     var clientKeyDoc = myPrincipalDatabase.GetDocument <BogusAuthorizeCouchDocument>(keyId);
 }
Esempio n. 3
0
        public void GetServersRootPublicKeyTest()
        {
            var keyPair      = ThaliCryptoUtilities.GenerateThaliAcceptablePublicPrivateKeyPair();
            var pkcs12Stream = ThaliCryptoUtilities.CreatePKCS12KeyStoreWithPublicPrivateKeyPair(
                keyPair,
                ThaliCryptoUtilities.DefaultPassPhrase);
            var cert      = ThaliCryptoUtilities.GetX509Certificate(pkcs12Stream, ThaliCryptoUtilities.DefaultPassPhrase);
            var serverKey = ThaliClientToDeviceHubUtilities.GetServersRootPublicKey(Host, Port, cert);

            Assert.IsNotNull(serverKey);
        }
Esempio n. 4
0
        public void ThaliWebRequestTest()
        {
            var keyPair      = ThaliCryptoUtilities.GenerateThaliAcceptablePublicPrivateKeyPair();
            var pkcs12Stream = ThaliCryptoUtilities.CreatePKCS12KeyStoreWithPublicPrivateKeyPair(
                keyPair,
                ThaliCryptoUtilities.DefaultPassPhrase);
            var cert             = ThaliCryptoUtilities.GetX509Certificate(pkcs12Stream, ThaliCryptoUtilities.DefaultPassPhrase);
            var serverKey        = ThaliClientToDeviceHubUtilities.GetServersRootPublicKey(Host, Port, cert);
            var serverHttpKeyUri = HttpKeyUri.BuildHttpKeyUri(serverKey, Host, Port, null, null);
            var thaliWebRequest  = ThaliClientToDeviceHubUtilities.CreateThaliWebRequest(serverHttpKeyUri, cert);

            thaliWebRequest.Method = "GET";
            thaliWebRequest.GetResponse().Close();
        }
Esempio n. 5
0
        private static XmlHttpResponse ExecuteProvisionLocalClientToLocalHub(
            XmlHttpRequest xmlHttpRequest,
            X509Certificate2 clientCert)
        {
            var hubHttpKeyUri = DiscoverRootCertIfNeeded(HttpKeyUri.BuildHttpKeyUri(xmlHttpRequest.url), clientCert);

            ThaliClientToDeviceHubUtilities.ProvisionThaliClient(
                hubHttpKeyUri.ServerPublicKey,
                hubHttpKeyUri.Host,
                hubHttpKeyUri.Port,
                clientCert);

            return new XmlHttpResponse { status = 200, transactionId = xmlHttpRequest.transactionId, responseText = hubHttpKeyUri.AbsoluteUri };
        }
Esempio n. 6
0
        public void Setup()
        {
            var tempDirectoryForSetupPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            tempDirectoryForSetup = Directory.CreateDirectory(tempDirectoryForSetupPath);
            var clientCert      = ThaliClientToDeviceHubUtilities.GetLocalClientCertificate(tempDirectoryForSetup);
            var serverPublicKey = ThaliClientToDeviceHubUtilities.GetServersRootPublicKey(Host, Port, clientCert);

            ThaliClientToDeviceHubUtilities.ProvisionThaliClient(serverPublicKey, Host, Port, clientCert);
            var couchClient = ThaliClientToDeviceHubUtilities.GetCouchClient(serverPublicKey, Host, Port, clientCert);

            var response = couchClient.DeleteDatabase(TestDatabaseName);

            var tempDirectoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());

            tempDirectory = Directory.CreateDirectory(tempDirectoryPath);
        }
Esempio n. 7
0
        private static XmlHttpResponse ExecuteProvisionLocalHubToRemoteHub(
            XmlHttpRequest xmlHttpRequest,
            X509Certificate2 clientCert)
        {
            var remoteHubHttpKeyUri = DiscoverRootCertIfNeeded(HttpKeyUri.BuildHttpKeyUri(xmlHttpRequest.url), clientCert);

            var localHubHttpKeyUri = HttpKeyUri.BuildHttpKeyUri(xmlHttpRequest.requestText);

            ThaliClientToDeviceHubUtilities.ProvisionKeyInPrincipalDatabase(remoteHubHttpKeyUri.ServerPublicKey, remoteHubHttpKeyUri.Host, remoteHubHttpKeyUri.Port, localHubHttpKeyUri.ServerPublicKey, clientCert);

            return new XmlHttpResponse
            {
                status = 200,
                transactionId = xmlHttpRequest.transactionId,
                responseText = remoteHubHttpKeyUri.AbsoluteUri
            };
        }
Esempio n. 8
0
        public static void bar()
        {
            var tempDirectoryPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
            var tempDirectory     = Directory.CreateDirectory(tempDirectoryPath);

            var clientCert = ThaliClientToDeviceHubUtilities.GetLocalClientCertificate(tempDirectory);

            var testGetJsonString = GenerateXmlHttpRequestJsonObjectForNonExistentDatabase();

            var xmlHttpRequest  = JsonConvert.DeserializeObject <XmlHttpRequest>(testGetJsonString);
            var xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);

            //Assert.AreEqual(xmlHttpResponse.status, 404);


            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
            xmlHttpResponse = (XmlHttpResponse)XmlHttpRequestProxy.ProcessMessage(xmlHttpRequest, clientCert);
        }
Esempio n. 9
0
        private static XmlHttpResponse ProxyRequest(
            XmlHttpRequest xmlHttpRequest,
            X509Certificate2 clientCert)
        {
            var httpKeyUri = HttpKeyUri.BuildHttpKeyUri(xmlHttpRequest.url);

            HttpWebRequest webRequest = ThaliClientToDeviceHubUtilities.CreateThaliWebRequest(httpKeyUri, clientCert);

            webRequest.Method = xmlHttpRequest.method;

            // There are multiple headers that cannot be set directly via webRequest.Headers. I only catch
            // two below that seem of some reasonable use.
            foreach (var headerNameValue in xmlHttpRequest.headers)
            {
                if (headerNameValue.Key.Equals("Accept", StringComparison.OrdinalIgnoreCase))
                {
                    webRequest.Accept = headerNameValue.Value;
                }
                else if (headerNameValue.Key.Equals("Content-Type", StringComparison.OrdinalIgnoreCase))
                {
                    webRequest.ContentType = headerNameValue.Value;
                }
                else
                {
                    webRequest.Headers.Add(headerNameValue.Key, headerNameValue.Value);
                }
            }

            if (string.IsNullOrWhiteSpace(xmlHttpRequest.requestText))
            {
                return ProcessResponse(xmlHttpRequest.transactionId, webRequest);
            }

            var bodyAsBytes = Encoding.UTF8.GetBytes(xmlHttpRequest.requestText);
            webRequest.GetRequestStream().Write(bodyAsBytes, 0, bodyAsBytes.Count());

            var response = ProcessResponse(xmlHttpRequest.transactionId, webRequest);

            return response;
        }
Esempio n. 10
0
        /// <summary>
        /// TODO: This whole method is just wrong, what happens if the server at the address changes its key?!?!?!
        /// TODO: Once we have a real discovery framework this whole 0.0 mechanism needs to go away.
        /// </summary>
        /// <param name="httpKeyUri"></param>
        /// <param name="clientCert"></param>
        /// <returns></returns>
        private static HttpKeyUri DiscoverRootCertIfNeeded(HttpKeyUri httpKeyUri, X509Certificate2 clientCert)
        {
            if (httpKeyUri.ServerPublicKey.Exponent.Equals(BigInteger.Zero)
                && httpKeyUri.ServerPublicKey.Modulus.Equals(BigInteger.Zero))
            {
                var host = httpKeyUri.Host;
                var port = httpKeyUri.Port;
                var hostPortTuple = new Tuple<string, int>(host, port);

                var serverPublicKey = HttpKeyStore.GetOrAdd(
                    hostPortTuple,
                    keyTuple => ThaliClientToDeviceHubUtilities.GetServersRootPublicKey(host, port, clientCert));

                var serverHttpKey = HttpKeyUri.BuildHttpKeyUri(
                    serverPublicKey,
                    host,
                    port,
                    httpKeyUri.PathWithoutPublicKey,
                    httpKeyUri.Query);
                return serverHttpKey;
            }

            return httpKeyUri;
        }
Esempio n. 11
0
        public static void MainLoop(bool synchronous)
        {
            Stream outStream = null;

            // TODO: If one of the console methods throws an exception then will it be caught by the outer catch? And if
            // there is an exception inside the inner try then don't I have to assume that the using will close the streams
            // before the outter catch can send a message? I'm not sure about the answers and don't have time to explore
            // at the moment so I'm using a double wrapping of try's to take care of it.
            try
            {
                using (var inStream = Console.OpenStandardInput())
                using (outStream = Console.OpenStandardOutput())
                {
                    try
                    {
                        ServicePointManager.DefaultConnectionLimit = 100;

                        var appdataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
                        var homeDirectory = string.Concat(appdataDirectory, @"\Google\Chrome\User Data\Thali");
                        if (!Directory.Exists(homeDirectory))
                        {
                            Directory.CreateDirectory(homeDirectory);
                        }

                        DirectoryInfo workingDirectory = new DirectoryInfo(homeDirectory);
                        
                        var clientCert = ThaliClientToDeviceHubUtilities.GetLocalClientCertificate(workingDirectory);
                        if (synchronous)
                        {
                            ChromeNativeHostUtilities.SynchronousRequestResponseMessageEngine<XmlHttpRequest>(
                                inStream,
                                outStream,
                                inMessage => ProcessMessage(inMessage, clientCert),
                                ProcessHostError);
                        }
                        else
                        {
                            ChromeNativeHostUtilities.AsynchronousRequestResponseMessageEngine<XmlHttpRequest>(
                                inStream,
                                outStream,
                                inMessage => ProcessMessage(inMessage, clientCert),
                                ProcessHostError);
                        }
                    }
                    catch (Exception e)
                    {
                        if (outStream != null)
                        {
                            ChromeNativeHostUtilities.ParallelSendMessage(ProcessHostError("oops! " + e.Message, null), outStream);
                        }                        
                    }
                }
            }
            catch (Exception e)
            {
                if (outStream != null)
                {
                    ChromeNativeHostUtilities.SendMessage(ProcessHostError("oops! " + e.Message, null), outStream);   
                }
            }
        }