blob: fefc7cf01dbf2507cc91172f61db84ba752aba07 [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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 Pesaventod8e0cad2021-05-26 21:43:47 -040026#include <boost/filesystem/operations.hpp>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050027
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040028namespace ndn::tests {
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050029
30using namespace ndn::security;
31
32KeyChainFixture::KeyChainFixture()
33 : m_keyChain("pib-memory:", "tpm-memory:")
34{
35}
36
37KeyChainFixture::~KeyChainFixture()
38{
39 boost::system::error_code ec;
40 for (const auto& certFile : m_certFiles) {
41 boost::filesystem::remove(certFile, ec); // ignore error
42 }
43}
44
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050045bool
46KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
47{
48 m_certFiles.push_back(filename);
49 try {
50 ndn::io::save(cert, filename);
51 return true;
52 }
53 catch (const ndn::io::Error&) {
54 return false;
55 }
56}
57
58bool
59KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
60{
61 Certificate cert;
62 try {
63 cert = identity.getDefaultKey().getDefaultCertificate();
64 }
65 catch (const Pib::Error&) {
66 return false;
67 }
68
69 return saveCert(cert, filename);
70}
71
72bool
73KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
74 bool allowCreate)
75{
76 Identity id;
77 try {
78 id = m_keyChain.getPib().getIdentity(identityName);
79 }
80 catch (const Pib::Error&) {
81 if (allowCreate) {
82 id = m_keyChain.createIdentity(identityName);
83 }
84 }
85
86 if (!id) {
87 return false;
88 }
89
90 return saveIdentityCert(id, filename);
91}
92
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040093} // namespace ndn::tests