public void TestKeyStoreMethods()
        {
            var keyPair            = ThaliCryptoUtilities.GenerateThaliAcceptablePublicPrivateKeyPair();
            var keyStoreBinary     = ThaliCryptoUtilities.CreatePKCS12KeyStoreWithPublicPrivateKeyPair(keyPair, ThaliCryptoUtilities.DefaultPassPhrase);
            var x509cert           = ThaliCryptoUtilities.GetX509Certificate(keyStoreBinary, ThaliCryptoUtilities.DefaultPassPhrase);
            var retrievedKeyParams = ((RSACryptoServiceProvider)x509cert.PrivateKey).ExportParameters(true);

            var originalBigIntegerRsaPublicKey  = new BigIntegerRSAPublicKey((RsaKeyParameters)keyPair.Public);
            var retrievedBigIntegerRsaPublicKey = new BigIntegerRSAPublicKey(retrievedKeyParams);

            Assert.IsTrue(originalBigIntegerRsaPublicKey.Equals(retrievedBigIntegerRsaPublicKey));

            var originalKeyParams = (RsaPrivateCrtKeyParameters)keyPair.Private;

            Assert.IsTrue(
                originalKeyParams.DP.Equals(new BigInteger(1, retrievedKeyParams.DP)) &&
                originalKeyParams.DQ.Equals(new BigInteger(1, retrievedKeyParams.DQ)) &&
                originalKeyParams.P.Equals(new BigInteger(1, retrievedKeyParams.P)) &&
                originalKeyParams.Q.Equals(new BigInteger(1, retrievedKeyParams.Q)));
        }
Example #2
0
        public void HttpKeyUriTests()
        {
            var          keyPair         = ThaliCryptoUtilities.GenerateThaliAcceptablePublicPrivateKeyPair();
            var          serverPublicKey = new BigIntegerRSAPublicKey((RsaKeyParameters)keyPair.Public);
            const string Host            = "foo.com";
            const int    Port            = 413;
            string       path            = "/ick";
            string       query           = "?ark";
            string       fragment        = "#bark";
            string       extraValue      = query + fragment;

            var httpKeyURL = HttpKeyUri.BuildHttpKeyUri(serverPublicKey, Host, Port, path, extraValue);

            // We want one we do manually just to make sure everything is o.k.
            Assert.IsTrue(Host.Equals(httpKeyURL.Host, StringComparison.Ordinal));
            Assert.IsTrue(Port == httpKeyURL.Port);
            Assert.IsTrue(serverPublicKey.Modulus.Equals(httpKeyURL.ServerPublicKey.Modulus));
            Assert.IsTrue(serverPublicKey.Exponent.Equals(httpKeyURL.ServerPublicKey.Exponent));
            Assert.IsTrue(query.Equals(httpKeyURL.Query, StringComparison.Ordinal));
            Assert.IsTrue(fragment.Equals(httpKeyURL.Fragment, StringComparison.Ordinal));
            Assert.IsTrue(httpKeyURL.PathWithoutPublicKey.Equals(path, StringComparison.Ordinal));

            string expectedURL = HttpKeyUri.HttpKeySchemeName + "://" + Host + ":" + Port + "/" +
                                 HttpKeyUri.RsaKeyType + ":" + serverPublicKey.Exponent + "." + serverPublicKey.Modulus +
                                 path + query + fragment;

            Assert.IsTrue(expectedURL.Equals(httpKeyURL.ToString(), StringComparison.Ordinal));

            string expectedHttpsURL = "https://" + Host + ":" + Port + path + query + fragment;

            Assert.IsTrue(expectedHttpsURL.Equals(httpKeyURL.CreateHttpsUrl(), StringComparison.Ordinal));

            // ReSharper disable once EqualExpressionComparison
            Assert.IsTrue(httpKeyURL.Equals(httpKeyURL));

            HttpKeyUri secondHttpKeyURL = HttpKeyUri.BuildHttpKeyUri(expectedURL);

            Assert.IsTrue(httpKeyURL.Equals(secondHttpKeyURL));

            HttpKeyUri thirdHttpKeyURL = HttpKeyUri.BuildHttpKeyUri(serverPublicKey, Host, Port, null, null);

            string expectedThirdURL = HttpKeyUri.HttpKeySchemeName + "://" + Host + ":" + Port + "/" +
                                      HttpKeyUri.RsaKeyType + ":" + serverPublicKey.Exponent + "." + serverPublicKey.Modulus;

            Assert.IsTrue(expectedThirdURL.Equals(thirdHttpKeyURL.ToString(), StringComparison.Ordinal));
            Assert.IsTrue(HttpKeyUri.BuildHttpKeyUri(expectedThirdURL).Equals(thirdHttpKeyURL));

            path     = "/ick  ?";
            query    = "??????    ";
            fragment = "###???///???";
            HttpKeyUri escapedChars = HttpKeyUri.BuildHttpKeyUri(serverPublicKey, Host, Port, path, query + fragment);

            expectedHttpsURL = "https://" + Host + ":" + Port + "/ick%20%20%3F" + "??????%20%20%20%20" + "###???///???";
            Assert.IsTrue(expectedHttpsURL.Equals(escapedChars.CreateHttpsUrl(), StringComparison.Ordinal));

            path       = "/ick/bick/bark/ark/mark/hark";
            httpKeyURL = HttpKeyUri.BuildHttpKeyUri(serverPublicKey, Host, Port, path, extraValue);
            Assert.IsTrue(httpKeyURL.PathWithoutPublicKey.Equals(path, StringComparison.Ordinal));

            httpKeyURL = HttpKeyUri.BuildHttpKeyUri(httpKeyURL.ToString());
            Assert.IsTrue(httpKeyURL.PathWithoutPublicKey.Equals(path, StringComparison.Ordinal));
        }