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-2023 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::security {
26 
27 void
28 ValidationPolicy::setInnerPolicy(unique_ptr<ValidationPolicy> innerPolicy)
29 {
30  if (innerPolicy == nullptr) {
31  NDN_THROW(std::invalid_argument("Inner policy argument cannot be nullptr"));
32  }
33 
34  if (m_validator != nullptr) {
35  innerPolicy->setValidator(*m_validator);
36  }
37 
38  if (m_innerPolicy == nullptr) {
39  m_innerPolicy = std::move(innerPolicy);
40  }
41  else {
42  m_innerPolicy->setInnerPolicy(std::move(innerPolicy));
43  }
44 }
45 
48 {
49  return *m_innerPolicy;
50 }
51 
52 void
54 {
55  m_validator = &validator;
56  if (m_innerPolicy != nullptr) {
57  m_innerPolicy->setValidator(validator);
58  }
59 }
60 
61 Name
63 {
64  if (si.getSignatureType() == tlv::DigestSha256) {
66  }
67 
68  if (!si.hasKeyLocator()) {
69  state.fail({ValidationError::INVALID_KEY_LOCATOR, "KeyLocator is missing"});
70  return {};
71  }
72 
73  const KeyLocator& kl = si.getKeyLocator();
74  if (kl.getType() != tlv::Name) {
75  state.fail({ValidationError::INVALID_KEY_LOCATOR, "KeyLocator type is not Name"});
76  return {};
77  }
78 
79  return kl.getName();
80 }
81 
83 getSignatureInfo(const Interest& interest, ValidationState& state)
84 {
85  auto fmt = state.getTag<SignedInterestFormatTag>();
86  BOOST_ASSERT(fmt);
87 
88  if (*fmt == SignedInterestFormat::V03) {
89  BOOST_ASSERT(interest.getSignatureInfo().has_value());
90  return *interest.getSignatureInfo();
91  }
92 
93  // Try the old Signed Interest format from Packet Specification v0.2
94  const Name& name = interest.getName();
95  if (name.size() < signed_interest::MIN_SIZE) {
97  "Interest name too short `" + name.toUri() + "`"});
98  return {};
99  }
100 
101  try {
102  return SignatureInfo(name[signed_interest::POS_SIG_INFO].blockFromValue());
103  }
104  catch (const tlv::Error& e) {
106  "Malformed SignatureInfo in `" + name.toUri() + "`: " + e.what()});
107  return {};
108  }
109 }
110 
111 Name
113 {
114  // handling special cases
115  if (keyLocator == SigningInfo::getDigestSha256Identity() ||
116  keyLocator == SigningInfo::getHmacIdentity()) {
117  return keyLocator;
118  }
119 
120  auto len = static_cast<ssize_t>(keyLocator.size());
121  // note that KEY_COMPONENT_OFFSET is negative
122  auto lowerBound = std::max<ssize_t>(len + Certificate::KEY_COMPONENT_OFFSET, 0);
123  for (ssize_t i = len - 1; i >= lowerBound; --i) {
124  if (keyLocator[i] == Certificate::KEY_COMPONENT) {
125  return keyLocator.getPrefix(i);
126  }
127  }
128 
129  NDN_THROW(KeyLocator::Error("KeyLocator `" + keyLocator.toUri() +
130  "` does not respect the naming conventions"));
131 }
132 
133 } // namespace ndn::security
Represents an Interest packet.
Definition: interest.hpp:50
std::optional< SignatureInfo > getSignatureInfo() const
Get the InterestSignatureInfo element.
Definition: interest.cpp:552
const Name & getName() const noexcept
Get the Interest name.
Definition: interest.hpp:179
const Name & getName() const
Get nested Name element.
uint32_t getType() const
Represents an absolute name.
Definition: name.hpp:45
PartialName getPrefix(ssize_t nComponents) const
Returns a prefix of the name.
Definition: name.hpp:241
size_t size() const noexcept
Returns the number of components.
Definition: name.hpp:180
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:324
Represents a SignatureInfo or InterestSignatureInfo TLV element.
int32_t getSignatureType() const noexcept
Get the SignatureType.
bool hasKeyLocator() const noexcept
Check if KeyLocator is present.
const KeyLocator & getKeyLocator() const
Get the KeyLocator element.
Provides a tag type for simple types.
Definition: tag.hpp:56
std::shared_ptr< T > getTag() const
Get a tag item.
Definition: tag-host.hpp:72
static constexpr ssize_t KEY_COMPONENT_OFFSET
static const name::Component KEY_COMPONENT
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.
@ INVALID_KEY_LOCATOR
The KeyLocator element is missing or has an invalid format.
@ MALFORMED_SIGNATURE
The signature (e.g., SignatureInfo element) is missing or malformed.
Abstraction that implements a validation policy for Interest and Data packets.
void setValidator(Validator &validator)
Set validator to which the policy is associated.
ValidationPolicy & getInnerPolicy()
Return the inner policy.
void setInnerPolicy(unique_ptr< ValidationPolicy > innerPolicy)
Set inner policy.
unique_ptr< ValidationPolicy > m_innerPolicy
virtual void fail(const ValidationError &error)=0
Call the failure callback.
Interface for validating data and interest packets.
Definition: validator.hpp:61
Represents an error in TLV encoding or decoding.
Definition: tlv.hpp:54
#define NDN_THROW(e)
Definition: exception.hpp:56
Contains the ndn-cxx security framework.
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.
SignatureInfo getSignatureInfo(const Interest &interest, ValidationState &state)
Extract SignatureInfo from a signed Interest.
Name getKeyLocatorName(const SignatureInfo &si, ValidationState &state)
Extract the KeyLocator name from a SignatureInfo element.
constexpr size_t MIN_SIZE
Minimum number of name components for an old-style Signed Interest.
constexpr ssize_t POS_SIG_INFO
@ Name
Definition: tlv.hpp:71
@ SignatureInfo
Definition: tlv.hpp:94
@ DigestSha256
Definition: tlv.hpp:128