blob: 106797b6b392d60c587b5798e1e85db665747c3d [file] [log] [blame]
Junxiao Shi047a6fb2017-06-08 16:16:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento11904062022-04-14 22:33:28 -04002/*
Davide Pesavento01a2a8c2024-12-13 14:25:50 -05003 * Copyright (c) 2014-2024, Regents of the University of California.
Junxiao Shi047a6fb2017-06-08 16:16:05 +00004 *
5 * This file is part of NDN repo-ng (Next generation of NDN repository).
6 * See AUTHORS.md for complete list of repo-ng authors and contributors.
7 *
8 * repo-ng 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 * repo-ng 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 * repo-ng, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity-management-fixture.hpp"
Davide Pesavento11904062022-04-14 22:33:28 -040021
Junxiao Shi047a6fb2017-06-08 16:16:05 +000022#include <ndn-cxx/security/pib/identity.hpp>
23#include <ndn-cxx/security/pib/key.hpp>
24#include <ndn-cxx/security/pib/pib.hpp>
Alexander Afanasyev15f67df2020-06-03 14:22:24 -040025#include <ndn-cxx/security/certificate.hpp>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000026#include <ndn-cxx/util/io.hpp>
Davide Pesavento11904062022-04-14 22:33:28 -040027
Davide Pesavento01a2a8c2024-12-13 14:25:50 -050028#include <filesystem>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000029
Davide Pesavento11904062022-04-14 22:33:28 -040030namespace repo::tests {
Junxiao Shi047a6fb2017-06-08 16:16:05 +000031
32IdentityManagementFixture::IdentityManagementFixture()
33 : m_keyChain("pib-memory:", "tpm-memory:")
34{
35 m_keyChain.createIdentity("/DEFAULT");
36}
37
38IdentityManagementFixture::~IdentityManagementFixture()
39{
Davide Pesavento01a2a8c2024-12-13 14:25:50 -050040 std::error_code ec;
Junxiao Shi047a6fb2017-06-08 16:16:05 +000041 for (const auto& certFile : m_certFiles) {
Davide Pesavento01a2a8c2024-12-13 14:25:50 -050042 std::filesystem::remove(certFile, ec); // ignore error
Junxiao Shi047a6fb2017-06-08 16:16:05 +000043 }
44}
45
46bool
47IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
48{
49 try {
50 m_keyChain.createIdentity(identity, params);
51 return true;
52 }
53 catch (const std::runtime_error&) {
54 return false;
55 }
56}
57
58bool
59IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
60{
Alexander Afanasyev15f67df2020-06-03 14:22:24 -040061 ndn::security::Certificate cert;
Junxiao Shi047a6fb2017-06-08 16:16:05 +000062 try {
63 cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
64 }
65 catch (const ndn::security::Pib::Error&) {
66 if (wantAdd && this->addIdentity(identity)) {
67 return this->saveIdentityCertificate(identity, filename, false);
68 }
69 return false;
70 }
71
72 m_certFiles.push_back(filename);
73 try {
74 ndn::io::save(cert, filename);
75 return true;
76 }
77 catch (const ndn::io::Error&) {
78 return false;
79 }
80}
81
Davide Pesavento11904062022-04-14 22:33:28 -040082} // namespace repo::tests