/// <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); }
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); } }
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)); }
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"); } }