示例#1
0
    def signInterest(self, interest, keyName=None, wireFormat=None):
        # Adds the nonce and timestamp here, because there is no
        # 'makeCommandInterest' call for this yet
        nonceValue = bytearray(8)
        for i in range(8):
            nonceValue[i] = self.random.randint(0, 0xff)
        timestampValue = bytearray(8)
        ts = int(timestamp() * 1000)
        for i in range(8):
            byte = ts & 0xff
            timestampValue[-(i + 1)] = byte
            ts = ts >> 8

        if wireFormat is None:
            wireFormat = WireFormat.getDefaultWireFormat()

        s = HmacWithSha256Signature()
        s.getKeyLocator().setType(KeyLocatorType.KEYNAME)
        s.getKeyLocator().setKeyName(keyName)

        interestName = interest.getName()
        interestName.append(nonceValue).append(timestampValue)
        interestName.append(wireFormat.encodeSignatureInfo(s))
        interestName.append(Name.Component())

        encoding = interest.wireEncode(wireFormat)
        signer = hmac.new(self.key, encoding.toSignedBuffer(), sha256)

        s.setSignature(Blob(signer.digest()))
        interest.setName(
            interestName.getPrefix(-1).append(
                wireFormat.encodeSignatureValue(s)))
def main():
    data = Data()
    data.wireDecode(TlvData)

    # 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.verifyDataWithHmacWithSha256(data, key):
        dump("Hard-coded data signature verification: VERIFIED")
    else:
        dump("Hard-coded data signature verification: FAILED")

    freshData = Data(Name("/ndn/abc"))
    signature = HmacWithSha256Signature()
    signature.getKeyLocator().setType(KeyLocatorType.KEYNAME)
    signature.getKeyLocator().setKeyName(Name("key1"))
    freshData.setSignature(signature)
    freshData.setContent("SUCCESS!")
    dump("Signing fresh data packet", freshData.getName().toUri())
    KeyChain.signWithHmacWithSha256(freshData, key)

    if KeyChain.verifyDataWithHmacWithSha256(freshData, key):
        dump("Freshly-signed data signature verification: VERIFIED")
    else:
        dump("Freshly-signed data signature verification: FAILED")
示例#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))
示例#4
0
    def signData(self, data, keyName=None, wireFormat=None):
        data.setSignature(HmacWithSha256Signature())
        s = data.getSignature()

        s.getKeyLocator().setType(KeyLocatorType.KEYNAME)
        s.getKeyLocator().setKeyName(keyName)

        if wireFormat is None:
            wireFormat = WireFormat.getDefaultWireFormat()
        encoded = data.wireEncode(wireFormat)
        signer = hmac.new(self.key, bytearray(encoded.toSignedBuffer()),
                          sha256)
        s.setSignature(Blob(signer.digest()))
        data.wireEncode(wireFormat)
示例#5
0
    def onInterest(self, prefix, interest, face, interestFilterId, filter):

        print "Got interest for device ID list."

        file = open("%s" % (deviceIDListName), "r")
        deviceIDList = file.read()

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

        face.putData(data)
示例#6
0
    def onInterest(self, prefix, interest, face, interestFilterId, filter):

        print "Got interest for latest device seq num."

        deviceID = str(interest.getName().getSubName(-1, 1).toUri()[1:])
        
        file = open("../repo-ng/seq/%s.seq" % (deviceID), "r")
        deviceIDList = file.read()

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

        face.putData(data)
示例#7
0
def scanForNistSensors():

    scanner = Scanner().withDelegate(ScanDelegate())
    scanner.scan(.1)

    if foundNistSensor == 0:
        print "Didn't find any nist sensors..."
        return False

    p = Peripheral(esp32Address)
    p.setMTU(500)

    #svcList = p.getServices()
    #print "Handle   UUID                                Properties"
    #print "-------------------------------------------------------"
    #for svc in svcList:
    #      print (str(svc.uuid))
    
    #chList = p.getCharacteristics()
    #print "Handle   UUID                                Properties"
    #print "-------------------------------------------------------"
    #for ch in chList:
    #         print ("  0x"+ format(ch.getHandle(),'02X')  +"   "+str(ch.uuid) +" " + ch.propertiesToString())

    nist_service_uuid = UUID("0000ffe0-0000-1000-8000-00805f9b34fb")
    nist_characteristic_uuid = UUID("beb5483e-36e1-4688-b7f5-ea07361b26a8")

    nistService = p.getServiceByUUID(nist_service_uuid)
    #nistCharacteristic = p.getCharacteristics(nist_characteristic_uuid)[0]
    nistCharacteristic = nistService.getCharacteristics("beb5483e-36e1-4688-b7f5-ea07361b26a8")[0]

    #readBytes = bytes(p.readCharacteristic(0x2A))
    #readBytes = bytes(nistCharacteristic.read())

    #print binascii.hexlify(readBytes)

    #with open('/home/pi/Desktop/esp32-ndn-ble/src/readBytes.txt', 'a') as the_file:
    #      the_file.seek(0)
    #      the_file.truncate()
    #      the_file.write(binascii.hexlify(readBytes))

    #TlvData = Blob(readBytes)

    #data = Data()
    #data.wireDecode(TlvData)

    # 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.verifyDataWithHmacWithSha256(data, key):
    #  dump("Hard-coded data signature verification: VERIFIED")
    #else:
    #  dump("Hard-coded data signature verification: FAILED")

    freshData = Data(Name("/netInfo"))
    signature = HmacWithSha256Signature()
    signature.getKeyLocator().setType(KeyLocatorType.KEYNAME)
    signature.getKeyLocator().setKeyName(Name("key1"))
    freshData.setSignature(signature)
    freshData.setContent("EdwardPi\n11111111\n192.168.4.1\n")
    dump("Signing fresh data packet", freshData.getName().toUri())
    KeyChain.signWithHmacWithSha256(freshData, key)

    if KeyChain.verifyDataWithHmacWithSha256(freshData, key):
      dump("Freshly-signed data signature verification: VERIFIED")
    else:
      dump("Freshly-signed data signature verification: FAILED")

    bytesSend = freshData.wireEncode()

    print binascii.hexlify(bytes(bytesSend))

    try:
        nistCharacteristic.write(bytes(bytesSend), True)
    except:
        print "Exception when trying to write to BLE characteristic."