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