blob: 9c5c17a2b2d2c85704e23c4f27a65581e827d83d [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento51974f62024-12-21 20:42:45 -05003 * Copyright (c) 2013-2024 Regents of the University of California.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library 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 Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "tests/key-chain-fixture.hpp"
23
24#include "ndn-cxx/util/io.hpp"
25
Davide Pesavento51974f62024-12-21 20:42:45 -050026#include <filesystem>
27#include <system_error>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050028
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050030
31using namespace ndn::security;
32
33KeyChainFixture::KeyChainFixture()
34 : m_keyChain("pib-memory:", "tpm-memory:")
35{
36}
37
38KeyChainFixture::~KeyChainFixture()
39{
Davide Pesavento51974f62024-12-21 20:42:45 -050040 std::error_code ec;
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050041 for (const auto& certFile : m_certFiles) {
Davide Pesavento51974f62024-12-21 20:42:45 -050042 std::filesystem::remove(certFile, ec); // ignore error
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050043 }
44}
45
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050046bool
47KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
48{
49 m_certFiles.push_back(filename);
50 try {
51 ndn::io::save(cert, filename);
52 return true;
53 }
54 catch (const ndn::io::Error&) {
55 return false;
56 }
57}
58
59bool
60KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
61{
62 Certificate cert;
63 try {
64 cert = identity.getDefaultKey().getDefaultCertificate();
65 }
66 catch (const Pib::Error&) {
67 return false;
68 }
69
70 return saveCert(cert, filename);
71}
72
73bool
74KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
75 bool allowCreate)
76{
77 Identity id;
78 try {
79 id = m_keyChain.getPib().getIdentity(identityName);
80 }
81 catch (const Pib::Error&) {
82 if (allowCreate) {
83 id = m_keyChain.createIdentity(identityName);
84 }
85 }
86
87 if (!id) {
88 return false;
89 }
90
91 return saveIdentityCert(id, filename);
92}
93
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040094} // namespace ndn::tests