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