Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Yingdi Yu <yingdi@cs.ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Alexander Afanasyev | e2dcdfd | 2014-02-07 15:53:28 -0800 | [diff] [blame] | 8 | #include "common.hpp" |
| 9 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 10 | #include "validator-regex.hpp" |
| 11 | #include "signature-sha256-with-rsa.hpp" |
| 12 | #include "certificate-cache-ttl.hpp" |
| 13 | |
| 14 | #include "../util/logging.hpp" |
| 15 | |
Yingdi Yu | 2115716 | 2014-02-28 13:02:34 -0800 | [diff] [blame] | 16 | INIT_LOGGER("ndn.ValidatorRegex"); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 17 | |
| 18 | using namespace std; |
| 19 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 20 | namespace ndn { |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 21 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 22 | const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 23 | |
| 24 | ValidatorRegex::ValidatorRegex(shared_ptr<Face> face, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 25 | shared_ptr<CertificateCache> certificateCache, |
| 26 | const int stepLimit) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 27 | : Validator(face) |
| 28 | , m_stepLimit(stepLimit) |
| 29 | , m_certificateCache(certificateCache) |
| 30 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 31 | if (!static_cast<bool>(face)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 32 | throw Error("Face is not set!"); |
| 33 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 34 | if (!static_cast<bool>(m_certificateCache)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 35 | m_certificateCache = make_shared<CertificateCacheTtl>(m_face->ioService()); |
| 36 | } |
| 37 | |
| 38 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 39 | ValidatorRegex::onCertificateValidated(const shared_ptr<const Data>& signCertificate, |
| 40 | const shared_ptr<const Data>& data, |
| 41 | const OnDataValidated& onValidated, |
| 42 | const OnDataValidationFailed& onValidationFailed) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 43 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 44 | shared_ptr<IdentityCertificate> certificate = |
| 45 | make_shared<IdentityCertificate>(boost::cref(*signCertificate)); |
| 46 | |
| 47 | if (!certificate->isTooLate() && !certificate->isTooEarly()) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 48 | { |
| 49 | m_certificateCache->insertCertificate(certificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 50 | |
| 51 | if (verifySignature(*data, certificate->getPublicKeyInfo())) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 52 | return onValidated(data); |
| 53 | else |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 54 | return onValidationFailed(data, |
| 55 | "Cannot verify signature: " + |
| 56 | data->getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 57 | } |
| 58 | else |
| 59 | { |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 60 | _LOG_DEBUG("Wrong validity:"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 61 | return onValidationFailed(data, |
| 62 | "Signing certificate " + |
| 63 | signCertificate->getName().toUri() + |
| 64 | " is no longer valid."); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 69 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 70 | const string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 71 | const shared_ptr<const Data>& data, |
| 72 | const OnDataValidationFailed& onValidationFailed) |
| 73 | { |
| 74 | onValidationFailed(data, failureInfo); |
| 75 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 76 | |
| 77 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 78 | ValidatorRegex::checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 79 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 80 | const OnDataValidated& onValidated, |
| 81 | const OnDataValidationFailed& onValidationFailed, |
| 82 | vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 83 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 84 | if (m_stepLimit == nSteps) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 85 | return onValidationFailed(data.shared_from_this(), |
| 86 | "Maximum steps of validation reached: " + |
| 87 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 88 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 89 | for (RuleList::iterator it = m_mustFailVerify.begin(); |
| 90 | it != m_mustFailVerify.end(); |
| 91 | it++) |
| 92 | if ((*it)->satisfy(data)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 93 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 94 | "Comply with mustFail policy: " + |
| 95 | data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 96 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 97 | for (RuleList::iterator it = m_verifyPolicies.begin(); |
| 98 | it != m_verifyPolicies.end(); |
| 99 | it++) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 100 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 101 | if ((*it)->satisfy(data)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 102 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 103 | try |
| 104 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 105 | SignatureSha256WithRsa sig(data.getSignature()); |
| 106 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 107 | Name keyLocatorName = sig.getKeyLocator().getName(); |
| 108 | shared_ptr<const Certificate> trustedCert; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 109 | if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 110 | trustedCert = m_certificateCache->getCertificate(keyLocatorName); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 111 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 112 | trustedCert = m_trustAnchors[keyLocatorName]; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 113 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 114 | if (static_cast<bool>(trustedCert)) |
| 115 | { |
| 116 | if (verifySignature(data, sig, trustedCert->getPublicKeyInfo())) |
| 117 | return onValidated(data.shared_from_this()); |
| 118 | else |
| 119 | return onValidationFailed(data.shared_from_this(), |
| 120 | "Cannot verify signature: " + |
| 121 | data.getName().toUri()); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | // _LOG_DEBUG("KeyLocator is not trust anchor"); |
| 126 | OnDataValidated onKeyValidated = |
| 127 | bind(&ValidatorRegex::onCertificateValidated, this, _1, |
| 128 | data.shared_from_this(), onValidated, onValidationFailed); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 129 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 130 | OnDataValidationFailed onKeyValidationFailed = |
| 131 | bind(&ValidatorRegex::onCertificateValidationFailed, this, _1, _2, |
| 132 | data.shared_from_this(), onValidationFailed); |
| 133 | |
| 134 | Interest interest(sig.getKeyLocator().getName()); |
| 135 | shared_ptr<ValidationRequest> nextStep = |
| 136 | make_shared<ValidationRequest>(boost::cref(interest), |
| 137 | onKeyValidated, |
| 138 | onKeyValidationFailed, |
| 139 | 3, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 140 | nSteps + 1); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 141 | |
| 142 | nextSteps.push_back(nextStep); |
| 143 | |
| 144 | return; |
| 145 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 146 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 147 | catch (const SignatureSha256WithRsa::Error& e) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 148 | { |
| 149 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 150 | "Not SignatureSha256WithRsa signature: " + |
| 151 | data.getName().toUri()); |
| 152 | } |
| 153 | catch (const KeyLocator::Error& e) |
| 154 | { |
| 155 | return onValidationFailed(data.shared_from_this(), |
| 156 | "Key Locator is not a name: " + |
| 157 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 158 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 162 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 163 | "No policy found for data: " + data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 164 | } |
| 165 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 166 | } // namespace ndn |