blob: c4a4590ca614391f427dc4da8037cd9073b88421 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07002/*
Davide Pesavento0dc02012021-11-23 22:55:03 -05003 * Copyright (c) 2013-2021 Regents of the University of California.
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Zhiyi Zhang8617a792017-01-17 16:45:56 -08006 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -07007 * 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.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080010 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070011 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
Zhiyi Zhang8617a792017-01-17 16:45:56 -080012 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070013 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080014 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070015 * 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/>.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080018 *
Zhiyi Zhang42d992d2019-07-07 16:46:50 -070019 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Zhiyi Zhang8617a792017-01-17 16:45:56 -080020 */
21
22#include "identity-management-fixture.hpp"
Davide Pesavento0dc02012021-11-23 22:55:03 -050023
tylerliua7bea662020-10-08 18:51:02 -070024#include <ndn-cxx/security/additional-description.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080025#include <ndn-cxx/util/io.hpp>
Davide Pesavento0dc02012021-11-23 22:55:03 -050026
Zhiyi Zhang8617a792017-01-17 16:45:56 -080027#include <boost/filesystem.hpp>
28
Zhiyi Zhang8617a792017-01-17 16:45:56 -080029namespace ndncert {
30namespace tests {
31
Davide Pesavento0dc02012021-11-23 22:55:03 -050032using namespace ndn::security;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070033
34IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
35{
36 boost::system::error_code ec;
37 for (const auto& certFile : m_certFiles) {
38 boost::filesystem::remove(certFile, ec); // ignore error
39 }
40}
41
42bool
43IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
44{
45 m_certFiles.insert(filename);
46 try {
Davide Pesavento0dc02012021-11-23 22:55:03 -050047 ndn::io::save(obj, filename);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070048 return true;
49 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050050 catch (const ndn::io::Error&) {
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070051 return false;
52 }
53}
54
55IdentityManagementFixture::IdentityManagementFixture()
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080056 : m_keyChain("pib-memory:", "tpm-memory:")
57{
58}
59
Davide Pesavento0dc02012021-11-23 22:55:03 -050060Identity
61IdentityManagementFixture::addIdentity(const Name& identityName, const ndn::KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080062{
63 auto identity = m_keyChain.createIdentity(identityName, params);
64 m_identities.insert(identityName);
65 return identity;
66}
67
68bool
Davide Pesavento0dc02012021-11-23 22:55:03 -050069IdentityManagementFixture::saveCertificate(const Identity& identity, const std::string& filename)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080070{
71 try {
72 auto cert = identity.getDefaultKey().getDefaultCertificate();
73 return saveCertToFile(cert, filename);
74 }
Davide Pesavento0dc02012021-11-23 22:55:03 -050075 catch (const Pib::Error&) {
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080076 return false;
77 }
78}
79
Davide Pesavento0dc02012021-11-23 22:55:03 -050080Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
Davide Pesavento0dc02012021-11-23 22:55:03 -050082 const Identity& issuer, const ndn::KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080083{
84 auto subIdentity = addIdentity(subIdentityName, params);
85
Davide Pesavento0dc02012021-11-23 22:55:03 -050086 Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080087 request.setName(request.getKeyName().append("parent").appendVersion());
88
89 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070090 auto now = time::system_clock::now();
Davide Pesavento0dc02012021-11-23 22:55:03 -050091 info.setValidityPeriod(ValidityPeriod(now, now + 7300_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080092
Davide Pesavento0dc02012021-11-23 22:55:03 -050093 AdditionalDescription description;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080094 description.set("type", "sub-certificate");
Zhiyi Zhang03664042020-10-09 11:28:24 -070095 info.addCustomTlv(description.wireEncode());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080096
97 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
98 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
99
100 return subIdentity;
101}
102
Davide Pesavento0dc02012021-11-23 22:55:03 -0500103Certificate
104IdentityManagementFixture::addCertificate(const Key& key, const std::string& issuer)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800105{
106 Name certificateName = key.getName();
107 certificateName
108 .append(issuer)
109 .appendVersion();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500110 Certificate certificate;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800111 certificate.setName(certificateName);
112
113 // set metainfo
Zhiyi Zhang8f1ade32020-10-14 16:42:57 -0700114 certificate.setContentType(ndn::tlv::ContentType_Key);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700115 certificate.setFreshnessPeriod(1_h);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800116
117 // set content
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700118 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800119
120 // set signature-info
121 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700122 auto now = time::system_clock::now();
Davide Pesavento0dc02012021-11-23 22:55:03 -0500123 info.setValidityPeriod(ValidityPeriod(now, now + 10_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800124
125 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
126 return certificate;
127}
128
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800129} // namespace tests
130} // namespace ndncert