blob: 56feb28fdbac350f62b195ab964d740ce1d29598 [file] [log] [blame]
Zhiyi Zhang84986cc2015-09-21 00:26:07 +08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhang19a11d22018-04-12 22:58:20 -07003 * Copyright (c) 2014-2018, Regents of the University of California
Zhiyi Zhang84986cc2015-09-21 00:26:07 +08004 *
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 *
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070019 * @author Zhiyi Zhang <zhiyi@cs.ucla.edu>
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080020 */
21
22#ifndef NDN_GEP_GROUP_MANAGER_HPP
23#define NDN_GEP_GROUP_MANAGER_HPP
24
25#include "group-manager-db.hpp"
26#include "algo/rsa.hpp"
27
28#include <ndn-cxx/security/key-chain.hpp>
29
30namespace ndn {
31namespace gep {
32
33class GroupManager
34{
35public:
36 class Error : public std::runtime_error
37 {
38 public:
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070039 explicit Error(const std::string& what)
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080040 : std::runtime_error(what)
41 {
42 }
43 };
44
45public:
Yingdi Yu4467c112015-10-19 09:27:45 -070046 /**
47 * @brief Create group manager
48 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070049 * The namespace of group manager is /[prefix]/read/[dataType]/
Yingdi Yu4467c112015-10-19 09:27:45 -070050 * The group management information (including user cert, schedule) is stored in a database
Yingdi Yu8c43fcc2016-03-09 18:23:57 -080051 * at @p dbPath.
52 * The group key will be an RSA key with @p paramLength bits.
53 * The FreshnessPeriod of data packet carrying the keys will be set to @p freshPeriod hours.
Yingdi Yu4467c112015-10-19 09:27:45 -070054 */
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070055 GroupManager(const Name& prefix,
56 const Name& dataType,
57 const std::string& dbPath,
58 const int paramLength,
59 const int freshPeriod);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080060
61 /**
62 * @brief Create a group key for interval which
63 * @p timeslot falls into
64 *
65 * This method creates a group key if it does not
66 * exist, and encrypts the key using public key of
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -070067 * all eligible members.
68 *
69 * @p needRegenerate should be true if 1.first time to call 2.a member was removed
70 * and it can be false if 1.not the first time to call 2.a member was added
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080071 *
72 * @returns The group key (the first one is the
73 * public key, and the rest are encrypted
74 * private key.
75 */
76 std::list<Data>
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -070077 getGroupKey(const TimeStamp& timeslot, bool needRegenerate = true);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080078
79 /// @brief Add @p schedule with @p scheduleName
80 void
81 addSchedule(const std::string& scheduleName, const Schedule& schedule);
82
83 /// @brief Delete schedule with name @p scheduleName
84 void
85 deleteSchedule(const std::string& scheduleName);
86
87 /// @brief Update a schedule by name @p scheduleName with a new @p schedule
88 void
89 updateSchedule(const std::string& scheduleName, const Schedule& schedule);
90
91 /// @brief Add @p memCert with @p scheduleName
92 void
93 addMember(const std::string& scheduleName, const Data& memCert);
94
Zhiyi Zhang19a11d22018-04-12 22:58:20 -070095 void
96 addMember(const std::string& scheduleName, const Name& keyName, const Buffer& key);
97
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080098 /// @brief Remove member with name @p identity from the group.
99 void
100 removeMember(const Name& identity);
101
102 /// @brief Update @p member with a schedule of @p schedule Name.
103 void
104 updateMemberSchedule(const Name& identity, const std::string& scheduleName);
105
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -0700106
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700107PUBLIC_WITH_TESTS_ELSE_PRIVATE :
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800108 /**
109 * @brief Calculate interval that covers @p timeslot
110 * and fill @p memberKeys with the info of members who is allowed to access the interval.
111 */
112 Interval
113 calculateInterval(const TimeStamp& timeslot, std::map<Name, Buffer>& certMap);
114
115 /**
116 * @brief Generate rsa key pairs according to the member variable m_paramLength.
117 * @p priKeyBuf The generated private key buffer
118 * @p pubKeyBuf The generated public key buffer
119 */
120 void
121 generateKeyPairs(Buffer& priKeyBuf, Buffer& pubKeyBuf) const;
122
123 /// @brief Create E-KEY data.
124 Data
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700125 createEKeyData(const std::string& startTs, const std::string& endTs, const Buffer& pubKeyBuf);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800126
127 /// @brief Create D-KEY data.
128 Data
Zhiyi Zhang19a11d22018-04-12 22:58:20 -0700129 createDKeyData(const std::string& startTs,
130 const std::string& endTs,
131 const Name& keyName,
132 const Buffer& priKeyBuf,
133 const Buffer& certKey);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800134
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -0700135 /// @brief Add a EKey to the database
136 void
137 addEKey(const Name& eKeyName, const Buffer& pubKey, const Buffer& priKey);
138
139 /// @brief Get the key pair from the database
140 std::tuple<Buffer, Buffer>
141 getEKey(const Name& eKeyName);
142
143 /// @brief Delete a EKey to the database
144 void
145 deleteEKey(const Name& eKeyName);
146
147 /// @brief The method should be called periodically because the table size will keep growing
148 void
149 cleanEKeys();
150
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800151private:
152 Name m_namespace;
153 GroupManagerDB m_db;
154 int m_paramLength;
155 int m_freshPeriod;
156
157 KeyChain m_keyChain;
158};
159
160} // namespace gep
161} // namespace ndn
162
163#endif // NDN_GEP_GROUP_MANAGER_HPP