Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 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 { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 29 | namespace pib { |
| 30 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 31 | class PibImpl; |
| 32 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 33 | /** |
| 34 | * @brief represents the PIB |
| 35 | * |
| 36 | * The PIB (Public Information Base) stores the public portion of a user's cryptography keys. |
| 37 | * The format and location of stored information is indicated by the PibLocator. |
| 38 | * The PIB is designed to work with a TPM (Trusted Platform Module) which stores private keys. |
| 39 | * There is a one-to-one association between PIB and TPM, and therefore the TpmLocator is recorded |
| 40 | * by the PIB to enforce this association and prevent one from operating on mismatched PIB and TPM. |
| 41 | * |
| 42 | * Information in the PIB is organized in a hierarchy of Identity-Key-Certificate. At the top level, |
| 43 | * the Pib class provides access to identities, and allows setting a default identity. Properties of |
| 44 | * an identity can be accessed after obtaining an Identity object. |
| 45 | * |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 46 | * @note Pib instance is created and managed only by v2::KeyChain. v2::KeyChain::getPib() |
| 47 | * returns a const reference to the managed Pib instance, through which it is possible to |
| 48 | * retrieve information about identities, keys, and certificates. |
| 49 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 50 | * @throw PibImpl::Error when underlying implementation has non-semantic error. |
| 51 | */ |
| 52 | class Pib : noncopyable |
| 53 | { |
| 54 | public: |
| 55 | /// @brief represents a semantic error |
| 56 | class Error : public std::runtime_error |
| 57 | { |
| 58 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 59 | using std::runtime_error::runtime_error; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 62 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 63 | ~Pib(); |
| 64 | |
| 65 | /** |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 66 | * @brief return the scheme of the PIB Locator |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 67 | */ |
| 68 | std::string |
| 69 | getScheme() const |
| 70 | { |
| 71 | return m_scheme; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @brief Get PIB Locator |
| 76 | */ |
| 77 | std::string |
| 78 | getPibLocator() const; |
| 79 | |
| 80 | /** |
| 81 | * @brief Set the corresponding TPM information to @p tpmLocator. |
| 82 | * |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 83 | * If the provided @p tpmLocator is different from the existing one, PIB will be reset. |
| 84 | * Otherwise, nothing will be changed. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 85 | */ |
| 86 | void |
| 87 | setTpmLocator(const std::string& tpmLocator); |
| 88 | |
| 89 | /** |
| 90 | * @brief Get TPM Locator |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 91 | * @throws Error if TPM locator is empty |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 92 | */ |
| 93 | std::string |
| 94 | getTpmLocator() const; |
| 95 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 96 | /** |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 97 | * @brief Reset content in PIB, including reset of the TPM locator |
| 98 | */ |
| 99 | void |
| 100 | reset(); |
| 101 | |
| 102 | /** |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 103 | * @brief Get an identity with name @p identityName. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 104 | * @throw Pib::Error if the identity does not exist. |
| 105 | */ |
| 106 | Identity |
| 107 | getIdentity(const Name& identityName) const; |
| 108 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 109 | /** |
| 110 | * @brief Get all the identities |
| 111 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 112 | const IdentityContainer& |
| 113 | getIdentities() const; |
| 114 | |
| 115 | /** |
| 116 | * @brief Get the default identity. |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 117 | * @throw Pib::Error if no default identity exists. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 118 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 119 | const Identity& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 120 | getDefaultIdentity() const; |
| 121 | |
| 122 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 123 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 124 | * @brief Create a Pib instance |
| 125 | * |
| 126 | * @param scheme The scheme for the Pib |
| 127 | * @param location The location for the Pib |
| 128 | * @param impl The backend implementation |
| 129 | */ |
| 130 | Pib(const std::string& scheme, const std::string& location, shared_ptr<PibImpl> impl); |
| 131 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | /** |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 133 | * @brief Add an identity. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 134 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 135 | * If no default identity is set before, the new identity will be set as the default identity |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 136 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 137 | * @return handle of the added identity. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 138 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 139 | Identity |
| 140 | addIdentity(const Name& identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 141 | |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 142 | /** |
| 143 | * @brief Remove an identity. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 144 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 145 | * If the default identity is being removed, no default identity will be selected. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 146 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 147 | void |
| 148 | removeIdentity(const Name& identity); |
| 149 | |
| 150 | /** |
Davide Pesavento | 3b101d0 | 2018-07-21 22:44:09 -0400 | [diff] [blame] | 151 | * @brief Set an identity as the default identity. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 152 | * |
| 153 | * Create the identity if it does not exist. |
| 154 | * |
| 155 | * @return handle of the default identity |
| 156 | */ |
| 157 | const Identity& |
| 158 | setDefaultIdentity(const Name& identity); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 159 | |
| 160 | shared_ptr<PibImpl> |
| 161 | getImpl() |
| 162 | { |
| 163 | return m_impl; |
| 164 | } |
| 165 | |
| 166 | protected: |
| 167 | std::string m_scheme; |
| 168 | std::string m_location; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 169 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 170 | mutable bool m_isDefaultIdentityLoaded; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 171 | mutable Identity m_defaultIdentity; |
| 172 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 173 | IdentityContainer m_identities; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 174 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 175 | shared_ptr<PibImpl> m_impl; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 176 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 177 | friend class v2::KeyChain; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 180 | } // namespace pib |
| 181 | |
| 182 | using pib::Pib; |
| 183 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 184 | } // namespace security |
| 185 | } // namespace ndn |
| 186 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 187 | #endif // NDN_SECURITY_PIB_PIB_HPP |