blob: f29153f2d7ac66fd957018bace1598e7c512fe11 [file] [log] [blame]
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento288141a2024-02-13 17:30:35 -05003 * Copyright (c) 2014-2024, Regents of the University of California,
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04004 * 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 Pesavento288141a2024-02-13 17:30:35 -050032namespace nlsr::tests {
Davide Pesavento8de8a8b2022-05-12 01:26:43 -040033
34using namespace ndn::security;
35
36KeyChainFixture::KeyChainFixture()
37 : m_keyChain("pib-memory:", "tpm-memory:")
38{
39}
40
41KeyChainFixture::~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
49bool
50KeyChainFixture::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
62bool
63KeyChainFixture::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
76bool
77KeyChainFixture::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
97Identity
98KeyChainFixture::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 Pesavento288141a2024-02-13 17:30:35 -0500106 opts.issuerId = ndn::name::Component::fromUri("parent");
Davide Pesavento8de8a8b2022-05-12 01:26:43 -0400107 m_keyChain.makeCertificate(request, ndn::signingByIdentity(issuer), opts);
108
109 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
110
111 return subIdentity;
112}
113
Davide Pesavento288141a2024-02-13 17:30:35 -0500114} // namespace nlsr::tests