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