blob: a78e3da44773711fc14178803ef223934afd4cc6 [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
21from pyndn import Name
22from pyndn import Face
philo5d4724e2014-11-10 19:34:05 +000023from base_node import BaseNode
philoLb92c0182015-05-15 05:37:25 -070024from commands import CertificateRequestMessage
philoLbd28e132015-04-16 23:54:21 -070025
26def dump(*list):
27 result = ""
28 for element in list:
29 result += (element if type(element) is str else repr(element)) + " "
30 print(result)
31
philo5d4724e2014-11-10 19:34:05 +000032class Device(BaseNode):
Teng Lianga0b49372015-05-15 05:30:27 -070033 def __init__(self,configFileName):
34 super(Device, self).__init__(configFileName=configFileName)
philo5d4724e2014-11-10 19:34:05 +000035
Teng Lianga0b49372015-05-15 05:30:27 -070036 #self.deviceSerial = self.getSerial()
philoLbd28e132015-04-16 23:54:21 -070037 self._callbackCount = 0
38
39 def onData(self, interest, data):
40 self._callbackCount += 1
41 dump("Got data packet with name", data.getName().toUri())
42 # Use join to convert each byte to chr.
43 dump(data.getContent().toRawStr())
44
Teng Lianga0b49372015-05-15 05:30:27 -070045 def beforeLoopStart(self):
46 pass
47
philoLbd28e132015-04-16 23:54:21 -070048 def onTimeout(self, interest):
49 self._callbackCount += 1
50 dump("Time out for interest", interest.getName().toUri())
51
philoLfb1b24e2015-05-15 05:33:25 -070052 def _sendCertificateRequest(self, keyIdentity):
53 """
54 We compose a command interest with our public key info so the controller
55 can sign us a certificate that can be used with other nodes in the network.
56 """
57
58 #TODO: GENERATE A NEW PUBLIC/PRIVATE PAIR INSTEAD OF COPYING
59 makeKey = False
60 try:
61 defaultKey = self._identityStorage.getDefaultKeyNameForIdentity(keyIdentity)
62 newKeyName = defaultKey
63 except SecurityException:
64 defaultIdentity = self._keyChain.getDefaultIdentity()
65 defaultKey = self._identityStorage.getDefaultKeyNameForIdentity(defaultIdentity)
66 newKeyName = self._identityStorage.getNewKeyName(keyIdentity, True)
67 makeKey = True
68
69 self.log.debug("Found key: " + defaultKey.toUri()+ " renaming as: " + newKeyName.toUri())
70
71 keyType = self._identityStorage.getKeyType(defaultKey)
72 keyDer = self._identityStorage.getKey(defaultKey)
73
74 if makeKey:
75 try:
76 privateDer = self._identityManager.getPrivateKey(defaultKey)
77 except SecurityException:
78 # XXX: is recovery impossible?
79 pass
80 else:
81 try:
82 self._identityStorage.addKey(newKeyName, keyType, keyDer)
83 self._identityManager.addPrivateKey(newKeyName, privateDer)
84 except SecurityException:
85 # TODO: key shouldn't exist...
86 pass
87
88 message = CertificateRequestMessage()
89 message.command.keyType = keyType
90 message.command.keyBits = keyDer.toRawStr()
91
92 for component in range(newKeyName.size()):
93 message.command.keyName.components.append(newKeyName.get(component).toEscapedString())
94
95 paramComponent = ProtobufTlv.encode(message)
96
97 interestName = Name(self._policyManager.getTrustRootIdentity()).append("certificateRequest").append(paramComponent)
98 interest = Interest(interestName)
99 interest.setInterestLifetimeMilliseconds(10000) # takes a tick to verify and sign
100 self._hmacHandler.signInterest(interest, keyName=self.prefix)
101
102 self.log.info("Sending certificate request to controller")
103 self.log.debug("Certificate request: "+interest.getName().toUri())
104 self.face.expressInterest(interest, self._onCertificateReceived, self._onCertificateTimeout)
105
philo5d4724e2014-11-10 19:34:05 +0000106if __name__ == '__main__':
philoLbd28e132015-04-16 23:54:21 -0700107 face = Face("")
108
Teng Lianga0b49372015-05-15 05:30:27 -0700109 device = Device("default.conf")
110
111 symKey = "symmetricKeyForBootStrapping"
112 bootStrapName = Name("/home/controller/bootstrap/light/id1/"+symKey)
113 dump("Express name ",bootStrapName.toUri())
114
115 face.expressInterest(bootStrapName, device.onData, device.onTimeout)
philoLbd28e132015-04-16 23:54:21 -0700116
117
philo5eec5e32014-11-08 08:57:52 +0000118 while device._callbackCount < 10:
philoLbd28e132015-04-16 23:54:21 -0700119 face.processEvents()
120 # We need to sleep for a few milliseconds so we don't use 100% of the CPU.
121 time.sleep(0.01)
122
123 face.shutdown()
124