blob: 0f085e768747b49575e23a85a45c1548822e7cda [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
20import time
Teng Liang52f43c32015-05-20 17:06:20 -070021import json
philoLbd28e132015-04-16 23:54:21 -070022from pyndn import Name
23from pyndn import Face
Teng Liang52f43c32015-05-20 17:06:20 -070024from pyndn import Interest
25from pyndn import KeyLocator, KeyLocatorType
philo5d4724e2014-11-10 19:34:05 +000026from base_node import BaseNode
Teng Liang52f43c32015-05-20 17:06:20 -070027from pyndn.security.security_exception import SecurityException
Teng Liang50429402015-05-22 16:01:17 -070028from pyndn.util import Blob
Teng Liang52f43c32015-05-20 17:06:20 -070029
philoLbd28e132015-04-16 23:54:21 -070030
31def dump(*list):
32 result = ""
33 for element in list:
34 result += (element if type(element) is str else repr(element)) + " "
35 print(result)
36
philo5d4724e2014-11-10 19:34:05 +000037class Device(BaseNode):
Teng Liang50429402015-05-22 16:01:17 -070038 def __init__(self,configFileName,face):
Teng Lianga0b49372015-05-15 05:30:27 -070039 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000040
Teng Lianga0b49372015-05-15 05:30:27 -070041 #self.deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070042 self._callbackCount = 0
Teng Liang50429402015-05-22 16:01:17 -070043 self._face = face
philoLbd28e132015-04-16 23:54:21 -070044
45 def onData(self, interest, data):
46 self._callbackCount += 1
Teng Liang50429402015-05-22 16:01:17 -070047 dump("Got data packet with name : ", data.getName().toUri())
philoLbd28e132015-04-16 23:54:21 -070048 # Use join to convert each byte to chr.
49 dump(data.getContent().toRawStr())
50
Teng Liang50429402015-05-22 16:01:17 -070051 #haven't checked symmetric key digest yet
52 if (data.getName().toUri().startswith(self._bootstrapPrefix)):
53 self.onBootstrapData(interest, data)
54
55 def expressBootstrapInterest(self):
56 symKey = "symmetricKeyForBootstrapping"
57
58 #generate bootstrap name /home/controller/bootstrap/<device-parameters>
59 bootstrapName = Name(device._bootstrapPrefix)
60
61 deviceParameters = {}
62 deviceParameters["category"] = "sensors"
63 deviceParameters["id"] = "T123456789"
64 bootstrapName.append(json.dumps(deviceParameters))
65
66 bootstrapInterest = Interest(bootstrapName)
67
68 bootstrapInterest.setInterestLifetimeMilliseconds(5000)
69
70 bootstrapKeyLocator = KeyLocator()
71 bootstrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
72 bootstrapKeyLocator.setKeyData(symKey)
73 bootstrapInterest.setKeyLocator(bootstrapKeyLocator)
74
75 dump("Express interest :",bootstrapInterest.toUri())
76 self._face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout)
77
78 def onBootstrapData(self, interest, data):
79 dump("Data received.")
80 content = json.loads(data.getContent().toRawStr(), encoding="latin-1")
81 deviceNewIdentity = Name(content["deviceNewIdentity"])
82 controllerIdentity = Name(content["controllerIdentity"])
83 controllerPublicKeyInfo = content["controllerPublicKey"]
84
85 #set new identity as default and generate default key-pair with KSK Certificate
86 self._identityStorage.addIdentity(deviceNewIdentity)
87 self._identityManager.setDefaultIdentity(deviceNewIdentity)
88 try:
89 self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity)
90 except SecurityException:
91 #generate new key-pair and certificate for new identity
92 dump("Installed new identity as default\nGenerating new key-pair and self signed certificate...")
93 newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True)
94 newCert = self._identityManager.selfSign(newKey)
95 self._identityManager.addCertificateAsIdentityDefault(newCert)
96
97 #add controller's identity and public key
98 keyType = controllerPublicKeyInfo["keyType"]
99 keyName = Name(controllerPublicKeyInfo["keyName"])
100 keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"])
101 dump("KeyType: ",keyType)
102 dump("keyName: ",keyName)
103 dump("Controller public key der : ",keyDer)
104
105 self._identityStorage.addIdentity(controllerIdentity)
106 try:
107 self._identityStorage.addKey(keyName, keyType, keyDer)
108 dump("Controller's identity, key and certificate installled.")
109 except SecurityException:
110 dump("Controller's identity, key, certificate already exists.")
111
112 #express an certificate request interest
113 defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() )
114 self.requestCertificate(defaultKeyName)
115
116
Teng Lianga0b49372015-05-15 05:30:27 -0700117 def beforeLoopStart(self):
118 pass
119
philoLbd28e132015-04-16 23:54:21 -0700120 def onTimeout(self, interest):
121 self._callbackCount += 1
122 dump("Time out for interest", interest.getName().toUri())
123
Teng Liang50429402015-05-22 16:01:17 -0700124 def requestCertificate(self, keyIdentity):
philoLfb1b24e2015-05-15 05:33:25 -0700125 """
126 We compose a command interest with our public key info so the controller
127 can sign us a certificate that can be used with other nodes in the network.
Teng Liang50429402015-05-22 16:01:17 -0700128 Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number>
129 """
130 certificateRequestName = self._keyChain.getDefaultIdentity()
131 deviceIdComponent = certificateRequestName.get(-1)
132 keyIdComponent = keyIdentity.get(-1)
philoLfb1b24e2015-05-15 05:33:25 -0700133
Teng Liang50429402015-05-22 16:01:17 -0700134 certificateRequestName = certificateRequestName
135 certificateRequestName.append("KEY")
136 #certificateRequestName.append(deviceIdComponent)
137 certificateRequestName.append(keyIdComponent)
138
139 key = self._identityManager.getPublicKey(keyIdentity)
140 keyInfo = {}
141 keyInfo["keyType"] = key.getKeyType()
142 keyInfo["keyDer"] = key.getKeyDer().toRawStr()
143
144 certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1"))
145
146 certificateRequestName.append("ID-CERT")
147
148 dump("Sending certificate request : ",certificateRequestName)
149
150 self._face.expressInterest(Interest(certificateRequestName), self.onCertificateData, self.onTimeout)
151 #TODO use symmetric key to sign
152
153 def onCertificateData(self, interest, data):
154 dump("OnCertificateData : ",data)
155
philoLfb1b24e2015-05-15 05:33:25 -0700156
philo5d4724e2014-11-10 19:34:05 +0000157if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -0700158 face = Face("")
159
Teng Liang50429402015-05-22 16:01:17 -0700160 device = Device("default.conf",face)
Teng Lianga0b49372015-05-15 05:30:27 -0700161
Teng Liang50429402015-05-22 16:01:17 -0700162 device.expressBootstrapInterest()
163 #device.requestCertificate(device._identityManager.getDefaultKeyNameForIdentity(device._keyChain.getDefaultIdentity() ))
philoLbd28e132015-04-16 23:54:21 -0700164
Teng Liang50429402015-05-22 16:01:17 -0700165 while device._callbackCount < 1000:
philoLbd28e132015-04-16 23:54:21 -0700166 face.processEvents()
167 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
168 time.sleep(0.01)
169
170 face.shutdown()
171