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: |
| 69 | |
| 70 | ~Pib(); |
| 71 | |
| 72 | /** |
| 73 | * @brief return the scheme of the PibLocator |
| 74 | */ |
| 75 | std::string |
| 76 | getScheme() const |
| 77 | { |
| 78 | return m_scheme; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @brief Get PIB Locator |
| 83 | */ |
| 84 | std::string |
| 85 | getPibLocator() const; |
| 86 | |
| 87 | /** |
| 88 | * @brief Set the corresponding TPM information to @p tpmLocator. |
| 89 | * |
| 90 | * If the provided @p tpmLocator is different from the existing one, the |
| 91 | * PIB will be reset, otherwise nothing will be changed. |
| 92 | * |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 93 | * @param tpmLocator The name for the new TPM locator |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 94 | */ |
| 95 | void |
| 96 | setTpmLocator(const std::string& tpmLocator); |
| 97 | |
| 98 | /** |
| 99 | * @brief Get TPM Locator |
| 100 | */ |
| 101 | std::string |
| 102 | getTpmLocator() const; |
| 103 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @brief Get an identity with name @p identityName. |
| 106 | * |
| 107 | * @param identityName The name for the identity to get. |
| 108 | * @throw Pib::Error if the identity does not exist. |
| 109 | */ |
| 110 | Identity |
| 111 | getIdentity(const Name& identityName) const; |
| 112 | |
| 113 | /// @brief Get all the identities |
| 114 | const IdentityContainer& |
| 115 | getIdentities() const; |
| 116 | |
| 117 | /** |
| 118 | * @brief Get the default identity. |
| 119 | * |
| 120 | * @return the default identity. |
| 121 | * @throws Pib::Error if no default identity. |
| 122 | */ |
| 123 | Identity& |
| 124 | getDefaultIdentity() const; |
| 125 | |
| 126 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
| 127 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 128 | /* |
| 129 | * @brief Create an identity with name @p identityName and return a reference to it. |
| 130 | * |
| 131 | * If there already exists an identity for the name @p identityName, then it is returned. |
| 132 | * If no default identity is set, the newly created identity will be set as the default. |
| 133 | * |
| 134 | * @param identityName The name for the identity to be added |
| 135 | */ |
| 136 | Identity |
| 137 | addIdentity(const Name& identityName); |
| 138 | |
| 139 | /* |
| 140 | * @brief Remove an identity with name @p identityName. |
| 141 | * |
| 142 | * @param identityName The name for the identity to be deleted |
| 143 | */ |
| 144 | void |
| 145 | removeIdentity(const Name& identityName); |
| 146 | |
| 147 | /** |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 148 | * @brief Set an identity with name @p identityName as the default identity. |
| 149 | * |
| 150 | * Also create the identity if it does not exist. |
| 151 | * |
| 152 | * @param identityName The name for the default identity. |
| 153 | * @return the default identity |
| 154 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 155 | Identity& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 156 | setDefaultIdentity(const Name& identityName); |
| 157 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 158 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 159 | /* |
| 160 | * @brief Create a new Pib with the specified @p location |
| 161 | * |
| 162 | * @param scheme The scheme for the Pib |
| 163 | * @param location The location for the Pib |
| 164 | * @param impl The backend implementation |
| 165 | */ |
| 166 | Pib(const std::string scheme, const std::string& location, shared_ptr<PibImpl> impl); |
| 167 | |
| 168 | shared_ptr<PibImpl> |
| 169 | getImpl() |
| 170 | { |
| 171 | return m_impl; |
| 172 | } |
| 173 | |
| 174 | protected: |
| 175 | std::string m_scheme; |
| 176 | std::string m_location; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 177 | |
| 178 | mutable bool m_hasDefaultIdentity; |
| 179 | mutable Identity m_defaultIdentity; |
| 180 | |
| 181 | mutable bool m_needRefreshIdentities; |
| 182 | mutable IdentityContainer m_identities; |
| 183 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 184 | shared_ptr<PibImpl> m_impl; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 187 | } // namespace pib |
| 188 | |
| 189 | using pib::Pib; |
| 190 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 191 | } // namespace security |
| 192 | } // namespace ndn |
| 193 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 194 | #endif // NDN_SECURITY_PIB_PIB_HPP |