blob: 3877a233735dae894252477ceb35bf3ffc23c877 [file] [log] [blame]
Yingdi Yud9715e32014-06-27 08:48:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyeve5a19b82017-01-30 22:30:46 -08002/*
Eric Newberryf165bb42020-05-22 11:28:45 -07003 * Copyright (c) 2013-2020 Regents of the University of California.
Yingdi Yud9715e32014-06-27 08:48:47 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * 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.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * 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/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "tests/identity-management-fixture.hpp"
23
24#include "ndn-cxx/security/v2/additional-description.hpp"
25#include "ndn-cxx/util/io.hpp"
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080026
27#include <boost/filesystem.hpp>
Yingdi Yud9715e32014-06-27 08:48:47 -070028
29namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070030namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070031
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080032namespace v2 = security::v2;
Yingdi Yud9715e32014-06-27 08:48:47 -070033
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080034IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
Yingdi Yud9715e32014-06-27 08:48:47 -070035{
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080036 boost::system::error_code ec;
37 for (const auto& certFile : m_certFiles) {
38 boost::filesystem::remove(certFile, ec); // ignore error
39 }
Yingdi Yud9715e32014-06-27 08:48:47 -070040}
41
42bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080043IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
Yingdi Yud9715e32014-06-27 08:48:47 -070044{
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080045 m_certFiles.insert(filename);
Yingdi Yud9715e32014-06-27 08:48:47 -070046 try {
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080047 io::save(obj, filename);
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080048 return true;
49 }
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080050 catch (const io::Error&) {
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080051 return false;
52 }
53}
54
Alexander Afanasyevadc71842017-01-26 22:17:58 -050055IdentityManagementFixture::IdentityManagementFixture()
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080056 : m_keyChain("pib-memory:", "tpm-memory:")
57{
58}
59
60security::Identity
Alexander Afanasyevadc71842017-01-26 22:17:58 -050061IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080062{
63 auto identity = m_keyChain.createIdentity(identityName, params);
64 m_identities.insert(identityName);
65 return identity;
66}
67
68bool
Alexander Afanasyevadc71842017-01-26 22:17:58 -050069IdentityManagementFixture::saveCertificate(const security::Identity& identity, const std::string& filename)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -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
Alexander Afanasyevadc71842017-01-26 22:17:58 -050081IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
82 const security::Identity& issuer, const KeyParams& params)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080083{
84 auto subIdentity = addIdentity(subIdentityName, params);
85
86 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
87
88 request.setName(request.getKeyName().append("parent").appendVersion());
89
90 SignatureInfo info;
Davide Pesavento0f830802018-01-16 23:58:58 -050091 auto now = time::system_clock::now();
92 info.setValidityPeriod(security::ValidityPeriod(now, now + 7300_days));
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080093
94 v2::AdditionalDescription description;
95 description.set("type", "sub-certificate");
Eric Newberryf165bb42020-05-22 11:28:45 -070096 info.addCustomTlv(description.wireEncode());
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080097
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070098 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080099 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
100
101 return subIdentity;
102}
103
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700104v2::Certificate
Alexander Afanasyevadc71842017-01-26 22:17:58 -0500105IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700106{
107 Name certificateName = key.getName();
108 certificateName
109 .append(issuer)
110 .appendVersion();
111 v2::Certificate certificate;
112 certificate.setName(certificateName);
113
114 // set metainfo
115 certificate.setContentType(tlv::ContentType_Key);
Davide Pesavento0f830802018-01-16 23:58:58 -0500116 certificate.setFreshnessPeriod(1_h);
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700117
118 // set content
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400119 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700120
121 // set signature-info
122 SignatureInfo info;
Davide Pesavento0f830802018-01-16 23:58:58 -0500123 auto now = time::system_clock::now();
124 info.setValidityPeriod(security::ValidityPeriod(now, now + 10_days));
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700125
126 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
127 return certificate;
128}
129
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700130} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -0700131} // namespace ndn