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