certificate-store.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, The University of Memphis,
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/>.
20  */
21 
22 #include "certificate-store.hpp"
23 #include "conf-parameter.hpp"
24 #include "logger.hpp"
25 
26 #include <ndn-cxx/util/io.hpp>
27 #include <fstream>
28 
29 namespace nlsr {
30 namespace security {
31 
33 
34 CertificateStore::CertificateStore(ndn::Face& face, ConfParameter& confParam, Lsdb& lsdb)
35  : m_face(face)
36  , m_confParam(confParam)
37  , m_validator(m_confParam.getValidator())
38 {
39  for (const auto& certfile : confParam.getIdCerts()) {
40  std::ifstream ifs(certfile);
41  insert(ndn::io::loadTlv<ndn::security::Certificate>(ifs));
42  }
43 
44  registerKeyPrefixes();
45 
46  m_afterSegmentValidatedConnection = lsdb.afterSegmentValidatedSignal.connect(
47  [this] (const ndn::Data& data) { afterFetcherSignalEmitted(data); });
48 }
49 
50 void
51 CertificateStore::insert(const ndn::security::Certificate& certificate)
52 {
53  m_certificates[certificate.getKeyName()] = certificate;
54  NLSR_LOG_TRACE("Certificate inserted successfully");
55 }
56 
57 const ndn::security::Certificate*
58 CertificateStore::find(const ndn::Name& name) const
59 {
60  if (ndn::security::Certificate::isValidName(name)) {
61  return findByCertName(name);
62  }
63  return findByKeyName(name);
64 }
65 
66 const ndn::security::Certificate*
67 CertificateStore::findByKeyName(const ndn::Name& keyName) const
68 {
69  auto it = m_certificates.find(keyName);
70  return it != m_certificates.end() ? &it->second : nullptr;
71 }
72 
73 const ndn::security::Certificate*
74 CertificateStore::findByCertName(const ndn::Name& certName) const
75 {
76  auto found = findByKeyName(ndn::security::extractKeyNameFromCertName(certName));
77  if (found == nullptr || found->getName() != certName) {
78  return nullptr;
79  }
80  return found;
81 }
82 
83 void
84 CertificateStore::clear()
85 {
86  m_certificates.clear();
87 }
88 
89 void
90 CertificateStore::setInterestFilter(const ndn::Name& prefix, bool loopback)
91 {
92  m_face.setInterestFilter(ndn::InterestFilter(prefix).allowLoopback(loopback),
93  std::bind(&CertificateStore::onKeyInterest, this, _1, _2),
94  std::bind(&CertificateStore::onKeyPrefixRegSuccess, this, _1),
95  std::bind(&CertificateStore::registrationFailed, this, _1),
96  m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
97 }
98 
99 void
100 CertificateStore::registerKeyPrefixes()
101 {
102  std::vector<ndn::Name> prefixes;
103 
104  // Router's NLSR certificate
105  ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix();
106  nlsrKeyPrefix.append("nlsr");
107  nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
108  prefixes.push_back(nlsrKeyPrefix);
109 
110  // Router's certificate
111  ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix();
112  routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
113  prefixes.push_back(routerKeyPrefix);
114 
115  // Router's operator's certificate
116  ndn::Name operatorKeyPrefix = m_confParam.getNetwork();
117  operatorKeyPrefix.append(m_confParam.getSiteName());
118  operatorKeyPrefix.append(std::string("%C1.Operator"));
119  prefixes.push_back(operatorKeyPrefix);
120 
121  // Router's site's certificate
122  ndn::Name siteKeyPrefix = m_confParam.getNetwork();
123  siteKeyPrefix.append(m_confParam.getSiteName());
124  siteKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
125  prefixes.push_back(siteKeyPrefix);
126 
127  // Start listening for interest of this router's NLSR certificate,
128  // router's certificate and site's certificate
129  for (const auto& i : prefixes) {
130  setInterestFilter(i);
131  }
132 }
133 
134 void
135 CertificateStore::onKeyInterest(const ndn::Name&, const ndn::Interest& interest)
136 {
137  NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
138 
139  const auto* cert = find(interest.getName());
140 
141  if (!cert) {
142  NLSR_LOG_TRACE("Certificate is not found for: " << interest);
143  return;
144  }
145  m_face.put(*cert);
146 }
147 
148 void
149 CertificateStore::onKeyPrefixRegSuccess(const ndn::Name& name)
150 {
151  NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful");
152 }
153 
154 void
155 CertificateStore::registrationFailed(const ndn::Name& name)
156 {
157  NLSR_LOG_ERROR("Failed to register prefix " << name);
158  NDN_THROW(std::runtime_error("Prefix registration failed"));
159 }
160 
161 void
162 CertificateStore::publishCertFromCache(const ndn::Name& keyName)
163 {
164  const auto* cert = m_validator.getUnverifiedCertCache().find(keyName);
165 
166  if (cert) {
167  insert(*cert);
168  NLSR_LOG_TRACE(*cert);
169  ndn::Name certName = ndn::security::extractKeyNameFromCertName(cert->getName());
170  NLSR_LOG_TRACE("Setting interest filter for: " << certName);
171 
172  setInterestFilter(certName);
173 
174  const ndn::Name& keyLocatorName = cert->getSignatureInfo().getKeyLocator().getName();
175  if (cert->getKeyName() != keyLocatorName) {
176  publishCertFromCache(keyLocatorName);
177  }
178  }
179  else {
180  // Happens for root cert
181  NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
182  }
183 }
184 
185 void
187 {
188  const auto keyName = lsaSegment.getSignatureInfo().getKeyLocator().getName();
189  if (!find(keyName)) {
190  NLSR_LOG_TRACE("Publishing certificate for: " << keyName);
191  publishCertFromCache(keyName);
192  }
193  else {
194  NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
195  }
196 }
197 
198 } // namespace security
199 } // namespace nlsr
A class to house all the configuration parameters for NLSR.
const ndn::security::SigningInfo & getSigningInfo() const
const ndn::Name & getSiteName() const
const std::unordered_set< std::string > & getIdCerts() const
const ndn::Name & getNetwork() const
const ndn::Name & getRouterPrefix() const
ndn::util::Signal< Lsdb, ndn::Data > afterSegmentValidatedSignal
Definition: lsdb.hpp:325
Store certificates for names.
const ndn::security::Certificate * find(const ndn::Name &name) const
Find a certificate.
void insert(const ndn::security::Certificate &certificate)
void afterFetcherSignalEmitted(const ndn::Data &lsaSegment)
CertificateStore(ndn::Face &face, ConfParameter &confParam, Lsdb &lsdb)
void publishCertFromCache(const ndn::Name &keyName)
Retrieves the chain of certificates from Validator's cache and store them in Nlsr's own CertificateSt...
Copyright (c) 2014-2018, The University of Memphis, Regents of the University of California.
#define NLSR_LOG_DEBUG(x)
Definition: logger.hpp:38
#define INIT_LOGGER(name)
Definition: logger.hpp:35
#define NLSR_LOG_ERROR(x)
Definition: logger.hpp:41
#define NLSR_LOG_TRACE(x)
Definition: logger.hpp:37
Copyright (c) 2014-2020, The University of Memphis, Regents of the University of California.