blob: 64d4119e1ad522e1490474e64e2683f26793b972 [file] [log] [blame]
philoLbd28e132015-04-16 23:54:21 -07001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
3# Copyright (C) 2014-2015 Regents of the University of California.
4# Author: Jeff Thompson <jefft0@remap.ucla.edu>
5#
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU Lesser General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU Lesser General Public License for more details.
15#
16# You should have received a copy of the GNU Lesser General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18# A copy of the GNU Lesser General Public License is in the file COPYING.
19
20
21import time
Teng Liang52f43c32015-05-20 17:06:20 -070022import json
philoLbd28e132015-04-16 23:54:21 -070023from pyndn import Name
24from pyndn import Data
25from pyndn import Face
Teng Liang50429402015-05-22 16:01:17 -070026from pyndn.key_locator import KeyLocator, KeyLocatorType
27
philoLbd28e132015-04-16 23:54:21 -070028from pyndn.security import KeyChain
philo5d4724e2014-11-10 19:34:05 +000029from base_node import BaseNode
Teng Liang50429402015-05-22 16:01:17 -070030from pyndn.security import SecurityException
31from pyndn.util import Blob
philoLbd28e132015-04-16 23:54:21 -070032
33def dump(*list):
34 result = ""
35 for element in list:
36 result += (element if type(element) is str else repr(element)) + " "
37 print(result)
38
philo5d4724e2014-11-10 19:34:05 +000039class Controller(BaseNode):
Teng Lianga0b49372015-05-15 05:30:27 -070040 def __init__(self,configFileName):
41 super(Controller, self).__init__(configFileName=configFileName)
philoLbd28e132015-04-16 23:54:21 -070042 self._responseCount = 0
Teng Liang4662b372015-05-27 15:48:36 -070043 self._symmetricKey = "symmetricKeyForBootstrapping"
44 self._prefix = "/home"
45 self._identity = "/home/controller/id999"
philoLbd28e132015-04-16 23:54:21 -070046
47 def onInterest(self, prefix, interest, transport, registeredPrefixId):
48 self._responseCount += 1
Teng Liang50429402015-05-22 16:01:17 -070049
Teng Liang4662b372015-05-27 15:48:36 -070050 interestName = interest.getName()
Teng Liang50429402015-05-22 16:01:17 -070051
Teng Liang4662b372015-05-27 15:48:36 -070052 #for bootstrap interest
53 if(interestName.toUri().startswith(self._bootstrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey):
54 dump("Reveived bootstrap interest")
Teng Liang50429402015-05-22 16:01:17 -070055 self.onBootstrapInterest(prefix, interest, transport, registeredPrefixId)
56
Teng Liang4662b372015-05-27 15:48:36 -070057 elif ("KEY" in interestName.toUri() and "ID-CERT" in interestName.toUri()):
58 dump("Reveived certificate request interest")
59 self.onCertificateRequest(prefix, interest, transport, registeredPrefixId)
Teng Liang50429402015-05-22 16:01:17 -070060
61 def onBootstrapInterest(self, prefix, interest, transport, registeredPrefixId):
Teng Liang4662b372015-05-27 15:48:36 -070062
63 interestName = interest.getName()
64 deviceParameters = json.loads(interestName.get(3).getValue().toRawStr())
65 deviceNewIdentity = Name("/home")
Teng Liang52f43c32015-05-20 17:06:20 -070066
Teng Liang4662b372015-05-27 15:48:36 -070067 #create new identity for device
Teng Liang50429402015-05-22 16:01:17 -070068 deviceNewIdentity.append(deviceParameters["category"])
69 deviceNewIdentity.append(deviceParameters["id"])
Teng Liang4662b372015-05-27 15:48:36 -070070
Teng Liang50429402015-05-22 16:01:17 -070071 #generate content
72 content = {}
73 content["deviceNewIdentity"] = deviceNewIdentity.toUri()
Teng Liang4662b372015-05-27 15:48:36 -070074 content["controllerIdentity"] = self._identity
Teng Liang52f43c32015-05-20 17:06:20 -070075
Teng Liang50429402015-05-22 16:01:17 -070076 #get public key of controller
77 pKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._identityManager.getDefaultIdentity())
78 pKey = self._identityManager.getPublicKey(pKeyName)
79
80 pKeyInfo = content["controllerPublicKey"] = {}
81 pKeyInfo["keyName"] = pKeyName.toUri()
82 pKeyInfo["keyType"] = pKey.getKeyType()
83 pKeyInfo["publicKeyDer"] = pKey.getKeyDer().toRawStr()
84 dump("Sent content : ",content)
Teng Liang4662b372015-05-27 15:48:36 -070085
86 #TODO generate signature for data
87
88 #generate data package
89 data = Data(interestName)
90 data.setContent(json.dumps(content,encoding="latin-1"))
91 #data.setSignature(signature)
Teng Liang50429402015-05-22 16:01:17 -070092 encodedData = data.wireEncode()
93 transport.send(encodedData.toBuffer())
94
95
96 def onCertificateRequest(self, prefix, interest, transport, registeredPrefixId):
Teng Liang4662b372015-05-27 15:48:36 -070097 interestName = interest.getName()
98 dump("interest name : ",interestName)
99
100 keyName = interestName[:3]
101 keyId = interestName.get(4)
102 keyName.append(keyId)
103 keyInfo = json.loads(interestName.get(5).getValue().toRawStr(),encoding="latin-1")
104 keyType = keyInfo['keyType']
105 keyDer = Blob().fromRawStr(keyInfo['keyDer'])
Teng Liang50429402015-05-22 16:01:17 -0700106
Teng Liang4662b372015-05-27 15:48:36 -0700107 dump("keyname: ",keyName)
108 dump("keyType ",keyInfo['keyType'])
109 dump("keyDer string",keyInfo['keyDer'])
110 dump("keyDer",keyDer)
Teng Liang50429402015-05-22 16:01:17 -0700111
Teng Liang4662b372015-05-27 15:48:36 -0700112 #device and controller are on one mechine, so it needs to be done.
113 self._identityManager.setDefaultIdentity(Name(self._identity))
114 try:
115 self._identityStorage.addKey(keyName, keyType, keyDer)
116 except SecurityException:
117 dump("The public key for device already exists ")
Teng Liang50429402015-05-22 16:01:17 -0700118
Teng Liang4662b372015-05-27 15:48:36 -0700119 signedCertificate = self._identityManager._generateCertificateForKey(keyName)
120 self._keyChain.sign(signedCertificate, self._identityManager.getDefaultCertificateName())
121 self._identityManager.addCertificate(signedCertificate)
122
123 encodedData = signedCertificate.wireEncode()
Teng Liang50429402015-05-22 16:01:17 -0700124 transport.send(encodedData.toBuffer())
philoLbd28e132015-04-16 23:54:21 -0700125
Teng Liang50429402015-05-22 16:01:17 -0700126
philoLbd28e132015-04-16 23:54:21 -0700127 def onRegisterFailed(self, prefix):
128 self._responseCount += 1
129 dump("Register failed for prefix", prefix.toUri())
130
Teng Lianga0b49372015-05-15 05:30:27 -0700131 def beforeLoopStart(self):
Teng Liang4662b372015-05-27 15:48:36 -0700132 identityName = Name(self._identity)
133
134 defaultIdentityExists = True
135 try:
136 defaultIdentityName = self._identityManager.getDefaultIdentity()
137 except:
138 defaultIdentityExists = False
139
Teng Lianga0b49372015-05-15 05:30:27 -0700140
Teng Liang4662b372015-05-27 15:48:36 -0700141 #dump(self._identityManager.getDefaultKeyNameForIdentity(self._identity))
142 if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName:
143 #make one
144 dump("Set default identity: ",identityName)
145 #self._identityManager.createIdentityAndCertificate(identityName)
146 self._identityStorage.addIdentity(identityName)
147 self._identityManager.setDefaultIdentity(identityName)
Teng Liang52f43c32015-05-20 17:06:20 -0700148
Teng Liang4662b372015-05-27 15:48:36 -0700149 try:
150 self._identityManager.getDefaultKeyNameForIdentity(identityName)
151 except SecurityException:
152 newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._identity), isKsk=True)
153 newCert = self._identityManager.selfSign(newKey)
154 dump("generated new KSK certificate ", newCert)
155 self._identityManager.addCertificateAsIdentityDefault(newCert)
156
157
158 #self.face.registerPrefix(self._prefix, self.onInterest, self.onRegisterFailed)
Teng Lianga0b49372015-05-15 05:30:27 -0700159
160
philo5d4724e2014-11-10 19:34:05 +0000161if __name__ == '__main__':
162
Teng Lianga0b49372015-05-15 05:30:27 -0700163 controller = Controller("default.conf")
164 controller.beforeLoopStart()
philoLbd28e132015-04-16 23:54:21 -0700165
Teng Liang4662b372015-05-27 15:48:36 -0700166 face = Face()
167 face.setCommandSigningInfo(controller._keyChain, controller._keyChain.getDefaultCertificateName())
168 dump("Register prefix : ", controller._prefix)
169 face.registerPrefix(controller._prefix, controller.onInterest, controller.onRegisterFailed)
philo5d4724e2014-11-10 19:34:05 +0000170
Teng Liang4662b372015-05-27 15:48:36 -0700171 while True:
philoLbd28e132015-04-16 23:54:21 -0700172 face.processEvents()
173 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
174 time.sleep(0.01)
175
176 face.shutdown()
177
Teng Liang4662b372015-05-27 15:48:36 -0700178
179
180
181