tylerliu | 729212e | 2020-09-26 14:31:25 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
| 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-module/challenge-private-key.hpp" |
| 22 | #include "test-common.hpp" |
| 23 | #include <ndn-cxx/security/signing-helpers.hpp> |
| 24 | #include <ndn-cxx/util/io.hpp> |
| 25 | |
| 26 | namespace ndn { |
| 27 | namespace ndncert { |
| 28 | namespace tests { |
| 29 | |
| 30 | BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementFixture) |
| 31 | |
| 32 | BOOST_AUTO_TEST_CASE(HandlePrivateKeyChallengeRequest) |
| 33 | { |
| 34 | // create trust anchor |
| 35 | ChallengePrivateKey challenge; |
| 36 | |
| 37 | // create certificate request |
| 38 | auto identityA = addIdentity(Name("/example")); |
| 39 | auto keyA = identityA.getDefaultKey(); |
| 40 | auto certA = keyA.getDefaultCertificate(); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 41 | CertificateRequest request(Name("/example"), "123", REQUEST_TYPE_REVOKE, Status::BEFORE_CHALLENGE, certA); |
tylerliu | 729212e | 2020-09-26 14:31:25 -0700 | [diff] [blame] | 42 | |
| 43 | security::v2::Certificate privateKeyProof; |
| 44 | privateKeyProof.setName(Name(keyA.getName()).append("proof-of-private-key").appendVersion()); |
| 45 | privateKeyProof.setContent(makeStringBlock(tlv::Content, "123")); |
| 46 | m_keyChain.sign(privateKeyProof, signingByKey(keyA)); |
| 47 | |
| 48 | std::stringstream ss; |
| 49 | io::save<security::v2::Certificate>(privateKeyProof, ss); |
| 50 | auto checkCert = *(io::load<security::v2::Certificate>(ss)); |
| 51 | BOOST_CHECK_EQUAL(checkCert, privateKeyProof); |
| 52 | ss.str(""); |
| 53 | ss.clear(); |
| 54 | |
| 55 | io::save<security::v2::Certificate>(privateKeyProof, ss); |
| 56 | std::string selfSignedStr = ss.str(); |
| 57 | ss.str(""); |
| 58 | ss.clear(); |
| 59 | |
| 60 | Block params = makeEmptyBlock(tlv_encrypted_payload); |
| 61 | params.push_back(makeStringBlock(tlv_selected_challenge, "Private Key")); |
| 62 | params.push_back(makeStringBlock(tlv_parameter_key, ChallengePrivateKey::JSON_PROOF_OF_PRIVATE_KEY)); |
| 63 | params.push_back(makeStringBlock(tlv_parameter_value, selfSignedStr)); |
| 64 | params.encode(); |
| 65 | |
| 66 | challenge.handleChallengeRequest(params, request); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame^] | 67 | BOOST_CHECK(request.m_status == Status::PENDING); |
tylerliu | 729212e | 2020-09-26 14:31:25 -0700 | [diff] [blame] | 68 | BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
| 69 | } |
| 70 | |
| 71 | BOOST_AUTO_TEST_SUITE_END() |
| 72 | |
| 73 | } // namespace tests |
| 74 | } // namespace ndncert |
| 75 | } // namespace ndn |