Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "rrset-factory.hpp" |
| 21 | #include "mgmt/management-tool.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 22 | #include "util/cert-helper.hpp" |
| 23 | |
| 24 | #include <ndn-cxx/security/signing-helpers.hpp> |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/algorithm/string/join.hpp> |
| 27 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
| 30 | |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 31 | RrsetFactory::RrsetFactory(const boost::filesystem::path& dbFile, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 32 | const Name& zoneName, |
| 33 | KeyChain& keyChain, |
| 34 | const Name& inputDskCertName) |
| 35 | : m_keyChain(keyChain) |
Yumin Xia | b87b9d9 | 2016-11-14 23:41:25 -0800 | [diff] [blame] | 36 | , m_dbFile(dbFile.string()) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 37 | , m_zone(zoneName) |
| 38 | , m_dskCertName(inputDskCertName) |
| 39 | , m_checked(false) |
| 40 | { |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 41 | Name identityName = Name(zoneName).append(label::NDNS_ITERATIVE_QUERY); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 42 | if (m_dskCertName == DEFAULT_CERT) { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 43 | m_dskName = CertHelper::getDefaultKeyNameOfIdentity(m_keyChain, identityName); |
| 44 | m_dskCertName = CertHelper::getDefaultCertificateNameOfIdentity(m_keyChain, identityName); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | RrsetFactory::checkZoneKey() |
| 50 | { |
| 51 | onlyCheckZone(); |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 52 | Name zoneIdentityName = Name(m_zone.getName()).append(label::NDNS_ITERATIVE_QUERY); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 53 | if (m_dskCertName != DEFAULT_CERT && |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 54 | !matchCertificate(m_dskCertName, zoneIdentityName)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 55 | NDN_THROW(Error("Cannot verify certificate")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | RrsetFactory::onlyCheckZone() |
| 61 | { |
| 62 | if (m_checked) { |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 63 | return; |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 64 | } |
| 65 | m_checked = true; |
| 66 | |
| 67 | DbMgr dbMgr(m_dbFile); |
| 68 | const Name& zoneName = m_zone.getName(); |
| 69 | if (!dbMgr.find(m_zone)) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 70 | NDN_THROW(Error(zoneName.toUri() + " is not presented in the NDNS db")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 74 | std::pair<Rrset, Name> |
| 75 | RrsetFactory::generateBaseRrset(const Name& label, |
| 76 | const name::Component& type, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 77 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 78 | const time::seconds& ttl) |
| 79 | { |
| 80 | Rrset rrset(&m_zone); |
| 81 | |
| 82 | rrset.setLabel(label); |
| 83 | rrset.setType(type); |
| 84 | rrset.setTtl(ttl); |
| 85 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 86 | Name name; |
| 87 | name.append(m_zone.getName()) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 88 | .append(label::NDNS_ITERATIVE_QUERY) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 89 | .append(label) |
| 90 | .append(type); |
| 91 | |
| 92 | if (version != VERSION_USE_UNIX_TIMESTAMP) { |
| 93 | name.append(name::Component::fromVersion(version)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 94 | } |
| 95 | else { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 96 | name.appendVersion(); |
| 97 | } |
| 98 | |
| 99 | rrset.setVersion(name.get(-1)); |
| 100 | |
| 101 | return std::make_pair(rrset, name); |
| 102 | } |
| 103 | |
| 104 | bool |
| 105 | RrsetFactory::matchCertificate(const Name& certName, const Name& identity) |
| 106 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 107 | try { |
| 108 | CertHelper::getCertificate(m_keyChain, identity, certName); |
| 109 | return true; |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 110 | } catch (const ndn::security::Pib::Error&) { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 111 | return false; |
| 112 | } |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | Rrset |
| 116 | RrsetFactory::generateNsRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 117 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 118 | time::seconds ttl, |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 119 | std::vector<Name> delegations) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 120 | { |
| 121 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 122 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | if (ttl == DEFAULT_RR_TTL) |
| 126 | ttl = m_zone.getTtl(); |
| 127 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 128 | std::pair<Rrset, Name> rrsetAndName = generateBaseRrset(label, label::NS_RR_TYPE, version, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 129 | const Name& name = rrsetAndName.second; |
| 130 | Rrset& rrset = rrsetAndName.first; |
| 131 | |
| 132 | Link link(name); |
Junxiao Shi | 81e9876 | 2022-01-11 18:17:24 +0000 | [diff] [blame] | 133 | link.setDelegationList(std::move(delegations)); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 134 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 135 | setContentType(link, NDNS_LINK, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 136 | sign(link); |
| 137 | rrset.setData(link.wireEncode()); |
| 138 | |
| 139 | return rrset; |
| 140 | } |
| 141 | |
| 142 | Rrset |
| 143 | RrsetFactory::generateTxtRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 144 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 145 | time::seconds ttl, |
| 146 | const std::vector<std::string>& strings) |
| 147 | { |
| 148 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 149 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if (ttl == DEFAULT_RR_TTL) |
| 153 | ttl = m_zone.getTtl(); |
| 154 | |
| 155 | Name name; |
| 156 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 157 | std::tie(rrset, name) = generateBaseRrset(label, label::TXT_RR_TYPE, version, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 158 | |
| 159 | std::vector<Block> rrs; |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 160 | rrs.reserve(strings.size()); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 161 | for (const auto& item : strings) { |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 162 | rrs.push_back(makeBinaryBlock(ndns::tlv::RrData, item.data(), item.size())); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | Data data(name); |
| 166 | data.setContent(wireEncode(rrs)); |
| 167 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 168 | setContentType(data, NDNS_RESP, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 169 | sign(data); |
| 170 | rrset.setData(data.wireEncode()); |
| 171 | |
| 172 | return rrset; |
| 173 | } |
| 174 | |
| 175 | Rrset |
| 176 | RrsetFactory::generateCertRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 177 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 178 | time::seconds ttl, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 179 | const ndn::security::Certificate& cert) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 180 | { |
| 181 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 182 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | if (ttl == DEFAULT_RR_TTL) |
| 186 | ttl = m_zone.getTtl(); |
| 187 | |
| 188 | Name name; |
| 189 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 190 | std::tie(rrset, name) = generateBaseRrset(label, label::APPCERT_RR_TYPE, version, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 191 | |
| 192 | Data data(name); |
| 193 | data.setContent(cert.wireEncode()); |
| 194 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 195 | setContentType(data, NDNS_KEY, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 196 | sign(data); |
| 197 | rrset.setData(data.wireEncode()); |
| 198 | |
| 199 | return rrset; |
| 200 | } |
| 201 | |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 202 | Rrset |
| 203 | RrsetFactory::generateAuthRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 204 | uint64_t version, |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 205 | time::seconds ttl) |
| 206 | { |
| 207 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 208 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (ttl == DEFAULT_RR_TTL) |
| 212 | ttl = m_zone.getTtl(); |
| 213 | |
| 214 | Name name; |
| 215 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 216 | std::tie(rrset, name) = generateBaseRrset(label, label::NS_RR_TYPE, version, ttl); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 217 | |
| 218 | Data data(name); |
| 219 | |
| 220 | setContentType(data, NDNS_AUTH, ttl); |
| 221 | sign(data); |
| 222 | rrset.setData(data.wireEncode()); |
| 223 | |
| 224 | return rrset; |
| 225 | } |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 226 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 227 | Rrset |
| 228 | RrsetFactory::generateDoeRrset(const Name& label, |
| 229 | uint64_t version, |
| 230 | time::seconds ttl, |
| 231 | const Name& lowerLabel, |
| 232 | const Name& upperLabel) |
| 233 | { |
| 234 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame] | 235 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | if (ttl == DEFAULT_RR_TTL) |
| 239 | ttl = m_zone.getTtl(); |
| 240 | |
| 241 | Name name; |
| 242 | Rrset rrset; |
| 243 | std::tie(rrset, name) = generateBaseRrset(label, label::DOE_RR_TYPE, version, ttl); |
| 244 | |
| 245 | std::vector<Block> range; |
| 246 | range.push_back(lowerLabel.wireEncode()); |
| 247 | range.push_back(upperLabel.wireEncode()); |
| 248 | |
| 249 | Data data(name); |
| 250 | data.setContent(wireEncode(range)); |
| 251 | |
| 252 | setContentType(data, NDNS_DOE, ttl); |
| 253 | sign(data); |
| 254 | rrset.setData(data.wireEncode()); |
| 255 | |
| 256 | return rrset; |
| 257 | } |
| 258 | |
| 259 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 260 | void |
| 261 | RrsetFactory::sign(Data& data) |
| 262 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 263 | m_keyChain.sign(data, signingByCertificate(m_dskCertName)); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 267 | RrsetFactory::setContentType(Data& data, NdnsContentType contentType, |
| 268 | const time::seconds& ttl) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 269 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 270 | data.setContentType(contentType); |
| 271 | data.setFreshnessPeriod(ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | template<encoding::Tag TAG> |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 275 | size_t |
| 276 | RrsetFactory::wireEncode(EncodingImpl<TAG>& encoder, const std::vector<Block>& rrs) const |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 277 | { |
| 278 | // Content :: = CONTENT-TYPE TLV-LENGTH |
| 279 | // Block* |
| 280 | |
| 281 | size_t totalLength = 0; |
| 282 | for (auto iter = rrs.rbegin(); iter != rrs.rend(); ++iter) { |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 283 | totalLength += prependBlock(encoder, *iter); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 286 | totalLength += encoder.prependVarNumber(totalLength); |
| 287 | totalLength += encoder.prependVarNumber(ndn::tlv::Content); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 288 | return totalLength; |
| 289 | } |
| 290 | |
Davide Pesavento | 9802612 | 2022-03-14 22:00:03 -0400 | [diff] [blame^] | 291 | Block |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 292 | RrsetFactory::wireEncode(const std::vector<Block>& rrs) const |
| 293 | { |
| 294 | EncodingEstimator estimator; |
| 295 | size_t estimatedSize = wireEncode(estimator, rrs); |
| 296 | EncodingBuffer buffer(estimatedSize, 0); |
| 297 | wireEncode(buffer, rrs); |
| 298 | return buffer.block(); |
| 299 | } |
| 300 | |
| 301 | std::vector<std::string> |
| 302 | RrsetFactory::wireDecodeTxt(const Block& wire) |
| 303 | { |
| 304 | std::vector<std::string> txts; |
| 305 | wire.parse(); |
| 306 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 307 | for (const auto& e : wire.elements()) { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 308 | txts.push_back(std::string(reinterpret_cast<const char*>(e.value()), |
| 309 | e.value_size())); |
| 310 | } |
| 311 | |
| 312 | return txts; |
| 313 | } |
| 314 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 315 | } // namespace ndns |
| 316 | } // namespace ndn |