blob: 38b77bb52afdd434fceb50bd5fb7090bd6cbb029 [file] [log] [blame]
Zhiyi Zhang0a89b722017-04-28 17:56:01 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
tylerliu182bc532020-09-25 01:54:45 -07003 * Copyright (c) 2017-2020, Regents of the University of California.
Zhiyi Zhang0a89b722017-04-28 17:56:01 -07004 *
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 Zhangdbd9d432020-10-07 15:56:27 -070021#include "identity-challenge/challenge-credential.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070022#include "test-common.hpp"
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070023
24namespace ndn {
25namespace ndncert {
26namespace tests {
27
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070028BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementFixture)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070029
30BOOST_AUTO_TEST_CASE(LoadConfig)
31{
Zhiyi Zhang63cd7d12020-10-10 15:25:48 -070032 ChallengeCredential challenge("./tests/unit-tests/config-files/config-challenge-credential");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070033 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Credential");
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070034
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070035 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070036 BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
37 auto cert = challenge.m_trustAnchors.front();
38 BOOST_CHECK_EQUAL(cert.getName(),
39 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
40}
41
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070042BOOST_AUTO_TEST_CASE(HandleChallengeRequest)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070043{
44 // create trust anchor
Zhiyi Zhang63cd7d12020-10-10 15:25:48 -070045 ChallengeCredential challenge("./tests/unit-tests/config-files/config-challenge-credential");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070046 auto identity = addIdentity(Name("/trust"));
47 auto key = identity.getDefaultKey();
48 auto trustAnchor = key.getDefaultCertificate();
49 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070050 challenge.m_trustAnchors.front() = trustAnchor;
51
52 // create certificate request
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070053 auto identityA = addIdentity(Name("/example"));
54 auto keyA = identityA.getDefaultKey();
55 auto certA = key.getDefaultCertificate();
Zhiyi Zhangc9ada1b2020-10-29 19:13:15 -070056 RequestId requestId = {1,2,3,4,5,6,7,8};
Zhiyi Zhang1f9551b2020-10-30 10:30:43 -070057 std::array<uint8_t, 16> aesKey;
58 ca::RequestState state(Name("/example"), requestId, RequestType::NEW,
59 Status::BEFORE_CHALLENGE, certA, std::move(aesKey));
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070060
tylerliu05405f52020-09-29 12:27:23 -070061 // create requester's credential
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070062 auto identityB = addIdentity(Name("/trust/cert"));
63 auto keyB = identityB.getDefaultKey();
tylerliu05405f52020-09-29 12:27:23 -070064 auto credentialName = Name(keyB.getName()).append("Credential").appendVersion();
tylerliua7bea662020-10-08 18:51:02 -070065 security::Certificate credential;
tylerliu05405f52020-09-29 12:27:23 -070066 credential.setName(credentialName);
67 credential.setContent(keyB.getPublicKey().data(), keyB.getPublicKey().size());
68 SignatureInfo signatureInfo;
tylerliu6563f932020-10-30 11:13:38 -070069 signatureInfo.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), time::system_clock::now() +
70 time::minutes(1)));
tylerliu05405f52020-09-29 12:27:23 -070071 m_keyChain.sign(credential, signingByCertificate(trustAnchor).setSignatureInfo(signatureInfo));
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070072 m_keyChain.addCertificate(keyB, credential);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070073
tylerliu05405f52020-09-29 12:27:23 -070074 // using private key to sign cert request
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070075 auto params = challenge.getRequestedParameterList(state.m_status, "");
Zhiyi Zhang8fdb36b2020-10-18 11:58:51 -070076 ChallengeCredential::fulfillParameters(params, m_keyChain, credential.getName(), requestId);
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070077 Block paramsTlv = challenge.genChallengeRequestTLV(state.m_status, "", std::move(params));
Zhiyi Zhang621a3b52020-10-03 21:55:11 -070078 challenge.handleChallengeRequest(paramsTlv, state);
Zhiyi Zhang34a8d432020-10-03 22:14:25 -070079 BOOST_CHECK_EQUAL(statusToString(state.m_status), statusToString(Status::PENDING));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070080}
81
82BOOST_AUTO_TEST_SUITE_END()
83
Zhiyi Zhange4891b72020-10-10 15:11:57 -070084} // namespace tests
85} // namespace ndncert
86} // namespace ndn