i
modified: base_node.py
deleted: commands/__init__.py
deleted: commands/cert-request.proto
deleted: commands/cert_request_pb2.py
deleted: commands/configure-device.proto
deleted: commands/configure_device_pb2.py
deleted: commands/send-pairing-info.proto
deleted: commands/send_pairing_info_pb2.py
deleted: commands/update-capabilities.proto
deleted: commands/update_capabilities_pb2.py
modified: controller.py
modified: end-device.py
diff --git a/end-device.py b/end-device.py
index 0f085e7..fb97a23 100644
--- a/end-device.py
+++ b/end-device.py
@@ -19,14 +19,15 @@
import time
import json
-from pyndn import Name
-from pyndn import Face
-from pyndn import Interest
+from pyndn import Name, Face, Interest, Data, ThreadsafeFace
from pyndn import KeyLocator, KeyLocatorType
from base_node import BaseNode
from pyndn.security.security_exception import SecurityException
from pyndn.util import Blob
-
+try:
+ import asyncio
+except ImportError:
+ import trollius as asyncio
def dump(*list):
result = ""
@@ -35,88 +36,78 @@
print(result)
class Device(BaseNode):
- def __init__(self,configFileName,face):
+ def __init__(self,configFileName):
super(Device, self).__init__(configFileName=configFileName)
- #self.deviceSerial = self.getSerial()
+ self._deviceSerial = self.getSerial()
self._callbackCount = 0
- self._face = face
+ self._symKey = "symmetricKeyForBootstrapping"
+ self._category = "sensors"
+ self._id = "T9273659"
- def onData(self, interest, data):
- self._callbackCount += 1
- dump("Got data packet with name : ", data.getName().toUri())
- # Use join to convert each byte to chr.
- dump(data.getContent().toRawStr())
-
- #haven't checked symmetric key digest yet
- if (data.getName().toUri().startswith(self._bootstrapPrefix)):
- self.onBootstrapData(interest, data)
def expressBootstrapInterest(self):
- symKey = "symmetricKeyForBootstrapping"
- #generate bootstrap name /home/controller/bootstrap/<device-parameters>
- bootstrapName = Name(device._bootstrapPrefix)
+ #generate bootstrap name /home/controller/bootstrap/<device-parameters>
+ bootstrapName = Name(self._bootstrapPrefix)
deviceParameters = {}
- deviceParameters["category"] = "sensors"
- deviceParameters["id"] = "T123456789"
+ deviceParameters["category"] = self._category
+ deviceParameters["id"] = self._deviceSerial
bootstrapName.append(json.dumps(deviceParameters))
bootstrapInterest = Interest(bootstrapName)
-
bootstrapInterest.setInterestLifetimeMilliseconds(5000)
-
bootstrapKeyLocator = KeyLocator()
bootstrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
- bootstrapKeyLocator.setKeyData(symKey)
+ bootstrapKeyLocator.setKeyData(self._symKey)
bootstrapInterest.setKeyLocator(bootstrapKeyLocator)
- dump("Express interest :",bootstrapInterest.toUri())
- self._face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout)
+ dump("Express bootstrap interest : ",bootstrapInterest.toUri())
+ self.face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout)
def onBootstrapData(self, interest, data):
- dump("Data received.")
- content = json.loads(data.getContent().toRawStr(), encoding="latin-1")
- deviceNewIdentity = Name(content["deviceNewIdentity"])
- controllerIdentity = Name(content["controllerIdentity"])
- controllerPublicKeyInfo = content["controllerPublicKey"]
+ dump("Bootstrap data received.")
+ content = json.loads(data.getContent().toRawStr(), encoding="latin-1")
+ deviceNewIdentity = Name(content["deviceNewIdentity"])
+ controllerIdentity = Name(content["controllerIdentity"])
+ controllerPublicKeyInfo = content["controllerPublicKey"]
- #set new identity as default and generate default key-pair with KSK Certificate
- self._identityStorage.addIdentity(deviceNewIdentity)
- self._identityManager.setDefaultIdentity(deviceNewIdentity)
- try:
- self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity)
- except SecurityException:
- #generate new key-pair and certificate for new identity
- dump("Installed new identity as default\nGenerating new key-pair and self signed certificate...")
- newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True)
- newCert = self._identityManager.selfSign(newKey)
- self._identityManager.addCertificateAsIdentityDefault(newCert)
-
- #add controller's identity and public key
- keyType = controllerPublicKeyInfo["keyType"]
- keyName = Name(controllerPublicKeyInfo["keyName"])
- keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"])
- dump("KeyType: ",keyType)
- dump("keyName: ",keyName)
- dump("Controller public key der : ",keyDer)
+ #set new identity as default and generate default key-pair with KSK Certificate
+ self._identityStorage.addIdentity(deviceNewIdentity)
+ self._identityManager.setDefaultIdentity(deviceNewIdentity)
+ try:
+ self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity)
+ except SecurityException:
+ #generate new key-pair and certificate for new identity
+ dump("Install new identity as default\nGenerate new key-pair and self signed certificate")
+ newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True)
+ newCert = self._identityManager.selfSign(newKey)
+ self._identityManager.addCertificateAsIdentityDefault(newCert)
+
+ #add controller's identity and public key
+ keyType = controllerPublicKeyInfo["keyType"]
+ keyName = Name(controllerPublicKeyInfo["keyName"])
+ keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"])
+ dump("Controller's KeyType: ",keyType)
+ dump("Controller's keyName: ",keyName)
+ dump("Controller public key der : ",keyDer)
- self._identityStorage.addIdentity(controllerIdentity)
- try:
- self._identityStorage.addKey(keyName, keyType, keyDer)
- dump("Controller's identity, key and certificate installled.")
- except SecurityException:
- dump("Controller's identity, key, certificate already exists.")
+ self._identityStorage.addIdentity(controllerIdentity)
+ try:
+ self._identityStorage.addKey(keyName, keyType, keyDer)
+ dump("Controller's identity, key and certificate installled.")
+ except SecurityException:
+ dump("Controller's identity, key, certificate already exists.")
- #express an certificate request interest
- defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() )
- self.requestCertificate(defaultKeyName)
+ #express an certificate request interest
+ defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() )
+ self.requestCertificate(defaultKeyName)
def beforeLoopStart(self):
- pass
-
+ self.expressBootstrapInterest()
+
def onTimeout(self, interest):
self._callbackCount += 1
dump("Time out for interest", interest.getName().toUri())
@@ -125,47 +116,39 @@
"""
We compose a command interest with our public key info so the controller
can sign us a certificate that can be used with other nodes in the network.
- Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number>
- """
- certificateRequestName = self._keyChain.getDefaultIdentity()
- deviceIdComponent = certificateRequestName.get(-1)
- keyIdComponent = keyIdentity.get(-1)
+ Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number>
+ """
+ certificateRequestName = self._keyChain.getDefaultIdentity()
+ deviceIdComponent = certificateRequestName.get(-1)
+ keyIdComponent = keyIdentity.get(-1)
- certificateRequestName = certificateRequestName
- certificateRequestName.append("KEY")
- #certificateRequestName.append(deviceIdComponent)
- certificateRequestName.append(keyIdComponent)
+ certificateRequestName = certificateRequestName
+ certificateRequestName.append("KEY")
+ #certificateRequestName.append(deviceIdComponent)
+ certificateRequestName.append(keyIdComponent)
- key = self._identityManager.getPublicKey(keyIdentity)
- keyInfo = {}
- keyInfo["keyType"] = key.getKeyType()
- keyInfo["keyDer"] = key.getKeyDer().toRawStr()
+ key = self._identityManager.getPublicKey(keyIdentity)
+ keyInfo = {}
+ keyInfo["keyType"] = key.getKeyType()
+ keyInfo["keyDer"] = key.getKeyDer().toRawStr()
- certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1"))
+ certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1"))
- certificateRequestName.append("ID-CERT")
-
- dump("Sending certificate request : ",certificateRequestName)
+ certificateRequestName.append("ID-CERT")
+
+ dump("Sending certificate request : ",certificateRequestName)
- self._face.expressInterest(Interest(certificateRequestName), self.onCertificateData, self.onTimeout)
- #TODO use symmetric key to sign
-
+ self.face.expressInterest(Interest(certificateRequestName), self.onCertificateData, self.onTimeout)
+ #TODO use symmetric key to sign
+
def onCertificateData(self, interest, data):
- dump("OnCertificateData : ",data)
-
+ dump("OnCertificateData : ",data)
+
if __name__ == '__main__':
- face = Face("")
- device = Device("default.conf",face)
+ device = Device("default.conf")
+ device.start()
- device.expressBootstrapInterest()
- #device.requestCertificate(device._identityManager.getDefaultKeyNameForIdentity(device._keyChain.getDefaultIdentity() ))
-
- while device._callbackCount < 1000:
- face.processEvents()
- # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
- time.sleep(0.01)
-
- face.shutdown()
+