blob: 010716c9c57b8ba9c4c2a95c34b8b7a11db240fa [file] [log] [blame]
Davide Pesavento66777622020-10-09 18:46:03 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento59984282022-02-16 22:41:03 -05003 * Copyright (c) 2014-2022, Regents of the University of California.
Davide Pesavento66777622020-10-09 18:46:03 -04004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-tools is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "tests/key-chain-fixture.hpp"
21
22#include <ndn-cxx/util/io.hpp>
23
Davide Pesavento832d2302022-05-13 14:26:51 -040024#include <boost/filesystem/operations.hpp>
Davide Pesavento66777622020-10-09 18:46:03 -040025
Davide Pesaventob3570c62022-02-19 19:19:00 -050026namespace ndn::tests {
Davide Pesavento66777622020-10-09 18:46:03 -040027
28using namespace ndn::security;
29
30KeyChainFixture::KeyChainFixture()
31 : m_keyChain("pib-memory:", "tpm-memory:")
32{
33}
34
35KeyChainFixture::~KeyChainFixture()
36{
37 boost::system::error_code ec;
38 for (const auto& certFile : m_certFiles) {
39 boost::filesystem::remove(certFile, ec); // ignore error
40 }
41}
42
Davide Pesavento66777622020-10-09 18:46:03 -040043bool
44KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
45{
46 m_certFiles.push_back(filename);
47 try {
48 ndn::io::save(cert, filename);
49 return true;
50 }
51 catch (const ndn::io::Error&) {
52 return false;
53 }
54}
55
56bool
57KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
58{
59 Certificate cert;
60 try {
61 cert = identity.getDefaultKey().getDefaultCertificate();
62 }
63 catch (const Pib::Error&) {
64 return false;
65 }
66
67 return saveCert(cert, filename);
68}
69
70bool
71KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
72 bool allowCreate)
73{
74 Identity id;
75 try {
76 id = m_keyChain.getPib().getIdentity(identityName);
77 }
78 catch (const Pib::Error&) {
79 if (allowCreate) {
80 id = m_keyChain.createIdentity(identityName);
81 }
82 }
83
84 if (!id) {
85 return false;
86 }
87
88 return saveIdentityCert(id, filename);
89}
90
Davide Pesaventob3570c62022-02-19 19:19:00 -050091} // namespace ndn::tests