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