blob: fb97a234bb92e209da3e4962f6ba53b938fbb5b4 [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 Liang52f43c32015-05-20 17:06:20 -070025from pyndn.security.security_exception import SecurityException
Teng Liang50429402015-05-22 16:01:17 -070026from pyndn.util import Blob
Teng Liang4662b372015-05-27 15:48:36 -070027try:
28 import asyncio
29except ImportError:
30 import trollius as asyncio
philoLbd28e132015-04-16 23:54:21 -070031
32def dump(*list):
33 result = ""
34 for element in list:
35 result += (element if type(element) is str else repr(element)) + " "
36 print(result)
37
philo5d4724e2014-11-10 19:34:05 +000038class Device(BaseNode):
Teng Liang4662b372015-05-27 15:48:36 -070039 def __init__(self,configFileName):
Teng Lianga0b49372015-05-15 05:30:27 -070040 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000041
Teng Liang4662b372015-05-27 15:48:36 -070042 self._deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070043 self._callbackCount = 0
Teng Liang4662b372015-05-27 15:48:36 -070044 self._symKey = "symmetricKeyForBootstrapping"
45 self._category = "sensors"
46 self._id = "T9273659"
philoLbd28e132015-04-16 23:54:21 -070047
Teng Liang50429402015-05-22 16:01:17 -070048
49 def expressBootstrapInterest(self):
Teng Liang50429402015-05-22 16:01:17 -070050
Teng Liang4662b372015-05-27 15:48:36 -070051 #generate bootstrap name /home/controller/bootstrap/<device-parameters>
52 bootstrapName = Name(self._bootstrapPrefix)
Teng Liang50429402015-05-22 16:01:17 -070053
54 deviceParameters = {}
Teng Liang4662b372015-05-27 15:48:36 -070055 deviceParameters["category"] = self._category
56 deviceParameters["id"] = self._deviceSerial
Teng Liang50429402015-05-22 16:01:17 -070057 bootstrapName.append(json.dumps(deviceParameters))
58
59 bootstrapInterest = Interest(bootstrapName)
Teng Liang50429402015-05-22 16:01:17 -070060 bootstrapInterest.setInterestLifetimeMilliseconds(5000)
Teng Liang50429402015-05-22 16:01:17 -070061 bootstrapKeyLocator = KeyLocator()
62 bootstrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST)
Teng Liang4662b372015-05-27 15:48:36 -070063 bootstrapKeyLocator.setKeyData(self._symKey)
Teng Liang50429402015-05-22 16:01:17 -070064 bootstrapInterest.setKeyLocator(bootstrapKeyLocator)
65
Teng Liang4662b372015-05-27 15:48:36 -070066 dump("Express bootstrap interest : ",bootstrapInterest.toUri())
67 self.face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout)
Teng Liang50429402015-05-22 16:01:17 -070068
69 def onBootstrapData(self, interest, data):
Teng Liang4662b372015-05-27 15:48:36 -070070 dump("Bootstrap data received.")
71 content = json.loads(data.getContent().toRawStr(), encoding="latin-1")
72 deviceNewIdentity = Name(content["deviceNewIdentity"])
73 controllerIdentity = Name(content["controllerIdentity"])
74 controllerPublicKeyInfo = content["controllerPublicKey"]
Teng Liang50429402015-05-22 16:01:17 -070075
Teng Liang4662b372015-05-27 15:48:36 -070076 #set new identity as default and generate default key-pair with KSK Certificate
77 self._identityStorage.addIdentity(deviceNewIdentity)
78 self._identityManager.setDefaultIdentity(deviceNewIdentity)
79 try:
80 self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity)
81 except SecurityException:
82 #generate new key-pair and certificate for new identity
83 dump("Install new identity as default\nGenerate new key-pair and self signed certificate")
84 newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True)
85 newCert = self._identityManager.selfSign(newKey)
86 self._identityManager.addCertificateAsIdentityDefault(newCert)
87
88 #add controller's identity and public key
89 keyType = controllerPublicKeyInfo["keyType"]
90 keyName = Name(controllerPublicKeyInfo["keyName"])
91 keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"])
92 dump("Controller's KeyType: ",keyType)
93 dump("Controller's keyName: ",keyName)
94 dump("Controller public key der : ",keyDer)
Teng Liang50429402015-05-22 16:01:17 -070095
Teng Liang4662b372015-05-27 15:48:36 -070096 self._identityStorage.addIdentity(controllerIdentity)
97 try:
98 self._identityStorage.addKey(keyName, keyType, keyDer)
99 dump("Controller's identity, key and certificate installled.")
100 except SecurityException:
101 dump("Controller's identity, key, certificate already exists.")
Teng Liang50429402015-05-22 16:01:17 -0700102
Teng Liang4662b372015-05-27 15:48:36 -0700103 #express an certificate request interest
104 defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() )
105 self.requestCertificate(defaultKeyName)
Teng Liang50429402015-05-22 16:01:17 -0700106
107
Teng Lianga0b49372015-05-15 05:30:27 -0700108 def beforeLoopStart(self):
Teng Liang4662b372015-05-27 15:48:36 -0700109 self.expressBootstrapInterest()
110
philoLbd28e132015-04-16 23:54:21 -0700111 def onTimeout(self, interest):
112 self._callbackCount += 1
113 dump("Time out for interest", interest.getName().toUri())
114
Teng Liang50429402015-05-22 16:01:17 -0700115 def requestCertificate(self, keyIdentity):
philoLfb1b24e2015-05-15 05:33:25 -0700116 """
117 We compose a command interest with our public key info so the controller
118 can sign us a certificate that can be used with other nodes in the network.
Teng Liang4662b372015-05-27 15:48:36 -0700119 Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number>
120 """
121 certificateRequestName = self._keyChain.getDefaultIdentity()
122 deviceIdComponent = certificateRequestName.get(-1)
123 keyIdComponent = keyIdentity.get(-1)
philoLfb1b24e2015-05-15 05:33:25 -0700124
Teng Liang4662b372015-05-27 15:48:36 -0700125 certificateRequestName = certificateRequestName
126 certificateRequestName.append("KEY")
127 #certificateRequestName.append(deviceIdComponent)
128 certificateRequestName.append(keyIdComponent)
Teng Liang50429402015-05-22 16:01:17 -0700129
Teng Liang4662b372015-05-27 15:48:36 -0700130 key = self._identityManager.getPublicKey(keyIdentity)
131 keyInfo = {}
132 keyInfo["keyType"] = key.getKeyType()
133 keyInfo["keyDer"] = key.getKeyDer().toRawStr()
Teng Liang50429402015-05-22 16:01:17 -0700134
Teng Liang4662b372015-05-27 15:48:36 -0700135 certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1"))
Teng Liang50429402015-05-22 16:01:17 -0700136
Teng Liang4662b372015-05-27 15:48:36 -0700137 certificateRequestName.append("ID-CERT")
138
139 dump("Sending certificate request : ",certificateRequestName)
Teng Liang50429402015-05-22 16:01:17 -0700140
Teng Liang4662b372015-05-27 15:48:36 -0700141 self.face.expressInterest(Interest(certificateRequestName), self.onCertificateData, self.onTimeout)
142 #TODO use symmetric key to sign
143
Teng Liang50429402015-05-22 16:01:17 -0700144 def onCertificateData(self, interest, data):
Teng Liang4662b372015-05-27 15:48:36 -0700145 dump("OnCertificateData : ",data)
146
philoLfb1b24e2015-05-15 05:33:25 -0700147
philo5d4724e2014-11-10 19:34:05 +0000148if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -0700149
Teng Liang4662b372015-05-27 15:48:36 -0700150 device = Device("default.conf")
151 device.start()
Teng Lianga0b49372015-05-15 05:30:27 -0700152
Teng Liang4662b372015-05-27 15:48:36 -0700153
philoLbd28e132015-04-16 23:54:21 -0700154