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 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 21 | using namespace std; |
| 22 | |
Yingdi Yu | fc40d87 | 2014-02-18 12:56:04 -0800 | [diff] [blame] | 23 | namespace ndn { |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 24 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 25 | const shared_ptr<CertificateCache> ValidatorRegex::DEFAULT_CERTIFICATE_CACHE; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 26 | |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 27 | ValidatorRegex::ValidatorRegex(Face& face, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 28 | shared_ptr<CertificateCache> certificateCache, |
| 29 | const int stepLimit) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 30 | : Validator(face) |
| 31 | , m_stepLimit(stepLimit) |
| 32 | , m_certificateCache(certificateCache) |
| 33 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 34 | if (!static_cast<bool>(m_certificateCache)) |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 35 | m_certificateCache = make_shared<CertificateCacheTtl>(ref(m_face.getIoService())); |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 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 = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 45 | make_shared<IdentityCertificate>(*signCertificate); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 60 | return onValidationFailed(data, |
| 61 | "Signing certificate " + |
| 62 | signCertificate->getName().toUri() + |
| 63 | " is no longer valid."); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 68 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data>& signCertificate, |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 69 | const string& failureInfo, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 70 | const shared_ptr<const Data>& data, |
| 71 | const OnDataValidationFailed& onValidationFailed) |
| 72 | { |
| 73 | onValidationFailed(data, failureInfo); |
| 74 | } |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 75 | |
| 76 | void |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 77 | ValidatorRegex::checkPolicy(const Data& data, |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 78 | int nSteps, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 79 | const OnDataValidated& onValidated, |
| 80 | const OnDataValidationFailed& onValidationFailed, |
| 81 | vector<shared_ptr<ValidationRequest> >& nextSteps) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 82 | { |
Yingdi Yu | 4b8c6a2 | 2014-04-15 23:00:54 -0700 | [diff] [blame] | 83 | if (m_stepLimit == nSteps) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 84 | return onValidationFailed(data.shared_from_this(), |
| 85 | "Maximum steps of validation reached: " + |
| 86 | data.getName().toUri()); |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 87 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 88 | for (RuleList::iterator it = m_mustFailVerify.begin(); |
| 89 | it != m_mustFailVerify.end(); |
| 90 | it++) |
| 91 | if ((*it)->satisfy(data)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 92 | return onValidationFailed(data.shared_from_this(), |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 93 | "Comply with mustFail policy: " + |
| 94 | data.getName().toUri()); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 95 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 96 | for (RuleList::iterator it = m_verifyPolicies.begin(); |
| 97 | it != m_verifyPolicies.end(); |
| 98 | it++) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 99 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 100 | if ((*it)->satisfy(data)) |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 101 | { |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 102 | try |
| 103 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 104 | SignatureSha256WithRsa sig(data.getSignature()); |
| 105 | |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 106 | Name keyLocatorName = sig.getKeyLocator().getName(); |
| 107 | shared_ptr<const Certificate> trustedCert; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 108 | if (m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName)) |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 109 | trustedCert = m_certificateCache->getCertificate(keyLocatorName); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 110 | else |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 111 | trustedCert = m_trustAnchors[keyLocatorName]; |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 112 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 113 | if (static_cast<bool>(trustedCert)) |
| 114 | { |
| 115 | if (verifySignature(data, sig, trustedCert->getPublicKeyInfo())) |
| 116 | return onValidated(data.shared_from_this()); |
| 117 | else |
| 118 | return onValidationFailed(data.shared_from_this(), |
| 119 | "Cannot verify signature: " + |
| 120 | data.getName().toUri()); |
| 121 | } |
| 122 | else |
| 123 | { |
Alexander Afanasyev | fc7d33a | 2014-05-12 18:04:51 -0700 | [diff] [blame] | 124 | // KeyLocator is not a trust anchor |
| 125 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 = |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 136 | make_shared<ValidationRequest>(interest, |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 147 | catch (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 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 153 | catch (KeyLocator::Error& e) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 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 |