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 | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 34 | ValidatorRegex::ValidatorRegex(Face& face, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 35 | shared_ptr<CertificateCache> certificateCache, |
| 36 | const int stepLimit) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 37 | : Validator(face) |
| 38 | , m_stepLimit(stepLimit) |
| 39 | , m_certificateCache(certificateCache) |
| 40 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 41 | if (!static_cast<bool>(m_certificateCache)) |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 42 | m_certificateCache = make_shared<CertificateCacheTtl>(ref(m_face.getIoService())); |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 45 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 46 | ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate, |
| 47 | const shared_ptr<const Data>& data, |
| 48 | const OnDataValidated& onValidated, |
| 49 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 50 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 51 | shared_ptr<IdentityCertificate> certificate = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 52 | make_shared<IdentityCertificate>(*signCertificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 53 | |
| 54 | if (!certificate->isTooLate() && !certificate->isTooEarly()) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 55 | { |
| 56 | m_certificateCache->insertCertificate(certificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 57 | |
| 58 | if (verifySignature(*data, certificate->getPublicKeyInfo())) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 59 | return onValidated(data); |
| 60 | else |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 61 | return onValidationFailed(data, |
| 62 | "Cannot verify signature: " + |
| 63 | data->getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 64 | } |
| 65 | else |
| 66 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 67 | return onValidationFailed(data, |
| 68 | "Signing certificate " + |
| 69 | signCertificate->getName().toUri() + |
| 70 | " is no longer valid."); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 75 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 76 | const std::string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 77 | const shared_ptr<const Data>& data, |
| 78 | const OnDataValidationFailed& onValidationFailed) |
| 79 | { |
| 80 | onValidationFailed(data, failureInfo); |
| 81 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 82 | |
| 83 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 84 | ValidatorRegex::checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 85 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 86 | const OnDataValidated& onValidated, |
| 87 | const OnDataValidationFailed& onValidationFailed, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 88 | std::vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 89 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 90 | if (m_stepLimit == nSteps) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 91 | return onValidationFailed(data.shared_from_this(), |
| 92 | "Maximum steps of validation reached: " + |
| 93 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 94 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 95 | for (RuleList::iterator it = m_mustFailVerify.begin(); |
| 96 | it != m_mustFailVerify.end(); |
| 97 | it++) |
| 98 | if ((*it)->satisfy(data)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 99 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | "Comply with mustFail policy: " + |
| 101 | data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 102 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 103 | for (RuleList::iterator it = m_verifyPolicies.begin(); |
| 104 | it != m_verifyPolicies.end(); |
| 105 | it++) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 106 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 107 | if ((*it)->satisfy(data)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 108 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 109 | try |
| 110 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 111 | if (!data.getSignature().hasKeyLocator()) |
| 112 | return onValidationFailed(data.shared_from_this(), |
| 113 | "Key Locator is missing in Data packet: " + |
| 114 | data.getName().toUri()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 115 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 116 | const KeyLocator& keyLocator = data.getSignature().getKeyLocator(); |
| 117 | if (keyLocator.getType() != KeyLocator::KeyLocator_Name) |
| 118 | return onValidationFailed(data.shared_from_this(), |
| 119 | "Key Locator is not a name: " + |
| 120 | data.getName().toUri()); |
| 121 | |
| 122 | |
| 123 | const Name& keyLocatorName = keyLocator.getName(); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 124 | shared_ptr<const Certificate> trustedCert; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 125 | if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 126 | trustedCert = m_certificateCache->getCertificate(keyLocatorName); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 127 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 128 | trustedCert = m_trustAnchors[keyLocatorName]; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 129 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 130 | if (static_cast<bool>(trustedCert)) |
| 131 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 132 | if (verifySignature(data, data.getSignature(), trustedCert->getPublicKeyInfo())) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 133 | return onValidated(data.shared_from_this()); |
| 134 | else |
| 135 | return onValidationFailed(data.shared_from_this(), |
| 136 | "Cannot verify signature: " + |
| 137 | data.getName().toUri()); |
| 138 | } |
| 139 | else |
| 140 | { |
Alexander Afanasyev | fc7d33a | 2014-05-12 18:04:51 -0700 | [diff] [blame] | 141 | // KeyLocator is not a trust anchor |
| 142 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 143 | OnDataValidated onKeyValidated = |
| 144 | bind(&ValidatorRegex::onCertificateValidated, this, _1, |
| 145 | data.shared_from_this(), onValidated, onValidationFailed); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 146 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 147 | OnDataValidationFailed onKeyValidationFailed = |
| 148 | bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2, |
| 149 | data.shared_from_this(), onValidationFailed); |
| 150 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 151 | Interest interest(keyLocatorName); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 152 | shared_ptr<ValidationRequest> nextStep = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 153 | make_shared<ValidationRequest>(interest, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 154 | onKeyValidated, |
| 155 | onKeyValidationFailed, |
| 156 | 3, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 157 | nSteps + 1); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 158 | |
| 159 | nextSteps.push_back(nextStep); |
| 160 | |
| 161 | return; |
| 162 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 163 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 164 | catch (Tlv::Error& e) |
| 165 | { |
| 166 | return onValidationFailed(data.shared_from_this(), |
| 167 | "Cannot decode signature"); |
| 168 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 169 | catch (KeyLocator::Error& e) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 170 | { |
| 171 | return onValidationFailed(data.shared_from_this(), |
| 172 | "Key Locator is not a name: " + |
| 173 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 174 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 178 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 179 | "No policy found for data: " + data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 180 | } |
| 181 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 182 | } // namespace ndn |