blob: 71c9680d9a97eadb0855834d8f9159a69a0b0902 [file] [log] [blame]
Shock Jiang0b165f42014-10-24 09:08:09 -07001/* -*- 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 "logger.hpp"
21#include "config.hpp"
22#include "validator.hpp"
23
24#include "ndn-cxx/data.hpp"
25#include <ndn-cxx/security/validator-config.hpp>
26
27
28namespace ndn {
29namespace ndns {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070030
31NDNS_LOG_INIT("validator")
Shock Jiang0b165f42014-10-24 09:08:09 -070032
33std::string Validator::VALIDATOR_CONF_FILE = DEFAULT_CONFIG_PATH "/" "validator.conf";
34
35Validator::Validator(Face& face, const std::string& confFile /* = VALIDATOR_CONF_FILE */)
36 : ValidatorConfig(face)
37{
38 try {
39 this->load(confFile);
40 NDNS_LOG_TRACE("Validator loads configuration: " << confFile);
41 }
42 catch (std::exception&) {
43 std::string config =
44 "rule \n"
45 "{ \n"
46 " id \"NDNS Validator\" \n"
47 " for data \n"
48 " checker \n"
49 " { \n"
50 " type customized \n"
51 " sig-type rsa-sha256 \n"
52 " key-locator \n"
53 " { \n"
54 " type name \n"
55 " hyper-relation \n"
56 " { \n"
57 " k-regex ^(<>*)<KEY>(<>*)<><ID-CERT>$ \n"
58 " k-expand \\\\1\\\\2 \n"
59 " h-relation is-prefix-of \n"
60 " p-regex ^(<>*)[<KEY><NDNS>](<>*)<><>$ \n"
61 " p-expand \\\\1\\\\2 \n"
62 " } \n"
63 " } \n"
64 " } \n"
65 "} \n"
66 " \n"
67 " \n"
68 "trust-anchor \n"
69 "{ \n"
70 " type file \n"
71 " file-name \""
72 ;
73
Shock Jiangbb4e15b2014-12-05 09:48:02 -080074 config += DEFAULT_CONFIG_PATH "/" "anchors/root.cert";
Shock Jiang0b165f42014-10-24 09:08:09 -070075
76 config +=
77 "\" \n"
78 "} \n"
79 " \n"
80 ;
81
82 this->load(config, "embededConf");
83 NDNS_LOG_TRACE("Validator loads embedded configuration with anchors path: anchors/root.cert");
84 }
85
86}
87
88void
89Validator::validate(const Data& data,
90 const OnDataValidated& onValidated,
91 const OnDataValidationFailed& onValidationFailed)
92{
93 NDNS_LOG_TRACE("[* ?? *] verify data: " << data.getName() << ". KeyLocator: "
94 << data.getSignature().getKeyLocator().getName());
95 ValidatorConfig::validate(data,
Shock Jiang0e2aee02014-11-17 11:19:36 -080096 [this, onValidated] (const shared_ptr<const Data>& data) {
Shock Jiang0b165f42014-10-24 09:08:09 -070097 this->onDataValidated(data);
Shock Jiang0e2aee02014-11-17 11:19:36 -080098 onValidated(data);
Shock Jiang0b165f42014-10-24 09:08:09 -070099 },
Shock Jiang0e2aee02014-11-17 11:19:36 -0800100 [this, onValidationFailed] (const shared_ptr<const Data>& data,
101 const std::string& str) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700102 this->onDataValidationFailed(data, str);
Shock Jiang0e2aee02014-11-17 11:19:36 -0800103 onValidationFailed(data, str);
Shock Jiang0b165f42014-10-24 09:08:09 -0700104 }
105 );
106}
107
108void
109Validator::onDataValidated(const shared_ptr<const Data>& data)
110{
111 NDNS_LOG_TRACE("[* VV *] pass validation: " << data->getName() << ". KeyLocator = "
112 << data->getSignature().getKeyLocator().getName());
113}
114
115void
116Validator::onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& str)
117{
118 NDNS_LOG_WARN("[* XX *] fail validation: " << data->getName() << ". due to: " << str
119 << ". KeyLocator = " << data->getSignature().getKeyLocator().getName());
120}
121
122} // namespace ndns
123} // namespace ndn