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