Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
| 10 | * |
| 11 | * This file is part of NLSR (Named-data Link State Routing). |
| 12 | * See AUTHORS.md for complete list of NLSR authors and contributors. |
| 13 | * |
| 14 | * NLSR is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
| 17 | * |
| 18 | * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 24 | */ |
| 25 | |
| 26 | #include "tests/key-chain-fixture.hpp" |
| 27 | |
| 28 | #include <ndn-cxx/util/io.hpp> |
| 29 | |
| 30 | #include <boost/filesystem/operations.hpp> |
| 31 | |
| 32 | namespace nlsr { |
| 33 | namespace test { |
| 34 | |
| 35 | using namespace ndn::security; |
| 36 | |
| 37 | KeyChainFixture::KeyChainFixture() |
| 38 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | KeyChainFixture::~KeyChainFixture() |
| 43 | { |
| 44 | boost::system::error_code ec; |
| 45 | for (const auto& certFile : m_certFiles) { |
| 46 | boost::filesystem::remove(certFile, ec); // ignore error |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | bool |
| 51 | KeyChainFixture::saveCert(const ndn::Data& cert, const std::string& filename) |
| 52 | { |
| 53 | m_certFiles.push_back(filename); |
| 54 | try { |
| 55 | ndn::io::save(cert, filename); |
| 56 | return true; |
| 57 | } |
| 58 | catch (const ndn::io::Error&) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | bool |
| 64 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
| 65 | { |
| 66 | Certificate cert; |
| 67 | try { |
| 68 | cert = identity.getDefaultKey().getDefaultCertificate(); |
| 69 | } |
| 70 | catch (const Pib::Error&) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return saveCert(cert, filename); |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | KeyChainFixture::saveIdentityCert(const ndn::Name& identityName, const std::string& filename, |
| 79 | bool allowCreate) |
| 80 | { |
| 81 | Identity id; |
| 82 | try { |
| 83 | id = m_keyChain.getPib().getIdentity(identityName); |
| 84 | } |
| 85 | catch (const Pib::Error&) { |
| 86 | if (allowCreate) { |
| 87 | id = m_keyChain.createIdentity(identityName); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | if (!id) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | return saveIdentityCert(id, filename); |
| 96 | } |
| 97 | |
| 98 | Identity |
| 99 | KeyChainFixture::addSubCertificate(const ndn::Name& subIdentityName, |
| 100 | const Identity& issuer, |
| 101 | const ndn::KeyParams& params) |
| 102 | { |
| 103 | auto subIdentity = m_keyChain.createIdentity(subIdentityName, params); |
| 104 | |
| 105 | auto request = subIdentity.getDefaultKey().getDefaultCertificate(); |
| 106 | ndn::security::MakeCertificateOptions opts; |
| 107 | opts.issuerId = ndn::name::Component::fromEscapedString("parent"); |
| 108 | m_keyChain.makeCertificate(request, ndn::signingByIdentity(issuer), opts); |
| 109 | |
| 110 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 111 | |
| 112 | return subIdentity; |
| 113 | } |
| 114 | |
| 115 | } // namespace test |
| 116 | } // namespace nlsr |