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