Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 22 | #ifndef NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP |
| 23 | #define NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP |
| 24 | |
| 25 | #include "../key-container.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | namespace pib { |
| 30 | |
| 31 | class PibImpl; |
| 32 | |
| 33 | namespace detail { |
| 34 | |
| 35 | /** |
| 36 | * @brief Backend instance of Identity |
| 37 | * |
| 38 | * An Identity has only one backend instance, but may have multiple frontend handles. |
| 39 | * Each frontend handle is associated with the only one backend IdentityImpl. |
| 40 | * |
| 41 | * @throw PibImpl::Error when underlying implementation has non-semantic error. |
| 42 | */ |
| 43 | class IdentityImpl : noncopyable |
| 44 | { |
| 45 | public: |
| 46 | /** |
| 47 | * @brief Create an Identity with @p identityName. |
| 48 | * |
| 49 | * @param identityName The name of the Identity. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 50 | * @param pibImpl The PIB backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 51 | * @param needInit If true, create the identity in backend when the identity does not exist. |
| 52 | * Otherwise, throw Pib::Error when the identity does not exist. |
| 53 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 54 | IdentityImpl(const Name& identityName, shared_ptr<PibImpl> pibImpl, bool needInit = false); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 55 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 56 | /** |
| 57 | * @brief Get the name of the identity. |
| 58 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | const Name& |
| 60 | getName() const |
| 61 | { |
| 62 | return m_name; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @brief Add a @p key of @p keyLen bytes with @p keyName (in PKCS#8 format). |
| 67 | * |
| 68 | * If no default key is set before, the new key will be set as the default key of the identity. |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 69 | * If a key with the same name already exists, overwrite the key. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | * |
| 71 | * @return the added key. |
| 72 | * @throw std::invalid_argument key name does not match identity |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 73 | */ |
| 74 | Key |
| 75 | addKey(const uint8_t* key, size_t keyLen, const Name& keyName); |
| 76 | |
| 77 | /** |
| 78 | * @brief Remove a key with @p keyName |
| 79 | * @throw std::invalid_argument @p keyName does not match identity |
| 80 | */ |
| 81 | void |
| 82 | removeKey(const Name& keyName); |
| 83 | |
| 84 | /** |
| 85 | * @brief Get a key with id @p keyName. |
| 86 | * |
| 87 | * @throw std::invalid_argument @p keyName does not match identity |
| 88 | * @throw Pib::Error the key does not exist. |
| 89 | */ |
| 90 | Key |
| 91 | getKey(const Name& keyName) const; |
| 92 | |
| 93 | /** |
| 94 | * @brief Get all keys for this Identity. |
| 95 | */ |
| 96 | const KeyContainer& |
| 97 | getKeys() const; |
| 98 | |
| 99 | /** |
| 100 | * @brief Set the key with id @p keyName. |
| 101 | * @throw std::invalid_argument @p keyName does not match identity |
| 102 | * @throw Pib::Error the key does not exist. |
| 103 | * @return The default key |
| 104 | */ |
| 105 | const Key& |
| 106 | setDefaultKey(const Name& keyName); |
| 107 | |
| 108 | /** |
| 109 | * @brief Add @p key of @p keyLen bytes with @p keyName and set it as the default key |
| 110 | * @throw std::invalid_argument @p keyName does not match identity |
| 111 | * @throw Pib::Error the key with the same name already exists |
| 112 | * @return the default key |
| 113 | */ |
| 114 | const Key& |
| 115 | setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName); |
| 116 | |
| 117 | /** |
| 118 | * @brief Get the default key for this Identity. |
| 119 | * @throw Pib::Error the default key does not exist. |
| 120 | */ |
| 121 | const Key& |
| 122 | getDefaultKey() const; |
| 123 | |
| 124 | private: |
| 125 | Name m_name; |
| 126 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 127 | shared_ptr<PibImpl> m_pib; |
| 128 | |
| 129 | KeyContainer m_keys; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 130 | mutable bool m_isDefaultKeyLoaded; |
| 131 | mutable Key m_defaultKey; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace detail |
| 135 | } // namespace pib |
| 136 | } // namespace security |
| 137 | } // namespace ndn |
| 138 | |
| 139 | #endif // NDN_SECURITY_PIB_DETAIL_IDENTITY_IMPL_HPP |