Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2019, 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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 22 | #include "identity-management-fixture.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 | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 58 | CertificateRequest request(Name("/example"), "123", STATUS_BEFORE_CHALLENGE, certA); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 59 | |
| 60 | // create requester's existing cert |
| 61 | auto identityB = addIdentity(Name("/trust/cert")); |
| 62 | auto keyB = identityB.getDefaultKey(); |
| 63 | auto certB = key.getDefaultCertificate(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 64 | |
| 65 | // using trust anchor to sign cert request to get credential |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 66 | Name credentialName = certB.getKeyName(); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 67 | credentialName.append("Credential").appendVersion(); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 68 | security::v2::Certificate selfSigned; |
| 69 | selfSigned.setName(credentialName); |
| 70 | selfSigned.setContent(makeStringBlock(tlv::Content, "123")); |
| 71 | m_keyChain.sign(selfSigned, signingByCertificate(trustAnchor)); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 72 | |
| 73 | // generate SELECT interest |
| 74 | std::stringstream ss; |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 75 | io::save<security::v2::Certificate>(selfSigned, ss); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 76 | auto checkCert = *(io::load<security::v2::Certificate>(ss)); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 77 | BOOST_CHECK_EQUAL(checkCert, selfSigned); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 78 | ss.str(""); |
| 79 | ss.clear(); |
| 80 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 81 | JsonSection params; |
| 82 | io::save<security::v2::Certificate>(selfSigned, ss); |
| 83 | std::string selfSignedStr = ss.str(); |
| 84 | params.add(ChallengeCredential::JSON_CREDENTIAL_SELF, selfSignedStr); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 85 | ss.str(""); |
| 86 | ss.clear(); |
| 87 | |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 88 | io::save<security::v2::Certificate>(certB, ss); |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 89 | std::string credentialStr = ss.str(); |
| 90 | params.add(ChallengeCredential::JSON_CREDENTIAL_CERT, credentialStr); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 91 | ss.str(""); |
| 92 | ss.clear(); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 93 | |
Zhiyi Zhang | af7c290 | 2019-03-14 22:13:21 -0700 | [diff] [blame] | 94 | challenge.handleChallengeRequest(params, request); |
| 95 | BOOST_CHECK_EQUAL(request.m_status, STATUS_PENDING); |
| 96 | BOOST_CHECK_EQUAL(request.m_challengeStatus, CHALLENGE_STATUS_SUCCESS); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | BOOST_AUTO_TEST_SUITE_END() |
| 100 | |
| 101 | } // namespace tests |
| 102 | } // namespace ndncert |
| 103 | } // namespace ndn |