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