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 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 102 | case tlv::SignatureSha256WithRsa: |
| 103 | case tlv::SignatureSha256WithEcdsa: |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 104 | { |
| 105 | if (!static_cast<bool>(m_keyLocatorChecker)) |
| 106 | throw Error("Strong signature requires KeyLocatorChecker"); |
| 107 | |
| 108 | return; |
| 109 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 110 | case tlv::DigestSha256: |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 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 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 142 | catch (tlv::Error& e) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 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 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 166 | if (signature.getType() == tlv::DigestSha256) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 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 | try |
| 170 | { |
| 171 | switch (signature.getType()) |
| 172 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 173 | case tlv::SignatureSha256WithRsa: |
| 174 | case tlv::SignatureSha256WithEcdsa: |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 175 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 176 | if (!signature.hasKeyLocator()) { |
| 177 | onValidationFailed(packet.shared_from_this(), |
| 178 | "Missing KeyLocator in SignatureInfo"); |
| 179 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 180 | break; |
| 181 | } |
| 182 | default: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 183 | { |
| 184 | onValidationFailed(packet.shared_from_this(), |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 185 | "Unsupported signature type: " + |
| 186 | boost::lexical_cast<std::string>(signature.getType())); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 187 | return -1; |
| 188 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 191 | catch (KeyLocator::Error& e) |
| 192 | { |
| 193 | onValidationFailed(packet.shared_from_this(), |
| 194 | "Cannot decode KeyLocator"); |
| 195 | return -1; |
| 196 | } |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 197 | catch (tlv::Error& e) |
| 198 | { |
| 199 | onValidationFailed(packet.shared_from_this(), |
| 200 | "Cannot decode signature"); |
| 201 | return -1; |
| 202 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 203 | |
| 204 | std::string failInfo; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 205 | if (m_keyLocatorChecker->check(packet, signature.getKeyLocator(), failInfo)) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 206 | return 0; |
| 207 | else |
| 208 | { |
| 209 | onValidationFailed(packet.shared_from_this(), failInfo); |
| 210 | return -1; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | private: |
| 215 | uint32_t m_sigType; |
| 216 | shared_ptr<KeyLocatorChecker> m_keyLocatorChecker; |
| 217 | }; |
| 218 | |
| 219 | class HierarchicalChecker : public CustomizedChecker |
| 220 | { |
| 221 | public: |
Alexander Afanasyev | a4297a6 | 2014-06-19 13:29:34 -0700 | [diff] [blame] | 222 | explicit |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 223 | HierarchicalChecker(uint32_t sigType) |
| 224 | : CustomizedChecker(sigType, |
| 225 | make_shared<HyperKeyLocatorNameChecker>("^(<>*)$", "\\1", |
| 226 | "^([^<KEY>]*)<KEY>(<>*)<ksk-.*><ID-CERT>$", |
| 227 | "\\1\\2", |
| 228 | KeyLocatorChecker::RELATION_IS_PREFIX_OF)) |
| 229 | { |
| 230 | } |
| 231 | }; |
| 232 | |
| 233 | class FixedSignerChecker : public Checker |
| 234 | { |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 235 | public: |
| 236 | FixedSignerChecker(uint32_t sigType, |
| 237 | const std::vector<shared_ptr<IdentityCertificate> >& signers) |
| 238 | : m_sigType(sigType) |
| 239 | { |
| 240 | for (std::vector<shared_ptr<IdentityCertificate> >::const_iterator it = signers.begin(); |
| 241 | it != signers.end(); it++) |
| 242 | m_signers[(*it)->getName().getPrefix(-1)] = (*it); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 243 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 244 | if (sigType != tlv::SignatureSha256WithRsa && |
| 245 | sigType != tlv::SignatureSha256WithEcdsa) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 246 | { |
| 247 | throw Error("FixedSigner is only meaningful for strong signature type"); |
| 248 | } |
| 249 | |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | virtual int8_t |
| 253 | check(const Data& data, |
| 254 | const OnDataChecked& onValidated, |
| 255 | const OnDataCheckFailed& onValidationFailed) |
| 256 | { |
| 257 | return check(data, data.getSignature(), onValidated, onValidationFailed); |
| 258 | } |
| 259 | |
| 260 | virtual int8_t |
| 261 | check(const Interest& interest, |
| 262 | const OnInterestChecked& onValidated, |
| 263 | const OnInterestCheckFailed& onValidationFailed) |
| 264 | { |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 265 | try |
| 266 | { |
| 267 | const Name& interestName = interest.getName(); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 268 | Signature signature(interestName[Checker::INTEREST_SIG_INFO].blockFromValue(), |
| 269 | interestName[Checker::INTEREST_SIG_VALUE].blockFromValue()); |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 270 | return check(interest, signature, onValidated, onValidationFailed); |
| 271 | } |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 272 | catch (Signature::Error& e) |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 273 | { |
Yingdi Yu | 96e6406 | 2014-04-15 19:57:33 -0700 | [diff] [blame] | 274 | onValidationFailed(interest.shared_from_this(), "Invalid signature"); |
Yingdi Yu | 20a0696 | 2014-04-17 12:56:04 -0700 | [diff] [blame] | 275 | return -1; |
| 276 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 277 | catch (tlv::Error& e) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 278 | { |
| 279 | onValidationFailed(interest.shared_from_this(), "Cannot decode signature related TLVs"); |
| 280 | return -1; |
| 281 | } |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | private: |
| 285 | template<class Packet, class OnValidated, class OnFailed> |
| 286 | int8_t |
| 287 | check(const Packet& packet, const Signature& signature, |
| 288 | const OnValidated& onValidated, |
| 289 | const OnFailed& onValidationFailed) |
| 290 | { |
| 291 | if (m_sigType != signature.getType()) |
| 292 | { |
| 293 | onValidationFailed(packet.shared_from_this(), |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 294 | "Signature type does not match: " + |
| 295 | boost::lexical_cast<std::string>(m_sigType) + |
| 296 | "!=" + |
| 297 | boost::lexical_cast<std::string>(signature.getType())); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 298 | return -1; |
| 299 | } |
| 300 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 301 | if (signature.getType() == tlv::DigestSha256) |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 302 | { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 303 | onValidationFailed(packet.shared_from_this(), |
| 304 | "FixedSigner does not allow Sha256 signature type"); |
| 305 | return -1; |
| 306 | } |
| 307 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 308 | try |
| 309 | { |
| 310 | switch (signature.getType()) |
| 311 | { |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 312 | case tlv::SignatureSha256WithRsa: |
| 313 | case tlv::SignatureSha256WithEcdsa: |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 314 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 315 | if (!signature.hasKeyLocator()) { |
| 316 | onValidationFailed(packet.shared_from_this(), |
| 317 | "Missing KeyLocator in SignatureInfo"); |
| 318 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 319 | break; |
| 320 | } |
| 321 | default: |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 322 | { |
| 323 | onValidationFailed(packet.shared_from_this(), |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 324 | "Unsupported signature type: " + |
| 325 | boost::lexical_cast<std::string>(signature.getType())); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 326 | return -1; |
| 327 | } |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 328 | } |
| 329 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 330 | const Name& keyLocatorName = signature.getKeyLocator().getName(); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 331 | |
| 332 | if (m_signers.find(keyLocatorName) == m_signers.end()) |
| 333 | { |
| 334 | onValidationFailed(packet.shared_from_this(), |
| 335 | "Signer is not in the fixed signer list: " + |
| 336 | keyLocatorName.toUri()); |
| 337 | return -1; |
| 338 | } |
| 339 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 340 | if (Validator::verifySignature(packet, signature, |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 341 | m_signers[keyLocatorName]->getPublicKeyInfo())) |
| 342 | { |
| 343 | onValidated(packet.shared_from_this()); |
| 344 | return 1; |
| 345 | } |
| 346 | else |
| 347 | { |
| 348 | onValidationFailed(packet.shared_from_this(), |
| 349 | "Signature cannot be validated"); |
| 350 | return -1; |
| 351 | } |
| 352 | } |
| 353 | catch (KeyLocator::Error& e) |
| 354 | { |
| 355 | onValidationFailed(packet.shared_from_this(), |
| 356 | "KeyLocator does not have name"); |
| 357 | return -1; |
| 358 | } |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 359 | catch (tlv::Error& e) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 360 | { |
| 361 | onValidationFailed(packet.shared_from_this(), |
| 362 | "Cannot decode signature"); |
| 363 | return -1; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | private: |
| 368 | typedef std::map<Name, shared_ptr<IdentityCertificate> > SignerList; |
| 369 | |
| 370 | uint32_t m_sigType; |
| 371 | SignerList m_signers; |
| 372 | }; |
| 373 | |
| 374 | class CheckerFactory |
| 375 | { |
| 376 | public: |
| 377 | /** |
| 378 | * @brief create a checker from configuration file. |
| 379 | * |
| 380 | * @param configSection The section containing the definition of checker. |
| 381 | * @param configFilename The configuration file name. |
| 382 | * @return a shared pointer to the created checker. |
| 383 | */ |
| 384 | static shared_ptr<Checker> |
| 385 | create(const ConfigSection& configSection, const std::string& configFilename) |
| 386 | { |
| 387 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 388 | |
| 389 | // Get checker.type |
| 390 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 391 | throw Error("Expect <checker.type>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 392 | |
| 393 | std::string type = propertyIt->second.data(); |
| 394 | |
| 395 | if (boost::iequals(type, "customized")) |
| 396 | return createCustomizedChecker(configSection, configFilename); |
| 397 | else if (boost::iequals(type, "hierarchical")) |
| 398 | return createHierarchicalChecker(configSection, configFilename); |
| 399 | else if (boost::iequals(type, "fixed-signer")) |
| 400 | return createFixedSignerChecker(configSection, configFilename); |
| 401 | else |
| 402 | throw Error("Unsupported checker type: " + type); |
| 403 | } |
| 404 | |
| 405 | private: |
| 406 | static shared_ptr<Checker> |
| 407 | createCustomizedChecker(const ConfigSection& configSection, |
| 408 | const std::string& configFilename) |
| 409 | { |
| 410 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 411 | propertyIt++; |
| 412 | |
| 413 | // Get checker.sig-type |
| 414 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 415 | throw Error("Expect <checker.sig-type>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 416 | |
| 417 | std::string sigType = propertyIt->second.data(); |
| 418 | propertyIt++; |
| 419 | |
| 420 | // Get checker.key-locator |
| 421 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "key-locator")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 422 | throw Error("Expect <checker.key-locator>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 423 | |
| 424 | shared_ptr<KeyLocatorChecker> keyLocatorChecker = |
| 425 | KeyLocatorCheckerFactory::create(propertyIt->second, configFilename); |
| 426 | propertyIt++; |
| 427 | |
| 428 | if (propertyIt != configSection.end()) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 429 | throw Error("Expect the end of checker"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 430 | |
Alexander Afanasyev | f73f063 | 2014-05-12 18:02:37 -0700 | [diff] [blame] | 431 | return make_shared<CustomizedChecker>(getSigType(sigType), keyLocatorChecker); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | static shared_ptr<Checker> |
| 435 | createHierarchicalChecker(const ConfigSection& configSection, |
| 436 | const std::string& configFilename) |
| 437 | { |
| 438 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 439 | propertyIt++; |
| 440 | |
| 441 | // Get checker.sig-type |
| 442 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 443 | throw Error("Expect <checker.sig-type>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 444 | |
| 445 | std::string sigType = propertyIt->second.data(); |
| 446 | propertyIt++; |
| 447 | |
| 448 | if (propertyIt != configSection.end()) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 449 | throw Error("Expect the end of checker"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 450 | |
Alexander Afanasyev | b67090a | 2014-04-29 22:31:01 -0700 | [diff] [blame] | 451 | return make_shared<HierarchicalChecker>(getSigType(sigType)); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static shared_ptr<Checker> |
| 455 | createFixedSignerChecker(const ConfigSection& configSection, |
| 456 | const std::string& configFilename) |
| 457 | { |
| 458 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 459 | propertyIt++; |
| 460 | |
| 461 | // Get checker.sig-type |
| 462 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "sig-type")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 463 | throw Error("Expect <checker.sig-type>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 464 | |
| 465 | std::string sigType = propertyIt->second.data(); |
| 466 | propertyIt++; |
| 467 | |
| 468 | std::vector<shared_ptr<IdentityCertificate> > signers; |
| 469 | for (; propertyIt != configSection.end(); propertyIt++) |
| 470 | { |
| 471 | if (!boost::iequals(propertyIt->first, "signer")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 472 | throw Error("Expect <checker.signer> but get <checker." + |
| 473 | propertyIt->first + ">"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 474 | |
| 475 | signers.push_back(getSigner(propertyIt->second, configFilename)); |
| 476 | } |
| 477 | |
| 478 | if (propertyIt != configSection.end()) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 479 | throw Error("Expect the end of checker"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 480 | |
| 481 | return shared_ptr<FixedSignerChecker>(new FixedSignerChecker(getSigType(sigType), |
| 482 | signers)); |
| 483 | } |
| 484 | |
| 485 | static shared_ptr<IdentityCertificate> |
| 486 | getSigner(const ConfigSection& configSection, const std::string& configFilename) |
| 487 | { |
| 488 | using namespace boost::filesystem; |
| 489 | |
| 490 | ConfigSection::const_iterator propertyIt = configSection.begin(); |
| 491 | |
| 492 | // Get checker.signer.type |
| 493 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "type")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 494 | throw Error("Expect <checker.signer.type>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 495 | |
| 496 | std::string type = propertyIt->second.data(); |
| 497 | propertyIt++; |
| 498 | |
| 499 | if (boost::iequals(type, "file")) |
| 500 | { |
| 501 | // Get checker.signer.file-name |
| 502 | if (propertyIt == configSection.end() || !boost::iequals(propertyIt->first, "file-name")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 503 | throw Error("Expect <checker.signer.file-name>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 504 | |
| 505 | path certfilePath = absolute(propertyIt->second.data(), |
| 506 | path(configFilename).parent_path()); |
| 507 | propertyIt++; |
| 508 | |
| 509 | if (propertyIt != configSection.end()) |
| 510 | throw Error("Expect the end of checker.signer"); |
| 511 | |
| 512 | shared_ptr<IdentityCertificate> idCert |
| 513 | = io::load<IdentityCertificate>(certfilePath.c_str()); |
| 514 | |
| 515 | if (static_cast<bool>(idCert)) |
| 516 | return idCert; |
| 517 | else |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 518 | throw Error("Cannot read certificate from file: " + |
| 519 | certfilePath.native()); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 520 | } |
| 521 | else if (boost::iequals(type, "base64")) |
| 522 | { |
| 523 | // Get checker.signer.base64-string |
| 524 | if (propertyIt == configSection.end() || |
| 525 | !boost::iequals(propertyIt->first, "base64-string")) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 526 | throw Error("Expect <checker.signer.base64-string>"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 527 | |
| 528 | std::stringstream ss(propertyIt->second.data()); |
| 529 | propertyIt++; |
| 530 | |
| 531 | if (propertyIt != configSection.end()) |
| 532 | throw Error("Expect the end of checker.signer"); |
| 533 | |
| 534 | shared_ptr<IdentityCertificate> idCert = io::load<IdentityCertificate>(ss); |
| 535 | |
| 536 | if (static_cast<bool>(idCert)) |
| 537 | return idCert; |
| 538 | else |
| 539 | throw Error("Cannot decode certificate from string"); |
| 540 | } |
| 541 | else |
| 542 | throw Error("Unsupported checker.signer type: " + type); |
| 543 | } |
| 544 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 545 | static uint32_t |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 546 | getSigType(const std::string& sigType) |
| 547 | { |
| 548 | if (boost::iequals(sigType, "rsa-sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 549 | return tlv::SignatureSha256WithRsa; |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 550 | else if (boost::iequals(sigType, "ecdsa-sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 551 | return tlv::SignatureSha256WithEcdsa; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 552 | else if (boost::iequals(sigType, "sha256")) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 553 | return tlv::DigestSha256; |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 554 | else |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 555 | throw Error("Unsupported signature type"); |
Yingdi Yu | 48e8c0c | 2014-03-19 12:01:55 -0700 | [diff] [blame] | 556 | } |
| 557 | }; |
| 558 | |
| 559 | } // namespace conf |
| 560 | } // namespace security |
| 561 | } // namespace ndn |
| 562 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 563 | #endif // NDN_SECURITY_CONF_CHECKER_HPP |