blob: b8faad01a2813e28268b7966f4dd46df75654505 [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
Yingdi Yuf56c68f2014-04-24 21:50:13 -070026BOOST_AUTO_TEST_CASE(SignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080027{
Yingdi Yuf56c68f2014-04-24 21:50:13 -070028 BOOST_REQUIRE_NO_THROW(KeyChain("sqlite3", "file"));
29 KeyChain keyChain("sqlite3", "file");
Yingdi Yu4270f202014-01-28 14:19:16 -080030
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070031 Name identityName("/TestSignedInterest/SignVerify");
32 identityName.appendVersion();
33
Yingdi Yu17bc3012014-02-10 17:37:12 -080034 Name certificateName;
35 BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080036
Yingdi Yu17bc3012014-02-10 17:37:12 -080037 Interest interest("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu2e57a582014-02-20 23:34:43 -080038 BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039
Yingdi Yu4270f202014-01-28 14:19:16 -080040 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
41
42 Interest interest2;
43 interest2.wireDecode(interestBlock);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044
Yingdi Yu2e57a582014-02-20 23:34:43 -080045 shared_ptr<PublicKey> publicKey;
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070046 BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(
47 keyChain.getDefaultKeyNameForIdentity(identityName)));
Yingdi Yu6ac97982014-01-30 14:49:21 -080048 bool result = Validator::verifySignature(interest2, *publicKey);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049
Yingdi Yu17bc3012014-02-10 17:37:12 -080050 BOOST_CHECK_EQUAL(result, true);
51
Yingdi Yu2e57a582014-02-20 23:34:43 -080052 keyChain.deleteIdentity(identityName);
Yingdi Yu4270f202014-01-28 14:19:16 -080053}
54
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080055class CommandInterestFixture
56{
57public:
58 CommandInterestFixture()
59 : m_validity(false)
60 {}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080062 void
63 validated(const shared_ptr<const Interest>& interest)
64 { m_validity = true; }
65
66 void
Yingdi Yu40587c02014-02-21 16:40:48 -080067 validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo)
68 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069 m_validity = false;
Yingdi Yu40587c02014-02-21 16:40:48 -080070 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080071
72 void
73 reset()
74 { m_validity = false; }
75
76 bool m_validity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077};
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080078
Yingdi Yu0fc447c2014-04-29 19:38:32 -070079BOOST_FIXTURE_TEST_CASE(CommandInterest, CommandInterestFixture)
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080080{
81 KeyChain keyChain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070082 Name identity("/TestCommandInterest/Validation");
83 identity.appendVersion();
84
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080085 Name certName;
86 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
87
88 CommandInterestGenerator generator;
89 CommandInterestValidator validator;
90
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070091 validator.addInterestRule("^<TestCommandInterest><Validation>",
92 *keyChain.getCertificate(certName));
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080093
94 //Test a legitimate command
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070095 shared_ptr<Interest> commandInterest1 =
96 make_shared<Interest>("/TestCommandInterest/Validation/Command1");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080097 generator.generateWithIdentity(*commandInterest1, identity);
98 validator.validate(*commandInterest1,
Yingdi Yuf56c68f2014-04-24 21:50:13 -070099 bind(&CommandInterestFixture::validated, this, _1),
100 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700101
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800102 BOOST_CHECK_EQUAL(m_validity, true);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700103
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800104 //Test an outdated command
105 reset();
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700106 shared_ptr<Interest> commandInterest2 =
107 make_shared<Interest>("/TestCommandInterest/Validation/Command2");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700108 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
109 timestamp -= time::seconds(5);
110
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800111 Name commandName = commandInterest2->getName();
112 commandName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700113 .appendNumber(timestamp.count())
114 .appendNumber(random::generateWord64());
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800115 commandInterest2->setName(commandName);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700116
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800117 keyChain.signByIdentity(*commandInterest2, identity);
118 validator.validate(*commandInterest2,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700119 bind(&CommandInterestFixture::validated, this, _1),
120 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700121
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800122 BOOST_CHECK_EQUAL(m_validity, false);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800124 //Test an unauthorized command
125 Name identity2("/TestCommandInterest/Validation2");
126 Name certName2;
127 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700128
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700129 shared_ptr<Interest> commandInterest3 =
130 make_shared<Interest>("/TestCommandInterest/Validation/Command3");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800131 generator.generateWithIdentity(*commandInterest3, identity2);
132 validator.validate(*commandInterest3,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700133 bind(&CommandInterestFixture::validated, this, _1),
134 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700135
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800136 BOOST_CHECK_EQUAL(m_validity, false);
137
138 //Test another unauthorized command
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -0700139 shared_ptr<Interest> commandInterest4 =
140 make_shared<Interest>("/TestCommandInterest/Validation2/Command");
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800141 generator.generateWithIdentity(*commandInterest4, identity);
142 validator.validate(*commandInterest4,
Yingdi Yuf56c68f2014-04-24 21:50:13 -0700143 bind(&CommandInterestFixture::validated, this, _1),
144 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700145
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800146 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
Yingdi Yu0fc447c2014-04-29 19:38:32 -0700152BOOST_FIXTURE_TEST_CASE(Exemption, CommandInterestFixture)
153{
154 KeyChain keyChain;
155 Name identity("/TestCommandInterest/AnyKey");
156
157 Name certName;
158 BOOST_REQUIRE_NO_THROW(certName = keyChain.createIdentity(identity));
159
160 CommandInterestGenerator generator;
161 CommandInterestValidator validator;
162
163 validator.addInterestBypassRule("^<TestCommandInterest><Exemption>");
164
165 //Test a legitimate command
166 shared_ptr<Interest> commandInterest1 =
167 make_shared<Interest>("/TestCommandInterest/Exemption/Command1");
168 generator.generateWithIdentity(*commandInterest1, identity);
169 validator.validate(*commandInterest1,
170 bind(&CommandInterestFixture::validated, this, _1),
171 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
172
173 BOOST_CHECK_EQUAL(m_validity, true);
174
175 BOOST_CHECK_NO_THROW(keyChain.deleteIdentity(identity));
176}
177
178
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800179
Yingdi Yu4270f202014-01-28 14:19:16 -0800180BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800181
182} // namespace ndn