示例#1
0
        public void testSetters()
        {
            CertificateV2 certificate = new CertificateV2();

            certificate.setName(new Name(
                                    "/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"));
            certificate.getMetaInfo().setFreshnessPeriod(3600 * 1000.0d);
            certificate.setContent(new Blob(PUBLIC_KEY, false));
            certificate.setSignature(generateFakeSignature());

            Assert.AssertEquals(new Name(
                                    "/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"),
                                certificate.getName());
            Assert.AssertEquals(new Name("/ndn/site1/KEY/ksk-1416425377094"),
                                certificate.getKeyName());
            Assert.AssertEquals(new Name("/ndn/site1"), certificate.getIdentity());
            Assert.AssertEquals(new Name.Component("0123"), certificate.getIssuerId());
            Assert.AssertEquals(new Name.Component("ksk-1416425377094"),
                                certificate.getKeyId());
            Assert.AssertEquals(new Name("/ndn/site1/KEY/ksk-2516425377094"), net.named_data.jndn.KeyLocator
                                .getFromSignature(certificate.getSignature()).getKeyName());
            Assert.AssertEquals(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20141111T050000"), certificate
                                .getValidityPeriod().getNotBefore(), 0);
            Assert.AssertEquals(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20141111T060000"), certificate
                                .getValidityPeriod().getNotAfter(), 0);

            try {
                certificate.getPublicKey();
            } catch (Exception ex) {
                Assert.Fail(ex.Message);
            }
        }
        public void testFindByInterest()
        {
            anchorContainer.insert("group1", certificatePath1.FullName,
                                   400.0d);
            Interest interest = new Interest(identity1.getName());

            Assert.AssertTrue(anchorContainer.find(interest) != null);
            Interest interest1 = new Interest(identity1.getName().getPrefix(-1));

            Assert.AssertTrue(anchorContainer.find(interest1) != null);
            Interest interest2 = new Interest(
                new Name(identity1.getName()).appendVersion(1));

            Assert.AssertTrue(anchorContainer.find(interest2) == null);

            CertificateV2 certificate3 = fixture.addCertificate(
                identity1.getDefaultKey(), "3");
            CertificateV2 certificate4 = fixture.addCertificate(
                identity1.getDefaultKey(), "4");
            CertificateV2 certificate5 = fixture.addCertificate(
                identity1.getDefaultKey(), "5");

            CertificateV2 certificate3Copy = new CertificateV2(certificate3);

            anchorContainer.insert("group2", certificate3Copy);
            anchorContainer.insert("group3", certificate4);
            anchorContainer.insert("group4", certificate5);

            Interest      interest3        = new Interest(certificate3.getKeyName());
            CertificateV2 foundCertificate = anchorContainer.find(interest3);

            Assert.AssertTrue(foundCertificate != null);
            Assert.AssertTrue(interest3.getName().isPrefixOf(foundCertificate.getName()));
            Assert.AssertTrue(certificate3.getName().equals(foundCertificate.getName()));

            interest3.getExclude().appendComponent(
                certificate3.getName().get(net.named_data.jndn.security.v2.CertificateV2.ISSUER_ID_OFFSET));
            foundCertificate = anchorContainer.find(interest3);
            Assert.AssertTrue(foundCertificate != null);
            Assert.AssertTrue(interest3.getName().isPrefixOf(foundCertificate.getName()));
            Assert.AssertTrue(!foundCertificate.getName().equals(certificate3.getName()));
        }
示例#3
0
        public void testConstructor()
        {
            CertificateV2 certificate = new CertificateV2();

            certificate.wireDecode(new Blob(CERT, false));

            Assert.AssertEquals(new Name(
                                    "/ndn/site1/KEY/ksk-1416425377094/0123/%FD%00%00%01I%C9%8B"),
                                certificate.getName());
            Assert.AssertEquals(new Name("/ndn/site1/KEY/ksk-1416425377094"),
                                certificate.getKeyName());
            Assert.AssertEquals(new Name("/ndn/site1"), certificate.getIdentity());
            Assert.AssertEquals(new Name.Component("0123"), certificate.getIssuerId());
            Assert.AssertEquals(new Name.Component("ksk-1416425377094"),
                                certificate.getKeyId());
            Assert.AssertEquals(new Name("/ndn/site1/KEY/ksk-2516425377094"), net.named_data.jndn.KeyLocator
                                .getFromSignature(certificate.getSignature()).getKeyName());
            Assert.AssertEquals(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150814T223739"), certificate
                                .getValidityPeriod().getNotBefore(), 0);
            Assert.AssertEquals(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150818T223738"), certificate
                                .getValidityPeriod().getNotAfter(), 0);

            try {
                certificate.getPublicKey();
            } catch (Exception ex) {
                Assert.Fail(ex.Message);
            }

            Data data = new Data();

            data.wireDecode(new Blob(CERT, false));
            CertificateV2 certificate2 = new CertificateV2(data);

            Assert.AssertEquals(certificate.getName(), certificate2.getName());
            Assert.AssertTrue(certificate.getPublicKey().equals(
                                  certificate2.getPublicKey()));
        }
        /// <summary>
        /// Add certificate into the container. If the certificate already exists,
        /// this replaces it.
        /// </summary>
        ///
        /// <param name="certificate">The certificate to add. This copies the object.</param>
        /// <exception cref="System.ArgumentException">if the name of the certificate does notmatch the key name.</exception>
        public void add(CertificateV2 certificate)
        {
            if (!keyName_.equals(certificate.getKeyName()))
            {
                throw new ArgumentException("The certificate name `"
                                            + certificate.getKeyName().toUri()
                                            + "` does not match the key name");
            }

            Name certificateName = new Name(certificate.getName());

            ILOG.J2CsMapping.Collections.Collections.Add(certificateNames_, certificateName);
            // Copy the certificate.
            ILOG.J2CsMapping.Collections.Collections.Put(certificates_, certificateName, new CertificateV2(certificate));
            pibImpl_.addCertificate(certificate);
        }
示例#5
0
        public void testOverwrite()
        {
            PibMemory pibImpl = new PibMemory();

            try {
                new PibKeyImpl(fixture.id1Key1Name, pibImpl);
                Assert.Fail("Did not throw the expected exception");
            } catch (Pib.Error ex) {
            } catch (Exception ex_0) {
                Assert.Fail("Did not throw the expected exception");
            }

            new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key1.buf(), pibImpl);
            PibKeyImpl key1 = new PibKeyImpl(fixture.id1Key1Name, pibImpl);

            // Overwriting the key should work.
            new PibKeyImpl(fixture.id1Key1Name, fixture.id1Key2.buf(), pibImpl);
            PibKeyImpl key2 = new PibKeyImpl(fixture.id1Key1Name, pibImpl);

            // key1 should have cached the original public key.
            Assert.AssertTrue(!key1.getPublicKey().equals(key2.getPublicKey()));
            Assert.AssertTrue(key2.getPublicKey().equals(fixture.id1Key2));

            key1.addCertificate(fixture.id1Key1Cert1);
            // Use the wire encoding to check equivalence.
            Assert.AssertTrue(key1.getCertificate(fixture.id1Key1Cert1.getName())
                              .wireEncode().equals(fixture.id1Key1Cert1.wireEncode()));

            CertificateV2 otherCert = new CertificateV2(fixture.id1Key1Cert1);

            ((Sha256WithRsaSignature)otherCert.getSignature()).getValidityPeriod()
            .setPeriod(net.named_data.jndn.util.Common.getNowMilliseconds(),
                       net.named_data.jndn.util.Common.getNowMilliseconds() + 1000);
            // Don't bother resigning so we don't have to load a private key.

            Assert.AssertTrue(fixture.id1Key1Cert1.getName().equals(otherCert.getName()));
            Assert.AssertTrue(otherCert.getContent().equals(
                                  fixture.id1Key1Cert1.getContent()));
            Assert.AssertFalse(otherCert.wireEncode().equals(
                                   fixture.id1Key1Cert1.wireEncode()));

            key1.addCertificate(otherCert);

            Assert.AssertTrue(key1.getCertificate(fixture.id1Key1Cert1.getName())
                              .wireEncode().equals(otherCert.wireEncode()));
        }
示例#6
0
        /// <summary>
        /// Add the certificate. If a certificate with the same name (without implicit
        /// digest) already exists, then overwrite the certificate. If the key or
        /// identity does not exist, they will be created. If no default certificate
        /// for the key has been set, then set the added certificate as the default for
        /// the key. If no default key was set for the identity, it will be set as the
        /// default key for the identity. If no default identity was selected, the
        /// certificate's identity becomes the default.
        /// </summary>
        ///
        /// <param name="certificate">The certificate to add. This copies the object.</param>
        public override void addCertificate(CertificateV2 certificate)
        {
            Name certificateNameCopy = new Name(certificate.getName());
            // getKeyName already makes a new Name.
            Name keyNameCopy = certificate.getKeyName();
            Name identity    = certificate.getIdentity();

            addKey(identity, keyNameCopy, certificate.getContent().buf());

            try {
                ILOG.J2CsMapping.Collections.Collections.Put(certificates_, certificateNameCopy, new CertificateV2(
                                                                 certificate));
            } catch (CertificateV2.Error ex) {
                // We don't expect an error in the copy constructor.
                throw new PibImpl.Error(ex.Message);
            }
            if (!defaultCertificateNames_.Contains(keyNameCopy))
            {
                ILOG.J2CsMapping.Collections.Collections.Put(defaultCertificateNames_, keyNameCopy, certificateNameCopy);
            }
        }
示例#7
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));
        }
示例#8
0
        public void testCertificateOperation()
        {
            PibMemory  pibImpl = new PibMemory();
            PibKeyImpl key11   = new PibKeyImpl(fixture.id1Key1Name,
                                                fixture.id1Key1.buf(), pibImpl);

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

            // The key should not have any certificates.
            Assert.AssertEquals(0, key11.getCertificates_().size());

            // Getting a non-existing certificate should throw Pib.Error.
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
                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 non-existing default certificate should throw Pib.Error.
            try {
                key11.getDefaultCertificate();
                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 certificate as the default should throw Pib.Error.
            try {
                key11.setDefaultCertificate(fixture.id1Key1Cert1.getName());
                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 certificate.
            key11.addCertificate(fixture.id1Key1Cert1);
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
            } catch (Exception ex_6) {
                Assert.Fail("Unexpected exception: " + ex_6.Message);
            }

            // The new certificate becomes the default when there was no default.
            try {
                key11.getDefaultCertificate();
            } catch (Exception ex_7) {
                Assert.Fail("Unexpected exception: " + ex_7.Message);
            }
            CertificateV2 defaultCert0 = key11.getDefaultCertificate();

            Assert.AssertTrue(fixture.id1Key1Cert1.getName()
                              .equals(defaultCert0.getName()));
            // Use the wire encoding to check equivalence.
            Assert.AssertTrue(fixture.id1Key1Cert1.wireEncode().equals(
                                  defaultCert0.wireEncode()));

            // Remove the certificate.
            key11.removeCertificate(fixture.id1Key1Cert1.getName());
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
                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 {
                key11.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");
            }

            // Set the default certificate directly.
            try {
                key11.setDefaultCertificate(fixture.id1Key1Cert1);
            } catch (Exception ex_12) {
                Assert.Fail("Unexpected exception: " + ex_12.Message);
            }
            try {
                key11.getDefaultCertificate();
            } catch (Exception ex_13) {
                Assert.Fail("Unexpected exception: " + ex_13.Message);
            }
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
            } catch (Exception ex_14) {
                Assert.Fail("Unexpected exception: " + ex_14.Message);
            }

            // Check the default cert.
            CertificateV2 defaultCert1 = key11.getDefaultCertificate();

            Assert.AssertTrue(fixture.id1Key1Cert1.getName()
                              .equals(defaultCert1.getName()));
            Assert.AssertTrue(defaultCert1.wireEncode().equals(
                                  fixture.id1Key1Cert1.wireEncode()));

            // Add another certificate.
            key11.addCertificate(fixture.id1Key1Cert2);
            Assert.AssertEquals(2, key11.getCertificates_().size());

            // Set the default certificate using a name.
            try {
                key11.setDefaultCertificate(fixture.id1Key1Cert2.getName());
            } catch (Exception ex_15) {
                Assert.Fail("Unexpected exception: " + ex_15.Message);
            }
            try {
                key11.getDefaultCertificate();
            } catch (Exception ex_16) {
                Assert.Fail("Unexpected exception: " + ex_16.Message);
            }
            CertificateV2 defaultCert2 = key11.getDefaultCertificate();

            Assert.AssertTrue(fixture.id1Key1Cert2.getName()
                              .equals(defaultCert2.getName()));
            Assert.AssertTrue(defaultCert2.wireEncode().equals(
                                  fixture.id1Key1Cert2.wireEncode()));

            // Remove a certificate.
            key11.removeCertificate(fixture.id1Key1Cert1.getName());
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
                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, key11.getCertificates_().size());

            // Set the default certificate directly again, which should change the default.
            try {
                key11.setDefaultCertificate(fixture.id1Key1Cert1);
            } catch (Exception ex_19) {
                Assert.Fail("Unexpected exception: " + ex_19.Message);
            }
            CertificateV2 defaultCert3 = key11.getDefaultCertificate();

            Assert.AssertTrue(fixture.id1Key1Cert1.getName()
                              .equals(defaultCert3.getName()));
            Assert.AssertTrue(defaultCert3.wireEncode().equals(
                                  fixture.id1Key1Cert1.wireEncode()));
            Assert.AssertEquals(2, key11.getCertificates_().size());

            // Remove all certificates.
            key11.removeCertificate(fixture.id1Key1Cert1.getName());
            try {
                key11.getCertificate(fixture.id1Key1Cert1.getName());
                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, key11.getCertificates_().size());
            key11.removeCertificate(fixture.id1Key1Cert2.getName());
            try {
                key11.getCertificate(fixture.id1Key1Cert2.getName());
                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");
            }

            try {
                key11.getDefaultCertificate();
                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");
            }
            Assert.AssertEquals(0, key11.getCertificates_().size());
        }
示例#9
0
        /// <summary>
        /// Add the certificate. If a certificate with the same name (without implicit
        /// digest) already exists, then overwrite the certificate. If the key or
        /// identity does not exist, they will be created. If no default certificate
        /// for the key has been set, then set the added certificate as the default for
        /// the key. If no default key was set for the identity, it will be set as the
        /// default key for the identity. If no default identity was selected, the
        /// certificate's identity becomes the default.
        /// </summary>
        ///
        /// <param name="certificate">The certificate to add. This copies the object.</param>
        /// <exception cref="PibImpl.Error">for a non-semantic (database access) error.</exception>
        public override void addCertificate(CertificateV2 certificate)
        {
            // Ensure the key exists.
            Blob content = certificate.getContent();

            addKey(certificate.getIdentity(), certificate.getKeyName(),
                   content.buf());

            if (!hasCertificate(certificate.getName()))
            {
                try {
                    PreparedStatement statement = database_
                                                  .prepareStatement(net.named_data.jndn.security.pib.PibSqlite3Base.INSERT_addCertificate);
                    statement.setBytes(1, certificate.getKeyName().wireEncode()
                                       .getImmutableArray());
                    statement.setBytes(2, certificate.getName().wireEncode()
                                       .getImmutableArray());
                    statement.setBytes(3, certificate.wireEncode()
                                       .getImmutableArray());

                    try {
                        statement.executeUpdate();
                    } finally {
                        statement.close();
                    }
                } catch (SQLException exception) {
                    throw new PibImpl.Error("PibSqlite3: SQLite error: "
                                            + exception);
                }
            }
            else
            {
                try {
                    PreparedStatement statement_0 = database_
                                                    .prepareStatement(net.named_data.jndn.security.pib.PibSqlite3Base.UPDATE_addCertificate);
                    statement_0.setBytes(1, certificate.wireEncode()
                                         .getImmutableArray());
                    statement_0.setBytes(2, certificate.getName().wireEncode()
                                         .getImmutableArray());

                    try {
                        statement_0.executeUpdate();
                    } finally {
                        statement_0.close();
                    }
                } catch (SQLException exception_1) {
                    throw new PibImpl.Error("PibSqlite3: SQLite error: "
                                            + exception_1);
                }
            }

            if (!hasDefaultCertificateOfKey(certificate.getKeyName()))
            {
                try {
                    setDefaultCertificateOfKey(certificate.getKeyName(),
                                               certificate.getName());
                } catch (Pib.Error ex) {
                    throw new PibImpl.Error(
                              "PibSqlite3: Error setting the default certificate: "
                              + ex);
                }
            }
        }
示例#10
0
 /// <summary>
 /// Add the certificate and set it as the default certificate of the key.
 /// If a certificate with the same name (without implicit digest) already
 /// exists, then overwrite the certificate.
 /// </summary>
 ///
 /// <param name="certificate">The certificate to add. This copies the object.</param>
 /// <exception cref="System.ArgumentException">if the name of the certificate does notmatch the key name.</exception>
 /// <returns>The default certificate.</returns>
 public CertificateV2 setDefaultCertificate(CertificateV2 certificate)
 {
     addCertificate(certificate);
     return(setDefaultCertificate(certificate.getName()));
 }
        public void testInsert()
        {
            // Static
            anchorContainer.insert("group1", certificate1);
            Assert.AssertTrue(anchorContainer.find(certificate1.getName()) != null);
            Assert.AssertTrue(anchorContainer.find(identity1.getName()) != null);
            CertificateV2 certificate = anchorContainer
                                        .find(certificate1.getName());

            try {
                // Re-inserting the same certificate should do nothing.
                anchorContainer.insert("group1", certificate1);
            } catch (Exception ex) {
                Assert.Fail("Unexpected exception: " + ex.Message);
            }
            // It should still be the same instance of the certificate.
            Assert.AssertTrue(certificate == anchorContainer.find(certificate1.getName()));
            // Cannot add a dynamic group when the static already exists.
            try {
                anchorContainer.insert("group1",
                                       certificatePath1.FullName, 400.0d);
                Assert.Fail("Did not throw the expected exception");
            } catch (TrustAnchorContainer.Error ex_0) {
            } catch (Exception ex_1) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(1, anchorContainer.getGroup("group1").size());
            Assert.AssertEquals(1, anchorContainer.size());

            // From file
            anchorContainer.insert("group2", certificatePath2.FullName,
                                   400.0d);
            Assert.AssertTrue(anchorContainer.find(certificate2.getName()) != null);
            Assert.AssertTrue(anchorContainer.find(identity2.getName()) != null);
            try {
                anchorContainer.insert("group2", certificate2);
                Assert.Fail("Did not throw the expected exception");
            } catch (TrustAnchorContainer.Error ex_2) {
            } catch (Exception ex_3) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                anchorContainer.insert("group2",
                                       certificatePath2.FullName, 400.0d);
                Assert.Fail("Did not throw the expected exception");
            } catch (TrustAnchorContainer.Error ex_4) {
            } catch (Exception ex_5) {
                Assert.Fail("Did not throw the expected exception");
            }

            Assert.AssertEquals(1, anchorContainer.getGroup("group2").size());
            Assert.AssertEquals(2, anchorContainer.size());

            certificatePath2.delete();
            // Wait for the refresh period to expire.
            ILOG.J2CsMapping.Threading.ThreadWrapper.sleep(500);

            Assert.AssertTrue(anchorContainer.find(identity2.getName()) == null);
            Assert.AssertTrue(anchorContainer.find(certificate2.getName()) == null);
            Assert.AssertEquals(0, anchorContainer.getGroup("group2").size());
            Assert.AssertEquals(1, anchorContainer.size());

            TrustAnchorGroup group = anchorContainer.getGroup("group1");

            Assert.AssertTrue(group  is  StaticTrustAnchorGroup);
            StaticTrustAnchorGroup staticGroup = (StaticTrustAnchorGroup)group;

            Assert.AssertEquals(1, staticGroup.size());
            staticGroup.remove(certificate1.getName());
            Assert.AssertEquals(0, staticGroup.size());
            Assert.AssertEquals(0, anchorContainer.size());

            try {
                anchorContainer.getGroup("non-existing-group");
                Assert.Fail("Did not throw the expected exception");
            } catch (TrustAnchorContainer.Error ex_6) {
            } catch (Exception ex_7) {
                Assert.Fail("Did not throw the expected exception");
            }
        }