Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2015, Regents of the University of California |
| 4 | * |
| 5 | * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN). |
| 6 | * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors. |
| 7 | * |
| 8 | * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * @author Zhiyi Zhang <dreamerbarrychang@gmail.com> |
| 20 | */ |
| 21 | |
| 22 | #include "group-manager.hpp" |
Yingdi Yu | 3decf4e | 2015-11-02 12:33:31 -0800 | [diff] [blame] | 23 | #include "algo/encryptor.hpp" |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 24 | #include "encrypted-content.hpp" |
| 25 | |
| 26 | #include <map> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace gep { |
| 30 | |
Yingdi Yu | 8c43fcc | 2016-03-09 18:23:57 -0800 | [diff] [blame] | 31 | GroupManager::GroupManager(const Name& prefix, const Name& dataType, const std::string& dbPath, |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 32 | const int paramLength, const int freshPeriod) |
Yingdi Yu | 4467c11 | 2015-10-19 09:27:45 -0700 | [diff] [blame] | 33 | : m_namespace(prefix) |
Yingdi Yu | 8c43fcc | 2016-03-09 18:23:57 -0800 | [diff] [blame] | 34 | , m_db(dbPath) |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 35 | , m_paramLength(paramLength) |
| 36 | , m_freshPeriod(freshPeriod) |
| 37 | { |
Yingdi Yu | 3decf4e | 2015-11-02 12:33:31 -0800 | [diff] [blame] | 38 | m_namespace.append(NAME_COMPONENT_READ).append(dataType); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | std::list<Data> |
Zhiyi Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 42 | GroupManager::getGroupKey(const TimeStamp& timeslot, bool needRegenerate) |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 43 | { |
| 44 | std::map<Name, Buffer> memberKeys; |
| 45 | std::list<Data> result; |
| 46 | |
| 47 | // get time interval |
| 48 | Interval finalInterval = calculateInterval(timeslot, memberKeys); |
| 49 | if (finalInterval.isValid() == false) |
| 50 | return result; |
| 51 | |
| 52 | std::string startTs = boost::posix_time::to_iso_string(finalInterval.getStartTime()); |
| 53 | std::string endTs = boost::posix_time::to_iso_string(finalInterval.getEndTime()); |
| 54 | |
| 55 | // generate the pri key and pub key |
| 56 | Buffer priKeyBuf, pubKeyBuf; |
Zhiyi Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 57 | Name eKeyName(m_namespace); |
| 58 | eKeyName.append(NAME_COMPONENT_E_KEY).append(startTs).append(endTs); |
| 59 | |
| 60 | if (!needRegenerate && m_db.hasEKey(eKeyName)) { |
| 61 | std::tie(pubKeyBuf, priKeyBuf) = getEKey(eKeyName); |
| 62 | } |
| 63 | else { |
| 64 | generateKeyPairs(priKeyBuf, pubKeyBuf); |
| 65 | if (m_db.hasEKey(eKeyName)) { |
| 66 | deleteEKey(eKeyName); |
| 67 | } |
| 68 | addEKey(eKeyName, pubKeyBuf, priKeyBuf); |
| 69 | } |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 70 | |
| 71 | // add the first element to the result |
| 72 | // E-KEY (public key) data packet name convention: |
| 73 | // /<data_type>/E-KEY/[start-ts]/[end-ts] |
| 74 | Data data = createEKeyData(startTs, endTs, pubKeyBuf); |
| 75 | result.push_back(data); |
| 76 | |
| 77 | // encrypt pri key with pub key from certificate |
| 78 | for (const auto& entry : memberKeys) { |
| 79 | const Name& keyName = entry.first; |
| 80 | const Buffer& certKey = entry.second; |
| 81 | |
| 82 | // generate the name of the packet |
| 83 | // D-KEY (private key) data packet name convention: |
| 84 | // /<data_type>/D-KEY/[start-ts]/[end-ts]/[member-name] |
| 85 | data = createDKeyData(startTs, endTs, keyName, priKeyBuf, certKey); |
| 86 | result.push_back(data); |
| 87 | } |
| 88 | return result; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | GroupManager::addSchedule(const std::string& scheduleName, const Schedule& schedule) |
| 93 | { |
| 94 | m_db.addSchedule(scheduleName, schedule); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | GroupManager::deleteSchedule(const std::string& scheduleName) |
| 99 | { |
| 100 | m_db.deleteSchedule(scheduleName); |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | GroupManager::updateSchedule(const std::string& scheduleName, const Schedule& schedule) |
| 105 | { |
| 106 | m_db.updateSchedule(scheduleName, schedule); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | GroupManager::addMember(const std::string& scheduleName, const Data& memCert) |
| 111 | { |
| 112 | IdentityCertificate cert(memCert); |
| 113 | m_db.addMember(scheduleName, cert.getPublicKeyName(), cert.getPublicKeyInfo().get()); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | GroupManager::removeMember(const Name& identity) |
| 118 | { |
| 119 | m_db.deleteMember(identity); |
| 120 | } |
| 121 | |
| 122 | void |
| 123 | GroupManager::updateMemberSchedule(const Name& identity, const std::string& scheduleName) |
| 124 | { |
| 125 | m_db.updateMemberSchedule(identity, scheduleName); |
| 126 | } |
| 127 | |
| 128 | Interval |
| 129 | GroupManager::calculateInterval(const TimeStamp& timeslot, std::map<Name, Buffer>& memberKeys) |
| 130 | { |
| 131 | // prepare |
| 132 | Interval positiveResult; |
| 133 | Interval negativeResult; |
| 134 | Interval tempInterval; |
| 135 | Interval finalInterval; |
| 136 | bool isPositive; |
| 137 | memberKeys.clear(); |
| 138 | |
| 139 | // get the all intervals from schedules |
| 140 | for (const std::string& scheduleName : m_db.listAllScheduleNames()) { |
| 141 | |
| 142 | const Schedule& schedule = m_db.getSchedule(scheduleName); |
| 143 | std::tie(isPositive, tempInterval) = schedule.getCoveringInterval(timeslot); |
| 144 | |
| 145 | if (isPositive) { |
| 146 | if (!positiveResult.isValid()) |
| 147 | positiveResult = tempInterval; |
| 148 | positiveResult && tempInterval; |
| 149 | |
| 150 | std::map<Name, Buffer> m = m_db.getScheduleMembers(scheduleName); |
| 151 | memberKeys.insert(m.begin(), m.end()); |
| 152 | } |
| 153 | else { |
| 154 | if (!negativeResult.isValid()) |
| 155 | negativeResult = tempInterval; |
| 156 | negativeResult && tempInterval; |
| 157 | } |
| 158 | } |
| 159 | if (!positiveResult.isValid()) { |
| 160 | // return invalid interval when there is no member has interval covering the time slot |
| 161 | return Interval(false); |
| 162 | } |
| 163 | |
| 164 | // get the final interval result |
| 165 | if (negativeResult.isValid()) |
| 166 | finalInterval = positiveResult && negativeResult; |
| 167 | else |
| 168 | finalInterval = positiveResult; |
| 169 | |
| 170 | return finalInterval; |
| 171 | } |
| 172 | |
| 173 | void |
| 174 | GroupManager::generateKeyPairs(Buffer& priKeyBuf, Buffer& pubKeyBuf) const |
| 175 | { |
| 176 | RandomNumberGenerator rng; |
| 177 | RsaKeyParams params(m_paramLength); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 178 | DecryptKey<algo::Rsa> privateKey = algo::Rsa::generateKey(rng, params); |
| 179 | priKeyBuf = privateKey.getKeyBits(); |
| 180 | EncryptKey<algo::Rsa> publicKey = algo::Rsa::deriveEncryptKey(priKeyBuf); |
| 181 | pubKeyBuf = publicKey.getKeyBits(); |
| 182 | } |
| 183 | |
| 184 | |
| 185 | Data |
| 186 | GroupManager::createEKeyData(const std::string& startTs, const std::string& endTs, |
| 187 | const Buffer& pubKeyBuf) |
| 188 | { |
Zhiyi Zhang | 3ac0d8d | 2015-10-28 14:07:09 +0800 | [diff] [blame] | 189 | Name name(m_namespace); |
Yingdi Yu | 3decf4e | 2015-11-02 12:33:31 -0800 | [diff] [blame] | 190 | name.append(NAME_COMPONENT_E_KEY).append(startTs).append(endTs); |
Zhiyi Zhang | 3ac0d8d | 2015-10-28 14:07:09 +0800 | [diff] [blame] | 191 | Data data(name); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 192 | data.setFreshnessPeriod(time::hours(m_freshPeriod)); |
| 193 | data.setContent(pubKeyBuf.get(), pubKeyBuf.size()); |
| 194 | m_keyChain.sign(data); |
| 195 | return data; |
| 196 | } |
| 197 | |
| 198 | Data |
| 199 | GroupManager::createDKeyData(const std::string& startTs, const std::string& endTs, |
| 200 | const Name& keyName, const Buffer& priKeyBuf, |
| 201 | const Buffer& certKey) |
| 202 | { |
Zhiyi Zhang | 3ac0d8d | 2015-10-28 14:07:09 +0800 | [diff] [blame] | 203 | Name name(m_namespace); |
Yingdi Yu | 3decf4e | 2015-11-02 12:33:31 -0800 | [diff] [blame] | 204 | name.append(NAME_COMPONENT_D_KEY); |
| 205 | name.append(startTs).append(endTs); |
Zhiyi Zhang | 3ac0d8d | 2015-10-28 14:07:09 +0800 | [diff] [blame] | 206 | Data data = Data(name); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 207 | data.setFreshnessPeriod(time::hours(m_freshPeriod)); |
Yingdi Yu | 883f420 | 2015-11-02 13:54:11 -0800 | [diff] [blame] | 208 | algo::EncryptParams eparams(tlv::AlgorithmRsaOaep); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 209 | algo::encryptData(data, priKeyBuf.buf(), priKeyBuf.size(), keyName, |
| 210 | certKey.buf(), certKey.size(), eparams); |
| 211 | m_keyChain.sign(data); |
| 212 | return data; |
| 213 | } |
| 214 | |
Zhiyi Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 215 | void |
| 216 | GroupManager::addEKey(const Name& eKeyName, const Buffer& pubKey, const Buffer& priKey) |
| 217 | { |
| 218 | m_db.addEKey(eKeyName, pubKey, priKey); |
| 219 | } |
| 220 | |
| 221 | std::tuple<Buffer, Buffer> |
| 222 | GroupManager::getEKey(const Name& eKeyName) |
| 223 | { |
| 224 | return m_db.getEKey(eKeyName); |
| 225 | } |
| 226 | |
| 227 | void |
| 228 | GroupManager::deleteEKey(const Name& eKeyName) |
| 229 | { |
| 230 | m_db.deleteEKey(eKeyName); |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | GroupManager::cleanEKeys() |
| 235 | { |
| 236 | m_db.cleanEKeys(); |
| 237 | } |
| 238 | |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 239 | } // namespace ndn |
| 240 | } // namespace ndn |