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 | /* |
| 3 | * Copyright (c) 2014-2017, 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> |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 23 | #include <ndn-cxx/security/v2/additional-description.hpp> |
| 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 | namespace v2 = security::v2; |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 32 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 33 | IdentityManagementBaseFixture::~IdentityManagementBaseFixture() |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 34 | { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 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 |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 42 | IdentityManagementBaseFixture::saveCertToFile(const Data& obj, const std::string& filename) |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 43 | { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 44 | m_certFiles.insert(filename); |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 45 | try { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 46 | io::save(obj, filename); |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 47 | return true; |
| 48 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 49 | catch (const io::Error&) { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 54 | IdentityManagementV2Fixture::IdentityManagementV2Fixture() |
| 55 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | security::Identity |
| 60 | IdentityManagementV2Fixture::addIdentity(const Name& identityName, const KeyParams& params) |
| 61 | { |
| 62 | auto identity = m_keyChain.createIdentity(identityName, params); |
| 63 | m_identities.insert(identityName); |
| 64 | return identity; |
| 65 | } |
| 66 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 67 | bool |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 68 | IdentityManagementV2Fixture::saveIdentityCertificate(const security::Identity& identity, |
| 69 | const std::string& filename) |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 70 | { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 71 | try { |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 72 | auto cert = identity.getDefaultKey().getDefaultCertificate(); |
| 73 | return saveCertToFile(cert, filename); |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 74 | } |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 75 | catch (const security::Pib::Error&) { |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | |
Yumin Xia | 2c509c2 | 2017-02-09 14:37:36 -0800 | [diff] [blame] | 80 | security::Identity |
| 81 | IdentityManagementV2Fixture::addSubCertificate(const Name& subIdentityName, |
| 82 | const security::Identity& issuer, const KeyParams& params) |
| 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; |
| 91 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 92 | time::system_clock::now() + time::days(7300))); |
| 93 | |
| 94 | v2::AdditionalDescription description; |
| 95 | description.set("type", "sub-certificate"); |
| 96 | info.appendTypeSpecificTlv(description.wireEncode()); |
| 97 | |
| 98 | m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info)); |
| 99 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 100 | |
| 101 | return subIdentity; |
| 102 | } |
| 103 | |
| 104 | v2::Certificate |
| 105 | IdentityManagementV2Fixture::addCertificate(const security::Key& key, const std::string& issuer) |
| 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); |
| 116 | certificate.setFreshnessPeriod(time::hours(1)); |
| 117 | |
| 118 | // set content |
| 119 | certificate.setContent(key.getPublicKey().data(), key.getPublicKey().size()); |
| 120 | |
| 121 | // set signature-info |
| 122 | SignatureInfo info; |
| 123 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 124 | time::system_clock::now() + time::days(10))); |
| 125 | |
| 126 | m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info)); |
| 127 | return certificate; |
| 128 | } |
| 129 | |
| 130 | |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 131 | } // namespace tests |
| 132 | } // namespace ndns |
| 133 | } // namespace ndn |