blob: 4fd92e1731461cc241df617d4a9894ae457ea2cd [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Yingdi Yu4270f202014-01-28 14:19:16 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Yu4270f202014-01-28 14:19:16 -080011 */
12
Yingdi Yu4270f202014-01-28 14:19:16 -080013#include "security/key-chain.hpp"
Yingdi Yu6ac97982014-01-30 14:49:21 -080014#include "security/validator.hpp"
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080015
Yingdi Yuc4f6fd72014-02-26 12:48:44 -080016#include "util/command-interest-generator.hpp"
17#include "util/command-interest-validator.hpp"
Yingdi Yu4270f202014-01-28 14:19:16 -080018
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070019#include "boost-test.hpp"
20
Yingdi Yu4270f202014-01-28 14:19:16 -080021using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080022namespace ndn {
Yingdi Yu4270f202014-01-28 14:19:16 -080023
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070024BOOST_AUTO_TEST_SUITE(SecurityTestSignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080025
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080026BOOST_AUTO_TEST_CASE (SignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080027{
28 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
29
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070030 Name identityName("/TestSignedInterest/SignVerify");
31 identityName.appendVersion();
32
Yingdi Yu17bc3012014-02-10 17:37:12 -080033 Name certificateName;
34 BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080035
Yingdi Yu17bc3012014-02-10 17:37:12 -080036 Interest interest("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu2e57a582014-02-20 23:34:43 -080037 BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038
Yingdi Yu4270f202014-01-28 14:19:16 -080039 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
40
41 Interest interest2;
42 interest2.wireDecode(interestBlock);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070043
Yingdi Yu2e57a582014-02-20 23:34:43 -080044 shared_ptr<PublicKey> publicKey;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070045 BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(
46 keyChain.getDefaultKeyNameForIdentity(identityName)));
Yingdi Yu6ac97982014-01-30 14:49:21 -080047 bool result = Validator::verifySignature(interest2, *publicKey);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048
Yingdi Yu17bc3012014-02-10 17:37:12 -080049 BOOST_CHECK_EQUAL(result, true);
50
Yingdi Yu2e57a582014-02-20 23:34:43 -080051 keyChain.deleteIdentity(identityName);
Yingdi Yu4270f202014-01-28 14:19:16 -080052}
53
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080054class CommandInterestFixture
55{
56public:
57 CommandInterestFixture()
58 : m_validity(false)
59 {}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070060
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080061 void
62 validated(const shared_ptr<const Interest>& interest)
63 { m_validity = true; }
64
65 void
Yingdi Yu40587c02014-02-21 16:40:48 -080066 validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo)
67 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 m_validity = false;
Yingdi Yu40587c02014-02-21 16:40:48 -080069 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080070
71 void
72 reset()
73 { m_validity = false; }
74
75 bool m_validity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076};
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080077
78BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
79{
80 KeyChain keyChain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070081 Name identity("/TestCommandInterest/Validation");
82 identity.appendVersion();
83
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080084 Name certName;
85 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
86
87 CommandInterestGenerator generator;
88 CommandInterestValidator validator;
89
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070090 validator.addInterestRule("^<TestCommandInterest><Validation>",
91 *keyChain.getCertificate(certName));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080092
93 //Test a legitimate command
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070094 shared_ptr<Interest> commandInterest1 =
95 make_shared<Interest>("/TestCommandInterest/Validation/Command1");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080096 generator.generateWithIdentity(*commandInterest1, identity);
97 validator.validate(*commandInterest1,
98 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -080099 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700100
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800101 BOOST_CHECK_EQUAL(m_validity, true);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800103 //Test an outdated command
104 reset();
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700105 shared_ptr<Interest> commandInterest2 =
106 make_shared<Interest>("/TestCommandInterest/Validation/Command2");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700107 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
108 timestamp -= time::seconds(5);
109
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800110 Name commandName = commandInterest2->getName();
111 commandName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112 .appendNumber(timestamp.count())
113 .appendNumber(random::generateWord64());
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800114 commandInterest2->setName(commandName);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700115
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800116 keyChain.signByIdentity(*commandInterest2, identity);
117 validator.validate(*commandInterest2,
118 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800119 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700120
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800121 BOOST_CHECK_EQUAL(m_validity, false);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700122
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800123 //Test an unauthorized command
124 Name identity2("/TestCommandInterest/Validation2");
125 Name certName2;
126 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700127
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700128 shared_ptr<Interest> commandInterest3 =
129 make_shared<Interest>("/TestCommandInterest/Validation/Command3");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800130 generator.generateWithIdentity(*commandInterest3, identity2);
131 validator.validate(*commandInterest3,
132 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800133 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700134
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800135 BOOST_CHECK_EQUAL(m_validity, false);
136
137 //Test another unauthorized command
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700138 shared_ptr<Interest> commandInterest4 =
139 make_shared<Interest>("/TestCommandInterest/Validation2/Command");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800140 generator.generateWithIdentity(*commandInterest4, identity);
141 validator.validate(*commandInterest4,
142 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800143 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700144
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800145 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 Yu4270f202014-01-28 14:19:16 -0800152BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800153
154} // namespace ndn