public RSASSA_PSS(DataPrimitives dataPrimitives, CryptoPrimitives cryptoPrimitives, HashAlgorithm hash, MGF mgf)
 {
     this.dataPrimitives   = dataPrimitives;
     this.cryptoPrimitives = cryptoPrimitives;
     this.hash             = hash;
     this.mgf = mgf;
 }
        public static void SetUpBeforeClass(TestContext context)
        {
            config = Config.Instance;

            plainText      = PLAIN_TEXT_HEX.FromHexString();
            sig            = SIGNATURE_HEX.FromHexString();
            pemCert        = PEM_CERT_HEX.FromHexString();
            invalidPemCert = INVALID_PEM_CERT.FromHexString();

            crypto = new CryptoPrimitives();
            crypto.Init();

            // TODO should do this in @BeforeClass. Need to find out how to get to
            // files from static junit method

            testCACert = new X509Certificate2("Resources/ca.crt".Locate());
            crypto.Store.AddCertificate(testCACert);

            X509Certificate2 cert = GenerateFromCertAndPriv();


            crypto.Store.AddCertificate(cert);
            //        crypto.getTrustStore().setKeyEntry("key", key, "123456".toCharArray(), certificates);
            //        pem.close();
        }
Exemple #3
0
        public void TestClientTLSCACertProperties()
        {
            Properties testprops = new Properties();

            testprops.Set("pemFile", "fixture/testPems/caBundled.pems".Locate() + ", " +                            // has 3 certs
                          "fixture/testPems/Org1MSP_CA.pem".Locate());                                              // has 1

            testprops.Set("pemBytes", File.ReadAllText("fixture/testPems/Org2MSP_CA.pem".Locate(), Encoding.UTF8)); //Can have pem bytes too. 1 cert


            Endpoint         endpoint = new Endpoint("grpcs://localhost:594", testprops);
            CryptoPrimitives cp       = new CryptoPrimitives();

            cp.Store.AddCertificate(endpoint.creds.RootCertificates);

            List <X509Certificate2> certs = cp.Store.Certificates.Select(a => a.X509Certificate2).ToList();

            HashSet <BigInteger> expected = new HashSet <BigInteger>()
            {
                new BigInteger("4804555946196630157804911090140692961"),
                new BigInteger("127556113420528788056877188419421545986539833585"),
                new BigInteger("704500179517916368023344392810322275871763581896"),
                new BigInteger("70307443136265237483967001545015671922421894552"),
                new BigInteger("276393268186007733552859577416965113792")
            };

            foreach (X509Certificate2 cert in certs)
            {
                BigInteger serialNumber = new BigInteger(cert.SerialNumber.FromHexString());
                Assert.IsTrue(expected.Contains(serialNumber), $"Missing certificate {serialNumber}");
            }

            Assert.AreEqual(expected.Count, certs.Count, "Didn't find the expected number of certs"); // should have same number.
        }
        public void TestSetCryptoSuite()
        {
            HFCAClient client = HFCAClient.Create("client", "http://localhost:99", null);

            CryptoPrimitives testcrypt = new CryptoPrimitives();

            client.CryptoSuite = testcrypt;
            Assert.AreEqual(testcrypt, client.CryptoSuite);
        }
        public void TestValidationOfCertWithFabicCAattributes()
        {
            ICryptoSuite     cryptoSuite      = Factory.Instance.GetCryptoSuite();
            Certificate      onceFailingPem   = Certificate.Create(File.ReadAllText("Fixture/testPems/peerCert.pem".Locate()));
            CryptoPrimitives cryptoPrimitives = (CryptoPrimitives)cryptoSuite;

            cryptoPrimitives.Store.AddCertificateFromFile("fixture/testPems/caBundled.pems".Locate());
            Assert.IsTrue(cryptoPrimitives.Store.Validate(onceFailingPem));
        }
        public void TestDefaultCrypto()
        {
            ICryptoSuite     cryptoSuite = Factory.Instance.GetCryptoSuite();
            CryptoPrimitives primitives  = (CryptoPrimitives)cryptoSuite;

            Assert.AreEqual("secp256r1", primitives.curveName);
            Assert.AreEqual(256, primitives.securityLevel);
            Assert.AreEqual("SHA2", primitives.hashAlgorithm);
            // Should be exactly same instance as it has the same properties.
            Assert.AreEqual(cryptoSuite, Factory.Instance.GetCryptoSuite());
        }
Exemple #7
0
 public static void SetupBeforeClass(TestContext context)
 {
     try
     {
         crypto = new CryptoPrimitives();
         crypto.Init();
     }
     catch (System.Exception e)
     {
         throw new System.Exception("HFCAAffiliationTest.setupBeforeClass failed!", e);
     }
 }
 public void TestSetTrustStoreNull()
 {
     try
     {
         CryptoPrimitives myCrypto = new CryptoPrimitives();
         myCrypto.Store = null;
         Assert.Fail("setTrustStore(null) should have thrown exception");
     }
     catch (System.Exception)
     {
         // ignored
     }
 }
        public void TestRegisterNoServerResponse()
        {
            Properties testProps = new Properties();
            HFCAClient client    = HFCAClient.Create("client", "https://localhost:99", testProps);

            CryptoPrimitives testcrypt = new CryptoPrimitives();

            client.CryptoSuite = testcrypt;

            RegistrationRequest regreq = new RegistrationRequest("name", "affiliation");

            client.Register(regreq, admin);
        }
 public void TestGetTrustStore()
 {
     // getTrustStore should have created a KeyStore if setTrustStore hasn't
     // been called
     try
     {
         CryptoPrimitives myCrypto = new CryptoPrimitives();
         Assert.IsNotNull(myCrypto.Store);
     }
     catch (CryptoException e)
     {
         Assert.Fail($"getTrustStore() fails with : {e.Message}");
     }
 }
 public void TestSetTrustStore()
 {
     try
     {
         CryptoPrimitives myCrypto = new CryptoPrimitives();
         KeyStore         keyStore = new KeyStore();
         //     myCrypto.setTrustStore(keyStore);
         myCrypto.Store = keyStore;
         Assert.AreEqual(keyStore, myCrypto.Store);
     }
     catch (System.Exception e)
     {
         Assert.Fail($"testSetTrustStore() should not have thrown Exception. Error: {e.Message}");
     }
 }
        public void TestGetSetProperties()
        {
            Properties propsIn = new Properties();

            try
            {
                string expectHash = "SHA3"; // use something different than default!
                propsIn.Set(Config.SECURITY_LEVEL, "384");
                propsIn.Set(Config.HASH_ALGORITHM, expectHash);
                //    testCrypto.setProperties(propsIn);
                //   testCrypto.init();
                ICryptoSuite testCrypto = Factory.Instance.GetCryptoSuite(propsIn);

                //          Assert.AreEqual(BouncyCastleProvider.class, getField(testCrypto, "SECURITY_PROVIDER").getClass());

                string           expectedCurve = config.GetSecurityCurveMapping()[384];
                CryptoPrimitives original      = (CryptoPrimitives)testCrypto;
                Assert.AreEqual("secp384r1", expectedCurve);
                Assert.AreEqual(expectedCurve, original.curveName);
                Assert.AreEqual(384, original.securityLevel);
                Properties cryptoProps = original.GetProperties();
                Assert.AreEqual(cryptoProps[Config.SECURITY_LEVEL], "384");
                cryptoProps = testCrypto.GetProperties();
                Assert.AreEqual(cryptoProps[Config.HASH_ALGORITHM], expectHash);
                Assert.AreEqual(expectHash, original.hashAlgorithm);
                Assert.AreEqual(cryptoProps[Config.SECURITY_LEVEL], "384");

                // Should be exactly same instance as it has the same properties.
                Assert.AreEqual(testCrypto, Factory.Instance.GetCryptoSuite(propsIn));
            }
            catch (CryptoException e)
            {
                Assert.Fail($"TestGetSetProperties should not throw exception. Error: {e.Message}");
            }
            catch (ArgumentException e)
            {
                Assert.Fail($"TestGetSetProperties should not throw exception. Error: {e.Message}");
            }
        }
        public void TestTLSTrustedCertProperites()
        {
            Properties testprops = new Properties();

            testprops.Set("pemFile", "fixture/testPems/caBundled.pems".Locate() + "," + // has 3 certs
                          "fixture/testPems/Org1MSP_CA.pem".Locate());                  // has 1

            testprops.Set("pemBytes", File.ReadAllText("fixture/testPems/Org2MSP_CA.pem".Locate()));

            CryptoPrimitives cpto = new CryptoPrimitives();

            cpto.Init();

            HFCAClient client = HFCAClient.Create("client", "https://localhost:99", testprops);

            client.CryptoSuite = cpto;
            client.SetUpSSL();
            int               count      = 0;
            KeyStore          trustStore = client.CryptoSuite.Store;
            List <BigInteger> expected   = new List <BigInteger>
            {
                new BigInteger("4804555946196630157804911090140692961"),
                new BigInteger("127556113420528788056877188419421545986539833585"),
                new BigInteger("704500179517916368023344392810322275871763581896"),
                new BigInteger("70307443136265237483967001545015671922421894552"),
                new BigInteger("276393268186007733552859577416965113792")
            };

            foreach (X509Certificate2 cert in trustStore.Certificates.Select(a => a.X509Certificate2))
            {
                BigInteger serialNumber = new BigInteger(cert.SerialNumber.FromHexString());
                Assert.IsTrue(expected.Contains(serialNumber), $"Missing certifiate with serial no. {serialNumber}");
                ++count;
            }

            Assert.AreEqual(expected.Count, count, "Number of CA certificates mismatch");
        }
        public void TestSetHashAlgorithmBadArg()
        {
            CryptoPrimitives testCrypto = new CryptoPrimitives();

            testCrypto.SetHashAlgorithm("FAKE");
        }
        public void TestKeyGenBadCrypto()
        {
            CryptoPrimitives tmpCrypto = new CryptoPrimitives();

            tmpCrypto.KeyGen();
        }
        public void TestSecurityLevel()
        {
            CryptoPrimitives testCrypto = new CryptoPrimitives();

            testCrypto.SetSecurityLevel(2001);
        }
        public void TestSetHashAlgorithm()
        {
            CryptoPrimitives testCrypto = new CryptoPrimitives();

            testCrypto.SetHashAlgorithm(null);
        }
Exemple #18
0
 public RSAES_PKCS1()
 {
     dataPrimitives   = new DataPrimitives();
     cryptoPrimitives = new CryptoPrimitives();
 }
Exemple #19
0
 public void SetUp()
 {
     dataPrimitives   = new DataPrimitives();
     cryptoPrimitives = new CryptoPrimitives();
     keyGenerator     = new KeyGenerator();
 }