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 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 50 | * If the key does not exist in the backend, it will be added. |
| 51 | * If a key with the same name already exists, it will be overwritten. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 52 | * |
| 53 | * @param keyName The name of the key. |
| 54 | * @param key The public key to add. |
| 55 | * @param keyLen The length of the key. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 56 | * @param pibImpl The Pib backend implementation. |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 57 | * @throw std::invalid_argument @p key is invalid. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 58 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 59 | 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] | 60 | |
| 61 | /** |
| 62 | * @brief Create a KeyImpl with @p keyName. |
| 63 | * |
| 64 | * @param keyName The name of the key. |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 65 | * @param pibImpl The Pib backend implementation. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 66 | * @throw Pib::Error the key does not exist. |
| 67 | */ |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 68 | KeyImpl(const Name& keyName, shared_ptr<PibImpl> pibImpl); |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 69 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 70 | /** |
| 71 | * @brief Get the name of the key. |
| 72 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 73 | const Name& |
| 74 | getName() const |
| 75 | { |
| 76 | return m_keyName; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @brief Get the name of the belonging identity. |
| 81 | */ |
| 82 | const Name& |
| 83 | getIdentity() const |
| 84 | { |
| 85 | return m_identity; |
| 86 | } |
| 87 | |
| 88 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 89 | * @brief Get key type. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 90 | */ |
| 91 | KeyType |
| 92 | getKeyType() const |
| 93 | { |
| 94 | return m_keyType; |
| 95 | } |
| 96 | |
| 97 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 98 | * @brief Get public key bits. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 99 | */ |
| 100 | const Buffer& |
| 101 | getPublicKey() const |
| 102 | { |
| 103 | return m_key; |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * @brief Add @p certificate. |
| 108 | * |
| 109 | * If no default certificate is set before, the new certificate will be set as the default |
| 110 | * certificate of the key. |
| 111 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 112 | * If a certificate with the same name (without implicit digest) already exists, it will |
| 113 | * be overwritten. |
Alexander Afanasyev | a10b2ff | 2017-01-30 12:44:15 -0800 | [diff] [blame] | 114 | * |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 115 | * @throw std::invalid_argument the certificate name does not match the key name. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 116 | */ |
| 117 | void |
| 118 | addCertificate(const v2::Certificate& certificate); |
| 119 | |
| 120 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 121 | * @brief Remove a certificate with @p certName. |
| 122 | * @throw std::invalid_argument @p certName does not match the key name. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 123 | */ |
| 124 | void |
| 125 | removeCertificate(const Name& certName); |
| 126 | |
| 127 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 128 | * @brief Get a certificate with @p certName. |
| 129 | * @throw std::invalid_argument @p certName does not match the key name. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 130 | * @throw Pib::Error the certificate does not exist. |
| 131 | */ |
| 132 | v2::Certificate |
| 133 | getCertificate(const Name& certName) const; |
| 134 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 135 | /** |
| 136 | * @brief Get all the certificates for this key. |
| 137 | */ |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 138 | const CertificateContainer& |
| 139 | getCertificates() const; |
| 140 | |
| 141 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 142 | * @brief Set an existing certificate with name @p certName as the default certificate. |
| 143 | * @throw std::invalid_argument @p certName does not match the key name. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 144 | * @throw Pib::Error the certificate does not exist. |
| 145 | * @return the default certificate |
| 146 | */ |
| 147 | const v2::Certificate& |
| 148 | setDefaultCertificate(const Name& certName); |
| 149 | |
| 150 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 151 | * @brief Add @p certificate and set it as the default certificate for this key. |
| 152 | * |
| 153 | * If a certificate with the same name (without implicit digest) already exists, it will |
| 154 | * be overwritten. |
| 155 | * |
| 156 | * @throw std::invalid_argument @p certificate does not match the key name. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 157 | * @return the default certificate |
| 158 | */ |
| 159 | const v2::Certificate& |
| 160 | setDefaultCertificate(const v2::Certificate& certificate); |
| 161 | |
| 162 | /** |
Davide Pesavento | 9285686 | 2017-05-15 21:35:08 -0400 | [diff] [blame] | 163 | * @brief Get the default certificate for this key. |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 164 | * @throw Pib::Error the default certificate does not exist. |
| 165 | */ |
| 166 | const v2::Certificate& |
| 167 | getDefaultCertificate() const; |
| 168 | |
| 169 | private: |
| 170 | Name m_identity; |
| 171 | Name m_keyName; |
| 172 | Buffer m_key; |
| 173 | KeyType m_keyType; |
| 174 | |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 175 | shared_ptr<PibImpl> m_pib; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 176 | |
| 177 | CertificateContainer m_certificates; |
Davide Pesavento | 50f6675 | 2017-05-15 20:57:12 -0400 | [diff] [blame] | 178 | mutable bool m_isDefaultCertificateLoaded; |
| 179 | mutable v2::Certificate m_defaultCertificate; |
Yingdi Yu | cbe72b0 | 2015-11-25 17:35:37 -0800 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | } // namespace detail |
| 183 | } // namespace pib |
| 184 | } // namespace security |
| 185 | } // namespace ndn |
| 186 | |
| 187 | #endif // NDN_SECURITY_PIB_DETAIL_KEY_IMPL_HPP |