blob: a9251b42287edd228332883d37a9ba3003e39525 [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/*
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -04003 * Copyright (c) 2013-2020 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"
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -040023#include <ndn-cxx/security/additional-description.hpp>
Zhiyi Zhang8617a792017-01-17 16:45:56 -080024#include <ndn-cxx/util/io.hpp>
25#include <boost/filesystem.hpp>
26
27namespace ndn {
28namespace ndncert {
29namespace tests {
30
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070031namespace v2 = security::v2;
32
33IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
34{
35 boost::system::error_code ec;
36 for (const auto& certFile : m_certFiles) {
37 boost::filesystem::remove(certFile, ec); // ignore error
38 }
39}
40
41bool
42IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
43{
44 m_certFiles.insert(filename);
45 try {
46 io::save(obj, filename);
47 return true;
48 }
49 catch (const io::Error&) {
50 return false;
51 }
52}
53
54IdentityManagementFixture::IdentityManagementFixture()
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080055 : m_keyChain("pib-memory:", "tpm-memory:")
56{
57}
58
59security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070060IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080061{
62 auto identity = m_keyChain.createIdentity(identityName, params);
63 m_identities.insert(identityName);
64 return identity;
65}
66
67bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070068IdentityManagementFixture::saveCertificate(const security::Identity& identity, const std::string& filename)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080069{
70 try {
71 auto cert = identity.getDefaultKey().getDefaultCertificate();
72 return saveCertToFile(cert, filename);
73 }
74 catch (const security::Pib::Error&) {
75 return false;
76 }
77}
78
79security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070080IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
81 const security::Identity& issuer, const KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080082{
83 auto subIdentity = addIdentity(subIdentityName, params);
84
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070085 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080086
87 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();
91 info.setValidityPeriod(security::ValidityPeriod(now, now + 7300_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080092
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070093 v2::AdditionalDescription description;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080094 description.set("type", "sub-certificate");
95 info.appendTypeSpecificTlv(description.wireEncode());
96
97 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
98 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
99
100 return subIdentity;
101}
102
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700103v2::Certificate
104IdentityManagementFixture::addCertificate(const security::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();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700110 v2::Certificate certificate;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800111 certificate.setName(certificateName);
112
113 // set metainfo
114 certificate.setContentType(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();
123 info.setValidityPeriod(security::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
Alexander Afanasyev7838cfd2020-06-03 14:16:43 -0400131} // namespace ndn