blob: 5ece79e8d1cc4c8cdbd525783b548e95f0353373 [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_IMPL_HPP
23#define NDN_SECURITY_PIB_IMPL_HPP
24
25#include <set>
26#include "identity-certificate.hpp"
27
28namespace ndn {
29namespace security {
30
31/**
32 * @brief Abstract class of PIB implementation
33 *
34 * This class defines the interface that an actual PIB (e.g., one based on sqlite3)
35 * implementation should provide.
36 */
37class PibImpl
38{
39public:
40 /**
41 * @brief represents a non-semantic error
42 *
43 * A subclass of PibImpl may throw a subclass of this type when
44 * there's a non-semantic error, such as a storage problem.
45 */
46 class Error : public std::runtime_error
47 {
48 public:
49 explicit
50 Error(const std::string& what)
51 : std::runtime_error(what)
52 {
53 }
54 };
55
56public:
57
58 virtual
59 ~PibImpl()
60 {
61 }
62
63public: // TpmLocator management
64
65 /**
66 * @brief Set the corresponding TPM information to @p tpmLocator.
67 *
68 * If the provided @p tpmLocator is different from the existing one, the
69 * content in PIB will be cleaned up, otherwise nothing will be changed.
70 *
71 * @param tmpLocator The name for the new tmpLocator
72 */
73 virtual void
74 setTpmLocator(const std::string& tpmLocator) = 0;
75
76 /**
77 * @brief Get TPM Locator
78 */
79 virtual std::string
80 getTpmLocator() const = 0;
81
82public: // Identity management
83
84 /**
85 * @brief Check the existence of an identity.
86 *
87 * @param identity The name of the identity.
88 * @return true if the identity exists, otherwise false.
89 */
90 virtual bool
91 hasIdentity(const Name& identity) const = 0;
92
93 /**
94 * @brief Add an identity.
95 *
96 * If the identity already exists, do nothing.
97 *
98 * @param identity The name of the identity to add.
99 */
100 virtual void
101 addIdentity(const Name& identity) = 0;
102
103 /**
104 * @brief Remove an identity
105 *
106 * If the identity does not exist, do nothing.
107 *
108 * @param identity The name of the identity to remove.
109 */
110 virtual void
111 removeIdentity(const Name& identity) = 0;
112
113 /// @brief Get the name of all the identities
114 virtual std::set<Name>
115 getIdentities() const = 0;
116
117 /**
118 * @brief Set an identity with name @p identityName as the default identity.
119 *
120 * Since adding an identity only requires the identity name, create the
121 * identity if it does not exist.
122 *
123 * @param identityName The name for the default identity.
124 */
125 virtual void
126 setDefaultIdentity(const Name& identityName) = 0;
127
128 /**
129 * @brief Get the default identity.
130 *
131 * @return The name for the default identity.
132 * @throws Pib::Error if no default identity.
133 */
134 virtual Name
135 getDefaultIdentity() const = 0;
136
137public: // Key management
138
139 /**
140 * @brief Check the existence of a key.
141 *
142 * @param identity The name of the belonged identity.
143 * @param keyId The key id component.
144 * @return true if the key exists, otherwise false. Return false if the identity does not exist
145 */
146 virtual bool
147 hasKey(const Name& identity, const name::Component& keyId) const = 0;
148
149 /**
150 * @brief Add a key.
151 *
152 * If the key already exists, do nothing.
153 * If the identity does not exist, add the identity as well.
154 *
155 * @param identity The name of the belonged identity.
156 * @param keyId The key id component.
157 * @param publicKey The public key bits.
158 */
159 virtual void
160 addKey(const Name& identity, const name::Component& keyId, const PublicKey& publicKey) = 0;
161
162 /**
163 * @brief Remove a key.
164 *
165 * If the key does not exist, do nothing.
166 *
167 * @param identity The name of the belonged identity.
168 * @param keyId The key id component.
169 */
170 virtual void
171 removeKey(const Name& identity, const name::Component& keyId) = 0;
172
173 /**
174 * @brief Get the key bits of a key.
175 *
176 * @param identity The name of the belonged identity.
177 * @param keyId The key id component.
178 * @return key bits
179 * @throws Pib::Error if the key does not exist.
180 */
181 virtual PublicKey
182 getKeyBits(const Name& identity, const name::Component& keyId) const = 0;
183
184 /**
185 * @brief Get all the key ids of an identity with name @p identity
186 *
187 * The returned key ids can be used to create a KeyContainer.
188 * With key id, identity name, backend implementation, one can create a Key frontend instance.
189 *
190 * @return the key id name component set. If the identity does not exist, return an empty set.
191 */
192 virtual std::set<name::Component>
193 getKeysOfIdentity(const Name& identity) const = 0;
194
195 /**
196 * @brief Set an key with id @p keyId as the default key of an identity with name @p identity.
197 *
198 * @param identity The name of the belonged identity.
199 * @param keyId The key id component.
200 * @throws Pib::Error if the key does not exist.
201 */
202 virtual void
203 setDefaultKeyOfIdentity(const Name& identity, const name::Component& keyId) = 0;
204
205 /**
206 * @brief Get the id of the default key of an identity with name @p identity.
207 *
208 * @param identity The name of the belonged identity.
209 * @throws Pib::Error if no default key or the identity does not exist.
210 */
211 virtual name::Component
212 getDefaultKeyOfIdentity(const Name& identity) const = 0;
213
214public: // Certificate Management
215
216 /**
217 * @brief Check the existence of a certificate with name @p certName.
218 *
219 * @param certName The name of the certificate.
220 * @return true if the certificate exists, otherwise false.
221 */
222 virtual bool
223 hasCertificate(const Name& certName) const = 0;
224
225 /**
226 * @brief Add a certificate.
227 *
228 * If the certificate already exists, do nothing.
229 * If the key or identity do not exist, add them as well.
230 *
231 * @param certificate The certificate to add.
232 */
233 virtual void
234 addCertificate(const IdentityCertificate& certificate) = 0;
235
236 /**
237 * @brief Remove a certificate with name @p certName.
238 *
239 * If the certificate does not exist, do nothing.
240 *
241 * @param certName The name of the certificate.
242 */
243 virtual void
244 removeCertificate(const Name& certName) = 0;
245
246 /**
247 * @brief Get a certificate with name @p certName.
248 *
249 * @param certName The name of the certificate.
250 * @return the certificate.
251 * @throws Pib::Error if the certificate does not exist.
252 */
253 virtual IdentityCertificate
254 getCertificate(const Name& certName) const = 0;
255
256 /**
257 * @brief Get a list of certificate names of a key with id @p keyId of @p identity.
258 *
259 * The returned certificate names can be used to create a CertificateContainer.
260 * With certificate name and backend implementation, one can obtain the certificate directly.
261 *
262 * @param identity The name of the belonging identity.
263 * @param keyId The key id.
264 * @return The certificate name set. If the key does not exist, return an empty set.
265 */
266 virtual std::set<Name>
267 getCertificatesOfKey(const Name& identity, const name::Component& keyId) const = 0;
268
269 /**
270 * @brief Set a cert with name @p certName as the default of a key with id @keyId of @identity.
271 *
272 * @param identity The name of the belonging identity.
273 * @param keyId The key id.
274 * @param certName The name of the certificate.
275 * @throws Pib::Error if the certificate with name @p certName does not exist.
276 */
277 virtual void
278 setDefaultCertificateOfKey(const Name& identity, const name::Component& keyId,
279 const Name& certName) = 0;
280
281 /**
282 * @brief Get the default certificate of a key with id @keyId of @identity.
283 *
284 * @param identity The name of the belonging identity.
285 * @param keyId The key id.
286 * @return a pointer to the certificate, null if no default certificate for the key.
287 * @throws Pib::Error if the default certificate does not exist.
288 */
289 virtual IdentityCertificate
290 getDefaultCertificateOfKey(const Name& identity, const name::Component& keyId) const = 0;
291
292};
293
294} // namespace security
295} // namespace ndn
296
297#endif // NDN_SECURITY_PIB_IMPL_HPP