blob: 8c6345835f587fb7c3aacc71710d100566b0d236 [file] [log] [blame]
Yingdi Yu7d773322015-03-22 21:32:48 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California
4 *
5 * This file is part of NSL (NDN Signature Logger).
6 * See AUTHORS.md for complete list of NSL authors and contributors.
7 *
8 * NSL is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NSL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NSL, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of nsl authors and contributors.
20 */
21
22#include "checker.hpp"
23
24#include <boost/algorithm/string.hpp>
25
26namespace nsl {
27namespace conf {
28
29Checker::~Checker()
30{
31}
32
33CustomizedChecker::CustomizedChecker(uint32_t sigType,
34 shared_ptr<KeyLocatorChecker> keyLocatorChecker)
35 : m_sigType(sigType)
36 , m_keyLocatorChecker(keyLocatorChecker)
37{
38 switch (sigType) {
39 case tlv::SignatureSha256WithRsa:
40 case tlv::SignatureSha256WithEcdsa:
41 {
42 if (!static_cast<bool>(m_keyLocatorChecker))
43 throw Error("Strong signature requires KeyLocatorChecker");
44
45 return;
46 }
47 case tlv::DigestSha256:
48 return;
49 default:
50 throw Error("Unsupported signature type");
51 }
52}
53
54bool
55CustomizedChecker::check(const Data& data)
56{
57 const Signature signature = data.getSignature();
58 if (m_sigType != signature.getType())
59 return false;
60
61 if (signature.getType() == tlv::DigestSha256)
62 return true;
63
64 try {
65 switch (signature.getType()) {
66 case tlv::SignatureSha256WithRsa:
67 case tlv::SignatureSha256WithEcdsa:
68 {
69 if (!signature.hasKeyLocator())
70 return false;
71 break;
72 }
73 default:
74 return false;
75 }
76 }
77 catch (KeyLocator::Error&) {
78 return false;
79 }
80 catch (tlv::Error& e) {
81 return false;
82 }
83
84 std::string failInfo;
85 return m_keyLocatorChecker->check(data, signature.getKeyLocator(), failInfo);
86}
87
88HierarchicalChecker::HierarchicalChecker(uint32_t sigType)
89 : CustomizedChecker(sigType,
90 make_shared<HyperKeyLocatorNameChecker>("^(<>*)$", "\\1",
91 "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$",
92 "\\1\\2",
93 KeyLocatorChecker::RELATION_IS_PREFIX_OF))
94{
95}
96
97shared_ptr<Checker>
98CheckerFactory::create(const ConfigSection& configSection)
99{
100 ConfigSection::const_iterator propertyIt = configSection.begin();
101
102 // Get checker.type
103 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type"))
104 throw Error("Expect <checker.type>");
105
106 std::string type = propertyIt->second.data();
107
108 if (boost::iequals(type, "customized"))
109 return createCustomizedChecker(configSection);
110 else if (boost::iequals(type, "hierarchical"))
111 return createHierarchicalChecker(configSection);
112 else
113 throw Error("Unsupported checker type: " + type);
114}
115
116shared_ptr<Checker>
117CheckerFactory::createCustomizedChecker(const ConfigSection& configSection)
118{
119 ConfigSection::const_iterator propertyIt = configSection.begin();
120 propertyIt++;
121
122 // Get checker.sig-type
123 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type"))
124 throw Error("Expect <checker.sig-type>");
125
126 std::string sigType = propertyIt->second.data();
127 propertyIt++;
128
129 // Get checker.key-locator
130 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "key-locator"))
131 throw Error("Expect <checker.key-locator>");
132
133 shared_ptr<KeyLocatorChecker> keyLocatorChecker =
134 KeyLocatorCheckerFactory::create(propertyIt->second);
135 propertyIt++;
136
137 if (propertyIt != configSection.end())
138 throw Error("Expect the end of checker");
139
140 return make_shared<CustomizedChecker>(getSigType(sigType), keyLocatorChecker);
141}
142
143shared_ptr<Checker>
144CheckerFactory::createHierarchicalChecker(const ConfigSection& configSection)
145{
146 ConfigSection::const_iterator propertyIt = configSection.begin();
147 propertyIt++;
148
149 // Get checker.sig-type
150 if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type"))
151 throw Error("Expect <checker.sig-type>");
152
153 std::string sigType = propertyIt->second.data();
154 propertyIt++;
155
156 if (propertyIt != configSection.end())
157 throw Error("Expect the end of checker");
158
159 return make_shared<HierarchicalChecker>(getSigType(sigType));
160}
161
162uint32_t
163CheckerFactory::getSigType(const std::string& sigType)
164{
165 if (boost::iequals(sigType, "rsa-sha256"))
166 return tlv::SignatureSha256WithRsa;
167 else if (boost::iequals(sigType, "ecdsa-sha256"))
168 return tlv::SignatureSha256WithEcdsa;
169 else if (boost::iequals(sigType, "sha256"))
170 return tlv::DigestSha256;
171 else
172 throw Error("Unsupported signature type");
173}
174
175} // namespace conf
176} // namespace nsl