/// <summary>
        /// Add a self-signed certificate made from the key and issuer ID.
        /// </summary>
        ///
        /// <param name="key">The key for the certificate.</param>
        /// <param name="issuerId">The issuer ID name component for the certificate name.</param>
        /// <returns>The new certificate.</returns>
        internal CertificateV2 addCertificate(PibKey key, String issuerId)
        {
            Name certificateName = new Name(key.getName());

            certificateName.append(issuerId).appendVersion(3);
            CertificateV2 certificate = new CertificateV2();

            certificate.setName(certificateName);

            // Set the MetaInfo.
            certificate.getMetaInfo().setType(net.named_data.jndn.ContentType.KEY);
            // One hour.
            certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0);

            // Set the content.
            certificate.setContent(key.getPublicKey());

            SigningInfo paras = new SigningInfo(key);
            // Validity period of 10 days.
            double now = net.named_data.jndn.util.Common.getNowMilliseconds();

            paras.setValidityPeriod(new ValidityPeriod(now, now + 10 * 24 * 3600
                                                       * 1000.0d));

            keyChain_.sign(certificate, paras);
            return(certificate);
        }
Пример #2
0
 /// <summary>
 /// Set this to type SignerType.KEY according to the given PibKey.
 /// This does not change the digest algorithm.
 /// </summary>
 ///
 /// <param name="key">key.getName().</param>
 /// <returns>This SigningInfo.</returns>
 public SigningInfo setPibKey(PibKey key)
 {
     reset(net.named_data.jndn.security.SigningInfo.SignerType.KEY);
     if (key != null)
     {
         name_ = key.getName();
     }
     key_ = key;
     return(this);
 }
Пример #3
0
        internal void makeCertificate(PibKey key, PibKey signer)
        {
            // Copy the default certificate.
            CertificateV2 request = new CertificateV2(key.getDefaultCertificate());

            request.setName(new Name(key.getName()).append("looper").appendVersion(
                                1));

            // Set SigningInfo.
            SigningInfo             // Set SigningInfo.
                paras = new SigningInfo(signer);
            // Validity period from 100 days before to 100 days after now.
            double now = net.named_data.jndn.util.Common.getNowMilliseconds();

            paras.setValidityPeriod(new ValidityPeriod(now - 100 * 24 * 3600
                                                       * 1000.0d, now + 100 * 24 * 3600 * 1000.0d));
            fixture_.keyChain_.sign(request, paras);
            fixture_.keyChain_.addCertificate(key, request);

            fixture_.cache_.insert(request);
        }
Пример #4
0
            public void processInterest(Interest interest, OnData onData,
                                        OnTimeout onTimeout, OnNetworkNack onNetworkNack)
            {
                try {
                    // Create another key for the same identity and sign it properly.
                    PibKey parentKey = outer_TestValidator.fixture_.keyChain_
                                       .createKey(outer_TestValidator.fixture_.subIdentity_);
                    PibKey requestedKey = outer_TestValidator.fixture_.subIdentity_.getKey(interest
                                                                                           .getName());

                    // Copy the Name.
                    Name certificateName = new Name(requestedKey.getName());
                    certificateName.append("looper").appendVersion(1);
                    CertificateV2 certificate = new CertificateV2();
                    certificate.setName(certificateName);

                    // Set the MetaInfo.
                    certificate.getMetaInfo().setType(net.named_data.jndn.ContentType.KEY);
                    // Set the freshness period to one hour.
                    certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0d);

                    // Set the content.
                    certificate.setContent(requestedKey.getPublicKey());

                    // Set SigningInfo.
                    SigningInfo                             // Set SigningInfo.
                        paras = new SigningInfo(parentKey);
                    // Validity period from 10 days before to 10 days after now.
                    double now = net.named_data.jndn.util.Common.getNowMilliseconds();
                    paras.setValidityPeriod(new ValidityPeriod(now - 10 * 24
                                                               * 3600 * 1000.0d, now + 10 * 24 * 3600 * 1000.0d));

                    outer_TestValidator.fixture_.keyChain_.sign(certificate, paras);
                    onData.onData(interest, certificate);
                } catch (Exception ex) {
                    Assert.Fail("Error in InfiniteCertificateChain: " + ex);
                }
            }
Пример #5
0
        public void testManagement()
        {
            Name identityName  = new Name("/test/id");
            Name identity2Name = new Name("/test/id2");

            Assert.AssertEquals(0, fixture_.keyChain_.getPib().getIdentities_().size());
            try {
                fixture_.keyChain_.getPib().getDefaultIdentity();
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex) {
            } catch (Exception ex_0) {
                Assert.Fail("Did not throw the expected exception");
            }

            // Create an identity.
            PibIdentity id = fixture_.keyChain_.createIdentityV2(identityName);

            Assert.AssertTrue(id != null);
            Assert.AssertTrue(fixture_.keyChain_.getPib().getIdentities_()
                              .getIdentities_().Contains(identityName));

            // The first added identity becomes the default identity.
            try {
                fixture_.keyChain_.getPib().getDefaultIdentity();
            } catch (Exception ex_1) {
                Assert.Fail("Unexpected exception: " + ex_1.Message);
            }

            // The default key of the added identity must exist.
            PibKey key = null;

            try {
                key = id.getDefaultKey();
            } catch (Exception ex_2) {
                Assert.Fail("Unexpected exception: " + ex_2.Message);
            }

            // The default certificate of the default key must exist.
            try {
                key.getDefaultCertificate();
            } catch (Exception ex_3) {
                Assert.Fail("Unexpected exception: " + ex_3.Message);
            }

            // Delete the key.
            Name key1Name = key.getName();

            try {
                id.getKey(key1Name);
            } catch (Exception ex_4) {
                Assert.Fail("Unexpected exception: " + ex_4.Message);
            }

            Assert.AssertEquals(1, id.getKeys_().size());
            fixture_.keyChain_.deleteKey(id, key);

            /* TODO: Implement key validity.
             *  // The key instance should not be valid anymore.
             *  assertTrue(!key);
             */

            try {
                id.getKey(key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_5) {
            } catch (Exception ex_6) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(0, id.getKeys_().size());

            // Create another key.
            fixture_.keyChain_.createKey(id);
            // The added key becomes the default key.
            try {
                id.getDefaultKey();
            } catch (Exception ex_7) {
                Assert.Fail("Unexpected exception: " + ex_7.Message);
            }

            PibKey key2 = id.getDefaultKey();

            Assert.AssertTrue(key2 != null);
            Assert.AssertTrue(!key2.getName().equals(key1Name));
            Assert.AssertEquals(1, id.getKeys_().size());
            try {
                key2.getDefaultCertificate();
            } catch (Exception ex_8) {
                Assert.Fail("Unexpected exception: " + ex_8.Message);
            }

            // Create a third key.
            PibKey key3 = fixture_.keyChain_.createKey(id);

            Assert.AssertTrue(!key3.getName().equals(key2.getName()));
            // The added key will not be the default key, because the default key already exists.
            Assert.AssertTrue(id.getDefaultKey().getName().equals(key2.getName()));
            Assert.AssertEquals(2, id.getKeys_().size());
            try {
                key3.getDefaultCertificate();
            } catch (Exception ex_9) {
                Assert.Fail("Unexpected exception: " + ex_9.Message);
            }

            // Delete the certificate.
            Assert.AssertEquals(1, key3.getCertificates_().size());
            CertificateV2 key3Cert1 = (CertificateV2)ILOG.J2CsMapping.Collections.Collections.ToArray(key3.getCertificates_()
                                                                                                      .getCertificates_().Values)[0];
            Name key3CertName = key3Cert1.getName();

            fixture_.keyChain_.deleteCertificate(key3, key3CertName);
            Assert.AssertEquals(0, key3.getCertificates_().size());
            try {
                key3.getDefaultCertificate();
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_10) {
            } catch (Exception ex_11) {
                Assert.Fail("Did not throw the expected exception");
            }

            // Add a certificate.
            fixture_.keyChain_.addCertificate(key3, key3Cert1);
            Assert.AssertEquals(1, key3.getCertificates_().size());
            try {
                key3.getDefaultCertificate();
            } catch (Exception ex_12) {
                Assert.Fail("Unexpected exception: " + ex_12.Message);
            }

            // Overwriting the certificate should work.
            fixture_.keyChain_.addCertificate(key3, key3Cert1);
            Assert.AssertEquals(1, key3.getCertificates_().size());
            // Add another certificate.
            CertificateV2 key3Cert2     = new CertificateV2(key3Cert1);
            Name          key3Cert2Name = new Name(key3.getName());

            key3Cert2Name.append("Self");
            key3Cert2Name.appendVersion(1);
            key3Cert2.setName(key3Cert2Name);
            fixture_.keyChain_.addCertificate(key3, key3Cert2);
            Assert.AssertEquals(2, key3.getCertificates_().size());

            // Set the default certificate.
            Assert.AssertTrue(key3.getDefaultCertificate().getName().equals(key3CertName));
            fixture_.keyChain_.setDefaultCertificate(key3, key3Cert2);
            Assert.AssertTrue(key3.getDefaultCertificate().getName().equals(key3Cert2Name));

            // Set the default key.
            Assert.AssertTrue(id.getDefaultKey().getName().equals(key2.getName()));
            fixture_.keyChain_.setDefaultKey(id, key3);
            Assert.AssertTrue(id.getDefaultKey().getName().equals(key3.getName()));

            // Set the default identity.
            PibIdentity id2 = fixture_.keyChain_.createIdentityV2(identity2Name);

            Assert.AssertTrue(fixture_.keyChain_.getPib().getDefaultIdentity().getName()
                              .equals(id.getName()));
            fixture_.keyChain_.setDefaultIdentity(id2);
            Assert.AssertTrue(fixture_.keyChain_.getPib().getDefaultIdentity().getName()
                              .equals(id2.getName()));

            // Delete an identity.
            fixture_.keyChain_.deleteIdentity(id);

            /* TODO: Implement identity validity.
             *  // The identity instance should not be valid any more.
             *  BOOST_CHECK(!id);
             */
            try {
                fixture_.keyChain_.getPib().getIdentity(identityName);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_13) {
            } catch (Exception ex_14) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertTrue(!fixture_.keyChain_.getPib().getIdentities_()
                              .getIdentities_().Contains(identityName));
        }
Пример #6
0
        public void testBasic()
        {
            PibMemory pibImpl = new PibMemory();

            // Start with an empty container.
            PibKeyContainer container = new PibKeyContainer(fixture.id1, pibImpl);

            Assert.AssertEquals(0, container.size());
            Assert.AssertEquals(0, container.getKeys_().Count);

            // Add the first key.
            PibKey key11 = container
                           .add(fixture.id1Key1.buf(), fixture.id1Key1Name);

            Assert.AssertTrue(fixture.id1Key1Name.equals(key11.getName()));
            Assert.AssertTrue(key11.getPublicKey().equals(fixture.id1Key1));
            Assert.AssertEquals(1, container.size());
            Assert.AssertEquals(1, container.getKeys_().Count);
            Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name));

            // Add the same key again.
            PibKey key12 = container
                           .add(fixture.id1Key1.buf(), fixture.id1Key1Name);

            Assert.AssertTrue(fixture.id1Key1Name.equals(key12.getName()));
            Assert.AssertTrue(key12.getPublicKey().equals(fixture.id1Key1));
            Assert.AssertEquals(1, container.size());
            Assert.AssertEquals(1, container.getKeys_().Count);
            Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name));

            // Add the second key.
            PibKey key21 = container
                           .add(fixture.id1Key2.buf(), fixture.id1Key2Name);

            Assert.AssertTrue(fixture.id1Key2Name.equals(key21.getName()));
            Assert.AssertTrue(key21.getPublicKey().equals(fixture.id1Key2));
            Assert.AssertEquals(2, container.size());
            Assert.AssertEquals(2, container.getKeys_().Count);
            Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key1Name));
            Assert.AssertTrue(container.getKeys_().Contains(fixture.id1Key2Name));

            // Get keys.
            try {
                container.get(fixture.id1Key1Name);
            } catch (Exception ex) {
                Assert.Fail("Unexpected exception: " + ex.Message);
            }
            try {
                container.get(fixture.id1Key2Name);
            } catch (Exception ex_0) {
                Assert.Fail("Unexpected exception: " + ex_0.Message);
            }
            Name id1Key3Name = net.named_data.jndn.security.pib.PibKey.constructKeyName(fixture.id1,
                                                                                        new Name.Component("non-existing-id"));

            try {
                container.get(id1Key3Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_1) {
            } catch (Exception ex_2) {
                Assert.Fail("Did not throw the expected exception");
            }

            // Get and check keys.
            PibKey key1 = container.get(fixture.id1Key1Name);
            PibKey key2 = container.get(fixture.id1Key2Name);

            Assert.AssertTrue(fixture.id1Key1Name.equals(key1.getName()));
            Assert.AssertTrue(key1.getPublicKey().equals(fixture.id1Key1));
            Assert.AssertEquals(fixture.id1Key2Name, key2.getName());
            Assert.AssertTrue(key2.getPublicKey().equals(fixture.id1Key2));

            // Create another container using the same PibImpl. The cache should be empty.
            PibKeyContainer container2 = new PibKeyContainer(fixture.id1, pibImpl);

            Assert.AssertEquals(2, container2.size());
            Assert.AssertEquals(0, container2.getKeys_().Count);

            // Get a key. The cache should be filled.
            try {
                container2.get(fixture.id1Key1Name);
            } catch (Exception ex_3) {
                Assert.Fail("Unexpected exception: " + ex_3.Message);
            }
            Assert.AssertEquals(2, container2.size());
            Assert.AssertEquals(1, container2.getKeys_().Count);

            try {
                container2.get(fixture.id1Key2Name);
            } catch (Exception ex_4) {
                Assert.Fail("Unexpected exception: " + ex_4.Message);
            }
            Assert.AssertEquals(2, container2.size());
            Assert.AssertEquals(2, container2.getKeys_().Count);

            // Remove a key.
            container2.remove(fixture.id1Key1Name);
            Assert.AssertEquals(1, container2.size());
            Assert.AssertEquals(1, container2.getKeys_().Count);
            Assert.AssertTrue(!container2.getKeys_().Contains(fixture.id1Key1Name));
            Assert.AssertTrue(container2.getKeys_().Contains(fixture.id1Key2Name));

            // Remove another key.
            container2.remove(fixture.id1Key2Name);
            Assert.AssertEquals(0, container2.size());
            Assert.AssertEquals(0, container2.getKeys_().Count);
            Assert.AssertTrue(!container2.getKeys_().Contains(fixture.id1Key2Name));
        }
Пример #7
0
        public void testKeyOperation()
        {
            PibMemory       pibImpl   = new PibMemory();
            PibIdentityImpl identity1 = new PibIdentityImpl(fixture.id1, pibImpl,
                                                            true);

            try {
                new PibIdentityImpl(fixture.id1, pibImpl, false);
            } catch (Exception ex) {
                Assert.Fail("Unexpected exception: " + ex.Message);
            }

            // The identity should not have any key.
            Assert.AssertEquals(0, identity1.getKeys_().size());

            // Getting non-existing key should throw Pib.Error.
            try {
                identity1.getKey(fixture.id1Key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_0) {
            } catch (Exception ex_1) {
                Assert.Fail("Did not throw the expected exception");
            }
            // Getting the default key should throw Pib.Error.
            try {
                identity1.getDefaultKey();
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_2) {
            } catch (Exception ex_3) {
                Assert.Fail("Did not throw the expected exception");
            }
            // Setting a non-existing key as the default key should throw Pib.Error.
            try {
                identity1.setDefaultKey(fixture.id1Key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_4) {
            } catch (Exception ex_5) {
                Assert.Fail("Did not throw the expected exception");
            }

            // Add a key.
            identity1.addKey(fixture.id1Key1.buf(), fixture.id1Key1Name);
            try {
                identity1.getKey(fixture.id1Key1Name);
            } catch (Exception ex_6) {
                Assert.Fail("Unexpected exception: " + ex_6.Message);
            }

            // A new key should become the default key when there is no default.
            try {
                identity1.getDefaultKey();
            } catch (Exception ex_7) {
                Assert.Fail("Unexpected exception: " + ex_7.Message);
            }
            PibKey defaultKey0 = identity1.getDefaultKey();

            Assert.AssertTrue(fixture.id1Key1Name.equals(defaultKey0.getName()));
            Assert.AssertTrue(defaultKey0.getPublicKey().equals(fixture.id1Key1));

            // Remove a key.
            identity1.removeKey(fixture.id1Key1Name);
            try {
                identity1.setDefaultKey(fixture.id1Key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_8) {
            } catch (Exception ex_9) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                identity1.getDefaultKey();
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_10) {
            } catch (Exception ex_11) {
                Assert.Fail("Did not throw the expected exception");
            }

            // Set the default key directly.
            try {
                identity1.setDefaultKey(fixture.id1Key1.buf(), fixture.id1Key1Name);
            } catch (Exception ex_12) {
                Assert.Fail("Unexpected exception: " + ex_12.Message);
            }
            try {
                identity1.getDefaultKey();
            } catch (Exception ex_13) {
                Assert.Fail("Unexpected exception: " + ex_13.Message);
            }
            try {
                identity1.getKey(fixture.id1Key1Name);
            } catch (Exception ex_14) {
                Assert.Fail("Unexpected exception: " + ex_14.Message);
            }

            // Check for a default key.
            PibKey defaultKey1 = identity1.getDefaultKey();

            Assert.AssertTrue(fixture.id1Key1Name.equals(defaultKey1.getName()));
            Assert.AssertTrue(defaultKey1.getPublicKey().equals(fixture.id1Key1));

            // Add another key.
            identity1.addKey(fixture.id1Key2.buf(), fixture.id1Key2Name);
            Assert.AssertEquals(2, identity1.getKeys_().size());

            // Set the default key using a name.
            try {
                identity1.setDefaultKey(fixture.id1Key2Name);
            } catch (Exception ex_15) {
                Assert.Fail("Unexpected exception: " + ex_15.Message);
            }
            try {
                identity1.getDefaultKey();
            } catch (Exception ex_16) {
                Assert.Fail("Unexpected exception: " + ex_16.Message);
            }
            PibKey defaultKey2 = identity1.getDefaultKey();

            Assert.AssertTrue(fixture.id1Key2Name.equals(defaultKey2.getName()));
            Assert.AssertTrue(defaultKey2.getPublicKey().equals(fixture.id1Key2));

            // Remove a key.
            identity1.removeKey(fixture.id1Key1Name);
            try {
                identity1.getKey(fixture.id1Key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_17) {
            } catch (Exception ex_18) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(1, identity1.getKeys_().size());

            // Seting the default key directly again should change the default.
            try {
                identity1.setDefaultKey(fixture.id1Key1.buf(), fixture.id1Key1Name);
            } catch (Exception ex_19) {
                Assert.Fail("Unexpected exception: " + ex_19.Message);
            }
            PibKey defaultKey3 = identity1.getDefaultKey();

            Assert.AssertTrue(fixture.id1Key1Name.equals(defaultKey3.getName()));
            Assert.AssertTrue(defaultKey3.getPublicKey().equals(fixture.id1Key1));
            Assert.AssertEquals(2, identity1.getKeys_().size());

            // Remove all keys.
            identity1.removeKey(fixture.id1Key1Name);
            try {
                identity1.getKey(fixture.id1Key1Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_20) {
            } catch (Exception ex_21) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(1, identity1.getKeys_().size());
            identity1.removeKey(fixture.id1Key2Name);
            try {
                identity1.getKey(fixture.id1Key2Name);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_22) {
            } catch (Exception ex_23) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(0, identity1.getKeys_().size());
            try {
                identity1.getDefaultKey();
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex_24) {
            } catch (Exception ex_25) {
                Assert.Fail("Did not throw the expected exception");
            }
        }