blob: ef859a6b30cf732f7b4875fd27d8b88ec89199f2 [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 *
Yingdi Yu03997682015-11-23 16:41:38 -080089 * If the identity already exists, do nothing. If no default identity has been set, set the
90 * 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 /**
Yingdi Yu03997682015-11-23 16:41:38 -080098 * @brief Remove an identity and related keys and certificates.
Yingdi Yu151b5572015-04-27 11:07:37 -070099 *
Yingdi Yu03997682015-11-23 16:41:38 -0800100 * If the default identity is being removed, no default identity will be selected. If the
101 * identity does not exist, do nothing.
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 *
Yingdi Yu03997682015-11-23 16:41:38 -0800123 * If @p identityName identity does not exist, it will be created.
Yingdi Yu151b5572015-04-27 11:07:37 -0700124 *
125 * @param identityName The name for the default identity.
126 */
127 virtual void
128 setDefaultIdentity(const Name& identityName) = 0;
129
130 /**
131 * @brief Get the default identity.
132 *
133 * @return The name for the default identity.
134 * @throws Pib::Error if no default identity.
135 */
136 virtual Name
137 getDefaultIdentity() const = 0;
138
139public: // Key management
Yingdi Yu151b5572015-04-27 11:07:37 -0700140 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700141 * @brief Check the existence of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700142 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700143 * @return true if the key exists, otherwise false. Return false if the identity does not exist
144 */
145 virtual bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700146 hasKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700147
148 /**
149 * @brief Add a key.
150 *
Yingdi Yu03997682015-11-23 16:41:38 -0800151 * If a key with the same name already exists, overwrite the key. If the identity does not
152 * exist, it will be created. If no default key of the identity has been set, set the added
153 * one as default key of the identity. If no default identity has been set, @p identity
154 * becomes the default.
Yingdi Yu151b5572015-04-27 11:07:37 -0700155 *
156 * @param identity The name of the belonged identity.
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700157 * @param keyName The key name.
158 * @param key The public key bits.
159 * @param keyLen The length of the public key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700160 */
161 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700162 addKey(const Name& identity, const Name& keyName, const uint8_t* key, size_t keyLen) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700163
164 /**
Yingdi Yu03997682015-11-23 16:41:38 -0800165 * @brief Remove a key with @p keyName and related certificates
Yingdi Yu151b5572015-04-27 11:07:37 -0700166 *
167 * If the key does not exist, do nothing.
Yingdi Yu151b5572015-04-27 11:07:37 -0700168 */
169 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700170 removeKey(const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700171
172 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700173 * @brief Get the key bits of a key with name @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700174 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700175 * @return key bits
176 * @throws Pib::Error if the key does not exist.
177 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700178 virtual Buffer
179 getKeyBits(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700180
181 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700182 * @brief Get all the key names of an identity with name @p identity
Yingdi Yu151b5572015-04-27 11:07:37 -0700183 *
Yingdi Yu03997682015-11-23 16:41:38 -0800184 * The returned key names can be used to create a KeyContainer. With key name and backend
185 * implementation, one can create a Key frontend instance.
Yingdi Yu151b5572015-04-27 11:07:37 -0700186 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700187 * @return the key name component set. If the identity does not exist, return an empty set.
Yingdi Yu151b5572015-04-27 11:07:37 -0700188 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700189 virtual std::set<Name>
Yingdi Yu151b5572015-04-27 11:07:37 -0700190 getKeysOfIdentity(const Name& identity) const = 0;
191
192 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700193 * @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 -0700194 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700195 * @throws Pib::Error if the key does not exist.
196 */
197 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700198 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700199
200 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700201 * @return The name of the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700202 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700203 * @throws Pib::Error if no default key or the identity does not exist.
204 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700205 virtual Name
Yingdi Yu151b5572015-04-27 11:07:37 -0700206 getDefaultKeyOfIdentity(const Name& identity) const = 0;
207
208public: // Certificate Management
Yingdi Yu151b5572015-04-27 11:07:37 -0700209 /**
210 * @brief Check the existence of a certificate with name @p certName.
211 *
212 * @param certName The name of the certificate.
213 * @return true if the certificate exists, otherwise false.
214 */
215 virtual bool
216 hasCertificate(const Name& certName) const = 0;
217
218 /**
219 * @brief Add a certificate.
220 *
Yingdi Yu03997682015-11-23 16:41:38 -0800221 * If a certificate with the same name (without implicit digest) already exists, overwrite
222 * the certificate. If the key or identity does not exist, they will be created. If no
223 * default certificate of the key has been set, set the added one as default certificate of
224 * the key. If no default key was set for the identity, it will be set as default key for
225 * the identity. If no default identity was selected, the certificate's identity becomes
226 * default.
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 *
Yingdi Yu03997682015-11-23 16:41:38 -0800256 * The returned certificate names can be used to create a CertificateContainer. With
257 * certificate name and backend implementation, one can obtain the certificate.
Yingdi Yu151b5572015-04-27 11:07:37 -0700258 *
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