blob: a152f73af90fea0f218db23ee0af9f58e34c6727 [file] [log] [blame]
Zhiyi Zhang8617a792017-01-17 16:45:56 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -07003 * Copyright (c) 2014-2019, Regents of the University of California,
Zhiyi Zhang8617a792017-01-17 16:45:56 -08004 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
10 *
11 * This file, originally written as part of NFD (Named Data Networking Forwarding Daemon),
12 * is a part of ndncert, a certificate management system based on NDN.
13 *
14 * ndncert is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation, either
16 * version 3 of the License, or (at your option) any later version.
17 *
18 * ndncert is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
20 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received copies of the GNU General Public License along with
23 * ndncert, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * See AUTHORS.md for complete list of ndncert authors and contributors.
26 */
27
28#include "identity-management-fixture.hpp"
29
30#include <ndn-cxx/util/io.hpp>
31#include <boost/filesystem.hpp>
32
33namespace ndn {
34namespace ndncert {
35namespace tests {
36
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070037namespace v2 = security::v2;
38
39IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
40{
41 boost::system::error_code ec;
42 for (const auto& certFile : m_certFiles) {
43 boost::filesystem::remove(certFile, ec); // ignore error
44 }
45}
46
47bool
48IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
49{
50 m_certFiles.insert(filename);
51 try {
52 io::save(obj, filename);
53 return true;
54 }
55 catch (const io::Error&) {
56 return false;
57 }
58}
59
60IdentityManagementFixture::IdentityManagementFixture()
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080061 : m_keyChain("pib-memory:", "tpm-memory:")
62{
63}
64
65security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070066IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080067{
68 auto identity = m_keyChain.createIdentity(identityName, params);
69 m_identities.insert(identityName);
70 return identity;
71}
72
73bool
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070074IdentityManagementFixture::saveCertificate(const security::Identity& identity, const std::string& filename)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080075{
76 try {
77 auto cert = identity.getDefaultKey().getDefaultCertificate();
78 return saveCertToFile(cert, filename);
79 }
80 catch (const security::Pib::Error&) {
81 return false;
82 }
83}
84
85security::Identity
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070086IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
87 const security::Identity& issuer, const KeyParams& params)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080088{
89 auto subIdentity = addIdentity(subIdentityName, params);
90
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070091 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080092
93 request.setName(request.getKeyName().append("parent").appendVersion());
94
95 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070096 auto now = time::system_clock::now();
97 info.setValidityPeriod(security::ValidityPeriod(now, now + 7300_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -080098
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -070099 v2::AdditionalDescription description;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800100 description.set("type", "sub-certificate");
101 info.appendTypeSpecificTlv(description.wireEncode());
102
103 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
104 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
105
106 return subIdentity;
107}
108
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700109v2::Certificate
110IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800111{
112 Name certificateName = key.getName();
113 certificateName
114 .append(issuer)
115 .appendVersion();
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700116 v2::Certificate certificate;
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800117 certificate.setName(certificateName);
118
119 // set metainfo
120 certificate.setContentType(tlv::ContentType_Key);
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700121 certificate.setFreshnessPeriod(1_h);
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800122
123 // set content
Zhiyi Zhang576aad12017-10-03 15:41:53 -0700124 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800125
126 // set signature-info
127 SignatureInfo info;
Zhiyi Zhangaf7c2902019-03-14 22:13:21 -0700128 auto now = time::system_clock::now();
129 info.setValidityPeriod(security::ValidityPeriod(now, now + 10_days));
Zhiyi Zhanga41c5732017-01-18 14:06:44 -0800130
131 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
132 return certificate;
133}
134
Zhiyi Zhang8617a792017-01-17 16:45:56 -0800135} // namespace tests
136} // namespace ndncert
137} // namespace ndn