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