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