Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [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 | #include "trust-anchor-group.hpp" |
| 23 | |
| 24 | #include "util/io.hpp" |
| 25 | #include "util/logger.hpp" |
| 26 | |
| 27 | #include <set> |
| 28 | #include <boost/filesystem.hpp> |
| 29 | #include <boost/range/adaptor/map.hpp> |
| 30 | #include <boost/range/algorithm/copy.hpp> |
| 31 | #include <boost/range/iterator_range.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace security { |
| 35 | namespace v2 { |
| 36 | |
| 37 | NDN_LOG_INIT(ndn.security.v2.TrustAnchorGroup); |
| 38 | |
| 39 | namespace fs = boost::filesystem; |
| 40 | |
| 41 | TrustAnchorGroup::TrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id) |
| 42 | : m_certs(certContainer) |
| 43 | , m_id(id) |
| 44 | { |
| 45 | } |
| 46 | |
| 47 | size_t |
| 48 | TrustAnchorGroup::size() const |
| 49 | { |
| 50 | return m_anchorNames.size(); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | TrustAnchorGroup::refresh() |
| 55 | { |
| 56 | // base method does nothing |
| 57 | } |
| 58 | |
| 59 | ////////////// |
| 60 | |
| 61 | StaticTrustAnchorGroup::StaticTrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id) |
| 62 | : TrustAnchorGroup(certContainer, id) |
| 63 | { |
| 64 | } |
| 65 | |
| 66 | void |
| 67 | StaticTrustAnchorGroup::add(Certificate&& cert) |
| 68 | { |
| 69 | if (m_anchorNames.count(cert.getName()) != 0) { |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | m_anchorNames.insert(cert.getName()); |
| 74 | m_certs.add(std::move(cert)); |
| 75 | } |
| 76 | |
| 77 | void |
| 78 | StaticTrustAnchorGroup::remove(const Name& certName) |
| 79 | { |
| 80 | m_anchorNames.erase(certName); |
| 81 | m_certs.remove(certName); |
| 82 | } |
| 83 | |
| 84 | ///////////// |
| 85 | |
| 86 | DynamicTrustAnchorGroup::DynamicTrustAnchorGroup(CertContainerInterface& certContainer, const std::string& id, |
| 87 | const boost::filesystem::path& path, |
| 88 | time::nanoseconds refreshPeriod, bool isDir) |
| 89 | : TrustAnchorGroup(certContainer, id) |
| 90 | , m_isDir(isDir) |
| 91 | , m_path(path) |
| 92 | , m_refreshPeriod(refreshPeriod) |
| 93 | { |
| 94 | if (refreshPeriod <= time::nanoseconds::zero()) { |
| 95 | BOOST_THROW_EXCEPTION(std::runtime_error("Refresh period for the dynamic group must be positive")); |
| 96 | } |
| 97 | |
| 98 | refresh(); |
| 99 | } |
| 100 | |
| 101 | void |
| 102 | DynamicTrustAnchorGroup::refresh() |
| 103 | { |
| 104 | if (m_expireTime > time::steady_clock::now()) { |
| 105 | return; |
| 106 | } |
| 107 | m_expireTime = time::steady_clock::now() + m_refreshPeriod; |
| 108 | NDN_LOG_TRACE("Reloading dynamic trust anchor group"); |
| 109 | |
| 110 | std::set<Name> oldAnchorNames = m_anchorNames; |
| 111 | |
| 112 | auto loadCert = [this, &oldAnchorNames] (const fs::path& file) { |
| 113 | auto cert = io::load<Certificate>(file.string()); |
| 114 | if (cert != nullptr) { |
| 115 | if (m_anchorNames.count(cert->getName()) == 0) { |
| 116 | m_anchorNames.insert(cert->getName()); |
| 117 | m_certs.add(std::move(*cert)); |
| 118 | } |
| 119 | else { |
| 120 | oldAnchorNames.erase(cert->getName()); |
| 121 | } |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | if (!m_isDir) { |
| 126 | loadCert(m_path); |
| 127 | } |
| 128 | else { |
| 129 | if (fs::exists(m_path)) { |
| 130 | std::for_each(fs::directory_iterator(m_path), fs::directory_iterator(), loadCert); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // remove old certs |
| 135 | for (const auto& oldAnchorName : oldAnchorNames) { |
| 136 | m_anchorNames.erase(oldAnchorName); |
| 137 | m_certs.remove(oldAnchorName); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } // namespace v2 |
| 142 | } // namespace security |
| 143 | } // namespace ndn |