Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Zhiyi Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 4 | * |
| 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 Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 23 | #include "util/io.hpp" |
| 24 | |
| 25 | #include <boost/filesystem.hpp> |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 28 | namespace tests { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 29 | |
| 30 | IdentityManagementFixture::IdentityManagementFixture() |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 31 | { |
| 32 | } |
| 33 | |
| 34 | IdentityManagementFixture::~IdentityManagementFixture() |
| 35 | { |
Yingdi Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 36 | for (const auto& identity : m_identities) { |
| 37 | m_keyChain.deleteIdentity(identity); |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 38 | } |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 39 | |
| 40 | boost::system::error_code ec; |
| 41 | for (const auto& certFile : m_certFiles) { |
| 42 | boost::filesystem::remove(certFile, ec); // ignore error |
| 43 | } |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | bool |
| 47 | IdentityManagementFixture::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 Yu | 4154634 | 2014-11-30 23:37:53 -0800 | [diff] [blame] | 54 | catch (std::runtime_error&) { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
| 57 | } |
| 58 | |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 59 | bool |
| 60 | IdentityManagementFixture::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 Zhang | 1e164cc | 2017-01-03 11:04:35 -0800 | [diff] [blame] | 84 | bool |
| 85 | IdentityManagementFixture::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 Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 112 | } // namespace tests |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 113 | } // namespace ndn |