blob: 385aadeec3dc0ee3be466611bfb8e0a9e8e10066 [file] [log] [blame]
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento61a80032020-06-08 18:56:32 -04002/*
Alexander Afanasyev5181d892020-06-06 18:05:47 -04003 * Copyright (c) 2014-2020, Regents of the University of California
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -04004 *
5 * NAC library is free software: you can redistribute it and/or modify it under the
6 * terms of the GNU Lesser General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option) any later version.
8 *
9 * NAC library is distributed in the hope that it will be useful, but WITHOUT ANY
10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
12 *
13 * You should have received copies of the GNU General Public License and GNU Lesser
14 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
15 * <http://www.gnu.org/licenses/>.
16 *
17 * See AUTHORS.md for complete list of NAC library authors and contributors.
18 */
19
20#include "identity-management-fixture.hpp"
21
22#include <ndn-cxx/util/io.hpp>
Alexander Afanasyev5181d892020-06-06 18:05:47 -040023#include <ndn-cxx/security/additional-description.hpp>
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040024
25#include <boost/filesystem.hpp>
26
27namespace ndn {
28namespace nac {
29namespace tests {
30
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040031IdentityManagementFixture::IdentityManagementFixture()
32 : m_keyChain("pib-memory:", "tpm-memory:")
33{
34}
35
36IdentityManagementFixture::~IdentityManagementFixture()
37{
38 boost::system::error_code ec;
39 for (const auto& certFile : m_certFiles) {
40 boost::filesystem::remove(certFile, ec); // ignore error
41 }
42}
43
Alexander Afanasyev5181d892020-06-06 18:05:47 -040044Identity
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040045IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params)
46{
47 auto identity = m_keyChain.createIdentity(identityName, params);
48 m_identities.insert(identityName);
49 return identity;
50}
51
52bool
Alexander Afanasyev5181d892020-06-06 18:05:47 -040053IdentityManagementFixture::saveIdentityCertificate(const Identity& identity,
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040054 const std::string& filename)
55{
56 try {
57 auto cert = identity.getDefaultKey().getDefaultCertificate();
58 return saveCertToFile(cert, filename);
59 }
60 catch (const security::Pib::Error&) {
61 return false;
62 }
63}
64
Alexander Afanasyev5181d892020-06-06 18:05:47 -040065Identity
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040066IdentityManagementFixture::addSubCertificate(const Name& subIdentityName,
Alexander Afanasyev5181d892020-06-06 18:05:47 -040067 const Identity& issuer, const KeyParams& params)
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040068{
69 auto subIdentity = addIdentity(subIdentityName, params);
70
Alexander Afanasyev5181d892020-06-06 18:05:47 -040071 Certificate request = subIdentity.getDefaultKey().getDefaultCertificate();
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040072
73 request.setName(request.getKeyName().append("parent").appendVersion());
74
75 SignatureInfo info;
76 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
77 time::system_clock::now() + time::days(7300)));
78
Alexander Afanasyev5181d892020-06-06 18:05:47 -040079 security::AdditionalDescription description;
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040080 description.set("type", "sub-certificate");
Davide Pesavento61a80032020-06-08 18:56:32 -040081 info.addCustomTlv(description.wireEncode());
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040082
83 m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info));
84 m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request);
85
86 return subIdentity;
87}
88
Alexander Afanasyev5181d892020-06-06 18:05:47 -040089Certificate
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040090IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer)
91{
92 Name certificateName = key.getName();
93 certificateName
94 .append(issuer)
95 .appendVersion();
Alexander Afanasyev5181d892020-06-06 18:05:47 -040096 Certificate certificate;
Alexander Afanasyev77f6ae12018-06-14 17:54:17 -040097 certificate.setName(certificateName);
98
99 // set metainfo
100 certificate.setContentType(tlv::ContentType_Key);
101 certificate.setFreshnessPeriod(time::hours(1));
102
103 // set content
104 certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size());
105
106 // set signature-info
107 SignatureInfo info;
108 info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(),
109 time::system_clock::now() + time::days(10)));
110
111 m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info));
112 return certificate;
113}
114
115bool
116IdentityManagementFixture::saveCertToFile(const Data& obj, const std::string& filename)
117{
118 m_certFiles.insert(filename);
119 try {
120 io::save(obj, filename);
121 return true;
122 }
123 catch (const io::Error&) {
124 return false;
125 }
126}
127
128} // namespace tests
129} // namespace nac
130} // namespace ndn