blob: ee80dd1c1a7b2ea762b902d165dae0225e395baa [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/*
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -08003 * Copyright (c) 2013-2017 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
22#include "identity-management-fixture.hpp"
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080023#include "util/io.hpp"
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080024#include "security/v2/additional-description.hpp"
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080025
26#include <boost/filesystem.hpp>
Yingdi Yud9715e32014-06-27 08:48:47 -070027
28namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070029namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070030
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080031namespace v2 = security::v2;
Yingdi Yud9715e32014-06-27 08:48:47 -070032
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080033IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
Yingdi Yud9715e32014-06-27 08:48:47 -070034{
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080035 boost::system::error_code ec;
36 for (const auto& certFile : m_certFiles) {
37 boost::filesystem::remove(certFile, ec); // ignore error
38 }
Yingdi Yud9715e32014-06-27 08:48:47 -070039}
40
41bool
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080042IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
Yingdi Yud9715e32014-06-27 08:48:47 -070043{
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080044 m_certFiles.insert(filename);
Yingdi Yud9715e32014-06-27 08:48:47 -070045 try {
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080046 io::save(obj, filename);
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080047 return true;
48 }
Alexander Afanasyev4c9a3d52017-01-03 17:45:19 -080049 catch (const io::Error&) {
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080050 return false;
51 }
52}
53
Alexander Afanasyevadc71842017-01-26 22:17:58 -050054IdentityManagementFixture::IdentityManagementFixture()
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080055 : m_keyChain("pib-memory:", "tpm-memory:")
56{
57}
58
59security::Identity
Alexander Afanasyevadc71842017-01-26 22:17:58 -050060IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080061{
62 auto identity = m_keyChain.createIdentity(identityName, params);
63 m_identities.insert(identityName);
64 return identity;
65}
66
67bool
Alexander Afanasyevadc71842017-01-26 22:17:58 -050068IdentityManagementFixture::saveCertificate(const security::Identity& identity, const std::string& filename)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -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
Alexander Afanasyevadc71842017-01-26 22:17:58 -050080IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
81 const security::Identity& issuer, const KeyParams& params)
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080082{
83 auto subIdentity = addIdentity(subIdentityName, params);
84
85 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
86
87 request.setName(request.getKeyName().append("parent").appendVersion());
88
89 SignatureInfo info;
90 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
91 time::system_clock::now() + time::days(7300)));
92
93 v2::AdditionalDescription description;
94 description.set("type", "sub-certificate");
95 info.appendTypeSpecificTlv(description.wireEncode());
96
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -070097 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
Alexander Afanasyevfc99b512017-01-04 11:10:36 -080098 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
99
100 return subIdentity;
101}
102
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700103v2::Certificate
Alexander Afanasyevadc71842017-01-26 22:17:58 -0500104IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700105{
106 Name certificateName = key.getName();
107 certificateName
108 .append(issuer)
109 .appendVersion();
110 v2::Certificate certificate;
111 certificate.setName(certificateName);
112
113 // set metainfo
114 certificate.setContentType(tlv::ContentType_Key);
115 certificate.setFreshnessPeriod(time::hours(1));
116
117 // set content
Davide Pesavento5d0b0102017-10-07 13:43:16 -0400118 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
Qiuhan Ding4caa0cc2015-10-23 20:31:27 -0700119
120 // set signature-info
121 SignatureInfo info;
122 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
123 time::system_clock::now() + time::days(10)));
124
125 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
126 return certificate;
127}
128
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700129} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -0700130} // namespace ndn