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