Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 24 | #include "common.hpp" |
| 25 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 26 | #include "validator-regex.hpp" |
| 27 | #include "signature-sha256-with-rsa.hpp" |
| 28 | #include "certificate-cache-ttl.hpp" |
| 29 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 30 | namespace ndn { |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 31 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 32 | const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 33 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame^] | 34 | ValidatorRegex::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 Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 45 | ValidatorRegex::ValidatorRegex(Face& face, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 46 | shared_ptr<CertificateCache> certificateCache, |
| 47 | const int stepLimit) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 48 | : Validator(face) |
| 49 | , m_stepLimit(stepLimit) |
| 50 | , m_certificateCache(certificateCache) |
| 51 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 52 | if (!static_cast<bool>(m_certificateCache)) |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame^] | 53 | m_certificateCache = make_shared<CertificateCacheTtl>(ref(face.getIoService())); |
| 54 | } |
| 55 | |
| 56 | void |
| 57 | ValidatorRegex::addDataVerificationRule(shared_ptr<SecRuleRelative> rule) |
| 58 | { |
| 59 | rule->isPositive() ? m_verifyPolicies.push_back(rule) : m_mustFailVerify.push_back(rule); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | ValidatorRegex::addTrustAnchor(shared_ptr<IdentityCertificate> certificate) |
| 64 | { |
| 65 | m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate; |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 68 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 69 | ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate, |
| 70 | const shared_ptr<const Data>& data, |
| 71 | const OnDataValidated& onValidated, |
| 72 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 73 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 74 | shared_ptr<IdentityCertificate> certificate = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 75 | make_shared<IdentityCertificate>(*signCertificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 76 | |
| 77 | if (!certificate->isTooLate() && !certificate->isTooEarly()) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 78 | { |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame^] | 79 | if (static_cast<bool>(m_certificateCache)) |
| 80 | m_certificateCache->insertCertificate(certificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 81 | |
| 82 | if (verifySignature(*data, certificate->getPublicKeyInfo())) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 83 | return onValidated(data); |
| 84 | else |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 85 | return onValidationFailed(data, |
| 86 | "Cannot verify signature: " + |
| 87 | data->getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 88 | } |
| 89 | else |
| 90 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 91 | return onValidationFailed(data, |
| 92 | "Signing certificate " + |
| 93 | signCertificate->getName().toUri() + |
| 94 | " is no longer valid."); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 99 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 100 | const std::string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 101 | const shared_ptr<const Data>& data, |
| 102 | const OnDataValidationFailed& onValidationFailed) |
| 103 | { |
| 104 | onValidationFailed(data, failureInfo); |
| 105 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 106 | |
| 107 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 108 | ValidatorRegex::checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 109 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 110 | const OnDataValidated& onValidated, |
| 111 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 112 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 113 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 114 | if (m_stepLimit == nSteps) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 115 | return onValidationFailed(data.shared_from_this(), |
| 116 | "Maximum steps of validation reached: " + |
| 117 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 118 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 119 | for (RuleList::iterator it = m_mustFailVerify.begin(); |
| 120 | it != m_mustFailVerify.end(); |
| 121 | it++) |
| 122 | if ((*it)->satisfy(data)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 123 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 124 | "Comply with mustFail policy: " + |
| 125 | data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 126 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 127 | for (RuleList::iterator it = m_verifyPolicies.begin(); |
| 128 | it != m_verifyPolicies.end(); |
| 129 | it++) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 130 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 131 | if ((*it)->satisfy(data)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 132 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 133 | try |
| 134 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 135 | if (!data.getSignature().hasKeyLocator()) |
| 136 | return onValidationFailed(data.shared_from_this(), |
| 137 | "Key Locator is missing in Data packet: " + |
| 138 | data.getName().toUri()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 139 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 140 | 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 Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 148 | shared_ptr<const Certificate> trustedCert; |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame^] | 149 | if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName) && |
| 150 | static_cast<bool>(m_certificateCache)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 151 | trustedCert = m_certificateCache->getCertificate(keyLocatorName); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 152 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 153 | trustedCert = m_trustAnchors[keyLocatorName]; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 154 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 155 | if (static_cast<bool>(trustedCert)) |
| 156 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 157 | if (verifySignature(data, data.getSignature(), trustedCert->getPublicKeyInfo())) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 158 | 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 Afanasyev | fc7d33a | 2014-05-12 18:04:51 -0700 | [diff] [blame] | 166 | // KeyLocator is not a trust anchor |
| 167 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 168 | OnDataValidated onKeyValidated = |
| 169 | bind(&ValidatorRegex::onCertificateValidated, this, _1, |
| 170 | data.shared_from_this(), onValidated, onValidationFailed); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 171 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 172 | OnDataValidationFailed onKeyValidationFailed = |
| 173 | bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2, |
| 174 | data.shared_from_this(), onValidationFailed); |
| 175 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 176 | Interest interest(keyLocatorName); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 177 | shared_ptr<ValidationRequest> nextStep = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 178 | make_shared<ValidationRequest>(interest, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 179 | onKeyValidated, |
| 180 | onKeyValidationFailed, |
| 181 | 3, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 182 | nSteps + 1); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 183 | |
| 184 | nextSteps.push_back(nextStep); |
| 185 | |
| 186 | return; |
| 187 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 188 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 189 | catch (KeyLocator::Error& e) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 190 | { |
| 191 | return onValidationFailed(data.shared_from_this(), |
| 192 | "Key Locator is not a name: " + |
| 193 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 194 | } |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 195 | catch (tlv::Error& e) |
| 196 | { |
| 197 | return onValidationFailed(data.shared_from_this(), |
| 198 | "Cannot decode signature"); |
| 199 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 203 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 204 | "No policy found for data: " + data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 207 | } // namespace ndn |