blob: dc384a8e395c522b2385ca9133c7ee65a7a04bc0 [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
Yingdi Yucbe72b02015-11-25 17:35:37 -080025#include "pib.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070026#include <set>
Yingdi Yu6ee2d362015-07-16 21:48:05 -070027#include "../v2/certificate.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070028
29namespace ndn {
30namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070031namespace pib {
Yingdi Yu151b5572015-04-27 11:07:37 -070032
33/**
34 * @brief Abstract class of PIB implementation
35 *
36 * This class defines the interface that an actual PIB (e.g., one based on sqlite3)
37 * implementation should provide.
38 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070039class PibImpl : noncopyable
Yingdi Yu151b5572015-04-27 11:07:37 -070040{
41public:
42 /**
43 * @brief represents a non-semantic error
44 *
45 * A subclass of PibImpl may throw a subclass of this type when
46 * there's a non-semantic error, such as a storage problem.
47 */
48 class Error : public std::runtime_error
49 {
50 public:
51 explicit
52 Error(const std::string& what)
53 : std::runtime_error(what)
54 {
55 }
56 };
57
58public:
Yingdi Yu151b5572015-04-27 11:07:37 -070059 virtual
Yingdi Yu6ee2d362015-07-16 21:48:05 -070060 ~PibImpl() = default;
Yingdi Yu151b5572015-04-27 11:07:37 -070061
62public: // TpmLocator management
Yingdi Yu151b5572015-04-27 11:07:37 -070063 /**
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070064 * @brief Set the corresponding TPM information to @p tpmLocator
Yingdi Yu151b5572015-04-27 11:07:37 -070065 *
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070066 * This method does not reset contents of the PIB
Yingdi Yu151b5572015-04-27 11:07:37 -070067 */
68 virtual void
69 setTpmLocator(const std::string& tpmLocator) = 0;
70
71 /**
72 * @brief Get TPM Locator
73 */
74 virtual std::string
75 getTpmLocator() const = 0;
76
77public: // Identity management
Yingdi Yu151b5572015-04-27 11:07:37 -070078 /**
79 * @brief Check the existence of an identity.
80 *
81 * @param identity The name of the identity.
82 * @return true if the identity exists, otherwise false.
83 */
84 virtual bool
85 hasIdentity(const Name& identity) const = 0;
86
87 /**
88 * @brief Add an identity.
89 *
Yingdi Yu03997682015-11-23 16:41:38 -080090 * If the identity already exists, do nothing. If no default identity has been set, set the
91 * added one as default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -070092 *
93 * @param identity The name of the identity to add.
94 */
95 virtual void
96 addIdentity(const Name& identity) = 0;
97
98 /**
Yingdi Yu03997682015-11-23 16:41:38 -080099 * @brief Remove an identity and related keys and certificates.
Yingdi Yu151b5572015-04-27 11:07:37 -0700100 *
Yingdi Yu03997682015-11-23 16:41:38 -0800101 * If the default identity is being removed, no default identity will be selected. If the
102 * identity does not exist, do nothing.
Yingdi Yu151b5572015-04-27 11:07:37 -0700103 *
104 * @param identity The name of the identity to remove.
105 */
106 virtual void
107 removeIdentity(const Name& identity) = 0;
108
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700109 /**
110 * @brief Erasing all certificates, keys, and identities
111 */
112 virtual void
113 clearIdentities() = 0;
114
115 /**
116 * @brief Get the name of all the identities
117 */
Yingdi Yu151b5572015-04-27 11:07:37 -0700118 virtual std::set<Name>
119 getIdentities() const = 0;
120
121 /**
122 * @brief Set an identity with name @p identityName as the default identity.
123 *
Yingdi Yu03997682015-11-23 16:41:38 -0800124 * If @p identityName identity does not exist, it will be created.
Yingdi Yu151b5572015-04-27 11:07:37 -0700125 *
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.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800135 * @throw Pib::Error no default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700136 */
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 *
Yingdi Yu03997682015-11-23 16:41:38 -0800152 * If a key with the same name already exists, overwrite the key. If the identity does not
153 * exist, it will be created. If no default key of the identity has been set, set the added
154 * one as default key of the identity. If no default identity has been set, @p identity
155 * becomes the default.
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 Yu03997682015-11-23 16:41:38 -0800166 * @brief Remove a key with @p keyName and related certificates
Yingdi Yu151b5572015-04-27 11:07:37 -0700167 *
168 * If the key does not exist, do nothing.
Yingdi Yu151b5572015-04-27 11:07:37 -0700169 */
170 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700171 removeKey(const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700172
173 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700174 * @brief Get the key bits of a key with name @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700175 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700176 * @return key bits
Yingdi Yucbe72b02015-11-25 17:35:37 -0800177 * @throw Pib::Error the key does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700178 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700179 virtual Buffer
180 getKeyBits(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700181
182 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700183 * @brief Get all the key names of an identity with name @p identity
Yingdi Yu151b5572015-04-27 11:07:37 -0700184 *
Yingdi Yu03997682015-11-23 16:41:38 -0800185 * The returned key names can be used to create a KeyContainer. With key name and backend
186 * implementation, one can create a Key frontend instance.
Yingdi Yu151b5572015-04-27 11:07:37 -0700187 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700188 * @return the key name component set. If the identity does not exist, return an empty set.
Yingdi Yu151b5572015-04-27 11:07:37 -0700189 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700190 virtual std::set<Name>
Yingdi Yu151b5572015-04-27 11:07:37 -0700191 getKeysOfIdentity(const Name& identity) const = 0;
192
193 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700194 * @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 -0700195 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800196 * @throw Pib::Error the key does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700197 */
198 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700199 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700200
201 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700202 * @return The name of the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700203 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800204 * @throw Pib::Error no default key or the identity does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700205 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700206 virtual Name
Yingdi Yu151b5572015-04-27 11:07:37 -0700207 getDefaultKeyOfIdentity(const Name& identity) const = 0;
208
209public: // Certificate Management
Yingdi Yu151b5572015-04-27 11:07:37 -0700210 /**
211 * @brief Check the existence of a certificate with name @p certName.
212 *
213 * @param certName The name of the certificate.
214 * @return true if the certificate exists, otherwise false.
215 */
216 virtual bool
217 hasCertificate(const Name& certName) const = 0;
218
219 /**
220 * @brief Add a certificate.
221 *
Yingdi Yu03997682015-11-23 16:41:38 -0800222 * If a certificate with the same name (without implicit digest) already exists, overwrite
223 * the certificate. If the key or identity does not exist, they will be created. If no
224 * default certificate of the key has been set, set the added one as default certificate of
225 * the key. If no default key was set for the identity, it will be set as default key for
226 * the identity. If no default identity was selected, the certificate's identity becomes
227 * default.
Yingdi Yu151b5572015-04-27 11:07:37 -0700228 *
229 * @param certificate The certificate to add.
230 */
231 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700232 addCertificate(const v2::Certificate& certificate) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700233
234 /**
235 * @brief Remove a certificate with name @p certName.
236 *
237 * If the certificate does not exist, do nothing.
238 *
239 * @param certName The name of the certificate.
240 */
241 virtual void
242 removeCertificate(const Name& certName) = 0;
243
244 /**
245 * @brief Get a certificate with name @p certName.
246 *
247 * @param certName The name of the certificate.
248 * @return the certificate.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800249 * @throw Pib::Error the certificate does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700250 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700251 virtual v2::Certificate
Yingdi Yu151b5572015-04-27 11:07:37 -0700252 getCertificate(const Name& certName) const = 0;
253
254 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700255 * @brief Get a list of certificate names of a key with id @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700256 *
Yingdi Yu03997682015-11-23 16:41:38 -0800257 * The returned certificate names can be used to create a CertificateContainer. With
258 * certificate name and backend implementation, one can obtain the certificate.
Yingdi Yu151b5572015-04-27 11:07:37 -0700259 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700260 * @return The certificate name set. If the key does not exist, return an empty set.
261 */
262 virtual std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700263 getCertificatesOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700264
265 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700266 * @brief Set a cert with name @p certName as the default of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700267 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800268 * @throw Pib::Error the certificate with name @p certName does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700269 */
270 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700271 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700272
273 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700274 * @return Get the default certificate of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700275 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800276 * @throw Pib::Error the default certificate does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700277 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700278 virtual v2::Certificate
279 getDefaultCertificateOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700280};
281
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700282} // namespace pib
Yingdi Yu151b5572015-04-27 11:07:37 -0700283} // namespace security
284} // namespace ndn
285
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700286#endif // NDN_SECURITY_PIB_PIB_IMPL_HPP