blob: a77dc4764e918f561483016b83e123010bac5abd [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
Teng Liang4662b372015-05-27 15:48:36 -070022from pyndn import Name, Face, Interest, Data, ThreadsafeFace
Teng Liang52f43c32015-05-20 17:06:20 -070023from pyndn import KeyLocator, KeyLocatorType
philo5d4724e2014-11-10 19:34:05 +000024from base_node import BaseNode
Teng Liangb09af862015-06-01 10:28:12 -070025from hmac_helper import HmacHelper
Teng Liang52f43c32015-05-20 17:06:20 -070026from pyndn.security.security_exception import SecurityException
Teng Liang50429402015-05-22 16:01:17 -070027from pyndn.util import Blob
Teng Liang4662b372015-05-27 15:48:36 -070028try:
29 import asyncio
30except ImportError:
31 import trollius as asyncio
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 Device(BaseNode):
Teng Liang4662b372015-05-27 15:48:36 -070040 def __init__(self,configFileName):
Teng Lianga0b49372015-05-15 05:30:27 -070041 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000042
Teng Liang4662b372015-05-27 15:48:36 -070043 self._deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070044 self._callbackCount = 0
Teng Liang4662b372015-05-27 15:48:36 -070045 self._symKey = "symmetricKeyForBootstrapping"
46 self._category = "sensors"
47 self._id = "T9273659"
Teng Liangb09af862015-06-01 10:28:12 -070048 self._hmacHelper = HmacHelper(self._symKey)
philoLbd28e132015-04-16 23:54:21 -070049
Teng Liang50429402015-05-22 16:01:17 -070050
51 def expressBootstrapInterest(self):
Teng Liang50429402015-05-22 16:01:17 -070052
Teng Liang4662b372015-05-27 15:48:36 -070053 #generate bootstrap name /home/controller/bootstrap/<device-parameters>
54 bootstrapName = Name(self._bootstrapPrefix)
Teng Liang50429402015-05-22 16:01:17 -070055
56 deviceParameters = {}
Teng Liang4662b372015-05-27 15:48:36 -070057 deviceParameters["category"] = self._category
Teng Liangb09af862015-06-01 10:28:12 -070058 deviceParameters["id"] = self._id
Teng Liang50429402015-05-22 16:01:17 -070059 bootstrapName.append(json.dumps(deviceParameters))
60
61 bootstrapInterest = Interest(bootstrapName)
Teng Liang50429402015-05-22 16:01:17 -070062 bootstrapInterest.setInterestLifetimeMilliseconds(5000)
Teng Liangb09af862015-06-01 10:28:12 -070063 #bootstrapKeyLocator = KeyLocator()
64 #bootstrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
65 #bootstrapKeyLocator.setKeyData(self._symKey)
66 #bootstrapInterest.setKeyLocator(bootstrapKeyLocator)
67 self._hmacHelper.signInterest(bootstrapInterest)
Teng Liang50429402015-05-22 16:01:17 -070068
Teng Liang4662b372015-05-27 15:48:36 -070069 dump("Express bootstrap interest : ",bootstrapInterest.toUri())
70 self.face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout)
Teng Liangb09af862015-06-01 10:28:12 -070071 def onInterest():
72 pass
73 def onRegisterFailed():
74 pass
Teng Liang50429402015-05-22 16:01:17 -070075
76 def onBootstrapData(self, interest, data):
Teng Liang4662b372015-05-27 15:48:36 -070077 dump("Bootstrap data received.")
Teng Liang50429402015-05-22 16:01:17 -070078
Teng Liangb09af862015-06-01 10:28:12 -070079 if (self._hmacHelper.verifyData(data)):
80 self.log.info("Bootstrap data is verified")
81 content = json.loads(data.getContent().toRawStr(), encoding="latin-1")
82 deviceNewIdentity = Name(content["deviceNewIdentity"])
83 controllerIdentity = Name(content["controllerIdentity"])
84 controllerPublicKeyInfo = content["controllerPublicKey"]
85
86 self.face.registerPrefix(content["deviceNewIdentity"],self.onInterest,self.onRegisterFailed)
87 #set new identity as default and generate default key-pair with KSK Certificate
88 self._identityStorage.addIdentity(deviceNewIdentity)
89 self._identityManager.setDefaultIdentity(deviceNewIdentity)
90 try:
91 self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity)
92 except SecurityException:
93 #generate new key-pair and certificate for new identity
94 dump("Install new identity as default\nGenerate new key-pair and self signed certificate")
95 newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True)
96 newCert = self._identityManager.selfSign(newKey)
97 self._identityManager.addCertificateAsIdentityDefault(newCert)
98
99 #add controller's identity and public key
100 keyType = controllerPublicKeyInfo["keyType"]
101 keyName = Name(controllerPublicKeyInfo["keyName"])
102 keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"])
103 dump("Controller's KeyType: ",keyType)
104 dump("Controller's keyName: ",keyName)
105 dump("Controller public key der : ",keyDer)
106
107 self._identityStorage.addIdentity(controllerIdentity)
108 try:
109 self._identityStorage.addKey(keyName, keyType, keyDer)
110 dump("Controller's identity, key and certificate installled.")
111 except SecurityException:
112 dump("Controller's identity, key, certificate already exists.")
113
114 #express an certificate request interest
115 defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() )
116 self.requestCertificate(defaultKeyName)
117 else:
118 self.log.info("Bootstrap data is not verified")
Teng Liang4662b372015-05-27 15:48:36 -0700119
Teng Liangb09af862015-06-01 10:28:12 -0700120
Teng Liang50429402015-05-22 16:01:17 -0700121
122
Teng Lianga0b49372015-05-15 05:30:27 -0700123 def beforeLoopStart(self):
Teng Liangb09af862015-06-01 10:28:12 -0700124 #self.face.registerPrefix('/', self.onInterest, self.onRegisterFailed)
Teng Liang4662b372015-05-27 15:48:36 -0700125 self.expressBootstrapInterest()
126
philoLbd28e132015-04-16 23:54:21 -0700127 def onTimeout(self, interest):
128 self._callbackCount += 1
129 dump("Time out for interest", interest.getName().toUri())
130
Teng Liang50429402015-05-22 16:01:17 -0700131 def requestCertificate(self, keyIdentity):
philoLfb1b24e2015-05-15 05:33:25 -0700132 """
133 We compose a command interest with our public key info so the controller
134 can sign us a certificate that can be used with other nodes in the network.
Teng Liang4662b372015-05-27 15:48:36 -0700135 Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number>
136 """
137 certificateRequestName = self._keyChain.getDefaultIdentity()
138 deviceIdComponent = certificateRequestName.get(-1)
139 keyIdComponent = keyIdentity.get(-1)
philoLfb1b24e2015-05-15 05:33:25 -0700140
Teng Liang4662b372015-05-27 15:48:36 -0700141 certificateRequestName = certificateRequestName
142 certificateRequestName.append("KEY")
143 #certificateRequestName.append(deviceIdComponent)
144 certificateRequestName.append(keyIdComponent)
Teng Liang50429402015-05-22 16:01:17 -0700145
Teng Liang4662b372015-05-27 15:48:36 -0700146 key = self._identityManager.getPublicKey(keyIdentity)
147 keyInfo = {}
148 keyInfo["keyType"] = key.getKeyType()
149 keyInfo["keyDer"] = key.getKeyDer().toRawStr()
Teng Liang50429402015-05-22 16:01:17 -0700150
Teng Liang4662b372015-05-27 15:48:36 -0700151 certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1"))
Teng Liang50429402015-05-22 16:01:17 -0700152
Teng Liang4662b372015-05-27 15:48:36 -0700153 certificateRequestName.append("ID-CERT")
Teng Liangb09af862015-06-01 10:28:12 -0700154
155 certificateRequest = Interest(certificateRequestName)
156 certificateRequest.setInterestLifetimeMilliseconds(5000)
157 self._hmacHelper.signInterest(certificateRequest)
Teng Liang4662b372015-05-27 15:48:36 -0700158
159 dump("Sending certificate request : ",certificateRequestName)
Teng Liang50429402015-05-22 16:01:17 -0700160
Teng Liangb09af862015-06-01 10:28:12 -0700161 self.face.expressInterest(certificateRequest, self.onCertificateData, self.onTimeout)
Teng Liang4662b372015-05-27 15:48:36 -0700162 #TODO use symmetric key to sign
163
Teng Liang50429402015-05-22 16:01:17 -0700164 def onCertificateData(self, interest, data):
Teng Liang4662b372015-05-27 15:48:36 -0700165 dump("OnCertificateData : ",data)
166
philoLfb1b24e2015-05-15 05:33:25 -0700167
philo5d4724e2014-11-10 19:34:05 +0000168if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -0700169
Teng Liangb09af862015-06-01 10:28:12 -0700170 device = Device("tmp.conf")
Teng Liang4662b372015-05-27 15:48:36 -0700171 device.start()
Teng Lianga0b49372015-05-15 05:30:27 -0700172
Teng Liang4662b372015-05-27 15:48:36 -0700173
philoLbd28e132015-04-16 23:54:21 -0700174