blob: 4b7a63eba8848d9cf0a9751653d13625618433a2 [file] [log] [blame]
Yingdi Yu4270f202014-01-28 14:19:16 -08001/**
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 Yu4270f202014-01-28 14:19:16 -08007#include "security/key-chain.hpp"
Yingdi Yu6ac97982014-01-30 14:49:21 -08008#include "security/validator.hpp"
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -08009
Yingdi Yuc4f6fd72014-02-26 12:48:44 -080010#include "util/command-interest-generator.hpp"
11#include "util/command-interest-validator.hpp"
Yingdi Yu4270f202014-01-28 14:19:16 -080012
Alexander Afanasyevb1db7c62014-04-03 14:57:25 -070013#include "boost-test.hpp"
14
Yingdi Yu4270f202014-01-28 14:19:16 -080015using namespace std;
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -080016namespace ndn {
Yingdi Yu4270f202014-01-28 14:19:16 -080017
Alexander Afanasyevd1b5c412014-03-27 15:03:51 -070018BOOST_AUTO_TEST_SUITE(SecurityTestSignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080019
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080020BOOST_AUTO_TEST_CASE (SignedInterest)
Yingdi Yu4270f202014-01-28 14:19:16 -080021{
22 KeyChainImpl<SecPublicInfoSqlite3, SecTpmFile> keyChain;
23
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070024 Name identityName("/TestSignedInterest/SignVerify");
25 identityName.appendVersion();
26
Yingdi Yu17bc3012014-02-10 17:37:12 -080027 Name certificateName;
28 BOOST_REQUIRE_NO_THROW(certificateName = keyChain.createIdentity(identityName));
Yingdi Yu4270f202014-01-28 14:19:16 -080029
Yingdi Yu17bc3012014-02-10 17:37:12 -080030 Interest interest("/TestSignedInterest/SignVerify/Interest1");
Yingdi Yu2e57a582014-02-20 23:34:43 -080031 BOOST_CHECK_NO_THROW(keyChain.signByIdentity(interest, identityName));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070032
Yingdi Yu4270f202014-01-28 14:19:16 -080033 Block interestBlock(interest.wireEncode().wire(), interest.wireEncode().size());
34
35 Interest interest2;
36 interest2.wireDecode(interestBlock);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070037
Yingdi Yu2e57a582014-02-20 23:34:43 -080038 shared_ptr<PublicKey> publicKey;
39 BOOST_REQUIRE_NO_THROW(publicKey = keyChain.getPublicKeyFromTpm(keyChain.getDefaultKeyNameForIdentity(identityName)));
Yingdi Yu6ac97982014-01-30 14:49:21 -080040 bool result = Validator::verifySignature(interest2, *publicKey);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070041
Yingdi Yu17bc3012014-02-10 17:37:12 -080042 BOOST_CHECK_EQUAL(result, true);
43
Yingdi Yu2e57a582014-02-20 23:34:43 -080044 keyChain.deleteIdentity(identityName);
Yingdi Yu4270f202014-01-28 14:19:16 -080045}
46
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080047class CommandInterestFixture
48{
49public:
50 CommandInterestFixture()
51 : m_validity(false)
52 {}
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080054 void
55 validated(const shared_ptr<const Interest>& interest)
56 { m_validity = true; }
57
58 void
Yingdi Yu40587c02014-02-21 16:40:48 -080059 validationFailed(const shared_ptr<const Interest>& interest, const string& failureInfo)
60 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 m_validity = false;
Yingdi Yu40587c02014-02-21 16:40:48 -080062 }
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080063
64 void
65 reset()
66 { m_validity = false; }
67
68 bool m_validity;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070069};
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080070
71BOOST_FIXTURE_TEST_CASE (CommandInterest, CommandInterestFixture)
72{
73 KeyChain keyChain;
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074 Name identity("/TestCommandInterest/Validation");
75 identity.appendVersion();
76
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080077 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 Yu40587c02014-02-21 16:40:48 -080090 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080092 BOOST_CHECK_EQUAL(m_validity, true);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070093
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -080094 //Test an outdated command
95 reset();
96 shared_ptr<Interest> commandInterest2 = make_shared<Interest>("/TestCommandInterest/Validation/Command2");
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070097 time::milliseconds timestamp = time::toUnixTimestamp(time::system_clock::now());
98 timestamp -= time::seconds(5);
99
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800100 Name commandName = commandInterest2->getName();
101 commandName
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700102 .appendNumber(timestamp.count())
103 .appendNumber(random::generateWord64());
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800104 commandInterest2->setName(commandName);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700105
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800106 keyChain.signByIdentity(*commandInterest2, identity);
107 validator.validate(*commandInterest2,
108 bind(&CommandInterestFixture::validated, this, _1),
Yingdi Yu40587c02014-02-21 16:40:48 -0800109 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700110
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800111 BOOST_CHECK_EQUAL(m_validity, false);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700112
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800113 //Test an unauthorized command
114 Name identity2("/TestCommandInterest/Validation2");
115 Name certName2;
116 BOOST_REQUIRE_NO_THROW(certName2 = keyChain.createIdentity(identity2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700117
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800118 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 Yu40587c02014-02-21 16:40:48 -0800122 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700123
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800124 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 Yu40587c02014-02-21 16:40:48 -0800131 bind(&CommandInterestFixture::validationFailed, this, _1, _2));
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -0700132
Alexander Afanasyev9cbf70a2014-02-17 18:07:51 -0800133 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 Yu4270f202014-01-28 14:19:16 -0800140BOOST_AUTO_TEST_SUITE_END()
Alexander Afanasyev0abb2da2014-01-30 18:07:57 -0800141
142} // namespace ndn