blob: ee1ad941fe3e076b01fdb9f2e27e7504bae15e20 [file] [log] [blame]
Alexander Afanasyev7e721412017-01-11 13:36:08 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
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-simple-hierarchy.hpp"
23
24namespace ndn {
25namespace security {
26namespace v2 {
27
28void
29ValidationPolicySimpleHierarchy::checkPolicy(const Data& data, const shared_ptr<ValidationState>& state,
30 const ValidationContinuation& continueValidation)
31{
32 if (!data.getSignature().hasKeyLocator()) {
33 return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Required key locator is missing"});
34 }
35 const KeyLocator& locator = data.getSignature().getKeyLocator();
36 if (locator.getType() != KeyLocator::KeyLocator_Name) {
37 return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Key locator not Name"});
38 }
39 if (locator.getName().getPrefix(-2).isPrefixOf(data.getName())) {
40 continueValidation(make_shared<CertificateRequest>(Interest(locator.getName())), state);
41 }
42 else {
43 state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Data signing policy violation for " +
44 data.getName().toUri() + " by " + locator.getName().toUri()});
45 }
46}
47
48void
49ValidationPolicySimpleHierarchy::checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
50 const ValidationContinuation& continueValidation)
51{
52 SignatureInfo info;
53 try {
54 info.wireDecode(interest.getName().at(signed_interest::POS_SIG_INFO).blockFromValue());
55 }
56 catch (const tlv::Error& e) {
57 return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Invalid signed interest (" +
58 std::string(e.what()) + ")"});
59 }
60 if (!info.hasKeyLocator()) {
61 return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Required key locator is missing"});
62 }
63 const KeyLocator& locator = info.getKeyLocator();
64 if (locator.getType() != KeyLocator::KeyLocator_Name) {
65 return state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Key locator not Name"});
66 }
67 if (locator.getName().getPrefix(-2).isPrefixOf(interest.getName())) {
68 continueValidation(make_shared<CertificateRequest>(Interest(locator.getName())), state);
69 }
70 else {
71 state->fail({ValidationError::Code::INVALID_KEY_LOCATOR, "Interest signing policy violation for " +
72 interest.getName().toUri() + " by " + locator.getName().toUri()});
73 }
74}
75
76} // namespace v2
77} // namespace security
78} // namespace ndn