Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | e5a19b8 | 2017-01-30 22:30:46 -0800 | [diff] [blame] | 2 | /* |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2020 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 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "tests/identity-management-fixture.hpp" |
| 23 | |
| 24 | #include "ndn-cxx/security/v2/additional-description.hpp" |
| 25 | #include "ndn-cxx/util/io.hpp" |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 26 | |
| 27 | #include <boost/filesystem.hpp> |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ndn { |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 30 | namespace tests { |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 32 | namespace v2 = security::v2; |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 33 | |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 34 | IdentityManagementBaseFixture::~IdentityManagementBaseFixture() |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 35 | { |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 36 | boost::system::error_code ec; |
| 37 | for (const auto& certFile : m_certFiles) { |
| 38 | boost::filesystem::remove(certFile, ec); // ignore error |
| 39 | } |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | bool |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 43 | IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename) |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 45 | m_certFiles.insert(filename); |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 46 | try { |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 47 | io::save(obj, filename); |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 48 | return true; |
| 49 | } |
Alexander Afanasyev | 4c9a3d5 | 2017-01-03 17:45:19 -0800 | [diff] [blame] | 50 | catch (const io::Error&) { |
Zhiyi Zhang | 0a939b4 | 2016-11-16 14:27:20 -0800 | [diff] [blame] | 51 | return false; |
| 52 | } |
| 53 | } |
| 54 | |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 55 | IdentityManagementFixture::IdentityManagementFixture() |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 56 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 57 | { |
| 58 | } |
| 59 | |
| 60 | security::Identity |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 61 | IdentityManagementFixture::addIdentity(const Name& identityName, const KeyParams& params) |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 62 | { |
| 63 | auto identity = m_keyChain.createIdentity(identityName, params); |
| 64 | m_identities.insert(identityName); |
| 65 | return identity; |
| 66 | } |
| 67 | |
| 68 | bool |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 69 | IdentityManagementFixture::saveCertificate(const security::Identity& identity, const std::string& filename) |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 70 | { |
| 71 | try { |
| 72 | auto cert = identity.getDefaultKey().getDefaultCertificate(); |
| 73 | return saveCertToFile(cert, filename); |
| 74 | } |
| 75 | catch (const security::Pib::Error&) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | security::Identity |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 81 | IdentityManagementFixture::addSubCertificate(const Name& subIdentityName, |
| 82 | const security::Identity& issuer, const KeyParams& params) |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 83 | { |
| 84 | auto subIdentity = addIdentity(subIdentityName, params); |
| 85 | |
| 86 | v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate(); |
| 87 | |
| 88 | request.setName(request.getKeyName().append("parent").appendVersion()); |
| 89 | |
| 90 | SignatureInfo info; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 91 | auto now = time::system_clock::now(); |
| 92 | info.setValidityPeriod(security::ValidityPeriod(now, now + 7300_days)); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 93 | |
| 94 | v2::AdditionalDescription description; |
| 95 | description.set("type", "sub-certificate"); |
Eric Newberry | f165bb4 | 2020-05-22 11:28:45 -0700 | [diff] [blame] | 96 | info.addCustomTlv(description.wireEncode()); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 97 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 98 | m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info)); |
Alexander Afanasyev | fc99b51 | 2017-01-04 11:10:36 -0800 | [diff] [blame] | 99 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 100 | |
| 101 | return subIdentity; |
| 102 | } |
| 103 | |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 104 | v2::Certificate |
Alexander Afanasyev | adc7184 | 2017-01-26 22:17:58 -0500 | [diff] [blame] | 105 | IdentityManagementFixture::addCertificate(const security::Key& key, const std::string& issuer) |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 106 | { |
| 107 | Name certificateName = key.getName(); |
| 108 | certificateName |
| 109 | .append(issuer) |
| 110 | .appendVersion(); |
| 111 | v2::Certificate certificate; |
| 112 | certificate.setName(certificateName); |
| 113 | |
| 114 | // set metainfo |
| 115 | certificate.setContentType(tlv::ContentType_Key); |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 116 | certificate.setFreshnessPeriod(1_h); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 117 | |
| 118 | // set content |
Davide Pesavento | 5d0b010 | 2017-10-07 13:43:16 -0400 | [diff] [blame] | 119 | certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size()); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 120 | |
| 121 | // set signature-info |
| 122 | SignatureInfo info; |
Davide Pesavento | 0f83080 | 2018-01-16 23:58:58 -0500 | [diff] [blame] | 123 | auto now = time::system_clock::now(); |
| 124 | info.setValidityPeriod(security::ValidityPeriod(now, now + 10_days)); |
Qiuhan Ding | 4caa0cc | 2015-10-23 20:31:27 -0700 | [diff] [blame] | 125 | |
| 126 | m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info)); |
| 127 | return certificate; |
| 128 | } |
| 129 | |
Alexander Afanasyev | e4f8c3b | 2016-06-23 16:03:48 -0700 | [diff] [blame] | 130 | } // namespace tests |
Yingdi Yu | d9715e3 | 2014-06-27 08:48:47 -0700 | [diff] [blame] | 131 | } // namespace ndn |