Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 152874a | 2024-02-20 22:07:07 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 10 | * |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 11 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 12 | * See AUTHORS.md for complete list of NFD authors and contributors. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 13 | * |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 14 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 15 | * of the GNU General Public License as published by the Free Software Foundation, |
| 16 | * either version 3 of the License, or (at your option) any later version. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 17 | * |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 18 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 19 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 20 | * PURPOSE. See the GNU General Public License for more details. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 21 | * |
Alexander Afanasyev | bc9ed49 | 2016-01-26 11:38:11 -0800 | [diff] [blame] | 22 | * You should have received a copy of the GNU General Public License along with |
| 23 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 26 | #include "tests/key-chain-fixture.hpp" |
| 27 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 28 | #include <ndn-cxx/util/io.hpp> |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 29 | |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame^] | 30 | #include <filesystem> |
| 31 | #include <system_error> |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::tests { |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 34 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 35 | using namespace ndn::security; |
| 36 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 37 | KeyChainFixture::KeyChainFixture() |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 38 | : m_keyChain("pib-memory:", "tpm-memory:") |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 39 | { |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Davide Pesavento | 1d12d2f | 2019-03-22 12:44:14 -0400 | [diff] [blame] | 42 | KeyChainFixture::~KeyChainFixture() |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 43 | { |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame^] | 44 | std::error_code ec; |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 45 | for (const auto& certFile : m_certFiles) { |
Davide Pesavento | 401d1a4 | 2024-12-19 21:10:22 -0500 | [diff] [blame^] | 46 | std::filesystem::remove(certFile, ec); // ignore error |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 47 | } |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 50 | bool |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 51 | KeyChainFixture::saveCert(const Data& cert, const std::string& filename) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 52 | { |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 53 | m_certFiles.push_back(filename); |
| 54 | try { |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 55 | ndn::io::save(cert, filename); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 56 | return true; |
| 57 | } |
| 58 | catch (const ndn::io::Error&) { |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 63 | bool |
| 64 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 65 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 66 | Certificate cert; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 67 | try { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 68 | cert = identity.getDefaultKey().getDefaultCertificate(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 69 | } |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 70 | catch (const Pib::Error&) { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return saveCert(cert, filename); |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename, |
| 79 | bool allowCreate) |
| 80 | { |
| 81 | Identity id; |
| 82 | try { |
| 83 | id = m_keyChain.getPib().getIdentity(identityName); |
| 84 | } |
| 85 | catch (const Pib::Error&) { |
| 86 | if (allowCreate) { |
| 87 | id = m_keyChain.createIdentity(identityName); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 88 | } |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 91 | if (!id) { |
| 92 | return false; |
| 93 | } |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 94 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame] | 95 | return saveIdentityCert(id, filename); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 98 | } // namespace nfd::tests |