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 |
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 | 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" |
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 | b09af86 | 2015-06-01 10:28:12 -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 |
| 162 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 163 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 164 | #dump(self._identityManager.getDefaultKeyNameForIdentity(self._identity)) |
| 165 | if not defaultIdentityExists or self._identityManager.getDefaultIdentity() != identityName: |
| 166 | #make one |
| 167 | dump("Set default identity: ",identityName) |
| 168 | #self._identityManager.createIdentityAndCertificate(identityName) |
| 169 | self._identityStorage.addIdentity(identityName) |
| 170 | self._identityManager.setDefaultIdentity(identityName) |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 171 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 172 | try: |
| 173 | self._identityManager.getDefaultKeyNameForIdentity(identityName) |
| 174 | except SecurityException: |
| 175 | newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(self._identity), isKsk=True) |
| 176 | newCert = self._identityManager.selfSign(newKey) |
| 177 | dump("generated new KSK certificate ", newCert) |
| 178 | self._identityManager.addCertificateAsIdentityDefault(newCert) |
| 179 | |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 180 | #self.face.setCommandSigningInfo(self._keyChain, self.getDefaultCertificateName()) |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame^] | 181 | self.face.registerPrefix(self._prefix, self.onInterest, self.onRegisterFailed) |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 182 | |
| 183 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 184 | |
| 185 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 186 | if __name__ == '__main__': |
| 187 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 188 | controller = Controller("default.conf") |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame^] | 189 | controller.start() |
| 190 | #controller.beforeLoopStart() |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 191 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 192 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame^] | 193 | #face = Face() |
| 194 | #controller.setFace(face) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 195 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame^] | 196 | #face.setCommandSigningInfo(controller._keyChain, controller.getDefaultCertificateName()) |
| 197 | #face.registerPrefix(controller._prefix, controller.onInterest, controller.onRegisterFailed) |
| 198 | #dump("Register prefix : ",controller._prefix) |
| 199 | #while True: |
| 200 | # face.processEvents() |
| 201 | # time.sleep(0.05) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 202 | |
Teng Liang | b1c842d | 2015-06-01 10:31:07 -0700 | [diff] [blame^] | 203 | # face.shutdown() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 204 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 205 | |
Teng Liang | b09af86 | 2015-06-01 10:28:12 -0700 | [diff] [blame] | 206 | |
| 207 | |
Teng Liang | 4662b37 | 2015-05-27 15:48:36 -0700 | [diff] [blame] | 208 | |
| 209 | |
| 210 | |