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 | /* |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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) { |
| 63 | return ; |
| 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 | |
| 74 | |
| 75 | std::pair<Rrset, Name> |
| 76 | RrsetFactory::generateBaseRrset(const Name& label, |
| 77 | const name::Component& type, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 78 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 79 | const time::seconds& ttl) |
| 80 | { |
| 81 | Rrset rrset(&m_zone); |
| 82 | |
| 83 | rrset.setLabel(label); |
| 84 | rrset.setType(type); |
| 85 | rrset.setTtl(ttl); |
| 86 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 87 | Name name; |
| 88 | name.append(m_zone.getName()) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 89 | .append(label::NDNS_ITERATIVE_QUERY) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 90 | .append(label) |
| 91 | .append(type); |
| 92 | |
| 93 | if (version != VERSION_USE_UNIX_TIMESTAMP) { |
| 94 | name.append(name::Component::fromVersion(version)); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 95 | } |
| 96 | else { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 97 | name.appendVersion(); |
| 98 | } |
| 99 | |
| 100 | rrset.setVersion(name.get(-1)); |
| 101 | |
| 102 | return std::make_pair(rrset, name); |
| 103 | } |
| 104 | |
| 105 | bool |
| 106 | RrsetFactory::matchCertificate(const Name& certName, const Name& identity) |
| 107 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 108 | try { |
| 109 | CertHelper::getCertificate(m_keyChain, identity, certName); |
| 110 | return true; |
Davide Pesavento | 4a315b3 | 2018-11-24 14:32:19 -0500 | [diff] [blame] | 111 | } catch (const ndn::security::Pib::Error&) { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 112 | return false; |
| 113 | } |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | Rrset |
| 117 | RrsetFactory::generateNsRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 118 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 119 | time::seconds ttl, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 120 | const ndn::DelegationList& delegations) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 121 | { |
| 122 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 123 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | if (ttl == DEFAULT_RR_TTL) |
| 127 | ttl = m_zone.getTtl(); |
| 128 | |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 129 | 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] | 130 | const Name& name = rrsetAndName.second; |
| 131 | Rrset& rrset = rrsetAndName.first; |
| 132 | |
| 133 | Link link(name); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 134 | link.setDelegationList(delegations); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 135 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 136 | setContentType(link, NDNS_LINK, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 137 | sign(link); |
| 138 | rrset.setData(link.wireEncode()); |
| 139 | |
| 140 | return rrset; |
| 141 | } |
| 142 | |
| 143 | Rrset |
| 144 | RrsetFactory::generateTxtRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 145 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 146 | time::seconds ttl, |
| 147 | const std::vector<std::string>& strings) |
| 148 | { |
| 149 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 150 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | if (ttl == DEFAULT_RR_TTL) |
| 154 | ttl = m_zone.getTtl(); |
| 155 | |
| 156 | Name name; |
| 157 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 158 | std::tie(rrset, name) = generateBaseRrset(label, label::TXT_RR_TYPE, version, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 159 | |
| 160 | std::vector<Block> rrs; |
| 161 | for (const auto& item : strings) { |
| 162 | rrs.push_back(makeBinaryBlock(ndns::tlv::RrData, |
| 163 | item.c_str(), |
| 164 | item.size())); |
| 165 | } |
| 166 | |
| 167 | Data data(name); |
| 168 | data.setContent(wireEncode(rrs)); |
| 169 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 170 | setContentType(data, NDNS_RESP, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 171 | sign(data); |
| 172 | rrset.setData(data.wireEncode()); |
| 173 | |
| 174 | return rrset; |
| 175 | } |
| 176 | |
| 177 | Rrset |
| 178 | RrsetFactory::generateCertRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 179 | uint64_t version, |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 180 | time::seconds ttl, |
Alexander Afanasyev | 60514ec | 2020-06-03 14:18:53 -0400 | [diff] [blame] | 181 | const ndn::security::Certificate& cert) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 182 | { |
| 183 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 184 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (ttl == DEFAULT_RR_TTL) |
| 188 | ttl = m_zone.getTtl(); |
| 189 | |
| 190 | Name name; |
| 191 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 192 | std::tie(rrset, name) = generateBaseRrset(label, label::APPCERT_RR_TYPE, version, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 193 | |
| 194 | Data data(name); |
| 195 | data.setContent(cert.wireEncode()); |
| 196 | |
Yumin Xia | 3c6b1fd | 2016-12-11 19:08:47 -0800 | [diff] [blame] | 197 | setContentType(data, NDNS_KEY, ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 198 | sign(data); |
| 199 | rrset.setData(data.wireEncode()); |
| 200 | |
| 201 | return rrset; |
| 202 | } |
| 203 | |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 204 | Rrset |
| 205 | RrsetFactory::generateAuthRrset(const Name& label, |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 206 | uint64_t version, |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 207 | time::seconds ttl) |
| 208 | { |
| 209 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 210 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | if (ttl == DEFAULT_RR_TTL) |
| 214 | ttl = m_zone.getTtl(); |
| 215 | |
| 216 | Name name; |
| 217 | Rrset rrset; |
Yumin Xia | d4e8ce5 | 2017-03-17 19:56:52 -0700 | [diff] [blame] | 218 | std::tie(rrset, name) = generateBaseRrset(label, label::NS_RR_TYPE, version, ttl); |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 219 | |
| 220 | Data data(name); |
| 221 | |
| 222 | setContentType(data, NDNS_AUTH, ttl); |
| 223 | sign(data); |
| 224 | rrset.setData(data.wireEncode()); |
| 225 | |
| 226 | return rrset; |
| 227 | } |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 228 | |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 229 | Rrset |
| 230 | RrsetFactory::generateDoeRrset(const Name& label, |
| 231 | uint64_t version, |
| 232 | time::seconds ttl, |
| 233 | const Name& lowerLabel, |
| 234 | const Name& upperLabel) |
| 235 | { |
| 236 | if (!m_checked) { |
Davide Pesavento | 948c50c | 2020-12-26 21:30:45 -0500 | [diff] [blame^] | 237 | NDN_THROW(Error("You have to call checkZoneKey before call generate functions")); |
Yumin Xia | 55a7cc4 | 2017-05-14 18:43:34 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | if (ttl == DEFAULT_RR_TTL) |
| 241 | ttl = m_zone.getTtl(); |
| 242 | |
| 243 | Name name; |
| 244 | Rrset rrset; |
| 245 | std::tie(rrset, name) = generateBaseRrset(label, label::DOE_RR_TYPE, version, ttl); |
| 246 | |
| 247 | std::vector<Block> range; |
| 248 | range.push_back(lowerLabel.wireEncode()); |
| 249 | range.push_back(upperLabel.wireEncode()); |
| 250 | |
| 251 | Data data(name); |
| 252 | data.setContent(wireEncode(range)); |
| 253 | |
| 254 | setContentType(data, NDNS_DOE, ttl); |
| 255 | sign(data); |
| 256 | rrset.setData(data.wireEncode()); |
| 257 | |
| 258 | return rrset; |
| 259 | } |
| 260 | |
| 261 | |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 262 | void |
| 263 | RrsetFactory::sign(Data& data) |
| 264 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 265 | m_keyChain.sign(data, signingByCertificate(m_dskCertName)); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | void |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 269 | RrsetFactory::setContentType(Data& data, NdnsContentType contentType, |
| 270 | const time::seconds& ttl) |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 271 | { |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 272 | data.setContentType(contentType); |
| 273 | data.setFreshnessPeriod(ttl); |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | template<encoding::Tag TAG> |
| 277 | inline size_t |
| 278 | RrsetFactory::wireEncode(EncodingImpl<TAG>& block, const std::vector<Block>& rrs) const |
| 279 | { |
| 280 | // Content :: = CONTENT-TYPE TLV-LENGTH |
| 281 | // Block* |
| 282 | |
| 283 | size_t totalLength = 0; |
| 284 | for (auto iter = rrs.rbegin(); iter != rrs.rend(); ++iter) { |
| 285 | totalLength += block.prependBlock(*iter); |
| 286 | } |
| 287 | |
| 288 | totalLength += block.prependVarNumber(totalLength); |
| 289 | totalLength += block.prependVarNumber(::ndn::tlv::Content); |
| 290 | |
| 291 | return totalLength; |
| 292 | } |
| 293 | |
| 294 | const Block |
| 295 | RrsetFactory::wireEncode(const std::vector<Block>& rrs) const |
| 296 | { |
| 297 | EncodingEstimator estimator; |
| 298 | size_t estimatedSize = wireEncode(estimator, rrs); |
| 299 | EncodingBuffer buffer(estimatedSize, 0); |
| 300 | wireEncode(buffer, rrs); |
| 301 | return buffer.block(); |
| 302 | } |
| 303 | |
| 304 | std::vector<std::string> |
| 305 | RrsetFactory::wireDecodeTxt(const Block& wire) |
| 306 | { |
| 307 | std::vector<std::string> txts; |
| 308 | wire.parse(); |
| 309 | |
Yumin Xia | a484ba7 | 2016-11-10 20:40:12 -0800 | [diff] [blame] | 310 | for (const auto& e : wire.elements()) { |
Yumin Xia | f853ad7 | 2016-11-03 21:14:07 -0700 | [diff] [blame] | 311 | txts.push_back(std::string(reinterpret_cast<const char*>(e.value()), |
| 312 | e.value_size())); |
| 313 | } |
| 314 | |
| 315 | return txts; |
| 316 | } |
| 317 | |
| 318 | |
| 319 | } // namespace ndns |
| 320 | } // namespace ndn |