blob: 0ae4da45a45de79e33bc7a76eee8ab7c9fdad160 [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 Yu6ac97982014-01-30 14:49:21 -080030using namespace std;
31
Yingdi Yufc40d872014-02-18 12:56:04 -080032namespace ndn {
Yingdi Yu6ac97982014-01-30 14:49:21 -080033
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070034const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE;
Yingdi Yu6ac97982014-01-30 14:49:21 -080035
Yingdi Yu96e64062014-04-15 19:57:33 -070036ValidatorRegex::ValidatorRegex(Face& face,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070037 shared_ptr<CertificateCache> certificateCache,
38 const int stepLimit)
Yingdi Yu6ac97982014-01-30 14:49:21 -080039 : Validator(face)
40 , m_stepLimit(stepLimit)
41 , m_certificateCache(certificateCache)
42{
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070043 if (!static_cast<bool>(m_certificateCache))
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070044 m_certificateCache = make_shared<CertificateCacheTtl>(ref(m_face.getIoService()));
Yingdi Yu96e64062014-04-15 19:57:33 -070045}
46
Yingdi Yu6ac97982014-01-30 14:49:21 -080047void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070048ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate,
49 const shared_ptr<const Data>& data,
50 const OnDataValidated& onValidated,
51 const OnDataValidationFailed& onValidationFailed)
Yingdi Yu6ac97982014-01-30 14:49:21 -080052{
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070053 shared_ptr<IdentityCertificate> certificate =
Alexander Afanasyevf73f0632014-05-12 18:02:37 -070054 make_shared<IdentityCertificate>(*signCertificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070055
56 if (!certificate->isTooLate() && !certificate->isTooEarly())
Yingdi Yu6ac97982014-01-30 14:49:21 -080057 {
58 m_certificateCache->insertCertificate(certificate);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070059
60 if (verifySignature(*data, certificate->getPublicKeyInfo()))
Yingdi Yu40587c02014-02-21 16:40:48 -080061 return onValidated(data);
62 else
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070063 return onValidationFailed(data,
64 "Cannot verify signature: " +
65 data->getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -080066 }
67 else
68 {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070069 return onValidationFailed(data,
70 "Signing certificate " +
71 signCertificate->getName().toUri() +
72 " is no longer valid.");
Yingdi Yu6ac97982014-01-30 14:49:21 -080073 }
74}
75
76void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070077ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate,
Yingdi Yu40587c02014-02-21 16:40:48 -080078 const string& failureInfo,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070079 const shared_ptr<const Data>& data,
80 const OnDataValidationFailed& onValidationFailed)
81{
82 onValidationFailed(data, failureInfo);
83}
Yingdi Yu6ac97982014-01-30 14:49:21 -080084
85void
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070086ValidatorRegex::checkPolicy(const Data& data,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070087 int nSteps,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070088 const OnDataValidated& onValidated,
89 const OnDataValidationFailed& onValidationFailed,
90 vector<shared_ptr<ValidationRequest> >& nextSteps)
Yingdi Yu6ac97982014-01-30 14:49:21 -080091{
Yingdi Yu4b8c6a22014-04-15 23:00:54 -070092 if (m_stepLimit == nSteps)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070093 return onValidationFailed(data.shared_from_this(),
94 "Maximum steps of validation reached: " +
95 data.getName().toUri());
Yingdi Yu40587c02014-02-21 16:40:48 -080096
Yingdi Yu48e8c0c2014-03-19 12:01:55 -070097 for (RuleList::iterator it = m_mustFailVerify.begin();
98 it != m_mustFailVerify.end();
99 it++)
100 if ((*it)->satisfy(data))
Yingdi Yu40587c02014-02-21 16:40:48 -0800101 return onValidationFailed(data.shared_from_this(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700102 "Comply with mustFail policy: " +
103 data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800104
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700105 for (RuleList::iterator it = m_verifyPolicies.begin();
106 it != m_verifyPolicies.end();
107 it++)
Yingdi Yu6ac97982014-01-30 14:49:21 -0800108 {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700109 if ((*it)->satisfy(data))
Yingdi Yu6ac97982014-01-30 14:49:21 -0800110 {
Yingdi Yu40587c02014-02-21 16:40:48 -0800111 try
112 {
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700113 SignatureSha256WithRsa sig(data.getSignature());
114
Yingdi Yu40587c02014-02-21 16:40:48 -0800115 Name keyLocatorName = sig.getKeyLocator().getName();
116 shared_ptr<const Certificate> trustedCert;
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700117 if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName))
Yingdi Yu40587c02014-02-21 16:40:48 -0800118 trustedCert = m_certificateCache->getCertificate(keyLocatorName);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800119 else
Yingdi Yu40587c02014-02-21 16:40:48 -0800120 trustedCert = m_trustAnchors[keyLocatorName];
Yingdi Yu6ac97982014-01-30 14:49:21 -0800121
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700122 if (static_cast<bool>(trustedCert))
123 {
124 if (verifySignature(data, sig, trustedCert->getPublicKeyInfo()))
125 return onValidated(data.shared_from_this());
126 else
127 return onValidationFailed(data.shared_from_this(),
128 "Cannot verify signature: " +
129 data.getName().toUri());
130 }
131 else
132 {
Alexander Afanasyevfc7d33a2014-05-12 18:04:51 -0700133 // KeyLocator is not a trust anchor
134
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700135 OnDataValidated onKeyValidated =
136 bind(&ValidatorRegex::onCertificateValidated, this, _1,
137 data.shared_from_this(), onValidated, onValidationFailed);
Yingdi Yu40587c02014-02-21 16:40:48 -0800138
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700139 OnDataValidationFailed onKeyValidationFailed =
140 bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2,
141 data.shared_from_this(), onValidationFailed);
142
143 Interest interest(sig.getKeyLocator().getName());
144 shared_ptr<ValidationRequest> nextStep =
Alexander Afanasyevf73f0632014-05-12 18:02:37 -0700145 make_shared<ValidationRequest>(interest,
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700146 onKeyValidated,
147 onKeyValidationFailed,
148 3,
Yingdi Yu4b8c6a22014-04-15 23:00:54 -0700149 nSteps + 1);
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700150
151 nextSteps.push_back(nextStep);
152
153 return;
154 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800155 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700156 catch (SignatureSha256WithRsa::Error& e)
Yingdi Yu40587c02014-02-21 16:40:48 -0800157 {
158 return onValidationFailed(data.shared_from_this(),
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700159 "Not SignatureSha256WithRsa signature: " +
160 data.getName().toUri());
161 }
Alexander Afanasyev2a7f7202014-04-23 14:25:29 -0700162 catch (KeyLocator::Error& e)
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700163 {
164 return onValidationFailed(data.shared_from_this(),
165 "Key Locator is not a name: " +
166 data.getName().toUri());
Yingdi Yu40587c02014-02-21 16:40:48 -0800167 }
Yingdi Yu6ac97982014-01-30 14:49:21 -0800168 }
169 }
170
Yingdi Yu48e8c0c2014-03-19 12:01:55 -0700171 return onValidationFailed(data.shared_from_this(),
Yingdi Yu40587c02014-02-21 16:40:48 -0800172 "No policy found for data: " + data.getName().toUri());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800173}
174
Yingdi Yufc40d872014-02-18 12:56:04 -0800175} // namespace ndn