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 |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 26 | from pyndn.key_locator import KeyLocator, KeyLocatorType |
| 27 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 28 | from pyndn.security import KeyChain |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 29 | from base_node import BaseNode |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 30 | from pyndn.security import SecurityException |
| 31 | from pyndn.util import Blob |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 32 | |
| 33 | def dump(*list): |
| 34 | result = "" |
| 35 | for element in list: |
| 36 | result += (element if type(element) is str else repr(element)) + " " |
| 37 | print(result) |
| 38 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 39 | class Controller(BaseNode): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 40 | def __init__(self,configFileName): |
| 41 | super(Controller, self).__init__(configFileName=configFileName) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 42 | self._responseCount = 0 |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 43 | self._symmetricKey = "symmetricKeyForBootstrapping" |
| 44 | self._prefix = "/home" |
| 45 | self._identity = "/home/controller/id999" |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 46 | |
| 47 | def onInterest(self, prefix, interest, transport, registeredPrefixId): |
| 48 | self._responseCount += 1 |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 49 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 50 | interestName = interest.getName() |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 51 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 52 | #for bootstrap interest |
| 53 | if(interestName.toUri().startswith(self._bootstrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey): |
| 54 | dump("Reveived bootstrap interest") |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 55 | self.onBootstrapInterest(prefix, interest, transport, registeredPrefixId) |
| 56 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 57 | elif ("KEY" in interestName.toUri() and "ID-CERT" in interestName.toUri()): |
| 58 | dump("Reveived certificate request interest") |
| 59 | self.onCertificateRequest(prefix, interest, transport, registeredPrefixId) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 60 | |
| 61 | def onBootstrapInterest(self, prefix, interest, transport, registeredPrefixId): |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 62 | |
| 63 | interestName = interest.getName() |
| 64 | deviceParameters = json.loads(interestName.get(3).getValue().toRawStr()) |
| 65 | deviceNewIdentity = Name("/home") |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 66 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 67 | #create new identity for device |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 68 | deviceNewIdentity.append(deviceParameters["category"]) |
| 69 | deviceNewIdentity.append(deviceParameters["id"]) |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 70 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 71 | #generate content |
| 72 | content = {} |
| 73 | content["deviceNewIdentity"] = deviceNewIdentity.toUri() |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 74 | content["controllerIdentity"] = self._identity |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 75 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 76 | #get public key of controller |
| 77 | pKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._identityManager.getDefaultIdentity()) |
| 78 | pKey = self._identityManager.getPublicKey(pKeyName) |
| 79 | |
| 80 | pKeyInfo = content["controllerPublicKey"] = {} |
| 81 | pKeyInfo["keyName"] = pKeyName.toUri() |
| 82 | pKeyInfo["keyType"] = pKey.getKeyType() |
| 83 | pKeyInfo["publicKeyDer"] = pKey.getKeyDer().toRawStr() |
| 84 | dump("Sent content : ",content) |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 85 | |
| 86 | #TODO generate signature for data |
| 87 | |
| 88 | #generate data package |
| 89 | data = Data(interestName) |
| 90 | data.setContent(json.dumps(content,encoding="latin-1")) |
| 91 | #data.setSignature(signature) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 92 | encodedData = data.wireEncode() |
| 93 | transport.send(encodedData.toBuffer()) |
| 94 | |
| 95 | |
| 96 | def onCertificateRequest(self, prefix, interest, transport, registeredPrefixId): |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 97 | interestName = interest.getName() |
| 98 | dump("interest name : ",interestName) |
| 99 | |
| 100 | keyName = interestName[:3] |
| 101 | keyId = interestName.get(4) |
| 102 | keyName.append(keyId) |
| 103 | keyInfo = json.loads(interestName.get(5).getValue().toRawStr(),encoding="latin-1") |
| 104 | keyType = keyInfo['keyType'] |
| 105 | keyDer = Blob().fromRawStr(keyInfo['keyDer']) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 106 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 107 | dump("keyname: ",keyName) |
| 108 | dump("keyType ",keyInfo['keyType']) |
| 109 | dump("keyDer string",keyInfo['keyDer']) |
| 110 | dump("keyDer",keyDer) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 111 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 112 | #device and controller are on one mechine, so it needs to be done. |
| 113 | self._identityManager.setDefaultIdentity(Name(self._identity)) |
| 114 | try: |
| 115 | self._identityStorage.addKey(keyName, keyType, keyDer) |
| 116 | except SecurityException: |
| 117 | dump("The public key for device already exists ") |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 118 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 119 | signedCertificate = self._identityManager._generateCertificateForKey(keyName) |
| 120 | self._keyChain.sign(signedCertificate, self._identityManager.getDefaultCertificateName()) |
| 121 | self._identityManager.addCertificate(signedCertificate) |
| 122 | |
| 123 | encodedData = signedCertificate.wireEncode() |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 124 | transport.send(encodedData.toBuffer()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 125 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 126 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 127 | def onRegisterFailed(self, prefix): |
| 128 | self._responseCount += 1 |
| 129 | dump("Register failed for prefix", prefix.toUri()) |
| 130 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 131 | def beforeLoopStart(self): |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 132 | identityName = Name(self._identity) |
| 133 | |
| 134 | defaultIdentityExists = True |
| 135 | try: |
| 136 | defaultIdentityName = self._identityManager.getDefaultIdentity() |
| 137 | except: |
| 138 | defaultIdentityExists = False |
| 139 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 140 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 141 | #dump(self._identityManager.getDefaultKeyNameForIdentity(self._identity)) |
| 142 | if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName: |
| 143 | #make one |
| 144 | dump("Set default identity: ",identityName) |
| 145 | #self._identityManager.createIdentityAndCertificate(identityName) |
| 146 | self._identityStorage.addIdentity(identityName) |
| 147 | self._identityManager.setDefaultIdentity(identityName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 148 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 149 | try: |
| 150 | self._identityManager.getDefaultKeyNameForIdentity(identityName) |
| 151 | except SecurityException: |
| 152 | newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._identity), isKsk=True) |
| 153 | newCert = self._identityManager.selfSign(newKey) |
| 154 | dump("generated new KSK certificate ", newCert) |
| 155 | self._identityManager.addCertificateAsIdentityDefault(newCert) |
| 156 | |
| 157 | |
| 158 | #self.face.registerPrefix(self._prefix, self.onInterest, self.onRegisterFailed) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 159 | |
| 160 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 161 | if __name__ == '__main__': |
| 162 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 163 | controller = Controller("default.conf") |
| 164 | controller.beforeLoopStart() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 165 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 166 | face = Face() |
| 167 | face.setCommandSigningInfo(controller._keyChain, controller._keyChain.getDefaultCertificateName()) |
| 168 | dump("Register prefix : ", controller._prefix) |
| 169 | face.registerPrefix(controller._prefix, controller.onInterest, controller.onRegisterFailed) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 170 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 171 | while True: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 172 | face.processEvents() |
| 173 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 174 | time.sleep(0.01) |
| 175 | |
| 176 | face.shutdown() |
| 177 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame^] | 178 | |
| 179 | |
| 180 | |
| 181 | |