NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: NDN, CCN, CCNx, content centric networks
API Documentation
validator-regex.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #include "common.hpp"
25 
26 #include "validator-regex.hpp"
29 
30 namespace ndn {
31 namespace security {
32 
33 const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE;
34 
36  shared_ptr<CertificateCache> certificateCache,
37  const int stepLimit)
38  : Validator(face)
39  , m_stepLimit(stepLimit)
40  , m_certificateCache(certificateCache)
41 {
42  if (!static_cast<bool>(m_certificateCache) && face != nullptr)
43  m_certificateCache = make_shared<CertificateCacheTtl>(ref(face->getIoService()));
44 }
45 
47  shared_ptr<CertificateCache> certificateCache,
48  const int stepLimit)
49  : Validator(face)
50  , m_stepLimit(stepLimit)
51  , m_certificateCache(certificateCache)
52 {
53  if (certificateCache == nullptr)
54  m_certificateCache = make_shared<CertificateCacheTtl>(ref(face.getIoService()));
55 }
56 
57 void
58 ValidatorRegex::addDataVerificationRule(shared_ptr<SecRuleRelative> rule)
59 {
60  rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule);
61 }
62 
63 void
64 ValidatorRegex::addTrustAnchor(shared_ptr<v1::IdentityCertificate> certificate)
65 {
66  m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate;
67 }
68 
69 void
70 ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate,
71  const shared_ptr<const Data>& data,
72  const OnDataValidated& onValidated,
73  const OnDataValidationFailed& onValidationFailed)
74 {
75  shared_ptr<v1::IdentityCertificate> certificate =
76  make_shared<v1::IdentityCertificate>(*signCertificate);
77 
78  if (!certificate->isTooLate() && !certificate->isTooEarly()) {
79  if (m_certificateCache != nullptr)
80  m_certificateCache->insertCertificate(certificate);
81 
82  if (verifySignature(*data, certificate->getPublicKeyInfo()))
83  return onValidated(data);
84  else
85  return onValidationFailed(data,
86  "Cannot verify signature: " +
87  data->getName().toUri());
88  }
89  else {
90  return onValidationFailed(data,
91  "Signing certificate " +
92  signCertificate->getName().toUri() +
93  " is no longer valid.");
94  }
95 }
96 
97 void
98 ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate,
99  const std::string& failureInfo,
100  const shared_ptr<const Data>& data,
101  const OnDataValidationFailed& onValidationFailed)
102 {
103  onValidationFailed(data, failureInfo);
104 }
105 
106 void
108  int nSteps,
109  const OnDataValidated& onValidated,
110  const OnDataValidationFailed& onValidationFailed,
111  std::vector<shared_ptr<ValidationRequest> >& nextSteps)
112 {
113  if (m_stepLimit == nSteps)
114  return onValidationFailed(data.shared_from_this(),
115  "Maximum steps of validation reached: " +
116  data.getName().toUri());
117 
118  for (RuleList::iterator it = m_mustFailVerify.begin();
119  it != m_mustFailVerify.end();
120  it++)
121  if ((*it)->satisfy(data))
122  return onValidationFailed(data.shared_from_this(),
123  "Comply with mustFail policy: " +
124  data.getName().toUri());
125 
126  for (RuleList::iterator it = m_verifyPolicies.begin();
127  it != m_verifyPolicies.end();
128  it++) {
129  if ((*it)->satisfy(data)) {
130  try {
131  if (!data.getSignature().hasKeyLocator())
132  return onValidationFailed(data.shared_from_this(),
133  "Key Locator is missing in Data packet: " +
134  data.getName().toUri());
135 
136  const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
137  if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
138  return onValidationFailed(data.shared_from_this(),
139  "Key Locator is not a name: " +
140  data.getName().toUri());
141 
142 
143  const Name& keyLocatorName = keyLocator.getName();
144  shared_ptr<const v1::Certificate> trustedCert;
145  if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName) &&
146  m_certificateCache != nullptr)
147  trustedCert = m_certificateCache->getCertificate(keyLocatorName);
148  else
149  trustedCert = m_trustAnchors[keyLocatorName];
150 
151  if (trustedCert != nullptr) {
152  if (verifySignature(data, data.getSignature(), trustedCert->getPublicKeyInfo()))
153  return onValidated(data.shared_from_this());
154  else
155  return onValidationFailed(data.shared_from_this(),
156  "Cannot verify signature: " +
157  data.getName().toUri());
158  }
159  else {
160  // KeyLocator is not a trust anchor
161 
162  OnDataValidated onKeyValidated =
164  data.shared_from_this(), onValidated, onValidationFailed);
165 
166  OnDataValidationFailed onKeyValidationFailed =
168  data.shared_from_this(), onValidationFailed);
169 
170  Interest interest(keyLocatorName);
171  shared_ptr<ValidationRequest> nextStep =
172  make_shared<ValidationRequest>(interest,
173  onKeyValidated,
174  onKeyValidationFailed,
175  3,
176  nSteps + 1);
177 
178  nextSteps.push_back(nextStep);
179 
180  return;
181  }
182  }
183  catch (const KeyLocator::Error& e) {
184  return onValidationFailed(data.shared_from_this(),
185  "Key Locator is not a name: " +
186  data.getName().toUri());
187  }
188  catch (const tlv::Error& e) {
189  return onValidationFailed(data.shared_from_this(),
190  "Cannot decode signature");
191  }
192  }
193  }
194 
195  return onValidationFailed(data.shared_from_this(),
196  "No policy found for data: " + data.getName().toUri());
197 }
198 
199 } // namespace security
200 } // namespace ndn
Copyright (c) 2011-2015 Regents of the University of California.
std::string toUri() const
Encode this name as a URI.
Definition: name.cpp:171
void addDataVerificationRule(shared_ptr< SecRuleRelative > rule)
Add a rule for data verification.
const Name & getName() const
Get name of the Data packet.
Definition: data.hpp:318
const Name & getName() const
get Name element
represents an Interest packet
Definition: interest.hpp:42
void addTrustAnchor(shared_ptr< v1::IdentityCertificate > certificate)
Add a trust anchor.
indicates KeyLocator contains a Name
Definition: key-locator.hpp:49
function< void(const shared_ptr< const Data > &, const std::string &)> OnDataValidationFailed
Callback to report a failed Data validation.
bool hasKeyLocator() const
Check if SignatureInfo block has a KeyLocator.
Definition: signature.hpp:132
function< void(const shared_ptr< const Data > &)> OnDataValidated
Callback to report a successful Data validation.
Table::const_iterator iterator
Definition: cs-internal.hpp:41
static const shared_ptr< CertificateCache > DEFAULT_CERTIFICATE_CACHE
virtual void checkPolicy(const Data &data, int nSteps, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed, std::vector< shared_ptr< ValidationRequest > > &nextSteps)
void onCertificateValidated(const shared_ptr< const Data > &signCertificate, const shared_ptr< const Data > &data, const OnDataValidated &onValidated, const OnDataValidationFailed &onValidationFailed)
provides the interfaces for packet validation.
Definition: validator.hpp:42
Provide a communication channel with local or remote NDN forwarder.
Definition: face.hpp:125
std::map< Name, shared_ptr< v1::IdentityCertificate > > m_trustAnchors
Name abstraction to represent an absolute name.
Definition: name.hpp:46
shared_ptr< CertificateCache > m_certificateCache
boost::asio::io_service & getIoService()
Return nullptr (cannot use IoService in simulations), preserved for API compatibility.
Definition: face.hpp:690
void onCertificateValidationFailed(const shared_ptr< const Data > &signCertificate, const std::string &failureInfo, const shared_ptr< const Data > &data, const OnDataValidationFailed &onValidationFailed)
Type getType() const
static bool verifySignature(const Data &data, const v1::PublicKey &publicKey)
Verify the data using the publicKey.
Definition: validator.cpp:105
const Signature & getSignature() const
Definition: data.hpp:348
const KeyLocator & getKeyLocator() const
Get KeyLocator.
Definition: signature.hpp:143
represents a Data packet
Definition: data.hpp:37
ValidatorRegex(Face *face=nullptr, shared_ptr< CertificateCache > certificateCache=DEFAULT_CERTIFICATE_CACHE, const int stepLimit=3)
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50