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 | |
| 21 | import time |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 22 | import json |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 23 | from pyndn import Name |
| 24 | from pyndn import Data |
| 25 | from pyndn import Face |
| 26 | from pyndn.security import KeyChain |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 27 | from base_node import BaseNode |
| 28 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 29 | |
| 30 | def dump(*list): |
| 31 | result = "" |
| 32 | for element in list: |
| 33 | result += (element if type(element) is str else repr(element)) + " " |
| 34 | print(result) |
| 35 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 36 | class Controller(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 37 | def __init__(self,configFileName): |
| 38 | super(Controller, self).__init__(configFileName=configFileName) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 39 | self._responseCount = 0 |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 40 | self._symmetricKey = "symmetricKeyForBootStrapping" |
| 41 | self._prefix = "/home/controller" |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 42 | self._bootStrapPrefix = "/home/controller/bootstrap" |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 43 | |
| 44 | def onInterest(self, prefix, interest, transport, registeredPrefixId): |
| 45 | self._responseCount += 1 |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 46 | |
| 47 | interestName = interest.getName() |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 48 | dump("Received interest ", interestName.toUri()) |
| 49 | |
| 50 | if(interestName.toUri().startswith(self._bootStrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey): |
| 51 | |
| 52 | deviceParameters = json.loads(interestName.get(3).getValue().toRawStr()) |
| 53 | deviceNewIdentity = Name("/home") |
| 54 | |
| 55 | #create new identity for device |
| 56 | deviceNewIdentity.append(deviceParameters["category"]) |
| 57 | deviceNewIdentity.append(deviceParameters["id"]) |
| 58 | dump("New identity for device: ",deviceNewIdentity) |
| 59 | |
| 60 | #create key-pair and certificate for new identity |
| 61 | self. |
| 62 | |
| 63 | data = Data(interestName) |
| 64 | content = {} |
| 65 | content["deviceNewIdentity"] = deviceNewIdentity.toUri() |
| 66 | content[] |
| 67 | content["controllerPublicKey"] = |
| 68 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 69 | |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 70 | #dump("Send data : ",content) |
| 71 | #data = Data(interest.getName()) |
| 72 | #data.setContent(content) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 73 | #self._keyChain.sign(data, self._certificateName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 74 | #encodedData = data.wireEncode() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 75 | #dump("Sent content", content) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 76 | #transport.send(encodedData.toBuffer()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 77 | |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 78 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 79 | def onRegisterFailed(self, prefix): |
| 80 | self._responseCount += 1 |
| 81 | dump("Register failed for prefix", prefix.toUri()) |
| 82 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 83 | def beforeLoopStart(self): |
| 84 | identityName = Name(self._prefix) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 85 | |
| 86 | defaultIdentityExists = True |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 87 | try: |
| 88 | defaultIdentityName = self._identityManager.getDefaultIdentity() |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 89 | except: |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 90 | defaultIdentityExists = False |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 91 | |
| 92 | |
| 93 | #dump(self._identityManager.getDefaultKeyNameForIdentity(self._prefix)) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 94 | if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName: |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 95 | #make one |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 96 | dump("Set default identity: ",identityName) |
| 97 | #self._identityManager.createIdentityAndCertificate(identityName) |
| 98 | self._identityStorage.addIdentity(identityName) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 99 | self._identityManager.setDefaultIdentity(identityName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 100 | |
| 101 | try: |
| 102 | getDefaultKeyNameForIdentity(identityName) |
| 103 | except: |
| 104 | newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._prefix), isKsk=True) |
| 105 | newCert = self._identityManager.selfSign(newKey) |
| 106 | dump("new certificate", newCert) |
| 107 | self._identityManager.addCertificateAsIdentityDefault(newCert) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 108 | |
| 109 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 110 | if __name__ == '__main__': |
| 111 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 112 | # The default Face will connect using a Unix socket, or to "localhost". |
| 113 | face = Face() |
| 114 | |
| 115 | # Use the system default key chain and certificate name to sign commands. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 116 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 117 | controller = Controller("default.conf") |
| 118 | controller.beforeLoopStart() |
| 119 | |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 120 | face.setCommandSigningInfo(controller.getKeyChain(), controller._keyChain.getDefaultCertificateName()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 121 | |
| 122 | # Also use the default certificate name to sign data packets. |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 123 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 124 | prefix = Name("/home/") |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 125 | dump("Register prefix", prefix) |
| 126 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 127 | face.registerPrefix(prefix, controller.onInterest, controller.onRegisterFailed) |
| 128 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 129 | identityName = controller._identityManager.getDefaultIdentity() |
| 130 | keyName = controller._identityManager.getDefaultKeyNameForIdentity(identityName) |
| 131 | |
| 132 | key = controller._identityManager.getPublicKey(keyName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame^] | 133 | #dump("key : ",key.getKeyDer().toHex()) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 134 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 135 | while controller._responseCount < 100: |
| 136 | face.processEvents() |
| 137 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 138 | time.sleep(0.01) |
| 139 | |
| 140 | face.shutdown() |
| 141 | |