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 | * |
| 89 | * If the identity already exists, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 90 | * 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] | 91 | * |
| 92 | * @param identity The name of the identity to add. |
| 93 | */ |
| 94 | virtual void |
| 95 | addIdentity(const Name& identity) = 0; |
| 96 | |
| 97 | /** |
| 98 | * @brief Remove an identity |
| 99 | * |
| 100 | * If the identity does not exist, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 101 | * Remove related keys and certificates as well. |
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 | * |
| 123 | * Since adding an identity only requires the identity name, create the |
| 124 | * identity if it does not exist. |
| 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. |
| 135 | * @throws Pib::Error if no default identity. |
| 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 | * |
| 152 | * If the key already exists, do nothing. |
| 153 | * If the identity does not exist, add the identity as well. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 154 | * If no default key of the identity has been set, set the added one as default |
| 155 | * key of the identity. |
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 | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 166 | * @brief Remove a key with @p keyName |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 167 | * |
| 168 | * If the key does not exist, do nothing. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 169 | * Remove related certificates as well. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 170 | */ |
| 171 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 172 | removeKey(const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 173 | |
| 174 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 175 | * @brief Get the key bits of a key with name @p keyName. |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 176 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 177 | * @return key bits |
| 178 | * @throws Pib::Error if the key does not exist. |
| 179 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 180 | virtual Buffer |
| 181 | getKeyBits(const Name& keyName) const = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 184 | * @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] | 185 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 186 | * The returned key names can be used to create a KeyContainer. |
| 187 | * 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] | 188 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 189 | * @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] | 190 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 191 | virtual std::set<Name> |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 192 | getKeysOfIdentity(const Name& identity) const = 0; |
| 193 | |
| 194 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 195 | * @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] | 196 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 197 | * @throws Pib::Error if the key does not exist. |
| 198 | */ |
| 199 | virtual void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 200 | setDefaultKeyOfIdentity(const Name& identity, const Name& keyName) = 0; |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 201 | |
| 202 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 203 | * @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] | 204 | * |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 205 | * @throws Pib::Error if no default key or the identity does not exist. |
| 206 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 207 | virtual Name |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 208 | getDefaultKeyOfIdentity(const Name& identity) const = 0; |
| 209 | |
| 210 | public: // Certificate Management |
Yingdi Yu | 151b557 | 2015-04-27 11:07:37 -0700 | [diff] [blame] | 211 | /** |
| 212 | * @brief Check the existence of a certificate with name @p certName. |
| 213 | * |
| 214 | * @param certName The name of the certificate. |
| 215 | * @return true if the certificate exists, otherwise false. |
| 216 | */ |
| 217 | virtual bool |
| 218 | hasCertificate(const Name& certName) const = 0; |
| 219 | |
| 220 | /** |
| 221 | * @brief Add a certificate. |
| 222 | * |
| 223 | * If the certificate already exists, do nothing. |
| 224 | * If the key or identity do not exist, add them as well. |
Yingdi Yu | 3bf91f5 | 2015-06-12 19:39:40 -0700 | [diff] [blame] | 225 | * If no default certificate of the key has been set, set the added one as |
| 226 | * default certificate of the key. |
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 | * |
| 256 | * The returned certificate names can be used to create a CertificateContainer. |
| 257 | * With certificate name and backend implementation, one can obtain the certificate directly. |
| 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 |