modified:   base_node.py
	modified:   controller.py
	modified:   end-device.py
diff --git a/controller.py b/controller.py
index d42eca3..8db4242 100644
--- a/controller.py
+++ b/controller.py
@@ -23,9 +23,12 @@
 from pyndn import Name
 from pyndn import Data
 from pyndn import Face
+from pyndn.key_locator import KeyLocator, KeyLocatorType
+
 from pyndn.security import KeyChain
 from base_node import BaseNode
-
+from pyndn.security import SecurityException
+from pyndn.util import Blob
 
 def dump(*list):
     result = ""
@@ -37,45 +40,92 @@
     def __init__(self,configFileName):
         super(Controller, self).__init__(configFileName=configFileName)
         self._responseCount = 0
-	self._symmetricKey = "symmetricKeyForBootStrapping"
-	self._prefix = "/home/controller"
-	self._bootStrapPrefix = "/home/controller/bootstrap"
+	self._symmetricKey = "symmetricKeyForBootstrapping"
+	self._prefix = "/home/controller/id999"
 
     def onInterest(self, prefix, interest, transport, registeredPrefixId):
         self._responseCount += 1
+
+	interestName = interest.getName()
+
+	#for bootstrap interest
+	if(interestName.toUri().startswith(self._bootstrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey):
+  	    dump("Reveived bootstrap interest")
+	    self.onBootstrapInterest(prefix, interest, transport, registeredPrefixId)   
+        
+    	elif ("KEY" in interestName.toUri() and "ID-CERT" in interestName.toUri()):
+	    dump("Reveived certificate request interest")
+	    self.onCertificateRequest(prefix, interest, transport, registeredPrefixId)
+
+    def onBootstrapInterest(self, prefix, interest, transport, registeredPrefixId):
 	
 	interestName = interest.getName()
-        dump("Received interest ", interestName.toUri())
-
-	if(interestName.toUri().startswith(self._bootStrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey):
-  	    
-	    deviceParameters = json.loads(interestName.get(3).getValue().toRawStr())
-	    deviceNewIdentity = Name("/home")
+	deviceParameters = json.loads(interestName.get(3).getValue().toRawStr())
+	deviceNewIdentity = Name("/home")
             
-	    #create new identity for device
-	    deviceNewIdentity.append(deviceParameters["category"])
-	    deviceNewIdentity.append(deviceParameters["id"])
-	    dump("New identity for device: ",deviceNewIdentity)
+	#create new identity for device
+        deviceNewIdentity.append(deviceParameters["category"])
+        deviceNewIdentity.append(deviceParameters["id"])
 	    
-	    #create key-pair and certificate for new identity
-	    self.
+        #generate content
+        content = {}
+        content["deviceNewIdentity"] = deviceNewIdentity.toUri()
+        content["controllerIdentity"] = self._prefix
 
-	    data = Data(interestName)
-	    content = {}
-	    content["deviceNewIdentity"] = deviceNewIdentity.toUri()
-	    content[]
-	    content["controllerPublicKey"] = 
+        #get public key of controller
+        pKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._identityManager.getDefaultIdentity())
+        pKey = self._identityManager.getPublicKey(pKeyName)
+
+        pKeyInfo = content["controllerPublicKey"] = {}
+        pKeyInfo["keyName"] = pKeyName.toUri()
+        pKeyInfo["keyType"] = pKey.getKeyType()
+        pKeyInfo["publicKeyDer"] = pKey.getKeyDer().toRawStr()
+        dump("Sent content : ",content)
+	      
+	#TODO generate signature for data
+	    
+	#generate data package
+	data = Data(interestName)
+	data.setContent(json.dumps(content,encoding="latin-1"))
+	#data.setSignature(signature)
+        encodedData = data.wireEncode()
+        transport.send(encodedData.toBuffer())
+
+
+    def onCertificateRequest(self, prefix, interest, transport, registeredPrefixId):
+	interestName = interest.getName()
+	dump("interest name : ",interestName)
+	
+	keyName = interestName[:3]
+	keyId = interestName.get(4)
+	keyName.append(keyId)
+	keyInfo = json.loads(interestName.get(5).getValue().toRawStr(),encoding="latin-1")
+	keyType = keyInfo['keyType']
+	keyDer = Blob().fromRawStr(keyInfo['keyDer'])
+
+	dump("keyname: ",keyName)
+	dump("keyType ",keyInfo['keyType'])
+	dump("keyDer string",keyInfo['keyDer'])
+	dump("keyDer",keyDer)
+
+	#device and controller are on one mechine, so it needs to be done.
+	self._identityManager.setDefaultIdentity(Name(self._prefix))
+	try:
+	    self._identityStorage.addKey(keyName, keyType, keyDer)
+	except SecurityException:
+	    dump("The public key for device already exists ")
+
+	signedCertificate = self._identityManager._generateCertificateForKey(keyName)
+	self._keyChain.sign(signedCertificate, self._identityManager.getDefaultCertificateName())
+	self._identityManager.addCertificate(signedCertificate)
+   	
 
 	
-		#dump("Send data : ",content)
-		#data = Data(interest.getName())
-        	#data.setContent(content)
-        	#self._keyChain.sign(data, self._certificateName)
-        	#encodedData = data.wireEncode()
-        #dump("Sent content", content)
-        #transport.send(encodedData.toBuffer())
+	encodedData = signedCertificate.wireEncode()
+        transport.send(encodedData.toBuffer())
 
-    
+
+
     def onRegisterFailed(self, prefix):
         self._responseCount += 1
         dump("Register failed for prefix", prefix.toUri())
@@ -99,11 +149,11 @@
 	    self._identityManager.setDefaultIdentity(identityName)
 
 	    try:
-	        getDefaultKeyNameForIdentity(identityName)
-	    except:
+	        self._identityManager.getDefaultKeyNameForIdentity(identityName)
+	    except SecurityException:
 	        newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._prefix), isKsk=True)
 	        newCert = self._identityManager.selfSign(newKey)
-	        dump("new certificate", newCert)
+	        dump("generated new KSK certificate ", newCert)
 	        self._identityManager.addCertificateAsIdentityDefault(newCert)
 
 
@@ -126,11 +176,6 @@
 
     face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed)
 
-    identityName = controller._identityManager.getDefaultIdentity()
-    keyName = controller._identityManager.getDefaultKeyNameForIdentity(identityName)
-	
-    key = controller._identityManager.getPublicKey(keyName)
-    #dump("key : ",key.getKeyDer().toHex())
     
     while controller._responseCount < 100:
         face.processEvents()