blob: b4097d2649d0ad85f1302703c25e336c350bb509 [file] [log] [blame]
Davide Pesavento013de9b2016-09-01 12:06:56 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev28181ee2020-06-03 13:58:47 -04002/*
3 * Copyright (c) 2014-2020, Regents of the University of California.
Davide Pesavento013de9b2016-09-01 12:06:56 +00004 *
5 * This file is part of ndn-tools (Named Data Networking Essential Tools).
6 * See AUTHORS.md for complete list of ndn-tools authors and contributors.
7 *
8 * ndn-tools 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 * ndn-tools 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 * ndn-tools, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity-management-fixture.hpp"
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040021
22#include <ndn-cxx/security/certificate.hpp>
Junxiao Shi20b22972017-05-24 21:04:16 +000023#include <ndn-cxx/security/pib/identity.hpp>
24#include <ndn-cxx/security/pib/key.hpp>
25#include <ndn-cxx/security/pib/pib.hpp>
Davide Pesavento013de9b2016-09-01 12:06:56 +000026#include <ndn-cxx/util/io.hpp>
Alexander Afanasyev28181ee2020-06-03 13:58:47 -040027
Davide Pesavento013de9b2016-09-01 12:06:56 +000028#include <boost/filesystem.hpp>
29
30namespace ndn {
31namespace tests {
32
33IdentityManagementFixture::IdentityManagementFixture()
Junxiao Shi20b22972017-05-24 21:04:16 +000034 : m_keyChain("pib-memory:", "tpm-memory:")
Davide Pesavento013de9b2016-09-01 12:06:56 +000035{
36}
37
38IdentityManagementFixture::~IdentityManagementFixture()
39{
Junxiao Shi20b22972017-05-24 21:04:16 +000040 boost::system::error_code ec;
Davide Pesavento013de9b2016-09-01 12:06:56 +000041 for (const auto& certFile : m_certFiles) {
42 boost::filesystem::remove(certFile, ec);
43 }
44}
45
46bool
47IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
48{
49 try {
50 m_keyChain.createIdentity(identity, params);
Davide Pesavento013de9b2016-09-01 12:06:56 +000051 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 Afanasyev28181ee2020-06-03 13:58:47 -040061 security::Certificate cert;
Davide Pesavento013de9b2016-09-01 12:06:56 +000062 try {
Junxiao Shi20b22972017-05-24 21:04:16 +000063 cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
Davide Pesavento013de9b2016-09-01 12:06:56 +000064 }
Junxiao Shi20b22972017-05-24 21:04:16 +000065 catch (const security::Pib::Error&) {
Davide Pesavento013de9b2016-09-01 12:06:56 +000066 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 {
Junxiao Shi20b22972017-05-24 21:04:16 +000074 io::save(cert, filename);
Davide Pesavento013de9b2016-09-01 12:06:56 +000075 return true;
76 }
77 catch (const io::Error&) {
78 return false;
79 }
80}
81
82} // namespace tests
83} // namespace ndn