blob: 721b311bb59f743d03c1ce4bdcd213e8e90f2c28 [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 {
30NDNS_LOG_INIT("validator");
31
32std::string Validator::VALIDATOR_CONF_FILE = DEFAULT_CONFIG_PATH "/" "validator.conf";
33
34Validator::Validator(Face& face, const std::string& confFile /* = VALIDATOR_CONF_FILE */)
35 : ValidatorConfig(face)
36{
37 try {
38 this->load(confFile);
39 NDNS_LOG_TRACE("Validator loads configuration: " << confFile);
40 }
41 catch (std::exception&) {
42 std::string config =
43 "rule \n"
44 "{ \n"
45 " id \"NDNS Validator\" \n"
46 " for data \n"
47 " checker \n"
48 " { \n"
49 " type customized \n"
50 " sig-type rsa-sha256 \n"
51 " key-locator \n"
52 " { \n"
53 " type name \n"
54 " hyper-relation \n"
55 " { \n"
56 " k-regex ^(<>*)<KEY>(<>*)<><ID-CERT>$ \n"
57 " k-expand \\\\1\\\\2 \n"
58 " h-relation is-prefix-of \n"
59 " p-regex ^(<>*)[<KEY><NDNS>](<>*)<><>$ \n"
60 " p-expand \\\\1\\\\2 \n"
61 " } \n"
62 " } \n"
63 " } \n"
64 "} \n"
65 " \n"
66 " \n"
67 "trust-anchor \n"
68 "{ \n"
69 " type file \n"
70 " file-name \""
71 ;
72
73 config += "anchors/root.cert";
74
75 config +=
76 "\" \n"
77 "} \n"
78 " \n"
79 ;
80
81 this->load(config, "embededConf");
82 NDNS_LOG_TRACE("Validator loads embedded configuration with anchors path: anchors/root.cert");
83 }
84
85}
86
87void
88Validator::validate(const Data& data,
89 const OnDataValidated& onValidated,
90 const OnDataValidationFailed& onValidationFailed)
91{
92 NDNS_LOG_TRACE("[* ?? *] verify data: " << data.getName() << ". KeyLocator: "
93 << data.getSignature().getKeyLocator().getName());
94 ValidatorConfig::validate(data,
95 [this, onValidated](const shared_ptr<const Data>& data)
96 // onValidated here cannot use reference, since this is non-block
97 {
98 onValidated(data);
99 this->onDataValidated(data);
100 },
101 [this, onValidationFailed](const shared_ptr<const Data>& data,
102 const std::string& str)
103 {
104 onValidationFailed(data, str);
105 this->onDataValidationFailed(data, str);
106 }
107 );
108}
109
110void
111Validator::onDataValidated(const shared_ptr<const Data>& data)
112{
113 NDNS_LOG_TRACE("[* VV *] pass validation: " << data->getName() << ". KeyLocator = "
114 << data->getSignature().getKeyLocator().getName());
115}
116
117void
118Validator::onDataValidationFailed(const shared_ptr<const Data>& data, const std::string& str)
119{
120 NDNS_LOG_WARN("[* XX *] fail validation: " << data->getName() << ". due to: " << str
121 << ". KeyLocator = " << data->getSignature().getKeyLocator().getName());
122}
123
124} // namespace ndns
125} // namespace ndn