blob: e65ff42e3cfbd94d68c9917bea6d674e44288c74 [file] [log] [blame]
Davide Pesavento8de8a8b2022-05-12 01:26:43 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014-2022, Regents of the University of California,
4 * 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.
10 *
11 * This file is part of NLSR (Named-data Link State Routing).
12 * See AUTHORS.md for complete list of NLSR authors and contributors.
13 *
14 * NLSR 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.
17 *
18 * NLSR 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.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "tests/key-chain-fixture.hpp"
27
28#include <ndn-cxx/util/io.hpp>
29
30#include <boost/filesystem/operations.hpp>
31
32namespace nlsr {
33namespace test {
34
35using namespace ndn::security;
36
37KeyChainFixture::KeyChainFixture()
38 : m_keyChain("pib-memory:", "tpm-memory:")
39{
40}
41
42KeyChainFixture::~KeyChainFixture()
43{
44 boost::system::error_code ec;
45 for (const auto& certFile : m_certFiles) {
46 boost::filesystem::remove(certFile, ec); // ignore error
47 }
48}
49
50bool
51KeyChainFixture::saveCert(const ndn::Data& cert, const std::string& filename)
52{
53 m_certFiles.push_back(filename);
54 try {
55 ndn::io::save(cert, filename);
56 return true;
57 }
58 catch (const ndn::io::Error&) {
59 return false;
60 }
61}
62
63bool
64KeyChainFixture::saveIdentityCert(const Identity& identity, const std::string& filename)
65{
66 Certificate cert;
67 try {
68 cert = identity.getDefaultKey().getDefaultCertificate();
69 }
70 catch (const Pib::Error&) {
71 return false;
72 }
73
74 return saveCert(cert, filename);
75}
76
77bool
78KeyChainFixture::saveIdentityCert(const ndn::Name& identityName, const std::string& filename,
79 bool allowCreate)
80{
81 Identity id;
82 try {
83 id = m_keyChain.getPib().getIdentity(identityName);
84 }
85 catch (const Pib::Error&) {
86 if (allowCreate) {
87 id = m_keyChain.createIdentity(identityName);
88 }
89 }
90
91 if (!id) {
92 return false;
93 }
94
95 return saveIdentityCert(id, filename);
96}
97
98Identity
99KeyChainFixture::addSubCertificate(const ndn::Name& subIdentityName,
100 const Identity& issuer,
101 const ndn::KeyParams& params)
102{
103 auto subIdentity = m_keyChain.createIdentity(subIdentityName, params);
104
105 auto request = subIdentity.getDefaultKey().getDefaultCertificate();
106 ndn::security::MakeCertificateOptions opts;
107 opts.issuerId = ndn::name::Component::fromEscapedString("parent");
108 m_keyChain.makeCertificate(request, ndn::signingByIdentity(issuer), opts);
109
110 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
111
112 return subIdentity;
113}
114
115} // namespace test
116} // namespace nlsr