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