Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | a3d809e | 2022-02-06 11:55:02 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2022 Regents of the University of California. |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "tests/key-chain-fixture.hpp" |
| 23 | |
| 24 | #include "ndn-cxx/util/io.hpp" |
| 25 | |
Davide Pesavento | d8e0cad | 2021-05-26 21:43:47 -0400 | [diff] [blame] | 26 | #include <boost/filesystem/operations.hpp> |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | namespace tests { |
| 30 | |
| 31 | using namespace ndn::security; |
| 32 | |
| 33 | KeyChainFixture::KeyChainFixture() |
| 34 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 35 | { |
| 36 | } |
| 37 | |
| 38 | KeyChainFixture::~KeyChainFixture() |
| 39 | { |
| 40 | boost::system::error_code ec; |
| 41 | for (const auto& certFile : m_certFiles) { |
| 42 | boost::filesystem::remove(certFile, ec); // ignore error |
| 43 | } |
| 44 | } |
| 45 | |
Davide Pesavento | 4c1ad4c | 2020-11-16 21:12:02 -0500 | [diff] [blame] | 46 | bool |
| 47 | KeyChainFixture::saveCert(const Data& cert, const std::string& filename) |
| 48 | { |
| 49 | m_certFiles.push_back(filename); |
| 50 | try { |
| 51 | ndn::io::save(cert, filename); |
| 52 | return true; |
| 53 | } |
| 54 | catch (const ndn::io::Error&) { |
| 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | bool |
| 60 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
| 61 | { |
| 62 | Certificate cert; |
| 63 | try { |
| 64 | cert = identity.getDefaultKey().getDefaultCertificate(); |
| 65 | } |
| 66 | catch (const Pib::Error&) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | return saveCert(cert, filename); |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename, |
| 75 | bool allowCreate) |
| 76 | { |
| 77 | Identity id; |
| 78 | try { |
| 79 | id = m_keyChain.getPib().getIdentity(identityName); |
| 80 | } |
| 81 | catch (const Pib::Error&) { |
| 82 | if (allowCreate) { |
| 83 | id = m_keyChain.createIdentity(identityName); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (!id) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return saveIdentityCert(id, filename); |
| 92 | } |
| 93 | |
| 94 | } // namespace tests |
| 95 | } // namespace ndn |