blob: 47dbd5c8dda4e8bf2d13cf3f226a73c24950b629 [file] [log] [blame]
Yingdi Yu151b5572015-04-27 11:07:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu6ee2d362015-07-16 21:48:05 -07003 * Copyright (c) 2013-2017 Regents of the University of California.
Yingdi Yu151b5572015-04-27 11:07:37 -07004 *
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 Afanasyev97709c02016-08-25 19:58:30 -070022#ifndef NDN_SECURITY_PIB_PIB_HPP
23#define NDN_SECURITY_PIB_PIB_HPP
Yingdi Yu151b5572015-04-27 11:07:37 -070024
Yingdi Yub8f8b342015-04-27 11:06:42 -070025#include "identity-container.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070026
27namespace ndn {
28namespace security {
29
Yingdi Yu1b0311c2015-06-10 14:58:47 -070030class KeyChain;
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031
32namespace pib {
33
Yingdi Yub8f8b342015-04-27 11:06:42 -070034class PibImpl;
35
Yingdi Yu151b5572015-04-27 11:07:37 -070036/**
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 */
51class Pib : noncopyable
52{
53public:
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 Yub8f8b342015-04-27 11:06:42 -070065public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070066 ~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 Yu7b3b5e92015-08-13 19:52:35 -070086 * If the provided @p tpmLocator is different from the existing one, PIB will be reset.
87 * Otherwise, nothing will be changed.
Yingdi Yub8f8b342015-04-27 11:06:42 -070088 */
89 void
90 setTpmLocator(const std::string& tpmLocator);
91
92 /**
93 * @brief Get TPM Locator
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070094 * @throws Error if TPM locator is empty
Yingdi Yub8f8b342015-04-27 11:06:42 -070095 */
96 std::string
97 getTpmLocator() const;
98
Yingdi Yuc8209892015-06-19 17:47:56 -070099 /**
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700100 * @brief Reset content in PIB, including reset of the TPM locator
101 */
102 void
103 reset();
104
105 /**
Yingdi Yuc8209892015-06-19 17:47:56 -0700106 * @brief Get an identity with name @p identityName.
Yingdi Yuc8209892015-06-19 17:47:56 -0700107 * @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 Yucbe72b02015-11-25 17:35:37 -0800118 * @throw Pib::Error if no default identity.
Yingdi Yuc8209892015-06-19 17:47:56 -0700119 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800120 const Identity&
Yingdi Yuc8209892015-06-19 17:47:56 -0700121 getDefaultIdentity() const;
122
123NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
Yingdi Yub8f8b342015-04-27 11:06:42 -0700124 /*
Yingdi Yucbe72b02015-11-25 17:35:37 -0800125 * @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 Yub8f8b342015-04-27 11:06:42 -0700134 * @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 Yub8f8b342015-04-27 11:06:42 -0700141
142 /**
Yingdi Yucbe72b02015-11-25 17:35:37 -0800143 * @brief Add an @p identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700144 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800145 * If no default identity is set before, the new identity will be set as the default identity
Yingdi Yub8f8b342015-04-27 11:06:42 -0700146 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800147 * @return handle of the added identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700148 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800149 Identity
150 addIdentity(const Name& identity);
Yingdi Yub8f8b342015-04-27 11:06:42 -0700151
Yingdi Yub8f8b342015-04-27 11:06:42 -0700152 /*
Yingdi Yucbe72b02015-11-25 17:35:37 -0800153 * @brief Remove an @p identity.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700154 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800155 * If the default identity is being removed, no default identity will be selected.
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156 */
Yingdi Yucbe72b02015-11-25 17:35:37 -0800157 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 Yub8f8b342015-04-27 11:06:42 -0700169
170 shared_ptr<PibImpl>
171 getImpl()
172 {
173 return m_impl;
174 }
175
176protected:
177 std::string m_scheme;
178 std::string m_location;
Yingdi Yuc8209892015-06-19 17:47:56 -0700179
Yingdi Yucbe72b02015-11-25 17:35:37 -0800180 mutable bool m_isDefaultIdentityLoaded;
Yingdi Yuc8209892015-06-19 17:47:56 -0700181 mutable Identity m_defaultIdentity;
182
Yingdi Yucbe72b02015-11-25 17:35:37 -0800183 IdentityContainer m_identities;
Yingdi Yuc8209892015-06-19 17:47:56 -0700184
Yingdi Yub8f8b342015-04-27 11:06:42 -0700185 shared_ptr<PibImpl> m_impl;
Yingdi Yucbe72b02015-11-25 17:35:37 -0800186
187 friend class KeyChain;
Yingdi Yu151b5572015-04-27 11:07:37 -0700188};
189
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700190} // namespace pib
191
192using pib::Pib;
193
Yingdi Yu151b5572015-04-27 11:07:37 -0700194} // namespace security
195} // namespace ndn
196
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700197#endif // NDN_SECURITY_PIB_PIB_HPP