Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2017, 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 "identity-management-fixture.hpp" |
| 22 | #include "challenge-module/challenge-credential.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, IdentityManagementV2Fixture) |
| 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 | |
| 44 | BOOST_AUTO_TEST_CASE(HandleSelect) |
| 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(); |
| 58 | CertificateRequest request(Name("/example"), "123", certA); |
| 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 | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 68 | security::v2::Certificate credential = certB; |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 69 | credential.setName(credentialName); |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 70 | credential.setContent(certB.getContent()); |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 71 | m_keyChain.sign(credential, signingByCertificate(trustAnchor)); |
| 72 | |
| 73 | // generate SELECT interest |
| 74 | std::stringstream ss; |
| 75 | io::save<security::v2::Certificate>(credential, ss); |
| 76 | auto checkCert = *(io::load<security::v2::Certificate>(ss)); |
| 77 | BOOST_CHECK_EQUAL(checkCert, credential); |
| 78 | ss.str(""); |
| 79 | ss.clear(); |
| 80 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 81 | std::list<std::string> paramList; |
Zhiyi Zhang | 70fe258 | 2017-05-19 15:01:03 -0700 | [diff] [blame] | 82 | io::save<security::v2::Certificate>(credential, ss); |
| 83 | std::string paramString = ss.str(); |
| 84 | paramList.push_back(paramString); |
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); |
| 89 | paramString = ss.str(); |
| 90 | paramList.push_back(paramString); |
| 91 | ss.str(""); |
| 92 | ss.clear(); |
| 93 | JsonSection credentialJson = challenge.genSelectParamsJson(ChallengeModule::WAIT_SELECTION, paramList); |
| 94 | |
Zhiyi Zhang | 0a89b72 | 2017-04-28 17:56:01 -0700 | [diff] [blame] | 95 | boost::property_tree::write_json(ss, credentialJson); |
| 96 | Block jsonContent = makeStringBlock(ndn::tlv::NameComponent, ss.str()); |
| 97 | |
| 98 | Name interestName("/example/CA"); |
| 99 | interestName.append("_SELECT").append("Fake-Request-ID").append("CREDENTIAL").append(jsonContent); |
| 100 | Interest interest(interestName); |
| 101 | |
| 102 | challenge.processSelectInterest(interest, request); |
| 103 | BOOST_CHECK_EQUAL(request.getStatus(), ChallengeModule::SUCCESS); |
| 104 | BOOST_CHECK_EQUAL(request.getChallengeSecrets().empty(), true); |
| 105 | } |
| 106 | |
| 107 | BOOST_AUTO_TEST_SUITE_END() |
| 108 | |
| 109 | } // namespace tests |
| 110 | } // namespace ndncert |
| 111 | } // namespace ndn |