Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2013 Regents of the University of California. |
| 3 | * @author: Yingdi Yu <yingdi0@cs.ucla.edu> |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 7 | #include "security/key-chain.hpp" |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 8 | #include "security/validator.hpp" |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 9 | |
Yingdi Yu | c4f6fd7 | 2014-02-26 12:48:44 -0800 | [diff] [blame] | 10 | #include "util/command-interest-generator.hpp" |
| 11 | #include "util/command-interest-validator.hpp" |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 12 | |
Alexander Afanasyev | b1db7c6 | 2014-04-03 14:57:25 -0700 | [diff] [blame] | 13 | #include "boost-test.hpp" |
| 14 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 15 | using namespace std; |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 16 | namespace ndn { |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | d1b5c41 | 2014-03-27 15:03:51 -0700 | [diff] [blame^] | 18 | BOOST_AUTO_TEST_SUITE(SecurityTestSignedInterest) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 19 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 20 | BOOST_AUTO_TEST_CASE (SignedInterest) |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 21 | { |
| 22 | KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain; |
| 23 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 24 | Name identityName("/TestSignedInterest/SignVerify"); |
| 25 | identityName.appendVersion(); |
| 26 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 27 | Name certificateName; |
| 28 | BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName)); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 29 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 30 | Interest interest("/TestSignedInterest/SignVerify/Interest1"); |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 31 | BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 32 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 33 | Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size()); |
| 34 | |
| 35 | Interest interest2; |
| 36 | interest2.wireDecode(interestBlock); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 37 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 38 | shared_ptr<PublicKey> publicKey; |
| 39 | BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName))); |
Yingdi Yu | 6ac9798 | 2014-01-30 14:49:21 -0800 | [diff] [blame] | 40 | bool result = Validator::verifySignature(interest2, *publicKey); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 41 | |
Yingdi Yu | 17bc301 | 2014-02-10 17:37:12 -0800 | [diff] [blame] | 42 | BOOST_CHECK_EQUAL(result, true); |
| 43 | |
Yingdi Yu | 2e57a58 | 2014-02-20 23:34:43 -0800 | [diff] [blame] | 44 | keyChain.deleteIdentity(identityName); |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 45 | } |
| 46 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 47 | class CommandInterestFixture |
| 48 | { |
| 49 | public: |
| 50 | CommandInterestFixture() |
| 51 | : m_validity(false) |
| 52 | {} |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 54 | void |
| 55 | validated(const shared_ptr<const Interest>& interest) |
| 56 | { m_validity = true; } |
| 57 | |
| 58 | void |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 59 | validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo) |
| 60 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 61 | m_validity = false; |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 62 | } |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 63 | |
| 64 | void |
| 65 | reset() |
| 66 | { m_validity = false; } |
| 67 | |
| 68 | bool m_validity; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 69 | }; |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 70 | |
| 71 | BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture) |
| 72 | { |
| 73 | KeyChain keyChain; |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | Name identity("/TestCommandInterest/Validation"); |
| 75 | identity.appendVersion(); |
| 76 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 77 | Name certName; |
| 78 | BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity)); |
| 79 | |
| 80 | CommandInterestGenerator generator; |
| 81 | CommandInterestValidator validator; |
| 82 | |
| 83 | validator.addInterestRule("^<TestCommandInterest><Validation>", *keyChain.getCertificate(certName)); |
| 84 | |
| 85 | //Test a legitimate command |
| 86 | shared_ptr<Interest> commandInterest1 = make_shared<Interest>("/TestCommandInterest/Validation/Command1"); |
| 87 | generator.generateWithIdentity(*commandInterest1, identity); |
| 88 | validator.validate(*commandInterest1, |
| 89 | bind(&CommandInterestFixture::validated, this, _1), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 90 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
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 | BOOST_CHECK_EQUAL(m_validity, true); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 94 | //Test an outdated command |
| 95 | reset(); |
| 96 | shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2"); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 97 | time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now()); |
| 98 | timestamp -= time::seconds(5); |
| 99 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 100 | Name commandName = commandInterest2->getName(); |
| 101 | commandName |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 102 | .appendNumber(timestamp.count()) |
| 103 | .appendNumber(random::generateWord64()); |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 104 | commandInterest2->setName(commandName); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 105 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 106 | keyChain.signByIdentity(*commandInterest2, identity); |
| 107 | validator.validate(*commandInterest2, |
| 108 | bind(&CommandInterestFixture::validated, this, _1), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 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, false); |
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 unauthorized command |
| 114 | Name identity2("/TestCommandInterest/Validation2"); |
| 115 | Name certName2; |
| 116 | BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2)); |
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 | shared_ptr<Interest> commandInterest3 = make_shared<Interest>("/TestCommandInterest/Validation/Command3"); |
| 119 | generator.generateWithIdentity(*commandInterest3, identity2); |
| 120 | validator.validate(*commandInterest3, |
| 121 | bind(&CommandInterestFixture::validated, this, _1), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 122 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 123 | |
Alexander Afanasyev | 9cbf70a | 2014-02-17 18:07:51 -0800 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(m_validity, false); |
| 125 | |
| 126 | //Test another unauthorized command |
| 127 | shared_ptr<Interest> commandInterest4 = make_shared<Interest>("/TestCommandInterest/Validation2/Command"); |
| 128 | generator.generateWithIdentity(*commandInterest4, identity); |
| 129 | validator.validate(*commandInterest4, |
| 130 | bind(&CommandInterestFixture::validated, this, _1), |
Yingdi Yu | 40587c0 | 2014-02-21 16:40:48 -0800 | [diff] [blame] | 131 | bind(&CommandInterestFixture::validationFailed, this, _1, _2)); |
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 | BOOST_CHECK_EQUAL(m_validity, false); |
| 134 | |
| 135 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity)); |
| 136 | BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity2)); |
| 137 | } |
| 138 | |
| 139 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 140 | BOOST_AUTO_TEST_SUITE_END() |
Alexander Afanasyev | 0abb2da | 2014-01-30 18:07:57 -0800 | [diff] [blame] | 141 | |
| 142 | } // namespace ndn |