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