NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
validation-policy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "validation-policy.hpp"
23 
24 namespace ndn {
25 namespace security {
26 namespace v2 {
27 
28 void
29 ValidationPolicy::setInnerPolicy(unique_ptr<ValidationPolicy> innerPolicy)
30 {
31  if (innerPolicy == nullptr) {
32  BOOST_THROW_EXCEPTION(std::invalid_argument("Inner policy argument cannot be nullptr"));
33  }
34 
35  if (m_validator != nullptr) {
36  innerPolicy->setValidator(*m_validator);
37  }
38 
39  if (m_innerPolicy == nullptr) {
40  m_innerPolicy = std::move(innerPolicy);
41  }
42  else {
43  m_innerPolicy->setInnerPolicy(std::move(innerPolicy));
44  }
45 }
46 
49 {
50  return *m_innerPolicy;
51 }
52 
53 void
55 {
56  m_validator = &validator;
57  if (m_innerPolicy != nullptr) {
58  m_innerPolicy->setValidator(validator);
59  }
60 }
61 
62 static Name
64 {
65  if (!si.hasKeyLocator()) {
66  state.fail({ValidationError::Code::INVALID_KEY_LOCATOR, "KeyLocator is missing"});
67  return Name();
68  }
69 
70  const KeyLocator& kl = si.getKeyLocator();
72  state.fail({ValidationError::Code::INVALID_KEY_LOCATOR, "KeyLocator type is not Name"});
73  return Name();
74  }
75 
76  return kl.getName();
77 }
78 
79 Name
81 {
82  return getKeyLocatorName(data.getSignature().getSignatureInfo(), state);
83 }
84 
85 Name
86 getKeyLocatorName(const Interest& interest, ValidationState& state)
87 {
88  const Name& name = interest.getName();
89  if (name.size() < signed_interest::MIN_SIZE) {
91  "Invalid signed Interest: name too short"});
92  return Name();
93  }
94 
95  SignatureInfo si;
96  try {
97  si.wireDecode(name.at(signed_interest::POS_SIG_INFO).blockFromValue());
98  }
99  catch (const tlv::Error& e) {
100  state.fail({ValidationError::Code::INVALID_KEY_LOCATOR,
101  "Invalid signed Interest: " + std::string(e.what())});
102  return Name();
103  }
104 
105  return getKeyLocatorName(si, state);
106 }
107 
108 } // namespace v2
109 } // namespace security
110 } // namespace ndn
void setInnerPolicy(unique_ptr< ValidationPolicy > innerPolicy)
Set inner policy.
Copyright (c) 2011-2015 Regents of the University of California.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Represents a SignatureInfo TLV element.
void setValidator(Validator &validator)
Set validator to which the policy is associated.
const Name & getName() const
get Name element
const size_t MIN_SIZE
minimal number of components for Signed Interest
represents an Interest packet
Definition: interest.hpp:42
ValidationPolicy & getInnerPolicy()
Return the inner policy.
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
Abstraction that implements validation policy for Data and Interest packets.
unique_ptr< ValidationPolicy > m_innerPolicy
const Signature & getSignature() const
Get Signature.
Definition: data.hpp:182
static Name getKeyLocatorName(const SignatureInfo &si, ValidationState &state)
bool hasKeyLocator() const
Check if KeyLocator exists.
Represents an absolute name.
Definition: name.hpp:42
void wireDecode(const Block &wire)
Decode from wire format.
const ssize_t POS_SIG_INFO
Type getType() const
virtual void fail(const ValidationError &error)=0
Call the failure callback.
const SignatureInfo & getSignatureInfo() const
Get SignatureInfo.
Definition: signature.hpp:69
Represents a Data packet.
Definition: data.hpp:35
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
Interface for validating data and interest packets.
Definition: validator.hpp:61
const Name & getName() const
Definition: interest.hpp:139