blob: af62de513f2e29effcfc352e01ce01a494964062 [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
Junxiao Shi7d728682022-04-01 01:21:13 +000047KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey,
48 optional<KeyLocator> keyLocator)
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050049{
Junxiao Shi7d728682022-04-01 01:21:13 +000050 const Key& signer = signingKey ? signingKey : key;
51
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050052 Certificate cert;
53 cert.setName(Name(key.getName())
54 .append(issuer)
55 .appendVersion());
56
57 // set metainfo
58 cert.setContentType(tlv::ContentType_Key);
59 cert.setFreshnessPeriod(1_h);
60
61 // set content
Davide Pesaventoa3d809e2022-02-06 11:55:02 -050062 cert.setContent(key.getPublicKey());
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050063
64 // set signature info
65 ndn::SignatureInfo info;
66 auto now = time::system_clock::now();
67 info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days));
Junxiao Shi7d728682022-04-01 01:21:13 +000068 if (keyLocator) {
69 info.setKeyLocator(*keyLocator);
70 }
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050071
Junxiao Shi7d728682022-04-01 01:21:13 +000072 m_keyChain.sign(cert, signingByKey(signer).setSignatureInfo(info));
Davide Pesavento4c1ad4c2020-11-16 21:12:02 -050073 return cert;
74}
75
76bool
77KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
78{
79 m_certFiles.push_back(filename);
80 try {
81 ndn::io::save(cert, filename);
82 return true;
83 }
84 catch (const ndn::io::Error&) {
85 return false;
86 }
87}
88
89bool
90KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
91{
92 Certificate cert;
93 try {
94 cert = identity.getDefaultKey().getDefaultCertificate();
95 }
96 catch (const Pib::Error&) {
97 return false;
98 }
99
100 return saveCert(cert, filename);
101}
102
103bool
104KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
105 bool allowCreate)
106{
107 Identity id;
108 try {
109 id = m_keyChain.getPib().getIdentity(identityName);
110 }
111 catch (const Pib::Error&) {
112 if (allowCreate) {
113 id = m_keyChain.createIdentity(identityName);
114 }
115 }
116
117 if (!id) {
118 return false;
119 }
120
121 return saveIdentityCert(id, filename);
122}
123
124} // namespace tests
125} // namespace ndn