philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 1 | # -*- Mode:python; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | # |
Teng Liang | 29aa601 | 2015-06-28 15:31:01 -0700 | [diff] [blame] | 3 | # Copyright (C) 2014 Regents of the University of California. |
| 4 | # Author: Teng Liang <philoliang2011@gmail.com> |
| 5 | # |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 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 |
Teng Liang | 29aa601 | 2015-06-28 15:31:01 -0700 | [diff] [blame] | 14 | # GNU General Public License for more details. |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 15 | # |
Teng Liang | 29aa601 | 2015-06-28 15:31:01 -0700 | [diff] [blame] | 16 | # You should have received a copy of the GNU General Public License |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
Teng Liang | 29aa601 | 2015-06-28 15:31:01 -0700 | [diff] [blame] | 18 | # A copy of the GNU General Public License is in the file COPYING. |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 19 | |
| 20 | |
| 21 | import time |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 22 | import json |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 23 | from pyndn import Name, Face, Interest, Data |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 24 | from pyndn.key_locator import KeyLocator, KeyLocatorType |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 25 | from hmac_helper import HmacHelper |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 26 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 27 | from pyndn.security import KeyChain |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 28 | from base_node import BaseNode |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 29 | from pyndn.security import SecurityException |
| 30 | from pyndn.util import Blob |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 31 | |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 32 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 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 | 938be58 | 2015-07-15 16:25:26 -0700 | [diff] [blame^] | 40 | def __init__(self,configFileName=None): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 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" |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 46 | self._hmacHelper = HmacHelper(self._symmetricKey) |
| 47 | |
| 48 | def setFace(self,face): |
| 49 | self.face = face |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 50 | |
| 51 | def onInterest(self, prefix, interest, transport, registeredPrefixId): |
| 52 | self._responseCount += 1 |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 53 | interestName = interest.getName() |
Teng Liang | 29aa601 | 2015-06-28 15:31:01 -0700 | [diff] [blame] | 54 | dump("received interest : ",interestName.toUri()) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 55 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 56 | #for bootstrap interest |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 57 | #if(interestName.toUri().startswith(self._bootstrapPrefix) and interest.getKeyLocator().getKeyData().toRawStr() == self._symmetricKey): |
| 58 | if(interestName.toUri().startswith(self._bootstrapPrefix) and self._hmacHelper.verifyInterest(interest)): |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 59 | dump("Reveived bootstrap interest") |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 60 | self.onBootstrapInterest(prefix, interest, transport, registeredPrefixId) |
| 61 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 62 | elif ("KEY" in interestName.toUri() and "ID-CERT" in interestName.toUri()): |
| 63 | dump("Reveived certificate request interest") |
| 64 | self.onCertificateRequest(prefix, interest, transport, registeredPrefixId) |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 65 | |
| 66 | def onBootstrapInterest(self, prefix, interest, transport, registeredPrefixId): |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 67 | if (self._hmacHelper.verifyInterest(interest)): |
| 68 | self.log.info("Bootstrap interest verified") |
| 69 | interestName = interest.getName() |
| 70 | deviceParameters = json.loads(interestName.get(3).getValue().toRawStr()) |
| 71 | deviceNewIdentity = Name("/home") |
| 72 | |
| 73 | #create new identity for device |
| 74 | deviceNewIdentity.append(deviceParameters["category"]) |
| 75 | deviceNewIdentity.append(deviceParameters["id"]) |
| 76 | |
| 77 | #generate content |
| 78 | content = {} |
| 79 | content["deviceNewIdentity"] = deviceNewIdentity.toUri() |
| 80 | content["controllerIdentity"] = self._identity |
| 81 | |
| 82 | #get public key of controller |
| 83 | pKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._identityManager.getDefaultIdentity()) |
| 84 | pKey = self._identityManager.getPublicKey(pKeyName) |
| 85 | |
| 86 | pKeyInfo = content["controllerPublicKey"] = {} |
| 87 | pKeyInfo["keyName"] = pKeyName.toUri() |
| 88 | pKeyInfo["keyType"] = pKey.getKeyType() |
| 89 | pKeyInfo["publicKeyDer"] = pKey.getKeyDer().toRawStr() |
| 90 | dump("Sent content : ",content) |
| 91 | |
| 92 | #TODO generate signature for data |
| 93 | |
| 94 | #generate data package |
| 95 | data = Data(interestName) |
| 96 | data.setContent(json.dumps(content,encoding="latin-1")) |
| 97 | #data.setSignature(signature) |
| 98 | self._hmacHelper.signData(data) |
| 99 | |
| 100 | #encodedData = data.wireEncode() |
| 101 | #transport.send(encodedData.toBuffer()) |
| 102 | self.sendData(data,transport,sign=False) |
| 103 | else: |
| 104 | self.log.info("Bootstrap interest not verified") |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 105 | |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 106 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 107 | |
| 108 | |
| 109 | def onCertificateRequest(self, prefix, interest, transport, registeredPrefixId): |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 110 | if (self._hmacHelper.verifyInterest(interest)): |
| 111 | self.log.info("certificate request interest verified") |
| 112 | interestName = interest.getName() |
| 113 | dump("interest name : ",interestName) |
| 114 | |
| 115 | keyName = interestName[:3] |
| 116 | keyId = interestName.get(4) |
| 117 | keyName.append(keyId) |
| 118 | keyInfo = json.loads(interestName.get(5).getValue().toRawStr(),encoding="latin-1") |
| 119 | keyType = keyInfo['keyType'] |
| 120 | keyDer = Blob().fromRawStr(keyInfo['keyDer']) |
| 121 | |
| 122 | #dump("keyname: ",keyName) |
| 123 | dump("keyType ",keyInfo['keyType']) |
| 124 | dump("keyDer string",keyInfo['keyDer']) |
| 125 | dump("keyDer",keyDer) |
| 126 | |
| 127 | #device and controller are on one mechine, so it needs to be done. |
| 128 | self._identityManager.setDefaultIdentity(Name(self._identity)) |
| 129 | try: |
| 130 | self._identityStorage.addKey(keyName, keyType, keyDer) |
| 131 | except SecurityException: |
| 132 | dump("The public key for device already exists ") |
| 133 | |
| 134 | signedCertificate = self._identityManager._generateCertificateForKey(keyName) |
| 135 | self._keyChain.sign(signedCertificate, self._identityManager.getDefaultCertificateName()) |
| 136 | self._identityManager.addCertificate(signedCertificate) |
| 137 | #self._hmacHelper.signData() |
| 138 | |
| 139 | #encodedData = signedCertificate.wireEncode() |
| 140 | #transport.send(encodedData.toBuffer()) |
| 141 | self.sendData(signedCertificate,transport,sign=False) |
| 142 | |
| 143 | self.log.info("Certificate sent back : {}".format(signedCertificate.__str__)) |
| 144 | print(signedCertificate) |
| 145 | else: |
| 146 | self.log.info("certificate request interest not verified") |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 147 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 148 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame] | 149 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 150 | def onRegisterFailed(self, prefix): |
| 151 | self._responseCount += 1 |
| 152 | dump("Register failed for prefix", prefix.toUri()) |
| 153 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 154 | def beforeLoopStart(self): |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 155 | identityName = Name(self._identity) |
| 156 | |
| 157 | defaultIdentityExists = True |
| 158 | try: |
| 159 | defaultIdentityName = self._identityManager.getDefaultIdentity() |
| 160 | except: |
| 161 | defaultIdentityExists = False |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 162 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 163 | if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName: |
| 164 | #make one |
Teng Liang | 1228600 | 2015-06-10 12:52:22 -0700 | [diff] [blame] | 165 | dump("Create identity and certificate for identity name: ",identityName) |
| 166 | self._keyChain.createIdentityAndCertificate(identityName) |
| 167 | self._identityManager.setDefaultIdentity(identityName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 168 | |
Teng Liang | 938be58 | 2015-07-15 16:25:26 -0700 | [diff] [blame^] | 169 | self.face.setCommandSigningInfo(self._keyChain, self._keyChain.getDefaultCertificateName()) |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame] | 170 | self.face.registerPrefix(self._prefix, self.onInterest, self.onRegisterFailed) |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 171 | |
| 172 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 173 | |
| 174 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 175 | if __name__ == '__main__': |
| 176 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 177 | controller = Controller("default.conf") |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame] | 178 | controller.start() |
| 179 | #controller.beforeLoopStart() |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 180 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 181 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame] | 182 | #face = Face() |
| 183 | #controller.setFace(face) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 184 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame] | 185 | #face.setCommandSigningInfo(controller._keyChain, controller.getDefaultCertificateName()) |
| 186 | #face.registerPrefix(controller._prefix, controller.onInterest, controller.onRegisterFailed) |
| 187 | #dump("Register prefix : ",controller._prefix) |
| 188 | #while True: |
| 189 | # face.processEvents() |
| 190 | # time.sleep(0.05) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 191 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame] | 192 | # face.shutdown() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 193 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 194 | |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 195 | |
| 196 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 197 | |
| 198 | |
| 199 | |