blob: e600e2002216935a9c22e90f30ff406c576faef6 [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 Zhang0a89b722017-04-28 17:56:01 -070021#include "challenge-module/challenge-credential.hpp"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070022#include "test-common.hpp"
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070023#include <ndn-cxx/security/signing-helpers.hpp>
24#include <ndn-cxx/util/io.hpp>
25
26namespace ndn {
27namespace ndncert {
28namespace tests {
29
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070030BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementFixture)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070031
32BOOST_AUTO_TEST_CASE(LoadConfig)
33{
34 ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070035 BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Credential");
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070036
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070037 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070038 BOOST_CHECK_EQUAL(challenge.m_trustAnchors.size(), 1);
39 auto cert = challenge.m_trustAnchors.front();
40 BOOST_CHECK_EQUAL(cert.getName(),
41 "/ndn/site1/KEY/%11%BC%22%F4c%15%FF%17/self/%FD%00%00%01Y%C8%14%D9%A5");
42}
43
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070044BOOST_AUTO_TEST_CASE(HandleChallengeRequest)
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070045{
46 // create trust anchor
47 ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test");
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070048 auto identity = addIdentity(Name("/trust"));
49 auto key = identity.getDefaultKey();
50 auto trustAnchor = key.getDefaultCertificate();
51 challenge.parseConfigFile();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070052 challenge.m_trustAnchors.front() = trustAnchor;
53
54 // create certificate request
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070055 auto identityA = addIdentity(Name("/example"));
56 auto keyA = identityA.getDefaultKey();
57 auto certA = key.getDefaultCertificate();
Zhiyi Zhang48f23782020-09-28 12:11:24 -070058 CertificateRequest request(Name("/example"), "123", REQUEST_TYPE_NEW, Status::BEFORE_CHALLENGE, certA);
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070059
60 // create requester's existing cert
61 auto identityB = addIdentity(Name("/trust/cert"));
62 auto keyB = identityB.getDefaultKey();
63 auto certB = key.getDefaultCertificate();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070064
65 // using trust anchor to sign cert request to get credential
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070066 Name credentialName = certB.getKeyName();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070067 credentialName.append("Credential").appendVersion();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068 security::v2::Certificate selfSigned;
69 selfSigned.setName(credentialName);
70 selfSigned.setContent(makeStringBlock(tlv::Content, "123"));
71 m_keyChain.sign(selfSigned, signingByCertificate(trustAnchor));
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070072
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070073 std::stringstream ss;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074 io::save<security::v2::Certificate>(selfSigned, ss);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070075 auto checkCert = *(io::load<security::v2::Certificate>(ss));
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070076 BOOST_CHECK_EQUAL(checkCert, selfSigned);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070077 ss.str("");
78 ss.clear();
79
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080 io::save<security::v2::Certificate>(selfSigned, ss);
81 std::string selfSignedStr = ss.str();
Zhiyi Zhang0a89b722017-04-28 17:56:01 -070082 ss.str("");
83 ss.clear();
84
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070085 io::save<security::v2::Certificate>(certB, ss);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 std::string credentialStr = ss.str();
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070087 ss.str("");
88 ss.clear();
Zhiyi Zhang70fe2582017-05-19 15:01:03 -070089
Suyong Won44d0cce2020-05-10 04:07:43 -070090 Block params = makeEmptyBlock(tlv_encrypted_payload);
Zhiyi Zhang22998612020-09-25 14:43:23 -070091 params.push_back(makeStringBlock(tlv_selected_challenge, "Credential"));
Suyong Won44d0cce2020-05-10 04:07:43 -070092 params.push_back(makeStringBlock(tlv_parameter_key, ChallengeCredential::JSON_CREDENTIAL_CERT));
93 params.push_back(makeStringBlock(tlv_parameter_value, credentialStr));
tylerliu729212e2020-09-26 14:31:25 -070094 params.push_back(makeStringBlock(tlv_parameter_key, ChallengeCredential::JSON_PROOF_OF_PRIVATE_KEY));
Zhiyi Zhang22998612020-09-25 14:43:23 -070095 params.push_back(makeStringBlock(tlv_parameter_value, selfSignedStr));
96 params.encode();
Suyong Won44d0cce2020-05-10 04:07:43 -070097
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070098 challenge.handleChallengeRequest(params, request);
Zhiyi Zhang48f23782020-09-28 12:11:24 -070099 BOOST_CHECK(request.m_status == Status::PENDING);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700100 BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS);
Zhiyi Zhang0a89b722017-04-28 17:56:01 -0700101}
102
103BOOST_AUTO_TEST_SUITE_END()
104
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -0700105} // namespace tests
106} // namespace ndncert
107} // namespace ndn