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