Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | #ifndef NDN_SECURITY_CONF_CHECKER_HPP |
| 25 | #define NDN_SECURITY_CONF_CHECKER_HPP |
| 26 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 27 | #include "common.hpp" |
| 28 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 29 | #include "key-locator-checker.hpp" |
| 30 | #include "../../util/io.hpp" |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 31 | #include "../validator.hpp" |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 32 | #include "../v1/identity-certificate.hpp" |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 34 | #include <boost/algorithm/string.hpp> |
| 35 | #include <boost/filesystem.hpp> |
| 36 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 37 | |
| 38 | namespace ndn { |
| 39 | namespace security { |
| 40 | namespace conf { |
| 41 | |
| 42 | class Checker |
| 43 | { |
| 44 | public: |
| 45 | typedef function<void(const shared_ptr<const Interest>&)> OnInterestChecked; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 46 | typedef function<void(const shared_ptr<const Interest>&, |
| 47 | const std::string&)> OnInterestCheckFailed; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 48 | typedef function<void(const shared_ptr<const Data>&)> OnDataChecked; |
| 49 | typedef function<void(const shared_ptr<const Data>&, const std::string&)> OnDataCheckFailed; |
| 50 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 51 | virtual |
| 52 | ~Checker() |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @brief check if data satisfies condition defined in the specific checker implementation |
| 58 | * |
| 59 | * @param data Data packet |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 60 | * @retval -1 if data is immediately invalid |
| 61 | * @retval 1 if data is immediately valid |
| 62 | * @retval 0 if further signature verification is needed. |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 63 | */ |
| 64 | virtual int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 65 | check(const Data& data) = 0; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * @brief check if interest satisfies condition defined in the specific checker implementation |
| 69 | * |
| 70 | * @param interest Interest packet |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 71 | * @retval -1 if interest is immediately invalid |
| 72 | * @retval 1 if interest is immediately valid |
| 73 | * @retval 0 if further signature verification is needed. |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 74 | */ |
| 75 | virtual int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 76 | check(const Interest& interest) = 0; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
| 79 | class CustomizedChecker : public Checker |
| 80 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 81 | public: |
| 82 | CustomizedChecker(uint32_t sigType, |
| 83 | shared_ptr<KeyLocatorChecker> keyLocatorChecker) |
| 84 | : m_sigType(sigType) |
| 85 | , m_keyLocatorChecker(keyLocatorChecker) |
| 86 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 87 | switch (sigType) { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 88 | case tlv::SignatureSha256WithRsa: |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 89 | case tlv::SignatureSha256WithEcdsa: { |
| 90 | if (!static_cast<bool>(m_keyLocatorChecker)) |
| 91 | BOOST_THROW_EXCEPTION(Error("Strong signature requires KeyLocatorChecker")); |
| 92 | return; |
| 93 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 94 | case tlv::DigestSha256: |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 95 | return; |
| 96 | default: |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 97 | BOOST_THROW_EXCEPTION(Error("Unsupported signature type")); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 98 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 101 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 102 | check(const Data& data) override |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 103 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 104 | return check(data, data.getSignature()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 107 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 108 | check(const Interest& interest) override |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 109 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 110 | try { |
| 111 | const Name& interestName = interest.getName(); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 112 | Signature signature(interestName[command_interest::POS_SIG_INFO].blockFromValue(), |
| 113 | interestName[command_interest::POS_SIG_VALUE].blockFromValue()); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 114 | return check(interest, signature); |
| 115 | } |
| 116 | catch (const Signature::Error& e) { |
| 117 | // Invalid signature |
| 118 | return -1; |
| 119 | } |
| 120 | catch (const tlv::Error& e) { |
| 121 | // Cannot decode signature related TLVs |
| 122 | return -1; |
| 123 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | private: |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 127 | template<class Packet> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 128 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 129 | check(const Packet& packet, const Signature& signature) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 130 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 131 | if (m_sigType != signature.getType()) { |
| 132 | // Signature type does not match |
| 133 | return -1; |
| 134 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 135 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 136 | if (signature.getType() == tlv::DigestSha256) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 137 | return 0; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 138 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 139 | try { |
| 140 | switch (signature.getType()) { |
| 141 | case tlv::SignatureSha256WithRsa: |
| 142 | case tlv::SignatureSha256WithEcdsa: { |
| 143 | if (!signature.hasKeyLocator()) { |
| 144 | // Missing KeyLocator in SignatureInfo |
| 145 | return -1; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 146 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 147 | break; |
| 148 | } |
| 149 | default: { |
| 150 | // Unsupported signature type |
| 151 | return -1; |
| 152 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 153 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 154 | } |
| 155 | catch (const KeyLocator::Error& e) { |
| 156 | // Cannot decode KeyLocator |
| 157 | return -1; |
| 158 | } |
| 159 | catch (const tlv::Error& e) { |
| 160 | // Cannot decode signature |
| 161 | return -1; |
| 162 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 163 | |
| 164 | std::string failInfo; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 165 | if (m_keyLocatorChecker->check(packet, signature.getKeyLocator(), failInfo)) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 166 | return 0; |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 167 | else { |
| 168 | return -1; |
| 169 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | private: |
| 173 | uint32_t m_sigType; |
| 174 | shared_ptr<KeyLocatorChecker> m_keyLocatorChecker; |
| 175 | }; |
| 176 | |
| 177 | class HierarchicalChecker : public CustomizedChecker |
| 178 | { |
| 179 | public: |
Alexander Afanasyev | a4297a6 | 2014-06-19 13:29:34 -0700 | [diff] [blame] | 180 | explicit |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 181 | HierarchicalChecker(uint32_t sigType) |
| 182 | : CustomizedChecker(sigType, |
| 183 | make_shared<HyperKeyLocatorNameChecker>("^(<>*)$", "\\1", |
| 184 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 185 | "\\1\\2", |
| 186 | KeyLocatorChecker::RELATION_IS_PREFIX_OF)) |
| 187 | { |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | class FixedSignerChecker : public Checker |
| 192 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 193 | public: |
| 194 | FixedSignerChecker(uint32_t sigType, |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 195 | const std::vector<shared_ptr<v1::IdentityCertificate>>& signers) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 196 | : m_sigType(sigType) |
| 197 | { |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 198 | for (std::vector<shared_ptr<v1::IdentityCertificate>>::const_iterator it = signers.begin(); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 199 | it != signers.end(); it++) |
| 200 | m_signers[(*it)->getName().getPrefix(-1)] = (*it); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 201 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 202 | if (sigType != tlv::SignatureSha256WithRsa && |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 203 | sigType != tlv::SignatureSha256WithEcdsa) { |
| 204 | BOOST_THROW_EXCEPTION(Error("FixedSigner is only meaningful for strong signature type")); |
| 205 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 208 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 209 | check(const Data& data) override |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 210 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 211 | return check(data, data.getSignature()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Davide Pesavento | 57c07df | 2016-12-11 18:41:45 -0500 | [diff] [blame] | 214 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 215 | check(const Interest& interest) override |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 216 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 217 | try { |
| 218 | const Name& interestName = interest.getName(); |
Alexander Afanasyev | 70244f4 | 2017-01-04 12:47:12 -0800 | [diff] [blame] | 219 | Signature signature(interestName[command_interest::POS_SIG_INFO].blockFromValue(), |
| 220 | interestName[command_interest::POS_SIG_VALUE].blockFromValue()); |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 221 | return check(interest, signature); |
| 222 | } |
| 223 | catch (const Signature::Error& e) { |
| 224 | // Invalid signature |
| 225 | return -1; |
| 226 | } |
| 227 | catch (const tlv::Error& e) { |
| 228 | // Cannot decode signature related TLVs |
| 229 | return -1; |
| 230 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | private: |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 234 | template<class Packet> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 235 | int8_t |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 236 | check(const Packet& packet, const Signature& signature) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 237 | { |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 238 | if (m_sigType != signature.getType()) { |
| 239 | // Signature type does not match |
| 240 | return -1; |
| 241 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 242 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 243 | if (signature.getType() == tlv::DigestSha256) { |
| 244 | // FixedSigner does not allow Sha256 signature type |
| 245 | return -1; |
| 246 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 247 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 248 | try { |
| 249 | switch (signature.getType()) { |
| 250 | case tlv::SignatureSha256WithRsa: |
| 251 | case tlv::SignatureSha256WithEcdsa: { |
| 252 | if (!signature.hasKeyLocator()) { |
| 253 | // Missing KeyLocator in SignatureInfo |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 254 | return -1; |
| 255 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 256 | break; |
| 257 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 258 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 259 | default: { |
| 260 | // Unsupported signature type |
| 261 | return -1; |
| 262 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 263 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 264 | |
| 265 | const Name& keyLocatorName = signature.getKeyLocator().getName(); |
| 266 | |
| 267 | if (m_signers.find(keyLocatorName) == m_signers.end()) { |
| 268 | // Signer is not in the fixed signer list |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 269 | return -1; |
| 270 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 271 | |
| 272 | if (Validator::verifySignature(packet, signature, |
| 273 | m_signers[keyLocatorName]->getPublicKeyInfo())) { |
| 274 | return 1; |
| 275 | } |
| 276 | else { |
| 277 | // Signature cannot be validated |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 278 | return -1; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 279 | } |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 280 | } |
| 281 | catch (const KeyLocator::Error& e) { |
| 282 | // KeyLocator does not have name |
| 283 | return -1; |
| 284 | } |
| 285 | catch (const tlv::Error& e) { |
| 286 | // Cannot decode signature |
| 287 | return -1; |
| 288 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | private: |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 292 | typedef std::map<Name, shared_ptr<v1::IdentityCertificate>> SignerList; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 293 | uint32_t m_sigType; |
| 294 | SignerList m_signers; |
| 295 | }; |
| 296 | |
| 297 | class CheckerFactory |
| 298 | { |
| 299 | public: |
| 300 | /** |
| 301 | * @brief create a checker from configuration file. |
| 302 | * |
| 303 | * @param configSection The section containing the definition of checker. |
| 304 | * @param configFilename The configuration file name. |
| 305 | * @return a shared pointer to the created checker. |
| 306 | */ |
| 307 | static shared_ptr<Checker> |
| 308 | create(const ConfigSection& configSection, const std::string& configFilename) |
| 309 | { |
| 310 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 311 | |
| 312 | // Get checker.type |
| 313 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 314 | BOOST_THROW_EXCEPTION(Error("Expect <checker.type>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 315 | |
| 316 | std::string type = propertyIt->second.data(); |
| 317 | |
| 318 | if (boost::iequals(type, "customized")) |
| 319 | return createCustomizedChecker(configSection, configFilename); |
| 320 | else if (boost::iequals(type, "hierarchical")) |
| 321 | return createHierarchicalChecker(configSection, configFilename); |
| 322 | else if (boost::iequals(type, "fixed-signer")) |
| 323 | return createFixedSignerChecker(configSection, configFilename); |
| 324 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 325 | BOOST_THROW_EXCEPTION(Error("Unsupported checker type: " + type)); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | private: |
| 329 | static shared_ptr<Checker> |
| 330 | createCustomizedChecker(const ConfigSection& configSection, |
| 331 | const std::string& configFilename) |
| 332 | { |
| 333 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 334 | propertyIt++; |
| 335 | |
| 336 | // Get checker.sig-type |
| 337 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 338 | BOOST_THROW_EXCEPTION(Error("Expect <checker.sig-type>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 339 | |
| 340 | std::string sigType = propertyIt->second.data(); |
| 341 | propertyIt++; |
| 342 | |
| 343 | // Get checker.key-locator |
| 344 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "key-locator")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 345 | BOOST_THROW_EXCEPTION(Error("Expect <checker.key-locator>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 346 | |
| 347 | shared_ptr<KeyLocatorChecker> keyLocatorChecker = |
| 348 | KeyLocatorCheckerFactory::create(propertyIt->second, configFilename); |
| 349 | propertyIt++; |
| 350 | |
| 351 | if (propertyIt != configSection.end()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 352 | BOOST_THROW_EXCEPTION(Error("Expect the end of checker")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 353 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 354 | return make_shared<CustomizedChecker>(getSigType(sigType), keyLocatorChecker); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | static shared_ptr<Checker> |
| 358 | createHierarchicalChecker(const ConfigSection& configSection, |
| 359 | const std::string& configFilename) |
| 360 | { |
| 361 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 362 | propertyIt++; |
| 363 | |
| 364 | // Get checker.sig-type |
| 365 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 366 | BOOST_THROW_EXCEPTION(Error("Expect <checker.sig-type>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 367 | |
| 368 | std::string sigType = propertyIt->second.data(); |
| 369 | propertyIt++; |
| 370 | |
| 371 | if (propertyIt != configSection.end()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 372 | BOOST_THROW_EXCEPTION(Error("Expect the end of checker")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 373 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 374 | return make_shared<HierarchicalChecker>(getSigType(sigType)); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | static shared_ptr<Checker> |
| 378 | createFixedSignerChecker(const ConfigSection& configSection, |
| 379 | const std::string& configFilename) |
| 380 | { |
| 381 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 382 | propertyIt++; |
| 383 | |
| 384 | // Get checker.sig-type |
| 385 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 386 | BOOST_THROW_EXCEPTION(Error("Expect <checker.sig-type>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 387 | |
| 388 | std::string sigType = propertyIt->second.data(); |
| 389 | propertyIt++; |
| 390 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 391 | std::vector<shared_ptr<v1::IdentityCertificate>> signers; |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 392 | for (; propertyIt != configSection.end(); propertyIt++) { |
| 393 | if (!boost::iequals(propertyIt->first, "signer")) |
| 394 | BOOST_THROW_EXCEPTION(Error("Expect <checker.signer> but get <checker." + |
| 395 | propertyIt->first + ">")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 396 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 397 | signers.push_back(getSigner(propertyIt->second, configFilename)); |
| 398 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 399 | |
| 400 | if (propertyIt != configSection.end()) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 401 | BOOST_THROW_EXCEPTION(Error("Expect the end of checker")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 402 | |
| 403 | return shared_ptr<FixedSignerChecker>(new FixedSignerChecker(getSigType(sigType), |
| 404 | signers)); |
| 405 | } |
| 406 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 407 | static shared_ptr<v1::IdentityCertificate> |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 408 | getSigner(const ConfigSection& configSection, const std::string& configFilename) |
| 409 | { |
| 410 | using namespace boost::filesystem; |
| 411 | |
| 412 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 413 | |
| 414 | // Get checker.signer.type |
| 415 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 416 | BOOST_THROW_EXCEPTION(Error("Expect <checker.signer.type>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 417 | |
| 418 | std::string type = propertyIt->second.data(); |
| 419 | propertyIt++; |
| 420 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 421 | if (boost::iequals(type, "file")) { |
| 422 | // Get checker.signer.file-name |
| 423 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "file-name")) |
| 424 | BOOST_THROW_EXCEPTION(Error("Expect <checker.signer.file-name>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 425 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 426 | path certfilePath = absolute(propertyIt->second.data(), |
| 427 | path(configFilename).parent_path()); |
| 428 | propertyIt++; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 429 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 430 | if (propertyIt != configSection.end()) |
| 431 | BOOST_THROW_EXCEPTION(Error("Expect the end of checker.signer")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 432 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 433 | shared_ptr<v1::IdentityCertificate> idCert |
| 434 | = io::load<v1::IdentityCertificate>(certfilePath.c_str()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 435 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 436 | if (static_cast<bool>(idCert)) |
| 437 | return idCert; |
| 438 | else |
| 439 | BOOST_THROW_EXCEPTION(Error("Cannot read certificate from file: " + |
| 440 | certfilePath.native())); |
| 441 | } |
| 442 | else if (boost::iequals(type, "base64")) { |
| 443 | // Get checker.signer.base64-string |
| 444 | if (propertyIt == configSection.end() || |
| 445 | !boost::iequals(propertyIt->first, "base64-string")) |
| 446 | BOOST_THROW_EXCEPTION(Error("Expect <checker.signer.base64-string>")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 447 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 448 | std::stringstream ss(propertyIt->second.data()); |
| 449 | propertyIt++; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 450 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 451 | if (propertyIt != configSection.end()) |
| 452 | BOOST_THROW_EXCEPTION(Error("Expect the end of checker.signer")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 453 | |
Alexander Afanasyev | 2fa5939 | 2016-07-29 17:24:23 -0700 | [diff] [blame] | 454 | shared_ptr<v1::IdentityCertificate> idCert = io::load<v1::IdentityCertificate>(ss); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 455 | |
Zhiyi Zhang | 044bb7e | 2016-06-10 00:02:37 -0700 | [diff] [blame] | 456 | if (static_cast<bool>(idCert)) |
| 457 | return idCert; |
| 458 | else |
| 459 | BOOST_THROW_EXCEPTION(Error("Cannot decode certificate from string")); |
| 460 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 461 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 462 | BOOST_THROW_EXCEPTION(Error("Unsupported checker.signer type: " + type)); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 465 | static uint32_t |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 466 | getSigType(const std::string& sigType) |
| 467 | { |
| 468 | if (boost::iequals(sigType, "rsa-sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 469 | return tlv::SignatureSha256WithRsa; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 470 | else if (boost::iequals(sigType, "ecdsa-sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 471 | return tlv::SignatureSha256WithEcdsa; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 472 | else if (boost::iequals(sigType, "sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 473 | return tlv::DigestSha256; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 474 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 475 | BOOST_THROW_EXCEPTION(Error("Unsupported signature type")); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 476 | } |
| 477 | }; |
| 478 | |
| 479 | } // namespace conf |
| 480 | } // namespace security |
| 481 | } // namespace ndn |
| 482 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 483 | #endif // NDN_SECURITY_CONF_CHECKER_HPP |