Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 22 | #ifndef NDN_SECURITY_PIB_PIB_IMPL_HPP |
| 23 | #define NDN_SECURITY_PIB_PIB_IMPL_HPP |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 25 | #include "pib.hpp" |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 26 | #include "../v2/certificate.hpp" |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 27 | |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 28 | #include <set> |
| 29 | |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 30 | namespace ndn { |
| 31 | namespace security { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 32 | namespace pib { |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 33 | |
| 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 Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 40 | class PibImpl : noncopyable |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 41 | { |
| 42 | public: |
| 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 Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 52 | using std::runtime_error::runtime_error; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | public: |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 56 | virtual |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 57 | ~PibImpl() = default; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 58 | |
| 59 | public: // TpmLocator management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 60 | /** |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 61 | * @brief Set the corresponding TPM information to @p tpmLocator |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 62 | * |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 63 | * This method does not reset contents of the PIB |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 64 | */ |
| 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 | |
| 74 | public: // Identity management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 75 | /** |
| 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 Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 87 | * If the identity already exists, do nothing. If no default identity has been set, set the |
| 88 | * added one as default identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 89 | * |
| 90 | * @param identity The name of the identity to add. |
| 91 | */ |
| 92 | virtual void |
| 93 | addIdentity(const Name& identity) = 0; |
| 94 | |
| 95 | /** |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 96 | * @brief Remove an identity and related keys and certificates. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 97 | * |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 98 | * If the default identity is being removed, no default identity will be selected. If the |
| 99 | * identity does not exist, do nothing. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 100 | * |
| 101 | * @param identity The name of the identity to remove. |
| 102 | */ |
| 103 | virtual void |
| 104 | removeIdentity(const Name& identity) = 0; |
| 105 | |
Yingdi Yu | 7b3b5e9 | 2015-08-13 19:52:35 -0700 | [diff] [blame] | 106 | /** |
| 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 Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 115 | 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 Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 121 | * If @p identityName identity does not exist, it will be created. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 122 | * |
| 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 132 | * @throw Pib::Error no default identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 133 | */ |
| 134 | virtual Name |
| 135 | getDefaultIdentity() const = 0; |
| 136 | |
| 137 | public: // Key management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 138 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 139 | * @brief Check the existence of a key with @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 140 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 141 | * @return true if the key exists, otherwise false. Return false if the identity does not exist |
| 142 | */ |
| 143 | virtual bool |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 144 | hasKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 145 | |
| 146 | /** |
| 147 | * @brief Add a key. |
| 148 | * |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 149 | * 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 Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 153 | * |
| 154 | * @param identity The name of the belonged identity. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 155 | * @param keyName The key name. |
| 156 | * @param key The public key bits. |
| 157 | * @param keyLen The length of the public key. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 158 | */ |
| 159 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 160 | addKey(const Name& identity, const Name& keyName, const uint8_t* key, size_t keyLen) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 163 | * @brief Remove a key with @p keyName and related certificates |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 164 | * |
| 165 | * If the key does not exist, do nothing. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 166 | */ |
| 167 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 168 | removeKey(const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 171 | * @brief Get the key bits of a key with name @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 172 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 173 | * @return key bits |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 174 | * @throw Pib::Error the key does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 175 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 176 | virtual Buffer |
| 177 | getKeyBits(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 178 | |
| 179 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 180 | * @brief Get all the key names of an identity with name @p identity |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 181 | * |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 182 | * 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 Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 184 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 185 | * @return the key name component set. If the identity does not exist, return an empty set. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 186 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 187 | virtual std::set<Name> |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 188 | getKeysOfIdentity(const Name& identity) const = 0; |
| 189 | |
| 190 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 191 | * @brief Set an key with @p keyName as the default key of an identity with name @p identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 192 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 193 | * @throw Pib::Error the key does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 194 | */ |
| 195 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 196 | setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 197 | |
| 198 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 199 | * @return The name of the default key of an identity with name @p identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 200 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 201 | * @throw Pib::Error no default key or the identity does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 202 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 203 | virtual Name |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 204 | getDefaultKeyOfIdentity(const Name& identity) const = 0; |
| 205 | |
| 206 | public: // Certificate Management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 207 | /** |
| 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 Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 219 | * 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 Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 225 | * |
| 226 | * @param certificate The certificate to add. |
| 227 | */ |
| 228 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 229 | addCertificate(const v2::Certificate& certificate) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 230 | |
| 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 Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 246 | * @throw Pib::Error the certificate does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 247 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 248 | virtual v2::Certificate |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 249 | getCertificate(const Name& certName) const = 0; |
| 250 | |
| 251 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 252 | * @brief Get a list of certificate names of a key with id @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 253 | * |
Yingdi Yu | 0399768 | 2015-11-23 16:41:38 -0800 | [diff] [blame] | 254 | * The returned certificate names can be used to create a CertificateContainer. With |
| 255 | * certificate name and backend implementation, one can obtain the certificate. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 256 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 257 | * @return The certificate name set. If the key does not exist, return an empty set. |
| 258 | */ |
| 259 | virtual std::set<Name> |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 260 | getCertificatesOfKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 261 | |
| 262 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 263 | * @brief Set a cert with name @p certName as the default of a key with @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 264 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 265 | * @throw Pib::Error the certificate with name @p certName does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 266 | */ |
| 267 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 268 | setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 271 | * @return Get the default certificate of a key with @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 272 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 273 | * @throw Pib::Error the default certificate does not exist. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 274 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 275 | virtual v2::Certificate |
| 276 | getDefaultCertificateOfKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 277 | }; |
| 278 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 279 | } // namespace pib |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 280 | } // namespace security |
| 281 | } // namespace ndn |
| 282 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 283 | #endif // NDN_SECURITY_PIB_PIB_IMPL_HPP |