blob: 1b9dcb9f1ad1c75a5d531cf1a85331f9e8ca282e [file] [log] [blame]
Yingdi Yu6ac97982014-01-30 14:49:21 -08001/* -*- 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 Afanasyeve2dcdfd2014-02-07 15:53:28 -08008#include "common.hpp"
9
Yingdi Yu6ac97982014-01-30 14:49:21 -080010#include "validator-regex.hpp"
11#include "signature-sha256-with-rsa.hpp"
12#include "certificate-cache-ttl.hpp"
13
14#include "../util/logging.hpp"
15
16INIT_LOGGER("ndn::ValidatorRegex");
17
18using namespace std;
19
20namespace ndn
21{
22
23const shared_ptr<CertificateCache> ValidatorRegex::DefaultCertificateCache = shared_ptr<CertificateCache>();
24
25ValidatorRegex::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
39void
40ValidatorRegex::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
71void
72ValidatorRegex::onCertificateValidationFailed(const shared_ptr<const Data> &signCertificate,
73 const shared_ptr<const Data> &data,
74 const OnDataValidationFailed &onValidationFailed)
75{ onValidationFailed(data); }
76
77void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080078ValidatorRegex::checkPolicy(const Data& data,
Yingdi Yu6ac97982014-01-30 14:49:21 -080079 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");
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080086 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -080087 return;
88 }
89
90 RuleList::iterator it = m_mustFailVerify.begin();
91 for(; it != m_mustFailVerify.end(); it++)
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080092 if((*it)->satisfy(data))
Yingdi Yu6ac97982014-01-30 14:49:21 -080093 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080094 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -080095 return;
96 }
97
98 it = m_verifyPolicies.begin();
99 for(; it != m_verifyPolicies.end(); it++)
100 {
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800101 if((*it)->satisfy(data))
Yingdi Yu6ac97982014-01-30 14:49:21 -0800102 {
103 try{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800104 SignatureSha256WithRsa sig(data.getSignature());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800105
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)){
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800114 if(verifySignature(data, sig, trustedCert->getPublicKeyInfo()))
115 onValidated(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800116 else
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800117 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800118
119 return;
120 }
121 else{
122 // _LOG_DEBUG("KeyLocator is not trust anchor");
123 OnDataValidated onKeyValidated = bind(&ValidatorRegex::onCertificateValidated, this,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800124 _1, data.shared_from_this(), onValidated, onValidationFailed);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800125
126 OnDataValidationFailed onKeyValidationFailed = bind(&ValidatorRegex::onCertificateValidationFailed, this,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800127 _1, data.shared_from_this(), onValidationFailed);
Yingdi Yu6ac97982014-01-30 14:49:21 -0800128
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());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800139 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800140 return;
141 }catch(KeyLocator::Error &e){
142 _LOG_DEBUG("ValidatorRegex Error: " << e.what());
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800143 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800144 return;
145 }
146 }
147 }
148
Alexander Afanasyev0222fba2014-02-09 23:16:02 -0800149 onValidationFailed(data.shared_from_this());
Yingdi Yu6ac97982014-01-30 14:49:21 -0800150 return;
151}
152
153}//ndn