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