Esempio n. 1
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()));
        }
Esempio n. 2
0
        public void testBasic()
        {
            PibMemory  pibImpl = new PibMemory();
            PibKeyImpl key11   = new PibKeyImpl(fixture.id1Key1Name,
                                                fixture.id1Key1.buf(), pibImpl);

            Assert.AssertTrue(fixture.id1Key1Name.equals(key11.getName()));
            Assert.AssertTrue(fixture.id1.equals(key11.getIdentityName()));
            Assert.AssertEquals(net.named_data.jndn.security.KeyType.RSA, key11.getKeyType());
            Assert.AssertTrue(key11.getPublicKey().equals(fixture.id1Key1));

            PibKeyImpl key11FromBackend = new PibKeyImpl(fixture.id1Key1Name,
                                                         pibImpl);

            Assert.AssertTrue(fixture.id1Key1Name.equals(key11FromBackend.getName()));
            Assert.AssertTrue(fixture.id1.equals(key11FromBackend.getIdentityName()));
            Assert.AssertEquals(net.named_data.jndn.security.KeyType.RSA, key11FromBackend.getKeyType());
            Assert.AssertTrue(key11FromBackend.getPublicKey().equals(fixture.id1Key1));
        }
Esempio n. 3
0
        /// <summary>
        /// Get the key with name keyName from the container.
        /// </summary>
        ///
        /// <param name="keyName">The name of the key.</param>
        /// <returns>The PibKey object.</returns>
        /// <exception cref="System.ArgumentException">if keyName does not match the identity name.</exception>
        /// <exception cref="Pib.Error">if the key does not exist.</exception>
        public PibKey get(Name keyName)
        {
            if (!identityName_.equals(net.named_data.jndn.security.pib.PibKey.extractIdentityFromKeyName(keyName)))
            {
                throw new ArgumentException("Key name `" + keyName.toUri()
                                            + "` does not match identity `" + identityName_.toUri()
                                            + "`");
            }

            PibKeyImpl pibKeyImpl = ILOG.J2CsMapping.Collections.Collections.Get(keys_, keyName);

            if (pibKeyImpl == null)
            {
                pibKeyImpl = new PibKeyImpl(keyName, pibImpl_);
                // Copy the Name.
                ILOG.J2CsMapping.Collections.Collections.Put(keys_, new Name(keyName), pibKeyImpl);
            }

            return(new PibKey(pibKeyImpl));
        }
Esempio n. 4
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());
        }
Esempio n. 5
0
        public void testErrors()
        {
            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");
            }

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

            try {
                new PibKeyImpl(new Name("/wrong"), pibImpl);
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_1) {
            } catch (Exception ex_2) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                new PibKeyImpl(new Name("/wrong"), fixture.id1Key1.buf(), pibImpl);
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_3) {
            } catch (Exception ex_4) {
                Assert.Fail("Did not throw the expected exception");
            }

            Blob wrongKey = new Blob("");

            try {
                new PibKeyImpl(fixture.id1Key2Name, wrongKey.buf(), pibImpl);
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_5) {
            } catch (Exception ex_6) {
                Assert.Fail("Did not throw the expected exception");
            }

            key11.addCertificate(fixture.id1Key1Cert1);
            try {
                key11.addCertificate(fixture.id1Key2Cert1);
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_7) {
            } catch (Exception ex_8) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                key11.removeCertificate(fixture.id1Key2Cert1.getName());
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_9) {
            } catch (Exception ex_10) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                key11.getCertificate(fixture.id1Key2Cert1.getName());
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_11) {
            } catch (Exception ex_12) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                key11.setDefaultCertificate(fixture.id1Key2Cert1);
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_13) {
            } catch (Exception ex_14) {
                Assert.Fail("Did not throw the expected exception");
            }

            try {
                key11.setDefaultCertificate(fixture.id1Key2Cert1.getName());
                Assert.Fail("Did not throw the expected exception");
            } catch (ArgumentException ex_15) {
            } catch (Exception ex_16) {
                Assert.Fail("Did not throw the expected exception");
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Create a PibKey which uses the impl backend implementation. This
 /// constructor should only be called by PibKeyContainer.
 /// </summary>
 ///
 public PibKey(PibKeyImpl impl)
 {
     impl_ = impl;
 }