blob: 3db4c8a4579314421a2e1f16084cdcabfe475496 [file] [log] [blame]
Davide Pesaventoba3f6892020-12-08 22:18:35 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento714dba02022-03-17 20:46:28 -04003 * Copyright (c) 2014-2022, Regents of the University of California
Davide Pesaventoba3f6892020-12-08 22:18:35 -05004 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#include "tests/key-chain-fixture.hpp"
21
22#include <ndn-cxx/util/io.hpp>
23
24#include <boost/filesystem.hpp>
25
Davide Pesaventobde084f2022-04-17 00:21:35 -040026namespace ndn::nac::tests {
Davide Pesaventoba3f6892020-12-08 22:18:35 -050027
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 Pesaventoba3f6892020-12-08 22:18:35 -050043bool
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 Pesaventobde084f2022-04-17 00:21:35 -040091} // namespace ndn::nac::tests