Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 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 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 31 | namespace detail { |
| 32 | class IdentityImpl; |
| 33 | } // namespace detail |
| 34 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 35 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 36 | * @brief A frontend handle of an Identity |
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 | * Identity is at the top level in PIB's Identity-Key-Certificate hierarchy. An identity has a |
| 39 | * Name, and contains zero or more keys, at most one of which is set as the default key of this |
| 40 | * 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] | 41 | */ |
| 42 | class Identity |
| 43 | { |
| 44 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 45 | /** |
| 46 | * @brief Default Constructor |
| 47 | * |
| 48 | * Identity created using this default constructor is just a place holder. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 49 | * It can obtain an actual instance from Pib::getIdentity(...). A typical |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 50 | * usage would be for exception handling: |
| 51 | * |
| 52 | * Identity id; |
| 53 | * try { |
| 54 | * id = pib.getIdentity(...); |
| 55 | * } |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 56 | * catch (const Pib::Error&) { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 57 | * ... |
| 58 | * } |
| 59 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 60 | * An Identity instance created using this constructor is invalid. Calling a |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 61 | * member method on an invalid Identity instance may cause an std::domain_error. |
| 62 | */ |
| 63 | Identity(); |
| 64 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 65 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 66 | * @brief Create an Identity with a backend implementation @p impl. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 67 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 68 | * This method should only be used by IdentityContainer. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 69 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 70 | explicit |
| 71 | Identity(weak_ptr<detail::IdentityImpl> impl); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 72 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 73 | /** |
| 74 | * @brief Get the name of the identity. |
| 75 | */ |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 76 | const Name& |
| 77 | getName() const; |
| 78 | |
| 79 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 80 | * @brief Get a key with id @p keyName. |
| 81 | * @throw std::invalid_argument @p keyName does not match identity |
| 82 | * @throw Pib::Error the key does not exist. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 83 | */ |
| 84 | Key |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 85 | getKey(const Name& keyName) const; |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 86 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 87 | /** |
| 88 | * @brief Get all keys for this identity. |
| 89 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 90 | const KeyContainer& |
| 91 | getKeys() const; |
| 92 | |
| 93 | /** |
| 94 | * @brief Get the default key for this Identity. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 95 | * @throw Pib::Error the default key does not exist. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 96 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 97 | const Key& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 98 | getDefaultKey() const; |
| 99 | |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame^] | 100 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 101 | * @return True if the identity instance is valid |
| 102 | */ |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 103 | explicit |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 104 | operator bool() const; |
| 105 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 106 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 107 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 108 | * @brief Add a @p key of @p keyLen bytes (in PKCS#8 format) with @p keyName. |
| 109 | * @return the handle of added key |
| 110 | * @throw std::invalid_argument key name does not match identity |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 111 | * |
| 112 | * If a key with the same name already exists, overwrite the key. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 113 | */ |
| 114 | Key |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 115 | addKey(const uint8_t* key, size_t keyLen, const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 116 | |
| 117 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 118 | * @brief Remove a key with @p keyName |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 119 | * @throw std::invalid_argument @p keyName does not match identity |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 120 | */ |
| 121 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 122 | removeKey(const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 123 | |
| 124 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 125 | * @brief Set an existing key with @p keyName as the default key. |
| 126 | * @throw std::invalid_argument @p keyName does not match identity |
| 127 | * @throw Pib::Error the key does not exist. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 128 | * @return The default key |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 129 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 130 | const Key& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 131 | setDefaultKey(const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | |
| 133 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 134 | * @brief Add a @p key of @p keyLen bytes with @p keyName and set it as the default key |
| 135 | * @throw std::invalid_argument @p keyName does not match identity |
| 136 | * @throw Pib::Error the key with the same name already exists. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 137 | * @return the default key |
| 138 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 139 | const Key& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 140 | setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 141 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 142 | private: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 143 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 144 | * @brief Check the validity of the instance |
| 145 | * @return a shared_ptr when the instance is valid |
| 146 | * @throw std::domain_error the instance is invalid |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 147 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 148 | shared_ptr<detail::IdentityImpl> |
| 149 | lock() const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 150 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 151 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 152 | weak_ptr<detail::IdentityImpl> m_impl; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 153 | |
| 154 | friend class v2::KeyChain; |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 155 | friend bool operator!=(const Identity&, const Identity&); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 158 | bool |
| 159 | operator!=(const Identity& lhs, const Identity& rhs); |
| 160 | |
| 161 | inline bool |
| 162 | operator==(const Identity& lhs, const Identity& rhs) |
| 163 | { |
| 164 | return !(lhs != rhs); |
| 165 | } |
| 166 | |
| 167 | std::ostream& |
| 168 | operator<<(std::ostream& os, const Identity& id); |
| 169 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 170 | } // namespace pib |
| 171 | |
| 172 | using pib::Identity; |
| 173 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 174 | } // namespace security |
| 175 | } // namespace ndn |
| 176 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 177 | #endif // NDN_SECURITY_PIB_IDENTITY_HPP |