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_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 | |
Davide Pesavento | bdcedf4 | 2017-10-15 14:56:28 -0400 | [diff] [blame^] | 80 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 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 | */ |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 128 | explicit |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 129 | operator bool() const; |
| 130 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 131 | NDN_CXX_PUBLIC_WITH_TESTS_ELSE_PRIVATE: // write operations should be private |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 132 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 133 | * @brief Add @p certificate. |
| 134 | * @throw std::invalid_argument certificate name does not match key name |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 135 | * |
| 136 | * If a certificate with the same name (without implicit digest) already exists, overwrite |
| 137 | * the certificate. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 138 | */ |
| 139 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 140 | addCertificate(const v2::Certificate& certificate) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 143 | * @brief Remove a certificate with @p certName |
| 144 | * @throw std::invalid_argument @p certName does not match key name |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 145 | */ |
| 146 | void |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 147 | removeCertificate(const Name& certName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 148 | |
| 149 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 150 | * @brief Set an existing certificate with @p certName as the default certificate |
| 151 | * @throw std::invalid_argument @p certName does not match key name |
| 152 | * @throw Pib::Error the certificate does not exist. |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 153 | * @return the default certificate |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 154 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 155 | const v2::Certificate& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 156 | setDefaultCertificate(const Name& certName) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 157 | |
| 158 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 159 | * @brief Add @p certificate and set it as the default certificate of the key |
| 160 | * @throw std::invalid_argument @p certificate does not match key name |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 161 | * @return the default certificate |
| 162 | */ |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 163 | const v2::Certificate& |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 164 | setDefaultCertificate(const v2::Certificate& certificate) const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 165 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 166 | private: |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 167 | /** |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 168 | * @brief Check the validity of the instance |
| 169 | * @return a shared_ptr when the instance is valid |
| 170 | * @throw std::domain_error the instance is invalid |
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 | shared_ptr<detail::KeyImpl> |
| 173 | lock() const; |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 174 | |
| 175 | private: |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 176 | weak_ptr<detail::KeyImpl> m_impl; |
Yingdi Yu | fe4733a | 2015-10-22 14:24:12 -0700 | [diff] [blame] | 177 | |
| 178 | friend class v2::KeyChain; |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 179 | friend bool operator!=(const Key&, const Key&); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Junxiao Shi | 5759be3 | 2017-10-15 00:00:52 +0000 | [diff] [blame] | 182 | bool |
| 183 | operator!=(const Key& lhs, const Key& rhs); |
| 184 | |
| 185 | inline bool |
| 186 | operator==(const Key& lhs, const Key& rhs) |
| 187 | { |
| 188 | return !(lhs != rhs); |
| 189 | } |
| 190 | |
| 191 | std::ostream& |
| 192 | operator<<(std::ostream& os, const Key& key); |
| 193 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 194 | } // namespace pib |
| 195 | |
| 196 | using pib::Key; |
| 197 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 198 | namespace v2 { |
| 199 | |
| 200 | /** |
| 201 | * @brief Construct key name based on the appropriate naming conventions |
| 202 | */ |
| 203 | Name |
| 204 | constructKeyName(const Name& identity, const name::Component& keyId); |
| 205 | |
Yingdi Yu | 6ee2d36 | 2015-07-16 21:48:05 -0700 | [diff] [blame] | 206 | /** |
| 207 | * @brief Check if @p keyName follow the naming conventions for the key name |
| 208 | */ |
| 209 | bool |
| 210 | isValidKeyName(const Name& keyName); |
| 211 | |
| 212 | /** |
| 213 | * @brief Extract identity namespace from the key name @p keyName |
| 214 | */ |
| 215 | Name |
| 216 | extractIdentityFromKeyName(const Name& keyName); |
| 217 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 218 | } // namespace v2 |
| 219 | |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 220 | } // namespace security |
| 221 | } // namespace ndn |
| 222 | |
Yingdi Yu | 0b60e7a | 2015-07-16 21:05:11 -0700 | [diff] [blame] | 223 | #endif // NDN_SECURITY_PIB_KEY_HPP |