コード例 #1
0
    def securityContextLookup(self, kid):
        kidBuf = u.str2buf(kid)

        eui64 = kidBuf[:-1]
        senderID = eui64 + [0x01]  # sender ID of JRC is reversed
        recipientID = eui64 + [0x00]

        # if eui-64 is found in the list of joined nodes, return the appropriate context
        # this is important for replay protection
        for dict in self.joinResource.joinedNodes:
            if dict['eui64'] == u.buf2str(eui64):
                log.info(
                    "Node {0} found in joinedNodes. Returning context {1}.".
                    format(binascii.hexlify(dict['eui64']),
                           str(dict['context'])))
                return dict['context']

        # if eui-64 is not found, create a new tentative context but only add it to the list of joined nodes in the GET
        # handler of the join resource
        context = oscoap.SecurityContext(
            masterSecret=self.MASTERSECRET,
            senderID=u.buf2str(senderID),
            recipientID=u.buf2str(recipientID),
            aeadAlgorithm=oscoap.AES_CCM_16_64_128())

        log.info(
            "Node {0} not found in joinedNodes. Instantiating new context based on the master secret."
            .format(binascii.hexlify(u.buf2str(eui64))))

        return context
コード例 #2
0
        respOptions = []
        respPayload = [ord(b) for b in 'hello world 1 2 3 4 5 6 7 8 9 0']

        return (respCode, respOptions, respPayload)


# open
c = coap.coap(ipAddress='::1')

testResource = testResource()

context = oscoap.SecurityContext(
    masterSecret=binascii.unhexlify('000102030405060708090A0B0C0D0E0F'),
    senderID=binascii.unhexlify('736572766572'),
    recipientID=binascii.unhexlify('636c69656e74'),
    aeadAlgorithm=oscoap.AES_CCM_16_64_128())

# add resource - context binding with authorized methods
testResource.addSecurityBinding((context, d.METHOD_ALL))

# install resource
c.addResource(testResource)

for t in threading.enumerate():
    print t.name

# let the server run
raw_input('\n\nServer running. Press Enter to close.\n\n')

# close
c.close()