blob: 1a9b43bb3826b330fe161e428d4ace3841155e87 [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 Pesavento152874a2024-02-20 22:07:07 -05003 * Copyright (c) 2014-2024, 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
Davide Pesavento401d1a42024-12-19 21:10:22 -050030#include <filesystem>
31#include <system_error>
Yanbiao Lic17de832014-11-21 17:51:45 -080032
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040033namespace nfd::tests {
Yanbiao Lic17de832014-11-21 17:51:45 -080034
Davide Pesavento21353752020-11-20 00:43:44 -050035using namespace ndn::security;
36
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040037KeyChainFixture::KeyChainFixture()
Junxiao Shi16a3adf2017-05-26 17:38:51 +000038 : m_keyChain("pib-memory:", "tpm-memory:")
Yanbiao Lic17de832014-11-21 17:51:45 -080039{
Yanbiao Lic17de832014-11-21 17:51:45 -080040}
41
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040042KeyChainFixture::~KeyChainFixture()
Yanbiao Lic17de832014-11-21 17:51:45 -080043{
Davide Pesavento401d1a42024-12-19 21:10:22 -050044 std::error_code ec;
Junxiao Shid7631272016-08-17 04:16:31 +000045 for (const auto& certFile : m_certFiles) {
Davide Pesavento401d1a42024-12-19 21:10:22 -050046 std::filesystem::remove(certFile, ec); // ignore error
Junxiao Shid7631272016-08-17 04:16:31 +000047 }
Yanbiao Lic17de832014-11-21 17:51:45 -080048}
49
Junxiao Shid7631272016-08-17 04:16:31 +000050bool
Davide Pesavento21353752020-11-20 00:43:44 -050051KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
Junxiao Shid7631272016-08-17 04:16:31 +000052{
Junxiao Shid7631272016-08-17 04:16:31 +000053 m_certFiles.push_back(filename);
54 try {
Junxiao Shi16a3adf2017-05-26 17:38:51 +000055 ndn::io::save(cert, filename);
Junxiao Shid7631272016-08-17 04:16:31 +000056 return true;
57 }
58 catch (const ndn::io::Error&) {
59 return false;
60 }
61}
62
Davide Pesavento21353752020-11-20 00:43:44 -050063bool
64KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000065{
Davide Pesavento21353752020-11-20 00:43:44 -050066 Certificate cert;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000067 try {
Davide Pesavento21353752020-11-20 00:43:44 -050068 cert = identity.getDefaultKey().getDefaultCertificate();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000069 }
Davide Pesavento21353752020-11-20 00:43:44 -050070 catch (const Pib::Error&) {
71 return false;
72 }
73
74 return saveCert(cert, filename);
75}
76
77bool
78KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
79 bool allowCreate)
80{
81 Identity id;
82 try {
83 id = m_keyChain.getPib().getIdentity(identityName);
84 }
85 catch (const Pib::Error&) {
86 if (allowCreate) {
87 id = m_keyChain.createIdentity(identityName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000088 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000089 }
90
Davide Pesavento21353752020-11-20 00:43:44 -050091 if (!id) {
92 return false;
93 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000094
Davide Pesavento21353752020-11-20 00:43:44 -050095 return saveIdentityCert(id, filename);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000096}
97
Davide Pesaventoe422f9e2022-06-03 01:30:23 -040098} // namespace nfd::tests