blob: 56f445e547f4ec92fa220e1c785b64dc01d8450c [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/*
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -07003 * Copyright (c) 2013-2018 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"
Zhiyi Zhang5d80e1e2020-09-25 11:34:54 -070023
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070024#include <ndn-cxx/security/v2/additional-description.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080025#include <ndn-cxx/util/io.hpp>
26#include <boost/filesystem.hpp>
27
28namespace ndn {
29namespace ndncert {
30namespace tests {
31
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070032namespace v2 = security::v2;
33
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 {
47 io::save(obj, filename);
48 return true;
49 }
50 catch (const io::Error&) {
51 return false;
52 }
53}
54
55IdentityManagementFixture::IdentityManagementFixture()
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080056 : m_keyChain("pib-memory:", "tpm-memory:")
57{
58}
59
60security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070061IdentityManagementFixture::addIdentity(const Name& identityName, const 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
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070069IdentityManagementFixture::saveCertificate(const security::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 }
75 catch (const security::Pib::Error&) {
76 return false;
77 }
78}
79
80security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070081IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
82 const security::Identity& issuer, const KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080083{
84 auto subIdentity = addIdentity(subIdentityName, params);
85
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080087
88 request.setName(request.getKeyName().append("parent").appendVersion());
89
90 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091 auto now = time::system_clock::now();
92 info.setValidityPeriod(security::ValidityPeriod(now, now + 7300_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080093
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070094 v2::AdditionalDescription description;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080095 description.set("type", "sub-certificate");
Zhiyi Zhangef6b36a2020-09-22 21:20:59 -070096 info.appendTypeSpecificTlv(description.wireEncode());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080097
98 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
99 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
100
101 return subIdentity;
102}
103
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700104v2::Certificate
105IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800106{
107 Name certificateName = key.getName();
108 certificateName
109 .append(issuer)
110 .appendVersion();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700111 v2::Certificate certificate;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800112 certificate.setName(certificateName);
113
114 // set metainfo
115 certificate.setContentType(tlv::ContentType_Key);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116 certificate.setFreshnessPeriod(1_h);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800117
118 // set content
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700119 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800120
121 // set signature-info
122 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700123 auto now = time::system_clock::now();
124 info.setValidityPeriod(security::ValidityPeriod(now, now + 10_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800125
126 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
127 return certificate;
128}
129
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800130} // namespace tests
131} // namespace ndncert
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -0400132} // namespace ndn