Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
| 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 | |
| 37 | IdentityManagementFixture::IdentityManagementFixture() |
| 38 | : m_keyChain("sqlite3", "file") |
| 39 | { |
| 40 | m_keyChain.getDefaultCertificate(); // side effect: create a default cert if it doesn't exist |
| 41 | } |
| 42 | |
| 43 | IdentityManagementFixture::~IdentityManagementFixture() |
| 44 | { |
| 45 | for (const auto& id : m_identities) { |
| 46 | m_keyChain.deleteIdentity(id); |
| 47 | } |
| 48 | |
| 49 | boost::system::error_code ec; |
| 50 | for (const auto& certFile : m_certFiles) { |
| 51 | boost::filesystem::remove(certFile, ec); // ignore error |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | bool |
| 56 | IdentityManagementFixture::addIdentity(const Name& identity, const ndn::KeyParams& params) |
| 57 | { |
| 58 | try { |
| 59 | m_keyChain.createIdentity(identity, params); |
| 60 | m_identities.push_back(identity); |
| 61 | return true; |
| 62 | } |
| 63 | catch (std::runtime_error&) { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | bool |
| 69 | IdentityManagementFixture::saveIdentityCertificate(const Name& identity, |
| 70 | const std::string& filename, bool wantAdd) |
| 71 | { |
| 72 | shared_ptr<ndn::IdentityCertificate> cert; |
| 73 | try { |
| 74 | cert = m_keyChain.getCertificate(m_keyChain.getDefaultCertificateNameForIdentity(identity)); |
| 75 | } |
| 76 | catch (const ndn::SecPublicInfo::Error&) { |
| 77 | if (wantAdd && this->addIdentity(identity)) { |
| 78 | return this->saveIdentityCertificate(identity, filename, false); |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | m_certFiles.push_back(filename); |
| 84 | try { |
| 85 | ndn::io::save(*cert, filename); |
| 86 | return true; |
| 87 | } |
| 88 | catch (const ndn::io::Error&) { |
| 89 | return false; |
| 90 | } |
| 91 | } |
| 92 | |
Zhiyi Zhang | a41c573 | 2017-01-18 14:06:44 -0800 | [diff] [blame] | 93 | IdentityManagementV2Fixture::IdentityManagementV2Fixture() |
| 94 | : m_keyChain("pib-memory:", "tpm-memory:") |
| 95 | { |
| 96 | } |
| 97 | |
| 98 | security::Identity |
| 99 | IdentityManagementV2Fixture::addIdentity(const Name& identityName, const KeyParams& params) |
| 100 | { |
| 101 | auto identity = m_keyChain.createIdentity(identityName, params); |
| 102 | m_identities.insert(identityName); |
| 103 | return identity; |
| 104 | } |
| 105 | |
| 106 | bool |
| 107 | IdentityManagementV2Fixture::saveIdentityCertificate(const security::Identity& identity, |
| 108 | const std::string& filename) |
| 109 | { |
| 110 | try { |
| 111 | auto cert = identity.getDefaultKey().getDefaultCertificate(); |
| 112 | return saveCertToFile(cert, filename); |
| 113 | } |
| 114 | catch (const security::Pib::Error&) { |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | security::Identity |
| 120 | IdentityManagementV2Fixture::addSubCertificate(const Name& subIdentityName, |
| 121 | const security::Identity& issuer, const KeyParams& params) |
| 122 | { |
| 123 | auto subIdentity = addIdentity(subIdentityName, params); |
| 124 | |
| 125 | security::v2::Certificate request = subIdentity.getDefaultKey().getDefaultCertificate(); |
| 126 | |
| 127 | request.setName(request.getKeyName().append("parent").appendVersion()); |
| 128 | |
| 129 | SignatureInfo info; |
| 130 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 131 | time::system_clock::now() + time::days(7300))); |
| 132 | |
| 133 | security::v2::AdditionalDescription description; |
| 134 | description.set("type", "sub-certificate"); |
| 135 | info.appendTypeSpecificTlv(description.wireEncode()); |
| 136 | |
| 137 | m_keyChain.sign(request, signingByIdentity(issuer).setSignatureInfo(info)); |
| 138 | m_keyChain.setDefaultCertificate(subIdentity.getDefaultKey(), request); |
| 139 | |
| 140 | return subIdentity; |
| 141 | } |
| 142 | |
| 143 | security::v2::Certificate |
| 144 | IdentityManagementV2Fixture::addCertificate(const security::Key& key, const std::string& issuer) |
| 145 | { |
| 146 | Name certificateName = key.getName(); |
| 147 | certificateName |
| 148 | .append(issuer) |
| 149 | .appendVersion(); |
| 150 | security::v2::Certificate certificate; |
| 151 | certificate.setName(certificateName); |
| 152 | |
| 153 | // set metainfo |
| 154 | certificate.setContentType(tlv::ContentType_Key); |
| 155 | certificate.setFreshnessPeriod(time::hours(1)); |
| 156 | |
| 157 | // set content |
| 158 | certificate.setContent(key.getPublicKey().buf(), key.getPublicKey().size()); |
| 159 | |
| 160 | // set signature-info |
| 161 | SignatureInfo info; |
| 162 | info.setValidityPeriod(security::ValidityPeriod(time::system_clock::now(), |
| 163 | time::system_clock::now() + time::days(10))); |
| 164 | |
| 165 | m_keyChain.sign(certificate, signingByKey(key).setSignatureInfo(info)); |
| 166 | return certificate; |
| 167 | } |
| 168 | |
| 169 | bool |
| 170 | IdentityManagementV2Fixture::saveCertToFile(const Data& obj, const std::string& filename) |
| 171 | { |
| 172 | m_certFiles.insert(filename); |
| 173 | try { |
| 174 | io::save(obj, filename); |
| 175 | return true; |
| 176 | } |
| 177 | catch (const io::Error&) { |
| 178 | return false; |
| 179 | } |
| 180 | } |
| 181 | |
Zhiyi Zhang | 8617a79 | 2017-01-17 16:45:56 -0800 | [diff] [blame] | 182 | } // namespace tests |
| 183 | } // namespace ndncert |
| 184 | } // namespace ndn |