Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -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 | |
| 22 | #ifndef NDN_SECURITY_IDENTITY_HPP |
| 23 | #define NDN_SECURITY_IDENTITY_HPP |
| 24 | |
| 25 | #include "key-container.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace security { |
| 29 | |
| 30 | class PibImpl; |
| 31 | class Pib; |
| 32 | class IdentityContainer; |
| 33 | |
| 34 | /** |
| 35 | * @brief represents an identity |
| 36 | * |
| 37 | * Identity is at the top level in PIB's Identity-Key-Certificate hierarchy. |
| 38 | * An identity has a Name, and contains one or more keys, one of which is set |
| 39 | * as the default key of this identity. Properties of a key can be accessed |
| 40 | * after obtaining a Key object. |
| 41 | * |
| 42 | * @throw PibImpl::Error when underlying implementation has non-semantic error. |
| 43 | */ |
| 44 | class Identity |
| 45 | { |
| 46 | public: |
| 47 | friend class Pib; |
| 48 | friend class IdentityContainer; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 49 | friend class KeyChain; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | |
| 51 | public: |
| 52 | /** |
| 53 | * @brief Default Constructor |
| 54 | * |
| 55 | * Identity created using this default constructor is just a place holder. |
| 56 | * It must obtain an actual instance from Pib::getIdentity(...). A typical |
| 57 | * usage would be for exception handling: |
| 58 | * |
| 59 | * Identity id; |
| 60 | * try { |
| 61 | * id = pib.getIdentity(...); |
| 62 | * } |
| 63 | * catch (Pib::Error&) { |
| 64 | * ... |
| 65 | * } |
| 66 | * |
| 67 | * An Identity instance created using the constructor is invalid. Calling a |
| 68 | * member method on an invalid Identity instance may cause an std::domain_error. |
| 69 | */ |
| 70 | Identity(); |
| 71 | |
| 72 | /// @brief Get the name of the identity. |
| 73 | const Name& |
| 74 | getName() const; |
| 75 | |
| 76 | /** |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 77 | * @brief Get a key with id @p keyId. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 78 | * |
Davide Pesavento | 18cf81b | 2015-09-12 23:36:43 +0200 | [diff] [blame] | 79 | * @param keyId The id of the key to get. |
| 80 | * @throw Pib::Error if the key does not exist. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 81 | */ |
| 82 | Key |
| 83 | getKey(const name::Component& keyId) const; |
| 84 | |
| 85 | /// @brief Get all the keys for this Identity. |
| 86 | const KeyContainer& |
| 87 | getKeys() const; |
| 88 | |
| 89 | /** |
| 90 | * @brief Get the default key for this Identity. |
| 91 | * |
| 92 | * @throws Pib::Error if the default key does not exist. |
| 93 | */ |
| 94 | Key& |
| 95 | getDefaultKey() const; |
| 96 | |
| 97 | /// @brief Check if the Identity instance is valid |
| 98 | operator bool() const; |
| 99 | |
| 100 | /// @brief Check if the Identity instance is invalid |
| 101 | bool |
| 102 | operator!() const; |
| 103 | |
| 104 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
| 105 | |
| 106 | /** |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 107 | * @brief Add a key. |
| 108 | * |
| 109 | * If the key already exists, do nothing. |
| 110 | * |
| 111 | * If no default key is set before, the new key will be set as the default key of the identity. |
| 112 | * |
| 113 | * @param publicKey The public key to add. |
| 114 | * @param keyId The key id component of the new key to add. |
| 115 | * By default, the keyId will be set to the hash of the public key bits. |
| 116 | * @return the added key or existing key with the same key id. |
| 117 | */ |
| 118 | Key |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 119 | addKey(const v1::PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * @brief Remove a key. |
| 123 | * |
| 124 | * @param keyId The key id component of the key to delete. |
| 125 | */ |
| 126 | void |
| 127 | removeKey(const name::Component& keyId); |
| 128 | |
| 129 | /** |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 130 | * @brief Set the key with id @p keyId as the default key. |
| 131 | * |
| 132 | * @param keyId The key id component of the default key. |
| 133 | * @return The default key |
| 134 | * @throws Pib::Error if the key does not exist. |
| 135 | */ |
| 136 | Key& |
| 137 | setDefaultKey(const name::Component& keyId); |
| 138 | |
| 139 | /** |
| 140 | * @brief Set the default key. |
| 141 | * |
| 142 | * If the key does not exist, add the key and set it as the default of the Identity. |
| 143 | * If the key exists, simply set it as the default key of the Identity. |
| 144 | * |
| 145 | * @param publicKey The public key to add. |
| 146 | * @param keyId The key id component of the default key. |
| 147 | * @return the default key |
| 148 | */ |
| 149 | Key& |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 150 | setDefaultKey(const v1::PublicKey& publicKey, const name::Component& keyId = EMPTY_KEY_ID); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 151 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 152 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: |
| 153 | /** |
| 154 | * @brief Create an Identity with @p identityName. |
| 155 | * |
| 156 | * @param identityName The name of the Identity. |
| 157 | * @param impl The backend implementation. |
| 158 | * @param needInit If true, create the identity in backend when the identity does not exist. |
| 159 | * Otherwise, throw Pib::Error when the identity does not exist. |
| 160 | */ |
| 161 | Identity(const Name& identityName, shared_ptr<PibImpl> impl, bool needInit = false); |
| 162 | |
| 163 | /** |
| 164 | * @brief Check the validity of this instance |
| 165 | * |
| 166 | * @throws std::domain_error if the instance is invalid |
| 167 | */ |
| 168 | void |
| 169 | validityCheck() const; |
| 170 | |
| 171 | public: |
| 172 | /** |
| 173 | * @brief The default value of keyId when add a new key. |
| 174 | * |
| 175 | * An empty keyId implies that the key digest should be used as the actual keyId. |
| 176 | */ |
| 177 | static const name::Component EMPTY_KEY_ID; |
| 178 | |
| 179 | private: |
| 180 | Name m_name; |
| 181 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 182 | mutable bool m_hasDefaultKey; |
| 183 | mutable Key m_defaultKey; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 184 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 185 | mutable bool m_needRefreshKeys; |
| 186 | mutable KeyContainer m_keys; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 187 | |
| 188 | shared_ptr<PibImpl> m_impl; |
| 189 | }; |
| 190 | |
| 191 | } // namespace security |
| 192 | } // namespace ndn |
| 193 | |
| 194 | #endif // NDN_SECURITY_IDENTITY_HPP |