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