tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 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 | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 21 | #include "detail/challenge-encoder.hpp" |
| 22 | #include "detail/error-encoder.hpp" |
| 23 | #include "detail/info-encoder.hpp" |
| 24 | #include "detail/new-renew-revoke-encoder.hpp" |
| 25 | #include "detail/probe-encoder.hpp" |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 26 | #include "detail/ca-configuration.hpp" |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 27 | #include "test-common.hpp" |
Zhiyi Zhang | 3f20f95 | 2020-11-19 19:26:43 -0800 | [diff] [blame] | 28 | #include "identity-management-fixture.hpp" |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ndn { |
| 31 | namespace ndncert { |
| 32 | namespace tests { |
| 33 | |
| 34 | BOOST_FIXTURE_TEST_SUITE(TestProtocolEncoding, IdentityManagementTimeFixture) |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 35 | |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 36 | BOOST_AUTO_TEST_CASE(InfoEncoding) |
| 37 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 38 | ca::CaConfig config; |
| 39 | config.load("tests/unit-tests/config-files/config-ca-1"); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 40 | |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 41 | requester::ProfileStorage caCache; |
| 42 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 43 | auto& cert = caCache.getKnownProfiles().front().m_cert; |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 44 | |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 45 | auto b = infotlv::encodeDataContent(config.caProfile, *cert); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 46 | auto item = infotlv::decodeDataContent(b); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 47 | |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 48 | BOOST_CHECK_EQUAL(*item.m_cert, *cert); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(item.m_caInfo, config.caProfile.m_caInfo); |
| 50 | BOOST_CHECK_EQUAL(item.m_caPrefix, config.caProfile.m_caPrefix); |
tylerliu | 2670ba9 | 2020-10-30 10:25:03 -0700 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL_COLLECTIONS(item.m_probeParameterKeys.begin(), item.m_probeParameterKeys.end(), |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 52 | config.caProfile.m_probeParameterKeys.begin(), config.caProfile.m_probeParameterKeys.end()); |
| 53 | BOOST_CHECK_EQUAL(item.m_maxValidityPeriod, config.caProfile.m_maxValidityPeriod); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | BOOST_AUTO_TEST_CASE(ErrorEncoding) |
| 57 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 58 | std::string msg = "Just to test"; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 59 | auto b = errortlv::encodeDataContent(ErrorCode::NAME_NOT_ALLOWED, msg); |
| 60 | auto item = errortlv::decodefromDataContent(b); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 61 | BOOST_CHECK_EQUAL(std::get<0>(item), ErrorCode::NAME_NOT_ALLOWED); |
| 62 | BOOST_CHECK_EQUAL(std::get<1>(item), msg); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | BOOST_AUTO_TEST_CASE(ProbeEncodingAppParam) |
| 66 | { |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 67 | std::multimap<std::string, std::string> parameters; |
| 68 | parameters.emplace("key1", "value1"); |
| 69 | parameters.emplace("key2", "value2"); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 70 | auto appParam = probetlv::encodeApplicationParameters(std::move(parameters)); |
| 71 | auto param1 = probetlv::decodeApplicationParameters(appParam); |
tylerliu | 4022633 | 2020-11-11 15:37:16 -0800 | [diff] [blame] | 72 | BOOST_CHECK_EQUAL(param1.size(), 2); |
| 73 | BOOST_CHECK_EQUAL(param1.find("key1")->second, "value1"); |
| 74 | BOOST_CHECK_EQUAL(param1.find("key2")->second, "value2"); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | BOOST_AUTO_TEST_CASE(ProbeEncodingData) |
| 78 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 79 | ca::CaConfig config; |
| 80 | config.load("tests/unit-tests/config-files/config-ca-5"); |
| 81 | std::vector<Name> names; |
| 82 | names.emplace_back("/ndn/1"); |
| 83 | names.emplace_back("/ndn/2"); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 84 | auto b = probetlv::encodeDataContent(names, 2, config.redirection); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 85 | std::vector<std::pair<Name, int>> retNames; |
| 86 | std::vector<Name> redirection; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 87 | probetlv::decodeDataContent(b, retNames, redirection); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(retNames.size(), names.size()); |
| 89 | auto it1 = retNames.begin(); |
| 90 | auto it2 = names.begin(); |
| 91 | for (; it1 != retNames.end() && it2 != names.end(); it1++, it2++) { |
| 92 | BOOST_CHECK_EQUAL(it1->first, *it2); |
| 93 | BOOST_CHECK_EQUAL(it1->second, 2); |
| 94 | } |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(redirection.size(), config.redirection.size()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 96 | auto it3 = redirection.begin(); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 97 | auto it4 = config.redirection.begin(); |
| 98 | for (; it3 != redirection.end() && it4 != config.redirection.end(); it3++, it4++) { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 99 | BOOST_CHECK_EQUAL(*it3, (*it4)->getFullName()); |
| 100 | } |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingParam) |
| 104 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 105 | requester::ProfileStorage caCache; |
| 106 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 107 | auto& certRequest = caCache.getKnownProfiles().front().m_cert; |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 108 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 109 | auto b = requesttlv::encodeApplicationParameters(RequestType::REVOKE, pub, *certRequest); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 110 | std::vector<uint8_t> returnedPub; |
| 111 | std::shared_ptr<security::Certificate> returnedCert; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 112 | requesttlv::decodeApplicationParameters(b, RequestType::REVOKE, returnedPub, returnedCert); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 113 | |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(returnedPub.size(), pub.size()); |
| 115 | for (auto it1 = returnedPub.begin(), it2 = pub.begin(); |
| 116 | it1 != returnedPub.end() && it2 != pub.end(); it1++, it2++) { |
| 117 | BOOST_CHECK_EQUAL(*it1, *it2); |
| 118 | } |
| 119 | BOOST_CHECK_EQUAL(*returnedCert, *certRequest); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(NewRevokeEncodingData) |
| 123 | { |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 124 | std::vector<uint8_t> pub = ECDHState().getSelfPubKey(); |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 125 | std::array<uint8_t, 32> salt = {{101}}; |
| 126 | RequestId id = {{102}}; |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 127 | std::vector<std::string> list; |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 128 | list.emplace_back("abc"); |
| 129 | list.emplace_back("def"); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 130 | auto b = requesttlv::encodeDataContent(pub, salt, id, Status::BEFORE_CHALLENGE, list); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 131 | std::vector<uint8_t> returnedPub; |
| 132 | std::array<uint8_t, 32> returnedSalt; |
| 133 | RequestId returnedId; |
| 134 | Status s; |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 135 | auto retlist = requesttlv::decodeDataContent(b, returnedPub, returnedSalt, returnedId, s); |
tylerliu | 2670ba9 | 2020-10-30 10:25:03 -0700 | [diff] [blame] | 136 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedPub.begin(), returnedPub.end(), pub.begin(), pub.end()); |
| 137 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedSalt.begin(), returnedSalt.end(), salt.begin(), salt.end()); |
| 138 | BOOST_CHECK_EQUAL_COLLECTIONS(returnedId.begin(), returnedId.end(), id.begin(), id.end()); |
Zhiyi Zhang | 4c259db | 2020-10-30 09:36:01 -0700 | [diff] [blame] | 139 | BOOST_CHECK_EQUAL(static_cast<size_t>(s), static_cast<size_t>(Status::BEFORE_CHALLENGE)); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 142 | BOOST_AUTO_TEST_CASE(ChallengeEncoding) |
| 143 | { |
| 144 | const uint8_t key[] = {0x23, 0x70, 0xe3, 0x20, 0xd4, 0x34, 0x42, 0x08, |
| 145 | 0xe0, 0xff, 0x56, 0x83, 0xf2, 0x43, 0xb2, 0x13}; |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 146 | requester::ProfileStorage caCache; |
| 147 | caCache.load("tests/unit-tests/config-files/config-client-1"); |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 148 | security::Certificate certRequest = *caCache.getKnownProfiles().front().m_cert; |
Zhiyi Zhang | 8059302 | 2020-11-17 10:55:48 -0800 | [diff] [blame] | 149 | RequestId id = {{102}}; |
Zhiyi Zhang | 1f5e86e | 2020-12-04 15:07:57 -0800 | [diff] [blame] | 150 | ca::RequestState state; |
| 151 | state.caPrefix = Name("/ndn/ucla"); |
| 152 | state.requestId = id; |
| 153 | state.requestType = RequestType::NEW; |
| 154 | state.status = Status::PENDING; |
| 155 | state.cert = certRequest; |
| 156 | std::memcpy(state.encryptionKey.data(), key, sizeof(key)); |
| 157 | state.challengeType = "pin"; |
Zhiyi Zhang | 01efccb | 2020-12-07 11:01:00 -0800 | [diff] [blame] | 158 | auto tp = time::system_clock::now(); |
| 159 | state.challengeState = ca::ChallengeState("test", tp, 3, time::seconds(3600), JsonSection()); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 160 | auto contentBlock = challengetlv::encodeDataContent(state, Name("/ndn/ucla/a/b/c")); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 161 | |
Zhiyi Zhang | 84e1184 | 2020-11-19 20:03:23 -0800 | [diff] [blame] | 162 | requester::RequestState context(m_keyChain, caCache.getKnownProfiles().front(), RequestType::NEW); |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 163 | context.requestId = id; |
| 164 | std::memcpy(context.aesKey.data(), key, sizeof(key)); |
Zhiyi Zhang | 01efccb | 2020-12-07 11:01:00 -0800 | [diff] [blame] | 165 | advanceClocks(time::seconds(10)); |
Zhiyi Zhang | f22ae24 | 2020-11-17 10:51:15 -0800 | [diff] [blame] | 166 | challengetlv::decodeDataContent(contentBlock, context); |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 167 | |
tylerliu | f2e6bb5 | 2020-12-13 13:23:05 -0800 | [diff] [blame] | 168 | BOOST_CHECK_EQUAL(static_cast<size_t>(context.status), static_cast<size_t>(Status::PENDING)); |
| 169 | BOOST_CHECK_EQUAL(context.challengeStatus, "test"); |
| 170 | BOOST_CHECK_EQUAL(context.remainingTries, 3); |
| 171 | BOOST_CHECK_EQUAL(context.freshBefore, tp + time::seconds(3600) + time::seconds(10)); |
| 172 | BOOST_CHECK_EQUAL(context.issuedCertName, "/ndn/ucla/a/b/c"); |
Zhiyi Zhang | 1f9551b | 2020-10-30 10:30:43 -0700 | [diff] [blame] | 173 | } |
tylerliu | d59f2cf | 2020-10-30 00:00:10 -0700 | [diff] [blame] | 174 | |
| 175 | BOOST_AUTO_TEST_SUITE_END() |
| 176 | |
tylerliu | 6563f93 | 2020-10-30 11:13:38 -0700 | [diff] [blame] | 177 | } // namespace tests |
| 178 | } // namespace ndncert |
| 179 | } // namespace ndn |