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) |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 69 | { |
| 70 | } |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 71 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 72 | void |
| 73 | validated(const shared_ptr<const Interest>& interest) |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 74 | { |
| 75 | m_validity = true; |
| 76 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 77 | |
| 78 | void |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 79 | validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo) |
| 80 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 81 | m_validity = false; |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 82 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 83 | |
| 84 | void |
| 85 | reset() |
Alexander Afanasyev | 24b75c8 | 2014-05-31 15:59:31 +0300 | [diff] [blame] | 86 | { |
| 87 | m_validity = false; |
| 88 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 89 | |
| 90 | bool m_validity; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 91 | }; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 92 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 93 | BOOST_FIXTURE_TEST_CASE(CommandInterest, CommandInterestFixture) |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 94 | { |
| 95 | KeyChain keyChain; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 96 | Name identity("/TestCommandInterest/Validation"); |
| 97 | identity.appendVersion(); |
| 98 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 99 | Name certName; |
| 100 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 101 | |
| 102 | CommandInterestGenerator generator; |
| 103 | CommandInterestValidator validator; |
| 104 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 105 | validator.addInterestRule("^<TestCommandInterest><Validation>", |
| 106 | *keyChain.getCertificate(certName)); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 107 | |
| 108 | //Test a legitimate command |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 109 | shared_ptr<Interest> commandInterest1 = |
| 110 | make_shared<Interest>("/TestCommandInterest/Validation/Command1"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 111 | generator.generateWithIdentity(*commandInterest1, identity); |
| 112 | validator.validate(*commandInterest1, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 113 | bind(&CommandInterestFixture::validated, this, _1), |
| 114 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(m_validity, true); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 117 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 118 | //Test an outdated command |
| 119 | reset(); |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 120 | shared_ptr<Interest> commandInterest2 = |
| 121 | make_shared<Interest>("/TestCommandInterest/Validation/Command2"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 122 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 123 | timestamp -= time::seconds(5); |
| 124 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 125 | Name commandName = commandInterest2->getName(); |
| 126 | commandName |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 127 | .appendNumber(timestamp.count()) |
| 128 | .appendNumber(random::generateWord64()); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 129 | commandInterest2->setName(commandName); |
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 | keyChain.signByIdentity(*commandInterest2, identity); |
| 132 | validator.validate(*commandInterest2, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 133 | bind(&CommandInterestFixture::validated, this, _1), |
| 134 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 135 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL(m_validity, false); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 138 | //Test an unauthorized command |
| 139 | Name identity2("/TestCommandInterest/Validation2"); |
| 140 | Name certName2; |
| 141 | BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 142 | |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 143 | shared_ptr<Interest> commandInterest3 = |
| 144 | make_shared<Interest>("/TestCommandInterest/Validation/Command3"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 145 | generator.generateWithIdentity(*commandInterest3, identity2); |
| 146 | validator.validate(*commandInterest3, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 147 | bind(&CommandInterestFixture::validated, this, _1), |
| 148 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 149 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 150 | BOOST_CHECK_EQUAL(m_validity, false); |
| 151 | |
| 152 | //Test another unauthorized command |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 153 | shared_ptr<Interest> commandInterest4 = |
| 154 | make_shared<Interest>("/TestCommandInterest/Validation2/Command"); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 155 | generator.generateWithIdentity(*commandInterest4, identity); |
| 156 | validator.validate(*commandInterest4, |
Yingdi Yu | f56c68f | 2014-04-24 21:50:13 -0700 | [diff] [blame] | 157 | bind(&CommandInterestFixture::validated, this, _1), |
| 158 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 159 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 160 | BOOST_CHECK_EQUAL(m_validity, false); |
| 161 | |
| 162 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 163 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2)); |
| 164 | } |
| 165 | |
Yingdi Yu | 0fc447c | 2014-04-29 19:38:32 -0700 | [diff] [blame] | 166 | BOOST_FIXTURE_TEST_CASE(Exemption, CommandInterestFixture) |
| 167 | { |
| 168 | KeyChain keyChain; |
| 169 | Name identity("/TestCommandInterest/AnyKey"); |
| 170 | |
| 171 | Name certName; |
| 172 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 173 | |
| 174 | CommandInterestGenerator generator; |
| 175 | CommandInterestValidator validator; |
| 176 | |
| 177 | validator.addInterestBypassRule("^<TestCommandInterest><Exemption>"); |
| 178 | |
| 179 | //Test a legitimate command |
| 180 | shared_ptr<Interest> commandInterest1 = |
| 181 | make_shared<Interest>("/TestCommandInterest/Exemption/Command1"); |
| 182 | generator.generateWithIdentity(*commandInterest1, identity); |
| 183 | validator.validate(*commandInterest1, |
| 184 | bind(&CommandInterestFixture::validated, this, _1), |
| 185 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
| 186 | |
| 187 | BOOST_CHECK_EQUAL(m_validity, true); |
| 188 | |
| 189 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 190 | } |
| 191 | |
| 192 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 193 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 194 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 195 | |
| 196 | } // namespace ndn |