blob: deac7683719e6bdc26b2759cb70a50a42af11e68 [file] [log] [blame]
Zhiyi Zhang84986cc2015-09-21 00:26:07 +08001/* -*- 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"
23#include "encryptor.hpp"
24#include "encrypted-content.hpp"
25
26#include <map>
27
28namespace ndn {
29namespace gep {
30
Yingdi Yu4467c112015-10-19 09:27:45 -070031static const name::Component E_KEY_COMPONENT("E-KEY");
32static const name::Component D_KEY_COMPONENT("D-KEY");
33static const name::Component READ_COMPONENT("read");
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080034
Yingdi Yu4467c112015-10-19 09:27:45 -070035GroupManager::GroupManager(const Name& prefix, const Name& dataType, const std::string& dbDir,
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080036 const int paramLength, const int freshPeriod)
Yingdi Yu4467c112015-10-19 09:27:45 -070037 : m_namespace(prefix)
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080038 , m_db(dbDir)
39 , m_paramLength(paramLength)
40 , m_freshPeriod(freshPeriod)
41{
Yingdi Yu4467c112015-10-19 09:27:45 -070042 m_namespace.append(READ_COMPONENT).append(dataType);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080043}
44
45std::list<Data>
46GroupManager::getGroupKey(const TimeStamp& timeslot)
47{
48 std::map<Name, Buffer> memberKeys;
49 std::list<Data> result;
50
51 // get time interval
52 Interval finalInterval = calculateInterval(timeslot, memberKeys);
53 if (finalInterval.isValid() == false)
54 return result;
55
56 std::string startTs = boost::posix_time::to_iso_string(finalInterval.getStartTime());
57 std::string endTs = boost::posix_time::to_iso_string(finalInterval.getEndTime());
58
59 // generate the pri key and pub key
60 Buffer priKeyBuf, pubKeyBuf;
61 generateKeyPairs(priKeyBuf, pubKeyBuf);
62
63 // add the first element to the result
64 // E-KEY (public key) data packet name convention:
65 // /<data_type>/E-KEY/[start-ts]/[end-ts]
66 Data data = createEKeyData(startTs, endTs, pubKeyBuf);
67 result.push_back(data);
68
69 // encrypt pri key with pub key from certificate
70 for (const auto& entry : memberKeys) {
71 const Name& keyName = entry.first;
72 const Buffer& certKey = entry.second;
73
74 // generate the name of the packet
75 // D-KEY (private key) data packet name convention:
76 // /<data_type>/D-KEY/[start-ts]/[end-ts]/[member-name]
77 data = createDKeyData(startTs, endTs, keyName, priKeyBuf, certKey);
78 result.push_back(data);
79 }
80 return result;
81}
82
83void
84GroupManager::addSchedule(const std::string& scheduleName, const Schedule& schedule)
85{
86 m_db.addSchedule(scheduleName, schedule);
87}
88
89void
90GroupManager::deleteSchedule(const std::string& scheduleName)
91{
92 m_db.deleteSchedule(scheduleName);
93}
94
95void
96GroupManager::updateSchedule(const std::string& scheduleName, const Schedule& schedule)
97{
98 m_db.updateSchedule(scheduleName, schedule);
99}
100
101void
102GroupManager::addMember(const std::string& scheduleName, const Data& memCert)
103{
104 IdentityCertificate cert(memCert);
105 m_db.addMember(scheduleName, cert.getPublicKeyName(), cert.getPublicKeyInfo().get());
106}
107
108void
109GroupManager::removeMember(const Name& identity)
110{
111 m_db.deleteMember(identity);
112}
113
114void
115GroupManager::updateMemberSchedule(const Name& identity, const std::string& scheduleName)
116{
117 m_db.updateMemberSchedule(identity, scheduleName);
118}
119
120Interval
121GroupManager::calculateInterval(const TimeStamp& timeslot, std::map<Name, Buffer>& memberKeys)
122{
123 // prepare
124 Interval positiveResult;
125 Interval negativeResult;
126 Interval tempInterval;
127 Interval finalInterval;
128 bool isPositive;
129 memberKeys.clear();
130
131 // get the all intervals from schedules
132 for (const std::string& scheduleName : m_db.listAllScheduleNames()) {
133
134 const Schedule& schedule = m_db.getSchedule(scheduleName);
135 std::tie(isPositive, tempInterval) = schedule.getCoveringInterval(timeslot);
136
137 if (isPositive) {
138 if (!positiveResult.isValid())
139 positiveResult = tempInterval;
140 positiveResult && tempInterval;
141
142 std::map<Name, Buffer> m = m_db.getScheduleMembers(scheduleName);
143 memberKeys.insert(m.begin(), m.end());
144 }
145 else {
146 if (!negativeResult.isValid())
147 negativeResult = tempInterval;
148 negativeResult && tempInterval;
149 }
150 }
151 if (!positiveResult.isValid()) {
152 // return invalid interval when there is no member has interval covering the time slot
153 return Interval(false);
154 }
155
156 // get the final interval result
157 if (negativeResult.isValid())
158 finalInterval = positiveResult && negativeResult;
159 else
160 finalInterval = positiveResult;
161
162 return finalInterval;
163}
164
165void
166GroupManager::generateKeyPairs(Buffer& priKeyBuf, Buffer& pubKeyBuf) const
167{
168 RandomNumberGenerator rng;
169 RsaKeyParams params(m_paramLength);
170 algo::EncryptParams eparams(tlv::AlgorithmRsaPkcs);
171 DecryptKey<algo::Rsa> privateKey = algo::Rsa::generateKey(rng, params);
172 priKeyBuf = privateKey.getKeyBits();
173 EncryptKey<algo::Rsa> publicKey = algo::Rsa::deriveEncryptKey(priKeyBuf);
174 pubKeyBuf = publicKey.getKeyBits();
175}
176
177
178Data
179GroupManager::createEKeyData(const std::string& startTs, const std::string& endTs,
180 const Buffer& pubKeyBuf)
181{
182 Name dataName(m_namespace);
183 dataName.append(E_KEY_COMPONENT).append(startTs).append(endTs);
184 Data data(dataName);
185 data.setFreshnessPeriod(time::hours(m_freshPeriod));
186 data.setContent(pubKeyBuf.get(), pubKeyBuf.size());
187 m_keyChain.sign(data);
188 return data;
189}
190
191Data
192GroupManager::createDKeyData(const std::string& startTs, const std::string& endTs,
193 const Name& keyName, const Buffer& priKeyBuf,
194 const Buffer& certKey)
195{
196 Name dataName(m_namespace);
197 dataName.append(D_KEY_COMPONENT);
198 dataName.append(startTs).append(endTs).append(keyName.getPrefix(-1));
199 Data data = Data(dataName);
200 data.setFreshnessPeriod(time::hours(m_freshPeriod));
201 algo::EncryptParams eparams(tlv::AlgorithmRsaPkcs);
202 algo::encryptData(data, priKeyBuf.buf(), priKeyBuf.size(), keyName,
203 certKey.buf(), certKey.size(), eparams);
204 m_keyChain.sign(data);
205 return data;
206}
207
208} // namespace ndn
209} // namespace ndn