blob: c71f1755b2af3d1f19e021228e8e5c0fb5546f10 [file] [log] [blame]
Yanbiao Lic17de832014-11-21 17:51:45 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +00002/*
Alexander Afanasyeva1583702020-06-03 13:55:45 -04003 * Copyright (c) 2014-2020, Regents of the University of California,
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Yanbiao Lic17de832014-11-21 17:51:45 -080010 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080011 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
Yanbiao Lic17de832014-11-21 17:51:45 -080013 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080014 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
Yanbiao Lic17de832014-11-21 17:51:45 -080017 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080018 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
Yanbiao Lic17de832014-11-21 17:51:45 -080021 *
Alexander Afanasyevbc9ed492016-01-26 11:38:11 -080022 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Yanbiao Lic17de832014-11-21 17:51:45 -080024 */
25
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040026#include "tests/key-chain-fixture.hpp"
27
Junxiao Shid7631272016-08-17 04:16:31 +000028#include <ndn-cxx/util/io.hpp>
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040029
Junxiao Shid7631272016-08-17 04:16:31 +000030#include <boost/filesystem.hpp>
Yanbiao Lic17de832014-11-21 17:51:45 -080031
32namespace nfd {
33namespace tests {
34
Davide Pesavento21353752020-11-20 00:43:44 -050035using namespace ndn::security;
36
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040037KeyChainFixture::KeyChainFixture()
Junxiao Shi16a3adf2017-05-26 17:38:51 +000038 : m_keyChain("pib-memory:", "tpm-memory:")
Yanbiao Lic17de832014-11-21 17:51:45 -080039{
Yanbiao Lic17de832014-11-21 17:51:45 -080040}
41
Davide Pesavento1d12d2f2019-03-22 12:44:14 -040042KeyChainFixture::~KeyChainFixture()
Yanbiao Lic17de832014-11-21 17:51:45 -080043{
Junxiao Shid7631272016-08-17 04:16:31 +000044 boost::system::error_code ec;
45 for (const auto& certFile : m_certFiles) {
46 boost::filesystem::remove(certFile, ec); // ignore error
47 }
Yanbiao Lic17de832014-11-21 17:51:45 -080048}
49
Davide Pesavento21353752020-11-20 00:43:44 -050050Certificate
51KeyChainFixture::makeCert(const Key& key, const std::string& issuer, const Key& signingKey)
Yanbiao Lic17de832014-11-21 17:51:45 -080052{
Davide Pesavento21353752020-11-20 00:43:44 -050053 Certificate cert;
54 cert.setName(Name(key.getName())
55 .append(issuer)
56 .appendVersion());
57
58 // set metainfo
59 cert.setContentType(tlv::ContentType_Key);
60 cert.setFreshnessPeriod(1_h);
61
62 // set content
63 cert.setContent(key.getPublicKey().data(), key.getPublicKey().size());
64
65 // set signature info
66 ndn::SignatureInfo info;
67 auto now = time::system_clock::now();
68 info.setValidityPeriod(ValidityPeriod(now - 30_days, now + 30_days));
69
70 m_keyChain.sign(cert, signingByKey(signingKey ? signingKey : key).setSignatureInfo(info));
71 return cert;
Yanbiao Lic17de832014-11-21 17:51:45 -080072}
73
Junxiao Shid7631272016-08-17 04:16:31 +000074bool
Davide Pesavento21353752020-11-20 00:43:44 -050075KeyChainFixture::saveCert(const Data& cert, const std::string& filename)
Junxiao Shid7631272016-08-17 04:16:31 +000076{
Junxiao Shid7631272016-08-17 04:16:31 +000077 m_certFiles.push_back(filename);
78 try {
Junxiao Shi16a3adf2017-05-26 17:38:51 +000079 ndn::io::save(cert, filename);
Junxiao Shid7631272016-08-17 04:16:31 +000080 return true;
81 }
82 catch (const ndn::io::Error&) {
83 return false;
84 }
85}
86
Davide Pesavento21353752020-11-20 00:43:44 -050087bool
88KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000089{
Davide Pesavento21353752020-11-20 00:43:44 -050090 Certificate cert;
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000091 try {
Davide Pesavento21353752020-11-20 00:43:44 -050092 cert = identity.getDefaultKey().getDefaultCertificate();
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +000093 }
Davide Pesavento21353752020-11-20 00:43:44 -050094 catch (const Pib::Error&) {
95 return false;
96 }
97
98 return saveCert(cert, filename);
99}
100
101bool
102KeyChainFixture::saveIdentityCert(const Name& identityName, const std::string& filename,
103 bool allowCreate)
104{
105 Identity id;
106 try {
107 id = m_keyChain.getPib().getIdentity(identityName);
108 }
109 catch (const Pib::Error&) {
110 if (allowCreate) {
111 id = m_keyChain.createIdentity(identityName);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000112 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000113 }
114
Davide Pesavento21353752020-11-20 00:43:44 -0500115 if (!id) {
116 return false;
117 }
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000118
Davide Pesavento21353752020-11-20 00:43:44 -0500119 return saveIdentityCert(id, filename);
Junxiao Shi5ba7dfc2018-09-26 14:24:05 +0000120}
121
Yanbiao Lic17de832014-11-21 17:51:45 -0800122} // namespace tests
123} // namespace nfd