void KeyChain::sign (Interest& interest, const Name& certificateName, WireFormat& wireFormat) { // TODO: Handle signature algorithms other than Sha256WithRsa. Sha256WithRsaSignature signature; signature.getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); signature.getKeyLocator().setKeyName(certificateName.getPrefix(-1)); // Append the encoded SignatureInfo. interest.getName().append(wireFormat.encodeSignatureInfo(signature)); // Append an empty signature so that the "signedPortion" is correct. interest.getName().append(Name::Component()); // Encode once to get the signed portion, and sign. SignedBlob encoding = interest.wireEncode(wireFormat); ptr_lib::shared_ptr<Signature> signedSignature = sign (encoding.signedBuf(), encoding.signedSize(), certificateName); // Remove the empty signature and append the real one. interest.setName(interest.getName().getPrefix(-1).append (wireFormat.encodeSignatureValue(*signedSignature))); }
TEST_F(TestInterestMethods, MatchesData) { Interest interest(Name("/A")); interest.setMinSuffixComponents(2); interest.setMaxSuffixComponents(2); interest.getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); interest.getKeyLocator().setKeyName(Name("/B")); interest.getExclude().appendComponent(Name::Component("J")); interest.getExclude().appendAny(); Data data(Name("/A/D")); Sha256WithRsaSignature signature; signature.getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); signature.getKeyLocator().setKeyName(Name("/B")); data.setSignature(signature); ASSERT_EQ(true, interest.matchesData(data)); // Check violating MinSuffixComponents. Data data1(data); data1.setName(Name("/A")); ASSERT_EQ(false, interest.matchesData(data1)); Interest interest1(interest); interest1.setMinSuffixComponents(1); ASSERT_EQ(true, interest1.matchesData(data1)); // Check violating MaxSuffixComponents. Data data2(data); data2.setName(Name("/A/E/F")); ASSERT_EQ(false, interest.matchesData(data2)); Interest interest2(interest); interest2.setMaxSuffixComponents(3); ASSERT_EQ(true, interest2.matchesData(data2)); // Check violating PublisherPublicKeyLocator. Data data3(data); Sha256WithRsaSignature signature3; signature3.getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); signature3.getKeyLocator().setKeyName(Name("/G")); data3.setSignature(signature3); ASSERT_EQ(false, interest.matchesData(data3)); Interest interest3(interest); interest3.getKeyLocator().setType(ndn_KeyLocatorType_KEYNAME); interest3.getKeyLocator().setKeyName(Name("/G")); ASSERT_EQ(true, interest3.matchesData(data3)); Data data4(data); data4.setSignature(DigestSha256Signature()); ASSERT_EQ(false, interest.matchesData(data4)); Interest interest4(interest); interest4.setKeyLocator(KeyLocator()); ASSERT_EQ(true, interest4.matchesData(data4)); // Check violating Exclude. Data data5(data); data5.setName(Name("/A/J")); ASSERT_EQ(false, interest.matchesData(data5)); Interest interest5(interest); interest5.getExclude().clear(); interest5.getExclude().appendComponent(Name::Component("K")); interest5.getExclude().appendAny(); ASSERT_EQ(true, interest5.matchesData(data5)); // Check violating Name. Data data6(data); data6.setName(Name("/H/I")); ASSERT_EQ(false, interest.matchesData(data6)); Data data7(data); data7.setName(Name("/A/B")); Interest interest7 (Name("/A/B/sha256digest=" "54008e240a7eea2714a161dfddf0dd6ced223b3856e9da96792151e180f3b128")); ASSERT_EQ(true, interest7.matchesData(data7)); // Check violating the implicit digest. Interest interest7b (Name("/A/B/%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00" "%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00%00")); ASSERT_EQ(false, interest7b.matchesData(data7)); // Check excluding the implicit digest. Interest interest8(Name("/A/B")); interest8.getExclude().appendComponent(interest7.getName().get(2)); ASSERT_EQ(false, interest8.matchesData(data7)); }