blob: 7043466de9dd2328251b93ce5080f26838d106d4 [file] [log] [blame]
philoLbd28e132015-04-16 23:54:21 -07001# -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2#
Teng Liang29aa6012015-06-28 15:31:01 -07003# Copyright (C) 2014 Regents of the University of California.
4# Author: Teng Liang <philoliang2011@gmail.com>
5#
philoLbd28e132015-04-16 23:54:21 -07006# 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
Teng Liang29aa6012015-06-28 15:31:01 -070014# GNU General Public License for more details.
philoLbd28e132015-04-16 23:54:21 -070015#
Teng Liang29aa6012015-06-28 15:31:01 -070016# You should have received a copy of the GNU General Public License
philoLbd28e132015-04-16 23:54:21 -070017# along with this program. If not, see <http://www.gnu.org/licenses/>.
Teng Liang29aa6012015-06-28 15:31:01 -070018# A copy of the GNU General Public License is in the file COPYING.
philoLbd28e132015-04-16 23:54:21 -070019
20
21import time
Teng Liang52f43c32015-05-20 17:06:20 -070022import json
Teng Liangb09af862015-06-01 10:28:12 -070023from pyndn import Name, Face, Interest, Data
Teng Liang50429402015-05-22 16:01:17 -070024from pyndn.key_locator import KeyLocator, KeyLocatorType
Teng Liangb09af862015-06-01 10:28:12 -070025from hmac_helper import HmacHelper
Teng Liang50429402015-05-22 16:01:17 -070026
philoLbd28e132015-04-16 23:54:21 -070027from pyndn.security import KeyChain
philo5d4724e2014-11-10 19:34:05 +000028from base_node import BaseNode
Teng Liang50429402015-05-22 16:01:17 -070029from pyndn.security import SecurityException
30from pyndn.util import Blob
philoLbd28e132015-04-16 23:54:21 -070031
Teng Liangb09af862015-06-01 10:28:12 -070032
philoLbd28e132015-04-16 23:54:21 -070033def 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"
Teng Liangb09af862015-06-01 10:28:12 -070046 self._hmacHelper = HmacHelper(self._symmetricKey)
47
48 def setFace(self,face):
49 self.face = face
philoLbd28e132015-04-16 23:54:21 -070050
51 def onInterest(self, prefix, interest, transport, registeredPrefixId):
52 self._responseCount += 1
Teng Liang4662b372015-05-27 15:48:36 -070053 interestName = interest.getName()
Teng Liang29aa6012015-06-28 15:31:01 -070054 dump("received interest : ",interestName.toUri())
Teng Liang50429402015-05-22 16:01:17 -070055
Teng Liang4662b372015-05-27 15:48:36 -070056 #for bootstrap interest
Teng Liangb09af862015-06-01 10:28:12 -070057 #if(interestName.toUri().startswith(self._bootstrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey):
58 if(interestName.toUri().startswith(self._bootstrapPrefix) and self._hmacHelper.verifyInterest(interest)):
Teng Liang4662b372015-05-27 15:48:36 -070059 dump("Reveived bootstrap interest")
Teng Liang50429402015-05-22 16:01:17 -070060 self.onBootstrapInterest(prefix, interest, transport, registeredPrefixId)
61
Teng Liang4662b372015-05-27 15:48:36 -070062 elif ("KEY" in interestName.toUri() and "ID-CERT" in interestName.toUri()):
63 dump("Reveived certificate request interest")
64 self.onCertificateRequest(prefix, interest, transport, registeredPrefixId)
Teng Liang50429402015-05-22 16:01:17 -070065
66 def onBootstrapInterest(self, prefix, interest, transport, registeredPrefixId):
Teng Liangb09af862015-06-01 10:28:12 -070067 if (self._hmacHelper.verifyInterest(interest)):
68 self.log.info("Bootstrap interest verified")
69 interestName = interest.getName()
70 deviceParameters = json.loads(interestName.get(3).getValue().toRawStr())
71 deviceNewIdentity = Name("/home")
72
73 #create new identity for device
74 deviceNewIdentity.append(deviceParameters["category"])
75 deviceNewIdentity.append(deviceParameters["id"])
76
77 #generate content
78 content = {}
79 content["deviceNewIdentity"] = deviceNewIdentity.toUri()
80 content["controllerIdentity"] = self._identity
81
82 #get public key of controller
83 pKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._identityManager.getDefaultIdentity())
84 pKey = self._identityManager.getPublicKey(pKeyName)
85
86 pKeyInfo = content["controllerPublicKey"] = {}
87 pKeyInfo["keyName"] = pKeyName.toUri()
88 pKeyInfo["keyType"] = pKey.getKeyType()
89 pKeyInfo["publicKeyDer"] = pKey.getKeyDer().toRawStr()
90 dump("Sent content : ",content)
91
92 #TODO generate signature for data
93
94 #generate data package
95 data = Data(interestName)
96 data.setContent(json.dumps(content,encoding="latin-1"))
97 #data.setSignature(signature)
98 self._hmacHelper.signData(data)
99
100 #encodedData = data.wireEncode()
101 #transport.send(encodedData.toBuffer())
102 self.sendData(data,transport,sign=False)
103 else:
104 self.log.info("Bootstrap interest not verified")
Teng Liang4662b372015-05-27 15:48:36 -0700105
Teng Liangb09af862015-06-01 10:28:12 -0700106
Teng Liang50429402015-05-22 16:01:17 -0700107
108
109 def onCertificateRequest(self, prefix, interest, transport, registeredPrefixId):
Teng Liangb09af862015-06-01 10:28:12 -0700110 if (self._hmacHelper.verifyInterest(interest)):
111 self.log.info("certificate request interest verified")
112 interestName = interest.getName()
113 dump("interest name : ",interestName)
114
115 keyName = interestName[:3]
116 keyId = interestName.get(4)
117 keyName.append(keyId)
118 keyInfo = json.loads(interestName.get(5).getValue().toRawStr(),encoding="latin-1")
119 keyType = keyInfo['keyType']
120 keyDer = Blob().fromRawStr(keyInfo['keyDer'])
121
122 #dump("keyname: ",keyName)
123 dump("keyType ",keyInfo['keyType'])
124 dump("keyDer string",keyInfo['keyDer'])
125 dump("keyDer",keyDer)
126
127 #device and controller are on one mechine, so it needs to be done.
128 self._identityManager.setDefaultIdentity(Name(self._identity))
129 try:
130 self._identityStorage.addKey(keyName, keyType, keyDer)
131 except SecurityException:
132 dump("The public key for device already exists ")
133
134 signedCertificate = self._identityManager._generateCertificateForKey(keyName)
135 self._keyChain.sign(signedCertificate, self._identityManager.getDefaultCertificateName())
136 self._identityManager.addCertificate(signedCertificate)
137 #self._hmacHelper.signData()
138
139 #encodedData = signedCertificate.wireEncode()
140 #transport.send(encodedData.toBuffer())
141 self.sendData(signedCertificate,transport,sign=False)
142
143 self.log.info("Certificate sent back : {}".format(signedCertificate.__str__))
144 print(signedCertificate)
145 else:
146 self.log.info("certificate request interest not verified")
Teng Liang4662b372015-05-27 15:48:36 -0700147
philoLbd28e132015-04-16 23:54:21 -0700148
Teng Liang50429402015-05-22 16:01:17 -0700149
philoLbd28e132015-04-16 23:54:21 -0700150 def onRegisterFailed(self, prefix):
151 self._responseCount += 1
152 dump("Register failed for prefix", prefix.toUri())
153
Teng Lianga0b49372015-05-15 05:30:27 -0700154 def beforeLoopStart(self):
Teng Liang4662b372015-05-27 15:48:36 -0700155 identityName = Name(self._identity)
156
157 defaultIdentityExists = True
158 try:
159 defaultIdentityName = self._identityManager.getDefaultIdentity()
160 except:
161 defaultIdentityExists = False
Teng Lianga0b49372015-05-15 05:30:27 -0700162
Teng Liang4662b372015-05-27 15:48:36 -0700163 if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName:
164 #make one
Teng Liang12286002015-06-10 12:52:22 -0700165 dump("Create identity and certificate for identity name: ",identityName)
166 self._keyChain.createIdentityAndCertificate(identityName)
167 self._identityManager.setDefaultIdentity(identityName)
Teng Liang52f43c32015-05-20 17:06:20 -0700168
Teng Liang12286002015-06-10 12:52:22 -0700169 self.face.setCommandSigningInfo(self._keyChain, self.getDefaultCertificateName())
Teng Liangb1c842d2015-06-01 10:31:07 -0700170 self.face.registerPrefix(self._prefix, self.onInterest, self.onRegisterFailed)
Teng Liangb09af862015-06-01 10:28:12 -0700171
172
Teng Lianga0b49372015-05-15 05:30:27 -0700173
174
philo5d4724e2014-11-10 19:34:05 +0000175if __name__ == '__main__':
176
Teng Lianga0b49372015-05-15 05:30:27 -0700177 controller = Controller("default.conf")
Teng Liangb1c842d2015-06-01 10:31:07 -0700178 controller.start()
179 #controller.beforeLoopStart()
Teng Liangb09af862015-06-01 10:28:12 -0700180
philoLbd28e132015-04-16 23:54:21 -0700181
Teng Liangb1c842d2015-06-01 10:31:07 -0700182 #face = Face()
183 #controller.setFace(face)
philo5d4724e2014-11-10 19:34:05 +0000184
Teng Liangb1c842d2015-06-01 10:31:07 -0700185 #face.setCommandSigningInfo(controller._keyChain, controller.getDefaultCertificateName())
186 #face.registerPrefix(controller._prefix, controller.onInterest, controller.onRegisterFailed)
187 #dump("Register prefix : ",controller._prefix)
188 #while True:
189 # face.processEvents()
190# time.sleep(0.05)
philoLbd28e132015-04-16 23:54:21 -0700191
Teng Liangb1c842d2015-06-01 10:31:07 -0700192 # face.shutdown()
philoLbd28e132015-04-16 23:54:21 -0700193
Teng Liang4662b372015-05-27 15:48:36 -0700194
Teng Liangb09af862015-06-01 10:28:12 -0700195
196
Teng Liang4662b372015-05-27 15:48:36 -0700197
198
199