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