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 | /** |
| 63 | * @brief Set the corresponding TPM information to @p tpmLocator. |
| 64 | * |
| 65 | * If the provided @p tpmLocator is different from the existing one, the |
| 66 | * content in PIB will be cleaned up, otherwise nothing will be changed. |
| 67 | * |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 68 | * @param tpmLocator The name for the new TPM locator |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 69 | */ |
| 70 | virtual void |
| 71 | setTpmLocator(const std::string& tpmLocator) = 0; |
| 72 | |
| 73 | /** |
| 74 | * @brief Get TPM Locator |
| 75 | */ |
| 76 | virtual std::string |
| 77 | getTpmLocator() const = 0; |
| 78 | |
| 79 | public: // Identity management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 80 | /** |
| 81 | * @brief Check the existence of an identity. |
| 82 | * |
| 83 | * @param identity The name of the identity. |
| 84 | * @return true if the identity exists, otherwise false. |
| 85 | */ |
| 86 | virtual bool |
| 87 | hasIdentity(const Name& identity) const = 0; |
| 88 | |
| 89 | /** |
| 90 | * @brief Add an identity. |
| 91 | * |
| 92 | * If the identity already exists, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 93 | * If no default identity has been set, set the added one as default identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 94 | * |
| 95 | * @param identity The name of the identity to add. |
| 96 | */ |
| 97 | virtual void |
| 98 | addIdentity(const Name& identity) = 0; |
| 99 | |
| 100 | /** |
| 101 | * @brief Remove an identity |
| 102 | * |
| 103 | * If the identity does not exist, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 104 | * Remove related keys and certificates as well. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 105 | * |
| 106 | * @param identity The name of the identity to remove. |
| 107 | */ |
| 108 | virtual void |
| 109 | removeIdentity(const Name& identity) = 0; |
| 110 | |
| 111 | /// @brief Get the name of all the identities |
| 112 | virtual std::set<Name> |
| 113 | getIdentities() const = 0; |
| 114 | |
| 115 | /** |
| 116 | * @brief Set an identity with name @p identityName as the default identity. |
| 117 | * |
| 118 | * Since adding an identity only requires the identity name, create the |
| 119 | * identity if it does not exist. |
| 120 | * |
| 121 | * @param identityName The name for the default identity. |
| 122 | */ |
| 123 | virtual void |
| 124 | setDefaultIdentity(const Name& identityName) = 0; |
| 125 | |
| 126 | /** |
| 127 | * @brief Get the default identity. |
| 128 | * |
| 129 | * @return The name for the default identity. |
| 130 | * @throws Pib::Error if no default identity. |
| 131 | */ |
| 132 | virtual Name |
| 133 | getDefaultIdentity() const = 0; |
| 134 | |
| 135 | public: // Key management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 136 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 137 | * @brief Check the existence of a key with @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 138 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 139 | * @return true if the key exists, otherwise false. Return false if the identity does not exist |
| 140 | */ |
| 141 | virtual bool |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 142 | hasKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * @brief Add a key. |
| 146 | * |
| 147 | * If the key already exists, do nothing. |
| 148 | * If the identity does not exist, add the identity as well. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 149 | * If no default key of the identity has been set, set the added one as default |
| 150 | * key of the identity. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 151 | * |
| 152 | * @param identity The name of the belonged identity. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 153 | * @param keyName The key name. |
| 154 | * @param key The public key bits. |
| 155 | * @param keyLen The length of the public key. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 156 | */ |
| 157 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 158 | 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] | 159 | |
| 160 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 161 | * @brief Remove a key with @p keyName |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 162 | * |
| 163 | * If the key does not exist, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 164 | * Remove related certificates as well. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 165 | */ |
| 166 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 167 | removeKey(const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 168 | |
| 169 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 170 | * @brief Get the key bits of a key with name @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 171 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 172 | * @return key bits |
| 173 | * @throws Pib::Error if the key does not exist. |
| 174 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 175 | virtual Buffer |
| 176 | getKeyBits(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 179 | * @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] | 180 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 181 | * The returned key names can be used to create a KeyContainer. |
| 182 | * With key name, identity name, backend implementation, one can create a Key frontend instance. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 183 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 184 | * @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] | 185 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 186 | virtual std::set<Name> |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 187 | getKeysOfIdentity(const Name& identity) const = 0; |
| 188 | |
| 189 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 190 | * @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] | 191 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 192 | * @throws Pib::Error if the key does not exist. |
| 193 | */ |
| 194 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 195 | setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 198 | * @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] | 199 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 200 | * @throws Pib::Error if no default key or the identity does not exist. |
| 201 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 202 | virtual Name |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 203 | getDefaultKeyOfIdentity(const Name& identity) const = 0; |
| 204 | |
| 205 | public: // Certificate Management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 206 | /** |
| 207 | * @brief Check the existence of a certificate with name @p certName. |
| 208 | * |
| 209 | * @param certName The name of the certificate. |
| 210 | * @return true if the certificate exists, otherwise false. |
| 211 | */ |
| 212 | virtual bool |
| 213 | hasCertificate(const Name& certName) const = 0; |
| 214 | |
| 215 | /** |
| 216 | * @brief Add a certificate. |
| 217 | * |
| 218 | * If the certificate already exists, do nothing. |
| 219 | * If the key or identity do not exist, add them as well. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 220 | * If no default certificate of the key has been set, set the added one as |
| 221 | * default certificate of the key. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 222 | * |
| 223 | * @param certificate The certificate to add. |
| 224 | */ |
| 225 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 226 | addCertificate(const v2::Certificate& certificate) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 227 | |
| 228 | /** |
| 229 | * @brief Remove a certificate with name @p certName. |
| 230 | * |
| 231 | * If the certificate does not exist, do nothing. |
| 232 | * |
| 233 | * @param certName The name of the certificate. |
| 234 | */ |
| 235 | virtual void |
| 236 | removeCertificate(const Name& certName) = 0; |
| 237 | |
| 238 | /** |
| 239 | * @brief Get a certificate with name @p certName. |
| 240 | * |
| 241 | * @param certName The name of the certificate. |
| 242 | * @return the certificate. |
| 243 | * @throws Pib::Error if the certificate does not exist. |
| 244 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 245 | virtual v2::Certificate |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 246 | getCertificate(const Name& certName) const = 0; |
| 247 | |
| 248 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 249 | * @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] | 250 | * |
| 251 | * The returned certificate names can be used to create a CertificateContainer. |
| 252 | * With certificate name and backend implementation, one can obtain the certificate directly. |
| 253 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 254 | * @return The certificate name set. If the key does not exist, return an empty set. |
| 255 | */ |
| 256 | virtual std::set<Name> |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 257 | getCertificatesOfKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 258 | |
| 259 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 260 | * @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] | 261 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 262 | * @throws Pib::Error if the certificate with name @p certName does not exist. |
| 263 | */ |
| 264 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 265 | setDefaultCertificateOfKey(const Name& keyName, const Name& certName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 266 | |
| 267 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 268 | * @return Get the default certificate of a key with @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 269 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 270 | * @throws Pib::Error if the default certificate does not exist. |
| 271 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 272 | virtual v2::Certificate |
| 273 | getDefaultCertificateOfKey(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 274 | }; |
| 275 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame^] | 276 | } // namespace pib |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 277 | } // namespace security |
| 278 | } // namespace ndn |
| 279 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 280 | #endif // NDN_SECURITY_PIB_PIB_IMPL_HPP |