Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame^] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California. |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 4 | * |
| 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 Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame^] | 21 | |
| 22 | #include <ndn-cxx/security/certificate.hpp> |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 23 | #include <ndn-cxx/security/pib/identity.hpp> |
| 24 | #include <ndn-cxx/security/pib/key.hpp> |
| 25 | #include <ndn-cxx/security/pib/pib.hpp> |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 26 | #include <ndn-cxx/util/io.hpp> |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame^] | 27 | |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 28 | #include <boost/filesystem.hpp> |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace tests { |
| 32 | |
| 33 | IdentityManagementFixture::IdentityManagementFixture() |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 34 | : m_keyChain("pib-memory:", "tpm-memory:") |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 35 | { |
| 36 | } |
| 37 | |
| 38 | IdentityManagementFixture::~IdentityManagementFixture() |
| 39 | { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 40 | boost::system::error_code ec; |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 41 | for (const auto& certFile : m_certFiles) { |
| 42 | boost::filesystem::remove(certFile, ec); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool |
| 47 | IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params) |
| 48 | { |
| 49 | try { |
| 50 | m_keyChain.createIdentity(identity, params); |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 51 | return true; |
| 52 | } |
| 53 | catch (const std::runtime_error&) { |
| 54 | return false; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | bool |
| 59 | IdentityManagementFixture::saveIdentityCertificate(const Name& identity, const std::string& filename, bool wantAdd) |
| 60 | { |
Alexander Afanasyev | 28181ee | 2020-06-03 13:58:47 -0400 | [diff] [blame^] | 61 | security::Certificate cert; |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 62 | try { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 63 | cert = m_keyChain.getPib().getIdentity(identity).getDefaultKey().getDefaultCertificate(); |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 64 | } |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 65 | catch (const security::Pib::Error&) { |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 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 { |
Junxiao Shi | 20b2297 | 2017-05-24 21:04:16 +0000 | [diff] [blame] | 74 | io::save(cert, filename); |
Davide Pesavento | 013de9b | 2016-09-01 12:06:56 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | catch (const io::Error&) { |
| 78 | return false; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | } // namespace tests |
| 83 | } // namespace ndn |