blob: 345eb79f4cfe962fb789be1594aff5361cff1806 [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"
Yingdi Yu3decf4e2015-11-02 12:33:31 -080023#include "algo/encryptor.hpp"
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080024#include "encrypted-content.hpp"
25
26#include <map>
27
28namespace ndn {
29namespace gep {
30
Yingdi Yu4467c112015-10-19 09:27:45 -070031GroupManager::GroupManager(const Name& prefix, const Name& dataType, const std::string& dbDir,
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080032 const int paramLength, const int freshPeriod)
Yingdi Yu4467c112015-10-19 09:27:45 -070033 : m_namespace(prefix)
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080034 , m_db(dbDir)
35 , m_paramLength(paramLength)
36 , m_freshPeriod(freshPeriod)
37{
Yingdi Yu3decf4e2015-11-02 12:33:31 -080038 m_namespace.append(NAME_COMPONENT_READ).append(dataType);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080039}
40
41std::list<Data>
42GroupManager::getGroupKey(const TimeStamp& timeslot)
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;
57 generateKeyPairs(priKeyBuf, pubKeyBuf);
58
59 // add the first element to the result
60 // E-KEY (public key) data packet name convention:
61 // /<data_type>/E-KEY/[start-ts]/[end-ts]
62 Data data = createEKeyData(startTs, endTs, pubKeyBuf);
63 result.push_back(data);
64
65 // encrypt pri key with pub key from certificate
66 for (const auto& entry : memberKeys) {
67 const Name& keyName = entry.first;
68 const Buffer& certKey = entry.second;
69
70 // generate the name of the packet
71 // D-KEY (private key) data packet name convention:
72 // /<data_type>/D-KEY/[start-ts]/[end-ts]/[member-name]
73 data = createDKeyData(startTs, endTs, keyName, priKeyBuf, certKey);
74 result.push_back(data);
75 }
76 return result;
77}
78
79void
80GroupManager::addSchedule(const std::string& scheduleName, const Schedule& schedule)
81{
82 m_db.addSchedule(scheduleName, schedule);
83}
84
85void
86GroupManager::deleteSchedule(const std::string& scheduleName)
87{
88 m_db.deleteSchedule(scheduleName);
89}
90
91void
92GroupManager::updateSchedule(const std::string& scheduleName, const Schedule& schedule)
93{
94 m_db.updateSchedule(scheduleName, schedule);
95}
96
97void
98GroupManager::addMember(const std::string& scheduleName, const Data& memCert)
99{
100 IdentityCertificate cert(memCert);
101 m_db.addMember(scheduleName, cert.getPublicKeyName(), cert.getPublicKeyInfo().get());
102}
103
104void
105GroupManager::removeMember(const Name& identity)
106{
107 m_db.deleteMember(identity);
108}
109
110void
111GroupManager::updateMemberSchedule(const Name& identity, const std::string& scheduleName)
112{
113 m_db.updateMemberSchedule(identity, scheduleName);
114}
115
116Interval
117GroupManager::calculateInterval(const TimeStamp& timeslot, std::map<Name, Buffer>& memberKeys)
118{
119 // prepare
120 Interval positiveResult;
121 Interval negativeResult;
122 Interval tempInterval;
123 Interval finalInterval;
124 bool isPositive;
125 memberKeys.clear();
126
127 // get the all intervals from schedules
128 for (const std::string& scheduleName : m_db.listAllScheduleNames()) {
129
130 const Schedule& schedule = m_db.getSchedule(scheduleName);
131 std::tie(isPositive, tempInterval) = schedule.getCoveringInterval(timeslot);
132
133 if (isPositive) {
134 if (!positiveResult.isValid())
135 positiveResult = tempInterval;
136 positiveResult && tempInterval;
137
138 std::map<Name, Buffer> m = m_db.getScheduleMembers(scheduleName);
139 memberKeys.insert(m.begin(), m.end());
140 }
141 else {
142 if (!negativeResult.isValid())
143 negativeResult = tempInterval;
144 negativeResult && tempInterval;
145 }
146 }
147 if (!positiveResult.isValid()) {
148 // return invalid interval when there is no member has interval covering the time slot
149 return Interval(false);
150 }
151
152 // get the final interval result
153 if (negativeResult.isValid())
154 finalInterval = positiveResult && negativeResult;
155 else
156 finalInterval = positiveResult;
157
158 return finalInterval;
159}
160
161void
162GroupManager::generateKeyPairs(Buffer& priKeyBuf, Buffer& pubKeyBuf) const
163{
164 RandomNumberGenerator rng;
165 RsaKeyParams params(m_paramLength);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800166 DecryptKey<algo::Rsa> privateKey = algo::Rsa::generateKey(rng, params);
167 priKeyBuf = privateKey.getKeyBits();
168 EncryptKey<algo::Rsa> publicKey = algo::Rsa::deriveEncryptKey(priKeyBuf);
169 pubKeyBuf = publicKey.getKeyBits();
170}
171
172
173Data
174GroupManager::createEKeyData(const std::string& startTs, const std::string& endTs,
175 const Buffer& pubKeyBuf)
176{
Zhiyi Zhang3ac0d8d2015-10-28 14:07:09 +0800177 Name name(m_namespace);
Yingdi Yu3decf4e2015-11-02 12:33:31 -0800178 name.append(NAME_COMPONENT_E_KEY).append(startTs).append(endTs);
Zhiyi Zhang3ac0d8d2015-10-28 14:07:09 +0800179 Data data(name);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800180 data.setFreshnessPeriod(time::hours(m_freshPeriod));
181 data.setContent(pubKeyBuf.get(), pubKeyBuf.size());
182 m_keyChain.sign(data);
183 return data;
184}
185
186Data
187GroupManager::createDKeyData(const std::string& startTs, const std::string& endTs,
188 const Name& keyName, const Buffer& priKeyBuf,
189 const Buffer& certKey)
190{
Zhiyi Zhang3ac0d8d2015-10-28 14:07:09 +0800191 Name name(m_namespace);
Yingdi Yu3decf4e2015-11-02 12:33:31 -0800192 name.append(NAME_COMPONENT_D_KEY);
193 name.append(startTs).append(endTs);
Zhiyi Zhang3ac0d8d2015-10-28 14:07:09 +0800194 Data data = Data(name);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800195 data.setFreshnessPeriod(time::hours(m_freshPeriod));
Yingdi Yu883f4202015-11-02 13:54:11 -0800196 algo::EncryptParams eparams(tlv::AlgorithmRsaOaep);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800197 algo::encryptData(data, priKeyBuf.buf(), priKeyBuf.size(), keyName,
198 certKey.buf(), certKey.size(), eparams);
199 m_keyChain.sign(data);
200 return data;
201}
202
203} // namespace ndn
204} // namespace ndn