blob: 5d28edd4a1094adb364e6111bee9cc02048cde42 [file] [log] [blame]
Yingdi Yu151b5572015-04-27 11:07:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
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
22#ifndef NDN_SECURITY_PIB_HPP
23#define NDN_SECURITY_PIB_HPP
24
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 Yub8f8b342015-04-27 11:06:42 -070031class PibImpl;
32
Yingdi Yu151b5572015-04-27 11:07:37 -070033/**
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 *
46 * @throw PibImpl::Error when underlying implementation has non-semantic error.
47 */
48class Pib : noncopyable
49{
50public:
Yingdi Yub8f8b342015-04-27 11:06:42 -070051 friend class KeyChain;
52
53public:
Yingdi Yu151b5572015-04-27 11:07:37 -070054 /// @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:
66
67 ~Pib();
68
69 /**
70 * @brief return the scheme of the PibLocator
71 */
72 std::string
73 getScheme() const
74 {
75 return m_scheme;
76 }
77
78 /**
79 * @brief Get PIB Locator
80 */
81 std::string
82 getPibLocator() const;
83
84 /**
85 * @brief Set the corresponding TPM information to @p tpmLocator.
86 *
87 * If the provided @p tpmLocator is different from the existing one, the
88 * PIB will be reset, otherwise nothing will be changed.
89 *
90 * @param tmpLocator The name for the new tmpLocator
91 */
92 void
93 setTpmLocator(const std::string& tpmLocator);
94
95 /**
96 * @brief Get TPM Locator
97 */
98 std::string
99 getTpmLocator() const;
100
Yingdi Yuc8209892015-06-19 17:47:56 -0700101 /**
102 * @brief Get an identity with name @p identityName.
103 *
104 * @param identityName The name for the identity to get.
105 * @throw Pib::Error if the identity does not exist.
106 */
107 Identity
108 getIdentity(const Name& identityName) const;
109
110 /// @brief Get all the identities
111 const IdentityContainer&
112 getIdentities() const;
113
114 /**
115 * @brief Get the default identity.
116 *
117 * @return the default identity.
118 * @throws Pib::Error if no default identity.
119 */
120 Identity&
121 getDefaultIdentity() const;
122
123NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private
124
Yingdi Yub8f8b342015-04-27 11:06:42 -0700125 /*
126 * @brief Create an identity with name @p identityName and return a reference to it.
127 *
128 * If there already exists an identity for the name @p identityName, then it is returned.
129 * If no default identity is set, the newly created identity will be set as the default.
130 *
131 * @param identityName The name for the identity to be added
132 */
133 Identity
134 addIdentity(const Name& identityName);
135
136 /*
137 * @brief Remove an identity with name @p identityName.
138 *
139 * @param identityName The name for the identity to be deleted
140 */
141 void
142 removeIdentity(const Name& identityName);
143
144 /**
Yingdi Yub8f8b342015-04-27 11:06:42 -0700145 * @brief Set an identity with name @p identityName as the default identity.
146 *
147 * Also create the identity if it does not exist.
148 *
149 * @param identityName The name for the default identity.
150 * @return the default identity
151 */
Yingdi Yuc8209892015-06-19 17:47:56 -0700152 Identity&
Yingdi Yub8f8b342015-04-27 11:06:42 -0700153 setDefaultIdentity(const Name& identityName);
154
Yingdi Yub8f8b342015-04-27 11:06:42 -0700155NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
156 /*
157 * @brief Create a new Pib with the specified @p location
158 *
159 * @param scheme The scheme for the Pib
160 * @param location The location for the Pib
161 * @param impl The backend implementation
162 */
163 Pib(const std::string scheme, const std::string& location, shared_ptr<PibImpl> impl);
164
165 shared_ptr<PibImpl>
166 getImpl()
167 {
168 return m_impl;
169 }
170
171protected:
172 std::string m_scheme;
173 std::string m_location;
Yingdi Yuc8209892015-06-19 17:47:56 -0700174
175 mutable bool m_hasDefaultIdentity;
176 mutable Identity m_defaultIdentity;
177
178 mutable bool m_needRefreshIdentities;
179 mutable IdentityContainer m_identities;
180
Yingdi Yub8f8b342015-04-27 11:06:42 -0700181 shared_ptr<PibImpl> m_impl;
Yingdi Yu151b5572015-04-27 11:07:37 -0700182};
183
184} // namespace security
185} // namespace ndn
186
187#endif // NDN_SECURITY_PIB_HPP