Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 2 | /* |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, Regents of the University of California. |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndncert, a certificate management system based on NDN. |
| 6 | * |
| 7 | * ndncert is free software: you can redistribute it and/or modify it under the terms |
| 8 | * of the GNU General Public License as published by the Free Software Foundation, either |
| 9 | * version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License along with |
| 16 | * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | * See AUTHORS.md for complete list of ndncert authors and contributors. |
| 19 | */ |
| 20 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 21 | #include "ca-module.hpp" |
Zhiyi Zhang | ad6cf93 | 2017-10-26 16:19:15 -0700 | [diff] [blame] | 22 | #include "database-fixture.hpp" |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 23 | #include "client-module.hpp" |
| 24 | #include "challenge-module.hpp" |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 25 | #include "challenge-module/challenge-pin.hpp" |
| 26 | #include "challenge-module/challenge-email.hpp" |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 27 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 28 | #include <ndn-cxx/util/dummy-client-face.hpp> |
| 29 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 30 | #include <ndn-cxx/security/transform/public-key.hpp> |
| 31 | #include <ndn-cxx/security/verification-helpers.hpp> |
| 32 | |
| 33 | namespace ndn { |
| 34 | namespace ndncert { |
| 35 | namespace tests { |
| 36 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 38 | |
| 39 | BOOST_AUTO_TEST_CASE(Initialization) |
| 40 | { |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 41 | util::DummyClientFace face(io, {true, true}); |
| 42 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 43 | BOOST_CHECK_EQUAL(ca.getCaConf().m_caName, "/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 44 | |
| 45 | auto identity = addIdentity(Name("/ndn/site2")); |
| 46 | auto key = identity.getDefaultKey(); |
| 47 | auto cert = key.getDefaultCertificate(); |
| 48 | ca.getCaStorage()->addCertificate("111", cert); |
| 49 | BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2")); |
| 50 | |
| 51 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 2); |
| 53 | BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 4); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(HandleProbe) |
| 57 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 59 | auto key = identity.getDefaultKey(); |
| 60 | auto cert = key.getDefaultCertificate(); |
| 61 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 62 | util::DummyClientFace face(io, {true, true}); |
| 63 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 64 | ca.setProbeHandler([&] (const JsonSection& probeInfo) { |
| 65 | return "example"; |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 66 | }); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 67 | advanceClocks(time::milliseconds(20), 60); |
| 68 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 69 | Interest interest("/ndn/CA/_PROBE"); |
| 70 | interest.setCanBePrefix(false); |
| 71 | JsonSection paramJson; |
| 72 | paramJson.add(JSON_CLIENT_PROBE_INFO, "zhiyi"); |
| 73 | interest.setApplicationParameters(ClientModule::paramFromJson(paramJson)); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 74 | |
| 75 | int count = 0; |
| 76 | face.onSendData.connect([&] (const Data& response) { |
| 77 | count++; |
| 78 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 79 | auto contentJson = ClientModule::getJsonFromData(response); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 80 | BOOST_CHECK_EQUAL(contentJson.get<std::string>(JSON_CA_NAME), "/ndn/example"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 81 | }); |
| 82 | face.receive(interest); |
| 83 | |
| 84 | advanceClocks(time::milliseconds(20), 60); |
| 85 | BOOST_CHECK_EQUAL(count, 1); |
| 86 | } |
| 87 | |
| 88 | BOOST_AUTO_TEST_CASE(HandleProbeInfo) |
| 89 | { |
| 90 | auto identity = addIdentity(Name("/ndn")); |
| 91 | auto key = identity.getDefaultKey(); |
| 92 | auto cert = key.getDefaultCertificate(); |
| 93 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 94 | util::DummyClientFace face(io, {true, true}); |
| 95 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 96 | ca.setProbeHandler([&] (const JsonSection& probeInfo) { |
| 97 | return "example"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 98 | }); |
| 99 | advanceClocks(time::milliseconds(20), 60); |
| 100 | |
| 101 | Interest interest("/ndn/CA/_PROBE/INFO"); |
| 102 | interest.setCanBePrefix(false); |
| 103 | |
| 104 | int count = 0; |
| 105 | face.onSendData.connect([&] (const Data& response) { |
| 106 | count++; |
| 107 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 108 | auto contentJson = ClientModule::getJsonFromData(response); |
| 109 | auto caItem = ClientConfig::extractCaItem(contentJson); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 110 | BOOST_CHECK_EQUAL(caItem.m_caName, "/ndn"); |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 111 | BOOST_CHECK_EQUAL(caItem.m_probe, ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode()); |
| 113 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 114 | }); |
| 115 | face.receive(interest); |
| 116 | |
| 117 | advanceClocks(time::milliseconds(20), 60); |
| 118 | BOOST_CHECK_EQUAL(count, 1); |
| 119 | } |
| 120 | |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 121 | BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler) |
| 122 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 123 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 124 | auto key = identity.getDefaultKey(); |
| 125 | auto cert = key.getDefaultCertificate(); |
| 126 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 127 | util::DummyClientFace face(io, {true, true}); |
| 128 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 129 | advanceClocks(time::milliseconds(20), 60); |
| 130 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 131 | Interest interest("/ndn/CA/_PROBE"); |
| 132 | interest.setCanBePrefix(false); |
| 133 | JsonSection paramJson; |
| 134 | paramJson.add(JSON_CLIENT_PROBE_INFO, "zhiyi"); |
| 135 | interest.setApplicationParameters(ClientModule::paramFromJson(paramJson)); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 136 | |
| 137 | int count = 0; |
| 138 | face.onSendData.connect([&] (const Data& response) { |
| 139 | count++; |
| 140 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 141 | auto contentJson = ClientModule::getJsonFromData(response); |
| 142 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_NAME) != ""); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 143 | }); |
| 144 | face.receive(interest); |
| 145 | |
| 146 | advanceClocks(time::milliseconds(20), 60); |
| 147 | BOOST_CHECK_EQUAL(count, 1); |
| 148 | } |
| 149 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 150 | BOOST_AUTO_TEST_CASE(HandleNew) |
| 151 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 152 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 153 | auto key = identity.getDefaultKey(); |
| 154 | auto cert = key.getDefaultCertificate(); |
| 155 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 156 | util::DummyClientFace face(io, {true, true}); |
| 157 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 158 | advanceClocks(time::milliseconds(20), 60); |
| 159 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 160 | ClientModule client(m_keyChain); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 161 | ClientCaItem item; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 162 | item.m_caName = Name("/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 163 | item.m_anchor = cert; |
| 164 | client.getClientConf().m_caItems.push_back(item); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 165 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 166 | auto interest = client.generateNewInterest(time::system_clock::now(), |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 167 | time::system_clock::now() + time::days(10), |
| 168 | Name("/ndn/zhiyi")); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 169 | |
| 170 | int count = 0; |
| 171 | face.onSendData.connect([&] (const Data& response) { |
| 172 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 173 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 174 | auto contentJson = ClientModule::getJsonFromData(response); |
| 175 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_ECDH) != ""); |
| 176 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_SALT) != ""); |
Zhiyi Zhang | ff4bcb6 | 2019-09-08 12:57:42 -0700 | [diff] [blame] | 177 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_REQUEST_ID) != ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 178 | auto challengesJson = contentJson.get_child(JSON_CA_CHALLENGES); |
| 179 | BOOST_CHECK(challengesJson.size() != 0); |
| 180 | |
| 181 | client.onNewResponse(response); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame^] | 182 | BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey), |
| 183 | ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 184 | }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 185 | face.receive(*interest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 186 | |
| 187 | advanceClocks(time::milliseconds(20), 60); |
| 188 | BOOST_CHECK_EQUAL(count, 1); |
| 189 | } |
| 190 | |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 191 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1) |
| 192 | { |
| 193 | auto identity = addIdentity(Name("/ndn")); |
| 194 | auto key = identity.getDefaultKey(); |
| 195 | auto cert = key.getDefaultCertificate(); |
| 196 | |
| 197 | util::DummyClientFace face(io, {true, true}); |
| 198 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test"); |
| 199 | advanceClocks(time::milliseconds(20), 60); |
| 200 | |
| 201 | ClientModule client(m_keyChain); |
| 202 | ClientCaItem item; |
| 203 | item.m_caName = Name("/ndn"); |
| 204 | item.m_anchor = cert; |
| 205 | client.getClientConf().m_caItems.push_back(item); |
| 206 | auto current_tp = time::system_clock::now(); |
| 207 | auto interest1 = client.generateNewInterest(current_tp, current_tp - time::hours(1), |
| 208 | Name("/ndn/zhiyi")); |
| 209 | auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(361), |
| 210 | Name("/ndn/zhiyi")); |
| 211 | auto interest3 = client.generateNewInterest(current_tp - time::hours(1), |
| 212 | current_tp + time::hours(2), |
| 213 | Name("/ndn/zhiyi")); |
| 214 | face.onSendData.connect([&] (const Data& response) { |
| 215 | BOOST_CHECK(false); |
| 216 | }); |
| 217 | face.receive(*interest1); |
| 218 | face.receive(*interest2); |
| 219 | face.receive(*interest3); |
| 220 | |
| 221 | advanceClocks(time::milliseconds(20), 60); |
| 222 | } |
| 223 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 224 | BOOST_AUTO_TEST_CASE(HandleNewWithProbeToken) |
| 225 | { |
| 226 | auto identity = addIdentity(Name("/ndn")); |
| 227 | auto key = identity.getDefaultKey(); |
| 228 | auto cert = key.getDefaultCertificate(); |
| 229 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 230 | util::DummyClientFace face(io, {true, true}); |
| 231 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 232 | ca.m_config.m_probe = "email"; |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 233 | advanceClocks(time::milliseconds(20), 60); |
| 234 | |
| 235 | ClientModule client(m_keyChain); |
| 236 | ClientCaItem item; |
| 237 | item.m_caName = Name("/ndn"); |
| 238 | item.m_anchor = cert; |
| 239 | client.getClientConf().m_caItems.push_back(item); |
| 240 | |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 241 | auto data = make_shared<Data>(Name("/ndn/CA/_PROBE/123")); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 242 | m_keyChain.sign(*data, signingByIdentity(ca.m_config.m_caName)); |
| 243 | |
| 244 | auto interest = client.generateNewInterest(time::system_clock::now(), |
| 245 | time::system_clock::now() + time::days(10), |
| 246 | Name("/ndn/zhiyi"), data); |
| 247 | |
| 248 | int count = 0; |
| 249 | face.onSendData.connect([&] (const Data& response) { |
| 250 | count++; |
| 251 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 252 | }); |
| 253 | face.receive(*interest); |
| 254 | |
| 255 | advanceClocks(time::milliseconds(20), 60); |
| 256 | BOOST_CHECK_EQUAL(count, 1); |
| 257 | } |
| 258 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 259 | BOOST_AUTO_TEST_CASE(HandleChallenge) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 260 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 261 | auto identity = addIdentity(Name("/ndn")); |
| 262 | auto key = identity.getDefaultKey(); |
| 263 | auto cert = key.getDefaultCertificate(); |
| 264 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 265 | util::DummyClientFace face(io, {true, true}); |
| 266 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 267 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 268 | |
| 269 | // generate NEW Interest |
| 270 | ClientModule client(m_keyChain); |
| 271 | ClientCaItem item; |
| 272 | item.m_caName = Name("/ndn"); |
| 273 | item.m_anchor = cert; |
| 274 | client.getClientConf().m_caItems.push_back(item); |
| 275 | auto newInterest = client.generateNewInterest(time::system_clock::now(), |
| 276 | time::system_clock::now() + time::days(10), Name("/ndn/zhiyi")); |
| 277 | |
| 278 | // generate CHALLENGE Interest |
| 279 | ChallengePin pinChallenge; |
| 280 | shared_ptr<Interest> challengeInterest = nullptr; |
| 281 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 282 | shared_ptr<Interest> challengeInterest3 = nullptr; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 283 | |
| 284 | int count = 0; |
| 285 | face.onSendData.connect([&] (const Data& response) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 286 | if (Name("/ndn/CA/_NEW").isPrefixOf(response.getName())) { |
| 287 | auto contentJson = ClientModule::getJsonFromData(response); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 288 | client.onNewResponse(response); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 289 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 290 | challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status, |
| 291 | client.m_challengeStatus, |
| 292 | paramJson)); |
| 293 | } |
| 294 | else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 295 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 296 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 297 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 298 | client.onChallengeResponse(response); |
| 299 | BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE); |
| 300 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 301 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 302 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 303 | challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status, |
| 304 | client.m_challengeStatus, |
| 305 | paramJson)); |
| 306 | } |
| 307 | else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
| 308 | count++; |
| 309 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 310 | |
| 311 | client.onChallengeResponse(response); |
| 312 | BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE); |
| 313 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 314 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 315 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
| 316 | auto request = ca.getCertificateRequest(*challengeInterest2); |
| 317 | auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, ""); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 318 | for (auto& i : paramJson) { |
| 319 | if (i.first == ChallengePin::JSON_PIN_CODE) |
| 320 | i.second.put("", secret); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 321 | } |
| 322 | challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestJson(client.m_status, |
| 323 | client.m_challengeStatus, |
| 324 | paramJson)); |
| 325 | } |
| 326 | else if (Name("/ndn/CA/_CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
| 327 | count++; |
| 328 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 329 | |
| 330 | client.onChallengeResponse(response); |
| 331 | BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS); |
| 332 | BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 333 | } |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 334 | }); |
| 335 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 336 | face.receive(*newInterest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 337 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 338 | face.receive(*challengeInterest); |
| 339 | advanceClocks(time::milliseconds(20), 60); |
| 340 | face.receive(*challengeInterest2); |
| 341 | advanceClocks(time::milliseconds(20), 60); |
| 342 | face.receive(*challengeInterest3); |
| 343 | advanceClocks(time::milliseconds(20), 60); |
| 344 | BOOST_CHECK_EQUAL(count, 3); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 345 | } |
| 346 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 347 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
| 348 | |
| 349 | } // namespace tests |
| 350 | } // namespace ndncert |
| 351 | } // namespace ndn |