validation-policy-simple-hierarchy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
23 
24 namespace ndn {
25 namespace security {
26 namespace v2 {
27 
28 void
29 ValidationPolicySimpleHierarchy::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 
48 void
49 ValidationPolicySimpleHierarchy::checkPolicy(const Interest& interest, const shared_ptr<ValidationState>& state,
50  const ValidationContinuation& continueValidation)
51 {
52  SignatureInfo info;
53  try {
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
const Name & getName() const
Definition: interest.hpp:226
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
bool hasKeyLocator() const
Check if SignatureInfo block has a KeyLocator.
Definition: signature.hpp:132
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Definition: signature.hpp:143
represents an Interest packet
Definition: interest.hpp:42
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
const KeyLocator & getKeyLocator() const
Get KeyLocator.
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:171
const Name & getName() const
get Name element
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
Block blockFromValue() const
Definition: block.cpp:437
Type getType() const
const Signature & getSignature() const
Definition: data.hpp:348
void wireDecode(const Block &wire)
Decode from a wire format.
const ssize_t POS_SIG_INFO
bool hasKeyLocator() const
Check if KeyLocator is set.
bool isPrefixOf(const Name &name) const
Check if the N components of this name are the same as the first N components of the given name...
Definition: name.cpp:308
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
Definition: name.hpp:241
represents a Data packet
Definition: data.hpp:37
const Component & at(ssize_t i) const
Get component at the specified index.
Definition: name.hpp:434
represents an error in TLV encoding or decoding