blob: 21a6b9f7f9f4601f9d06f438d275d685947951a6 [file] [log] [blame]
Yanbiao Lic17de832014-11-21 17:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +00002/*
Davide Pesaventoa599d2a2022-02-16 18:52:43 -05003 * Copyright (c) 2014-2022, Regents of the University of California,
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Yanbiao Lic17de832014-11-21 17:51:45 -080010 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
Yanbiao Lic17de832014-11-21 17:51:45 -080013 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080014 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
Yanbiao Lic17de832014-11-21 17:51:45 -080017 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080018 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
Yanbiao Lic17de832014-11-21 17:51:45 -080021 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080022 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yanbiao Lic17de832014-11-21 17:51:45 -080024 */
25
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040026#include "tests/key-chain-fixture.hpp"
27
Junxiao Shid7631272016-08-17 04:16:31 +000028#include <ndn-cxx/util/io.hpp>
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029
Junxiao Shid7631272016-08-17 04:16:31 +000030#include <boost/filesystem.hpp>
Yanbiao Lic17de832014-11-21 17:51:45 -080031
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040032namespace nfd::tests {
Yanbiao Lic17de832014-11-21 17:51:45 -080033
Davide Pesavento21353752020-11-20 00:43:44 -050034using namespace ndn::security;
35
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040036KeyChainFixture::KeyChainFixture()
Junxiao Shi16a3adf2017-05-26 17:38:51 +000037 : m_keyChain("pib-memory:", "tpm-memory:")
Yanbiao Lic17de832014-11-21 17:51:45 -080038{
Yanbiao Lic17de832014-11-21 17:51:45 -080039}
40
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040041KeyChainFixture::~KeyChainFixture()
Yanbiao Lic17de832014-11-21 17:51:45 -080042{
Junxiao Shid7631272016-08-17 04:16:31 +000043 boost::system::error_code ec;
44 for (const auto& certFile : m_certFiles) {
45 boost::filesystem::remove(certFile, ec); // ignore error
46 }
Yanbiao Lic17de832014-11-21 17:51:45 -080047}
48
Junxiao Shid7631272016-08-17 04:16:31 +000049bool
Davide Pesavento21353752020-11-20 00:43:44 -050050KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
Junxiao Shid7631272016-08-17 04:16:31 +000051{
Junxiao Shid7631272016-08-17 04:16:31 +000052 m_certFiles.push_back(filename);
53 try {
Junxiao Shi16a3adf2017-05-26 17:38:51 +000054 ndn::io::save(cert, filename);
Junxiao Shid7631272016-08-17 04:16:31 +000055 return true;
56 }
57 catch (const ndn::io::Error&) {
58 return false;
59 }
60}
61
Davide Pesavento21353752020-11-20 00:43:44 -050062bool
63KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000064{
Davide Pesavento21353752020-11-20 00:43:44 -050065 Certificate cert;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000066 try {
Davide Pesavento21353752020-11-20 00:43:44 -050067 cert = identity.getDefaultKey().getDefaultCertificate();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000068 }
Davide Pesavento21353752020-11-20 00:43:44 -050069 catch (const Pib::Error&) {
70 return false;
71 }
72
73 return saveCert(cert, filename);
74}
75
76bool
77KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
78 bool allowCreate)
79{
80 Identity id;
81 try {
82 id = m_keyChain.getPib().getIdentity(identityName);
83 }
84 catch (const Pib::Error&) {
85 if (allowCreate) {
86 id = m_keyChain.createIdentity(identityName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000087 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000088 }
89
Davide Pesavento21353752020-11-20 00:43:44 -050090 if (!id) {
91 return false;
92 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000093
Davide Pesavento21353752020-11-20 00:43:44 -050094 return saveIdentityCert(id, filename);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000095}
96
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040097} // namespace nfd::tests