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 | 4270f20 | 2014-01-28 14:19:16 -0800 | [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. |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 22 | #include "security/key-chain.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 23 | #include "security/validator.hpp" |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 24 | |
Yingdi Yu | c4f6fd7 | 2014-02-26 12:48:44 -0800 | [diff] [blame] | 25 | #include "util/command-interest-generator.hpp" |
| 26 | #include "util/command-interest-validator.hpp" |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 28 | #include "boost-test.hpp" |
| 29 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 30 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 31 | namespace ndn { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame] | 33 | BOOST_AUTO_TEST_SUITE(SecurityTestSignedInterest) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 34 | |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 35 | BOOST_AUTO_TEST_CASE(SignedInterest) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 36 | { |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 37 | BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file")); |
| 38 | KeyChain keyChain("sqlite3", "file"); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 40 | Name identityName("/TestSignedInterest/SignVerify"); |
| 41 | identityName.appendVersion(); |
| 42 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 43 | Name certificateName; |
| 44 | BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName)); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 45 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 46 | Interest interest("/TestSignedInterest/SignVerify/Interest1"); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 47 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 48 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 49 | Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size()); |
| 50 | |
| 51 | Interest interest2; |
| 52 | interest2.wireDecode(interestBlock); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 53 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 54 | shared_ptr<PublicKey> publicKey; |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 55 | BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm( |
| 56 | keyChain.getDefaultKeyNameForIdentity(identityName))); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 57 | bool result = Validator::verifySignature(interest2, *publicKey); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 58 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 59 | BOOST_CHECK_EQUAL(result, true); |
| 60 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 61 | keyChain.deleteIdentity(identityName); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 64 | class CommandInterestFixture |
| 65 | { |
| 66 | public: |
| 67 | CommandInterestFixture() |
| 68 | : m_validity(false) |
| 69 | {} |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 71 | void |
| 72 | validated(const shared_ptr<const Interest>& interest) |
| 73 | { m_validity = true; } |
| 74 | |
| 75 | void |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 76 | validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo) |
| 77 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 78 | m_validity = false; |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 79 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 80 | |
| 81 | void |
| 82 | reset() |
| 83 | { m_validity = false; } |
| 84 | |
| 85 | bool m_validity; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 86 | }; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 87 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 88 | BOOST_FIXTURE_TEST_CASE(CommandInterest, CommandInterestFixture) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 89 | { |
| 90 | KeyChain keyChain; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | Name identity("/TestCommandInterest/Validation"); |
| 92 | identity.appendVersion(); |
| 93 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 94 | Name certName; |
| 95 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 96 | |
| 97 | CommandInterestGenerator generator; |
| 98 | CommandInterestValidator validator; |
| 99 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 100 | validator.addInterestRule("^<TestCommandInterest><Validation>", |
| 101 | *keyChain.getCertificate(certName)); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 102 | |
| 103 | //Test a legitimate command |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 104 | shared_ptr<Interest> commandInterest1 = |
| 105 | make_shared<Interest>("/TestCommandInterest/Validation/Command1"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 106 | generator.generateWithIdentity(*commandInterest1, identity); |
| 107 | validator.validate(*commandInterest1, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 108 | bind(&CommandInterestFixture::validated, this, _1), |
| 109 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 110 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(m_validity, true); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 113 | //Test an outdated command |
| 114 | reset(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 115 | shared_ptr<Interest> commandInterest2 = |
| 116 | make_shared<Interest>("/TestCommandInterest/Validation/Command2"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 117 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 118 | timestamp -= time::seconds(5); |
| 119 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 120 | Name commandName = commandInterest2->getName(); |
| 121 | commandName |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 122 | .appendNumber(timestamp.count()) |
| 123 | .appendNumber(random::generateWord64()); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 124 | commandInterest2->setName(commandName); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 126 | keyChain.signByIdentity(*commandInterest2, identity); |
| 127 | validator.validate(*commandInterest2, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 128 | bind(&CommandInterestFixture::validated, this, _1), |
| 129 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 130 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 131 | BOOST_CHECK_EQUAL(m_validity, false); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 133 | //Test an unauthorized command |
| 134 | Name identity2("/TestCommandInterest/Validation2"); |
| 135 | Name certName2; |
| 136 | BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 138 | shared_ptr<Interest> commandInterest3 = |
| 139 | make_shared<Interest>("/TestCommandInterest/Validation/Command3"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 140 | generator.generateWithIdentity(*commandInterest3, identity2); |
| 141 | validator.validate(*commandInterest3, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 142 | bind(&CommandInterestFixture::validated, this, _1), |
| 143 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 145 | BOOST_CHECK_EQUAL(m_validity, false); |
| 146 | |
| 147 | //Test another unauthorized command |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 148 | shared_ptr<Interest> commandInterest4 = |
| 149 | make_shared<Interest>("/TestCommandInterest/Validation2/Command"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 150 | generator.generateWithIdentity(*commandInterest4, identity); |
| 151 | validator.validate(*commandInterest4, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 152 | bind(&CommandInterestFixture::validated, this, _1), |
| 153 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 155 | BOOST_CHECK_EQUAL(m_validity, false); |
| 156 | |
| 157 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 158 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2)); |
| 159 | } |
| 160 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 161 | BOOST_FIXTURE_TEST_CASE(Exemption, CommandInterestFixture) |
| 162 | { |
| 163 | KeyChain keyChain; |
| 164 | Name identity("/TestCommandInterest/AnyKey"); |
| 165 | |
| 166 | Name certName; |
| 167 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 168 | |
| 169 | CommandInterestGenerator generator; |
| 170 | CommandInterestValidator validator; |
| 171 | |
| 172 | validator.addInterestBypassRule("^<TestCommandInterest><Exemption>"); |
| 173 | |
| 174 | //Test a legitimate command |
| 175 | shared_ptr<Interest> commandInterest1 = |
| 176 | make_shared<Interest>("/TestCommandInterest/Exemption/Command1"); |
| 177 | generator.generateWithIdentity(*commandInterest1, identity); |
| 178 | validator.validate(*commandInterest1, |
| 179 | bind(&CommandInterestFixture::validated, this, _1), |
| 180 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
| 181 | |
| 182 | BOOST_CHECK_EQUAL(m_validity, true); |
| 183 | |
| 184 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 185 | } |
| 186 | |
| 187 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 188 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 189 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 190 | |
| 191 | } // namespace ndn |