blob: 2e19ab108f13bdb1b91074b0827d176b9d030cd8 [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#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:
39 explicit
40 Error(const std::string& what)
41 : std::runtime_error(what)
42 {
43 }
44 };
45
46public:
Yingdi Yu4467c112015-10-19 09:27:45 -070047 /**
48 * @brief Create group manager
49 *
Alexander Afanasyev9d7f8fe2016-08-05 11:28:06 -070050 * The namespace of group manager is /[prefix]/read/[dataType]/
Yingdi Yu4467c112015-10-19 09:27:45 -070051 * The group management information (including user cert, schedule) is stored in a database
Yingdi Yu8c43fcc2016-03-09 18:23:57 -080052 * at @p dbPath.
53 * The group key will be an RSA key with @p paramLength bits.
54 * The FreshnessPeriod of data packet carrying the keys will be set to @p freshPeriod hours.
Yingdi Yu4467c112015-10-19 09:27:45 -070055 */
Yingdi Yu8c43fcc2016-03-09 18:23:57 -080056 GroupManager(const Name& prefix, const Name& dataType, const std::string& dbPath,
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080057 const int paramLength, const int freshPeriod);
58
59 /**
60 * @brief Create a group key for interval which
61 * @p timeslot falls into
62 *
63 * This method creates a group key if it does not
64 * exist, and encrypts the key using public key of
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -070065 * all eligible members.
66 *
67 * @p needRegenerate should be true if 1.first time to call 2.a member was removed
68 * 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 +080069 *
70 * @returns The group key (the first one is the
71 * public key, and the rest are encrypted
72 * private key.
73 */
74 std::list<Data>
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -070075 getGroupKey(const TimeStamp& timeslot, bool needRegenerate = true);
Zhiyi Zhang84986cc2015-09-21 00:26:07 +080076
77 /// @brief Add @p schedule with @p scheduleName
78 void
79 addSchedule(const std::string& scheduleName, const Schedule& schedule);
80
81 /// @brief Delete schedule with name @p scheduleName
82 void
83 deleteSchedule(const std::string& scheduleName);
84
85 /// @brief Update a schedule by name @p scheduleName with a new @p schedule
86 void
87 updateSchedule(const std::string& scheduleName, const Schedule& schedule);
88
89 /// @brief Add @p memCert with @p scheduleName
90 void
91 addMember(const std::string& scheduleName, const Data& memCert);
92
93 /// @brief Remove member with name @p identity from the group.
94 void
95 removeMember(const Name& identity);
96
97 /// @brief Update @p member with a schedule of @p schedule Name.
98 void
99 updateMemberSchedule(const Name& identity, const std::string& scheduleName);
100
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -0700101
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800102PUBLIC_WITH_TESTS_ELSE_PRIVATE:
103 /**
104 * @brief Calculate interval that covers @p timeslot
105 * and fill @p memberKeys with the info of members who is allowed to access the interval.
106 */
107 Interval
108 calculateInterval(const TimeStamp& timeslot, std::map<Name, Buffer>& certMap);
109
110 /**
111 * @brief Generate rsa key pairs according to the member variable m_paramLength.
112 * @p priKeyBuf The generated private key buffer
113 * @p pubKeyBuf The generated public key buffer
114 */
115 void
116 generateKeyPairs(Buffer& priKeyBuf, Buffer& pubKeyBuf) const;
117
118 /// @brief Create E-KEY data.
119 Data
120 createEKeyData(const std::string& startTs, const std::string& endTs,
121 const Buffer& pubKeyBuf);
122
123 /// @brief Create D-KEY data.
124 Data
125 createDKeyData(const std::string& startTs, const std::string& endTs, const Name& keyName,
126 const Buffer& priKeyBuf, const Buffer& certKey);
127
Zhiyi Zhang67f90aa2016-10-16 14:29:15 -0700128 /// @brief Add a EKey to the database
129 void
130 addEKey(const Name& eKeyName, const Buffer& pubKey, const Buffer& priKey);
131
132 /// @brief Get the key pair from the database
133 std::tuple<Buffer, Buffer>
134 getEKey(const Name& eKeyName);
135
136 /// @brief Delete a EKey to the database
137 void
138 deleteEKey(const Name& eKeyName);
139
140 /// @brief The method should be called periodically because the table size will keep growing
141 void
142 cleanEKeys();
143
Zhiyi Zhang84986cc2015-09-21 00:26:07 +0800144private:
145 Name m_namespace;
146 GroupManagerDB m_db;
147 int m_paramLength;
148 int m_freshPeriod;
149
150 KeyChain m_keyChain;
151};
152
153} // namespace gep
154} // namespace ndn
155
156#endif // NDN_GEP_GROUP_MANAGER_HPP