def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)
    
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())
    
    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = Interest(Name("/ndn/abc"))
    freshInterest.setMustBeFresh(False)
    dump(freshInterest.toUri())
    freshInterest.setMinSuffixComponents(4)
    freshInterest.setMaxSuffixComponents(6)
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(bytearray(
      [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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.setInterestLifetimeMilliseconds(30000)
    freshInterest.setChildSelector(1)
    freshInterest.setMustBeFresh(True);
    freshInterest.setScope(2)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)
def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    # Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName())
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = Interest(Name("/ndn/abc"))
    freshInterest.setMustBeFresh(False)
    dump(freshInterest.toUri())
    freshInterest.setMinSuffixComponents(4)
    freshInterest.setMaxSuffixComponents(6)
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(bytearray(
      [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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.setInterestLifetimeMilliseconds(30000)
    freshInterest.setChildSelector(1)
    freshInterest.setMustBeFresh(True);
    freshInterest.setScope(2)

    identityStorage = MemoryIdentityStorage()
    privateKeyStorage = MemoryPrivateKeyStorage()
    keyChain = KeyChain(IdentityManager(identityStorage, privateKeyStorage),
                        SelfVerifyPolicyManager(identityStorage))

    # Initialize the storage.
    keyName = Name("/testname/DSK-123")
    certificateName = keyName.getSubName(0, keyName.size() - 1).append(
      "KEY").append(keyName[-1]).append("ID-CERT").append("0")
    identityStorage.addKey(keyName, KeyType.RSA, Blob(DEFAULT_RSA_PUBLIC_KEY_DER))
    privateKeyStorage.setKeyPairForKeyName(
      keyName, KeyType.RSA, DEFAULT_RSA_PUBLIC_KEY_DER, 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)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)

    keyChain.verifyInterest(
      reDecodedFreshInterest, makeOnVerified("Freshly-signed Interest"),
      makeOnVerifyFailed("Freshly-signed Interest"))
예제 #3
0
class TestInterestDump(ut.TestCase):
    def setUp(self):
        self.referenceInterest = Interest()
        self.referenceInterest.wireDecode(codedInterest)

    def test_dump(self):
        # see if the dump format is the same as we expect
        decodedDump = dumpInterest(self.referenceInterest)
        self.assertEqual(initialDump, decodedDump, 'Initial dump does not have expected format')

    def test_redecode(self):
        # check that we encode and decode correctly
        encoding = self.referenceInterest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertEqual(initialDump, redecodedDump, 'Re-decoded interest does not match original')

    def test_create_fresh(self):
        freshInterest = createFreshInterest()
        freshDump = dumpInterest(freshInterest)
        self.assertTrue(interestDumpsEqual(initialDump, freshDump), 'Fresh interest does not match original')

        reDecodedFreshInterest = Interest()
        reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
        reDecodedFreshDump = dumpInterest(reDecodedFreshInterest)

        self.assertTrue(interestDumpsEqual(freshDump, reDecodedFreshDump), 'Redecoded fresh interest does not match original')
예제 #4
0
class TestInterestDump(ut.TestCase):
    def setUp(self):
        self.referenceInterest = Interest()
        self.referenceInterest.wireDecode(codedInterest)

    def test_dump(self):
        # see if the dump format is the same as we expect
        decodedDump = dumpInterest(self.referenceInterest)
        self.assertEqual(initialDump, decodedDump,
                         'Initial dump does not have expected format')

    def test_redecode(self):
        # check that we encode and decode correctly
        encoding = self.referenceInterest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertEqual(initialDump, redecodedDump,
                         'Re-decoded interest does not match original')

    def test_create_fresh(self):
        freshInterest = createFreshInterest()
        freshDump = dumpInterest(freshInterest)
        self.assertTrue(interestDumpsEqual(initialDump, freshDump),
                        'Fresh interest does not match original')

        reDecodedFreshInterest = Interest()
        reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
        reDecodedFreshDump = dumpInterest(reDecodedFreshInterest)

        self.assertTrue(interestDumpsEqual(freshDump, reDecodedFreshDump),
                        'Redecoded fresh interest does not match original')
예제 #5
0
    def test_max_ndn_packet_size(self):
        # Construct an interest whose encoding is one byte larger than getMaxNdnPacketSize.
        targetSize = Face.getMaxNdnPacketSize() + 1
        # Start with an interest which is almost the right size.
        interest = Interest()
        interest.getName().append(bytearray(targetSize))
        initialSize = interest.wireEncode().size()
        # Now replace the component with the desired size which trims off the extra encoding.
        interest.setName(
          (Name().append(bytearray(targetSize - (initialSize - targetSize)))))
        interestSize = interest.wireEncode().size()
        self.assertEqual(targetSize, interestSize,
          "Wrong interest size for MaxNdnPacketSize")

        with self.assertRaises(RuntimeError):
            # If no error is raised, then expressInterest didn't throw an
            # exception when the interest size exceeds getMaxNdnPacketSize()
            self.face.expressInterest(interest, Mock(), Mock())
def main():
    # Silence the warning from Interest wire encode.
    Interest.setDefaultCanBePrefix(True)

    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    # Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName())
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = (Interest(Name("/ndn/abc"))
      .setMustBeFresh(False)
      .setMinSuffixComponents(4)
      .setMaxSuffixComponents(6)
      .setInterestLifetimeMilliseconds(30000)
      .setChildSelector(1)
      .setMustBeFresh(True))
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(bytearray(
      [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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.getForwardingHint().add(1, Name("/A"))
    dump(freshInterest.toUri())

    # Set up the KeyChain.
    keyChain = KeyChain("pib-memory:", "tpm-memory:")
    keyChain.importSafeBag(SafeBag
      (Name("/testname/KEY/123"),
       Blob(DEFAULT_RSA_PRIVATE_KEY_DER, False),
       Blob(DEFAULT_RSA_PUBLIC_KEY_DER, False)))
    validator = Validator(ValidationPolicyFromPib(keyChain.getPib()))

    # Make a Face just so that we can sign the interest.
    face = Face("localhost")
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
    face.makeCommandInterest(freshInterest)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)

    validator.validate(
      reDecodedFreshInterest, makeSuccessCallback("Freshly-signed Interest"),
      makeFailureCallback("Freshly-signed Interest"))
예제 #7
0
    def test_max_ndn_packet_size(self):
        # Construct an interest whose encoding is one byte larger than getMaxNdnPacketSize.
        targetSize = Face.getMaxNdnPacketSize() + 1
        # Start with an interest which is almost the right size.
        interest = Interest()
        interest.getName().append(bytearray(targetSize))
        initialSize = interest.wireEncode().size()
        # Now replace the component with the desired size which trims off the extra encoding.
        interest.setName(
          (Name().append(bytearray(targetSize - (initialSize - targetSize)))))
        interestSize = interest.wireEncode().size()
        self.assertEqual(targetSize, interestSize,
          "Wrong interest size for MaxNdnPacketSize")

        with self.assertRaises(RuntimeError):
            # If no error is raised, then expressInterest didn't throw an
            # exception when the interest size exceeds getMaxNdnPacketSize()
            self.face.expressInterest(interest, Mock(), Mock())
예제 #8
0
def main():
    # Silence the warning from Interest wire encode.
    Interest.setDefaultCanBePrefix(True)

    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    # Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName())
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = (Interest(
        Name("/ndn/abc")).setMustBeFresh(False).setMinSuffixComponents(
            4).setMaxSuffixComponents(6).setInterestLifetimeMilliseconds(
                30000).setChildSelector(1).setMustBeFresh(True))
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(
        bytearray([
            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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.getForwardingHint().add(1, Name("/A"))
    dump(freshInterest.toUri())

    # Set up the KeyChain.
    keyChain = KeyChain("pib-memory:", "tpm-memory:")
    keyChain.importSafeBag(
        SafeBag(Name("/testname/KEY/123"),
                Blob(DEFAULT_RSA_PRIVATE_KEY_DER, False),
                Blob(DEFAULT_RSA_PUBLIC_KEY_DER, False)))
    validator = Validator(ValidationPolicyFromPib(keyChain.getPib()))

    # Make a Face just so that we can sign the interest.
    face = Face("localhost")
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
    face.makeCommandInterest(freshInterest)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)

    validator.validate(reDecodedFreshInterest,
                       makeSuccessCallback("Freshly-signed Interest"),
                       makeFailureCallback("Freshly-signed Interest"))
def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    # Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName())
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = (Interest(
        Name("/ndn/abc")).setMustBeFresh(False).setMinSuffixComponents(
            4).setMaxSuffixComponents(6).setInterestLifetimeMilliseconds(
                30000).setChildSelector(1).setMustBeFresh(True))
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(
        bytearray([
            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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.getForwardingHint().add(1, Name("/A"))
    dump(freshInterest.toUri())

    # Set up the KeyChain.
    pibImpl = PibMemory()
    keyChain = KeyChain(pibImpl, TpmBackEndMemory(),
                        SelfVerifyPolicyManager(pibImpl))
    # This puts the public key in the pibImpl used by the SelfVerifyPolicyManager.
    keyChain.importSafeBag(
        SafeBag(Name("/testname/KEY/123"),
                Blob(DEFAULT_RSA_PRIVATE_KEY_DER, False),
                Blob(DEFAULT_RSA_PUBLIC_KEY_DER, False)))

    # Make a Face just so that we can sign the interest.
    face = Face("localhost")
    face.setCommandSigningInfo(keyChain, keyChain.getDefaultCertificateName())
    face.makeCommandInterest(freshInterest)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)

    keyChain.verifyInterest(reDecodedFreshInterest,
                            makeOnVerified("Freshly-signed Interest"),
                            makeOnValidationFailed("Freshly-signed Interest"))
예제 #10
0
    def test_redecode_implicit_digest_exclude(self):
        # Check that we encode and decode correctly with an implicit digest exclude.
        interest = Interest(Name("/A"))
        interest.getExclude().appendComponent(Name("/sha256digest=" +
          "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").get(0))
        dump = dumpInterest(interest)

        encoding = interest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertTrue(interestDumpsEqual(dump, redecodedDump),
          "Re-decoded interest does not match original")
예제 #11
0
    def test_redecode_implicit_digest_exclude(self):
        # Check that we encode and decode correctly with an implicit digest exclude.
        interest = Interest(Name("/A"))
        interest.getExclude().appendComponent(Name("/sha256digest=" +
          "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").get(0))
        dump = dumpInterest(interest)

        encoding = interest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertTrue(interestDumpsEqual(dump, redecodedDump),
          "Re-decoded interest does not match original")
def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = Interest(Name("/ndn/abc"))
    freshInterest.setMustBeFresh(False)
    dump(freshInterest.toUri())
    freshInterest.setMinSuffixComponents(4)
    freshInterest.setMaxSuffixComponents(6)
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(
        bytearray([
            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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    freshInterest.setInterestLifetimeMilliseconds(30000)
    freshInterest.setChildSelector(1)
    freshInterest.setMustBeFresh(True)
    freshInterest.setScope(2)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)
예제 #13
0
    def requestData(self):
        """
        Interest:
            Name: /ndn/no/ntnu/<device>/sensorPull/<nonce>
            Selector: KeyLocator = ID

        The ID of the requesting Device is stored in KeyLocator in the Interest, 

        Sign the Interest and send.
        """
        self.dataRequestStart = time.clock()
        # Session used in namePrefix
        session = str(int(round(util.getNowMilliseconds() / 1000.0)))
        self.name = Name(self.baseName).append("device2").append(
            "sensorPull").append(session)
        interest = Interest(self.name)
        self.ibs_scheme.signInterest(self.signature_master_public_key,
                                     self.signature_private_key,
                                     self.deviceName, interest)

        interest.wireEncode()
        logging.info("Expressing interest name: " + interest.toUri())
        self.face.expressInterest(interest, self.onData, self.onTimeout)
예제 #14
0
class TestInterestDump(ut.TestCase):
    def setUp(self):
        self.referenceInterest = Interest()
        self.referenceInterest.wireDecode(codedInterest)

    def test_dump(self):
        # see if the dump format is the same as we expect
        decodedDump = dumpInterest(self.referenceInterest)
        self.assertEqual(initialDump, decodedDump,
                         'Initial dump does not have expected format')

    def test_redecode(self):
        # check that we encode and decode correctly
        encoding = self.referenceInterest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertEqual(initialDump, redecodedDump,
                         'Re-decoded interest does not match original')

    def test_redecode_implicit_digest_exclude(self):
        # Check that we encode and decode correctly with an implicit digest exclude.
        interest = Interest(Name("/A"))
        interest.getExclude().appendComponent(
            Name(
                "/sha256digest=" +
                "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f"
            ).get(0))
        dump = dumpInterest(interest)

        encoding = interest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertTrue(interestDumpsEqual(dump, redecodedDump),
                        "Re-decoded interest does not match original")

    def test_create_fresh(self):
        freshInterest = createFreshInterest()
        freshDump = dumpInterest(freshInterest)
        self.assertTrue(interestDumpsEqual(initialDump, freshDump),
                        'Fresh interest does not match original')

        reDecodedFreshInterest = Interest()
        reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
        reDecodedFreshDump = dumpInterest(reDecodedFreshInterest)

        self.assertTrue(interestDumpsEqual(freshDump, reDecodedFreshDump),
                        'Redecoded fresh interest does not match original')
예제 #15
0
    def test_set_application_parameters(self):
        interest = Interest("/ndn")
        self.assertTrue(not interest.hasApplicationParameters())
        applicationParameters = Blob(bytearray([ 0x23, 0x00 ]))
        interest.setApplicationParameters(applicationParameters)
        self.assertTrue(interest.hasApplicationParameters())
        self.assertTrue(interest.getApplicationParameters().equals
                        (applicationParameters))

        decodedInterest = Interest()
        decodedInterest.wireDecode(interest.wireEncode())
        self.assertTrue(decodedInterest.getApplicationParameters().equals
                        (applicationParameters))

        interest.setApplicationParameters(Blob())
        self.assertTrue(not interest.hasApplicationParameters())
예제 #16
0
class TestInterestDump(ut.TestCase):
    def setUp(self):
        self.referenceInterest = Interest()
        self.referenceInterest.wireDecode(codedInterest)

    def test_dump(self):
        # see if the dump format is the same as we expect
        decodedDump = dumpInterest(self.referenceInterest)
        self.assertEqual(initialDump, decodedDump, 'Initial dump does not have expected format')

    def test_redecode(self):
        # check that we encode and decode correctly
        encoding = self.referenceInterest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertEqual(initialDump, redecodedDump, 'Re-decoded interest does not match original')

    def test_redecode_implicit_digest_exclude(self):
        # Check that we encode and decode correctly with an implicit digest exclude.
        interest = Interest(Name("/A"))
        interest.getExclude().appendComponent(Name("/sha256digest=" +
          "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").get(0))
        dump = dumpInterest(interest)

        encoding = interest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertTrue(interestDumpsEqual(dump, redecodedDump),
          "Re-decoded interest does not match original")

    def test_create_fresh(self):
        freshInterest = createFreshInterest()
        freshDump = dumpInterest(freshInterest)
        self.assertTrue(interestDumpsEqual(initialDump, freshDump), 'Fresh interest does not match original')

        reDecodedFreshInterest = Interest()
        reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
        reDecodedFreshDump = dumpInterest(reDecodedFreshInterest)

        self.assertTrue(interestDumpsEqual(freshDump, reDecodedFreshDump), 'Redecoded fresh interest does not match original')
예제 #17
0
class TestInterestDump(ut.TestCase):
    def setUp(self):
        self.referenceInterest = Interest()
        self.referenceInterest.wireDecode(codedInterest)

    def test_dump(self):
        # see if the dump format is the same as we expect
        decodedDump = dumpInterest(self.referenceInterest)
        self.assertEqual(initialDump, decodedDump, 'Initial dump does not have expected format')

    def test_redecode(self):
        # check that we encode and decode correctly
        encoding = self.referenceInterest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertEqual(initialDump, redecodedDump, 'Re-decoded interest does not match original')

    def test_redecode_implicit_digest_exclude(self):
        # Check that we encode and decode correctly with an implicit digest exclude.
        interest = Interest(Name("/A"))
        interest.getExclude().appendComponent(Name("/sha256digest=" +
          "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f").get(0))
        dump = dumpInterest(interest)

        encoding = interest.wireEncode()
        reDecodedInterest = Interest()
        reDecodedInterest.wireDecode(encoding)
        redecodedDump = dumpInterest(reDecodedInterest)
        self.assertTrue(interestDumpsEqual(dump, redecodedDump),
          "Re-decoded interest does not match original")

    def test_create_fresh(self):
        freshInterest = createFreshInterest()
        freshDump = dumpInterest(freshInterest)
        self.assertTrue(interestDumpsEqual(initialDump, freshDump), 'Fresh interest does not match original')

        reDecodedFreshInterest = Interest()
        reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
        reDecodedFreshDump = dumpInterest(reDecodedFreshInterest)

        self.assertTrue(interestDumpsEqual(freshDump, reDecodedFreshDump), 'Redecoded fresh interest does not match original')

    def test_no_selectors_must_be_fresh(self):
        interest = Interest()
        interest.wireDecode(codedInterestNoSelectors)
        self.assertEqual(False, interest.getMustBeFresh(),
          "MustBeFresh should be false if no selectors")

    def test_decode_v03_as_v02(self):
        interest1 = Interest()
        interest1.wireDecode(simpleCodedInterestV03)

        dump1 = dumpInterest(interest1)
        self.assertEqual(dump1, simpleCodedInterestV03Dump,
          "Decoded simpleCodedInterestV03 does not match the dump")

        interest2 = Interest()
        interest2.wireDecode(fullCodedInterestV03)

        dump2 = dumpInterest(interest2)
        self.assertEqual(dump2, fullCodedInterestV03Dump,
          "Decoded fullCodedInterestV03Dump does not match the dump")
예제 #18
0
    def onNewData(self, interest, data):
        """
        !! Again \n in public key??
        Got data:  {
            "ecdh-pub": "Aqxofe3QdsAfgbtS8TMxv31oudNKoSV307ci5gNXm88h\n",
            "salt": "12935684137560555161",
            "request-id": "14275252044236690531",
            "status": "0",
            "challenges": [
                {
                    "challenge-id": "Email"
                }
            ]
        }
        1. Verify data
        2. Derive shared secret
        """
        content = data.getContent()
        print("Got data: ", content)
        if not VerificationHelpers.verifyDataSignature(data, self.anchor):
            print("Cannot verify signature from: {}".format(self.caPrefix))
        else:
            print("Successfully verified data with hard-coded certificate")

        contentJson = json.loads(content.__str__())
        peerKeyBase64 = contentJson['ecdh-pub']
        self.status = contentJson['status']
        self.requestId = contentJson['request-id']
        self.challenges = contentJson['challenges']

        print(peerKeyBase64)

        serverPubKey = ec.EllipticCurvePublicKey.from_encoded_point(
            ec.SECP256R1(), b64decode(peerKeyBase64))

        shared_key = self.ecdh.private_key.exchange(ec.ECDH(), serverPubKey)
        derived_key = HKDF(algorithm=hashes.SHA256(),
                           length=32,
                           salt=contentJson['salt'].encode(),
                           info=b'handshake data',
                           backend=default_backend()).derive(shared_key)

        self.ecdh.derived_key = derived_key
        print(shared_key)
        for t in shared_key:
            print(t)

        challengeInterestName = Name(
            self.caPrefix).append("CA").append("_CHALLENGE").append(
                self.requestId)
        challengeInterest = Interest(challengeInterestName)
        challengeInterest.setMustBeFresh(True)
        challengeInterest.setCanBePrefix(False)

        # Encrypt the interest parameters
        challengeJson = json.dumps(
            {
                "selected-challenge": "Email",
                "email": "*****@*****.**"
            },
            indent=4)
        raw = self.pad(challengeJson, 16)
        print("raw", raw)
        iv = Random.new().read(AES.block_size)
        #cipher = AES.new(self.ecdh.derived_key, AES.MODE_CBC, iv)
        cipher = AES.new(shared_key, AES.MODE_CBC, iv)
        print(iv)
        xx = cipher.encrypt(raw)

        print(cipher.decrypt(xx))

        print("Printing iv:")
        for t in iv:
            print(t)

        encoder = TlvEncoder(256)
        saveLength = len(encoder)
        encoder.writeBlobTlv(632, iv)
        encoder.writeBlobTlv(630, cipher.encrypt(raw))
        #encoder.writeTypeAndLength(36, len(encoder) - saveLength)

        challengeInterest.setApplicationParameters(Blob(encoder.getOutput()))
        challengeInterest.appendParametersDigestToName()

        self.keyChain.sign(challengeInterest, SigningInfo(self.key))

        with open('foobar.tlv', 'wb') as f:
            f.write(challengeInterest.wireEncode().buf())
        self.face.expressInterest(challengeInterest, self.onChallengeData,
                                  self.onTimeout)
예제 #19
0
def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)
    dump("Interest:")
    dumpInterest(interest)

    # Set the name again to clear the cached encoding so we encode again.
    interest.setName(interest.getName())
    encoding = interest.wireEncode()
    dump("")
    dump("Re-encoded interest", encoding.toHex())

    reDecodedInterest = Interest()
    reDecodedInterest.wireDecode(encoding)
    dump("Re-decoded Interest:")
    dumpInterest(reDecodedInterest)

    freshInterest = (Interest(
        Name("/ndn/abc")).setMustBeFresh(False).setMinSuffixComponents(
            4).setMaxSuffixComponents(6).setInterestLifetimeMilliseconds(
                30000).setChildSelector(1).setMustBeFresh(True))
    freshInterest.getKeyLocator().setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
    freshInterest.getKeyLocator().setKeyData(
        bytearray([
            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.getExclude().appendComponent(Name("abc")[0]).appendAny()
    dump(freshInterest.toUri())

    identityStorage = MemoryIdentityStorage()
    privateKeyStorage = MemoryPrivateKeyStorage()
    keyChain = KeyChain(IdentityManager(identityStorage, privateKeyStorage),
                        SelfVerifyPolicyManager(identityStorage))

    # Initialize the storage.
    keyName = Name("/testname/DSK-123")
    certificateName = keyName.getSubName(
        0,
        keyName.size() - 1).append("KEY").append(
            keyName[-1]).append("ID-CERT").append("0")
    identityStorage.addKey(keyName, KeyType.RSA,
                           Blob(DEFAULT_RSA_PUBLIC_KEY_DER))
    privateKeyStorage.setKeyPairForKeyName(keyName, KeyType.RSA,
                                           DEFAULT_RSA_PUBLIC_KEY_DER,
                                           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)

    reDecodedFreshInterest = Interest()
    reDecodedFreshInterest.wireDecode(freshInterest.wireEncode())
    dump("")
    dump("Re-decoded fresh Interest:")
    dumpInterest(reDecodedFreshInterest)

    keyChain.verifyInterest(reDecodedFreshInterest,
                            makeOnVerified("Freshly-signed Interest"),
                            makeOnVerifyFailed("Freshly-signed Interest"))