blob: ba72466a270102c6c1db847298a2b9453e63c02e [file] [log] [blame]
Yingdi Yu151b5572015-04-27 11:07:37 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi68b53852018-07-25 13:56:38 -06002/*
3 * Copyright (c) 2013-2018 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 Yu6ee2d362015-07-16 21:48:05 -070026#include "../v2/certificate.hpp"
Yingdi Yu151b5572015-04-27 11:07:37 -070027
Junxiao Shi68b53852018-07-25 13:56:38 -060028#include <set>
29
Yingdi Yu151b5572015-04-27 11:07:37 -070030namespace ndn {
31namespace security {
Yingdi Yu6ee2d362015-07-16 21:48:05 -070032namespace pib {
Yingdi Yu151b5572015-04-27 11:07:37 -070033
34/**
35 * @brief Abstract class of PIB implementation
36 *
37 * This class defines the interface that an actual PIB (e.g., one based on sqlite3)
38 * implementation should provide.
39 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -070040class PibImpl : noncopyable
Yingdi Yu151b5572015-04-27 11:07:37 -070041{
42public:
43 /**
44 * @brief represents a non-semantic error
45 *
46 * A subclass of PibImpl may throw a subclass of this type when
47 * there's a non-semantic error, such as a storage problem.
48 */
49 class Error : public std::runtime_error
50 {
51 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060052 using std::runtime_error::runtime_error;
Yingdi Yu151b5572015-04-27 11:07:37 -070053 };
54
55public:
Yingdi Yu151b5572015-04-27 11:07:37 -070056 virtual
Yingdi Yu6ee2d362015-07-16 21:48:05 -070057 ~PibImpl() = default;
Yingdi Yu151b5572015-04-27 11:07:37 -070058
59public: // TpmLocator management
Yingdi Yu151b5572015-04-27 11:07:37 -070060 /**
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070061 * @brief Set the corresponding TPM information to @p tpmLocator
Yingdi Yu151b5572015-04-27 11:07:37 -070062 *
Yingdi Yu7b3b5e92015-08-13 19:52:35 -070063 * This method does not reset contents of the PIB
Yingdi Yu151b5572015-04-27 11:07:37 -070064 */
65 virtual void
66 setTpmLocator(const std::string& tpmLocator) = 0;
67
68 /**
69 * @brief Get TPM Locator
70 */
71 virtual std::string
72 getTpmLocator() const = 0;
73
74public: // Identity management
Yingdi Yu151b5572015-04-27 11:07:37 -070075 /**
76 * @brief Check the existence of an identity.
77 *
78 * @param identity The name of the identity.
79 * @return true if the identity exists, otherwise false.
80 */
81 virtual bool
82 hasIdentity(const Name& identity) const = 0;
83
84 /**
85 * @brief Add an identity.
86 *
Yingdi Yu03997682015-11-23 16:41:38 -080087 * If the identity already exists, do nothing. If no default identity has been set, set the
88 * added one as default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -070089 *
90 * @param identity The name of the identity to add.
91 */
92 virtual void
93 addIdentity(const Name& identity) = 0;
94
95 /**
Yingdi Yu03997682015-11-23 16:41:38 -080096 * @brief Remove an identity and related keys and certificates.
Yingdi Yu151b5572015-04-27 11:07:37 -070097 *
Yingdi Yu03997682015-11-23 16:41:38 -080098 * If the default identity is being removed, no default identity will be selected. If the
99 * identity does not exist, do nothing.
Yingdi Yu151b5572015-04-27 11:07:37 -0700100 *
101 * @param identity The name of the identity to remove.
102 */
103 virtual void
104 removeIdentity(const Name& identity) = 0;
105
Yingdi Yu7b3b5e92015-08-13 19:52:35 -0700106 /**
107 * @brief Erasing all certificates, keys, and identities
108 */
109 virtual void
110 clearIdentities() = 0;
111
112 /**
113 * @brief Get the name of all the identities
114 */
Yingdi Yu151b5572015-04-27 11:07:37 -0700115 virtual std::set<Name>
116 getIdentities() const = 0;
117
118 /**
119 * @brief Set an identity with name @p identityName as the default identity.
120 *
Yingdi Yu03997682015-11-23 16:41:38 -0800121 * If @p identityName identity does not exist, it will be created.
Yingdi Yu151b5572015-04-27 11:07:37 -0700122 *
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.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800132 * @throw Pib::Error no default identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700133 */
134 virtual Name
135 getDefaultIdentity() const = 0;
136
137public: // Key management
Yingdi Yu151b5572015-04-27 11:07:37 -0700138 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700139 * @brief Check the existence of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700140 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700141 * @return true if the key exists, otherwise false. Return false if the identity does not exist
142 */
143 virtual bool
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700144 hasKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700145
146 /**
147 * @brief Add a key.
148 *
Yingdi Yu03997682015-11-23 16:41:38 -0800149 * If a key with the same name already exists, overwrite the key. If the identity does not
150 * exist, it will be created. If no default key of the identity has been set, set the added
151 * one as default key of the identity. If no default identity has been set, @p identity
152 * becomes the default.
Yingdi Yu151b5572015-04-27 11:07:37 -0700153 *
154 * @param identity The name of the belonged identity.
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700155 * @param keyName The key name.
156 * @param key The public key bits.
157 * @param keyLen The length of the public key.
Yingdi Yu151b5572015-04-27 11:07:37 -0700158 */
159 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700160 addKey(const Name& identity, const Name& keyName, const uint8_t* key, size_t keyLen) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700161
162 /**
Yingdi Yu03997682015-11-23 16:41:38 -0800163 * @brief Remove a key with @p keyName and related certificates
Yingdi Yu151b5572015-04-27 11:07:37 -0700164 *
165 * If the key does not exist, do nothing.
Yingdi Yu151b5572015-04-27 11:07:37 -0700166 */
167 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700168 removeKey(const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700169
170 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700171 * @brief Get the key bits of a key with name @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700172 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700173 * @return key bits
Yingdi Yucbe72b02015-11-25 17:35:37 -0800174 * @throw Pib::Error the key does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700175 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700176 virtual Buffer
177 getKeyBits(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700178
179 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700180 * @brief Get all the key names of an identity with name @p identity
Yingdi Yu151b5572015-04-27 11:07:37 -0700181 *
Yingdi Yu03997682015-11-23 16:41:38 -0800182 * The returned key names can be used to create a KeyContainer. With key name and backend
183 * implementation, one can create a Key frontend instance.
Yingdi Yu151b5572015-04-27 11:07:37 -0700184 *
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700185 * @return the key name component set. If the identity does not exist, return an empty set.
Yingdi Yu151b5572015-04-27 11:07:37 -0700186 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700187 virtual std::set<Name>
Yingdi Yu151b5572015-04-27 11:07:37 -0700188 getKeysOfIdentity(const Name& identity) const = 0;
189
190 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700191 * @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 -0700192 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800193 * @throw Pib::Error the key does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700194 */
195 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700196 setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700197
198 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700199 * @return The name of the default key of an identity with name @p identity.
Yingdi Yu151b5572015-04-27 11:07:37 -0700200 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800201 * @throw Pib::Error no default key or the identity does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700202 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700203 virtual Name
Yingdi Yu151b5572015-04-27 11:07:37 -0700204 getDefaultKeyOfIdentity(const Name& identity) const = 0;
205
206public: // Certificate Management
Yingdi Yu151b5572015-04-27 11:07:37 -0700207 /**
208 * @brief Check the existence of a certificate with name @p certName.
209 *
210 * @param certName The name of the certificate.
211 * @return true if the certificate exists, otherwise false.
212 */
213 virtual bool
214 hasCertificate(const Name& certName) const = 0;
215
216 /**
217 * @brief Add a certificate.
218 *
Yingdi Yu03997682015-11-23 16:41:38 -0800219 * If a certificate with the same name (without implicit digest) already exists, overwrite
220 * the certificate. If the key or identity does not exist, they will be created. If no
221 * default certificate of the key has been set, set the added one as default certificate of
222 * the key. If no default key was set for the identity, it will be set as default key for
223 * the identity. If no default identity was selected, the certificate's identity becomes
224 * default.
Yingdi Yu151b5572015-04-27 11:07:37 -0700225 *
226 * @param certificate The certificate to add.
227 */
228 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700229 addCertificate(const v2::Certificate& certificate) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700230
231 /**
232 * @brief Remove a certificate with name @p certName.
233 *
234 * If the certificate does not exist, do nothing.
235 *
236 * @param certName The name of the certificate.
237 */
238 virtual void
239 removeCertificate(const Name& certName) = 0;
240
241 /**
242 * @brief Get a certificate with name @p certName.
243 *
244 * @param certName The name of the certificate.
245 * @return the certificate.
Yingdi Yucbe72b02015-11-25 17:35:37 -0800246 * @throw Pib::Error the certificate does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700247 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700248 virtual v2::Certificate
Yingdi Yu151b5572015-04-27 11:07:37 -0700249 getCertificate(const Name& certName) const = 0;
250
251 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700252 * @brief Get a list of certificate names of a key with id @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700253 *
Yingdi Yu03997682015-11-23 16:41:38 -0800254 * The returned certificate names can be used to create a CertificateContainer. With
255 * certificate name and backend implementation, one can obtain the certificate.
Yingdi Yu151b5572015-04-27 11:07:37 -0700256 *
Yingdi Yu151b5572015-04-27 11:07:37 -0700257 * @return The certificate name set. If the key does not exist, return an empty set.
258 */
259 virtual std::set<Name>
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700260 getCertificatesOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700261
262 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700263 * @brief Set a cert with name @p certName as the default of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700264 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800265 * @throw Pib::Error the certificate with name @p certName does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700266 */
267 virtual void
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700268 setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700269
270 /**
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700271 * @return Get the default certificate of a key with @p keyName.
Yingdi Yu151b5572015-04-27 11:07:37 -0700272 *
Yingdi Yucbe72b02015-11-25 17:35:37 -0800273 * @throw Pib::Error the default certificate does not exist.
Yingdi Yu151b5572015-04-27 11:07:37 -0700274 */
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700275 virtual v2::Certificate
276 getDefaultCertificateOfKey(const Name& keyName) const = 0;
Yingdi Yu151b5572015-04-27 11:07:37 -0700277};
278
Yingdi Yu6ee2d362015-07-16 21:48:05 -0700279} // namespace pib
Yingdi Yu151b5572015-04-27 11:07:37 -0700280} // namespace security
281} // namespace ndn
282
Alexander Afanasyev97709c02016-08-25 19:58:30 -0700283#endif // NDN_SECURITY_PIB_PIB_IMPL_HPP