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