Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 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 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 32 | namespace nlsr::tests { |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 33 | |
| 34 | using namespace ndn::security; |
| 35 | |
| 36 | KeyChainFixture::KeyChainFixture() |
| 37 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 38 | { |
| 39 | } |
| 40 | |
| 41 | KeyChainFixture::~KeyChainFixture() |
| 42 | { |
| 43 | boost::system::error_code ec; |
| 44 | for (const auto& certFile : m_certFiles) { |
| 45 | boost::filesystem::remove(certFile, ec); // ignore error |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | bool |
| 50 | KeyChainFixture::saveCert(const ndn::Data& cert, const std::string& filename) |
| 51 | { |
| 52 | m_certFiles.push_back(filename); |
| 53 | try { |
| 54 | ndn::io::save(cert, filename); |
| 55 | return true; |
| 56 | } |
| 57 | catch (const ndn::io::Error&) { |
| 58 | return false; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | bool |
| 63 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
| 64 | { |
| 65 | Certificate cert; |
| 66 | try { |
| 67 | cert = identity.getDefaultKey().getDefaultCertificate(); |
| 68 | } |
| 69 | catch (const Pib::Error&) { |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | return saveCert(cert, filename); |
| 74 | } |
| 75 | |
| 76 | bool |
| 77 | KeyChainFixture::saveIdentityCert(const ndn::Name& identityName, const std::string& filename, |
| 78 | bool allowCreate) |
| 79 | { |
| 80 | Identity id; |
| 81 | try { |
| 82 | id = m_keyChain.getPib().getIdentity(identityName); |
| 83 | } |
| 84 | catch (const Pib::Error&) { |
| 85 | if (allowCreate) { |
| 86 | id = m_keyChain.createIdentity(identityName); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if (!id) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | return saveIdentityCert(id, filename); |
| 95 | } |
| 96 | |
| 97 | Identity |
| 98 | KeyChainFixture::addSubCertificate(const ndn::Name& subIdentityName, |
| 99 | const Identity& issuer, |
| 100 | const ndn::KeyParams& params) |
| 101 | { |
| 102 | auto subIdentity = m_keyChain.createIdentity(subIdentityName, params); |
| 103 | |
| 104 | auto request = subIdentity.getDefaultKey().getDefaultCertificate(); |
| 105 | ndn::security::MakeCertificateOptions opts; |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 106 | opts.issuerId = ndn::name::Component::fromUri("parent"); |
Davide Pesavento | 8de8a8b | 2022-05-12 01:26:43 -0400 | [diff] [blame] | 107 | m_keyChain.makeCertificate(request, ndn::signingByIdentity(issuer), opts); |
| 108 | |
| 109 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 110 | |
| 111 | return subIdentity; |
| 112 | } |
| 113 | |
Davide Pesavento | 288141a | 2024-02-13 17:30:35 -0500 | [diff] [blame] | 114 | } // namespace nlsr::tests |