blob: ab60698cec5ff8d372f199f702b6668c65c57921 [file] [log] [blame]
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Yumin Xia2c509c22017-02-09 14:37:36 -08002/*
3 * Copyright (c) 2014-2017, Regents of the University of California.
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08004 *
Yumin Xia2c509c22017-02-09 14:37:36 -08005 * This file is part of NDNS (Named Data Networking Domain Name Service).
Alexander Afanasyevfde570c2016-12-19 16:02:55 -08006 * See AUTHORS.md for complete list of NDNS authors and contributors.
7 *
8 * NDNS is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "identity-management-fixture.hpp"
21
22#include <ndn-cxx/util/io.hpp>
Yumin Xia2c509c22017-02-09 14:37:36 -080023#include <ndn-cxx/security/v2/additional-description.hpp>
24
25#include <boost/filesystem.hpp>
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080026
27namespace ndn {
28namespace ndns {
29namespace tests {
30
Yumin Xia2c509c22017-02-09 14:37:36 -080031namespace v2 = security::v2;
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080032
Yumin Xia2c509c22017-02-09 14:37:36 -080033IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080034{
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080035 boost::system::error_code ec;
36 for (const auto& certFile : m_certFiles) {
37 boost::filesystem::remove(certFile, ec); // ignore error
38 }
39}
40
41bool
Yumin Xia2c509c22017-02-09 14:37:36 -080042IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080043{
Yumin Xia2c509c22017-02-09 14:37:36 -080044 m_certFiles.insert(filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080045 try {
Yumin Xia2c509c22017-02-09 14:37:36 -080046 io::save(obj, filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080047 return true;
48 }
Yumin Xia2c509c22017-02-09 14:37:36 -080049 catch (const io::Error&) {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080050 return false;
51 }
52}
53
Yumin Xia2c509c22017-02-09 14:37:36 -080054IdentityManagementV2Fixture::IdentityManagementV2Fixture()
55 : m_keyChain("pib-memory:", "tpm-memory:")
56{
57}
58
59security::Identity
60IdentityManagementV2Fixture::addIdentity(const Name& identityName, const KeyParams& params)
61{
62 auto identity = m_keyChain.createIdentity(identityName, params);
63 m_identities.insert(identityName);
64 return identity;
65}
66
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080067bool
Yumin Xia2c509c22017-02-09 14:37:36 -080068IdentityManagementV2Fixture::saveIdentityCertificate(const security::Identity& identity,
69 const std::string& filename)
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080070{
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080071 try {
Yumin Xia2c509c22017-02-09 14:37:36 -080072 auto cert = identity.getDefaultKey().getDefaultCertificate();
73 return saveCertToFile(cert, filename);
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080074 }
Yumin Xia2c509c22017-02-09 14:37:36 -080075 catch (const security::Pib::Error&) {
Alexander Afanasyevfde570c2016-12-19 16:02:55 -080076 return false;
77 }
78}
79
Yumin Xia2c509c22017-02-09 14:37:36 -080080security::Identity
81IdentityManagementV2Fixture::addSubCertificate(const Name& subIdentityName,
82 const security::Identity& issuer, const KeyParams& params)
83{
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;
91 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
92 time::system_clock::now() + time::days(7300)));
93
94 v2::AdditionalDescription description;
95 description.set("type", "sub-certificate");
96 info.appendTypeSpecificTlv(description.wireEncode());
97
98 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
99 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
100
101 return subIdentity;
102}
103
104v2::Certificate
105IdentityManagementV2Fixture::addCertificate(const security::Key& key, const std::string& issuer)
106{
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);
116 certificate.setFreshnessPeriod(time::hours(1));
117
118 // set content
119 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
120
121 // set signature-info
122 SignatureInfo info;
123 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
124 time::system_clock::now() + time::days(10)));
125
126 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
127 return certificate;
128}
129
130
Alexander Afanasyevfde570c2016-12-19 16:02:55 -0800131} // namespace tests
132} // namespace ndns
133} // namespace ndn