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 | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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 { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 31 | namespace security { |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 32 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 33 | const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 35 | ValidatorRegex::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 Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 46 | ValidatorRegex::ValidatorRegex(Face& face, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 47 | shared_ptr<CertificateCache> certificateCache, |
| 48 | const int stepLimit) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 49 | : Validator(face) |
| 50 | , m_stepLimit(stepLimit) |
| 51 | , m_certificateCache(certificateCache) |
| 52 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 53 | if (certificateCache == nullptr) |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 64 | ValidatorRegex::addTrustAnchor(shared_ptr<v1::IdentityCertificate> certificate) |
Yingdi Yu | 4e9b069 | 2014-11-04 16:13:56 -0800 | [diff] [blame] | 65 | { |
| 66 | m_trustAnchors[certificate->getName().getPrefix(-1)] = certificate; |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 67 | } |
| 68 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 69 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 70 | ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate, |
| 71 | const shared_ptr<const Data>& data, |
| 72 | const OnDataValidated& onValidated, |
| 73 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 74 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 75 | shared_ptr<v1::IdentityCertificate> certificate = |
| 76 | make_shared<v1::IdentityCertificate>(*signCertificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 78 | if (!certificate->isTooLate() && !certificate->isTooEarly()) { |
| 79 | if (m_certificateCache != nullptr) |
| 80 | m_certificateCache->insertCertificate(certificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 82 | if (verifySignature(*data, certificate->getPublicKeyInfo())) |
| 83 | return onValidated(data); |
| 84 | else |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 85 | return onValidationFailed(data, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 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 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 98 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 99 | const std::string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | const shared_ptr<const Data>& data, |
| 101 | const OnDataValidationFailed& onValidationFailed) |
| 102 | { |
| 103 | onValidationFailed(data, failureInfo); |
| 104 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 105 | |
| 106 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 107 | ValidatorRegex::checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 108 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 109 | const OnDataValidated& onValidated, |
| 110 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 111 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 112 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 113 | if (m_stepLimit == nSteps) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 114 | return onValidationFailed(data.shared_from_this(), |
| 115 | "Maximum steps of validation reached: " + |
| 116 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 117 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 118 | for (RuleList::iterator it = m_mustFailVerify.begin(); |
| 119 | it != m_mustFailVerify.end(); |
| 120 | it++) |
| 121 | if ((*it)->satisfy(data)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 122 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 123 | "Comply with mustFail policy: " + |
| 124 | data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 125 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 126 | for (RuleList::iterator it = m_verifyPolicies.begin(); |
| 127 | it != m_verifyPolicies.end(); |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 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()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 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()); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 141 | |
| 142 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 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]; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 150 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 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()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 158 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 159 | 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 Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 192 | } |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 193 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 194 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 195 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 196 | "No policy found for data: " + data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 197 | } |
| 198 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame^] | 199 | } // namespace security |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 200 | } // namespace ndn |