Esempio n. 1
0
TEST_F(TestInterestMethods, VerifyDigestSha256)
{
  // Create a KeyChain but we don't need to add keys.
  ptr_lib::shared_ptr<MemoryIdentityStorage> identityStorage
    (new MemoryIdentityStorage());
  KeyChain keyChain
    (ptr_lib::make_shared<IdentityManager>
      (identityStorage, ptr_lib::make_shared<MemoryPrivateKeyStorage>()),
     ptr_lib::make_shared<SelfVerifyPolicyManager>(identityStorage.get()));

  ptr_lib::shared_ptr<Interest> interest(new Interest(Name("/test/signed-interest")));
  keyChain.signWithSha256(*interest);

  VerifyCounter counter;
  keyChain.verifyInterest
    (interest, bind(&VerifyCounter::onVerified, &counter, _1),
     // Cast to disambiguate from the deprecated OnVerifyInterestFailed.
     (const OnInterestValidationFailed)bind
       (&VerifyCounter::onInterestValidationFailed, &counter, _1, _2));
  ASSERT_EQ(counter.onValidationFailedCallCount_, 0) << "Signature verification failed";
  ASSERT_EQ(counter.onVerifiedCallCount_, 1) << "Verification callback was not used.";
}
int main(int argc, char** argv)
{
  try {
    Interest interest;
    interest.wireDecode(TlvInterest, sizeof(TlvInterest));
    cout << "Interest:" << endl;
    dumpInterest(interest);

    // Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName());
    Blob encoding = interest.wireEncode();
    cout << endl << "Re-encoded interest " << encoding.toHex() << endl;

    Interest reDecodedInterest;
    reDecodedInterest.wireDecode(encoding);
    cout << "Re-decoded Interest:" << endl;
    dumpInterest(reDecodedInterest);

    Interest freshInterest(Name("/ndn/abc"));
    freshInterest.setMinSuffixComponents(4)
      .setMaxSuffixComponents(6)
      .setInterestLifetimeMilliseconds(30000)
      .setChildSelector(1)
      .setMustBeFresh(true);
    freshInterest.getKeyLocator().setType(ndn_KeyLocatorType_KEY_LOCATOR_DIGEST);
    uint8_t digest[] = {
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F };
    freshInterest.getKeyLocator().setKeyData(Blob(digest, sizeof(digest)));
    freshInterest.getExclude().appendComponent(Name("abc")[0]).appendAny();

    ptr_lib::shared_ptr<MemoryIdentityStorage> identityStorage
      (new MemoryIdentityStorage());
    ptr_lib::shared_ptr<MemoryPrivateKeyStorage> privateKeyStorage
      (new MemoryPrivateKeyStorage());
    KeyChain keyChain
      (ptr_lib::make_shared<IdentityManager>(identityStorage, privateKeyStorage),
       ptr_lib::make_shared<SelfVerifyPolicyManager>(identityStorage.get()));

    // Initialize the storage.
    Name keyName("/testname/DSK-123");
    Name certificateName = keyName.getSubName(0, keyName.size() - 1).append
      ("KEY").append(keyName[-1]).append("ID-CERT").append("0");
    identityStorage->addKey
      (keyName, KEY_TYPE_RSA, Blob(DEFAULT_RSA_PUBLIC_KEY_DER,
       sizeof(DEFAULT_RSA_PUBLIC_KEY_DER)));
    privateKeyStorage->setKeyPairForKeyName
      (keyName, KEY_TYPE_RSA, DEFAULT_RSA_PUBLIC_KEY_DER,
       sizeof(DEFAULT_RSA_PUBLIC_KEY_DER), DEFAULT_RSA_PRIVATE_KEY_DER,
       sizeof(DEFAULT_RSA_PRIVATE_KEY_DER));

    // Make a Face just so that we can sign the interest.
    Face face("localhost");
    face.setCommandSigningInfo(keyChain, certificateName);
    face.makeCommandInterest(freshInterest);

    ptr_lib::shared_ptr<Interest> reDecodedFreshInterest(new Interest());
    reDecodedFreshInterest->wireDecode(freshInterest.wireEncode());
    cout << endl << "Re-decoded fresh Interest:" << endl;
    dumpInterest(*reDecodedFreshInterest);

    keyChain.verifyInterest
      (reDecodedFreshInterest, bind(&onVerified, "Freshly-signed Interest", _1),
       bind(&onVerifyFailed, "Freshly-signed Interest", _1));
  } catch (std::exception& e) {
    cout << "exception: " << e.what() << endl;
  }
  return 0;
}