blob: 08f4c73fa809a729dc9a33a6b788bea02b85d8e2 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yingdi Yu6ac97982014-01-30 14:49:21 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Yingdi Yu6ac97982014-01-30 14:49:21 -080022 */
23
Alexander Afanasyeve2dcdfd2014-02-07 15:53:28 -080024#include "common.hpp"
25
Yingdi Yu6ac97982014-01-30 14:49:21 -080026#include "validator-regex.hpp"
27#include "signature-sha256-with-rsa.hpp"
28#include "certificate-cache-ttl.hpp"
29
Yingdi Yufc40d872014-02-18 12:56:04 -080030namespace ndn {
Yingdi Yu6ac97982014-01-30 14:49:21 -080031
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070032const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE;
Yingdi Yu6ac97982014-01-30 14:49:21 -080033
Yingdi Yu4e9b0692014-11-04 16:13:56 -080034ValidatorRegex::ValidatorRegex(Face* face,
35 shared_ptr<CertificateCache> certificateCache,
36 const int stepLimit)
37 : Validator(face)
38 , m_stepLimit(stepLimit)
39 , m_certificateCache(certificateCache)
40{
41 if (!static_cast<bool>(m_certificateCache) && face != nullptr)
42 m_certificateCache = make_shared<CertificateCacheTtl>(ref(face->getIoService()));
43}
44
Yingdi Yu96e64062014-04-15 19:57:33 -070045ValidatorRegex::ValidatorRegex(Face& face,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070046 shared_ptr<CertificateCache> certificateCache,
47 const int stepLimit)
Yingdi Yu6ac97982014-01-30 14:49:21 -080048 : Validator(face)
49 , m_stepLimit(stepLimit)
50 , m_certificateCache(certificateCache)
51{
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070052 if (!static_cast<bool>(m_certificateCache))
Yingdi Yu4e9b0692014-11-04 16:13:56 -080053 m_certificateCache = make_shared<CertificateCacheTtl>(ref(face.getIoService()));
54}
55
56void
57ValidatorRegex::addDataVerificationRule(shared_ptr<SecRuleRelative> rule)
58{
59 rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule);
60}
61
62void
63ValidatorRegex::addTrustAnchor(shared_ptr<IdentityCertificate> certificate)
64{
65 m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate;
Yingdi Yu96e64062014-04-15 19:57:33 -070066}
67
Yingdi Yu6ac97982014-01-30 14:49:21 -080068void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070069ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate,
70 const shared_ptr<const Data>& data,
71 const OnDataValidated& onValidated,
72 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu6ac97982014-01-30 14:49:21 -080073{
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070074 shared_ptr<IdentityCertificate> certificate =
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070075 make_shared<IdentityCertificate>(*signCertificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070076
77 if (!certificate->isTooLate() && !certificate->isTooEarly())
Yingdi Yu6ac97982014-01-30 14:49:21 -080078 {
Yingdi Yu4e9b0692014-11-04 16:13:56 -080079 if (static_cast<bool>(m_certificateCache))
80 m_certificateCache->insertCertificate(certificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070081
82 if (verifySignature(*data, certificate->getPublicKeyInfo()))
Yingdi Yu40587c02014-02-21 16:40:48 -080083 return onValidated(data);
84 else
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070085 return onValidationFailed(data,
86 "Cannot verify signature: " +
87 data->getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -080088 }
89 else
90 {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070091 return onValidationFailed(data,
92 "Signing certificate " +
93 signCertificate->getName().toUri() +
94 " is no longer valid.");
Yingdi Yu6ac97982014-01-30 14:49:21 -080095 }
96}
97
98void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070099ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate,
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700100 const std::string& failureInfo,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700101 const shared_ptr<const Data>& data,
102 const OnDataValidationFailed& onValidationFailed)
103{
104 onValidationFailed(data, failureInfo);
105}
Yingdi Yu6ac97982014-01-30 14:49:21 -0800106
107void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700108ValidatorRegex::checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700109 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700110 const OnDataValidated& onValidated,
111 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700112 std::vector<shared_ptr<ValidationRequest> >& nextSteps)
Yingdi Yu6ac97982014-01-30 14:49:21 -0800113{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700114 if (m_stepLimit == nSteps)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700115 return onValidationFailed(data.shared_from_this(),
116 "Maximum steps of validation reached: " +
117 data.getName().toUri());
Yingdi Yu40587c02014-02-21 16:40:48 -0800118
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700119 for (RuleList::iterator it = m_mustFailVerify.begin();
120 it != m_mustFailVerify.end();
121 it++)
122 if ((*it)->satisfy(data))
Yingdi Yu40587c02014-02-21 16:40:48 -0800123 return onValidationFailed(data.shared_from_this(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700124 "Comply with mustFail policy: " +
125 data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800126
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700127 for (RuleList::iterator it = m_verifyPolicies.begin();
128 it != m_verifyPolicies.end();
129 it++)
Yingdi Yu6ac97982014-01-30 14:49:21 -0800130 {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700131 if ((*it)->satisfy(data))
Yingdi Yu6ac97982014-01-30 14:49:21 -0800132 {
Yingdi Yu40587c02014-02-21 16:40:48 -0800133 try
134 {
Yingdi Yu4a557052014-07-09 16:40:37 -0700135 if (!data.getSignature().hasKeyLocator())
136 return onValidationFailed(data.shared_from_this(),
137 "Key Locator is missing in Data packet: " +
138 data.getName().toUri());
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700139
Yingdi Yu4a557052014-07-09 16:40:37 -0700140 const KeyLocator& keyLocator = data.getSignature().getKeyLocator();
141 if (keyLocator.getType() != KeyLocator::KeyLocator_Name)
142 return onValidationFailed(data.shared_from_this(),
143 "Key Locator is not a name: " +
144 data.getName().toUri());
145
146
147 const Name& keyLocatorName = keyLocator.getName();
Yingdi Yu40587c02014-02-21 16:40:48 -0800148 shared_ptr<const Certificate> trustedCert;
Yingdi Yu4e9b0692014-11-04 16:13:56 -0800149 if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName) &&
150 static_cast<bool>(m_certificateCache))
Yingdi Yu40587c02014-02-21 16:40:48 -0800151 trustedCert = m_certificateCache->getCertificate(keyLocatorName);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800152 else
Yingdi Yu40587c02014-02-21 16:40:48 -0800153 trustedCert = m_trustAnchors[keyLocatorName];
Yingdi Yu6ac97982014-01-30 14:49:21 -0800154
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700155 if (static_cast<bool>(trustedCert))
156 {
Yingdi Yu4a557052014-07-09 16:40:37 -0700157 if (verifySignature(data, data.getSignature(), trustedCert->getPublicKeyInfo()))
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700158 return onValidated(data.shared_from_this());
159 else
160 return onValidationFailed(data.shared_from_this(),
161 "Cannot verify signature: " +
162 data.getName().toUri());
163 }
164 else
165 {
Alexander Afanasyevfc7d33a2014-05-12 18:04:51 -0700166 // KeyLocator is not a trust anchor
167
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700168 OnDataValidated onKeyValidated =
169 bind(&ValidatorRegex::onCertificateValidated, this, _1,
170 data.shared_from_this(), onValidated, onValidationFailed);
Yingdi Yu40587c02014-02-21 16:40:48 -0800171
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700172 OnDataValidationFailed onKeyValidationFailed =
173 bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2,
174 data.shared_from_this(), onValidationFailed);
175
Yingdi Yu4a557052014-07-09 16:40:37 -0700176 Interest interest(keyLocatorName);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700177 shared_ptr<ValidationRequest> nextStep =
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700178 make_shared<ValidationRequest>(interest,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700179 onKeyValidated,
180 onKeyValidationFailed,
181 3,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700182 nSteps + 1);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700183
184 nextSteps.push_back(nextStep);
185
186 return;
187 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800188 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700189 catch (KeyLocator::Error& e)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700190 {
191 return onValidationFailed(data.shared_from_this(),
192 "Key Locator is not a name: " +
193 data.getName().toUri());
Yingdi Yu40587c02014-02-21 16:40:48 -0800194 }
Junxiao Shic2b8d242014-11-04 08:35:29 -0700195 catch (tlv::Error& e)
196 {
197 return onValidationFailed(data.shared_from_this(),
198 "Cannot decode signature");
199 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800200 }
201 }
202
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700203 return onValidationFailed(data.shared_from_this(),
Yingdi Yu40587c02014-02-21 16:40:48 -0800204 "No policy found for data: " + data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800205}
206
Yingdi Yufc40d872014-02-18 12:56:04 -0800207} // namespace ndn