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