def main():
    interest = Interest()
    interest.wireDecode(TlvInterest)

    # Use a hard-wired secret for testing. In a real application the signer
    # ensures that the verifier knows the shared key and its keyName.
    key = Blob(bytearray([
       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    ]))

    if KeyChain.verifyInterestWithHmacWithSha256(interest, key):
      dump("Hard-coded interest signature verification: VERIFIED")
    else:
      dump("Hard-coded interest signature verification: FAILED")

    freshInterest = Interest(Name("/ndn/abc"))
    freshInterest.setMustBeFresh(False)
    keyName = Name("key1")
    dump("Signing fresh interest", freshInterest.getName().toUri())
    KeyChain.signWithHmacWithSha256(freshInterest, key, keyName)

    if KeyChain.verifyInterestWithHmacWithSha256(freshInterest, key):
      dump("Freshly-signed interest signature verification: VERIFIED")
    else:
      dump("Freshly-signed interest signature verification: FAILED")
def main():
    # Silence the warning from Interest wire encode.
    Interest.setDefaultCanBePrefix(True)

    interest = Interest()
    interest.wireDecode(TlvInterest)

    # Use a hard-wired secret for testing. In a real application the signer
    # ensures that the verifier knows the shared key and its keyName.
    key = Blob(bytearray([
       0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
      16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
    ]))

    if KeyChain.verifyInterestWithHmacWithSha256(interest, key):
      dump("Hard-coded interest signature verification: VERIFIED")
    else:
      dump("Hard-coded interest signature verification: FAILED")

    freshInterest = Interest(Name("/ndn/abc"))
    freshInterest.setMustBeFresh(False)
    keyName = Name("key1")
    dump("Signing fresh interest", freshInterest.getName().toUri())
    KeyChain.signWithHmacWithSha256(freshInterest, key, keyName)

    if KeyChain.verifyInterestWithHmacWithSha256(freshInterest, key):
      dump("Freshly-signed interest signature verification: VERIFIED")
    else:
      dump("Freshly-signed interest signature verification: FAILED")
Exemplo n.º 3
0
    def onInterest(self, prefix, interest, face, interestFilterId, filter):

        key = Blob(
            bytearray([
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
                18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31
            ]))

        print "Got onboarding interest with name: %s" % (
            interest.getName().toUri())

        try:
            if KeyChain.verifyInterestWithHmacWithSha256(interest, key):
                dump("Onboarding interest signature verification: VERIFIED")
            else:
                dump("Onboarding interest signature verification: FAILED")
        except:
            print "Exception when attempting to verify onboarding interest signature."

        data = Data(interest.getName())
        signature = HmacWithSha256Signature()
        signature.getKeyLocator().setType(KeyLocatorType.KEYNAME)
        signature.getKeyLocator().setKeyName(Name("key1"))
        data.setSignature(signature)
        data.setContent("")
        dump("Signing onboarding response data packet", data.getName().toUri())
        KeyChain.signWithHmacWithSha256(data, key)

        deviceID = str(interest.getName().getSubName(-3, 1).toUri()[1:])
        deviceIP = str(interest.getName().getSubName(-4, 1).toUri()[1:])

        print "Device ip: %s" % (deviceIP)
        print "Device ID: %s" % (deviceID)

        routeToRegister = str(Name(deviceID))

        registerRouteWithNameAndIp(routeToRegister, deviceIP)

        thread = threading.Thread(target=run_data_fetcher, args=(deviceID))
        thread.daemon = True  # Daemonize thread
        thread.start()

        #commandRouteToRegister = "/device/command/" + deviceID

        #registerRouteWithNameAndIp(commandRouteToRegister, deviceIP)

        face.putData(data)

        with open('%s' % (deviceIDListName), 'a') as the_file:
            the_file.seek(0)
            read_file = open('%s' % (deviceIDListName), 'r')
            if deviceID not in read_file.read():
                the_file.write('%s\n' % (deviceID))
Exemplo n.º 4
0
    def _onInterest(self, prefix, interest, face, interestFilterId, filter):
        """
        Process a received broadcast interest.
        """
        # Verify the HMAC signature.
        verified = False
        try:
            verified = KeyChain.verifyInterestWithHmacWithSha256(
                interest, self._hmacKey)
        except:
            # Treat a decoding failure as verification failure.
            pass
        if not verified:
            # Signature verification failure.
            logging.getLogger(__name__).info(
                "Dropping Interest with failed signature: %s",
                interest.getName().toUri())
            return

        encoding = interest.getName().get(
            self._applicationBroadcastPrefix.size()).getValue()
        receivedStateVector = StateVectorSync2018.decodeStateVector(encoding)
        logging.getLogger(__name__).info("Received broadcast state vector %s",
                                         str(receivedStateVector))

        (syncStates, needToReply) = self._mergeStateVector(receivedStateVector)
        if len(syncStates) > 0:
            # Inform the application up new sync states.
            try:
                self._onReceivedSyncState(syncStates)
            except:
                logging.exception("Error in onReceivedSyncState")

        if needToReply:
            # Inform other members who may need to be updated.
            logging.getLogger(__name__).info(
                "Received state vector was outdated. Broadcast state vector %s",
                str(self._stateVector))
            self._broadcastStateVector()