blob: c5aed7312251db7aa7712464c4288bd6055958ef [file] [log] [blame]
Muktadir Chowdhuryf04f9892017-08-20 20:42:56 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2017, Regents of the University of California,
4 * 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 is part of NLSR (Named-data Link State Routing).
12 * See AUTHORS.md for complete list of NLSR authors and contributors.
13 *
14 * NLSR 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,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NLSR is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NLSR, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26#include "identity-management-fixture.hpp"
27
28#include <ndn-cxx/util/io.hpp>
29#include <ndn-cxx/security/v2/additional-description.hpp>
30
31#include <boost/filesystem.hpp>
32
33namespace nlsr {
34namespace tests {
35
36namespace v2 = ndn::security::v2;
37namespace io = ndn::io;
38namespace time = ndn::time;
39
40IdentityManagementBaseFixture::~IdentityManagementBaseFixture()
41{
42 boost::system::error_code ec;
43 for (const auto& certFile : m_certFiles) {
44 boost::filesystem::remove(certFile, ec); // ignore error
45 }
46}
47
48bool
49IdentityManagementBaseFixture::saveCertToFile(const ndn::Data& obj,
50 const std::string& filename)
51{
52 m_certFiles.insert(filename);
53 try {
54 io::save(obj, filename);
55 return true;
56 }
57 catch (const io::Error&) {
58 return false;
59 }
60}
61
62IdentityManagementFixture::IdentityManagementFixture()
63 : m_keyChain("pib-memory:", "tpm-memory:")
64{
65}
66
67ndn::security::Identity
68IdentityManagementFixture::addIdentity(const ndn::Name& identityName,
69 const ndn::KeyParams& params)
70{
71 auto identity = m_keyChain.createIdentity(identityName, params);
72 m_identities.insert(identityName);
73 return identity;
74}
75
76bool
77IdentityManagementFixture::saveCertificate(const ndn::security::Identity& identity,
78 const std::string& filename)
79{
80 try {
81 auto cert = identity.getDefaultKey().getDefaultCertificate();
82 return saveCertToFile(cert, filename);
83 }
84 catch (const ndn::security::Pib::Error&) {
85 return false;
86 }
87}
88
89ndn::security::Identity
90IdentityManagementFixture::addSubCertificate(const ndn::Name& subIdentityName,
91 const ndn::security::Identity& issuer,
92 const ndn::KeyParams& params)
93{
94 auto subIdentity = addIdentity(subIdentityName, params);
95
96 v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
97
98 request.setName(request.getKeyName().append("parent").appendVersion());
99
100 ndn::SignatureInfo info;
101 info.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
102 time::system_clock::now()
103 + time::days(7300)));
104
105 v2::AdditionalDescription description;
106 description.set("type", "sub-certificate");
107 info.appendTypeSpecificTlv(description.wireEncode());
108
109 m_keyChain.sign(request, ndn::signingByIdentity(issuer).setSignatureInfo(info));
110 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
111
112 return subIdentity;
113}
114
115v2::Certificate
116IdentityManagementFixture::addCertificate(const ndn::security::Key& key,
117 const std::string& issuer)
118{
119 ndn::Name certificateName = key.getName();
120 certificateName
121 .append(issuer)
122 .appendVersion();
123 v2::Certificate certificate;
124 certificate.setName(certificateName);
125
126 // set metainfo
127 certificate.setContentType(ndn::tlv::ContentType_Key);
128 certificate.setFreshnessPeriod(time::hours(1));
129
130 // set content
131 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
132
133 // set signature-info
134 ndn::SignatureInfo info;
135 info.setValidityPeriod(ndn::security::ValidityPeriod(time::system_clock::now(),
136 time::system_clock::now() + time::days(10)));
137
138 m_keyChain.sign(certificate, ndn::signingByKey(key).setSignatureInfo(info));
139 return certificate;
140}
141
142} // namespace tests
143} // namespace nlsr