blob: 1a3025e948a496fa4e3fddbd49a58a6f8de1b966 [file] [log] [blame]
Saurab Dulal427e0122019-11-28 11:58:02 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -04002/*
Davide Pesaventod90338d2021-01-07 17:50:05 -05003 * Copyright (c) 2014-2021, The University of Memphis,
Saurab Dulal427e0122019-11-28 11:58:02 -06004 * 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 Afanasyev0ad01f32020-06-03 14:12:58 -040020 */
Saurab Dulal427e0122019-11-28 11:58:02 -060021
22#include "certificate-store.hpp"
23#include "conf-parameter.hpp"
24#include "logger.hpp"
25
26#include <ndn-cxx/util/io.hpp>
27
28namespace nlsr {
29namespace security {
30
31INIT_LOGGER(CertificateStore);
32
33CertificateStore::CertificateStore(ndn::Face& face, ConfParameter& confParam, Lsdb& lsdb)
34 : m_face(face)
35 , m_confParam(confParam)
Saurab Dulal427e0122019-11-28 11:58:02 -060036 , m_validator(m_confParam.getValidator())
Saurab Dulal427e0122019-11-28 11:58:02 -060037{
38 for (const auto& x: confParam.getIdCerts()) {
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040039 auto idCert = ndn::io::load<ndn::security::Certificate>(x);
Saurab Dulal427e0122019-11-28 11:58:02 -060040 insert(*idCert);
41 }
42
43 registerKeyPrefixes();
Ashlesh Gawande5d93aa52020-06-13 18:57:45 -070044
45 m_afterSegmentValidatedConnection = lsdb.afterSegmentValidatedSignal.connect(
46 [this] (const ndn::Data& data) { afterFetcherSignalEmitted(data); });
Saurab Dulal427e0122019-11-28 11:58:02 -060047}
48
49void
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040050CertificateStore::insert(const ndn::security::Certificate& certificate)
Saurab Dulal427e0122019-11-28 11:58:02 -060051{
52 m_certificates[certificate.getKeyName()] = certificate;
53 NLSR_LOG_TRACE("Certificate inserted successfully");
54}
55
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -040056const ndn::security::Certificate*
Saurab Dulal427e0122019-11-28 11:58:02 -060057CertificateStore::find(const ndn::Name& keyName) const
58{
59 auto it = m_certificates.find(keyName);
60 return it != m_certificates.end() ? &it->second : nullptr;
61}
62
63void
64CertificateStore::clear()
65{
66 m_certificates.clear();
67}
68
69void
70CertificateStore::setInterestFilter(const ndn::Name& prefix, bool loopback)
71{
72 m_face.setInterestFilter(ndn::InterestFilter(prefix).allowLoopback(loopback),
73 std::bind(&CertificateStore::onKeyInterest, this, _1, _2),
74 std::bind(&CertificateStore::onKeyPrefixRegSuccess, this, _1),
75 std::bind(&CertificateStore::registrationFailed, this, _1),
76 m_confParam.getSigningInfo(), ndn::nfd::ROUTE_FLAG_CAPTURE);
77}
78
79void
80CertificateStore::registerKeyPrefixes()
81{
82 std::vector<ndn::Name> prefixes;
83
84 // Router's NLSR certificate
85 ndn::Name nlsrKeyPrefix = m_confParam.getRouterPrefix();
86 nlsrKeyPrefix.append("nlsr");
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070087 nlsrKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -060088 prefixes.push_back(nlsrKeyPrefix);
89
90 // Router's certificate
91 ndn::Name routerKeyPrefix = m_confParam.getRouterPrefix();
Ashlesh Gawande7a231c02020-06-12 20:06:44 -070092 routerKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -060093 prefixes.push_back(routerKeyPrefix);
94
95 // Router's operator's certificate
96 ndn::Name operatorKeyPrefix = m_confParam.getNetwork();
97 operatorKeyPrefix.append(m_confParam.getSiteName());
98 operatorKeyPrefix.append(std::string("%C1.Operator"));
99 prefixes.push_back(operatorKeyPrefix);
100
101 // Router's site's certificate
102 ndn::Name siteKeyPrefix = m_confParam.getNetwork();
103 siteKeyPrefix.append(m_confParam.getSiteName());
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700104 siteKeyPrefix.append(ndn::security::Certificate::KEY_COMPONENT);
Saurab Dulal427e0122019-11-28 11:58:02 -0600105 prefixes.push_back(siteKeyPrefix);
106
107 // Start listening for interest of this router's NLSR certificate,
108 // router's certificate and site's certificate
109 for (const auto& i : prefixes) {
110 setInterestFilter(i);
111 }
112}
113
114void
115CertificateStore::onKeyInterest(const ndn::Name& name, const ndn::Interest& interest)
116{
117 NLSR_LOG_DEBUG("Got interest for certificate. Interest: " << interest.getName());
118
119 const auto* cert = find(interest.getName());
120
121 if (!cert) {
122 NLSR_LOG_TRACE("Certificate is not found for: " << interest);
123 return;
124 }
125 m_face.put(*cert);
126}
127
128void
129CertificateStore::onKeyPrefixRegSuccess(const ndn::Name& name)
130{
Davide Pesaventod90338d2021-01-07 17:50:05 -0500131 NLSR_LOG_DEBUG("KEY prefix: " << name << " registration is successful");
Saurab Dulal427e0122019-11-28 11:58:02 -0600132}
133
134void
135CertificateStore::registrationFailed(const ndn::Name& name)
136{
Davide Pesaventod90338d2021-01-07 17:50:05 -0500137 NLSR_LOG_ERROR("Failed to register prefix " << name);
138 NDN_THROW(std::runtime_error("Prefix registration failed"));
Saurab Dulal427e0122019-11-28 11:58:02 -0600139}
140
141void
142CertificateStore::publishCertFromCache(const ndn::Name& keyName)
143{
144 const auto* cert = m_validator.getUnverifiedCertCache().find(keyName);
145
146 if (cert) {
147 insert(*cert);
148 NLSR_LOG_TRACE(*cert);
Alexander Afanasyev0ad01f32020-06-03 14:12:58 -0400149 ndn::Name certName = ndn::security::extractKeyNameFromCertName(cert->getName());
Saurab Dulal427e0122019-11-28 11:58:02 -0600150 NLSR_LOG_TRACE("Setting interest filter for: " << certName);
151
152 setInterestFilter(certName);
153
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700154 const ndn::Name& keyLocatorName = cert->getSignatureInfo().getKeyLocator().getName();
155 if (cert->getKeyName() != keyLocatorName) {
156 publishCertFromCache(keyLocatorName);
Saurab Dulal427e0122019-11-28 11:58:02 -0600157 }
158 }
159 else {
160 // Happens for root cert
161 NLSR_LOG_TRACE("Cert for " << keyName << " was not found in the Validator's cache. ");
162 }
163}
164
165void
166CertificateStore::afterFetcherSignalEmitted(const ndn::Data& lsaSegment)
167{
Ashlesh Gawande7a231c02020-06-12 20:06:44 -0700168 const auto keyName = lsaSegment.getSignatureInfo().getKeyLocator().getName();
Saurab Dulal427e0122019-11-28 11:58:02 -0600169 if (!find(keyName)) {
170 NLSR_LOG_TRACE("Publishing certificate for: " << keyName);
171 publishCertFromCache(keyName);
172 }
173 else {
174 NLSR_LOG_TRACE("Certificate is already in the store: " << keyName);
175 }
176}
177
178} // namespace security
179} // namespace nlsr