Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
| 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 | |
| 22 | #ifndef NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP |
| 23 | #define NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP |
| 24 | |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 25 | #include "../../security-common.hpp" |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 26 | #include "../certificate-container.hpp" |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace security { |
| 30 | namespace pib { |
| 31 | |
| 32 | class PibImpl; |
| 33 | |
| 34 | namespace detail { |
| 35 | |
| 36 | /** |
| 37 | * @brief Backend instance of Key |
| 38 | * |
| 39 | * An Key has only one backend instance, but may have multiple frontend handles. |
| 40 | * Each frontend handle is associated with the only one backend KeyImpl. |
| 41 | * |
| 42 | * @throw PibImpl::Error when underlying implementation has non-semantic error. |
| 43 | */ |
| 44 | class KeyImpl : noncopyable |
| 45 | { |
| 46 | public: |
| 47 | /** |
| 48 | * @brief Create a KeyImpl with @p keyName. |
| 49 | * |
| 50 | * If the key does not exist in the backend, create it in backend. |
| 51 | * |
| 52 | * @param keyName The name of the key. |
| 53 | * @param key The public key to add. |
| 54 | * @param keyLen The length of the key. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 55 | * @param pibImpl The Pib backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 56 | * @throw Pib::Error a key with the same @p keyName already exists. |
| 57 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 58 | KeyImpl(const Name& keyName, const uint8_t* key, size_t keyLen, shared_ptr<PibImpl> pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * @brief Create a KeyImpl with @p keyName. |
| 62 | * |
| 63 | * @param keyName The name of the key. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 64 | * @param pibImpl The Pib backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 65 | * @throw Pib::Error the key does not exist. |
| 66 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 67 | KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 68 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 69 | /** |
| 70 | * @brief Get the name of the key. |
| 71 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 72 | const Name& |
| 73 | getName() const |
| 74 | { |
| 75 | return m_keyName; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @brief Get the name of the belonging identity. |
| 80 | */ |
| 81 | const Name& |
| 82 | getIdentity() const |
| 83 | { |
| 84 | return m_identity; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @brief Get key type |
| 89 | */ |
| 90 | KeyType |
| 91 | getKeyType() const |
| 92 | { |
| 93 | return m_keyType; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @brief Get public key bits |
| 98 | */ |
| 99 | const Buffer& |
| 100 | getPublicKey() const |
| 101 | { |
| 102 | return m_key; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @brief Add @p certificate. |
| 107 | * |
| 108 | * If no default certificate is set before, the new certificate will be set as the default |
| 109 | * certificate of the key. |
| 110 | * |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 111 | * If a certificate with the same name (without implicit digest) already exists, overwrite |
| 112 | * the certificate. |
| 113 | * |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 114 | * @throw std::invalid_argument certificate name does not match key name |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 115 | */ |
| 116 | void |
| 117 | addCertificate(const v2::Certificate& certificate); |
| 118 | |
| 119 | /** |
| 120 | * @brief Remove a certificate with @p certName |
| 121 | * @throw std::invalid_argument @p certName does not match key name |
| 122 | */ |
| 123 | void |
| 124 | removeCertificate(const Name& certName); |
| 125 | |
| 126 | /** |
| 127 | * @brief Get a certificate with @p certName |
| 128 | * @throw std::invalid_argument @p certName does not match key name |
| 129 | * @throw Pib::Error the certificate does not exist. |
| 130 | */ |
| 131 | v2::Certificate |
| 132 | getCertificate(const Name& certName) const; |
| 133 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 134 | /** |
| 135 | * @brief Get all the certificates for this key. |
| 136 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 137 | const CertificateContainer& |
| 138 | getCertificates() const; |
| 139 | |
| 140 | /** |
| 141 | * @brief Set an existing one with @p certName as the default certificate |
| 142 | * @throw std::invalid_argument @p certName does not match key name |
| 143 | * @throw Pib::Error the certificate does not exist. |
| 144 | * @return the default certificate |
| 145 | */ |
| 146 | const v2::Certificate& |
| 147 | setDefaultCertificate(const Name& certName); |
| 148 | |
| 149 | /** |
| 150 | * @brief Add @p certificate and set it as the default certificate of the key |
| 151 | * @throw std::invalid_argument @p certificate does not match key name |
| 152 | * @throw Pib::Error the certificate with the same name already exists. |
| 153 | * @return the default certificate |
| 154 | */ |
| 155 | const v2::Certificate& |
| 156 | setDefaultCertificate(const v2::Certificate& certificate); |
| 157 | |
| 158 | /** |
| 159 | * @brief Get the default certificate for this Key. |
| 160 | * @throw Pib::Error the default certificate does not exist. |
| 161 | */ |
| 162 | const v2::Certificate& |
| 163 | getDefaultCertificate() const; |
| 164 | |
| 165 | private: |
| 166 | Name m_identity; |
| 167 | Name m_keyName; |
| 168 | Buffer m_key; |
| 169 | KeyType m_keyType; |
| 170 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 171 | shared_ptr<PibImpl> m_pib; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 172 | |
| 173 | CertificateContainer m_certificates; |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame^] | 174 | mutable bool m_isDefaultCertificateLoaded; |
| 175 | mutable v2::Certificate m_defaultCertificate; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | } // namespace detail |
| 179 | } // namespace pib |
| 180 | } // namespace security |
| 181 | } // namespace ndn |
| 182 | |
| 183 | #endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP |