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