philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 1 | # -*- 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 | import time |
| 21 | from pyndn import Name |
| 22 | from pyndn import Face |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 23 | from base_node import BaseNode |
philoL | b92c018 | 2015-05-15 05:37:25 -0700 | [diff] [blame^] | 24 | from commands import CertificateRequestMessage |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 25 | |
| 26 | def dump(*list): |
| 27 | result = "" |
| 28 | for element in list: |
| 29 | result += (element if type(element) is str else repr(element)) + " " |
| 30 | print(result) |
| 31 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 32 | class Device(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 33 | def __init__(self,configFileName): |
| 34 | super(Device, self).__init__(configFileName=configFileName) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 35 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 36 | #self.deviceSerial = self.getSerial() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 37 | 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 Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 45 | def beforeLoopStart(self): |
| 46 | pass |
| 47 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 48 | def onTimeout(self, interest): |
| 49 | self._callbackCount += 1 |
| 50 | dump("Time out for interest", interest.getName().toUri()) |
| 51 | |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 52 | 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 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 106 | if __name__ == '__main__': |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 107 | face = Face("") |
| 108 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 109 | 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) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 116 | |
| 117 | |
philo | 5eec5e3 | 2014-11-08 08:57:52 +0000 | [diff] [blame] | 118 | while device._callbackCount < 10: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 119 | 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 | |