Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 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 | |
| 22 | #include "key.hpp" |
| 23 | #include "pib-impl.hpp" |
| 24 | #include "pib.hpp" |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | |
| 29 | Key::Key() |
| 30 | : m_hasDefaultCertificate(false) |
| 31 | , m_needRefreshCerts(false) |
| 32 | , m_impl(nullptr) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | Key::Key(const Name& identityName, const name::Component& keyId, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 37 | const v1::PublicKey& publicKey, shared_ptr<PibImpl> impl) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 38 | : m_id(identityName) |
| 39 | , m_keyId(keyId) |
| 40 | , m_key(publicKey) |
| 41 | , m_hasDefaultCertificate(false) |
| 42 | , m_needRefreshCerts(true) |
| 43 | , m_impl(impl) |
| 44 | { |
| 45 | validityCheck(); |
| 46 | |
| 47 | m_keyName = m_id; |
| 48 | m_keyName.append(m_keyId); |
| 49 | |
| 50 | m_impl->addIdentity(m_id); |
| 51 | m_impl->addKey(m_id, m_keyId, publicKey); |
| 52 | } |
| 53 | |
| 54 | Key::Key(const Name& identityName, const name::Component& keyId, |
| 55 | shared_ptr<PibImpl> impl) |
| 56 | : m_id(identityName) |
| 57 | , m_keyId(keyId) |
| 58 | , m_hasDefaultCertificate(false) |
| 59 | , m_needRefreshCerts(true) |
| 60 | , m_impl(impl) |
| 61 | { |
| 62 | validityCheck(); |
| 63 | |
| 64 | m_keyName = m_id; |
| 65 | m_keyName.append(m_keyId); |
| 66 | |
| 67 | m_key = m_impl->getKeyBits(m_id, m_keyId); |
| 68 | } |
| 69 | |
| 70 | const Name& |
| 71 | Key::getName() const |
| 72 | { |
| 73 | validityCheck(); |
| 74 | |
| 75 | return m_keyName; |
| 76 | } |
| 77 | |
| 78 | const Name& |
| 79 | Key::getIdentity() const |
| 80 | { |
| 81 | validityCheck(); |
| 82 | |
| 83 | return m_id; |
| 84 | } |
| 85 | |
| 86 | const name::Component& |
| 87 | Key::getKeyId() const |
| 88 | { |
| 89 | validityCheck(); |
| 90 | |
| 91 | return m_keyId; |
| 92 | } |
| 93 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 94 | const v1::PublicKey& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 95 | Key::getPublicKey() const |
| 96 | { |
| 97 | validityCheck(); |
| 98 | |
| 99 | return m_key; |
| 100 | } |
| 101 | |
| 102 | void |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 103 | Key::addCertificate(const v1::IdentityCertificate& certificate) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 104 | { |
| 105 | validityCheck(); |
| 106 | |
| 107 | if (!m_needRefreshCerts && |
| 108 | m_certificates.find(certificate.getName()) == m_certificates.end()) { |
| 109 | // if we have already loaded all the certificate, but the new certificate is not one of them |
| 110 | // the CertificateContainer should be refreshed |
| 111 | m_needRefreshCerts = true; |
| 112 | } |
| 113 | |
| 114 | m_impl->addCertificate(certificate); |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | Key::removeCertificate(const Name& certName) |
| 119 | { |
| 120 | validityCheck(); |
| 121 | |
| 122 | if (m_hasDefaultCertificate && m_defaultCertificate.getName() == certName) |
| 123 | m_hasDefaultCertificate = false; |
| 124 | |
| 125 | m_impl->removeCertificate(certName); |
| 126 | m_needRefreshCerts = true; |
| 127 | } |
| 128 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 129 | v1::IdentityCertificate |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 130 | Key::getCertificate(const Name& certName) const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 131 | { |
| 132 | validityCheck(); |
| 133 | |
| 134 | return m_impl->getCertificate(certName); |
| 135 | } |
| 136 | |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 137 | const CertificateContainer& |
| 138 | Key::getCertificates() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 139 | { |
| 140 | validityCheck(); |
| 141 | |
| 142 | if (m_needRefreshCerts) { |
Alexander Afanasyev | 66ca203 | 2015-12-04 13:17:02 -0800 | [diff] [blame] | 143 | m_certificates = CertificateContainer(m_impl->getCertificatesOfKey(m_id, m_keyId), m_impl); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 144 | m_needRefreshCerts = false; |
| 145 | } |
| 146 | |
| 147 | return m_certificates; |
| 148 | } |
| 149 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 150 | const v1::IdentityCertificate& |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 151 | Key::setDefaultCertificate(const Name& certName) |
| 152 | { |
| 153 | validityCheck(); |
| 154 | |
| 155 | m_defaultCertificate = m_impl->getCertificate(certName); |
| 156 | m_impl->setDefaultCertificateOfKey(m_id, m_keyId, certName); |
| 157 | m_hasDefaultCertificate = true; |
| 158 | return m_defaultCertificate; |
| 159 | } |
| 160 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 161 | const v1::IdentityCertificate& |
| 162 | Key::setDefaultCertificate(const v1::IdentityCertificate& certificate) |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 163 | { |
| 164 | addCertificate(certificate); |
| 165 | return setDefaultCertificate(certificate.getName()); |
| 166 | } |
| 167 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 168 | const v1::IdentityCertificate& |
Yingdi Yu | c820989 | 2015-06-19 17:47:56 -0700 | [diff] [blame] | 169 | Key::getDefaultCertificate() const |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 170 | { |
| 171 | validityCheck(); |
| 172 | |
| 173 | if (!m_hasDefaultCertificate) { |
| 174 | m_defaultCertificate = m_impl->getDefaultCertificateOfKey(m_id, m_keyId); |
| 175 | m_hasDefaultCertificate = true; |
| 176 | } |
| 177 | |
| 178 | return m_defaultCertificate; |
| 179 | } |
| 180 | |
| 181 | Key::operator bool() const |
| 182 | { |
| 183 | return !(this->operator!()); |
| 184 | } |
| 185 | |
| 186 | bool |
| 187 | Key::operator!() const |
| 188 | { |
| 189 | return (m_impl == nullptr); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | Key::validityCheck() const |
| 194 | { |
| 195 | if (m_impl == nullptr) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 196 | BOOST_THROW_EXCEPTION(std::domain_error("Invalid Key instance")); |
Yingdi Yu | b8f8b34 | 2015-04-27 11:06:42 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | } // namespace security |
| 200 | } // namespace ndn |