blob: 9b343f571e66f14d1c09e75f19f0dafee5eab80e [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_IMPL_HPP
23#define NDN_SECURITY_PIB_PIB_IMPL_HPP
Yingdi Yu151b5572015-04-27 11:07:37 -070024
25#include <set>
Yingdi Yu6ee2d362015-07-16 21:48:05 -070026#include "../v2/certificate.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070027
28namespace ndn {
29namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070030namespace pib {
Yingdi Yu151b5572015-04-27 11:07:37 -070031
32/**
33 * @brief Abstract class of PIB implementation
34 *
35 * This class defines the interface that an actual PIB (e.g., one based on sqlite3)
36 * implementation should provide.
37 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070038class PibImpl : noncopyable
Yingdi Yu151b5572015-04-27 11:07:37 -070039{
40public:
41 /**
42 * @brief represents a non-semantic error
43 *
44 * A subclass of PibImpl may throw a subclass of this type when
45 * there's a non-semantic error, such as a storage problem.
46 */
47 class Error : public std::runtime_error
48 {
49 public:
50 explicit
51 Error(const std::string& what)
52 : std::runtime_error(what)
53 {
54 }
55 };
56
57public:
Yingdi Yu151b5572015-04-27 11:07:37 -070058 virtual
Yingdi Yu6ee2d362015-07-16 21:48:05 -070059 ~PibImpl() = default;
Yingdi Yu151b5572015-04-27 11:07:37 -070060
61public: // TpmLocator management
Yingdi Yu151b5572015-04-27 11:07:37 -070062 /**
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070063 * @brief Set the corresponding TPM information to @p tpmLocator
Yingdi Yu151b5572015-04-27 11:07:37 -070064 *
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070065 * This method does not reset contents of the PIB
Yingdi Yu151b5572015-04-27 11:07:37 -070066 */
67 virtual void
68 setTpmLocator(const std::string& tpmLocator) = 0;
69
70 /**
71 * @brief Get TPM Locator
72 */
73 virtual std::string
74 getTpmLocator() const = 0;
75
76public: // Identity management
Yingdi Yu151b5572015-04-27 11:07:37 -070077 /**
78 * @brief Check the existence of an identity.
79 *
80 * @param identity The name of the identity.
81 * @return true if the identity exists, otherwise false.
82 */
83 virtual bool
84 hasIdentity(const Name& identity) const = 0;
85
86 /**
87 * @brief Add an identity.
88 *
89 * If the identity already exists, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -070090 * If no default identity has been set, set the added one as default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -070091 *
92 * @param identity The name of the identity to add.
93 */
94 virtual void
95 addIdentity(const Name& identity) = 0;
96
97 /**
98 * @brief Remove an identity
99 *
100 * If the identity does not exist, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700101 * Remove related keys and certificates as well.
Yingdi Yu151b5572015-04-27 11:07:37 -0700102 *
103 * @param identity The name of the identity to remove.
104 */
105 virtual void
106 removeIdentity(const Name& identity) = 0;
107
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700108 /**
109 * @brief Erasing all certificates, keys, and identities
110 */
111 virtual void
112 clearIdentities() = 0;
113
114 /**
115 * @brief Get the name of all the identities
116 */
Yingdi Yu151b5572015-04-27 11:07:37 -0700117 virtual std::set<Name>
118 getIdentities() const = 0;
119
120 /**
121 * @brief Set an identity with name @p identityName as the default identity.
122 *
123 * Since adding an identity only requires the identity name, create the
124 * identity if it does not exist.
125 *
126 * @param identityName The name for the default identity.
127 */
128 virtual void
129 setDefaultIdentity(const Name& identityName) = 0;
130
131 /**
132 * @brief Get the default identity.
133 *
134 * @return The name for the default identity.
135 * @throws Pib::Error if no default identity.
136 */
137 virtual Name
138 getDefaultIdentity() const = 0;
139
140public: // Key management
Yingdi Yu151b5572015-04-27 11:07:37 -0700141 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700142 * @brief Check the existence of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700143 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700144 * @return true if the key exists, otherwise false. Return false if the identity does not exist
145 */
146 virtual bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700147 hasKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700148
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.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700154 * If no default key of the identity has been set, set the added one as default
155 * key of the identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700156 *
157 * @param identity The name of the belonged identity.
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700158 * @param keyName The key name.
159 * @param key The public key bits.
160 * @param keyLen The length of the public key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700161 */
162 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700163 addKey(const Name& identity, const Name& keyName, const uint8_t* key, size_t keyLen) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700164
165 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700166 * @brief Remove a key with @p keyName
Yingdi Yu151b5572015-04-27 11:07:37 -0700167 *
168 * If the key does not exist, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700169 * Remove related certificates as well.
Yingdi Yu151b5572015-04-27 11:07:37 -0700170 */
171 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700172 removeKey(const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700173
174 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700175 * @brief Get the key bits of a key with name @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700176 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700177 * @return key bits
178 * @throws Pib::Error if the key does not exist.
179 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700180 virtual Buffer
181 getKeyBits(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700182
183 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700184 * @brief Get all the key names of an identity with name @p identity
Yingdi Yu151b5572015-04-27 11:07:37 -0700185 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700186 * The returned key names can be used to create a KeyContainer.
187 * With key name, identity name, backend implementation, one can create a Key frontend instance.
Yingdi Yu151b5572015-04-27 11:07:37 -0700188 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700189 * @return the key name component set. If the identity does not exist, return an empty set.
Yingdi Yu151b5572015-04-27 11:07:37 -0700190 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700191 virtual std::set<Name>
Yingdi Yu151b5572015-04-27 11:07:37 -0700192 getKeysOfIdentity(const Name& identity) const = 0;
193
194 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700195 * @brief Set an key with @p keyName as the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700196 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700197 * @throws Pib::Error if the key does not exist.
198 */
199 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700200 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700201
202 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700203 * @return The name of the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700204 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700205 * @throws Pib::Error if no default key or the identity does not exist.
206 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700207 virtual Name
Yingdi Yu151b5572015-04-27 11:07:37 -0700208 getDefaultKeyOfIdentity(const Name& identity) const = 0;
209
210public: // Certificate Management
Yingdi Yu151b5572015-04-27 11:07:37 -0700211 /**
212 * @brief Check the existence of a certificate with name @p certName.
213 *
214 * @param certName The name of the certificate.
215 * @return true if the certificate exists, otherwise false.
216 */
217 virtual bool
218 hasCertificate(const Name& certName) const = 0;
219
220 /**
221 * @brief Add a certificate.
222 *
223 * If the certificate already exists, do nothing.
224 * If the key or identity do not exist, add them as well.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700225 * If no default certificate of the key has been set, set the added one as
226 * default certificate of the key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700227 *
228 * @param certificate The certificate to add.
229 */
230 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700231 addCertificate(const v2::Certificate& certificate) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700232
233 /**
234 * @brief Remove a certificate with name @p certName.
235 *
236 * If the certificate does not exist, do nothing.
237 *
238 * @param certName The name of the certificate.
239 */
240 virtual void
241 removeCertificate(const Name& certName) = 0;
242
243 /**
244 * @brief Get a certificate with name @p certName.
245 *
246 * @param certName The name of the certificate.
247 * @return the certificate.
248 * @throws Pib::Error if the certificate does not exist.
249 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700250 virtual v2::Certificate
Yingdi Yu151b5572015-04-27 11:07:37 -0700251 getCertificate(const Name& certName) const = 0;
252
253 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700254 * @brief Get a list of certificate names of a key with id @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700255 *
256 * The returned certificate names can be used to create a CertificateContainer.
257 * With certificate name and backend implementation, one can obtain the certificate directly.
258 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700259 * @return The certificate name set. If the key does not exist, return an empty set.
260 */
261 virtual std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700262 getCertificatesOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700263
264 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700265 * @brief Set a cert with name @p certName as the default of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700266 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700267 * @throws Pib::Error if the certificate with name @p certName does not exist.
268 */
269 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700270 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700271
272 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700273 * @return Get the default certificate of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700274 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700275 * @throws Pib::Error if the default certificate does not exist.
276 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700277 virtual v2::Certificate
278 getDefaultCertificateOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700279};
280
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700281} // namespace pib
Yingdi Yu151b5572015-04-27 11:07:37 -0700282} // namespace security
283} // namespace ndn
284
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700285#endif // NDN_SECURITY_PIB_PIB_IMPL_HPP