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_KEY_HPP |
| 23 | #define NDN_SECURITY_PIB_KEY_HPP |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 24 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 25 | #include "certificate-container.hpp" |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 26 | #include "../security-common.hpp" |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 31 | namespace v2 { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 32 | class KeyChain; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 33 | } // namespace v2 |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 34 | |
| 35 | namespace pib { |
| 36 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 37 | namespace detail { |
| 38 | class KeyImpl; |
| 39 | } // namespace detail |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 42 | * @brief A frontend handle of a key instance |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 43 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 44 | * Key is at the second level in PIB's Identity-Key-Certificate hierarchy. A Key has a Name |
| 45 | * (identity + "KEY" + keyId), and contains one or more certificates, one of which is set as |
| 46 | * the default certificate of this key. A certificate can be directly accessed from a Key |
| 47 | * object. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 48 | */ |
| 49 | class Key |
| 50 | { |
| 51 | public: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 52 | /** |
| 53 | * @brief Default Constructor |
| 54 | * |
| 55 | * Key created using this default constructor is just a place holder. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 56 | * It can obtain an actual instance from Identity::getKey(...). A typical |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 57 | * usage would be for exception handling: |
| 58 | * |
| 59 | * Key key; |
| 60 | * try { |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 61 | * key = identity.getKey(...); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 62 | * } |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 63 | * catch (const Pib::Error&) { |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 64 | * ... |
| 65 | * } |
| 66 | * |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 67 | * A Key instance created using this constructor is invalid. Calling a |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 68 | * member method on an invalid Key instance may cause an std::domain_error. |
| 69 | */ |
| 70 | Key(); |
| 71 | |
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 | * @brief Create a Key with a backend implementation @p impl. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 74 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 75 | * This method should only be used by KeyContainer. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 76 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 77 | explicit |
| 78 | Key(weak_ptr<detail::KeyImpl> impl); |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 79 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 80 | /* |
| 81 | * @brief Get key name. |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 82 | */ |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 83 | const Name& |
| 84 | getName() const; |
| 85 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 86 | /** |
| 87 | * @brief Get the name of the belonging identity. |
| 88 | */ |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 89 | const Name& |
| 90 | getIdentity() const; |
| 91 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 92 | /** |
| 93 | * @brief Get key type. |
| 94 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 95 | KeyType |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 96 | getKeyType() const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 97 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 98 | /** |
| 99 | * @brief Get public key bits. |
| 100 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 101 | const Buffer& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 102 | getPublicKey() const; |
| 103 | |
| 104 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 105 | * @brief Get a certificate with @p certName |
| 106 | * @throw std::invalid_argument @p certName does not match key name |
| 107 | * @throw Pib::Error the certificate does not exist. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 108 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 109 | v2::Certificate |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 110 | getCertificate(const Name& certName) const; |
| 111 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 112 | /** |
| 113 | * @brief Get all certificates for this key. |
| 114 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 115 | const CertificateContainer& |
| 116 | getCertificates() const; |
| 117 | |
| 118 | /** |
| 119 | * @brief Get the default certificate for this Key. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 120 | * @throw Pib::Error the default certificate does not exist. |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 121 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 122 | const v2::Certificate& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 123 | getDefaultCertificate() const; |
| 124 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 125 | /** |
| 126 | * @brief Check if the Key instance is valid. |
| 127 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 128 | operator bool() const; |
| 129 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 130 | /** |
| 131 | * @brief Check if the Key instance is invalid. |
| 132 | */ |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 133 | bool |
| 134 | operator!() const; |
| 135 | |
| 136 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 137 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 138 | * @brief Add @p certificate. |
| 139 | * @throw std::invalid_argument certificate name does not match key name |
| 140 | * @throw Pib::Error a certificate with the same name already exists |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 141 | */ |
| 142 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 143 | addCertificate(const v2::Certificate& certificate) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 146 | * @brief Remove a certificate with @p certName |
| 147 | * @throw std::invalid_argument @p certName does not match key name |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 148 | */ |
| 149 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 150 | removeCertificate(const Name& certName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 153 | * @brief Set an existing certificate with @p certName as the default certificate |
| 154 | * @throw std::invalid_argument @p certName does not match key name |
| 155 | * @throw Pib::Error the certificate does not exist. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 156 | * @return the default certificate |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 157 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 158 | const v2::Certificate& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 159 | setDefaultCertificate(const Name& certName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 162 | * @brief Add @p certificate and set it as the default certificate of the key |
| 163 | * @throw std::invalid_argument @p certificate does not match key name |
| 164 | * @throw Pib::Error the certificate with the same name already exists. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 165 | * @return the default certificate |
| 166 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 167 | const v2::Certificate& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 168 | setDefaultCertificate(const v2::Certificate& certificate) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 169 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 170 | private: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 171 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 172 | * @brief Check the validity of the instance |
| 173 | * @return a shared_ptr when the instance is valid |
| 174 | * @throw std::domain_error the instance is invalid |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 175 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 176 | shared_ptr<detail::KeyImpl> |
| 177 | lock() const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 178 | |
| 179 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 180 | weak_ptr<detail::KeyImpl> m_impl; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 181 | |
| 182 | friend class v2::KeyChain; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 183 | }; |
| 184 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 185 | } // namespace pib |
| 186 | |
| 187 | using pib::Key; |
| 188 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 189 | namespace v2 { |
| 190 | |
| 191 | /** |
| 192 | * @brief Construct key name based on the appropriate naming conventions |
| 193 | */ |
| 194 | Name |
| 195 | constructKeyName(const Name& identity, const name::Component& keyId); |
| 196 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 197 | /** |
| 198 | * @brief Check if @p keyName follow the naming conventions for the key name |
| 199 | */ |
| 200 | bool |
| 201 | isValidKeyName(const Name& keyName); |
| 202 | |
| 203 | /** |
| 204 | * @brief Extract identity namespace from the key name @p keyName |
| 205 | */ |
| 206 | Name |
| 207 | extractIdentityFromKeyName(const Name& keyName); |
| 208 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 209 | } // namespace v2 |
| 210 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 211 | } // namespace security |
| 212 | } // namespace ndn |
| 213 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 214 | #endif // NDN_SECURITY_PIB_KEY_HPP |