blob: 0b296f748a2a12b7f7653c382784976959d1786b [file] [log] [blame]
Yingdi Yud9715e32014-06-27 08:48:47 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
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"
24
25#include <boost/filesystem.hpp>
Yingdi Yud9715e32014-06-27 08:48:47 -070026
27namespace ndn {
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -070028namespace tests {
Yingdi Yud9715e32014-06-27 08:48:47 -070029
30IdentityManagementFixture::IdentityManagementFixture()
Yingdi Yud9715e32014-06-27 08:48:47 -070031{
32}
33
34IdentityManagementFixture::~IdentityManagementFixture()
35{
Yingdi Yu41546342014-11-30 23:37:53 -080036 for (const auto& identity : m_identities) {
37 m_keyChain.deleteIdentity(identity);
Yingdi Yud9715e32014-06-27 08:48:47 -070038 }
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080039
40 boost::system::error_code ec;
41 for (const auto& certFile : m_certFiles) {
42 boost::filesystem::remove(certFile, ec); // ignore error
43 }
Yingdi Yud9715e32014-06-27 08:48:47 -070044}
45
46bool
47IdentityManagementFixture::addIdentity(const Name& identity, const KeyParams& params)
48{
49 try {
50 m_keyChain.createIdentity(identity, params);
51 m_identities.push_back(identity);
52 return true;
53 }
Yingdi Yu41546342014-11-30 23:37:53 -080054 catch (std::runtime_error&) {
Yingdi Yud9715e32014-06-27 08:48:47 -070055 return false;
56 }
57}
58
Zhiyi Zhang0a939b42016-11-16 14:27:20 -080059bool
60IdentityManagementFixture::saveIdentityCertificate(const Name& identity,
61 const std::string& filename, bool wantAdd)
62{
63 shared_ptr<ndn::IdentityCertificate> cert;
64 try {
65 cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity));
66 }
67 catch (const ndn::SecPublicInfo::Error&) {
68 if (wantAdd && this->addIdentity(identity)) {
69 return this->saveIdentityCertificate(identity, filename, false);
70 }
71 return false;
72 }
73
74 m_certFiles.push_back(filename);
75 try {
76 ndn::io::save(*cert, filename);
77 return true;
78 }
79 catch (const ndn::io::Error&) {
80 return false;
81 }
82}
83
Zhiyi Zhang1e164cc2017-01-03 11:04:35 -080084bool
85IdentityManagementFixture::addSubCertificate(const Name& identity, const Name& issuer,
86 const KeyParams& params)
87{
88 if (!m_keyChain.doesIdentityExist(issuer))
89 return false;
90 if (!m_keyChain.doesIdentityExist(identity)) {
91 addIdentity(identity, params);
92 }
93 Name identityKeyName;
94 try {
95 identityKeyName = m_keyChain.getDefaultKeyNameForIdentity(identity);
96 }
97 catch (const ndn::SecPublicInfo::Error&) {
98 identityKeyName = m_keyChain.generateRsaKeyPairAsDefault(identity, true);
99 }
100 std::vector<ndn::CertificateSubjectDescription> subjectDescription;
101 shared_ptr<ndn::IdentityCertificate> identityCert =
102 m_keyChain.prepareUnsignedIdentityCertificate(identityKeyName,
103 issuer,
104 time::system_clock::now(),
105 time::system_clock::now() + time::days(7300),
106 subjectDescription);
107 m_keyChain.sign(*identityCert, security::signingByIdentity(issuer));
108 m_keyChain.addCertificateAsIdentityDefault(*identityCert);
109 return true;
110}
111
Alexander Afanasyeve4f8c3b2016-06-23 16:03:48 -0700112} // namespace tests
Yingdi Yud9715e32014-06-27 08:48:47 -0700113} // namespace ndn