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