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