blob: b307c839d2419d6b8753b16e40721ae41cea566c [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
101 /*
102 * @brief Create an identity with name @p identityName and return a reference to it.
103 *
104 * If there already exists an identity for the name @p identityName, then it is returned.
105 * If no default identity is set, the newly created identity will be set as the default.
106 *
107 * @param identityName The name for the identity to be added
108 */
109 Identity
110 addIdentity(const Name& identityName);
111
112 /*
113 * @brief Remove an identity with name @p identityName.
114 *
115 * @param identityName The name for the identity to be deleted
116 */
117 void
118 removeIdentity(const Name& identityName);
119
120 /**
121 * @brief Get an identity with name @p identityName.
122 *
123 * @param identityName The name for the identity to get.
124 * @throw Pib::Error if the identity does not exist.
125 */
126 Identity
127 getIdentity(const Name& identityName);
128
129 /// @brief Get all the identities
130 IdentityContainer
131 getIdentities() const;
132
133 /**
134 * @brief Set an identity with name @p identityName as the default identity.
135 *
136 * Also create the identity if it does not exist.
137 *
138 * @param identityName The name for the default identity.
139 * @return the default identity
140 */
141 Identity
142 setDefaultIdentity(const Name& identityName);
143
144 /**
145 * @brief Get the default identity.
146 *
147 * @return the default identity.
148 * @throws Pib::Error if no default identity.
149 */
150 Identity
151 getDefaultIdentity();
152
153NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE:
154 /*
155 * @brief Create a new Pib with the specified @p location
156 *
157 * @param scheme The scheme for the Pib
158 * @param location The location for the Pib
159 * @param impl The backend implementation
160 */
161 Pib(const std::string scheme, const std::string& location, shared_ptr<PibImpl> impl);
162
163 shared_ptr<PibImpl>
164 getImpl()
165 {
166 return m_impl;
167 }
168
169protected:
170 std::string m_scheme;
171 std::string m_location;
172 shared_ptr<PibImpl> m_impl;
Yingdi Yu151b5572015-04-27 11:07:37 -0700173};
174
175} // namespace security
176} // namespace ndn
177
178#endif // NDN_SECURITY_PIB_HPP