blob: 5294ff05bc9c4764317e2b966776c3ae357b4d8a [file] [log] [blame]
tylerliuf51e3162020-12-20 19:22:59 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento0dc02012021-11-23 22:55:03 -05002/*
Davide Pesavento6f1a2ab2022-03-17 03:57:21 -04003 * Copyright (c) 2017-2022, Regents of the University of California.
tylerliuf51e3162020-12-20 19:22:59 -08004 *
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"
tylerliuf51e3162020-12-20 19:22:59 -080022#include "detail/challenge-encoder.hpp"
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040023
Davide Pesavento0dc02012021-11-23 22:55:03 -050024#include "test-common.hpp"
tylerliuf51e3162020-12-20 19:22:59 -080025
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040026namespace ndncert::tests {
tylerliuf51e3162020-12-20 19:22:59 -080027
Junxiao Shibdcf52e2022-04-24 21:49:01 +000028class ChallengePossessionFixture : public IdentityManagementFixture
29{
30public:
31 void
32 createTrustAnchor()
33 {
34 trustAnchor = addIdentity("/trust").getDefaultKey().getDefaultCertificate();
35 challenge.parseConfigFile();
36 challenge.m_trustAnchors.front() = trustAnchor;
37 }
38
39 void
40 createCertificateRequest()
41 {
42 state.caPrefix = "/example";
43 state.requestId = RequestId{{101}};
44 state.requestType = RequestType::NEW;
45 state.cert = addIdentity("/example").getDefaultKey().getDefaultCertificate();
46 }
47
48 void
49 createRequesterCredential()
50 {
51 auto keyB = addIdentity("/trust/cert").getDefaultKey();
52 ndn::security::MakeCertificateOptions opts;
53 opts.issuerId = ndn::name::Component("Credential");
54 opts.validity.emplace(ndn::security::ValidityPeriod::makeRelative(-1_s, 1_min));
55 credential = m_keyChain.makeCertificate(keyB, signingByCertificate(trustAnchor), opts);
56 m_keyChain.addCertificate(keyB, credential);
57 }
58
59 void
60 signCertRequest()
61 {
62 auto params = challenge.getRequestedParameterList(state.status, "");
63 ChallengePossession::fulfillParameters(params, m_keyChain, credential.getName(), std::array<uint8_t, 16>{});
64 Block paramsTlv = challenge.genChallengeRequestTLV(state.status, "", params);
65 challenge.handleChallengeRequest(paramsTlv, state);
66 BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::CHALLENGE));
67 BOOST_REQUIRE(state.challengeState.has_value());
68 BOOST_CHECK_EQUAL(state.challengeState->challengeStatus, "need-proof");
69 }
70
71 void
72 replyFromServer(ndn::span<const uint8_t, 16> nonce)
73 {
74 auto params2 = challenge.getRequestedParameterList(state.status, state.challengeState->challengeStatus);
75 ChallengePossession::fulfillParameters(params2, m_keyChain, credential.getName(), nonce);
76 Block paramsTlv2 = challenge.genChallengeRequestTLV(state.status, state.challengeState->challengeStatus, params2);
77 challenge.handleChallengeRequest(paramsTlv2, state);
78 }
79
80public:
81 ChallengePossession challenge{"tests/unit-tests/config-files/config-challenge-possession"};
82 Certificate trustAnchor;
83 ca::RequestState state;
84 Certificate credential;
85};
86
87BOOST_FIXTURE_TEST_SUITE(TestChallengePossession, ChallengePossessionFixture)
tylerliuf51e3162020-12-20 19:22:59 -080088
89BOOST_AUTO_TEST_CASE(LoadConfig)
90{
tylerliuf51e3162020-12-20 19:22:59 -080091 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Possession");
92
93 challenge.parseConfigFile();
94 BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
95 auto cert = challenge.m_trustAnchors.front();
96 BOOST_CHECK_EQUAL(cert.getName(),
97 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
98}
99
100BOOST_AUTO_TEST_CASE(HandleChallengeRequest)
101{
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000102 createTrustAnchor();
103 createCertificateRequest();
104 createRequesterCredential();
105 signCertRequest();
tylerliuf51e3162020-12-20 19:22:59 -0800106
Davide Pesavento0dc02012021-11-23 22:55:03 -0500107 auto nonceBuf = ndn::fromHex(state.challengeState->secrets.get("nonce", ""));
tylerliuf51e3162020-12-20 19:22:59 -0800108 std::array<uint8_t, 16> nonce{};
109 memcpy(nonce.data(), nonceBuf->data(), 16);
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000110 replyFromServer(nonce);
tylerliuf51e3162020-12-20 19:22:59 -0800111 BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::PENDING));
112}
113
114BOOST_AUTO_TEST_CASE(HandleChallengeRequestProofFail)
115{
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000116 createTrustAnchor();
117 createCertificateRequest();
118 createRequesterCredential();
119 signCertRequest();
tylerliuf51e3162020-12-20 19:22:59 -0800120
tylerliuf51e3162020-12-20 19:22:59 -0800121 std::array<uint8_t, 16> nonce{};
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000122 replyFromServer(nonce);
tylerliuf51e3162020-12-20 19:22:59 -0800123 BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::FAILURE));
124}
125
Davide Pesavento0dc02012021-11-23 22:55:03 -0500126BOOST_AUTO_TEST_SUITE_END() // TestChallengePossession
tylerliuf51e3162020-12-20 19:22:59 -0800127
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400128} // namespace ndncert::tests