Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #include "security/conf/checker.hpp" |
| 23 | #include "security/key-chain.hpp" |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 24 | #include "identity-management-fixture.hpp" |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 25 | #include "boost-test.hpp" |
| 26 | |
| 27 | namespace ndn { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 28 | namespace security { |
| 29 | namespace conf { |
| 30 | namespace tests { |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 31 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 32 | BOOST_FIXTURE_TEST_SUITE(SecurityConfChecker, IdentityManagementFixture) |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 33 | |
| 34 | void |
| 35 | interestChecked(const shared_ptr<const Interest>& interest) |
| 36 | { |
| 37 | BOOST_CHECK(true); |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | interestCheckFailed(const shared_ptr<const Interest>& interest, |
| 42 | const std::string& failureInfo) |
| 43 | { |
| 44 | std::cerr << failureInfo << std::endl; |
| 45 | BOOST_CHECK(false); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | interestCheckedFalse(const shared_ptr<const Interest>& interest) |
| 50 | { |
| 51 | BOOST_CHECK(false); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | interestCheckFailedFalse(const shared_ptr<const Interest>& interest, |
| 56 | const std::string& failureInfo) |
| 57 | { |
| 58 | BOOST_CHECK(true); |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | dataChecked(const shared_ptr<const Data>& interest) |
| 63 | { |
| 64 | BOOST_CHECK(true); |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | dataCheckFailed(const shared_ptr<const Data>& interest, |
| 69 | const std::string& failureInfo) |
| 70 | { |
| 71 | std::cerr << failureInfo << std::endl; |
| 72 | BOOST_CHECK(false); |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | dataCheckedFalse(const shared_ptr<const Data>& interest) |
| 77 | { |
| 78 | BOOST_CHECK(false); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | dataCheckFailedFalse(const shared_ptr<const Data>& interest, |
| 83 | const std::string& failureInfo) |
| 84 | { |
| 85 | BOOST_CHECK(true); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(CustomizedCheckerTest1) |
| 90 | { |
| 91 | using security::conf::CustomizedChecker; |
| 92 | using security::conf::KeyLocatorChecker; |
| 93 | using security::conf::RelationKeyLocatorNameChecker; |
| 94 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 95 | Name identity("/SecurityTestConfChecker/CustomizedCheckerTest1"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 96 | BOOST_REQUIRE(addIdentity(identity, RsaKeyParams())); |
| 97 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 98 | |
| 99 | Name identity2("/SecurityTestConfChecker/CustomizedCheckerTest1Wrong"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 100 | BOOST_REQUIRE(addIdentity(identity2, RsaKeyParams())); |
| 101 | Name certName2 = m_keyChain.getDefaultCertificateNameForIdentity(identity2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 102 | |
| 103 | Name packetName("/SecurityTestConfChecker/CustomizedCheckerTest1/Data"); |
| 104 | shared_ptr<Data> data1 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 105 | m_keyChain.sign(*data1, |
| 106 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 107 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 108 | |
| 109 | shared_ptr<Data> data2 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 110 | m_keyChain.sign(*data2, |
| 111 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 112 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 113 | |
| 114 | shared_ptr<Interest> interest1 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 115 | m_keyChain.sign(*interest1, |
| 116 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 117 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 118 | |
| 119 | shared_ptr<Interest> interest2 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 120 | m_keyChain.sign(*interest2, |
| 121 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 122 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 123 | |
| 124 | int8_t result = 0; |
| 125 | |
| 126 | |
| 127 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerEqual1 = |
| 128 | make_shared<RelationKeyLocatorNameChecker>(certName.getPrefix(-1), |
| 129 | KeyLocatorChecker::RELATION_EQUAL); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 130 | CustomizedChecker checker1(tlv::SignatureSha256WithRsa, keyLocatorCheckerEqual1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 131 | |
| 132 | result = checker1.check(*data1, |
| 133 | bind(dataChecked, _1), |
| 134 | bind(dataCheckFailed, _1, _2)); |
| 135 | BOOST_CHECK_EQUAL(result, 0); |
| 136 | |
| 137 | result = checker1.check(*data2, |
| 138 | bind(dataCheckedFalse, _1), |
| 139 | bind(dataCheckFailedFalse, _1, _2)); |
| 140 | BOOST_CHECK_EQUAL(result, -1); |
| 141 | |
| 142 | result = checker1.check(*interest1, |
| 143 | bind(interestChecked, _1), |
| 144 | bind(interestCheckFailed, _1, _2)); |
| 145 | BOOST_CHECK_EQUAL(result, 0); |
| 146 | |
| 147 | result = checker1.check(*interest2, |
| 148 | bind(interestCheckedFalse, _1), |
| 149 | bind(interestCheckFailedFalse, _1, _2)); |
| 150 | BOOST_CHECK_EQUAL(result, -1); |
| 151 | |
| 152 | |
| 153 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerEqual2 = |
| 154 | make_shared<RelationKeyLocatorNameChecker>(identity, |
| 155 | KeyLocatorChecker::RELATION_EQUAL); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 156 | CustomizedChecker checker2(tlv::SignatureSha256WithRsa, keyLocatorCheckerEqual2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 157 | |
| 158 | result = checker2.check(*data1, |
| 159 | bind(dataCheckedFalse, _1), |
| 160 | bind(dataCheckFailedFalse, _1, _2)); |
| 161 | BOOST_CHECK_EQUAL(result, -1); |
| 162 | |
| 163 | result = checker2.check(*interest1, |
| 164 | bind(interestCheckedFalse, _1), |
| 165 | bind(interestCheckFailedFalse, _1, _2)); |
| 166 | BOOST_CHECK_EQUAL(result, -1); |
| 167 | |
| 168 | |
| 169 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerPrefix1 = |
| 170 | make_shared<RelationKeyLocatorNameChecker>(certName.getPrefix(-1), |
| 171 | KeyLocatorChecker::RELATION_IS_PREFIX_OF); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 172 | CustomizedChecker checker3(tlv::SignatureSha256WithRsa, keyLocatorCheckerPrefix1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 173 | |
| 174 | result = checker3.check(*data1, |
| 175 | bind(dataChecked, _1), |
| 176 | bind(dataCheckFailed, _1, _2)); |
| 177 | BOOST_CHECK_EQUAL(result, 0); |
| 178 | |
| 179 | result = checker3.check(*data2, |
| 180 | bind(dataCheckedFalse, _1), |
| 181 | bind(dataCheckFailedFalse, _1, _2)); |
| 182 | BOOST_CHECK_EQUAL(result, -1); |
| 183 | |
| 184 | |
| 185 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerPrefix2 = |
| 186 | make_shared<RelationKeyLocatorNameChecker>(identity, |
| 187 | KeyLocatorChecker::RELATION_IS_PREFIX_OF); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 188 | CustomizedChecker checker4(tlv::SignatureSha256WithRsa, keyLocatorCheckerPrefix2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 189 | |
| 190 | result = checker4.check(*data1, |
| 191 | bind(dataChecked, _1), |
| 192 | bind(dataCheckFailed, _1, _2)); |
| 193 | BOOST_CHECK_EQUAL(result, 0); |
| 194 | |
| 195 | result = checker4.check(*data2, |
| 196 | bind(dataCheckedFalse, _1), |
| 197 | bind(dataCheckFailedFalse, _1, _2)); |
| 198 | BOOST_CHECK_EQUAL(result, -1); |
| 199 | |
| 200 | |
| 201 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerStrict1 = |
| 202 | make_shared<RelationKeyLocatorNameChecker>(certName.getPrefix(-1), |
| 203 | KeyLocatorChecker::RELATION_IS_STRICT_PREFIX_OF); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 204 | CustomizedChecker checker5(tlv::SignatureSha256WithRsa, keyLocatorCheckerStrict1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 205 | |
| 206 | result = checker5.check(*data1, |
| 207 | bind(dataCheckedFalse, _1), |
| 208 | bind(dataCheckFailedFalse, _1, _2)); |
| 209 | BOOST_CHECK_EQUAL(result, -1); |
| 210 | |
| 211 | result = checker5.check(*data2, |
| 212 | bind(dataCheckedFalse, _1), |
| 213 | bind(dataCheckFailedFalse, _1, _2)); |
| 214 | BOOST_CHECK_EQUAL(result, -1); |
| 215 | |
| 216 | shared_ptr<RelationKeyLocatorNameChecker> keyLocatorCheckerStrict2 = |
| 217 | make_shared<RelationKeyLocatorNameChecker>(identity, |
| 218 | KeyLocatorChecker::RELATION_IS_STRICT_PREFIX_OF); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 219 | CustomizedChecker checker6(tlv::SignatureSha256WithRsa, keyLocatorCheckerStrict2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 220 | |
| 221 | result = checker6.check(*data1, |
| 222 | bind(dataCheckedFalse, _1), |
| 223 | bind(dataCheckFailedFalse, _1, _2)); |
| 224 | BOOST_CHECK_EQUAL(result, 0); |
| 225 | |
| 226 | result = checker6.check(*data2, |
| 227 | bind(dataCheckedFalse, _1), |
| 228 | bind(dataCheckFailedFalse, _1, _2)); |
| 229 | BOOST_CHECK_EQUAL(result, -1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | BOOST_AUTO_TEST_CASE(CustomizedCheckerTest2) |
| 233 | { |
| 234 | using security::conf::CustomizedChecker; |
| 235 | using security::conf::KeyLocatorChecker; |
| 236 | using security::conf::RegexKeyLocatorNameChecker; |
| 237 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 238 | Name identity("/SecurityTestConfChecker/CustomizedCheckerTest2"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 239 | BOOST_REQUIRE(addIdentity(identity, RsaKeyParams())); |
| 240 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 241 | |
| 242 | Name identity2("/SecurityTestConfChecker/CustomizedCheckerTest2Wrong"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 243 | BOOST_REQUIRE(addIdentity(identity2, RsaKeyParams())); |
| 244 | Name certName2 = m_keyChain.getDefaultCertificateNameForIdentity(identity2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 245 | |
| 246 | Name packetName("/SecurityTestConfChecker/CustomizedCheckerTest2/Data"); |
| 247 | shared_ptr<Data> data1 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 248 | m_keyChain.sign(*data1, |
| 249 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 250 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 251 | |
| 252 | shared_ptr<Data> data2 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 253 | m_keyChain.sign(*data2, |
| 254 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 255 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 256 | |
| 257 | shared_ptr<Interest> interest1 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 258 | m_keyChain.sign(*interest1, |
| 259 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 260 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 261 | |
| 262 | shared_ptr<Interest> interest2 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 263 | m_keyChain.sign(*interest2, |
| 264 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 265 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 266 | |
| 267 | int8_t result = 0; |
| 268 | |
| 269 | |
| 270 | shared_ptr<RegexKeyLocatorNameChecker> keyLocatorCheckerRegex1 = |
| 271 | make_shared<RegexKeyLocatorNameChecker>( |
| 272 | Regex("^<SecurityTestConfChecker><CustomizedCheckerTest2>")); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 273 | CustomizedChecker checker1(tlv::SignatureSha256WithRsa, keyLocatorCheckerRegex1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 274 | |
| 275 | result = checker1.check(*data1, |
| 276 | bind(dataChecked, _1), |
| 277 | bind(dataCheckFailed, _1, _2)); |
| 278 | BOOST_CHECK_EQUAL(result, 0); |
| 279 | |
| 280 | result = checker1.check(*data2, |
| 281 | bind(dataCheckedFalse, _1), |
| 282 | bind(dataCheckFailedFalse, _1, _2)); |
| 283 | BOOST_CHECK_EQUAL(result, -1); |
| 284 | |
| 285 | result = checker1.check(*interest1, |
| 286 | bind(interestChecked, _1), |
| 287 | bind(interestCheckFailed, _1, _2)); |
| 288 | BOOST_CHECK_EQUAL(result, 0); |
| 289 | |
| 290 | result = checker1.check(*interest2, |
| 291 | bind(interestCheckedFalse, _1), |
| 292 | bind(interestCheckFailedFalse, _1, _2)); |
| 293 | BOOST_CHECK_EQUAL(result, -1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | BOOST_AUTO_TEST_CASE(CustomizedCheckerTest3) |
| 297 | { |
| 298 | using security::conf::CustomizedChecker; |
| 299 | using security::conf::KeyLocatorChecker; |
| 300 | using security::conf::RegexKeyLocatorNameChecker; |
| 301 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 302 | Name identity("/SecurityTestConfChecker/CustomizedCheckerTest3"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 303 | BOOST_REQUIRE(addIdentity(identity, EcdsaKeyParams())); |
| 304 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 305 | |
| 306 | Name identity2("/SecurityTestConfChecker/CustomizedCheckerTest3Wrong"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 307 | BOOST_REQUIRE(addIdentity(identity2, EcdsaKeyParams())); |
| 308 | Name certName2 = m_keyChain.getDefaultCertificateNameForIdentity(identity2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 309 | |
| 310 | Name packetName("/SecurityTestConfChecker/CustomizedCheckerTest3/Data"); |
| 311 | shared_ptr<Data> data1 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 312 | m_keyChain.sign(*data1, |
| 313 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 314 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 315 | |
| 316 | shared_ptr<Data> data2 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 317 | m_keyChain.sign(*data2, |
| 318 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 319 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 320 | |
| 321 | shared_ptr<Interest> interest1 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 322 | m_keyChain.sign(*interest1, |
| 323 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 324 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 325 | |
| 326 | shared_ptr<Interest> interest2 = make_shared<Interest>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 327 | m_keyChain.sign(*interest2, |
| 328 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 329 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 330 | |
| 331 | int8_t result = 0; |
| 332 | |
| 333 | |
| 334 | shared_ptr<RegexKeyLocatorNameChecker> keyLocatorCheckerRegex1 = |
| 335 | make_shared<RegexKeyLocatorNameChecker>( |
| 336 | Regex("^<SecurityTestConfChecker><CustomizedCheckerTest3>")); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 337 | CustomizedChecker checker1(tlv::SignatureSha256WithEcdsa, keyLocatorCheckerRegex1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 338 | |
| 339 | result = checker1.check(*data1, |
| 340 | bind(dataChecked, _1), |
| 341 | bind(dataCheckFailed, _1, _2)); |
| 342 | BOOST_CHECK_EQUAL(result, 0); |
| 343 | |
| 344 | result = checker1.check(*data2, |
| 345 | bind(dataCheckedFalse, _1), |
| 346 | bind(dataCheckFailedFalse, _1, _2)); |
| 347 | BOOST_CHECK_EQUAL(result, -1); |
| 348 | |
| 349 | result = checker1.check(*interest1, |
| 350 | bind(interestChecked, _1), |
| 351 | bind(interestCheckFailed, _1, _2)); |
| 352 | BOOST_CHECK_EQUAL(result, 0); |
| 353 | |
| 354 | result = checker1.check(*interest2, |
| 355 | bind(interestCheckedFalse, _1), |
| 356 | bind(interestCheckFailedFalse, _1, _2)); |
| 357 | BOOST_CHECK_EQUAL(result, -1); |
| 358 | |
| 359 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 360 | CustomizedChecker checker2(tlv::SignatureSha256WithRsa, keyLocatorCheckerRegex1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 361 | |
| 362 | result = checker2.check(*data1, |
| 363 | bind(dataCheckedFalse, _1), |
| 364 | bind(dataCheckFailedFalse, _1, _2)); |
| 365 | BOOST_CHECK_EQUAL(result, -1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | BOOST_AUTO_TEST_CASE(HierarchicalCheckerTest1) |
| 369 | { |
| 370 | using security::conf::HierarchicalChecker; |
| 371 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 372 | Name identity("/SecurityTestConfChecker/HierarchicalCheckerTest1"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 373 | BOOST_REQUIRE(addIdentity(identity, EcdsaKeyParams())); |
| 374 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 375 | |
| 376 | Name identity2("/SecurityTestConfChecker/HierarchicalCheckerTest1/Data"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 377 | BOOST_REQUIRE(addIdentity(identity2, RsaKeyParams())); |
| 378 | Name certName2 = m_keyChain.getDefaultCertificateNameForIdentity(identity2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 379 | |
| 380 | Name packetName("/SecurityTestConfChecker/HierarchicalCheckerTest1/Data"); |
| 381 | Name packetName2("/SecurityTestConfChecker"); |
| 382 | Name packetName3("/SecurityTestConfChecker/HierarchicalCheckerTest1"); |
| 383 | |
| 384 | shared_ptr<Data> data1 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 385 | m_keyChain.sign(*data1, |
| 386 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 387 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 388 | |
| 389 | shared_ptr<Data> data2 = make_shared<Data>(packetName2); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 390 | m_keyChain.sign(*data2, |
| 391 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 392 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 393 | |
| 394 | shared_ptr<Data> data3 = make_shared<Data>(packetName3); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 395 | m_keyChain.sign(*data3, |
| 396 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 397 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 398 | |
| 399 | shared_ptr<Data> data4 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 400 | m_keyChain.sign(*data4, |
| 401 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 402 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 403 | |
| 404 | shared_ptr<Data> data5 = make_shared<Data>(packetName2); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 405 | m_keyChain.sign(*data5, |
| 406 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 407 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 408 | |
| 409 | shared_ptr<Data> data6 = make_shared<Data>(packetName3); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 410 | m_keyChain.sign(*data6, |
| 411 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 412 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 413 | |
| 414 | int8_t result = 0; |
| 415 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 416 | HierarchicalChecker checker1(tlv::SignatureSha256WithEcdsa); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 417 | |
| 418 | result = checker1.check(*data1, |
| 419 | bind(dataChecked, _1), |
| 420 | bind(dataCheckFailed, _1, _2)); |
| 421 | BOOST_CHECK_EQUAL(result, 0); |
| 422 | |
| 423 | result = checker1.check(*data2, |
| 424 | bind(dataCheckedFalse, _1), |
| 425 | bind(dataCheckFailedFalse, _1, _2)); |
| 426 | BOOST_CHECK_EQUAL(result, -1); |
| 427 | |
| 428 | result = checker1.check(*data3, |
| 429 | bind(dataChecked, _1), |
| 430 | bind(dataCheckFailed, _1, _2)); |
| 431 | BOOST_CHECK_EQUAL(result, 0); |
| 432 | |
| 433 | result = checker1.check(*data4, |
| 434 | bind(dataCheckedFalse, _1), |
| 435 | bind(dataCheckFailedFalse, _1, _2)); |
| 436 | BOOST_CHECK_EQUAL(result, -1); |
| 437 | |
| 438 | result = checker1.check(*data5, |
| 439 | bind(dataCheckedFalse, _1), |
| 440 | bind(dataCheckFailedFalse, _1, _2)); |
| 441 | BOOST_CHECK_EQUAL(result, -1); |
| 442 | |
| 443 | result = checker1.check(*data6, |
| 444 | bind(dataCheckedFalse, _1), |
| 445 | bind(dataCheckFailedFalse, _1, _2)); |
| 446 | BOOST_CHECK_EQUAL(result, -1); |
| 447 | |
| 448 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 449 | HierarchicalChecker checker2(tlv::SignatureSha256WithRsa); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 450 | |
| 451 | result = checker2.check(*data1, |
| 452 | bind(dataCheckedFalse, _1), |
| 453 | bind(dataCheckFailedFalse, _1, _2)); |
| 454 | BOOST_CHECK_EQUAL(result, -1); |
| 455 | |
| 456 | result = checker2.check(*data2, |
| 457 | bind(dataCheckedFalse, _1), |
| 458 | bind(dataCheckFailedFalse, _1, _2)); |
| 459 | BOOST_CHECK_EQUAL(result, -1); |
| 460 | |
| 461 | result = checker2.check(*data3, |
| 462 | bind(dataCheckedFalse, _1), |
| 463 | bind(dataCheckFailedFalse, _1, _2)); |
| 464 | BOOST_CHECK_EQUAL(result, -1); |
| 465 | |
| 466 | result = checker2.check(*data4, |
| 467 | bind(dataChecked, _1), |
| 468 | bind(dataCheckFailed, _1, _2)); |
| 469 | BOOST_CHECK_EQUAL(result, 0); |
| 470 | |
| 471 | result = checker2.check(*data5, |
| 472 | bind(dataCheckedFalse, _1), |
| 473 | bind(dataCheckFailedFalse, _1, _2)); |
| 474 | BOOST_CHECK_EQUAL(result, -1); |
| 475 | |
| 476 | result = checker2.check(*data6, |
| 477 | bind(dataCheckedFalse, _1), |
| 478 | bind(dataCheckFailedFalse, _1, _2)); |
| 479 | BOOST_CHECK_EQUAL(result, -1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | BOOST_AUTO_TEST_CASE(FixedSignerCheckerTest1) |
| 483 | { |
| 484 | using security::conf::FixedSignerChecker; |
| 485 | |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 486 | Name identity("/SecurityTestConfChecker/FixedSignerCheckerTest1"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 487 | BOOST_REQUIRE(addIdentity(identity, EcdsaKeyParams())); |
| 488 | Name certName = m_keyChain.getDefaultCertificateNameForIdentity(identity); |
| 489 | shared_ptr<IdentityCertificate> cert1 = m_keyChain.getCertificate(certName); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 490 | |
| 491 | Name identity2("/SecurityTestConfChecker/FixedSignerCheckerTest1Wrong"); |
Yingdi Yu | 3ed09d0 | 2014-10-13 16:24:08 -0700 | [diff] [blame] | 492 | BOOST_REQUIRE(addIdentity(identity2, RsaKeyParams())); |
| 493 | Name certName2 = m_keyChain.getDefaultCertificateNameForIdentity(identity2); |
| 494 | shared_ptr<IdentityCertificate> cert2 = m_keyChain.getCertificate(certName2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 495 | |
| 496 | Name packetName("/Test/Data"); |
| 497 | |
| 498 | shared_ptr<Data> data1 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 499 | m_keyChain.sign(*data1, |
| 500 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 501 | identity)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 502 | |
| 503 | shared_ptr<Data> data2 = make_shared<Data>(packetName); |
Yingdi Yu | 1b0311c | 2015-06-10 14:58:47 -0700 | [diff] [blame] | 504 | m_keyChain.sign(*data2, |
| 505 | security::SigningInfo(security::SigningInfo::SIGNER_TYPE_ID, |
| 506 | identity2)); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 507 | |
| 508 | std::vector<shared_ptr<IdentityCertificate> > certSet1; |
| 509 | certSet1.push_back(cert1); |
| 510 | |
| 511 | std::vector<shared_ptr<IdentityCertificate> > certSet2; |
| 512 | certSet2.push_back(cert2); |
| 513 | |
| 514 | |
| 515 | int8_t result = 0; |
| 516 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 517 | FixedSignerChecker checker1(tlv::SignatureSha256WithEcdsa, certSet1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 518 | |
| 519 | result = checker1.check(*data1, |
| 520 | bind(dataChecked, _1), |
| 521 | bind(dataCheckFailed, _1, _2)); |
| 522 | BOOST_CHECK_EQUAL(result, 1); |
| 523 | |
| 524 | result = checker1.check(*data2, |
| 525 | bind(dataCheckedFalse, _1), |
| 526 | bind(dataCheckFailedFalse, _1, _2)); |
| 527 | BOOST_CHECK_EQUAL(result, -1); |
| 528 | |
| 529 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 530 | FixedSignerChecker checker2(tlv::SignatureSha256WithRsa, certSet1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 531 | |
| 532 | result = checker2.check(*data1, |
| 533 | bind(dataCheckedFalse, _1), |
| 534 | bind(dataCheckFailedFalse, _1, _2)); |
| 535 | BOOST_CHECK_EQUAL(result, -1); |
| 536 | |
| 537 | result = checker2.check(*data2, |
| 538 | bind(dataCheckedFalse, _1), |
| 539 | bind(dataCheckFailedFalse, _1, _2)); |
| 540 | BOOST_CHECK_EQUAL(result, -1); |
| 541 | |
| 542 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 543 | FixedSignerChecker checker3(tlv::SignatureSha256WithEcdsa, certSet2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 544 | |
| 545 | result = checker3.check(*data1, |
| 546 | bind(dataCheckedFalse, _1), |
| 547 | bind(dataCheckFailedFalse, _1, _2)); |
| 548 | BOOST_CHECK_EQUAL(result, -1); |
| 549 | |
| 550 | result = checker3.check(*data2, |
| 551 | bind(dataCheckedFalse, _1), |
| 552 | bind(dataCheckFailedFalse, _1, _2)); |
| 553 | BOOST_CHECK_EQUAL(result, -1); |
| 554 | |
| 555 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 556 | FixedSignerChecker checker4(tlv::SignatureSha256WithRsa, certSet2); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 557 | |
| 558 | result = checker4.check(*data1, |
| 559 | bind(dataCheckedFalse, _1), |
| 560 | bind(dataCheckFailedFalse, _1, _2)); |
| 561 | BOOST_CHECK_EQUAL(result, -1); |
| 562 | |
| 563 | result = checker4.check(*data2, |
| 564 | bind(dataChecked, _1), |
| 565 | bind(dataCheckFailed, _1, _2)); |
| 566 | BOOST_CHECK_EQUAL(result, 1); |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | BOOST_AUTO_TEST_SUITE_END() |
| 570 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 571 | } // namespace tests |
| 572 | } // namespace conf |
| 573 | } // namespace security |
Yingdi Yu | 5ec0ee3 | 2014-06-24 16:26:09 -0700 | [diff] [blame] | 574 | } // namespace ndn |