tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 6f1a2ab | 2022-03-17 03:57:21 -0400 | [diff] [blame] | 3 | * Copyright (c) 2017-2022, Regents of the University of California. |
tylerliu | f51e316 | 2020-12-20 19:22:59 -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 | |
| 21 | #include "challenge/challenge-possession.hpp" |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 22 | #include "detail/challenge-encoder.hpp" |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 23 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 24 | #include "tests/boost-test.hpp" |
| 25 | #include "tests/key-chain-fixture.hpp" |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 26 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 27 | namespace ndncert::tests { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 28 | |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 29 | class ChallengePossessionFixture : public KeyChainFixture |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 30 | { |
| 31 | public: |
| 32 | void |
| 33 | createTrustAnchor() |
| 34 | { |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 35 | trustAnchor = m_keyChain.createIdentity("/trust").getDefaultKey().getDefaultCertificate(); |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 36 | challenge.parseConfigFile(); |
| 37 | challenge.m_trustAnchors.front() = trustAnchor; |
| 38 | } |
| 39 | |
| 40 | void |
| 41 | createCertificateRequest() |
| 42 | { |
| 43 | state.caPrefix = "/example"; |
| 44 | state.requestId = RequestId{{101}}; |
| 45 | state.requestType = RequestType::NEW; |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 46 | state.cert = m_keyChain.createIdentity("/example").getDefaultKey().getDefaultCertificate(); |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void |
| 50 | createRequesterCredential() |
| 51 | { |
Davide Pesavento | 829aff6 | 2022-05-15 20:30:34 -0400 | [diff] [blame] | 52 | auto keyB = m_keyChain.createIdentity("/trust/cert").getDefaultKey(); |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 53 | ndn::security::MakeCertificateOptions opts; |
| 54 | opts.issuerId = ndn::name::Component("Credential"); |
| 55 | opts.validity.emplace(ndn::security::ValidityPeriod::makeRelative(-1_s, 1_min)); |
| 56 | credential = m_keyChain.makeCertificate(keyB, signingByCertificate(trustAnchor), opts); |
| 57 | m_keyChain.addCertificate(keyB, credential); |
| 58 | } |
| 59 | |
| 60 | void |
| 61 | signCertRequest() |
| 62 | { |
| 63 | auto params = challenge.getRequestedParameterList(state.status, ""); |
| 64 | ChallengePossession::fulfillParameters(params, m_keyChain, credential.getName(), std::array<uint8_t, 16>{}); |
| 65 | Block paramsTlv = challenge.genChallengeRequestTLV(state.status, "", params); |
| 66 | challenge.handleChallengeRequest(paramsTlv, state); |
| 67 | BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::CHALLENGE)); |
| 68 | BOOST_REQUIRE(state.challengeState.has_value()); |
| 69 | BOOST_CHECK_EQUAL(state.challengeState->challengeStatus, "need-proof"); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | replyFromServer(ndn::span<const uint8_t, 16> nonce) |
| 74 | { |
| 75 | auto params2 = challenge.getRequestedParameterList(state.status, state.challengeState->challengeStatus); |
| 76 | ChallengePossession::fulfillParameters(params2, m_keyChain, credential.getName(), nonce); |
| 77 | Block paramsTlv2 = challenge.genChallengeRequestTLV(state.status, state.challengeState->challengeStatus, params2); |
| 78 | challenge.handleChallengeRequest(paramsTlv2, state); |
| 79 | } |
| 80 | |
| 81 | public: |
| 82 | ChallengePossession challenge{"tests/unit-tests/config-files/config-challenge-possession"}; |
| 83 | Certificate trustAnchor; |
| 84 | ca::RequestState state; |
| 85 | Certificate credential; |
| 86 | }; |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_SUITE(TestChallengePossession, ChallengePossessionFixture) |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 89 | |
| 90 | BOOST_AUTO_TEST_CASE(LoadConfig) |
| 91 | { |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 92 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Possession"); |
| 93 | |
| 94 | challenge.parseConfigFile(); |
| 95 | BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1); |
| 96 | auto cert = challenge.m_trustAnchors.front(); |
| 97 | BOOST_CHECK_EQUAL(cert.getName(), |
| 98 | "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5"); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(HandleChallengeRequest) |
| 102 | { |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 103 | createTrustAnchor(); |
| 104 | createCertificateRequest(); |
| 105 | createRequesterCredential(); |
| 106 | signCertRequest(); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 107 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 108 | auto nonceBuf = ndn::fromHex(state.challengeState->secrets.get("nonce", "")); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 109 | std::array<uint8_t, 16> nonce{}; |
| 110 | memcpy(nonce.data(), nonceBuf->data(), 16); |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 111 | replyFromServer(nonce); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 112 | BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::PENDING)); |
| 113 | } |
| 114 | |
| 115 | BOOST_AUTO_TEST_CASE(HandleChallengeRequestProofFail) |
| 116 | { |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 117 | createTrustAnchor(); |
| 118 | createCertificateRequest(); |
| 119 | createRequesterCredential(); |
| 120 | signCertRequest(); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 121 | |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 122 | std::array<uint8_t, 16> nonce{}; |
Junxiao Shi | bdcf52e | 2022-04-24 21:49:01 +0000 | [diff] [blame] | 123 | replyFromServer(nonce); |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 124 | BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::FAILURE)); |
| 125 | } |
| 126 | |
Davide Pesavento | 0dc0201 | 2021-11-23 22:55:03 -0500 | [diff] [blame] | 127 | BOOST_AUTO_TEST_SUITE_END() // TestChallengePossession |
tylerliu | f51e316 | 2020-12-20 19:22:59 -0800 | [diff] [blame] | 128 | |
Davide Pesavento | 0d1d11c | 2022-04-11 22:11:34 -0400 | [diff] [blame] | 129 | } // namespace ndncert::tests |