blob: 171216f08f834a7bb8fe060ed255bb828da355dd [file] [log] [blame]
Davide Pesavento013de9b2016-09-01 12:06:56 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shi20b22972017-05-24 21:04:16 +00003 * Copyright (c) 2014-2017, 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"
Junxiao Shi20b22972017-05-24 21:04:16 +000021#include <ndn-cxx/security/pib/identity.hpp>
22#include <ndn-cxx/security/pib/key.hpp>
23#include <ndn-cxx/security/pib/pib.hpp>
24#include <ndn-cxx/security/v2/certificate.hpp>
Davide Pesavento013de9b2016-09-01 12:06:56 +000025#include <ndn-cxx/util/io.hpp>
26#include <boost/filesystem.hpp>
27
28namespace ndn {
29namespace tests {
30
31IdentityManagementFixture::IdentityManagementFixture()
Junxiao Shi20b22972017-05-24 21:04:16 +000032 : m_keyChain("pib-memory:", "tpm-memory:")
Davide Pesavento013de9b2016-09-01 12:06:56 +000033{
34}
35
36IdentityManagementFixture::~IdentityManagementFixture()
37{
Junxiao Shi20b22972017-05-24 21:04:16 +000038 boost::system::error_code ec;
Davide Pesavento013de9b2016-09-01 12:06:56 +000039 for (const auto& certFile : m_certFiles) {
40 boost::filesystem::remove(certFile, ec);
41 }
42}
43
44bool
45IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
46{
47 try {
48 m_keyChain.createIdentity(identity, params);
Davide Pesavento013de9b2016-09-01 12:06:56 +000049 return true;
50 }
51 catch (const std::runtime_error&) {
52 return false;
53 }
54}
55
56bool
57IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd)
58{
Junxiao Shi20b22972017-05-24 21:04:16 +000059 security::v2::Certificate cert;
Davide Pesavento013de9b2016-09-01 12:06:56 +000060 try {
Junxiao Shi20b22972017-05-24 21:04:16 +000061 cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate();
Davide Pesavento013de9b2016-09-01 12:06:56 +000062 }
Junxiao Shi20b22972017-05-24 21:04:16 +000063 catch (const security::Pib::Error&) {
Davide Pesavento013de9b2016-09-01 12:06:56 +000064 if (wantAdd && this->addIdentity(identity)) {
65 return this->saveIdentityCertificate(identity, filename, false);
66 }
67 return false;
68 }
69
70 m_certFiles.push_back(filename);
71 try {
Junxiao Shi20b22972017-05-24 21:04:16 +000072 io::save(cert, filename);
Davide Pesavento013de9b2016-09-01 12:06:56 +000073 return true;
74 }
75 catch (const io::Error&) {
76 return false;
77 }
78}
79
80} // namespace tests
81} // namespace ndn