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 | /* |
Alexander Afanasyev | a158370 | 2020-06-03 13:55:45 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2020, 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 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 30 | #include <boost/filesystem.hpp> |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 31 | |
| 32 | namespace nfd { |
| 33 | namespace tests { |
| 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 | { |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 44 | boost::system::error_code ec; |
| 45 | for (const auto& certFile : m_certFiles) { |
| 46 | boost::filesystem::remove(certFile, ec); // ignore error |
| 47 | } |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 50 | Certificate |
| 51 | KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey) |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 52 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 53 | Certificate cert; |
| 54 | cert.setName(Name(key.getName()) |
| 55 | .append(issuer) |
| 56 | .appendVersion()); |
| 57 | |
| 58 | // set metainfo |
| 59 | cert.setContentType(tlv::ContentType_Key); |
| 60 | cert.setFreshnessPeriod(1_h); |
| 61 | |
| 62 | // set content |
| 63 | cert.setContent(key.getPublicKey().data(), key.getPublicKey().size()); |
| 64 | |
| 65 | // set signature info |
| 66 | ndn::SignatureInfo info; |
| 67 | auto now = time::system_clock::now(); |
| 68 | info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days)); |
| 69 | |
| 70 | m_keyChain.sign(cert, signingByKey(signingKey ? signingKey : key).setSignatureInfo(info)); |
| 71 | return cert; |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 74 | bool |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 75 | KeyChainFixture::saveCert(const Data& cert, const std::string& filename) |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 76 | { |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 77 | m_certFiles.push_back(filename); |
| 78 | try { |
Junxiao Shi | 16a3adf | 2017-05-26 17:38:51 +0000 | [diff] [blame] | 79 | ndn::io::save(cert, filename); |
Junxiao Shi | d763127 | 2016-08-17 04:16:31 +0000 | [diff] [blame] | 80 | return true; |
| 81 | } |
| 82 | catch (const ndn::io::Error&) { |
| 83 | return false; |
| 84 | } |
| 85 | } |
| 86 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 87 | bool |
| 88 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 89 | { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 90 | Certificate cert; |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 91 | try { |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 92 | cert = identity.getDefaultKey().getDefaultCertificate(); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 93 | } |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 94 | catch (const Pib::Error&) { |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | return saveCert(cert, filename); |
| 99 | } |
| 100 | |
| 101 | bool |
| 102 | KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename, |
| 103 | bool allowCreate) |
| 104 | { |
| 105 | Identity id; |
| 106 | try { |
| 107 | id = m_keyChain.getPib().getIdentity(identityName); |
| 108 | } |
| 109 | catch (const Pib::Error&) { |
| 110 | if (allowCreate) { |
| 111 | id = m_keyChain.createIdentity(identityName); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 112 | } |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 115 | if (!id) { |
| 116 | return false; |
| 117 | } |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 118 | |
Davide Pesavento | 2135375 | 2020-11-20 00:43:44 -0500 | [diff] [blame^] | 119 | return saveIdentityCert(id, filename); |
Junxiao Shi | 5ba7dfc | 2018-09-26 14:24:05 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Yanbiao Li | c17de83 | 2014-11-21 17:51:45 -0800 | [diff] [blame] | 122 | } // namespace tests |
| 123 | } // namespace nfd |