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