blob: 6f233f12ee914a7d239f2ba0645393c34248be10 [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 /**
63 * @brief Set the corresponding TPM information to @p tpmLocator.
64 *
65 * If the provided @p tpmLocator is different from the existing one, the
66 * content in PIB will be cleaned up, otherwise nothing will be changed.
67 *
Davide Pesavento18cf81b2015-09-12 23:36:43 +020068 * @param tpmLocator The name for the new TPM locator
Yingdi Yu151b5572015-04-27 11:07:37 -070069 */
70 virtual void
71 setTpmLocator(const std::string& tpmLocator) = 0;
72
73 /**
74 * @brief Get TPM Locator
75 */
76 virtual std::string
77 getTpmLocator() const = 0;
78
79public: // Identity management
Yingdi Yu151b5572015-04-27 11:07:37 -070080 /**
81 * @brief Check the existence of an identity.
82 *
83 * @param identity The name of the identity.
84 * @return true if the identity exists, otherwise false.
85 */
86 virtual bool
87 hasIdentity(const Name& identity) const = 0;
88
89 /**
90 * @brief Add an identity.
91 *
92 * If the identity already exists, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -070093 * If no default identity has been set, set the added one as default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -070094 *
95 * @param identity The name of the identity to add.
96 */
97 virtual void
98 addIdentity(const Name& identity) = 0;
99
100 /**
101 * @brief Remove an identity
102 *
103 * If the identity does not exist, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700104 * Remove related keys and certificates as well.
Yingdi Yu151b5572015-04-27 11:07:37 -0700105 *
106 * @param identity The name of the identity to remove.
107 */
108 virtual void
109 removeIdentity(const Name& identity) = 0;
110
111 /// @brief Get the name of all the identities
112 virtual std::set<Name>
113 getIdentities() const = 0;
114
115 /**
116 * @brief Set an identity with name @p identityName as the default identity.
117 *
118 * Since adding an identity only requires the identity name, create the
119 * identity if it does not exist.
120 *
121 * @param identityName The name for the default identity.
122 */
123 virtual void
124 setDefaultIdentity(const Name& identityName) = 0;
125
126 /**
127 * @brief Get the default identity.
128 *
129 * @return The name for the default identity.
130 * @throws Pib::Error if no default identity.
131 */
132 virtual Name
133 getDefaultIdentity() const = 0;
134
135public: // Key management
Yingdi Yu151b5572015-04-27 11:07:37 -0700136 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700137 * @brief Check the existence of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700138 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700139 * @return true if the key exists, otherwise false. Return false if the identity does not exist
140 */
141 virtual bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700142 hasKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700143
144 /**
145 * @brief Add a key.
146 *
147 * If the key already exists, do nothing.
148 * If the identity does not exist, add the identity as well.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700149 * If no default key of the identity has been set, set the added one as default
150 * key of the identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700151 *
152 * @param identity The name of the belonged identity.
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700153 * @param keyName The key name.
154 * @param key The public key bits.
155 * @param keyLen The length of the public key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700156 */
157 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700158 addKey(const Name& identity, const Name& keyName, const uint8_t* key, size_t keyLen) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700159
160 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700161 * @brief Remove a key with @p keyName
Yingdi Yu151b5572015-04-27 11:07:37 -0700162 *
163 * If the key does not exist, do nothing.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700164 * Remove related certificates as well.
Yingdi Yu151b5572015-04-27 11:07:37 -0700165 */
166 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700167 removeKey(const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700168
169 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170 * @brief Get the key bits of a key with name @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700171 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700172 * @return key bits
173 * @throws Pib::Error if the key does not exist.
174 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700175 virtual Buffer
176 getKeyBits(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700177
178 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700179 * @brief Get all the key names of an identity with name @p identity
Yingdi Yu151b5572015-04-27 11:07:37 -0700180 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700181 * The returned key names can be used to create a KeyContainer.
182 * With key name, identity name, backend implementation, one can create a Key frontend instance.
Yingdi Yu151b5572015-04-27 11:07:37 -0700183 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700184 * @return the key name component set. If the identity does not exist, return an empty set.
Yingdi Yu151b5572015-04-27 11:07:37 -0700185 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700186 virtual std::set<Name>
Yingdi Yu151b5572015-04-27 11:07:37 -0700187 getKeysOfIdentity(const Name& identity) const = 0;
188
189 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700190 * @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 -0700191 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700192 * @throws Pib::Error if the key does not exist.
193 */
194 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700195 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700196
197 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700198 * @return The name of the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700199 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700200 * @throws Pib::Error if no default key or the identity does not exist.
201 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700202 virtual Name
Yingdi Yu151b5572015-04-27 11:07:37 -0700203 getDefaultKeyOfIdentity(const Name& identity) const = 0;
204
205public: // Certificate Management
Yingdi Yu151b5572015-04-27 11:07:37 -0700206 /**
207 * @brief Check the existence of a certificate with name @p certName.
208 *
209 * @param certName The name of the certificate.
210 * @return true if the certificate exists, otherwise false.
211 */
212 virtual bool
213 hasCertificate(const Name& certName) const = 0;
214
215 /**
216 * @brief Add a certificate.
217 *
218 * If the certificate already exists, do nothing.
219 * If the key or identity do not exist, add them as well.
Yingdi Yu3bf91f52015-06-12 19:39:40 -0700220 * If no default certificate of the key has been set, set the added one as
221 * default certificate of the key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700222 *
223 * @param certificate The certificate to add.
224 */
225 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700226 addCertificate(const v2::Certificate& certificate) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700227
228 /**
229 * @brief Remove a certificate with name @p certName.
230 *
231 * If the certificate does not exist, do nothing.
232 *
233 * @param certName The name of the certificate.
234 */
235 virtual void
236 removeCertificate(const Name& certName) = 0;
237
238 /**
239 * @brief Get a certificate with name @p certName.
240 *
241 * @param certName The name of the certificate.
242 * @return the certificate.
243 * @throws Pib::Error if the certificate does not exist.
244 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700245 virtual v2::Certificate
Yingdi Yu151b5572015-04-27 11:07:37 -0700246 getCertificate(const Name& certName) const = 0;
247
248 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700249 * @brief Get a list of certificate names of a key with id @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700250 *
251 * The returned certificate names can be used to create a CertificateContainer.
252 * With certificate name and backend implementation, one can obtain the certificate directly.
253 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700254 * @return The certificate name set. If the key does not exist, return an empty set.
255 */
256 virtual std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700257 getCertificatesOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700258
259 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700260 * @brief Set a cert with name @p certName as the default of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700261 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700262 * @throws Pib::Error if the certificate with name @p certName does not exist.
263 */
264 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700265 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700266
267 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700268 * @return Get the default certificate of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700269 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700270 * @throws Pib::Error if the default certificate does not exist.
271 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700272 virtual v2::Certificate
273 getDefaultCertificateOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700274};
275
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700276} // namespace pib
Yingdi Yu151b5572015-04-27 11:07:37 -0700277} // namespace security
278} // namespace ndn
279
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700280#endif // NDN_SECURITY_PIB_PIB_IMPL_HPP