validation-policy-simple-hierarchy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 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 
23 
24 namespace ndn {
25 namespace security {
26 inline namespace v2 {
27 
28 void
29 ValidationPolicySimpleHierarchy::checkPolicy(const Data& data, const shared_ptr<ValidationState>& state,
30  const ValidationContinuation& continueValidation)
31 {
32  Name klName = getKeyLocatorName(data.getSignatureInfo(), *state);
33  if (!state->getOutcome()) { // already failed
34  return;
35  }
36 
37  Name identity;
38  try {
39  identity = extractIdentityNameFromKeyLocator(klName);
40  }
41  catch (const KeyLocator::Error& e) {
42  state->fail({ValidationError::INVALID_KEY_LOCATOR, e.what()});
43  return;
44  }
45 
46  if (!identity.isPrefixOf(data.getName())) {
47  state->fail({ValidationError::POLICY_ERROR,
48  "Data " + data.getName().toUri() + " signed by " + klName.toUri()});
49  return;
50  }
51 
52  continueValidation(make_shared<CertificateRequest>(klName), state);
53 }
54 
55 void
56 ValidationPolicySimpleHierarchy::checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
57  const ValidationContinuation& continueValidation)
58 {
59  auto sigInfo = getSignatureInfo(interest, *state);
60  if (!state->getOutcome()) { // already failed
61  return;
62  }
63  Name klName = getKeyLocatorName(sigInfo, *state);
64  if (!state->getOutcome()) { // already failed
65  return;
66  }
67 
68  Name identity;
69  try {
70  identity = extractIdentityNameFromKeyLocator(klName);
71  }
72  catch (const KeyLocator::Error& e) {
73  state->fail({ValidationError::INVALID_KEY_LOCATOR, e.what()});
74  return;
75  }
76 
77  if (!identity.isPrefixOf(interest.getName())) {
78  state->fail({ValidationError::POLICY_ERROR,
79  "Interest " + interest.getName().toUri() + " signed by " + klName.toUri()});
80  return;
81  }
82 
83  continueValidation(make_shared<CertificateRequest>(klName), state);
84 }
85 
86 } // inline namespace v2
87 } // namespace security
88 } // namespace ndn
Represents a Data packet.
Definition: data.hpp:39
const SignatureInfo & getSignatureInfo() const noexcept
Get the SignatureInfo element.
Definition: data.hpp:224
const Name & getName() const noexcept
Get the data name.
Definition: data.hpp:129
Represents an Interest packet.
Definition: interest.hpp:50
const Name & getName() const noexcept
Definition: interest.hpp:173
Represents an absolute name.
Definition: name.hpp:44
void toUri(std::ostream &os, name::UriFormat format=name::UriFormat::DEFAULT) const
Write URI representation of the name to the output stream.
Definition: name.cpp:349
bool isPrefixOf(const Name &other) const noexcept
Check if this name is a prefix of another name.
Definition: name.cpp:300
@ POLICY_ERROR
The packet violates the validation rules enforced by the policy.
@ INVALID_KEY_LOCATOR
The KeyLocator element is missing or has an invalid format.
void checkPolicy(const Data &data, const shared_ptr< ValidationState > &state, const ValidationContinuation &continueValidation) override
Check data against the policy.
std::function< void(const shared_ptr< CertificateRequest > &certRequest, const shared_ptr< ValidationState > &state)> ValidationContinuation
Name getKeyLocatorName(const SignatureInfo &si, ValidationState &state)
Extract the KeyLocator name from a SignatureInfo element.
SignatureInfo getSignatureInfo(const Interest &interest, ValidationState &state)
Extract SignatureInfo from a signed Interest.
Name extractIdentityNameFromKeyLocator(const Name &keyLocator)
Extract identity name from key, version-less certificate, or certificate name.
Definition: data.cpp:25