Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "validation-policy.hpp" |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 23 | #include "../signing-info.hpp" |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 24 | |
| 25 | namespace ndn { |
| 26 | namespace security { |
| 27 | namespace v2 { |
| 28 | |
| 29 | void |
| 30 | ValidationPolicy::setInnerPolicy(unique_ptr<ValidationPolicy> innerPolicy) |
| 31 | { |
| 32 | if (innerPolicy == nullptr) { |
| 33 | BOOST_THROW_EXCEPTION(std::invalid_argument("Inner policy argument cannot be nullptr")); |
| 34 | } |
| 35 | |
| 36 | if (m_validator != nullptr) { |
| 37 | innerPolicy->setValidator(*m_validator); |
| 38 | } |
| 39 | |
| 40 | if (m_innerPolicy == nullptr) { |
| 41 | m_innerPolicy = std::move(innerPolicy); |
| 42 | } |
| 43 | else { |
| 44 | m_innerPolicy->setInnerPolicy(std::move(innerPolicy)); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | ValidationPolicy& |
| 49 | ValidationPolicy::getInnerPolicy() |
| 50 | { |
| 51 | return *m_innerPolicy; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ValidationPolicy::setValidator(Validator& validator) |
| 56 | { |
| 57 | m_validator = &validator; |
| 58 | if (m_innerPolicy != nullptr) { |
| 59 | m_innerPolicy->setValidator(validator); |
| 60 | } |
| 61 | } |
| 62 | |
Junxiao Shi | 830ba97 | 2017-06-23 22:44:41 +0000 | [diff] [blame] | 63 | static Name |
| 64 | getKeyLocatorName(const SignatureInfo& si, ValidationState& state) |
| 65 | { |
Alexander Afanasyev | 31fd467 | 2018-06-17 13:25:52 -0400 | [diff] [blame] | 66 | if (si.getSignatureType() == tlv::DigestSha256) { |
| 67 | return SigningInfo::getDigestSha256Identity(); |
| 68 | } |
| 69 | |
Junxiao Shi | 830ba97 | 2017-06-23 22:44:41 +0000 | [diff] [blame] | 70 | if (!si.hasKeyLocator()) { |
| 71 | state.fail({ValidationError::Code::INVALID_KEY_LOCATOR, "KeyLocator is missing"}); |
| 72 | return Name(); |
| 73 | } |
| 74 | |
| 75 | const KeyLocator& kl = si.getKeyLocator(); |
| 76 | if (kl.getType() != KeyLocator::KeyLocator_Name) { |
| 77 | state.fail({ValidationError::Code::INVALID_KEY_LOCATOR, "KeyLocator type is not Name"}); |
| 78 | return Name(); |
| 79 | } |
| 80 | |
| 81 | return kl.getName(); |
| 82 | } |
| 83 | |
| 84 | Name |
| 85 | getKeyLocatorName(const Data& data, ValidationState& state) |
| 86 | { |
| 87 | return getKeyLocatorName(data.getSignature().getSignatureInfo(), state); |
| 88 | } |
| 89 | |
| 90 | Name |
| 91 | getKeyLocatorName(const Interest& interest, ValidationState& state) |
| 92 | { |
| 93 | const Name& name = interest.getName(); |
| 94 | if (name.size() < signed_interest::MIN_SIZE) { |
| 95 | state.fail({ValidationError::INVALID_KEY_LOCATOR, |
| 96 | "Invalid signed Interest: name too short"}); |
| 97 | return Name(); |
| 98 | } |
| 99 | |
| 100 | SignatureInfo si; |
| 101 | try { |
| 102 | si.wireDecode(name.at(signed_interest::POS_SIG_INFO).blockFromValue()); |
| 103 | } |
| 104 | catch (const tlv::Error& e) { |
| 105 | state.fail({ValidationError::Code::INVALID_KEY_LOCATOR, |
| 106 | "Invalid signed Interest: " + std::string(e.what())}); |
| 107 | return Name(); |
| 108 | } |
| 109 | |
| 110 | return getKeyLocatorName(si, state); |
| 111 | } |
| 112 | |
Alexander Afanasyev | b54aa57 | 2017-03-21 19:40:49 -0500 | [diff] [blame] | 113 | } // namespace v2 |
| 114 | } // namespace security |
| 115 | } // namespace ndn |