Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2021, The University of Memphis, |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 4 | * Regents of the University of California, |
| 5 | * Arizona Board of Regents. |
| 6 | * |
| 7 | * This file is part of NLSR (Named-data Link State Routing). |
| 8 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 9 | * |
| 10 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 11 | * of the GNU General Public License as published by the Free Software Foundation, |
| 12 | * either version 3 of the License, or (at your option) any later version. |
| 13 | * |
| 14 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 15 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE. See the GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along with |
| 19 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 20 | */ |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 21 | |
| 22 | #include "certificate-store.hpp" |
| 23 | #include "conf-parameter.hpp" |
| 24 | #include "logger.hpp" |
| 25 | |
| 26 | #include <ndn-cxx/util/io.hpp> |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame^] | 27 | #include <fstream> |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 28 | |
| 29 | namespace nlsr { |
| 30 | namespace security { |
| 31 | |
| 32 | INIT_LOGGER(CertificateStore); |
| 33 | |
| 34 | CertificateStore::CertificateStore(ndn::Face& face, ConfParameter& confParam, Lsdb& lsdb) |
| 35 | : m_face(face) |
| 36 | , m_confParam(confParam) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 37 | , m_validator(m_confParam.getValidator()) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 38 | { |
Davide Pesavento | 7bc3d43 | 2021-10-25 21:08:04 -0400 | [diff] [blame^] | 39 | for (const auto& certfile : confParam.getIdCerts()) { |
| 40 | std::ifstream ifs(certfile); |
| 41 | insert(ndn::io::loadTlv<ndn::security::Certificate>(ifs)); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | registerKeyPrefixes(); |
Ashlesh Gawande | 5d93aa5 | 2020-06-13 18:57:45 -0700 | [diff] [blame] | 45 | |
| 46 | m_afterSegmentValidatedConnection = lsdb.afterSegmentValidatedSignal.connect( |
| 47 | [this] (const ndn::Data& data) { afterFetcherSignalEmitted(data); }); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | void |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 51 | CertificateStore::insert(const ndn::security::Certificate& certificate) |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 52 | { |
| 53 | m_certificates[certificate.getKeyName()] = certificate; |
| 54 | NLSR_LOG_TRACE("Certificate inserted successfully"); |
| 55 | } |
| 56 | |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 57 | const ndn::security::Certificate* |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 58 | CertificateStore::find(const ndn::Name& keyName) const |
| 59 | { |
| 60 | auto it = m_certificates.find(keyName); |
| 61 | return it != m_certificates.end() ? &it->second : nullptr; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | CertificateStore::clear() |
| 66 | { |
| 67 | m_certificates.clear(); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | CertificateStore::setInterestFilter(const ndn::Name& prefix, bool loopback) |
| 72 | { |
| 73 | m_face.setInterestFilter(ndn::InterestFilter(prefix).allowLoopback(loopback), |
| 74 | std::bind(&CertificateStore::onKeyInterest, this, _1, _2), |
| 75 | std::bind(&CertificateStore::onKeyPrefixRegSuccess, this, _1), |
| 76 | std::bind(&CertificateStore::registrationFailed, this, _1), |
| 77 | m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE); |
| 78 | } |
| 79 | |
| 80 | void |
| 81 | CertificateStore::registerKeyPrefixes() |
| 82 | { |
| 83 | std::vector<ndn::Name> prefixes; |
| 84 | |
| 85 | // Router's NLSR certificate |
| 86 | ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix(); |
| 87 | nlsrKeyPrefix.append("nlsr"); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 88 | nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 89 | prefixes.push_back(nlsrKeyPrefix); |
| 90 | |
| 91 | // Router's certificate |
| 92 | ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix(); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 93 | routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 94 | prefixes.push_back(routerKeyPrefix); |
| 95 | |
| 96 | // Router's operator's certificate |
| 97 | ndn::Name operatorKeyPrefix = m_confParam.getNetwork(); |
| 98 | operatorKeyPrefix.append(m_confParam.getSiteName()); |
| 99 | operatorKeyPrefix.append(std::string("%C1.Operator")); |
| 100 | prefixes.push_back(operatorKeyPrefix); |
| 101 | |
| 102 | // Router's site's certificate |
| 103 | ndn::Name siteKeyPrefix = m_confParam.getNetwork(); |
| 104 | siteKeyPrefix.append(m_confParam.getSiteName()); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 105 | siteKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 106 | prefixes.push_back(siteKeyPrefix); |
| 107 | |
| 108 | // Start listening for interest of this router's NLSR certificate, |
| 109 | // router's certificate and site's certificate |
| 110 | for (const auto& i : prefixes) { |
| 111 | setInterestFilter(i); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void |
| 116 | CertificateStore::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest) |
| 117 | { |
| 118 | NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName()); |
| 119 | |
| 120 | const auto* cert = find(interest.getName()); |
| 121 | |
| 122 | if (!cert) { |
| 123 | NLSR_LOG_TRACE("Certificate is not found for: " << interest); |
| 124 | return; |
| 125 | } |
| 126 | m_face.put(*cert); |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | CertificateStore::onKeyPrefixRegSuccess(const ndn::Name& name) |
| 131 | { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 132 | NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful"); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
| 136 | CertificateStore::registrationFailed(const ndn::Name& name) |
| 137 | { |
Davide Pesavento | d90338d | 2021-01-07 17:50:05 -0500 | [diff] [blame] | 138 | NLSR_LOG_ERROR("Failed to register prefix " << name); |
| 139 | NDN_THROW(std::runtime_error("Prefix registration failed")); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | void |
| 143 | CertificateStore::publishCertFromCache(const ndn::Name& keyName) |
| 144 | { |
| 145 | const auto* cert = m_validator.getUnverifiedCertCache().find(keyName); |
| 146 | |
| 147 | if (cert) { |
| 148 | insert(*cert); |
| 149 | NLSR_LOG_TRACE(*cert); |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 150 | ndn::Name certName = ndn::security::extractKeyNameFromCertName(cert->getName()); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 151 | NLSR_LOG_TRACE("Setting interest filter for: " << certName); |
| 152 | |
| 153 | setInterestFilter(certName); |
| 154 | |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 155 | const ndn::Name& keyLocatorName = cert->getSignatureInfo().getKeyLocator().getName(); |
| 156 | if (cert->getKeyName() != keyLocatorName) { |
| 157 | publishCertFromCache(keyLocatorName); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | else { |
| 161 | // Happens for root cert |
| 162 | NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. "); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | CertificateStore::afterFetcherSignalEmitted(const ndn::Data& lsaSegment) |
| 168 | { |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 169 | const auto keyName = lsaSegment.getSignatureInfo().getKeyLocator().getName(); |
Saurab Dulal | 427e012 | 2019-11-28 11:58:02 -0600 | [diff] [blame] | 170 | if (!find(keyName)) { |
| 171 | NLSR_LOG_TRACE("Publishing certificate for: " << keyName); |
| 172 | publishCertFromCache(keyName); |
| 173 | } |
| 174 | else { |
| 175 | NLSR_LOG_TRACE("Certificate is already in the store: " << keyName); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | } // namespace security |
| 180 | } // namespace nlsr |