blob: 73e27c4430fec860c218613bf1acea7eb7e7beea [file] [log] [blame]
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
Davide Pesaventoa3d809e2022-02-06 11:55:02 -05003 * Copyright (c) 2013-2022 Regents of the University of California.
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -05004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "tests/key-chain-fixture.hpp"
23
24#include "ndn-cxx/util/io.hpp"
25
Davide Pesaventod8e0cad2021-05-26 21:43:47 -040026#include <boost/filesystem/operations.hpp>
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050027
28namespace ndn {
29namespace tests {
30
31using namespace ndn::security;
32
33KeyChainFixture::KeyChainFixture()
34 : m_keyChain("pib-memory:", "tpm-memory:")
35{
36}
37
38KeyChainFixture::~KeyChainFixture()
39{
40 boost::system::error_code ec;
41 for (const auto& certFile : m_certFiles) {
42 boost::filesystem::remove(certFile, ec); // ignore error
43 }
44}
45
46Certificate
47KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey)
48{
49 Certificate cert;
50 cert.setName(Name(key.getName())
51 .append(issuer)
52 .appendVersion());
53
54 // set metainfo
55 cert.setContentType(tlv::ContentType_Key);
56 cert.setFreshnessPeriod(1_h);
57
58 // set content
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050059 cert.setContent(key.getPublicKey());
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050060
61 // set signature info
62 ndn::SignatureInfo info;
63 auto now = time::system_clock::now();
64 info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days));
65
66 m_keyChain.sign(cert, signingByKey(signingKey ? signingKey : key).setSignatureInfo(info));
67 return cert;
68}
69
70bool
71KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
72{
73 m_certFiles.push_back(filename);
74 try {
75 ndn::io::save(cert, filename);
76 return true;
77 }
78 catch (const ndn::io::Error&) {
79 return false;
80 }
81}
82
83bool
84KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
85{
86 Certificate cert;
87 try {
88 cert = identity.getDefaultKey().getDefaultCertificate();
89 }
90 catch (const Pib::Error&) {
91 return false;
92 }
93
94 return saveCert(cert, filename);
95}
96
97bool
98KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
99 bool allowCreate)
100{
101 Identity id;
102 try {
103 id = m_keyChain.getPib().getIdentity(identityName);
104 }
105 catch (const Pib::Error&) {
106 if (allowCreate) {
107 id = m_keyChain.createIdentity(identityName);
108 }
109 }
110
111 if (!id) {
112 return false;
113 }
114
115 return saveIdentityCert(id, filename);
116}
117
118} // namespace tests
119} // namespace ndn