blob: caa2e6c1f0abff5feef296358025360ee7895ec0 [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 Afanasyev2fa59392016-07-29 17:24:23 -07003 * Copyright (c) 2013-2016 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 {
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070031namespace security {
Yingdi Yu6ac97982014-01-30 14:49:21 -080032
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070033const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE;
Yingdi Yu6ac97982014-01-30 14:49:21 -080034
Yingdi Yu4e9b0692014-11-04 16:13:56 -080035ValidatorRegex::ValidatorRegex(Face* face,
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
Yingdi Yu96e64062014-04-15 19:57:33 -070046ValidatorRegex::ValidatorRegex(Face& face,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070047 shared_ptr<CertificateCache> certificateCache,
48 const int stepLimit)
Yingdi Yu6ac97982014-01-30 14:49:21 -080049 : Validator(face)
50 , m_stepLimit(stepLimit)
51 , m_certificateCache(certificateCache)
52{
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070053 if (certificateCache == nullptr)
Yingdi Yu4e9b0692014-11-04 16:13:56 -080054 m_certificateCache = make_shared<CertificateCacheTtl>(ref(face.getIoService()));
55}
56
57void
58ValidatorRegex::addDataVerificationRule(shared_ptr<SecRuleRelative> rule)
59{
60 rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule);
61}
62
63void
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070064ValidatorRegex::addTrustAnchor(shared_ptr<v1::IdentityCertificate> certificate)
Yingdi Yu4e9b0692014-11-04 16:13:56 -080065{
66 m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate;
Yingdi Yu96e64062014-04-15 19:57:33 -070067}
68
Yingdi Yu6ac97982014-01-30 14:49:21 -080069void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070070ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate,
71 const shared_ptr<const Data>& data,
72 const OnDataValidated& onValidated,
73 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu6ac97982014-01-30 14:49:21 -080074{
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070075 shared_ptr<v1::IdentityCertificate> certificate =
76 make_shared<v1::IdentityCertificate>(*signCertificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070077
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070078 if (!certificate->isTooLate() && !certificate->isTooEarly()) {
79 if (m_certificateCache != nullptr)
80 m_certificateCache->insertCertificate(certificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070081
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070082 if (verifySignature(*data, certificate->getPublicKeyInfo()))
83 return onValidated(data);
84 else
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070085 return onValidationFailed(data,
Alexander Afanasyev2fa59392016-07-29 17:24:23 -070086 "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 }
Yingdi Yu6ac97982014-01-30 14:49:21 -080095}
96
97void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070098ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate,
Yingdi Yu5ec0ee32014-06-24 16:26:09 -070099 const std::string& failureInfo,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700100 const shared_ptr<const Data>& data,
101 const OnDataValidationFailed& onValidationFailed)
102{
103 onValidationFailed(data, failureInfo);
104}
Yingdi Yu6ac97982014-01-30 14:49:21 -0800105
106void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700107ValidatorRegex::checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700108 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700109 const OnDataValidated& onValidated,
110 const OnDataValidationFailed& onValidationFailed,
Yingdi Yu5ec0ee32014-06-24 16:26:09 -0700111 std::vector<shared_ptr<ValidationRequest> >& nextSteps)
Yingdi Yu6ac97982014-01-30 14:49:21 -0800112{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700113 if (m_stepLimit == nSteps)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700114 return onValidationFailed(data.shared_from_this(),
115 "Maximum steps of validation reached: " +
116 data.getName().toUri());
Yingdi Yu40587c02014-02-21 16:40:48 -0800117
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700118 for (RuleList::iterator it = m_mustFailVerify.begin();
119 it != m_mustFailVerify.end();
120 it++)
121 if ((*it)->satisfy(data))
Yingdi Yu40587c02014-02-21 16:40:48 -0800122 return onValidationFailed(data.shared_from_this(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700123 "Comply with mustFail policy: " +
124 data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800125
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700126 for (RuleList::iterator it = m_verifyPolicies.begin();
127 it != m_verifyPolicies.end();
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700128 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());
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700135
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700136 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());
Yingdi Yu4a557052014-07-09 16:40:37 -0700141
142
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700143 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];
Yingdi Yu6ac97982014-01-30 14:49:21 -0800150
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700151 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());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800158 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700159 else {
160 // KeyLocator is not a trust anchor
161
162 OnDataValidated onKeyValidated =
163 bind(&ValidatorRegex::onCertificateValidated, this, _1,
164 data.shared_from_this(), onValidated, onValidationFailed);
165
166 OnDataValidationFailed onKeyValidationFailed =
167 bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2,
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 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800192 }
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700193 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800194
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700195 return onValidationFailed(data.shared_from_this(),
Yingdi Yu40587c02014-02-21 16:40:48 -0800196 "No policy found for data: " + data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800197}
198
Alexander Afanasyev2fa59392016-07-29 17:24:23 -0700199} // namespace security
Yingdi Yufc40d872014-02-18 12:56:04 -0800200} // namespace ndn