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 | |
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 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 100 | /* |
| 101 | * @return True if the identity instance is valid |
| 102 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 103 | operator bool() const; |
| 104 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 105 | /** |
| 106 | * @return True if the identity instance is invalid |
| 107 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 108 | bool |
| 109 | operator!() const; |
| 110 | |
| 111 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 112 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 113 | * @brief Add a @p key of @p keyLen bytes (in PKCS#8 format) with @p keyName. |
| 114 | * @return the handle of added key |
| 115 | * @throw std::invalid_argument key name does not match identity |
| 116 | * @throw Pib::Error a key with the same name already exists |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 117 | */ |
| 118 | Key |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 119 | addKey(const uint8_t* key, size_t keyLen, const Name& keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 122 | * @brief Remove a key with @p keyName |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 123 | * @throw std::invalid_argument @p keyName does not match identity |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 124 | */ |
| 125 | void |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 126 | removeKey(const Name& keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 129 | * @brief Set an existing key with @p keyName as the default key. |
| 130 | * @throw std::invalid_argument @p keyName does not match identity |
| 131 | * @throw Pib::Error the key does not exist. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 132 | * @return The default key |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 133 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 134 | const Key& |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 135 | setDefaultKey(const Name& keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 136 | |
| 137 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 138 | * @brief Add a @p key of @p keyLen bytes with @p keyName and set it as the default key |
| 139 | * @throw std::invalid_argument @p keyName does not match identity |
| 140 | * @throw Pib::Error the key with the same name already exists. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 141 | * @return the default key |
| 142 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 143 | const Key& |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 144 | setDefaultKey(const uint8_t* key, size_t keyLen, const Name& keyName); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 145 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 146 | private: |
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 | * @brief Check the validity of the instance |
| 149 | * @return a shared_ptr when the instance is valid |
| 150 | * @throw std::domain_error the instance is invalid |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 151 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 152 | shared_ptr<detail::IdentityImpl> |
| 153 | lock() const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 154 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 155 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame^] | 156 | weak_ptr<detail::IdentityImpl> m_impl; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 159 | } // namespace pib |
| 160 | |
| 161 | using pib::Identity; |
| 162 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 163 | } // namespace security |
| 164 | } // namespace ndn |
| 165 | |
Alexander Afanasyev | 97709c0 | 2016-08-25 19:58:30 -0700 | [diff] [blame] | 166 | #endif // NDN_SECURITY_PIB_IDENTITY_HPP |