blob: cc694e69b3b948f7f50998a358962ab05850e113 [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 Pesavento829aff62022-05-15 20:30:34 -040024#include "tests/boost-test.hpp"
25#include "tests/key-chain-fixture.hpp"
tylerliuf51e3162020-12-20 19:22:59 -080026
Davide Pesavento0d1d11c2022-04-11 22:11:34 -040027namespace ndncert::tests {
tylerliuf51e3162020-12-20 19:22:59 -080028
Davide Pesavento829aff62022-05-15 20:30:34 -040029class ChallengePossessionFixture : public KeyChainFixture
Junxiao Shibdcf52e2022-04-24 21:49:01 +000030{
31public:
32 void
33 createTrustAnchor()
34 {
Davide Pesavento829aff62022-05-15 20:30:34 -040035 trustAnchor = m_keyChain.createIdentity("/trust").getDefaultKey().getDefaultCertificate();
Junxiao Shibdcf52e2022-04-24 21:49:01 +000036 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 Pesavento829aff62022-05-15 20:30:34 -040046 state.cert = m_keyChain.createIdentity("/example").getDefaultKey().getDefaultCertificate();
Junxiao Shibdcf52e2022-04-24 21:49:01 +000047 }
48
49 void
50 createRequesterCredential()
51 {
Davide Pesavento829aff62022-05-15 20:30:34 -040052 auto keyB = m_keyChain.createIdentity("/trust/cert").getDefaultKey();
Junxiao Shibdcf52e2022-04-24 21:49:01 +000053 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
81public:
82 ChallengePossession challenge{"tests/unit-tests/config-files/config-challenge-possession"};
83 Certificate trustAnchor;
84 ca::RequestState state;
85 Certificate credential;
86};
87
88BOOST_FIXTURE_TEST_SUITE(TestChallengePossession, ChallengePossessionFixture)
tylerliuf51e3162020-12-20 19:22:59 -080089
90BOOST_AUTO_TEST_CASE(LoadConfig)
91{
tylerliuf51e3162020-12-20 19:22:59 -080092 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
101BOOST_AUTO_TEST_CASE(HandleChallengeRequest)
102{
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000103 createTrustAnchor();
104 createCertificateRequest();
105 createRequesterCredential();
106 signCertRequest();
tylerliuf51e3162020-12-20 19:22:59 -0800107
Davide Pesavento0dc02012021-11-23 22:55:03 -0500108 auto nonceBuf = ndn::fromHex(state.challengeState->secrets.get("nonce", ""));
tylerliuf51e3162020-12-20 19:22:59 -0800109 std::array<uint8_t, 16> nonce{};
110 memcpy(nonce.data(), nonceBuf->data(), 16);
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000111 replyFromServer(nonce);
tylerliuf51e3162020-12-20 19:22:59 -0800112 BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::PENDING));
113}
114
115BOOST_AUTO_TEST_CASE(HandleChallengeRequestProofFail)
116{
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000117 createTrustAnchor();
118 createCertificateRequest();
119 createRequesterCredential();
120 signCertRequest();
tylerliuf51e3162020-12-20 19:22:59 -0800121
tylerliuf51e3162020-12-20 19:22:59 -0800122 std::array<uint8_t, 16> nonce{};
Junxiao Shibdcf52e2022-04-24 21:49:01 +0000123 replyFromServer(nonce);
tylerliuf51e3162020-12-20 19:22:59 -0800124 BOOST_CHECK_EQUAL(statusToString(state.status), statusToString(Status::FAILURE));
125}
126
Davide Pesavento0dc02012021-11-23 22:55:03 -0500127BOOST_AUTO_TEST_SUITE_END() // TestChallengePossession
tylerliuf51e3162020-12-20 19:22:59 -0800128
Davide Pesavento0d1d11c2022-04-11 22:11:34 -0400129} // namespace ndncert::tests