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 | |
| 16 | INIT_LOGGER("ndn::ValidatorRegex"); |
| 17 | |
| 18 | using namespace std; |
| 19 | |
| 20 | namespace ndn |
| 21 | { |
| 22 | |
| 23 | const shared_ptr<CertificateCache> ValidatorRegex::DefaultCertificateCache = shared_ptr<CertificateCache>(); |
| 24 | |
| 25 | ValidatorRegex::ValidatorRegex(shared_ptr<Face> face, |
| 26 | shared_ptr<CertificateCache> certificateCache /* = DefaultCertificateCache */, |
| 27 | const int stepLimit /* = 3 */) |
| 28 | : Validator(face) |
| 29 | , m_stepLimit(stepLimit) |
| 30 | , m_certificateCache(certificateCache) |
| 31 | { |
| 32 | if(!static_cast<bool>(face)) |
| 33 | throw Error("Face is not set!"); |
| 34 | |
| 35 | if(!static_cast<bool>(m_certificateCache)) |
| 36 | m_certificateCache = make_shared<CertificateCacheTtl>(m_face->ioService()); |
| 37 | } |
| 38 | |
| 39 | void |
| 40 | ValidatorRegex::onCertificateValidated(const shared_ptr<const Data> &signCertificate, |
| 41 | const shared_ptr<const Data> &data, |
| 42 | const OnDataValidated &onValidated, |
| 43 | const OnDataValidationFailed &onValidationFailed) |
| 44 | { |
| 45 | shared_ptr<IdentityCertificate> certificate = make_shared<IdentityCertificate>(*signCertificate); |
| 46 | |
| 47 | if(!certificate->isTooLate() && !certificate->isTooEarly()) |
| 48 | { |
| 49 | m_certificateCache->insertCertificate(certificate); |
| 50 | |
| 51 | try{ |
| 52 | if(verifySignature(*data, certificate->getPublicKeyInfo())) |
| 53 | { |
| 54 | onValidated(data); |
| 55 | return; |
| 56 | } |
| 57 | }catch(Signature::Error &e){ |
| 58 | _LOG_DEBUG("ValidatorRegex Error: " << e.what()); |
| 59 | onValidationFailed(data); |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | _LOG_DEBUG("Wrong Invalidity: " << e.what()); |
| 66 | onValidationFailed(data); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | void |
| 72 | ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data> &signCertificate, |
| 73 | const shared_ptr<const Data> &data, |
| 74 | const OnDataValidationFailed &onValidationFailed) |
| 75 | { onValidationFailed(data); } |
| 76 | |
| 77 | void |
| 78 | ValidatorRegex::checkPolicy(const shared_ptr<const Data> &data, |
| 79 | int stepCount, |
| 80 | const OnDataValidated &onValidated, |
| 81 | const OnDataValidationFailed &onValidationFailed, |
| 82 | vector<shared_ptr<ValidationRequest> > &nextSteps) |
| 83 | { |
| 84 | if(m_stepLimit == stepCount){ |
| 85 | _LOG_DEBUG("reach the maximum steps of verification"); |
| 86 | onValidationFailed(data); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | RuleList::iterator it = m_mustFailVerify.begin(); |
| 91 | for(; it != m_mustFailVerify.end(); it++) |
| 92 | if((*it)->satisfy(*data)) |
| 93 | { |
| 94 | onValidationFailed(data); |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | it = m_verifyPolicies.begin(); |
| 99 | for(; it != m_verifyPolicies.end(); it++) |
| 100 | { |
| 101 | if((*it)->satisfy(*data)) |
| 102 | { |
| 103 | try{ |
| 104 | SignatureSha256WithRsa sig(data->getSignature()); |
| 105 | |
| 106 | Name keyLocatorName = sig.getKeyLocator().getName(); |
| 107 | shared_ptr<const Certificate> trustedCert; |
| 108 | if(m_trustAnchors.end() == m_trustAnchors.find(keyLocatorName)) |
| 109 | trustedCert = m_certificateCache->getCertificate(keyLocatorName); |
| 110 | else |
| 111 | trustedCert = m_trustAnchors[keyLocatorName]; |
| 112 | |
| 113 | if(static_cast<bool>(trustedCert)){ |
| 114 | if(verifySignature(*data, sig, trustedCert->getPublicKeyInfo())) |
| 115 | onValidated(data); |
| 116 | else |
| 117 | onValidationFailed(data); |
| 118 | |
| 119 | return; |
| 120 | } |
| 121 | else{ |
| 122 | // _LOG_DEBUG("KeyLocator is not trust anchor"); |
| 123 | OnDataValidated onKeyValidated = bind(&ValidatorRegex::onCertificateValidated, this, |
| 124 | _1, data, onValidated, onValidationFailed); |
| 125 | |
| 126 | OnDataValidationFailed onKeyValidationFailed = bind(&ValidatorRegex::onCertificateValidationFailed, this, |
| 127 | _1, data, onValidationFailed); |
| 128 | |
| 129 | shared_ptr<ValidationRequest> nextStep = make_shared<ValidationRequest>(Interest(boost::cref(sig.getKeyLocator().getName())), |
| 130 | onKeyValidated, |
| 131 | onKeyValidationFailed, |
| 132 | 3, |
| 133 | stepCount + 1); |
| 134 | nextSteps.push_back(nextStep); |
| 135 | return; |
| 136 | } |
| 137 | }catch(SignatureSha256WithRsa::Error &e){ |
| 138 | _LOG_DEBUG("ValidatorRegex Error: " << e.what()); |
| 139 | onValidationFailed(data); |
| 140 | return; |
| 141 | }catch(KeyLocator::Error &e){ |
| 142 | _LOG_DEBUG("ValidatorRegex Error: " << e.what()); |
| 143 | onValidationFailed(data); |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | onValidationFailed(data); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | }//ndn |