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 | /* |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, 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"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 43 | BOOST_CHECK_EQUAL(ca.getCaConf().m_caPrefix, "/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); |
swa770 | cf1d8f7 | 2020-04-21 23:12:39 -0700 | [diff] [blame] | 53 | BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 4); // onInfo, onProbe, onNew, onChallenge |
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"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 64 | ca.setProbeHandler([&] (const Block& probeInfo) { |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 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 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 69 | Interest interest("/ndn/CA/PROBE"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 70 | interest.setCanBePrefix(false); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 71 | |
| 72 | Block paramTLV = makeEmptyBlock(tlv::ApplicationParameters); |
| 73 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, JSON_CLIENT_PROBE_INFO)); |
| 74 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi")); |
| 75 | paramTLV.encode(); |
| 76 | interest.setApplicationParameters(paramTLV); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 77 | |
| 78 | int count = 0; |
| 79 | face.onSendData.connect([&] (const Data& response) { |
| 80 | count++; |
| 81 | BOOST_CHECK(security::verifySignature(response, cert)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 82 | Block contentBlock = response.getContent(); |
| 83 | contentBlock.parse(); |
| 84 | BOOST_CHECK_EQUAL(readString(contentBlock.get(tlv_ca_prefix)), "/ndn/example"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 85 | }); |
| 86 | face.receive(interest); |
| 87 | |
| 88 | advanceClocks(time::milliseconds(20), 60); |
| 89 | BOOST_CHECK_EQUAL(count, 1); |
| 90 | } |
| 91 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 92 | BOOST_AUTO_TEST_CASE(HandleInfo) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 93 | { |
| 94 | auto identity = addIdentity(Name("/ndn")); |
| 95 | auto key = identity.getDefaultKey(); |
| 96 | auto cert = key.getDefaultCertificate(); |
| 97 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 98 | util::DummyClientFace face(io, {true, true}); |
| 99 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 100 | ca.setProbeHandler([&] (const Block& probeInfo) { |
Yufeng Zhang | 424d036 | 2019-06-12 16:48:27 -0700 | [diff] [blame] | 101 | return "example"; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 102 | }); |
| 103 | advanceClocks(time::milliseconds(20), 60); |
| 104 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 105 | Interest interest("/ndn/CA/INFO"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 106 | interest.setCanBePrefix(false); |
| 107 | |
| 108 | int count = 0; |
| 109 | face.onSendData.connect([&] (const Data& response) { |
| 110 | count++; |
| 111 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 112 | auto contentJson = ClientModule::getJsonFromData(response); |
| 113 | auto caItem = ClientConfig::extractCaItem(contentJson); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(caItem.m_caName, "/ndn"); |
Zhiyi Zhang | a1fc623 | 2019-06-13 14:56:59 -0700 | [diff] [blame] | 115 | BOOST_CHECK_EQUAL(caItem.m_probe, ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 116 | BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode()); |
| 117 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 118 | }); |
| 119 | face.receive(interest); |
| 120 | |
| 121 | advanceClocks(time::milliseconds(20), 60); |
| 122 | BOOST_CHECK_EQUAL(count, 1); |
| 123 | } |
| 124 | |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 125 | BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler) |
| 126 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 127 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 128 | auto key = identity.getDefaultKey(); |
| 129 | auto cert = key.getDefaultCertificate(); |
| 130 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 131 | util::DummyClientFace face(io, {true, true}); |
| 132 | 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] | 133 | advanceClocks(time::milliseconds(20), 60); |
| 134 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 135 | Interest interest("/ndn/CA/PROBE"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 136 | interest.setCanBePrefix(false); |
| 137 | JsonSection paramJson; |
| 138 | paramJson.add(JSON_CLIENT_PROBE_INFO, "zhiyi"); |
| 139 | interest.setApplicationParameters(ClientModule::paramFromJson(paramJson)); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 140 | |
| 141 | int count = 0; |
| 142 | face.onSendData.connect([&] (const Data& response) { |
| 143 | count++; |
| 144 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 145 | auto contentJson = ClientModule::getJsonFromData(response); |
| 146 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_NAME) != ""); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 147 | }); |
| 148 | face.receive(interest); |
| 149 | |
| 150 | advanceClocks(time::milliseconds(20), 60); |
| 151 | BOOST_CHECK_EQUAL(count, 1); |
| 152 | } |
| 153 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 154 | BOOST_AUTO_TEST_CASE(HandleNew) |
| 155 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 156 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 157 | auto key = identity.getDefaultKey(); |
| 158 | auto cert = key.getDefaultCertificate(); |
| 159 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 160 | util::DummyClientFace face(io, {true, true}); |
| 161 | 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] | 162 | advanceClocks(time::milliseconds(20), 60); |
| 163 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 164 | ClientModule client(m_keyChain); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 165 | ClientCaItem item; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 166 | item.m_caName = Name("/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 167 | item.m_anchor = cert; |
| 168 | client.getClientConf().m_caItems.push_back(item); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 169 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 170 | auto interest = client.generateNewInterest(time::system_clock::now(), |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 171 | time::system_clock::now() + time::days(10), |
| 172 | Name("/ndn/zhiyi")); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 173 | |
| 174 | int count = 0; |
| 175 | face.onSendData.connect([&] (const Data& response) { |
| 176 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 177 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 178 | auto contentJson = ClientModule::getJsonFromData(response); |
| 179 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_ECDH) != ""); |
| 180 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_SALT) != ""); |
Zhiyi Zhang | ff4bcb6 | 2019-09-08 12:57:42 -0700 | [diff] [blame] | 181 | BOOST_CHECK(contentJson.get<std::string>(JSON_CA_REQUEST_ID) != ""); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 182 | auto challengesJson = contentJson.get_child(JSON_CA_CHALLENGES); |
| 183 | BOOST_CHECK(challengesJson.size() != 0); |
| 184 | |
| 185 | client.onNewResponse(response); |
Zhiyi Zhang | 8da54d6 | 2019-11-21 00:03:05 -0800 | [diff] [blame] | 186 | BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey), |
| 187 | ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 188 | }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 189 | face.receive(*interest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 190 | |
| 191 | advanceClocks(time::milliseconds(20), 60); |
| 192 | BOOST_CHECK_EQUAL(count, 1); |
| 193 | } |
| 194 | |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 195 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1) |
| 196 | { |
| 197 | auto identity = addIdentity(Name("/ndn")); |
| 198 | auto key = identity.getDefaultKey(); |
| 199 | auto cert = key.getDefaultCertificate(); |
| 200 | |
| 201 | util::DummyClientFace face(io, {true, true}); |
| 202 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test"); |
| 203 | advanceClocks(time::milliseconds(20), 60); |
| 204 | |
| 205 | ClientModule client(m_keyChain); |
| 206 | ClientCaItem item; |
| 207 | item.m_caName = Name("/ndn"); |
| 208 | item.m_anchor = cert; |
| 209 | client.getClientConf().m_caItems.push_back(item); |
| 210 | auto current_tp = time::system_clock::now(); |
| 211 | auto interest1 = client.generateNewInterest(current_tp, current_tp - time::hours(1), |
| 212 | Name("/ndn/zhiyi")); |
| 213 | auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(361), |
| 214 | Name("/ndn/zhiyi")); |
| 215 | auto interest3 = client.generateNewInterest(current_tp - time::hours(1), |
| 216 | current_tp + time::hours(2), |
| 217 | Name("/ndn/zhiyi")); |
| 218 | face.onSendData.connect([&] (const Data& response) { |
| 219 | BOOST_CHECK(false); |
| 220 | }); |
| 221 | face.receive(*interest1); |
| 222 | face.receive(*interest2); |
| 223 | face.receive(*interest3); |
| 224 | |
| 225 | advanceClocks(time::milliseconds(20), 60); |
| 226 | } |
| 227 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 228 | BOOST_AUTO_TEST_CASE(HandleNewWithProbeToken) |
| 229 | { |
| 230 | auto identity = addIdentity(Name("/ndn")); |
| 231 | auto key = identity.getDefaultKey(); |
| 232 | auto cert = key.getDefaultCertificate(); |
| 233 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 234 | util::DummyClientFace face(io, {true, true}); |
| 235 | 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] | 236 | ca.m_config.m_probe = "email"; |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 237 | advanceClocks(time::milliseconds(20), 60); |
| 238 | |
| 239 | ClientModule client(m_keyChain); |
| 240 | ClientCaItem item; |
| 241 | item.m_caName = Name("/ndn"); |
| 242 | item.m_anchor = cert; |
| 243 | client.getClientConf().m_caItems.push_back(item); |
| 244 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 245 | auto data = make_shared<Data>(Name("/ndn/CA/PROBE/123")); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 246 | m_keyChain.sign(*data, signingByIdentity(ca.m_config.m_caName)); |
| 247 | |
| 248 | auto interest = client.generateNewInterest(time::system_clock::now(), |
| 249 | time::system_clock::now() + time::days(10), |
| 250 | Name("/ndn/zhiyi"), data); |
| 251 | |
| 252 | int count = 0; |
| 253 | face.onSendData.connect([&] (const Data& response) { |
| 254 | count++; |
| 255 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 256 | }); |
| 257 | face.receive(*interest); |
| 258 | |
| 259 | advanceClocks(time::milliseconds(20), 60); |
| 260 | BOOST_CHECK_EQUAL(count, 1); |
| 261 | } |
| 262 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 263 | BOOST_AUTO_TEST_CASE(HandleChallenge) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 264 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 265 | auto identity = addIdentity(Name("/ndn")); |
| 266 | auto key = identity.getDefaultKey(); |
| 267 | auto cert = key.getDefaultCertificate(); |
| 268 | |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 269 | util::DummyClientFace face(io, {true, true}); |
| 270 | 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] | 271 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 272 | |
| 273 | // generate NEW Interest |
| 274 | ClientModule client(m_keyChain); |
| 275 | ClientCaItem item; |
| 276 | item.m_caName = Name("/ndn"); |
| 277 | item.m_anchor = cert; |
| 278 | client.getClientConf().m_caItems.push_back(item); |
| 279 | auto newInterest = client.generateNewInterest(time::system_clock::now(), |
| 280 | time::system_clock::now() + time::days(10), Name("/ndn/zhiyi")); |
| 281 | |
| 282 | // generate CHALLENGE Interest |
| 283 | ChallengePin pinChallenge; |
| 284 | shared_ptr<Interest> challengeInterest = nullptr; |
| 285 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 286 | shared_ptr<Interest> challengeInterest3 = nullptr; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 287 | |
| 288 | int count = 0; |
| 289 | face.onSendData.connect([&] (const Data& response) { |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 290 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 291 | auto contentJson = ClientModule::getJsonFromData(response); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 292 | client.onNewResponse(response); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 293 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 294 | challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 295 | client.m_challengeStatus, |
| 296 | paramJson)); |
| 297 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 298 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 299 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 300 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 301 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 302 | client.onChallengeResponse(response); |
| 303 | BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE); |
| 304 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 305 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 306 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 307 | challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 308 | client.m_challengeStatus, |
| 309 | paramJson)); |
| 310 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 311 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 312 | count++; |
| 313 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 314 | |
| 315 | client.onChallengeResponse(response); |
| 316 | BOOST_CHECK_EQUAL(client.m_status, STATUS_CHALLENGE); |
| 317 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 318 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 319 | auto paramJson = pinChallenge.getRequirementForChallenge(client.m_status, client.m_challengeStatus); |
| 320 | auto request = ca.getCertificateRequest(*challengeInterest2); |
| 321 | auto secret = request.m_challengeSecrets.get(ChallengePin::JSON_PIN_CODE, ""); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 322 | for (auto& i : paramJson) { |
| 323 | if (i.first == ChallengePin::JSON_PIN_CODE) |
| 324 | i.second.put("", secret); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 325 | } |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame^] | 326 | challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 327 | client.m_challengeStatus, |
| 328 | paramJson)); |
| 329 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 330 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 331 | count++; |
| 332 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 333 | |
| 334 | client.onChallengeResponse(response); |
| 335 | BOOST_CHECK_EQUAL(client.m_status, STATUS_SUCCESS); |
| 336 | BOOST_CHECK_EQUAL(client.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 337 | } |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 338 | }); |
| 339 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 340 | face.receive(*newInterest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 341 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 342 | face.receive(*challengeInterest); |
| 343 | advanceClocks(time::milliseconds(20), 60); |
| 344 | face.receive(*challengeInterest2); |
| 345 | advanceClocks(time::milliseconds(20), 60); |
| 346 | face.receive(*challengeInterest3); |
| 347 | advanceClocks(time::milliseconds(20), 60); |
| 348 | BOOST_CHECK_EQUAL(count, 3); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 349 | } |
| 350 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 351 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
| 352 | |
| 353 | } // namespace tests |
| 354 | } // namespace ndncert |
| 355 | } // namespace ndn |