Yingdi Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California |
Yingdi Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 5 | * 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 Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 9 | * 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 Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 13 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 14 | * 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 Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 17 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame^] | 18 | * 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 Yu | 7d77332 | 2015-03-22 21:32:48 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include "checker.hpp" |
| 23 | |
| 24 | #include <boost/algorithm/string.hpp> |
| 25 | |
| 26 | namespace nsl { |
| 27 | namespace conf { |
| 28 | |
| 29 | Checker::~Checker() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | CustomizedChecker::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 | |
| 54 | bool |
| 55 | CustomizedChecker::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 | |
| 88 | HierarchicalChecker::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 | |
| 97 | shared_ptr<Checker> |
| 98 | CheckerFactory::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 | |
| 116 | shared_ptr<Checker> |
| 117 | CheckerFactory::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 | |
| 143 | shared_ptr<Checker> |
| 144 | CheckerFactory::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 | |
| 162 | uint32_t |
| 163 | CheckerFactory::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 |