blob: 681e076d92abcb2dd9da4dfe3f64d5f53915dd7a [file] [log] [blame]
Junxiao Shi047a6fb2017-06-08 16:16:05 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev15f67df2020-06-03 14:22:24 -04003 * Copyright (c) 2014-2020, 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"
21#include <ndn-cxx/security/pib/identity.hpp>
22#include <ndn-cxx/security/pib/key.hpp>
23#include <ndn-cxx/security/pib/pib.hpp>
Alexander Afanasyev15f67df2020-06-03 14:22:24 -040024#include <ndn-cxx/security/certificate.hpp>
Junxiao Shi047a6fb2017-06-08 16:16:05 +000025#include <ndn-cxx/util/io.hpp>
26#include <boost/filesystem.hpp>
27
28namespace repo {
29namespace tests {
30
31IdentityManagementFixture::IdentityManagementFixture()
32 : m_keyChain("pib-memory:", "tpm-memory:")
33{
34 m_keyChain.createIdentity("/DEFAULT");
35}
36
37IdentityManagementFixture::~IdentityManagementFixture()
38{
39 boost::system::error_code ec;
40 for (const auto& certFile : m_certFiles) {
41 boost::filesystem::remove(certFile, ec); // ignore error
42 }
43}
44
45bool
46IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params)
47{
48 try {
49 m_keyChain.createIdentity(identity, params);
50 return true;
51 }
52 catch (const std::runtime_error&) {
53 return false;
54 }
55}
56
57bool
58IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
59{
Alexander Afanasyev15f67df2020-06-03 14:22:24 -040060 ndn::security::Certificate cert;
Junxiao Shi047a6fb2017-06-08 16:16:05 +000061 try {
62 cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
63 }
64 catch (const ndn::security::Pib::Error&) {
65 if (wantAdd && this->addIdentity(identity)) {
66 return this->saveIdentityCertificate(identity, filename, false);
67 }
68 return false;
69 }
70
71 m_certFiles.push_back(filename);
72 try {
73 ndn::io::save(cert, filename);
74 return true;
75 }
76 catch (const ndn::io::Error&) {
77 return false;
78 }
79}
80
81} // namespace tests
82} // namespace repo