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