blob: 8215b75a23c1a057391719192c6947a09df09471 [file] [log] [blame]
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
Davide Pesavento1bff1b22020-06-08 18:46:05 -04003 * Copyright (c) 2014-2020, Regents of the University of California.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 *
Yumin Xia2c509c22017-02-09 14:37:36 -08005 * This file is part of NDNS (Named Data Networking Domain Name Service).
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08006 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS 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 * NDNS 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 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity-management-fixture.hpp"
21
22#include <ndn-cxx/util/io.hpp>
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040023#include <ndn-cxx/security/additional-description.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080024
25#include <boost/filesystem.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080026
27namespace ndn {
28namespace ndns {
29namespace tests {
30
Yumin Xia2c509c22017-02-09 14:37:36 -080031IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080032{
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080033 boost::system::error_code ec;
34 for (const auto& certFile : m_certFiles) {
35 boost::filesystem::remove(certFile, ec); // ignore error
36 }
37}
38
39bool
Yumin Xia2c509c22017-02-09 14:37:36 -080040IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080041{
Yumin Xia2c509c22017-02-09 14:37:36 -080042 m_certFiles.insert(filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080043 try {
Yumin Xia2c509c22017-02-09 14:37:36 -080044 io::save(obj, filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080045 return true;
46 }
Yumin Xia2c509c22017-02-09 14:37:36 -080047 catch (const io::Error&) {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080048 return false;
49 }
50}
51
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040052IdentityManagementFixture::IdentityManagementFixture()
Yumin Xia2c509c22017-02-09 14:37:36 -080053 : m_keyChain("pib-memory:", "tpm-memory:")
54{
55}
56
57security::Identity
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040058IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
Yumin Xia2c509c22017-02-09 14:37:36 -080059{
60 auto identity = m_keyChain.createIdentity(identityName, params);
61 m_identities.insert(identityName);
62 return identity;
63}
64
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080065bool
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040066IdentityManagementFixture::saveIdentityCertificate(const security::Identity& identity,
Yumin Xia2c509c22017-02-09 14:37:36 -080067 const std::string& filename)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080068{
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080069 try {
Yumin Xia2c509c22017-02-09 14:37:36 -080070 auto cert = identity.getDefaultKey().getDefaultCertificate();
71 return saveCertToFile(cert, filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080072 }
Yumin Xia2c509c22017-02-09 14:37:36 -080073 catch (const security::Pib::Error&) {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080074 return false;
75 }
76}
77
Yumin Xia2c509c22017-02-09 14:37:36 -080078security::Identity
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040079IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
Yumin Xia2c509c22017-02-09 14:37:36 -080080 const security::Identity& issuer, const KeyParams& params)
81{
82 auto subIdentity = addIdentity(subIdentityName, params);
83
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040084 security::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Yumin Xia2c509c22017-02-09 14:37:36 -080085
86 request.setName(request.getKeyName().append("parent").appendVersion());
87
88 SignatureInfo info;
89 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
90 time::system_clock::now() + time::days(7300)));
91
Alexander Afanasyev60514ec2020-06-03 14:18:53 -040092 security::AdditionalDescription description;
Yumin Xia2c509c22017-02-09 14:37:36 -080093 description.set("type", "sub-certificate");
Davide Pesavento1bff1b22020-06-08 18:46:05 -040094 info.addCustomTlv(description.wireEncode());
Yumin Xia2c509c22017-02-09 14:37:36 -080095
96 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
97 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
98
99 return subIdentity;
100}
101
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400102security::Certificate
103IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
Yumin Xia2c509c22017-02-09 14:37:36 -0800104{
105 Name certificateName = key.getName();
106 certificateName
107 .append(issuer)
108 .appendVersion();
Alexander Afanasyev60514ec2020-06-03 14:18:53 -0400109 security::Certificate certificate;
Yumin Xia2c509c22017-02-09 14:37:36 -0800110 certificate.setName(certificateName);
111
112 // set metainfo
113 certificate.setContentType(tlv::ContentType_Key);
114 certificate.setFreshnessPeriod(time::hours(1));
115
116 // set content
117 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
118
119 // set signature-info
120 SignatureInfo info;
121 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
122 time::system_clock::now() + time::days(10)));
123
124 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
125 return certificate;
126}
127
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800128} // namespace tests
129} // namespace ndns
130} // namespace ndn