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