blob: c6428755f93722e109d0d326e5903364dcbca000 [file] [log] [blame]
Shock Jiang0b165f42014-10-24 09:08:09 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Yumin Xiaacd21332016-11-28 22:54:48 -08003 * Copyright (c) 2014-2017, Regents of the University of California.
Shock Jiang0b165f42014-10-24 09:08:09 -07004 *
5 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "validator.hpp"
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080021
22#include "test-common.hpp"
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include "util/cert-helper.hpp"
24
25#include <ndn-cxx/util/io.hpp>
Shock Jiang0b165f42014-10-24 09:08:09 -070026
27namespace ndn {
28namespace ndns {
29namespace tests {
Alexander Afanasyevc7c99002015-10-09 17:27:30 -070030
31NDNS_LOG_INIT("ValidatorTest")
Shock Jiang0b165f42014-10-24 09:08:09 -070032
33BOOST_AUTO_TEST_SUITE(Validator)
34
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080035class Fixture : public IdentityManagementFixture
Shock Jiang0b165f42014-10-24 09:08:09 -070036{
37public:
38 Fixture()
39 : m_testId1("/test02")
40 , m_testId2("/test02/ndn")
41 , m_testId3("/test02/ndn/edu")
42 , m_randomId("/test03")
Junxiao Shibbf7ac82016-07-14 14:45:15 +000043 , m_face(m_keyChain, {false, true})
Shock Jiang0b165f42014-10-24 09:08:09 -070044 {
Yumin Xia2c509c22017-02-09 14:37:36 -080045 m_randomDsk = createRoot(Name(m_randomId).append("NDNS")); // generate a root cert
Shock Jiang0b165f42014-10-24 09:08:09 -070046
Yumin Xia2c509c22017-02-09 14:37:36 -080047 m_dsk1 = createRoot(Name(m_testId1).append("NDNS")); // replace to root cert
48 m_dsk2 = createIdentity(Name(m_testId2).append("NDNS"), m_dsk1);
49 m_dsk3 = createIdentity(Name(m_testId3).append("NDNS"), m_dsk2);
Shock Jiang0b165f42014-10-24 09:08:09 -070050
Junxiao Shibbf7ac82016-07-14 14:45:15 +000051 m_face.onSendInterest.connect(bind(&Fixture::respondInterest, this, _1));
Shock Jiang0b165f42014-10-24 09:08:09 -070052 }
53
54 ~Fixture()
55 {
Junxiao Shibbf7ac82016-07-14 14:45:15 +000056 m_face.getIoService().stop();
57 m_face.shutdown();
Shock Jiang0b165f42014-10-24 09:08:09 -070058 }
59
Yumin Xia2c509c22017-02-09 14:37:36 -080060 const Key
61 createIdentity(const Name& id, const Key& parentKey)
Shock Jiang0b165f42014-10-24 09:08:09 -070062 {
Yumin Xia2c509c22017-02-09 14:37:36 -080063 Identity identity = addIdentity(id);
64 Key defaultKey = identity.getDefaultKey();
65 m_keyChain.deleteKey(identity, defaultKey);
Shock Jiang0b165f42014-10-24 09:08:09 -070066
Yumin Xia2c509c22017-02-09 14:37:36 -080067 Key ksk = m_keyChain.createKey(identity);
68 Name defaultKskCert = ksk.getDefaultCertificate().getName();
69 m_keyChain.deleteCertificate(ksk, defaultKskCert);
70
71 Key dsk = m_keyChain.createKey(identity);
72 Name defaultDskCert = dsk.getDefaultCertificate().getName();
73 m_keyChain.deleteCertificate(dsk, defaultDskCert);
74
75 auto kskCert = CertHelper::createCertificate(m_keyChain, ksk, parentKey, "CERT", time::days(100));
76 auto dskCert = CertHelper::createCertificate(m_keyChain, dsk, ksk, "CERT", time::days(100));
77
78 m_keyChain.addCertificate(ksk, kskCert);
79 m_keyChain.addCertificate(dsk, dskCert);
80
81 m_keyChain.setDefaultKey(identity, dsk);
82 return dsk;
Shock Jiang0b165f42014-10-24 09:08:09 -070083 }
84
Yumin Xia2c509c22017-02-09 14:37:36 -080085 const Key
Shock Jiang0b165f42014-10-24 09:08:09 -070086 createRoot(const Name& root)
87 {
Yumin Xia2c509c22017-02-09 14:37:36 -080088 Identity rootIdentity = addIdentity(root);
89 auto cert = rootIdentity.getDefaultKey().getDefaultCertificate();
90 ndn::io::save(cert, TEST_CONFIG_PATH "/anchors/root.cert");
Shock Jiang0b165f42014-10-24 09:08:09 -070091 NDNS_LOG_TRACE("save root cert "<< m_rootCert <<
92 " to: " << TEST_CONFIG_PATH "/anchors/root.cert");
Yumin Xia2c509c22017-02-09 14:37:36 -080093 return rootIdentity.getDefaultKey();
Shock Jiang0b165f42014-10-24 09:08:09 -070094 }
95
Shock Jiang0b165f42014-10-24 09:08:09 -070096 void
Shock Jiang0e2aee02014-11-17 11:19:36 -080097 respondInterest(const Interest& interest)
Shock Jiang0b165f42014-10-24 09:08:09 -070098 {
Yumin Xia2c509c22017-02-09 14:37:36 -080099 Name keyName = interest.getName();
100 Name identityName = keyName.getPrefix(-2);
101 NDNS_LOG_TRACE("validator needs cert of KEY: " << keyName);
102 auto cert = m_keyChain.getPib().getIdentity(identityName)
103 .getKey(keyName)
104 .getDefaultCertificate();
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000105 m_face.getIoService().post([this, cert] {
Yumin Xia2c509c22017-02-09 14:37:36 -0800106 m_face.receive(cert);
Alexander Afanasyeva81266c2015-05-10 20:18:57 -0700107 });
Shock Jiang0b165f42014-10-24 09:08:09 -0700108 }
109
110public:
111 Name m_testId1;
112 Name m_testId2;
113 Name m_testId3;
114 Name m_randomId;
115
116 Name m_rootCert;
117
Yumin Xia2c509c22017-02-09 14:37:36 -0800118 Key m_dsk1;
119 Key m_dsk2;
120 Key m_dsk3;
Shock Jiang0b165f42014-10-24 09:08:09 -0700121
Yumin Xia2c509c22017-02-09 14:37:36 -0800122 Key m_randomDsk;
Shock Jiang0b165f42014-10-24 09:08:09 -0700123
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000124 ndn::util::DummyClientFace m_face;
Shock Jiang0b165f42014-10-24 09:08:09 -0700125};
126
127
128BOOST_FIXTURE_TEST_CASE(Basic, Fixture)
129{
130 // validator must be created after root key is saved to the target
Yumin Xia2c509c22017-02-09 14:37:36 -0800131 auto validator = NdnsValidatorBuilder::create(m_face, TEST_CONFIG_PATH "/" "validator.conf");
Shock Jiang0b165f42014-10-24 09:08:09 -0700132
Yumin Xia2c509c22017-02-09 14:37:36 -0800133 // case1: record of testId3, signed by its dsk, should be successful validated.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800134 Name dataName;
135 dataName
136 .append(m_testId3)
137 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700138 .append("rrLabel")
139 .append("rrType")
140 .appendVersion();
141 shared_ptr<Data> data = make_shared<Data>(dataName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800142 m_keyChain.sign(*data, signingByKey(m_dsk3));
Shock Jiang0b165f42014-10-24 09:08:09 -0700143
144 bool hasValidated = false;
Yumin Xia2c509c22017-02-09 14:37:36 -0800145 validator->validate(*data,
146 [&] (const Data& data) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700147 hasValidated = true;
148 BOOST_CHECK(true);
149 },
Yumin Xia2c509c22017-02-09 14:37:36 -0800150 [&] (const Data& data, const security::v2::ValidationError& str) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700151 hasValidated = true;
152 BOOST_CHECK(false);
153 });
154
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000155 m_face.processEvents(time::milliseconds(-1));
Shock Jiang0b165f42014-10-24 09:08:09 -0700156
157 BOOST_CHECK_EQUAL(hasValidated, true);
158
Yumin Xia2c509c22017-02-09 14:37:36 -0800159 // case2: signing testId2's data by testId3's key, which should failed in validation
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800160 dataName = Name();
161 dataName
162 .append(m_testId2)
Yumin Xia2c509c22017-02-09 14:37:36 -0800163 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700164 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800165 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700166 .appendVersion();
167 data = make_shared<Data>(dataName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800168 m_keyChain.sign(*data, signingByKey(m_dsk3)); // key's owner's name is longer than data owner's
Shock Jiang0b165f42014-10-24 09:08:09 -0700169
170 hasValidated = false;
Yumin Xia2c509c22017-02-09 14:37:36 -0800171 validator->validate(*data,
172 [&] (const Data& data) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700173 hasValidated = true;
174 BOOST_CHECK(false);
175 },
Yumin Xia2c509c22017-02-09 14:37:36 -0800176 [&] (const Data& data, const security::v2::ValidationError& str) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700177 hasValidated = true;
178 BOOST_CHECK(true);
179 });
180
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000181 m_face.processEvents(time::milliseconds(-1));
Shock Jiang0b165f42014-10-24 09:08:09 -0700182 // cannot pass verification due to key's owner's name is longer than data owner's
183 BOOST_CHECK_EQUAL(hasValidated, true);
184
Yumin Xia2c509c22017-02-09 14:37:36 -0800185 // case4: totally wrong key to sign
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800186 dataName = Name();
187 dataName
188 .append(m_testId2)
Yumin Xia918343d2017-03-17 19:04:55 -0700189 .append("NDNS")
Shock Jiang0b165f42014-10-24 09:08:09 -0700190 .append("rrLabel")
Yumin Xia2c509c22017-02-09 14:37:36 -0800191 .append("CERT")
Shock Jiang0b165f42014-10-24 09:08:09 -0700192 .appendVersion();
193 data = make_shared<Data>(dataName);
Yumin Xia2c509c22017-02-09 14:37:36 -0800194 m_keyChain.sign(*data, signingByKey(m_randomDsk));
Shock Jiang0b165f42014-10-24 09:08:09 -0700195
196 hasValidated = false;
Yumin Xia2c509c22017-02-09 14:37:36 -0800197 validator->validate(*data,
198 [&] (const Data& data) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700199 hasValidated = true;
200 BOOST_CHECK(false);
201 },
Yumin Xia2c509c22017-02-09 14:37:36 -0800202 [&] (const Data& data, const security::v2::ValidationError& str) {
Shock Jiang0b165f42014-10-24 09:08:09 -0700203 hasValidated = true;
204 BOOST_CHECK(true);
205 });
206
Junxiao Shibbf7ac82016-07-14 14:45:15 +0000207 m_face.processEvents(time::milliseconds(-1));
Shock Jiang0b165f42014-10-24 09:08:09 -0700208 // cannot pass due to a totally mismatched key
209 BOOST_CHECK_EQUAL(hasValidated, true);
210}
211
212BOOST_AUTO_TEST_SUITE_END()
213
214} // namespace tests
215} // namespace ndns
216} // namespace ndn