Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 2 | /* |
Yumin Xia | acd2133 | 2016-11-28 22:54:48 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California. |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 4 | * |
| 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 | |
Yumin Xia | 99c821a | 2017-04-07 11:01:08 -0700 | [diff] [blame^] | 20 | #include "validator/validator.hpp" |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 21 | |
| 22 | #include "test-common.hpp" |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 23 | #include "util/cert-helper.hpp" |
| 24 | |
| 25 | #include <ndn-cxx/util/io.hpp> |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndns { |
| 29 | namespace tests { |
Alexander Afanasyev | c7c9900 | 2015-10-09 17:27:30 -0700 | [diff] [blame] | 30 | |
| 31 | NDNS_LOG_INIT("ValidatorTest") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(Validator) |
| 34 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 35 | class Fixture : public IdentityManagementFixture |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 36 | { |
| 37 | public: |
| 38 | Fixture() |
| 39 | : m_testId1("/test02") |
| 40 | , m_testId2("/test02/ndn") |
| 41 | , m_testId3("/test02/ndn/edu") |
| 42 | , m_randomId("/test03") |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 43 | , m_face(m_keyChain, {false, true}) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 44 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 45 | m_randomDsk = createRoot(Name(m_randomId).append("NDNS")); // generate a root cert |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 46 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 47 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 50 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 51 | m_face.onSendInterest.connect(bind(&Fixture::respondInterest, this, _1)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | ~Fixture() |
| 55 | { |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 56 | m_face.getIoService().stop(); |
| 57 | m_face.shutdown(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 60 | const Key |
| 61 | createIdentity(const Name& id, const Key& parentKey) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 62 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 63 | Identity identity = addIdentity(id); |
| 64 | Key defaultKey = identity.getDefaultKey(); |
| 65 | m_keyChain.deleteKey(identity, defaultKey); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 66 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 67 | 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 Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 85 | const Key |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 86 | createRoot(const Name& root) |
| 87 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 88 | Identity rootIdentity = addIdentity(root); |
| 89 | auto cert = rootIdentity.getDefaultKey().getDefaultCertificate(); |
| 90 | ndn::io::save(cert, TEST_CONFIG_PATH "/anchors/root.cert"); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 91 | NDNS_LOG_TRACE("save root cert "<< m_rootCert << |
| 92 | " to: " << TEST_CONFIG_PATH "/anchors/root.cert"); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 93 | return rootIdentity.getDefaultKey(); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 96 | void |
Shock Jiang | 0e2aee0 | 2014-11-17 11:19:36 -0800 | [diff] [blame] | 97 | respondInterest(const Interest& interest) |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 98 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 99 | 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 Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 105 | m_face.getIoService().post([this, cert] { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 106 | m_face.receive(cert); |
Alexander Afanasyev | a81266c | 2015-05-10 20:18:57 -0700 | [diff] [blame] | 107 | }); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public: |
| 111 | Name m_testId1; |
| 112 | Name m_testId2; |
| 113 | Name m_testId3; |
| 114 | Name m_randomId; |
| 115 | |
| 116 | Name m_rootCert; |
| 117 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 118 | Key m_dsk1; |
| 119 | Key m_dsk2; |
| 120 | Key m_dsk3; |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 121 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 122 | Key m_randomDsk; |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 123 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 124 | ndn::util::DummyClientFace m_face; |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | |
| 128 | BOOST_FIXTURE_TEST_CASE(Basic, Fixture) |
| 129 | { |
| 130 | // validator must be created after root key is saved to the target |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 131 | auto validator = NdnsValidatorBuilder::create(m_face, TEST_CONFIG_PATH "/" "validator.conf"); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 132 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 133 | // case1: record of testId3, signed by its dsk, should be successful validated. |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 134 | Name dataName; |
| 135 | dataName |
| 136 | .append(m_testId3) |
| 137 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 138 | .append("rrLabel") |
| 139 | .append("rrType") |
| 140 | .appendVersion(); |
| 141 | shared_ptr<Data> data = make_shared<Data>(dataName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 142 | m_keyChain.sign(*data, signingByKey(m_dsk3)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 143 | |
| 144 | bool hasValidated = false; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 145 | validator->validate(*data, |
| 146 | [&] (const Data& data) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 147 | hasValidated = true; |
| 148 | BOOST_CHECK(true); |
| 149 | }, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 150 | [&] (const Data& data, const security::v2::ValidationError& str) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 151 | hasValidated = true; |
| 152 | BOOST_CHECK(false); |
| 153 | }); |
| 154 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 155 | m_face.processEvents(time::milliseconds(-1)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 156 | |
| 157 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 158 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 159 | // case2: signing testId2's data by testId3's key, which should failed in validation |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 160 | dataName = Name(); |
| 161 | dataName |
| 162 | .append(m_testId2) |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 163 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 164 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 165 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 166 | .appendVersion(); |
| 167 | data = make_shared<Data>(dataName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 168 | m_keyChain.sign(*data, signingByKey(m_dsk3)); // key's owner's name is longer than data owner's |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 169 | |
| 170 | hasValidated = false; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 171 | validator->validate(*data, |
| 172 | [&] (const Data& data) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 173 | hasValidated = true; |
| 174 | BOOST_CHECK(false); |
| 175 | }, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 176 | [&] (const Data& data, const security::v2::ValidationError& str) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 177 | hasValidated = true; |
| 178 | BOOST_CHECK(true); |
| 179 | }); |
| 180 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 181 | m_face.processEvents(time::milliseconds(-1)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 182 | // cannot pass verification due to key's owner's name is longer than data owner's |
| 183 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 184 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 185 | // case4: totally wrong key to sign |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 186 | dataName = Name(); |
| 187 | dataName |
| 188 | .append(m_testId2) |
Yumin Xia | 918343d | 2017-03-17 19:04:55 -0700 | [diff] [blame] | 189 | .append("NDNS") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 190 | .append("rrLabel") |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 191 | .append("CERT") |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 192 | .appendVersion(); |
| 193 | data = make_shared<Data>(dataName); |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 194 | m_keyChain.sign(*data, signingByKey(m_randomDsk)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 195 | |
| 196 | hasValidated = false; |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 197 | validator->validate(*data, |
| 198 | [&] (const Data& data) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 199 | hasValidated = true; |
| 200 | BOOST_CHECK(false); |
| 201 | }, |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 202 | [&] (const Data& data, const security::v2::ValidationError& str) { |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 203 | hasValidated = true; |
| 204 | BOOST_CHECK(true); |
| 205 | }); |
| 206 | |
Junxiao Shi | bbf7ac8 | 2016-07-14 14:45:15 +0000 | [diff] [blame] | 207 | m_face.processEvents(time::milliseconds(-1)); |
Shock Jiang | 0b165f4 | 2014-10-24 09:08:09 -0700 | [diff] [blame] | 208 | // cannot pass due to a totally mismatched key |
| 209 | BOOST_CHECK_EQUAL(hasValidated, true); |
| 210 | } |
| 211 | |
| 212 | BOOST_AUTO_TEST_SUITE_END() |
| 213 | |
| 214 | } // namespace tests |
| 215 | } // namespace ndns |
| 216 | } // namespace ndn |