Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /* |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California. |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [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 "tests/key-chain-fixture.hpp" |
| 21 | |
| 22 | #include <ndn-cxx/util/io.hpp> |
| 23 | |
| 24 | #include <boost/filesystem.hpp> |
| 25 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 26 | namespace ndn::tests { |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 27 | |
| 28 | using namespace ndn::security; |
| 29 | |
| 30 | KeyChainFixture::KeyChainFixture() |
| 31 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | KeyChainFixture::~KeyChainFixture() |
| 36 | { |
| 37 | boost::system::error_code ec; |
| 38 | for (const auto& certFile : m_certFiles) { |
| 39 | boost::filesystem::remove(certFile, ec); // ignore error |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | Certificate |
| 44 | KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey) |
| 45 | { |
| 46 | Certificate cert; |
| 47 | cert.setName(Name(key.getName()) |
| 48 | .append(issuer) |
| 49 | .appendVersion()); |
| 50 | |
| 51 | // set metainfo |
| 52 | cert.setContentType(tlv::ContentType_Key); |
| 53 | cert.setFreshnessPeriod(1_h); |
| 54 | |
| 55 | // set content |
Davide Pesavento | 5998428 | 2022-02-16 22:41:03 -0500 | [diff] [blame] | 56 | cert.setContent(key.getPublicKey()); |
Davide Pesavento | 6677762 | 2020-10-09 18:46:03 -0400 | [diff] [blame] | 57 | |
| 58 | // set signature info |
| 59 | ndn::SignatureInfo info; |
| 60 | auto now = time::system_clock::now(); |
| 61 | info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days)); |
| 62 | |
| 63 | m_keyChain.sign(cert, signingByKey(signingKey ? signingKey : key).setSignatureInfo(info)); |
| 64 | return cert; |
| 65 | } |
| 66 | |
| 67 | bool |
| 68 | KeyChainFixture::saveCert(const Data& cert, const std::string& filename) |
| 69 | { |
| 70 | m_certFiles.push_back(filename); |
| 71 | try { |
| 72 | ndn::io::save(cert, filename); |
| 73 | return true; |
| 74 | } |
| 75 | catch (const ndn::io::Error&) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename) |
| 82 | { |
| 83 | Certificate cert; |
| 84 | try { |
| 85 | cert = identity.getDefaultKey().getDefaultCertificate(); |
| 86 | } |
| 87 | catch (const Pib::Error&) { |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | return saveCert(cert, filename); |
| 92 | } |
| 93 | |
| 94 | bool |
| 95 | KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename, |
| 96 | bool allowCreate) |
| 97 | { |
| 98 | Identity id; |
| 99 | try { |
| 100 | id = m_keyChain.getPib().getIdentity(identityName); |
| 101 | } |
| 102 | catch (const Pib::Error&) { |
| 103 | if (allowCreate) { |
| 104 | id = m_keyChain.createIdentity(identityName); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | if (!id) { |
| 109 | return false; |
| 110 | } |
| 111 | |
| 112 | return saveIdentityCert(id, filename); |
| 113 | } |
| 114 | |
Davide Pesavento | b3570c6 | 2022-02-19 19:19:00 -0500 | [diff] [blame^] | 115 | } // namespace ndn::tests |