Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2018, Regents of the University of California |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 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 | * |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 19 | * @author Zhiyi Zhang <zhiyi@cs.ucla.edu> |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 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 | |
| 30 | namespace ndn { |
| 31 | namespace gep { |
| 32 | |
| 33 | class GroupManager |
| 34 | { |
| 35 | public: |
| 36 | class Error : public std::runtime_error |
| 37 | { |
| 38 | public: |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 39 | explicit Error(const std::string& what) |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 40 | : std::runtime_error(what) |
| 41 | { |
| 42 | } |
| 43 | }; |
| 44 | |
| 45 | public: |
Yingdi Yu | 4467c11 | 2015-10-19 09:27:45 -0700 | [diff] [blame] | 46 | /** |
| 47 | * @brief Create group manager |
| 48 | * |
Alexander Afanasyev | 9d7f8fe | 2016-08-05 11:28:06 -0700 | [diff] [blame] | 49 | * The namespace of group manager is /[prefix]/read/[dataType]/ |
Yingdi Yu | 4467c11 | 2015-10-19 09:27:45 -0700 | [diff] [blame] | 50 | * The group management information (including user cert, schedule) is stored in a database |
Yingdi Yu | 8c43fcc | 2016-03-09 18:23:57 -0800 | [diff] [blame] | 51 | * 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 Yu | 4467c11 | 2015-10-19 09:27:45 -0700 | [diff] [blame] | 54 | */ |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 55 | GroupManager(const Name& prefix, |
| 56 | const Name& dataType, |
| 57 | const std::string& dbPath, |
| 58 | const int paramLength, |
| 59 | const int freshPeriod); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 60 | |
| 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 Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 67 | * 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 Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 71 | * |
| 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 Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 77 | getGroupKey(const TimeStamp& timeslot, bool needRegenerate = true); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 78 | |
| 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 Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 95 | void |
| 96 | addMember(const std::string& scheduleName, const Name& keyName, const Buffer& key); |
| 97 | |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 98 | /// @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 Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 106 | |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 107 | PUBLIC_WITH_TESTS_ELSE_PRIVATE : |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 108 | /** |
| 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 Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 125 | createEKeyData(const std::string& startTs, const std::string& endTs, const Buffer& pubKeyBuf); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 126 | |
| 127 | /// @brief Create D-KEY data. |
| 128 | Data |
Zhiyi Zhang | 19a11d2 | 2018-04-12 22:58:20 -0700 | [diff] [blame^] | 129 | createDKeyData(const std::string& startTs, |
| 130 | const std::string& endTs, |
| 131 | const Name& keyName, |
| 132 | const Buffer& priKeyBuf, |
| 133 | const Buffer& certKey); |
Zhiyi Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 134 | |
Zhiyi Zhang | 67f90aa | 2016-10-16 14:29:15 -0700 | [diff] [blame] | 135 | /// @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 Zhang | 84986cc | 2015-09-21 00:26:07 +0800 | [diff] [blame] | 151 | private: |
| 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 |