validation-policy.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2020 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 
24 
25 namespace ndn {
26 namespace security {
27 inline namespace v2 {
28 
29 void
30 ValidationPolicy::setInnerPolicy(unique_ptr<ValidationPolicy> innerPolicy)
31 {
32  if (innerPolicy == nullptr) {
33  NDN_THROW(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 
50 {
51  return *m_innerPolicy;
52 }
53 
54 void
56 {
57  m_validator = &validator;
58  if (m_innerPolicy != nullptr) {
59  m_innerPolicy->setValidator(validator);
60  }
61 }
62 
63 static Name
64 getKeyLocatorName(const SignatureInfo& si, ValidationState& state)
65 {
66  if (si.getSignatureType() == tlv::DigestSha256) {
68  }
69 
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() != tlv::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.getSignatureInfo(), state);
88 }
89 
90 Name
91 getKeyLocatorName(const Interest& interest, ValidationState& state)
92 {
93  auto fmt = state.getTag<SignedInterestFormatTag>();
94  BOOST_ASSERT(fmt);
95 
96  if (*fmt == SignedInterestFormat::V03) {
97  BOOST_ASSERT(interest.getSignatureInfo());
98  return getKeyLocatorName(*interest.getSignatureInfo(), state);
99  }
100 
101  // Try Signed Interest format from Packet Specification v0.2
102  const Name& name = interest.getName();
103  if (name.size() < signed_interest::MIN_SIZE) {
104  state.fail({ValidationError::INVALID_KEY_LOCATOR, "Invalid signed Interest: name too short"});
105  return Name();
106  }
107 
108  SignatureInfo si;
109  try {
111  }
112  catch (const tlv::Error& e) {
113  state.fail({ValidationError::Code::INVALID_KEY_LOCATOR,
114  "Invalid signed Interest: " + std::string(e.what())});
115  return Name();
116  }
117 
118  return getKeyLocatorName(si, state);
119 }
120 
121 Name
123 {
124  // handling special cases
125  if (keyLocator == SigningInfo::getDigestSha256Identity() ||
126  keyLocator == SigningInfo::getHmacIdentity()) {
127  return keyLocator;
128  }
129 
130  for (ssize_t i = 1; i <= std::min<ssize_t>(-Certificate::KEY_COMPONENT_OFFSET, keyLocator.size()); ++i) {
131  if (keyLocator[-i] == Certificate::KEY_COMPONENT) {
132  return keyLocator.getPrefix(-i);
133  }
134  }
135 
136  NDN_THROW(KeyLocator::Error("KeyLocator name `" + keyLocator.toUri() + "` does not respect the naming conventions"));
137 }
138 
139 } // inline namespace v2
140 } // namespace security
141 } // namespace ndn
Block blockFromValue() const
Definition: block.cpp:329
Represents a Data packet.
Definition: data.hpp:38
const SignatureInfo & getSignatureInfo() const noexcept
Get SignatureInfo.
Definition: data.hpp:227
Represents an Interest packet.
Definition: interest.hpp:50
const Name & getName() const noexcept
Definition: interest.hpp:173
optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo.
Definition: interest.cpp:549
Represents an absolute name.
Definition: name.hpp:46
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:213
const Component & at(ssize_t i) const
Returns an immutable reference to the component at the specified index, with bounds checking.
Definition: name.cpp:171
size_t size() const
Returns the number of components.
Definition: name.hpp:155
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:348
Represents a SignatureInfo or InterestSignatureInfo TLV element.
int32_t getSignatureType() const noexcept
Get SignatureType.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
void wireDecode(const Block &wire, Type type=Type::Data)
Decode from wire format.
const KeyLocator & getKeyLocator() const
Get KeyLocator.
provides a tag type for simple types
Definition: tag.hpp:59
shared_ptr< T > getTag() const
get a tag item
Definition: tag-host.hpp:66
static const Name & getDigestSha256Identity()
A localhost identity to indicate that the signature is generated using SHA-256.
static const Name & getHmacIdentity()
A localhost identity to indicate that the signature is generated using an HMAC key.
static const name::Component KEY_COMPONENT
static const ssize_t KEY_COMPONENT_OFFSET
Abstraction that implements validation policy for Data and Interest packets.
ValidationPolicy & getInnerPolicy()
Return the inner policy.
void setValidator(Validator &validator)
Set validator to which the policy is associated.
unique_ptr< ValidationPolicy > m_innerPolicy
void setInnerPolicy(unique_ptr< ValidationPolicy > innerPolicy)
Set inner policy.
virtual void fail(const ValidationError &error)=0
Call the failure callback.
Interface for validating data and interest packets.
Definition: validator.hpp:62
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
#define NDN_THROW(e)
Definition: exception.hpp:61
Name extractIdentityNameFromKeyLocator(const Name &keyLocator)
Extract identity name from key, version-less certificate, or certificate name.
@ V03
Sign Interest using Packet Specification v0.3 semantics.
const size_t MIN_SIZE
minimal number of components for Signed Interest
const ssize_t POS_SIG_INFO
@ Name
Definition: tlv.hpp:67
@ KeyLocator
Definition: tlv.hpp:88
@ DigestSha256
Definition: tlv.hpp:133
Definition: data.cpp:25