Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
tylerliu | 182bc53 | 2020-09-25 01:54:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2020, Regents of the University of California. |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 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 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 21 | #include "challenge-module/challenge-credential.hpp" |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 22 | #include "test-common.hpp" |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 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 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 30 | BOOST_FIXTURE_TEST_SUITE(TestChallengeCredential, IdentityManagementFixture) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 31 | |
| 32 | BOOST_AUTO_TEST_CASE(LoadConfig) |
| 33 | { |
| 34 | ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test"); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 35 | BOOST_CHECK_EQUAL(challenge.CHALLENGE_TYPE, "Credential"); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 36 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 37 | challenge.parseConfigFile(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 38 | 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 Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 44 | BOOST_AUTO_TEST_CASE(HandleChallengeRequest) |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 45 | { |
| 46 | // create trust anchor |
| 47 | ChallengeCredential challenge("./tests/unit-tests/challenge-credential.conf.test"); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 48 | auto identity = addIdentity(Name("/trust")); |
| 49 | auto key = identity.getDefaultKey(); |
| 50 | auto trustAnchor = key.getDefaultCertificate(); |
| 51 | challenge.parseConfigFile(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 52 | challenge.m_trustAnchors.front() = trustAnchor; |
| 53 | |
| 54 | // create certificate request |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 55 | auto identityA = addIdentity(Name("/example")); |
| 56 | auto keyA = identityA.getDefaultKey(); |
| 57 | auto certA = key.getDefaultCertificate(); |
Zhiyi Zhang | e232a74 | 2020-09-29 17:34:17 -0700 | [diff] [blame^] | 58 | RequestState request(Name("/example"), "123", RequestType::NEW, Status::BEFORE_CHALLENGE, certA); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 59 | |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 60 | // create requester's credential |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 61 | auto identityB = addIdentity(Name("/trust/cert")); |
| 62 | auto keyB = identityB.getDefaultKey(); |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 63 | auto credentialName = Name(keyB.getName()).append("Credential").appendVersion(); |
| 64 | security::v2::Certificate credential; |
| 65 | credential.setName(credentialName); |
| 66 | credential.setContent(keyB.getPublicKey().data(), keyB.getPublicKey().size()); |
| 67 | SignatureInfo signatureInfo; |
| 68 | signatureInfo.setValidityPeriod(security::ValidityPeriod(system_clock::now(), system_clock::now() + time::minutes(1))); |
| 69 | m_keyChain.sign(credential, signingByCertificate(trustAnchor).setSignatureInfo(signatureInfo)); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 70 | |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 71 | // using private key to sign cert request |
| 72 | Name idSignatureName = credential.getKeyName(); |
| 73 | idSignatureName.append("request-id-signature").appendVersion(); |
| 74 | security::v2::Certificate idSignature; |
| 75 | idSignature.setName(idSignatureName); |
| 76 | idSignature.setContent(makeStringBlock(tlv::Content, "123")); |
| 77 | m_keyChain.sign(idSignature, signingByCertificate(credential)); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 78 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 79 | std::stringstream ss; |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 80 | io::save<security::v2::Certificate>(idSignature, ss); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 81 | auto checkCert = *(io::load<security::v2::Certificate>(ss)); |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(checkCert, idSignature); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 83 | ss.str(""); |
| 84 | ss.clear(); |
| 85 | |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 86 | io::save<security::v2::Certificate>(idSignature, ss); |
| 87 | std::string idSignatureStr = ss.str(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 88 | ss.str(""); |
| 89 | ss.clear(); |
| 90 | |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 91 | io::save<security::v2::Certificate>(credential, ss); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 92 | std::string credentialStr = ss.str(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 93 | ss.str(""); |
| 94 | ss.clear(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 95 | |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 96 | Block params = makeEmptyBlock(tlv_encrypted_payload); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 97 | params.push_back(makeStringBlock(tlv_selected_challenge, "Credential")); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 98 | params.push_back(makeStringBlock(tlv_parameter_key, ChallengeCredential::PARAMETER_KEY_CREDENTIAL_CERT)); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 99 | params.push_back(makeStringBlock(tlv_parameter_value, credentialStr)); |
Zhiyi Zhang | 4604983 | 2020-09-28 17:08:12 -0700 | [diff] [blame] | 100 | params.push_back(makeStringBlock(tlv_parameter_key, ChallengeCredential::PARAMETER_KEY_PROOF_OF_PRIVATE_KEY)); |
tylerliu | 05405f5 | 2020-09-29 12:27:23 -0700 | [diff] [blame] | 101 | params.push_back(makeStringBlock(tlv_parameter_value, idSignatureStr)); |
Zhiyi Zhang | 2299861 | 2020-09-25 14:43:23 -0700 | [diff] [blame] | 102 | params.encode(); |
Suyong Won | 44d0cce | 2020-05-10 04:07:43 -0700 | [diff] [blame] | 103 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 104 | challenge.handleChallengeRequest(params, request); |
Zhiyi Zhang | 48f2378 | 2020-09-28 12:11:24 -0700 | [diff] [blame] | 105 | BOOST_CHECK(request.m_status == Status::PENDING); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | BOOST_AUTO_TEST_SUITE_END() |
| 109 | |
Zhiyi Zhang | 5d80e1e | 2020-09-25 11:34:54 -0700 | [diff] [blame] | 110 | } // namespace tests |
| 111 | } // namespace ndncert |
| 112 | } // namespace ndn |