blob: d42eca398f042133d631539669b24f0436f2e6f8 [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
26from pyndn.security import KeyChain
philo5d4724e2014-11-10 19:34:05 +000027from base_node import BaseNode
28
philoLbd28e132015-04-16 23:54:21 -070029
30def dump(*list):
31 result = ""
32 for element in list:
33 result += (element if type(element) is str else repr(element)) + " "
34 print(result)
35
philo5d4724e2014-11-10 19:34:05 +000036class Controller(BaseNode):
Teng Lianga0b49372015-05-15 05:30:27 -070037 def __init__(self,configFileName):
38 super(Controller, self).__init__(configFileName=configFileName)
philoLbd28e132015-04-16 23:54:21 -070039 self._responseCount = 0
Teng Lianga0b49372015-05-15 05:30:27 -070040 self._symmetricKey = "symmetricKeyForBootStrapping"
41 self._prefix = "/home/controller"
Teng Liang52f43c32015-05-20 17:06:20 -070042 self._bootStrapPrefix = "/home/controller/bootstrap"
philoLbd28e132015-04-16 23:54:21 -070043
44 def onInterest(self, prefix, interest, transport, registeredPrefixId):
45 self._responseCount += 1
Teng Lianga0b49372015-05-15 05:30:27 -070046
47 interestName = interest.getName()
Teng Liang52f43c32015-05-20 17:06:20 -070048 dump("Received interest ", interestName.toUri())
49
50 if(interestName.toUri().startswith(self._bootStrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey):
51
52 deviceParameters = json.loads(interestName.get(3).getValue().toRawStr())
53 deviceNewIdentity = Name("/home")
54
55 #create new identity for device
56 deviceNewIdentity.append(deviceParameters["category"])
57 deviceNewIdentity.append(deviceParameters["id"])
58 dump("New identity for device: ",deviceNewIdentity)
59
60 #create key-pair and certificate for new identity
61 self.
62
63 data = Data(interestName)
64 content = {}
65 content["deviceNewIdentity"] = deviceNewIdentity.toUri()
66 content[]
67 content["controllerPublicKey"] =
68
Teng Lianga0b49372015-05-15 05:30:27 -070069
Teng Liang52f43c32015-05-20 17:06:20 -070070 #dump("Send data : ",content)
71 #data = Data(interest.getName())
72 #data.setContent(content)
Teng Lianga0b49372015-05-15 05:30:27 -070073 #self._keyChain.sign(data, self._certificateName)
Teng Liang52f43c32015-05-20 17:06:20 -070074 #encodedData = data.wireEncode()
philoLbd28e132015-04-16 23:54:21 -070075 #dump("Sent content", content)
Teng Liang52f43c32015-05-20 17:06:20 -070076 #transport.send(encodedData.toBuffer())
philoLbd28e132015-04-16 23:54:21 -070077
Teng Liang52f43c32015-05-20 17:06:20 -070078
philoLbd28e132015-04-16 23:54:21 -070079 def onRegisterFailed(self, prefix):
80 self._responseCount += 1
81 dump("Register failed for prefix", prefix.toUri())
82
Teng Lianga0b49372015-05-15 05:30:27 -070083 def beforeLoopStart(self):
84 identityName = Name(self._prefix)
Teng Liang52f43c32015-05-20 17:06:20 -070085
86 defaultIdentityExists = True
Teng Lianga0b49372015-05-15 05:30:27 -070087 try:
88 defaultIdentityName = self._identityManager.getDefaultIdentity()
Teng Lianga0b49372015-05-15 05:30:27 -070089 except:
Teng Liang52f43c32015-05-20 17:06:20 -070090 defaultIdentityExists = False
Teng Lianga0b49372015-05-15 05:30:27 -070091
92
93 #dump(self._identityManager.getDefaultKeyNameForIdentity(self._prefix))
Teng Liang52f43c32015-05-20 17:06:20 -070094 if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName:
Teng Lianga0b49372015-05-15 05:30:27 -070095 #make one
Teng Liang52f43c32015-05-20 17:06:20 -070096 dump("Set default identity: ",identityName)
97 #self._identityManager.createIdentityAndCertificate(identityName)
98 self._identityStorage.addIdentity(identityName)
Teng Lianga0b49372015-05-15 05:30:27 -070099 self._identityManager.setDefaultIdentity(identityName)
Teng Liang52f43c32015-05-20 17:06:20 -0700100
101 try:
102 getDefaultKeyNameForIdentity(identityName)
103 except:
104 newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._prefix), isKsk=True)
105 newCert = self._identityManager.selfSign(newKey)
106 dump("new certificate", newCert)
107 self._identityManager.addCertificateAsIdentityDefault(newCert)
Teng Lianga0b49372015-05-15 05:30:27 -0700108
109
philo5d4724e2014-11-10 19:34:05 +0000110if __name__ == '__main__':
111
philoLbd28e132015-04-16 23:54:21 -0700112 # The default Face will connect using a Unix socket, or to "localhost".
113 face = Face()
114
115 # Use the system default key chain and certificate name to sign commands.
philo5d4724e2014-11-10 19:34:05 +0000116
Teng Lianga0b49372015-05-15 05:30:27 -0700117 controller = Controller("default.conf")
118 controller.beforeLoopStart()
119
Teng Liang52f43c32015-05-20 17:06:20 -0700120 face.setCommandSigningInfo(controller.getKeyChain(), controller._keyChain.getDefaultCertificateName())
philoLbd28e132015-04-16 23:54:21 -0700121
122 # Also use the default certificate name to sign data packets.
philo5d4724e2014-11-10 19:34:05 +0000123
philoLbd28e132015-04-16 23:54:21 -0700124 prefix = Name("/home/")
Teng Lianga0b49372015-05-15 05:30:27 -0700125 dump("Register prefix", prefix)
126
philoLbd28e132015-04-16 23:54:21 -0700127 face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed)
128
Teng Lianga0b49372015-05-15 05:30:27 -0700129 identityName = controller._identityManager.getDefaultIdentity()
130 keyName = controller._identityManager.getDefaultKeyNameForIdentity(identityName)
131
132 key = controller._identityManager.getPublicKey(keyName)
Teng Liang52f43c32015-05-20 17:06:20 -0700133 #dump("key : ",key.getKeyDer().toHex())
Teng Lianga0b49372015-05-15 05:30:27 -0700134
philoLbd28e132015-04-16 23:54:21 -0700135 while controller._responseCount < 100:
136 face.processEvents()
137 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
138 time.sleep(0.01)
139
140 face.shutdown()
141