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