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