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