blob: 59098d781cf27ce009339688b63118dbf8915ff9 [file] [log] [blame]
Davide Pesavento66777622020-10-09 18:46:03 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesavento59984282022-02-16 22:41:03 -05003 * Copyright (c) 2014-2022, Regents of the University of California.
Davide Pesavento66777622020-10-09 18:46:03 -04004 *
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 Pesaventob3570c62022-02-19 19:19:00 -050026namespace ndn::tests {
Davide Pesavento66777622020-10-09 18:46:03 -040027
28using namespace ndn::security;
29
30KeyChainFixture::KeyChainFixture()
31 : m_keyChain("pib-memory:", "tpm-memory:")
32{
33}
34
35KeyChainFixture::~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
43Certificate
44KeyChainFixture::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 Pesavento59984282022-02-16 22:41:03 -050056 cert.setContent(key.getPublicKey());
Davide Pesavento66777622020-10-09 18:46:03 -040057
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
67bool
68KeyChainFixture::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
80bool
81KeyChainFixture::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
94bool
95KeyChainFixture::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 Pesaventob3570c62022-02-19 19:19:00 -0500115} // namespace ndn::tests