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" |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 26 | #include "protocol-detail/info.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 27 | #include "test-common.hpp" |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndncert { |
| 31 | namespace tests { |
| 32 | |
Zhiyi Zhang | ae123bf | 2017-04-14 12:24:53 -0700 | [diff] [blame] | 33 | BOOST_FIXTURE_TEST_SUITE(TestCaModule, DatabaseFixture) |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 34 | |
| 35 | BOOST_AUTO_TEST_CASE(Initialization) |
| 36 | { |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 37 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 38 | 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] | 39 | BOOST_CHECK_EQUAL(ca.getCaConf().m_caPrefix, "/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 40 | |
| 41 | auto identity = addIdentity(Name("/ndn/site2")); |
| 42 | auto key = identity.getDefaultKey(); |
| 43 | auto cert = key.getDefaultCertificate(); |
| 44 | ca.getCaStorage()->addCertificate("111", cert); |
| 45 | BOOST_CHECK_EQUAL(ca.getCaStorage()->getCertificate("111").getIdentity(), Name("/ndn/site2")); |
| 46 | |
| 47 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(ca.m_registeredPrefixHandles.size(), 2); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 49 | BOOST_CHECK_EQUAL(ca.m_interestFilterHandles.size(), 5); // onInfo, onProbe, onNew, onChallenge, onRevoke |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 52 | BOOST_AUTO_TEST_CASE(HandleProbe) |
| 53 | { |
| 54 | auto identity = addIdentity(Name("/ndn")); |
| 55 | auto key = identity.getDefaultKey(); |
| 56 | auto cert = key.getDefaultCertificate(); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 57 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 58 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
| 59 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
| 60 | ca.setNameAssignmentFunction([&](const std::vector<std::tuple<std::string, std::string>>) -> std::vector<std::string> { |
| 61 | std::vector<std::string> result; |
| 62 | result.push_back("example"); |
| 63 | return result; |
| 64 | }); |
| 65 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 66 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 67 | Interest interest("/ndn/CA/PROBE"); |
| 68 | interest.setCanBePrefix(false); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 69 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 70 | Block paramTLV = makeEmptyBlock(tlv::ApplicationParameters); |
| 71 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, JSON_CLIENT_PROBE_INFO)); |
| 72 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi")); |
| 73 | paramTLV.encode(); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 74 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 75 | interest.setApplicationParameters(paramTLV); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 76 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 77 | int count = 0; |
| 78 | face.onSendData.connect([&](const Data& response) { |
| 79 | count++; |
| 80 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 81 | Block contentBlock = response.getContent(); |
| 82 | contentBlock.parse(); |
| 83 | Block probeResponse = contentBlock.get(tlv_probe_response); |
| 84 | probeResponse.parse(); |
| 85 | Name caName; |
| 86 | caName.wireDecode(probeResponse.get(tlv::Name)); |
| 87 | BOOST_CHECK_EQUAL(caName, "/ndn/example"); |
| 88 | }); |
| 89 | face.receive(interest); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 90 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 91 | advanceClocks(time::milliseconds(20), 60); |
| 92 | BOOST_CHECK_EQUAL(count, 1); |
| 93 | } |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 94 | |
swa770 | 20643ac | 2020-03-26 02:24:45 -0700 | [diff] [blame] | 95 | BOOST_AUTO_TEST_CASE(HandleInfo) |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 96 | { |
| 97 | auto identity = addIdentity(Name("/ndn")); |
| 98 | auto key = identity.getDefaultKey(); |
| 99 | auto cert = key.getDefaultCertificate(); |
| 100 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 101 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 102 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 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; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 109 | face.onSendData.connect([&](const Data& response) { |
| 110 | count++; |
| 111 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 112 | auto contentBlock = response.getContent(); |
| 113 | contentBlock.parse(); |
| 114 | auto caItem = INFO::decodeClientConfigFromContent(contentBlock); |
| 115 | BOOST_CHECK_EQUAL(caItem.m_caPrefix, "/ndn"); |
| 116 | BOOST_CHECK_EQUAL(caItem.m_probe, ""); |
| 117 | BOOST_CHECK_EQUAL(caItem.m_anchor.wireEncode(), cert.wireEncode()); |
| 118 | BOOST_CHECK_EQUAL(caItem.m_caInfo, "ndn testbed ca"); |
| 119 | }); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 120 | face.receive(interest); |
| 121 | |
| 122 | advanceClocks(time::milliseconds(20), 60); |
| 123 | BOOST_CHECK_EQUAL(count, 1); |
| 124 | } |
| 125 | |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 126 | BOOST_AUTO_TEST_CASE(HandleProbeUsingDefaultHandler) |
| 127 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 128 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 129 | auto key = identity.getDefaultKey(); |
| 130 | auto cert = key.getDefaultCertificate(); |
| 131 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 132 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 133 | 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] | 134 | advanceClocks(time::milliseconds(20), 60); |
| 135 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 136 | Interest interest("/ndn/CA/PROBE"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 137 | interest.setCanBePrefix(false); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 138 | |
| 139 | Block paramTLV = makeEmptyBlock(tlv::ApplicationParameters); |
| 140 | paramTLV.push_back(makeStringBlock(tlv_parameter_key, JSON_CLIENT_PROBE_INFO)); |
| 141 | paramTLV.push_back(makeStringBlock(tlv_parameter_value, "zhiyi")); |
| 142 | paramTLV.encode(); |
| 143 | |
| 144 | interest.setApplicationParameters(paramTLV); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 145 | |
| 146 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 147 | face.onSendData.connect([&](const Data& response) { |
| 148 | count++; |
| 149 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 150 | auto contentBlock = response.getContent(); |
| 151 | contentBlock.parse(); |
| 152 | auto probeResponseBlock = contentBlock.get(tlv_probe_response); |
| 153 | probeResponseBlock.parse(); |
| 154 | Name caPrefix; |
| 155 | caPrefix.wireDecode(probeResponseBlock.get(tlv::Name)); |
| 156 | BOOST_CHECK(caPrefix != ""); |
| 157 | }); |
Zhiyi Zhang | a63b737 | 2017-05-17 14:14:34 -0700 | [diff] [blame] | 158 | face.receive(interest); |
| 159 | |
| 160 | advanceClocks(time::milliseconds(20), 60); |
| 161 | BOOST_CHECK_EQUAL(count, 1); |
| 162 | } |
| 163 | |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 164 | BOOST_AUTO_TEST_CASE(HandleNew) |
| 165 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 166 | auto identity = addIdentity(Name("/ndn")); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 167 | auto key = identity.getDefaultKey(); |
| 168 | auto cert = key.getDefaultCertificate(); |
| 169 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 170 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 171 | 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] | 172 | advanceClocks(time::milliseconds(20), 60); |
| 173 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 174 | ClientModule client(m_keyChain); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 175 | ClientCaItem item; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 176 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 177 | item.m_anchor = cert; |
| 178 | client.getClientConf().m_caItems.push_back(item); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 179 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 180 | auto interest = client.generateNewInterest(time::system_clock::now(), |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 181 | time::system_clock::now() + time::days(1), |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 182 | Name("/ndn/zhiyi")); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 183 | |
| 184 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 185 | face.onSendData.connect([&](const Data& response) { |
| 186 | count++; |
| 187 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 188 | auto contentBlock = response.getContent(); |
| 189 | contentBlock.parse(); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 190 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 191 | BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != ""); |
| 192 | BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != ""); |
| 193 | BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != ""); |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 194 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 195 | auto challengeBlockCount = 0; |
| 196 | for (auto const& element : contentBlock.elements()) { |
| 197 | if (element.type() == tlv_challenge) { |
| 198 | challengeBlockCount++; |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 199 | } |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 200 | } |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 201 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 202 | BOOST_CHECK(challengeBlockCount != 0); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 203 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 204 | client.onNewResponse(response); |
| 205 | BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey), |
| 206 | ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey)); |
| 207 | }); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 208 | face.receive(*interest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 209 | |
| 210 | advanceClocks(time::milliseconds(20), 60); |
| 211 | BOOST_CHECK_EQUAL(count, 1); |
| 212 | } |
| 213 | |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 214 | BOOST_AUTO_TEST_CASE(HandleNewWithInvalidValidityPeriod1) |
| 215 | { |
| 216 | auto identity = addIdentity(Name("/ndn")); |
| 217 | auto key = identity.getDefaultKey(); |
| 218 | auto cert = key.getDefaultCertificate(); |
| 219 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 220 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 221 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test"); |
| 222 | advanceClocks(time::milliseconds(20), 60); |
| 223 | |
| 224 | ClientModule client(m_keyChain); |
| 225 | ClientCaItem item; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 226 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 227 | item.m_anchor = cert; |
| 228 | client.getClientConf().m_caItems.push_back(item); |
| 229 | auto current_tp = time::system_clock::now(); |
| 230 | auto interest1 = client.generateNewInterest(current_tp, current_tp - time::hours(1), |
| 231 | Name("/ndn/zhiyi")); |
| 232 | auto interest2 = client.generateNewInterest(current_tp, current_tp + time::days(361), |
| 233 | Name("/ndn/zhiyi")); |
| 234 | auto interest3 = client.generateNewInterest(current_tp - time::hours(1), |
| 235 | current_tp + time::hours(2), |
| 236 | Name("/ndn/zhiyi")); |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 237 | face.onSendData.connect([&](const Data& response) { |
| 238 | BOOST_CHECK(false); |
| 239 | }); |
Zhiyi Zhang | 1a735bc | 2019-07-04 21:36:49 -0700 | [diff] [blame] | 240 | face.receive(*interest1); |
| 241 | face.receive(*interest2); |
| 242 | face.receive(*interest3); |
| 243 | |
| 244 | advanceClocks(time::milliseconds(20), 60); |
| 245 | } |
| 246 | |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 247 | BOOST_AUTO_TEST_CASE(HandleNewWithProbeToken) |
| 248 | { |
| 249 | auto identity = addIdentity(Name("/ndn")); |
| 250 | auto key = identity.getDefaultKey(); |
| 251 | auto cert = key.getDefaultCertificate(); |
| 252 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 253 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 254 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 255 | advanceClocks(time::milliseconds(20), 60); |
| 256 | |
| 257 | ClientModule client(m_keyChain); |
| 258 | ClientCaItem item; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 259 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 260 | item.m_anchor = cert; |
| 261 | client.getClientConf().m_caItems.push_back(item); |
| 262 | |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 263 | auto data = make_shared<Data>(Name("/ndn/CA/PROBE/123")); |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 264 | m_keyChain.sign(*data, signingByIdentity(ca.m_config.m_caPrefix)); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 265 | |
| 266 | auto interest = client.generateNewInterest(time::system_clock::now(), |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 267 | time::system_clock::now() + time::days(1), |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 268 | Name("/ndn/zhiyi"), data); |
| 269 | |
| 270 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 271 | face.onSendData.connect([&](const Data& response) { |
| 272 | count++; |
| 273 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 274 | }); |
Zhiyi Zhang | 5f749a2 | 2019-06-12 17:02:33 -0700 | [diff] [blame] | 275 | face.receive(*interest); |
| 276 | |
| 277 | advanceClocks(time::milliseconds(20), 60); |
| 278 | BOOST_CHECK_EQUAL(count, 1); |
| 279 | } |
| 280 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 281 | BOOST_AUTO_TEST_CASE(HandleChallenge) |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 282 | { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 283 | auto identity = addIdentity(Name("/ndn")); |
| 284 | auto key = identity.getDefaultKey(); |
| 285 | auto cert = key.getDefaultCertificate(); |
| 286 | |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 287 | util::DummyClientFace face(io, m_keyChain, {true, true}); |
Zhiyi Zhang | 42d992d | 2019-07-07 16:46:50 -0700 | [diff] [blame] | 288 | 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] | 289 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 290 | |
| 291 | // generate NEW Interest |
| 292 | ClientModule client(m_keyChain); |
| 293 | ClientCaItem item; |
Suyong Won | 256c906 | 2020-05-11 02:45:56 -0700 | [diff] [blame] | 294 | item.m_caPrefix = Name("/ndn"); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 295 | item.m_anchor = cert; |
| 296 | client.getClientConf().m_caItems.push_back(item); |
| 297 | auto newInterest = client.generateNewInterest(time::system_clock::now(), |
Suyong Won | 7968f7a | 2020-05-12 01:01:25 -0700 | [diff] [blame] | 298 | time::system_clock::now() + time::days(1), Name("/ndn/zhiyi")); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 299 | |
| 300 | // generate CHALLENGE Interest |
| 301 | ChallengePin pinChallenge; |
| 302 | shared_ptr<Interest> challengeInterest = nullptr; |
| 303 | shared_ptr<Interest> challengeInterest2 = nullptr; |
| 304 | shared_ptr<Interest> challengeInterest3 = nullptr; |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 305 | |
| 306 | int count = 0; |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 307 | face.onSendData.connect([&](const Data& response) { |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 308 | if (Name("/ndn/CA/NEW").isPrefixOf(response.getName())) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 309 | client.onNewResponse(response); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 310 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 311 | challengeInterest = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 312 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 313 | std::move(paramList))); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 314 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 315 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 0) { |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 316 | count++; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 317 | BOOST_CHECK(security::verifySignature(response, cert)); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 318 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 319 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 320 | BOOST_CHECK(client.m_status == Status::CHALLENGE); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 321 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::NEED_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 322 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 323 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 324 | challengeInterest2 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 325 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 326 | std::move(paramList))); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 327 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 328 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 1) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 329 | count++; |
| 330 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 331 | |
| 332 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 333 | BOOST_CHECK(client.m_status == Status::CHALLENGE); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 334 | BOOST_CHECK_EQUAL(client.m_challengeStatus, ChallengePin::WRONG_CODE); |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 335 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 336 | auto paramList = pinChallenge.getRequestedParameterList(client.m_status, client.m_challengeStatus); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 337 | auto request = ca.getCertificateRequest(*challengeInterest2); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 338 | auto secret = request.m_challengeSecrets.get(ChallengePin::PARAMETER_KEY_CODE, ""); |
| 339 | std::get<1>(paramList[0]) = secret; |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 340 | challengeInterest3 = client.generateChallengeInterest(pinChallenge.genChallengeRequestTLV(client.m_status, |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 341 | client.m_challengeStatus, |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 342 | std::move(paramList))); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 343 | } |
swa770 | de007bc | 2020-03-24 21:26:21 -0700 | [diff] [blame] | 344 | else if (Name("/ndn/CA/CHALLENGE").isPrefixOf(response.getName()) && count == 2) { |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 345 | count++; |
| 346 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 347 | |
| 348 | client.onChallengeResponse(response); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 349 | BOOST_CHECK(client.m_status == Status::SUCCESS); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 350 | } |
Davide Pesavento | 914d05f | 2019-07-13 16:20:19 -0400 | [diff] [blame] | 351 | }); |
| 352 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 353 | face.receive(*newInterest); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 354 | advanceClocks(time::milliseconds(20), 60); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 355 | face.receive(*challengeInterest); |
| 356 | advanceClocks(time::milliseconds(20), 60); |
| 357 | face.receive(*challengeInterest2); |
| 358 | advanceClocks(time::milliseconds(20), 60); |
| 359 | face.receive(*challengeInterest3); |
| 360 | advanceClocks(time::milliseconds(20), 60); |
| 361 | BOOST_CHECK_EQUAL(count, 3); |
Zhiyi Zhang | 1c0bd37 | 2017-12-18 18:32:55 +0800 | [diff] [blame] | 362 | } |
| 363 | |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 364 | BOOST_AUTO_TEST_CASE(HandleRevoke) |
| 365 | { |
| 366 | auto identity = addIdentity(Name("/ndn")); |
| 367 | auto key = identity.getDefaultKey(); |
| 368 | auto cert = key.getDefaultCertificate(); |
| 369 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 370 | util::DummyClientFace face(io, {true, true}); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 371 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
| 372 | advanceClocks(time::milliseconds(20), 60); |
| 373 | |
| 374 | //generate a certificate |
| 375 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 376 | auto clientKey = clientIdentity.getDefaultKey(); |
| 377 | security::v2::Certificate clientCert; |
| 378 | clientCert.setName(Name(clientKey.getName()).append("cert-request").appendVersion()); |
| 379 | clientCert.setContentType(tlv::ContentType_Key); |
| 380 | clientCert.setFreshnessPeriod(time::hours(24)); |
| 381 | clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size()); |
| 382 | SignatureInfo signatureInfo; |
| 383 | signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 384 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 385 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 386 | CertificateRequest certRequest(Name("/ndn"), "122", REQUEST_TYPE_NEW, Status::SUCCESS, clientCert); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 387 | auto issuedCert = ca.issueCertificate(certRequest); |
| 388 | |
| 389 | ClientModule client(m_keyChain); |
| 390 | ClientCaItem item; |
| 391 | item.m_caName = Name("/ndn"); |
| 392 | item.m_anchor = cert; |
| 393 | client.getClientConf().m_caItems.push_back(item); |
| 394 | |
| 395 | auto interest = client.generateRevokeInterest(issuedCert); |
| 396 | |
| 397 | int count = 0; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 398 | face.onSendData.connect([&](const Data& response) { |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 399 | count++; |
| 400 | BOOST_CHECK(security::verifySignature(response, cert)); |
| 401 | auto contentBlock = response.getContent(); |
| 402 | contentBlock.parse(); |
| 403 | |
| 404 | BOOST_CHECK(readString(contentBlock.get(tlv_ecdh_pub)) != ""); |
| 405 | BOOST_CHECK(readString(contentBlock.get(tlv_salt)) != ""); |
| 406 | BOOST_CHECK(readString(contentBlock.get(tlv_request_id)) != ""); |
| 407 | |
| 408 | auto challengeBlockCount = 0; |
| 409 | for (auto const& element : contentBlock.elements()) { |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 410 | if (element.type() == tlv_challenge) { |
| 411 | challengeBlockCount++; |
| 412 | } |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | BOOST_CHECK(challengeBlockCount != 0); |
| 416 | |
| 417 | client.onRevokeResponse(response); |
| 418 | BOOST_CHECK_EQUAL_COLLECTIONS(client.m_aesKey, client.m_aesKey + sizeof(client.m_aesKey), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 419 | ca.m_aesKey, ca.m_aesKey + sizeof(ca.m_aesKey)); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 420 | }); |
| 421 | face.receive(*interest); |
| 422 | |
| 423 | advanceClocks(time::milliseconds(20), 60); |
| 424 | BOOST_CHECK_EQUAL(count, 1); |
| 425 | } |
| 426 | |
| 427 | BOOST_AUTO_TEST_CASE(HandleRevokeWithBadCert) |
| 428 | { |
| 429 | auto identity = addIdentity(Name("/ndn")); |
| 430 | auto key = identity.getDefaultKey(); |
| 431 | auto cert = key.getDefaultCertificate(); |
| 432 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 433 | util::DummyClientFace face(io, {true, true}); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 434 | CaModule ca(face, m_keyChain, "tests/unit-tests/ca.conf.test", "ca-storage-memory"); |
| 435 | advanceClocks(time::milliseconds(20), 60); |
| 436 | |
| 437 | //generate a certificate |
| 438 | auto clientIdentity = m_keyChain.createIdentity("/ndn/qwerty"); |
| 439 | auto clientKey = clientIdentity.getDefaultKey(); |
| 440 | security::v2::Certificate clientCert; |
| 441 | clientCert.setName(Name(clientKey.getName()).append("NDNCERT").append(std::to_string(1473283247810732701))); |
| 442 | clientCert.setContentType(tlv::ContentType_Key); |
| 443 | clientCert.setFreshnessPeriod(time::hours(24)); |
| 444 | clientCert.setContent(clientKey.getPublicKey().data(), clientKey.getPublicKey().size()); |
| 445 | SignatureInfo signatureInfo; |
| 446 | signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 447 | time::system_clock::now() + time::hours(10))); |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 448 | m_keyChain.sign(clientCert, signingByKey(clientKey.getName()).setSignatureInfo(signatureInfo)); |
| 449 | |
| 450 | ClientModule client(m_keyChain); |
| 451 | ClientCaItem item; |
| 452 | item.m_caName = Name("/ndn"); |
| 453 | item.m_anchor = cert; |
| 454 | client.getClientConf().m_caItems.push_back(item); |
| 455 | |
| 456 | auto interest = client.generateRevokeInterest(clientCert); |
| 457 | |
| 458 | int count = 0; |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 459 | face.onSendData.connect([&](const Data& response) { |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 460 | count++; |
| 461 | }); |
| 462 | face.receive(*interest); |
| 463 | |
| 464 | advanceClocks(time::milliseconds(20), 60); |
| 465 | BOOST_CHECK_EQUAL(count, 0); |
| 466 | } |
| 467 | |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame^] | 468 | BOOST_AUTO_TEST_SUITE_END() // TestCaModule |
Zhiyi Zhang | f5246c4 | 2017-01-26 09:39:20 -0800 | [diff] [blame] | 469 | |
Zhiyi Zhang | fc1678a | 2020-05-12 16:52:14 -0700 | [diff] [blame] | 470 | } // namespace tests |
| 471 | } // namespace ndncert |
| 472 | } // namespace ndn |