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 |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 21 | import json |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 22 | from pyndn import Name |
| 23 | from pyndn import Face |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 24 | from pyndn import Interest |
| 25 | from pyndn import KeyLocator, KeyLocatorType |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 26 | from base_node import BaseNode |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 27 | from pyndn.security.security_exception import SecurityException |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 28 | from pyndn.util import Blob |
Teng Liang | 52f43c3 | 2015-05-20 17:06:20 -0700 | [diff] [blame] | 29 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 30 | |
| 31 | def dump(*list): |
| 32 | result = "" |
| 33 | for element in list: |
| 34 | result += (element if type(element) is str else repr(element)) + " " |
| 35 | print(result) |
| 36 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 37 | class Device(BaseNode): |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 38 | def __init__(self,configFileName,face): |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 39 | super(Device, self).__init__(configFileName=configFileName) |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 40 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 41 | #self.deviceSerial = self.getSerial() |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 42 | self._callbackCount = 0 |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 43 | self._face = face |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 44 | |
| 45 | def onData(self, interest, data): |
| 46 | self._callbackCount += 1 |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 47 | dump("Got data packet with name : ", data.getName().toUri()) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 48 | # Use join to convert each byte to chr. |
| 49 | dump(data.getContent().toRawStr()) |
| 50 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 51 | #haven't checked symmetric key digest yet |
| 52 | if (data.getName().toUri().startswith(self._bootstrapPrefix)): |
| 53 | self.onBootstrapData(interest, data) |
| 54 | |
| 55 | def expressBootstrapInterest(self): |
| 56 | symKey = "symmetricKeyForBootstrapping" |
| 57 | |
| 58 | #generate bootstrap name /home/controller/bootstrap/<device-parameters> |
| 59 | bootstrapName = Name(device._bootstrapPrefix) |
| 60 | |
| 61 | deviceParameters = {} |
| 62 | deviceParameters["category"] = "sensors" |
| 63 | deviceParameters["id"] = "T123456789" |
| 64 | bootstrapName.append(json.dumps(deviceParameters)) |
| 65 | |
| 66 | bootstrapInterest = Interest(bootstrapName) |
| 67 | |
| 68 | bootstrapInterest.setInterestLifetimeMilliseconds(5000) |
| 69 | |
| 70 | bootstrapKeyLocator = KeyLocator() |
| 71 | bootstrapKeyLocator.setType(KeyLocatorType.KEY_LOCATOR_DIGEST) |
| 72 | bootstrapKeyLocator.setKeyData(symKey) |
| 73 | bootstrapInterest.setKeyLocator(bootstrapKeyLocator) |
| 74 | |
| 75 | dump("Express interest :",bootstrapInterest.toUri()) |
| 76 | self._face.expressInterest(bootstrapInterest, self.onBootstrapData, self.onTimeout) |
| 77 | |
| 78 | def onBootstrapData(self, interest, data): |
| 79 | dump("Data received.") |
| 80 | content = json.loads(data.getContent().toRawStr(), encoding="latin-1") |
| 81 | deviceNewIdentity = Name(content["deviceNewIdentity"]) |
| 82 | controllerIdentity = Name(content["controllerIdentity"]) |
| 83 | controllerPublicKeyInfo = content["controllerPublicKey"] |
| 84 | |
| 85 | #set new identity as default and generate default key-pair with KSK Certificate |
| 86 | self._identityStorage.addIdentity(deviceNewIdentity) |
| 87 | self._identityManager.setDefaultIdentity(deviceNewIdentity) |
| 88 | try: |
| 89 | self._identityManager.getDefaultKeyNameForIdentity(deviceNewIdentity) |
| 90 | except SecurityException: |
| 91 | #generate new key-pair and certificate for new identity |
| 92 | dump("Installed new identity as default\nGenerating new key-pair and self signed certificate...") |
| 93 | newKey = self._identityManager.generateRSAKeyPairAsDefault(Name(deviceNewIdentity), isKsk=True) |
| 94 | newCert = self._identityManager.selfSign(newKey) |
| 95 | self._identityManager.addCertificateAsIdentityDefault(newCert) |
| 96 | |
| 97 | #add controller's identity and public key |
| 98 | keyType = controllerPublicKeyInfo["keyType"] |
| 99 | keyName = Name(controllerPublicKeyInfo["keyName"]) |
| 100 | keyDer = Blob().fromRawStr(controllerPublicKeyInfo["publicKeyDer"]) |
| 101 | dump("KeyType: ",keyType) |
| 102 | dump("keyName: ",keyName) |
| 103 | dump("Controller public key der : ",keyDer) |
| 104 | |
| 105 | self._identityStorage.addIdentity(controllerIdentity) |
| 106 | try: |
| 107 | self._identityStorage.addKey(keyName, keyType, keyDer) |
| 108 | dump("Controller's identity, key and certificate installled.") |
| 109 | except SecurityException: |
| 110 | dump("Controller's identity, key, certificate already exists.") |
| 111 | |
| 112 | #express an certificate request interest |
| 113 | defaultKeyName = self._identityManager.getDefaultKeyNameForIdentity(self._keyChain.getDefaultIdentity() ) |
| 114 | self.requestCertificate(defaultKeyName) |
| 115 | |
| 116 | |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 117 | def beforeLoopStart(self): |
| 118 | pass |
| 119 | |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 120 | def onTimeout(self, interest): |
| 121 | self._callbackCount += 1 |
| 122 | dump("Time out for interest", interest.getName().toUri()) |
| 123 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 124 | def requestCertificate(self, keyIdentity): |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 125 | """ |
| 126 | We compose a command interest with our public key info so the controller |
| 127 | can sign us a certificate that can be used with other nodes in the network. |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 128 | Name format : /home/<device-category>/KEY/<device-id>/<key-id>/<publickey>/ID-CERT/<version-number> |
| 129 | """ |
| 130 | certificateRequestName = self._keyChain.getDefaultIdentity() |
| 131 | deviceIdComponent = certificateRequestName.get(-1) |
| 132 | keyIdComponent = keyIdentity.get(-1) |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 133 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 134 | certificateRequestName = certificateRequestName |
| 135 | certificateRequestName.append("KEY") |
| 136 | #certificateRequestName.append(deviceIdComponent) |
| 137 | certificateRequestName.append(keyIdComponent) |
| 138 | |
| 139 | key = self._identityManager.getPublicKey(keyIdentity) |
| 140 | keyInfo = {} |
| 141 | keyInfo["keyType"] = key.getKeyType() |
| 142 | keyInfo["keyDer"] = key.getKeyDer().toRawStr() |
| 143 | |
| 144 | certificateRequestName.append(json.dumps(keyInfo, encoding="latin-1")) |
| 145 | |
| 146 | certificateRequestName.append("ID-CERT") |
| 147 | |
| 148 | dump("Sending certificate request : ",certificateRequestName) |
| 149 | |
| 150 | self._face.expressInterest(Interest(certificateRequestName), self.onCertificateData, self.onTimeout) |
| 151 | #TODO use symmetric key to sign |
| 152 | |
| 153 | def onCertificateData(self, interest, data): |
| 154 | dump("OnCertificateData : ",data) |
| 155 | |
philoL | fb1b24e | 2015-05-15 05:33:25 -0700 | [diff] [blame] | 156 | |
philo | 5d4724e | 2014-11-10 19:34:05 +0000 | [diff] [blame] | 157 | if __name__ == '__main__': |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 158 | face = Face("") |
| 159 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 160 | device = Device("default.conf",face) |
Teng Liang | a0b4937 | 2015-05-15 05:30:27 -0700 | [diff] [blame] | 161 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 162 | device.expressBootstrapInterest() |
| 163 | #device.requestCertificate(device._identityManager.getDefaultKeyNameForIdentity(device._keyChain.getDefaultIdentity() )) |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 164 | |
Teng Liang | 5042940 | 2015-05-22 16:01:17 -0700 | [diff] [blame^] | 165 | while device._callbackCount < 1000: |
philoL | bd28e13 | 2015-04-16 23:54:21 -0700 | [diff] [blame] | 166 | face.processEvents() |
| 167 | # We need to sleep for a few milliseconds so we don't use 100% of the CPU. |
| 168 | time.sleep(0.01) |
| 169 | |
| 170 | face.shutdown() |
| 171 | |