Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2014-2020, Regents of the University of California, |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 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> |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 29 | #include <ndn-cxx/security/additional-description.hpp> |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 30 | |
| 31 | #include <boost/filesystem.hpp> |
| 32 | |
| 33 | namespace nlsr { |
| 34 | namespace tests { |
| 35 | |
Alexander Afanasyev | 0ad01f3 | 2020-06-03 14:12:58 -0400 | [diff] [blame] | 36 | namespace v2 = ndn::security; |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 37 | namespace io = ndn::io; |
| 38 | namespace time = ndn::time; |
| 39 | |
| 40 | IdentityManagementBaseFixture::~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 | |
| 48 | bool |
| 49 | IdentityManagementBaseFixture::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 | |
| 62 | IdentityManagementFixture::IdentityManagementFixture() |
| 63 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | ndn::security::Identity |
| 68 | IdentityManagementFixture::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 | |
| 76 | bool |
| 77 | IdentityManagementFixture::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 | |
| 89 | ndn::security::Identity |
| 90 | IdentityManagementFixture::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"); |
Ashlesh Gawande | 7a231c0 | 2020-06-12 20:06:44 -0700 | [diff] [blame] | 107 | info.addCustomTlv(description.wireEncode()); |
Muktadir Chowdhury | f04f989 | 2017-08-20 20:42:56 -0500 | [diff] [blame] | 108 | |
| 109 | m_keyChain.sign(request, ndn::signingByIdentity(issuer).setSignatureInfo(info)); |
| 110 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 111 | |
| 112 | return subIdentity; |
| 113 | } |
| 114 | |
| 115 | v2::Certificate |
| 116 | IdentityManagementFixture::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 |