blob: 8f68f1f91b4f0af29acc5e2bff91fcfe24be64e6 [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:
Yingdi Yub8f8b342015-04-27 11:06:42 -070054 friend class KeyChain;
55
56public:
Yingdi Yu151b5572015-04-27 11:07:37 -070057 /// @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 Yub8f8b342015-04-27 11:06:42 -070068public:
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 Pesavento18cf81b2015-09-12 23:36:43 +020093 * @param tpmLocator The name for the new TPM locator
Yingdi Yub8f8b342015-04-27 11:06:42 -070094 */
95 void
96 setTpmLocator(const std::string& tpmLocator);
97
98 /**
99 * @brief Get TPM Locator
100 */
101 std::string
102 getTpmLocator() const;
103
Yingdi Yuc8209892015-06-19 17:47:56 -0700104 /**
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
126NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
127
Yingdi Yub8f8b342015-04-27 11:06:42 -0700128 /*
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 Yub8f8b342015-04-27 11:06:42 -0700148 * @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 Yuc8209892015-06-19 17:47:56 -0700155 Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700156 setDefaultIdentity(const Name& identityName);
157
Yingdi Yub8f8b342015-04-27 11:06:42 -0700158NDN_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
174protected:
175 std::string m_scheme;
176 std::string m_location;
Yingdi Yuc8209892015-06-19 17:47:56 -0700177
178 mutable bool m_hasDefaultIdentity;
179 mutable Identity m_defaultIdentity;
180
181 mutable bool m_needRefreshIdentities;
182 mutable IdentityContainer m_identities;
183
Yingdi Yub8f8b342015-04-27 11:06:42 -0700184 shared_ptr<PibImpl> m_impl;
Yingdi Yu151b5572015-04-27 11:07:37 -0700185};
186
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700187} // namespace pib
188
189using pib::Pib;
190
Yingdi Yu151b5572015-04-27 11:07:37 -0700191} // namespace security
192} // namespace ndn
193
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700194#endif // NDN_SECURITY_PIB_PIB_HPP