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