Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 28 | namespace ndn { |
| 29 | namespace ndns { |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 30 | |
| 31 | NDNS_LOG_INIT("validator") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 32 | |
| 33 | std::string Validator::VALIDATOR_CONF_FILE = DEFAULT_CONFIG_PATH "/" "validator.conf"; |
| 34 | |
| 35 | Validator::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 Jiang | bb4e15b | 2014-12-05 09:48:02 -0800 | [diff] [blame] | 74 | config += DEFAULT_CONFIG_PATH "/" "anchors/root.cert"; |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 75 | |
| 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 | |
| 88 | void |
| 89 | Validator::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 Jiang | 0e2aee0 | 2014-11-17 11:19:36 -0800 | [diff] [blame] | 96 | [this, onValidated] (const shared_ptr<const Data>& data) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 97 | this->onDataValidated(data); |
Shock Jiang | 0e2aee0 | 2014-11-17 11:19:36 -0800 | [diff] [blame] | 98 | onValidated(data); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 99 | }, |
Shock Jiang | 0e2aee0 | 2014-11-17 11:19:36 -0800 | [diff] [blame] | 100 | [this, onValidationFailed] (const shared_ptr<const Data>& data, |
| 101 | const std::string& str) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 102 | this->onDataValidationFailed(data, str); |
Shock Jiang | 0e2aee0 | 2014-11-17 11:19:36 -0800 | [diff] [blame] | 103 | onValidationFailed(data, str); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 104 | } |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | void |
| 109 | Validator::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 | |
| 115 | void |
| 116 | Validator::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 |