blob: 7bbcf6ae7279cdc352377c1043056ffb67aefb25 [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa3d809e2022-02-06 11:55:02 -05003 * Copyright (c) 2013-2022 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
28namespace ndn {
29namespace tests {
30
31using namespace ndn::security;
32
33KeyChainFixture::KeyChainFixture()
34 : m_keyChain("pib-memory:", "tpm-memory:")
35{
36}
37
38KeyChainFixture::~KeyChainFixture()
39{
40 boost::system::error_code ec;
41 for (const auto& certFile : m_certFiles) {
42 boost::filesystem::remove(certFile, ec); // ignore error
43 }
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
94} // namespace tests
95} // namespace ndn