blob: 9925aca3dcaae33d110feead25a20230486dc804 [file] [log] [blame]
Jiewen Tan870b29b2014-11-17 19:09:49 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
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 "management-tool.hpp"
21#include "logger.hpp"
22#include "ndns-label.hpp"
23#include "ndns-tlv.hpp"
24
25#include <string>
26#include <iomanip>
27
28#include <boost/filesystem/operations.hpp>
29#include <boost/filesystem/path.hpp>
30#include <boost/algorithm/string/replace.hpp>
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080031#include <boost/lexical_cast.hpp>
Jiewen Tan870b29b2014-11-17 19:09:49 -080032
33#include <ndn-cxx/util/io.hpp>
34#include <ndn-cxx/util/regex.hpp>
35#include <ndn-cxx/encoding/oid.hpp>
36#include <ndn-cxx/security/cryptopp.hpp>
37
38namespace ndn {
39namespace ndns {
40
41NDNS_LOG_INIT("ManagementTool");
42
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080043ManagementTool::ManagementTool(const std::string& dbFile, KeyChain& keyChain)
44 : m_keyChain(keyChain)
45 , m_dbMgr(dbFile)
Jiewen Tan870b29b2014-11-17 19:09:49 -080046{
47}
48
49void
50ManagementTool::createZone(const Name &zoneName,
51 const Name& parentZoneName,
52 const time::seconds& cacheTtl,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080053 const time::seconds& certValidity,
Jiewen Tan870b29b2014-11-17 19:09:49 -080054 const Name& kskCertName,
55 const Name& dskCertName)
56{
57 bool isRoot = zoneName == ROOT_ZONE;
58
59 //check preconditions
60 Zone zone(zoneName, cacheTtl);
61 if (m_dbMgr.find(zone)) {
62 throw Error(zoneName.toUri() + " is already presented in the NDNS db");
63 }
64
65 if (!isRoot && parentZoneName.equals(zoneName)) {
66 throw Error("Parent zone name can not be the zone itself");
67 }
68
69 if (!isRoot && !parentZoneName.isPrefixOf(zoneName)) {
70 throw Error(parentZoneName.toUri() + " is not a prefix of " + zoneName.toUri());
71 }
72
73 if (kskCertName != DEFAULT_CERT) {
74 if (!matchCertificate(kskCertName, zoneName)) {
75 throw Error("Cannot verify KSK certificate");
76 }
77 }
78
79 if (dskCertName != DEFAULT_CERT) {
80 if (!matchCertificate(dskCertName, zoneName)) {
81 throw Error("Cannot verify DSK certificate");
82 }
83 }
84
85 if (kskCertName == DEFAULT_CERT && isRoot) {
86 throw Error("Cannot generate KSK for root zone");
87 }
88
89 //first generate KSK and DSK to the keyChain system, and add DSK as default
90 NDNS_LOG_INFO("Start generating KSK and DSK and their corresponding certificates");
91 time::system_clock::TimePoint notBefore = time::system_clock::now();
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -080092 time::system_clock::TimePoint notAfter = notBefore + certValidity;
Jiewen Tan870b29b2014-11-17 19:09:49 -080093 shared_ptr<IdentityCertificate> kskCert;
94
95 if (kskCertName == DEFAULT_CERT) {
96 //create KSK's certificate
97 Name kskName = m_keyChain.generateRsaKeyPair(zoneName, true);
98 std::vector<CertificateSubjectDescription> kskDesc;
99 kskCert = m_keyChain.prepareUnsignedIdentityCertificate(kskName, zoneName, notBefore, notAfter,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800100 kskDesc, parentZoneName);
101 kskCert->setFreshnessPeriod(cacheTtl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800102
103 m_keyChain.selfSign(*kskCert);
104 m_keyChain.addCertificate(*kskCert);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800105 NDNS_LOG_INFO("Generated KSK: " << kskCert->getName());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800106 }
107 else {
108 kskCert = m_keyChain.getCertificate(kskCertName);
109 }
110
111 Name dskName;
112 shared_ptr<IdentityCertificate> dskCert;
113 if (dskCertName == DEFAULT_CERT) {
114 dskName = m_keyChain.generateRsaKeyPairAsDefault(zoneName, false);
115 //create DSK's certificate
116 std::vector<CertificateSubjectDescription> dskDesc;
117 dskCert = m_keyChain.prepareUnsignedIdentityCertificate(dskName, zoneName, notBefore, notAfter,
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800118 dskDesc, zoneName);
119 dskCert->setFreshnessPeriod(cacheTtl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800120 m_keyChain.sign(*dskCert, kskCert->getName());
121 m_keyChain.addCertificateAsKeyDefault(*dskCert);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800122 NDNS_LOG_INFO("Generated DSK: " << dskCert->getName());
Jiewen Tan870b29b2014-11-17 19:09:49 -0800123 }
124 else {
125 dskCert = m_keyChain.getCertificate(dskCertName);
126 dskName = dskCert->getPublicKeyName();
127 m_keyChain.setDefaultKeyNameForIdentity(dskName);
128 m_keyChain.setDefaultCertificateNameForKey(dskCert->getName());
129 }
130
131 //second add zone to the database
132 NDNS_LOG_INFO("Start adding new zone to data base");
133 addZone(zone);
134
135 //third create ID-cert
136 NDNS_LOG_INFO("Start creating DSK's ID-CERT");
137 addIdCert(zone, dskCert, cacheTtl);
138}
139
140void
141ManagementTool::deleteZone(const Name& zoneName)
142{
143 //check pre-conditions
144 Zone zone(zoneName);
145 if (!m_dbMgr.find(zone)) {
146 throw Error(zoneName.toUri() + " is not presented in the NDNS db");
147 }
148
149 //first remove all rrsets of this zone from local ndns database
150 std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone);
151 for (Rrset& rrset : rrsets) {
152 m_dbMgr.remove(rrset);
153 }
154
155 //second remove zone from local ndns database
156 removeZone(zone);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800157}
158
159void
160ManagementTool::exportCertificate(const Name& certName, const std::string& outFile)
161{
162 //search for the certificate, start from KeyChain then local NDNS database
163 shared_ptr<IdentityCertificate> cert;
164 if (m_keyChain.doesCertificateExist(certName)) {
165 cert = m_keyChain.getCertificate(certName);
166 }
167 else {
168 shared_ptr<Regex> regex = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
169 if (regex->match(certName) != true) {
170 throw Error("Certificate name is illegal");
171 }
172 Name zoneName = regex->expand("\\1");
173 Name label = regex->expand("\\2");
174
175 Zone zone(zoneName);
176 Rrset rrset(&zone);
177 rrset.setLabel(label);
178 rrset.setType(label::CERT_RR_TYPE);
179 if (m_dbMgr.find(rrset)) {
180 Data data(rrset.getData());
181 cert = make_shared<IdentityCertificate>(data);
182 }
183 else {
184 throw Error("Cannot find the cert: " + certName.toUri());
185 }
186 }
187
188 if (outFile == DEFAULT_IO) {
189 ndn::io::save(*cert, std::cout);
190 }
191 else {
192 ndn::io::save(*cert, outFile);
193 NDNS_LOG_INFO("save cert to file: " << outFile);
194 }
195}
196
197void
198ManagementTool::addRrSet(const Name& zoneName,
199 const Name& label,
200 const name::Component& type,
201 NdnsType ndnsType,
202 const uint64_t version,
203 const std::vector<std::string>& contents,
204 const Name& inputDskCertName,
205 const time::seconds& ttl)
206{
207 // check pre-condition
208 Zone zone(zoneName);
209 if (!m_dbMgr.find(zone)) {
210 throw Error(zoneName.toUri() + " is not presented in the NDNS db");
211 }
212
213 if (ndnsType == NDNS_UNKNOWN) {
214 throw Error("The ndns type is unknown");
215 }
216
217 if (type == label::CERT_RR_TYPE) {
218 throw Error("It cannot handle ID-CERT rrset type");
219 }
220
221 // check strange rr type and ndns type combination
222 if (type == label::NS_RR_TYPE && ndnsType == NDNS_RAW) {
223 throw Error("NS cannot be of the type NDNS_RAW");
224 }
225
226 if (type == label::TXT_RR_TYPE && ndnsType != NDNS_RESP) {
227 throw Error("TXT cannot be of the type NDNS_RAW or NDNS_AUTH");
228 }
229
230 if (ndnsType == NDNS_RAW && contents.size() != 1) {
231 throw Error("NDNS_RAW must contain a single content element");
232 }
233
234 Name dskName;
235 Name dskCertName = inputDskCertName;
236 if (dskCertName == DEFAULT_CERT) {
237 dskName = m_keyChain.getDefaultKeyNameForIdentity(zoneName);
238 dskCertName = m_keyChain.getDefaultCertificateNameForKey(dskName);
239 }
240 else {
241 if (!matchCertificate(dskCertName, zoneName)) {
242 throw Error("Cannot verify certificate");
243 }
244 }
245
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800246 time::seconds actualTtl = ttl;
247 if (ttl == DEFAULT_RR_TTL)
248 actualTtl = zone.getTtl();
249
Jiewen Tan870b29b2014-11-17 19:09:49 -0800250 // set rrset
251 Rrset rrset(&zone);
252 rrset.setLabel(label);
253 rrset.setType(type);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800254 rrset.setTtl(actualTtl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800255
256 // set response
257 Response re;
258 re.setZone(zoneName);
259 re.setQueryType(label::NDNS_ITERATIVE_QUERY);
260 re.setRrLabel(label);
261 re.setRrType(type);
262 re.setNdnsType(ndnsType);
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800263 re.setFreshnessPeriod(actualTtl);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800264
265 //set content according to ndns type
266 if (ndnsType == NDNS_RAW) {
267 Block tmp = ndn::dataBlock(ndn::tlv::Content, contents[0].c_str(), contents[0].length());
268 re.setAppContent(tmp);
269 }
270 else if (ndnsType != NDNS_AUTH) {
271 if (contents.empty()) {
272 re.addRr("");
273 }
274 else {
275 for (const auto& item : contents) {
276 re.addRr(item);
277 }
278 }
279 }
280
Jiewen Tan870b29b2014-11-17 19:09:49 -0800281 if (version != VERSION_USE_UNIX_TIMESTAMP) {
282 name::Component tmp = name::Component::fromVersion(version);
283 re.setVersion(tmp);
284 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800285 shared_ptr<Data> data = re.toData();
Jiewen Tan870b29b2014-11-17 19:09:49 -0800286 m_keyChain.sign(*data, dskCertName);
287
288 rrset.setVersion(re.getVersion());
289 rrset.setData(data->wireEncode());
290
291 if (m_dbMgr.find(rrset)) {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800292 throw Error("Duplicate " + boost::lexical_cast<std::string>(rrset));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800293 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800294 NDNS_LOG_INFO("Added " << rrset);
295
Jiewen Tan870b29b2014-11-17 19:09:49 -0800296 m_dbMgr.insert(rrset);
297}
298
299void
300ManagementTool::addRrSet(const Name& zoneName,
301 const std::string& inFile,
302 const time::seconds& ttl,
303 const Name& inputDskCertName)
304{
305 //check precondition
306 Zone zone(zoneName);
307 if (!m_dbMgr.find(zone)) {
308 throw Error(zoneName.toUri() + " is not presented in the NDNS db");
309 }
310
311 Name dskName;
312 Name dskCertName = inputDskCertName;
313 if (dskCertName == DEFAULT_CERT) {
314 dskName = m_keyChain.getDefaultKeyNameForIdentity(zoneName);
315 dskCertName = m_keyChain.getDefaultCertificateNameForKey(dskName);
316 }
317 else {
318 if (!matchCertificate(dskCertName, zoneName)) {
319 throw Error("Cannot verify certificate");
320 }
321 }
322
323 if (inFile != DEFAULT_IO) {
324 boost::filesystem::path dir = boost::filesystem::path(inFile);
325 if (!boost::filesystem::exists(dir) || boost::filesystem::is_directory(dir)) {
326 throw Error("Data: " + inFile + " does not exist");
327 }
328 }
329
330 //first load the data
331 shared_ptr<Data> data;
332 if (inFile == DEFAULT_IO)
333 data = ndn::io::load<ndn::Data>(std::cin);
334 else
335 data = ndn::io::load<ndn::Data>(inFile);
336
Jiewen Tand2d21822015-03-19 15:37:03 -0700337 if (data == nullptr) {
338 throw Error("input does not contain a valid Data packet (is it in base64 format?)");
339 }
340
Jiewen Tan870b29b2014-11-17 19:09:49 -0800341 //determine whether the data is a self-signed certificate
342 shared_ptr<Regex> regex1 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT><>");
343 if (regex1->match(data->getName())) {
344 IdentityCertificate scert(*data);
345 Name keyName = scert.getPublicKeyName();
346 Name keyLocator = scert.getSignature().getKeyLocator().getName();
347
348 //if it is, extract the content and name from the data, and resign it using the dsk.
349 shared_ptr<Regex> regex2 = make_shared<Regex>("(<>*)<KEY>(<>+)<ID-CERT>");
350 BOOST_VERIFY(regex2->match(keyLocator) == true);
351 if (keyName == regex2->expand("\\1\\2")) {
352 shared_ptr<Data> pre = data;
353 Name name = pre->getName();
354 //check whether the name is legal or not. if not converting it to a legal name
355 if (zoneName != regex1->expand("\\1")) {
356 Name comp1 = regex1->expand("\\1").getSubName(zoneName.size());
357 Name comp2 = regex1->expand("\\2");
358 name = zoneName;
359 name.append("KEY");
360 name.append(comp1);
361 name.append(comp2);
362 name.append("ID-CERT");
363 name.append(pre->getName().get(-1));
364 }
365
366 data = make_shared<Data>();
367 data->setName(name);
368 data->setContent(pre->getContent());
369
370 m_keyChain.sign(*data, dskCertName);
371 }
372 }
373
374 // create response for the input data
375 Response re;
376 Name hint;
377 re.fromData(hint, zoneName, *data);
378 Name label = re.getRrLabel();
379 name::Component type = re.getRrType();
380
381 Rrset rrset(&zone);
382 rrset.setLabel(label);
383 rrset.setType(type);
384 if (ttl == DEFAULT_RR_TTL)
385 rrset.setTtl(zone.getTtl());
386 else
387 rrset.setTtl(ttl);
388 rrset.setVersion(re.getVersion());
389 rrset.setData(data->wireEncode());
390
391 if (m_dbMgr.find(rrset)) {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800392 throw Error("Duplicate " + boost::lexical_cast<std::string>(rrset));
Jiewen Tan870b29b2014-11-17 19:09:49 -0800393 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800394 NDNS_LOG_INFO("Added " << rrset);
Jiewen Tan870b29b2014-11-17 19:09:49 -0800395 m_dbMgr.insert(rrset);
396}
397
398void
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800399ManagementTool::listZone(const Name& zoneName, std::ostream& os, const bool printRaw)
400{
Jiewen Tan870b29b2014-11-17 19:09:49 -0800401 Zone zone(zoneName);
402 if (!m_dbMgr.find(zone)) {
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800403 throw Error("Zone " + zoneName.toUri() + " is not found in the database");
Jiewen Tan870b29b2014-11-17 19:09:49 -0800404 }
405
406 //first output the zone name
407 os << "; Zone " << zoneName.toUri() << std::endl << std::endl;
408
409 //second output all rrsets
410 std::vector<Rrset> rrsets = m_dbMgr.findRrsets(zone);
411
412 //set width for different columns
413 size_t labelWidth = 0;
414 size_t ttlWidth = 0;
415 size_t typeWidth = 0;
416 for (Rrset& rrset : rrsets) {
417 Data data(rrset.getData());
418 Response re;
419 Name hint;
420 re.fromData(hint, zoneName, data);
421
422 if (rrset.getLabel().toUri().size() > labelWidth)
423 labelWidth = rrset.getLabel().toUri().size();
424
425 std::stringstream seconds;
426 seconds << rrset.getTtl().count();
427 if (seconds.str().size() > ttlWidth)
428 ttlWidth = seconds.str().size();
429
430 if (rrset.getType().toUri().size() > typeWidth)
431 typeWidth = rrset.getType().toUri().size();
432 }
433
434 //output
435 for (Rrset& rrset : rrsets) {
436 Data data(rrset.getData());
437 Response re;
438 Name hint;
439 re.fromData(hint, zoneName, data);
440 int iteration = re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH ?
441 1 : re.getRrs().size();
442 const std::vector<Block> &rrs = re.getRrs();
443
444 if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) {
445 os << "; rrset=" << rrset.getLabel().toUri()
446 << " type=" << rrset.getType().toUri()
447 << " version=" << rrset.getVersion().toUri()
448 << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri()
449 << std::endl;
450 }
451
452 for (int i = 0; i < iteration; i++) {
453 os.setf(os.left);
454 os.width(labelWidth + 2);
455 os << rrset.getLabel().toUri();
456
457 os.width(ttlWidth + 2);
458 os << rrset.getTtl().count();
459
460 os.width(typeWidth + 2);
461 os << rrset.getType().toUri();
462
463 if (re.getNdnsType() != NDNS_RAW && re.getNdnsType() != NDNS_AUTH) {
464 using namespace CryptoPP;
465 if (rrset.getType() == label::TXT_RR_TYPE) {
466 os.write(reinterpret_cast<const char*>(rrs[i].value()), rrs[i].value_size());
467 os << std::endl;
468 }
469 else if (rrset.getType() == label::NS_RR_TYPE) {
470 //TODO output the NS data once we have it
471 os << std::endl;
472 }
473 else {
474 StringSource ss(rrs[i].wire(), rrs[i].size(), true,
475 new Base64Encoder(new FileSink(os), true, 64));
476 }
477 }
478 }
479
480 if (re.getNdnsType() == NDNS_RAW || re.getNdnsType() == NDNS_AUTH) {
481 os.width();
482 os << "; content-type=" << re.getNdnsType()
483 << " version=" << rrset.getVersion().toUri()
484 << " signed-by=" << data.getSignature().getKeyLocator().getName().toUri();
485 os << std::endl;
486
487 if (printRaw && re.getNdnsType() == NDNS_RAW) {
488 using namespace CryptoPP;
489 std::stringstream sstream;
490 StringSource ss(re.getAppContent().wire(), re.getAppContent().size(), true,
491 new Base64Encoder(new FileSink(sstream), true, 64));
492
493 std::string content = sstream.str();
494 std::string delimiter = "\n";
495 size_t pos = 0;
496 std::string token;
497 while ((pos = content.find(delimiter)) != std::string::npos) {
498 token = content.substr(0, pos);
499 os << "; " << token << std::endl;
500 content.erase(0, pos + delimiter.length());
501 }
Jiewen Tan870b29b2014-11-17 19:09:49 -0800502 }
Alexander Afanasyevd6b3bda2014-11-25 17:33:58 -0800503 os << std::endl;
Jiewen Tan870b29b2014-11-17 19:09:49 -0800504 }
505 else {
506 os << std::endl;
507 }
508 }
509}
510
511void
512ManagementTool::listAllZones(std::ostream& os) {
513 std::vector<Zone> zones = m_dbMgr.listZones();
514
515 size_t nameWidth = 0;
516 for (const Zone& zone : zones) {
517 if (zone.getName().toUri().size() > nameWidth)
518 nameWidth = zone.getName().toUri().size();
519 }
520
521 for (const Zone& zone : zones) {
522 os.setf(os.left);
523 os.width(nameWidth + 2);
524 os << zone.getName().toUri();
525
526 os << "; default-ttl=" << zone.getTtl().count();
527 os << " default-key=" << m_keyChain.getDefaultKeyNameForIdentity(zone.getName());
528 os << " default-certificate="
529 << m_keyChain.getDefaultCertificateNameForIdentity(zone.getName());
530 os << std::endl;
531 }
532}
533
534void
535ManagementTool::removeRrSet(const Name& zoneName, const Name& label, const name::Component& type)
536{
537 Zone zone(zoneName);
538 Rrset rrset(&zone);
539 rrset.setLabel(label);
540 rrset.setType(type);
541
542 if (!m_dbMgr.find(rrset)) {
543 return;
544 }
545 NDNS_LOG_INFO("Remove rrset with zone-id: " << zone.getId() << " label: " << label << " type: "
546 << type);
547 m_dbMgr.remove(rrset);
548}
549
550void
551ManagementTool::getRrSet(const Name& zoneName,
552 const Name& label,
553 const name::Component& type,
554 std::ostream& os)
555{
556 Zone zone(zoneName);
557 Rrset rrset(&zone);
558 rrset.setLabel(label);
559 rrset.setType(type);
560
561 if (!m_dbMgr.find(rrset)) {
562 os << "No record is found" << std::endl;
563 return;
564 }
565
566 using namespace CryptoPP;
567 StringSource ss(rrset.getData().wire(), rrset.getData().size(), true,
568 new Base64Encoder(new FileSink(os), true, 64));
569}
570
571void
572ManagementTool::addIdCert(Zone& zone, shared_ptr<IdentityCertificate> cert,
573 const time::seconds& ttl)
574{
575 Rrset rrset(&zone);
576 size_t size = zone.getName().size();
577 Name label = cert->getName().getSubName(size + 1, cert->getName().size() - size - 3);
578 rrset.setLabel(label);
579 rrset.setType(label::CERT_RR_TYPE);
580 rrset.setTtl(ttl);
581 rrset.setVersion(cert->getName().get(-1));
582 rrset.setData(cert->wireEncode());
583
584 if (m_dbMgr.find(rrset)) {
585 throw Error("ID-CERT with label=" + label.toUri() +
586 " is already presented in local NDNS databse");
587 }
588 NDNS_LOG_INFO("Add rrset with zone-id: " << zone.getId() << " label: " << label << " type: "
589 << label::CERT_RR_TYPE);
590 m_dbMgr.insert(rrset);
591}
592
593void
594ManagementTool::addZone(Zone& zone)
595{
596 if (m_dbMgr.find(zone)) {
597 throw Error("Zone with Name=" + zone.getName().toUri() +
598 " is already presented in local NDNS databse");
599 }
600 NDNS_LOG_INFO("Add zone with Name: " << zone.getName().toUri());
601 m_dbMgr.insert(zone);
602}
603
604void
605ManagementTool::removeZone(Zone& zone)
606{
607 if (!m_dbMgr.find(zone)) {
608 return;
609 }
610 NDNS_LOG_INFO("Remove zone with Name: " << zone.getName().toUri());
611 m_dbMgr.remove(zone);
612}
613
614bool
615ManagementTool::matchCertificate(const Name& certName, const Name& identity)
616{
617 if (!m_keyChain.doesCertificateExist(certName)) {
618 NDNS_LOG_WARN(certName.toUri() << " is not presented in KeyChain");
619 return false;
620 }
621
622 //check its public key information
623 shared_ptr<IdentityCertificate> cert = m_keyChain.getCertificate(certName);
624 Name keyName = cert->getPublicKeyName();
625
626 if (!identity.isPrefixOf(keyName) || identity.size()!=keyName.size()-1) {
627 NDNS_LOG_WARN(keyName.toUri() << " is not a key of " << identity.toUri());
628 return false;
629 }
630
631 if (!m_keyChain.doesKeyExistInTpm(keyName, KEY_CLASS_PRIVATE)) {
632 NDNS_LOG_WARN("Private key: " << keyName.toUri() << " is not presented in KeyChain");
633 return false;
634 }
635
636 return true;
637}
638
639} // namespace ndns
640} // namespace ndn