Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 15f67df | 2020-06-03 14:22:24 -0400 | [diff] [blame^] | 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 15f67df | 2020-06-03 14:22:24 -0400 | [diff] [blame^] | 24 | #include <ndn-cxx/security/certificate.hpp> |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 25 | #include <ndn-cxx/util/io.hpp> |
| 26 | #include <boost/filesystem.hpp> |
| 27 | |
| 28 | namespace repo { |
| 29 | namespace tests { |
| 30 | |
| 31 | IdentityManagementFixture::IdentityManagementFixture() |
| 32 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 33 | { |
| 34 | m_keyChain.createIdentity("/DEFAULT"); |
| 35 | } |
| 36 | |
| 37 | IdentityManagementFixture::~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 | |
| 45 | bool |
| 46 | IdentityManagementFixture::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 | |
| 57 | bool |
| 58 | IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd) |
| 59 | { |
Alexander Afanasyev | 15f67df | 2020-06-03 14:22:24 -0400 | [diff] [blame^] | 60 | ndn::security::Certificate cert; |
Junxiao Shi | 047a6fb | 2017-06-08 16:16:05 +0000 | [diff] [blame] | 61 | 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 |