Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #ifndef NDN_SECURITY_PIB_PIB_HPP |
| 23 | #define NDN_SECURITY_PIB_PIB_HPP |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | #include "identity-container.hpp" |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 30 | class KeyChain; |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 31 | |
| 32 | namespace pib { |
| 33 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 34 | class PibImpl; |
| 35 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 36 | /** |
| 37 | * @brief represents the PIB |
| 38 | * |
| 39 | * The PIB (Public Information Base) stores the public portion of a user's cryptography keys. |
| 40 | * The format and location of stored information is indicated by the PibLocator. |
| 41 | * The PIB is designed to work with a TPM (Trusted Platform Module) which stores private keys. |
| 42 | * There is a one-to-one association between PIB and TPM, and therefore the TpmLocator is recorded |
| 43 | * by the PIB to enforce this association and prevent one from operating on mismatched PIB and TPM. |
| 44 | * |
| 45 | * Information in the PIB is organized in a hierarchy of Identity-Key-Certificate. At the top level, |
| 46 | * the Pib class provides access to identities, and allows setting a default identity. Properties of |
| 47 | * an identity can be accessed after obtaining an Identity object. |
| 48 | * |
| 49 | * @throw PibImpl::Error when underlying implementation has non-semantic error. |
| 50 | */ |
| 51 | class Pib : noncopyable |
| 52 | { |
| 53 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 54 | friend class KeyChain; |
| 55 | |
| 56 | public: |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 57 | /// @brief represents a semantic error |
| 58 | class Error : public std::runtime_error |
| 59 | { |
| 60 | public: |
| 61 | explicit |
| 62 | Error(const std::string& what) |
| 63 | : std::runtime_error(what) |
| 64 | { |
| 65 | } |
| 66 | }; |
| 67 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 69 | ~Pib(); |
| 70 | |
| 71 | /** |
| 72 | * @brief return the scheme of the PibLocator |
| 73 | */ |
| 74 | std::string |
| 75 | getScheme() const |
| 76 | { |
| 77 | return m_scheme; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @brief Get PIB Locator |
| 82 | */ |
| 83 | std::string |
| 84 | getPibLocator() const; |
| 85 | |
| 86 | /** |
| 87 | * @brief Set the corresponding TPM information to @p tpmLocator. |
| 88 | * |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame^] | 89 | * If the provided @p tpmLocator is different from the existing one, PIB will be reset. |
| 90 | * Otherwise, nothing will be changed. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 91 | */ |
| 92 | void |
| 93 | setTpmLocator(const std::string& tpmLocator); |
| 94 | |
| 95 | /** |
| 96 | * @brief Get TPM Locator |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame^] | 97 | * @throws Error if TPM locator is empty |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 98 | */ |
| 99 | std::string |
| 100 | getTpmLocator() const; |
| 101 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 102 | /** |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame^] | 103 | * @brief Reset content in PIB, including reset of the TPM locator |
| 104 | */ |
| 105 | void |
| 106 | reset(); |
| 107 | |
| 108 | /** |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 109 | * @brief Get an identity with name @p identityName. |
| 110 | * |
| 111 | * @param identityName The name for the identity to get. |
| 112 | * @throw Pib::Error if the identity does not exist. |
| 113 | */ |
| 114 | Identity |
| 115 | getIdentity(const Name& identityName) const; |
| 116 | |
| 117 | /// @brief Get all the identities |
| 118 | const IdentityContainer& |
| 119 | getIdentities() const; |
| 120 | |
| 121 | /** |
| 122 | * @brief Get the default identity. |
| 123 | * |
| 124 | * @return the default identity. |
| 125 | * @throws Pib::Error if no default identity. |
| 126 | */ |
| 127 | Identity& |
| 128 | getDefaultIdentity() const; |
| 129 | |
| 130 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
| 131 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | /* |
| 133 | * @brief Create an identity with name @p identityName and return a reference to it. |
| 134 | * |
| 135 | * If there already exists an identity for the name @p identityName, then it is returned. |
| 136 | * If no default identity is set, the newly created identity will be set as the default. |
| 137 | * |
| 138 | * @param identityName The name for the identity to be added |
| 139 | */ |
| 140 | Identity |
| 141 | addIdentity(const Name& identityName); |
| 142 | |
| 143 | /* |
| 144 | * @brief Remove an identity with name @p identityName. |
| 145 | * |
| 146 | * @param identityName The name for the identity to be deleted |
| 147 | */ |
| 148 | void |
| 149 | removeIdentity(const Name& identityName); |
| 150 | |
| 151 | /** |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 152 | * @brief Set an identity with name @p identityName as the default identity. |
| 153 | * |
| 154 | * Also create the identity if it does not exist. |
| 155 | * |
| 156 | * @param identityName The name for the default identity. |
| 157 | * @return the default identity |
| 158 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 159 | Identity& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 160 | setDefaultIdentity(const Name& identityName); |
| 161 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 162 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 163 | /* |
| 164 | * @brief Create a new Pib with the specified @p location |
| 165 | * |
| 166 | * @param scheme The scheme for the Pib |
| 167 | * @param location The location for the Pib |
| 168 | * @param impl The backend implementation |
| 169 | */ |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame^] | 170 | Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 171 | |
| 172 | shared_ptr<PibImpl> |
| 173 | getImpl() |
| 174 | { |
| 175 | return m_impl; |
| 176 | } |
| 177 | |
| 178 | protected: |
| 179 | std::string m_scheme; |
| 180 | std::string m_location; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 181 | |
| 182 | mutable bool m_hasDefaultIdentity; |
| 183 | mutable Identity m_defaultIdentity; |
| 184 | |
| 185 | mutable bool m_needRefreshIdentities; |
| 186 | mutable IdentityContainer m_identities; |
| 187 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 188 | shared_ptr<PibImpl> m_impl; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 189 | }; |
| 190 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 191 | } // namespace pib |
| 192 | |
| 193 | using pib::Pib; |
| 194 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 195 | } // namespace security |
| 196 | } // namespace ndn |
| 197 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 198 | #endif // NDN_SECURITY_PIB_PIB_HPP |