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